Unity Fluid
Position Based Dynamics
This fluid sim runs entirely on the GPU using Unity Compute Shaders, implementing the method from this research paper. The current implementation uses a Bitonic sort for spatial hashing, though other parallel sorting algorithms could potentially offer better performance.
GPU Handles Collisions
Since the fluid system runs on the GPU, we have to represent physical objects as a set of particles on the GPU. I then just made a script that checks if an object is moving, if it is, we update the fluid collision particle positions.
Collision example
We have to take our “boundary particles” compute buffer (commonly refferred to as our fluid domain) and add our collision particles. Here is a simple script that does that.
|
|
The NumParticles
property determines the existing boundary particle count:
public int NumParticles { get; private set; }
.
We need to keep track of this value to ensure we aren’t increasing or decreasing the buffer more than we need to.
Currently there isn’t any voxelization of colliders, it simply computes the collider size by the bounding box of the GameObject.
Planned features
I want to eventually add voxelization to this, as well as improve the performance. I have some ideas involving chunking our fluid domain so we could potentially run a lot of particles (>100k).