April 18, 2019

Fake fluid simulations in Unity - or how to make the water system from Sprinkle game

0 comments
To research water simulation models I read a PhD thesis called Large-Scale Water Simulation in Games. The author has been working with the water system in Cities: Skylines, so if you want to learn more about how that was done you should read the thesis. But in the beginning of the thesis, the author was discussing different water systems in games. One of the games mentioned was Sprinkle, which is a mobile game where you have a fire truck and your job is to extinguish fires. It looks like this:


I played the game several years ago before I was interested in water systems, but when I saw it now I asked myself "How did they made that water system?" And above all, how could they simulate a water system on a mobile device? To simulate realistic water systems, you have to simulate the Navier-Stokes equations which is taking up a lot of computer power, so you are never doing it in games - and especially not in a mobile game from 2011.

One of the developers has written a blog post on how they made the water system: Sprinkle behind the scenes, and there's also a GDC talk pdf: Constraint fluids in Sprinkle. So what Sprinkle is doing is that they simulate water by using 600 particles and each particle disappears after 3.5 seconds to not make the simulation go slower. To display the particles on the screen they are using a "dynamic particle mesh" which I have no idea what it is, but in the GDC talk they say it's "point splatting with a few tricks." To simulate the particles they are using the built-in physics system together with a simplified Navier-Stokes simulation system. So it looks like this behind the scenes (this is before they add the dynamic particle mesh, so you can clearly see the particles):


To recreate the basic idea I found an article which is implementing something similar in Unity: Liquid Physics. The author is using just Unity's built-in collision system to simulate water, lava, and air. To create the solid surface from the particles, each particle consists of a circle with a gradient circle attached to it. The gradient gets a color (red, green, or blue), and then a render texture is used to capture the different colors:


...and then you use some shader tricks to generate a more solid surface that looks like the element being simulated. To change the type of particle to for example water, you experiment with the rigidbody settings to make something that feels like water. And in the end my version looks like this: