Unity Smoothed Particle Hydrodynamics Save

SPH in the Unity engine implemented in three different ways using MonoBehaviour, Entity-Component-System, and ComputeShader

Project README

Unity Smoothed Particle Hydrodynamics

This repository contains two Unity projects showcasing fluid simulations with the SPH method. There are three different implementations using:

  1. MonoBehaviour (single-threaded CPU)
  2. ComputeShader (GPU)
  3. Entity Component System (DOTS, multi-threaded CPU via JobSystem)

As spatial acceleration structure a uniform grid (as described by Simon Green) has been chosen. Particles are represented as uniformely sized spheres. Their size (= interaction radius) has been chosen with respect to the grid cell size (= 2x particle radius) so that one particle must only consider 8 cells in total for its neighbour search. There is no collision geometry involved (only boundary condition for simulation space; particles are guided only by SPH). The implementations are deliberately similar to ease understanding of the SPH method and its performance impact when implemented using different parallelisation techniques, namely the Entity-Component-System (ECS) and ComputeShaders.

Usage: Open one of the two unity projects. After restoring packages you may start the application in the editor (you can also build although note the difference of building for ECS via BuildConfiguration). The MonoBehaviour implementation is disabled by default as it shares a scene with the ComputeShader implementation that is enabled by default (see "MonoBehaviour CPU SPH" and "ComputeShader GPU SPH" gameobjects in "SPH" scene). You can influence simulation parameters and view debug information on the gameobjects' SPH management scripts (ParticleManager.cs for MonoBehaviour implementation and ComputeShaderParticleManager.cs for ComputeShader implementation). Be careful with the MonoBehaviour implementaion's simulation parameters, as you can easily freeze your unity editor if you want to simulate too many particles. The ECS project does not have any way to change and view simulation parameters in the editor. Therefore, you can only influence the simulation by editing the SPHSystem.cs file. Also be careful when changing the particle interaction radius and grid cell size because it could break the neighbour search.

Known limitations:

  • Due to the hashgrid's limitation of having a maximum number of particles per cell, the simulation may become unstable if more particles than that occupy a cell's space. See issue #1 for more information.

Knowledge and inspiration for the code in this repository was taken from:

https://user-images.githubusercontent.com/34295773/138610298-1851bf02-e4a4-4335-a02d-151825802617.mp4

sensibleConfig

FYI: If you ever wondered just how big the difference between (parallel-)CPU and GPU implementations is, here is a plot measuring the average frametimes over the number of particles for each implementation on a system with an AMD Ryzen 3600 and NVIDIA GeForce RTX 2060 Super:

AvgFrametimeBase_System2

Open Source Agenda is not affiliated with "Unity Smoothed Particle Hydrodynamics" Project. README Source: Gornhoth/Unity-Smoothed-Particle-Hydrodynamics

Open Source Agenda Badge

Open Source Agenda Rating