Galaxy (WebGPU)

glint · Examples · Tutorials · Gallery

Half a million stars, each on its own tilted ellipse: turning the orbits a little more further out is all it takes to make spiral arms. WGSL compute moves them; a GLSL pass adds the glow. Needs WebGPU.

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

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

// Half a million stars on orbits around a heavy core, each drawn as a single point.
// Orbits are ellipses turned a little more the further out they are: that's what makes the spiral arms
// (the "density wave" picture). update only moves each star along its own orbit.

struct Star {
    radius: f32,     // orbit size
    angle: f32,      // where it is on its orbit
    tilt: f32,       // extra turn of this orbit, for the arms
    height: f32,     // above/below the disc
    temp: f32,       // colour: 0 red dwarf .. 1 blue giant
}

alias Particle = Star;

fn hash(n: u32) -> f32 {
    var x = n * 747796405u + 2891336453u;
    x = ((x >> ((x >> 28u) + 4u)) ^ x) * 277803737u;
    return f32((x >> 22u) ^ x) / 4294967295.0;
}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    let r = pow(hash(i * 5u), 1.6) * 0.95 + 0.02;
    particles[i].radius = r;
    particles[i].angle = hash(i * 5u + 1u) * 6.2831853;
    particles[i].tilt = r * params.arms;
    particles[i].height = (hash(i * 5u + 2u) - 0.5) * 0.06 * (1.0 - r);
    particles[i].temp = hash(i * 5u + 3u);
}

@compute @workgroup_size(64)
fn update(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    // Inner stars go round faster (roughly Keplerian near the core, flatter further out).
    let r = particles[i].radius;
    particles[i].angle += glint.timeDelta * params.speed * 0.25 / (r + 0.08);
    particles[i].tilt = r * params.arms;
}

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

@vertex
fn vs(@builtin(instance_index) i: u32) -> VOut {
    let s = particles[i];
    // An ellipse, turned by the orbit's tilt.
    let e = vec2f(cos(s.angle), sin(s.angle) * 0.62) * s.radius;
    let c = cos(s.tilt);
    let sn = sin(s.tilt);
    let flat = vec2f(e.x * c - e.y * sn, e.x * sn + e.y * c);
    // Look at the disc from above at an angle.
    let view = vec2f(flat.x, flat.y * 0.55 + s.height);
    let aspect = glint.resolution.x / glint.resolution.y;
    var o: VOut;
    o.pos = vec4f(view.x / aspect * 1.6, view.y * 1.6, 0.0, 1.0);
    let hot = mix(vec3f(1.0, 0.45, 0.25), vec3f(0.55, 0.7, 1.0), s.temp);
    let core = exp(-s.radius * 9.0) * vec3f(1.0, 0.8, 0.5);
    o.color = (hot + core) * params.brightness * (1.0 + glint.beat * 1.5);
    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;
    vec3 col = texture(pass0, uv).rgb;
    // A cheap glow: a few wider rings of samples, fainter as they get further.
    vec3 glow = vec3(0.0);
    for (int ring = 1; ring <= 3; ring++) {
        float r = float(ring * ring) * 2.5 / resolution.y;
        for (int k = 0; k < 8; k++) {
            float a = float(k) * 0.785398 + float(ring);
            glow += texture(pass0, uv + vec2(cos(a), sin(a)) * r * vec2(resolution.y / resolution.x, 1.0)).rgb / float(ring * 8);
        }
    }
    col += glow * 0.6;
    fragColor = vec4(1.0 - exp(-col * 1.2), 1.0);
}

More WebGPU examples

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