My challenge is to create a Scene that loads a single .obj
file where the object is fully visible in the PerspectiveCamera
upon scene initialization.
- The field of view (FOV) is set at 60
- The objects vary in size
TrackballControls
are utilized for camera control
Despite attempting this specified solution, I encountered issues.
The provided code snippet is as follows:
FOV = 60
scene = new THREE.Scene()
camera = new THREE.PerspectiveCamera( FOV, 500 / 350, 0.1, 10000 )
scene.add( camera )
objLoader = new THREE.OBJLoader();
objLoader.load('/airboat.obj', function (object) {
scene.add( object )
var boundingBox = new THREE.Box3();
boundingBox.setFromObject( object );
var sphere = boundingBox.getBoundingSphere()
// not functioning correctly
var center = boundingBox.getCenter();
var size = boundingBox.getSize();
var maxDim = Math.max( size.x, size.y, size.z );
var cameraZ = maxDim / 2 / Math.tan(Math.PI * FOV / 360);
camera.lookAt(center)
camera.position.set( center.x, center.y, cameraZ );
camera.updateProjectionMatrix();
})
UPDATE
For further reference, I have prepared this fiddle (please scroll down the JS code) where @Brakebein's solution typically works well, except for instances where the bounding box edges are not visible