TExcelTAD
Home › Shaders & Graphics Programming › Image-Based Lighting

Image-Based Lighting

In the PBR lesson the gold spheres were almost black: a metal only reflects, and two point lights are very little to reflect. In the real world light arrives from every direction: the sky, the ground, the walls. Image-based lighting (IBL) treats a whole environment image (an HDR photo, a cubemap of the level, a procedural sky) as a light source. It is what makes PBR materials actually look like chrome, brushed steel or glossy plastic, and it provides most of the ambient light in every modern engine.

The problem: an integral per pixel

The correct ambient color of a pixel is the sum of the light from every direction of the hemisphere, weighted by the BRDF. Doing that with hundreds of samples per pixel per frame is far too slow. IBL splits the integral into parts that can be precomputed once per environment:

Part What is precomputed Stored as Looked up with
diffuse irradiance the environment convolved with a cosine lobe: "how much light hits a surface facing direction N" a tiny cubemap (32×32 per face) or 9 spherical-harmonics coefficients the normal N
prefiltered specular the environment blurred by GGX lobes of increasing roughness the mip levels of a cubemap (mip 0 = mirror, last mip = fully rough) the reflection vector R and roughness → mip level
BRDF integration the rest of the specular integral, which depends only on NdotV and roughness a 2D lookup texture (the "BRDF LUT"), usually red/green 512×512 (NdotV, roughness)

This is the split-sum approximation from Brian Karis' "Real Shading in Unreal Engine 4" (2013). The final ambient is two lookups and a few multiplies:

glsl
vec3 irradiance  = texture(uIrradianceMap, N).rgb;
vec3 prefiltered = textureLod(uPrefilteredMap, R, roughness * MAX_MIP).rgb;
vec2 brdf        = texture(uBrdfLut, vec2(NdotV, roughness)).rg;

vec3 F       = F_SchlickRoughness(NdotV, F0, roughness);
vec3 kd      = (1.0 - F) * (1.0 - metallic);
vec3 ambient = kd * irradiance * albedo + prefiltered * (F0 * brdf.x + brdf.y);

IBL in a fragment shader

The playground has no textures, so the example below uses a procedural sky as the environment: a gradient sky, a sun and a ground plane, all computed from a direction. The two precomputed maps are replaced by small loops that sample the sky around N (irradiance) and around R (a cone that widens with roughness). The BRDF LUT is replaced by Karis' analytic fit for mobile, which matches the texture closely.

Top row: gold with roughness 0.05, 0.3, 0.6 and 0.9. Bottom row: white plastic with the same roughness values. Drag horizontally to rotate the environment.

glsl · try it yourself
const float PI = 3.14159265;

vec3 sky(vec3 d, float spin) {
    float c = cos(spin), s = sin(spin);
    d.xz = mat2(c, -s, s, c) * d.xz;
    vec3 sunDir = normalize(vec3(0.6, 0.45, -0.65));
    vec3 col = mix(vec3(0.55, 0.7, 0.95), vec3(0.08, 0.2, 0.6), sqrt(clamp(d.y, 0.0, 1.0)));   // sky
    col += vec3(12.0, 10.0, 7.0) * pow(max(dot(d, sunDir), 0.0), 400.0);                 // sun
    col += vec3(0.6, 0.5, 0.3) * pow(max(dot(d, sunDir), 0.0), 8.0);                     // glow
    if (d.y < 0.0) {                                                                      // ground
        float checker = mod(floor(d.x / max(-d.y, 0.05) * 2.0) + floor(d.z / max(-d.y, 0.05) * 2.0), 2.0);
        col = mix(vec3(0.25, 0.22, 0.2), vec3(0.4, 0.37, 0.33), checker) * smoothstep(-0.0, -0.08, d.y)
            + vec3(0.5, 0.55, 0.6) * smoothstep(-0.08, 0.0, d.y);
    }
    return col;
}

// Average of the environment over a cone around dir. spread = 0 is a mirror; 1 is a hemisphere.
vec3 envCone(vec3 dir, float spread, float spin) {
    vec3 t = normalize(cross(abs(dir.y) < 0.99 ? vec3(0, 1, 0) : vec3(1, 0, 0), dir));
    vec3 b = cross(dir, t);
    vec3 sum = vec3(0.0);
    const int N = 24;
    for (int i = 0; i < N; i++) {
        float fi = float(i) + 0.5;
        float r = sqrt(fi / float(N)) * spread;                 // even spread over the disk
        float a = fi * 2.39996;                                 // golden angle
        sum += sky(normalize(dir + (t * cos(a) + b * sin(a)) * r), spin);
    }
    return sum / float(N);
}

