#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, ISerializationCallbackReceiver { #region preference settings private EasyColliderPreferences ECEPreferences { get { return EasyColliderPreferences.Preferences; } } public float CommonScale { get { return ECEPreferences.CommonScalingMultiplier; } } public float DefaultScale { get { return ECEPreferences.DefaultScale; } } public bool DisplayAllVertices { get { return ECEPreferences.DisplayAllVertices; } } public Color DisplayVertexColor { get { return ECEPreferences.DisplayVerticesColour; } } public GIZMO_TYPE GizmoType { get { return ECEPreferences.GizmoType; } } public Color HoveredVertexColor { get { return ECEPreferences.HoverVertColour; } } public Color OverlapVertexColor { get { return ECEPreferences.OverlapSelectedVertColour; } } public Color SelectedVertexColor { get { return ECEPreferences.SelectedVertColour; } } public bool UseFixedGizmoScale { get { return ECEPreferences.UseFixedGizmoScale; } } #endregion /// /// calculated density scale if use density scale is enabled. /// public float DensityScale = 0.0f; /// /// List of all valid vertex positions in world space /// public HashSet DisplayVertexPositions = new HashSet(); /// /// Should gizmos be drawn /// public bool DrawGizmos = true; /// /// Set of hovered vertices in world space /// public HashSet HoveredVertexPositions = new HashSet(); /// /// Set of selected vertices in world space /// public HashSet SelectedVertexPositions = new HashSet(); float GetScale() { // *10 makes it more equivalent to how the shader is drawn. float scale = DefaultScale * CommonScale * 10; if (scale <= 0.0f) { scale = ECEPreferences.DefaultScale * 10; } return scale; } void OnDrawGizmos() { if (DrawGizmos) { // Keep track of gizmos color to reset at end Color original = Gizmos.color; // Selected vertices. // scale for spheres. float scale = GetScale(); // size for cubes Vector3 size = Vector3.one * scale; // default scaling of 1.0f float handleSize = 1.0f; // Display all vertices. if (DisplayAllVertices) { Gizmos.color = DisplayVertexColor; foreach (Vector3 vert in DisplayVertexPositions) { if (UseFixedGizmoScale) { handleSize = HandleUtility.GetHandleSize(vert); } DrawAGizmo(vert, size * handleSize, scale * handleSize, GizmoType); } } // Selected vertices Gizmos.color = SelectedVertexColor; foreach (Vector3 vert in SelectedVertexPositions) { if (UseFixedGizmoScale) { handleSize = HandleUtility.GetHandleSize(vert); } DrawAGizmo(vert, size * handleSize, scale * handleSize, GizmoType); } // Hover vertices. Gizmos.color = HoveredVertexColor; foreach (Vector3 vert in HoveredVertexPositions) { if (SelectedVertexPositions.Contains(vert)) { if (UseFixedGizmoScale) { handleSize = HandleUtility.GetHandleSize(vert); } Gizmos.color = OverlapVertexColor; DrawAGizmo(vert, size * handleSize, scale * handleSize, GizmoType); } else { if (UseFixedGizmoScale) { handleSize = HandleUtility.GetHandleSize(vert); } Gizmos.color = HoveredVertexColor; DrawAGizmo(vert, size * handleSize, scale * handleSize, 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); } public List SerializedDisplayVertexPositions = new List(); public List SerializedHoveredVertexPositions = new List(); public List SerializedSelectedVertexPositions = new List(); public void OnBeforeSerialize() { SerializedDisplayVertexPositions.AddRange(DisplayVertexPositions); SerializedHoveredVertexPositions.AddRange(HoveredVertexPositions); SerializedSelectedVertexPositions.AddRange(SelectedVertexPositions); } public void OnAfterDeserialize() { DisplayVertexPositions.UnionWith(SerializedDisplayVertexPositions); HoveredVertexPositions.UnionWith(SerializedHoveredVertexPositions); SelectedVertexPositions.UnionWith(SerializedSelectedVertexPositions); } } } #endif