Skip to content

feat(shadow): baked + dynamic per-receiver shadow projection#31

Merged
apresmoi merged 31 commits into
mainfrom
feat/baked-shadows
May 24, 2026
Merged

feat(shadow): baked + dynamic per-receiver shadow projection#31
apresmoi merged 31 commits into
mainfrom
feat/baked-shadows

Conversation

@apresmoi
Copy link
Copy Markdown
Collaborator

Summary

  • Adds per-receiver-face shadow projection: every coplanar face group on a receiver mesh gets its own SVG, and every caster's projected silhouette is 3D-clipped + Sutherland-Hodgman-clipped to that face's outline. Validated against a Möller-Trumbore raytracer (0 false negatives on front-facing receivers).
  • Extends shadow to baked mode: CPU-bakes the projection into each cast leaf's inline matrix3d, drops back-facing polys from the DOM (no opacity gating), and re-emits leaves on light change without redrawing the atlas. Mirrored across polycss, react, and vue.
  • Stack of per-receiver-face shadow performance optimisations on top of the new projection: mount-once SVGs with display:none toggling, per-caster AABB pre-cull, lowered path coord precision, dropped path stroke, and cached caster world-vertices/AABB across light updates.

Performance

Real-shadow drag bench (apple + electric-pole, ?norayt), versus the pre-perf-stack baseline of the same projection algorithm:

Metric Before After Δ
frame_time_p50_ms 16.3 16.0 -2%
frame_time_p95_ms 33.3 25.6 -23%
frame_time_p99_ms 39.9 33.5 -16%
script ms/frame 29.6 12.0 -60%
compositorMain ms/frame 14.9 7.0 -53%
paint / prePaint / layout / style -65% to -79%

Visual baseline preserved: 12 of 16 multi-angle / multi-light screenshots are byte-identical to the projection-only baseline; the rest differ by ≤340 bytes of sub-pixel AA in PNG entropy.

Test plan

  • pnpm test — 563 tests across all four packages, all green
  • pnpm build — packages + website built locally; tsup DTS gate passes
  • Visual: apple-and-pole drag matches baseline across 4 light directions × 4 camera angles (bench/results/baselines/shadows/ vs bench/results/optI/)
  • Raytrace cross-check (bench/real-shadow.html without ?norayt): TP/FP/FN diff stays within the documented per-tri-clip envelope
  • Manual: website lighting + shadow controls still react live in both baked and dynamic modes

apresmoi added 30 commits May 23, 2026 02:03
Replaces the per-face per-caster-hull approach with per-triangle
projection that 3D-clips each caster tri against the receiver plane
half-space before projecting to the face's 2D plane. Fixes the case
where tris straddle the plane (top of a tall pole occluding an
apple): the previous algorithm dropped below-plane vertices and the
hull collapsed to a line, missing the shadow entirely.

bench/real-shadow.html runs a Möller-Trumbore raytracer alongside the
fast algorithm as ground truth, with a per-face yellow→blue coverage
overlay and TP/FP/FN diff readout. Source of truth for future
shadow-algorithm work.
# Conflicts:
#	packages/polycss/src/api/createPolyScene.ts
Stack of four scene-shadow optimisations:

- Mount each per-face shadow SVG once and toggle display:none instead of recreating per frame; eliminates ~150 createElement/removeChild + ~600 setAttribute mutations per drag tick

- Pre-build per-caster-polygon items (world-space verts + 3D AABB) once per frame; project the 8 AABB corners onto each face plane and bbox-cull against the face outline before the per-tri Sutherland-Hodgman loop

- Round path coordinates to 1 decimal (sub-pixel for receiver-plane CSS px); ~30% smaller path strings to parse/raster

- Drop the per-path stroke; the implicit AA seam bleed was not visually load-bearing in any tested scene

Real-shadow drag bench (apple + electric pole, ?norayt):

- script ms/frame: 29.6 -> 12.6 (-57%)

- frame_time_p95_ms: 33.3 -> 25.0 (-25%)

- frame_time_p99_ms: 39.9 -> 33.7 (-15%)

- compositorMain ms/frame: 14.9 -> 7.3 (-51%)

- paint/prePaint/layout/style all -64..-79%
Per-caster cached per-polygon data (world-space vertices and 3D AABB) is invariant under light direction. Previously rebuilt every emitSceneReceiverShadows call (once per frame during drag). Now cached on the MeshEntry and invalidated only on geometry/position change.

Real-shadow drag bench: script -5%, raster -4%, compositorMain -4%, gpuViz -2%.
@apresmoi apresmoi merged commit 4a64b5d into main May 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant