Skip to content

Instantly share code, notes, and snippets.

@xaliphostes
Last active February 7, 2024 08:20
Show Gist options
  • Save xaliphostes/009cc1d79b6ece0f9bf8345279428c26 to your computer and use it in GitHub Desktop.
Save xaliphostes/009cc1d79b6ece0f9bf8345279428c26 to your computer and use it in GitHub Desktop.
camera tween movement
import { Vector3 } from 'three'
import * as TWEEN from 'tween'
export type Callback = () => void
export function setView(position: Vector3, target: Vector3, duration = 0, callback: Callback = null) {
let endPosition = position.clone()
let endTarget = target.clone()
const startPosition = this.position.clone()
const startTarget = this.getPivot()
const easing = TWEEN.Easing.Quartic.Out
if (duration === 0) {
this.position.copy(endPosition)
this.lookAt(endTarget)
}
else {
let value = { x: 0 }
let tween = new TWEEN.Tween(value).to({ x: 1 }, duration)
tween.easing(easing)
tween.onUpdate(() => {
let t = value.x
this.position.set(
(1 - t) * startPosition.x + t * endPosition.x,
(1 - t) * startPosition.y + t * endPosition.y,
(1 - t) * startPosition.z + t * endPosition.z
)
this.lookAt(new THREE.Vector3(
(1 - t) * startTarget.x + t * endTarget.x,
(1 - t) * startTarget.y + t * endTarget.y,
(1 - t) * startTarget.z + t * endTarget.z,
))
})
tween.start()
if (callback !== null) {
tween.onComplete(() => callback())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment