Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Constant screen-size gizmos?

Discussion in 'Editor & General Support' started by duke, Oct 17, 2010.

  1. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    How do I go about ensuring, say, Gizmos.DrawWireCube is a fixed size relative to the viewport camera?
     
  2. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    I'm not sure what you want to do; gizmos are drawn in world space, so their size is going to change relative to how far away they are. If you want a gizmo to appear the same size on screen regardless of where it is in world space, you'll need to how far away the camera is and scale the gizmo accordingly.
     
  3. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    I know this is REALLY old, but it's still the first thing that comes up in Google when searching for screen size gizmos.

    HandleUtility.GetHandleSize(worldPos) does exactly what you want, but unfortunately it's only available in the editor...

    But then I realized I could just use ILSpy to check out what GetHandleSize does, and rewrote it to use Gizmos matrix, and now it works great. So here it is:


    Code (CSharp):
    1.  
    2.  
    3.  
    4.     public static float GetGizmoSize(Vector3 position)
    5.     {
    6.         Camera current = Camera.current;
    7.         position = Gizmos.matrix.MultiplyPoint(position);
    8.  
    9.         if (current)
    10.         {
    11.             Transform transform = current.transform;
    12.             Vector3 position2 = transform.position;
    13.             float z = Vector3.Dot(position - position2, transform.TransformDirection(new Vector3(0f, 0f, 1f)));
    14.             Vector3 a = current.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(0f, 0f, z)));
    15.             Vector3 b = current.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(1f, 0f, z)));
    16.             float magnitude = (a - b).magnitude;
    17.             return 80f / Mathf.Max(magnitude, 0.0001f);
    18.         }
    19.  
    20.         return 20f;
    21.     }
    22.  
    23.  
     
    Last edited: Oct 31, 2014
    michi_b, Lunatix, a436t4ataf and 2 others like this.
  4. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629