Simple Liquid Simulation in Unity!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome i'm your code monkey let's check out how we can make the simplest form a fluid simulation in unity now this is a very complex topic and making 100 realistic fluids is pretty much bleeding edge research so what we're going to build here is the simplest most straightforward implementation possible so let's first think of what do we mean when we think of a fluid essentially it's meant to be formless shapeless like water so as you put water into a cup it becomes a cup as you put water into a bottle it becomes a bottle and if you put it in a teapot it becomes a teapot at the same time it also has physics so water doesn't go through walls and as you know water by itself is not a single solid thing but instead it is made up of billions or trillions of individual molecules so this is how we're going to simulate it we're just going to make tons of tiny physics particles and let the physics engine do its thing so here i am in my scene i just have a bunch of interesting shapes and let's begin by making our first singular water molecule so we just create an empty game object let's name this just the liquid particle then over here let's reset the transform and for the visual let's add a sprite render and for the sprite i'm going to use this simple circle so there it is just push it up there yep there you go just a tiny circle now let's send it in blue just to pretend like it's water okay next up we're going to use physics so let's add a circle collider so this is the shape of our particle and the default yep it already matches the sprite perfectly and finally we add a rigid body in this case a rigid body 2d so just with this we should have one physically accurate particle so if i press play it should fall down and run down here let's see and yep there's the tiny particle it falls down and rolls down so here we have a physically accurate particle all right so far so good however in my case it's falling a bit too slowly i would prefer if it was a bit faster so for that we can play around over here with the rigid body properties specifically you can play around with the mass and over here the gravity scale now obviously the scale that you use here will depend on the unity scale of the game that you're using in my case if i leave the mass at one input gravity scale at three it looks pretty good so yep now it falls a bit faster which i think it looks better now another thing we can add is a physics material so we can simply right click go into create and in here create a physics material on 2d let's name this no friction and here you see the two properties for the physics material and we can just set friction down to zero so this will make it so that the particle easily slides along so just select the particle and over here on the rigid body just drag that physics material and also here on the angular drag settings to zero so it has absolutely no drag anywhere so there it is now our particle is nicely slippery just like water on the other hand if you wanted to make some slimy goo you would perhaps increase the friction so it all depends on what liquid you're trying to simulate learn all about vr and ar with the patreon sponsor xr bootcamp it's a 6-8 week bootcamp taught by industry professionals learn how to interact in vr optimize your rendering and learn about dots check them out at xrbootcamp.com and use the coupon cm10 to get 10 off any of the master classes okay so far so good now like i said this is just a single particle so before we make a bunch let's turn into a prefab so just drag it right here into our folder yep now it's a pre-file and now just duplicate it tons of times all right so there it is i just duplicated and made tons of prefabs and if we run and if there they are they all fall down all right awesome so physically we have our scene working exactly as intended but visually this definitely does not look like water it looks like a bunch of different separate circles that are clearly separate from one another so even if i move it in order to make it fall down yep they fall down but again they are just particles they do not look like a liquid so let's solve that and the way we're going to do that is with a bit of visual trickery what we want to do is essentially merge the various particles when they are near one another so we want to fill in all of these tiny gaps and one way that we can do that is pretty much just blur the particles so they don't look like they're merged together and one way to do that is by rendering the particles into a separate texture so over here in my scene i've got my main camera there you go just a basic 2d camera and now i'm going to duplicate this let's name this one the fluid camera and on this one the tricky thing we're going to do is we're only going to render just the particles so we can do that by playing around with the layers so let's select our prefab we're going to go into the layers and we're going to add a new layer and in here let's add a layer and name it fluids then on the prefab let's make sure we set it into that layer and now we select first of all the original main camera and in here we go into the coloring mask and we make sure that this one renders everything except the fluids so if we look over here on the camera preview we do not see our prefabs and then we go into the fluid camera and on this one we only want to render the fluids so let's select first nothing and then just the fluids okay so far so good now we don't want to render this camera to our screen but instead we want to render it into a texture so let's go here create and we're going to create a new render texture here it is let's name this the fluid render texture and now up here on the size of the render texture we can pretty much automatically blur the particles so just use a small size and 256x256 sounds pretty good and just with that it will automatically have some blurriness so we've got this tiny texture in here and just select our fluid camera and on the output instead of outputting to the screen let's output into this texture as soon as we do and if we inspect the texture yep that's what it looks like so now we want to see this one in game so for that let's create a new object let's select a 3d object and select a flat quad now on this squad let's first scale it up by a bit and in here we want to render our render texture so let's make a material to render it let's go up here simply create a brand new material make it for the fluid and on the quad we're going to display that fluid material let's also get rid of the mesh collider and in here for the texture itself let's drag our render texture and there you go now this squad is showing what the second camera sees and by the way if you find the video helpful consider subscribing and hitting the like button it really helps out the channel so now we just need to make these match up so for that we can just actually take the quad and go all the way up there and in here we make it a child of the fluid camera then put it on x and y of zero and yep now it's positioned exactly and now in order to make sure that the size is correct we need to go and see how our camera is set up and over here you can see that it's an orthographic camera with a size of 50. now the 50 orthographic size is essentially half of the total vertical size so it means there's 50 units from here down to the center and 50 more from here down to here so the total from top to bottom is 100 so that's why we go into the quad and in here set the y into 100 and let's put the x also at 100. so if you don't like this enable and disable yep it is perfectly positioned and as you can see because of the low resolution of the texture it already has quite a bit of blurriness okay so far so good however this texture is meant to only show the particles and leave the rest of it transparent right now we can actually see a background as well as some post-crossing effects so you can see some vignette in there we don't want that so let's select this camera and first of all to get rid of the post processing just up here untick it yep there you go and now we have our solid color let's make sure that the background type is set to solid color and for the background color select black and then 0 on the alpha so with this it is correct however here it does not look correct that is because the material also needs to be transparent so let's select our fluid material and instead of making it opaque just make it transparent and yep there you go now it does work exactly as intended and just one final thing which is make sure that the quad is in front of the camera so right now if i go into the game view i can't see it that's because the quad is on a z of -159 so it's way behind the camera in my case my fluid camera is on his head of minus 10 so for the quad itself let's put it on pause 10. and yep there you go now it does show up okay so we can now run this and see how it looks like and yep it doesn't even look correct so we have all of our particles they are being rendered onto a blurry texture and then that texture is being overlaid on top so we have a main camera rendering the rest of the scene and then a second camera rendering just the particles however like this as you can see it obviously still doesn't look exactly very good so it still looks like individual particles now we want the shape as a home to be blurry but the visual is meant to be clear so for that we're going to make a simple custom shader let's simply right click and we're going to create we're going to create a new shader and let's make a new unload graph let's name it our fluid shader and just double click to open it okay so here we have our shader open up in shadow graph now i've covered a quick getting start guide to shadow graph so go watch that if you're not familiar with this tool essentially lets us very easily make some awesome shaders and one that we're going to make here is extremely simple the first thing we need is obviously a texture so let's add a new texture 2d let's name it our main text which is the standard name so for the reference underscore mean text okay then for the default we can actually select it so i've selected the fluid render texture and now we just simply grab the texture on here then we need to sample it so sample our texture 2d and just like this we can see what the camera sees now the trick for making our effect work is going to be using the step node i've already used this node previously when making the grass wind shader this is very useful what it does is it only grabs values above a certain threshold and ignores the rest so we take something with smooth values and clamps them to either one or zero so over here we have an edge and an input and on the input let's drag the alpha channel from our simple texture yep there it is so we have white where there is something in black where it's transparent and now up here if we modify the edge you can see roughly what happens in there so the lower there and lower this you can see that it becomes more white so this is essentially taking the blurry parts and either make them solid or completely transparent so the end result is that we take away the blurriness of the render texture and make a solid shape out of it so instead of modifying a value in here let's make a simple vector one let's name it the thickness and let's defound it 2.3 so just drag it in there and connect it onto that one okay so we have this and we can send this directly over to the alpha channel and here on the preview let's right click select the quad and then we're going to need the colors so for the color let's define another property for our color just name it our color let's default it to a watery blue okay and by the way let's also make it hdr just to have a bit more fun all right and we're going to add a multiply node and we simply multiply this one by our color okay so we have this and here we have our color output and just go in there alright so there we can see the preview we just have an issue with transparency so on the master node just click on the gear icon and in here on surface instead of opaque make it into transparent and on blend make sure you set the alpha and here we have our texture so if we go back and on the fluid material we just go into shadow graphs and use our shader and yep there it is now the gaps do get a bit more closed off and we can play around over here with the thickness so as we increase we get more gaps and if i put this in like 0.1 then it starts to become like solid shape now we can improve this a bit further so let's go into each individual particle and by the way here's a quick unity tip so right away i can see the particles because the quad is on top over here on the hierarchy on the left side we've got this button which essentially says we cannot touch this so now if i click it goes through the quad and there's another one just to hide it so that way i can easily see the particles all right now on the particle let's modify it a bit let's actually open the prefab to see it easily so instead of using a perfect circle let's swap this for a faded circle so here i have one just like this so there you go and now for division let's make it a bit bigger so 1.5 on each side and for the circumference size let's make it a bit smaller so something like point three so this way now the circle itself has a bit of fadeness which again that's what's going to help in order to merge the whole visual into one thing and just with this if we now try and yep there it is now it does indeed look like water so it no longer looks like a ton of individual particles now it doesn't look like a solid shape and if we try to see them in action if there you go they fall down and yep now it does start to look like a liquid and now here i have swapped out for a bit of a more complex shader so the logic is all still the same and yep now it does start to look a lot more like water so it does have a bit of foam on top so it looks quite nice so here is how the shade works and it's a bit more complex but it's really just following the exact same logic as previously up here i just add a bunch of noise in order to make the water feel like something moving so that's just the visual and the same thing that we had previously is what we have here so we grab the texture we sample it then we do a step node and multiply another color and so on and the only difference is over here for the phone so we just sample the texture but instead of sampling on the exact same position we just add a tiny offset so that means that the sample texture is a bit higher then we just do a subtract node and essentially that gives us the very edge on the top then just take that do a step node multiply by a certain foam collar and yep everything looks like that so here is the final effect and it does look pretty good you can download and inspect the final shader in the downloadable project phones but essentially it works exactly like the outline shader that i covered previously so just shifting the texture and doing a subtraction and here it is looking awesome so it's got some noise to make it seem like there's actual water rather than just a solid object and you've got a nice foam and everything still behaves exactly as you would expect now like i said this is the simplest way to do fluid simulation as you can tell the big issue with this is with regards to either accuracy or performance so the more particles that you use and the smaller that you make them the more accurate the simulation becomes but remember that these are all physics objects so the more you have the more it drags on performance so the system works great but do keep that in mind and don't go too crazy with extremely tiny particles and now we can just play around with the color in order to get some different liquids so if you test it in red we now have some lava if we tint it in green then we have some slimy goo or acid and yep there it is some very awesome simple fluid simulations we can use this exact same system to make a gas as opposed to a liquid the only thing you really need to do is just go up here onto the gravity scale instead of putting a positive value just put a negative value and yep there it is and now instead of a liquid you have a gas that floats upwards and again you can pretend that this is a different thing depending on the colors so if you make it in purple it looks like some toxic gas if you put it in black then it looks like some pollution and if you put it in white then it looks like some sort of steam so here is the final result we've got both liquids and gas and it all looks awesome so this is an excellent system to use whenever you need some very simple fluids stay tuned for an upcoming video where i will be using this to make a nice puzzle game as always you can download the project files and utilities from unitycodemonkey.com if you found this video helpful consider liking and subscribing this video is made possible thanks to these awesome supporters go to patreon.com unitycodemonkey to get some perks and help keep the videos free for everyone post any questions you have in comments and i'll see you next time [Music] you
Info
Channel: Code Monkey
Views: 52,166
Rating: undefined out of 5
Keywords: unity fluid simulation, unity liquid simulation, unity fluid tutorial, unity liquid physics, unity fluid, unity liquid, unity water, unity gas simulation, code monkey, liquid, fluid, gas, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, unity tutorials
Id: _8v4DRhHu2g
Channel Id: undefined
Length: 15min 45sec (945 seconds)
Published: Sat Jan 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.