Voronoi Diagrams and Lloyd Relaxation
Partition space by nearest seed (Voronoi), then repeatedly move each seed to the centroid of its cell. The cells become evenly sized and well-shaped, ideal for regions, provinces and tessellations.
Also known as centroidal Voronoi tessellation, CVT, Lloyd's algorithm
In the family Procedural generation, point distributions
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | No built-in Lloyd relaxation Voronoi diagram generator. |
| Unity | None found | Shader Graph includes Voronoi noise, but no built-in Lloyd relaxation solver. |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Given seeds, the Voronoi diagram gives each seed the region of points closer to it than to any other; its dual is the Delaunay triangulation, computable in O(n log n) with Fortune's sweep or incremental Delaunay. Lloyd's iteration computes each cell's centroid (area-weighted, optionally with a density function) and moves the seed there, then recomputes the diagram. Each step reduces a quantisation energy and the process converges to a centroidal Voronoi tessellation (CVT) with hexagon-like cells in 2D. A few iterations (2 to 5) suffice for visually even cells without losing all randomness. Clipping the diagram to a boundary needs care at the border.
When to choose it
Reach for it when
- voronoi cells are wildly uneven in size
- need evenly shaped map regions or provinces
- crack or fragment patterns look too irregular
- relax random seeds into regular looking cells
Choose it for map regions, cracks and fragment shapes, cell-based terrain and mesh point distributions where regular but irregular-looking cells are wanted. Prefer Poisson Disk Sampling (Bridson) when you only need points, Worley (Cellular) Noise for a per-pixel Voronoi texture with no explicit mesh, and Wang Tiles (Aperiodic Tile Synthesis) for aperiodic tilings with stored tiles.
Field marks
- Too many iterations converge to a uniform hex grid that looks artificial; stop early
- Cells at domain boundaries are unbounded or clipped asymmetrically, so seeds drift outward
- Degenerate configurations (co-circular points) create zero-length edges and numerical instabilities
- Density-weighted CVT needs numeric integration, which is slow and noisy for sharp density changes
Presets
Voronoi cells of random sites (left) vs one Lloyd relaxation step (right)
{
"CELLS": 7,
"SAMP": 6
}Show GLSL (39 lines)
// Voronoi of jittered sites: left = raw random sites, right = after one Lloyd step (sites moved to their cell centroids, sampled).
const float CELLS = 7.0;
const int SAMP = 6;
vec2 hash2(vec2 p) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453); }
vec2 site0(vec2 c) { return c + 0.08 + 0.84 * hash2(c); }
vec2 relaxed(vec2 c) {
vec2 s = site0(c), sum = vec2(0.0); float n = 0.0;
for (int j = 0; j < SAMP; j++) for (int i = 0; i < SAMP; i++) {
vec2 q = s + (vec2(float(i), float(j)) + 0.5) / float(SAMP) * 2.4 - 1.2;
vec2 qc = floor(q);
float bd = 1e9; vec2 bs = vec2(0);
for (int b = -1; b <= 1; b++) for (int a = -1; a <= 1; a++) {
vec2 sc = site0(qc + vec2(a, b));
float d = distance(q, sc);
if (d < bd) { bd = d; bs = sc; }
}
if (distance(bs, s) < 1e-4) { sum += q; n += 1.0; }
}
return n > 0.0 ? sum / n : s;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = fragCoord / iResolution.xy;
bool R = uv.x > 0.5;
vec2 p = uv * CELLS, ip = floor(p);
float d1 = 1e9, d2 = 1e9; vec2 best = vec2(0); vec2 bc = vec2(0);
for (int j = -1; j <= 1; j++) for (int i = -1; i <= 1; i++) {
vec2 c = ip + vec2(i, j);
vec2 s = R ? relaxed(c) : site0(c);
float d = distance(p, s);
if (d < d1) { d2 = d1; d1 = d; best = s; bc = c; } else if (d < d2) d2 = d;
}
vec3 col = 0.45 + 0.4 * cos(6.2831 * (hash2(bc).x + vec3(0.0, 0.33, 0.67)));
col *= 0.8 + 0.2 * smoothstep(0.0, 0.1, d2 - d1);
col = mix(col, vec3(0.05), smoothstep(0.05, 0.0, d2 - d1) * 0.8);
col = mix(col, vec3(0.05), smoothstep(0.1, 0.07, d1));
col = mix(col, vec3(1.0), smoothstep(0.004, 0.0, abs(uv.x - 0.5)));
fragColor = vec4(pow(col, vec3(0.8)), 1.0);
}
Attribution: written for this index
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| voronoi-lloyd-relaxation-specimen | Tesla T4 1920x1080 | 703× baseline (extreme) · 23.1 ms / 23.5 ms | opengl-timer-query · 20 frames · GPU Weight Atlas · Colab T4 sweep |
| voronoi-lloyd-relaxation-specimen | NVIDIA L4 1920x1080 | 738× baseline (extreme) · 11.7 ms / 12.0 ms · 2.0× faster on L4 | opengl-timer-query · Cross-architecture validation |
| voronoi-lloyd-relaxation-specimen | NVIDIA A100-SXM4-40GB 1920x1080 | 503× baseline (extreme) · 14.5 ms / 14.7 ms · 1.6× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
From the citation graph (Crossref reference lists): cited by Sample Distribution Shadow Maps (SDSM).
Sources
- primaryS. Lloyd (1982). Least squares quantization in PCM. IEEE Transactions on Information Theory.Verified via crossref · 2026-11-29doi:10.1109/tit.1982.1056489
- primaryQiang Du, Vance Faber, Max Gunzburger (1999). Centroidal Voronoi Tessellations: Applications and Algorithms. SIAM Review.Verified via crossref · 2026-11-29doi:10.1137/s0036144599352836
- implementationd3 (2018). d3/d3-delaunay. Compute the Voronoi diagram of a set of two-dimensional points.GitHub API · license ISC · 2026-11-29