Unity 3d镜头适配,分辨率aspect和fov的关系

屏幕分辨率(camera.aspect)和camera fov关系如上图

怎么根据不同分辨率算出对应的fov来适配

假设距离为distance = 1000 默认fov = 35

知道距离和角度算默认屏幕高度

double Height = 2.0 * distance * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);

这里算出对应的屏幕高度,以1920*1080分辨率为准,计算当前屏幕高度(aspect是camera.aspect

float currentHeight = (float)Height * (1920f / 1080f) / aspect;

知道屏幕高度和距离计算对应的角度,就是fov

float CurrentFov = 2 * Mathf.Atan(currentHeight  * 0.5f / distance ) * Mathf.Rad2Deg;

代码如下:

    public static float CameraView(float aspect,float fov)
    {
        double frustumHeight = 2.0 * 1000 * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
        float CurrentFov = 2 * Mathf.Atan((float)frustumHeight * (1920f / 1080f) / aspect * 0.5f / 1000) * Mathf.Rad2Deg;
        if(CurrentFov > fov)
        {
            return CurrentFov;
        }
        return fov;
    }

猜你喜欢

转载自blog.csdn.net/SnoopyNa2Co3/article/details/83893093