Marching cubes generating holes in the grid

I am working on implementing Marching Cubes in Unity. My code is based on the Paul Bourke code actually with a lot of changes, but in any case, I check to see if the block in position is zero if it is less than the debugging texture.

Here is an image of the problem

This is my MC script

public class MarchingCubes { private World world; private Chunk chunk; private List<Vector3> vertices = new List<Vector3> (); private List<Vector3> normals = new List<Vector3> (); private Vector3[] ns; private List<int> triangles = new List<int> (); private List<Vector2> uvs = new List<Vector2> (); private Vector3[] positions = new Vector3[8]; private float[] corners = new float[8]; private Vector3i size = new Vector3i (16, 128, 16); Vector3[] vertlist = new Vector3[12]; private float isolevel = 1f; private float Corner (Vector3i pos) { int x = pos.x; int y = pos.y; int z = pos.z; if (x < size.x && z < size.z) { return chunk.GetValue (x, y, z); } else { int ix = chunk.X, iz = chunk.Z; int rx = chunk.region.x, rz = chunk.region.z; if (x >= size.x) { ix++; x = 0; } if (z >= size.z) { iz++; z = 0; } return chunk.region.GetChunk (ix, iz).GetValue (x, y, z); } } Block block; public Mesh MarchChunk (World world, Chunk chunk, Mesh mesh) { this.world = world; this.chunk = chunk; vertices.Clear (); triangles.Clear (); uvs.Clear (); for (int x = 0; x < size.x; x++) { for (int y = 1; y < size.y - 2; y++) { for (int z = 0; z < size.z; z++) { block = chunk.GetBlock (x, y, z); int cubeIndex = 0; for (int i = 0; i < corners.Length; i++) { corners [i] = Corner (new Vector3i (x, y, z) + offset [i]); positions [i] = (new Vector3i (x, y, z) + offset [i]).ToVector3 (); if (corners [i] < isolevel) cubeIndex |= (1 << i); } if (eTable [cubeIndex] == 0) continue; for (int i = 0; i < vertlist.Length; i++) { if ((eTable [cubeIndex] & 1 << i) == 1 << i) vertlist [i] = LinearInt (positions [eCons [i, 0]], positions [eCons [i, 1]], corners [eCons [i, 0]], corners [eCons [i, 1]]); } for (int i = 0; triTable [cubeIndex, i] != -1; i += 3) { int index = vertices.Count; vertices.Add (vertlist [triTable [cubeIndex, i]]); vertices.Add (vertlist [triTable [cubeIndex, i + 1]]); vertices.Add (vertlist [triTable [cubeIndex, i + 2]]); float tec = (0.125f); Vector2 uvBase = block != null ? block.UV : new Vector2 (); uvs.Add (uvBase); uvs.Add (uvBase + new Vector2 (0, tec)); uvs.Add (uvBase + new Vector2 (tec, tec)); triangles.Add (index + 0); triangles.Add (index + 1); triangles.Add (index + 2); } } } } if (mesh == null) mesh = new Mesh (); mesh.Clear (); mesh.vertices = vertices.ToArray (); mesh.triangles = triangles.ToArray (); mesh.uv = uvs.ToArray (); mesh.RecalculateNormals (); return mesh; } bool IsBitSet (int b, int pos) { return ((b & pos) == pos); } Vector3 LinearInt (Vector3 p1, Vector3 p2, float v1, float v2) { Vector3 p; px = p1.x + (isolevel - v1) * (p2.x - p1.x) / (v2 - v1); py = p1.y + (isolevel - v1) * (p2.y - p1.y) / (v2 - v1); pz = p1.z + (isolevel - v1) * (p2.z - p1.z) / (v2 - v1); return p; } private static int[,] eCons = new int[12, 2] { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 }, { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 } }; private static Vector3i[] offset = new Vector3i[8] { new Vector3i (0, 0, 1), new Vector3i (1, 0, 1), new Vector3i (1, 0, 0), new Vector3i (0, 0, 0), new Vector3i (0, 1, 1), new Vector3i (1, 1, 1), new Vector3i (1, 1, 0), new Vector3i (0, 1, 0) }; } 

I did not put the tables in the sample, because they are the same as those specified in the Bourke code.

EDIT: I have not figured out that the cell value in the blue triangles is 0, so they should not be triangulated, but the cell value below them is 1, and because of this, the upper triangle is created to complete the grid.

+7
c # unity3d marching-cubes
source share

No one has answered this question yet.

See related questions:

1758
How do I generate a random int number?
1106
Create a generic method restricting T to Enum
662
Random number generator generates only one random number
4
Marching Cubes - Missing Triangles in a Grid
2
help: how to smooth the grid created by marching cubes in real time?
2
Unity Game Engine Crashing when trying to update vertex grid positions in real time using a script
one
double marching cubes table
one
SortedList cannot find some existing keys (Unity3d, C #)
0
Marching cubes isovalue
0
Calculation of normals, indices and UV coordinates for a marching cubes grid

All Articles