在Three.js中改变子对象的世界位置 [英] Changing the World Position of a Child Object in Three.js

查看:262
本文介绍了在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我的代码,'孩子'实际上是一组对象(我希望每个人保持在同一个世界的位置)

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并将其更新为存储值来反转上述过程,但没有成功。我知道改变矩阵世界是不明智的,但这是唯一已经证明富有成效的方法。

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.

有一些工具可以帮助你做到这一点:

There are tools to help you do that:

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

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

three.js r.67

three.js r.67

这篇关于在Three.js中改变子对象的世界位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