Skip to content

Viewer perf: cached sun shadows, lit-only outside lighting, static merge for baked scenes - #1224

Merged
wass08 merged 2 commits into
mainfrom
wawa/render-perf
Nov 6, 2026
Merged

wass08 merged 2 commits into
mainfrom
wawa/render-perf

Conversation

@wass08

@wass08 wass08 commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

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):

  • A big baked house is ~4,000 meshes drawn once per pass, and on WebGPU each draw costs CPU to encode.
  • Static meshes merge per level, ~24 m cell, material and surface role into one mesh under their level, so level visibility, explode and solo still apply.
  • A moving part's meshes (sash, door leaf, shutter) merge under that part and swing with it.
  • The originals stay on the existing BATCHED_LAYER via hideFromScene(…, 'batched'): never drawn or shadowed, but still hit by pointer, reticle and room rays and by the walkthrough collider, and still outlined.
  • Interactive items, loose glass, ceilings and roofs, the site, zones and plugin kinds stay individual.
  • Batches follow monochrome by role. The merge rebuilds if a merged mesh is shown or hidden on its own.
  • ?merge=0 turns it off for comparison.

Results

Median, M5 Max, headless, baked GLBs of two production projects:

Overview Walkthrough
House, 4.3k meshes, midday 10 → 32 fps 22 → 50 fps (cap)
Condo, 72 windows, night 10 → 42 fps 18 → 50 fps (cap)

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

  1. Open a big baked project in the viewer; compare with ?merge=0; add ?perf for the overlay.
  2. In walkthrough, open a door or window: it swings (merged under its moving part).
  3. Hover or select furniture: the outline still draws.
  4. Change time of day: shadows follow.

Checklist

  • I've tested this locally with bun dev
  • My code follows the existing code style (run bun check to verify)
  • I've updated relevant documentation (if applicable)
  • This PR targets the main branch

🤖 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 mergeBakedStatic batched 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.

wass08 and others added 2 commits November 6, 2026 08:26
…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]>

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

❌ 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit bf8111e. Configure here.

}
if (shown) h += (i % 97) + 1
}
return h

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit bf8111e. Configure here.

@wass08
wass08 merged commit 0602f9e into main Nov 6, 2026
4 checks passed
@wass08
wass08 deleted the wawa/render-perf branch November 6, 2026 14:24
@Aymericr Aymericr mentioned this pull request Nov 10, 2026
2 of 4 tasks
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