Skip to content

editor: restore saved camera projection and lens on open and view - #1305

Closed
kruschdev wants to merge 4 commits into
pascalorg:mainfrom
kruschdev:fix/saved-camera-projection-lens
Closed

kruschdev wants to merge 4 commits into
pascalorg:mainfrom
kruschdev:fix/saved-camera-projection-lens

Conversation

@kruschdev

@kruschdev kruschdev commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #1280

When opening a project with a saved camera view (site.camera or building.camera) or invoking the saved-camera view actions (camera-controls:view, Preview mode, and header/room frameViewerNode framing), only position and target were previously applied, and handleNodeCapture only recorded position, target, and mode without fov or zoom. Views saved in orthographic mode or with a custom lens therefore reopened in the active projection and lens instead of the saved one.

I updated the saved-camera capture and restore paths so projection and lens round-trip together while keeping older saved views backwards-compatible:

  • Saved camera capture and application (packages/editor/src/components/editor/saved-camera-view.ts, custom-camera-controls.tsx): captureSavedCameraView records fov in perspective mode and settled zoom in orthographic mode alongside position, target, and mode. CustomCameraControls switches cameraMode when needed and defers pose + lens application until the matching Three.js camera and CameraControls instance mount across project open (openingFrame), camera-controls:view (handleNodeView), Preview entry, and camera-controls:frame (handleFrame), while ignoring initial null to level_0 floor hydration so off-floor saved targets are not reset to targetY.
  • Views bar / node framing (packages/core/src/events/bus.ts, packages/viewer/src/ui/lib/viewer-selection.ts): CameraControlFrameEvent.pose and viewerFrameEvent forward optional mode, fov, and zoom from node.camera, and frameViewerCamera applies perspective fov and orthographic zoom when present.
  • Stable default camera prop (packages/viewer/src/components/viewer/viewer-camera.tsx): Hoisted DEFAULT_CAMERA_POSITION so React Three Fiber prop reconciliation does not reset camera.position to [10, 10, 10] when ViewerCamera re-renders after a non-transitioned camera restore.
  • Public schema change (packages/core/src/schema/camera.ts, camera.test.ts): Changed CameraSchema.mode from .default('perspective') to .optional(). For @pascal-app/core consumers and MCP clients reading node.camera.mode, legacy { position, target } cameras now parse with mode: undefined instead of 'perspective' so restoring an older saved view preserves the active projection and lens. Added packages/core/src/schema/camera.test.ts verifying legacy cameras parse without mode and round-trip cleanly.
  • Backwards compatibility: Older saved views that omit mode, fov, or zoom keep the active projection and lens unchanged and apply position and target as before.

How to test

  1. Run bunx biome check on the modified files in packages/core, packages/viewer, and packages/editor (0 errors, 0 warnings).
  2. Run bunx turbo run build check-types test --filter=@pascal-app/core --filter=@pascal-app/viewer --filter=@pascal-app/editor and bun run check:render-bundle.
  3. Verify the new #1280 round-trip suite in packages/editor/src/components/editor/saved-camera-view.test.tsx: perspective views with custom fov and orthographic views with custom zoom round-trip across capture, project open, camera-controls:view, and frameViewerNode, while legacy { position, target } views preserve the current projection and lens.

Screenshots / screen recording

Screen recording: cold project open on saved orthographic view (zoom = 85), switching via the views bar to Studio saved in perspective mode (fov = 24), switching back to Studio House (orthographic, zoom = 85), and reloading the project:

Saved orthographic zoom and perspective FOV restored on open, views bar, and reload

Project opened cold with site.camera saved in orthographic mode at zoom = 85 (position: [12, 10, 12], target: [2, 1.2, -1.5]):

Project cold open on orthographic saved view at zoom = 85

After switching to Perspective and clicking Scene -> Site -> Camera snapshot -> View snapshot (restores identical orthographic projection and zoom = 85):

Restored orthographic saved view via View snapshot

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

Note

Medium Risk
Touches core camera schema semantics and many editor camera lifecycle paths (open, preview, framing); behavior change for legacy node.camera without mode, mitigated by tests but worth regression on navigation edge cases.

Overview
Extends saved node cameras so projection and lens settings round-trip with position/target: orthographic zoom and perspective FOV are captured on snapshot, restored on project open, View snapshot, preview, and views-bar / frameViewerNode framing, with deferred application when cameraMode must switch first.

