An Opensource, GPU-Accelerated, Particle-Based, Multi-phase, Real-time Fluid Simulation Engine for Unity and a game which showcases its features.
This is the ULTIMATE FREE 2D fluid simulation engine for Unity. Experience realtime interactive physics and fluid simulation in a neat package you can import and use in your own projects! Got any neat ideas? Feel free to alter the codebase and contribute as you see fit!
Source Code:
https://github.com/Victor2266/The-Fluid-Toy
👥 Team Members
- Victor Do
- Davis Cheung
- Cameron Tuffner-Lyons
- JJ Marr
🕹 Download and Play Demo Here:
🖥️💻 Supported platforms
- Windows
- Mac
- Linux
- Android
- WebGPU
🚀 Key Features
🧊 Collision Systems
Integrates with Unity’s GameObject and Rigidbody systems.
- Box Colliders:
- Combine multiple static or dynamic oriented boxes to create complex shapes (e.g., pinwheels)
- These are controlled by the CPU and sent to the GPU for particle-obstacle interactions
- Circle Colliders:
- Small colliders for particles: GPU-accelerated using uniform spatial subdivision hashing grids
- Large colliders: CPU-managed to prevent GPU performance degradation
- Complex Colliders (Not Implemented):
- Texture-based density maps for complex shapes
- Runtime-alterable collider geometry via alpha threshold sampling
- Player-editable collider deformation through brush tools
- Dynamically changing boundary size:
- Capability to resize the simulation boundary for any window size at any time
⚡ Dynamic Particle Management
- Activation/Deactivation:
- Particle structs with enable flags for conditional processing
- Memory-optimized allocation using pre-allocated buffers
- Draw Brush which uses interlocked add operation to avoid race condition
- Eraser Brush which randomizes particle position for even density distribution
- Source/Drain Objects:
- Source: Spawns particles with configurable initial velocities
- Drain: Disables particles using spatial triggers
- Automatic buffer management with particle recycling
- Multi-Fluid Support:
- Data-oriented design with fluid property tables
- 64px×1px gradient textures per fluid type stitched into 2D atlas
- Shader-driven visual differentiation using dynamic branching
🌐 Simulation Properties
- All States of Matter:
- Gasses
- Liquids
- Powders
- Solids
- Edge Behavior Modes:
- Solid: Acts as immovable wall (default)
- Void: Disables particles and randomizes positions
- Position randomization avoids spatial hash collisions in void mode
- Loop: Warps particles to opposite boundary
- Gravity Behavior Modes:
- Normal
- Reversed
- Radial
- Left
- Right
- Zero
- Temperature Simulation:
- Particles have individual temperatures
- When close together, particle temperatures will diffuse and reach equilibrium temp at a rate determined by the particle type’s diffusivity
- Certain particle types will lose temperature to the ambient environment based on entropy values
- Temperature is used for certain visual shaders such as for Lava and Fire.
- Thermal Boxes will heat/cool particles which touch them to a set temperature, the speed can be controlled by the box’s thermal conductivity.
- State Change Behaviors:
- Particles can change states after reaching temperture thresholds
🎮 Game Systems
- Fluid Density Detection:
- AsyncGPUReadback for non-blocking particle position queries
- Spatial density calculations for level event triggers
- Temperature Detection:
- AsyncGPUReadback for non-blocking particle temp queries
- Spatial temperature calculations for level event triggers
- Audio System:
- Dual-channel mixer with independent SFX/music control
- Progression System:
- Time-based star ratings (1-3 stars per level)
- Cross-platform save data in OS-specific registries
🖥️ UI/UX Features
- Menu System:
- Main menu with Play, Sandbox, Settings, and Quit
- Level selection screen with progress visualization
- Pause menu with settings adjustments for resolution, framerate and sound
- Contextual Tooltips:
- Hover-sensitive help system
- Mobile-optimized touch-and-hold activation
- Graphics Settings:
- Resolution, refresh rate, and fullscreen controls
- Developer-level unlock shortcuts
- Cross-Platform Controls:
- Unified input system for mouse/touchscreen
- Contextual UI trays with brush/level controls
🎮 Game Content
Sandbox Mode
- Dual Purpose:
- Player can freely play and experiment with all fluid and obstacle types
- Performance benchmarking environment
- Continuous Integration: Always updated with latest features
Levels 1-10
- Each level is a demonstration of a different mechanic:
1. Tavern Trouble
- Objective: Fill a beer mug using gravity manipulation
- Async particle position readback for level completion detection
2. Carnival Chaos
- Objective: Shoot a moving target with a water gun
- Introduction to source objects
3. Lava Logic
- Objective: Mix lava to reach desire temperture
- Introduction to Temperture Simulation
4. Factory Fun
- Objective: Open gates and gas valves to move water through pipes
- Introduction to Thermal Boxes and obstacle movement
5. Planetary Devastation
- Objective: Activate Laser Weapons to Destroy Planet
- Introduction to Radial Gravity, Laser Beams, and Volatile Particles
6. Forged in Fire
- Objective: Activate pour molten lava to cast a sword
- Introduction to State Changes
7. Bartending Simulator
- Objective: Fulfill orders as a bartender
- Introduction to fluid containers / composite colliders
8. Pipes Puzzle
- Objective: Rearrange the pipes to fill containers with water
- Introduction to pressure and flow as game mechanics
9. Ultimate Battle
- Objective: Defeat Opponent in a Clash of Power
- Introduction to particle collisions as a game mechanic
⚙️ Technical Highlights
CPU-GPU Architecture
- Memory Bridges:
- Compute buffers for particle data transfer
- Structured buffers for collider information
- Constant buffers for simulation setting properties
- Command Execution:
- ComputeShader.Dispatch for kernel launches
- AsyncGPUReadback for non-blocking data retrieval
- Graphics.DrawMeshInstancedIndirect for rendering
Compute Shader Optimization
- Dispatch Strategy:
- Thread groups sized to GPU wavefront (32-64 threads)
- Particle property tables loaded per-threadgroup
- Thread Synchronization:
- InterlockedAdd for controlled particle spawning
- Position randomization seed generated using frame number and atomic counter value
Visual Pipeline
- Dual-pass rendering with custom alpha blending
- Branching Vertex/fragment shaders for each visual-type (Velocity Coloured, Fuzzy Outline, Temperture Coloured, Glowing, etc)
- Precomputed texture atlas for fluid colour gradients
- Custom Post Processing Effects for Additional Bloom and Screen Distortion Effects
Hybrid Workload Distribution
- CPU Responsibilities:
- Large obstacle collider transformations
- Game state management
- UI/input processing
- GPU Pipeline:
- SPH fluid solver (with density/pressure/viscosity/friction/temperture kernels)
- Spatial hashing for neighbor detection
- Collision resolution for fluid particles
- Async particle buffer feedback for fluid property sensors
🙏 Acknowledgments
- Special thanks to Sebastian Lague for inspirational fluid simulation content.
- Thanks to The Powder Toy for physics sim inspiration.
– Testing: Validated through family/friend playtests
Note: Screenshots have been omitted for brevity. More visual documentation is available in development whitepapers.