THREE.js 旋转副本不起作用

THREE.js rotation copy is not working

我有一个错误,我复制了一个对象的 Quaternion 并将其传递给一个函数,在该函数中它被应用到另一个对象以保持它们的旋转同步。我无法将 Quaternion 应用于第二个对象。

给定对象 1 为 msh,对象 2 为 msh2,此代码不会将 msh 的旋转应用于 msh2

  var rot = new THREE.Quaternion().copy(msh.rotation);
  msh2.rotation.copy(rot);

您可以在这个 Stack Snippet 中进一步了解这一点,它以最小的可重现方式包含问题(但不是我在真实项目中使用的确切代码)

var renderer = new THREE.WebGLRenderer({canvas : $("#can")[0]});
renderer.setSize($("#can").width(), $("#can").height());
var cam = new THREE.PerspectiveCamera(45, $("#can").width() / $("#can").height(), 0.1, 100);
cam.position.set(0,2,6);
cam.quaternion.multiply(new THREE.Quaternion().setFromAxisAngle( new THREE.Vector3( 1, 0, 0 ), -Math.PI/8 ));
var scene = new THREE.Scene();

var geo = new THREE.BoxGeometry(1,1,1);
var mat = new THREE.MeshBasicMaterial({color : 0xff0000});
var msh = new THREE.Mesh(geo,mat);
scene.add(msh);

geo = new THREE.BoxGeometry(1,2,1);
mat = new THREE.MeshBasicMaterial({color : 0xff00ff});
var msh2 = new THREE.Mesh(geo,mat);
msh2.position.set(2,0,0);
scene.add(msh2);

function render() {
  requestAnimationFrame( render );
  msh.rotateX(0.001);
  msh.rotateY(0.002);
  //For some reason, this doesn't work??
  var rot = new THREE.Quaternion().copy(msh.rotation);
  msh2.rotation.copy(rot);
  //Yet this does (but it doesn't fit the flow of my
  //original project because I don't want to pass
  //objects around.
  //msh2.rotation.copy(msh.rotation);
  renderer.render( scene, cam );
}
render();
<script src="https://cdn.rawgit.com/mrdoob/three.js/dev/build/three.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="can" width="400" height="300"></canvas>

Also see the JSFiddle here

我是不是漏掉了什么?我一直用 Vector3 这样做,所以我不明白为什么我不能在这里这样做...

如果我对你的理解是正确的,你希望两个物体以相同的速度和角度旋转。您只需要使用以下两行更新 render(); 函数:

  msh2.rotateX(0.001);
  msh2.rotateY(0.002);

Updated JSFiddle

msh.rotation 是 THREE.Euler 的实例,但您正试图复制 to/from THREE.Quaternion。 msh2.rotation.copy(msh.rotation) 应该可以正常工作,msh2.quaternion.copy(msh.quaternion)