一直以為Unity中的相機FOV指的是frustum兩個對角邊的方向夾角,所以在看一篇教程的時候怎么算都算不對。后來靈機一動,查了一下,才發現Unity中的Fov指的是垂直方向的FOV:
參見這里:https://docs.unity3d.com/ScriptReference/Camera-fieldOfView.html
This is the vertical field of view; horizontal FOV varies depending on the viewport's aspect ratio.
圖示如下:
圖片來源:http://rungame.me/blog/2014/05/12/u3d-projection/
附上我看的教程的部分代碼,用於計算frustum的四個邊:
private Matrix4x4 GetFrustumCorners(Camera cam) { float camFov = cam.fieldOfView; float camAspect = cam.aspect; Matrix4x4 frustumCorners = Matrix4x4.identity; float fovWHalf = camFov * 0.5f; float tan_fov = Mathf.Tan(fovWHalf * Mathf.Deg2Rad); Vector3 toTop = Vector3.up * tan_fov; Vector3 toRight = Vector3.right * tan_fov * camAspect; Vector3 topLeft = (-Vector3.forward - toRight + toTop); Vector3 topRight = (-Vector3.forward + toRight + toTop); Vector3 bottomRight = (-Vector3.forward + toRight - toTop); Vector3 bottomLeft = (-Vector3.forward - toRight - toTop); frustumCorners.SetRow(0, topLeft); frustumCorners.SetRow(1, topRight); frustumCorners.SetRow(2, bottomRight); frustumCorners.SetRow(3, bottomLeft); return frustumCorners; }