Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After saving the current state of the camera, it can be restored through state data #4143

Closed
dtboy1995 opened this issue Mar 8, 2025 · 2 comments
Labels
enhancement New feature or request question

Comments

@dtboy1995
Copy link

Prerequisites

@speckle/viewer version 2.20.0-alpha4

What package are you referring to?

@speckle/viewer version 2.20.0-alpha4

Is your feature request related to a problem? Please describe.

After saving the current state of the camera, it can be restored through state data

Here are some codes

let camera = viewer.getRenderer().renderingCamera;
// save
function saveCameraState(camera) {
  const state = {
    position: {
      x: camera.position.x,
      y: camera.position.y,
      z: camera.position.z,
    },
    rotation: {
      x: camera.rotation.x,
      y: camera.rotation.y,
      z: camera.rotation.z,
      order: camera.rotation.order, // 必须保存旋转顺序
    },
    scale: { x: camera.scale.x, y: camera.scale.y, z: camera.scale.z },
    fov: camera.fov,
    near: camera.near,
    far: camera.far,
  };
  // save to localstorage
  localStorage.setItem('cameraState', JSON.stringify(state));
  return state;
}
// restore
const savedState = localStorage.getItem('cameraState');
if (savedState) {
  const state = JSON.parse(savedState);
  const aspect =
    viewer.getContainer().offsetWidth /
    viewer.getContainer().offsetHeight;
  camera.position.set(
    state.position.x,
    state.position.y,
    state.position.z
  );
  camera.rotation.set(
    state.rotation.x,
    state.rotation.y,
    state.rotation.z,
    state.rotation.order
  );
  camera.scale.set(state.scale.x, state.scale.y, state.scale.z);
  camera.fov = state.fov;
  camera.near = state.near;
  camera.far = state.far;
  camera.aspect = aspect;
  camera.updateProjectionMatrix();
  viewer.requestRender();
}

Describe the solution you'd like

Although the above code can be executed successfully, it only retains a moment. As long as I move the mouse, the model immediately returns to the default perspective. May I ask why this is happening and how to solve this problem? Thank you so much

Problem GIF

Image

Thank you so much
Be deeply grateful

@dtboy1995 dtboy1995 added enhancement New feature or request question labels Mar 8, 2025
@dtboy1995
Copy link
Author

use setCameraView

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question
Projects
None yet
Development

No branches or pull requests

1 participant