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
0
Question by AndrewRH · Jul 27, 2012 at 12:33 PM · gizmosgizmofrustum

Scene view stretches camera frustum gizmo

Hi,

I am drawing my own camera frustums using Gizmos because I want camera frustums to always be visible, not just when I select the camera object.

The problem is that when I resize the 'scene' window the gizmo stretches as if it's trying to match the aspect ratio of the scene view. The Unity camera frustum however doesn't stretch like this.

Attached is my code and screenshots. Thanks!

 using UnityEngine; 
 using System.Collections;
 
 [RequireComponent(typeof(Camera))]
 public class ShowCameraFrustum : MonoBehaviour 
 {
  void OnDrawGizmos()
  {
  // Top left
  Vector3 tlN = this.camera.ScreenToWorldPoint(new Vector3(Screen.width*0, Screen.height, this.camera.nearClipPlane));
  Vector3 tlF = this.camera.ScreenToWorldPoint(new Vector3(Screen.width*0, Screen.height, this.camera.farClipPlane));
 
  // Top right
  Vector3 trN = this.camera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, this.camera.nearClipPlane));
  Vector3 trF = this.camera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, this.camera.farClipPlane));
  
  // Bottom left
  Vector3 blN = this.camera.ScreenToWorldPoint(new Vector3(0.0f, 0.0f, this.camera.nearClipPlane));
  Vector3 blF = this.camera.ScreenToWorldPoint(new Vector3(0.0f, 0.0f, this.camera.farClipPlane));
  
  // Bottom right
  Vector3 brN = this.camera.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, this.camera.nearClipPlane));
  Vector3 brF = this.camera.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, this.camera.farClipPlane));
  
  Gizmos.color = Color.white;
  
  // Near
  Gizmos.DrawLine(tlN, trN);
  Gizmos.DrawLine(trN, brN);
  Gizmos.DrawLine(brN, blN);
  Gizmos.DrawLine(blN, tlN);
  
  // Far
  Gizmos.DrawLine(tlF, trF);
  Gizmos.DrawLine(trF, brF);
  Gizmos.DrawLine(brF, blF);
  Gizmos.DrawLine(blF, tlF);
 
  // Sides
  Gizmos.DrawLine(tlN, tlF);
  Gizmos.DrawLine(trN, trF);
  Gizmos.DrawLine(brN, brF);
  Gizmos.DrawLine(blN, blF);
  }
 }

alt text alt text

frustum1.png (29.3 kB)
frustum2.png (22.7 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

4 Replies

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

Answer by AndrewRH · Jul 27, 2012 at 05:46 PM

SOLVED

Using the code from here to get the size of the game viewport: http://answers.unity3d.com/questions/179775/game-window-size-from-editor-window-in-editor-mode.html

Naturally as this is using reflection and undocumented 'features' it's a bit of a hack but it's good enough for me :) Hope this little script is usesome to someone :)

Here's the final code:

 using UnityEngine; 
 using System.Collections;
 
 [RequireComponent(typeof(Camera))]
 public class ShowCameraFrustum : MonoBehaviour 
 {
  public static Vector2 GetMainGameViewSize()
  {
      System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
      System.Reflection.MethodInfo GetSizeOfMainGameView = T.GetMethod("GetSizeOfMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
      System.Object Res = GetSizeOfMainGameView.Invoke(null,null);
      return (Vector2)Res;
  }
  
  void OnDrawGizmos()
  {
  Camera cam = this.camera;
  
  // Top left
  Vector3 tlN = cam.ScreenToWorldPoint(new Vector3(Screen.width*0, Screen.height, cam.nearClipPlane));
  Vector3 tlF = cam.ScreenToWorldPoint(new Vector3(Screen.width*0, Screen.height, cam.farClipPlane));
  
  // Top right
  Vector3 trN = cam.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, cam.nearClipPlane));
  Vector3 trF = cam.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, cam.farClipPlane));
  
  // Bottom left
  Vector3 blN = cam.ScreenToWorldPoint(new Vector3(0.0f, 0.0f, cam.nearClipPlane));
  Vector3 blF = cam.ScreenToWorldPoint(new Vector3(0.0f, 0.0f, cam.farClipPlane));
  
  // Bottom right
  Vector3 brN = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, cam.nearClipPlane));
  Vector3 brF = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, cam.farClipPlane));
  
  // Scale for aspect ratio
  Vector2 gameViewsize = GetMainGameViewSize();
  float gameViewAspect = gameViewsize.x / gameViewsize.y;
  float s = gameViewAspect / cam.aspect; 
  tlN.x *= s;
  tlF.x *= s;
  trN.x *= s;
  trF.x *= s;
  blN.x *= s;
  blF.x *= s;
  brN.x *= s;
  brF.x *= s;
  
  Gizmos.color = Color.white;
  
  // Near
  Gizmos.DrawLine(tlN, trN);
  Gizmos.DrawLine(trN, brN);
  Gizmos.DrawLine(brN, blN);
  Gizmos.DrawLine(blN, tlN);
  
  // Far
  Gizmos.DrawLine(tlF, trF);
  Gizmos.DrawLine(trF, brF);
  Gizmos.DrawLine(brF, blF);
  Gizmos.DrawLine(blF, tlF);
 
  // Sides
  Gizmos.DrawLine(tlN, tlF); 
  Gizmos.DrawLine(trN, trF);
  Gizmos.DrawLine(brN, brF);
  Gizmos.DrawLine(blN, blF);
  }
 }


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
avatar image
1

Answer by Bunny83 · Jul 27, 2012 at 02:00 PM

Don't use Screen.width / .height. Use Camera.ViewportToWorldPoint with normalized coordinates instead (0.0 - 1.0).

Btw, i'm not sure if Unity will use the size of the scene view for all cameras, in this case, there's not much you can do about that. If not, make sure you keep your game view at the same size. Each camera as an aspect ratio and it's always calculated from the rendertarget.

You might want to set a fix aspect ratio for your camera.

Comment
Add comment · Show 1 · 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 AndrewRH · Jul 27, 2012 at 04:02 PM 0
Share

I trid Camera.ViewporttoWorldPoint - same thing It seems the aspect ratio of the camera changes when I resize the 'scene' window even though game window size remains static...weird!

avatar image
0

Answer by AndrewRH · Jul 27, 2012 at 04:38 PM

UPDATE

It seems the the aspect ratio set in the 'game' view affects the camera frustum display in Unity. I can't seem to find a way to get this aspect ratio though...

I found a fix that works:

 float gameAspect = 1280.0f / 720.0f;  // hardcoded for now
 float scale = gameAspect / camera.aspect;
 tlN.x *= scale;
 tlF.x *= scale;

...

So this scales the X component for all the points in the code above. This works well (in this case if you 'game' view is set to resolution 1280:720 or 16:9 aspect. It's possible to set this resolution to 'free' or 4:3 which breaks it though, so I guess this is partial fix.

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
avatar image
0

Answer by SamHagelund · Nov 27, 2012 at 03:39 PM

put this on a camera script

void OnDrawGizmos() { Gizmos.matrix = transform.localToWorldMatrix; Gizmos.DrawFrustum( transform.position, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, 1 ); }

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

7 People are following this question.

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

Related Questions

Select object by selecting gizmo or handle? 1 Answer

Make hidden gizmos invisible 0 Answers

Component on SceneView camera has wrong gameObject In OnDrawGizmos. 0 Answers

Gizmos at RunTime 1 Answer

Controlling OnDrawGizmo 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