From d681f3efb2e0d0e2118081ae789c312f129d5990 Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 19 Aug 2024 07:48:11 -0600 Subject: [PATCH] fix(GizmoHelper): allow GizmoHelper to be used with camera-controls (#2066) --- src/core/GizmoHelper.tsx | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/core/GizmoHelper.tsx b/src/core/GizmoHelper.tsx index 8f72c4fb2..bf7b737f9 100644 --- a/src/core/GizmoHelper.tsx +++ b/src/core/GizmoHelper.tsx @@ -4,6 +4,7 @@ import { Group, Matrix4, Object3D, OrthographicCamera as OrthographicCameraImpl, import { OrthographicCamera } from './OrthographicCamera' import { OrbitControls as OrbitControlsType } from 'three-stdlib' import { Hud } from './Hud' +import { CameraControls as CameraControlsType } from './CameraControls' type GizmoHelperContext = { tweenCamera: (direction: Vector3) => void @@ -22,7 +23,7 @@ const [q1, q2] = [/* @__PURE__ */ new Quaternion(), /* @__PURE__ */ new Quaterni const target = /* @__PURE__ */ new Vector3() const targetPosition = /* @__PURE__ */ new Vector3() -type ControlsProto = { update(): void; target: Vector3 } +type ControlsProto = { update(delta?: number): void; target: Vector3 } export type GizmoHelperProps = JSX.IntrinsicElements['group'] & { alignment?: @@ -48,6 +49,10 @@ const isOrbitControls = (controls: ControlsProto): controls is OrbitControlsType return 'minPolarAngle' in (controls as OrbitControlsType) } +const isCameraControls = (controls: CameraControlsType | ControlsProto): controls is CameraControlsType => { + return 'getTarget' in (controls as CameraControlsType) +} + export const GizmoHelper = ({ alignment = 'bottom-right', margin = [80, 80], @@ -76,7 +81,11 @@ export const GizmoHelper = ({ const tweenCamera = React.useCallback( (direction: Vector3) => { animating.current = true - if (defaultControls || onTarget) focusPoint.current = defaultControls?.target || onTarget?.() + if (defaultControls || onTarget) { + focusPoint.current = + onTarget?.() || + (isCameraControls(defaultControls) ? defaultControls.getTarget(focusPoint.current) : defaultControls?.target) + } radius.current = mainCamera.position.distanceTo(target) // Rotate from current camera orientation @@ -115,8 +124,12 @@ export const GizmoHelper = ({ mainCamera.position.set(0, 0, 1).applyQuaternion(q1).multiplyScalar(radius.current).add(focusPoint.current) mainCamera.up.set(0, 1, 0).applyQuaternion(q1).normalize() mainCamera.quaternion.copy(q1) + + if (isCameraControls(defaultControls)) + defaultControls.setPosition(mainCamera.position.x, mainCamera.position.y, mainCamera.position.z) + if (onUpdate) onUpdate() - else if (defaultControls) defaultControls.update() + else if (defaultControls) defaultControls.update(delta) invalidate() } }