{
 "id": "wang-tiles",
 "title": "Wang Tiles (Aperiodic Tile Synthesis)",
 "aliases": [
  "stochastic tiling with Wang tiles"
 ],
 "domain": "procgen",
 "subdomain": "tiling and texture",
 "stage": "procedural",
 "realtime": "realtime",
 "platforms": [
  "cpu",
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": 2003,
 "inputs": [
  "texture"
 ],
 "outputs": [
  "texture",
  "layout"
 ],
 "style": "both",
 "dynamic": "static",
 "summary": "A small set of square tiles with coloured edges is assembled so adjacent edge colours match, producing non-repeating textures or distributions from a compact atlas that can be looked up per pixel.",
 "how_it_works": "Each tile carries a colour on each edge, and a tile set with all edge-colour combinations (for two colours per axis, 16 tiles; a reduced 8-tile set suffices for a scanline order) can tile the plane so every neighbouring pair of edges agrees. Tiles are generated from source patches so content crossing a shared edge colour is identical: for textures, patches are quilted together; for point sets, a stored Poisson disk set per tile makes a precomputed blue-noise distribution. At runtime a pseudo-random choice (or a hash of cell coordinates) picks, for each cell, a tile consistent with its left and top neighbours. This gives non-periodic coverage with a small atlas and constant-time per-pixel evaluation.",
 "when_to_use": "Choose it for large non-repeating ground textures, blue-noise scattering on the fly (e.g. foliage on a huge terrain) and sampling patterns. Prefer `poisson-disk-sampling` for one-time point generation, `wave-function-collapse` for richer adjacency rules and level layouts, and `worley-noise` for a procedural cell pattern with no stored tiles.",
 "cost": {
  "class": "cheap",
  "notes": "One hash and an atlas lookup per cell; the cost is in precomputing tile content."
 },
 "failure_modes": [
  "Seams visible if edge colours are matched but the interior features differ in structure",
  "Few tile colours show repetition: the same motif recurs along rows or columns",
  "Tile-based blue noise has visible tile boundaries in the power spectrum",
  "Tile content must be authored per texture; oriented or anisotropic textures need extra care"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/882262.882265",
   "type": "paper",
   "title": "Wang Tiles for image and texture generation",
   "authors": [
    "Michael F. Cohen",
    "Jonathan Shade",
    "Stefan Hiller",
    "Oliver Deussen"
   ],
   "year": 2003,
   "venue": "ACM Transactions on Graphics",
   "url": "https://doi.org/10.1145/882262.882265",
   "ids": {
    "doi": "10.1145/882262.882265"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882265",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882265",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882265",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882265",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882265",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "cohen2003wang"
  }
 ],
 "presets": [
  {
   "id": "wang-tiles-specimen",
   "title": "Two-colour Wang tiles with automatically matching edges forming paths",
   "runtime": "glsl-fragment",
   "params": {
    "TILES": 6
   },
   "code": "// Wang tiles with 2 edge colours: each tile's edge colours come from a hash of the shared edge, so matching is automatic.\nconst float TILES = 6.0;\nfloat eh(vec2 e, float k) { return step(0.5, fract(sin(dot(e, vec2(12.9898, 78.233)) + k * 31.7) * 43758.5453)); }\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n    vec2 uv = fragCoord / iResolution.xy * TILES;\n    vec2 id = floor(uv), f = fract(uv) - 0.5;\n    float N = eh(id + vec2(0, 1), 1.0), S = eh(id, 1.0), E = eh(id + vec2(1, 0), 2.0), W = eh(id, 2.0);\n    // quadrant triangles tinted by the edge colour on that side\n    float side;\n    if (abs(f.x) > abs(f.y)) side = f.x > 0.0 ? E : W; else side = f.y > 0.0 ? N : S;\n    vec3 c0 = vec3(0.16, 0.2, 0.35), c1 = vec3(0.95, 0.6, 0.25);\n    vec3 col = mix(c0, c1, side) * (0.7 + 0.3 * (1.0 - 2.0 * max(abs(f.x), abs(f.y))));\n    // coloured edges continue as paths through the tile centre, so matching edges form continuous lines\n    float d = 1e9;\n    if (N > 0.5) d = min(d, abs(f.x) + step(f.y, 0.0) * 9.0);\n    if (S > 0.5) d = min(d, abs(f.x) + step(0.0, f.y) * 9.0);\n    if (E > 0.5) d = min(d, abs(f.y) + step(f.x, 0.0) * 9.0);\n    if (W > 0.5) d = min(d, abs(f.y) + step(0.0, f.x) * 9.0);\n    col = mix(col, vec3(0.97, 0.93, 0.8), smoothstep(0.07, 0.055, d));\n    vec2 g = abs(f);\n    col = mix(col, vec3(0.03), smoothstep(0.012, 0.0, 0.5 - max(g.x, g.y)));\n    fragColor = vec4(pow(col, vec3(0.8)), 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "wang-tiles",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.4532,
      "std": 0.2485,
      "unique_colors": 54
     },
     "image": "/specimens/wang-tiles-specimen.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "poisson-disk-sampling",
  "wave-function-collapse",
  "voronoi-lloyd-relaxation"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "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/wang-tiles.json"
 },
 "solves": [
  "texture tiling visibly repeats across large surfaces",
  "non-periodic patterns from a small set of tiles",
  "tile floors and terrain without obvious seams",
  "cheap infinite texture without repetition"
 ],
 "authority": "Cohen et al., 2003",
 "url": "https://texelatlas.vercel.app/t/wang-tiles/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 2.45,
  "tier": "light",
  "p50_ms": 0.0805,
  "p95_ms": 0.0832,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "wang-tiles-specimen",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 1.94,
   "tier": "feather",
   "p50_ms": 0.0308,
   "p95_ms": 0.031,
   "speedup_x": 2.61,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 1.57,
   "tier": "feather",
   "p50_ms": 0.0452,
   "p95_ms": 0.0452,
   "speedup_x": 1.78,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}