Why does my Chunk generator reset the vertex positions?
I am currently working on a chunk generator for a block- based game like Minecraft, but with hexagonal blocks instead of cubes. The basic idea to generate one chunk is taking the vertices of one template block and copying them into a new mesh, adding an offset everytime, to put the blocks into the right position. And it basically works, but only with relatively small amounts of blocks like in the first picture with a size of 10x10x10 (H, W, D) blocks.
But if I get to a certain amount of blocks the vertex positions seem to be reset to the chunk objects origin, so the next blocks will be generate inside the already existing blocks, (pictures 2 and 3)
This is the code I use to calculate the Vertex coordinates (it's called in the Start method):
void GetBlockData() { Mesh mesh = GetComponent().mesh; blockVertices = new Vector3[mesh.vertices.Length];
for(int i = 0; i < mesh.vertices.Length; i++)
{
Vector3 temp = mesh.vertices[i];
Vector3 newVec = new Vector3(temp.x, temp.z, temp.y);
blockVertices[i] = newVec;
}
blockUV = mesh.uv;
blockTriangles = mesh.triangles;
for (int i = 0; i < blockTriangles.Length - 2; i += 3)
{
int a = blockTriangles[i];
int b = blockTriangles[i + 2];
blockTriangles[i] = b;
blockTriangles[i + 2] = a;
}
}
Why does that happen? Is there anything I don't see? I literally tried everything that came into my mind to fix it, but I cant find the mistake.
I'm actually having the exact same issue.. it's so baffling would love to know if you ever figured this one out!
Answer by DJ_Design · Nov 18, 2021 at 10:47 AM
Hi, I found the issue to be mesh limitations inside unity. You will notice, if you remove the sides/bottom of your mesh, the chunk create further along your defined limits.
To remedy this, optimize your mesh by reducing verts, and consider splitting your mesh into separate meshes. There is also the option to do; mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
after you initialize your mesh, which will gave you a TON more room at the cost of more allocation, instead of 65k you get up to 4 BILLION vertices! Enjoy.
Hope this helps!