Graphics.RenderMeshIndirect

Been looking over the new instancing render API in Graphics. RenderMeshIndirect seems to be missing a submesh index, so how do you render a mesh with multiple materials? Or am I just missing something?

Where is this in the docs?

It’s a newer API meant to replace the Graphics.Draw* functions.

1 Like

The sub mesh will be based on the index range you provide in the indirect args.

Ah ok, that makes sense. Thank you!

1 Like

Hey sorry to dig this up, but I’m still not clear on how you’d render multiple materials. You can break up the submesh, but each call to Graphics.RenderMeshIndirect only takes one material through it’s RenderParams, no? And it seems like calling RenderMeshIndirect multiple times every frame with different RenderParams doesn’t seem to work. So how would I render an object with multiple materials?

I’m not super familiar with the newer RenderMesh APIs but it seems to me like this should be the correct way to do it.

So after playing around with it for a bit, I think I got it to work:

    void Update()
    {
        uint index = 0;
        uint newIndex = 0;
        RenderParams rp;
        for (int i = 0;i<materials.Length;i++)
        {
            //list of materials used on the mesh renderer
            rp = new RenderParams(materials[i]);

            rp.worldBounds = new Bounds(Vector3.zero, 10000 * Vector3.one); // use tighter bounds for better FOV culling
            rp.matProps = new MaterialPropertyBlock();
            rp.matProps.SetMatrix("_ObjectToWorld", Matrix4x4.Translate(new Vector3(0, 0, 0)));

            //offset the startIndex based on how many indexes we've traversed
            commandData[0].startIndex = index;
            newIndex = mesh.GetIndexCount(i);
            index += newIndex;
            commandData[0].indexCountPerInstance = newIndex;
            commandData[0].instanceCount = 10000;

            commandBufs[i].SetData(commandData);
            Graphics.RenderMeshIndirect(rp, mesh, commandBufs[i], commandCount);
        }

    }

I believe the main thing I changed that fixed it was using a different Command Buffer for each call rather than reusing the same Command Buffer each time. Does that make sense? The whole Command Buffer concept is new to me and there isn’t a ton of documentation on the new RenderMesh methods.

3 Likes