{
 "id": "histogram-preserving-blending",
 "title": "Histogram-preserving blending (stochastic texturing)",
 "aliases": [],
 "domain": "materials",
 "subdomain": "texture synthesis",
 "stage": "material",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "unity",
  "unreal",
  "godot",
  "blender"
 ],
 "year_introduced": 2018,
 "inputs": [
  "texture"
 ],
 "outputs": [
  "texture"
 ],
 "style": "photoreal",
 "dynamic": "both",
 "summary": "Heitz and Neyret's by-example noise: tile a texture stochastically on a triangular grid and blend overlapping tiles in a Gaussianised colour space so the blended result keeps the exemplar's histogram, instead of washing out to grey.",
 "how_it_works": "Naive random blending of texture tiles averages colours, which narrows the histogram and loses contrast. The method first transforms the exemplar to a Gaussian colour space by histogram transfer and stores an inverse lookup table. A triangle grid selects three random tile offsets per pixel; the three Gaussianised samples are blended linearly with barycentric weights, which keeps Gaussian statistics, then variance is renormalised by the weights' length, and the table is inverted to return to colour space. Result: non-repeating texture with the same colour distribution as the input, evaluated with three fetches and no precomputed large texture. Works best on stochastic (non-structured) exemplars like rock, grass or dirt, and requires texture compression of the Gaussianised data to avoid quantisation.",
 "when_to_use": "Choose it to remove tiling on stochastic textures such as terrain and large floors. Prefer `hex-tiling` when you want a cheaper, simpler hexagonal variant that works with arbitrary textures (it has weaker statistics preservation). Prefer `image-quilting` when the texture has structure that must be preserved. Combine with `triplanar-mapping` on cliffs.",
 "cost": {
  "class": "cheap",
  "notes": "Three texture fetches plus a LUT inversion per map; modest overhead over plain tiling."
 },
 "failure_modes": [
  "Fails on structured textures (bricks, tiles): blending destroys edges and creates ghosting",
  "Gaussian transform needs precomputation and extra LUT storage per texture",
  "Compressed textures can band after the inverse LUT",
  "Blending normals or roughness maps needs separate care, since they are not colour-histogram data"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/3233304",
   "type": "paper",
   "title": "High-Performance By-Example Noise using a Histogram-Preserving Blending Operator",
   "authors": [
    "Eric Heitz",
    "Fabrice Neyret"
   ],
   "year": 2018,
   "venue": "Proceedings of the ACM on Computer Graphics and Interactive Techniques",
   "url": "https://doi.org/10.1145/3233304",
   "ids": {
    "doi": "10.1145/3233304"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3233304",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3233304",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3233304",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3233304",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3233304",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "heitz2018high"
  }
 ],
 "presets": [
  {
   "id": "histogram-preserving-blending-live",
   "title": "Histogram-Preserving Blending — linear hex-tile washout (left) vs variance-preserving stochastic blend (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_TILE_SCALE": 5.5,
    "P_BLEND_SOFT": 0.75,
    "P_CAM_DRIFT": 0.6,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_TILE_SCALE = 5.5; // P Hex Grid Scale [2.5, 10.0, 0.25]\nconst float P_BLEND_SOFT = 0.75; // P Overlap Width [0.2, 1.0, 0.02]\nconst float P_CAM_DRIFT = 0.60; // P Glide Speed [0.0, 2.0, 0.05]\nconst float P_SPLIT = 0.48; // P Linear / Histogram [0.0, 1.0, 0.02]\n\nvec2 hash22(vec2 p) {\n  vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));\n  p3 += dot(p3, p3.yzx + 33.33);\n  return fract((p3.xx + p3.yz) * p3.zy);\n}\n\n// High-contrast exemplar stone-pebble & lichen texture (Gaussian-like zero-mean scalar + rich palette)\n// Returns vec4(rgb, gaussianVal) where gaussianVal has mean 0 and unit variance for exact HPB math\nvec4 sampleExemplar(vec2 uv) {\n  vec2 g = uv * 4.5;\n  vec2 i = floor(g);\n  vec2 f = fract(g);\n  float d1 = 8.0;\n  float d2 = 8.0;\n  vec2 id = vec2(0.0);\n  for (int y = -1; y <= 1; y++) {\n    for (int x = -1; x <= 1; x++) {\n      vec2 n = vec2(float(x), float(y));\n      vec2 o = hash22(i + n);\n      vec2 r = n + 0.18 + 0.64 * o - f;\n      float d = dot(r, r);\n      if (d < d1) { d2 = d1; d1 = d; id = i + n; }\n      else if (d < d2) { d2 = d; }\n    }\n  }\n  float edge = sqrt(d2) - sqrt(d1);\n  float pebble = smoothstep(0.04, 0.18, edge);\n  float grain = sin(uv.x * 48.0 + sin(uv.y * 36.0)) * 0.12;\n  float h = clamp(pebble * (0.68 + 0.32 * hash22(id).x) + grain, 0.0, 1.0);\n  // Zero-mean centered Gaussian proxy for variance-preserving blend\n  float gVal = (h - 0.52) * 2.2;\n  vec3 rock = mix(vec3(0.10, 0.12, 0.15), vec3(0.76, 0.68, 0.56), h);\n  rock = mix(rock, vec3(0.26, 0.44, 0.24), smoothstep(0.08, 0.02, edge) * 0.72);\n  return vec4(rock, gVal);\n}\n\n// Map centered Gaussianized value back through non-linear exemplar CDF -> razor-sharp contrast!\nvec3 invCDFColor(float gVal, vec3 avgTint) {\n  // Smooth S-curve matching the bimodal pebble/mortar histogram\n  float h = clamp(0.52 + 0.46 * tanh(gVal * 1.15), 0.0, 1.0);\n  vec3 c = mix(vec3(0.07, 0.09, 0.12), vec3(0.82, 0.72, 0.58), smoothstep(0.18, 0.82, h));\n  c = mix(c, vec3(0.22, 0.46, 0.22), smoothstep(0.32, 0.12, h) * 0.65);\n  return mix(c, avgTint * 1.15, 0.28);\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 screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  bool isHPB = screenX >= split;\n  float scale = clamp(P_TILE_SCALE, 2.0, 12.0);\n  float soft = clamp(P_BLEND_SOFT, 0.15, 1.0);\n  float drift = iTime * max(P_CAM_DRIFT, 0.0) * 0.35;\n\n  // Oblique ground plane to showcase large-scale non-repeating stochastic tiling\n  vec3 ro = vec3(0.0, 1.15, 2.0 - drift);\n  vec3 rd = normalize(vec3(uv.x, uv.y - 0.36, -1.25));\n  vec3 sky = mix(vec3(0.035, 0.048, 0.082), vec3(0.12, 0.18, 0.28), smoothstep(-0.15, 0.5, uv.y));\n  vec3 col = sky;\n\n  if (rd.y < -0.01) {\n    float t = -ro.y / rd.y;\n    if (t > 0.1 && t < 24.0) {\n      vec3 p = ro + rd * t;\n      vec2 st = p.xz * scale * 0.38;\n\n      // Triangular simplex lattice for 3-tap stochastic hex-tile blending (Heitz & Neyret 2018)\n      const mat2 skew = mat2(1.0, 0.0, -0.57735027, 1.15470054);\n      vec2 sUV = skew * st;\n      vec2 base = floor(sUV);\n      vec2 f = fract(sUV);\n      vec2 v1 = base;\n      vec2 v2 = base + ((f.x > f.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0));\n      vec2 v3 = base + vec2(1.0, 1.0);\n\n      // Barycentric weights of the 3 stochastic tiles\n      float w1 = (f.x > f.y) ? (1.0 - f.x) : (1.0 - f.y);\n      float w2 = abs(f.x - f.y);\n      float w3 = min(f.x, f.y);\n\n      // Sharpen or widen overlap region with P_BLEND_SOFT\n      float pw = mix(4.5, 1.15, soft);\n      w1 = pow(max(w1, 1e-4), pw);\n      w2 = pow(max(w2, 1e-4), pw);\n      w3 = pow(max(w3, 1e-4), pw);\n      float wSum = w1 + w2 + w3;\n      w1 /= wSum; w2 /= wSum; w3 /= wSum;\n\n      // 3 randomized tile offsets\n      vec4 t1 = sampleExemplar(st + hash22(v1) * 7.3);\n      vec4 t2 = sampleExemplar(st + hash22(v2) * 7.3);\n      vec4 t3 = sampleExemplar(st + hash22(v3) * 7.3);\n\n      vec3 linRGB = w1 * t1.rgb + w2 * t2.rgb + w3 * t3.rgb;\n      if (!isHPB) {\n        // Left: Standard linear blending (notice muddy contrast loss inside overlap zones!)\n        col = linRGB;\n      } else {\n        // Right: Histogram-Preserving Blending normalizes by L2 norm sqrt(w1^2 + w2^2 + w3^2)!\n        float invL2 = 1.0 / sqrt(max(w1 * w1 + w2 * w2 + w3 * w3, 1e-4));\n        float gBlend = (w1 * t1.w + w2 * t2.w + w3 * t3.w) * invL2;\n        col = invCDFColor(gBlend, linRGB);\n      }\n\n      // Subtle hex-blend overlap indicator contour\n      float overlap = 1.0 - max(w1, max(w2, w3));\n      col += (isHPB ? vec3(0.04, 0.14, 0.16) : vec3(0.16, 0.05, 0.08)) * smoothstep(0.35, 0.65, overlap) * 0.35;\n      col = mix(col, sky, 1.0 - exp(-t * 0.075));\n    }\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "histogram-preserving-blending",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T20:35:49.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.698,
      "std": 0.1199,
      "min": 0.4136,
      "max": 0.9014
     },
     "image": "/specimens/histogram-preserving-blending-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "hex-tiling",
  "image-quilting",
  "triplanar-mapping",
  "procedural-noise-texturing"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "triplanar-mapping",
   "why": "Removes visible repetition from each of the three projected fetches."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/materials/histogram-preserving-blending.json"
 },
 "solves": [
  "blending tiles washes the texture out to grey",
  "tiling hidden but contrast and detail get lost",
  "need seamless non-repeating terrain texture",
  "stochastic tiling must keep the original colour distribution"
 ],
 "authority": "Heitz & Neyret, 2018",
 "url": "https://texelatlas.vercel.app/t/histogram-preserving-blending/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 18.52,
  "tier": "moderate",
  "p50_ms": 0.6093,
  "p95_ms": 0.6211,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "histogram-preserving-blending-live",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 15.46,
   "tier": "moderate",
   "p50_ms": 0.2458,
   "p95_ms": 0.2482,
   "speedup_x": 2.48,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 12.03,
   "tier": "moderate",
   "p50_ms": 0.3466,
   "p95_ms": 0.3468,
   "speedup_x": 1.76,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}