After sticking with an outdated version of Three.js for a while, I decided to upgrade to the latest (r68). Little did I know, I would encounter some unexpected issues - specifically, the removal of geometry.computeCentroids() and .centroid property.
My current challenge lies in loading models using the OBJMTLLoader library. The absence of the .computeCentroids() method means I am unable to retrieve them. Despite my attempts to calculate centroids through alternative methods, such as:
- Three.js How to get position of a mesh?
- Three.js move and rotate the center of geometry
- Three.js Child Mesh position always return (0,0,0)
Even attempts at using the .localToWorld(point) methods have proven futile, consistently returning (0,0,0).
In my current workaround, when I click on a mesh, I apply the following calculation:
if (mesh.centroid === undefined) {
mesh.centroid = new THREE.Vector3();
mesh.centroid = mesh.geometry.center();
console.log(mesh.centroid); }
A similar approach is taken when adding a new object to the scene:
//desperate try to find different values for centroids
object.updateMatrix();
scene.updateMatrix();
object.updateMatrixWorld();
scene.updateMatrixWorld();
Strangely enough, the calculated centroids are not consistent. Regardless of the mesh clicked or the order, the console.log() displays the same vectors every time:
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…}
If anyone has encountered a similar issue, please share your experience. Though I haven't explored previous versions of three.js, my goal is to stick with the latest update.
Your insights are greatly appreciated.
EDIT: An important detail I failed to mention earlier - the desired centroid should be in WORLD coordinates.
Following mrdoob's suggestion, I managed to obtain centroids. However, the identical values across meshes suggest that .calculateBoundingBoxes() calculates based on the main parent object, rather than individual geometries. Each geometry contains all vertices of the object, leading to the uniform bounding box values.