Fireworks (WebGPU)

glint · Examples · Tutorials · Gallery

A display of 24 shells, 2,000 sparks each: they climb, burst into spheres and rings, fall under gravity and drag, and crackle as they die. Flares on the beat; trails from a GLSL pass. Needs WebGPU (Chrome, Edge, Safari 26).

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

Fireworks (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

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

Sparks: WGSL module

// Shells of 2000 sparks each: every shell climbs, bursts, and its sparks fall under gravity and drag, crackling as
// they die. Each shell has its own clock; the beat makes them flare.
struct Particle {
    pos: vec2f,
    vel: vec2f,
    life: f32,
    cycle: 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 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);
}

const PER_SHELL = 2000u;

fn shellClock(s: u32) -> vec2f {
    let period = 3.5 + hash(s * 31u) * 2.5;
    let t = glint.time * params.pace + hash(s * 17u) * period;
    return vec2f(floor(t / period), fract(t / period) * period);
}

@compute @workgroup_size(64)
fn update(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    let s = i / PER_SHELL;
    let clock = shellClock(s);
    var p = particles[i];
    let rise = 1.1;
    let seed = s * 1000u + u32(clock.x) * 7919u;
    let burst = vec2f((hash(seed) - 0.5) * 1.6, 0.15 + hash(seed + 1u) * 0.5);
    p.cycle = clock.x;
    if (clock.y < rise) {
        // The shell climbing (a few sparks trailing it).
        let k = clock.y / rise;
        p.pos = mix(vec2f(burst.x, -1.0), burst, 1.0 - (1.0 - k) * (1.0 - k)) + (hash3(i).xy - 0.5) * 0.015;
        p.life = select(0.0, 0.5, i % 25u == 0u);
    } else {
        // After the burst, where a spark is follows from the time since (drag k, gravity g): the same at any frame
        // rate. A sphere of sparks seen from the side, some shells with a ring.
        let tb = clock.y - rise;
        let a = hash(i * 3u) * 6.2832;
        let ring = hash(seed + 5u) > 0.6;
        let r = select(pow(hash(i * 3u + 1u), 0.35), 1.0, ring && i % 3u == 0u);
        let v0 = vec2f(cos(a), sin(a)) * r * (0.55 + hash(seed + 2u) * 0.35);
        let kd = 1.6;
        let g = vec2f(0.0, -0.35);
        let e = exp(-kd * tb);
        p.pos = burst + v0 * (1.0 - e) / kd + g * (tb / kd - (1.0 - e) / (kd * kd));
        p.vel = v0 * e + g * (1.0 - e) / kd;
        p.life = 1.0 - tb * (0.32 + hash(i * 7u) * 0.25);
    }
    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 {
    let p = particles[i];
    let s = i / PER_SHELL;
    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 size = 2.2 / glint.resolution;
    let aspect = glint.resolution.x / glint.resolution.y;
    var o: VOut;
    o.pos = vec4f(p.pos.x / aspect * 1.4, p.pos.y, 0.0, 1.0) + vec4f(corners[v] * size, 0.0, 0.0);
    o.corner = corners[v];
    let hue = 0.55 + 0.45 * cos(6.2832 * (hash(s * 13u + u32(p.cycle) * 5u) + vec3f(0.0, 0.33, 0.67)));
    let life = max(p.life, 0.0);
    // Crackle: dying sparks flicker.
    let flick = select(1.0, step(0.5, hash(i + glint.frame * 31u)) * 2.0, life < 0.25);
    o.color = mix(vec3f(1.0, 0.9, 0.7), hue, smoothstep(0.95, 0.6, life)) * life * flick * params.brightness * (1.0 + glint.beat);
    return o;
}

@fragment
fn fs(v: VOut) -> @location(0) vec4f {
    let a = smoothstep(1.0, 0.0, length(v.corner));
    return vec4f(v.color * a, 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.9 + texture(pass0, uv).rgb;
    fragColor = vec4(col, 1.0);
}

Glow: fragment shader

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform sampler2D pass1;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    vec2 px = 1.0 / resolution;
    vec3 col = texture(pass1, uv).rgb;
    vec3 halo = vec3(0.0);
    for (int ring = 1; ring <= 3; ring++) {
        float r = float(ring * ring) * 2.0;
        for (int k = 0; k < 8; k++) {
            float a = float(k) * 0.785398 + float(ring) * 0.4;
            halo += texture(pass1, uv + vec2(cos(a), sin(a)) * r * px).rgb / float(ring * 8);
        }
    }
    col += halo * 0.5;
    fragColor = vec4(1.0 - exp(-col * 1.2), 1.0);
}

More WebGPU examples

Murmuration (WebGPU) · Galaxy collision (WebGPU) · Lorenz cloud (WebGPU) · Clifford dust (WebGPU) · Fountain (WebGPU) · Silk (WebGPU) · Shape shifter (WebGPU) · Curl smoke (WebGPU)