fluid
A fluid solve with coloured dye.
fluid(opts: Parameters<typeof fluidInput>[0] = {}): FluidInput
Import simulate from shaders/std and use simulate.fluid.
velocity/dye.res: grid sizes (cells on the long side). 256 / 512 is a good start. -dye.space: 'oklab'carries the dye in OKLab, so colours mix cleanly instead of going muddy. -forces: WGSL returning avec2facceleration for this cell (uv per second²). It seescellUV,velocity(this cell's),dye(this cell's colour), and everything a body sees. -inject: WGSL returning avec4fof dye to add this frame at this cell (premultiplied: the alpha is how much). It seesdye, the cell's dye before this frame's addition (straight linear colour, alpha how much), so a spout can top up to a set thickness:return vec4f(color.rgb, 1.0) * max(target - dye.a, 0.0).isNew(period)is true on the first frame of eachperiod-second slot: use it to add a drop once instead of every frame. -init: WGSL returning the dye's first state at this cell (straight linear colour, alpha how much), written once when the fluid starts: a pool that's already full. -stir,dissipationanddrain.floortake a number or a prop (stir: p('smear')), read live each frame. -stir: how hard the pointer pushes the fluid (0 turns it off; default 1). A drag swirls it; for clicks, readevents('click')ininjectand add dye where they land. -boundary.floor: 'solid'makes the bottom a wall dye piles up against (paint pooling);boundary.walls: 'solid'does the same at the sides.drain.floorremoves that much dye per second near the floor so the canvas never fills. -dissipation: how fast the flow's motion dies away (default 0.2; 0 keeps it swirling, higher feels thicker). The dye itself never fades;drainremoves it. -pressureIterations: solver accuracy (default 20); more keeps swirls tighter, at a cost. - Read the dye asink(uv): straight-alpha colour, alpha how much dye is there (in OKLab mode it comes back as ordinary linear colour).ink.velocity(uv).xyis the flow, in uv per second. Shade from the dye's alpha (its slope gives a surface normal) for a thick, glossy look. - The solve runs at a fixed size, so its cost doesn't grow with the canvas;dtfollows Speed.
Parameters
| Name | Type | Notes |
|---|---|---|
opts | Parameters<typeof fluidInput>[0] | Optional. Default {}. |
Example
inputs: {
ink: simulate.fluid({
velocity: 256, dye: {res: 512, space: 'oklab'}, boundary: {floor: 'solid'}, drain: {floor: 0.05},
forces: wgsl`return vec2f(0.0, gravity * dye.a);`,
inject: wgsl`
let id = floor(time * drops);
let at = vec2f(hash11(id), 0.08);
let k = select(0.0, 1.0, isNew(1.0 / drops)) * (1.0 - smoothstep(0.0, 0.03, distance(cellUV, at)));
return colors(hash11(id + 1.0)) * k;
`,
}),
},
paint: wgsl`return ink(uv);`,