TrackballUIDrawer.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.Rendering.Universal
  4. {
  5. // TODO: Should be cleaned up and put into CoreRP/Editor
  6. sealed class TrackballUIDrawer
  7. {
  8. static readonly int s_ThumbHash = "colorWheelThumb".GetHashCode();
  9. static GUIStyle s_WheelThumb;
  10. static Vector2 s_WheelThumbSize;
  11. static Material s_Material;
  12. Func<Vector4, Vector3> m_ComputeFunc;
  13. bool m_ResetState;
  14. Vector2 m_CursorPos;
  15. static TrackballUIDrawer()
  16. {
  17. if (s_Material == null)
  18. s_Material = new Material(Shader.Find("Hidden/Universal Render Pipeline/Editor/Trackball")) { hideFlags = HideFlags.HideAndDontSave };
  19. }
  20. public void OnGUI(SerializedProperty property, SerializedProperty overrideState, GUIContent title, Func<Vector4, Vector3> computeFunc)
  21. {
  22. if (property.propertyType != SerializedPropertyType.Vector4)
  23. {
  24. Debug.LogWarning("TrackballUIDrawer requires a Vector4 property");
  25. return;
  26. }
  27. m_ComputeFunc = computeFunc;
  28. var value = property.vector4Value;
  29. using (new EditorGUILayout.VerticalScope())
  30. {
  31. using (new EditorGUI.DisabledScope(!overrideState.boolValue))
  32. DrawWheel(ref value, overrideState.boolValue);
  33. DrawLabelAndOverride(title, overrideState);
  34. }
  35. if (m_ResetState)
  36. {
  37. value = new Vector4(1f, 1f, 1f, 0f);
  38. m_ResetState = false;
  39. }
  40. property.vector4Value = value;
  41. }
  42. void DrawWheel(ref Vector4 value, bool overrideState)
  43. {
  44. var wheelRect = GUILayoutUtility.GetAspectRect(1f);
  45. float size = wheelRect.width;
  46. float hsize = size / 2f;
  47. float radius = 0.38f * size;
  48. Vector3 hsv;
  49. Color.RGBToHSV(value, out hsv.x, out hsv.y, out hsv.z);
  50. float offset = value.w;
  51. // Thumb
  52. var thumbPos = Vector2.zero;
  53. float theta = hsv.x * (Mathf.PI * 2f);
  54. thumbPos.x = Mathf.Cos(theta + (Mathf.PI / 2f));
  55. thumbPos.y = Mathf.Sin(theta - (Mathf.PI / 2f));
  56. thumbPos *= hsv.y * radius;
  57. // Draw the wheel
  58. if (Event.current.type == EventType.Repaint)
  59. {
  60. // Style init
  61. if (s_WheelThumb == null)
  62. {
  63. s_WheelThumb = new GUIStyle("ColorPicker2DThumb");
  64. s_WheelThumbSize = new Vector2(
  65. !Mathf.Approximately(s_WheelThumb.fixedWidth, 0f) ? s_WheelThumb.fixedWidth : s_WheelThumb.padding.horizontal,
  66. !Mathf.Approximately(s_WheelThumb.fixedHeight, 0f) ? s_WheelThumb.fixedHeight : s_WheelThumb.padding.vertical
  67. );
  68. }
  69. // Retina support
  70. float scale = EditorGUIUtility.pixelsPerPoint;
  71. // Wheel texture
  72. var oldRT = RenderTexture.active;
  73. var rt = RenderTexture.GetTemporary((int)(size * scale), (int)(size * scale), 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
  74. s_Material.SetFloat("_Offset", offset);
  75. s_Material.SetFloat("_DisabledState", overrideState && GUI.enabled ? 1f : 0.5f);
  76. s_Material.SetVector("_Resolution", new Vector2(size * scale, size * scale / 2f));
  77. Graphics.Blit(null, rt, s_Material, EditorGUIUtility.isProSkin ? 0 : 1);
  78. RenderTexture.active = oldRT;
  79. GUI.DrawTexture(wheelRect, rt);
  80. RenderTexture.ReleaseTemporary(rt);
  81. var thumbSize = s_WheelThumbSize;
  82. var thumbSizeH = thumbSize / 2f;
  83. s_WheelThumb.Draw(new Rect(wheelRect.x + hsize + thumbPos.x - thumbSizeH.x, wheelRect.y + hsize + thumbPos.y - thumbSizeH.y, thumbSize.x, thumbSize.y), false, false, false, false);
  84. }
  85. // Input
  86. var bounds = wheelRect;
  87. bounds.x += hsize - radius;
  88. bounds.y += hsize - radius;
  89. bounds.width = bounds.height = radius * 2f;
  90. hsv = GetInput(bounds, hsv, thumbPos, radius);
  91. value = Color.HSVToRGB(hsv.x, hsv.y, 1f);
  92. value.w = offset;
  93. // Offset
  94. var sliderRect = GUILayoutUtility.GetRect(1f, 17f);
  95. float padding = sliderRect.width * 0.05f; // 5% padding
  96. sliderRect.xMin += padding;
  97. sliderRect.xMax -= padding;
  98. value.w = GUI.HorizontalSlider(sliderRect, value.w, -1f, 1f);
  99. if (m_ComputeFunc == null)
  100. return;
  101. // Values
  102. var displayValue = m_ComputeFunc(value);
  103. using (new EditorGUI.DisabledGroupScope(true))
  104. {
  105. var valuesRect = GUILayoutUtility.GetRect(1f, 17f);
  106. valuesRect.width /= 3f;
  107. GUI.Label(valuesRect, displayValue.x.ToString("F2"), EditorStyles.centeredGreyMiniLabel);
  108. valuesRect.x += valuesRect.width;
  109. GUI.Label(valuesRect, displayValue.y.ToString("F2"), EditorStyles.centeredGreyMiniLabel);
  110. valuesRect.x += valuesRect.width;
  111. GUI.Label(valuesRect, displayValue.z.ToString("F2"), EditorStyles.centeredGreyMiniLabel);
  112. valuesRect.x += valuesRect.width;
  113. }
  114. }
  115. void DrawLabelAndOverride(GUIContent title, SerializedProperty overrideState)
  116. {
  117. // Title
  118. var areaRect = GUILayoutUtility.GetRect(1f, 17f);
  119. var labelSize = EditorStyles.miniLabel.CalcSize(title);
  120. var labelRect = new Rect(areaRect.x + areaRect.width / 2 - labelSize.x / 2, areaRect.y, labelSize.x, labelSize.y);
  121. GUI.Label(labelRect, title, EditorStyles.miniLabel);
  122. // Override checkbox
  123. var overrideRect = new Rect(labelRect.x - 17, labelRect.y + 3, 17f, 17f);
  124. overrideState.boolValue = GUI.Toggle(overrideRect, overrideState.boolValue, EditorGUIUtility.TrTextContent("", "Override this setting for this volume."), CoreEditorStyles.smallTickbox);
  125. }
  126. Vector3 GetInput(Rect bounds, Vector3 hsv, Vector2 thumbPos, float radius)
  127. {
  128. var e = Event.current;
  129. var id = GUIUtility.GetControlID(s_ThumbHash, FocusType.Passive, bounds);
  130. var mousePos = e.mousePosition;
  131. if (e.type == EventType.MouseDown && GUIUtility.hotControl == 0 && bounds.Contains(mousePos))
  132. {
  133. if (e.button == 0)
  134. {
  135. var center = new Vector2(bounds.x + radius, bounds.y + radius);
  136. float dist = Vector2.Distance(center, mousePos);
  137. if (dist <= radius)
  138. {
  139. e.Use();
  140. m_CursorPos = new Vector2(thumbPos.x + radius, thumbPos.y + radius);
  141. GUIUtility.hotControl = id;
  142. GUI.changed = true;
  143. }
  144. }
  145. else if (e.button == 1)
  146. {
  147. e.Use();
  148. GUI.changed = true;
  149. m_ResetState = true;
  150. }
  151. }
  152. else if (e.type == EventType.MouseDrag && e.button == 0 && GUIUtility.hotControl == id)
  153. {
  154. e.Use();
  155. GUI.changed = true;
  156. m_CursorPos += e.delta * 0.2f; // Sensitivity
  157. GetWheelHueSaturation(m_CursorPos.x, m_CursorPos.y, radius, out hsv.x, out hsv.y);
  158. }
  159. else if (e.rawType == EventType.MouseUp && e.button == 0 && GUIUtility.hotControl == id)
  160. {
  161. e.Use();
  162. GUIUtility.hotControl = 0;
  163. }
  164. return hsv;
  165. }
  166. void GetWheelHueSaturation(float x, float y, float radius, out float hue, out float saturation)
  167. {
  168. float dx = (x - radius) / radius;
  169. float dy = (y - radius) / radius;
  170. float d = Mathf.Sqrt(dx * dx + dy * dy);
  171. hue = Mathf.Atan2(dx, -dy);
  172. hue = 1f - ((hue > 0) ? hue : (Mathf.PI * 2f) + hue) / (Mathf.PI * 2f);
  173. saturation = Mathf.Clamp01(d);
  174. }
  175. }
  176. }