javascript 如何获取子对象的全局/世界位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15098479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the global/world position of a child object?
提问by TunnelVision
How do I get the global position of an Object3D inside another Object3D?
如何在另一个 Object3D 中获取 Object3D 的全局位置?
setup:
设置:
var parent = new THREE.Object3D();
parent.position.set(100, 100, 100);
var child = new THREE.Object3D();
child.position.set(100, 100, 100);
parent.add(child);
scene.add(parent);
notes:
笔记:
I thought that this would be the way to do it:
我认为这将是这样做的方法:
console.log(child.localToWorld(parent.position));
...but it gives me (100, 100, 100), not (200, 200, 200).
...但它给了我(100, 100, 100),而不是(200, 200, 200)。
回答by WestLangley
First call
第一次通话
scene.updateMatrixWorld();
or in your example, it is sufficient to call
或者在您的示例中,调用就足够了
parent.updateMatrixWorld();
Then extract the world position
然后提取世界位置
var vector = new THREE.Vector3();
vector.setFromMatrixPosition( child.matrixWorld );
In practice, however, the renderer calls updateMatrixWorld()
for you in each render loop, so you don't have to.
然而,实际上,渲染器updateMatrixWorld()
会在每个渲染循环中调用您,因此您不必这样做。
EDIT: updated to three.js r.65
编辑:更新为three.js r.65
回答by zwcloud
In threejs r89you can just get the world position by Object3D.getWorldPosition
. I don't know when it was added.
在 Threejs r89 中,您可以通过Object3D.getWorldPosition
. 不知道什么时候加的。