屏幕分辨率(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;
}