问题描述
我有一个 THREE.Scene
,其中添加了两个网格,meshA
和 meshB
,每个网格都以不同的方式旋转.我的目标是从场景中移除 meshB
并将其重新添加为 meshA
的子项,同时保留其全局位置和旋转——换句话说,位置和meshB
的旋转在这段代码前后应该是一样的.
I have a THREE.Scene
with two meshes added, meshA
and meshB
, each of which has been rotated in different ways. 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.
我目前的,几乎可以工作的尝试如下:
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);
定位工作正常;只有 meshA
和 meshB
都围绕同一轴旋转时,旋转才有效——如果 meshA
和 meshB
已绕不同轴旋转,meshB
似乎在此代码前后更改旋转.
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.
我如何更正上面的代码(或不同方法的想法)的任何想法,以便 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
并将其重新添加为 meshA
的子项,同时保持其全局位置和旋转.
You want to remove meshB
from the scene and re-add it as a child of meshA
, while preserving its global position and rotation.
你可以这样做:
meshA.attach( meshB );
three.js r.109
three.js r.109
这篇关于Three.js - 计算相对旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!