- Home /
Mesh.CombineMeshes - only "main" meshes are combined
Here's the thing: I'm trying to combine meshes in one object for optimization reasons. But the thing is that it doesn't include stuff that is in different submeshes, only "main" mesh of the object is combined. Here's the code I'm using for that:
if (LevEditGlobals.currentGO.transform.childCount>0){
ArrayList arr = new ArrayList();
for (int i = 0;i<LevEditGlobals.currentGO.transform.childCount;i++){
MeshFilter mf = LevEditGlobals.currentGO.transform.GetChild(i).gameObject.GetComponent<MeshFilter>();
if (mf!=null){
//some children may not have meshfilters
CombineInstance ci = new CombineInstance();
ci.mesh = mf.sharedMesh;
ci.transform = mf.transform.localToWorldMatrix;
arr.Add (ci);
}
}
CombineInstance ci2 = new CombineInstance();
ci2.mesh = LevEditGlobals.currentGO.GetComponent<MeshFilter>().sharedMesh;
ci2.transform = LevEditGlobals.currentGO.GetComponent<MeshFilter>().transform.localToWorldMatrix;
arr.Add(ci2);
CombineInstance[] cis = arr.ToArray(typeof(CombineInstance)) as CombineInstance[];
propermesh = new Mesh();
propermesh.name = "Combined Object";
propermesh.CombineMeshes(cis);
meshchanged = true;
}
It's quite lengthy, but the gist is it walks through children of specific object (if there's any) and if specific child has meshfilter, it adds its mesh to the CombineInstance ArrayList, then at the end adds "parent" object to it, generate CombineInstance array out of arraylist and combine meshes.
How to make it combine submeshes of the object if it has any as well?
Are you sure that it's not just invisible because of a missing material or something? By default, i think, Combine$$anonymous$$eshes() does also include submeshes, if not specified otherwise.
100% positive - when I pause the game and select object in question Unity's wireframe selector doesn't show "missing" parts.
A look at the Scripting API reveals that Combine$$anonymous$$eshes also accepts a parameter that flattens all submeshes into one mesh, maybe that will help?
Bummer. Have you tried creting a new mesh via script, passing it the vehicles and triangles from the meshes that are to be combined manually?
I don't think I'm advanced enough to do that. Plus, meshes aren't necessarily aligned, pivot-wise so I don't know how to compensate for that either.
Shouldn't be too hard. Something like this:
public $$anonymous$$esh[] meshArray = new $$anonymous$$esh[0];
public $$anonymous$$esh new$$anonymous$$esh;
private void CombineAll$$anonymous$$eshes() {
new$$anonymous$$esh = new $$anonymous$$esh();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Vector2> uvs= new List<Vector2>();
foreach($$anonymous$$esh mesh in meshArray) {
foreach(Vector3 vertex in mesh.vertices) {
vertices.Add(vertex);
}
foreach(int triangleIndex in mesh.triangles) {
triangles.Add(triangleIndex );
}
foreach(Vector2 uv in mesh.uv) {
uvs.Add(uv);
}
new$$anonymous$$esh.vertices = vertices.ToArray();
new$$anonymous$$esh.triangles = triangles.ToArray();
new$$anonymous$$esh.uv= uvs.ToArray();
}
}
Your answer
Follow this Question
Related Questions
Gather all the Meshes into 1 1 Answer
Merging meshes 0 Answers
Are submeshes of combined mesh can share the same mesh instance? 0 Answers
Static batching - Getting submesh 1 Answer
How to know which material is used by a submesh in script ? 0 Answers