Skip to content

Instantly share code, notes, and snippets.

@ousttrue
Last active July 10, 2023 19:52
Show Gist options
  • Save ousttrue/09002101ac3fc0c2973f to your computer and use it in GitHub Desktop.
Save ousttrue/09002101ac3fc0c2973f to your computer and use it in GitHub Desktop.
draw camera frustum
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class CameraGizmoDrawer : MonoBehaviour
{
Camera m_camera;
void OnDrawGizmos()
{
if (m_camera == null)
{
m_camera = gameObject.GetComponent<Camera>();
}
Color tempColor = Gizmos.color;
Matrix4x4 tempMat = Gizmos.matrix;
if (this.m_camera.orthographic)
{
Camera c = m_camera;
var size = c.orthographicSize;
Gizmos.DrawWireCube(Vector3.forward * (c.nearClipPlane + (c.farClipPlane-c.nearClipPlane)/2)
, new Vector3(size * 2.0f, size * 2.0f * c.aspect, c.farClipPlane-c.nearClipPlane));
}
else
{
Camera c = m_camera;
Gizmos.matrix = Matrix4x4.TRS(this.transform.position, this.transform.rotation, Vector3.one);
Gizmos.DrawFrustum(Vector3.zero, c.fieldOfView, c.farClipPlane, c.nearClipPlane, c.aspect);
}
Gizmos.color = tempColor;
Gizmos.matrix = tempMat;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment