Fountain (WebGPU)

glint · Examples · Tutorials · Gallery

A garden fountain at night: water thrown up from a ring of jets and a tall centre jet, streaking as it falls, splashing back off the pool, reflected rippling in it. The jets pulse with the bass. Needs WebGPU (Chrome, Edge, Safari 26).

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

Fountain (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.

Water: WGSL module

// A fountain: water thrown up from a ring of jets and a tall centre jet, falling under gravity, splashing back up off
// the pool in smaller drops. The jets pulse with the bass.
struct Particle {
    pos: vec3f,
    life: f32,
    vel: vec3f,
    bounces: 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 launch(i: u32, salt: u32) -> Particle {
    var p: Particle;
    let jet = i % 9u;
    let h = hash3(i * 3u + salt);
    var dir: vec3f;
    if (jet == 0u) {
        p.pos = vec3f(0.0, 0.15, 0.0);
        dir = normalize(vec3f((h.x - 0.5) * 0.08, 1.0, (h.y - 0.5) * 0.08)) * (3.0 + glint.spectrum.x * 1.2) * params.power;
    } else {
        let a = f32(jet) * 0.785398 + glint.time * 0.2;
        p.pos = vec3f(cos(a) * 0.6, 0.1, sin(a) * 0.6);
        dir = normalize(vec3f(cos(a) * 0.35 + (h.x - 0.5) * 0.1, 1.0, sin(a) * 0.35 + (h.y - 0.5) * 0.1)) * (2.2 + glint.spectrum.x) * params.power;
    }
    p.vel = dir * (0.95 + h.z * 0.1);
    p.life = 1.0;
    p.bounces = 0.0;
    return p;
}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    var p = launch(i, 0u);
    p.life = -hash(i) * 2.5;   // not yet: start at different times
    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);
    if (p.life < 0.0) {
        p.life += dt;
        if (p.life >= 0.0) { p = launch(i, glint.frame); }
        particles[i] = p;
        return;
    }
    p.vel.y -= 4.0 * dt;
    p.vel *= exp(-0.15 * dt);
    p.pos += p.vel * dt;
    if (p.pos.y < 0.0) {
        if (p.bounces < 1.0 && hash(i + glint.frame) < 0.35) {
            // A splash: a smaller drop flung back up.
            let h = hash3(i * 5u + glint.frame);
            p.pos.y = 0.0;
            p.vel = vec3f((h.x - 0.5) * 1.2, 0.6 + h.y * 0.9, (h.z - 0.5) * 1.2);
            p.bounces = 1.0;
        } else {
            p = launch(i, glint.frame);
        }
    }
    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 - vec3f(0.0, 1.0, 0.0), glint.time * 0.08, 0.25, 4.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));
    let size = 2.0 / glint.resolution * select(1.0, 0.7, p.bounces > 0.0);
    var o: VOut;
    // Streaks: stretched along the screen-space velocity.
    let ahead = view3(p.pos + p.vel * 0.012 - vec3f(0.0, 1.0, 0.0), glint.time * 0.08, 0.25, 4.0);
    let d = ahead.xy - c.xy;
    let k = (corners[v].y * 0.5 + 0.5);
    o.pos = vec4f(c.xy + d * k + vec2f(corners[v].x * size.x, 0.0) * 1.5 + vec2f(0.0, corners[v].y * size.y * 0.5), 0.0, 1.0);
    if (p.life < 0.0) { o.pos = vec4f(9.0, 9.0, 0.0, 1.0); }
    o.corner = corners[v];
    o.color = mix(vec3f(0.5, 0.75, 1.0), vec3f(0.95, 0.98, 1.0), clamp(p.pos.y * 0.4, 0.0, 1.0)) * params.brightness;
    return o;
}

@fragment
fn fs(v: VOut) -> @location(0) vec4f {
    let a = smoothstep(1.0, 0.2, abs(v.corner.x));
    return vec4f(v.color * a, 1.0);
}

Pool: fragment shader

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform float time;
uniform sampler2D pass0;

in vec2 v_texcoord;
out vec4 fragColor;

// Night garden: the water lit from below, reflected (rippling) in the pool, a dark hedge behind.
void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    float horizon = 0.36;
    vec3 col = mix(vec3(0.02, 0.03, 0.06), vec3(0.005, 0.01, 0.025), uv.y);
    col += texture(pass0, uv).rgb;
    if (uv.y < horizon) {
        float depth = horizon - uv.y;
        vec2 r = vec2(uv.x + sin(uv.y * 160.0 + time * 3.0) * 0.003 * (1.0 + depth * 10.0), horizon + depth * 0.9);
        col = vec3(0.01, 0.02, 0.04) + texture(pass0, r).rgb * 0.45 * exp(-depth * 4.0);
        col += vec3(0.04, 0.08, 0.12) * exp(-abs(uv.x - 0.5) * 6.0) * exp(-depth * 8.0);
    }
    fragColor = vec4(1.0 - exp(-col * 1.4), 1.0);
}

More WebGPU examples

Galaxy collision (WebGPU) · Lorenz cloud (WebGPU) · Clifford dust (WebGPU) · Fireworks (WebGPU) · Silk (WebGPU) · Shape shifter (WebGPU) · Curl smoke (WebGPU) · Field lines (WebGPU)