Greedy meshing for voxel worlds
Turns a chunk of blocky voxels into far fewer quads by hiding interior faces and merging coplanar neighbouring faces of the same type into larger rectangles.
Also known as greedy mesher, binary greedy meshing
In the family Geometry, meshing & signed distance fields, voxel meshing
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | No built-in voxel mesher; third-party godot_voxel uses face culling rather than greedy quad merging. |
| Unity | None found | No built-in voxel terrain engine or greedy quad-merging mesher. |
| Unreal 5.7 | None found | No voxel mesher in engine. |
| Blender 5.2 | None found | No voxel mesher built in. |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
First cull faces between two solid voxels, leaving only the visible boundary. Then, for each of the six face directions and each slice perpendicular to it, build a 2D mask of visible faces tagged with their block type, light and any other attribute that must match. Sweep the mask: at the first unvisited cell, grow the rectangle along one axis while neighbours have the same tag, then grow along the other axis while the entire row matches, emit one quad, and clear those mask cells. The result is not minimal but close, and linear in chunk volume. Ambient occlusion baked per vertex or per face blocks merging unless it matches, so merge keys must include it. Bitwise variants process whole columns of 64 voxels with bit tricks for much faster meshing.
When to choose it
Reach for it when
- voxel chunk has far too many triangles
- minecraft style terrain mesh optimisation
- merge adjacent block faces into bigger quads
- chunk remeshing takes too long after edits
Choose it for Minecraft-style worlds where chunk meshes are rebuilt on edits and vertex count or draw bandwidth matters. Prefer Surface Nets, Marching Cubes or Dual Contouring when the voxel data is a smooth scalar field rather than blocks, meshoptimizer mesh optimization to post-process any mesh, and ray marching over the grid with Voxel grid DDA ray traversal (Amanatides–Woo) when you would rather not build a mesh at all.
Field marks
- T-junction cracks and sparkling pixels where large merged quads meet smaller ones, especially with texture atlases
- Texture atlas bleeding or stretched UVs because merged quads need tiling rather than a single tile
- Per-vertex AO or lighting gradients look wrong on merged quads, so merge keys must include them or lighting must be moved to the shader
- Chunk borders need neighbour data, or you get seams and hidden-face mistakes at edges
Presets
Greedy voxel meshing — per-voxel 1×1 quad faces (left) vs merged maximal rectangular greedy quads (right)
{
"P_VOXEL_GRID": 1,
"P_MERGE_COLORS": 0.78,
"P_SPLIT": 0.45,
"P_ROT_SPEED": 1
}Show GLSL (164 lines)
const float P_VOXEL_GRID = 1.0; // P Wire Contrast [0.2, 1.6, 0.05]
const float P_MERGE_COLORS = 0.78; // P Merged Quad Tint [0.0, 1.0, 0.02]
const float P_SPLIT = 0.45; // P Naive / Greedy Split [0.0, 1.0, 0.02]
const float P_ROT_SPEED = 1.0; // P Orbit Speed [0.0, 2.5, 0.05]
mat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
vec3 hash33(vec3 p) {
p = fract(p * vec3(0.1031, 0.1030, 0.0973));
p += dot(p, p.yxz + 33.33);
return fract((p.xxy + p.yxx) * p.zyx);
}
// Terraced voxel chunk height (integer voxel layers inside [-6, 6]^2 -> ideal for greedy rectangular merging!)
float voxelChunkHeight(vec2 cell) {
vec2 a = abs(cell);
if (a.x > 5.5 || a.y > 5.5) return -10.0;
// Crisp stepped architectural plateaus (flat runs merge cleanly into single large greedy quads!)
float h = 1.0;
if (a.x <= 3.5 && a.y <= 3.5) h = 3.0;
if (cell.x >= -2.5 && cell.x <= 1.5 && cell.y >= -1.5 && cell.y <= 2.5) h = 5.0;
if (cell.x >= 2.5 && cell.y <= -1.5) h = 4.0;
if (cell.x <= -3.5 && cell.y >= 1.5) h = 2.0;
return h;
}
// Evaluate solid occupancy of integer voxel coordinate v
bool isVoxelSolid(vec3 v) {
if (v.y < 0.0 || v.y > 6.0) return false;
float h = voxelChunkHeight(v.xz);
return v.y < h;
}
// Exact Amanatides-Woo 3D DDA voxel traversal returning hit position, voxel coord, and face normal
bool traceVoxels(vec3 ro, vec3 rd, out float tHit, out vec3 hitPos, out vec3 voxCoord, out vec3 hitNorm) {
// Intersect chunk bounding box [-6, 0, -6] to [6, 6, 6]
vec3 bMin = vec3(-6.0, 0.0, -6.0);
vec3 bMax = vec3(6.0, 6.0, 6.0);
vec3 invD = 1.0 / (rd + sign(rd + 1e-6) * 1e-5);
vec3 t0 = (bMin - ro) * invD;
vec3 t1 = (bMax - ro) * invD;
float tNear = max(max(min(t0.x, t1.x), min(t0.y, t1.y)), min(t0.z, t1.z));
float tFar = min(min(max(t0.x, t1.x), max(t0.y, t1.y)), max(t0.z, t1.z));
if (tFar < max(tNear, 0.0)) return false;
float tStart = max(tNear, 0.0) + 0.001;
vec3 p = ro + rd * tStart;
vec3 mapP = floor(p);
vec3 deltaDist = abs(invD);
vec3 stepDir = sign(rd);
vec3 sideDist = (stepDir * (mapP - p) + (stepDir * 0.5) + 0.5) * deltaDist;
vec3 mask = vec3(0.0);
for (int i = 0; i < 42; i++) {
if (isVoxelSolid(mapP)) {
tHit = tStart + dot(mask, sideDist - deltaDist);
hitPos = ro + rd * tHit;
voxCoord = mapP;
// Handle immediate entry hit on outer bounding box face
if (dot(mask, mask) < 0.5) {
vec3 rel = abs(hitPos - vec3(0.0, 3.0, 0.0)) / vec3(6.0, 3.0, 6.0);
if (rel.x > rel.y && rel.x > rel.z) hitNorm = vec3(sign(hitPos.x), 0.0, 0.0);
else if (rel.y > rel.z) hitNorm = vec3(0.0, sign(hitPos.y - 3.0), 0.0);
else hitNorm = vec3(0.0, 0.0, sign(hitPos.z));
} else {
hitNorm = -mask * stepDir;
}
return true;
}
mask = step(sideDist.xyz, sideDist.yzx) * step(sideDist.xyz, sideDist.zxy);
sideDist += mask * deltaDist;
mapP += mask * stepDir;
if (any(lessThan(mapP, bMin - vec3(1.0))) || any(greaterThan(mapP, bMax + vec3(1.0)))) break;
}
return false;
}
vec3 aces(vec3 x) {
x = max(x, 0.0);
return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float screenX = fragCoord.x / max(iResolution.x, 1.0);
float split = clamp(P_SPLIT, 0.0, 1.0);
float wireGain = clamp(P_VOXEL_GRID, 0.1, 1.8);
float mergeTint = clamp(P_MERGE_COLORS, 0.0, 1.0);
float spin = iTime * max(P_ROT_SPEED, 0.0);
// Orbiting isometric-style 3/4 camera around the voxel island
float camAng = 0.65 + spin * 0.25;
vec3 ro = vec3(cos(camAng) * 14.5, 10.5, sin(camAng) * 14.5);
vec3 ta = vec3(0.0, 2.0, 0.0);
vec3 fw = normalize(ta - ro);
vec3 rt = normalize(cross(fw, vec3(0.0, 1.0, 0.0)));
vec3 up = cross(rt, fw);
vec3 rd = normalize(fw * 1.65 + rt * uv.x + up * uv.y);
vec3 col = mix(vec3(0.035, 0.048, 0.075), vec3(0.10, 0.14, 0.21), smoothstep(-0.5, 0.6, uv.y));
float tHit;
vec3 hp, vc, n;
if (traceVoxels(ro, rd, tHit, hp, vc, n)) {
// Tangent axes (uAxis, vAxis) on the hit voxel face
vec3 uAxis = (abs(n.y) > 0.5) ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 vAxis = normalize(cross(n, uAxis));
vec2 fLocal = vec2(fract(dot(hp, uAxis)), fract(dot(hp, vAxis)));
// Naive 1x1 voxel face wireframe (draws border around EVERY single voxel unit!)
float dNaive = min(min(fLocal.x, 1.0 - fLocal.x), min(fLocal.y, 1.0 - fLocal.y));
float naiveWire = smoothstep(0.085, 0.02, dNaive);
// Greedy Voxel Meshing: check whether adjacent coplanar neighbors share the EXACT same exposed face!
// If neighbor is solid AND its outward neighbor (along +n) is empty, the face edge is MERGED away!
bool mergeU0 = isVoxelSolid(vc - uAxis) && !isVoxelSolid(vc - uAxis + n);
bool mergeU1 = isVoxelSolid(vc + uAxis) && !isVoxelSolid(vc + uAxis + n);
bool mergeV0 = isVoxelSolid(vc - vAxis) && !isVoxelSolid(vc - vAxis + n);
bool mergeV1 = isVoxelSolid(vc + vAxis) && !isVoxelSolid(vc + vAxis + n);
float dGreedy = 1.0;
if (!mergeU0) dGreedy = min(dGreedy, fLocal.x);
if (!mergeU1) dGreedy = min(dGreedy, 1.0 - fLocal.x);
if (!mergeV0) dGreedy = min(dGreedy, fLocal.y);
if (!mergeV1) dGreedy = min(dGreedy, 1.0 - fLocal.y);
float greedyWire = smoothstep(0.10, 0.022, dGreedy);
// Deterministic greedy patch ID color by walking toward (-u, -v) corner of the coplanar run
vec3 patchAnchor = vc;
for (int s = 0; s < 5; s++) {
if (isVoxelSolid(patchAnchor - uAxis) && !isVoxelSolid(patchAnchor - uAxis + n)) patchAnchor -= uAxis;
}
for (int s = 0; s < 5; s++) {
if (isVoxelSolid(patchAnchor - vAxis) && !isVoxelSolid(patchAnchor - vAxis + n)) patchAnchor -= vAxis;
}
vec3 patchCol = 0.30 + 0.65 * hash33(patchAnchor * 1.7 + n * 5.3 + 9.1);
// Base biome voxel albedo (grass top, sandstone mid, basalt base)
vec3 biomeAlb = (n.y > 0.5) ? vec3(0.32, 0.72, 0.36) : ((vc.y >= 3.0) ? vec3(0.78, 0.64, 0.44) : vec3(0.42, 0.48, 0.56));
bool isGreedy = screenX >= split;
vec3 alb = isGreedy ? mix(biomeAlb, patchCol, mergeTint * 0.72) : biomeAlb;
vec3 l1 = normalize(vec3(0.55, 0.88, 0.45));
float diff = max(dot(n, l1), 0.0);
float hemi = 0.32 + 0.16 * n.y;
col = alb * (hemi + 0.85 * diff);
if (isGreedy) {
// Crisp bright border ONLY around merged maximal greedy quads!
col = mix(col, vec3(0.98, 0.96, 0.72), greedyWire * 0.85 * min(wireGain, 1.2));
// Very faint hint of eliminated internal voxel grid lines
col = mix(col, col * 0.88, naiveWire * 0.25);
} else {
// Heavy dark grid on every unmerged 1x1 voxel face + diagonal split (2 tris per face!)
float diag = smoothstep(0.055, 0.012, abs(fLocal.x - fLocal.y));
col = mix(col, vec3(0.04, 0.06, 0.09), max(naiveWire * 0.85, diag * 0.38) * min(wireGain, 1.2));
}
}
float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
col = mix(col, vec3(0.95, 0.97, 1.0), sep);
fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| greedy-voxel-meshing-live | Tesla T4 1920x1080 | 14.2× baseline (moderate) · 0.446 ms / 0.462 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| greedy-voxel-meshing-live | NVIDIA L4 1920x1080 | 13.9× baseline (moderate) · 0.220 ms / 0.225 ms · 2.0× faster on L4 | opengl-timer-query · Cross-architecture validation |
| greedy-voxel-meshing-live | NVIDIA A100-SXM4-40GB 1920x1080 | 9.76× baseline (moderate) · 0.281 ms / 0.283 ms · 1.6× faster on A100 | opengl-timer-query · Cross-architecture validation |
Crosses
- Voxel grid DDA ray traversal (Amanatides–Woo): The same voxel grid can be edited and picked with DDA rays while the greedy mesher produces the chunk geometry for rendering.
Sources
- implementationmikolalysenko (2013). mikolalysenko/greedy-mesher. Greedy mesh compilerGitHub API · license MIT · 2026-11-29