← Thu, Nov 5 digest

Spreading Liquid with Three.js

read article ↗ hn thread ↗

A developer details how they built a pragmatic liquid spreading and mixing simulation using Three.js fragment shaders, height maps, and backtracing.

read it A thorough technical breakdown of faking fluid dynamics and surface shading efficiently in WebGL.

summary

A developer shares the implementation details behind a web project simulating spreading and mixing liquid foundation. Rather than a physically accurate model, the effect combines velocity maps, height maps for fake 3D normals, and alpha history maps to leave realistic traces. The author breaks down the custom fragment shaders used for backtracing, texture precision trade-offs, and color mixing. This write-up offers a pragmatic look at prioritizing visual appeal over strict volume conservation in WebGL.

key points

  1. The simulation trades strict volume conservation for performance, using velocity backtracing on height maps to approximate fluid movement visually.
  2. An alpha history map records maximum thickness over time to leave paint-like traces as the liquid spreads and thins out.
  3. Surface normals are computed dynamically from immediate height map neighbors in fragment shaders, driving Matcap highlights without vertex geometry deformation.

who should read it

Frontend developers and graphics programmers interested in WebGL shader effects.

discussion warm

consensus

Commenters praise the satisfying visual effect and color mixing, while noting the predictable quirk where non-conservative volume causes fluid to grow or shrink during circular motions.

pushback

A few users debate the author's melancholy closing thought on documenting techniques before AI masters them, while another questions if Three.js was strictly necessary.

best comment by andai ↗

This is neat. It doesn't seem to conserve volume. If you spread the blob "inward", all the fluid disappears and you get a tiny thin spot.

That's a general problem I often run into with simulations. When you get it in the opposite direction it can have some very amusing results (e.g. energy in a physical system continues to increase).

It directly tests the simulation's non-conservative volume behavior by pointing out how spreading fluid inward causes it to disappear.

Generated by an LLM from the article and thread. Check the sources before relying on it.