vec2 envBRDFApprox(float NdotV, float roughness) {              // Karis, "Physically Based Shading on Mobile"
    const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
    const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
    vec4 r = roughness * c0 + c1;
    float a004 = min(r.x * r.x, exp2(-9.28 * NdotV)) * r.x + r.y;
    return vec2(-1.04, 1.04) * a004 + r.zw;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    float spin = iMouse.z > 0.0 ? iMouse.x / iResolution.x * 6.28 : iTime * 0.2;
    vec2 p = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
    vec3 ro = vec3(0.0, 0.0, 4.3);
    vec3 rd = normalize(vec3(p, -2.4));
    vec3 color = sky(rd, spin);

    for (int i = 0; i < 8; i++) {
        float col = float(i % 4), row = float(i / 4);
        vec3 center = vec3((col - 1.5) * 1.15, row < 1.0 ? 0.6 : -0.6, 0.0);
        vec3 oc = ro - center;
        float b = dot(oc, rd), h = b * b - dot(oc, oc) + 0.25;   // radius 0.5
        if (h < 0.0) continue;
        vec3 pos = ro + rd * (-b - sqrt(h));
        vec3 N = normalize(pos - center), V = -rd, R = reflect(rd, N);
        float NdotV = max(dot(N, V), 1e-4);
        float roughness = mix(0.05, 0.9, col / 3.0);
        bool gold = row < 1.0;
        vec3 albedo = gold ? vec3(1.0, 0.71, 0.29) : vec3(0.9);
        float metallic = gold ? 1.0 : 0.0;
        vec3 F0 = mix(vec3(0.04), albedo, metallic);

        vec3 irradiance = envCone(N, 1.0, spin);                    // stands in for the irradiance map
        vec3 prefiltered = envCone(R, roughness * roughness * 1.5, spin);  // stands in for the mip chain
        vec2 brdf = envBRDFApprox(NdotV, roughness);
        vec3 kd = (1.0 - F0) * (1.0 - metallic);
        color = kd * albedo * irradiance + prefiltered * (F0 * brdf.x + brdf.y);
        break;
    }
    color = color / (1.0 + color);
    fragColor = vec4(pow(color, vec3(1.0 / 2.2)), 1.0);
}

Notice that the gold looks like gold now, the sharp reflections blur as roughness rises, and the white plastic picks up blue from the sky on top and brown from the ground below. That color bleeding is the irradiance term.

Note: 24 samples per lookup keep this example light enough for any GPU, but they are visibly noisy on the rough spheres. That noise is exactly what the precomputation removes: a real renderer runs thousands of samples once, stores the result in textures and pays two texture fetches per pixel at runtime.

Where the environment comes from

Source Used for
HDR panoramas (.hdr, .exr, equirectangular) product viewers, look-dev, outdoor skies; Poly Haven publishes free CC0 ones
reflection probes captured in the level interiors; the engine renders a cubemap at a point and blends between nearby probes (Unity Reflection Probes, Unreal Sphere/Box Reflection Captures, Godot ReflectionProbe)
a procedural sky dynamic time of day (Unity's Procedural Skybox, Unreal's Sky Atmosphere, Godot's ProceduralSkyMaterial / PhysicalSkyMaterial)
real-time global illumination Unreal's Lumen, Godot's SDFGI, Unity's Adaptive Probe Volumes add bounced light that static probes miss

Probes are captured from one point, so reflections on large flat floors look misplaced. Box projection (parallax-corrected cubemaps) fixes that for rooms, and screen-space or ray-traced reflections take over for mirrors and wet streets.

In Three.js

Three.js does all the precomputation with PMREMGenerator (Prefiltered, Mipmapped Radiance Environment Map). Assign the result to scene.environment and every MeshStandardMaterial and MeshPhysicalMaterial in the scene uses it for ambient diffuse and specular. The RoomEnvironment addon builds a simple lit room in code, so no files are needed:

html · try it yourself
<style>body { margin: 0 } canvas { display: block }</style>
<script type="module">
import * as THREE from 'three';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild(renderer.domElement);

const scene = new THREE.Scene();
const pmrem = new THREE.PMREMGenerator(renderer);
const envMap = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;   // prefiltered cubemap-like texture
scene.environment = envMap;                // lights every PBR material
scene.background = new THREE.Color(0x202024);
pmrem.dispose();

const camera = new THREE.PerspectiveCamera(35, innerWidth / innerHeight, 0.1, 50);
camera.position.set(0, 0, 9);

const geometry = new THREE.SphereGeometry(0.5, 64, 32);
const spheres = new THREE.Group();
for (let row = 0; row < 2; row++) {
  for (let col = 0; col < 5; col++) {
    const material = new THREE.MeshStandardMaterial({
      color: row === 0 ? 0xffb54a : 0xd02020,
      metalness: row === 0 ? 1 : 0,
      roughness: col / 4,
    });
    const mesh = new THREE.Mesh(geometry, material);
    mesh.position.set((col - 2) * 1.25, row === 0 ? 0.7 : -0.7, 0);
    spheres.add(mesh);
  }
}
scene.add(spheres);

renderer.setAnimationLoop((ms) => {
  spheres.rotation.y = Math.sin(ms / 3000) * 0.4;
  renderer.render(scene, camera);
});
</script>

There is not a single light in that scene: everything comes from the environment. For an HDR file you would load it with HDRLoader (or EXRLoader), set texture.mapping = THREE.EquirectangularReflectionMapping and assign it to scene.environment; Three.js then runs the PMREM step for you.

Test yourself

In the split-sum approximation, what does the BRDF lookup texture depend on?

  1. The environment map
  2. NdotV and roughness only
  3. The light position
  4. The albedo color

How does a prefiltered specular cubemap encode different roughness values?

  1. In separate files per material
  2. In its mip levels: sharper at mip 0, blurrier at higher mips
  3. In the alpha channel
  4. It cannot; roughness is applied afterwards

Which Three.js property makes all MeshStandardMaterial objects use an environment for lighting?

  1. scene.background
  2. scene.environment
  3. renderer.envMap
  4. camera.environment