How it works
Every stroke lays a bundle of hair-thin threads of dye into a real, viscous stable-fluids simulation. The fluid drags them into lazy S-curves around the vortices the stroke leaves behind; they hold for about three seconds, then thin and fade. Two things make it work. Second-order MacCormack advection keeps the threads crisp and separate for their whole life; toggle Advection in the hero and semi-Lagrangian widens each thread until the display drops it, within about 1.5 s (turn on Raw dye in the controls to watch the blur itself). A display pass then rebuilds each thread's round cross-section from the dye field, so it can be lit like a chrome tube: a white-hot core, coloured flanks and a rim, with bloom on dark and a soft cast shadow on light.
Pipeline (per frame)
pointer ─▶ timestamped samples, read back 25 ms late at each step's own time
─▶ critically damped spring (ω 60 rad/s), sub-stepped ≤ 0.012 screen heights
│ fixed 60 Hz step from an accumulator: 0–1 steps per frame at 120 Hz, 2 at 30 Hz
▼
SIM STEP S = velocity 228×128 RG16F (sim 128, 16:9) D = dye 1820×1024 RGBA16F (dye 1024)
strands ×~10 capsules over the path the deposit point (0.1 h behind the brush)
passed, ridges at p − u·dt·age, tapered heads, blend MAX ──▶ dye D
grip discs ×1–6 Gaussian disc per spring sub-step, blend SRC_ALPHA,
mix(v, 0.4·v_stroke, w·grip), target ≤ 1.6 h/s ──▶ vel S
stop fade hard stop or cut-off stroke: dye × (1 − k·disc) at heads ──▶ dye D
vorticity curl inline (5 taps) + confinement, |v| ≤ 3 h/s ──▶ v₀ S
viscosity ×3 fused red-black Gauss-Seidel on (I − ν·dt·∇²) v = v₀ ⇄ vel S
divergence open boundary (zero-gradient outflow) ──▶ div S R16F
pressure ×6 fused red-black SOR, ω 1.8, warm start ×0.85, p = 0 out ⇄ p S R16F
gradient v −= ½·∇p (consistent with the ½ in the divergence) ──▶ vel S
backtrace midpoint (RK2) displacement and its reverse, B-spline u ──▶ disp S RGBA16F
dye pass A semi-Lagrangian step along disp ──▶ tmp D
dye pass B reverse step + ½ error, clamp to the 4 source texels, fade ──▶ dye D
velocity advect semi-Lagrangian self-advection, fade 0.5/s ──▶ vel S
RENDER (only on frames where a step ran or a control changed)
bloom prefilter tube shading 2×2 per texel, soft knee + Karis average ──▶ 640×360 RGBA16F
bloom 3 dual-filter downs, 3 ups ──▶ 640×360 … 80×45
composite 4×4 B-spline dye stencil → Hessian of log d → crest distance, σ, peak
→ tube shading per channel (4 corner lights, glints, core, rim)
→ fresher strands in front, text guard, clear zone
dark: background screened by 1 − e^−(strands + bloom)
light: paper × offset multi-scale shadow, strands over
+ interleaved-gradient-noise dither ──▶ canvas 2560×1440
draws: 16 sim passes + ~10–16 splats per sim step, 8 per rendered frame
The ideas that matter
- Grip, not impulse. The stroke blends the fluid's velocity toward the stroke's velocity with a capped weight. It never adds force on top. The fluid is dragged like a spoon through honey and the threads are left behind as a wake. Impulse splats read as smoke.
- The wake must be slower than the pen. The grip pulls the fluid toward only 40% of the stroke velocity; if the wake jet ran at the brush's own speed, freshly laid thread would ride it as fast as it is laid and bunch into tufts. Threads are laid 0.1 screen heights behind the brush, clear of the sideways outflow lobes the grip pushes out around it, which would otherwise splay each thread's tip into a nail head.
- Lay the dye where it would already be. Each step lays the stretch of path the deposit point passed during the last step. Laid all at once, its older end would be a step younger than the dye laid just before it, which has already ridden the flow, and every junction would show a small sideways step that the flow later shears into bristles. So the ridge is evaluated at p − u·dt·age, with the age in steps since the deposit point passed that arc length, and each batch continues the previous one exactly where its end has drifted to.
- A clean resampled pointer. Events and frames arrive irregularly; a 52 fps phone gets one or two steps per frame. Each fixed step moves the brush toward where the pointer was 25 ms before that step's own time, interpolated between timestamped samples, so the brush speed and the wake jet don't pulse from step to step. A new stroke starts the spring already moving at the pointer's speed, so the brush never accelerates from rest through the start of the path.
- Living ends. Nothing is laid over the first 0.08 h of a stroke, where its starting vortex sits, and the bundle fades in over the next 0.16 h while the grip eases in over 0.2 h. Every thread has its own head, staggered up to 0.03 h behind the bundle's and tapered over 0.04 h with a log-parabolic profile, so the bundle ends in rounded tips instead of a ribbon-cable cut. After a hard stop the wake jet keeps going and would ram the heads into the still fluid ahead; a short multiplicative fade over that spot ends the bundle in a soft taper instead. The same fade applies when a stroke is cut off while still moving (the intro ghost handing over to your pointer), and while the brush brakes the newest heads are laid faint, so the jet can't squash them into paddles and comb them into a fan. Heads are also laid faint wherever the flow at the deposit point runs sideways at more than about 30% of the wake's along-stroke speed; if the deposit stops there, the thread ends in a wisp.
- Discs, not capsules, for the grip. Each spring sub-step stamps one Gaussian disc, spaced far closer than the brush σ, so the summed push along the path is ripple-free with no hard front. Per-step capsules double-blend their joints, and the shear layer prints that pulse into the threads.
- A backtrace that doesn't fold. The first-order map x → x − dt·u(x) folds back on itself once dt·|∇u| exceeds 1 and tears threads into dashes where jets cross. The dye backtrace is midpoint (RK2), x − dt·u(x − ½dt·u(x)), which stays one-to-one far beyond that in shear and compression. It is computed once per step at sim resolution, with its reverse for MacCormack, so each dye texel takes one B-spline lookup of it. The grip's target speed is capped at 1.6 screen heights per second (1.2 on phones), and the vorticity pass hard-limits the flow at 3 h/s.
- Threads that tile exactly. Each capsule is cut at its start by the previous segment's direction and at its end by its own, tested where the dye was laid, so the offset threads run continuously through curves. They are MAX-blended, so overlaps never pile up, and neighbouring capsules overlap by two texels, because two half-weights at a shared joint would leave a dip. In tight turns the inner threads are pinched out before they can fold over each other, and a turn tighter than the bundle drops the deposit for a moment. The curvature behind both is the brush's heading change per spring sub-step over the distance travelled, averaged over ±0.03 h of path around the deposit point. That point trails the brush, so the average needs no lag and lands on the turn itself; a running low-pass put the drop a tenth of a height past it at speed.
- Four dye channels. Thread
igoes into channeli % 4, so neighbouring threads never share a channel and never merge into one band, even when the flow squeezes them together. - MacCormack with a limiter. Pass B advects the forward result backwards and adds half the round-trip error (Selle et al. 2008), then clamps to the min/max of the four texels the forward backtrace blended, fetched at exact centres. The clamp alone is the limiter. Selle's alternative, reverting to semi-Lagrangian wherever the result overshoots, throws the whole correction away on every oscillating stripe, and the research measurements put it well behind the clamp on thin dye (32% against 56% stripe contrast at a 12 px period after 3 s). Velocity is sampled with a cubic B-spline, so hard-stretched threads don't crease along the coarse sim cells.
- The tube is reconstructed, not stored. A Gaussian ridge is a parabolic cylinder in log space. From a C² cubic B-spline reconstruction of the dye, the display gets the gradient and the full Hessian of log d analytically. The Hessian's most negative eigenvalue gives the thread's width σ, and its eigenvector the direction across it; only the gradient's component along that direction places the pixel, at crest distance q = |∇log d·e| / (−λ₁). Density that varies along a thread (bunching, stretching) changes its brightness, not its outline, so it never breaks into beads or grows fuzzy flanks. Where the curvature along the thread rivals the curvature across it (a torn, corrugated or crossing patch), the tube tapers away, and where a channel has spread to more than about 1.75× the width it was laid at (a head squashed into a paddle, threads merged into a sheet), it dissolves instead of drawing a fan of scales. A squashed paddle also flips the Hessian's axes, and would otherwise be drawn as a short bar across the bundle. Every thread gets a full round cross-section with an analytic one-pixel edge, softened over two or three more pixels where the Hessian degenerates, so such patches dissolve rather than stair-step. Fading threads taper as well as dim, and hold their full width and brightness until their crest density falls below about a quarter.
- Fresher in front. Where threads of different channels overlap, the denser (younger) one is drawn in front, using a blend weighted by e14·p of crest density p. Clearly different ages sort; two alike mix smoothly, so there is no seam where they swap. A fixed channel order would draw a dim old thread across a fresh bundle as a row of dark rungs.
- Highlights for a thread, not a flat cylinder. On a real cylinder seen face-on, a light only glints on strands perpendicular to it, so with four corner lights most strands had no highlight and Shine did nothing. Each half-vector loses 60% of its component along the strand, so every thread carries a glint line from each light. The Blinn-Phong lobe is partly energy-normalised (√((s+8)/98)): low Shine is a broad satin fill, high Shine a thin bright chrome line. On dark the rim is a wide u⁴ profile, so it reads on a five-pixel tube; on light it stays narrow, so the graphite keeps a solid silhouette.
- A consistent projection, in few passes. The divergence uses the ½ central-difference factor, so the gradient does too. Red-black SOR with ω = 1.8 converges 2–4× faster per sweep than Jacobi, and each pass runs a full sweep: red cells update from the old black ones, and each black cell recomputes its four red neighbours' new values on the fly. The viscosity solve does the same with Gauss-Seidel. On this GPU every small pass costs about 0.2 ms, almost all of it fixed overhead, so halving the pass count matters far more than the extra fetches. Pressure runs 6 sweeps (5 on phones). Read back after a fast stroke, the projection removes 40% of the RMS divergence at 6 sweeps, 56% at 10 and 69% at 16, but side-by-side replays of the same stroke at 6 and 10 sweeps showed no visible difference in the threads, so the page keeps the cheaper solve. The open boundary lets the wake leave the screen instead of bouncing.
- Fixed 60 Hz. Semi-Lagrangian smear grows with step count. A variable step on a 120 Hz screen would triple it over a thread's life, so the sim runs on a fixed step and frames without a step skip the GPU entirely.
- No full-resolution scene target. The strands are shaded twice per frame: 2×2-supersampled at quarter resolution for the bloom prefilter, and at full resolution inside the composite. That removes a 30 MB HDR target written and read every frame, and the bloom pyramid starts at a quarter of the canvas.
- Restraint on both themes. Dark: the threads are dim chrome bodies with bright crests, and a soft-knee, Karis-averaged dual-filter bloom adds the halo without flickering on thin lines. Light: graphite and pewter with a white gloss; fading threads thin out toward the paper instead of darkening, and the bloom pyramid's coverage channel, offset from the light, becomes a multi-scale cast shadow. The threads dim behind the hero copy (the text guard) so the words stay readable. Both themes are dithered, so neither shows banding.
- A resize keeps the picture. When the window or a phone changes shape, the dye and the flow are copied into the new targets in one bilinear pass each, keeping height units and centring the frame, and the brush and its path move with them. The threads survive, and the next stroke continues without a jump.
Parameters
Presets
Performance & compatibility
- Measured (verification harness, Apple M5, Chrome, 2560×1440 backing, default preset, sim 128, dye 1024): 60 fps during the fast sweep in every run, JS 0.1 ms. GPU per frame: 3.4–4.1 ms mean and 4.7–9.0 ms p95 on the quieter harness runs; the GPU was shared with up to three other pages under test that day, and contended runs read 6–11 ms mean. The previous build measured 3.7/6.0 ms on a quiet run; this one adds a little composite ALU (strands ordered by freshness, thread highlights). Earlier A/B runs (switching one stage off every 0.3 s) put the sim step at about 2–3 ms, the strand shading (composite plus bloom prefilter) at about 1.7 ms and the bloom chain at under 1 ms.
- What scales it: on this GPU each small pass costs about 0.15–0.25 ms whatever its size, so the pass count matters most: 16 sim passes per step (the fused red-black sweeps halved them) and 8 per rendered frame. After that, canvas pixels (the full-resolution shading in the composite) and dye resolution (the two MacCormack passes). Every full-target pass invalidates its framebuffer first, so tile-based GPUs (Apple, mobile) skip reloading it. Splats draw only the stroke's bounding box. GPU memory is about 50 MB at 2560×1440: dye ×3 at 45 MB, bloom at 5 MB, sim under 1 MB. A resize briefly holds the old dye beside the new one while it is copied over.
- Requirements: WebGL2 with
EXT_color_buffer_float(16-bit float render targets). Without them the page shows a message over the poster frame. - Mobile: a 390 px phone (DPR capped at 2) gets a 780×1688 canvas. On coarse pointers the dye is capped at 640 (640×1385), the pressure and viscosity solves run one sweep fewer, the wake speed cap drops to 1.2 h/s, the turn-drop comes earlier and corrugated patches dissolve sooner; on portrait screens the bundle (spacing, thickness, wake, lag, clear zone) shrinks with the aspect, to 60% on a phone, because the height unit is large next to the width. The harness touch drag ran at 53 fps (45 fps with two other pages under test). Touch drags draw threads, and the canvas sits behind the page content, so it never blocks taps. Thread width has a floor of 1.3 dye texels, so small canvases and the gallery thumbnail (DPR 1, dye ≤ 512) stay resolvable.
- First paint and intro: the first 0.4 s of the ghost stroke is simulated before the first paint, so the page opens on a bundle already in flight; the 4.4 s intro then sweeps an S over the top, ties a loop and slows to a stop, away from the copy. A preset switch clears the dye and the flow, so every look starts fresh.
- Reduced motion: a still frame built from the pre-roll plus 2.5 s of ghost-pointer simulation, with a Play anyway button. The loop stops 7 s after the last movement (measured 7.08 s), by which time every thread has faded (about 5 s at the default fade).
References & credits
- Jos Stam, “Stable Fluids”, SIGGRAPH 1999. dgp.toronto.edu. Paper.
- Mark Harris, “Fast Fluid Dynamics Simulation on the GPU”, GPU Gems ch. 38. developer.nvidia.com. Book text, free online.
- Fedkiw, Stam & Jensen, “Visual Simulation of Smoke”, 2001 (vorticity confinement). stanford.edu. Paper.
- Selle, Fedkiw, Kim, Liu & Rossignac, “An Unconditionally Stable MacCormack Method”, J. Sci. Comput. 2008. gatech.edu. Paper (the limiter).
- Crane, Llamas & Tariq, “Real-Time Simulation and Rendering of 3D Fluids”, GPU Gems 3 ch. 30. developer.nvidia.com. Book text (MacCormack on the GPU).
- Sigg & Hadwiger, “Fast Third-Order Texture Filtering”, GPU Gems 2 ch. 20. developer.nvidia.com. Book text (B-spline sampling).
- Pavel Dobryakov, WebGL-Fluid-Simulation. github.com/PavelDoGreat. MIT. The reference for the soft-knee bloom pyramid and the stable-fluids pass structure on WebGL. No code copied.
- Jorge Jimenez, “Next Generation Post Processing in Call of Duty: Advanced Warfare”, SIGGRAPH 2014 Advances in Real-Time Rendering. iryoku.com. Talk slides (Karis average, interleaved gradient noise).
- Marius Bjørge, “Bandwidth-Efficient Rendering”, SIGGRAPH 2015 Moving Mobile Graphics. community.arm.com. Talk notes (dual-filter blur).
- Look benchmark: the servicetitan.com/max hero, which is Kevin Levron's
TubesCursor(threejs-components, ISC). This page is a fluid-native reinterpretation; no code or assets from it are used. - Research notes and measurements:
research/eulerian-fluids.mdin this library. Everything on this page is implemented from scratch for the FX library.