Digital rain

glint · Examples · Tutorials · Gallery

Falling columns of glyphs with bright heads and fading trails. Each glyph is five by seven random dots, mirrored so it looks like a character.

Digital rain: a 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

One pass, in GLSL.

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;

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

void main() {
    float size = max(10.0, floor(resolution.y / 36.0));
    vec2 cell = floor(gl_FragCoord.xy / size), f = fract(gl_FragCoord.xy / size);
    float rows = ceil(resolution.y / size);
    // Each column has its own speed and start; the head wraps round with a gap.
    float speed = 5.0 + hash(cell.x) * 12.0;
    float head = mod(time * speed + hash(cell.x + 7.0) * 100.0, rows + 25.0);
    float behind = head - (rows - cell.y);
    float trail = behind < 0.0 ? 0.0 : exp(-behind * 0.12);
    // The glyph changes now and then.
    float glyph = floor(hash(cell) * 50.0 + time * (0.5 + 2.0 * hash(cell + 3.0)));
    // A 5x7 dot grid, mirrored left-right (glyphs look better symmetric-ish).
    vec2 g = (f - vec2(0.12, 0.08)) / vec2(0.76, 0.84) * vec2(5.0, 7.0);
    vec2 gi = floor(g), gf = fract(g);
    float on = 0.0;
    if (gi.x >= 0.0 && gi.x < 5.0 && gi.y >= 0.0 && gi.y < 7.0) {
        float x = min(gi.x, 4.0 - gi.x);
        on = step(0.5, hash(vec2(glyph, x * 7.0 + gi.y))) * smoothstep(0.5, 0.3, length(gf - 0.5));
    }
    vec3 col = vec3(0.1, 1.0, 0.35) * trail * on;
    col += vec3(0.8, 1.0, 0.85) * on * smoothstep(1.5, 0.0, behind) * step(0.0, behind);
    col += vec3(0.0, 0.08, 0.02) * trail;
    // Scanlines.
    col *= 0.85 + 0.15 * sin(gl_FragCoord.y * 3.14159);
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

DNA helix · Clock · Desert dunes · Apollonian · Bokeh · ASCII · Soap bubbles · Sierpinski tetrahedron