I’m building a game with a voxel-based infinite terrain generation, like Minecraft. I have already done the occlusion culling work, and it helped a lot, but my 16x64x16 chunks are still very slow/laggy when rendering a new terrain (I don’t know why exactly), which is unplayable.
I thought about using multi-threading to render chunks, but Minecraft only uses one thread and it’s very fast to generate terrain. I was looking for a way to optimize my game without “pushing the dust under the carpet”.
Maybe the occlusion culling process is taking too long to be executed, or my chunks are too big, I can’t find a good way to optimize it and make the game playable… Any Ideas?
This is how I’m creating meshes:
Voxel[,,] voxels;
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
private void SetupVoxels()
{
this.voxels = new Voxel[xSize + 2, ySize + 2, zSize + 2];
for (int x = 0; x < xSize + 2; x++)
{
for (int y = 0; y < ySize + 1; y++)
{
for (int z = 0; z < zSize + 2; z++)
{
Vector3 blockPos = new Vector3(x, y, z);
Voxel voxel = new Voxel(blockPos, chunkPos, 0, ySize);
voxels[x, y, z] = voxel;
} //z
} //y
} //x
}
private void Generate()
{
SetupVoxels();
int tris = 0;
//Go through each block
for (int x = 1; x < xSize + 1; x++)
{
for (int y = 0; y < ySize; y++)
{
for (int z = 1; z < zSize + 1; z++)
{
if (voxels[x, y, z].GetVoxelType() != BlockType.Air)
{
Vector3 blockPos = new Vector3(x, y, z);
Voxel voxel = new Voxel(blockPos, chunkPos, tris, ySize);
if (voxels[x, y, z - 1].GetVoxelType() == BlockType.Air)
{
voxel.AddFace(BlockSide.Front);
}
if (voxels[x - 1, y, z].GetVoxelType() == BlockType.Air)
{
voxel.AddFace(BlockSide.Left);
}
if (voxels[x, y, z + 1].GetVoxelType() == BlockType.Air)
{
voxel.AddFace(BlockSide.Back);
}
if (voxels[x + 1, y, z].GetVoxelType() == BlockType.Air)
{
voxel.AddFace(BlockSide.Right);
}
if (voxels[x, y + 1, z].GetVoxelType() == BlockType.Air)
{
voxel.AddFace(BlockSide.Top);
}
if (y > 0)
{
if (voxels[x, y - 1, z].GetVoxelType() == BlockType.Air)
{
voxel.AddFace(BlockSide.Bottom);
}
}
triangles.AddRange(voxel.Triangles);
vertices.AddRange(voxel.Vertices);
voxels[x, y, z] = voxel;
tris += 8;
}
}
}
}
}