Wave Function Collapse

also: WFC, model synthesis

First appeared 2007 · Paul Merrell — The technique was first described by Paul Merrell as "model synthesis" in his 2007 i3D paper; the widely-used "wave function collapse" variant and name came from Maxim Gumin's 2016 implementation (see naming). · timeline ↗

community / folk Name · coined by Maxim Gumin (2016) — A community / open-source coinage: Maxim Gumin named his September 2016 GitHub implementation "WaveFunctionCollapse", borrowing the term by analogy to the quantum-mechanical collapse of a superposition. This later name displaced the inventor's own — Paul Merrell had named the technique "model synthesis" in 2007 — a clear case of a popular community name eclipsing the originator's.

In one line A constraint-based generator that fills a grid with tiles so that every local adjacency rule is satisfied, producing globally coherent patterns from a small example. Named by analogy to quantum "collapse" — each cell starts as a superposition of all possible tiles and is resolved one at a time.

Wave Function Collapse · Open in new tab ↗ · Motion begins only when you press Run.

The code

The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.

wave-function-collapse/index.html
const SEED = Math.floor(Math.random()*1e9);

// Simple "circuit/pipe" WFC. Each tile has connector sockets on its 4 edges
// (0 = none, 1 = wire). Tiles may only sit next to tiles whose facing socket
// matches. Min-entropy collapse + constraint propagation (no backtracking;
// on contradiction we just reset that cell to all options).
//
// Tiles (edges as [top,right,bottom,left]):
const TILES = [
  { e: [0,0,0,0], draw: () => {} },                                   // blank
  { e: [1,1,0,0], draw: (g,c)=>{ arc(c,c,c,c,-HALF_PI,0); } },        // ┗ corner
  { e: [0,1,1,0], draw: (g,c)=>{ arc(c,c,c,c,0,HALF_PI); } },         // ┏
  { e: [0,0,1,1], draw: (g,c)=>{ arc(c,c,c,c,HALF_PI,PI); } },        // ┓
  { e: [1,0,0,1], draw: (g,c)=>{ arc(c,c,c,c,PI,PI+HALF_PI); } },     // ┛
  { e: [1,0,1,0], draw: (g,c)=>{ line(c,0,c,g); } },                  // │
  { e: [0,1,0,1], draw: (g,c)=>{ line(0,c,g,c); } },                  // ─
];

let N, cell, wave;

function setup() {
  randomSeed(SEED);
  const s = Math.min(windowWidth, windowHeight);
  createCanvas(s, s);
  background('#10131a');
  N = 20;
  cell = s / N;

  wave = Array.from({ length: N*N }, () => TILES.map((_, i) => i));
  // collapse loop
  for (let guard = 0; guard < N*N*4; guard++) {
    const i = lowestEntropy();
    if (i < 0) break;
    const opts = wave[i];
    wave[i] = [opts[Math.floor(random(opts.length))]];
    propagate();
  }

  // render
  stroke(120, 220, 200); strokeWeight(cell * 0.16); noFill(); strokeCap(ROUND);
  for (let y = 0; y < N; y++)
    for (let x = 0; x < N; x++) {
      const opts = wave[y*N+x];
      if (opts.length !== 1) continue;
      push();
      translate(x*cell, y*cell);
      const t = TILES[opts[0]];
      t.draw(cell, cell/2);
      pop();
    }
}

function lowestEntropy() {
  let best = -1, bestC = 1e9;
  for (let i = 0; i < wave.length; i++) {
    const c = wave[i].length;
    if (c > 1 && c < bestC) { bestC = c; best = i; }
  }
  return best;
}

function opp(dir) { return (dir + 2) % 4; } // 0 top,1 right,2 bottom,3 left

function propagate() {
  const stack = [];
  for (let i = 0; i < wave.length; i++) stack.push(i);
  while (stack.length) {
    const i = stack.pop();
    const x = i % N, y = (i / N) | 0;
    const dirs = [[0,-1,0],[1,0,1],[0,1,2],[-1,0,3]];
    for (const [dx, dy, dir] of dirs) {
      const nx = x+dx, ny = y+dy;
      if (nx<0||ny<0||nx>=N||ny>=N) continue;
      const ni = ny*N+nx;
      const allowed = new Set();
      for (const o of wave[i]) allowed.add(TILES[o].e[dir]);
      const before = wave[ni].length;
      wave[ni] = wave[ni].filter((o) => allowed.has(TILES[o].e[opp(dir)]));
      if (wave[ni].length === 0) wave[ni] = TILES.map((_, k) => k); // relax on contradiction
      if (wave[ni].length !== before) stack.push(ni);
    }
  }
}

Open the demo source ↗

How it works

Derive adjacency rules (which tiles may sit next to which) from a sample or a hand-authored tileset. Every output cell begins as a "superposition" of all tiles. Then loop: 1. Observe: pick the cell with the lowest entropy (fewest remaining options) and collapse it to a single tile, chosen at random by weight. 2. Propagate: remove now-impossible options from neighbours per the adjacency rules, cascading the constraint outward until everything is consistent. 3. Repeat until all cells are collapsed. If a cell's options ever drop to zero (a contradiction), backtrack or restart. It is essentially constraint propagation (arc consistency) plus weighted random choice, and works for both tile sets and overlapping NxN patterns from a bitmap.

Parameters

Lineage

Precedents
Paul Merrell
Related
Cellular Automata, Truchet Tiles, Modular Assembly / Parametric Character

Frequently combined with

Works using this algorithm (2)

nullMachines
kerim-safa-loackme
Rituals - Venice
aaron-penne-x-boreta

Last updated 2026-07-16