Skip to content

feat(core): fit shadow maps to DirectionalLight _shadowBounds (5/5) - #10913

Open
charlieforward9 wants to merge 6 commits into
fix/shadow-module-keep-bindingsfrom
feat/shadow-bounds
Open

charlieforward9 wants to merge 6 commits into
fix/shadow-module-keep-bindingsfrom
feat/shadow-bounds

Conversation

@charlieforward9

@charlieforward9 charlieforward9 commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Part 5 of 5 of the shadow fixes, follows #10903.

A shadow light fits its shadow map to everything in view. When the view spans kilometers (zoomed out, or steeply pitched), small casters such as trees get a few shadow-map texels each and the light frustum is kilometers deep. The shadow test uses a fixed threshold on normalized depth (smoothstep(0.001, 0.01, ...)), so shadows of objects a few meters tall fade out and the rest are blocky. #10893 fixes the fit above ~70° pitch but can't help here: the view itself is that large. Apps often know where their casters are, so this lets them say so:

new DirectionalLight({
  direction: [-1, -1, -2],
  _shadow: true,
  // Where the casters are, up to their tallest height
  _shadowBounds: [[minLng, minLat, 0], [maxLng, maxLat, 30]]
});

Change List

  • DirectionalLight: experimental _shadowBounds, [[minX, minY, minZ], [maxX, maxY, maxZ]] in world coordinates
    • The map is fitted to the light-space box of the view intersected with that of the bounds; with no overlap, the view fit is kept
    • Geometry just outside the bounds may still cast and receive shadows; geometry beyond the light-aligned box does not
  • shadow shader: no shadow outside the light's frustum, instead of sampling the clamped edge
    • Without bounds the frustum contains the whole view, so nothing visible changes
  • Export ShadowBounds type
  • Unit tests
  • Render test shadow-effect-bounds: 5 m columns at zoom 17, pitch 70, crisp with bounds
  • Documentation

A shadow light fits its shadow map to everything in view. Zoomed out or
steeply pitched, that spans kilometers: small casters get a few blocky
texels each, and the deep light frustum pushes their depth difference
below the shadow test's threshold, so their shadows fade out.

DirectionalLight accepts an experimental _shadowBounds box. Its shadow
map is fitted to the part of the bounds in view, falling back to the
view fit when they don't overlap. The shadow shader now returns no
shadow outside the light's frustum instead of sampling the clamped map
edge.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@greptile-apps

greptile-apps Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects shadow rendering in the bounded directional-light workflow.

The PR should not merge until the existing globe bounds issue is addressed.

Findings

  1. P1 Shadows disappear inside globe bounds ▶

Summary

Adds experimental _shadowBounds to DirectionalLight to tighten shadow maps around known casters.

  • Directional lights can focus shadow maps on bounds around their casters.

Reviews (6) · Last reviewed commit: "Merge branch 'fix/shadow-module-keep-bin..." · Reviewed by Greptile

@coveralls

coveralls commented Oct 23, 2026 •

Copy link
Copy Markdown

Coverage Status

Coverage is 84.184% — feat/shadow-bounds into fix/shadow-module-keep-bindings. No base build found for fix/shadow-module-keep-bindings.

charlieforward9 and others added 3 commits October 23, 2026 14:25
The shadow map covers a light-aligned box around the bounds, so geometry
just outside them can still cast and receive shadows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	test/modules/core/shaderlib/shadow/shadow.spec.ts
#	test/render/test-cases/effects.spec.ts
@charlieforward9 charlieforward9 changed the title feat(core): fit shadow maps to DirectionalLight _shadowBounds feat(core): fit shadow maps to DirectionalLight _shadowBounds (5/5) Oct 26, 2026
@charlieforward9
charlieforward9 changed the base branch from master to fix/shadow-module-keep-bindings October 26, 2026 04:00
@greptile-apps

greptile-apps Bot commented Oct 26, 2026

Copy link
Copy Markdown

Comments Outside Diff

These findings could not be posted inline.

  • P1 Southern globe shadows disappear modules/core/src/shaderlib/shadow/shadow.ts:173 ▶

    getFrustumCorners clips every geospatial view to z >= 0, but in GlobeViewport, z measures north–south position, not height above ground. When a globe view crosses the equator, this removes the southern part of its visible volume. With a light aligned to the polar axis, southern objects fall outside the fitted shadow map and lose their shadows, even without _shadowBounds.

    Apply the ground-plane clip only to views whose ground is actually z = 0.

Comment on lines +239 to +242
for (const x of [min[0], max[0]]) {
for (const y of [min[1], max[1]]) {
for (const z of [min[2] ?? 0, max[2] ?? 0]) {
corners.push(viewport.projectPosition([x, y, z]));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Shadows disappear inside globe bounds

getBoundsCorners projects only the eight corners of _shadowBounds, but a globe's curved surface can extend beyond those projected points. For example, bounds [[-20, 30, 0], [20, 31, 100]] extend farther along negative common-space Y at longitude 0 than at either longitude edge. With light direction [0, 0, -1] and a view covering that area, the fitted box excludes the middle of the bounds, so objects there lose their shadows.

Include the curved surface's extrema when fitting globe bounds, or explicitly limit this option to supported projections.

@athomct athomct left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR targets the branch of #10903, which is closed and replaced by #11109. Please rebase this PR on master, or on #11109, and change the base to match.

I agree with the greptile comment on shadow.ts:242. In GlobeView, the eight projected corners do not include the curved surface between them. Please add points on the surface between the corners, or document that _shadowBounds is for planar views only.

The greptile comment on getFrustumCorners (z >= 0 in GlobeView) is from #10893, not from this PR. The shadow loss in the southern hemisphere is already on master: casters outside the fitted box are not in the shadow map, so those receivers get no correct shadow there either. The new early return only removes the clamp-to-edge streaks. You do not need to handle this in this PR.

Edited after a second check: the southern-hemisphere shadow loss is already on master.

const corners: number[][] = [];
for (const x of [min[0], max[0]]) {
for (const y of [min[1], max[1]]) {
for (const z of [min[2] ?? 0, max[2] ?? 0]) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?? 0 accepts 2D bounds such as [[minLng, minLat], [maxLng, maxLat]]. With the default direction [0, 0, -1], all eight corners have the same light depth. near then equals far, so clipLightBox returns the view box and the bounds do nothing, with no warning. With a light at an angle, casters taller than the flat box fall outside the near plane and lose their shadows. Please require three values in ShadowBounds, or give flat bounds a minimum height.

let idCount = 0;

/** Axis-aligned box in world coordinates: `[[minX, minY, minZ], [maxX, maxY, maxZ]]` */
export type ShadowBounds = [number[], number[]];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please type this as [[number, number, number], [number, number, number]]. Then the type rejects flat 2D bounds.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants