使用 slerp 旋转时补间相机位置 - THREE.js [英] Tween camera position while rotation with slerp -- THREE.js

查看:42
本文介绍了使用 slerp 旋转时补间相机位置 - THREE.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在旋转时补间 camera 位置.

I want to tween camera position while rotation.

这是我的功能:

function moveAndLookAt(camera, dstpos, dstlookat, options) {
  options || (options = {duration: 300});

  var origpos = new THREE.Vector3().copy(camera.position); // original position
  var origrot = new THREE.Euler().copy(camera.rotation); // original rotation

  camera.position.set(dstpos.x, dstpos.y, dstpos.z);
  camera.lookAt(dstlookat);
  var dstrot = new THREE.Euler().copy(camera.rotation)

  // reset original position and rotation
  camera.position.set(origpos.x, origpos.y, origpos.z);
  camera.rotation.set(origrot.x, origrot.y, origrot.z);

  //
  // Tweening
  //

  // position
  new TWEEN.Tween(camera.position).to({
    x: dstpos.x,
    y: dstpos.y,
    z: dstpos.z
  }, options.duration).start();;

  // rotation (using slerp)
  (function () {
    var qa = camera.quaternion; // src quaternion
    var qb = new THREE.Quaternion().setFromEuler(dstrot); // dst quaternion
    var qm = new THREE.Quaternion();

    var o = {t: 0};
    new TWEEN.Tween(o).to({t: 1}, options.duration).onUpdate(function () {
      THREE.Quaternion.slerp(qa, qb, qm, o.t);
      camera.quaternion.set(qm.x, qm.y, qm.z, qm.w);
    }).start();
  }).call(this);
}

效果很好:http://codepen.io/abernier/pen/zxzObm

唯一的问题是用于旋转的补间似乎不是线性的...导致位置补间(线性)衰减.

The only problem is the tween for rotation does NOT seem to be linear... causing decay with position's tween (which is linear).

如何将 slerp 变成线性补间?

How can I turn slerp into a linear tween ?

谢谢

推荐答案

在你的代码中

// rotation (using slerp)
(function () {
var qa = camera.quaternion; // src quaternion

改成

qa = new THREE.Quaternion().copy(camera.quaternion); // src quaternion

你这样做的方式,qa 与相机四元数相同,它在 slerp 演算中反馈.它必须是一个常量变量.

The way you do it, qa is the same as the camera quaternion, and it feeds back in the slerp calculus. It must be a constant variable.

这篇关于使用 slerp 旋转时补间相机位置 - THREE.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