[SOLVED]
note: this is my first time using the unity forum, so I might have to move this somewhere else
So I’m working on a procedurally generated voxel game, and it worked fine using cubes, but that was way too laggy, so I switched to using meshes and only showing the faces which have a neighbor air block.
It all works “fine”, but it doesn’t matter the chunk, after a few hundred blocks it just doesn’t make nothing. Not like in generating, but not showing in the mesh.
I can provide more details if you’d like. I belive its something about the triangles or vertices, but I’m not too sure.
(1) This is the code for making triangles/vertices for a single cube:
private MarchResult calculateBlockSide(Block block, string side, float3[] blockCorners)
{
MarchResult result = new MarchResult();
switch (side)
{
case "top":
result.vertices.Add(blockCorners[5]);
result.vertices.Add(blockCorners[4]);
result.vertices.Add(blockCorners[7]);
result.vertices.Add(blockCorners[6]);
break;
case "bottom":
result.vertices.Add(blockCorners[0]);
result.vertices.Add(blockCorners[1]);
result.vertices.Add(blockCorners[2]);
result.vertices.Add(blockCorners[3]);
break;
case "left":
result.vertices.Add(blockCorners[7]);
result.vertices.Add(blockCorners[4]);
result.vertices.Add(blockCorners[0]);
result.vertices.Add(blockCorners[3]);
break;
case "right":
result.vertices.Add(blockCorners[5]);
result.vertices.Add(blockCorners[6]);
result.vertices.Add(blockCorners[2]);
result.vertices.Add(blockCorners[1]);
break;
case "back":
result.vertices.Add(blockCorners[4]);
result.vertices.Add(blockCorners[5]);
result.vertices.Add(blockCorners[1]);
result.vertices.Add(blockCorners[0]);
break;
case "front":
result.vertices.Add(blockCorners[6]);
result.vertices.Add(blockCorners[7]);
result.vertices.Add(blockCorners[3]);
result.vertices.Add(blockCorners[2]);
break;
}
result.triangles.Add(0);
result.triangles.Add(1);
result.triangles.Add(3);
result.triangles.Add(1);
result.triangles.Add(2);
result.triangles.Add(3);
return result;
}
public List<MarchResult> calculateBlockMesh(Block block, List<BlockNeighbor> neighbors)
{
List<MarchResult> result = new List<MarchResult>(8);
float3[] blockCorners = new float3[]
{
CornerTable[0] + block.position,
CornerTable[1] + block.position,
CornerTable[2] + block.position,
CornerTable[3] + block.position,
CornerTable[4] + block.position,
CornerTable[5] + block.position,
CornerTable[6] + block.position,
CornerTable[7] + block.position,
};
for (int i = 0; i < neighbors.Count; i++)
{
string name = NormalDefinitions[Normals.IndexOf(neighbors[i].position)];
MarchResult sideResult = calculateBlockSide(block, name, blockCorners);
result.Add(sideResult);
}
/*result.uvs.AddRange(uvs);
result.normals.AddRange(normals);*/
/*result.uvs.Add(new Vector2(0, 0));
result.uvs.Add(new Vector2(0, 1));
result.uvs.Add(new Vector2(1, 1));*/
return result;
}
(2) This is the code for deciding for which blocks to create a mesh:
public List<MarchResult> chunkMesh = new List<MarchResult>();
private void __built_mesh(Chunk[,,] ChunkList, Action func)
{
chunkMesh.Clear();
for (int x = 0; x < chunk_size.x; x++)
{
for (int z = 0; z < chunk_size.z; z++)
{
for (int y = 0; y < chunk_size.y; y++)
{
Block block = blocks[x, y, z];
if (block.material != Material.Air)
{
List<Block> neighbors = __get_neighbors(block, ChunkList, x, y, z);
List<BlockNeighbor> VisibleNeighbors = new List<BlockNeighbor>();
bool isVisible = false;
for (int i = 0; i < neighbors.Count; i++)
{
Block neighbor = neighbors[i];
//if (neighbor.material != Material.Air)
if (neighbor.material == Material.Air)
{
BlockNeighbor bN = new BlockNeighbor();
bN.block = neighbor;
bN.position = neighbor.position - block.position;
VisibleNeighbors.Add(bN);
block.Visible = true;
isVisible = true;
}
}
if (isVisible)
{
List<MarchResult> result = March.calculateBlockMesh(block, VisibleNeighbors);
chunkMesh.AddRange(result);
}
}
}
}
}
updateMesh = true;
func();
}
(3) This is the code for joining all the chunks and actually displaying the mesh:
public void DisplayChunks(Mesh mesh)
{
List<Color> newColors = new List<Color>();
List<Vector3> newNormals = new List<Vector3>();
List<Vector3> newVertices = new List<Vector3>();
List<Vector2> newUV = new List<Vector2>();
List<int> newTriangles = new List<int>();
int totalFaces = 0;
for (int i = 0; i < ChunksBuilt.Count; i++)
{
int3 cP = ChunksBuilt[i];
Chunk chunk = Chunks[cP.x, cP.y, cP.z];
chunk.updateMesh = false;
for (int k = 0; k < chunk.chunkMesh.Count; k++)
{
MarchResult face = chunk.chunkMesh[k];
newColors.AddRange(face.colors);
newNormals.AddRange(face.normals);
newVertices.AddRange(face.vertices);
newUV.AddRange(face.uvs);
for (int x = 0; x < face.triangles.Count; x++)
{
newTriangles.Add((totalFaces * 4) + face.triangles[x]);
}
totalFaces++;
}
}
//mesh.Clear();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.colors = newColors.ToArray();
mesh.normals = newNormals.ToArray();
mesh.uv = newUV.ToArray();
mesh.RecalculateNormals();
//mesh.Optimize();
}
I belive it might be something with the totalFaces variable since I had trouble with that before, but I’m not too sure.
How it looks with a mesh:
You can see the cutout in the down corner right.
How its supposed to look:
Any help is appreciated