---
id: "markov-junior-rewrite-rules"
title: "MarkovJunior (Rewrite-Rule Generation)"
aliases: [MarkovJunior, probabilistic rewrite rules]
domain: "procgen"
stage: "procedural"
realtime: "interactive"
platforms: [cpu]
year_introduced: 2022
inputs: [layout]
outputs: [voxels, texture, layout]
style: "both"
dynamic: "static"
cost: "offline"
authority: "mxgmn, 2022"
url: "https://texelatlas.vercel.app/t/markov-junior-rewrite-rules/"
sources_verified_at: "2026-11-29"
---
# MarkovJunior (Rewrite-Rule Generation) (mxgmn, 2022)

> A probabilistic programming language for grid generation: sequences of pattern-rewrite rules, optionally guided by inference toward goal states, with WFC available as one of its node types.

## Reach for it when

- prototype a dungeon or maze generator with readable rules
- grow voxel structures from simple pattern rewrites
- grid generation with goals like a path from a to b
- procedural content without an example to imitate
- generate grid patterns guided by constraints

## How it works

A model is a tree of nodes executed over a grid of colours. A rewrite node has an input pattern and output pattern, like 'RB -> WW'; at each step it picks a random matching location and rewrites it. Nodes are composed with sequential, Markov (repeat until nothing applies) and one-shot semantics, giving growth processes, maze carvers and flood fills. Inference nodes use constraint propagation and search to steer rewriting toward a goal state, such as a path connecting two points, rather than hoping random rewriting finds it. Also includes a WFC node and a convolution node. Models are XML descriptions running in a C# interpreter that renders 2D and 3D output.

## When to use it

Choose it when you want to prototype grid-based generators quickly (mazes, dungeons, voxel buildings, growth patterns) with explicit, readable rules and goal inference. Prefer [Wave Function Collapse](https://texelatlas.vercel.app/t/wave-function-collapse.md) when you have an example to imitate, and [BSP Dungeon Generation](https://texelatlas.vercel.app/t/bsp-dungeon-generation.md) or [L-Systems](https://texelatlas.vercel.app/t/l-systems.md) when a hand-written recursive procedure is clearer than a rule database.

## Cost

offline — Interpreter cost scales with grid size and rule count; inference nodes add search cost. Designed for authoring, not per-frame use.
Measured GPU Weight: 3.38× baseline (light) — 0.1112 ms median / 0.1142 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 2.77× baseline (light) — 0.0441 ms median (2.52× faster on L4).
Ampere A100 scaling: 2.12× baseline (light) — 0.0611 ms median (1.82× faster on A100).

## Failure modes

- Rule order and node semantics are subtle: a misplaced Markov node can loop forever or terminate immediately
- Inference can fail to find any solution, leaving the run in an unsolved state
- Output is grid-bound; exporting to meshes needs an extra meshing step
- Random rewriting without inference often misses global goals like connectivity

## Dataflow

- Inputs: layout
- Outputs: voxels, texture, layout
- Platforms: cpu

## Presets

### MarkovJunior — discrete symbol rewrite grid (left) vs sculpted dungeon tile architecture (right) (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_GRID_RES": 16,
  "P_RULE_STAGE": 0.78,
  "P_ANIM_RATE": 0.85,
  "P_SPLIT": 0.46
}
```

```glsl
const float P_GRID_RES = 16.0; // P Grid Resolution [10.0, 26.0, 2.0]
const float P_RULE_STAGE = 0.78; // P Rewrite Progress [0.1, 1.0, 0.02]
const float P_ANIM_RATE = 0.85; // P Rewrite Sweep [0.0, 2.5, 0.05]
const float P_SPLIT = 0.46; // P Symbols / Tiles [0.0, 1.0, 0.02]

float hash21(vec2 p) {
  p = fract(p * vec2(127.1, 311.7));
  p += dot(p, p + 34.5);
  return fract(p.x * p.y);
}

