- Home /
Draw Camera Frustum
Is there an editor gizmo to keep the camera frustum drawn even when the camera is not selected?
You could implement one using the `Gizmos.DrawLine` function by creating and attaching a custom $$anonymous$$onoBehaviour
to your camera using the `OnDrawGizmos` message handler. The camera component appears to implement the OnDrawGizmosSelected
message.
Answer by KelNishi · Jun 19, 2013 at 02:10 PM
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Camera))]
public class CameraFrustumGizmo : MonoBehaviour {
public virtual void OnDrawGizmos() {
Matrix4x4 temp = Gizmos.matrix;
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
if (camera.orthographic) {
float spread = camera.farClipPlane - camera.nearClipPlane;
float center = (camera.farClipPlane + camera.nearClipPlane)*0.5f;
Gizmos.DrawWireCube(new Vector3(0,0,center), new Vector3(camera.orthographicSize*2*camera.aspect, camera.orthographicSize*2, spread));
} else {
Gizmos.DrawFrustum(Vector3.zero, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);
}
Gizmos.matrix = temp;
}
}
camera.aspect is only set correct if the game tab is visilbe in Editor. I had to change the matrix like this:
int screenWidth = 960, ScreenHeight = 640;
ar = (float) screenWidth / (float) ScreenHeight;
Gizmos.matrix = $$anonymous$$atrix4x4.TRS(camera.transform.position, camera.transform.rotation, new Vector3(1.0f, ar, 1.0f));
Gizmos.DrawFrustum(Vector3.zero, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, ar);
Updated code, works with Unity 2018.2.6f1
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class CameraFrustumGizmo : $$anonymous$$onoBehaviour
{
public virtual void OnDrawGizmos()
{
$$anonymous$$atrix4x4 temp = Gizmos.matrix;
Gizmos.matrix = $$anonymous$$atrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
Camera camera = GetComponent<Camera>();
if (camera.orthographic)
{
float spread = camera.farClipPlane - camera.nearClipPlane;
float center = (camera.farClipPlane + camera.nearClipPlane) * 0.5f;
Gizmos.DrawWireCube(new Vector3(0, 0, center), new Vector3(camera.orthographicSize * 2 * camera.aspect, camera.orthographicSize * 2, spread));
}
else
{
Gizmos.DrawFrustum(new Vector3(0, 0, (camera.nearClipPlane)), camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);
}
Gizmos.matrix = temp;
}
}
In 2019.3, for me, the gizmo starts to late (the near clip)
To solve it, I change the line: Gizmos.DrawFrustum(new Vector3(0, 0, (camera.nearClipPlane)), camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);
with: Gizmos.DrawFrustum(Vector3.zero, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);
Answer by gianticristian · Oct 09, 2015 at 01:36 AM
void OnDrawGizmos () {
// Gizmo Frustum
Gizmos.matrix = transform.localToWorldMatrix; // For the rotation bug
Gizmos.DrawFrustum(transform.position, Camera.main.fieldOfView, Camera.main.nearClipPlane, Camera.main.farClipPlane, Camera.main.aspect);
}
What's the point of this answer? The question got answered 2 years ago and you just posted the same thing...
This has less code, more clean. Is not just for you, a couple of guys ask me the same thing
Shouldn't the near and far clip plane parameters be switched?
For 5.6+, this is a better answer.
Also, if you just want a small frustum giving you the idea of what the camera sees, change Camera.main.farClipPlane
to 1
.
I'm not sure what changed, but now I need a different first argument:
Gizmos.DrawFrustum(Vector3.forward * cam.nearClipPlane, cam.fieldOfView, cam.nearClipPlane, 1, cam.aspect);
(I also cached the camera object to avoid repeated searches.)