High-Performance Fluid Dynamics
This engine achieves a consistent 75 FPS by offloading heavy numerical analysis to WebAssembly (WASM). By compiling Rust to WASM, the simulation executes at near-native speeds, allowing the browser to process the thousands of linear operations required for every single.
Computational Load
On a typical 128 × 128 grid, the solver manages over 16,000 cells. With 20 iterations per project and diffuse step to ensure numerical stability, the Rust core performs over 1.3 million calculations per frame.
Rendering Pipeline
Instead of slow DOM updates, the density field is passed as a Raw Float32 Array directly to a WebGL Fragment Shader. This allows for per-pixel interpolation and hardware-accelerated color mapping at zero CPU cost.
The simulation solves the Navier-Stokes equations for incompressible flow. The mathematical core relies on four primary stages executed in a tight loop:
- Diffusion: Uses the Gauss-Seidel iterative method to solve the heat equation, preventing the fluid from becoming a "checkerboard" of isolated values.
- Advection: Implements a Linear Back-Trace technique to move density through the velocity field, maintaining fluid momentum while minimizing numerical dissipation.
- Incompressible Projection: A critical step that solves a Poisson Equation to force the velocity field to be mass-conserving, creating realistic vortices and swirls.
- WASM-to-GPU Bridge: Velocity and density data are updated in Rust memory and mapped to a WebGL texture buffer, bypassing the overhead of JavaScript's garbage collector.
Interactive: Move the mouse around the canvas to inject momentum.