// Deterministic MarkovJunior-style multi-stage grid symbol classifier:
// Returns symbol ID: 0=void/black, 1=corridor/white, 2=room/gold, 3=river/cyan, 4=wall/crimson
float evalMarkovCell(vec2 cell, float stage) {
  // Stage 1: Growing orthogonal maze corridors from central seed
  vec2 macro = floor(cell * 0.5);
  float h = hash21(macro);
  float isCorrX = step(0.5, mod(cell.y, 2.0)) * step(0.32, h);
  float isCorrY = step(0.5, mod(cell.x, 2.0)) * step(h, 0.68);
  float sym = max(isCorrX, isCorrY);

  // Stage 2: Room-carving rewrite rule (B-W-W -> R-R-R) in seeded chambers
  vec2 roomCell = floor(cell / 5.0);
  vec2 roomLocal = mod(cell, 5.0);
  if (hash21(roomCell + 7.3) > 0.45 && roomLocal.x >= 1.0 && roomLocal.x <= 3.0 && roomLocal.y >= 1.0 && roomLocal.y <= 3.0) {
    if (stage > 0.35) sym = 2.0;
  }

  // Stage 3: Winding river rewrite rule cutting through grid
  float riverX = floor(sin(cell.y * 0.38) * 2.2 + 0.5);
  if (abs(cell.x - riverX) < 0.6 && stage > 0.60) {
    sym = 3.0;
  }

  // Stage 4: Wall-border rewrite rule: any black cell adjacent to corridor/room becomes wall (4)
  if (sym < 0.5 && stage > 0.25) {
    float n1 = step(0.5, mod(cell.y + 1.0, 2.0));
    float n2 = step(0.5, mod(cell.x + 1.0, 2.0));
    if (n1 + n2 > 0.5) sym = 4.0;
  }
  return sym;
}

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);
  bool isSymbols = screenX < split;
  float gridRes = clamp(floor(P_GRID_RES + 0.5), 8.0, 28.0);
  float stage = clamp(P_RULE_STAGE, 0.05, 1.0);
  float sweep = iTime * max(P_ANIM_RATE, 0.0);

  vec2 g = uv * gridRes;
  vec2 cell = floor(g);
  vec2 f = fract(g);

  // Expanding Markov rewrite wave radiating outward from seed origin
  float distSeed = length(cell) / (gridRes * 0.65);
  float activeWave = fract(sweep * 0.16) * 1.35 + stage * 0.55;
  float cellStage = stage * smoothstep(activeWave + 0.18, activeWave - 0.08, distSeed);
  float frontier = exp(-abs(distSeed - activeWave) * 18.0);

  float sym = evalMarkovCell(cell, cellStage);
  vec2 dEdge = min(f, 1.0 - f);
  float border = smoothstep(0.08, 0.02, min(dEdge.x, dEdge.y));

  vec3 col;
  if (isSymbols) {
    // Left: Pure MarkovJunior flat color-coded symbol grid (classic B/W/R/U/Y palette) + active rewrite cursor
    vec3 symCol = vec3(0.06, 0.07, 0.10); // 0 = black void
    if (sym > 0.5 && sym < 1.5) symCol = vec3(0.90, 0.92, 0.96); // 1 = white path
    else if (sym > 1.5 && sym < 2.5) symCol = vec3(0.98, 0.76, 0.20); // 2 = gold room
    else if (sym > 2.5 && sym < 3.5) symCol = vec3(0.14, 0.80, 0.96); // 3 = cyan water
    else if (sym > 3.5) symCol = vec3(0.84, 0.24, 0.26); // 4 = crimson wall

    col = mix(symCol, vec3(0.04, 0.05, 0.08), border * 0.65);
    col += vec3(0.32, 1.0, 0.72) * frontier * 0.55;
  } else {
    // Right: Rendered beveled dungeon tiles & animated water from the rewritten symbol grid!
    float bevel = smoothstep(0.0, 0.18, min(dEdge.x, dEdge.y));
    vec2 slope = vec2(smoothstep(0.82, 1.0, f.x) - smoothstep(0.18, 0.0, f.x),
                      smoothstep(0.82, 1.0, f.y) - smoothstep(0.18, 0.0, f.y));
    vec3 n = normalize(vec3(-slope.x * 1.4, -slope.y * 1.4, 1.0));
    vec3 lDir = normalize(vec3(0.55, 0.65, 0.82));
    float diff = max(dot(n, lDir), 0.0);

    vec3 tileCol = vec3(0.05, 0.06, 0.09);
    if (sym > 0.5 && sym < 1.5) {
      // Carved stone corridor flagstones
      tileCol = vec3(0.46, 0.48, 0.52) * (0.45 + 0.65 * diff) * (0.75 + 0.25 * bevel);
    } else if (sym > 1.5 && sym < 2.5) {
      // Warm inlaid golden mosaic room floor
      float inlay = smoothstep(0.28, 0.20, length(f - 0.5));
      tileCol = mix(vec3(0.68, 0.52, 0.26), vec3(0.96, 0.80, 0.34), inlay) * (0.45 + 0.65 * diff);
    } else if (sym > 2.5 && sym < 3.5) {
      // Flowing subterranean canal water
      float rip = 0.5 + 0.5 * sin(g.y * 6.0 - sweep * 4.5 + sin(g.x * 4.0));
      tileCol = mix(vec3(0.06, 0.42, 0.62), vec3(0.22, 0.86, 0.96), rip * 0.65 + 0.25);
    } else if (sym > 3.5) {
      // Raised masonry dungeon wall parapet
      float brick = step(0.10, fract(f.x * 2.0 + step(0.5, f.y) * 0.5));
      tileCol = mix(vec3(0.26, 0.16, 0.18), vec3(0.54, 0.28, 0.26), brick) * (0.38 + 0.82 * diff);
    }
    col = tileCol + vec3(0.28, 0.95, 0.82) * frontier * 0.25;
  }

  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: MIT — written for this index.

## Lineage

- extends [Wave Function Collapse](https://texelatlas.vercel.app/t/wave-function-collapse.md)

## Combination ideas

- [Fog that flows through generated caves](https://texelatlas.vercel.app/ideas/drafts-through-generated-dungeons/)

## Sources

1. [primary] mxgmn (2022) mxgmn/MarkovJunior. https://github.com/mxgmn/MarkovJunior License: MIT. (verified via github on 2026-11-29)
2. [implementation] mxgmn (2022) mxgmn/MarkovJunior. https://github.com/mxgmn/MarkovJunior License: MIT. (verified via github on 2026-11-29)

## Cite this entry

Texel. "MarkovJunior (Rewrite-Rule Generation)." https://texelatlas.vercel.app/t/markov-junior-rewrite-rules/ (accessed 2026-12-01).
