Visible normal distribution sampling (GGX VNDF)
Sample only the microfacet normals visible from the viewer, rather than the whole distribution, for lower variance and no wasted samples below the horizon. Heitz's ellipsoid-stretch derivation gives a compact exact sampler for GGX.
In the family Materials, BRDFs & texturing, microfacet BRDF
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 | Forward+ and Mobile PBR shaders evaluate isotropic/anisotropic GGX BRDFs and cubemap filtering without Heitz VNDF importance sampling. |
| Unity | Built in | HDRP path tracing and SSR sample visible normals internally docs Shader library function; not a user setting. |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Naively importance-sampling D(h)(n.h) produces half-vectors that may be backfacing or masked, wasting samples and creating variance at grazing angles. The visible normal distribution D_v(h) = G1(v) max(0, v.h) D(h) / (n.v) is the distribution of normals a viewer actually sees. Heitz shows that for GGX it is the projection of a hemisphere of an ellipsoid, so one can transform the view vector by the roughness, sample a disk-based hemisphere in the stretched space, and transform back, avoiding any rejection. The resulting reflected direction's pdf is D_v(h)/(4 v.h), which cancels the Fresnel-masking factors and leaves weights near FG2/G1. Later variants (spherical caps, bounded VNDF) reduce cost further and avoid below-horizon directions.
When to choose it
Reach for it when
- noise at grazing angles in glossy sampling
- wasted samples on backfacing microfacets
- importance sampling ggx has high variance
- monte carlo specular rendering converges slowly
Choose it for Monte Carlo evaluation of GGX in path tracers, ReSTIR-style integrators, and pre-filtering of specular IBL on the GPU. Prefer plain D(h) sampling only when matching legacy implementations. Pair with Smith masking-shadowing function and GGX / Trowbridge-Reitz normal distribution since the sampler assumes Smith masking.
Field marks
- Assumes Smith masking, so any other G invalidates the pdf
- Numerical precision near zero roughness causes degenerate half-vectors; clamp alpha
- Backfacing shading normals need the unbiased variant
- Samples reflected below the horizon still occur, so a visibility check is needed
Presets
GGX VNDF Sampling — standard NDF high-variance grazing noise (left) vs low-variance visible-normal sampling (right)
{
"P_ROUGHNESS": 0.42,
"P_NOISE_SCALE": 1,
"P_LIGHT_SPIN": 0.55,
"P_SPLIT": 0.48
}Show GLSL (140 lines)
const float P_ROUGHNESS = 0.42; // P GGX Roughness [0.12, 0.85, 0.02]
const float P_NOISE_SCALE = 1.0; // P MC Variance Vis [0.2, 1.8, 0.05]
const float P_LIGHT_SPIN = 0.55; // P Env Spin [0.0, 2.2, 0.05]
const float P_SPLIT = 0.48; // P NDF / VNDF Split [0.0, 1.0, 0.02]
float hash21(vec2 p) {
p = fract(p * vec2(127.1, 311.7));
p += dot(p, p + 34.5);
return fract(p.x * p.y);
}
// High-contrast studio environment for Monte Carlo GGX reflection comparison
vec3 evalEnv(vec3 d, float spin) {
float ang = atan(d.z, d.x) + spin * 0.45;
vec3 sky = mix(vec3(0.04, 0.06, 0.11), vec3(0.18, 0.28, 0.45), smoothstep(-0.2, 0.7, d.y));
// Bright overhead softboxes & studio horizon strips
float box1 = smoothstep(0.26, 0.16, abs(sin(ang * 2.0))) * smoothstep(0.18, 0.52, d.y);
float sun = pow(max(dot(d, normalize(vec3(cos(spin * 0.45), 0.62, sin(spin * 0.45)))), 0.0), 32.0);
float rimStrip = exp(-abs(d.y - 0.08) * 18.0) * (0.4 + 0.6 * cos(ang * 3.0));
return sky + vec3(2.4, 2.2, 1.9) * box1 + vec3(3.2, 2.6, 1.8) * sun + vec3(0.4, 0.85, 1.0) * rimStrip * 0.7;
}
// Orthonormal tangent basis from surface normal n
void buildBasis(vec3 n, out vec3 t, out vec3 b) {
vec3 up = (abs(n.y) < 0.99) ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);
t = normalize(cross(up, n));
b = cross(n, t);
}
// Heitz 2018 exact GGX VNDF microfacet normal sampler in local tangent space (n = +Z)
vec3 sampleGGX_VNDF(vec3 Ve, float alpha, vec2 u) {
vec3 Vh = normalize(vec3(alpha * Ve.x, alpha * Ve.y, max(Ve.z, 1e-4)));
float lensq = Vh.x * Vh.x + Vh.y * Vh.y;
vec3 T1 = (lensq > 1e-6) ? vec3(-Vh.y, Vh.x, 0.0) / sqrt(lensq) : vec3(1.0, 0.0, 0.0);
vec3 T2 = cross(Vh, T1);
float r = sqrt(clamp(u.x, 0.0, 0.9999));
float phi = 6.2831853 * u.y;
float t1 = r * cos(phi);
float t2 = r * sin(phi);
float s = 0.5 * (1.0 + Vh.z);
t2 = (1.0 - s) * sqrt(max(1.0 - t1 * t1, 0.0)) + s * t2;
vec3 Nh = t1 * T1 + t2 * T2 + sqrt(max(0.0, 1.0 - t1 * t1 - t2 * t2)) * Vh;
return normalize(vec3(alpha * Nh.x, alpha * Nh.y, max(0.0, Nh.z)));
}
// Standard GGX NDF sampler (ignores view direction -> samples backfacing/masked microfacets at grazing angles!)
vec3 sampleGGX_NDF(float alpha, vec2 u) {
float phi = 6.2831853 * u.y;
float cosT = sqrt(max((1.0 - u.x) / max(1.0 + (alpha * alpha - 1.0) * u.x, 1e-5), 0.0));
float sinT = sqrt(max(1.0 - cosT * cosT, 0.0));
return vec3(sinT * cos(phi), sinT * sin(phi), cosT);
}
float smithG1(float ndx, float alpha) {
float a2 = alpha * alpha;
return 2.0 * ndx / max(ndx + sqrt(a2 + (1.0 - a2) * ndx * ndx), 1e-4);
}
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 isVNDF = screenX >= split;
float rough = clamp(P_ROUGHNESS, 0.10, 0.90);
float alpha = rough * rough;
float nGain = clamp(P_NOISE_SCALE, 0.1, 2.0);
float spin = iTime * max(P_LIGHT_SPIN, 0.0);
vec3 ro = vec3(0.0, 0.42, 2.25);
vec3 rd = normalize(vec3(uv - vec2(0.0, 0.08), -1.48));
vec3 col = evalEnv(rd, spin) * 0.35;
// Intersect matte-gold metallic sphere (R=0.58) and grazing metallic ground plane (y=-0.58)
float b = dot(ro, rd);
float c = dot(ro, ro) - 0.58 * 0.58;
float disc = b * b - c;
float tSph = (disc > 0.0 && (-b - sqrt(disc)) > 0.05) ? (-b - sqrt(disc)) : 1e5;
float tPl = (rd.y < -0.01) ? ((-0.58 - ro.y) / rd.y) : 1e5;
if (tPl < 0.05 || tPl > 14.0) tPl = 1e5;
float tHit = min(tSph, tPl);
if (tHit < 1e4) {
vec3 p = ro + rd * tHit;
vec3 n = (tSph < tPl) ? normalize(p) : vec3(0.0, 1.0, 0.0);
vec3 v = -rd;
vec3 T, B;
buildBasis(n, T, B);
vec3 Ve = normalize(vec3(dot(v, T), dot(v, B), max(dot(v, n), 0.005)));
// 4-sample Monte Carlo estimator per pixel: NDF suffers massive grazing fireflies/rejections; VNDF stays crisp!
vec3 acc = vec3(0.0);
vec3 f0 = (tSph < tPl) ? vec3(0.95, 0.76, 0.42) : vec3(0.55, 0.64, 0.78);
vec2 pixSeed = floor(fragCoord * mix(1.0, 0.65, clamp(nGain - 0.5, 0.0, 0.7)));
float frameJit = floor(iTime * 12.0) * 0.037;
for (int s = 0; s < 4; s++) {
float fs = float(s);
vec2 u = vec2(
hash21(pixSeed + vec2(fs * 17.3, frameJit * 11.1)),
hash21(pixSeed * 1.37 + vec2(frameJit * 7.3, fs * 29.7))
);
u = clamp(u, 0.002, 0.998);
vec3 hLocal = isVNDF ? sampleGGX_VNDF(Ve, alpha, u) : sampleGGX_NDF(alpha, u);
vec3 hWorld = normalize(T * hLocal.x + B * hLocal.y + n * hLocal.z);
vec3 lWorld = reflect(-v, hWorld);
float ndl = dot(n, lWorld);
float ndv = max(dot(n, v), 0.005);
float ndh = max(dot(n, hWorld), 0.005);
float vdh = max(dot(v, hWorld), 0.005);
if (ndl > 0.002) {
vec3 F = f0 + (1.0 - f0) * pow(1.0 - vdh, 5.0);
vec3 env = evalEnv(lWorld, spin);
if (isVNDF) {
// VNDF weight reduces cleanly to G1(L) * F -- bounded in [0, 1], zero grazing weight explosion!
float w = smithG1(ndl, alpha);
acc += env * F * w;
} else {
// Classic NDF weight G2 * (V.H) / (N.V * N.H) explodes at grazing N.V -> 0!
float G2 = smithG1(ndl, alpha) * smithG1(ndv, alpha);
float w = clamp((G2 * vdh) / max(ndv * ndh, 0.015), 0.0, 4.5 * nGain);
acc += env * F * w;
}
}
}
col = acc * 0.25;
if (tPl < tSph) col = mix(col, evalEnv(rd, spin) * 0.35, 1.0 - exp(-tPl * 0.14));
}
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.
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| vndf-sampling-live | Tesla T4 1920x1080 | 12.8× baseline (moderate) · 0.421 ms / 0.441 ms | opengl-timer-query · 32 frames · GPU Weight Atlas · Colab T4 sweep |
| vndf-sampling-live | NVIDIA L4 1920x1080 | 10.3× baseline (moderate) · 0.163 ms / 0.165 ms · 2.6× faster on L4 | opengl-timer-query · Cross-architecture validation |
| vndf-sampling-live | NVIDIA A100-SXM4-40GB 1920x1080 | 7.88× baseline (moderate) · 0.227 ms / 0.227 ms · 1.9× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
Sources
- primaryEric Heitz. Sampling the GGX Distribution of Visible Normals. JCGT 2018.Verified via live page · 2026-11-29