简体   繁体   English

如何获取子对象的全局/世界位置?

[英]How to get the global/world position of a child object?

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)

You can extract the world position like so:您可以像这样提取世界位置:

var target = new THREE.Vector3(); // create once an reuse it

child.getWorldPosition( target );

target will contain the result. target将包含结果。

EDIT: updated to three.js r.120编辑:更新为three.js r.120

In threejs r89 you can just get the world position by Object3D.getWorldPosition .在 Threejs r89 中,您可以通过Object3D.getWorldPosition获取世界位置。 I don't know when it was added.不知道什么时候加的。

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

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