Texel A field guide to 3D techniques

Depth Anything V2 × Screen-space Volumetric Light Scattering (God Rays)

Light shafts for any photo. Add sun shafts to a single photo or video frame in the browser, occluded by the real objects in the picture. Monocular depth supplies both the occluder mask and the per-pixel distance that sets how much haze sits in front.

Open in StudioReproduced352× · 10.9 ms

Depth Anything V2Capture · Interactivein: photosout: depthdepthScreen-space Volumetric Light Scattering (God Rays)Volume · Real-timein: depth, ldr-imageout: ldr-image

Verified proof

reproduced · 2026-12-01texel-lablicense MIT
Specimen render of Photo depth god rays (left: luminance-mask 2D shafts, right: monocular depth sky mask + depth-weighted haze)
Cross-proof specimen. Photo depth god rays (left: luminance-mask 2D shafts, right: monocular depth sky mask + depth-weighted haze). Verified WebGL2 proof · 2026-12-01

Split-screen WebGL2 proof comparing luminance-only 2D radial god rays on a single LDR forest-viaduct photograph (left, falsely emitting from bright sunlit foreground stonework and washing out close silhouettes) against monocular relative-depth guided volumetric light scattering that thresholds far sky depth cleanly and weights shaft haze by Beer-Lambert per-pixel distance (right).

Chromium Headless ANGLE/SwiftShader WebGL2 · 512x512 · rejects bright foreground false emitters and preserves crisp foreground silhouette contrast while scattering rich depth-weighted crepuscular shafts through midground arches

Show GLSL proof (168 lines)
const float P_SHAFT_DENSITY = 0.92; // P Ray March Density [0.3, 1.5, 0.02]
const float P_HAZE_DEPTH = 1.65; // P Depth Haze Weight [0.2, 3.5, 0.05]
const float P_SKY_THRESH = 0.86; // P Far Depth Threshold [0.65, 0.96, 0.01]
const float P_SUN_EXPOSURE = 1.15; // P Crepuscular Gain [0.2, 2.2, 0.05]

float hash11(float p) {
  p = fract(p * 0.1031);
  p *= p + 33.33;
  return fract(p * (p + p));
}

float vnoise1(float x) {
  float i = floor(x);
  float f = fract(x);
  float u = f * f * (3.0 - 2.0 * f);
  return mix(hash11(i), hash11(i + 1.0), u);
}

float fbm1(float x) {
  float s = 0.0;
  float a = 0.5;
  for (int i = 0; i < 4; i++) {
    s += a * vnoise1(x);
    x = x * 2.07 + 3.1;
    a *= 0.5;
  }
  return s;
}

