Texel A field guide to 3D techniques

Hosek-Wilkie Sky Model × Precomputed Radiance Transfer

Day-cycle relighting for a dot product. Static buildings and props relit through a full day with soft self-shadowing and interreflection, without rebakes. An analytic sky gives a fresh environment whenever the sun moves; it is projected to spherical harmonics and dotted with baked transfer.

Open in StudioReproduced296× · 9.18 ms

Hosek-Wilkie Sky ModelVolume · Real-timein: material-paramsout: radiance, hdr-imagehdr-imagePrecomputed Radiance TransferLighting · Real-timein: mesh, hdr-imageout: radiance

Verified proof

reproduced · 2026-12-01texel-lablicense MIT
Specimen render of Time-of-day PRT (left: flat ambient + direct sun, right: Hosek-Wilkie SH projection dotted with PRT transfer)
Cross-proof specimen. Time-of-day PRT (left: flat ambient + direct sun, right: Hosek-Wilkie SH projection dotted with PRT transfer). Verified WebGL2 proof · 2026-12-01

Split-screen WebGL2 proof comparing flat hemisphere sky ambient plus direct sun (left) against an analytic Hosek-Wilkie sky projected live into windowed order-3 spherical harmonics and dotted with directional self-shadowing and interreflection PRT transfer vectors on a static stone colonnade (right).

Chromium Headless ANGLE/SwiftShader WebGL2 · 512x512 · relights static colonnade alcoves and plinths smoothly from dawn to noon with directional sky chromaticity and soft interreflection via order-3 SH dot product

Show GLSL proof (260 lines)
const float P_SUN_ELEV = 0.28; // P Sun Elevation [0.04, 1.25, 0.02]
const float P_TURBIDITY = 3.20; // P Sky Turbidity [1.5, 8.0, 0.1]
const float P_BOUNCE_STRENGTH = 0.78; // P PRT Interreflection [0.0, 1.5, 0.05]
const float P_SH_WINDOW = 0.86; // P SH Ringing Window [0.3, 1.0, 0.02]

// Analytic Hosek-Wilkie-inspired sky radiance (excluding the sharp sun disc when includeDisc=false)
vec3 evalHosekSky(vec3 dir, vec3 sunDir, float turbidity, bool includeDisc) {
  float cosTheta = max(dir.y, 0.008);
  float cosGamma = clamp(dot(dir, sunDir), -1.0, 1.0);
  float gamma = acos(cosGamma);
  float sunElev = clamp(sunDir.y, 0.02, 1.0);
  float tNorm = clamp((turbidity - 1.5) / 6.5, 0.0, 1.0);

  // Spectral zenith-to-horizon scattering shift with sun elevation and turbidity
  vec3 zenithDay = vec3(0.10, 0.28, 0.78);
  vec3 zenithLow = vec3(0.06, 0.11, 0.34);
  vec3 zenith = mix(zenithLow, zenithDay, pow(sunElev, 0.55));
  zenith = mix(zenith, vec3(0.28, 0.34, 0.44), tNorm * 0.45);

  vec3 horizDay = vec3(0.62, 0.76, 0.92);
  vec3 horizLow = vec3(1.18, 0.46, 0.14);
  vec3 horiz = mix(horizLow, horizDay, smoothstep(0.04, 0.48, sunElev));
  horiz = mix(horiz, vec3(0.92, 0.70, 0.48), tNorm * 0.35);

  // Hosek-Wilkie angular brightening & Mie circumsolar glow
  float horizonTerm = exp(-cosTheta * (3.2 - 1.2 * tNorm));
  float mieAureole = (0.35 + 1.15 * tNorm) * exp(-gamma * (2.6 - 0.9 * tNorm)) / (0.28 + cosTheta);
  float rayleigh = 0.75 + 0.25 * cosGamma * cosGamma;
  vec3 sunTint = mix(vec3(1.35, 0.44, 0.12), vec3(1.05, 0.92, 0.74), smoothstep(0.05, 0.45, sunElev));

  vec3 sky = mix(zenith, horiz, horizonTerm) * rayleigh + sunTint * mieAureole * 0.48;
  if (dir.y < 0.0) {
    // Ground bounce term below horizon for stable SH projection
    vec3 ground = mix(vec3(0.18, 0.12, 0.08), vec3(0.28, 0.24, 0.19), sunElev) * (0.45 + 0.55 * sunElev);
    sky = mix(horiz * 0.55, ground, clamp(-dir.y * 3.0, 0.0, 1.0));
  }
  if (includeDisc) {
    float disc = smoothstep(0.9982, 0.9994, cosGamma);
    sky += sunTint * disc * 18.0;
  }
  return max(sky, vec3(0.0));
}

