New CRAZY fluid simulations in Blender!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] you probably think that the fluid simulation has to be three-dimensional and very slow but today we will make a Floyd simulation that is two-dimensional and quite fast in real life water is made out of molecules in one drop of water there are around that many molecules that's 200 times more than sand cranes on Earth blender struggles with a few million particles so simulating trillions and trillions and billions and billions of them would be quite impossible storing just the ID of each of these particles would require a tower of 1 terabyte micr SD cards that is 15,000 kilm High there's only one solution we lie we let the viewer think that we are simulating the entire world water surface but actually we divide it into a grid and simulate what water would do in those grid points so how does it work let's create the grid the grid is made of points that we call cells each cell has a velocity Vector to show the speed of water around this point and this velocity field evolves over time by the direction defined by it the velocity field can evolve however it wants but the Only Rule is that the velocity vectors must not create any compression of the fluid the Divergence of this Vector field must be zero Divergence means stretching or shrinking a Divergent velocity field does not look like water and that's because water is incompressible you can't make Water Act in any of these ways so if you correct velocity to avoid Divergence we are all good and the liquid flows freely that's everything you need to make a liquid simulation so let's start this is a new kind of tutorial format where we won't make everything from scratch we will be using some pre-made node groups and I will explain why we do things so the focus is on why instead of exactly the Integrity of the notes if you want to get the pre-made node groups you can get the file with all of them from the description you can just start creating into this file or append them in your own project and if you want to get like the full from scratch tutorial you can get this actually if you sign up for my course weight list which is free and you guys seem to like this idea so let's let's try it out and let's see how it works so let's get into blender and let's open up some geometry notes and let's see what our first task is first task is to add a grd for simulation uh Sim and let's add a GRE that we want to use as the basis for our simulation so let's get the GD let's put the GD here this is our simulation so as you remember uh the GD has some kind of a resolution as you saw from all of those animations so let's add like maybe 40 for our resolution and this looks like that and now the next step is to add a velocity field that actually is representing the water at those locations so for example if I have this location an arrow that's pointing there this means you know around you know in this area water molecules will all move in this area however much there will be um so let's add a very simple velocity field that looks like that and for that let's just add a store named attribute and let's add a velocity attribute the velocity attribute has directions in it so it's not a float attribute it's a vector attribute that has three components for X Y and Z and if you want to move to the right just um like the animation shows we have to put some kind of a velocity here isn't this true and if you want to know preview this attribute on the surface then what we have to do is to add a named attribute node and inside here we can preview different attributes like for example the position of of those points here and also the velocity of those points that we just created now keep in mind you have to preview this attribute on where it actually stored you know or it has to be stored when you try to view it it doesn't work here so we have this in place but obviously I think you agree with me that if you have water that is moving to the right currently it is red then we cannot actually see this I mean it doesn't work currently if you play this but even if it worked we still couldn't be able to see this because the whole grid is full of that same looking water so let's do so that we only make half of the grid having this liquid that flows to the right so for that what we have to do is um is to somehow eliminate uh or select uh this side and eliminate the other and we can do this very easily if you know we just uh take the position separate this into X Y and Z which is the same and I mean if if on the X the position of the uh points is larger than zero greater than zero then we are going to store our velocity there so we can do it store it here but now if you play this animation I mean still nothing happens right and that's because we are not doing this on every frame if we were to do this on every frame this should you know move like that to the right let's do this on every frame let's start a simulation node let's um and let's uh add this here and this sounds all good and and nice but I mean still there is nothing in those simulation nodes that is moving thing if you were to add like a transform Noe for example this one moves our grd right but what we need to do is to move our attribute and for that we can use an a note group called ad vector now the advection node group does so that it moves the attribute uh in our case velocity could be attribute called carrot whatever we want but we don't have an attribute called carrot so we're going to use velocity now this moves the velocity by the velocity itself so if you play this you see moves to the right and if it's it's you know a bit too fast right now so if you do like that it is much much slower how it works internally is that you have the Velocity attribute and and then we have this Lo called sample nearest surface and that's going to do so that it's going to find for example in this location okay my velocity is going uh let's say here uh so this means I have to take this value from here to there the next frame but what it does is that it does it in the inverse way is the subtraction here it it's going to invert the velocity and it's going to take the value from here to the current uh it has a lot of benefits for example if you have a lot of CPU cores in your computer then I mean if you were to do this then multiple things could Point into the same location and then it would be like clashing and stuff but if you do this the other way around you just read from a certain position you just read from here to there so on this is how it works and advs the velocity attribute by itself but let's add a more interesting velocity field that is just something worth watching so let's delete this selection here and let's add a noise texture note here that acts as our velocity I want to put this here and put this here and now this looks like that if you play disappears super fast this has many reasons number one the velocity is just too fast so let's take a vector math note and let's scale this thing down to 0.1 for example and I see you know it moves but it's very dark and also moves into only into One Direction in in up there you know that's because noise field in blender by default has ranges from 0 to 1 and not from minus one to one so it means it only Moves In One Direction again a big fan of One Direction here and we need to get this range to be both on the negative side and on the positive side so I'm going to add a subtraction node 0.1 and now if you play this you see it moves firstly it's super dark and secondly it is having some massive problems which you will get to in a minut it first let's fix the problem that it is dark I mean it's dark because the value has been multiplied with something but if we make this you know stronger what actually happens is that you know the velocity gets also faster which is not what we want we would like this to be you know slower and also if you for example add increase the FPS then it gets even faster so we should make this frame rate independent and for that we need to introduce the time step so what I'm going to do is that I'm going to add a store time step node group and this one allows me to input the FPS here of my scene for example 60 and iterations Let's ignore for this time and it allows you to introduce the FPS and then it's going to divide one divided by this FPS and it's going to store this as a Delta time attribute so if you are doing something for example 2,000 frames per second it's going to still multiply this smaller so that it's the same speed if going to do this I don't know one frame per second it's gonna keep the same value so it's always frame rate independent and this thing here didn't work for me that's why I made a whole new note group now we just have to go into the adve thing here and multiply the velocity that we use with um a Delta time attribute that is created and this makes the velocity much slower so now it um looks like that even though it is you know super super visible ible and bright in physics simulations you use very often a Time step unless you have some kind of a old computer game where the physics speed is dependent on the frame rate so when you look down you actually run faster which is one of the funniest things I think so if you now see the surface of this velocity you see it does look like liquid right it does look pretty much like liquid but there are some problems which and those problems are those strange lines here and those lines are because of Divergence in this fluid field and Divergence I'm going to explain this in a minute basically makes so that you know the vectors are nicely strolling against each other and they're like no I'm not going to back off and they just clash in Center here neither one of those is going to do like fluid does you know fluid is incompressible and ready for compromises so it's going to just turn away Floyd is going to turn away from this from this this place it's not going to you know just make like a make a duel there it's just backing off so we have to make those vectors act like a fluid and we have to get rid of Divergence for that Divergence is how much water flows out of your cell if the neighboring velocities orlow into the center cell there is too much INF flow and diverg is negative if neighboring cells point away then too much flows out and diverg is positive here no compression is created and therefore there is no Divergence the inflow and outflow are balanced for each cell and the same happens here the liquid has no B pressing areas it's just swirling around and this creates a perfect fluid velocity field because a perfect fluid velocity field doesn't have any Divergent areas and is therefore incompressible our noisy field obviously has some compressing Divergent areas and we want to get rid of them to get a nice fluid-like behavior and we can solve this by calculating a counter Force called pressure pressure will push water away from those compressed pressure przed areas and to know our pressure we first need to know our Divergence because the Divergence is the basis for calculating the pressure so how do you calculate it each cell has four neighbors that can flow water in or out of the cell water can flow in or out in the x or in the y direction so on each axis one neighbor has to be for outflow and one for inflow let's make the agreement that for any cell the up and right are outflow cells and the left and down are inflow cells each neighbor has their own velocity that shows where water moves around those neighbors and the velocity is made of the X and Y components let's find out how much is the net outflow from the center cell on the x-axis so let's do outflow on x minus inflow on x 3 - 2 and we get one but since this is per two cells and we only need to know for one let's divide the answer by two 0.5 and let's do the same for the y- axis so outflow on Yus inflow on Y is 4 - 7 which is - 3 / 2 is - 1.5 and now let's add the X and Y outflows together and we get minus one so on average there's a bit too much water flowing into the cell and if we do this for all the cells in our simulation we get the feeli like that so let's calculate this in blender to take our fluid from this to that so let's find the Divergence of this gred here not very hard when you have a Divergence note group just waiting for you let's put this here Divergence and this does exactly what we just looked at it's going to find the net outflow so it's going to do outflow minus inflow outflow minus inflow multiply with 0.5 or divide with two add them together and wrote this I write this as a Divergence attribute called D what does the D look like let's take the d let's go to the first frame to rewrite the attributes let's click on the viewer and we don't see anything I was getting worried for a second and then I remembered looked at this look at the first frame of this animation each cell has four neighbors of course it has but we are just not defining them I mean we are reading some data from the neighbors like you know oh neighbor right but we are actually not writing telling geals we are our neighbors so we should find the neighbors to find the neighbors uh let's add a node group called store Neighbors and how it works is um pretty simple so for example for this cell now think logically how would you find the neighbors of this cell first you need to find what is the length of the uh of the GD here what is the length of the edge okay you find the length of the edge what do you do next we go inside you see that's exactly what we do here we store an attribute called Edge length that is uh finding the length length of of each Edge and each Edge and writing this into every point and now we just go up by this Edge length down and right and left by this Edge length like we do here we add this Edge length on the Y to the position we subtract this from the position we add this on the X we subtract this on the X and we find the nearest point if we go up by this amount so if we go here like the nearest Point most probably is going to be in case of a greed the neighbor we store those indices index we get the index of the nearest point we store those as attributes like left and right down and up and indices are just like you know addresses of geometric elements uh when you're doing computer Graphics so later on we can for example use a note called evaluate at index we can put index you know 68 here and okay from this index I want to get I don't know a certain value like I don't know the radius of a certain certain point for example this is how those neighbors work and now we have them in place and what we can do is we can uh see the Divergence attribute the moment that we enable the neighbors calculates itself and we get something like that but is this the correct Divergence this is always good to know if you're doing simulations things have to be correct so here's a way to uh verify let's add a velocity attribute and set position let's move our grd by the velocity itself it's quite strong though so let's just scale this thing down a little bit and you see where Divergence is negative we have inflow it is like shrinking there the water is Flowing away from those areas and where it is positive too much is flowing in so now we need to solve this using a counter Force called pressure if our Divergence looks like that this means there are a lot of water molecules compressed into these lower areas and the pressure is super high there so we need a fixing Vector that tells our velocity to turn back do not go in these areas for this we'll calculate the pressure the fluid is causing and then up this slope of the pressure we'll find some vectors and if we subtract these vectors from our velocity it will make the velocity turn away from these areas and become like a fluid because it backs up and the formula is quite simple the whole internet was full of them we need to take the average pressure of the neighbor cells um and subtract a bit of Divergence as you noticed where Divergence is negative we have a lot of pressure so if we subtract a bit of that negative Divergence it gets positive and Notch is our resulting pressure to be you know more correct with each frame we repeat this about 30 times and this Notch is to pressure to be you know pretty correct for our non-scientific use cases and makes it act like a fluid so let's add a little uh pressure node group here pressure let's put this here and this has also an iteration what you have to do is you know first what we do here is that we make a guess for the pressure well let's think the pressure is going to be zero and then we're going to let formula do its magic and it's going to find guess the value and it's going to get more correct with the every iteration which is like crazy right and if you're using an older blender version you just have to you know copy those iterations 20 times and add a pressure guess in the start and this is what you're going to do but we are you know uh the high Supremacy race of blender four so we are using a repeat input Zone here and 20 iterations how does the pressure look like let's get the pressure attribute and let's preview this pressure it looks like that Divergence pressure aha I can see a pattern here so where the Divergence is negative for example you know you know these areas right here if we have a lot of you know inflow everything is Flowing inside it's a crisis but pressure is very positive here so it's to just you know push the water away from those regions and uh this is basically what we need but you know to push something away you need a Direction so the pressure field currently is black and white and this doesn't have any Direction I mean we it's not a vector a vector has a Direction a float does not have a direction and for that what we need to do is as remember find those vectors and we can do this by if you're using mathematical language we find the derivative of the uh pressure field or the gradient maybe I just Ed some very wrong mathematic vocabulary here but the thing is we have to find the rate of change you know so if you for example use the uh set position and they put the pressure into the offset and we use use a combine x y c z here you know the pressure is like that and we find have to basically find the vectors that you know Point down from these slopes like that or point up from the slopes doesn't matter we just need those vectors whichever direction is okay we can just invert if we need and for that we have a thing called correct velocity a not group like that which does exactly what we just described it takes the uh outflow inflow outflow inflow uh pressures finds the from the right neighbor F finds pressure and so on and it subtracts them and multiplies and combines them into a direction Vector so if you for example have something higher on the right side lower on the left side then you can subtract them and tell okay my I'm going up by this amount this is how uh taking a derivative of a function Works which in our case is a pressure field and what we do in the end is that we subtract them from the vector just as promised and this should in theory make our velocity turn um you know back and be a bit more of a fluid like thing so let's preview the velocity let's add all remove all of this nonsense here let's play this and I would say it looks definitely better than it looked before if you remove correct velocity yep if you enable this it gets better and it seems to me it's correct but you are like well that's kind of a boring boring thing I mean a result like that a goddamn theoretical Vector field for the result of this tutorial of course not I'm not going to let you walk away with that let's make this scene that is on the thumbnail the scene here is made of different little points that are all a certain color and then it forms this nice like a sand but it isn't sand and like this awesome Floyd like motion so for that the first thing that we need is points and let's add a new object for those points like a plane I'm going to add some geometry notes I'm going to call points press home to go to this view here and we don't actually need this default plane it's just a not very important one I'm going to take the fluid in here I'm going to you know call the object itself fluid and where I have the fluid simulation and I'm going to take the plane which I'm going to call uh points so these are going to be the points that on the surface of our plane to add those points there is a very simple thing called distribute points on faces for example like 500 points or the delit is 500 and you see okay now I have those on the surface and this is like one object the points the fluid is one object points are a separate object however those points are not moving so let's add something to move those with um and we already you know know that we want to move them on every frame uh because we want to get like an animation kind of similar to this intro scene here so for that let's add a simulation Zone because we want to move those points in every frame whatever we want to do on every frame we can put inside here so let's add a set position node to move each one of those points we can for example put like 0.01 here and now if you play you know they start to move into this direction into they all move into One Direction by the way but um we want to use the velocity of the fluid to move those points so where do we get this um the velocity of the fluid is here and these are the points you know so this is already new kind of geometry so what happens if you just take a named attribute like that and we use the velocity and we I mean we preview this on those points and we see oh okay those points have some velocity but if you play this you know you see the velocity is the same on every frame let's remove the set position you know the fluid velocity is changing but the point velocity isn't so if it put the set position node back here and we connect the velocity to the offset what you see is that they move away in first of course super fast so let's just um scale down the velocity itself with the time step multiplying with delta T gives the same effect as the uh you know the fluid in the fluid simulation itself where we make this frame rate independent and therefore having a reasonable speed so now you see okay this works like that but it doesn't look like FL like fluid because the points are moving through themselves but they should be moving you know like a fluid field like you see here and that's because they have the same velocity on every frame so for example if um uh the point is appearing here it gets the velocity from here and during the whole lifetime it's just moving on every frame with the same velocity bum bum but what we want to do is that when it moves here it gets a new velocity maybe it moves here maybe it moves here maybe it moves here so this is what we want so we need to reread uh from the faes of the fluid the same velocity all the time and for that let's use sample nearest surface the sample nearest surface note takes uh information from the nearest surface to each point so it suits our case very well so we can just take the mesh and use the vector and take the velocity from uh from this mesh and let's also you know multiply this with delta T so if you now play this you see the points are moving but they're not like you know collapsing or anything they're acting like a real fluid does so let's um let's put this thing uh here and let's just you know group all of that and let's call this this uh move points so now we're moving the points but um the problem is that when we move them we get those large you know balding kind of holes here where we don't have points which is logical because to move away from there so I mean I can understand why there are no points so the only solution for that is that we need to add more but if you make this frame rate a bit longer so I can show you if I play this and I chase the count here we don't get anymore and we don't get more because what's before simulation noes is only executed once then at the first frame it's going to be taken into the simulation input then it's going to be you know doing everything that is in the middle here which is moving the points currently and then it comes in again and like that and again and this means that whatever we do here is only red in once so we need to inst did add points on every uh frame so let's do like that so if you play this now does this make things better not a lot reason is the same we take in the geometry from the fluid we add some points on the surface and then we uh move the points with um with some logic and then we take in the same points again and we uh try to distribute some points on faces but we don't have any more faces because we only get points in now so we should uh if I go to the second frame you know you see we have this uh input geometry as unsupported type Point Cloud so you can't distribute points on a point Cloud what we need to do is that we need to separate the mesh that comes in and we need to feed this in on every frame so for that I can use a separate components separates the mesh and the points so I want to move the points I'm going to take a joint geometry and currently my system does so that it adds points in every frame and moves the old points that already have been added but uh it still doesn't work you know the mesh here isn't still pass through on every frame so let's connect the Miss Mish here again and if we do like that then you see something really cool and let's use a Point radius uh to better visualize this I'm going to put 1 cm here and you see we get those lines like velocity lines which is a cool effect on its own I mean this whole fluid simulation thing is like a gold mine you know you discover so many interesting different techniques and and results that you can do and one of those is that but what we want to do is not to get those lines and those lines appear uh because on every frame we add those points into the exact same locations because the C SE is the same so if you were to change this on every frame this would result in something more I would say natural that doesn't have this uh liny Behavior so let's do like that and I see we add them you know randomly on every frame which is awesome which is cool let's play this and what is the next issue that we are facing they are going over the border and this wouldn't be kind of a big problem but we are dealing with computers and they have limits so you see you know down here you're using memory 2.3 GB currently we should get rid of the points that we don't actually need that and if they are not on the surface of the fluid uh they will be just moving by like the closest velocity to them from this surface here which doesn't make sense it doesn't look like fluid anymore either way so let's delete them for that I'm going to add a node group called delete points if out of bounds and I'm going to add this before I move the points because I want to you know make those moving calculations on as little points as possible to save on CPU power uh but deletion is like removal of points is pretty cheap of an operation so um it's wiser more wise to do this this way so now this works like that um and how this node group works is it Compares if the point is you know further than uh 0.25 on either side and if it is on X or on Y is going to delete them it only works for boxes you know but it is just for this use case this note group and now this works uh but the problem is the more we play you see the FPS kind of starts to drop and starts to drop because uh we are getting more and more points added to our scene uh if you start out with uh here you know with let's say 100,000 points here we have already like get 400,000 points so which we should delete them if they are older than something if they've been in our little 3D scene for more than let's say 200 frames let's delete them create an age attribute for each point what this create age node group does is that inside it takes an age attribute and adds one to it on every frame and does this so for all the points that have been added not for the new points because I mean the new points on the next frame will be passed to the old points and this is how it works and now what we need to do is we need to delete uh points if over Ag and here we can specify the uh maximum age so if I play this you know if this is zero it's going to they're just going to live for one uh one second one frame sorry but if I put here like 60 frames it's going to be exactly for 1 second I have this Frame ratees up all the time because although I'm constantly adding points I'm also removing them at the same pace which is awesome how this works is that it takes the stores the maximum age as an attribute called age Max and if the uh H is larger than the H Max it's going to delete them and of course I could have done that as well but we need this four later on I'm going to show you why they appear and I'm going to make them a little bit smaller so that uh it looks a bit better but to see uh when you look at this you see this kind of a flicker Behavior here and that's because points get added and deleted and they just disappear like like that and this doesn't make sense they have like this binary visibility so let's do so that when they are born their ages is small and when they get you know grow and grow get like an adult they get larger and when they start to die they are get deleted they are going to you know shrink off and this is very possible to do because we know the maximum age of each point so let's use a note group called any animate radius uh done in a stupid way actually but what it does internally is that it maps from the age and the age Max um so M Maps this Maps this to range of Z to one and then makes this range with this kind of a shape and then what it does is that it multiplies the resulting thing with a certain radius that we want so let's put here like R is 0.01 for example and this is probably too big uh I'm going to put here another zero you see some more flickering that's because the points that appear actually have some some kind of R so I'm going to put this to zero and now you see this is very very smooth because everything that appears is also at uh the radius of zero and what ties off is uh also with a radius of zero now our system is ready and looks awesome but now we need to put the color here so let's add a material for that let's take a sit material uh let's put this here let's actually see what the result of our points is okay we get the points but you also get the grid itself and honestly I don't want to put the material on the grid so I'm going to use the separate components uh once more and want to only put put this on the points you know and then join this together with the mesh in the end like that so I can like put two different materials here I'm going to add one for the floor which I'm going to use here and one for the points which I'm going to uh use for the points themselves now if you switch to Cycles you can see that all those um points are rendered as spheres so if you want to do this in Eevee you can but this will get very performance hungry because those icos speres like you can have like a lot of those but we have those points here and let's now open up our Shader editor and start to shade them um I'm going to take the floor first I'm going to just make this black and now if I play this you see okay on the surface we have some points and for the material of the points let's add a noise texture first because uh the noise texture is um it is a very good way to add those colors here and I mean you can add this in different in different ways for example you can use the object coordinates of this thing but the problem is when the points move the noise itself you know doesn't change too much so if I add like a greater than comparison here you know the points move but the color doesn't move so let's do so that we store for each point the location where they come from and so even if the point for example you know starts here and moves here it's it's going to still have the same uh attribute as the position so I can you know map the texture as it if it flows with the points so I'm going to get back to the geometry noes and when I create new points I want to store a named attribute here that is precisely the position of those points uh a vector attribute and I'm going to call this um OG uh po like an original position um and now let's go back to our uh Shader editor press home find the nodes that we have here and now if we take an attribute nodes and they preview the OG post we can see something like that does this work let's use this for the mapping of our noise and let's see if we start to play this doesn't work too much and this um can be because of the um age of the particles because currently this is like um six XD frames let's make it like 150 and and you see this starts to move much more because uh the points are alive longer so that it distorts the field much more and we can put here like maybe 200 uh and decrease the count of the points a little bit and we get a nice fluid distorted field also if you want to get the make the effect stronger you can go to your fluid Sim itself and make the velocity a bit stronger kind of like that and now you see this is much more what I'm talking about you know this looks better much better uh but it's kind of boring to add a noise here I mean you can uh you can get great results with this I made the intro with the noise for example so I'm going to take an image texture from I believe it's from pixabay or or somewhere whatever where it was from and originally this looks you know kind of like that but then if you distort this and we play this a little bit you can get something really cool here of course this is not the only U picture you can use here you can use every anything you can you want or for example remove some kind of um a components from this picture like for the thumbnail I remove the green component of this picture and now if you add like a lot more points like let's make sure like 50,000 and you play this you can get some awesome awesome awesome results that you can use in any kind of rendes but how do you make these other things that you saw in the intro I have an extended tutorial where we buil this whole Floyd simulation from scratch also with explanations and I will show you also how these scenes are made you can get this from patreon or you can also get this from um the geometry notes beginner for weight list which is on ban normal.com almost a th000 people have already signed up which is great awesome and we need you so we have 1,000 this is the last video of this year I want to fully concentrate on the course make it the best piece of content I have ever made so I need time for that thank you for your attention see you soon
Info
Channel: Bad Normals
Views: 130,410
Rating: undefined out of 5
Keywords:
Id: l_vnAz0Yo8w
Channel Id: undefined
Length: 39min 21sec (2361 seconds)
Published: Thu Nov 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.