Skip to content

Commit

Permalink
fix(GizmoHelper): allow GizmoHelper to be used with camera-controls (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
a3ng7n authored Aug 19, 2024
1 parent 25685bd commit d681f3e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/core/GizmoHelper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?:
Expand All @@ -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],
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down

0 comments on commit d681f3e

Please sign in to comment.