两点之间过渡的缓动动画函数,可以调整相机半径调整相机远近。
实现如下:
export const moveTarget = async (
camera: BABYLON.ArcRotateCamera,
options: MoveTarget,
): Promise<void> => {
const { position, radius, speed } = options
const ease = new BABYLON.CubicEase()
ease.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEINOUT)
const animationParams = {
speed,
duration: 120,
easingFunction: ease,
}
// 定义半径动画
const radiusAnimation = animateProperty(camera, 'radius', camera.radius, radius, animationParams)
// 定义目标动画
const targetAnimation = animateProperty(
camera,
'target',
camera.target,
position,
animationParams,
)
// 等待两个动画完成
await Promise.all([radiusAnimation, targetAnimation])
}
// 辅助函数,用于创建并启动动画
export const animateProperty = (
target: any,
property: string,
from: any,
to: any,
params: any,
): Promise<void> => {
return new Promise((resolve) => {
const animation = BABYLON.Animation.CreateAndStartAnimation(
'animation',
target,
property,
params.speed,
params.duration,
from,
to,
0,
params.easingFunction,
)
animation?.waitAsync().then(() => {
resolve()
})
})
}