Three.js - 计算相对旋转

问题描述 投票:0回答:1

我有一个加上两个网格的THREE.ScenemeshAmeshB,每个都以不同的方式旋转。我的目标是从场景中移除meshB并将其重新添加为meshA的子节点,同时保留其全局位置和旋转 - 换句话说,meshB的位置和旋转在此代码之前和之后应该看起来相同。

我目前几乎正在努力的尝试如下:

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);

定位工作正常;只有当meshAmeshB围绕同一轴旋转时,旋转才有效 - 如果meshAmeshB围绕不同的轴旋转,则meshB似乎会在此代码之前和之后改变旋转。

任何想法如何我可以纠正上面的代码(或一个不同方法的想法),以便meshB在删除和重新添加到场景之前和之后仍然具有相同的“全局”轮换?

谢谢!

javascript rotation three.js scenegraph
1个回答
2
投票

有三个.js工具可用于这样做:

THREE.SceneUtils.detach( child, parent, scene ); // remove child from parent and add to scene

THREE.SceneUtils.attach( child, scene, parent ); // remove child from scene and add to parent

全球方向没有变化。

要使用这些方法,必须将文件/examples/js/utils/SceneUtils.js添加到项目中。

three.js r.96

© www.soinside.com 2019 - 2024. All rights reserved.