Search Unity

Resolved Confuse About Performance tests using two identical gameobjects with very different results

Discussion in 'General Graphics' started by dr4, Jul 26, 2021.

  1. dr4

    dr4

    Joined:
    Jan 14, 2015
    Posts:
    108
    I have been doing some performance tests today by generating millions of meshes and combining them together, I was having very bad performance issues (expected) but when I saw why I was very very confuse:

    I have a very simple lowpoly mesh, I put a script together to multiply that mesh a million times, that test went flawless using this code:


    Code (CSharp):
    1.     void Generate()
    2.     {
    3.         combine = new CombineInstance[1000000];
    4.  
    5.         for (int i = 0; i < 1000000; i++)
    6.         {
    7.             if(numberToGenerate == numberGenerated)
    8.             {
    9.                 break;
    10.             }
    11.  
    12.             GeneratedObjectsPosition(objectToGenerate);
    13.             combine[i].mesh = meshFilter.sharedMesh;
    14.             combine[i].transform = objectToGenerate.transform.localToWorldMatrix;
    15.             numberGenerated++;
    16.         }
    17.  
    18.         PrepareContainer().GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
    19.  
    20.         if(numberGenerated < numberToGenerate)
    21.         {
    22.             Generate();
    23.         }
    24.     }
    I ended up with what I wanted:

    upload_2021-7-26_23-37-51.png

    and for my surprise the performance is amazing so I decided to go for 2 millions. As you can see in the code above I'm diving the objects in 2 (of a million each) to avoid problems (unity crashes if I try to combine more than that in one object), so after running that code I end up with 2 million meshes in 2 different objects:

    upload_2021-7-26_23-40-24.png

    But now the performance is awful, I can barely do anything in Unity at all, I assumed that this was the limit but then I noticed something odd, if I delete the second Mesh, and multiple the first one (so I have 2 million again) the performance is amazing, and if I multiply it 2 more times the performance is still very good, but if I delete the first Mesh and multiply the second one the performance is beyond terrible, so basically the first object has an awesome performance even if I multiply it many times but the second has a terrible performance, and they are identical, being created in the exact same way and applying the same rules, the code use to create those objects before combining meshes in them:


    Code (CSharp):
    1.     GameObject PrepareContainer()
    2.     {
    3.         var cloneOriginalContainer = Instantiate(originalContainer, transform);
    4.         var format = IndexFormat.UInt32;
    5.         cloneOriginalContainer.GetComponent<MeshFilter>().mesh = new Mesh { indexFormat = format };
    6.         return cloneOriginalContainer;
    7.     }
    the first one has an amazing performance, the last one has a terrible one but they are identical clones of that first gameobject (that one is always empty, I'm just using it as a mold for the others):

    upload_2021-7-26_23-53-17.png

    Would someone be able to clarify why this is happening?
     

    Attached Files:

  2. dr4

    dr4

    Joined:
    Jan 14, 2015
    Posts:
    108
    the solution was create a bew Mesh instead of use the original, I still don't understand why (I cannot see any difference in the result), but adding this instead of using the shared mesh directly solved the issue:

    Code (CSharp):
    1. var newMesh = (Mesh)Instantiate(meshFilter.sharedMesh);