Lorenz cloud (WebGPU)

glint · Examples · Tutorials · Gallery

A million particles riding the Lorenz attractor, the butterfly of chaos: each follows the same three equations from a hair apart, and together they show its two wings in depth. Coloured by speed; rho changes its shape. Needs WebGPU (Chrome, Edge, Safari 26).

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

Lorenz cloud (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.

Cloud: WGSL module

// A million particles riding the Lorenz attractor (the butterfly of chaos): each follows the same three equations,
// starting a hair apart, and they spread over its two wings. Coloured by speed.
struct Particle {
    pos: vec3f,
    age: 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 lorenz(p: vec3f) -> vec3f {
    let s = 10.0;
    let r = params.rho;
    let b = 8.0 / 3.0;
    return vec3f(s * (p.y - p.x), p.x * (r - p.z) - p.y, p.x * p.y - b * p.z);
}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    // Already on the attractor: run each one a few hundred steps first.
    var q = vec3f(1.0, 1.0, 20.0) + (hash3(i * 3u) - 0.5) * 30.0;
    let steps = 300u + u32(hash(i * 11u) * 300.0);
    for (var k = 0u; k < steps; k++) { q += lorenz(q) * 0.006; }
    particles[i].pos = q;
    particles[i].age = hash(i * 9u) * 20.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 h = min(glint.timeDelta, 0.033) * params.speed * 0.25;
    // Runge-Kutta, two steps a frame.
    for (var k = 0; k < 2; k++) {
        let k1 = lorenz(p.pos);
        let k2 = lorenz(p.pos + k1 * h * 0.5);
        let k3 = lorenz(p.pos + k2 * h * 0.5);
        let k4 = lorenz(p.pos + k3 * h);
        p.pos += (k1 + 2.0 * k2 + 2.0 * k3 + k4) * h / 6.0;
    }
    p.age -= glint.timeDelta;
    if (p.age < 0.0) {
        p.pos = vec3f(1.0, 1.0, 20.0) + (hash3(i * 3u + glint.frame * 7u) - 0.5) * 30.0;
        p.age = 10.0 + hash(i + glint.frame) * 20.0;
    }
    particles[i] = p;
}

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

@vertex
fn vs(@builtin(instance_index) i: u32) -> VOut {
    let p = particles[i];
    // The wings lie in x-z: stand it up to face us.
    let c = view3(vec3f(p.pos.x, 25.0 - p.pos.z, p.pos.y) * 0.05, sin(glint.time * 0.1) * 0.6, 0.15, 3.0);
    var o: VOut;
    o.pos = vec4f(c.xy, 0.0, 1.0);
    let speed = length(lorenz(p.pos)) / 150.0;
    o.color = mix(vec3f(0.15, 0.4, 1.0), vec3f(1.0, 0.45, 0.15), clamp(speed, 0.0, 1.0)) * params.brightness * (1.0 + glint.beat);
    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.3), 1.0);
}

More WebGPU examples

A million particles (WebGPU) · Galaxy (WebGPU) · Murmuration (WebGPU) · Galaxy collision (WebGPU) · Clifford dust (WebGPU) · Fireworks (WebGPU) · Fountain (WebGPU) · Silk (WebGPU)