Three.js - Algorithm to set the camera so the whole scene shows?

这一生的挚爱 提交于 2019-12-24 14:13:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!