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.
Gizmos.DrawFrustum(new Vector3(0, 0, 0), gameObject.camera.fieldOfView, 10, gameObject.camera.nearClipPlane, gameObject.camera.pixelWidth / gameObject.camera.pixelHeight);
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
Anxo
September 24, 2014, 3:38pm
2
Did you find a solution to this problem? I am trying the same.
//Matrix4x4 rotationMatrix = Matrix4x4.TRS (transform.position, transform.rotation, transform.lossyScale);
Matrix4x4 rotationMatrix = transform.localToWorldMatrix;
Gizmos.matrix = rotationMatrix;
Gizmos.DrawFrustum (transform.position, camera.fieldOfView, Vector3.Distance (transform.position, target.transform.position), minRange, camera.aspect);
//Gizmos.DrawFrustum (transform.position, fov, maxRange, minRange, aspect);
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.
public static Rectangle CalculateCameraFrustRect(Camera cam, float dist)
{
Rectangle rect = new Rectangle();
float frustumHeight = 2.0F * dist * Mathf.Tan(cam.fieldOfView * 0.5F * Mathf.Deg2Rad);
rect.p1 = new Vector3(-frustumHeight * cam.aspect / 2, frustumHeight / 2, dist);
rect.p2 = new Vector3(frustumHeight * cam.aspect / 2, frustumHeight / 2, dist);
rect.p3 = new Vector3(frustumHeight * cam.aspect / 2, -frustumHeight / 2, dist);
rect.p4 = new Vector3(-frustumHeight * cam.aspect / 2, -frustumHeight / 2, dist);
return (rect);
}
Use this to draw it…
Gizmos.color = whiteLine;
Rectangle far = CalculateCameraFrustRect(camera, camera.farClipPlane);
Rectangle near = CalculateCameraFrustRect(camera, camera.nearClipPlane);
Gizmos.DrawLine(far.p1, far.p2);
Gizmos.DrawLine(far.p2, far.p3);
Gizmos.DrawLine(far.p3, far.p4);
Gizmos.DrawLine(far.p4, far.p1);
Gizmos.DrawLine(near.p1, near.p2);
Gizmos.DrawLine(near.p2, near.p3);
Gizmos.DrawLine(near.p3, near.p4);
Gizmos.DrawLine(near.p4, near.p1);
Gizmos.DrawLine(near.p1, far.p1);
Gizmos.DrawLine(near.p2, far.p2);
Gizmos.DrawLine(near.p3, far.p3);
Gizmos.DrawLine(near.p4, far.p4);
Hope this helps. Let me know if you have any problems.
Doc
hpjohn
September 30, 2014, 10:44am
4
Do this
Matrix4x4 m = transform.localToWorldMatrix;
Matrix4x4 m2 = Matrix4x4.identity;
m2[1, 1] *= camera.aspect;
Gizmos.matrix = m * m2;
Gizmos.DrawFrustum( transform.position, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect );
Works, as long as you don’t scale the camera (which has no effect anyway, so why)
hpjohn:
Do this
Matrix4x4 m = transform.localToWorldMatrix;
Matrix4x4 m2 = Matrix4x4.identity;
m2[1, 1] *= camera.aspect;
Gizmos.matrix = m * m2;
Gizmos.DrawFrustum( transform.position, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect );
Works, as long as you don’t scale the camera (which has no effect anyway, so why)
hpJohn,
Can please you explain this a little? I think this may be the same issue I was having with Camera.WorldToScreenPoint.
Thanks
hpjohn
October 1, 2014, 1:15am
6
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.