Neon city

glint · Examples · Tutorials · Gallery

Flying down an endless street of raymarched towers: domain repetition, per-building heights, lit windows, wet reflective road and bloom.

Audio-reactive: it moves with music from a microphone, line-in or a file.

Neon city: an audio-reactive GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

3 passes, drawn in this order each frame, in GLSL.

City: fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform float speed;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float sdBox(vec3 p, vec3 b) { vec3 q = abs(p) - b; return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); }

// Buildings on a grid of 4x4 lots, leaving a street along z at x = 0.
vec2 map(vec3 p) {
    vec2 cell = floor(p.xz / 4.0);
    vec3 q = p;
    q.xz = mod(q.xz, 4.0) - 2.0;
    float h = 2.0 + 8.0 * hash(cell) * hash(cell + 3.1);
    float street = abs(p.x) < 4.0 ? 1e9 : 0.0;
    float d = sdBox(q - vec3(0.0, h * 0.5, 0.0), vec3(1.4, h * 0.5, 1.4)) + street;
    return vec2(min(d, p.y), d < p.y ? hash(cell) : -1.0);
}

vec3 shade(vec3 ro, vec3 rd, out float tHit) {
    float t = 0.0;
    vec2 h = vec2(0.0);
    for (int i = 0; i < 120; i++) {
        h = map(ro + rd * t);
        if (h.x < 0.002 || t > 80.0) break;
        t += h.x * 0.9;
    }
    tHit = t;
    vec3 sky = mix(vec3(0.05, 0.02, 0.12), vec3(0.3, 0.05, 0.3), exp(-max(rd.y, 0.0) * 6.0));
    if (t > 80.0) return sky;
    vec3 p = ro + rd * t;
    if (h.y < 0.0) return vec3(0.01);   // road (reflections added by the caller)
    // Windows: a grid on the building faces, some lit, flickering with the music.
    vec2 w = abs(fract(p.y * 1.5) - 0.5) < 0.3 ? vec2(fract(p.x * 1.5 + p.z * 1.5), floor(p.y * 1.5)) : vec2(0.0);
    // Window edges soft over about a pixel (fwidth), so they don't stair-step when the picture is scaled.
    float wx = fract(w.x * 3.0);
    float win = smoothstep(0.3 - fwidth(wx), 0.3 + fwidth(wx), wx) * step(0.5, hash(floor(p.xz * 1.5) + w.y + h.y * 17.0));
    vec3 neon = 0.5 + 0.5 * cos(6.2831 * (h.y + vec3(0.0, 0.33, 0.67)));
    vec3 col = vec3(0.02, 0.02, 0.04) + win * neon * (0.8 + spectrum.y * 2.0);
    col = mix(col, sky, 1.0 - exp(-0.0006 * t * t));
    return col;
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float z = time * speed + spectrumIntegrated.x * 4.0;
    vec3 ro = vec3(sin(time * 0.3) * 1.5, 2.5 + sin(time * 0.2), z);
    float yaw = (mouse.x - 0.5) * 1.5;
    vec3 fw = normalize(vec3(sin(yaw), -0.05 + (mouse.y - 0.5) * 0.5, cos(yaw)));
    vec3 rt = normalize(cross(fw, vec3(0, 1, 0))), up = cross(rt, fw);
    vec3 rd = normalize(uv.x * rt + uv.y * up + 1.3 * fw);
    float t;
    vec3 col = shade(ro, rd, t);
    vec3 p = ro + rd * t;
    if (t < 80.0 && p.y < 0.01) {
        // Wet road: a blurred reflection of the city.
        vec3 rr = reflect(rd, vec3(0.0, 1.0, 0.0));
        rr.x += 0.02 * sin(p.z * 3.0);
        float t2;
        col += shade(p + vec3(0.0, 0.01, 0.0), rr, t2) * 0.45;
        col += vec3(1.0, 0.3, 0.6) * smoothstep(0.2, 0.0, abs(abs(p.x) - 3.0)) * 0.6;   // lane lines
    }
    col += beat * vec3(0.4, 0.1, 0.5) * 0.2;
    // Linear colour; depth in alpha for the Look pass's edge smoothing.
    fragColor = vec4(col, min(t, 80.0) / 80.0);
}

Bloom: fragment shader

#version 300 es
precision highp float;

uniform sampler2D pass0;
uniform vec2 resolution;

in vec2 v_texcoord;
out vec4 fragColor;

// The lit windows and lane lights, gathered on a golden-angle spiral: a soft glow.
void main() {
    vec2 st = gl_FragCoord.xy / resolution;
    vec2 aspect = vec2(resolution.y / resolution.x, 1.0);
    vec3 glow = vec3(0.0);
    for (int i = 0; i < 48; i++) {
        float fi = float(i) + 0.5;
        vec2 dir = vec2(cos(fi * 2.39996), sin(fi * 2.39996)) * sqrt(fi / 48.0) * aspect;
        glow += max(texture(pass0, st + dir * 0.05).rgb - 0.35, 0.0);
    }
    fragColor = vec4(glow / 48.0, 1.0);
}

Look: fragment shader

#version 300 es
precision highp float;

uniform sampler2D pass0;
uniform sampler2D pass1;
uniform vec2 resolution;
uniform float bloom;

in vec2 v_texcoord;
out vec4 fragColor;

// Smooths only the building edges (where depth jumps between neighbours: a rotated-grid average of four samples),
// adds the bloom, and applies the look.
void main() {
    vec2 st = gl_FragCoord.xy / resolution;
    vec2 px = 1.0 / resolution;
    vec4 c = texture(pass0, st);
    vec4 a = texture(pass0, st + vec2(0.4, 0.2) * px), b = texture(pass0, st + vec2(-0.2, 0.4) * px);
    vec4 d = texture(pass0, st + vec2(-0.4, -0.2) * px), e = texture(pass0, st + vec2(0.2, -0.4) * px);
    float edge = abs(a.a - c.a) + abs(b.a - c.a) + abs(d.a - c.a) + abs(e.a - c.a);
    vec3 col = mix(c.rgb, (c.rgb + a.rgb + b.rgb + d.rgb + e.rgb) * 0.2, smoothstep(0.0, 0.01, edge));
    col += texture(pass1, st).rgb * bloom;
    fragColor = vec4(pow(col, vec3(0.8)), 1.0);
}

More Showpieces examples

KIFS tunnel · Truchet weave · Voronoi crystals · Domain warping · Galaxy · Fluid · GPU particles · Physarum