在 Three.js 中更改对象父级? [英] Change Object parent in Three.js?
本文介绍了在 Three.js 中更改对象父级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以更改任何对象的父级?
Is it possible to change the parent of any object?
例如:
scene.add(Object);
Object.Add(OtherObject);
OtherObject.parent = scene; // <-- Something like this
我只知道我可以删除对象并在新的父对象中再次创建它,但我希望有另一种方法.我有一些在飞机上的物体,如果我点击它们,我想把它们固定在相机上,这样我就可以一直在相机前看到.如果我再次单击它们,我想再次将父级设置为飞机.
I just know that I can remove the Object and create it again in the new parent, but I'll hope there's another way. I got some Object that are on a plane, if I click them I want fix them to the camera so I can see all the time in front of the camera. And If I click them Again I wanna set the parent again to the plane.
为什么这不起作用?
var obj = clicked.clone();
camera.add(obj);
obj.position.set(0,0,-20);
推荐答案
最简单的方法是使用 THREE.SceneUtils 对象的 detach 和 attach 函数:
Easiest way is to use the detach and attach functions of the THREE.SceneUtils object:
THREE.SceneUtils.detach( child, parent, scene );
THREE.SceneUtils.attach( child, scene, parent );
这篇关于在 Three.js 中更改对象父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文