- Home /
Is there a way to tint the outline of the camera in the Editor?
While making 2D stuff and when you use very bright backgrounds, it is very hard to see the outline of the camera viewport, if not impossible. Is there any way to tint that outline of the camera?
Here is what I mean:
It vanishes with the gradient.
Secondary Question: Is it possible to always show the outline of the camera without having it selected?
Answer by Hellium · Apr 23, 2018 at 06:20 PM
Attach the following script to your camera:
using UnityEngine ;
[RequireComponent( typeof( Camera ) ) ]
public class CameraGizmos : MonoBehaviour
{
[SerializeField, HideInInspector]
new private Camera camera ;
[SerializeField, HideInInspector]
private Texture2D viewportTexture ;
[SerializeField]
private Color frustumColor = Color.red ;
[SerializeField]
private Color viewportColor = Color.red ;
[SerializeField]
private bool alwaysDrawGizmos = true;
private Vector3[] frustumCorners = new Vector3[4];
private Rect viewport = new Rect(0, 0, 1, 1);
public Camera Camera
{
get
{
if (camera == null)
camera = GetComponent<Camera>();
return camera ;
}
}
public Texture2D ViewportTexture
{
get
{
if (viewportTexture == null)
viewportTexture = new Texture2D(1,1);
return viewportTexture ;
}
}
void OnDrawGizmos()
{
if( alwaysDrawGizmos )
DrawCameraGizmos( Camera );
}
void OnDrawGizmosSelected()
{
if( !alwaysDrawGizmos )
DrawCameraGizmos( Camera );
}
public void DrawCameraGizmos( Camera camera )
{
// Save gizmos settings
Color color = Gizmos.color ;
Matrix4x4 matrix = Gizmos.matrix ;
// Set gizmos matrix
Gizmos.matrix = Matrix4x4.TRS(camera.transform.position, camera.transform.rotation, Vector3.one);
// Compute viewport dimensions
Gizmos.color = viewportColor ;
camera.CalculateFrustumCorners(viewport, camera.farClipPlane, Camera.MonoOrStereoscopicEye.Mono, frustumCorners);
float width = Vector3.Distance( frustumCorners[2], frustumCorners[1] ) ;
float height = Vector3.Distance( frustumCorners[1], frustumCorners[0] ) ;
// Draw viewport using texture
ViewportTexture.SetPixel(0, 0, viewportColor);
ViewportTexture.Apply();
Gizmos.DrawGUITexture(new Rect(transform.position.x - width * 0.5f, transform.position.y - height * 0.5f, width, height), ViewportTexture );
// Draw frustum
Gizmos.color = frustumColor ;
Gizmos.DrawFrustum(Vector3.forward * camera.nearClipPlane, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect );
// Restore gizmos settings
Gizmos.color = color ;
Gizmos.matrix = matrix;
}
}
Hey Helium!
Thanks for the script, but that only tints the frustum. I need the viewport tinted, as shown in the screenshot. I have tried various things of getting that rect from the camera, but without any result so far.
Script edited in order to draw a cube fitting the camera's viewport.
Thank you so much. Still struggling a bit with all the math. I've edited to be closer to the camera, by using the near clip plane to draw the cube and set Alpha to 80. Perfect now :).
Answer by GothSeiDank · Apr 23, 2018 at 07:24 PM
I have edited @Hellium's script to be able to dynamically set the position of the cube within the view port plane and to be able to toggle the frustum and cube independently. I have also resolved one warning.
[RequireComponent(typeof(Camera))]
public class CameraGizmos : MonoBehaviour
{
[SerializeField, HideInInspector]
private new Camera camera;
[SerializeField]
private float cubePosition = 5.0f;
[SerializeField]
private Color frustumColor = Color.red;
[SerializeField]
private Color viewportColor = new Color(200, 0, 0, 80);
[SerializeField]
private bool alwaysDrawGizmos = true;
[SerializeField]
private bool alwaysDrawFrustum = false;
[SerializeField]
private bool alwaysDrawCube = true;
private Vector3[] frustumCorners = new Vector3[4];
private Rect viewport = new Rect(0, 0, 1, 1);
public Camera Camera
{
get
{
if (camera == null)
camera = GetComponent<Camera>();
return camera;
}
}
void OnDrawGizmos()
{
if (alwaysDrawGizmos)
DrawCameraGizmos(Camera);
}
void OnDrawGizmosSelected()
{
if (!alwaysDrawGizmos)
DrawCameraGizmos(Camera);
}
public void DrawCameraGizmos(Camera camera)
{
if (alwaysDrawGizmos)
{
// Save gizmos settings
Color color = Gizmos.color;
Matrix4x4 matrix = Gizmos.matrix;
// Set gizmos matrix
Gizmos.matrix = Matrix4x4.TRS(camera.transform.position, camera.transform.rotation, Vector3.one);
if (alwaysDrawCube)
{
// Draw viewport
Gizmos.color = viewportColor;
camera.CalculateFrustumCorners(viewport, camera.farClipPlane, Camera.MonoOrStereoscopicEye.Mono, frustumCorners);
float width = Vector3.Distance(frustumCorners[2], frustumCorners[1]);
float height = Vector3.Distance(frustumCorners[1], frustumCorners[0]);
Gizmos.DrawCube(Vector3.forward * (camera.nearClipPlane + cubePosition), new Vector3(width, height, 0.01f));
}
// Draw frustum
if (alwaysDrawFrustum)
{
Gizmos.color = frustumColor;
Gizmos.DrawFrustum(Vector3.forward * camera.nearClipPlane, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);
}
// Restore gizmos settings
Gizmos.color = color;
Gizmos.matrix = matrix;
}
}
}