So I recently got marching cubes working (almost) and it works fine except this is happening. The thing is that it is only happening to the vertices furthest from me if I am standing at 90 degrees and at the vertices that are working.
Here is a closer example
If anyone could just give me some ideas as to why this is happening, I feel like maybe something is wrong with my density values or something, but if this has happened to you or you think you know what might be happening I would LOVE to hear it. If you would like to see my code then please just ask and I will send it.
3dRaven
(3dRaven)
February 1, 2025, 7:15pm
2
Looks like you might be missing the last vertex when building your triangle mesh. You are probably looping through a set of positions. Are you sure you didn’t do a for loop with less than instead of less or equal to?
Here is my MarchCube function where I actually build the triangle mesh,
void UMarchingCubesMeshComponent::MarchCube(
const FVector& Position,
const TArray<float>& CornerValues,
TArray<FVector>& Vertices,
TArray<int32>& Triangles,
TArray<FVector>& Normals)
{
// Calculate cube index
int32 CubeIndex = 0;
for (int32 i = 0; i < 8; i++)
{
if (CornerValues[i] < SurfaceLevel)
{
CubeIndex |= (1 << i);
}
}
// Get edge flags
int32 EdgeFlags = CubeEdgeFlags[CubeIndex];
if (EdgeFlags == 0) return;
// Calculate vertex positions
TArray<FVector> EdgeVertices;
EdgeVertices.SetNum(12);
const float Scale = 100.0f;
for (int32 i = 0; i < 12; i++)
{
if ((EdgeFlags & (1 << i)) != 0)
{
// Get the two vertices that make up this edge
int32 V1 = EdgeConnection[i][0];
int32 V2 = EdgeConnection[i][1];
// Get the positions of the edge vertices
FVector P1 = Position + FVector(
VertexOffset[V1][0] * Scale,
VertexOffset[V1][1] * Scale,
VertexOffset[V1][2] * Scale
);
FVector P2 = Position + FVector(
VertexOffset[V2][0] * Scale,
VertexOffset[V2][1] * Scale,
VertexOffset[V2][2] * Scale
);
// Interpolate to find exact intersection point
EdgeVertices[i] = InterpolateVerts(P1, P2, CornerValues[V1], CornerValues[V2]);
}
}
// Track unique vertices
TMap<FVector, int32> VertexIndices;
int32 BaseVertexIndex = Vertices.Num();
// Create triangles
for (int32 i = 0; TriangleConnectionTable[CubeIndex][i] != -1; i += 3)
{
TArray<int32> TriIndices;
TriIndices.SetNum(3);
// Process each vertex of the triangle
for (int32 j = 0; j < 3; j++)
{
int32 EdgeIndex = TriangleConnectionTable[CubeIndex][i + j];
FVector Vertex = EdgeVertices[EdgeIndex];
// Try to find existing vertex
const int32* ExistingIndex = VertexIndices.Find(Vertex);
if (ExistingIndex)
{
TriIndices[j] = *ExistingIndex;
}
else
{
// Add new vertex
int32 NewIndex = Vertices.Add(Vertex);
VertexIndices.Add(Vertex, NewIndex);
TriIndices[j] = NewIndex;
// Calculate normal
FVector Normal;
if (j == 0)
{
FVector Edge1 = EdgeVertices[TriangleConnectionTable[CubeIndex][i + 1]] - Vertex;
FVector Edge2 = EdgeVertices[TriangleConnectionTable[CubeIndex][i + 2]] - Vertex;
Normal = FVector::CrossProduct(Edge1, Edge2).GetSafeNormal();
}
else if (j == 1)
{
FVector Edge1 = EdgeVertices[TriangleConnectionTable[CubeIndex][i + 2]] - Vertex;
FVector Edge2 = EdgeVertices[TriangleConnectionTable[CubeIndex][i]] - Vertex;
Normal = FVector::CrossProduct(Edge1, Edge2).GetSafeNormal();
}
else
{
FVector Edge1 = EdgeVertices[TriangleConnectionTable[CubeIndex][i]] - Vertex;
FVector Edge2 = EdgeVertices[TriangleConnectionTable[CubeIndex][i + 1]] - Vertex;
Normal = FVector::CrossProduct(Edge1, Edge2).GetSafeNormal();
}
Normals.Add(Normal);
}
}
// Front face
Triangles.Add(TriIndices[0]);
Triangles.Add(TriIndices[1]);
Triangles.Add(TriIndices[2]);
// back face
Triangles.Add(TriIndices[0]);
Triangles.Add(TriIndices[2]);
Triangles.Add(TriIndices[1]);
}
}
But I don’t see where I am missing a vertex? This should be right, right?