// Project sun-excluded Hosek-Wilkie sky into 9 order-3 SH coefficients with anti-ringing window
void projectSkySH(vec3 sunDir, float turbidity, out vec3 sh[9]) {
  for (int i = 0; i < 9; i++) sh[i] = vec3(0.0);
  const int N = 24;
  float w0 = 4.0 * 3.1415926 / float(N);
  for (int i = 0; i < N; i++) {
    float fi = float(i) + 0.5;
    float y = 1.0 - 2.0 * (fi / float(N));
    float rad = sqrt(max(0.0, 1.0 - y * y));
    float phi = fi * 2.3999632;
    vec3 d = vec3(cos(phi) * rad, y, sin(phi) * rad);
    vec3 L = evalHosekSky(d, sunDir, turbidity, false) * w0;
    float b0 = 0.282095;
    float b1 = 0.488603;
    float b2 = 1.092548;
    float b3 = 0.315392;
    float b4 = 0.546274;
    sh[0] += L * b0;
    sh[1] += L * (b1 * d.y);
    sh[2] += L * (b1 * d.z);
    sh[3] += L * (b1 * d.x);
    sh[4] += L * (b2 * d.x * d.y);
    sh[5] += L * (b2 * d.y * d.z);
    sh[6] += L * (b3 * (3.0 * d.z * d.z - 1.0));
    sh[7] += L * (b2 * d.x * d.z);
    sh[8] += L * (b4 * (d.x * d.x - d.y * d.y));
  }
  // Hanning/Lanczos windowing to damp order-2 sunset ringing
  float w1 = mix(0.75, 1.0, P_SH_WINDOW);
  float w2 = mix(0.28, 0.88, P_SH_WINDOW);
  for (int i = 1; i <= 3; i++) sh[i] *= w1;
  for (int i = 4; i <= 8; i++) sh[i] *= w2;
}

// Evaluate order-3 SH irradiance along bent transfer direction with visibility cone sharpening
vec3 evalSHTransfer(vec3 sh[9], vec3 bentDir, float visFrac) {
  // Cosine lobe zonal harmonics scaled by aperture visibility
  float c0 = 3.1415926 * visFrac;
  float c1 = 2.0943951 * pow(visFrac, 0.72);
  float c2 = 0.7853982 * pow(visFrac, 0.55);
  vec3 d = normalize(bentDir);
  vec3 res = sh[0] * (0.282095 * c0);
  res += (sh[1] * d.y + sh[2] * d.z + sh[3] * d.x) * (0.488603 * c1);
  res += sh[4] * (1.092548 * d.x * d.y * c2);
  res += sh[5] * (1.092548 * d.y * d.z * c2);
  res += sh[6] * (0.315392 * (3.0 * d.z * d.z - 1.0) * c2);
  res += sh[7] * (1.092548 * d.x * d.z * c2);
  res += sh[8] * (0.546274 * (d.x * d.x - d.y * d.y) * c2);
  return max(res, vec3(0.0)) / 3.1415926;
}

