简体   繁体   English

如何将全局转换应用于子对象

[英]How to apply global transformation to child object

I'm trying to apply a global position (from a WebVR controller) to the child object of a parent that has several transformations. 我正在尝试将全局位置(从WebVR控制器)应用于具有多个转换的父级的子级对象。 It's not a first generation child, though there aren't any transformations applied to any of the object in between: 它不是第一代子级,尽管在这之间没有任何转换应用于任何对象:

Scene 现场
¦ |
¦ |
Object (with transformations) 对象 (带有转换)
¦ |
¦ |
Object (no transformations) 对象 (无变换)
¦ |
¦ |
Child ( cube ) (Where I want to apply a global position) 子级多维数据集 )(我想在其中应用全局位置的位置)

If I apply these transformations to a cube that's a direct child of the scene, it works fine: 如果我将这些转换应用于作为场景直接子对象的多维数据集,则可以正常工作:

cube.position.copy(globalPosition);

But I need to apply it to the child object. 但是我需要将其应用于子对象。 I've tried to convert the global position to the local one by doing: 我尝试通过执行以下操作将全球位置转换为本地位置:

const worldToLocal = new THREE.Matrix4().getInverse(cube.parent.matrixWorld);
cube.position.copy(globalPosition);
cube.applyMatrix(worldToLocal);

But the cube is not in the same position as if it were a child of the scene. 但是该多维数据集与场景中的子元素的位置不同。 Not sure what else to try, thanks for the help! 不知道还有什么尝试,谢谢您的帮助!

Good day everyone! 今天是个好日子! I advice to regroup your meshes. 我建议重新组合您的网格物体。 If you want to change position in world of all group, you need to apply position on parent object, transform use on child objects. 如果要更改所有组的世界中的位置,则需要在父对象上应用位置,转换在子对象上的使用。 For grouping try to use empty mesh and use method parentMesh.add(childMesh). 对于分组,请尝试使用空网格并使用方法parentMesh.add(childMesh)。 Hope this help's ;) 希望这可以帮助 ;)

I got stuck with this issue a long time ago, and ended up grouping it differently and making cube a direct child of the scene and that seemed to work at the time, but now, cube 's previous parents started acting weird, they would rotate to a strange position and not move if I tried to change their position or rotation. 很久以前,我就一直困扰着这个问题,最终以不同的方式对它进行分组,使cubescene的直接子对象,这在当时似乎很有效,但是现在, cube的前任父母开始表现得很奇怪,他们会轮流到一个奇怪的位置,并且如果我尝试更改其位置或旋转,则不要移动。 That's why I know wanted to fix it properly. 这就是为什么我知道想要正确修复它的原因。

So I ended up doing something similar to what @WestLangley mentioned, but I also need to transform the rotation, so I used matrices: 所以我最终做了类似于@WestLangley提到的事情,但是我还需要变换旋转,所以我使用了矩阵:

const worldToLocal = new THREE.Matrix4().getInverse(cube.parent.matrixWorld);
const poseMatrix = new THREE.Matrix4();
poseMatrix.makeRotationFromQuaternion(globalQuaternion);
poseMatrix.setPosition(globalPosition);
poseMatrix.multiplyMatrices(worldToLocal, poseMatrix);
poseMatrix.decompose(cube.position, cube.quaternion, {});

Thanks a lot for everyone's help! 非常感谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM