Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.js How to get position of a mesh?

With the below code, position of a mesh is returned as (0, 0, 0) but it is not. So is the positioın vector calculated after render process?

me.scene.add(objMesh); //me is a project class
objMesh.updateMatrixWorld(true);
alert(objMesh.position.x + ',' + objMesh.position.y + ',' + objMesh.position.z);

objMesh is created from objfile, it is added to the scene correctly and centroid is approx (-8, 3, 0) but position vector of objMesh is (0, 0, 0) do we have to auto calculate something first or should i calculate it manually from geometry vertices of the mesh ?

http://81.214.75.32:8181/admin is the url

the site is in Turkish so i will translate the UI items

in the site there is "Dosya" menu item oppen the menu item and select "Proje Aç" a dialog appears in that dialog select MUTFAK_1 scene will appear in that scene, every meshes position is (0, 0, 0) is that possible :)

like image 309
Tezcan Avatar asked Jan 08 '13 09:01

Tezcan


People also ask

How do I change the position of a mesh in 3 JS?

Use mesh. position. set(x,y,z) because you will do not need force updating, if you set mesh. position = xxx, you only changing the object attribute, not always moving object in scene real time.

How do I center an object in 3 JS?

In three. js, an object's "center" is the origin in the object's local coordinate system. If your object is not rotating around the point you want, then you need to translate the vertices of your geometry so the desired center is at the origin. geometry.

What is a mesh in three Js?

A Three. js Mesh is a base class that inherits from Object3d and is used to instantiate polygonal objects by combining a Geometry with a Material. Mesh is also the base class for the more advanced MorphAnimMesh and SkinnedMesh classes.


1 Answers

object.position is always local to the object. If you want to get the position in world space you need to get it from object.matrixWorld.

Try with this:

scene.add(objMesh);
scene.updateMatrixWorld(true);
var position = new THREE.Vector3();
position.getPositionFromMatrix( objMesh.matrixWorld );
alert(position.x + ',' + position.y + ',' + position.z);

r58


Update:

The function getPositionFromMatrix() has been renamed to setFromMatrixPosition().

like image 113
mrdoob Avatar answered Sep 25 '22 14:09

mrdoob