|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using UnityEditor; |
| 5 | +using UnityEngine; |
| 6 | + |
| 7 | +namespace Microsoft.MixedReality.GraphicsTools.Editor |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Improves object selection and adds a shortcut to create a configured game object and component from the game object context menu. |
| 11 | + /// </summary> |
| 12 | + [CustomEditor(typeof(AreaLight))] |
| 13 | + public class AreaLightInspector : UnityEditor.Editor |
| 14 | + { |
| 15 | + private void OnSceneGUI() |
| 16 | + { |
| 17 | + AreaLight light = target as AreaLight; |
| 18 | + |
| 19 | + if (light == null) |
| 20 | + { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + if (light.enabled) |
| 25 | + { |
| 26 | + Handles.color = light.Color; |
| 27 | + } |
| 28 | + else |
| 29 | + { |
| 30 | + Handles.color = Color.gray; |
| 31 | + } |
| 32 | + |
| 33 | + EditorGUI.BeginChangeCheck(); |
| 34 | + Vector2 size = DrawRectHandles(light.transform.rotation, light.transform.position, light.Size); |
| 35 | + if (EditorGUI.EndChangeCheck()) |
| 36 | + { |
| 37 | + Undo.RecordObject(light, "Adjust Area Light Size"); |
| 38 | + light.Size = size; |
| 39 | + } |
| 40 | + |
| 41 | + // Draw the area light's normal only if it will not overlap with the current tool. |
| 42 | + if (!((Tools.current == Tool.Move || Tools.current == Tool.Scale) && Tools.pivotRotation == PivotRotation.Local)) |
| 43 | + { |
| 44 | + Handles.DrawLine(light.transform.position, light.transform.position + light.transform.forward); |
| 45 | + } |
| 46 | + |
| 47 | + Handles.color = new Color(255.0f / 255.0f, 165.0f / 255.0f, 0.0f / 255.0f); // Orange. |
| 48 | + |
| 49 | + // Different visual representation for runtime and edit mode because runtime using CullingGroup to cull lights. |
| 50 | + if (Application.isPlaying) |
| 51 | + { |
| 52 | + var bounds = light.SphereBoundsWorldSpace; |
| 53 | + Handles.RadiusHandle(Quaternion.identity, bounds.position, bounds.radius); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + var bounds = light.BoundsWorldSpace; |
| 58 | + Handles.DrawWireCube(bounds.center, bounds.size); |
| 59 | + } |
| 60 | + |
| 61 | + if (light.CullingActive) |
| 62 | + { |
| 63 | + Handles.Label(light.transform.position, $"Visible: {light.IsVisible}\nDist: {light.Distance.ToString("n1")}"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private bool HasFrameBounds() { return true; } |
| 68 | + |
| 69 | + private Bounds OnGetFrameBounds() |
| 70 | + { |
| 71 | + var light = target as AreaLight; |
| 72 | + Debug.Assert(light != null); |
| 73 | + return light.BoundsWorldSpace; |
| 74 | + } |
| 75 | + |
| 76 | + [MenuItem("GameObject/Light/Graphics Tools/Area Light")] |
| 77 | + private static void CreateAreaLight(MenuCommand menuCommand) |
| 78 | + { |
| 79 | + GameObject gameObject = InspectorUtilities.CreateGameObjectFromMenu<AreaLight>(menuCommand); |
| 80 | + |
| 81 | + if (gameObject != null) |
| 82 | + { |
| 83 | + gameObject.transform.position = new Vector3(0.0f, 1.0f, 0.0f); |
| 84 | + gameObject.transform.rotation = Quaternion.Euler(0.0f, 180.0f, 0.0f); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static float SizeSlider(Vector3 p, Vector3 d, float r) |
| 89 | + { |
| 90 | + Vector3 position = p + d * r; |
| 91 | + float size = HandleUtility.GetHandleSize(position); |
| 92 | + bool temp = GUI.changed; |
| 93 | + GUI.changed = false; |
| 94 | + position = Handles.Slider(position, d, size * 0.03f, Handles.DotHandleCap, 0.0f); |
| 95 | + |
| 96 | + if (GUI.changed) |
| 97 | + { |
| 98 | + r = Vector3.Dot(position - p, d); |
| 99 | + } |
| 100 | + |
| 101 | + GUI.changed |= temp; |
| 102 | + return r; |
| 103 | + } |
| 104 | + private static Color ToActiveColorSpace(Color color) |
| 105 | + { |
| 106 | + return (QualitySettings.activeColorSpace == ColorSpace.Linear) ? color.linear : color; |
| 107 | + } |
| 108 | + |
| 109 | + private static Vector2 DrawRectHandles(Quaternion rotation, Vector3 position, Vector2 size) |
| 110 | + { |
| 111 | + Vector3 up = rotation * Vector3.up; |
| 112 | + Vector3 right = rotation * Vector3.right; |
| 113 | + |
| 114 | + float halfWidth = 0.5f * size.x; |
| 115 | + float halfHeight = 0.5f * size.y; |
| 116 | + |
| 117 | + Vector3 topRight = position + up * halfHeight + right * halfWidth; |
| 118 | + Vector3 bottomRight = position - up * halfHeight + right * halfWidth; |
| 119 | + Vector3 bottomLeft = position - up * halfHeight - right * halfWidth; |
| 120 | + Vector3 topLeft = position + up * halfHeight - right * halfWidth; |
| 121 | + |
| 122 | + // Draw the rectangle. |
| 123 | + Handles.DrawLine(topRight, bottomRight); |
| 124 | + Handles.DrawLine(bottomRight, bottomLeft); |
| 125 | + Handles.DrawLine(bottomLeft, topLeft); |
| 126 | + Handles.DrawLine(topLeft, topRight); |
| 127 | + |
| 128 | + // Give handles twice the alpha of the lines. |
| 129 | + Color originalColor = Handles.color; |
| 130 | + Color color = Handles.color; |
| 131 | + color.a = Mathf.Clamp01(Handles.color.a * 2); |
| 132 | + Handles.color = ToActiveColorSpace(color); |
| 133 | + |
| 134 | + // Draw the handles. |
| 135 | + halfHeight = SizeSlider(position, up, halfHeight); |
| 136 | + halfHeight = SizeSlider(position, -up, halfHeight); |
| 137 | + halfWidth = SizeSlider(position, right, halfWidth); |
| 138 | + halfWidth = SizeSlider(position, -right, halfWidth); |
| 139 | + |
| 140 | + size.x = Mathf.Max(0.0f, 2.0f * halfWidth); |
| 141 | + size.y = Mathf.Max(0.0f, 2.0f * halfHeight); |
| 142 | + |
| 143 | + Handles.color = originalColor; |
| 144 | + |
| 145 | + return size; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments