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

fix(react-components): Improve Scene home camera button and bump to 0.55.2 #4695

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion react-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cognite/reveal-react-components",
"version": "0.55.1",
"version": "0.55.2",
"exports": {
".": {
"import": "./dist/index.js",
Expand Down
53 changes: 37 additions & 16 deletions react-components/src/hooks/useSceneDefaultCamera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import { useMemo } from 'react';
import { useSceneConfig } from '../query/useSceneConfig';
import { Vector3, Quaternion, Euler, MathUtils, Box3 } from 'three';
import { CDF_TO_VIEWER_TRANSFORMATION, type Cognite3DViewer } from '@cognite/reveal';
import { Vector3, Quaternion, Euler, MathUtils, Matrix4 } from 'three';
import { CDF_TO_VIEWER_TRANSFORMATION } from '@cognite/reveal';
import { type SceneConfiguration } from '../components/SceneContainer/sceneTypes';
import { useReveal } from '../components/RevealCanvas/ViewerContext';

Expand All @@ -32,13 +32,39 @@ export const useSceneDefaultCamera = (
};
}

// Use a good default camera position if no camera configuration is provided
if (
data.sceneConfiguration.cameraTranslationX === 0 &&
data.sceneConfiguration.cameraTranslationY === 0 &&
data.sceneConfiguration.cameraTranslationZ === 0 &&
data.sceneConfiguration.cameraEulerRotationX === 0 &&
data.sceneConfiguration.cameraEulerRotationY === 0 &&
data.sceneConfiguration.cameraEulerRotationZ === 0 &&
data.sceneConfiguration.cameraTargetX === undefined &&
data.sceneConfiguration.cameraTargetY === undefined &&
data.sceneConfiguration.cameraTargetZ === undefined
) {
return {
fitCameraToSceneDefault: () => {
if (viewer.models.length === 0) {
const position = new Vector3(-100, 200, 400);
const target = new Vector3();
viewer.cameraManager.setCameraState({ position, target });
} else {
viewer.fitCameraToModels(viewer.models);
}
},
isFetched: false
};
}

const position = new Vector3(
data.sceneConfiguration.cameraTranslationX,
data.sceneConfiguration.cameraTranslationY,
data.sceneConfiguration.cameraTranslationZ
);

const target = extractCameraTarget(data.sceneConfiguration, viewer);
const target = extractCameraTarget(data.sceneConfiguration);
position.applyMatrix4(CDF_TO_VIEWER_TRANSFORMATION);
target.applyMatrix4(CDF_TO_VIEWER_TRANSFORMATION);

Expand All @@ -58,8 +84,12 @@ export const useSceneDefaultCamera = (
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);
const up = new Vector3(0, 1, 0);
const right = new Vector3().crossVectors(up, direction).normalize();
const recomputedUp = new Vector3().crossVectors(direction, right).normalize();
const rotationMatrix = new Matrix4();
rotationMatrix.makeBasis(right, recomputedUp, direction);
const quaternion = new Quaternion().setFromRotationMatrix(rotationMatrix);

viewer.cameraManager.setCameraState({ position, rotation: quaternion });
}
Expand All @@ -69,7 +99,7 @@ export const useSceneDefaultCamera = (
}, [viewer, data?.sceneConfiguration]);
};

function extractCameraTarget(scene: SceneConfiguration, viewer: Cognite3DViewer): Vector3 {
function extractCameraTarget(scene: SceneConfiguration): Vector3 {
if (scene.cameraTargetX !== undefined) {
return new Vector3(scene.cameraTargetX, scene.cameraTargetY, scene.cameraTargetZ);
} else {
Expand All @@ -87,15 +117,6 @@ function extractCameraTarget(scene: SceneConfiguration, viewer: Cognite3DViewer)
scene.cameraTranslationY,
scene.cameraTranslationZ
);
// As a heuristic, use distance to center of all models' bounding
// boxes as target distance
const positionToSceneCenterDistance = position.distanceTo(
viewer.models
.reduce((acc, m) => acc.union(m.getModelBoundingBox()), new Box3())
.getCenter(new Vector3())
);
return position
.clone()
.add(new Vector3(0, 0, -positionToSceneCenterDistance).applyQuaternion(rotation));
return position.clone().add(new Vector3(0, 0, -50).applyQuaternion(rotation));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Models are not neccesarily loaded, will thus often have the wrong bounding box. Using a constant for distance to target instead

}
}
Loading