A real-time 2D fluid simulation built in Unity using compute shaders.

- Playable build: https://denizin.itch.io/gpu-fluid-sim
- GPU Fluid Simulation Showcase Video: https://youtu.be/ddDL2ACR0iU
- GPU Fluid Interface Showcase Video: https://youtu.be/019dr-vicb0
The project simulates smoke-like fluid behavior on the GPU with obstacle interaction, vorticity confinement, gravity, debug views, and multiple emitter types.
On my test machine, the GPU simulation/render pass measures around 0.28 ms in Unity's GPU timing tools.
This number depends on simulation resolution, pressure iteration count, debug view, and GPU hardware.
My Specs:
- NVIDIA Geforce GTX 4070
- Intel 13th Gen i5-13400
- 32GB Ram 3200 MT/s
- Real-time 2D fluid simulation on the GPU
- Unity compute shader implementation
- Density and velocity advection
- Pressure projection using iterative pressure solving
- Vorticity confinement for stronger swirling motion
- Gravity support
- Obstacle drawing and obstacle collision
- Multiple emitter types:
- Point fountain
- Line emitter
- Vortex emitter
- Directional burst emitters
- Debug visualization modes:
- Density
- Velocity magnitude
- Velocity direction
- Divergence
- Pressure
- Obstacle mask
- Runtime performance display with FPS and simulation timing
The simulation is based on a grid-based fluid solver inspired by stable fluids.
Most simulation steps are executed on the GPU through Unity compute shaders.
The main simulation loop includes:
- Add external forces and density (Input)
- Advect velocity
- Apply gravity
- Compute Curl
- Apply vorticity confinement
- Compute divergence
- Iteratively solve pressure
- Project velocity to reduce divergence
- Advect density
- Render the selected debug or density view
The project includes several debug visualization modes to make the simulation easier to inspect:
| View Mode | Purpose |
|---|---|
| Density | Shows the visible smoke/fluid density |
| Velocity Magnitude | Shows the strength of the velocity field |
| Velocity Direction | Shows the direction of the velocity field |
| Divergence | Shows areas where the velocity field is not divergence-free |
| Pressure | Shows the pressure solve result |
| Obstacle | Shows the obstacle mask used by the solver |
| Input | Action |
|---|---|
| Left Mouse | Add density / velocity |
| Right Mouse | Draw obstacles |
| Middle Mouse | Clear obstacles |
| Q/W/E/R/A/S/D/F/G/Z/X/C/V | Trigger showcase emitters |
| 1-2-3-4-5-6-7 | Switch between debug views |
- Clone the repository.
- Open the project in Unity.
- Use Unity version 6000.4.3f1 or newer.
- Open the 'Fluid2D_GPU' scene.
- Enter Play Mode.
Also you can find the executable of the project on my itch.io page https://denizin.itch.io/gpu-fluid-sim
This project was inspired by and built while studying the following resources:
- Mike Ash — Fluid Simulation for Dummies
- Sebastian Lague — Coding Adventure: Simulating Smoke
Possible future improvements include:
- Moving more emitter logic fully to the GPU
- Adding higher-quality fluid rendering
- Adding color ramp controls
- Adding different Fluids and Fluid mixing
- Improving pressure solve performance
- Expanding the project into 3D fluid simulation experiments
The repository also includes a CPU fluid simulation prototype based on Mike Ash's Fluid Simulation for Dummies. It was used as a learning/reference implementation before moving the main simulation pipeline to the GPU.