Wave Function Collapse (WFC) is one of the more quietly influential procedural-generation algorithms of the last decade — the technique behind a long list of generative tilemaps, textures, and level layouts, and a frequent building block in generative-art tools despite its name sounding borrowed from quantum mechanics (it is, loosely, but the game-generation algorithm devised by Maxim Gumin, building on earlier work by Paul Merrell, is a constraint-solving method, not a physics simulation).
The core idea
Start with a small example image — a simple tilemap of a landscape, say — and a grid you want to fill that’s much larger than the example. WFC’s job is to generate new output that preserves the local patterns found in the example: every small neighborhood of cells in the output should look like a neighborhood that actually occurred in the source. The algorithm treats every cell in the output grid as a superposition of possibilities (all the tile types that haven’t yet been ruled out for that cell), then repeatedly:
- Observes the cell with the lowest entropy — the fewest remaining possibilities — and collapses it to one definite value, chosen randomly weighted by how often that tile appeared in the source.
- Propagates that choice outward: neighboring cells have their own possibility sets narrowed to only the values compatible with what their neighbor just became.
- Repeats until every cell has collapsed to a single value, or the algorithm hits a contradiction (no valid tile fits a cell) and has to restart.
That propagation step is the whole trick. It’s why WFC output looks locally coherent — rivers connect to rivers, walls meet at sensible corners — without the algorithm ever reasoning globally about the whole map at once.
Where to actually try it
- The Coding Train’s Coding Challenge #171 works through a JavaScript/p5.js implementation of the tiled model from scratch, which pairs naturally with this site’s own p5.js tutorials if you’re starting from zero.
- Boris the Brave’s “Wave Function Collapse Explained” reframes the algorithm explicitly as constraint solving, which is the better mental model once you’re past the first toy example and debugging why your generator keeps hitting contradictions.
- Godot has WFC implementations in its asset library if you want generated levels inside an actual game engine rather than a standalone sketch.
Get your first output running
- Pick a small, simple source image or tilemap — something with clear, repeatable local structure (a basic platformer tileset or a simple landscape works better as a first attempt than anything photorealistic).
- Choose an overlapping model implementation (rather than the simpler tile model) if your source is a bitmap rather than a predefined tile set — it extracts NxN pixel patterns automatically instead of requiring you to define adjacency rules by hand.
- Run it at a small output size first (32×32 or so). WFC’s contradiction-and-restart behavior gets dramatically slower as grid size grows, so debug small before scaling up.
- Once you have output you’re happy with, increase output dimensions and start experimenting with the pattern size (N) — small N values capture local texture only; larger N values start capturing bigger structural motifs from your source, at a real performance cost.