---
id: "histogram-preserving-blending"
title: "Histogram-preserving blending (stochastic texturing)"
domain: "materials"
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"
cost: "cheap"
authority: "Heitz & Neyret, 2018"
url: "https://texelatlas.vercel.app/t/histogram-preserving-blending/"
sources_verified_at: "2026-11-29"
---
# Histogram-preserving blending (stochastic texturing) (Heitz & Neyret, 2018)

> 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.

## Reach for it when

- 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

## 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 it

Choose it to remove tiling on stochastic textures such as terrain and large floors. Prefer [Hex-tiling](https://texelatlas.vercel.app/t/hex-tiling.md) when you want a cheaper, simpler hexagonal variant that works with arbitrary textures (it has weaker statistics preservation). Prefer [Image quilting](https://texelatlas.vercel.app/t/image-quilting.md) when the texture has structure that must be preserved. Combine with [Triplanar mapping](https://texelatlas.vercel.app/t/triplanar-mapping.md) on cliffs.

## Cost

cheap — Three texture fetches plus a LUT inversion per map; modest overhead over plain tiling.
Measured GPU Weight: 18.52× baseline (moderate) — 0.6093 ms median / 0.6211 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 15.46× baseline (moderate) — 0.2458 ms median (2.48× faster on L4).
Ampere A100 scaling: 12.03× baseline (moderate) — 0.3466 ms median (1.76× faster on A100).

## 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

## Dataflow

- Inputs: texture
- Outputs: texture
- Platforms: webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, godot, blender

## Presets

### Histogram-Preserving Blending — linear hex-tile washout (left) vs variance-preserving stochastic blend (right) (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_TILE_SCALE": 5.5,
  "P_BLEND_SOFT": 0.75,
  "P_CAM_DRIFT": 0.6,
  "P_SPLIT": 0.48
}
```

```glsl
const float P_TILE_SCALE = 5.5; // P Hex Grid Scale [2.5, 10.0, 0.25]
const float P_BLEND_SOFT = 0.75; // P Overlap Width [0.2, 1.0, 0.02]
const float P_CAM_DRIFT = 0.60; // P Glide Speed [0.0, 2.0, 0.05]
const float P_SPLIT = 0.48; // P Linear / Histogram [0.0, 1.0, 0.02]

vec2 hash22(vec2 p) {
  vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
  p3 += dot(p3, p3.yzx + 33.33);
  return fract((p3.xx + p3.yz) * p3.zy);
}

// High-contrast exemplar stone-pebble & lichen texture (Gaussian-like zero-mean scalar + rich palette)
// Returns vec4(rgb, gaussianVal) where gaussianVal has mean 0 and unit variance for exact HPB math
vec4 sampleExemplar(vec2 uv) {
  vec2 g = uv * 4.5;
  vec2 i = floor(g);
  vec2 f = fract(g);
  float d1 = 8.0;
  float d2 = 8.0;
  vec2 id = vec2(0.0);
  for (int y = -1; y <= 1; y++) {
    for (int x = -1; x <= 1; x++) {
      vec2 n = vec2(float(x), float(y));
      vec2 o = hash22(i + n);
      vec2 r = n + 0.18 + 0.64 * o - f;
      float d = dot(r, r);
      if (d < d1) { d2 = d1; d1 = d; id = i + n; }
      else if (d < d2) { d2 = d; }
    }
  }
  float edge = sqrt(d2) - sqrt(d1);
  float pebble = smoothstep(0.04, 0.18, edge);
  float grain = sin(uv.x * 48.0 + sin(uv.y * 36.0)) * 0.12;
  float h = clamp(pebble * (0.68 + 0.32 * hash22(id).x) + grain, 0.0, 1.0);
  // Zero-mean centered Gaussian proxy for variance-preserving blend
  float gVal = (h - 0.52) * 2.2;
  vec3 rock = mix(vec3(0.10, 0.12, 0.15), vec3(0.76, 0.68, 0.56), h);
  rock = mix(rock, vec3(0.26, 0.44, 0.24), smoothstep(0.08, 0.02, edge) * 0.72);
  return vec4(rock, gVal);
}

