Greedy Meshing Algorithm for Voxel Terrain in Unity

Procedural Cave Generation in Unity (Cellular Automata Method)
Procedural Cave Generation in Unity (Cellular Automata Method)
June 29, 2026
Ultra-Optimized Mesh Generation in Unity Using Jobs and Burst
Ultra-Optimized Mesh Generation in Unity Using Jobs and Burst
July 1, 2026
Procedural Cave Generation in Unity (Cellular Automata Method)
Procedural Cave Generation in Unity (Cellular Automata Method)
June 29, 2026
Ultra-Optimized Mesh Generation in Unity Using Jobs and Burst
Ultra-Optimized Mesh Generation in Unity Using Jobs and Burst
July 1, 2026

Greedy Meshing Algorithm for Voxel Terrain in Unity

Voxel terrain systems represent the world as a grid of cubes. Games like Minecraft generate massive environments using voxels.

However, a naive voxel mesh can contain an enormous number of triangles. If every block generates all of its faces, performance quickly becomes a problem.

This is where the Greedy Meshing Algorithm becomes essential.


What Is Greedy Meshing?

Greedy meshing is an optimization technique used in voxel engines to reduce the number of rendered faces.

Instead of generating one quad per block face, greedy meshing merges adjacent faces into larger quads.

For example:

  • Without greedy meshing → 16 small quads
  • With greedy meshing → 1 large quad

This drastically reduces triangle count and improves performance.


Why Greedy Meshing Is Important

A naive voxel terrain might generate six faces for every block.

For a chunk with 16×16×16 blocks:

  • Total blocks: 4096
  • Naive faces: up to 24576

With greedy meshing, the number of faces can be reduced by more than 90%.


Basic Idea Behind the Algorithm

Greedy meshing works by scanning a 2D slice of the voxel grid and merging adjacent faces that share the same material.

The algorithm:

  • Scan the grid row by row
  • Detect adjacent faces with the same type
  • Merge them into a larger quad
  • Mark merged cells as processed

Voxel Chunk Data Structure

First we define the voxel chunk.

public class VoxelChunk
{
    public int size = 16;
    public int[,,] voxels;

    public VoxelChunk()
    {
        voxels = new int[size, size, size];
    }
}

Each voxel stores a block type (0 = empty).


Face Visibility Check

Before generating faces, we must check if a face is visible.

bool IsFaceVisible(int x, int y, int z)
{
    if (voxels[x, y, z] == 0)
        return false;

    if (x + 1 >= size)
        return true;

    return voxels[x + 1, y, z] == 0;
}

This prevents hidden faces from being generated.


Greedy Meshing Step

The algorithm merges adjacent faces along a grid.

void GreedyMesh()
{
    bool[,] merged = new bool[size, size];

    for(int y = 0; y < size; y++)
    {
        for(int x = 0; x < size; x++)
        {
            if(merged[x,y])
                continue;

            int width = 1;

            while(x + width < size && !merged[x + width, y])
            {
                width++;
            }

            int height = 1;
            bool done = false;

            while(!done && y + height < size)
            {
                for(int k = 0; k < width; k++)
                {
                    if(merged[x + k, y + height])
                    {
                        done = true;
                        break;
                    }
                }

                if(!done)
                    height++;
            }

            for(int dx = 0; dx < width; dx++)
                for(int dy = 0; dy < height; dy++)
                    merged[x + dx, y + dy] = true;

            AddQuad(x, y, width, height);
        }
    }
}

This merges multiple small faces into one larger quad.


Generating the Quad

Once a merged area is detected, we generate a quad.

void AddQuad(int x, int y, int width, int height)
{
    // Create vertices and triangles
}

The quad replaces many small faces with a single larger one.


Performance Benefits

  • Fewer vertices
  • Fewer triangles
  • Lower draw calls
  • Better GPU performance

Greedy meshing can reduce geometry complexity dramatically.


Additional Improvements

Professional voxel engines often combine greedy meshing with other techniques.

  • Chunk systems
  • LOD terrain
  • Async mesh generation
  • Unity Jobs + Burst

Games Using Greedy Meshing

  • Minecraft
  • Cube World
  • Vintage Story
  • Voxel-based sandbox games

These games rely on optimized voxel mesh generation to render large worlds efficiently.


Final Thoughts

Greedy meshing is one of the most important optimizations in voxel terrain systems.

By merging adjacent faces into larger quads, it dramatically reduces the number of polygons and improves performance.

If you are building a voxel engine or Minecraft-style terrain in Unity, greedy meshing should be part of your core rendering pipeline.


 

Leave a Reply

Your email address will not be published. Required fields are marked *


Skip to toolbar