0
\$\begingroup\$

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!

\$\endgroup\$
4
  • \$\begingroup\$ DrawMeshInstanced() is deprecated, you should use RenderMeshInstanced(). 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 with DrawMeshInstanced() or RenderMeshInstanced(), since the documentation makes it clear that all of the instanced meshes are treated as one object. \$\endgroup\$ Commented Mar 1, 2024 at 0:15
  • \$\begingroup\$ I will look into the command RenderMeshInstanced() but that was not really the point of my question. As I said in my post, it's a little project of mine where I challenged myself on making grass mesh by myself through script - it's not about the functionality. I think there is. I've seen many implementation where they calculate what matrices are visible to the camera and what not, then by getting it from the buffer they display those matrices -> that's the part I don't know. I know very little about compute shaders or getting/setting data from/for them. \$\endgroup\$ Commented Mar 1, 2024 at 12:34
  • \$\begingroup\$ I was thinking about this backwards - I thought you wanted to perform frustum culling after calling RenderMeshInstanced(), but you probably meant to do culling first and then only call RenderMeshInstanced() 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\$ Commented Mar 2, 2024 at 1:52
  • 1
    \$\begingroup\$ Either way, it's generally best to take a stab at it yourself before asking for help, so you can say "I've tried this and got stuck here", instead of what could be interpreted as "please do the whole thing for me". \$\endgroup\$ Commented Mar 2, 2024 at 2:09

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.