Monday, February 26, 2024

GPU Instanced Grass in Unity - Part 6

 

GPU Instanced Grass in Unity


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


Part 6: Use Command Buffer to change the rendering order



Right now there is some glitching when we rotate the camera fast.

It is becasue we get the depth texture and generate hi-z inside OnPostRender(), which happens when the camera finishes render the scene. We can see the mesh is drawn before the depth mipmap is drawn, so we cannnot get the Hi-Z of current frame.

With command buffer, we can adjust the rendering order.

We can get the depth texture, generate Hi-Z mipmap, then draw the grass.




We duplicate the C# script grass.cs and rename the class to be GrassWithCommandBuffer.

We declare three command buffers, and add them.



We comment out the OnPostRender() because we want to get the depth texture before the grass is drawn. We also don't need to dispatch the compute shader inside Update() since we are gonna do that together in OnPostRender( ).



 

We add the command buffer in Start.



Now we need another pass to get the camera depth in our DepthTextureMipmap shader.



Now we are able to get the depth texture before the grass is been drawn. We get the correct result now and as we can see the FPS is not being affected .






No comments:

Post a Comment

GPU Instanced Grass in Unity - Part 7

  GPU Instanced Grass in Unity For the original post, please go to  https://www.yuque.com/qwerasdwww/dfy5xd Part 7: Add Shadow Now the grass...