#if (UNITY_EDITOR) using System.Collections.Generic; using UnityEngine; using UnityEditor; namespace ECE { /// /// Used to draw gizmos for selected / hovered vertices /// Gizmos draw significantly faster than handles. /// [System.Serializable] public class EasyColliderGizmos : MonoBehaviour { public float DensityScale = 0.0f; public bool UseDensityScale = false; public float CommonScale = 1.0f; /// /// Should all valid vertices be displayed /// public bool DisplayAllVertices = false; /// /// Color to display all valid vertices with /// public Color DisplayVertexColor = Color.blue; /// /// List of all valid vertex positions in world space /// public HashSet DisplayVertexPositions = new HashSet(); /// /// Scale to display all valid vertex positions /// public float DisplayVertexScale = 0.05f; /// /// Should gizmos be drawn /// public bool DrawGizmos = true; /// /// Type of gizmo use when drawing gizmos /// public GIZMO_TYPE GizmoType = GIZMO_TYPE.SPHERE; /// /// Color of hovered vertices /// public Color HoveredVertexColor = Color.cyan; /// /// Set of hovered vertices in world space /// public HashSet HoveredVertexPositions = new HashSet(); /// /// Scale of hovered vertices /// public float HoveredVertexScale = 0.1f; /// /// Color of overlapped vertices /// public Color OverlapVertexColor = Color.red; /// /// Scale of overlapped vertices /// public float OverlapVertexScale = 0.125f; /// /// Color of selected vertices /// public Color SelectedVertexColor = Color.green; /// /// Set of selected vertices in world space /// public HashSet SelectedVertexPositions = new HashSet(); /// /// Scale of selected vertices /// public float SelectedVertexScale = 0.15f; /// /// Should HandleUtility.GetHandleSize be used for each vertice to maintain a fixed scale and ignore distance? /// public bool UseFixedGizmoScale = true; void OnDrawGizmos() { if (DrawGizmos) { // Keep track of gizmos color to reset at end Color original = Gizmos.color; // Selected vertices. // size is modified for each vertex if using fixed scaling from handle utility. Vector3 size = Vector3.zero; // original size is kept track of to make calculations easier. Vector3 originalSize = Vector3.zero; // scale for spheres. float scale = 0.0f; float originalScale = 0.0f; float handleSize = 0.0f; // Display all vertices. if (DisplayAllVertices) { Gizmos.color = DisplayVertexColor; originalScale = UseDensityScale ? DensityScale * CommonScale : DisplayVertexScale * CommonScale; originalSize = Vector3.one * originalScale; foreach (Vector3 vert in DisplayVertexPositions) { scale = originalScale; size = originalSize; if (UseFixedGizmoScale) { handleSize = HandleUtility.GetHandleSize(vert); scale *= handleSize; size *= handleSize; } DrawAGizmo(vert, size, scale, GizmoType); } } // Selected vertices Gizmos.color = SelectedVertexColor; originalScale = UseDensityScale ? DensityScale * CommonScale : SelectedVertexScale * CommonScale; originalSize = Vector3.one * originalScale; foreach (Vector3 vert in SelectedVertexPositions) { scale = originalScale; size = originalSize; if (UseFixedGizmoScale) { handleSize = HandleUtility.GetHandleSize(vert); scale *= handleSize; size *= handleSize; } DrawAGizmo(vert, size, scale, GizmoType); } // Hover vertices. Gizmos.color = HoveredVertexColor; originalScale = UseDensityScale ? DensityScale * CommonScale : HoveredVertexScale * CommonScale; originalSize = Vector3.one * originalScale; float originalScaleOverlap = UseDensityScale ? DensityScale * CommonScale : OverlapVertexScale * CommonScale; Vector3 originalSizeOverlap = Vector3.one * originalScaleOverlap; foreach (Vector3 vert in HoveredVertexPositions) { if (SelectedVertexPositions.Contains(vert)) { scale = originalScaleOverlap; size = originalSizeOverlap; if (UseFixedGizmoScale) { handleSize = HandleUtility.GetHandleSize(vert); scale *= handleSize; size *= handleSize; } Gizmos.color = OverlapVertexColor; DrawAGizmo(vert, size, scale, GizmoType); } else { scale = originalScale; size = originalSize; if (UseFixedGizmoScale) { handleSize = HandleUtility.GetHandleSize(vert); scale *= handleSize; size *= handleSize; } Gizmos.color = HoveredVertexColor; DrawAGizmo(vert, size, scale, GizmoType); } } Gizmos.color = original; } } /// /// Draws a gizmo of type at position at size or scale. /// /// World position to draw at /// Size of cube to draw /// Radius of sphere to draw /// Sphere or Cubes? private void DrawAGizmo(Vector3 position, Vector3 size, float scale, GIZMO_TYPE gizmoType) { switch (gizmoType) { case GIZMO_TYPE.SPHERE: Gizmos.DrawSphere(position, scale / 2); break; case GIZMO_TYPE.CUBE: Gizmos.DrawCube(position, size); break; } } /// /// Sets the set of selected vertices from a list of selected world vertices /// /// List of world vertex positions that are selected public void SetSelectedVertices(List worldVertices) { SelectedVertexPositions.Clear(); SelectedVertexPositions.UnionWith(worldVertices); } } } #endif