如何调整three.js透视相机的视野以适应一个球体?

6
我有一个球位于摄像机前方,半径和距离都已知。如何调整摄像机的视场角(FOV),以在任意视口大小内完全适配摄像机和球?

Image of the angle I'm looking for

这个回答类似,但我想调整视野而不是相机距离。

1个回答

9
为了调整视场角以适应球体,我需要使用反三角函数来计算从距离球体到球体上最远可见点形成的三角形中的角度。
可以给出正确角度的三角形如下图所示: Image of the triangle that will give the correct angle
// to get the fov to fit the sphere into the camera
var vFOV = 2 * Math.asin(sphereRadius / distance);
// get the project's aspect ratio to calculate a horizontal fov
var aspect = this.width / this.height;
// more trig to calculate a horizontal fov, used to fit a sphere horizontally
var hFOV = 2 * Math.atan(Math.tan(vFOV / 2) / aspect);

这将给出弧度制的答案。将hFOV或vFOV乘以度数fov * (180 / Math.PI),并应用于camera.fov
我最初陷入了使用错误三角形的陷阱。正如这个答案所述,“出现这种情况是因为球体被裁剪了,就像你站在一个大球体旁边,你看不到它的“北极”。
不要这样做;下图显示了用于裁剪视图的错误三角形:image of the wrong triangle for a cropped view

我不需要解决这个问题,但我很喜欢你的解释和图示。 - Kieran F.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接