A new saved-camera-view module centralizes read/capture/apply (including settled orthographic zoom via camera-controls’ _zoomEnd). CustomCameraControls routes opening frame, preview, capture/view, and frame events through applyOrDeferSavedView and tightens level-follow so initial floor hydration does not stomp off-floor saved targets. CameraControlFrameEvent.pose and frameViewerCamera forward and apply optional mode, fov, and zoom. CameraSchema.mode is optional (no default 'perspective') so legacy { position, target } data keeps the active projection/lens on restore. ViewerCamera hoists default position to avoid R3F resetting the camera after instant restores.

Added schema and integration tests covering legacy cameras and #1280 round-trips (open, view, framing, preview exit).

Reviewed by Cursor Bugbot for commit 783208b. Bugbot is set up for automated code reviews on this repo. Configure here.

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

Stale Bugbot comment from a previous run.

@Aymericr Aymericr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @kruschdev for taking #1280, and for keeping older saved views (position + target only) working. Capturing fov/zoom with the view and forwarding them through CameraControlFrameEvent.pose is the right shape. A few points before this can go in:

  1. Schema change in CameraSchema: dropping .default('perspective') from mode changes what every parsed node looks like. Old saved cameras now parse with mode: undefined instead of 'perspective'. I see why you want that (an old view shouldn't force perspective), but it's a public schema change for @pascal-app/core consumers and MCP clients that read node.camera.mode. Please call it out in the description, and add a schema test showing an old camera parses without mode and still round-trips.
  2. _zoomEnd: captureSavedCameraView reads a private CameraControls field. If camera-controls renames it, the code silently falls back to camera.zoom, which can be mid-transition. Could you capture after the zoom settles, or keep this behind a small helper with a comment and a test that fails if the field disappears?
  3. Screen recording: this changes what the user sees on project open and from the views bar. Please add a short clip showing an orthographic view with custom zoom, and a perspective view with a non-default FOV, saved, then reopened and restored from the views bar.
  4. Small: saved-camera-view.ts has its import statements split around a type declaration. Biome may not catch that, but grouping the imports at the top reads better.

I couldn't run saved-camera-view.test.tsx locally (it needs built viewer packages), so CI will be the check. It's waiting on maintainer approval for this fork.

@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 1 potential issue.

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 cd93aaa. Configure here.

kruschdev added a commit to kruschdev/editor that referenced this pull request Nov 23, 2026
@kruschdev

Copy link
Copy Markdown
Contributor Author

Thanks @Aymericr! I addressed all four points in cd93aaa and updated the PR description:

  1. Public CameraSchema change & schema test: Called out the CameraSchema.mode change (.default('perspective') -> .optional()) in the PR description for @pascal-app/core consumers and MCP clients, and added packages/core/src/schema/camera.test.ts verifying that legacy { position, target } cameras parse with mode: undefined and round-trip cleanly through JSON serialization and SiteNode.parse.
  2. _zoomEnd helper & regression guard: Extracted readSettledOrthographicZoom in saved-camera-view.ts with a comment explaining why it reads camera-controls's private _zoomEnd field (with fallback to camera.zoom), and added explicit assertions in saved-camera-view.test.tsx checking '_zoomEnd' in orthoControls and its mid-transition value so the test fails loudly if camera-controls ever renames or removes that field.
  3. Screen recording: Added a short recording to the PR description showing cold project open landing on a saved orthographic view (zoom = 85), restoring Studio from the views bar in perspective mode with a non-default fov = 24, restoring Studio House back to orthographic (zoom = 85), and reloading the project.
  4. Import grouping: Grouped all import statements at the top of saved-camera-view.ts above type SavedCameraData.

@Aymericr Aymericr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[Maintainer] Thanks @kruschdev. You addressed the earlier review well: the schema change is called out and has a test, the screen recording is in the description, and legacy position+target views keep working. Two Bugbot findings at 783208b are still open and I think they matter before merge:

  1. saved-camera-view.ts: Bugbot says a legacy { position, target } camera still gets mode: 'perspective' filled in by a Zod parse somewhere (for example updateNode), so readSavedCameraView would treat it as an intentional perspective snapshot and switch projection. You made CameraSchema.mode optional; please confirm no other schema (the site/building camera fields) re-applies the default, and add a test that goes through updateNode rather than only CameraSchema.parse.
  2. custom-camera-controls.tsx: leaving preview can remount the Three.js camera when cameraMode switched, and the level-follow effect treats the remount as cameraChanged and rest-pivots onto the floor, overwriting the pose just restored by takePreviewCameraRestore. A test for preview entry in a different projection followed by exit would pin it.

Still open from the first review: _zoomEnd is a private CameraControls field; a small helper with a comment and a test that fails if the field disappears would make that safe.

CI: GitHub Actions have not run on this head (fork workflows are waiting for maintainer approval). Fixes #1280 is correct.

@kruschdev

Copy link
Copy Markdown
Contributor Author

Thanks @Aymericr! Both Bugbot findings and the _zoomEnd item are already addressed at 783208b:

  • CameraSchema.mode and updateNode: fixed in f2b69e54 and cd93aaa0. I verified packages/core/src/schema/camera.ts is the only schema in packages/core/src/schema mentioning perspective (SiteNode, BuildingNode, and LevelNode all inherit CameraSchema via base.ts), and saved-camera-view.test.tsx tests a legacy { position, target } camera directly through SiteNode.parse and useScene.getState().updateNode('site_0', ...).
  • Preview exit rest-pivot after a projection switch: fixed in 783208b8 (guarding the level-follow effect while pendingAppliedPose or activePoseInterpolation is active) with a regression test in saved-camera-view.test.tsx (preview exit after a projection-switching saved preview view restores the editor pose without level-follow rest-pivot).
  • _zoomEnd helper and regression guard: added in cd93aaa0 via readSettledOrthographicZoom in saved-camera-view.ts plus explicit expect('_zoomEnd' in orthoControls).toBe(true) assertions in saved-camera-view.test.tsx.

The branch merges cleanly with main and is ready for CI approval.

@Aymericr Aymericr added the bot:bugs label Dec 2, 2026 — with Cursor

Aymericr commented Dec 3, 2026

Copy link
Copy Markdown
Member

Review at 783208b8, run with Codex (gpt-6.2-astra) over two rounds: a full review, then each finding re-checked against the diff. Only findings that survived are listed.

  • P2 — packages/editor/src/components/editor/custom-camera-controls.tsx:891: After the opening view lands, a camera remount can call reframeOpening.current?.() at line 804. The retry calls setCameraMode(desiredMode) at line 894 before checking framedView && !forced at line 898, undoing the user’s projection change. Fix: Move that guard before the mode switch; keep forced reframing intact.
  • P2 — packages/editor/src/components/editor/custom-camera-controls.tsx:755: If an orthographic restore is queued and the store switches back to perspective before orthographic controls mount, the pending view still requests orthographic. On the next floor change, the effect records the new floor at line 758, fails the camera match at line 761, and returns at line 771 without clearing the request or following the floor. Fix: Discard a pending restore whose explicit mode no longer matches the store, then fall through to floor handling before updating the floor refs.
  • P2 — packages/editor/src/components/editor/custom-camera-controls.tsx:773: Picking another floor during pose interpolation writes the new currentLevelId into previousLevelIdRef at line 775 and returns at line 778. The comparison at line 787 can no longer detect that pick, so the camera never follows it. Fix: Suppress the pivot for a camera remount, but let an actual floor or level-mode change cancel interpolation and run level follow.
  • P2 — packages/viewer/src/ui/lib/viewer-selection.ts:463: With perspective controls and an orthographic snapshot, frameViewerCamera calls setLookAt on the perspective camera. The isOrthographicCamera check at line 468 skips the saved zoom, and the function never switches projection. The integration test exercises the editor handler, which intercepts saved poses before this function runs. Fix: Have the viewer’s frame-event handler switch projection and apply the pose and lens once matching controls mount; test that path without CustomCameraControls.

This changes visible UI, so please attach before/after screenshots or a short clip.

P0/P1: 0. CI hasn't run on this fork PR yet because it needs a maintainer to approve the workflow run; it merges once that's green.

Aymericr added a commit that referenced this pull request Dec 3, 2026
…es (#1350)

* core: apply per-fallback-locale plural rules in PascalI18n

Ported from #1345 at 03da2d2.

Co-authored-by: kruschdev <249158235+kruschdev@users.noreply.github.com>

* editor: restore saved camera projection and lens on open and view

Ported from #1305 at 783208b.

Co-authored-by: kruschdev <249158235+kruschdev@users.noreply.github.com>

* nodes: make item physical width, height and depth editable in the item panel

Ported from #1308 at 4fed217.

Co-authored-by: kruschdev <249158235+kruschdev@users.noreply.github.com>

* core: keep active-locale formatting and one resolver for fallback plurals

Review fixes for #1345 (Codex review at 03da2d2):
- P2 registry.ts:192: `scope.i18n._()`/`t()` used the merged active-locale
  runtime while `scope._()` used the catalog owner's. The public runtime now
  routes through the same resolver.
- P2 registry.ts:230: the catalog locale also drove number/date formatting,
  so `de-CH` formatted a `de` message's numbers as German. Only a message that
  pluralizes now renders with its catalog's runtime; every other message keeps
  the active locale's formatting.
- P3 i18n.test.tsx:124: the pt-PT override test now registers the same id in
  `pt` with different text and asserts the regional message wins.

* nodes: bound item dimension edits and keep them on the item they started on

Review fixes for #1308 (Codex review at 4fed217):
- P1 dimensions.ts:50: a dimension entry could exceed the Scale maximum of 10
  and accepted a non-finite quotient. MIN/MAX_ITEM_SCALE now bound both the
  Scale and Dimensions controls, and a non-finite quotient is refused.
- P2 dimensions.ts:84: the aspect lock clamped each axis separately and
  changed proportions at the minimum. One factor, limited by every axis's
  bounds, now applies to all three axes.
- P2 panel.tsx:65: a selection change mid-drag dropped the paused preview.
  The drag remembers its item, the dimension sliders are keyed by item so the
  gesture ends on a selection change, and cancel restores that item without
  recording history.
- P3 panel.tsx:110: strict equality recorded an undo step for a retyped
  value that round-trips through the base dimension; scales now compare with
  a relative tolerance.

* editor: keep typed measurements exact and make in-field nudges a draft

Review fixes for #1308 (Codex review at 4fed217):
- P2 panel.tsx:462: SliderControl rounded every parsed value in the display
  unit, so `1 m` typed in imperial mode stored 0.999744 m. A measurement typed
  with its own unit is now kept exactly; a bare number still rounds in the
  display unit it is read in.
- P2 slider-control.tsx:358: Escape could not cancel in-field arrow nudges,
  which wrote the scene on every press. Nudges now edit the draft text (and
  preview only when the control previews typing); Enter or blur commits once,
  Escape discards.

* editor: let floor picks and projection changes win over pending camera restores

Review fixes for #1305 (Codex review at 783208b):
- P2 custom-camera-controls.tsx:891: after the opening view landed, a camera
  remount re-ran the opening frame, which switched projection back before its
  "already framed" guard and undid the person's projection change. Once the
  view has landed the opening no longer switches projection.
- P2 custom-camera-controls.tsx:755: a pending saved view whose projection the
  store had already switched away from held the level follow forever. It is
  now discarded, and the floor change runs.
- P2 custom-camera-controls.tsx:773: a floor picked while a pose interpolated
  was recorded and lost. A camera remount still keeps the pose; a floor or
  level-mode change ends it and the camera follows.

* viewer: frame a saved view in its own projection outside the editor

Review fix for #1305 (Codex review at 783208b):
- P2 viewer-selection.ts:463: with perspective controls and an orthographic
  snapshot, frameViewerCamera aimed the perspective camera and dropped the
  saved zoom. frameViewerCameraInProjection switches projection first, and
  ViewerCameraControls frames the same event once the matching controls
  mount. Tested on the helper, without CustomCameraControls.

* editor: type the level id in the camera level-follow tests

* viewer: open the standalone viewer on the site view's saved projection and lens

Review fix for #1350 (Codex P1, viewer-camera-controls.tsx): the opening
path read `site.camera` and called only `setLookAt`, so a read-only viewer
opened an orthographic or custom-lens site view in its current projection with
the default lens. The opening now frames through the same projection-aware
path as the views bar, with `savedViewPose` shared by both.

Co-authored-by: Aymeric Rabot <Aymericr@users.noreply.github.com>

* editor: keep the landed opening view's pose across a projection switch

Review fix for #1350 (Bugbot, custom-camera-controls.tsx:902): once the
opening view had landed, a projection switch remounted the camera at the
default pose, and the opening frame returned early, which also skipped the
level follow's rest pivot. The remount reframe now puts the new camera at the
saved view's position and target in the projection the person picked; the
follow timers still leave the camera alone.

Co-authored-by: Aymeric Rabot <Aymericr@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: kruschdev <249158235+kruschdev@users.noreply.github.com>
Co-authored-by: Aymeric Rabot <Aymericr@users.noreply.github.com>

Aymericr commented Dec 3, 2026

Copy link
Copy Markdown
Member

Thank you @kruschdev! This landed in #1350 (squash merge 1eb1b27) as its own commit, with you credited as co-author. Fork CI couldn't run here, so we ported it onto current main.

Saved views now restore their projection and lens on open and on view, as you intended. On top of that, review fixes cover a few edge cases. A projection the user picks after the view lands is kept. A stale pending view is dropped. A floor change ends an in-flight pose. The standalone viewer's opening now uses the same projection-aware framing. Each fix has an integration test.

Closing this one as landed. Thanks again!

@Aymericr Aymericr closed this Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Maintainer] Saved camera views should restore projection and lens, not just position and target

2 participants