All I want to do is load an OBJ file and translate its coordinates to the world origins (0,0,0) so that orbit controls work perfectly (no Pivot points please).
I'd like to load random OBJ objects with different geometries/center points and have them translated automatically to the scene origin. In other words, a 'hard coded' translate solution for a specific model won't work
This has got to be one of the most common scenarios for Three JS (basic 3d object viewer), so I'm surprised I can't find a definitive solution on SO.
Unfortunately there are a lot of older answers with deprecated functions, so I would really appreciate a new answer even if there are similar solutions out there.
Things I've tried
the code below fits the object nicely to the camera, but doesn't solve the translation/orbiting problem.
// fit camera to object var bBox = new THREE.Box3().setFromObject(scene); var height = bBox.size().y; var dist = height / (2 * Math.tan(camera.fov * Math.PI / 360)); var pos = scene.position; // fudge factor so the object doesn't take up the whole view camera.position.set(pos.x, pos.y, dist * 0.5); camera.lookAt(pos);
Apparently the
geometry.center()
is good for translating an object's coordinates back to the origin, but theTHREE.GeometryUtils.center
has been replaced bygeometry.center()
and I keep getting errors when trying to use it.when loading OBJs, geometry has now been replaced by bufferGeometry. I can't seem to cast the buffergeometry into geometry in order to use the
center()
function. do I have to place this in the object traverse > child loop like so? this seems unnecessarily complicated.geometry = new THREE.Geometry().fromBufferGeometry( child.geometry );
My code is just a very simple OBJLoader.
var objLoader = new THREE.OBJLoader();
objLoader.setPath('assets/');
objLoader.load('BasketballNet_Skull.obj', function (object) {
object.traverse( function ( child ) {
if ( child instanceof THREE.Mesh ) {
child.material = material;
}
} );
scene.add(object);
});
(BTW first real question on SO so forgive any formatting / noob issues)