How to draw Handles transparent occluded by geometry
Asked Answered
S

1

0

I’ve build a custom Editor for Setting Up specific Systems in our game world. I want my Handles to be transparent when occluded by any objects (especially my colliders to see if they intersect with the terrain).

Right now they’re just drawn on top of erverything else, whereas Unity’s BoxColliderEditor for example switches to draw transparent lines when the collider is stuck in or hidden by any geometry.

My OnSceneGUI is registered to SceneView.onSceneGUIDelegate and within that i call DrawCollider():

private void DrawCollider(BoxCollider col, bool selected) {

            Color clr = Handles.color;

            if (selected) {
                Handles.color = Color.magenta;
            }

            Vector3 center = col.center;
            Vector3 size = col.size;
            Vector3 minPos = center - size * 0.5f;
            Vector3 maxPos = center + size * 0.5f;

            Matrix4x4 matrix = Handles.matrix;
            Handles.matrix = Matrix4x4.TRS(col.transform.position, col.transform.rotation, Vector3.one);

            //Draw Box
            Handles.DrawWireCube(center, size);

            if (selected) {

                Vector3 topRight = this.DrawMovementHandler(new Vector3(maxPos.x, center.y + size.y * 0.5f, maxPos.z));
                Vector3 botLeft = this.DrawMovementHandler(new Vector3(minPos.x, center.y - size.y * 0.5f, minPos.z));

                if (topRight.z > botLeft.z && topRight.x > botLeft.x && topRight.y > botLeft.y) {

                    col.center = (topRight + botLeft) * 0.5f;
                    col.size = topRight - botLeft;

                }
            }

            Handles.matrix = matrix;
            Handles.color = clr;
        }

I just don’t understand how Unity’s BoxColliders are drawn differently. As far as i figured out Unity is also just calling Handles.DrawWireCube internally in it’s OnSceneGUI Method (from BoxEditor.cs):

      Color color = Handles.color;
      Handles.color = boxColor;
      Vector3 minPos = center - size * 0.5f;
      Vector3 maxPos = center + size * 0.5f;
      Matrix4x4 matrix = Handles.matrix;
      Handles.matrix = transform;
      int hotControl = GUIUtility.hotControl;
      if (!handlesOnly)
        Handles.DrawWireCube(center, size);
      Vector3 point = transform.inverse.MultiplyPoint(Camera.current.transform.position);
      bool isCameraInsideBox = new Bounds(center, size).Contains(point);
      Handles.color = midPointHandleColor;
      this.MidpointHandles(ref minPos, ref maxPos, Handles.matrix, isCameraInsideBox);
      if (hotControl != GUIUtility.hotControl && GUIUtility.hotControl != 0)
        this.m_HandleControlID = GUIUtility.hotControl;
      bool changed = GUI.changed;
      if (changed)
      {
        center = (maxPos + minPos) * 0.5f;
        size = maxPos - minPos;
      }
      Handles.color = color;
      Handles.matrix = matrix;

So i was just wondering where the magic happens and how the depth value is conscidered for drawing these handles.

Unity Box:

My Box:

Sikorski answered 12/4 at 14:5 Comment(0)
I
0

try this :wink:

Iago answered 14/7 at 14:42 Comment(1)

something like this might helps. but i’m not on my computer right now so it’s not realy a optimized and relieable answer. just consider about override allready created asset at that path. have a look what options there are to save an asset in the assets folder

Bucharest

© 2022 - 2024 — McMap. All rights reserved.