I am generating island made of cubes in unity. For each cell in a grid, I draw a cube made of vertices and add it to a mesh.
Here is a sample of my code :
void DrawTerrainMesh(Cell[,,] grid){
Mesh mesh = new();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
for (int z = 0; z < z_size; z++) {
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
Cell cell = grid[x, y, z];
if (cell == null)
continue;
if (cell.cellType == Cell.CellType.Earth) {
// Calculate the positions of the 8 vertices
Vector3[] cubeVertices = new Vector3[8];
for (int i = 0; i < 2; i++) {
float xv = i == 0 ? 0.5f : -0.5f;
for (int j = 0; j < 2; j++) {
float yv = j == 0 ? 0.5f : -0.5f;
for (int k = 0; k < 2; k++) {
float zv = k == 0 ? 0.5f : -0.5f;
Vector3 vertex = new Vector3(xv + x - size/2, zv + z,yv + y - size/2);
cubeVertices[i * 4 + j * 2 + k] = vertex;
}
}
}
AddTriangles(vertices, triangles, cubeVertices[0], cubeVertices[2], cubeVertices[4], cubeVertices[6]); // Top
AddTriangles(vertices, triangles, cubeVertices[0], cubeVertices[1], cubeVertices[2], cubeVertices[3]); // left
AddTriangles(vertices, triangles, cubeVertices[2], cubeVertices[3], cubeVertices[6], cubeVertices[7]); // back
AddTriangles(vertices, triangles, cubeVertices[3], cubeVertices[1], cubeVertices[7], cubeVertices[5]); // Bottom
AddTriangles(vertices, triangles, cubeVertices[4], cubeVertices[6], cubeVertices[5], cubeVertices[7]); // right
AddTriangles(vertices, triangles, cubeVertices[0], cubeVertices[4], cubeVertices[1], cubeVertices[5]); // front */
}
}
}
}
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
print(mesh.vertices.Length);
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
}
The problem is that it only draw the first two layers of my island (which are supposed to be made more than 2 layers).
Here is an example :
So it was supposed to draw my whole island but instead it draws only the first two layers.
I know the problem doesn't come from my code itself (I mean not that I can see), because if i only draw the top face of my cubes, it draws every layers of the island :
AddTriangles(vertices, triangles, cubeVertices[0], cubeVertices[2], cubeVertices[4], cubeVertices[6]); // Top
//AddTriangles(vertices, triangles, cubeVertices[0], cubeVertices[1], cubeVertices[2], cubeVertices[3]); // left
//AddTriangles(vertices, triangles, cubeVertices[2], cubeVertices[3], cubeVertices[6], cubeVertices[7]); // back
//AddTriangles(vertices, triangles, cubeVertices[3], cubeVertices[1], cubeVertices[7], cubeVertices[5]); // Bottom
//AddTriangles(vertices, triangles, cubeVertices[4], cubeVertices[6], cubeVertices[5], cubeVertices[7]); // right
//AddTriangles(vertices, triangles, cubeVertices[0], cubeVertices[4], cubeVertices[1], cubeVertices[5]); // front */
Here you can see the top of the moutains of the island.
So my guess is that unity limits the number of vertices a mesh can have. Is it true? How to deal with this?
Any idea?
This will depend on the mesh index format. By default, it will use uint16 indexing which is a maximum if 65536 vertices. Index format can also be made to use uint32, which bumps this number up to 4 billion. For more information, see the documentation about Mesh.indexFormat