THREE.js 旋转复制不起作用

Cob*_*urn 2 javascript quaternions three.js

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

给定对象 1msh和对象 2 ,此代码将不会应用tomsh2的旋转mshmsh2

  var rot = new THREE.Quaternion().copy(msh.rotation);
  msh2.rotation.copy(rot);
Run Code Online (Sandbox Code Playgroud)

您可以在这个堆栈片段中进一步看到这一点,其中以最小的可重现方式包含问题(但这不是我在实际项目中使用的确切代码)

  var rot = new THREE.Quaternion().copy(msh.rotation);
  msh2.rotation.copy(rot);
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)

另请参阅此处的 JSFiddle

我在这里错过了什么吗?我一直这样做Vector3,所以我不明白为什么我不能在这里这样做......

小智 5

msh.rotation 是 THREE.Euler 的一个实例,但您正在尝试复制到 THREE.Quaternion 或从 THREE.Quaternion 复制。msh2.rotation.copy(msh.rotation) 应该可以正常工作, msh2.quaternion.copy(msh.quaternion) 也可以正常工作