canvas - ThreeJS - 沿样条线移动相机,同时保持轨道控制平移
问题描述
THREE.CatmullRomCurve3
我有一个带有一堆点的样条实例。使用鼠标滚轮时,我可以在样条曲线上上下移动相机。
但是,在样条曲线上设置相机位置时,我失去了 OrbitControls 旋转/平移。我想用鼠标沿着样条线移动相机,同时仍然能够用光标环顾四周。
任何人都知道如何做到这一点?下面是一些重要的代码:
// Increment/Decrement number when scrolling via mouse wheel
let camPosIndex = 0;
canvas.addEventListener('wheel', (e) => {
camPosIndex += -Math.sign(e.deltaY) * 0.1;
if (camPosIndex < 0) {
camPosIndex = 0;
}
});
// Animation loop
tick() {
// Update camera around spline
if (camera && cameraSplinePoints.length > 0 && cameraSpline) {
const camPos = cameraSpline.getPoint(camPosIndex / 100);
const camRot = cameraSpline.getTangent(camPosIndex / 100);
camera.position.x = camPos.x;
camera.position.y = camPos.y;
camera.position.z = camPos.z;
camera.rotation.x = camRot.x;
camera.rotation.y = camRot.y;
camera.rotation.z = camRot.z;
// Somewhere above or below, the camera is now ignoring the orbitcontrols panning.
camera.lookAt(cameraSpline.getPointAt((camPosIndex+1) / 100));
}
// Update controls
controls.update()
// Render
renderer.render(scene, camera)
// Call tick again on the next frame
window.requestAnimationFrame(tick)
}
解决方案
我可以通过将目标设置为相机位置并朝其方向看来计算它。
在我的渲染循环中,我通过以下方式设置位置和旋转:
controls.object.position.set(camPos.x, camPos.y, camPos.z);
controls.target = new THREE.Vector3().addVectors(
controls.object.position,
controls.object.getWorldDirection()
);
推荐阅读
- tensorflow - 将 Keras/tensorflow 模型从 Linux 迁移到 Windows
- r - cbind 通过常见列表元素名称的 data.frames 列表
- javascript - 将字符串拆分为每行 36 个字符的数组,不分词
- vuejs2 - 在 XAMPP 上使用 Vue CLI 3
- javascript - 未捕获的 TypeError:URL 不是 javascript 中的构造函数
- c++ - 条件变量的“等待”函数在提供谓词时导致意外行为
- botframework - Bot Framework 网络聊天+语音+特定语音
- wordpress - 如何修复古腾堡 InnerBlocks 以响应 RangeControl
- docker - docker run 失败并显示 /bin/sh:0 -c 需要一个参数
- bixby - 如何在 Bixby 编程中访问 Content Provider