Search Unity

Calculate diagonal field of view

Discussion in 'Scripting' started by PieterAlbers, Jan 11, 2020.

  1. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    247
    Hi all,

    Can someone help me out with calculating the camera's diagonal field of view.

    I have the vertical (obviously ;)) and horizontal already but I need the diagonal one.

    I am using the default camera so no access to sensor size or focal length.

    -Pieter
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Check out the
    .fieldOfView
    property on the
    Camera
    object.

    Field of view in this particular Unity3D camera context refers to half the angle between your eye and the top and bottom of the frame, i.e., the vertical angle of view.

    Lateral angle of your frame is dependent on screen aspect, and that is given by the expression
    (float)Screen.width / Screen.height
    . That will give you the lateral angular field of view, given the vertical angle.

    The pythagorean theorem should therefore be able to give you the diagonal angle, based on vertical and lateral fields of view.
     
    Bunny83 likes this.
  3. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    247
    Thanks for your reply and apologies for not replying sooner - just got back behind the computer.

    I believe it is not as simple as you describe and I already gave that a try before posting my question - it is not giving me correct results. I should have been more clear on that.

    Calculating the horizontal field of view is done as stated here https://forum.unity.com/threads/how-to-calculate-horizontal-field-of-view.16114/

    I have some code that converts diagonal to vertical.

    I need something similar for diagonal fov - still messin with it
     
  4. Daniels118

    Daniels118

    Joined:
    May 18, 2021
    Posts:
    2
    The diagonal can be calclated using pythagorean theorem, but you have to apply it to the linear sizes of the screen (not FOVs) and then get back to an angle. These are the conceptual steps:
    Vertical FOV -> screen height -> screen width -> diagonal -> Diagonal FOV​
    In code:
    Code (CSharp):
    1. float radAngle = cam.fieldOfView * Mathf.Deg2Rad;
    2. float vTan = Mathf.Tan(radAngle / 2f);
    3. float hTan = vTan * cam.aspect;
    4. float dTan = Mathf.Sqrt(hTan * hTan + vTan * vTan);
    5. float radDFOV = 2f * Mathf.Atan(dTan);
    6. diagonalFOV = Mathf.Rad2Deg * radDFOV;
     
    Bunny83 likes this.