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(GizmoHelper): allow GizmoHelper to be used with camera-controls #2066

Merged
Merged
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
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