How to implement a Fluid Simulation on the CPU with Unity (ECS/Job System)
In summer 2018, while I was working at Atomic Raccoon (an Indie Game Studio in Paris), we were wondering how to run a simulation with many characters interacting with each others like a fluid.
Unity had released the Entity Component System (ECS), the Job System, and the Burst compiler a few months back. They advertised high speed, with an up to x100 performance boost.
I suggested that we could implement a fluid simulation with those new tools. This would allow us to familiarize with this new way of coding by prototyping a small physic engine. I knew some ways existed to run a fluid simulation on the GPU, so it’s already parallelizable.
I started looking in which physic simulation I could try to implement, and I found the Soft Particle Hydrodynamic (SPH) algorithms.
Before implementing it though, I looked at how Unity made their boid (or “fish”) demo, which shows multiple fishes interacting with each other. I watched multiple presentations on the ECS and the Job System and looked into this project to understand how it all works.
This post is more about how to port something to the ECS and Job system rather than how to actually implement SPH in Unity. Though, I talk about how I optimized some parts of the algorithm even further than parallelization. The source code is available here: https://github.com/leonardo-montes/Unity-ECS-Job-System-SPH.