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: Camera rotation target from new camera position #3409

Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion examples/src/utils/CustomCameraManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ export class CustomCameraManager implements CameraManager {
const target =
state.target ??
(state.rotation
? CameraManagerHelper.calculateNewTargetFromRotation(this._camera, state.rotation, this._controls.target)
? CameraManagerHelper.calculateNewTargetFromRotation(
this._camera,
state.rotation,
this._controls.target,
position
)
: this._controls.target);

this._camera.position.copy(position);
Expand Down
6 changes: 4 additions & 2 deletions viewer/packages/camera-manager/src/CameraManagerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ export class CameraManagerHelper {
* @param camera Used camera instance.
* @param rotation New camera rotation in quaternion form.
* @param currentTarget Current camera target.
* @param position New camera position.
* @returns
*/
static calculateNewTargetFromRotation(
camera: THREE.PerspectiveCamera,
rotation: THREE.Quaternion,
currentTarget: THREE.Vector3
currentTarget: THREE.Vector3,
position: THREE.Vector3
): THREE.Vector3 {
const distToTarget = currentTarget.clone().sub(camera.position);
const tempCam = camera.clone();
Expand All @@ -54,7 +56,7 @@ export class CameraManagerHelper {
.getWorldDirection(new THREE.Vector3())
.normalize()
.multiplyScalar(distToTarget.length())
.add(tempCam.position);
.add(position);

return newTarget;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ export class DefaultCameraManager implements CameraManager {
? CameraManagerHelper.calculateNewTargetFromRotation(
this._camera,
state.rotation,
this._controls.getState().target
this._controls.getState().target,
newPosition
)
: this._controls.getState().target);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe(CameraManagerHelper.name, () => {
// creates rotation around Y axis of 180 degrees.
const rotation = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI);

const newTarget = CameraManagerHelper.calculateNewTargetFromRotation(camera, rotation, target);
const newTarget = CameraManagerHelper.calculateNewTargetFromRotation(camera, rotation, target, camera.position);

expect(newTarget.x).toBeCloseTo(target.x);
expect(newTarget.y).toBeCloseTo(target.y);
Expand Down
2 changes: 1 addition & 1 deletion viewer/reveal.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export class CameraManagerHelper {
target: THREE_2.Vector3;
};
static calculateNewRotationFromTarget(camera: THREE_2.PerspectiveCamera, newTarget: THREE_2.Vector3): THREE_2.Quaternion;
static calculateNewTargetFromRotation(camera: THREE_2.PerspectiveCamera, rotation: THREE_2.Quaternion, currentTarget: THREE_2.Vector3): THREE_2.Vector3;
static calculateNewTargetFromRotation(camera: THREE_2.PerspectiveCamera, rotation: THREE_2.Quaternion, currentTarget: THREE_2.Vector3, position: THREE_2.Vector3): THREE_2.Vector3;
static updateCameraNearAndFar(camera: THREE_2.PerspectiveCamera, combinedBbox: THREE_2.Box3): void;
}

Expand Down
Loading