Unreal Engine 4 Tutorials: Gerstner Wave Displacement from Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello i'm going to talk about how to set up personal waves to do water simulation in unreal engine first thing you want to do is find this web page which shows you the equations the first most simple equation is this one here which is going to show you how to um change the z location of your mesh so get that out of the way first thing we're going to want to do make a folder for this [Music] [Music] all right so we can save our current map we can make sure that it starts up in that map like that okay so if you notice we don't have a mesh to use to make the waves on so first thing i'm going to do is make that blender it's pretty easy to do just add a plane right and i set mine to 1000 centimeters which is 10 meters right [Music] now you're going to want to subdivide it [Music] i'm going to do this up to about one yeah that's good about 8 000 triangles just for this test now we export it as an fbx [Music] i have a static mesh preset that i made but basically all it does is only export the selected object only the mesh it leaves these by default and then it doesn't change any of this stuff because it's unrelated the thing is with importing it you need to make sure you know that you have good uvs which we do so now we've done that exported that right okay yes you don't have to save it i also like to change this your editor preferences you can set the buttons to small makes it a lot easier to see for me so we're going to need a to import that fbx see we cannot generate collision that's fine i don't want it to generate lightmap viewes because i wanted to use the ones i have let's just do that there we go i have no simple collision there we go now that we have our plane let's throw that in the world it's going to reset its location so we're going to need a material right [Music] i'm going to go ahead and create a material instance for it too i'm going to use the material instance on it on our water plane and this just makes it easier to change things and tweak them later right so to start with we're going to want some color just for testing i'm just going to go ahead and set this to unlit in fact just to make this faster i'm not i'm not concerned really about the look of the water so much as the way the water is going to move all right so if you see we have world position offset which is going to move our vertices if we look back at this equation right here what is in the equation right so the first thing is you can see that there's a sine wave right so let's add sine all right and the output of the sine wave oh it's only open writer the output of the sine wave is multiplied by amplitude so let's multiply if you hold down s and click you can get a parameter let's go ahead and set that to one if you look the sign see so ai times sign you have the direction which is a horizontal vector vector perpendicular to the wavefront that is dot product dot dotted with the x y position of the of the uh mesh right so let's make a direction vector so to make it a parameter might as well make it a 4 because when you convert it to a parameter that's what you're going to get if you try to make a like a horizontal vector which would be a two dimensional vector you convert that into parameter it will just turn into that anyway so all right so we have direction here let's go ahead and set our direction just to something arbitrary like that right yeah it's dot product did with the coordinates so we need our position right here's our world position and we only need the x y right so let's get a component mass which will get you just the x y of the world position right we're masking off z which would be in the b channel can i keep doing writer sorry yeah so our x y coordinates dot product is with the direction so let's take the direction [Music] we want to create an x y vector of the direction right so i'm appending those into one two dimensional vector now we're going to do a dot product with this direction right let's get that out of the way here look back at that formula that times w i so if you look up here it tells you what w is see they're kind of this is this is a mistake here w is a radial frequency um it's not even w it's it's a greek letter in electrical engineering it's w equals 2 pi f so they forgot a pi here should be 2 pi over l because wavelength and frequency are related inversely right so let's go ahead and create our radial frequency so it's going to be 2 pi over l where l is the wavelength so let's make let's go ahead and make that a thousand which is one meter right now we need pi because it's in here somewhere there it is pi so if we do two two pi over l right we'll get wr all right so w is 2 pi over l so we're going to multiply this dot product of thing with w i right okay then we're going to add time times q qi so let's get time so what was it time times q i plus it so what is qi let's see so q i is the speed times the radial frequency again where they dropped off the two pi so we need speed times radial frequency all right so let's make a speed [Music] set that to 30. we're going to multiply that times wi right right so s so q is speed times two pi over l which is radial frequency so that's what we got q i now right t times q i now we're going to add that with this now that's going to feed our sine wave okay so something is happening it doesn't look too great yet let's take a look here let's adjust some of our parameters there we go all right let's just make amplitude 10 so we can see it easier reduce this so we do have waves but this looks all wrong right ah i know what's wrong it's because i fed all of that into world position offset instead of z right because when we look at that equation this output is just for height so this goes into z right and we have no x or y okay there we go that's what we wanted let's see it's easier to see a wave fireframe maybe sort of see i'm going to kill that sky sphere there so we can see better yeah look at that you can see the vertices moving so of course if you want better resolution in your waves you're going to need more vertices right if we scaled this down maybe it's like half size turn that on first now our plane is more dense okay so if you look that's the wave function that's the simple simple sine wave function for z but if we want to do gerstner waves we need to do more so the first thing we're going to do is to take this out and put it into a material function right so okay we can get rid of this megaflow 3 part okay so when you have a material function it no longer accepts these normal parameters here you have to make function inputs so we don't need a vector three we need a scalar we'll call it amplitude [Music] we will set the previous value okay what others do we have we have wavelength and speed and direction right [Music] okay yeah so i guess our direction we can actually set to a vector 2 if we wanted and get rid of that which is already the correct vector size there we go you've functioned this now we can do let's take our simple sine function put it in here right and move our function parameters like this i forgot to name that one huh amplitude speed web length oh forgot to name direction there we go okay [Music] there we go there we go now we have moved all that functionality into a function and we kept our input parameters now we should have exactly the same thing as we did before which we do okay so we have our simple sine function we have our waves now if we look here continue down we will get to the gerstner way part here and there's new equations but you'll notice if you look at this one this one is actually exactly the same as that simple sine wave up here they reordered it a little bit but it's the same so the gerstener waves we can use this for z still which is nice but we need to make a new material function for that so let's just duplicate this one [Music] if you go into the cursor wave this is going to output our z right because we already created what is needed for the z if you look here you'll see that what's being fed into the sine wave is what's being fed into the cosine right yeah w-i-d-i dot all that stuff let's see actually so if we look at that we've already got this function we want to make these new functions so this is the z part of the output right so let's and then what's feeding into the sine wave is this we got to use that for our cosines to feed into them right so let's go ahead and make cosine two cosines okay what's feeding into the sine is the same thing that they need so let's go here and here okay so if you see that what this is this is the x component of the direction and this is the y component of the directions so we just need to get the x and y component to multiply by the cosine so here's the direction let's just get the x component and we'll multiply that this is going to be the x part so here's the x component multiply that y component multiply that right okay so that gets us this through here on both of those now we just need to multiply by qiai right so what is qiai it explains it here let's see qi is q over wi times ai times the number of ways and ai is the amplitude so we're going to need to create a qi and multiply it by the amplitude qi is q which i call steepness over w i times ai times number of ways okay so let's create a steepness it says to keep it between 0 and 1. so let's do that let's go ahead and set it to 23. steepness so qi is going to be the steepness divided by wi times ai [Music] see where's our wi here's our wi all right here's our amplitude which is ai that's w-i-a-i times number of waves so we're going to need the number of waves as well [Music] all right so that was w-i-a-i number of ways q divided by it so q divided by this so that should be our qi uh so let's just call that ui all right now i just need q i times a i ai is our amplitude yeah so now this is going to give us this term right so we just need to multiply it by this thing we've already created which we already set up here okay all right so now we have the x y and z part of this in one function okay so now if we put the gerstner wave function in there instead oh well i forgot to do something didn't we which is i made parameters that are not function parameters right you won't be able to access them so we need to turn steepness and number of waves into function parameters [Music] all right so here's stevens i'm going to actually copy and paste this steepness we're going to need it in a second right so let's put that there let's duplicate that here we go okay cool now when we bring the gerstner waves into here there we go so let's reconnect these things all right now we've got these two all right remember this result is going to be x y and z now instead of just z but if you look back at the paper look at this so the z is just a summation of all the ways the x and the y add to the x and y what does that mean so we're going to we're going to need to add the world position of x and y so position so let's get x so we're going to add right there's like a yeah there we go break out float three that's what i wanted right okay so x is going to add to the current x before going into here right z is just going to go right into there and then y is also messed up tonight okay so if you see what i did was i got the world position of x and y and i added it to the result of this wave if you look that's what they're doing so here's x plus the wave y plus the wave just z okay okay so now we should have a single gerstner wave if we look whoa it's interesting looking right so let's go ahead and take a look at our settings all right let's turn the steepness down there we go so that is a single gerstner wave okay well you might be asking how do i have more than one wave this is fairly simple now that we have it in a material function right let's go back to our waves function here and here's a single wave all right so we can just duplicate this this will be wave 2. of course now you're going to need parameters for all of these that are new names and i would suggest using the cat the group so that you can keep these separated easily in your material instance so what i mean by that is you go in here and you type wave one now you make each one of these wave one it's kind of tedious i can do it oh i can sweet okay now let's go through here set these all to the radiator okay now if we look at our material instance we've just got wave one in there because the other one's not hooked up yet but okay so here's our second wave how do we combine them well if you look they're just a summation right so we would just take z here and add it with the z and now we would put that into z right and if you look here you sum before you add to the x and y so we would just do this add the x's add the y's right and now this needs to go into where we added like these wires need to get away right this is the new x you're going to add this is the new y okay there we go now oh take a look at that now if we look at our mi waves again because i made those categories you can see they're separated now so let's take the second wave and we'll change this direction to the opposite direction about that let's change its steepness back down get over to steepness pretty quick all right or maybe we'll how about this let's change this to make it go the other other way that we can turn up the amplitude let's turn the wavelength down change the speed of this wave you can you can decouple the waves by setting the amplitude to zero so you can see what this wave is doing that's a good way to get it to look the way you want before they're combined all right and then once you've done that you can combine them now we have kirsten waves let me know if you have any questions
Info
Channel: Stephen Maloney
Views: 4,403
Rating: 4.9220781 out of 5
Keywords:
Id: ANaER2rvd9M
Channel Id: undefined
Length: 34min 54sec (2094 seconds)
Published: Mon Aug 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.