How it works
The pool is 65,536 particles, and the particles are the liquid: they carry its shape and momentum, so splashes, sheets and droplets come for free and never smear. Every substep they are scattered onto a 327×184 staggered grid (plus 8 hidden columns at each side), the grid makes the flow incompressible, and the change is handed back to the particles. This is the FLIP/PIC method, running entirely in WebGL2 fragment shaders. What you see is not the particles but a surface rebuilt from them: a density field, cut at an iso level and mirrored like liquid chrome.
Pipeline (per frame)
pointer segment ─▶ paddle capsule (push-out radius R, solid cells to R − 3, swept per substep)
+ a swell under a hovering cursor, a soft lid under the hero copy
1 substep per 60 Hz frame (+1 for a fast sweep with the paddle in the pool)
┌─ simulation, per substep ── 8 passes (7 when calm) ──────────────────────────────────────┐
│ [1] P2G gl.POINTS 3 px × 65,536, blend ONE+ONE, 2 render targets │
│ acc RGBA16F 344×185 Σw·u, Σw·v, Σw_u, Σw_v tent weights computed │
│ cell RG16F 344×185 Σw_c, particles in cell from gl_FragCoord │
│ [2] FORCES 3 render targets in one pass │
│ old RGBA16F normalised grid (the FLIP reference) │
│ vel RGBA16F + gravity, paddle grip, hover swell, copy lid, walls │
│ p RGBA32F (warm-start p, A = 1/diagonal, B = rhs·A, ρ/ρ₀) │
│ fluid = holds a particle or ρ ≥ ¼ρ₀; diagonal = fluid │
│ neighbours + ghost-fluid 1/θ for air ones; air, solid = 0 │
│ rhs = (∇·v − k·(ρ/ρ₀ − 1)) / Δt (pull-in on interior cells only) │
│ [2b] VISC ×2 only when Viscosity > 0: 4 Jacobi steps on (I − νΔt∇²) v = v₀, 2 per │
│ pass (each fragment replays its cone in registers); the second pass │
│ also writes p, from the diffused velocity │
│ [3] PRESSURE ×4 RGBA32F 344×185 red-black SOR, ω = 1.82, 3 iterations per pass: │
│ each fragment loads the 85 cells within 6 of it and replays the │
│ sweeps in registers, p += ω(A·Σp_nb − B − p); 12 iterations, 9 when │
│ calm, 6 in each half of a split frame, 24 after a pour │
│ [4] PROJECT 2 render targets RGBA32F 344×185: every u / v face once, │
│ v −= Δt∇p (ghost fluid at the surface), solid faces take the wall / │
│ paddle velocity, stored pre-weighted (w·v, w·v_saved, w, w_saved); │
│ air–air faces get weight 0 │
│ [5] G2P RGBA32F 512×128 (x, y, vx, vy): 2 branch-free 8-fetch bilinear gathers, │
│ v = mix(PIC, FLIP, 0.94) (slow surface → PIC), RK2, speed and step │
│ ≤ 2.4 screen heights/s, paddle push-out, walls │
└──────────────────────────────────────────────────────────────────────────────────────────┘
┌─ surface, per frame ── 8 passes ─────────────────────────────────────────────────────────┐
│ [6] SPLAT gl.POINTS r = 2 cells, poly6, + copies mirrored at the walls (one draw) │
│ → field RG16F 683×372 density, speed·density (0.25× canvas + margin) │
│ [7] BLUR 13-tap Gaussian H+V, σ = 1.4 cells → medium RG16F 683×372 │
│ H+V σ = 7 cells → wide RG32F 171×372 (¼ width, full height) │
│ [8] DEEP RG32F 171×372: wide blurred again vertically (σ 14 cells, ≤ 0.3 × depth) │
│ [9] NORMALS RGBA16F 683×372: ∇ lip(medium) + ∇ dome(wide) + ∇ deep │
│ [10] DISPLAY 2560×1440 → premultiplied canvas │
│ coverage smoothstep(iso ± fwidth): medium blur when calm, raw when fast │
│ dark page: mirror of a studio seen through a 40° lens (floor, wall, │
│ horizon strip, key softbox) + tight crest bloom │
│ light page: Fresnel + Blinn-Phong + thickness tint + contact + soft shadow │
└──────────────────────────────────────────────────────────────────────────────────────────┘
┌─ every 4th frame ── 1 pass ──────────────────────────────────────────────────────────────┐
│ [11] REDUCE RGBA16F 128×16: per block, the top occupied cell and fastest cell velocity │
│ → PBO + fence, read a frame later: is the paddle in the liquid? how calm? │
└──────────────────────────────────────────────────────────────────────────────────────────┘
The ideas that matter
- The grid is a scratchpad. Particles hold position and velocity in an RGBA32F texture. Positions need full float: half precision steps by a quarter cell at x ≈ 300, enough to make a resting pool shimmer. Each substep the grid is rebuilt from the particles, used once and thrown away. Nothing is advected on the grid, so there is no numerical smearing: a droplet thrown across the screen stays a droplet.
- Scatter without compute shaders. Each particle is drawn as a 3-pixel point centred on its nearest grid node. Those nine fragments cover every staggered face it touches, and each fragment computes its own tent weight from
gl_FragCoord. Additive blending sums Σw·u, Σw·v and the weights in one RGBA16F texel. The targets are 16-bit on purpose, because Apple GPUs can't blend 32-bit float. - FLIP vs PIC. PIC replaces a particle's velocity with the grid's (smooth, but it damps everything). FLIP adds only the grid's change (lively, keeps every ripple, but noisy). The FLIP ratio blends the two: 0.94 makes mercury splashy, and Honey's 0.55 plus implicit viscosity makes it pour. Slow particles in surface cells (ρ/ρ₀ < 0.85) lean most of the way to PIC. That irons FLIP's 3–6 cell crinkle out of a settling meniscus, while splashes and the bulk keep the full ratio.
- A free surface that doesn't foam. Marking a cell as liquid only when a particle sits in it turns every cell that empties for a moment into a p = 0 bubble, and an under-converged solve in a deep pool then opens dozens of them. Here a cell is liquid when it holds a particle or its tent density reaches a quarter of rest. Air enters the pressure stencil through the ghost-fluid rule: the surface sits where the density level set ρ/ρ₀ = ½ crosses between two cell centres (the fraction θ), and air adds 1/θ to the diagonal. Surface cells therefore stop flipping between full and empty, and that flipping was most of FLIP's surface noise. A gentle volume-drift term pushes over-dense cells apart and pulls under-dense interior cells back together, so voids close and the pool neither swells nor sags. The solver is warm-started and seeded with hydrostatic pressure, which carries most of the work from one substep to the next. It runs 12 iterations per substep, 9 when the pool is calm (paddle out of the liquid, typical speed under 0.35 screen heights per second), 24 for the first 40 substeps after a pour or resize, and up to 1.75× more in deep portrait pools. The first 18 substeps after a pour run silently, before the first frame.
- Surface particles only see liquid. A face between two air cells carries no pressure correction, and a surface particle that averaged it in would stick or jitter. The projection pass gives those faces zero weight in both the new and the saved grid, which works as a one-sided velocity extension. The FLIP change is then taken over exactly the same faces.
- Dependent passes, not pixels, are the budget. On a 300×170 grid each pass is nearly free to shade, but every pass waits for the one before it. So divergence and cell typing ride along as a third output of the forces pass, and that output also folds each fluid cell's Poisson stencil into its texel: A = 1/diagonal (fluid neighbours plus the ghost-fluid 1/θ of air ones) and B = rhs·A, with air and solid cells all zeros. The solver then needs no cell types, and a relaxation is just p += ω(A·Σp − B − p). Red-black SOR runs three whole iterations per pass. Each fragment loads the 85 cells within six of it and replays the three red and black sweeps over the shrinking cone in registers, ending with the value three one-iteration passes would give (checked against a CPU replay to float rounding). The code is generated straight-line, one scalar per cell. Four or six iterations per pass spill registers and were slower, so 12 iterations take 4 passes instead of 12. Viscosity does two Jacobi steps per pass the same way, with each step rounded to half float as the ping-pong between them would be. The projection runs once per face in its own pass and stores each face pre-weighted. G2P then reads 16 texels per particle instead of re-projecting the same 16 faces from 64 reads, which had made it the most expensive pass in the frame. Its bilinear gathers are branch-free, so all eight fetches of a gather go out together. A violent stroke can split a frame into two substeps, but each half then runs 6 pressure iterations in 2 passes, so no frame runs more than 20 passes at default settings, plus the readback pass every 4th frame (25 in the previous version).
- Knowing where the liquid is. Every 4th frame a 128×16 pass scans the grid in blocks for the highest occupied cell and the fastest cell velocity, and the result is read back through a pixel-pack buffer once its fence has signalled. The page never waits on the GPU for it. The CPU uses it to add a substep only when the paddle is actually dipped into the liquid and sweeping fast, to drop to the calm iteration count, and to blend the silhouette toward its smooth version as the pool settles. Two first tries were slow. A MAX-blended scatter of every particle into a few pixels serialises on the GPU (about 4 ms), and so does one fragment looping over a whole column.
- The cursor is a solid paddle. Particles inside the swept capsule are pushed to its round surface with their inward velocity removed, and grid faces inside it take its velocity. The solid cells stop three cells short of the push-out radius, because their staircase of zero-velocity faces would otherwise shape the hole into a polygon. A softer ring of influence mixes the liquid's velocity toward the paddle's (it never adds), so a fast flick throws sheets. A cursor that rests for about two seconds shrinks its paddle away entirely and the pool closes over it. Rising out of the pool, the paddle lets go of the liquid it carries, so a flick throws a sheet instead of lifting a tower. A cursor hovering up to 0.4 screen heights above the pool pulls a mound up under it that follows it sideways. That mix stops a little below the paddle, so the liquid reaches toward the cursor without climbing onto it. Under the hero copy a soft lid brakes rising liquid, so sloshes and swells stay below the text you are reading. The intro ghost scoops through the pool and flicks a sheet into the air within the first second, using a smaller paddle so it never carves an unexplained hole, and lets go of the pool as soon as its stroke ends.
- A rounded surface, not an outline. Where the liquid is calm, the silhouette is the medium blur cut at the iso level, a smooth meniscus. Where it moves fast, the raw splat shows through, so droplets stay crisp; that blend is also scaled by a time-smoothed liveliness of the whole pool from the readback, so a settling pool goes back to the smooth silhouette. Three height profiles give the normals: a circular "lip" from the medium blur rounds droplets and sheet edges, a dome from the wide blur makes the soft rim, and a deep profile (the wide blur blurred again vertically by 14 cells, in its own ¼-width pass, so the normals pass reads it in 5 taps instead of recomputing it in 65) keeps curving 20+ cells below the surface. The body therefore reflects a gradient of the studio, not one flat value. A mirror's horizon line is unforgiving. The wide blur is stored in 32-bit float at full field height, because half-float steps and ¼-res texel kinks both showed as a stepped horizon. The deep σ shrinks to 0.3× the pool depth in shallow pools, so they still have a saturated core rather than tracing density noise. The normals are computed at field resolution and sampled bilinearly. Differencing the magnified ¼-resolution blurs per screen pixel leaves a kink at every texel, and a mirror reflection turns those kinks into stripes.
- Chrome is the environment. Metal has no colour of its own, so the look lives in what it reflects. On dark pages the studio has a near-black floor and a grey wall behind the viewer, which the deep body reflects. Just above the horizon is a bright strip with a crisp lower edge that fades upward, which the curving body turns into a chrome line that bends with every wave. Above that is a dim sky, then a key softbox overhead. The softbox lies only in front of the liquid, so the silhouette itself reflects the dark backdrop: a dark Fresnel edge with the bright rim just inside it, like real mercury, and no glowing halo. The studio is seen through a ~40° lens, so the view ray changes across the screen and even the flat bottom of the pool reflects a gradient, from wall to floor. A soft shoulder rolls highlights off before they clip. On light pages the studio is soft and page-coloured, and a dielectric's grazing Fresnel is limited, so milk and ink keep their colour right to the edge. Milk's paper is a shade darker than its body, and the body gets a faint occlusion low down, so it reads as a volume. Dither covers the body, the bloom and the shadow.
- Walls you never see. The simulation is 8 columns wider than the screen on each side, so a film that runs up a side wall stays off-screen. The viscosity solve treats the walls as free-slip; the ghost faces past the right and top walls used to make viscous liquid cling to the right edge. Particles are mirrored across a side wall for the splat only below the rest level, so the density doesn't sag at the edges.
- Calm starts and endings. The pool is poured slightly tilted and pre-rolled, so it is already sloshing, and already continuous, on the first frame. Over the last few seconds before the shell's idle stop, a damping ramp settles the liquid, so it never freezes mid-wave. Substep counts round to the nearest whole number, so a 19 ms vsync hitch no longer doubles the work. At most one substep is added on top of the setting, and at most 4 × 64k particle-substeps run per frame (128k particles run at most 2). One speed limit, 2.4 screen heights per second, caps both the particle velocity and the CFL guard on its displacement per substep. The guard is a speed rather than a cell count, so a 120 Hz display runs exactly as fast as 60 Hz. A change to Particles or Fill level re-pours the pool and re-sizes the render field to the new grid. A resize re-fits the grid to the new aspect and rescales the particles on both axes, so a rotated phone keeps the same fill level.
Parameters
Presets
Performance & compatibility
- Measured by the verification harness at 2560×1440 on an Apple M5 (10-core GPU), Chrome, during the scripted pointer sweep. Every number here comes from serial runs through the library's GPU lock, so no other harness or probe was on the GPU. The last full run read 7.7 ms mean, 12.4 ms p95 GPU per frame (timer query), 0.26 ms JS, 60 fps. Over 14 runs of the default preset on this version, the mean ranged 5.7–8.4 ms (median 6.4) and the p95 9.3–13.9 ms (median 11.2); the best full run read 6.3 / 9.9. By preset, in the last round: Mercury 6.2 / 9.6, Milk 6.3 / 9.9, Ink 6.8 / 10.3 and Honey 7.2 / 10.7 ms mean / p95. The lock keeps other agents off the GPU, but the window server, a background malware scan, a backup client and other apps still used it, so single runs varied by ±2 ms at p95; Milk, Ink and Honey each had a run at 12–14.7 ms p95. The fairer comparison is interleaved, against the previous version of this page in the same minutes (3–6 rounds each). There this version reads 13–39% less mean and 6–41% less p95 GPU time: 5.8 / 9.3 against 9.6 / 14.4 ms for the default (6.7 / 11.8 against 9.9 / 14.7 under heavier load), and per preset Mercury 7.6 / 12.2 against 9.6 / 14.5, Milk 6.7 / 10.0 against 9.7 / 15.2, Ink 6.8 / 9.9 against 10.6 / 16.8, and Honey 7.1 / 11.0 against 8.2 / 11.7.
- Where the time went. Run 20–30 frames back to back inside one timer query and the GPU stays at a high clock, which measures the work itself. Measured that way, a frame of the sweep fell from about 3.9 ms to about 1.6–1.9 ms. Each step was checked against the previous output (bit-identical, or equal to float rounding). Moving G2P's inline projection into its own pass saved about 0.6–0.8 ms; G2P had been the most expensive pass. The deep profile's own ¼-width pass saved about 0.8 ms; the normals pass had been recomputing it 5 times per texel. Three SOR iterations per pass saved about 0.3 ms, and the branch-free G2P gathers 0.25 ms. Viscosity now takes 2 passes per substep instead of 5. At 60 fps the GPU runs at a low clock, so the per-frame timer reads about 3–4× the sustained cost, and it moves less than the work does. Removing the whole display pass, for example, takes only about 10% off it. Tried and dropped: 4 or 6 SOR iterations per pass (the bigger cones spill registers and ran slower than 3), 2 per pass (lighter, but 6 passes were slower than 4), all 4 viscosity steps plus the divergence in one pass (each fragment's cone cost more than the passes it saved), and packing the display's inputs into one texture (no measurable change).
- Pass counts: 16 per frame at defaults, 15 when calm and 20 at most during a violent stroke, plus the readback every 4th frame (previously 22, 19 and 25). Substeps multiply the 8 simulation passes. Viscosity adds 2 passes per substep. Particles scale the scatter, gather and splat roughly linearly. The grid is sized for 4 particles per filled cell, so a lower Fill level means a taller grid for the same count, and the 128k (131,072) maximum means a 466×262 grid, which is why it is held to 2 substeps a frame.
- Phones (coarse pointer) and gallery thumbnails run about 40–45% of the particle count with the same pipeline. At 390×844 that is 26k particles on a 100×216 grid with a 780×1688 canvas (DPR capped at 2). The harness measured 60 fps for touch drags. On touch the paddle and the swell exist only while a finger is down. A portrait screen gets a narrower, lower intro stroke and leans on the smooth silhouette, because fewer particles cover each pixel.
- Requirements: WebGL2 with
EXT_color_buffer_float(RGBA32F particles, pressure and projected faces, blendable RGBA16F / RG16F scatter targets) andgl.POINTSwith attribute-lessgl_VertexIDdraws.OES_texture_float_linearis optional: it lets the wide blur and the deep profile be 32-bit, and without it they are RG16F. Without the required extensions the page shows the poster frame and a message. Withprefers-reduced-motionit simulates 2 s of the ghost stroke (the scoop and flick), lifts the paddle out for 1 s so the pool closes over a settling wave, and shows that one frame with a Play button. - The render loop stops 9 s after the last input. The canvas sits under the page content, so the liquid never takes a click.
References & credits
- Zhu & Bridson, “Animating Sand as a Fluid”, SIGGRAPH 2005: the incompressible PIC/FLIP recipe, seeding, velocity extension and RK2 particle advection. cs.ubc.ca (paper).
- Bridson & Müller-Fischer, “Fluid Simulation for Computer Graphics”, SIGGRAPH course notes: MAC grids, free-surface and solid boundary conditions, and the ghost-fluid free-surface pressure condition (after Gibou et al. 2002 and Enright et al. 2003). cs.ubc.ca (course notes).
- David Li, “Fluid Particles” (github.com/dli/fluid, MIT): the point-splat P2G and density-based volume correction in WebGL. Ideas only, no code copied.
- Matthias Müller, Ten Minute Physics “FLIP Fluid” (18-flip.html, MIT): drift compensation, over-relaxed pressure iterations and particle push-out for a moving obstacle. Ideas only, no code copied.
- Jiang et al., “The Affine Particle-In-Cell Method”, SIGGRAPH 2015: the APIC alternative to the FLIP/PIC blend (not used here). math.ucdavis.edu.
- Dither hash after Dave Hoskins, “Hash without Sine” (shadertoy.com/view/4djSRW, MIT).
- Grip-style cursor coupling (mix toward the pointer velocity, never add) and implicit Jacobi viscosity follow the stable-fluids cursor trail this library grew out of. Everything else is written from scratch for this page.