Neon rain

glint · Examples · Tutorials · Gallery

A city street at night in the rain: neon signs flickering on the kick, their glow in the wet air, and the road mirroring it all, puddles sharp and the rest blurred, rings where the drops land.

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

Neon rain: 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.

Scene: fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform vec3 spectrum;
uniform float beat;
uniform float beatTime;
uniform int beatCount;
uniform sampler2D fft;
uniform float rain;

in vec2 v_texcoord;
out vec4 fragColor;

// 1 on each kick, fading; without audio, a steady pulse twice a second.
float kick() { return beatCount > 0 ? beat : pow(1.0 - fract(time * 2.0), 4.0); }
// Seconds since the last kick, and which kick it was (the same fallback).
float sinceKick() { return beatCount > 0 ? beatTime : fract(time * 2.0) / 2.0; }
float kickId() { return beatCount > 0 ? float(beatCount) : floor(time * 2.0); }
// One FFT bin, 0..1 across the spectrum (bass on the left).
float bin(float x) { return texture(fft, vec2(x, 0.25)).r; }
// Is anything playing? (Examples fake their input when not.)
bool silent() { return spectrum.x + spectrum.y + spectrum.z < 0.0001; }

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

float hash13(vec3 p) {
    uvec3 q = uvec3(ivec3(floor(p)) + 1000000) * uvec3(1597334673u, 3812015801u, 2798796415u);
    uint n = (q.x ^ q.y ^ q.z) * 1597334673u;
    return float(n >> 8) / 16777216.0;
}
float hash12(vec2 p) {
    uvec2 q = uvec2(ivec2(floor(p)) + 1000000) * uvec2(1597334673u, 3812015801u);
    uint n = (q.x ^ q.y) * 1597334673u;
    return float(n >> 8) / 16777216.0;
}
float noise3(vec3 p) {
    vec3 i = floor(p), f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(mix(hash13(i), hash13(i + vec3(1.0, 0.0, 0.0)), f.x), mix(hash13(i + vec3(0.0, 1.0, 0.0)), hash13(i + vec3(1.0, 1.0, 0.0)), f.x), f.y),
               mix(mix(hash13(i + vec3(0.0, 0.0, 1.0)), hash13(i + vec3(1.0, 0.0, 1.0)), f.x), mix(hash13(i + vec3(0.0, 1.0, 1.0)), hash13(i + vec3(1.0, 1.0, 1.0)), f.x), f.y), f.z);
}
float noise2(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash12(i), hash12(i + vec2(1.0, 0.0)), f.x), mix(hash12(i + vec2(0.0, 1.0)), hash12(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm3(vec3 p, int oct) {
    float s = 0.0, a = 0.5;
    for (int i = 0; i < 8; i++) {
        if (i >= oct) break;
        s += a * noise3(p);
        p = p * 2.02 + vec3(13.1, 7.7, 3.3);
        a *= 0.5;
    }
    return s;
}
float fbm2(vec2 p, int oct) {
    float s = 0.0, a = 0.5;
    mat2 m = mat2(1.6, 1.2, -1.2, 1.6);
    for (int i = 0; i < 10; i++) {
        if (i >= oct) break;
        s += a * noise2(p);
        p = m * p;
        a *= 0.5;
    }
    return s;
}
vec3 look(vec3 ro, vec3 ta, vec2 uv, float zoom) {
    vec3 f = normalize(ta - ro), r = normalize(cross(vec3(0.0, 1.0, 0.0), f)), u = cross(f, r);
    return normalize(uv.x * r + uv.y * u + zoom * f);
}
// Henyey-Greenstein: how much light scatters towards the eye (g > 0: mostly forwards).
float hg(float c, float g) { float g2 = g * g; return (1.0 - g2) / (12.566 * pow(1.0 + g2 - 2.0 * g * c, 1.5)); }
// A starfield on directions: three layers of jittered points, warm to cool.
vec3 starfield(vec3 d) {
    vec3 col = vec3(0.0);
    for (int l = 0; l < 3; l++) {
        float sc = 110.0 + float(l) * 160.0;
        vec3 q = d * sc;
        vec3 id = floor(q);
        float h = hash13(id + float(l) * 37.0);
        if (h > 0.982) {
            vec3 j = vec3(hash13(id + 1.3), hash13(id + 2.7), hash13(id + 5.1)) - 0.5;
            float dd = length(fract(q) - 0.5 - j * 0.6);
            col += mix(vec3(1.0, 0.8, 0.6), vec3(0.6, 0.75, 1.0), hash13(id + 9.0)) * exp(-dd * dd * 60.0) * (h - 0.982) * 90.0;
        }
    }
    return col;
}

float bass, kk;

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); }

