Three.js - Algorithm to set the camera so the whol

2019-08-05 20:10发布

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条回答
爷、活的狠高调
2楼-- · 2019-08-05 20:14

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.

查看更多
登录 后发表回答