Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrackballUIDrawer.cs 8.9KB

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