const float CELL = 7.0;

vec3 signColour(float h) { return 0.6 + 0.4 * cos(6.2832 * (h + vec3(0.0, 0.33, 0.67))) * vec3(1.0, 0.8, 1.0) + vec3(0.3, 0.0, 0.2) * step(0.5, h); }

// x: distance; y: 1 building, 2 road, 3 sign, 4 pavement; z: the sign's colour.
vec3 map(vec3 p) {
    vec3 res = vec3(p.y, 2.0, 0.0);
    float pave = sdBox(vec3(abs(p.x) - 5.0, p.y - 0.06, 0.0), vec3(1.4, 0.12, 1e4));
    if (pave < res.x) res = vec3(pave, 4.0, 0.0);
    float side = sign(p.x);
    float k0 = floor(p.z / CELL);
    for (int j = -1; j <= 1; j++) {
        float k = k0 + float(j);
        float id = k * 2.0 + side;
        float h = 9.0 + 26.0 * hash(id * 3.1);
        float back = 0.6 * hash(id * 7.3);
        vec3 c = vec3(side * (10.4 + back), h * 0.5, (k + 0.5) * CELL);
        float b = sdBox(p - c, vec3(4.0, h * 0.5, CELL * 0.5 - 0.35));
        if (b < res.x) res = vec3(b, 1.0, 0.0);
        // A vertical sign sticking out from each facade, and a strip over the shop window.
        vec3 sc = vec3(side * (6.25 + back), 5.5 + 2.0 * hash(id * 1.9), (k + 0.3 + 0.4 * hash(id * 2.3)) * CELL);
        float s = sdBox(p - sc, vec3(0.06, 1.6 + 0.8 * hash(id * 4.1), 0.45)) - 0.03;
        float strip = sdBox(p - vec3(side * (6.35 + back), 3.1, (k + 0.5) * CELL), vec3(0.04, 0.05, CELL * 0.35));
        float sg = min(s, strip);
        if (sg < res.x) res = vec3(sg, 3.0, hash(id * 5.7 + (s < strip ? 0.0 : 0.5)));
    }
    return res;
}

// The signs on their own (for their glow in the rain and on the street).
vec2 signs(vec3 p) {
    float side = sign(p.x);
    float k0 = floor(p.z / CELL);
    vec2 best = vec2(1e9, 0.0);
    for (int j = -1; j <= 1; j++) {
        float k = k0 + float(j);
        float id = k * 2.0 + side;
        float back = 0.6 * hash(id * 7.3);
        vec3 sc = vec3(side * (6.25 + back), 5.5 + 2.0 * hash(id * 1.9), (k + 0.3 + 0.4 * hash(id * 2.3)) * CELL);
        float s = sdBox(p - sc, vec3(0.06, 1.6 + 0.8 * hash(id * 4.1), 0.45));
        float strip = sdBox(p - vec3(side * (6.35 + back), 3.1, (k + 0.5) * CELL), vec3(0.04, 0.05, CELL * 0.35));
        if (s < best.x) best = vec2(s, hash(id * 5.7));
        if (strip < best.x) best = vec2(strip, hash(id * 5.7 + 0.5));
    }
    return best;
}

// Signs flicker now and then, more on the kick.
float lit(float h) { return 0.35 + 0.65 * step(0.12 + 0.25 * kk, hash(h * 91.0 + floor(time * 9.0) * 0.37)); }

vec3 normal(vec3 p) {
    vec2 e = vec2(0.003, 0.0);
    return normalize(vec3(map(p + e.xyy).x - map(p - e.xyy).x, map(p + e.yxy).x - map(p - e.yxy).x, map(p + e.yyx).x - map(p - e.yyx).x));
}

vec3 march(vec3 ro, vec3 rd, int steps, out float t, out vec3 glowAcc) {
    t = 0.02;
    glowAcc = vec3(0.0);
    vec3 h = vec3(0.0);
    for (int i = 0; i < 200; i++) {
        if (i >= steps) break;
        vec3 p = ro + rd * t;
        h = map(p);
        vec2 s = signs(p);
        glowAcc += signColour(s.y) * lit(s.y) * exp(-s.x * 2.5) * max(h.x, 0.05) * 0.05;
        if (h.x < 0.0008 * t) return h;
        t += h.x * 0.9;
        if (t > 70.0) break;
    }
    return vec3(t, 0.0, 0.0);
}

