javascript - Three.js 上的平滑相机控制开关

标签 javascript 3d three.js webvr

我正在开发一个网站,该网站允许用户使用 VRControls 查看天空中的物体,并使用轨迹球控件查看他们最喜欢的物体,点击物体即可初始化。这是演示。 https://jsfiddle.net/tushuhei/4f1Lum5n/6/

function focus (obj, target) {
  var dummyCamera = camera.clone();
  controls = new THREE.TrackballControls(dummyCamera);
  controls.target.set(obj.point.x, obj.point.y, obj.point.z);
  new TWEEN.Tween(camera.position)
    .to(target, 1000)
    .onComplete(function() {
      transitioning = false;
      controls.dispose();
      controls = new THREE.TrackballControls(camera);
      controls.target.set(obj.point.x, obj.point.y, obj.point.z);
  }).start();
}

TWEEN 非常适合从 WebVR 到轨迹球模式的转换,反之亦然,但在转换结束时仍有一点差距。我猜这是因为过渡完成阶段相机旋转的间隙。

考虑到相机位置和旋转,是否有任何方法可以使两个不同相机控件之间的过渡平滑?

谢谢,

最佳答案

您使用 dummyCamera 的方法是正确的。您需要在初始四元数和最后一个四元数之间获取最终四元数和 slerp,而补间负责位置。

// Save the initial quaternion so that we can use it as a 
// starting point for the slerp.
var startQuaternion = camera.quaternion.clone();

// Apply the tracking controls to a cloned dummy camera so 
// that we can get the final quaternion.
var dummyCamera = camera.clone();
dummyCamera.position.set(target.x, target.y, target.z);
var dummyControls = new THREE.TrackballControls(dummyCamera);
dummyControls.target.set(obj.point.x, obj.point.y, obj.point.z);
dummyControls.update();

// Disable VR controls, so that it doesn't compete with 
// the tween for control  of the camera.
// (Note: now you need to check if the controls are 
// null in the animate function)
controls.dispose();
controls = null;

new TWEEN.Tween(camera.position)
.to(target, 1000)
.onUpdate(function (timestamp){
  // Slerp the camera quaternion as well. 
  // timestamp is the eased time value from the tween.
  THREE.Quaternion.slerp(
    startQuaternion, dummyCamera.quaternion, camera.quaternion, timestamp);
})
.onComplete(function() {
  controls = new THREE.TrackballControls(camera);
  controls.target.set(obj.point.x, obj.point.y, obj.point.z);
}).start();

https://jsfiddle.net/4f1Lum5n/10/

附言到目前为止,您实现此方法的方式可能会让 VR 用户感到恶心。接管用户的观点通常是不好的做法。另一种解决方案可能是将用户放在宇宙飞船或平台上,然后在平台上进行补间,以便用户始终可以控制相机。

关于javascript - Three.js 上的平滑相机控制开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34324495/

相关文章:

3d - 实时 GI - 体素锥追踪

c++ - 在 C++ 中可视化 3D 条形图

javascript - 为什么 FileReader 不将文件传递给 three.js 场景使用的 loader.load()?

来自jpg图像的纹理贴图的three.js平面几何即将出现黑色

javascript - AnimationMixer 不会播放动画(gltf 文件)

javascript - 重复项的两个总和问题 - 更惯用的 Javascript 解决方案

javascript - 点击overlay div播放youtube视频

javascript - 服务中的 angularJS $stateParams

javascript - 使用 three.js 自定义形状

javascript - backbone, javascript mvc - 使用 javascript 设计 View