Skip to content

Instantly share code, notes, and snippets.

@kiranmaya
Forked from tronster/DrawCameraViewFrustrum.cs
Created November 1, 2015 20:27
Show Gist options
  • Save kiranmaya/3933c76f4e77e2386eb8 to your computer and use it in GitHub Desktop.
Save kiranmaya/3933c76f4e77e2386eb8 to your computer and use it in GitHub Desktop.
Unity3d Camera cage from camera to near plane in editor.
using UnityEngine;
using System.Collections;
// Camera cage from camera to near plane in editor.
public class DrawCameraViewFrustrum : MonoBehaviour
{
public Color clr = new Color(0.1f, 0.14f, 0.8f, 0.5f);
public void OnDrawGizmos()
{
Gizmos.color= clr;
float near = GetComponent<Camera>().nearClipPlane;
if ( GetComponent<Camera>().orthographic )
{
// Orthographic cage
Gizmos.DrawWireCube(
transform.position + new Vector3(0,0,near/2),
new Vector3(
(GetComponent<Camera>().orthographicSize*GetComponent<Camera>().aspect)*2,
GetComponent<Camera>().orthographicSize*2,
near));
}
else
{
// Perspective cage
float angleTan = Mathf.Tan( GetComponent<Camera>().fieldOfView );
Gizmos.DrawWireCube(
transform.position + new Vector3(0,0,near/2),
new Vector3(
(angleTan * near * GetComponent<Camera>().aspect)/2 ,
(angleTan * near)/2,
near));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment