Skip to content

feat(render3d): add point and spot light shadow maps - #726

Merged
houko merged 3 commits into
mainfrom
feat/render3d-local-shadows-255
Nov 24, 2026
Merged

houko merged 3 commits into
mainfrom
feat/render3d-local-shadows-255

Conversation

@houko

@houko houko commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Shadows for the first spot light and the first point light of a LightRig in packages/game/render3d.

  • Spot light shadows (SpotShadowMap, SpotShadowConfig): a depth-only FormatDepth32Float perspective map updated from the rig's SpotLight (fovY = 2 * OuterConeAngle, far plane = Range, or Far when Range is 0), with hard and 3×3 PCF filtering, depth and normal bias, and 256-byte dynamic-offset caster slots.
  • Point light shadows (PointShadowMap, PointShadowConfig): the six 90° cube faces packed into one 3×2 depth atlas, updated from the rig's PointLight, with per-face viewport/scissor isolation, analytic perspective depth in the shader, and per-tile UV clamping so PCF never bleeds across tiles. Each face owns its caster slots, so faces can be refreshed in any order or skipped.
  • Lit PBR integration: LitPBRSpotShadowWGSL / LitPBRPointShadowWGSL (NewLitPBRSpotShadowPipeline, NewLitPBRPointShadowPipeline, or LitPBRPipelineOptions.SpotShadowLayout / PointShadowLayout) are LitPBRWGSL with the map at LocalShadowGroup (= ShadowGroup, 3): per-draw model matrix and material from PBRDrawUniform, and the shadowed light shaded exactly like the rest of the rig. shade_pbr_visibility in LightWGSL and ShadePointVisibility scale directional light 0, point light 0 and spot light 0.
  • Custom shaders: SpotShadowWGSL / PointShadowWGSL expose only sample_spot_shadow / sample_point_shadow; SpotShadowWGSLForGroup / PointShadowWGSLForGroup move the bindings so a shader can bind both (tested at groups 3 and 2 next to the camera and light rig).
  • Casters and limits: DrawCaster(pass, mesh, model) bool like DirectionalShadowMap, returning false when the pass or face is full or Update has not run; MaxCasters capped at MaxLocalShadowCasters and checked against the device's MaxBufferSize.
  • CPU references and lifecycle: EvaluateSpotShadow, EvaluatePointShadow, PointShadowSelectFace, zero-allocation uniform encoding, Retained device-loss restoration, scope disposal, and golden renders (local_shadow_spot.png, local_shadow_point.png).
  • Docs: docs/en|zh/render3d.md, the package overview in doc.go, changelog.d/render3d-local-shadows-255.md, and the design note.

Review follow-up

Rebased onto main, which added LightRig spot lights, the directional shadow at group 3, and new render3d doc sections. The first version used its own light types and lighting (no 1/π diffuse term, a different attenuation clamp, Range required), ready-made pipelines without a model matrix and with a constant albedo, and @group(1), which collides with LightGroup. All of that now goes through the rig and the shared PBR path, as described above.

Verification

  • gofmt clean
  • go tool nsgo test ./render3d (325 passed: GPU-vs-CPU pixel checks against ShadePointVisibility, goldens, full and NoGPUCompute profiles, device-loss restore, caster slot limits, spot+point composition)
  • go tool nsgo check -target linux,web ./render3d
  • go tool nsgo test . (3372 passed)
  • go run ./tools/changelog check

Screenshots

Golden renders (128×96, software renderer):

  • packages/game/render3d/testdata/golden/local_shadow_spot.png
  • packages/game/render3d/testdata/golden/local_shadow_point.png

Closes #255

@github-actions github-actions Bot added size/XL documentation Improvements or additions to documentation pkg/render3d area/agents enhancement New feature or request conflict Conflicts with the target branch; resolve before merging labels Nov 24, 2026
…gh the PBR path

The first version of the local shadows carried its own light types and lighting model, which disagreed with LightRig (no 1/pi in the diffuse term, a different near-field clamp, Range required), its ready-made pipelines ignored per-draw transforms and used a constant albedo, and its WGSL hard-coded @group(1), which collides with LightGroup.

SpotShadowMap and PointShadowMap now update from the rig's SpotLight and PointLight (far plane = Range, or the config's Far when Range is 0). The WGSL only samples visibility, at LocalShadowGroup (= ShadowGroup, 3), with SpotShadowWGSLForGroup and PointShadowWGSLForGroup to move it. LightWGSL gains shade_pbr_visibility, which scales directional light 0, point light 0 and spot light 0, and ShadePointVisibility mirrors it. LitPBRSpotShadowWGSL and LitPBRPointShadowWGSL (NewLitPBRSpotShadowPipeline, NewLitPBRPointShadowPipeline, LitPBRPipelineOptions.SpotShadowLayout and PointShadowLayout) are LitPBRWGSL with the map bound.

Casters follow DirectionalShadowMap: BeginShadowPass(dev, target) and BeginFacePass(dev, target, face), DrawCaster returns false when the pass or face is full or Update has not run. Each point face owns its slots, so partial or reordered face refreshes never exhaust the buffer. MaxCasters is capped at MaxLocalShadowCasters and checked against MaxBufferSize. Doc comments are unwrapped.
Rewrite the render3d.md section in both languages for the LightRig-based API, list the new group 3 options next to skinning, add the package overview paragraph, and update the changelog fragment and the design note.
@houko
houko force-pushed the feat/render3d-local-shadows-255 branch from 17ec932 to c34cb7a Compare November 24, 2026 18:09
@houko
houko merged commit 705bf8c into main Nov 24, 2026
22 of 36 checks passed
@houko
houko deleted the feat/render3d-local-shadows-255 branch November 24, 2026 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agents conflict Conflicts with the target branch; resolve before merging documentation Improvements or additions to documentation enhancement New feature or request pkg/render3d size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3D light: shadows from point and spot lights

1 participant