简体   繁体   English

如何获得对象的世界位置(Three.js)

[英]How to get an object's world position (Three.js)

I want to place my already placed object at a new location, but it moves from the local position and not global. 我想将已经放置的对象放置在新位置,但是它是从本地位置而不是全局位置移动的。

this._scene.updateMatrixWorld();
this._scene.add(mesh);
var v1 = new THREE.Vector3();    
v1.setFromMatrixPosition( mesh.matrixWorld );  
mesh.position.set(v1.x +2, v1.y + 2, v1.z + 2);

What I did to solve my problem: 我为解决问题所做的工作:

Every mesh got a geometry attribute. 每个网格都有一个几何属性。 That is why you can call mesh.geometry. 这就是为什么您可以调用mesh.geometry的原因。 From this point i created a BoundingSphere around my mesh. 从这一点开始,我在网格周围创建了一个BoundingSphere。

mesh.geometry.computeBoundingSphere();

Now it is possible to get the world position of my boundingSphere, which simultaneously is the world position of my mesh. 现在可以获取我的boundingSphere的世界位置,同时它也是我的网格物体的世界位置。

var vector = mesh.geometry.boundingSphere.center;

Congratulations! 恭喜! 'vector' got the center world position in (x,y,z) of your mesh. “向量”在网格的(x,y,z)中获得中心世界位置。

Just to clarify, 'mesh' is a THREE.Mesh object. 为了澄清起见,“ mesh”是一个THREE.Mesh对象。

Wouldn't you just then move the object itself by the increment? 您难道不只是随即以增量移动对象本身吗? I'm not sure why you need the matrix involved? 我不确定为什么需要涉及矩阵?

mesh.position.set(mesh.position.x +2, mesh.position.y + 2, mesh.position.z + 2);

edit... if you need to use matrix, you need to set the matrixWorld. 编辑...如果需要使用matrix,则需要设置matrixWorld。 Look at http://threejs.org/docs/#Reference/Core/Object3D.matrixWorld but using the position setter will do the heavy lifting for you. 查看http://threejs.org/docs/#Reference/Core/Object3D.matrixWorld,但使用位置设置器将为您带来繁重的工作。

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

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