Liquid Physics

Page

You can find a playable version at the bottom of this tutorial.

 

Introduction:

Unity 3D is great for a ton of things, however there is a particular problem which hasnt been covered properly and that is Liquids Physics.

I personally think its due to the fact liquid physics are usually complex to implement, and regularly expensive for a game, in my research the only liquid physics package i found was Fluvio to be honest i havent used it yet, but i think i found a simplier solution!

In this tutorial i will show you how to implement your own water particles that will run in pretty much any device (yes on mobiles too) .

I can pretty much guarantee this will be the simplier and maybe the least expensive way to implement them, but not the most realistic. Also if you know how to code and use rigidbodies collisions in unity you are all set to do some crazy experiments with them.

I recommend before starting the tutorial you download the example project and try to recreate it in a new scene with the same assets so you get the idea more clearly.

PreRequisites:

  • Knowledge of Rigidbodies in Unity
  • What a shader is, and how to use them in Unity 3D.
  • Unity 5.X or Unity 4.X PRO. (For the Render Textures)

 

Metaballs:

Ok, so if you have done your homework you have proably found something about this guys, if you havent there are a couple of fantastic  tutorials on how do they work and what are them.

So the first step is to understand how a metaball shader works, for this you can take a look at my shader code below:

Link to shader

If you notice, the last line :

color=floor(color/0.1)*0.5;

Its the one that makes the magic in the metaballs and the one that makes the effect of the blobs merging between them.

Without the floor function.

With the floor function.

Metaballs in Unity:

Great! So we know what a metaball is! But how will this help us in Unity?

Well first things first, we have to port our GLSL code to CG for unity to understand it.

The most important part of the shader is the fragment:

half4 frag (v2f i) : COLOR{
half4 texcol,finalColor;
texcol = tex2D (_MainTex, i.uv);
finalColor=half4(0.0,texcol.g/2.0,texcol.b,texcol.a);
if(finalColor.a>0.2){
finalColor.a=0.5;
finalColor.b=floor((finalColor.b/0.1)*0.5);
}
return finalColor;
}

The complete shader is inside the project in the link at the bottom.

I  you noticed, i am using a texture to take away the color for the shader instead of defining each blob inside it, this is due to the fact that we dont need to compute every blob for a point because we got the magic of Photoshop to do the work for us.

So lets create a image that looks exactly like a white blob, and apply that texture to a plane in Unity:

_Tut1_3_Liquid_Texture

Great! So the next step is to use our shader, for that select it on the material menu under Custom/Metaballs:

The result Should be something like this:

 

Ok so it looks pretty cool, but how will this simulate liquids? Well its pretty much done by now there is just one last trick that makes the whole difference.

But before we get to the final trick lets finish setting up our Water Drops.

Our “Water Drops” are going to be using sphere colliders with rigidbodies, you can setup the friction or density using any tutorial you found about rigidbody configurations.

To make our Water Drops look merged you have to make sure the collider doesn’t cover all the image:

Notice the size of the collider!

Notice the size of the collider!

In this way, when the spheres are close to each other it will look as if they were merged.

Pretty neat huh? I hope you dont feel scammed right now, i promise it will look like in the demo at the top in the end!

If you have noticed, what the shader does is simply merge colors with white intensities, so what we have to do is to simulate the same thing that happened in the GLSL shader above, for that we are going to need…

 

Render Textures:

What is a render texture? Well its pretty simple, its the output of a camera into a texture.

I can think of a better example other than this.

And how will this help us out?  Well this is the neat trick, a Render texture behaves like a Regular texture, so we can place any shader in it, if we apply our metaball shader to this, if our Camera sees something white it will marge its colors as our experiment above.

Howeve how are we going to prevent any white of our game to be transformed? For that we are going to need 2 Cameras, the first one looking to our Regular scene, and the other only looking to our Metaball spheres.

Lets start by creating a Liquids Layer:

_Tut1_12_newLayer       _Tut1_13_createLayer

Now create a render texture from the menu:

_Tut1_5_RenderTexture_create

Now Duplicate your Camera and after that drag and drop your Render Texture to one of them (The camera whith the culling mask of Liquids only)

Setup your Culling Mask as the picture below:


_Tut1_11_shaderSelect

Great! Now create another plane and set its texture to our new Render Texture.

_Tut1_6_RenderTexture_apply2

Now, if we add some of our Water Drops in the Scene and press play the result should be something like this:

Notice how the camera on the bottom cant see the spheres!

Finally you only need to place the plane over your scene, synchronizedwith the spheres on your scene camera:

Thats it! after this setup you can easily use the spheres inside your game mechanics as any other spheres and they will look preety cool, i hope you liked the tutorial and the method described.

Remember you can download the full project below and if something is not clear please let me know in te comments section!

Demo:

Use the keyboard Arrows to move the fans.

Press 1 2 or 3 to change between examples.

 

 

Before downloading:

If you find my tutorials useful please consider helping me, you can do this by letting me know about cleaver things you do with it so i can post them as reference on this site, or by donating.

Also if you are interested in a particular problem let me know so i can keep it in mind for my next turorial.

Download the full project!

Previous Releases

First Release, basic water
Second Release, Color merging