Curl smoke (WebGPU)

glint · Examples · Tutorials · Gallery

Smoke rising from a fire: 400,000 puffs carried by 3D curl noise (the swirl of a noise field: no clumps, no gaps, like real smoke), born glowing orange and cooling to grey as they climb. Needs WebGPU (Chrome, Edge, Safari 26).

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

Curl smoke (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.

Smoke: WGSL module

// Smoke from a fire: 400,000 particles carried up by curl noise (the swirl of a noise field, which never piles up or
// thins out: smoke-like), born hot and orange at the bottom, cooling to grey as they rise.
struct Particle {
    pos: vec3f,
    life: 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 h3(c: vec3i) -> f32 {
    let b = vec3u(c + vec3i(100000));
    return hash((b.x * 73856093u) ^ (b.y * 19349663u) ^ (b.z * 83492791u));
}

fn n3(p: vec3f) -> f32 {
    let i = vec3i(floor(p));
    let f = fract(p);
    let u = f * f * (3.0 - 2.0 * f);
    return mix(mix(mix(h3(i), h3(i + vec3i(1, 0, 0)), u.x), mix(h3(i + vec3i(0, 1, 0)), h3(i + vec3i(1, 1, 0)), u.x), u.y),
               mix(mix(h3(i + vec3i(0, 0, 1)), h3(i + vec3i(1, 0, 1)), u.x), mix(h3(i + vec3i(0, 1, 1)), h3(i + vec3i(1, 1, 1)), u.x), u.y), u.z);
}

fn potential(p: vec3f) -> vec3f {
    return vec3f(n3(p), n3(p + vec3f(31.4, 7.2, 3.9)), n3(p + vec3f(-12.7, 19.3, 41.1)));
}

fn curl(p: vec3f) -> vec3f {
    let e = 0.05;
    let dx = (potential(p + vec3f(e, 0.0, 0.0)) - potential(p - vec3f(e, 0.0, 0.0))) / (2.0 * e);
    let dy = (potential(p + vec3f(0.0, e, 0.0)) - potential(p - vec3f(0.0, e, 0.0))) / (2.0 * e);
    let dz = (potential(p + vec3f(0.0, 0.0, e)) - potential(p - vec3f(0.0, 0.0, e))) / (2.0 * e);
    return vec3f(dy.z - dz.y, dz.x - dx.z, dx.y - dy.x);
}

fn born(i: u32, salt: u32) -> vec3f {
    let h = hash3(i * 3u + salt);
    let a = h.x * 6.2832;
    let r = sqrt(h.y) * 0.18;
    return vec3f(cos(a) * r, -0.9, sin(a) * r);
}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    // The column already full: each starts partway up, as old as that makes it.
    let life = hash(i);
    particles[i].pos = born(i, 0u) + vec3f((hash(i * 7u) - 0.5) * 0.3 * (1.0 - life), (1.0 - life) * 1.6, (hash(i * 13u) - 0.5) * 0.3 * (1.0 - life));
    particles[i].life = life;
}

@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 q = p.pos * params.scale + vec3f(0.0, -glint.time * 0.3, 0.0);
    let v = curl(q) * 0.35 * params.swirl + vec3f(0.0, 0.55 + glint.spectrum.x * 0.5, 0.0);
    p.pos += v * dt;
    p.life -= dt * 0.28;
    if (p.life < 0.0 || p.pos.y > 1.2) {
        p.pos = born(i, glint.frame);
        p.life = 1.0;
    }
    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.1, 0.1, 2.6);
    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 age = 1.0 - p.life;
    // Puffs grow as they rise.
    let size = (2.0 + age * 10.0) / glint.resolution;
    var o: VOut;
    o.pos = vec4f(c.xy + corners[v] * size, 0.0, 1.0);
    o.corner = corners[v];
    let fire = vec3f(1.0, 0.45, 0.1) * 3.0;
    let ember = vec3f(1.0, 0.75, 0.4);
    let smoke = vec3f(0.35, 0.36, 0.4);
    var col = mix(fire, ember, smoothstep(0.0, 0.15, age));
    col = mix(col, smoke, smoothstep(0.15, 0.45, age));
    o.color = col * p.life * params.brightness / (1.0 + age * 8.0);
    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 * a, 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.5), 1.0);
}

More WebGPU examples

Fireworks (WebGPU) · Fountain (WebGPU) · Silk (WebGPU) · Shape shifter (WebGPU) · Field lines (WebGPU)