// What a surface looks like, without reflections.
vec3 shade(vec3 p, vec3 n, vec3 rd, vec3 h) {
    if (h.y > 2.5 && h.y < 3.5) return signColour(h.z) * 4.0 * lit(h.z) * (1.0 + bass);
    vec2 s = signs(p + n * 0.1);
    vec3 neon = signColour(s.y) * lit(s.y) * 2.0 / (1.0 + s.x * s.x * 0.6);
    vec3 alb = h.y < 1.5 ? vec3(0.05, 0.05, 0.06) : h.y > 3.5 ? vec3(0.09) : vec3(0.03, 0.03, 0.035);
    vec3 col = alb * (neon + vec3(0.04, 0.05, 0.08));
    if (h.y < 1.5 && abs(n.x) > 0.5) {
        // Windows: a grid, some lit (warm or cold), on the street side of each building.
        vec2 w = vec2(p.z, p.y) * vec2(1.1, 0.8);
        vec2 id = floor(w), f = fract(w);
        float frame = step(0.15, f.x) * step(f.x, 0.85) * step(0.2, f.y) * step(f.y, 0.8) * step(4.5, p.y);
        float on = step(0.55, hash(id + floor(p.x)));
        col += frame * on * mix(vec3(1.0, 0.7, 0.4), vec3(0.6, 0.8, 1.0), hash(id * 1.7)) * 0.6;
    }
    return col;
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    bass = silent() ? 0.2 + 0.2 * kick() : spectrum.x;
    kk = kick();
    float z = time * 1.4;
    vec3 ro = vec3(sin(time * 0.1) * 1.2, 1.7 + 0.04 * sin(time * 5.6), z);
    vec3 rd = look(ro, vec3(sin(time * 0.1 + 0.6) * 1.0, 2.4, z + 8.0), uv, 1.25);
    float t;
    vec3 haze;
    vec3 h = march(ro, rd, 200, t, haze);
    vec3 col = vec3(0.01, 0.012, 0.02) + vec3(0.06, 0.03, 0.08) * max(rd.y, 0.0);
    if (h.y > 0.5) {
        vec3 p = ro + rd * t;
        vec3 n = normal(p);
        col = shade(p, n, rd, h);
        if (h.y > 1.5 && h.y < 2.5 || h.y > 3.5) {
            // Wet ground: puddles are mirrors, the rest a blurry sheen, rings where drops land.
            float puddle = smoothstep(0.45, 0.6, fbm2(p.xz * 0.35, 4));
            vec2 g = p.xz * 2.0;
            vec2 id = floor(g);
            float ph = fract(time * 1.3 + hash(id));
            float ring = sin(length(fract(g) - 0.5 - (vec2(hash(id + 1.0), hash(id + 2.0)) - 0.5) * 0.5) * 40.0 - ph * 30.0) * exp(-ph * 4.0) * rain;
            vec3 rn = normalize(n + vec3(ring * 0.05, 0.0, ring * 0.05) + (vec3(noise2(p.xz * 30.0), 0.0, noise2(p.zx * 30.0)) - 0.5) * (1.0 - puddle) * 0.25);
            vec3 rr = reflect(rd, rn);
            float rt;
            vec3 rhaze;
            vec3 rh = march(p + n * 0.02, rr, 110, rt, rhaze);
            vec3 rc = vec3(0.02, 0.02, 0.04);
            if (rh.y > 0.5) {
                vec3 rp = p + n * 0.02 + rr * rt;
                rc = shade(rp, normal(rp), rr, rh);
            }
            float fres = 0.04 + 0.96 * pow(1.0 - max(dot(rn, -rd), 0.0), 5.0);
            col += (rc + rhaze) * mix(fres * 0.6, 0.85, puddle);
        }
        col = mix(col, vec3(0.03, 0.025, 0.05), 1.0 - exp(-t * 0.03));
    }
    col += haze * (1.0 + 0.5 * rain);
    // Rain in front of everything, lit by the signs.
    for (int l = 0; l < 3; l++) {
        float s = 1.0 + float(l) * 0.6;
        vec2 g = (uv + vec2(uv.y * 0.08, 0.0)) * vec2(90.0, 2.5) * s + vec2(0.0, time * 7.0 * s);
        vec2 id = floor(g), f = fract(g);
        float drop = step(1.0 - 0.06 * rain, hash(id + float(l) * 13.0)) * smoothstep(0.1, 0.0, abs(f.x - 0.5)) * f.y;
        col += (vec3(0.25, 0.3, 0.4) + haze * 2.0) * drop * 0.2;
    }
    fragColor = vec4(col, min(t / 60.0, 1.0));
}

Bloom: fragment shader

#version 300 es
precision highp float;

uniform sampler2D pass0;
uniform vec2 resolution;

in vec2 v_texcoord;
out vec4 fragColor;

