Murmuration (WebGPU)

glint · Examples · Tutorials · Gallery

4,096 starlings at sunset, every one checking every other each frame (16 million checks): keep apart, fly together, drift to the middle, and swerve as one from the hawk. The bass startles them. Needs WebGPU (Chrome, Edge, Safari 26).

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

Murmuration (WebGPU): an audio-reactive WebGPU (WGSL) 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

2 passes, drawn in this order each frame, in WGSL and GLSL.

Birds: WGSL module

// Starlings: every bird looks at every other bird each frame (4096 x 4096 checks): keep apart from the very
// close, match the heading of the near, drift to the middle of the near, and turn away from the hawk.
struct Particle {
    pos: vec2f,
    vel: vec2f,
}

fn hash(n: u32) -> f32 {
    var x = n * 747796405u + 2891336453u;
    x = ((x >> ((x >> 28u) + 4u)) ^ x) * 277803737u;
    return f32((x >> 22u) ^ x) / 4294967295.0;
}
fn hash3(n: u32) -> vec3f { return vec3f(hash(n), hash(n + 1u), hash(n + 2u)); }
// A 3D view: turn about y, tilt about x, perspective; returns clip position and depth.
fn view3(p: vec3f, yaw: f32, pitch: f32, dist: f32) -> vec4f {
    let cy = cos(yaw);
    let sy = sin(yaw);
    var q = vec3f(p.x * cy - p.z * sy, p.y, p.x * sy + p.z * cy);
    let cp = cos(pitch);
    let sp = sin(pitch);
    q = vec3f(q.x, q.y * cp - q.z * sp, q.y * sp + q.z * cp);
    let z = q.z + dist;
    let aspect = glint.resolution.x / glint.resolution.y;
    return vec4f(q.x / z * 1.8 / aspect, q.y / z * 1.8, 0.0, z);
}

fn hawk() -> vec2f {
    let t = glint.time * 0.35;
    return vec2f(sin(t * 1.3) * 0.9, sin(t * 0.9 + 1.0) * 0.45);
}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    particles[i].pos = (vec2f(hash(i * 2u), hash(i * 2u + 1u)) - 0.5) * vec2f(1.2, 0.6);
    let a = hash(i * 7u) * 6.2832;
    particles[i].vel = vec2f(cos(a), sin(a)) * 0.2;
}

@compute @workgroup_size(64)
fn update(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    var p = particles[i];
    let dt = min(glint.timeDelta, 0.033);
    var sep = vec2f(0.0);
    var ali = vec2f(0.0);
    var coh = vec2f(0.0);
    var n = 0.0;
    let r = params.reach * 0.05;
    for (var j = 0u; j < glint.count; j++) {
        if (j == i) { continue; }
        let o = particles[j];
        let d = o.pos - p.pos;
        let d2 = dot(d, d);
        if (d2 < r * r) {
            n += 1.0;
            ali += o.vel;
            coh += d;
            if (d2 < r * r * 0.12) { sep -= d / (d2 + 0.00002); }
        }
    }
    var acc = vec2f(0.0);
    if (n > 0.0) {
        acc += (ali / n - p.vel) * 2.0;
        acc += coh / n * 6.0;
        acc += sep * 0.0006;
    }
    // Stay in the sky, flee the hawk, and the bass startles them.
    let aspect = glint.resolution.x / glint.resolution.y;
    acc -= p.pos * vec2f(1.0 / aspect, 1.6) * smoothstep(0.5, 1.0, length(p.pos * vec2f(1.0 / aspect, 1.6))) * 3.0;
    let fromHawk = p.pos - hawk();
    acc += normalize(fromHawk) * 2.5 / (1.0 + dot(fromHawk, fromHawk) * 60.0);
    acc += normalize(fromHawk) * glint.spectrum.x * 1.5;
    p.vel += acc * dt;
    let speed = length(p.vel);
    p.vel = p.vel / max(speed, 1e-5) * clamp(speed, 0.25, 0.6) * params.speed;
    p.pos += p.vel * dt;
    particles[i] = p;
}

struct VOut {
    @builtin(position) pos: vec4f,
    @location(0) shade: f32,
}

@vertex
fn vs(@builtin(vertex_index) v: u32, @builtin(instance_index) i: u32) -> VOut {
    let p = particles[i];
    let f = normalize(p.vel + vec2f(1e-5, 0.0));
    let s = vec2f(-f.y, f.x);
    // A little arrow of a bird, wings beating.
    let flap = sin(glint.time * 18.0 + f32(i)) * 0.4;
    var corners = array(vec2f(1.0, 0.0), vec2f(-0.6, 1.0 + flap), vec2f(-0.3, 0.0), vec2f(1.0, 0.0), vec2f(-0.3, 0.0), vec2f(-0.6, -1.0 - flap));
    let c = corners[v] * 0.006;
    let w = p.pos + f * c.x + s * c.y;
    let aspect = glint.resolution.x / glint.resolution.y;
    var o: VOut;
    o.pos = vec4f(w.x / aspect, w.y, 0.0, 1.0);
    o.shade = 1.0;
    return o;
}

@fragment
fn fs(v: VOut) -> @location(0) vec4f {
    return vec4f(v.shade * 0.6, v.shade * 0.6, v.shade * 0.6, 1.0);
}

Sky: fragment shader

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform float time;
uniform sampler2D pass0;

in vec2 v_texcoord;
out vec4 fragColor;

// A winter sunset, the birds dark against it where they're dense.
void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    vec3 sky = mix(vec3(1.0, 0.62, 0.35), vec3(0.35, 0.42, 0.62), smoothstep(0.0, 0.9, uv.y));
    sky = mix(sky, vec3(1.0, 0.85, 0.6), exp(-length((uv - vec2(0.7, 0.12)) * vec2(1.8, 3.0)) * 3.0) * 0.6);
    float birds = clamp(texture(pass0, uv).r * 1.6, 0.0, 1.0);
    vec3 col = mix(sky, vec3(0.06, 0.05, 0.07), birds);
    // Bare trees along the bottom.
    float x = uv.x * 18.0;
    float ridge = 0.06 + 0.03 * sin(x * 0.7) + 0.02 * sin(x * 2.3) + 0.04 * pow(abs(sin(x * 1.3 + 1.0)), 30.0);
    col = mix(col, vec3(0.05, 0.04, 0.06), smoothstep(ridge + 0.004, ridge, uv.y));
    fragColor = vec4(col, 1.0);
}

More WebGPU examples

A million particles (WebGPU) · Galaxy (WebGPU) · Galaxy collision (WebGPU) · Lorenz cloud (WebGPU) · Clifford dust (WebGPU) · Fireworks (WebGPU)