{
 "id": "wave-function-collapse",
 "title": "Wave Function Collapse",
 "aliases": [
  "WFC",
  "overlapping-model WFC",
  "tiled-model WFC"
 ],
 "domain": "procgen",
 "subdomain": "constraint-based synthesis",
 "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",
 "summary": "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.",
 "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": "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, `markov-junior-rewrite-rules` for sequential rule rewriting with explicit goals, and `bsp-dungeon-generation` for structured room layouts with guaranteed connectivity.",
 "cost": {
  "class": "offline",
  "notes": "Cost grows with grid cells times tile count; contradictions force restarts, so large outputs usually use chunking."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "gh:mxgmn/wavefunctioncollapse",
   "type": "code",
   "title": "mxgmn/WaveFunctionCollapse",
   "description": "Bitmap & tilemap generation from a single example with the help of ideas from quantum mechanics",
   "authors": [
    "mxgmn"
   ],
   "year": 2016,
   "venue": "GitHub",
   "url": "https://github.com/mxgmn/WaveFunctionCollapse",
   "ids": {
    "repo": "mxgmn/WaveFunctionCollapse"
   },
   "license": "NOASSERTION",
   "extra": {
    "stars": 25578,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-10-15T12:21:27Z",
    "language": "C#",
    "homepage": null,
    "license_name": "Other"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "mxgmn2016mxgmn"
  },
  {
   "role": "implementation",
   "key": "gh:mxgmn/wavefunctioncollapse",
   "type": "code",
   "title": "mxgmn/WaveFunctionCollapse",
   "description": "Bitmap & tilemap generation from a single example with the help of ideas from quantum mechanics",
   "authors": [
    "mxgmn"
   ],
   "year": 2016,
   "venue": "GitHub",
   "url": "https://github.com/mxgmn/WaveFunctionCollapse",
   "ids": {
    "repo": "mxgmn/WaveFunctionCollapse"
   },
   "license": "NOASSERTION",
   "extra": {
    "stars": 25578,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-10-15T12:21:27Z",
    "language": "C#",
    "homepage": null,
    "license_name": "Other"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/WaveFunctionCollapse",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "mxgmn2016mxgmn"
  },
  {
   "role": "implementation",
   "key": "gh:boristhebrave/debroglie",
   "type": "code",
   "title": "BorisTheBrave/DeBroglie",
   "description": "DeBroglie is a C# library implementing the Wave Function Collapse algorithm with support for additional non-local constraints, and other useful features.",
   "authors": [
    "BorisTheBrave"
   ],
   "year": 2018,
   "venue": "GitHub",
   "url": "https://github.com/BorisTheBrave/DeBroglie",
   "ids": {
    "repo": "BorisTheBrave/DeBroglie"
   },
   "license": "MIT",
   "extra": {
    "stars": 535,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2025-12-21T11:55:24Z",
    "language": "C#",
    "homepage": "https://boristhebrave.github.io/DeBroglie/",
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/BorisTheBrave/DeBroglie",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/BorisTheBrave/DeBroglie",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/BorisTheBrave/DeBroglie",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/BorisTheBrave/DeBroglie",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/BorisTheBrave/DeBroglie",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/BorisTheBrave/DeBroglie",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "boristhebrave2018boristhebrave"
  }
 ],
 "presets": [
  {
   "id": "wave-function-collapse-live",
   "title": "Wave function collapse — propagating edge-constraint tile resolution & entropy glow",
   "runtime": "glsl-fragment",
   "params": {
    "P_GRID_SIZE": 9,
    "P_WAVE_SPEED": 0.85,
    "P_TRACE_WIDTH": 0.24,
    "P_ENTROPY_GLOW": 1
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_GRID_SIZE = 9.0; // P Grid Cells [4.0, 16.0, 1.0]\nconst float P_WAVE_SPEED = 0.85; // P Collapse Speed [0.0, 2.5, 0.05]\nconst float P_TRACE_WIDTH = 0.24; // P Conduit Width [0.1, 0.42, 0.01]\nconst float P_ENTROPY_GLOW = 1.0; // P Frontier Glow [0.0, 2.0, 0.05]\n\nfloat hash21(vec2 p) {\n  p = fract(p * vec2(234.34, 435.345));\n  p += dot(p, p + 34.23);\n  return fract(p.x * p.y);\n}\n\n// Shared edge-socket oracle guaranteeing 100% Wang adjacency compatibility across N/E/S/W boundaries\nfloat hEdge(vec2 cell) {\n  // Horizontal edge between (cell.x, cell.y) and (cell.x, cell.y+1)\n  return step(0.46, hash21(cell * 1.37 + vec2(11.3, 27.9)));\n}\nfloat vEdge(vec2 cell) {\n  // Vertical edge between (cell.x, cell.y) and (cell.x+1, cell.y)\n  return step(0.46, hash21(cell * 1.71 + vec2(53.1, 19.7)));\n}\n\nfloat sdSegment(vec2 p, vec2 a, vec2 b) {\n  vec2 pa = p - a, ba = b - a;\n  float h = clamp(dot(pa, ba) / (dot(ba, ba) + 1e-5), 0.0, 1.0);\n  return length(pa - ba * h);\n}\n\n// Render one of the 16 edge-matched WFC architectural/circuit tiles given its 4 sockets (N, E, S, W)\nvec2 evalTileSDF(vec2 f, float N, float E, float S, float W, float seed) {\n  // f in [-0.5, 0.5]^2\n  float d = 1.0;\n  float count = N + E + S + W;\n\n  if (count < 0.5) {\n    // Empty courtyard tile with recessed square emblem\n    d = max(abs(f.x), abs(f.y)) - 0.16;\n  } else if (count < 1.5) {\n    // Terminal node cap\n    if (N > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.0, 0.5)));\n    if (S > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.0, -0.5)));\n    if (E > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.5, 0.0)));\n    if (W > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(-0.5, 0.0)));\n    d = min(d, length(f) - 0.16);\n  } 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))) {\n    // Smooth 90-degree quarter-arc corner conduit\n    vec2 corner = vec2((E > 0.5 ? 0.5 : -0.5), (N > 0.5 ? 0.5 : -0.5));\n    d = abs(length(f - corner) - 0.5);\n  } else {\n    // Straight, T-junction, or 4-way cross hub\n    if (N > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.0, 0.5)));\n    if (S > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.0, -0.5)));\n    if (E > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(0.5, 0.0)));\n    if (W > 0.5) d = min(d, sdSegment(f, vec2(0.0, 0.0), vec2(-0.5, 0.0)));\n    if (count > 2.5) d = min(d, length(f) - (0.12 + 0.05 * seed));\n  }\n  return vec2(d, count);\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float px = 1.0 / iResolution.y;\n\n  float gridN = floor(clamp(P_GRID_SIZE, 4.0, 18.0) + 0.5);\n  float spd = max(P_WAVE_SPEED, 0.0);\n  float traceW = clamp(P_TRACE_WIDTH, 0.08, 0.45) * 0.42;\n\n  vec2 gp = uv * gridN;\n  vec2 cell = floor(gp);\n  vec2 f = fract(gp) - 0.5;\n\n  // Cycle wave propagation across the grid so viewer sees live collapse frontier resolving states\n  float cycle = fract(iTime * 0.18 * spd + 0.35);\n  float waveFront = cycle * (gridN * 1.45) - gridN * 0.55;\n  // Manhattan + radial heuristic mimics lowest-entropy propagation front\n  float cellDist = length(cell + 0.5) + 0.45 * hash21(cell * 3.1);\n  float collapseProgress = smoothstep(waveFront + 1.35, waveFront - 0.25, cellDist);\n  float frontier = smoothstep(1.4, 0.0, abs(cellDist - waveFront));\n\n  // Query shared sockets for this cell\n  float N = hEdge(cell);\n  float S = hEdge(cell - vec2(0.0, 1.0));\n  float E = vEdge(cell);\n  float W = vEdge(cell - vec2(1.0, 0.0));\n  float seed = hash21(cell * 7.3 + 1.9);\n\n  vec2 tile = evalTileSDF(f, N, E, S, W, seed);\n  float dTrace = tile.x - traceW;\n\n  // Superposed uncollapsed candidate ghost patterns (blurred blend of alternative states)\n  vec2 ghost1 = evalTileSDF(f, 1.0 - N, E, 1.0 - S, W, 0.5);\n  vec2 ghost2 = evalTileSDF(f, N, 1.0 - E, S, 1.0 - W, 0.5);\n  float ghostMask = 0.5 * smoothstep(traceW + 0.04, traceW - 0.04, ghost1.x) +\n                    0.5 * smoothstep(traceW + 0.04, traceW - 0.04, ghost2.x);\n\n  // Tile substrate palette (deep indigo ceramic PCB / architectural stone)\n  vec3 tileBg = mix(vec3(0.055, 0.072, 0.115), vec3(0.085, 0.105, 0.16), 0.5 + 0.5 * seed);\n  vec2 borderD = abs(f);\n  float cellFrame = smoothstep(0.455, 0.49, max(borderD.x, borderD.y));\n  tileBg = mix(tileBg, vec3(0.025, 0.032, 0.05), cellFrame * 0.7);\n\n  // Collapsed conduit shading: metallic bevel + luminous gold/cyan signal trace\n  float aa = gridN * px * 1.4;\n  float outerFill = smoothstep(aa, -aa, dTrace);\n  float innerTrack = smoothstep(aa, -aa, tile.x - traceW * 0.42);\n  float edgeRim = smoothstep(0.045, 0.0, abs(dTrace));\n\n  vec3 conduitBase = mix(vec3(0.16, 0.34, 0.48), vec3(0.24, 0.56, 0.68), smoothstep(0.0, 4.0, tile.y));\n  vec3 coreCol = mix(vec3(0.32, 0.92, 1.0), vec3(1.05, 0.78, 0.32), step(2.5, tile.y));\n  float signalPulse = 0.72 + 0.28 * sin((uv.x + uv.y) * 14.0 - iTime * 3.5);\n\n  vec3 collapsedCol = tileBg;\n  collapsedCol = mix(collapsedCol, conduitBase, outerFill);\n  collapsedCol = mix(collapsedCol, coreCol * signalPulse, innerTrack);\n  collapsedCol += vec3(0.85, 0.95, 1.0) * edgeRim * 0.35;\n\n  // Uncollapsed quantum-superposition cell appearance (translucent shimmering possibilities)\n  float shimmer = 0.5 + 0.5 * sin(iTime * 6.0 + cell.x * 2.1 + cell.y * 3.7);\n  vec3 superCol = tileBg * 0.85 + vec3(0.18, 0.42, 0.68) * ghostMask * (0.45 + 0.35 * shimmer);\n  // Entropy indicator bars inside uncollapsed tiles\n  float entropyDots = smoothstep(0.06, 0.02, length(f - vec2(0.0, 0.0))) * (0.4 + 0.6 * shimmer);\n  superCol += vec3(0.42, 0.72, 1.0) * entropyDots * 0.45;\n\n  vec3 col = mix(superCol, collapsedCol, collapseProgress);\n\n  // Bright amber-cyan wave-collapse frontier highlight\n  float glowAmt = clamp(P_ENTROPY_GLOW, 0.0, 2.2);\n  col += vec3(1.1, 0.72, 0.25) * frontier * (0.28 + 0.75 * cellFrame) * glowAmt;\n  col += vec3(0.35, 0.92, 1.0) * frontier * innerTrack * 0.6 * glowAmt;\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "wave-function-collapse",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T16:50:13.852Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.55,
      "std": 0.1879,
      "unique_colors": 482
     },
     "image": "/specimens/wave-function-collapse-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "model-synthesis",
  "markov-junior-rewrite-rules",
  "wang-tiles",
  "bsp-dungeon-generation"
 ],
 "extends": [
  "model-synthesis"
 ],
 "supersedes": [],
 "combines_with": [
  {
   "id": "poisson-disk-sampling",
   "why": "Poisson points place fixed anchor tiles or landmarks that WFC then fills around."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/procgen/wave-function-collapse.json"
 },
 "solves": [
  "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"
 ],
 "authority": "mxgmn, 2016",
 "url": "https://texelatlas.vercel.app/t/wave-function-collapse/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 7.18,
  "tier": "moderate",
  "p50_ms": 0.2362,
  "p95_ms": 0.2408,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "wave-function-collapse-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 6.05,
   "tier": "moderate",
   "p50_ms": 0.0962,
   "p95_ms": 0.0963,
   "speedup_x": 2.46,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 4.67,
   "tier": "light",
   "p50_ms": 0.1345,
   "p95_ms": 0.1358,
   "speedup_x": 1.76,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}