在 Three.js 中改变子对象的世界位置 [英] Changing the World Position of a Child Object in Three.js
问题描述
Three.js 有没有办法改变子对象的世界位置?
Is there a way in Three.js to change the world position of a child object?
我的最终目标是:
1) 将孩子与父母分离(不改变位置)
2) 将父级移动到不同的位置
3) 重新连接孩子(不改变位置)
1) Detach the child from the parent (without it's location changing)
2) Move the parent to a different location
3) Reattach the child (without it's location changing)
在我的代码中,孩子"实际上是一组对象(我希望每个对象都保持在相同的世界位置)
In my code, the 'child' is actually a group of objects (and I'm looking to keep each in the same world position)
目前,我通过以下方式实现了1和2:
Currently, I have achieved 1 and 2 in the following way:
scene.updateMatrixWorld();
var vector = new THREE.Vector3();
vector.setFromMatrixPosition( child.matrixWorld );
grandParent.remove( parent );
scene.add( parent );
parent.children[0].position = vector;
我试图通过存储 matrixWorld 并将其更新为存储的值来反转上述过程,但没有成功.我知道改变 matrixWorld 是不明智的,但这是唯一已证明有效的方法.
I've tried to reverse the above process by storing the matrixWorld, and updating it to the stored value, but was unsuccessful. I know that altering the matrixWorld is ill-advised, but it's the only method that has proven fruitful.
推荐答案
是的,您可以将子级与父级分离,转换父级,然后重新附加子级.
Yes, you can detach the child from the parent, transform the parent, and the reattach the child.
使用的方法是:
parent.attach( object );
将 object
添加为 parent
的子元素,同时保持对象的世界变换.
which adds object
as a child of parent
, while maintaining the object's world transform.
three.js r.113
three.js r.113
这篇关于在 Three.js 中改变子对象的世界位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!