Skip to content

Instantly share code, notes, and snippets.

@tronster
Created March 18, 2015 12:23
Show Gist options
  • Save tronster/9a5637f47798b7f25a74 to your computer and use it in GitHub Desktop.
Save tronster/9a5637f47798b7f25a74 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