Three.js - 设置相机以便整个场景显示的算法? [英] Three.js - Algorithm to set the camera so the whole scene shows?
问题描述
当我设置场景、添加几何图形等时,如何设置相机才能看到整个场景?我正在尝试使用边界框实现算法,但我有点卡住了.
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.
推荐答案
你真的只需要使用 Math.abs(number)
找到所有变量之间的最大绝对值,一旦你有找到最大的你可以将相机的深度(position.z)设置为该数字.我做了一个简单的函数,它接收 2 个数字并返回最大值.
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);
或
camera.position.set(yourX, yourY, greatestNumber);
好吧,我希望它有所帮助.
Well I hope it helps.
这篇关于Three.js - 设置相机以便整个场景显示的算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!