# How to merge generated meshes

Hi,
I have a vision of a procedural tile based (I like the tile based aesthetic) terrain generation but I am having trouble understanding how mesh merging works. I will attach my code and hopefully someone can point me in the right direction (or even suggest a better approach to this problem).

I expected this code to produce a 5 x 5 grid of tiles at y coord 0, but I don’t see anything when I run it. I think my problem lies in setting the transform in the CombineInstance. At the moment I am using Matrix4x4.TRS to produce a transform but to be honest I don’t really understand what I am doing, and the documentation is pretty sparse. In fact if anybody knows of a good resource for learning these kinds of processes I would be very grateful.

Thanks for reading, and for any help

``````public class TilesGenerate : MonoBehaviour
{

Mesh currentMesh;
public float tileHeight = 0.2f;

public int xWidth = 5;
public int zDepth = 5;

// Start is called before the first frame update
void Start()
{
currentMesh = GenerateMesh();
GetComponent<MeshFilter>().sharedMesh = currentMesh;
}

// Update is called once per frame
void Update()
{

}

Mesh CreateTile(float x, float z)
{
Mesh mesh = new Mesh();
Vector3[] Vertices = new Vector3[] {
new Vector3(x + 0, 0, z + 0), //0
new Vector3(x + 1, 0, z + 0), //1
new Vector3(x + 1, 0, z + 1), //2
new Vector3(x + 0, 0, z + 1), //3

new Vector3(x + 0, -tileHeight, z + 0),     //4
new Vector3(x + 1, -tileHeight, z + 0),     //5
new Vector3(x + 1, -tileHeight, z + 1),     //6
new Vector3(x + 0, -tileHeight, z + 1),     //7

new Vector3(x + 0, 0, z + 0),  //8
new Vector3(x + 1, 0, z + 0),  //9
new Vector3(x + 1, 0, z + 1),  //10
new Vector3(x + 0, 0, z + 1),  //11

new Vector3(x + 0, -tileHeight, z + 0), //12
new Vector3(x + 1, -tileHeight, z + 0), //13
new Vector3(x + 1, -tileHeight, z + 1), //14
new Vector3(x + 0, -tileHeight, z + 1)  //15

};

int[] Triangles = new int[] {
2, 1, 0,
3, 2, 0,

4, 5, 6,
4, 6, 7,

//sides
12, 15, 11,
12, 11, 8,

12, 8, 9,
9, 13, 12,

10, 14, 13,
13, 9, 10,

15, 14, 10,
10, 11, 15
};

mesh.vertices = Vertices;
mesh.triangles = Triangles;
mesh.RecalculateNormals();

return mesh;
}

Mesh GenerateMesh() {
CombineInstance[] combine = new CombineInstance[xWidth * zDepth];
int i = 0;
for (int z = 0; z < zDepth; z++)
{
for (int x = 0; x < xWidth; x++)
{
Mesh tile = CreateTile(x, z);
combine*.mesh = tile;*
``````

combine*.transform = Matrix4x4.TRS(new Vector3(x, 0, z), Quaternion.identity, Vector3.one);*
i++;
}
}

Mesh mesh = new Mesh();
mesh.CombineMeshes(combine);

return mesh;

}

void OnDrawGizmos()
{
if (currentMesh != null)
{
foreach (Vector3 vertex in currentMesh.vertices)
{
Gizmos.DrawSphere(vertex, 0.15f);
}
}
}
}

It is possible to combine generated meshes in runtime with the plugin “Easy Mesh Combiner MT”, available in the Asset Store. It is able to combine meshes without loss of quality. Unfortunately, it’s not free… But it’s pretty cheap. It’s worth checking out. Here is the link: Easy Mesh Combiner MT - Scene Mesh Merge, Atlasing Support & More | Game Toolkits | Unity Asset Store

Load the Meshes: Load the individual generated meshes that you want to merge. Each mesh should be represented by a set of vertices and faces/triangles. Align the Meshes: If the meshes are not already aligned properly, you may need to align them so that they fit together correctly. This typically involves translating, rotating, and scaling the meshes appropriately. There are various algorithms and techniques available for mesh alignment, such as Iterative Closest Point (ICP) or Procrustes analysis. prepaidgiftbalance