3

I've been using an old revision of Three.js for quite some time, so I decided to upgrade to the latest (r68). I knew I would bump into some issues, but I wasn't expecting the removal of geometry.computeCentroids() and .centroid property.

I'm loading models using the OBJMTLLoader library. The problem with this is that since it no longer has the .computeCentroids() method, I have no way of getting them. I've tried to calculate them using other methods, but to no avail:

I've also tried to use the .localToWorld(point) methods, but they're not working either. It always returns (0,0,0).

For now, when I click a mesh, I'm calculating (or trying to calculate) its centroid with:

 if (mesh.centroid === undefined) {
    mesh.centroid = new THREE.Vector3();
    mesh.centroid = mesh.geometry.center();
    console.log(mesh.centroid); }

I'm also using this whenever I add a new object to the scene (which has child meshes):

//desperate try to find different values for centroids
object.updateMatrix();
scene.updateMatrix();
object.updateMatrixWorld();
scene.updateMatrixWorld();

What's even more strange is that the centroids I'm trying to calculate aren't consistent. It always shows the same vectors in the console.log(), no matter which order I click the meshes.

THREE.Vector3 {x: 158.89799999999997, y: -4.115949999999998, z: 67.75310000000002, constructor: function, set: function…}
THREE.Vector3 {x: 0.000005004882780212938, y: 0.16375010757446518, z: 0.0000024658203301441972, constructor: function, set: function…}
THREE.Vector3 {x: -1.7053025658242404e-13, y: -0.0818749484539012, z: 1.1368683772161603e-13, constructor: function, set: function…}

I was wondering if someone had a similar issue with this. I haven't tried previous revisions of three.js, since I really want to upgrade to the latest version and keep it consistent from there.

Thank you in advance.

EDIT: Forgot to mention an important detail. I want the centroid in WORLD coordinates.

After using mrdoob 's method, I was able to get a centroid. I always get the same value for every mesh because, even though I'm using .calculateBoundingBoxes() on each mesh, these bounds refer to the main parent object. Each geometry has all of the object's vertices assigned, and I'm assuming that the bounding boxes are calculated according to these vertices, and that's why I'm getting the same values for every geometry.

Community
  • 1
  • 1
VitorOliveira
  • 35
  • 2
  • 8
  • Maybe related, I just made a computeCentroids() function to calculate face centroids https://gist.github.com/DelvarWorld/c9c41b549d0b1e97da8890a79e3ab8d0 to replace the functionality removed in newer three builds – Andy Ray Jul 05 '16 at 03:47

1 Answers1

8

If you want to compute the centroid of the whole geometry this is the code you need:

geometry.computeBoundingBox();

var centroid = new THREE.Vector3();
centroid.addVectors( geometry.boundingBox.min, geometry.boundingBox.max );
centroid.multiplyScalar( - 0.5 );

centroid.applyMatrix4( mesh.matrixWorld );
mrdoob
  • 19,334
  • 4
  • 63
  • 62
  • I tried using the above method, but I get the same centroids for every object: `THREE.Vector3 {x: 158.89799999999997, y: -3.9521999999999977, z: 67.75310000000002, constructor: function, set: function…}` I just edited my question because I forgot an important detail: I want to use the world coordinates of the calculated centroid. – VitorOliveira Aug 13 '14 at 08:52
  • Updated answer with the world space bit. – mrdoob Aug 13 '14 at 16:45
  • 1
    Thank you for your help! I found out that the problem for having the same values might be related to Loader itself, but I do have the world coordinates of the full object now. – VitorOliveira Aug 13 '14 at 17:52
  • 1
    I had to change `centroid.multiplyScalar( - 0.5 );` to `centroid.multiplyScalar( 0.5 );`. OBJLoader r73 – 2pha Jan 12 '16 at 02:43