Silk (WebGPU)

glint · Examples · Tutorials · Gallery

A sheet of glowing silk, 16,000 points joined by springs, hanging from a rail and blowing in wandering gusts (harder with the bass). Every point is pulled by its neighbours; drawn as see-through cloth catching the light. Needs WebGPU (Chrome, Edge, Safari 26).

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

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

Cloth: WGSL module

// A sheet of silk (160 x 100 points) hanging from a rail, blowing in gusts: each point is pulled towards where its
// neighbours say it should be (springs), weighed down, pushed by the wind. Drawn as glowing, see-through cloth.
struct Particle {
    pos: vec3f,
    pinned: f32,
    vel: vec3f,
    pad: 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);
}

const W = 160u;
const H = 100u;
const SPACING = 0.016;

fn rest(i: u32) -> vec3f {
    let x = f32(i % W) - f32(W - 1u) * 0.5;
    let y = f32(i / W);
    return vec3f(x * SPACING, 0.8 - y * SPACING, 0.0);
}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    particles[i].pos = rest(i);
    particles[i].vel = vec3f(0.0);
    particles[i].pinned = select(0.0, 1.0, i / W == 0u && (i % 8u == 0u || i % W == W - 1u));
}

// Pulls a point towards the length it should be from neighbour j (half the error: the neighbour does the other half).
fn fix(p: vec3f, j: u32, len: f32) -> vec3f {
    let d = particles[j].pos - p;
    let l = length(d);
    return d / max(l, 1e-5) * (l - len) * 0.5;
}

@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];
    if (p.pinned > 0.5) { return; }
    let dt = min(glint.timeDelta, 0.033);
    // Position-based: move with the forces, then towards where the neighbours say it should be (it can't blow up,
    // however stiff, unlike springs).
    let t = glint.time;
    let gust = (0.5 + 0.5 * sin(t * 0.7 + p.pos.x * 2.0)) * (0.6 + 0.4 * sin(t * 2.3 + p.pos.y * 5.0 + p.pos.x * 3.0));
    let acc = vec3f(0.0, -0.6, 0.0) + vec3f(0.3 * sin(t * 0.3), 0.0, 1.0) * gust * params.wind * (0.7 + glint.spectrum.x * 1.2);
    let old = p.pos;
    p.vel = p.vel * exp(-1.5 * dt) + acc * dt;
    p.pos += p.vel * dt;
    let x = i % W;
    let y = i / W;
    var c = vec3f(0.0);
    var n = 0.0;
    if (x > 0u) { c += fix(p.pos, i - 1u, SPACING); n += 1.0; }
    if (x < W - 1u) { c += fix(p.pos, i + 1u, SPACING); n += 1.0; }
    if (y > 0u) { c += fix(p.pos, i - W, SPACING) * 1.6; n += 1.0; }
    if (y < H - 1u) { c += fix(p.pos, i + W, SPACING); n += 1.0; }
    if (x > 1u) { c += fix(p.pos, i - 2u, SPACING * 2.0) * 0.4; }
    if (x < W - 2u) { c += fix(p.pos, i + 2u, SPACING * 2.0) * 0.4; }
    p.pos += c * (4.0 / max(n, 1.0)) * 0.45;
    p.vel = (p.pos - old) / max(dt, 1e-4);
    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 {
    var o: VOut;
    let x = i % W;
    let y = i / W;
    if (x >= W - 1u || y >= H - 1u) { o.pos = vec4f(9.0, 9.0, 0.0, 1.0); o.color = vec3f(0.0); return o; }
    // Two triangles per cell, between this point and its right and lower neighbours.
    var pick = array(0u, 1u, W, W, 1u, W + 1u);
    let j = i + pick[v];
    let a = particles[i].pos;
    let n = normalize(cross(particles[i + 1u].pos - a, particles[i + W].pos - a));
    let c = view3(particles[j].pos, -0.4 + sin(glint.time * 0.05) * 0.3, 0.12, 2.6);
    o.pos = vec4f(c.xy, 0.0, 1.0);
    // Sheen: bright where it faces the light, edges glowing where it turns away.
    let light = normalize(vec3f(-0.4, 0.5, -1.0));
    let facing = abs(dot(n, light));
    let rim = pow(1.0 - abs(n.z), 2.0);
    let hue = mix(vec3f(0.9, 0.25, 0.45), vec3f(0.35, 0.3, 1.0), f32(y) / f32(H));
    o.color = (hue * (0.25 + facing * 0.8) + vec3f(1.0, 0.85, 0.7) * pow(facing, 12.0) * 0.8 + hue * rim * 0.6) * params.brightness;
    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.4), 1.0);
}

More WebGPU examples

Lorenz cloud (WebGPU) · Clifford dust (WebGPU) · Fireworks (WebGPU) · Fountain (WebGPU) · Shape shifter (WebGPU) · Curl smoke (WebGPU) · Field lines (WebGPU)