I was following this tutorial to create voxel terrain with chunks https://www.youtube.com/watch?v=4jni5NV_EjE
But somewhere in this part, it causes the editor to crash whenever I generate a chunk:
// Add faces, verticies, UVs and Normals.
int Triangle_num = 0;
for (int I = 0; 1 < 6; I++)
{
int NewIndex = Index + bMask*.X + (bMask*.Y * ChunkLineElements) + (bMask*.Z * ChunkLineElementsP2);
bool flag = false;
if(MeshIndex >= 20) flag = true;
else if((X + bMask*.X < ChunkLineElements) && (X + bMask*.X >= 0) && (Y + bMask*.Y < ChunkLineElements) && (Y + bMask*.Y >= 0))
{
if (NewIndex < ChunkFields.Num() && NewIndex >= 0)
if (ChunkFields[NewIndex] < 1) flag = true;
}
else flag = true;
if (flag) {
Triangles.Add(bTriangles[0] + Triangle_num + ElementID);
Triangles.Add(bTriangles[1] + Triangle_num + ElementID);
Triangles.Add(bTriangles[2] + Triangle_num + ElementID);
Triangles.Add(bTriangles[3] + Triangle_num + ElementID);
Triangles.Add(bTriangles[4] + Triangle_num + ElementID);
Triangles.Add(bTriangles[5] + Triangle_num + ElementID);
Triangle_num += 4; // Add 4 verticies to next triangle.
switch (I) {
case 0: {
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Normals.Append(bNormals0, ARRAY_COUNT(bNormals0));
break;
}
case 1: {
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Normals.Append(bNormals1, ARRAY_COUNT(bNormals1));
break;
}
case 2: {
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Normals.Append(bNormals2, ARRAY_COUNT(bNormals2));
break;
}
case 3: {
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Normals.Append(bNormals3, ARRAY_COUNT(bNormals3));
break;
}
case 4: {
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Normals.Append(bNormals5, ARRAY_COUNT(bNormals4));
break;
}
case 5: {
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), -VoxelSizeHalf + (Z * VoxelSize)));
Vertices.Add(FVector(-VoxelSizeHalf + (X * VoxelSize), -VoxelSizeHalf + (Y * VoxelSize), VoxelSizeHalf + (Z * VoxelSize)));
Normals.Append(bNormals4, ARRAY_COUNT(bNormals5));
break;
}
}
UVs.Append(bUVs, ARRAY_COUNT(bUVs));
//FColor color = FColor::MakeRandomColor();
//FColor color = FColor(RandomStream.FRand() * 256, RandomStream.FRand() * 256, RandomStream.FRand() * 256, rand() % 5);
FColor color = FColor(255, 255, 255, I);
VertexColors.Add(color); VertexColors.Add(color); VertexColors.Add(color); VertexColors.Add(color);
}
}
I’m pretty new at C++ so I cant really find my mistake here, there is no error showing anywhere…
Oh and also this part is in the start of the file:
const int32 bTriangles] = { 2, 1, 0, 0, 3, 2 };
const FVector2D bUVs] = { FVector2D(0.0, 0.0), FVector2D(0.0, 1.0), FVector2D(1.0, 1.0), FVector2D(1.0, 0.0) };
const FVector bNormals0] = { FVector(0, 0, 1), FVector(0, 0, 1), FVector(0, 0, 1), FVector(0, 0, 1) };
const FVector bNormals1] = { FVector(0, 0, -1), FVector(0, 0, -1), FVector(0, 0, -1), FVector(0, 0, -1) };
const FVector bNormals2] = { FVector(0, 1, 0), FVector(0, 1, 0), FVector(0, 1, 0), FVector(0, 1, 0) };
const FVector bNormals3] = { FVector(0, -1, 1), FVector(0, -1, 0), FVector(0, -1, 0), FVector(0, -1, 0) };
const FVector bNormals4] = { FVector(1, 0, 1), FVector(1, 0, 0), FVector(1, 0, 0), FVector(1, 0, 0) };
const FVector bNormals5] = { FVector(-1, 0, 1), FVector(-1, 0, 0), FVector(-1, 0, 0), FVector(-1, 0, 0) };
const FVector bMask] = { FVector(0.0, 0.0, 1.0), FVector(0.0, 0.0, -1.0), FVector(0.0, 1.0, 0.0), FVector(0.0, -1.0, 0.0), FVector(1.0, 0.0, 0.0), FVector(-1.0, 0.0, 0.0)};
Someone see anything wrong here? Tried my best to figure it out but It’s still crashing