问题
When I setup my scene, add geometries etc, how do I set the camera so I can see my whole scene? I'm trying to implement an algorithm using boundingboxes, but I'm kinda stuck.
回答1:
You really only need to find the greatest absolute value between all your variables by using Math.abs(number)
, once you have found the greatest of all you can set the depth(position.z) of the camera to that number. I have made a simple function which receives 2 numbers and returns the greatest.
function findGreatestAbsolute( firstNumber, secondNumber ) {
if( Math.abs( firstNumber ) > Math.abs( secondNumber )) {
return Math.abs( firstNumber );
} else { return Math.abs( secondNumber ); }
}
You can also use Arrays if there are too many numbers. Once you have found your number you do:
camera = new THREE.PerspectiveCamera( cameraFov, windowHalfX / windowHalfY , 1, someDepth );
camera.position.z(greatestNumber);
or
camera.position.set(yourX, yourY, greatestNumber);
Well I hope it helps.
来源:https://stackoverflow.com/questions/13572258/three-js-algorithm-to-set-the-camera-so-the-whole-scene-shows