Three.js r68 - Unable to get geometry centroids using OBJMTLLoader
I have been using the old version of Three.js for quite some time, so I decided to upgrade to the latest version (r68). I knew I was going to run into some problems, but I didn't expect to remove the geometry.computeCentroids () and .centroid properties.
I am loading models using OBJMTLLoader library. The problem is that since it no longer has a .computeCentroids () method, I have no way to get them. I tried to calculate them using other methods but to no avail:
- Three.js How to get the grid position?
- Three.js move and rotate the center of the geometry
- Three.js child grid constant position (0,0,0)
I've also tried using the .localToWorld (point) methods, but they don't work either. It always returns (0,0,0).
So far, when I click on a grid, I calculate (or try to calculate) its centroid with:
if (mesh.centroid === undefined) {
mesh.centroid = new THREE.Vector3();
mesh.centroid = mesh.geometry.center();
console.log(mesh.centroid); }
I also use this when 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 weirder is that the centroids I'm trying to calculate are incompatible. It always shows the exact same vectors in console.log (), no matter what order I click on the cells.
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 anyone has a similar problem. I have not tried previous versions of three.js as I really want to upgrade to the latest version and keep it consistent.
Thanks in advance.
EDIT: Forgot to mention an important detail. I want the centroid to be at WORLD coordinates.
After using the mrdoob method, I was able to get the centroid. I always get the same value for each mesh because even though I use .calculateBoundingBoxes () for each mesh, these constraints are relative to the main parent. Each geometry has all the assigned vertices of the objects, and I am assuming that the bounding boxes are calculated according to those vertices, and therefore I get the same values for each geometry.
source to share
If you want to calculate the center of gravity of all 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 );
source to share