First time posting here, and right away I just want to say that I’ve been following along for a few weeks as a non-user, and this community is great. I’m very happy to have access to such an active and friendly group of folks during what’s undoubtedly going to be a long learning process.
I’ve successfully rescaled and repositioned a GLTF after import, and though the bounding box around the changed model is displaying at the correct position, the actual center of the object is still off. I can’t figure out how to center the model. I’m guessing it has something to do either with function scope or with failing to convert the local matrix position of the object into the global matrix outside of the loader function.
Here’s the relevant code, all inside the GLTF loader:
// Create bounding box with info on size of model, then rescale model to max size of 1 boundingBox = new THREE.Box3().setFromObject( model ); let boundingBoxSize = boundingBox.getSize( new THREE.Vector3() ); let maxAxis = Math.max( boundingBoxSize.x, boundingBoxSize.y, boundingBoxSize.z ); model.scale.multiplyScalar( 1.0 / maxAxis ); // Reset bounding box, create its center as inverted array, translate model along that array, reset bounding box boundingBox.setFromObject(model); let boxCenter = boundingBox.getCenter( new THREE.Vector3() ); let centerArrayInverse = ; let centerInverse = boxCenter.multiplyScalar( -1 ).toArray(centerArrayInverse); model.translateX( centerArrayInverse ); model.translateY( centerArrayInverse ); model.translateZ( centerArrayInverse ); boundingBox.setFromObject(model);
Here’s an image of where the camera is pointing, using an
if (model) camera.lookAt( model.position );
in the animate():
In other words, it’s off-center - mostly on the z-axis.
If it’s easy for you to figure out: what mistake am I making here?
I’m hoping the code isn’t too confusing. Happy to take, apply, and remember any and all advice I can get.
Very happy to be learning three.js, and once I’m done with this and get it working I’ll be happy to share the complete code in a new thread for posterity.