Repository navigation
Viewer perf: cached sun shadows, lit-only outside lighting, static merge for baked scenes - #1224
Conversation
…it sources only - The sun's 4096 shadow map re-rendered every frame, a full extra pass over every caster. It now renders only when its placement, frustum or settings change or a caster moves, appears, vanishes or swaps geometry (a cheap per-frame signature over a caster list refreshed with the bounds). Orbiting and walking reuse it. - The outside spill and lantern loops ran over every window and exterior lamp for every pixel, day and night. They now loop over compact lists of the windows whose room has a lamp on and the exterior lamps that are on (rewritten on lamp and door changes), and indoor pixels skip them. Pixel-identical. Baked GLB walkthrough, M5 Max: 22 → 50 fps (capped) on a 4.3k-draw house, 18 → 32 fps on a 72-window condo at night; daytime GPU on the condo 85 → 40 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ewer's batching) A big baked house is ~4,000 meshes, each drawn once per pass; on WebGPU every draw costs CPU to encode, so the overview spent ~60 ms a frame just submitting. Static meshes now merge per level, ~24 m cell, material and surface role into one mesh under their level (level visibility, explode and solo still apply); a moving part's meshes (a sash, a door leaf, a shutter) merge under that part and swing with it, glass included. The originals stay on the batched layer: never drawn or shadowed, still hit by pointer, reticle and room rays and the walkthrough collider, still outlined. Interactive items, loose glass, ceilings and roofs, the site, zones and plugin kinds stay individual; batches follow monochrome by role, and the merge rebuilds if a merged mesh is shown or hidden on its own. ?merge=0 turns it off. M5 Max, baked GLBs: 72-window condo overview 14 → 42 fps (8.6k → 1.8k draws); 4.3k-mesh house overview 14 → 32 fps. Renders match the unmerged scene (mean pixel difference ~1/255); the walkthrough collider is identical. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit bf8111e. Configure here.
| mesh.geometry.id * 0.019 + | ||
| 0.5) | ||
| } | ||
| return h |
There was a problem hiding this comment.
Shadow cache ignores layer hiding
Medium Severity
Cached sun shadows only invalidate from ancestor visible, world matrices, and geometry.id. Isolation and other hides go through layers (hideFromScene), so those casters still look shown and needsUpdate never flips. Isolated objects keep their shadows until something else happens to change the signature. New casters are also invisible to the hash until the 0.4s collect runs.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit bf8111e. Configure here.
| } | ||
| if (shown) h += (i % 97) + 1 | ||
| } | ||
| return h |
There was a problem hiding this comment.
Merge misses layer-based hides
Medium Severity
visibilitySignature only walks .visible up to the level, but isolation (and the comment on the merge effect) hide via hideFromScene layers. A merged original stays on BATCHED_LAYER and is never drawn, while its batch is not rebuilt, so an isolated merged object disappears or the batch keeps drawing geometry that should be gone.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit bf8111e. Configure here.


What does this PR do?
Ports the two big performance optimizations from the daylight look's reference renderer that #1217 didn't bring over, and trims the outside-lighting shader. Baked GLB scenes (viewer, walkthrough) were slow on big houses even on fast machines.
1. Cached sun shadows. The sun's shadow map (4096 px on High) re-rendered every frame, a full extra pass over every caster, moon included at night. It now re-renders only when something it depends on changes: the light's placement, frustum or settings, or a caster that moves, appears, vanishes or swaps geometry. That covers editing, opening doors, exploding or toggling levels, and time of day. It's tracked with a cheap per-frame signature, so no system has to remember to invalidate. Orbiting and walking reuse the map.
2. Outside lighting from lit sources only. The night window-spill and lantern loops ran over every window and exterior lamp, for every pixel, day and night. They now loop over compact lists of the windows whose room has a lamp on and the exterior lamps that are on, rewritten on lamp and door changes. Indoor pixels skip them. The output is pixel-identical.
3. Static merge for baked artifacts (
lib/glb-merge.ts):BATCHED_LAYERviahideFromScene(…, 'batched'): never drawn or shadowed, but still hit by pointer, reticle and room rays and by the walkthrough collider, and still outlined.?merge=0turns it off for comparison.Results
Median, M5 Max, headless, baked GLBs of two production projects:
The condo overview goes from ~8,600 to ~1,800 draws per frame. Merged and unmerged renders match (mean pixel difference ~1/255, day and night), monochrome and exploded levels look the same, the walkthrough collider is identical, and shadows still update on a time-of-day change.
How to test
?merge=0; add?perffor the overlay.Checklist
bun devbun checkto verify)mainbranch🤖 Generated with Claude Code
Note
Medium Risk
Broad renderer changes affect shadow invalidation, daylight shaders, and walkthrough collisions, so any missed edge case could yield stale lighting or interaction bugs despite targeted tests.
Overview
Performs major baked-GLB optimizations by introducing
mergeBakedStaticbatched meshes (toggleable via?merge=0) with lifecycle rebuilding when interactives or visibility signatures change, refreshes monochrome styling on merged batches, fixes outline proxies for batched geometry, and extends raycast/collider paths (including walkthrough BVHs) to respect the new layers.Caches daylight sun shadow maps by disabling
autoUpdate, periodically hashing casters' transforms/visibility against light frustum changes, and falling back to the old behavior for non-daylight looks.Optimizes nighttime exterior lighting shaders by recording active portal and exterior-lamp indices in room data textures, looping only lit contributors, skipping interior fragments, and adding early distance checks plus tests for the merge logic.
Risk: Performance-focused refactor touches rendering, collisions, and shading simultaneously; subtle mismatches between batch visibility, shadow invalidation, or active portal lists could cause stale visuals or physics edges, though bun tests cover merge expectations.
Reviewed by Cursor Bugbot for commit bf8111e. Bugbot is set up for automated code reviews on this repo. Configure here.