Skip to content

fix(three-live): pre-load camera-orbit clamp; make auto-rotate match a camera orbit - #131

Merged
jbingham merged 3 commits into
mainfrom
fix/turntable-lights-camera-orbit
Dec 3, 2026
Merged

jbingham merged 3 commits into
mainfrom
fix/turntable-lights-camera-orbit

Conversation

@jbingham

@jbingham jbingham commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

fix(three-live): pre-load camera-orbit clamp; make auto-rotate match a camera orbit

Two viewer bugs reported from the Three-Architecture live hero (a two-story
house, camera-orbit="28deg 19deg 23.47m", auto-rotate, shadow-intensity):

  1. Wrong initial zoom. camera-orbit is applied in initViewer() before the
    model exists, so setInitialCamera clamped 23.47 m against the placeholder
    radius = 1 bounds (baseMax = 8 m) and froze it as customDistance;
    loadGLBData never re-resolved it. ThreeView now keeps the raw
    distance/zoom request and resolves it against the real bounds on load
    (unless the user/API has since changed the distance). Small models are no
    longer clamped up to 0.5 m. min/max-camera-orbit still apply.

  2. A deep false shadow at the second-floor joint that appeared only while
    auto-rotating, never when orbiting with the mouse. Three causes; the
    turntable is now equivalent to orbiting the camera (the documented
    auto-rotate contract):

    • Scene-authored lights (KHR_lights_punctual, scene-script lights) stayed
      fixed in world space while modelRootMatrix turned the model, sweeping
      facades through sun angles the scene never authored (and invalidating
      solar studies). New src/model-lights.ts (resolveWorldLights) maps
      authored lights through the model root on the WebGL2 path, the WebGPU
      path, and in shadow-map planning, keeping slot order aligned. The
      viewer's default studio lights and WebXR light estimates are tagged
      worldSpace: true (mirroring MeshGpu.worldSpace) and stay fixed; the
      IBL environment also stays fixed (model-viewer turntable semantics).
    • Shadow frustums were fit to the axis-aligned world AABB, which inflates
      ~30% at oblique turntable angles, changing texel size, normal bias, and
      PCF radius every frame. They are now fit in model-root space
      (computeModelSceneBounds) and mapped back through inverse(root).
    • The WebGL2 contact-shadow plugin classified casters by world AABB, so
      thin walls became "casters" mid-rotation and stamped a dark quad at the
      upper-floor slab height (the visible band). It now classifies and
      clusters in root space and rotates its quads with the model.

Verified with a headless-Chrome repro of the real scene: initial distance
8 m -> 23.47 m; mean |mouse-orbit - auto-rotate| RGB delta at 60deg
27.6 -> 2.2 (the remainder is the intentionally world-fixed IBL; 0.55 with
the environment off).

Tests: shadow_maps_test (resolveWorldLights semantics; bias, PCF radius and
model-space shadow map invariant under turntable; light follows model;
worldSpace lights fixed; scaled AR placement) and three_view_test (pre-load
camera-orbit, resetCamera, user zoom survives reload, small model,
max-camera-orbit clamp, default lights worldSpace, contact-shadow quads
rotate rigidly). npm test passes.

…a camera orbit

Two viewer bugs reported from the Three-Architecture live hero (a two-story
house, `camera-orbit="28deg 19deg 23.47m"`, `auto-rotate`, `shadow-intensity`):

1. Wrong initial zoom. `camera-orbit` is applied in `initViewer()` before the
   model exists, so `setInitialCamera` clamped 23.47 m against the placeholder
   `radius = 1` bounds (`baseMax = 8 m`) and froze it as `customDistance`;
   `loadGLBData` never re-resolved it. ThreeView now keeps the raw
   distance/zoom request and resolves it against the real bounds on load
   (unless the user/API has since changed the distance). Small models are no
   longer clamped up to 0.5 m. `min/max-camera-orbit` still apply.

2. A deep false shadow at the second-floor joint that appeared only while
   auto-rotating, never when orbiting with the mouse. Three causes; the
   turntable is now equivalent to orbiting the camera (the documented
   `auto-rotate` contract):
   - Scene-authored lights (KHR_lights_punctual, scene-script lights) stayed
     fixed in world space while `modelRootMatrix` turned the model, sweeping
     facades through sun angles the scene never authored (and invalidating
     solar studies). New `src/model-lights.ts` (`resolveWorldLights`) maps
     authored lights through the model root on the WebGL2 path, the WebGPU
     path, and in shadow-map planning, keeping slot order aligned. The
     viewer's default studio lights and WebXR light estimates are tagged
     `worldSpace: true` (mirroring `MeshGpu.worldSpace`) and stay fixed; the
     IBL environment also stays fixed (model-viewer turntable semantics).
   - Shadow frustums were fit to the axis-aligned world AABB, which inflates
     ~30% at oblique turntable angles, changing texel size, normal bias, and
     PCF radius every frame. They are now fit in model-root space
     (`computeModelSceneBounds`) and mapped back through `inverse(root)`.
   - The WebGL2 `contact-shadow` plugin classified casters by world AABB, so
     thin walls became "casters" mid-rotation and stamped a dark quad at the
     upper-floor slab height (the visible band). It now classifies and
     clusters in root space and rotates its quads with the model.

Verified with a headless-Chrome repro of the real scene: initial distance
8 m -> 23.47 m; mean |mouse-orbit - auto-rotate| RGB delta at 60deg
27.6 -> 2.2 (the remainder is the intentionally world-fixed IBL; 0.55 with
the environment off).

Tests: shadow_maps_test (resolveWorldLights semantics; bias, PCF radius and
model-space shadow map invariant under turntable; light follows model;
worldSpace lights fixed; scaled AR placement) and three_view_test (pre-load
camera-orbit, resetCamera, user zoom survives reload, small model,
max-camera-orbit clamp, default lights worldSpace, contact-shadow quads
rotate rigidly). `npm test` passes.
…o-root lookup

- Dedupe the turntable shadow-fit helpers into model-lights (rotateDir /
  transformPoint) and simplify resolveWorldLights, trimming the WebGPU
  bundle; raise its gzip budget 25 -> 26 KB (scripts/build.js, README,
  AGENTS) for the light/shadow correctness fix (~0.8 KB gz).
- scene_edits_test: walk up from the test entry point, cwd, and __dirname to
  find the repo root so the package-exports check also works from bundled /
  symlinked runfiles trees (google3 Blaze).
RealityKit mesh/material generation aborts the process on a GPU-less
simulator host (standalone simctl spawn, as in google3 Blaze), which crashed
the whole ThreeCompilerTests bundle. Add requireMetalDevice() and use it in
the three tests that build RealityKit entities, matching the existing Metal
guard in testMakeTextureKeepsRawBytesAndColorAlpha.
@jbingham
jbingham merged commit 46b261c into main Dec 3, 2026
12 checks passed
@jbingham
jbingham deleted the fix/turntable-lights-camera-orbit branch December 3, 2026 21:36
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