Wave Function Collapse

別名: WFC、model synthesis

初出 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). · 年表 ↗

コミュニティ・通称 名称 · 命名 Maxim Gumin (2016) — コミュニティ/オープンソース由来の命名。Maxim Gumin が 2016 年 9 月の GitHub 実装を "WaveFunctionCollapse" と名付けた(重ね合わせの収束という量子力学の比喩からの借用)。 この後発の名が発明者の名を覆い隠した——Paul Merrell は 2007 年に同技法を 「model synthesis」と命名していた。普及したコミュニティ名が原発案者の名を凌駕した典型例。

ひとことで 局所的な隣接制約からタイルを伝播的に確定させてグローバルに整合した模様を作るアルゴリズム。

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

コード

上のデモの実際のスケッチ(サンドボックスで再現可能)。これで全部です。

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);
    }
  }
}

デモのソースを開く ↗

仕組み

隣接規則(どのタイルが隣り合えるか)を見本タイルセットから導く。各セルは全タイルの「重ね合わせ」から始め、(1) 観測=エントロピー最小(選択肢最少)のセルを選び重み付きランダムで 1 タイルに確定、(2) 伝播=隣接規則で不可能になった選択肢を近傍から除去し、矛盾がなくなるまで連鎖、(3) 全セル確定まで反復。選択肢が 0(矛盾)になればバックトラックか再起動。本質は制約伝播(弧整合)+重み付きランダム選択で、タイルセットにもビットマップの NxN 重畳パターンにも使える。

パラメータ

系譜

先行例
Paul Merrell
関連
Cellular Automata、Truchet Tiles、Modular Assembly / Parametric Character

よく組み合わされるアルゴリズム

このアルゴリズムを使う作品 (2)

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

最終更新 2026-07-16