PBR: Metallic and Roughness
Blinn-Phong has knobs (shininess, a specular color) that look right under one light and wrong under another, and every artist tunes them differently. Physically based rendering (PBR) replaces those knobs with physical properties of the material, mainly base color, metallic and roughness, and computes the light with formulas that conserve energy. The result is that a material authored once looks plausible in a sunny level, a dark cave and a studio lightbox. Unity's URP Lit and HDRP Lit, Unreal's default lit material, Godot's StandardMaterial3D, Three.js' MeshStandardMaterial and glTF all use the same metallic/roughness model this lesson builds.
The model in one equation
For each light, the outgoing color is
Lo = (kd * albedo / PI + specular) * lightColor * NdotLalbedo / PIis the Lambert diffuse term (thePImakes it conserve energy).specularis the Cook-Torrance microfacet term:D * G * F / (4 * NdotV * NdotL).kdis how much light is left for diffuse after the reflection:(1 - F) * (1 - metallic).
Each letter of the specular term models what happens on a surface made of tiny mirror facets:
| Term | Name | Question it answers | Usual formula |
|---|---|---|---|
| D | normal distribution | what fraction of microfacets face the half vector H? |
GGX / Trowbridge-Reitz |
| G | geometry (shadowing-masking) | how many of them are hidden by other facets, from the light or the eye? | Smith with Schlick-GGX |
| F | Fresnel | how much light reflects instead of entering the surface at this angle? | Schlick's approximation |
const float PI = 3.14159265;
float D_GGX(float NdotH, float roughness) {
float a = roughness * roughness; // artists' roughness is squared ("alpha")
float a2 = a * a;
float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
return a2 / (PI * d * d);
}
float G_SchlickGGX(float NdotX, float k) { return NdotX / (NdotX * (1.0 - k) + k); }
float G_Smith(float NdotV, float NdotL, float roughness) {
float k = (roughness + 1.0) * (roughness + 1.0) / 8.0; // k for direct lights
return G_SchlickGGX(NdotV, k) * G_SchlickGGX(NdotL, k);
}
vec3 F_Schlick(float cosTheta, vec3 F0) {
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}Metallic and F0
F0 is the reflectance when you look straight at the surface. It is the key difference between the two kinds of material:
| Material | F0 | Diffuse | Highlight color |
|---|---|---|---|
| dielectric (plastic, wood, stone, skin, paint) | about 0.04 (grey, 4%) for almost all of them | the base color | white |
| metal (gold, copper, iron, aluminum) | high and colored: the base color itself (gold ≈ (1.00, 0.71, 0.29)) |
none: metals absorb refracted light | tinted |
So the metallic parameter is a switch between the two, implemented as a lerp:
vec3 F0 = mix(vec3(0.04), albedo, metallic);
vec3 kd = (1.0 - F) * (1.0 - metallic);Metallic should be 0 or 1 for real materials. Values in between are for transitions (rust on metal, dust, masks blurred by mipmapping), not for "slightly metal" surfaces.
The material grid
Each sphere below uses the same code. Left to right, roughness goes from 0.05 to 1.0; the top row is a red dielectric, the bottom row gold metal. Two lights, a key and a rim, rotate slowly.
const float PI = 3.14159265;
float D_GGX(float NdotH, float roughness) {
float a = roughness * roughness, a2 = a * a;
float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
return a2 / (PI * d * d);
}
float G_SchlickGGX(float NdotX, float k) { return NdotX / (NdotX * (1.0 - k) + k); }
float G_Smith(float NdotV, float NdotL, float roughness) {
float k = (roughness + 1.0) * (roughness + 1.0) / 8.0;
return G_SchlickGGX(NdotV, k) * G_SchlickGGX(NdotL, k);
}
vec3 F_Schlick(float cosTheta, vec3 F0) { return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); }
vec3 shade(vec3 N, vec3 V, vec3 L, vec3 lightColor, vec3 albedo, float metallic, float roughness) {
vec3 H = normalize(V + L);
float NdotL = max(dot(N, L), 0.0), NdotV = max(dot(N, V), 1e-4);
float NdotH = max(dot(N, H), 0.0), VdotH = max(dot(V, H), 0.0);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
vec3 F = F_Schlick(VdotH, F0);
vec3 specular = D_GGX(NdotH, roughness) * G_Smith(NdotV, NdotL, roughness) * F
/ (4.0 * NdotV * NdotL + 1e-4);
vec3 kd = (1.0 - F) * (1.0 - metallic);
return (kd * albedo / PI + specular) * lightColor * NdotL;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
const float COLS = 5.0, ROWS = 2.0;
vec2 cellSize = iResolution.xy / vec2(COLS, ROWS);
vec2 cell = floor(fragCoord / cellSize);
vec2 local = (fragCoord - (cell + 0.5) * cellSize) / (0.42 * min(cellSize.x, cellSize.y));
float roughness = mix(0.05, 1.0, cell.x / (COLS - 1.0));
bool metal = cell.y < 1.0; // bottom row
vec3 albedo = metal ? vec3(1.0, 0.71, 0.29) : vec3(0.8, 0.1, 0.08);
float metallic = metal ? 1.0 : 0.0;
vec3 color = vec3(0.02);
float r2 = dot(local, local);
if (r2 < 1.0) {
vec3 N = vec3(local, sqrt(1.0 - r2)); // sphere seen from the front
vec3 V = vec3(0.0, 0.0, 1.0);
float a = iTime * 0.5;
vec3 key = normalize(vec3(cos(a), 0.8, sin(a) + 0.6));
vec3 rim = normalize(vec3(-0.7, -0.2, -0.3));
color = shade(N, V, key, vec3(4.0), albedo, metallic, roughness)
+ shade(N, V, rim, vec3(1.5, 1.6, 2.0), albedo, metallic, roughness)
+ albedo * 0.03; // tiny ambient (see the IBL lesson)
}
color = color / (1.0 + color); // Reinhard tone mapping
fragColor = vec4(pow(color, vec3(1.0 / 2.2)), 1.0); // linear -> gamma
}Things to see:
- Energy conservation. A rough highlight is wider but dimmer; a smooth one is tiny and very bright. The total reflected energy is about the same, because GGX's
Dis normalized. - Metals are dark without reflections. A metal has no diffuse term, so the gold spheres are black except where a light reflects. Under a real environment (next lesson, Image-Based Lighting) they reflect the surroundings and look like metal. This is the most common "my PBR looks wrong" surprise: PBR materials need an environment.
- Fresnel. Even the red plastic reflects strongly at grazing angles (the rim light on the edges), because
Frises toward 1 there.
Explore one material
Drag the mouse: horizontal position sets roughness, vertical position sets metallic. The base color is a light blue, so you can see how a metal tints its highlight while a dielectric keeps it white.
const float PI = 3.14159265;
float D_GGX(float NdotH, float r) { float a = r * r, a2 = a * a; float d = NdotH * NdotH * (a2 - 1.0) + 1.0; return a2 / (PI * d * d); }
float G1(float x, float k) { return x / (x * (1.0 - k) + k); }
vec3 F_Schlick(float c, vec3 F0) { return F0 + (1.0 - F0) * pow(1.0 - c, 5.0); }
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 m = iMouse.z > 0.0 ? iMouse.xy / iResolution.xy : vec2(0.35, 0.0);
float roughness = clamp(m.x, 0.03, 1.0);
float metallic = clamp(m.y * 1.2 - 0.1, 0.0, 1.0);
vec3 albedo = vec3(0.3, 0.6, 0.95);
vec2 p = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
vec3 color = mix(vec3(0.05, 0.05, 0.07), vec3(0.15, 0.16, 0.2), fragCoord.y / iResolution.y);
float r2 = dot(p, p) / 0.7;
if (r2 < 1.0) {
vec3 N = vec3(p / sqrt(0.7), sqrt(1.0 - r2));
vec3 V = vec3(0.0, 0.0, 1.0);
vec3 L = normalize(vec3(0.6 * cos(iTime * 0.7), 0.7, 0.6));
vec3 H = normalize(V + L);
float NdotL = max(dot(N, L), 0.0), NdotV = max(N.z, 1e-4), NdotH = max(dot(N, H), 0.0);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
vec3 F = F_Schlick(max(dot(V, H), 0.0), F0);
float k = (roughness + 1.0) * (roughness + 1.0) / 8.0;
vec3 spec = D_GGX(NdotH, roughness) * G1(NdotV, k) * G1(NdotL, k) * F / (4.0 * NdotV * NdotL + 1e-4);
vec3 kd = (1.0 - F) * (1.0 - metallic);
color = (kd * albedo / PI + spec) * vec3(5.0) * NdotL + albedo * 0.05 * (1.0 - metallic * 0.7);
}
// a little readout: two bars at the bottom-left
vec2 uv = fragCoord / iResolution.xy;
if (uv.y < 0.03 && uv.x < roughness * 0.3) color = vec3(1.0, 0.8, 0.2);
if (uv.y > 0.04 && uv.y < 0.07 && uv.x < metallic * 0.3) color = vec3(0.4, 0.8, 1.0);
color = color / (1.0 + color);
fragColor = vec4(pow(color, vec3(1.0 / 2.2)), 1.0);
}The yellow bar is roughness, the blue bar metallic.
Linear light, tone mapping, gamma
The two last lines of both shaders are not decoration:
- All lighting math happens in linear space. Light adds linearly; sRGB-encoded colors do not. Textures authored in sRGB (base color) are converted to linear when sampled (the GPU does it for
SRGB8_ALPHA8textures), data textures (normal, roughness, metallic) are already linear. - Tone mapping compresses the unbounded result (the highlights above reach 20 or more) into 0..1. Reinhard
c / (1 + c)is the simplest; games use ACES-like or AgX curves (see Color Grading, Tone Mapping and Vignette). - Gamma encoding (
pow(c, 1/2.2), or the exact sRGB curve) turns linear values into what the monitor expects. Forget it and everything looks too dark with harsh falloff; do it twice and everything looks washed out.
Engines do steps 2 and 3 for you at the end of the frame, which is why a custom shader in an engine must not apply its own gamma.
Beyond the basics
Production PBR adds more than this lesson: image-based ambient light, a multiple-scattering energy compensation term (rough metals lose energy with single-scattering GGX), clear coat (car paint), sheen (cloth), anisotropy (brushed metal), subsurface scattering (skin) and transmission (glass). The reference write-ups are Brian Karis' "Real Shading in Unreal Engine 4" (SIGGRAPH 2013) and the Filament documentation by Google, which is free online and derives every term.
Test yourself
What is a typical F0 (reflectance at normal incidence) for a non-metal such as plastic?
- 0.0
- About 0.04
- About 0.5
- The base color
Why does a fully metallic sphere look almost black when lit only by a couple of point lights?
- Metals have no specular term
- Metals have no diffuse term, so they only show reflections of lights and the environment
- The Fresnel term is zero for metals
- Metals need a higher roughness
Why is roughness squared before it is used in the GGX distribution?
- To make it always positive
- So the slider feels perceptually linear to artists
- Because GLSL requires it
- To convert it to gamma space