Skip to content

Commit

Permalink
fix changing Object3D.DEFAULT_UP after creating a screen camera store
Browse files Browse the repository at this point in the history
  • Loading branch information
bbohlender committed Jan 21, 2025
1 parent f534cc7 commit 26592f0
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions packages/handle/src/screen/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ function computeOriginToCameraOffset(
target: Vector3,
cameraDistanceToOrigin: number,
cameraRotation: Euler,
zToUp: Quaternion,
yToUp: Quaternion,
): void {
target.copy(zAxis).applyEuler(cameraRotation).applyQuaternion(zToUp).multiplyScalar(cameraDistanceToOrigin)
target.copy(zAxis).applyEuler(cameraRotation).applyQuaternion(yToUp).multiplyScalar(cameraDistanceToOrigin)
}

function buildCameraPositionUpdate(
Expand All @@ -42,12 +42,12 @@ function buildCameraPositionUpdate(
y: number,
z: number,
origin: Readonly<Vector3Tuple>,
upToZ: Quaternion,
upToY: Quaternion,
): void {
v1Helper.set(x, y, z)
v2Helper.set(...origin)
v1Helper.sub(v2Helper)
v1Helper.applyQuaternion(upToZ)
v1Helper.applyQuaternion(upToY)
const distance = v1Helper.length()
v1Helper.divideScalar(distance)
qHelper.setFromUnitVectors(zAxis, v1Helper)
Expand All @@ -59,7 +59,8 @@ function buildCameraPositionUpdate(

const vectorHelper = new Vector3()

const zToUpHelper = new Quaternion()
const yToUpHelper = new Quaternion()
const upToYHelper = new Quaternion()

export function computeScreenCameraStoreTransformation(
pitch: number,
Expand All @@ -70,26 +71,24 @@ export function computeScreenCameraStoreTransformation(
rotation?: Quaternion,
up = Object3D.DEFAULT_UP,
) {
zToUpHelper.setFromUnitVectors(yAxis, up)
yToUpHelper.setFromUnitVectors(yAxis, up)
eHelper.set(pitch, yaw, 0, 'YXZ')
if (position != null) {
computeOriginToCameraOffset(position, cameraDistanceToOrigin, eHelper, zToUpHelper)
computeOriginToCameraOffset(position, cameraDistanceToOrigin, eHelper, yToUpHelper)
const [x, y, z] = origin
position.x += x
position.y += y
position.z += z
}
if (rotation != null) {
rotation.setFromEuler(eHelper).premultiply(zToUpHelper)
rotation.setFromEuler(eHelper).premultiply(yToUpHelper)
}
}

export function createScreenCameraStore(
{ distance = 5, origin = [0, 0, 0], pitch: rotationX = 0, yaw: rotationY = 0 }: Partial<ScreenCameraState> = {},
up = Object3D.DEFAULT_UP,
) {
const upToZ = new Quaternion().setFromUnitVectors(up, yAxis)
const zToUp = upToZ.clone().invert()
return createStore<ScreenCameraStateAndFunctions>((set, get) => ({
distance,
origin,
Expand All @@ -102,11 +101,13 @@ export function createScreenCameraStore(
},
setCameraPosition(x, y, z, keepOffsetToOrigin = false) {
const update: Partial<ScreenCameraState> = {}
buildCameraPositionUpdate(update, x, y, z, get().origin, upToZ)
upToYHelper.setFromUnitVectors(up, yAxis)
buildCameraPositionUpdate(update, x, y, z, get().origin, upToYHelper)
if (keepOffsetToOrigin === true) {
const state = get()
eHelper.set(state.pitch, state.yaw, 0, 'YXZ')
computeOriginToCameraOffset(vectorHelper, state.distance, eHelper, zToUp)
yToUpHelper.setFromUnitVectors(yAxis, up)
computeOriginToCameraOffset(vectorHelper, state.distance, eHelper, yToUpHelper)
vectorHelper.x -= x
vectorHelper.y -= y
vectorHelper.z -= z
Expand All @@ -122,7 +123,8 @@ export function createScreenCameraStore(
if (keepOffsetToCamera === false) {
const { pitch, distance, origin: oldOrigin, yaw } = get()
computeScreenCameraStoreTransformation(pitch, yaw, distance, oldOrigin, vectorHelper, undefined, up)
buildCameraPositionUpdate(update, vectorHelper.x, vectorHelper.y, vectorHelper.z, origin, upToZ)
upToYHelper.setFromUnitVectors(up, yAxis)
buildCameraPositionUpdate(update, vectorHelper.x, vectorHelper.y, vectorHelper.z, origin, upToYHelper)
}
set(update)
},
Expand Down

0 comments on commit 26592f0

Please sign in to comment.