- Home /
How do I combine meshes in code?
So let's say I have six separate sides that I created that represents a cube. But I'll need to mash all six side meshes into one mesh that would represent a cube. And to be even more efficient, I need to take all cube meshes within a 16x16x256 area and combine them into a chunk mesh, which will then be attached to a GameObject. So I know how to create all the different sides (right, left, top, bottom, front, back) but I just can't figure out to combine the meshes together. I also need to have multiple materials for the same mesh if that is possible. The code below demonstrates how I go about creating a side.
public Mesh CreateSide(Vector3[] vertices, int[] triangles){
Mesh mesh = new Mesh ();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = standardMeshUV;
MeshUtility.Optimize (mesh);
mesh.RecalculateNormals ();
return mesh;
}
public Mesh CreateFrontSide(){
Vector3[] vertices = new Vector3[] {
new Vector3 (-1, 1, 1),
new Vector3 (1, 1, 1),
new Vector3 (-1, -1, 1),
new Vector3 (1, -1, 1)
};
int[] triangles = new int[] {
0, 2, 3,
3, 1, 0,
};
return CreateSide (meshFilter, vertices, triangles);
}
Did you try the example of the documentation?
https://docs.unity3d.com/ScriptReference/$$anonymous$$esh.Combine$$anonymous$$eshes.html
There's a lot going on in that example, I'm looking for the simplest example of combining a list of meshes into one mesh.
Okay, I don't think $$anonymous$$esh.Combine$$anonymous$$eshes is what I'm looking for, I think that I just need to add in all the vertices and triangles and such from every mesh that I'm looking at.
Answer by $$anonymous$$ · Aug 27, 2018 at 02:02 AM
Apparently, the simplest option is to just store all the meshes in GameObjects, combine them using mesh.CombineMeshes, and then delete all the GameObjects that were created. I'm sure there is a better option in terms of taking up fewer resources, but it seems to work alright.
public GameObject CreateChunk(short chunkX, short chunkY){
List<GameObject> physicalBlocks = new List<GameObject> ();
for (short y = 0; y < height; y++) {
for (short x = (short)(chunkX * chunkSize); x < (short)(chunkX * chunkSize) + chunkSize; x++) {
for (short z = (short)(chunkY * chunkSize); z < (short)(chunkY * chunkSize) + chunkSize; z++) {
GameObject physicalBlock = CreatePhysicalBlock (blocks [x, y, z]);
if (physicalBlock != null) {
physicalBlocks.Add (physicalBlock);
physicalBlock.name = x + "," + y + "," + z;
}
}
}
}
//Merge physical blocks into one chunk.
CombineInstance[] combine = new CombineInstance[physicalBlocks.Count];
for(int i = 0; i < physicalBlocks.Count; i++){
combine[i].mesh = physicalBlocks[i].GetComponent<MeshFilter> ().sharedMesh;
combine[i].transform = physicalBlocks[i].transform.localToWorldMatrix;
}
GameObject chunk = new GameObject ();
chunk.AddComponent<MeshCollider> ();
chunk.AddComponent<MeshFilter> ();
chunk.AddComponent<MeshRenderer> ();
chunk.transform.GetComponent<MeshFilter> ().mesh = new Mesh ();
chunk.transform.GetComponent<MeshFilter> ().mesh.CombineMeshes (combine, true);
chunk.GetComponent<MeshCollider> ().sharedMesh = chunk.GetComponent<MeshFilter> ().sharedMesh;
chunk.GetComponent<MeshRenderer>().material = atlasMaterial;
chunk.transform.GetComponent<MeshFilter> ().mesh.RecalculateBounds ();
chunk.transform.GetComponent<MeshFilter> ().mesh.RecalculateNormals ();
MeshUtility.Optimize (chunk.GetComponent<MeshFilter> ().mesh);
for (int i = physicalBlocks.Count; i > 0; i--) {
Destroy (physicalBlocks[i - 1]);
}
chunk.name = "Chunk_" + chunkX + "_" + chunkY;
return chunk;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612173432im_/https://answers.unity.com/themes/thub/images/avi.jpg)