// Map centered Gaussianized value back through non-linear exemplar CDF -> razor-sharp contrast!
vec3 invCDFColor(float gVal, vec3 avgTint) {
  // Smooth S-curve matching the bimodal pebble/mortar histogram
  float h = clamp(0.52 + 0.46 * tanh(gVal * 1.15), 0.0, 1.0);
  vec3 c = mix(vec3(0.07, 0.09, 0.12), vec3(0.82, 0.72, 0.58), smoothstep(0.18, 0.82, h));
  c = mix(c, vec3(0.22, 0.46, 0.22), smoothstep(0.32, 0.12, h) * 0.65);
  return mix(c, avgTint * 1.15, 0.28);
}

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 screenX = fragCoord.x / max(iResolution.x, 1.0);
  float split = clamp(P_SPLIT, 0.0, 1.0);
  bool isHPB = screenX >= split;
  float scale = clamp(P_TILE_SCALE, 2.0, 12.0);
  float soft = clamp(P_BLEND_SOFT, 0.15, 1.0);
  float drift = iTime * max(P_CAM_DRIFT, 0.0) * 0.35;

  // Oblique ground plane to showcase large-scale non-repeating stochastic tiling
  vec3 ro = vec3(0.0, 1.15, 2.0 - drift);
  vec3 rd = normalize(vec3(uv.x, uv.y - 0.36, -1.25));
  vec3 sky = mix(vec3(0.035, 0.048, 0.082), vec3(0.12, 0.18, 0.28), smoothstep(-0.15, 0.5, uv.y));
  vec3 col = sky;

  if (rd.y < -0.01) {
    float t = -ro.y / rd.y;
    if (t > 0.1 && t < 24.0) {
      vec3 p = ro + rd * t;
      vec2 st = p.xz * scale * 0.38;

      // Triangular simplex lattice for 3-tap stochastic hex-tile blending (Heitz & Neyret 2018)
      const mat2 skew = mat2(1.0, 0.0, -0.57735027, 1.15470054);
      vec2 sUV = skew * st;
      vec2 base = floor(sUV);
      vec2 f = fract(sUV);
      vec2 v1 = base;
      vec2 v2 = base + ((f.x > f.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0));
      vec2 v3 = base + vec2(1.0, 1.0);

      // Barycentric weights of the 3 stochastic tiles
      float w1 = (f.x > f.y) ? (1.0 - f.x) : (1.0 - f.y);
      float w2 = abs(f.x - f.y);
      float w3 = min(f.x, f.y);

      // Sharpen or widen overlap region with P_BLEND_SOFT
      float pw = mix(4.5, 1.15, soft);
      w1 = pow(max(w1, 1e-4), pw);
      w2 = pow(max(w2, 1e-4), pw);
      w3 = pow(max(w3, 1e-4), pw);
      float wSum = w1 + w2 + w3;
      w1 /= wSum; w2 /= wSum; w3 /= wSum;

      // 3 randomized tile offsets
      vec4 t1 = sampleExemplar(st + hash22(v1) * 7.3);
      vec4 t2 = sampleExemplar(st + hash22(v2) * 7.3);
      vec4 t3 = sampleExemplar(st + hash22(v3) * 7.3);

      vec3 linRGB = w1 * t1.rgb + w2 * t2.rgb + w3 * t3.rgb;
      if (!isHPB) {
        // Left: Standard linear blending (notice muddy contrast loss inside overlap zones!)
        col = linRGB;
      } else {
        // Right: Histogram-Preserving Blending normalizes by L2 norm sqrt(w1^2 + w2^2 + w3^2)!
        float invL2 = 1.0 / sqrt(max(w1 * w1 + w2 * w2 + w3 * w3, 1e-4));
        float gBlend = (w1 * t1.w + w2 * t2.w + w3 * t3.w) * invL2;
        col = invCDFColor(gBlend, linRGB);
      }

      // Subtle hex-blend overlap indicator contour
      float overlap = 1.0 - max(w1, max(w2, w3));
      col += (isHPB ? vec3(0.04, 0.14, 0.16) : vec3(0.16, 0.05, 0.08)) * smoothstep(0.35, 0.65, overlap) * 0.35;
      col = mix(col, sky, 1.0 - exp(-t * 0.075));
    }
  }

  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
  col = mix(col, vec3(0.95, 0.97, 1.0), sep);

  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: MIT — written for this index.

## Lineage

- extended by [Hex-tiling](https://texelatlas.vercel.app/t/hex-tiling.md)

## Combines with

- [Triplanar mapping](https://texelatlas.vercel.app/t/triplanar-mapping.md): Removes visible repetition from each of the three projected fetches.

## Sources

1. [primary] Eric Heitz, Fabrice Neyret (2018) High-Performance By-Example Noise using a Histogram-Preserving Blending Operator. Proceedings of the ACM on Computer Graphics and Interactive Techniques. https://doi.org/10.1145/3233304 (verified via crossref on 2026-11-29)

## Cite this entry

Texel. "Histogram-preserving blending (stochastic texturing)." https://texelatlas.vercel.app/t/histogram-preserving-blending/ (accessed 2026-12-01).
