Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GothSeiDank · Apr 23, 2018 at 05:26 PM · cameraeditor

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:

screenshot of the problem

It vanishes with the gradient.

Secondary Question: Is it possible to always show the outline of the camera without having it selected?

unitycamera.png (103.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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; 
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GothSeiDank · Apr 23, 2018 at 06:38 PM 0
Share

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.

avatar image Hellium GothSeiDank · Apr 23, 2018 at 06:54 PM 0
Share

Script edited in order to draw a cube fitting the camera's viewport.

avatar image GothSeiDank Hellium · Apr 23, 2018 at 07:03 PM 0
Share

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 :).

avatar image
0

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


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

150 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Infinite Render Distance? 0 Answers

Camera.main.pixel incorrectly return scene view's size in OnDrawGizmos 0 Answers

[SOLVED] anyone had this before? "Editor Camera already has a render stack set... recursive rendering? UnityEditor.DockArea:OnGUI()" 2 Answers

C# Renderer.IsVisible counts when editor camera sees object 0 Answers

Best way to manage multiple camera angles? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges