[英]Three.js - calculating relative rotations
I have a THREE.Scene
with two meshes added, meshA
and meshB
, each of which has been rotated in different ways.我有一个
THREE.Scene
添加了两个网格, meshA
和meshB
,每个网格都以不同的方式旋转。 My goal is to remove meshB
from the scene and re-add it as a child of meshA
, while preserving its global position and rotation -- in other words, the position and rotation of meshB
should appear the same before and after this code.我的目标是从场景中移除
meshB
并将其重新添加为meshA
的子meshA
,同时保留其全局位置和旋转——换句话说,在此代码之前和之后, meshB
的位置和旋转应该显示相同。
My current, almost working attempt is as follows:我目前的,几乎可以工作的尝试如下:
var globalOffset = new THREE.Vector3().subVectors( meshB.position, meshA.position );
var localOffset = meshA.worldToLocal( meshB.position );
var rotationOffset = meshA.quaternion.clone().inverse();
var rotation = meshB.quaternion.clone().multiply( rotationOffset );
scene.remove(meshB);
meshA.add(meshB);
meshB.position = localOffset;
meshB.rotation.setFromQuaternion(rotation);
The positioning works fine;定位工作正常; the rotation only works if both
meshA
and meshB
have been rotated around the same axis -- if meshA
and meshB
have been rotated around different axes, meshB
appears to change rotation before and after this code.只有当
meshA
和meshB
都围绕同一轴旋转时,旋转才有效——如果meshA
和meshB
围绕不同的轴旋转, meshB
在这段代码之前和之后似乎改变了旋转。
Any ideas how I can correct the code above (or an idea for a different approach) so that meshB
still has the same "global" rotation before and after being removed and re-added to the scene?有什么想法可以更正上面的代码(或不同方法的想法),以便
meshB
在被删除并重新添加到场景之前和之后仍然具有相同的“全局”旋转?
Thanks!谢谢!
You want to remove meshB
from the scene and re-add it as a child of meshA
, while preserving its global position and rotation.您想从场景中移除
meshB
并将其重新添加为meshA
的子meshA
,同时保留其全局位置和旋转。
You can do that like so:你可以这样做:
meshA.attach( meshB );
three.js r.109三.js r.109
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.