// Evaluates a synthetic LDR photograph + its monocular relative depth (0 = near foreground, 1 = far sky)
void evalPhotoAndDepth(vec2 p, vec2 sunPos, out vec3 photoCol, out float relDepth) {
  // Sky gradient + warm sun glow
  float skyT = clamp((p.y + 0.35) / 0.85, 0.0, 1.0);
  vec3 skyCol = mix(vec3(0.95, 0.56, 0.24), vec3(0.14, 0.26, 0.52), pow(skyT, 0.62));
  float sunD = length(p - sunPos);
  skyCol += vec3(1.4, 0.92, 0.45) * exp(-sunD * 5.2) * 0.85;
  skyCol += vec3(1.8, 1.45, 0.95) * smoothstep(0.085, 0.028, sunD);
  // Thin high clouds
  float cld = smoothstep(0.42, 0.78, fbm1(p.x * 4.5 + p.y * 6.0));
  skyCol = mix(skyCol, vec3(0.96, 0.72, 0.46), cld * 0.25 * smoothstep(0.0, 0.45, p.y));

  photoCol = skyCol;
  relDepth = 1.0;

  // Layer 1: Distant mountain ridge (relDepth = 0.82)
  float mtnH = -0.06 + 0.22 * fbm1(p.x * 3.8 + 1.2) - 0.14 * abs(p.x);
  if (p.y < mtnH) {
    photoCol = mix(vec3(0.19, 0.20, 0.28), vec3(0.32, 0.27, 0.31), clamp((p.y + 0.3) * 1.6, 0.0, 1.0));
    relDepth = 0.82;
  }

  // Layer 2: Midground ruined stone viaduct with open arches + misty pines (relDepth = 0.52)
  float viaductTop = 0.21;
  float viaductDeck = 0.14;
  float span = mod(p.x + 0.55, 0.22) - 0.11;
  float archRad = 0.072;
  bool inArchHole = (p.y < viaductDeck) && ((p.y < 0.03 && abs(span) < archRad) || (length(vec2(span, p.y - 0.03)) < archRad));
  bool inViaduct = (p.y < viaductTop && p.y > -0.45) && !inArchHole;
  // Midground pine spires rising between piers
  float pineBase = -0.14 + 0.19 * pow(fbm1(p.x * 16.0 + 4.3), 1.35);
  float pineTip = pineBase + 0.09 * smoothstep(0.4, 0.95, sin(p.x * 54.0) * 0.5 + 0.5);
  if (p.y < pineTip) {
    photoCol = vec3(0.08, 0.11, 0.10) * (0.75 + 0.5 * fbm1(p.x * 40.0 + p.y * 25.0));
    relDepth = 0.56;
  }
  if (inViaduct) {
    float stone = 0.72 + 0.28 * fbm1(p.x * 35.0 + floor(p.y * 28.0) * 1.7);
    // Warm rim catch on right side of viaduct stonework
    float rim = smoothstep(0.055, 0.075, abs(span)) * 0.45;
    photoCol = vec3(0.24, 0.21, 0.20) * stone + vec3(0.42, 0.26, 0.12) * rim;
    relDepth = 0.50;
  }

  // Layer 3: Close foreground framed stone pier & overhanging bright sunlit limestone wall (relDepth = 0.16)
  // Deliberately include a bright sunlit stone face on the foreground pier so luminance-only god rays falsely bloom from it!
  bool fgLeftPier = (p.x < -0.175) && (p.y < 0.45 - smoothstep(-0.26, -0.175, p.x) * 0.12);
  bool fgRightPier = (p.x > 0.195) && (p.y < 0.45 - smoothstep(0.28, 0.195, p.x) * 0.10);
  bool fgLintel = (p.y > 0.36 + 0.04 * cos(p.x * 6.0));
  bool fgGround = (p.y < -0.30 + 0.03 * sin(p.x * 9.0));
  // Foreground overhanging pine bough silhouette
  float branch = 0.26 + 0.06 * sin(p.x * 14.0) + 0.03 * fbm1(p.x * 52.0);
  bool fgBough = (p.x < -0.04) && (p.y > branch) && (p.y < branch + 0.065 * smoothstep(-0.04, -0.22, p.x));
  if (fgLeftPier || fgRightPier || fgLintel || fgGround || fgBough) {
    float block = 0.78 + 0.22 * fbm1(p.x * 28.0 + floor(p.y * 20.0) * 2.3);
    // Bright sunlit chalky stone band on the close foreground pier (traps naive luminance mask!)
    float sunlitWall = smoothstep(0.08, 0.22, p.y) * smoothstep(0.32, 0.18, p.y) * (fgRightPier ? 1.0 : 0.65);
    vec3 fgStone = vec3(0.11, 0.10, 0.11) * block + vec3(0.76, 0.68, 0.56) * sunlitWall;
    if (fgBough) fgStone = vec3(0.04, 0.05, 0.04);
    if (fgGround) fgStone = vec3(0.09, 0.08, 0.07) * block;
    photoCol = fgStone;
    relDepth = fgBough ? 0.10 : 0.16;
  }
}

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 useDepth = uv.x > 0.0;
  vec2 p = vec2(useDepth ? (uv.x - 0.25) : (uv.x + 0.25), uv.y);
  p.x *= 1.55;

  // Animated sun drifting gently behind the viaduct arches
  vec2 sunPos = vec2(0.02 + 0.13 * sin(iTime * 0.45), 0.11 + 0.05 * cos(iTime * 0.35));

  vec3 baseCol;
  float depth0;
  evalPhotoAndDepth(p, sunPos, baseCol, depth0);

  // Radial volumetric light scattering march toward the sun
  const int SAMPLES = 28;
  vec2 delta = (p - sunPos) * (P_SHAFT_DENSITY / float(SAMPLES));
  vec2 cur = p;
  float illumDecay = 1.0;
  vec3 shafts = vec3(0.0);
  for (int i = 0; i < SAMPLES; i++) {
    cur -= delta;
    vec3 sCol;
    float sDepth;
    evalPhotoAndDepth(cur, sunPos, sCol, sDepth);
    float emitMask = 0.0;
    if (!useDepth) {
      // Left: naive 2D luminance threshold (bright sunlit foreground stone falsely passes as sky!)
      float lum = dot(sCol, vec3(0.2126, 0.7152, 0.0722));
      emitMask = smoothstep(0.48, 0.72, lum);
    } else {
      // Right: Depth Anything V2 relative-depth far threshold cleanly isolates true sky behind arches
      emitMask = smoothstep(P_SKY_THRESH - 0.04, P_SKY_THRESH + 0.04, sDepth);
    }
    vec3 sampleRad = mix(vec3(1.0, 0.64, 0.28), sCol, 0.45) * emitMask;
    shafts += sampleRad * illumDecay * (1.15 / float(SAMPLES));
    illumDecay *= 0.945;
  }

  vec3 col = baseCol;
  if (!useDepth) {
    // Left: flat additive composite washes over close foreground arches indiscriminately
    col += shafts * P_SUN_EXPOSURE * 1.15;
  } else {
    // Right: second use of monocular depth — Beer-Lambert distance-weighted haze puts shafts BEHIND foreground stone
    float depthHaze = 1.0 - exp(-pow(depth0, 1.35) * P_HAZE_DEPTH * 1.35);
    // Subtle edge wrap only right on backlit silhouette rim
    float rimWrap = 0.09 + 0.91 * depthHaze;
    col += shafts * P_SUN_EXPOSURE * 1.45 * rimWrap;
    // Slight aerial perspective lift on distant planes only
    col = mix(col, vec3(0.95, 0.66, 0.36), smoothstep(0.45, 0.92, depth0) * 0.12 * P_SUN_EXPOSURE);
  }

  // Bottom strip: visualize the extracted sky mask (left: noisy luminance mask, right: clean monocular depth map)
  if (uv.y < -0.405) {
    if (!useDepth) {
      float lum = dot(baseCol, vec3(0.2126, 0.7152, 0.0722));
      col = vec3(smoothstep(0.48, 0.72, lum));
    } else {
      // Inferno-like plasma ramp for monocular relative depth
      vec3 nearC = vec3(0.08, 0.04, 0.22);
      vec3 midC = vec3(0.76, 0.24, 0.40);
      vec3 farC = vec3(0.99, 0.88, 0.44);
      col = depth0 < 0.5 ? mix(nearC, midC, depth0 * 2.0) : mix(midC, farC, (depth0 - 0.5) * 2.0);
    }
  }
  col = acesTone(col);
  if (abs(uv.y + 0.405) < 0.0025) col = vec3(0.88, 0.85, 0.80);
  if (abs(uv.x) < 0.003) col = vec3(0.96, 0.94, 0.88);
  fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}

