I'm working on a little project for a while - creating an area where you can spawn a bunch of grass. I've overcame a major headache when figuring out the GPU instancing for the mesh and the code looks like this:
public class Grass : MonoBehaviour
{
[HideInInspector]
public Mesh mesh;
[HideInInspector]
public List<Matrix4x4> matrices;
[HideInInspector]
public Material material;
private void Update()
{
Graphics.DrawMeshInstanced(mesh, 0, material, matrices);
}
}
Now that I'm done with this part I want to optimize things. Shaders of any kind and me are complete strangers so I'm asking for anyone's help on writing a compute shader that would work with my code to simulate frustum culling.
Thanks!
DrawMeshInstanced()is deprecated, you should useRenderMeshInstanced(). Is the grass placed on a Unity Terrain? If so, is there a reason you aren't using the vegetation features already built into the Terrain system? I'm not sure if there's any way to add frustrum culling withDrawMeshInstanced()orRenderMeshInstanced(), since the documentation makes it clear that all of the instanced meshes are treated as one object. \$\endgroup\$RenderMeshInstanced(), but you probably meant to do culling first and then only callRenderMeshInstanced()for the meshes that aren't culled. That approach makes sense, but I don't work with compute shaders enough to advise you on how to do it. You might consider first trying to implement frustum culling on the CPU, and then try a compute shader later if you see a performance bottleneck. \$\endgroup\$