Search Unity

Gizmos.DrawFrustum does not match standard camera (BUG?)

Discussion in 'Scripting' started by DrDecipher, Aug 13, 2014.

  1. DrDecipher

    DrDecipher

    Joined:
    Dec 5, 2012
    Posts:
    54
    If you attach a Gizmos.DrawFrustum to a camera it does not match the default camera. The horizontal FOV is the same but the height is not. If you set the aspect in the game view to 1:1 then they line up, but anything else and the gizmo is verticly to small.

    Code (CSharp):
    1. Gizmos.DrawFrustum(new Vector3(0, 0, 0), gameObject.camera.fieldOfView, 10, gameObject.camera.nearClipPlane, gameObject.camera.pixelWidth / gameObject.camera.pixelHeight);
    2.  
    3. Gizmos.DrawFrustum(new Vector3(0, 0, 0), gameObject.camera.fieldOfView, 10, gameObject.camera.nearClipPlane, gameObject.camera.[URL='http://docs.unity3d.com/ScriptReference/Camera-aspect.html']aspect[/URL]);
    Thoughts?
    Doc
     
  2. Anxo

    Anxo

    Joined:
    Jan 7, 2014
    Posts:
    13
    Did you find a solution to this problem? I am trying the same.

    Code (CSharp):
    1.     //Matrix4x4 rotationMatrix = Matrix4x4.TRS (transform.position, transform.rotation, transform.lossyScale);
    2.                         Matrix4x4 rotationMatrix = transform.localToWorldMatrix;
    3.                         Gizmos.matrix = rotationMatrix;
    4.                        
    5.                         Gizmos.DrawFrustum (transform.position, camera.fieldOfView, Vector3.Distance (transform.position, target.transform.position), minRange, camera.aspect);
    6.                         //Gizmos.DrawFrustum (transform.position, fov, maxRange, minRange, aspect);
     
  3. DrDecipher

    DrDecipher

    Joined:
    Dec 5, 2012
    Posts:
    54
    I just wrote my own.

    The following code will return a rect. The Rect is it the four corners of the rectangle in world space. Clockwise UL,UR,DR,DL.

    Code (CSharp):
    1.    
    2. public static Rectangle CalculateCameraFrustRect(Camera cam, float dist)
    3.     {
    4.         Rectangle rect = new Rectangle();
    5.         float frustumHeight = 2.0F * dist * Mathf.Tan(cam.fieldOfView * 0.5F * Mathf.Deg2Rad);
    6.         rect.p1 = new Vector3(-frustumHeight * cam.aspect / 2, frustumHeight / 2, dist);
    7.         rect.p2 = new Vector3(frustumHeight * cam.aspect / 2, frustumHeight / 2, dist);
    8.         rect.p3 = new Vector3(frustumHeight * cam.aspect / 2, -frustumHeight / 2, dist);
    9.         rect.p4 = new Vector3(-frustumHeight * cam.aspect / 2, -frustumHeight / 2, dist);
    10.  
    11.         return (rect);
    12.     }


    Use this to draw it...
    Code (CSharp):
    1.  
    2.                 Gizmos.color = whiteLine;
    3.  
    4.                 Rectangle far = CalculateCameraFrustRect(camera, camera.farClipPlane);
    5.                 Rectangle near = CalculateCameraFrustRect(camera, camera.nearClipPlane);
    6.  
    7.                 Gizmos.DrawLine(far.p1, far.p2);
    8.                 Gizmos.DrawLine(far.p2, far.p3);
    9.                 Gizmos.DrawLine(far.p3, far.p4);
    10.                 Gizmos.DrawLine(far.p4, far.p1);
    11.                 Gizmos.DrawLine(near.p1, near.p2);
    12.                 Gizmos.DrawLine(near.p2, near.p3);
    13.                 Gizmos.DrawLine(near.p3, near.p4);
    14.                 Gizmos.DrawLine(near.p4, near.p1);
    15.                 Gizmos.DrawLine(near.p1, far.p1);
    16.                 Gizmos.DrawLine(near.p2, far.p2);
    17.                 Gizmos.DrawLine(near.p3, far.p3);
    18.                 Gizmos.DrawLine(near.p4, far.p4);
    19.  
    Hope this helps. Let me know if you have any problems.
    Doc
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Do this
    Code (CSharp):
    1.  
    2.         Matrix4x4 m = transform.localToWorldMatrix;
    3.         Matrix4x4 m2 = Matrix4x4.identity;
    4.         m2[1, 1] *= camera.aspect;
    5.         Gizmos.matrix = m * m2;
    6.         Gizmos.DrawFrustum( transform.position, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect );
    7.  
    Works, as long as you don't scale the camera (which has no effect anyway, so why)
     
  5. DrDecipher

    DrDecipher

    Joined:
    Dec 5, 2012
    Posts:
    54
    hpJohn,

    Can please you explain this a little? I think this may be the same issue I was having with Camera.WorldToScreenPoint.

    Thanks
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    No.
    It was trial and error.

    I was pretty sure that the problem wasn't in the actual Gizmos.DrawFrustum call, because I tried multiplying the values by arbitrary amounts, and no combination seemed to get close (or just the right kind of wrong to know what to fix), so that just left the gizmo matrix.

    Unlikely, WorldToScreenPoint works fine without the need for trickery.