Creating Simple Grass Shader in Unity URP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to another Unity tutorial video today I'll be showing you how to create an interactive grass Shader using Shader graph within the urp pipeline before we start make sure you have the necessary environment set up so let's get started first let's create a basic grass model in blender to create a simple grass model we can start by making a plane and adding a grass texture to it this will give us a flat surface with the appearance of grass we'll duplicate this plane to ensure that our model can be seen from different angles I used a triangular Arrangement but you can also use a cross Arrangement if you prefer once the model is complete export it and let's head over to Unity to start creating the grass Shader I've already set up a scene but there's no grass in it yet so let's drag the grass model we just created into our scene and then create a grass Shader [Music] select the urp and then choose the lit Shader graph give it a name you prefer and double-click to open it [Music] now we can start editing our Shader foreign first we need to check the alpha clipping option because our grass texture requires transparency next we create a 2d texture and connect it to the sample texture node [Music] we can control the color of the texture by multiplying it with a color let's create a color variable and give it a name [Music] to the multiply node and set the default color to white I recommend using white as the default color because black can make the texture too dark and difficult to see after saving let's create a material right click on our Shader graph file and create a material make sure to select the Shader we just created let's choose a grass texture to see how it looks [Music] it looks a bit strange it seems like we need to make some adjustments set the render face to both so that our Shader can render both sides of the play model also I forgot to connect the alpha value of the texture to our Shader earlier after making this connection our Shader should look more normal next let's create a float variable to control the smoothness change the mode to slider in the node settings so we can control the value range [Music] after saving let's see how the result looks the model's Reflections look a bit strange probably due to the issue with the model's normals let's go back to the Shader graph to fix this problem create a vector 3 node and change its y value to 1 so that we have a normal facing upwards let's see if this solves the problem the model looks a bit too shiny let's reduce the smoothness value to find the appropriate reflection coefficient great we have completed the basic grass Shader next let's simulate the effect of wind blowing through the grass we can use a sine function waveform to simulate the movement of the wind just like in this picture by inputting time into the sine function we can obtain an up and down oscillating waveform let's go back to Shader graph to create this effect right click and type sign to find the sign node then add a time node we can connect the time input directly to the sine node or use a float to control the speed of the animation by multiplying it with time before connecting it here [Music] with the sine wave we can offset the vertices to simulate the effect of wind blowing the grass add a position node [Music] combine it with the sine wave [Music] and don't forget to transform the vertex space before outputting since the input requires object space we need to perform the transformation first [Music] it works however currently we can only control the speed of the wind let's add another variable to control the strength of the Wind [Music] by multiplying it with the sine wave we can control the amplitude of the wave which will allow us to adjust the amount of vertex displacement [Music] okay but right now the vertices of the entire grass model are being displaced I'd like to keep the base stationary as it's more realistic we can achieve this by multiplying the displacement amount by the textures V coordinate which represents the texture's Y value ranging from zero for the bottom most pixel to one for the topmost pixel this way we can achieve our goal connect the V coordinate to the color of the Shader and we can see that it creates a gradient from black to white the black represents zero white represents one and the Shades of Gray are in between [Music] let's multiply the offset amount by the V coordinate which should keep the bottom of the model from moving with the offset [Music] great now the bottom of the model is not affected by the wind anymore let's move on to another issue we need to solve if there is only one grass in the scene the animation of the wind looks pretty normal however if we add more grass into the scene you will notice that the swinging animation of each grass is the same this looks very unnatural so we need to fix this issue first we need to find the differences between these grasses as you can see the world position of each grass are different we can improve The Swinging issue by utilizing this point [Music] add the world position to the time variable so that each vertex has a different time value [Music] if this approach works done now it looks better [Music] now delete the extra grass and keep only the original one let's add LOD group component to our grass so it won't be rendered when it's too far away from the camera this can help reduce the processing load of unity when dealing with rendering we need to adjust an appropriate distance when the camera is too far away from the grass it disappears we can control the distance at which it disappears from the control panel and then drag the entire model to the file area below to turn it into a prefab making it more convenient to use then let's create a new tree in the terrain and select the grass model we just made after that you can paint grasses on the terrain now press plate to see if the Shader is working properly it seems to be working well [Music] alright that's the end of this video we have completed a basic grass Shader in the next video I will show you how to add interactive effects between the grass Shader and the player as well as some other interesting settings stay tuned finally if you have any questions about this video please leave a comment below I'll reply as soon as possible see you in the next video foreign
Info
Channel: 帕羅 Paro
Views: 12,343
Rating: undefined out of 5
Keywords:
Id: jyIukFoDfu0
Channel Id: undefined
Length: 8min 9sec (489 seconds)
Published: Sat Apr 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.