Skip to content

Deniz-ARAS/GPU-Fluid-Sim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 

Repository files navigation

Unity Compute Shader

Real-Time 2D GPU Fluid Simulation in Unity

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

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

Features

  • 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
FluidSim_Vortex Sim

Technical Overview

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:

  1. Add external forces and density (Input)
  2. Advect velocity
  3. Apply gravity
  4. Compute Curl
  5. Apply vorticity confinement
  6. Compute divergence
  7. Iteratively solve pressure
  8. Project velocity to reduce divergence
  9. Advect density
  10. Render the selected debug or density view
FluidSim_Vorticity

Debug Views

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
FluidSim_DEbugViews FluidSim_Collision

Controls

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

How to Run

  • 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

References

This project was inspired by and built while studying the following resources:

  • Mike Ash — Fluid Simulation for Dummies
  • Sebastian Lague — Coding Adventure: Simulating Smoke

Future Improvements

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

Project Status

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors