Sunday, February 25, 2024

GPU Instanced Grass in Unity - Part 5

 

GPU Instanced Grass in Unity


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


Part 5: More Optimization with Hi-Z Occlusion Culling


We want to cull off those grass which are on the opposite of terrain.

We can use hierarchical depth buffer to determine which parts of a scene are occluded.

We first want to get the depth map of current scene. We want the depth texture to be able to generate mipmap.




To get the current depth texture, we have to do it inside OnPostRender().





Now we need to generate the mipmap of the depth texture.



We create a new shader and calculate the max depth value of each pixel of the mipmap.

For each pixel on the current mipmap, we sample the surround four pixel and get the min depth value of them, the result is the depth value of the corrsponding pixel on next level mipmap.




Now for each AABB of the grass, we compare the depth value of the bounding box with the depth value of the mipmap, and decide whether to cull it.




In the C# script, we send the depth texture and texture size to the compute shader.



We need to know the vertex coordinate of the bounding box inside the NDC coordinate system, and compare the z component to the depth value stored in the depth texture.









No comments:

Post a Comment

common art sprint#6

 Added some per instance randomness