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?
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;
}
}
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;
}
}
}