Parametric fields, compiled for the browser.
WebGPU compute kernels and deterministic mesh solvers from Yuleng Lab.
Three compute passes, zero host stalls.
Switch stages to inspect bindings and copy WGSL.
@group(0) @binding(0) var vin: texture_2d<f32>;
@group(0) @binding(1)
var vout: texture_storage_2d<rgba16float, write>;
@compute @workgroup_size(8, 8, 1)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
let c = vec2<i32>(id.xy);
let v = textureLoad(vin, c, 0).xy;
let p = vec2<i32>(vec2<f32>(c) - v * 0.016);
textureStore(vout, c, textureLoad(vin, p, 0));
}
@group(0) @binding(0) var pin: texture_2d<f32>;
@group(0) @binding(1) var divt: texture_2d<f32>;
@group(0) @binding(2) var pout: texture_storage_2d<r32float, write>;
@compute @workgroup_size(8, 8, 1)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
let c = vec2<i32>(id.xy);
let s = textureLoad(pin, c + vec2(1, 0), 0).x
+ textureLoad(pin, c - vec2(1, 0), 0).x
+ textureLoad(pin, c + vec2(0, 1), 0).x
+ textureLoad(pin, c - vec2(0, 1), 0).x;
let d = textureLoad(divt, c, 0).x;
textureStore(pout, c, vec4(0.25 * (s - d), 0.0, 0.0, 1.0));
}
@group(0) @binding(0) var sdf: texture_2d<f32>;
@group(0) @binding(1) var<storage, read_write> args: Indirect;
@group(0) @binding(2) var<storage, read_write> segs: array<vec4f>;
@compute @workgroup_size(8, 8, 1)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
let c = vec2<i32>(id.xy);
let a = textureLoad(sdf, c, 0).x;
let b = textureLoad(sdf, c + vec2(1, 0), 0).x;
if ((a < 0.0) != (b < 0.0)) {
let i = atomicAdd(&args.verts, 2u) / 2u;
segs[i] = vec4f(f32(c.x), f32(c.y), f32(c.x), f32(c.y + 1));
}
}
Keep geometry on the device.
0 B
Host readback per frame on 1024×1024 solver passes.
Frame dispatch graph
rgba16float, indirect
Staging readback
mapAsync copies 4.19 MB to host
Indirect graph
dispatchWorkgroupsIndirect feeds draw directly
Operator note
Indirect counts keep contours resident in VRAM.
Targets, limits, and modules.
Dependency-free ES modules and standalone WGSL kernels.
| Subsystem | Specification | Delivery |
|---|---|---|
| Compute backend | WebGPU WGSL | Zero dependencies |
| Workgroup tile | 8×8 threads, ping-pong | Static layout |
| Field precision | f16 velocity, f32 SDF | Fixed step |
| Package format | Native ES modules | No bundler |
| License and source | MIT or Apache-2.0 | ops@yuleng.eu.org |
Run a solver in plain JavaScript.
Bind a canvas, pick a kernel, and stream uniform state.
import { createSolver } from "/assets/yuleng-field.js";
const solver = await createSolver({
canvas: document.getElementById("field-canvas"),
kernel: "curl",
});
solver.setUniforms({ freq: 2.8, visc: 0.32 });
solver.stepLoop();