GRASS SWAY in Unity - SHADER GRAPH

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll use shader graph to create a grass shader that sways in the wind while we'll be using grass as an example the shader can easily be used for other kinds of foliage to achieve this effect will be using vertex displacement what this means is that we'll be using shader graph to control the positions of the vertices in our mesh in our case we want to take these vertices and offset them in a way that feels like they're being affected by wind in fact vertex displacement can be used to create all kinds of cool-looking effects from waves in water to swimming fish also this video is sponsored by unity and special thanks to Jack d'hubert Andrew cannon go John Shannon infinity PBR and glassful entertainment for their support on patreon also I just recovered from a cold so if my voice sounds a bit off I apologize that is why and with that let's get started in this video we'll be using the HD render pipeline but you can of course get a very similar result using lightweight the project here is already set up for each GRP will of course have a link in the description on how to do this in case you're interested so first we need to make sure that HTTP is up-to-date to do this we'll go window let's select the package manager we'll make sure to show preview packages and scroll to where it says High Definition render pipeline now mine is already up to date otherwise you will have an option here to update it now as you can see I've gone ahead and set up a simple example scene here I just have a cube as the floor and then I have these different grass objects spread out now the assets that I'm using here are really simple as you can see I have the grass mesh which is basically just a collection of intersecting planes and then I have three textures that we're going to be using if you want to download these assets I'll of course have a link to them in the description but as you can see our grass doesn't currently look a lot like grass so let's go ahead and change this with a shader let's go to a project and hit create we'll go under your shader and because we're using HDR P will go under HDR P and select the lit graph this is much like the PBR graph for light weight we and then name our shader I'm just gonna call it grass and we can then double click it to open it in shader graph so before we actually start modifying this shader let's go ahead and change a preview to instead of showing a sphere it will actually show our grass object to do this we'll right click hit custom mesh and then we can simply select our grass mesh there we go and first what I would like to do is start with what I like to call the base shader and buy the base shader I mean the part of the shader that allows us to plug in a few textures to make a grass actually look like grass now in our case we have three different textures the first one of these is the base color so let's go to our blackboard here and let's create a new texture and we'll call this one base color and that's the default texture that's go ahead and select our green grass albedo we can then take this texture from our blackboard and make a node out of it now you might think that you can just plug this directly into our base color but because this is a texture and our base color is a vector3 we first need to go through a sample texture node and we then take the output of this node and plug it into our base color as you can see our grass is now colored however we can't actually see the shape of our grass yet and the reason for this is that we also need to take the Alpha of our base color and plug into the Alpha stat and by default this isn't going to change anything the reason for this is that we need to go to our lid master settings so press this car here and change the surface type from opaque to transparent because we want everything around our grass to be transparent and as you can see this already kind of works we also need to go down here and select alpha clipping to make sure everything around our glass won't be included so stuff like specular lighting will disappear and there we go our grass is starting to look like grass you'll also see that if we look at our grass from behind it disappears the reason for this is that we need a grass to be double-sided to do this we'll go to the double sided parameter and change from disabled to flipped normals and we now see our grass from behind as well awesome so now we can go ahead and create our next texture 2d we call this one metallic let's change the default texture to the blue one here let's also drag this in let's hook it up to a sampler texture node now this texture is pretty cool because it's created in such a way that the different color channels contain different information about our material so the red channel here is meant to control the metallic property the green channel will control the smoothness and the alpha chain is going to control our ambient occlusion such you can see that's already made the grass look a lot more realistic and added a lot of depth and we're now ready to add the last texture so let's go ahead and create a texture 2d and this is going to be a normal map I'm gonna set the default texture here to the green grass normal I'm gonna make a bit of space for this let's drag it in let's hook it up to a saml texture node now for this one we want to change the type from default to normal because we're working with a normal map and we can just go ahead and plug this directly into the normal slot and that's pretty much it for the base part of our shader I'm just gonna go ahead and collapse all these let's hit save acid and if we head back into unity we can select the material that is currently applied to all of our grass meshes and here we can change the Shady to Shady grass grass and as you can see we now have grass in our scene really really cold so that concludes the base shader now we need to add vertex animation to actually make the grass move with the wind to do this we modified the position input of the lit master node now this is done in three steps first we get the position of our objects vertices in world space wall space means that the position is relative to the center of our scene we use the X&Y positions to create UVs and offset these UVs over time for the second step we generate some noise in world space based on the UVs think of it like we are overlaying a scrolling noise texture on top of our entire scene that will affect all of our grass now based on this noise texture we can push and pull the grass on the x-axis for low values or black parts we pull on the vertices and for high values or white parts we push on them now we could stop here but the effect would look pretty weird because the grass would be moving equally from top to bottom instead for our third step we want to make sure the bottom of a grass stays in place to do this we need to create a gradient that goes from bottom to top where the bottom value is zero and the top value is one this means that at the bottom we'll have low values that result in no or little movement and at the top we will have higher values resulting in more movement to create this gradient we use the local you've ease of our grass model as you can see from the grass textures a model is set in such a way where the bottom of the texture is at the bottom of the model and the top of the texture is at the top of the model because you v's go from 0 to 1 naturally we just use the y-axis of amylose movies as our gradient now if that confused you in any way I completely understand you've ease and vertices can be hard to wrap your head around at first if you'd like to get a deeper understanding of the two we have a small video series on mesh generation that I recommend you check out or you can just follow me for now so this here is the base part of our shading now for step one the very first thing that we want to do is create a precision node this is simply going to get the position of our vertices in world space we're also going to create a tiling and offset node as the name suggests this allows us to offset UVs in our case we want to do this over time to create the scrolling effect so we'll take the position of our vertices and input that as our UVs and then to offset them over time we first need to create a time node and we could plug this in directly or we could create a property that allows us to control how fast this should be so let's go ahead and do this let's create a vector two because we want to control it on both the X and the y let's call it wind movement I'm just going to fold it to six on the X and zero on the Y and let's drag it in here so now we can simply multiply our time with our wind movements let's take our time here pull it out into a multiply node and take our wind movement and plug it into the other part of our multiply node let's take the output here and plug it into the offset and I'm just gonna collapse this here and pull them together to create a bit of space and while we're not seeing anything yet that is pretty much it for step one we've now created the UVs and they are being offset over time next step is adding the noise let's go ahead and create a gradient noise node as you can see this creates some pseudo random noise and we can go ahead and control the scale of it right here in fact let's go ahead and change the scale based on a property so let's go ahead and create a vector one let's call it wind density and let's just fold it to something like two let's drag it in here and plug it into our scale call and you might have guessed it at this point but our gradient noise has a UV input let's go ahead and take the UVs from our tiling an offset node and just plug them right in there and you can now see that our noise is scrolling by over time based on our wind movement and that we can zoom in and out based on the wind density really cool again I'm just gonna collapse some of this to make some more room and we want to play around with this noise a bit to give it this strength that we want so the first problem with our noise as it is is that all the values are currently going between zero and one from black to white now by itself that's fine but the problem in our case is that if all the values are positive we're always going to be pushing on the grass we also want to pull it back so since all the values are between zero and one we can really simply change this by just subtracting by 0.5 so let's take the output here put it into a subtract node let's just subtract 5.5 this way our noise will now go between negative 0.5 and positive 0.5 and you might already kind of start to feel how this organic like movement can be used to emulate stuff like wind of course we want to be able to control this strength of our wind so let's go ahead and create a vector one for this as well let's call it wind strength and let's default this 2.3 we'll simply take this wind strength and multiplied together with the noise so we'll create a multiply node here and plug those two in so we've now just reduced the strength of our wind a bit and by strength I mean how harshly our wind is going to influence our grass so at this point we have a scrolling noise texture the next thing that we want to do is use this noise to offset the exposition of our vertices so let's go ahead and take all of this and make some more room and again we can go ahead and get the current position of our vertices specimen riding position let's go ahead and output this into a split node this is going to take our three components XY and z and separate them as you can see it's named them RG and B and the a channel can just be ignored so if we want to offset on the X we need to modify the our channel in fact let's just take our noise here and add it right on top let's create an ADD node let's take the output of our noise let's take the X of our position so the our channel here and add them together and you can now see the effect that our noise has on our x-axis really cool let's move this over here and again let's make some more space now the other two components y&z we want to stay the same let's use a combine note to take the X into our our channel the Y to the Y and the Z to the Z there we go so we're just keeping the same values here but we are adding the noise on top of our X awesome and that is it for step two now at this point we are ready to see how this looks in the scene then the problem is that our coordinates are currently in world space and as you can see a master node wants the position in object space in other words we need to convert from being relative to the center of a scene to being relative to the center of our object luckily there is a handy note that will do this conversion for us it's called the transform node and we use it to simply convert from wall space to object space now in light-weight we would go ahead and simply plug this right in and then plug it directly into the position on a master node however as you can see our preview disappears that's because this doesn't work in HDR P because it uses camera relative rendering because of this we first need to subtract our camera position the light weight you can completely skip this step but all we need to do is before we transform our position we need to subtract so we'll add a subtract node here and the value that we need to subtract is our camera position so camera position there we go and we can then put that into the transform node so just a tiny extra step that we need to do because of how the renderer works and now if we save the asset and head into unity we can see the noise working on our grass of course it currently looks really weird that's mostly because we need to adjust the settings for our grass let's go ahead and select our grass material and I think the main problem here is that our wind density is way too high I'm gonna go ahead and dial this down to something like point zero six and already I think that starts to look a lot more like wind we can also see the definite problem of a grass moving with the base as well so let's go ahead and change this with the third and final step so to make sure the bottom of the grass stays fixed to the ground we need to first of all create a gradient based on our UVs so let's again add some more space here and we want to do this before we start converting everything into object space and getting the UVs of our object is super simple we just create a UV node and this now has the UVs of a model nice we discussed we want to use the y axis of our UV s so we need to split this using a split node and only take the Y axis which is the green Channel in fact let's just try and preview this and as you can see this creates a really nice gradient going from black to white so we'll take this gradient here and we'll use it to interpolate between the offset position that we created here and the default position that our vertices have to do this we're going to use a lobe node so herbs tends for linear interpolation and it's going to allow us to transition between two values based on a third value now that sounds more complicated than it really is all that we do is that we plug in our first value which is the default position of our vertices so just the position here we're simply going to plug that into value AE then we take the second value so in our case that's the offset position of our vertices so that is the output of our combined node here and we want to plant between the two based on the gradient of our UVs here so we'll simply input that as the T value so when T is zero and our gradient is black we're going to be using the default position and as we get higher and our gradient becomes more white and we get closer to one we're going to be using more of the offset position so really cool and we now just take the output of this and put it into a subtract node to all of the conversion from world to object space and that pretty much concludes our entire grass shader if we now just move this over to make it look a bit neater and hit save asset and go into unity voila we can see it working as you can see the base of our grass stays fixed to the ground and as we get higher and higher more and more of the grass is influenced we can also see that it looks fairly nice and random and that the grass is kind of moving together in chunks you can of course always go into the material settings and change stuff like how much the wind moves let's try and double that to get kind of more rapid changes you can also change the wind strength if you wanted to more heavily influence the grass this might be a bit much and you can change the wind density to control if you want to move different parts locally or if you want the grass to kind of move together as a unit awesome that's pretty much it for this video if you want to learn more about vertex displacement or how you can get more advanced with vegetation in general we'll definitely have some things for that in the description also thanks to unity we are flying to GDC this week which means that won't be a video next Sunday however we will be live-streaming the unity GDC keynote on march 18 at 5 p.m. Pacific time so stay tuned for that and that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in February and special thanks to James P Jake Gilbert and you can link Oh John Shannon infinity PPR Sybok Nami Dennis Sullivan Travis Dylan face Tamara Phi Lila set Ronan Tintin fence Cuba Chris mechanical mind thanks so long custom Sue Ellen Gregor Pearce cool Swedish key rock pain Tim afforda Park Erasmus and Noah Kiyosaki you guys Rock
Info
Channel: Brackeys
Views: 724,533
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, model, texture, models, textures, material, materials, how, to, howto, learn, course, series, tutorial, tutorials, fix, tip, game, development, develop, games, programming, basic, basics, C#, shader, shader graph, graph, grass, sway, vegetation, wind, realistic, plants, grass sway, vertex, vertex displacement, vertices, HDRP, UVs, lightweight, foliage
Id: L_Bzcw9tqTc
Channel Id: undefined
Length: 16min 56sec (1016 seconds)
Published: Sun Mar 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.