How to Optimize a Voxel-Based Procedural Terrain Generation

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;
                    }


                }
            }
        }

    }

Although this post is specific to voxel terrain, anyone looking into making a voxel game should try to optimize their 3D voxel models, since they can be quite inefficient.


I’ve created the Ultimate Voxel Optimizer as a way of helping this problem. Using classic meshing techniques with voxel models doesn’t result in the most optimal models. UVO, however, uses a custom meshing algorithm to ensure that you get the best reduction out of all model optimizer programs.

Simply load in your .vox models from MagicaVoxel, select your export method and format, and click export. Some models can receive a 50% reduction in polygon count, with more features coming soon! The program is currently $3.99, and available on Windows, Mac, and Linux!


You can get more info at Ultimate Voxel Optimizer by Nat