Last active
July 10, 2023 19:52
draw camera frustum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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