简体   繁体   English

如何在Three.js中从任何摄像机角度实现ZoomAll功能?

[英]How can I implement ZoomAll functionality from any camera angle in Three.js?

I have a scene with many Object3D objects containing blocks, cylinders and meshes. 我有一个场景,其中包含许多包含块,圆柱体和网格物体的Object3D对象。 I also have a perspective camera and use trackballcontrols. 我也有一个透视相机,并使用轨迹球控件。 What I need is a way to set the camera position without modifying the FOV or the camera angle and insure that all objects in the scene are visible on screen. 我需要的是一种无需更改FOV或摄像机角度即可设置摄像机位置的方法,并确保场景中的所有对象都在屏幕上可见。 I have successfully done this with preset top, bottom, left and right views using the bounding box of the entire scene, but I need this to work from any camera angle since the user can rotate the view using trackball controls into any position. 我已经使用整个场景的边界框成功完成了预设的顶视图,底视图,左视图和右视图的操作,但是我需要从任何摄像机角度进行操作,因为用户可以使用轨迹球控件将视图旋转到任何位置。

If there were a way to get a 2d bounding rectangle based on the camera position, target and the list of objects, I think I could get it to work, but I am having trouble finding how to do this. 如果有一种方法可以根据摄像机的位置,目标和对象列表来获取2d边界矩形,我想我可以使它正常工作,但是我在寻找方法上遇到了麻烦。

The functionality would work like this: With a scene containing many objects, the user rotates the camera with the mouse to some arbitrary position and then clicks a "Zoom All" button. 该功能将按以下方式工作:在包含许多对象的场景中,用户用鼠标将摄像机旋转到任意位置,然后单击“全部缩放”按钮。 Then, the camera is zoomed(moved) out or in(keeping FOV constant) to make all objects fit to the screen. 然后,将摄像机放大(移动)或放大(保持FOV恒定)以使所有对象适合屏幕。

How about rotate and translate the whole szene, so that the camera lies on an axis looking at the origin. 如何旋转和平移整个场景,使相机位于观察原点的轴上。

From there get the bounding box, calculate the needed distance for the camera and translate the camera. 从那里得到边界框,计算摄像机所需的距离并平移摄像机。

Then rotate and translate the whole szene back. 然后旋转并平移整个szene。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM