---
id: "wave-function-collapse"
title: "Wave Function Collapse"
aliases: [WFC, overlapping-model WFC, tiled-model WFC]
domain: "procgen"
stage: "procedural"
realtime: "interactive"
platforms: [cpu, python, unity, unreal, godot, webgl2]
year_introduced: 2016
inputs: [texture, layout]
outputs: [texture, voxels, layout]
style: "both"
dynamic: "static"
cost: "offline"
authority: "mxgmn, 2016"
url: "https://texelatlas.vercel.app/t/wave-function-collapse/"
sources_verified_at: "2026-11-29"
---
# Wave Function Collapse (mxgmn, 2016)

> Constraint-solving generator that fills a grid with tiles or pixels so all local neighbourhoods match an example. Each cell keeps a set of possible states and repeatedly collapses the lowest-entropy cell, then propagates the consequences.

## Reach for it when

- want a level or town generated from an example
- tile placement must obey neighbour rules
- need procedural voxel structures that stay coherent
- texture synthesis that matches local patterns

## How it works

Starting from an example bitmap or a tile set with adjacency rules, the algorithm extracts which patterns may sit next to each other (overlapping model: all NxN patterns of the sample; tiled model: explicit adjacency). Every output cell starts as a superposition of all patterns. A loop picks the cell with the lowest Shannon entropy among those not yet determined, collapses it to one pattern with weighted random choice, then propagates constraints (arc consistency) to its neighbours, pruning impossible patterns. If any cell loses all options, the run has hit a contradiction and is restarted or backtracked. Output is local-consistency only, so global structure comes from the input; designers add fixed tiles, border conditions or non-local constraints to steer it.

## When to use it

Choose it for tile-based levels, towns, voxel structures and texture synthesis where local adjacency rules are easy to specify or learn from an example. Prefer [Model Synthesis](https://texelatlas.vercel.app/t/model-synthesis.md) when you want the earlier chunked, non-backtracking formulation for large 3D scenes, [MarkovJunior (Rewrite-Rule Generation)](https://texelatlas.vercel.app/t/markov-junior-rewrite-rules.md) for sequential rule rewriting with explicit goals, and [BSP Dungeon Generation](https://texelatlas.vercel.app/t/bsp-dungeon-generation.md) for structured room layouts with guaranteed connectivity.

## Cost

offline — Cost grows with grid cells times tile count; contradictions force restarts, so large outputs usually use chunking.
Measured GPU Weight: 7.18× baseline (moderate) — 0.2362 ms median / 0.2408 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 6.05× baseline (moderate) — 0.0962 ms median (2.46× faster on L4).
Ampere A100 scaling: 4.67× baseline (light) — 0.1345 ms median (1.76× faster on A100).

## Failure modes

- Contradictions: a grid can become unsolvable, requiring restarts or backtracking and making runtime unbounded
- Only local correctness: no guarantee of global connectivity, reachability or coherent large-scale structure
- Overlapping model with large N memorises the input; with small N yields incoherent noise
- Tile weights and symmetry settings dramatically change output, so poorly tuned tile sets over-represent a common tile
- Large grids are slow without chunked or windowed solving

## Dataflow

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

## Presets

### Wave function collapse — propagating edge-constraint tile resolution & entropy glow (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_GRID_SIZE": 9,
  "P_WAVE_SPEED": 0.85,
  "P_TRACE_WIDTH": 0.24,
  "P_ENTROPY_GLOW": 1
}
```

```glsl
const float P_GRID_SIZE = 9.0; // P Grid Cells [4.0, 16.0, 1.0]
const float P_WAVE_SPEED = 0.85; // P Collapse Speed [0.0, 2.5, 0.05]
const float P_TRACE_WIDTH = 0.24; // P Conduit Width [0.1, 0.42, 0.01]
const float P_ENTROPY_GLOW = 1.0; // P Frontier Glow [0.0, 2.0, 0.05]

float hash21(vec2 p) {
  p = fract(p * vec2(234.34, 435.345));
  p += dot(p, p + 34.23);
  return fract(p.x * p.y);
}

// Shared edge-socket oracle guaranteeing 100% Wang adjacency compatibility across N/E/S/W boundaries
float hEdge(vec2 cell) {
  // Horizontal edge between (cell.x, cell.y) and (cell.x, cell.y+1)
  return step(0.46, hash21(cell * 1.37 + vec2(11.3, 27.9)));
}
float vEdge(vec2 cell) {
  // Vertical edge between (cell.x, cell.y) and (cell.x+1, cell.y)
  return step(0.46, hash21(cell * 1.71 + vec2(53.1, 19.7)));
}

float sdSegment(vec2 p, vec2 a, vec2 b) {
  vec2 pa = p - a, ba = b - a;
  float h = clamp(dot(pa, ba) / (dot(ba, ba) + 1e-5), 0.0, 1.0);
  return length(pa - ba * h);
}

// Render one of the 16 edge-matched WFC architectural/circuit tiles given its 4 sockets (N, E, S, W)
vec2 evalTileSDF(vec2 f, float N, float E, float S, float W, float seed) {
  // f in [-0.5, 0.5]^2
  float d = 1.0;
  float count = N + E + S + W;

  if (count < 0.5) {
    // Empty courtyard tile with recessed square emblem
    d = max(abs(f.x), abs(f.y)) - 0.16;
  } else if (count < 1.5) {
    // Terminal node cap
    if (N > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.0, 0.5)));
    if (S > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.0, -0.5)));
    if (E > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.5, 0.0)));
    if (W > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(-0.5, 0.0)));
    d = min(d, length(f) - 0.16);
  } else if (count < 2.5 && ((N > 0.5 && E > 0.5) || (E > 0.5 && S > 0.5) || (S > 0.5 && W > 0.5) || (W > 0.5 && N > 0.5))) {
    // Smooth 90-degree quarter-arc corner conduit
    vec2 corner = vec2((E > 0.5 ? 0.5 : -0.5), (N > 0.5 ? 0.5 : -0.5));
    d = abs(length(f - corner) - 0.5);
  } else {
    // Straight, T-junction, or 4-way cross hub
    if (N > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.0, 0.5)));
    if (S > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.0, -0.5)));
    if (E > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.5, 0.0)));
    if (W > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(-0.5, 0.0)));
    if (count > 2.5) d = min(d, length(f) - (0.12 + 0.05 * seed));
  }
  return vec2(d, count);
}

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 px = 1.0 / iResolution.y;

  float gridN = floor(clamp(P_GRID_SIZE, 4.0, 18.0) + 0.5);
  float spd = max(P_WAVE_SPEED, 0.0);
  float traceW = clamp(P_TRACE_WIDTH, 0.08, 0.45) * 0.42;

  vec2 gp = uv * gridN;
  vec2 cell = floor(gp);
  vec2 f = fract(gp) - 0.5;

  // Cycle wave propagation across the grid so viewer sees live collapse frontier resolving states
  float cycle = fract(iTime * 0.18 * spd + 0.35);
  float waveFront = cycle * (gridN * 1.45) - gridN * 0.55;
  // Manhattan + radial heuristic mimics lowest-entropy propagation front
  float cellDist = length(cell + 0.5) + 0.45 * hash21(cell * 3.1);
  float collapseProgress = smoothstep(waveFront + 1.35, waveFront - 0.25, cellDist);
  float frontier = smoothstep(1.4, 0.0, abs(cellDist - waveFront));

  // Query shared sockets for this cell
  float N = hEdge(cell);
  float S = hEdge(cell - vec2(0.0, 1.0));
  float E = vEdge(cell);
  float W = vEdge(cell - vec2(1.0, 0.0));
  float seed = hash21(cell * 7.3 + 1.9);

  vec2 tile = evalTileSDF(f, N, E, S, W, seed);
  float dTrace = tile.x - traceW;

  // Superposed uncollapsed candidate ghost patterns (blurred blend of alternative states)
  vec2 ghost1 = evalTileSDF(f, 1.0 - N, E, 1.0 - S, W, 0.5);
  vec2 ghost2 = evalTileSDF(f, N, 1.0 - E, S, 1.0 - W, 0.5);
  float ghostMask = 0.5 * smoothstep(traceW + 0.04, traceW - 0.04, ghost1.x) +
                    0.5 * smoothstep(traceW + 0.04, traceW - 0.04, ghost2.x);

  // Tile substrate palette (deep indigo ceramic PCB / architectural stone)
  vec3 tileBg = mix(vec3(0.055, 0.072, 0.115), vec3(0.085, 0.105, 0.16), 0.5 + 0.5 * seed);
  vec2 borderD = abs(f);
  float cellFrame = smoothstep(0.455, 0.49, max(borderD.x, borderD.y));
  tileBg = mix(tileBg, vec3(0.025, 0.032, 0.05), cellFrame * 0.7);

  // Collapsed conduit shading: metallic bevel + luminous gold/cyan signal trace
  float aa = gridN * px * 1.4;
  float outerFill = smoothstep(aa, -aa, dTrace);
  float innerTrack = smoothstep(aa, -aa, tile.x - traceW * 0.42);
  float edgeRim = smoothstep(0.045, 0.0, abs(dTrace));

  vec3 conduitBase = mix(vec3(0.16, 0.34, 0.48), vec3(0.24, 0.56, 0.68), smoothstep(0.0, 4.0, tile.y));
  vec3 coreCol = mix(vec3(0.32, 0.92, 1.0), vec3(1.05, 0.78, 0.32), step(2.5, tile.y));
  float signalPulse = 0.72 + 0.28 * sin((uv.x + uv.y) * 14.0 - iTime * 3.5);

  vec3 collapsedCol = tileBg;
  collapsedCol = mix(collapsedCol, conduitBase, outerFill);
  collapsedCol = mix(collapsedCol, coreCol * signalPulse, innerTrack);
  collapsedCol += vec3(0.85, 0.95, 1.0) * edgeRim * 0.35;

  // Uncollapsed quantum-superposition cell appearance (translucent shimmering possibilities)
  float shimmer = 0.5 + 0.5 * sin(iTime * 6.0 + cell.x * 2.1 + cell.y * 3.7);
  vec3 superCol = tileBg * 0.85 + vec3(0.18, 0.42, 0.68) * ghostMask * (0.45 + 0.35 * shimmer);
  // Entropy indicator bars inside uncollapsed tiles
  float entropyDots = smoothstep(0.06, 0.02, length(f - vec2(0.0, 0.0))) * (0.4 + 0.6 * shimmer);
  superCol += vec3(0.42, 0.72, 1.0) * entropyDots * 0.45;

  vec3 col = mix(superCol, collapsedCol, collapseProgress);

  // Bright amber-cyan wave-collapse frontier highlight
  float glowAmt = clamp(P_ENTROPY_GLOW, 0.0, 2.2);
  col += vec3(1.1, 0.72, 0.25) * frontier * (0.28 + 0.75 * cellFrame) * glowAmt;
  col += vec3(0.35, 0.92, 1.0) * frontier * innerTrack * 0.6 * glowAmt;

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

