User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active

Combine at Runtime

Easy Combine could, of course, also be used at runtime. But because some the features are not accessible at runtime, such as lightmap unwrapping etc. one needs to write some lines of code to implement a custom mesh grouper that calls Easy Combine API methods.

To allow a grouping of large meshes one have to check the vertex pointer for overflows (values that exceed the range of the 16-bit integer):

private static List<List<GameObject>> GetCombineGroups(IList<GameObject> selectedObjects)
{
      List<List<GameObject>> groups = new List<List<GameObject>>();
      groups.Add(new List<GameObject>());
      List<GameObject> active = groups[0];

      int vertexCount = 0;

      foreach (GameObject obj in selectedObjects)
      {
           Mesh mesh = obj.GetMesh();

           if (mesh != null)
           {
                int count = mesh.vertexCount;
                if (vertexCount + count < UInt16.MaxValue)
                {
                    active.Add(obj);

                    vertexCount += count;
                }
                else
                {
                    active = new List<GameObject>();
                    active.Add(obj);

                    groups.Add(active);

                    vertexCount = count;
                }
            }
        }

        return groups;
   }

 

The combining can be started as follows:

    List<List<GameObject>> groups = GetCombineGroups(objectsToCombine);



    foreach(List<GameObject> group in groups)
    {
        GameObject combinedObject = EasyCombine.Combine(group, UV2Mode.Keep, false, false);
    } 

 

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active

Performance Considerations

Easy Combine isn't meant to be a replacement of other optimization techniques currently present in Unity, but as an essential extension for good framerates. Generally, one always has to make a trade-off between all optimization techniques to benefit the most.
When combining your entire game world into one mega mesh, one could probably save draw calls, but the occlusion culling procedure cannot cull anything because the mega mesh is always in sight, which, in turn, results in more drawn geometry.


The best practice is to combine objects that can be put together in local visibility groups. Such a visibility group could be a table with all its dishes or a shelf with a bunch of books. This way, the occlusion system can calculate reasonable occlusion trees, and the system can cull a lot of the objects while the objects in sight have just a few draw calls.

One has to find the right balance between draw calls and occlusion culling group sizes to get the best possible performance.

 

Copyright © 2020 by Sycoforge Technologies. All Rights Reserved.