---
id: "cellular-automata-cave-generation"
title: "Cellular automata cave generation"
aliases: [CA caves, cave smoothing rule, 4-5 rule]
domain: "procgen"
stage: "procedural"
realtime: "realtime"
platforms: [cpu, webgl2, python, unity, unreal, godot]
year_introduced: 2010
inputs: [layout]
outputs: [layout, voxels]
style: "stylized"
dynamic: "static"
cost: "cheap"
authority: "Johnson et al., 2010"
url: "https://texelatlas.vercel.app/t/cellular-automata-cave-generation/"
sources_verified_at: "2026-11-29"
---
# Cellular automata cave generation (Johnson et al., 2010)

> 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.

## 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

## 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](https://texelatlas.vercel.app/t/marching-cubes.md) or [Surface Nets](https://texelatlas.vercel.app/t/surface-nets.md) 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 use it

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](https://texelatlas.vercel.app/t/bsp-dungeon-generation.md) for rooms and corridors, [Wave Function Collapse](https://texelatlas.vercel.app/t/wave-function-collapse.md) or [Model Synthesis](https://texelatlas.vercel.app/t/model-synthesis.md) when adjacency rules and authored tiles matter, [MarkovJunior (Rewrite-Rule Generation)](https://texelatlas.vercel.app/t/markov-junior-rewrite-rules.md) for rule-based tile rewriting, and [Worley (Cellular) Noise](https://texelatlas.vercel.app/t/worley-noise.md) or [Fractal Brownian Motion and Multifractals](https://texelatlas.vercel.app/t/fractal-brownian-motion.md) thresholds when you need continuous, unbounded fields.

## Cost

cheap — A few passes over the grid; trivial on the CPU for 2D, moderate for 3D volumes and chunked infinite worlds.
Measured GPU Weight: 71.56× baseline (extreme) — 2.3542 ms median / 2.4307 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 70.81× baseline (extreme) — 1.1259 ms median (2.09× faster on L4).
Ampere A100 scaling: 52.46× baseline (heavy) — 1.5109 ms median (1.56× faster on A100).

## Failure modes

- 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

## Dataflow

- Inputs: layout
- Outputs: layout, voxels
- Platforms: cpu, webgl2, python, unity, unreal, godot

## Presets

### Cellular automata caves — B5678/S45678 Moore-neighborhood cavern carving (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_FILL_DENSITY": 0.48,
  "P_SMOOTHING": 1,
  "P_CELL_SCALE": 26,
  "P_WATER_LEVEL": 0.42
}
```

```glsl
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).

## Combines with

- [Surface Nets](https://texelatlas.vercel.app/t/surface-nets.md): 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

1. [primary] Lawrence 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. https://doi.org/10.1145/1814256.1814266 (verified via crossref on 2026-11-29)

## Cite this entry

Texel. "Cellular automata cave generation." https://texelatlas.vercel.app/t/cellular-automata-cave-generation/ (accessed 2026-12-01).
