Wave Function Collapse
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.
Also known as WFC, overlapping-model WFC, tiled-model WFC
In the family Procedural generation, constraint-based synthesis
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | |
| Unity | None found | |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
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 choose it
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
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 when you want the earlier chunked, non-backtracking formulation for large 3D scenes, MarkovJunior (Rewrite-Rule Generation) for sequential rule rewriting with explicit goals, and BSP Dungeon Generation for structured room layouts with guaranteed connectivity.
Field marks
- 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
Presets
Wave function collapse — propagating edge-constraint tile resolution & entropy glow
{
"P_GRID_SIZE": 9,
"P_WAVE_SPEED": 0.85,
"P_TRACE_WIDTH": 0.24,
"P_ENTROPY_GLOW": 1
}Show GLSL (136 lines)
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).
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| wave-function-collapse-live | Tesla T4 1920x1080 | 7.18× baseline (moderate) · 0.236 ms / 0.241 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| wave-function-collapse-live | NVIDIA L4 1920x1080 | 6.05× baseline (moderate) · 0.096 ms / 0.096 ms · 2.5× faster on L4 | opengl-timer-query · Cross-architecture validation |
| wave-function-collapse-live | NVIDIA A100-SXM4-40GB 1920x1080 | 4.67× baseline (light) · 0.135 ms / 0.136 ms · 1.8× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
Crosses
- Generated levels with free voxel GI: Tile-based generated levels get dynamic global illumination with no voxelisation pass: the solver's voxel grid is the cone-tracing volume. Each tile type carries pre-voxelised directional opacity, so thin walls stop leaking light.
- Poisson Disk Sampling (Bridson): Poisson points place fixed anchor tiles or landmarks that WFC then fills around.
Sources
- primarymxgmn (2016). mxgmn/WaveFunctionCollapse. Bitmap & tilemap generation from a single example with the help of ideas from quantum mechanicsGitHub API · license NOASSERTION · 2026-11-29
- implementationmxgmn (2016). mxgmn/WaveFunctionCollapse. Bitmap & tilemap generation from a single example with the help of ideas from quantum mechanicsGitHub API · license NOASSERTION · 2026-11-29
- implementationBorisTheBrave (2018). BorisTheBrave/DeBroglie. DeBroglie is a C# library implementing the Wave Function Collapse algorithm with support for additional non-local constraints, and other useful features.GitHub API · license MIT · 2026-11-29