代码之家  ›  专栏  ›  技术社区  ›  user2132190

计算目标四元数而不使用lookAt作弊

  •  0
  • user2132190  · 技术社区  · 7 年前

    在不存储当前四元数和使用lookAt的情况下,如何计算相机的目标四元数(稍后用于slerp)?

    这给了我想要的结果(即四元数):

    // newTarget is Vector3
    let fromQuaternion = (new THREE.Quaternion()).copy(this.camera.quaternion);
    this.camera.lookAt(newTarget);
    let toQuaternion = (new THREE.Quaternion()).copy(this.camera.quaternion);
    this.camera.quaternion.set(fromQuaternion.x, fromQuaternion.y, fromQuaternion.z, fromQuaternion.w);
    

    我试图通过以下方式避免上述情况,但这不起作用(得到更大的四元数):

    // currentTarget, newTarget are Vector3
    var _c = (new THREE.Vector3()).copy(this.camera.position);
    var _t0 = (new THREE.Vector3()).copy(currentTarget).sub(_c).normalize();
    var _t1 = (new THREE.Vector3()).copy(newTarget).sub(_c).normalize();
    var toQuaternion = new THREE.Quaternion();
    toQuaternion.setFromUnitVectors(_t1, _t0);
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   user2132190    7 年前

    反转四元数的方向并将其初始化为当前摄影机工作:

        var _c = (new THREE.Vector3()).copy(this.camera.position);
        var _t0 = (new THREE.Vector3()).copy(currentTarget).sub(_c).normalize();
        var _t1 = (new THREE.Vector3()).copy(this.orbitControls.target).sub(_c).normalize();
        var toQuaternion = new THREE.Quaternion();
        toQuaternion.setFromUnitVectors(_t0, _t1);
        toQuaternion.multiply(this.camera.quaternion);