A high-performance Smoothed Particle Hydrodynamics (SPH) fluid simulator implemented in Unity, capable of simulating 10,000 particles at 30 FPS on modest hardware. Inspired by Sebastian Lague's tutorials and the scientific work in MDPI 2023: SPH Fluid Simulation.
shp2.mp4
Untitled.video.-.Made.with.Clipchamp.4.mp4
- Real-time SPH simulation of fluids with up to 10k particles
- Density, pressure, viscosity calculations per particle
- Spatial hashing for efficient neighbor search
- GPU-optimized instanced rendering with velocity-based color mapping
- Collision handling with world bounds and arbitrary colliders
- Icosphere-based particles for realistic visualization
This project implements SPH (Smoothed Particle Hydrodynamics) with a focus on accuracy and performance:
-
Particle System
FluidParticlestruct stores position, velocity, acceleration, density, and pressure.- Particles are integrated with explicit Euler integration each frame.
-
Density & Pressure Calculation
- Uses Poly6 kernel for smooth density computation.
- Pressure computed via Equation of State:
[ P_i = k \left( \left(\frac{\rho_i}{\rho_0}\right)^7 - 1 \right) ] - Ensures realistic fluid compressibility.
-
Force Computation
- Pressure forces via Spiky gradient kernel.
- Viscosity forces via Viscosity Laplacian kernel.
- Includes gravity and optional velocity damping.
-
Spatial Hashing
- Accelerates neighbor search using a 3D grid hash: [ hash = (x \cdot 73856093) \oplus (y \cdot 19349663) \oplus (z \cdot 83492791) ]
- Only checks nearby cells, drastically reducing complexity from O(n²) → O(n).
-
Boundary Handling
- World bounds handled via simple position reflection.
- Collider interaction uses closest-point approximation and reflective velocity.
- Box collisions approximate normal along the smallest penetration axis.
-
Visualization
- GPU instanced rendering for 10k particles at 30 FPS.
- Velocity-based coloring:
- Dark blue → Mint → Yellow → Red/Orange for speed mapping.
- Mesh: dynamically generated Icosphere for smooth spheres.
| Metric | Value |
|---|---|
| Particle Count | 10,000 |
| Frame Rate | 30 FPS |
| GPU | GTX 960 |
| CPU | i5-12600KF |
| RAM | 16 GB |
Achieves smooth simulation on mid-range hardware thanks to instanced rendering and spatial hashing.
- Unity C# for simulation & rendering
- Custom Shader for particle colors and lighting
- MaterialPropertyBlock batching for efficient GPU uploads
- Kernel functions implemented directly in C# for easy modification:
- Poly6, Spiky gradient, Viscosity Laplacian
- Inspired by Sebastian Lague SPH tutorials
- Research paper: MDPI Applied Sciences, 2023
- Clone the repository:
git clone https://github.com/yourusername/hydrodynamics-sph.git
Open in Unity 2021.3+
Assign particleMaterial and collision layers in HydrodynamicsManager.
Hit Play to run simulation.