Why they connect

Depth Anything V2 outputs relative depth from one image. Screen-space Volumetric Light Scattering (God Rays) takes depth and an ldr-image: the far end of the depth range (sky and distant background) becomes the bright mask, near pixels become occluders, and the radial blur toward the chosen sun position accumulates shafts. Depth is used a second time to weight the shafts, so near foreground objects receive little haze and distant ones more, which a mask-only version cannot do.

How to build it

Run the small Depth Anything V2 model with transformers.js on WebGPU, once per image or every few frames for video. Normalise inverse depth to 0–1 and choose the far threshold from its histogram, so sky detection adapts per image. Let the user drag the sun position; compute the shaft pass at half resolution with a few dozen samples along the sun direction, then upsample with a depth-aware filter. Composite as additive light scaled by distance-based haze. For video, smooth depth and threshold over time to hide flicker. Everything runs client-side, so photos never leave the device.

Field marks

  1. Relative depth has no absolute scale, so the far threshold can misread bright walls as sky.
  2. Depth flicker across video frames makes shafts shimmer.
  3. Fine foliage against the sky gives blurry occluder edges.
  4. Shafts only look plausible when the sun is in or near the frame.

Prior art

No prior art found on 2026-11-29.

Searches run (4)
  • arXiv: all:"monocular depth" AND all:"god rays" → 0 results
  • GitHub repos: "monocular depth" "god rays" → 0 results
  • arXiv: all:"depth estimation" AND all:"light shafts" AND all:"photo" → 0 results
  • GitHub repos: "depth estimation" "light shafts" photo → 0 results