
two objects in the scene. the cube rotate axis should be the cube\'s center,th
The pivot solution was not working for me.
With OBJLoader.js (for loading .obj objects), you need to get the boundingBox of the object, get its center, multiply scalar by -1, and use this to translate the geometry of EVERY child's geometry. Then, you need to reUpdate the boundingBox if you want to use it for a purpose.
Here is a function that loads an obj and "normalize" its geometry vertexes, given the directory and name of the OBJ and MTL (texture) files (for example, if the OBJ and MTL files are dir1/myObject.obj and dir1/myObject.mtl, then you call loadObj('dir1','myObject')).
function loadObj(dir, objName) {
var onProgress = function(xhr) {
if (xhr.lengthComputable) {
var percentComplete = xhr.loaded / xhr.total * 100;
console.log(Math.round(percentComplete, 2) + '% downloaded');
}
};
var onError = function(xhr) {};
// Manager
var manager = new THREE.LoadingManager();
manager.onProgress = function(item, loaded, total) {
console.log( 'Started loading file: ' + item + '.\nLoaded ' + loaded + ' of ' + total + ' files.' );
};
var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath(dir);
mtlLoader.load(objName + '.mtl', function(materials) {
materials.preload();
// Model
var loader = new THREE.OBJLoader(manager);
loader.setMaterials(materials);
loader.setPath(dir);
loader.load(objName + '.obj', function (object) {
var objBbox = new THREE.Box3().setFromObject(object);
// Geometry vertices centering to world axis
var bboxCenter = objBbox.getCenter().clone();
bboxCenter.multiplyScalar(-1);
object.traverse(function (child) {
if (child instanceof THREE.Mesh) {
child.geometry.translate(bboxCenter.x, bboxCenter.y, bboxCenter.z);
}
});
objBbox.setFromObject(object); // Update the bounding box
scene.add(object);
}, onProgress, onError);
});
}