0
点赞
收藏
分享

微信扫一扫

three.js 在使用OrbitControls的时候视野移动


OrbitControls–轨道控制器。相机交由其控制 编程控制 camera 旋转无效 但是 position有效
可以结合 controls.target 和 cmera.position 视线移动 旋转动画

/**
* @description: 视野切换动画
* @param {number[]} targetCamera 目标位置的相机位置
* @param {number[]} targetAxis 目标位置的坐标轴位置
* @param {number} speed 速度
* @return {void}
*/
function move(targetCamera, targetAxis,speed=16) {
// const targetCamera = [-10, 20, 26];
// const targetAxis = [-9, 17, 0];

const diffCamera = [];
const diffAxis = [];

for (let i = 0; i < 3; i++) {
diffCamera.push(distance(camera.position[String.fromCharCode(120 + i)], targetCamera[i]))
diffAxis.push(distance(controls.target[String.fromCharCode(120 + i)], targetAxis[i]))
}

let count = 0;
const r = () => {
if (count < speed) {
camera.position.x += diffCamera[0] / speed;
camera.position.y += diffCamera[1] / speed;
camera.position.z += diffCamera[2] / speed;

controls.target.x += diffAxis[0] / speed;
controls.target.y += diffAxis[1] / speed;
controls.target.z += diffAxis[2] / speed;
requestAnimationFrame(r);
count++;
controls.update()
}
}
r();
}

function distance(x1, x2) {
//都是负数 -3 => -5 = -2
if (x1 < 0 && x2 < 0) {
return (x2 * -1 - x1 * -1) * -1;
}
return x2 - x1;
}


举报

相关推荐

0 条评论