float sdBox3(vec3 p, vec3 b) {
  vec3 q = abs(p) - b;
  return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

float sdCyl(vec3 p, float r, float h) {
  vec2 d = abs(vec2(length(p.xz), p.y)) - vec2(r, h);
  return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}

// Static limestone colonnade + alcove + sculpted sphere
float mapScene(vec3 p) {
  float floorP = p.y + 0.55;
  // Stepped temple plinth
  float plinth1 = sdBox3(p - vec3(0.0, -0.44, 0.0), vec3(1.55, 0.11, 1.15));
  float plinth2 = sdBox3(p - vec3(0.0, -0.32, -0.05), vec3(1.25, 0.07, 0.88));
  // Back wall with recessed arch alcove
  float backWall = sdBox3(p - vec3(0.0, 0.35, -0.82), vec3(1.35, 0.85, 0.22));
  float archCut = min(sdBox3(p - vec3(0.0, 0.12, -0.66), vec3(0.46, 0.42, 0.28)), length(p.xy - vec2(0.0, 0.54)) - 0.46);
  backWall = max(backWall, -archCut);
  // Entablature roof beam
  float roof = sdBox3(p - vec3(0.0, 1.08, -0.22), vec3(1.42, 0.14, 0.88));
  // Fluted pillars
  float cols = 1e5;
  for (int i = -1; i <= 1; i++) {
    if (i == 0) continue;
    float cx = float(i) * 0.92;
    vec3 cp = p - vec3(cx, 0.34, 0.36);
    float ang = atan(cp.z, cp.x);
    float flute = 0.012 * cos(ang * 12.0);
    cols = min(cols, sdCyl(cp, 0.165 + flute, 0.68));
    cols = min(cols, sdBox3(p - vec3(cx, 0.96, 0.36), vec3(0.22, 0.05, 0.22)));
  }
  // Center pedestal + stone sphere
  float ped = sdBox3(p - vec3(0.0, -0.10, 0.02), vec3(0.24, 0.18, 0.24));
  float sph = length(p - vec3(0.0, 0.34, 0.02)) - 0.28;
  float d = min(floorP, min(plinth1, plinth2));
  d = min(d, min(backWall, roof));
  d = min(d, min(cols, min(ped, sph)));
  return d;
}

vec3 calcNormal(vec3 p) {
  vec2 e = vec2(0.0025, 0.0);
  return normalize(vec3(
    mapScene(p + e.xyy) - mapScene(p - e.xyy),
    mapScene(p + e.yxy) - mapScene(p - e.yxy),
    mapScene(p + e.yyx) - mapScene(p - e.yyx)
  ));
}

float calcSoftShadow(vec3 ro, vec3 rd) {
  float res = 1.0;
  float t = 0.03;
  for (int i = 0; i < 22; i++) {
    float h = mapScene(ro + rd * t);
    res = min(res, 7.5 * h / t);
    t += clamp(h, 0.025, 0.22);
    if (res < 0.005 || t > 5.0) break;
  }
  return clamp(res, 0.0, 1.0);
}

// Simulates precomputed directional sky transfer (bent normal + sky aperture + bounce occlusion)
void evalBakedPRT(vec3 p, vec3 n, out vec3 bentN, out float visFrac, out vec3 bounceDir, out float bounceFrac) {
  bentN = n * 0.25;
  visFrac = 0.0;
  bounceDir = vec3(0.0);
  bounceFrac = 0.0;
  // 6 hemisphere probe directions approximating offline PRT ray-cast transfer
  vec3 up = abs(n.y) < 0.9 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);
  vec3 t1 = normalize(cross(up, n));
  vec3 t2 = cross(n, t1);
  for (int i = 0; i < 6; i++) {
    float a = float(i) * 1.0471975 + 0.35;
    vec3 d = normalize(n * 0.62 + (t1 * cos(a) + t2 * sin(a)) * 0.78);
    float occ = 1.0;
    float dist = 0.08;
    for (int s = 0; s < 4; s++) {
      float h = mapScene(p + d * dist);
      if (h < 0.025) { occ = 0.0; break; }
      dist += max(h, 0.14);
    }
    if (occ > 0.5) {
      bentN += d;
      visFrac += 1.0;
    } else {
      // Occluded rays gather 1-bounce interreflection from opposite sunlit stone
      vec3 hitP = p + d * dist;
      vec3 hitN = calcNormal(hitP);
      bounceDir += hitN;
      bounceFrac += 1.0;
    }
  }
  visFrac = clamp(visFrac / 6.0, 0.08, 1.0);
  bentN = normalize(bentN);
  bounceFrac = clamp(bounceFrac / 6.0, 0.0, 1.0);
  bounceDir = length(bounceDir) > 0.01 ? normalize(bounceDir) : vec3(0.0, 1.0, 0.0);
}

vec3 acesTone(vec3 x) {
  return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  bool usePRT = uv.x > 0.0;
  vec2 suv = vec2(usePRT ? (uv.x - 0.25) : (uv.x + 0.25), uv.y);

  // Animated time-of-day sun arc centered on P_SUN_ELEV
  float az = -0.85 + 0.72 * sin(iTime * 0.35);
  float elev = clamp(P_SUN_ELEV + 0.16 * cos(iTime * 0.35), 0.05, 1.25);
  vec3 sunDir = normalize(vec3(sin(az) * cos(elev), sin(elev), -cos(az) * cos(elev) * 0.65 + 0.35));
  vec3 sunCol = mix(vec3(2.8, 0.82, 0.22), vec3(2.6, 2.25, 1.80), smoothstep(0.06, 0.42, sunDir.y));

  vec3 sh[9];
  projectSkySH(sunDir, P_TURBIDITY, sh);

  vec3 ro = vec3(0.0, 0.42, 3.05);
  vec3 rd = normalize(vec3(suv.x * 1.15, suv.y - 0.04, -1.48));

  vec3 col = evalHosekSky(rd, sunDir, P_TURBIDITY, true);
  float t = 0.0;
  bool hit = false;
  for (int i = 0; i < 64; i++) {
    float d = mapScene(ro + rd * t);
    if (d < 0.0025) { hit = true; break; }
    t += d * 0.92;
    if (t > 12.0) break;
  }
  if (hit) {
    vec3 p = ro + rd * t;
    vec3 n = calcNormal(p);
    // Warm travertine stone albedo with subtle strata
    float strata = 0.5 + 0.5 * sin(p.y * 22.0 + sin(p.x * 7.0) * 1.4);
    vec3 albedo = mix(vec3(0.68, 0.62, 0.54), vec3(0.82, 0.77, 0.68), strata * 0.45);
    if (p.y < -0.53) albedo = vec3(0.46, 0.43, 0.38);

    float ndl = max(dot(n, sunDir), 0.0);
    float shad = ndl > 0.001 ? calcSoftShadow(p + n * 0.01, sunDir) : 0.0;
    vec3 direct = sunCol * ndl * shad * 0.95;

    vec3 indirect = vec3(0.0);
    if (!usePRT) {
      // Left: naive flat hemisphere ambient (misses sky chromatic directionality, self-shadowing, and interreflection)
      vec3 skyFlat = mix(vec3(0.20, 0.16, 0.14), vec3(0.28, 0.38, 0.58), 0.5 + 0.5 * n.y);
      indirect = skyFlat * (0.45 + 0.55 * sunDir.y);
    } else {
      // Right: Hosek-Wilkie SH sky dotted with PRT directional transfer + 1-bounce warm stone interreflection
      vec3 bentN; float visFrac; vec3 bDir; float bFrac;
      evalBakedPRT(p, n, bentN, visFrac, bDir, bFrac);
      vec3 skyIrr = evalSHTransfer(sh, bentN, visFrac);
      // Interreflection bounce carries warm sunlit stone energy into shaded colonnade alcoves
      vec3 bounceSky = evalSHTransfer(sh, bDir, 0.75) + sunCol * max(dot(bDir, sunDir), 0.0) * 0.38;
      vec3 bounce = bounceSky * vec3(0.76, 0.64, 0.50) * bFrac * P_BOUNCE_STRENGTH;
      indirect = skyIrr * 1.25 + bounce;
    }
    col = albedo * (direct + indirect);
    // Subtle atmospheric aerial perspective
    float fog = 1.0 - exp(-t * 0.045);
    col = mix(col, evalHosekSky(vec3(rd.x, max(rd.y, 0.05), rd.z), sunDir, P_TURBIDITY, false), fog * 0.45);
  }
  col = acesTone(col);
  if (abs(uv.x) < 0.003) col = vec3(0.95, 0.92, 0.86);
  fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}

Why they connect

Hosek-Wilkie Sky Model outputs an hdr-image of sky radiance for the current sun elevation and turbidity. Precomputed Radiance Transfer consumes that hdr-image: it is projected to low-order spherical harmonics (see Spherical-Harmonic Lighting (Irradiance Environment Maps)) and dotted with per-vertex or per-texel transfer vectors baked offline, which already contain self-shadowing and bounce light. The sun disc is far too sharp for low-order coefficients, so it is removed from the sky before projection and handled as a separate direct light with shadow maps.

How to build it

Bake order-3 or order-4 transfer vectors per vertex in a Blender or Python script by casting rays from each vertex. In three.js on WebGL2, evaluate the Hosek-Wilkie model on the CPU whenever the sun moves, render it into a small cube map without the sun, and project to SH. Upload the coefficients as uniforms and compute the dot product in the vertex shader. Add a directional light for the sun with cascaded shadows and a colour taken from the same model. Window the coefficients to reduce ringing near the horizon.

Field marks

  1. Low-order SH cannot represent sharp shadows, so the sun must stay a separate light.
  2. Only rigid, static geometry is supported.
  3. Ringing appears for strongly directional skies near sunset.
  4. The sky model is not valid below the horizon, so night needs another source.

Prior art

No prior art found on 2026-11-29.

Searches run (4)
  • arXiv: all:"precomputed radiance transfer" AND all:"sky model" → 0 results
  • GitHub repos: "precomputed radiance transfer" "sky model" → 0 results
  • arXiv: all:"precomputed radiance transfer" AND all:"time of day" → 0 results
  • GitHub repos: "precomputed radiance transfer" "time of day" → 0 results