0
\$\begingroup\$

I have millions of objects that I want to render. I found that instantiate is not suitable, so I'm using Graphics.RenderMesh which is amazingly fast and light. But here comes the problem: it takes CPU resources because we are using a loop for each object and then rendering it:

void DrwaTree(Vector3 position, Quaternion rotation, Vector3 scale)
{
    Matrix4x4 matrix = Matrix4x4.TRS(position, rotation, scale);
      
    for (int i = 0; i < materials.Length; i++)
    {
        RenderParams rp = new RenderParams(materials[i]);
        rp.shadowCastingMode = TreeShadowCasting[i];
        Graphics.RenderMesh(rp, TreeMesh, i, matrix);
    }
}

This function is called a million times in different scripts with different meshes. I want to find a way to make it less CPU-intensive.

One current approach I am applying is to bring different objects under a common center point (multiple center points for different objects). Then, I calculate the distance between the center point and the camera position to execute a specific loop only for objects near the camera. Is there any alternative or more appropriate approach?

\$\endgroup\$
3
  • \$\begingroup\$ Have you looked into using instanced rendering, using DrawMeshInstanced? This lets you draw many copies of the same mesh with different transformation matrices in one big batched call. \$\endgroup\$ Commented Jan 16, 2024 at 13:09
  • \$\begingroup\$ Yeah we have tried that but it is limited to 1024 objects \$\endgroup\$ Commented Jan 19, 2024 at 7:06
  • 1
    \$\begingroup\$ So you render one batch of 1024, then a second batch for items 1025-2048... You can make one batch per chunk to completely skip drawing all trees in chunks that are outside the camera frustum. If you have more than a few batches this size, it's probably time to stop drawing full meshes with multiple materials for every tree anyway and switch to imposters for the more distant ones. \$\endgroup\$ Commented Jan 19, 2024 at 12:05

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.