Wednesday, July 17, 2024

Common art sprint #5

 

Final Rendering video. There are some noise, need to denoise them for the following sprint.




Monday, July 1, 2024

Wednesday, June 19, 2024

Wednesday, February 28, 2024

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 doesn't cast or reveive shadow. First we will add shadow to the default grass then add shadow to the grass with command buffer.

In the grass shader, we need two pass. The first pass to receice shadow and the second pass to cast shadow. We will be using Unity's built in shadow parameters and functions.



Run the code and we can see the grass is casting shadow to the environment.



However we lost the grass in the camera because we add a new pass and DrawMeshInstancedIndirect() needs a new parameter. 

We need to fix this in the C# script by using material property block.

Material property block allows us to draw multiple objects with the same mesh but different material property.



Now we successfully add shadow to the grass.

 



Now let's add shadow with command buffer.

We need to create our own shadow map because the built-in shadowmap cannot get the instanced mesh. Thus we need to add a command buffer to cast shadow.

In the C# script, it is very similar to previous method. We declare the property block along with other parameters we need to get the shadow map and initialize them.

To get the shadow map, we add a camera to the directional light, set it to be orthogonal and disable it.

 


Create a function to prepare what we need to get the shadow map.



Now create a shader to generate the shadow map. We need two passes, one for the noninstanced objects and one for the instanced mesh grass. The fragment shader is the same. We need to process the vertex differently.




Now we get a shadow map.



Now we use a new material and a new shader to replace our previous grass material and shader.

In this shader, we add the shadow data we get from our custom shadow map. We need to write in our shadow map and get the matrix from world space to light space because the shadow coordinate we get is from the virtual camera we put on the directional light.

In the OnPreRender(), we need to draw the grass from the light position so that we can get where the grass should cast shadow. We then need to draw the grass from the main camera and it will receive shadow.






Now we successfully add shadow to our grass.



Also with the shadow infomation, we can see that our culling is working.









common art sprint#6

 Added some per instance randomness