// Glow: only what's brighter than white, gathered on a golden-angle spiral at two sizes (a tight core, a wide haze).
void main() {
    vec2 st = gl_FragCoord.xy / resolution;
    vec2 aspect = vec2(resolution.y / resolution.x, 1.0);
    vec3 tight = vec3(0.0), wide = vec3(0.0);
    for (int i = 0; i < 64; i++) {
        float fi = float(i) + 0.5;
        vec2 dir = vec2(cos(fi * 2.39996), sin(fi * 2.39996)) * sqrt(fi / 64.0) * aspect;
        tight += max(texture(pass0, st + dir * 0.03).rgb - 1.0, 0.0);
        wide += max(texture(pass0, st + dir * 0.12).rgb - 1.0, 0.0);
    }
    fragColor = vec4((tight * 0.6 + wide * 0.4) / 64.0, 1.0);
}

Lens: fragment shader

#version 300 es
precision highp float;

uniform sampler2D pass0;
uniform sampler2D pass1;
uniform vec2 resolution;
uniform float time;
uniform float exposure;
uniform float aperture;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

vec3 aces(vec3 x) { return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0); }

void main() {
    vec2 st = gl_FragCoord.xy / resolution;
    vec2 aspect = vec2(resolution.y / resolution.x, 1.0);
    // Cinema bars.
    float bars = abs(st.y - 0.5) * 2.0;
    if (bars > resolution.x / resolution.y / 2.39 * 1.0 && resolution.x > resolution.y) { fragColor = vec4(0.0, 0.0, 0.0, 1.0); return; }

    // Depth of field, focused on whatever's in the middle of the frame (it's usually the orb).
    float focus = texture(pass0, vec2(0.5)).a;
    float depth = texture(pass0, st).a;
    float blur = clamp(abs(depth - focus) / max(depth, 0.01) * 0.005 * aperture, 0.0, 0.008);
    // Chromatic aberration: red and blue land a little apart towards the edges.
    vec2 fromCentre = st - 0.5;
    float ca = 0.004 * dot(fromCentre, fromCentre) * 4.0;
    vec3 col = vec3(0.0);
    float total = 0.0;
    for (int i = 0; i < 48; i++) {
        float fi = float(i) + 0.5;
        vec2 o = vec2(cos(fi * 2.39996), sin(fi * 2.39996)) * sqrt(fi / 48.0) * blur * aspect * 2.0;
        // Don't let sharp foreground bleed into the blur behind it: weight by the sample's own blur.
        vec4 c = texture(pass0, st + o);
        float w = clamp(abs(c.a - focus) / max(c.a, 0.01) * 0.005 * aperture / max(blur, 0.0005), 0.2, 1.0);
        col += vec3(texture(pass0, st + o + fromCentre * ca).r, c.g, texture(pass0, st + o - fromCentre * ca).b) * w;
        total += w;
    }
    col /= total;

    // Bloom, a horizontal streak (anamorphic lens), and ghosts mirrored through the middle (lens flare).
    vec3 bloom = texture(pass1, st).rgb;
    vec3 streak = vec3(0.0);
    for (int i = -12; i <= 12; i++) streak += texture(pass1, st + vec2(float(i) * 0.02, 0.0)).rgb * exp(-abs(float(i)) * 0.2);
    vec3 ghosts = vec3(0.0);
    for (int k = 1; k <= 3; k++) ghosts += texture(pass1, 0.5 - fromCentre * (0.5 + float(k) * 0.35)).rgb * pal(float(k) * 0.3) * 0.25;
    col += bloom * 0.7 + streak * vec3(0.3, 0.5, 1.0) * 0.06 + ghosts * 0.6;

    col = aces(col * exposure);
    col = pow(col, vec3(0.4545));
    // Grade: more contrast and colour, cool shadows and warm highlights (the film look).
    float luma = dot(col, vec3(0.2126, 0.7152, 0.0722));
    col = mix(vec3(luma), col, 1.25);
    col = mix(col, col * col * (3.0 - 2.0 * col), 0.6);
    col *= mix(vec3(0.85, 1.0, 1.1), vec3(1.1, 1.0, 0.85), smoothstep(0.1, 0.7, luma));
    col *= 1.0 - 0.35 * dot(fromCentre, fromCentre) * 2.5;                               // vignette
    col += (hash(gl_FragCoord.xy + fract(time) * 713.0) - 0.5) * 0.035;               // film grain
    fragColor = vec4(col, 1.0);
}

More Showpieces examples

Event horizon · Storm sea · Crystal caves · Nebula · Aurora lake · Golden dunes · Sunken temple · Ringed world