danp
1
Using the combine meshes example from the docs is creating long triangles where they shouldn’t be.
Before combining.
After combining.
Here’s the code that combines the meshes
public static void Combine (GameObject go) {
MeshFilter[] meshFilters = go.GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length)
{
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.active = false;
i++;
}
go.transform.GetComponent<MeshFilter>().mesh = new Mesh();
go.transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
go.active = true;
}
Am I missing something? Does CombineMeshes not work?
So I know this is a couple years to late for you DanP, but if anyone else runs into this like I have, here is the issue:
The resulting mesh has too many verts.
I solved this by creating a List<CombineInstance[ ]>
public void CombineMeshesOfLikeMaterials()
{
foreach(GameObject parent in shipPartParentsOfUniqueMaterialColors)
{
MeshFilter[] meshFilters = parent.GetComponentsInChildren<MeshFilter>();
List<CombineInstance> combineSubMeshes = new List<CombineInstance>();
List<CombineInstance[]> combineList = new List<CombineInstance[]>();
int currentMeshVertCount = 0;
for (int i = 0; i < meshFilters.Length; i++)
{
if (currentMeshVertCount + meshFilters[i].sharedMesh.vertexCount >= 65000)
{
combineList.Add(combineSubMeshes.ToArray());
combineSubMeshes = new List<CombineInstance>();
currentMeshVertCount = 0;
}
currentMeshVertCount += meshFilters[i].sharedMesh.vertexCount;
CombineInstance combine = new CombineInstance();
combine.mesh = meshFilters[i].sharedMesh;
combine.transform = meshFilters[i].transform.localToWorldMatrix;
combineSubMeshes.Add(combine);
Object.Destroy(meshFilters[i].gameObject);
}
// Add last submesh that was under 65000 verts
combineList.Add(combineSubMeshes.ToArray());
for (int i = 0; i < combineList.Count; i++)
{
GameObject shipPartParent = new GameObject(parent.name + "_" + i.ToString());
MeshFilter parentMeshFilter = shipPartParent.AddComponent<MeshFilter>() as MeshFilter;
parentMeshFilter.mesh = new Mesh();
parentMeshFilter.mesh.CombineMeshes(combineList[i]);
parentMeshFilter.transform.position = Vector3.zero;
shipPartParent.SetActive(true);
MeshRenderer parentRenderer = shipPartParent.AddComponent<MeshRenderer>() as MeshRenderer;
Material parentMat = Object.Instantiate((Material)Resources.Load("SvgDeckMaterial", typeof(Material)));
parentMat.color = parent.GetComponent<MeshRenderer>().material.color;//partColor;
parentRenderer.material = parentMat;
shipPartParent.transform.SetParent(parent.transform);
}
}
}
2 Likes
In 2021 it can be solved by increasing the indexFormat:
Mesh finalMesh = new Mesh();
finalMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;