跪拜 Guibai
← All articles
WebGL · Three.js

Faking Fluid: How Lusion's Screen Effect Runs on a Single Velocity Texture, Recreated in Three.js

By 西伯利亚小火龙 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Interactive screens that read as fluid simulation no longer need a physics solver: a 1/8-resolution velocity texture and a 9-tap distortion pass produce the same visual on low-end GPUs. That makes this a practical pattern for marketing pages, hero banners, and any WebGL scene where realism comes from clever sampling rather than expensive math.

Summary

Lusion's screen fluid effect, where mouse strokes bend, smear, and rainbow-tint a background, runs without any fluid solver. A single 8-bit RGBA texture stores velocity and two ink weights; the scene is sampled nine times along that velocity field to fake refraction and chromatic aberration. The recreation, written in Three.js, keeps the simulation at 1/4 resolution and the velocity field at 1/8, with semi-Lagrangian advection carrying ink each frame and a procedural base of thin concentric rings that make the displacement visible.

The post walks through the full pipeline: velocity channels encoded with a +0.5 offset, brush painting as a line segment with radius mapped to pointer speed, a 9-tap Gaussian blur on the downsampled velocity field, and a distortion pass that adds phase-offset sine stripes for the rainbow edge. After the mouse stops, a 60ms-to-200ms idle logic shuts off external push and lets the ink spread via gradient diffusion, capped by a CFL condition of 0.75 texels per frame. Six practical pitfalls are spelled out, including the 8-bit quantization dead zone that requires a minimum per-frame decay, ping-pong RT swapping, coordinate-system mismatches, dt clamping, resize resets, and linear color-space clears.

Takeaways
— Velocity field is stored in an RGBA texture: R and G hold velocity x/y + 0.5, B and A hold two ink weights.
— Simulation textures run at 1/4 screen resolution; the velocity field is downsampled and blurred to 1/8 resolution.
— Semi-Lagrangian advection traces each pixel backwards along the inverse velocity to sample the previous frame; push strength is 25.
— Per-frame decay coefficients: velocity 0.975, weight1 0.95, weight2 0.8.
— Mouse brush is a line segment with radius mapped from pointer speed (max radius = max(40, viewport width/20)).
— Refraction pass samples the rendered scene 9 times along the velocity direction and averages; parameters amount=3, multiplier=5, rgbShift=0.5, colorMultiplier=10, shade=1.25.
— Phase-offset sine stripes (0,2,4) produce chromatic aberration, concentrated at the brush edge via smoothstep(0.4, -0.9, weight).
— After 60ms of idle, the external push is scaled off over 140ms; ink then spreads via gradient diffusion, capped at 0.75 texels per frame (CFL).
— 8-bit texture clears must use linear (0.5,0.5,0,0) to avoid sRGB conversion.
— Pitfalls: 8-bit quantization dead zone requires min decay 0.004/frame; ping-pong RTs must be swapped each frame; NDC and CSS coordinate systems must not be mixed; dt clamped to 0.05s; resize resets mouse baseline; clearing RTs uses linear values.
Conclusions

The choice of a low-resolution 8-bit velocity field keeps the effect cheap enough for full-frame interactive use, but it forces the developer to solve quantization dead zones and downsampling artifacts; the tradeoff pays off on GPUs where memory bandwidth is scarce.

The illusion depends on the background: thin concentric rings amplify displacement and chromatic aberration, so the same velocity field on a plain color would be invisible. The base image is part of the effect, not just a decorative texture.

The idle behavior (spreading instead of sliding) is a key cue that makes the ink feel like liquid rather than a dragged sprite; without it, residual velocity would make the blob drift like a rigid body.

The whole approach is a post-processing chain that never touches geometry, which means it can be layered onto any existing scene and composed with other passes (Smaa, Fsr) without breaking the render loop.

Concepts & terms
Semi-Lagrangian advection
A numerical method for moving quantities along a velocity field: instead of pushing each particle forward, trace each pixel backward to the previous frame and sample that value, giving stable, low-cost transport.
Ping-pong rendering
A technique that swaps between two render targets each frame so the shader never reads and writes the same texture, which would be undefined behavior in WebGL.
CFL condition
The Courant–Friedrichs–Lewy stability limit for numerical advection; it caps how many texels a particle can move in one frame to keep the simulation from diverging.
Chromatic aberration
A color-fringing artifact where red, green, and blue channels shift by different amounts, here faked with phase-offset sine stripes to mimic the dispersion seen at brush edges.
From the discussion
Featured comments
组合搭配函数

Best of all is the line that the velocity field isn't computed — it's pushed. Take the whole trick apart and it's an 8-bit texture plus semi-Lagrangian advection, no wonder it runs at full framerate. The quantization dead-zone pitfall is described very concretely: after multiplying data by 0.95, a value smaller than 1/255 gets rounded back up to 1, turning the whole screen gray. You'd never anticipate that kind of problem without actually running it once. One question: the idle logic that starts easing off 60ms after release — on a touchscreen, could the instant the finger lifts be misjudged as stopping? Is there separate handling for touch?

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