- Home /
How convert to camera FOV from diagonal FOV?
Hey all. I need to set the camera's FOV to a certain value, but that value is given to me as the 'diagonal FOV'. And Unity's cameras FOV is 'vertical FOV'. So I need to do this conversion. I realize it means using the aspect ratio, right? or somehow getting the view frustum? Does anyone know the formula for this?
Yes, why is there something else I should be using? Camera.fieldOfView is vertical FOV. I need to convert a diagonal FOV (like, say 73 degrees) into what ever number that is in the vertical so I can set it, thus producing a 73 degree diagonal FOV on that camera.
Answer by Scribe · Dec 03, 2013 at 12:30 AM
I believe this should work, its just some trigonometry:
var diagonalFOV : float = 100;
private var camAspect : float;
private var diagonalRatio : float;
private var verticalFOV : float;
function Start () {
camAspect = camera.aspect;
diagonalRatio = Mathf.Sqrt(1 + Mathf.Pow(camAspect, 2));
verticalFOV = (Mathf.Atan(Mathf.Tan(diagonalFOV*(Mathf.Deg2Rad/2.0))/diagonalRatio))*Mathf.Rad2Deg*2;
camera.fieldOfView = verticalFOV;
}
I have tested this so it doesn't give errors though I'm unable to test if it is the correct output angle, so hopefully my maths is correct!
Scribe
Thanks! I'll check it against this thing I found: http://vrguy.blogspot.com/2013/04/converting-diagonal-field-of-view-and.html
Hmm. The numbers are sorta close but not matching for my 73 degree example and 4:3 ratio it should be closer to 43 degrees I think but this code gives me 47 degrees.
I'm not sure where the differences in values is co$$anonymous$$g from, I've made a picture to try to demonstrate my thought process and so others might be able to point out any errors, personally I can't see anything wrong with my calculations on a second look!
http://i1.$$anonymous$$us.com/igbWuV$$anonymous$$vDqEXb.png
from this I deduced that my code could be rewritten as
var diagonalFOV : float = 73;
private var camAspect : float;
function Start () {
camAspect = camera.aspect;
camera.fieldOfView = 2.0*$$anonymous$$athf.Atan($$anonymous$$athf.Tan(diagonalFOV*$$anonymous$$athf.Deg2Rad/2.0)/$$anonymous$$athf.Sqrt(1 + $$anonymous$$athf.Pow(camAspect, 2)))*$$anonymous$$athf.Rad2Deg;
}
though this gives the same answer my old code did
$$anonymous$$aybe you can find where I've gone wrong, or possibly the site you linked to is incorrect though thats probably less likely
Scribe
I worked it out and also came up with similar results. I believe the linked site is actually incorrect - he's taking the Pythagorean theorem and arbitrarily applying it to angles, which is a very naughty thing for him to do. It'll give approximations at certain ranges, but will start to differ significantly with higher values.
Doing some quick debugs in Unity I see that with a V FOV of 42 and an aspect ratio of 4:3 the diagonal FOV should be 65.22, not 70 like the site claims.
For a diagonal at 73 degrees and aspect ratio of 4:3, the V FOV should be 47.88021.