WebGPU fluid simulation using Smoothed Particle Hydrodynamics (SPH) and PIC/FLIP methods.
This project is ported from Sebastian Lague's Fluid-Sim Unity project and David Li's Fluid Particles WebGL project. It explores various rendering and simulation techniques for real-time fluids in the browser, leveraging the power of WebGPU for both compute-heavy physics and advanced visualization pipelines.
- GPU-Accelerated Physics: SPH calculations (Density, Pressure, Viscosity) and PIC/FLIP grid-based solving run entirely in WebGPU compute shaders.
- Efficient Spatial Sorting: Uses a parallel Prefix-Sum and Linear Grid approach to handle tens of thousands of particles at 60 FPS.
- Diverse Renderers: From simple billboards to complex screen-space refraction and volumetric raymarching.
- Interactive Simulation: Real-time particle interaction (push/pull), dynamic obstacle manipulation, and smoothly interpolating bounding boxes.
- Cross-Platform UI: Responsive settings panel with Material Icons and performance statistics.
fluid-2d.mp4
- Canvas 2D: A baseline implementation using the standard HTML5 Canvas API.
- WebGPU 2D: A 2D simulation utilizing WebGPU compute shaders for physics and a custom render pipeline.
fluid-3d.mp4
The project features several specialized 3D rendering techniques and simulation methods:
Renders fluid particles as camera-facing quads.
- Techniques: Vertex pulling, indirect instanced rendering, and frustum culling.
- Shading: Features dynamic shadow mapping and velocity-based color gradients.
Extracts a polygonal mesh from the fluid density field in real-time.
- Techniques: GPU-based Marching Cubes algorithm, density probing, and hardware-accelerated mesh rendering.
- Visuals: Classic "metaball" look with a solid, reflective surface.
Visualizes the fluid by marching rays through a density field.
- Techniques: Signed Distance Fields (SDF) approximation from particles, volumetric lighting, and refraction.
- Visuals: Provides a thick, jelly-like or deep-water aesthetic with realistic light extinction.
A high-end rendering pipeline that treats fluid as a continuous surface.
- Pipeline:
- Depth Pass: Renders particle depth to a texture.
- Smoothing Pass: Applies a bilateral blur to remove "bumpy" particle artifacts.
- Thickness Pass: Calculates fluid volume for light absorption.
- Normal Pass: Reconstructs surface normals from the depth buffer.
- Foam Simulation: Advects foam particles based on trapped air and kinetic energy.
- Composite: Final shading with refraction, Fresnel effects, and shadows.
A high-performance PIC/FLIP hybrid simulation with dynamic boundaries.
flip-fluid.mp4
- Techniques: Hybrid particle-grid solver using a staggered MAC grid for pressure projection.
- Red-Black Gauss-Seidel Solver: Optimized pressure solver that converges ~2x faster than standard Jacobi iteration by updating cells in alternating parity phases.
- Dynamic Boundaries: Features a smoothly interpolating bounding box that reacts to physics in real-time.
- Visual Decoupling: Particle rendering radius is independent of physical spacing, allowing for highly customizable visual densities.
The main entry point that allows hot-swapping between all 3D renderers while preserving the simulation state (gravity, bounds, particle positions).
- A browser with WebGPU support (Chrome 113+, Edge 113+, or Firefox Nightly with flags).
- Node.js (v20.19.0 or higher recommended).
# Clone the repository
git clone https://github.com/jeantimex/fluid.git
# Install dependencies
npm install# Start the Vite development server
npm run dev# Build the project
npm run build- Inspired by Sebastian Lague's "Coding Adventure: Simulating Fluids" and "Rendering Fluids".
- Based on the Smoothed Particle Hydrodynamics (SPH) formulation for incompressible flow and the PIC/FLIP hybrid method.
- Heavily influenced by David Li's work on real-time WebGL fluid simulations.
This project is licensed under the MIT License - see the LICENSE file for details.