Thursday, February 22, 2024

GPU Instanced Grass in Unity - Part 3

 

GPU Instanced Grass in Unity


For the original post, please go to https://www.yuque.com/qwerasdwww/dfy5xd


Part 3: Add Interaction with Character



First we need to draw grass only on the terrain, not any other objects such as characters, so we need to add a layer when detecting rays.




Let's add paramters of the interaction.

We use the character position to determine which area of the grass needs to bend, and the trampleRadius to control the size of that area.




We add a new parameter called quaternion inside the grass struct to calculate the bend.

We also need to fix the size of the compute buffer.




We now do the calculation inside the compute shader.



Now we use the value we get from the compute shader to calculate the acutal shape of the grass inside the V/F shader. We calculate the corresponding matrix of the quaternion and use this new matrix as our new rotation matrix. And we apply the new matrix to vertex and get the bended shape of the grass.











Now we want the grass back to its original position after the character is gone.

We use the variable trample to track down the grass bending.

This trample value will give us a grey scale 0-1 value. We will use this value to calculater how much should the grass bend based on the distance from the character.

We add trample to the grass struct in the C# script, and add the size of the compute buffer.







In the compute shader, we calculate the trample value based on the distance between the character and the grass.



Finally we use the trample value to lerp between grass original shape and trampled shape.





No comments:

Post a Comment

common art sprint#6

 Added some per instance randomness