How it works

How it works

These beads are 3000 particles of liquid simulated with SPH (smoothed-particle hydrodynamics) in its position-based form, the family that includes Position Based Fluids (PBF). It runs on the CPU in plain JavaScript, in about 2.1ms per frame. The idea that makes it look like liquid metal is surface tension: the particles pull on each other wherever the liquid is thin. A bead that your cursor cuts in two snaps into two rounder beads, wobbles, rolls back into its dimple in the tray and merges again. Where liquid ends up lying across the ridge between two dimples, the ridge parts it, so the tray keeps all of its beads however long you play. Your cursor is also a small lamp over the tray: every bead reflects it, and the liquid you pass drifts after it. The GPU does only the rendering. It turns the particles into a smooth metaball height field and lights it like chrome in a photo studio, or like a water drop acting as a lens on linen.

Pipeline (per frame)

CPU · typed arrays · lengths in h, the SPH kernel radius                JS ≈ 2.1ms at 3000 particles
 pointer   wake: liquid near this frame's path eased toward the pointer's velocity (a capped blend)
           lamp: a critically damped follower of the pointer, about 0.15s behind
 dimples   7 drifting Gaussian bowls (5–6 below 2850 particles), soft-max'd so ridges part them;
           each bowl's depth follows its deficit against a target share → force grid 48×28 → bilinear
 bridges   a bead with ≥6% of itself in a second bowl's basin: its particles pulled to their own dimple,
           and those with a neighbour across the ridge marked for the ridge knife
 ┌ substep ×2 at 60 Hz (Δt = 1/120s), ×1.5 with Extra substeps; more when k·max(1,τ)·Δt²·ρ0^1.5 > 0.9 ┐
 │ at most 10000 particle-substeps per frame: beyond that the frame simulates less time (slow motion) │
 │ integrate  v += Δt·dimple pull, × tray friction;  x_prev = x;  x += Δt·v                           │
 │ grid       counting sort into h×h cells (≈60×34); half stencil → ≈67k neighbour entries            │
 │ relax      Gauss-Seidel, per particle:  ρ = Σ(1−r)²   ρn = Σ(1−r)³                  Clavet 2005    │
 │            P = k(ρ−ρ0), ×tension where negative (cohesion);  Pn = 0.4k·ρn (always repulsive)       │
 │            each neighbour pair moves ½·Δt²(P·q + Pn·q²) apart, half each way (q = 1−r)             │
 │ knife      strip of half-width R + pointed nose 3R long: particles eased sideways                  │
 │ ridge      marked ridge-line particles eased apart at 2.5 h/s, each toward its own dimple          │
 │ walls; v = (x − x_prev)/Δt                                                                         │
 └────────────────────────────────────────────────────────────────────────────────────────────────────┘
 XSPH      once per frame, every neighbour pair blends toward its mean velocity            Monaghan 1992
 clusters  union-find on the neighbour lists: beads under ~10 particles fade; each bead → a dimple
 depth     surface = density deficit + lopsided neighbourhood; 3 sweeps of d = min(d, d_j + r_ij)
 upload    (x, y, depth, weight) per particle, Float32 → instanced vertex buffer (48 KB)

GPU · canvas 2560×1440 (DPR 2)                                     11 draws on Mercury/Gold, 5 on Water
 SPLAT fine    N octagons, r = 3.2 spacings (50px), additive, MRT ─▶ F RGBA16F 1280×720  (w, ∇w)
                                                                ─▶ D RGBA16F 1280×720  (w·d, d·∇w)
 SPLAT coarse  N octagons, r = 159px                            ─▶ G RGBA16F  160×90   (w, ∇w)
 BROAD DOME    H+V 9-tap blur of ∇G, σ = 5.5% of short side    ─▶ G′ RGBA16F 160×90   (w, ∇w + 0.9·blur)
 DISPLAY       edge distance (f−iso)/|∇f|, smooth-min'd with the depth D/F → rim + G′ dome → normal
               chrome/gold: perspective reflection of a procedural studio and the cursor lamp, every
               light filtered to the pixel footprint, Fresnel, sun glint, soft shoulder
                                                               ─▶ RGBA8 2560×1440 + excess
               water: refract the tray (mipmapped weave), Fresnel, dark far rim, caustic ─▶ canvas
               tray: weave 512² RGBA8, shadow + AO from G′ (B-spline), skipped where no bead is near
 BRIGHT        ¼ res: 4×4 box of the display's over-white excess ─▶ RGBA16F 640×360  (dark only)
 BLUR ×4       H+V 9-tap linear-sampled Gaussian, radii 1.6 and 3.3 texels × short side / 1440
 COMPOSITE     display + bloom × Glints, ±½ LSB dither ─▶ canvas RGBA8

The ideas that matter

Parameters

Presets

Performance & compatibility

References & credits