Galaxy collision (WebGPU)

glint · Examples · Tutorials · Gallery

Two disc galaxies falling through each other under real gravity: every star pulls on every other (4,096 x 4,096 each frame), and tides draw out long arms and bridges, as they do in the sky. Rewind to watch it again. Needs WebGPU (Chrome, Edge, Safari 26).

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

Galaxy collision (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.

Stars: WGSL module

// Two disc galaxies falling through each other: real gravity, every star pulled by every other (4096 x 4096),
// softened so close passes don't fling stars away. Tides pull out long arms and bridges.
struct Particle {
    pos: vec3f,
    mass: f32,
    vel: vec3f,
    galaxy: 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);
}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    let g = select(-1.0, 1.0, i % 2u == 0u);
    let centre = vec3f(g * 1.6, g * 0.25, 0.0);
    let bulk = vec3f(-g * 0.22, 0.0, g * 0.1);
    var p: Particle;
    p.galaxy = g;
    p.mass = 1.0 / f32(glint.count);
    if (i < 2u) {
        // The two cores: heavy.
        p.pos = centre;
        p.vel = bulk;
        p.mass = 0.35;
    } else {
        let r = 0.08 + pow(hash(i * 3u), 0.7) * 0.75;
        let a = hash(i * 3u + 1u) * 6.2832;
        var q = vec3f(cos(a) * r, (hash(i * 3u + 2u) - 0.5) * 0.03, sin(a) * r);
        // Each disc tilted its own way.
        if (g > 0.0) { q = vec3f(q.x, q.y * 0.8 + q.z * 0.6, -q.y * 0.6 + q.z * 0.8); }
        let up = select(vec3f(0.0, 1.0, 0.0), vec3f(0.0, 0.8, -0.6), g > 0.0);
        let speed = sqrt(0.35 / r) * 0.95;
        p.pos = centre + q;
        p.vel = bulk + normalize(cross(up, q)) * speed * g;
    }
    particles[i] = p;
}

@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) * params.speed * 0.4;
    var acc = vec3f(0.0);
    for (var j = 0u; j < glint.count; j++) {
        let o = particles[j];
        let d = o.pos - p.pos;
        let r2 = dot(d, d) + 0.004;
        acc += d * o.mass / (r2 * sqrt(r2));
    }
    p.vel += acc * dt;
    p.pos += p.vel * dt;
    // Started again every 70 s, before it all settles into one blob.
    particles[i] = p;
}

struct VOut {
    @builtin(position) pos: vec4f,
    @location(0) 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.05, 0.5, 2.8);
    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 = select(1.6, 6.0, i < 2u) / glint.resolution;
    var o: VOut;
    o.pos = vec4f(c.xy + corners[v] * size * 2.0, 0.0, 1.0);
    let hue = select(vec3f(1.0, 0.7, 0.45), vec3f(0.5, 0.7, 1.0), p.galaxy > 0.0);
    o.color = hue * params.brightness * (1.0 + glint.beat * 0.5) / max(c.w * 0.3, 0.3);
    return o;
}

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

Glow: fragment shader

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform sampler2D pass0;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    vec2 px = 1.0 / resolution;
    vec3 col = texture(pass0, 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(pass0, uv + vec2(cos(a), sin(a)) * r * px).rgb / float(ring * 8);
        }
    }
    col += halo * 0.5;
    fragColor = vec4(1.0 - exp(-col * 1.6), 1.0);
}

More WebGPU examples

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