So with my current prototype I have a 50x50x6 level of constructed cubes from procedural generation methods, with an amount of 1x1x1 cubes upwards of 10,000. I’m noticing that the performance of this on my machine (which is fairly high-end) is rather slow, and there is a low frame rate. Any recommendations for improving performance?
Hmm… after some experimentation, it seems that the CombineMeshes function has an upper limit of UInt16.Max, or 65536. So after hacking some code together, it seems that while my floors and ceilings (each grouped together because of a shared material) are all placed in the scene, only a fraction of the walls are.
Do implementations divide up meshes to combine in logical groups? (E.g., rooms, passageways, etc.)
var floorComponent = new List<Component>();
....
for (int x = 0; x < the3dMap.Length.X; ++x)
{
for (int y = 0; y < the3dMap.Length.Y; ++y)
{
for (int z = 0; z < the3dMap.Length.Z; ++z)
{
created = null;
MapCell3D cell = the3dMap.GetCell(x, y, z);
if (cell.Flags == MapCellFlag.Floor)
{
created = (GameObject)Instantiate(floor, new Vector3(x, y, z), Quaternion.identity);
created.transform.parent = myMap.transform;
floorComponent.Add(created.GetComponent<MeshFilter>());
}
...
}
}
}
int max = UInt16.MaxValue;
var floorMaterial = new Material(floor.GetComponent<MeshRenderer>().material);
....
CombineInstance[] floorCombine = new CombineInstance[floorComponent.Count];
int count = 0;
foreach (var componentMesh in floorComponent)
{
var floorMesh = (MeshFilter) componentMesh;
floorCombine[count].mesh = floorMesh.sharedMesh;
floorCombine[count].transform = floorMesh.transform.localToWorldMatrix;
floorMesh.gameObject.active = false;
++count;
}
floorGameObject.AddComponent<MeshFilter>();
floorGameObject.GetComponent<MeshFilter>().mesh.CombineMeshes(floorCombine);
floorGameObject.AddComponent<MeshCollider>();
floorGameObject.AddComponent<MeshRenderer>();
floorGameObject.GetComponent<MeshRenderer>().material = floorMaterial;
floorGameObject.active = true;
Also, after combining the meshes, several cubes in the combined mesh don’t display their materials properly. The upper triangle on the outward-facing side of cube section of wall is a warped version of the material.
Also… my prefabs often lose their materials after completing a test play of the scene. Any ideas?