Field lines (WebGPU)

glint · Examples · Tutorials · Gallery

The electric field of four drifting charges (two positive, two negative): 200,000 particles leave the positive ones and follow the field to the negative, their trails drawing the lines of force as they bend. Needs WebGPU (Chrome, Edge, Safari 26).

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

Field lines (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

3 passes, drawn in this order each frame, in WGSL and GLSL.

Particles: WGSL module

// Electric field lines: four charges drifting about (two +, two -); 200,000 particles leave the positive ones and
// follow the field until a negative one swallows them. The trails pass draws the lines they trace.
struct Particle {
    pos: vec2f,
    life: f32,
    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);
}

fn charge(k: u32) -> vec3f {
    let t = glint.time * params.drift * 0.15;
    let fk = f32(k);
    let p = vec2f(sin(t * (0.7 + fk * 0.13) + fk * 2.1) * 0.95, sin(t * (0.9 + fk * 0.11) + fk * 1.3) * 0.55);
    return vec3f(p, select(-1.0, 1.0, k % 2u == 0u));
}

fn field(p: vec2f) -> vec2f {
    var e = vec2f(0.0);
    for (var k = 0u; k < 4u; k++) {
        let c = charge(k);
        let d = p - c.xy;
        e += c.z * d / pow(dot(d, d) + 0.0004, 1.5);
    }
    return e;
}

fn spawn(i: u32, salt: u32) -> vec2f {
    let k = (i % 2u) * 2u;
    let a = hash(i * 3u + salt) * 6.2832;
    return charge(k).xy + vec2f(cos(a), sin(a)) * 0.03;
}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) id: vec3u) {
    let i = id.x;
    if (i >= glint.count) { return; }
    // Spread along their lines already, so the picture is there from the start.
    var q = spawn(i, 0u);
    let steps = u32(hash(i * 5u) * 250.0);
    for (var k = 0u; k < steps; k++) { q += normalize(field(q) + vec2f(1e-6)) * 0.008; }
    particles[i].pos = q;
    particles[i].life = hash(i * 7u) * 3.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 dt = min(glint.timeDelta, 0.033);
    // Along the field at a steady pace (its direction, not its strength).
    let e = field(p.pos);
    p.pos += normalize(e + vec2f(1e-6)) * dt * 0.5;
    p.life -= dt;
    var swallowed = false;
    for (var k = 1u; k < 4u; k += 2u) {
        if (length(p.pos - charge(k).xy) < 0.025) { swallowed = true; }
    }
    if (p.life < 0.0 || swallowed || abs(p.pos.x) > 2.0 || abs(p.pos.y) > 1.2) {
        p.pos = spawn(i, glint.frame);
        p.life = 3.0 + hash(i + glint.frame) * 2.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];
    let aspect = glint.resolution.x / glint.resolution.y;
    var o: VOut;
    o.pos = vec4f(p.pos.x / aspect, p.pos.y, 0.0, 1.0);
    let strength = clamp(log(length(field(p.pos)) + 1.0) * 0.18, 0.0, 1.0);
    o.color = mix(vec3f(0.2, 0.35, 1.0), vec3f(1.0, 0.75, 0.3), strength) * params.brightness * (1.0 + glint.beat * 0.8);
    return o;
}

@fragment
fn fs(v: VOut) -> @location(0) vec4f {
    return vec4f(v.color, 1.0);
}

Trails: fragment shader

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform sampler2D feedback;
uniform sampler2D pass0;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    vec3 col = texture(feedback, uv).rgb * 0.94 + texture(pass0, uv).rgb;
    fragColor = vec4(col, 1.0);
}

Glow: fragment shader

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform sampler2D pass1;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    vec2 px = 1.0 / resolution;
    vec3 col = texture(pass1, 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(pass1, uv + vec2(cos(a), sin(a)) * r * px).rgb / float(ring * 8);
        }
    }
    col += halo * 0.5;
    fragColor = vec4(1.0 - exp(-col * 1.6), 1.0);
}

More WebGPU examples

Fountain (WebGPU) · Silk (WebGPU) · Shape shifter (WebGPU) · Curl smoke (WebGPU)