Skip to content

Commit

Permalink
Set rotation instead of setting target twice in order to preserve piv…
Browse files Browse the repository at this point in the history
…ot point
  • Loading branch information
anders-hopland committed Jul 31, 2024
1 parent ea7034a commit 20d8d5b
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions react-components/src/hooks/useSceneDefaultCamera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ export const useSceneDefaultCamera = (
fitCameraToSceneDefault: () => {
const initialCameraState = viewer.cameraManager.getCameraState();

viewer.cameraManager.setCameraState({ position, target });

// Check if camera pivot point is in origin
// If not, preserve the target
// Preserve pivot point if user has already set a pivot point
// If not, set the pivot point near the center of the scene
// When moving Scene to system space, we will extend the data
// model to store pos, rot and pivot
if (
!(
initialCameraState.target.x === 0 &&
initialCameraState.target.y === 0 &&
initialCameraState.target.z === 0
)
initialCameraState.target.x === 0 &&
initialCameraState.target.y === 0 &&
initialCameraState.target.z === 0
) {
viewer.cameraManager.setCameraState({
target: initialCameraState.target
});
viewer.cameraManager.setCameraState({ position, target });
} else {
const direction = new Vector3().subVectors(position, target).normalize();
const rotation = new Euler().setFromVector3(direction);
const quaternion = new Quaternion().setFromEuler(rotation);

viewer.cameraManager.setCameraState({ position, rotation: quaternion });
}
},
isFetched: true
Expand Down

0 comments on commit 20d8d5b

Please sign in to comment.