After various projects importing all types on models: gltf, fbx, animated-gltf, obj+mtl… Bbox and centering has been one of the most challenging and frustrating issues. Curious if anyone here has a reliable approach. Here are some of the issues I have faced.
-
Box3. Often this approach, which is most recommended online, gives very inaccurate results, especially for animated models.
new THREE.Box3().setFromObject(mesh);
-
Traverse geometry. Using this approach often results in completely different values than a Box3 bbox, which is confusing. Any idea why?. Also when you scale the mesh it seems to have no affect on the geometry which can be tricky. Is it safe/recommended to scale or translate the geometry directly?
mesh.traverse( ( child ) => { if ( child instanceof THREE.Mesh ) { if(child.geometry){ //just make sure it has geo? child.geometry.computeBoundingBox(); ... do something ... } }
-
Bones. Creating bounding box from bones has been the most reliable however this would require your model to have bones. For animated models, this seems the only way to have an accurate bbox.
var bone_min = {x:Infinity,y:Infinity,z:Infinity}; var bone_max = {x:-Infinity,y:-Infinity,z:-Infinity}; for(var b=0;b<skeleton.bones.length;b++){ var child = skeleton.bones[b]; var position = new THREE.Vector3(); child.getWorldPosition (position); if(position.x < bone_min.x){ bone_min.x = position.x; } if(position.y < bone_min.y){ bone_min.y = position.y; } if(position.z < bone_min.z){ bone_min.z = position.z; } if(position.x > bone_max.x){ bone_max.x = position.x; } if(position.y > bone_max.y){ bone_max.y = position.y; } if(position.z > bone_max.z){ bone_max.z = position.z; } }...