Unity URP - Make the grass shader interactive with the player.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there welcome back in this video we're going to continue where we left off last time and show you how to make the grass interact with the player we'll also fix some issues and make some improvements to make the grass look even more amazing so let's Dive Right In first let's fix the issue with the grass shader's Reflections as you can see when removing the camera slightly the reflections on the grass look strange so let's solve this problem the solution is simple we just need to adjust the smoothness value of each pixel we can achieve this by multiplying the smoothness value by a color channel of the texture great but currently the effect is not very noticeable so let's try another Channel since the grass is green we'll switch to the green Channel much better now let's move on to the next part next we'll Implement Collision detection between the grass and the player but first we need to let the Shader know the player's position so it can calculate the vertex displacement values correctly open your player control script and go to the update function we need to use the set Global Vector method in the Shader to turn the player's position into a global value so that our grass Shader can use it to use Global values in Shader graph we need to create a variable with the same name as the one in the script the reference name should also be the same and make sure to add an underscore to the name then we need to uncheck exposed this way we can get the player's position in the Shader when the player approaches the grass the vertices of the grass model should move in the opposite direction of the player's position and when the player leaves the interaction range the vertices of the grass should return to their original position first we need to get the distance between the vertices in the player then we use a clamp node to limit the distance create a variable named interaction distance to control the maximum value of the limit let's connect the result to the color to see the effect now when the player gets close the color of the grass will turn black which means the displacement value is zero however those outside the interaction range are white which is one this is the opposite of what we expected so we need to make some corrections use a remap node to solve this problem we need to remap the values from zero to the interaction distance to a range of 1 to 0. make sure to swap the positions of 0 and 1 in the output values let's see how the result looks now great now the colors have been swapped let's adjust the value of interaction distance to see if it works properly next we need to determine the direction in which the vertices should be moved to calculate the direction Vector between two points we simply subtract the starting position from the target Point's position and then divide by the distance between the two points this will give us a normalized direction Vector with a length of 1. first subtract the player's position from the world position then we can use the normalized node to convert the vector's length to 1. next create a variable to control the strength of the vertex movement multiply the result by the interaction range finally add the calculation result to the wind displacement [Music] now the entire grass is moving we need to adjust it [Music] multiply the vertex displacement by the texture coordinates V value so the roots of the grass will not move along the Y value of vertex displacement is not accurate that is because the player's position is located at the player's feet therefore we need to move the player position in the Shader upward by the radius of the collider so that the grass displacement will look more normal next let's go back to the Shader graph we need to multiply the Y value of the displacement Direction by minus 0.5 [Music] press the play button to test it out great we have successfully implemented the collision between the grass and the player next let's go to blender and subdivide each face of the grass into more faces this will make our grass look smoother when saving the file make sure not to override the original file then let's go back to Unity and remember to assign the grass Shader first open the grass prefab we created and drag the grass model with more faces into it [Music] and set it as lod0 now we can switch between two models based on the distance between the grass and the camera the grass that is far away from the camera will use a model with fewer faces while the grass that is closer will use a model with more faces let's select the fade mode to make the transition smoother don't forget to check the option for supporting LOD Crossfade in the Shader [Music] finally we can also disable Shadow casting for the grass that is far away to reduce performance overhead [Music] alright I think that's the end of this tutorial video don't forget to click the like button below or leave a comment about the content of the tutorial see you in the next video [Music] thank you
Info
Channel: 帕羅 Paro
Views: 10,365
Rating: undefined out of 5
Keywords:
Id: 9a7U5ySd6pU
Channel Id: undefined
Length: 6min 26sec (386 seconds)
Published: Sun Apr 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.