- Home /
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()
{
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
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[] {
//top quad
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
//bottom quad
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
//top quad copy for sides
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
//bottom quad copy for sides
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[] {
//top quad
2, 1, 0,
3, 2, 0,
//bottom quad
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[i].mesh = tile;
combine[i].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);
}
}
}
}
I have decided on a different and much more sensible approach following a tutorial by b3agz on youtube to create a $$anonymous$$ecraft-like game, and then simply scale the whole mesh down on the y axis to make the tiles flat instead of cubes. the first video is here: https://www.youtube.com/watch?v=h66IN1Pndd0&list=PLVsTSlfj0qsWEJ-5e$$anonymous$$tXsYp03Y9yF1dEn
I highly, highly recommend his videos briliiantly explained, even for (almost) beginners
Your answer
Follow this Question
Related Questions
CombineMeshes not working on one machine but is on another 1 Answer
Combining meshes (different materials) together for rotation/translation 1 Answer
Combining meshes clears old mesh 0 Answers
Drawcalls vs MeshCombiners 1 Answer
skinned mesh renderer, combine meshes can't see the result, what am I doing wrong/ missing? 0 Answers