Wednesday, February 21, 2024

GPU Instanced Grass in Unity - Part 1

GPU Instanced Grass in Unity


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


Part 1: Use DrawMeshInstancedIndirect and compute shader to draw static grass mesh


First, create a C# script and declare parameters we need to draw static grass.



Define the struct of grass, and create an array to store the grass.



Declare the parameters of compute shader.




Now we start to initialize the comput buffer, we need to know how many grass each threadgroup needs to work on and what is the total count of grass.




We start to write data into v/f shader.




We do the initialize work in Start(), and dispatch the compute shader and draw mesh in Update().




Finally we need to destroy the buffer.



Now let's create the compute shader first, write the grass struct and databuffer into it and leave it. 


Write the compute buffer data into the V/F shader. We apply the transformation matrix to vertex using grassScale and clumps.position. Then we add normal and half-lambert light.




Finally we can get the static grass mesh rendered.







No comments:

Post a Comment

common art sprint#6

 Added some per instance randomness