简体   繁体   English

在 Three.js 中改变子对象的世界位置

[英]Changing the World Position of a Child Object in Three.js

Is there a way in Three.js to change the world position of a child object? Three.js 有没有办法改变子对象的世界位置?

My ultimate goal is to:我的最终目标是:

1) Detach the child from the parent (without it's location changing) 1)将孩子与父母分离(不改变它的位置)
2) Move the parent to a different location 2)将父级移动到不同的位置
3) Reattach the child (without it's location changing) 3)重新连接孩子(不改变位置)

In my code, the 'child' is actually a group of objects (and I'm looking to keep each in the same world position)在我的代码中,“孩子”实际上是一组对象(我希望每个对象都保持在相同的世界位置)

Currently, I have achieved 1 and 2 in the following way:目前,我通过以下方式实现了 1 和 2:

scene.updateMatrixWorld();
var vector = new THREE.Vector3();
vector.setFromMatrixPosition( child.matrixWorld );
grandParent.remove( parent );
scene.add( parent );
parent.children[0].position = vector;

I've tried to reverse the above process by storing the matrixWorld, and updating it to the stored value, but was unsuccessful.我试图通过存储 matrixWorld 并将其更新为存储的值来反转上述过程,但没有成功。 I know that altering the matrixWorld is ill-advised, but it's the only method that has proven fruitful.我知道改变 matrixWorld 是不明智的,但这是唯一已证明有效的方法。

Yes, you can detach the child from the parent, transform the parent, and the reattach the child.是的,您可以将子项与父项分离,转换父项,然后重新附加子项。

The method to use to do that is:用于执行此操作的方法是:

parent.attach( object );

which adds object as a child of parent , while maintaining the object's world transform.它将object添加为parent的子级,同时保持对象的世界变换。

three.js r.113三.js r.113

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

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