Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by ina · Feb 18, 2013 at 10:21 PM · cameraeditorfrustum

Draw Camera Frustum

Is there an editor gizmo to keep the camera frustum drawn even when the camera is not selected?

Comment
Add comment · Show 2
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 Benproductions1 · Feb 18, 2013 at 10:57 PM 1
Share

No, but you could write one :)

avatar image numberkruncher · Feb 19, 2013 at 03:58 AM 0
Share

You could implement one using the `Gizmos.DrawLine` function by creating and attaching a custom $$anonymous$$onoBehaviour to your camera using the `OnDrawGizmos` message handler. The camera component appears to implement the OnDrawGizmosSelected message.

2 Replies

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

Answer by KelNishi · Jun 19, 2013 at 02:10 PM

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Camera))]
 public class CameraFrustumGizmo : MonoBehaviour {
 
     public virtual void OnDrawGizmos() {
         Matrix4x4 temp = Gizmos.matrix;
         Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
         if (camera.orthographic) {
             float spread = camera.farClipPlane - camera.nearClipPlane;
             float center = (camera.farClipPlane + camera.nearClipPlane)*0.5f;
             Gizmos.DrawWireCube(new Vector3(0,0,center), new Vector3(camera.orthographicSize*2*camera.aspect, camera.orthographicSize*2, spread));
         } else {
             Gizmos.DrawFrustum(Vector3.zero, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);
         }
         Gizmos.matrix = temp;
     }
 }
Comment
Add comment · Show 9 · 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 whydoidoit · Jun 19, 2013 at 02:24 PM 1
Share

Nice one :)

avatar image Deeweext · Dec 11, 2013 at 06:06 PM 0
Share

This gives an incorrect view, somehow it's not exact.

avatar image mimminito · Jul 06, 2014 at 09:42 PM 0
Share

You should add this to the Unity Wiki

avatar image pjezek · Jul 09, 2014 at 12:48 AM 0
Share

camera.aspect is only set correct if the game tab is visilbe in Editor. I had to change the matrix like this:

     int screenWidth = 960, ScreenHeight = 640;
     ar = (float) screenWidth / (float) ScreenHeight;
     Gizmos.matrix = $$anonymous$$atrix4x4.TRS(camera.transform.position, camera.transform.rotation, new Vector3(1.0f, ar, 1.0f));
     Gizmos.DrawFrustum(Vector3.zero, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, ar);
 

 
avatar image Letail · Sep 11, 2018 at 03:58 PM 1
Share

Updated code, works with Unity 2018.2.6f1

 using UnityEngine;
 
 [RequireComponent(typeof(Camera))]
 public class CameraFrustumGizmo : $$anonymous$$onoBehaviour
 {
     public virtual void OnDrawGizmos()
     {
         $$anonymous$$atrix4x4 temp = Gizmos.matrix;
         Gizmos.matrix = $$anonymous$$atrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
         Camera camera = GetComponent<Camera>();
         if (camera.orthographic)
         {
             float spread = camera.farClipPlane - camera.nearClipPlane;
             float center = (camera.farClipPlane + camera.nearClipPlane) * 0.5f;
             Gizmos.DrawWireCube(new Vector3(0, 0, center), new Vector3(camera.orthographicSize * 2 * camera.aspect, camera.orthographicSize * 2, spread));
         }
         else
         {
             Gizmos.DrawFrustum(new Vector3(0, 0, (camera.nearClipPlane)), camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);
         }
         Gizmos.matrix = temp;
     }
 }
avatar image FiveXGames Letail · Dec 04, 2019 at 09:00 AM 0
Share

In 2019.3, for me, the gizmo starts to late (the near clip)

To solve it, I change the line: Gizmos.DrawFrustum(new Vector3(0, 0, (camera.nearClipPlane)), camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);

with: Gizmos.DrawFrustum(Vector3.zero, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);

Show more comments
avatar image
3

Answer by gianticristian · Oct 09, 2015 at 01:36 AM

 void OnDrawGizmos () {
         // Gizmo Frustum
         Gizmos.matrix = transform.localToWorldMatrix;           // For the rotation bug
         Gizmos.DrawFrustum(transform.position, Camera.main.fieldOfView, Camera.main.nearClipPlane, Camera.main.farClipPlane, Camera.main.aspect);
 }
Comment
Add comment · Show 5 · 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 Bunny83 · Oct 09, 2015 at 03:55 AM -1
Share

What's the point of this answer? The question got answered 2 years ago and you just posted the same thing...

avatar image gianticristian · Oct 09, 2015 at 11:26 AM 0
Share

This has less code, more clean. Is not just for you, a couple of guys ask me the same thing

avatar image coute · Apr 04, 2018 at 07:59 AM 0
Share

Shouldn't the near and far clip plane parameters be switched?

avatar image idbrii · Nov 20, 2018 at 11:17 PM 0
Share
  • For 5.6+, this is a better answer.

Also, if you just want a small frustum giving you the idea of what the camera sees, change Camera.main.farClipPlane to 1.

avatar image idbrii · Feb 05, 2019 at 10:30 PM 0
Share

I'm not sure what changed, but now I need a different first argument:

 Gizmos.DrawFrustum(Vector3.forward * cam.nearClipPlane, cam.fieldOfView, cam.nearClipPlane, 1, cam.aspect);

(I also cached the camera object to avoid repeated searches.)

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

23 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

Related Questions

Unity terrain and odd frustum culling behavior 0 Answers

Why does Viking Village Water move with camera in Editor? 0 Answers

Why my Roguelike 2D graphics are naughty in devices? 1 Answer

Dividng clip space by "w" warps the output? 1 Answer

Drawing Handles 1 Answer


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