## Lineage

- extends [Model Synthesis](https://texelatlas.vercel.app/t/model-synthesis.md)
- extended by [MarkovJunior (Rewrite-Rule Generation)](https://texelatlas.vercel.app/t/markov-junior-rewrite-rules.md)

## Combines with

- [Poisson Disk Sampling (Bridson)](https://texelatlas.vercel.app/t/poisson-disk-sampling.md): Poisson points place fixed anchor tiles or landmarks that WFC then fills around.

## Combination ideas

- [Generated levels with free voxel GI](https://texelatlas.vercel.app/ideas/wfc-voxels-as-gi-volume/)

## Sources

1. [primary] mxgmn (2016) mxgmn/WaveFunctionCollapse. https://github.com/mxgmn/WaveFunctionCollapse License: NOASSERTION. (verified via github on 2026-11-29)
2. [implementation] mxgmn (2016) mxgmn/WaveFunctionCollapse. https://github.com/mxgmn/WaveFunctionCollapse License: NOASSERTION. (verified via github on 2026-11-29)
3. [implementation] BorisTheBrave (2018) BorisTheBrave/DeBroglie. https://github.com/BorisTheBrave/DeBroglie License: MIT. (verified via github on 2026-11-29)

## Cite this entry

Texel. "Wave Function Collapse." https://texelatlas.vercel.app/t/wave-function-collapse/ (accessed 2026-12-01).
