最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get an object's world position (Three.js) - Stack Overflow

programmeradmin0浏览0评论

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);

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);
Share Improve this question edited Mar 3, 2016 at 16:36 user128511 asked Mar 3, 2016 at 14:05 user2982195user2982195 1531 silver badge9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

What I did to solve my problem:

Every mesh got a geometry attribute. That is why you can call mesh.geometry. From this point i created a BoundingSphere around my mesh.

mesh.geometry.puteBoundingSphere();

Now it is possible to get the world position of my boundingSphere, which simultaneously is the world position of my mesh.

var vector = mesh.geometry.boundingSphere.center;

Congratulations! 'vector' got the center world position in (x,y,z) of your mesh.

Just to clarify, 'mesh' is a THREE.Mesh object.

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. Look at http://threejs/docs/#Reference/Core/Object3D.matrixWorld but using the position setter will do the heavy lifting for you.

发布评论

评论列表(0)

  1. 暂无评论