A million particles (WebGPU)

glint · Examples · Tutorials · Gallery

A WGSL compute shader moves a million particles through a swirling flow every frame; hold the mouse to pull them in, and the bass pushes them out. A GLSL pass adds trails. Needs WebGPU (Chrome, Edge, Safari 26).

A million particles (WebGPU): a 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.

Particles: WGSL module

// WebGPU: compute shaders move every particle, then each is drawn as a little quad.
// From glint: glint.time, timeDelta, frame, count, resolution, mouse, mouseDown, spectrum, beat,
// beatPhase, bpm, loopPhase; params.<name> for your parameters; particles[i] (the Particle struct).
// Particle count and draw mode are in Pass settings (Instances). init runs at the start and on rewind.
// The sliders are params.speed, params.scale, params.size and params.brightness.


struct Particle {
    pos: vec2f,
    vel: vec2f,
    life: f32,
}

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 field(p: vec2f, t: f32) -> f32 {
    return sin(p.x * 2.1 + t) * cos(p.y * 1.7 - t * 0.7) + 0.5 * sin(p.x * 4.3 - p.y * 3.1 + t * 1.3);
}

fn gradient(p: vec2f, t: f32) -> vec2f {
    let e = 0.01;
    return vec2f(field(p + vec2f(e, 0.0), t) - field(p - vec2f(e, 0.0), t), field(p + vec2f(0.0, e), t) - field(p - vec2f(0.0, e), t)) / (2.0 * e);
}

// A swirling flow: the curl of some moving sine waves, which runs along the field's contour lines, plus a pull
// towards its zero line, so the particles gather into long flowing filaments.
fn flow(p: vec2f, t: f32) -> vec2f {
    let g = gradient(p, t);
    return vec2f(g.y, -g.x) - field(p, t) * g * 1.5;
}

@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 * 3u), hash(i * 3u + 1u)) * 2.0 - 1.0;
    particles[i].vel = vec2f(0.0);
    particles[i].life = hash(i * 3u + 2u);
}

@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.05);
    let aspect = glint.resolution.x / glint.resolution.y;
    var force = flow(p.pos * params.scale, glint.time * 0.2) * params.speed;
    // Hold the mouse button to pull them in.
    let m = (glint.mouse * 2.0 - 1.0) * vec2f(aspect, 1.0);
    let toMouse = m - p.pos * vec2f(aspect, 1.0);
    force += glint.mouseDown * normalize(toMouse) * 3.0 / (1.0 + dot(toMouse, toMouse) * 4.0);
    // Bass kicks them outwards.
    force += normalize(p.pos + 1e-4) * glint.spectrum.x * 2.0;
    p.vel = mix(p.vel, force, 1.0 - exp(-4.0 * dt));
    p.pos += p.vel * dt;
    p.life -= dt * 0.1;
    // Old or off screen: start again somewhere random.
    if (p.life < 0.0 || any(abs(p.pos) > vec2f(1.2))) {
        let s = i * 7u + glint.frame * 13u;
        p.pos = vec2f(hash(s), hash(s + 1u)) * 2.0 - 1.0;
        p.vel = vec2f(0.0);
        p.life = 1.0;
    }
    particles[i] = p;
}

struct VOut {
    @builtin(position) pos: vec4f,
    @location(0) corner: vec2f,
    @location(1) color: vec3f,
}

@vertex
fn vs(@builtin(vertex_index) v: u32, @builtin(instance_index) i: u32) -> VOut {
    var corners = array(vec2f(-1.0, -1.0), vec2f(1.0, -1.0), vec2f(-1.0, 1.0), vec2f(-1.0, 1.0), vec2f(1.0, -1.0), vec2f(1.0, 1.0));
    let p = particles[i];
    let c = corners[v];
    // Size in pixels, whatever the resolution.
    let size = params.size / glint.resolution;
    var o: VOut;
    o.pos = vec4f(p.pos + c * size, 0.0, 1.0);
    o.corner = c;
    let speed = length(p.vel);
    o.color = mix(vec3f(0.1, 0.35, 1.0), vec3f(1.0, 0.55, 0.2), clamp(speed * 1.5, 0.0, 1.0)) * smoothstep(0.0, 0.2, p.life) * smoothstep(1.0, 0.9, p.life);
    return o;
}

@fragment
fn fs(v: VOut) -> @location(0) vec4f {
    // A soft dot; the pass adds them up (Blend: add).
    let a = smoothstep(1.0, 0.0, length(v.corner));
    return vec4f(v.color * a * params.brightness, 1.0);
}

Trails: fragment shader

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform sampler2D feedback;
uniform sampler2D pass0;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    vec3 col = texture(feedback, uv).rgb * 0.82 + texture(pass0, uv).rgb;
    fragColor = vec4(col, 1.0);
}

More WebGPU examples

Galaxy (WebGPU) · Murmuration (WebGPU) · Galaxy collision (WebGPU) · Lorenz cloud (WebGPU)