Cellular automata cave generation
Fills a grid with random walls, then repeatedly replaces each cell by a majority-style rule over its neighbours, which erodes noise into organic cave chambers and tunnels.
Also known as CA caves, cave smoothing rule, 4-5 rule
In the family Procedural generation, cave and dungeon layout
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 | |
| Unity | None found | |
| Unreal 5.7 | None found | No built-in cave generator. |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Initialise a 2D or 3D grid with each cell wall with probability around 45 percent. Run a few smoothing iterations where every cell counts its wall neighbours in the 8-cell or 26-cell neighbourhood: a cell becomes a wall if it has at least a threshold number of wall neighbours and otherwise open (a common choice is the 4-5 rule, with an early-stage variant that also fills isolated pockets). Noise collapses into blobs and the blobs merge into cave-like caverns. Afterwards, flood-fill to label connected open regions, drop tiny ones, and connect the survivors with tunnels or keep only the largest. Border cells are forced solid. Convert to a mesh with Marching Cubes or Surface Nets for smooth cave walls, or to blocks for voxel games. An infinite variant processes chunks with a window of neighbours and seed-hash initialisation.
When to choose it
Reach for it when
- want organic cave layouts from simple rules
- random noise grid to cave chambers
- roguelike map generator with few parameters
- generated caves are disconnected rooms
Choose it for quick organic caves and island-like shapes in grids where you want few knobs and no authored layout. Prefer BSP Dungeon Generation for rooms and corridors, Wave Function Collapse or Model Synthesis when adjacency rules and authored tiles matter, MarkovJunior (Rewrite-Rule Generation) for rule-based tile rewriting, and Worley (Cellular) Noise or Fractal Brownian Motion and Multifractals thresholds when you need continuous, unbounded fields.
Field marks
- Disconnected caverns unless a flood-fill and tunnel-carving pass joins them
- Result looks uniformly blobby with no large-scale structure or guaranteed path length
- Chunked generation shows seams at borders because each chunk smooths without the neighbours' cells
- Parameters are brittle: a small change to the initial wall density flips between solid rock and one open void
Presets
Cellular automata caves — B5678/S45678 Moore-neighborhood cavern carving
{
"P_FILL_DENSITY": 0.48,
"P_SMOOTHING": 1,
"P_CELL_SCALE": 26,
"P_WATER_LEVEL": 0.42
}Show GLSL (112 lines)
const float P_FILL_DENSITY = 0.48; // P Fill Density [0.25, 0.72, 0.01]
const float P_SMOOTHING = 1.0; // P CA Smoothing [0.0, 2.0, 0.05]
const float P_CELL_SCALE = 26.0; // P Cell Scale [10.0, 48.0, 1.0]
const float P_WATER_LEVEL = 0.42; // P Aquifer Glow [0.0, 1.0, 0.02]
float hash21(vec2 p) {
p = fract(p * vec2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// Initial random rock fill with gentle macro cavern bias
float initCell(vec2 cell, float fill) {
float h = hash21(cell);
// Subtle macro room seed so connected caverns emerge reliably
float macro = 0.5 + 0.5 * sin(cell.x * 0.22 + 0.7) * cos(cell.y * 0.21 - 0.4);
float thresh = clamp(fill + (macro - 0.5) * 0.18, 0.15, 0.85);
return step(1.0 - thresh, h);
}
// Pass 1: 3x3 Moore neighborhood count (birth/survival >= 5 of 9 cells)
float caPass1(vec2 c, float fill) {
float sum = 0.0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
sum += initCell(c + vec2(float(dx), float(dy)), fill);
}
}
return smoothstep(4.2, 5.2, sum);
}
// Pass 2: second smoothing generation over Pass 1
float caPass2(vec2 c, float fill) {
float sum = 0.0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
sum += caPass1(c + vec2(float(dx), float(dy)), fill);
}
}
return sum / 9.0;
}
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 scale = clamp(P_CELL_SCALE, 8.0, 56.0);
vec2 pan = vec2(iTime * 0.45, sin(iTime * 0.22) * 1.4);
vec2 gPos = uv * scale + pan;
vec2 cell = floor(gPos);
vec2 f = fract(gPos);
float fill = clamp(P_FILL_DENSITY, 0.2, 0.8);
float sm = clamp(P_SMOOTHING, 0.0, 2.0);
// Bilinearly sample smoothed CA field across 4 neighboring cells for crisp organic walls
vec2 w = f * f * (3.0 - 2.0 * f);
float s00 = mix(caPass1(cell, fill), caPass2(cell, fill), clamp(sm, 0.0, 1.0));
float s10 = mix(caPass1(cell + vec2(1.0, 0.0), fill), caPass2(cell + vec2(1.0, 0.0), fill), clamp(sm, 0.0, 1.0));
float s01 = mix(caPass1(cell + vec2(0.0, 1.0), fill), caPass2(cell + vec2(0.0, 1.0), fill), clamp(sm, 0.0, 1.0));
float s11 = mix(caPass1(cell + vec2(1.0, 1.0), fill), caPass2(cell + vec2(1.0, 1.0), fill), clamp(sm, 0.0, 1.0));
float field = mix(mix(s00, s10, w.x), mix(s01, s11, w.x), w.y);
// Blend between crisp tile-level CA and interpolated smooth cave wall
float rawCell = caPass2(cell, fill);
float caveVal = mix(rawCell, field, clamp(sm * 0.55, 0.0, 1.0));
// Gradient for rim bevel & top-left torch lighting
vec2 grad = vec2(
mix(s10 - s00, s11 - s01, w.y),
mix(s01 - s00, s11 - s10, w.x)
);
vec3 nrm = normalize(vec3(-grad * 4.5, 1.0));
vec2 torchPos = vec2(0.28 * cos(iTime * 0.6), 0.22 * sin(iTime * 0.48));
vec3 lDir = normalize(vec3(torchPos - uv, 0.35));
float torchAtt = 1.0 / (1.0 + 3.8 * dot(torchPos - uv, torchPos - uv));
// Wall vs open floor mask
float isWall = smoothstep(0.46, 0.54, caveVal);
float wallRim = smoothstep(0.12, 0.0, abs(caveVal - 0.5));
// Stratified cavern limestone coloring
float strata = 0.5 + 0.5 * sin(gPos.y * 1.4 + sin(gPos.x * 0.45) * 1.6);
float grain = hash21(floor(gPos * 4.0));
vec3 rockCol = mix(vec3(0.14, 0.12, 0.11), vec3(0.32, 0.27, 0.23), strata * 0.65 + 0.35 * grain);
float ndl = max(dot(nrm, lDir), 0.0);
rockCol *= (0.35 + 1.15 * ndl * torchAtt);
rockCol += vec3(0.55, 0.42, 0.28) * wallRim * (0.4 + 0.9 * torchAtt);
// Open cavern floor with subtle pebble texture and glowing turquoise aquifer pools
float depth = clamp((0.48 - caveVal) * 2.5, 0.0, 1.0);
vec3 floorCol = mix(vec3(0.08, 0.085, 0.095), vec3(0.04, 0.055, 0.07), depth);
floorCol *= (0.5 + 1.4 * torchAtt);
float waterMask = smoothstep(0.58 - 0.42 * P_WATER_LEVEL, 0.78 - 0.42 * P_WATER_LEVEL, depth);
float ripple = 0.5 + 0.5 * sin(gPos.x * 2.6 + iTime * 2.2) * cos(gPos.y * 2.4 - iTime * 1.8);
vec3 waterCol = vec3(0.06, 0.46, 0.58) * (0.75 + 0.45 * ripple) + vec3(0.18, 0.85, 0.92) * pow(ripple, 5.0) * 0.45;
floorCol = mix(floorCol, waterCol, waterMask * clamp(P_WATER_LEVEL * 1.2, 0.0, 1.0));
// Subtle tile-grid overlay to reveal underlying cellular lattice
vec2 cellEdge = abs(f - 0.5);
float tileLine = smoothstep(0.45, 0.5, max(cellEdge.x, cellEdge.y));
vec3 col = mix(floorCol, rockCol, isWall);
col = mix(col, col * 0.72 + vec3(0.03, 0.04, 0.05), tileLine * 0.35);
// Warm moving explorer torch aura
col += vec3(1.0, 0.68, 0.32) * exp(-length(uv - torchPos) * 7.5) * 0.35;
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 |
|---|---|---|---|
| cellular-automata-cave-generation-live | Tesla T4 1920x1080 | 71.6× baseline (extreme) · 2.35 ms / 2.43 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| cellular-automata-cave-generation-live | NVIDIA L4 1920x1080 | 70.8× baseline (extreme) · 1.13 ms / 1.13 ms · 2.1× faster on L4 | opengl-timer-query · Cross-architecture validation |
| cellular-automata-cave-generation-live | NVIDIA A100-SXM4-40GB 1920x1080 | 52.5× baseline (heavy) · 1.51 ms / 1.51 ms · 1.6× faster on A100 | opengl-timer-query · Cross-architecture validation |
Crosses
- Surface Nets: Treating the wall mask as a scalar field and meshing it with surface nets gives smooth organic cave walls from the blocky automaton output.
Sources
- primaryLawrence Johnson, Georgios N. Yannakakis, Julian Togelius (2010). Cellular automata for real-time generation of infinite cave levels. Proceedings of the 2010 Workshop on Procedural Content Generation in Games.Verified via crossref · 2026-11-29doi:10.1145/1814256.1814266