Shape shifter (WebGPU)

glint · Examples · Tutorials · Gallery

300,000 particles flowing from a sphere to a torus, a cube, a double helix and a heart and round again, swirling on the way and bursting on the beat. Each knows its own spot on every shape. Needs WebGPU (Chrome, Edge, Safari 26).

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

Shape shifter (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.

Particles: WGSL module

// 300,000 particles flowing from shape to shape (sphere, torus, cube, double helix, heart) every few seconds: each
// knows its own spot on every shape and springs towards it, with a swirl on the way.
struct Particle {
    pos: vec3f,
    seed: f32,
    vel: vec3f,
    pad: 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);
}

fn goalOf(i: u32, shape: u32) -> vec3f {
    let u = hash(i * 2u);
    let v = hash(i * 2u + 1u);
    let a = u * 6.2832;
    switch shape {
        case 0u: {
            let z = v * 2.0 - 1.0;
            let r = sqrt(1.0 - z * z);
            return vec3f(cos(a) * r, z, sin(a) * r) * 0.8;
        }
        case 1u: {
            let b = v * 6.2832;
            return vec3f((0.65 + 0.25 * cos(b)) * cos(a), 0.25 * sin(b), (0.65 + 0.25 * cos(b)) * sin(a));
        }
        case 2u: {
            let face = i % 6u;
            let s = vec2f(u, v) * 2.0 - 1.0;
            var q = vec3f(s, 1.0);
            if (face == 1u) { q = vec3f(s, -1.0); }
            if (face == 2u) { q = vec3f(1.0, s); }
            if (face == 3u) { q = vec3f(-1.0, s); }
            if (face == 4u) { q = vec3f(s.x, 1.0, s.y); }
            if (face == 5u) { q = vec3f(s.x, -1.0, s.y); }
            return q * 0.55;
        }
        case 3u: {
            let side = select(0.0, 3.14159, i % 2u == 0u);
            let y = v * 2.0 - 1.0;
            let t = y * 9.0 + side;
            let rung = select(1.0, u, i % 7u == 0u);
            return vec3f(cos(t) * 0.35 * rung, y * 0.9, sin(t) * 0.35 * rung);
        }
        default: {
            // A heart: the classic curve, thickened into a shell.
            let t = a;
            let x = 16.0 * pow(sin(t), 3.0);
            let y = 13.0 * cos(t) - 5.0 * cos(2.0 * t) - 2.0 * cos(3.0 * t) - cos(4.0 * t);
            let k = sqrt(v);
            return vec3f(x * k, y * k + 2.0, (hash(i * 5u) - 0.5) * 10.0 * (1.0 - k * 0.6)) * 0.045;
        }
    }
}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    particles[i].pos = goalOf(i, u32(glint.time / params.hold) % 5u);
    particles[i].vel = vec3f(0.0);
}

@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);
    let shape = u32(glint.time / params.hold) % 5u;
    let goal = goalOf(i, shape);
    let d = goal - p.pos;
    // A spring towards its spot, a swirl round the vertical, and a kick on the beat.
    var acc = d * 9.0 + cross(vec3f(0.0, 1.0, 0.0), d) * 6.0;
    acc += normalize(p.pos + vec3f(1e-4)) * glint.beat * 3.0;
    p.vel = (p.vel + acc * dt) * exp(-3.5 * dt);
    p.pos += p.vel * dt;
    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 c = view3(p.pos, glint.time * 0.3, 0.35, 3.0);
    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));
    var o: VOut;
    o.pos = vec4f(c.xy + corners[v] * 1.4 / glint.resolution * 3.0 / c.w, 0.0, 1.0);
    o.corner = corners[v];
    let speed = length(p.vel);
    let base = 0.55 + 0.45 * cos(6.2832 * (glint.time * 0.04 + p.pos.y * 0.3 + vec3f(0.0, 0.33, 0.67)));
    o.color = mix(base, vec3f(1.0, 0.95, 0.85), clamp(speed * 0.5, 0.0, 1.0)) * params.brightness * (2.6 - c.w * 0.6);
    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.6 + 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.3), 1.0);
}

More WebGPU examples

Clifford dust (WebGPU) · Fireworks (WebGPU) · Fountain (WebGPU) · Silk (WebGPU) · Curl smoke (WebGPU) · Field lines (WebGPU)