I’ve implemented the marching cubes algorithm, successfully.
However, it only works so long as the chunk is smaller than 19 x 19 x 19 in size.
For some reason, at that size, the vertices are not in the correct locations, the mesh is lopsided and broken.
Here’s the correct output at 18^3:
The broken output at 19^3:
As the size increases, more and more of the vertices on one side are compressed into the other.
This is the code that creates the vertices for the mesh.
I tried replacing the algorithm with just filling the entire mesh with triangles to confirm the mesh volume, and that’s it really is broken and it’s not a mistake with the algorithm, I confirmed that much.
I’m completely lost at this point.
I don’t really need the chunks to be that much larger, but I wish I could solve this bug.
Any help is appreciated.
private Vector3[] InitSharedVertices() {
var verts = new List<Vector3>();
var vertsArr = new Vector3[12 * ((size.x - 1) * (size.y - 1) * (size.z - 1))];
for (int i = 0; i < size.x - 1; i++) {
for (int j = 0; j < size.y - 1; j++) {
for (int k = 0; k < size.z - 1; k++) {
// 12 points for each cube.
// The center of the current cube in local coordinates.
var c = new Vector3(halfCubeSize * (2 * i + 1), halfCubeSize * (2 * j + 1), halfCubeSize * (2 * k + 1));
// Note: Do not change this order.
// Bottom - back, left, front, right
verts.Add(c + new Vector3(0, -halfCubeSize, -halfCubeSize));
verts.Add(c + new Vector3(-halfCubeSize, -halfCubeSize, 0));
verts.Add(c + new Vector3(0, -halfCubeSize, halfCubeSize));
verts.Add(c + new Vector3(halfCubeSize, -halfCubeSize, 0));
// Middle - left back, left front, right front, right back
verts.Add(c + new Vector3(-halfCubeSize, 0, -halfCubeSize));
verts.Add(c + new Vector3(-halfCubeSize, 0, halfCubeSize));
verts.Add(c + new Vector3(halfCubeSize, 0, halfCubeSize));
verts.Add(c + new Vector3(halfCubeSize, 0, -halfCubeSize));
// Top - back, left, front, right
verts.Add(c + new Vector3(0, halfCubeSize, -halfCubeSize));
verts.Add(c + new Vector3(-halfCubeSize, halfCubeSize, 0));
verts.Add(c + new Vector3(0, halfCubeSize, halfCubeSize));
verts.Add(c + new Vector3(halfCubeSize, halfCubeSize, 0));
}
}
}
return verts.ToArray();
}