欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity3D 中的FOV

程序员文章站 2022-07-12 23:35:24
...

一直以为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. 

  图示如下:

Unity3D 中的FOV

图片来源: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;
    }