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;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章