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.

244 lines
9.4 KiB

  1. using System.Collections.Generic;
  2. using System.Reflection;
  3. using UnityEngine;
  4. using UnityEngine.PostProcessing;
  5. namespace UnityEditor.PostProcessing
  6. {
  7. [CustomPropertyDrawer(typeof(TrackballGroupAttribute))]
  8. sealed class TrackballGroupDrawer : PropertyDrawer
  9. {
  10. static Material s_Material;
  11. const int k_MinWheelSize = 80;
  12. const int k_MaxWheelSize = 256;
  13. bool m_ResetState;
  14. // Cached trackball computation methods (for speed reasons)
  15. static Dictionary<string, MethodInfo> m_TrackballMethods = new Dictionary<string, MethodInfo>();
  16. internal static int m_Size
  17. {
  18. get
  19. {
  20. int size = Mathf.FloorToInt(EditorGUIUtility.currentViewWidth / 3f) - 18;
  21. size = Mathf.Clamp(size, k_MinWheelSize, k_MaxWheelSize);
  22. return size;
  23. }
  24. }
  25. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  26. {
  27. if (s_Material == null)
  28. s_Material = new Material(Shader.Find("Hidden/Post FX/UI/Trackball")) { hideFlags = HideFlags.HideAndDontSave };
  29. position = new Rect(position.x, position.y, position.width / 3f, position.height);
  30. int size = m_Size;
  31. position.x += 5f;
  32. var enumerator = property.GetEnumerator();
  33. while (enumerator.MoveNext())
  34. {
  35. var prop = enumerator.Current as SerializedProperty;
  36. if (prop == null || prop.propertyType != SerializedPropertyType.Color)
  37. continue;
  38. OnWheelGUI(position, size, prop.Copy());
  39. position.x += position.width;
  40. }
  41. }
  42. void OnWheelGUI(Rect position, int size, SerializedProperty property)
  43. {
  44. if (Event.current.type == EventType.Layout)
  45. return;
  46. var value = property.colorValue;
  47. float offset = value.a;
  48. var wheelDrawArea = position;
  49. wheelDrawArea.height = size;
  50. if (wheelDrawArea.width > wheelDrawArea.height)
  51. {
  52. wheelDrawArea.x += (wheelDrawArea.width - wheelDrawArea.height) / 2.0f;
  53. wheelDrawArea.width = position.height;
  54. }
  55. wheelDrawArea.width = wheelDrawArea.height;
  56. float hsize = size / 2f;
  57. float radius = 0.38f * size;
  58. Vector3 hsv;
  59. Color.RGBToHSV(value, out hsv.x, out hsv.y, out hsv.z);
  60. if (Event.current.type == EventType.Repaint)
  61. {
  62. float scale = EditorGUIUtility.pixelsPerPoint;
  63. // Wheel texture
  64. var oldRT = RenderTexture.active;
  65. var rt = RenderTexture.GetTemporary((int)(size * scale), (int)(size * scale), 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
  66. s_Material.SetFloat("_Offset", offset);
  67. s_Material.SetFloat("_DisabledState", GUI.enabled ? 1f : 0.5f);
  68. s_Material.SetVector("_Resolution", new Vector2(size * scale, size * scale / 2f));
  69. Graphics.Blit(null, rt, s_Material, EditorGUIUtility.isProSkin ? 0 : 1);
  70. RenderTexture.active = oldRT;
  71. GUI.DrawTexture(wheelDrawArea, rt);
  72. RenderTexture.ReleaseTemporary(rt);
  73. // Thumb
  74. var thumbPos = Vector2.zero;
  75. float theta = hsv.x * (Mathf.PI * 2f);
  76. float len = hsv.y * radius;
  77. thumbPos.x = Mathf.Cos(theta + (Mathf.PI / 2f));
  78. thumbPos.y = Mathf.Sin(theta - (Mathf.PI / 2f));
  79. thumbPos *= len;
  80. var thumbSize = FxStyles.wheelThumbSize;
  81. var thumbSizeH = thumbSize / 2f;
  82. FxStyles.wheelThumb.Draw(new Rect(wheelDrawArea.x + hsize + thumbPos.x - thumbSizeH.x, wheelDrawArea.y + hsize + thumbPos.y - thumbSizeH.y, thumbSize.x, thumbSize.y), false, false, false, false);
  83. }
  84. var bounds = wheelDrawArea;
  85. bounds.x += hsize - radius;
  86. bounds.y += hsize - radius;
  87. bounds.width = bounds.height = radius * 2f;
  88. hsv = GetInput(bounds, hsv, radius);
  89. value = Color.HSVToRGB(hsv.x, hsv.y, 1f);
  90. value.a = offset;
  91. // Luminosity booster
  92. position = wheelDrawArea;
  93. float oldX = position.x;
  94. float oldW = position.width;
  95. position.y += position.height + 4f;
  96. position.x += (position.width - (position.width * 0.75f)) / 2f;
  97. position.width = position.width * 0.75f;
  98. position.height = EditorGUIUtility.singleLineHeight;
  99. value.a = GUI.HorizontalSlider(position, value.a, -1f, 1f);
  100. // Advanced controls
  101. var data = Vector3.zero;
  102. if (TryGetDisplayValue(value, property, out data))
  103. {
  104. position.x = oldX;
  105. position.y += position.height;
  106. position.width = oldW / 3f;
  107. using (new EditorGUI.DisabledGroupScope(true))
  108. {
  109. GUI.Label(position, data.x.ToString("F2"), EditorStyles.centeredGreyMiniLabel);
  110. position.x += position.width;
  111. GUI.Label(position, data.y.ToString("F2"), EditorStyles.centeredGreyMiniLabel);
  112. position.x += position.width;
  113. GUI.Label(position, data.z.ToString("F2"), EditorStyles.centeredGreyMiniLabel);
  114. position.x += position.width;
  115. }
  116. }
  117. // Title
  118. position.x = oldX;
  119. position.y += position.height;
  120. position.width = oldW;
  121. GUI.Label(position, property.displayName, EditorStyles.centeredGreyMiniLabel);
  122. if (m_ResetState)
  123. {
  124. value = Color.clear;
  125. m_ResetState = false;
  126. }
  127. property.colorValue = value;
  128. }
  129. bool TryGetDisplayValue(Color color, SerializedProperty property, out Vector3 output)
  130. {
  131. output = Vector3.zero;
  132. MethodInfo method;
  133. if (!m_TrackballMethods.TryGetValue(property.name, out method))
  134. {
  135. var field = ReflectionUtils.GetFieldInfoFromPath(property.serializedObject.targetObject, property.propertyPath);
  136. if (!field.IsDefined(typeof(TrackballAttribute), false))
  137. return false;
  138. var attr = (TrackballAttribute)field.GetCustomAttributes(typeof(TrackballAttribute), false)[0];
  139. const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
  140. method = typeof(ColorGradingComponent).GetMethod(attr.method, flags);
  141. m_TrackballMethods.Add(property.name, method);
  142. }
  143. if (method == null)
  144. return false;
  145. output = (Vector3)method.Invoke(property.serializedObject.targetObject, new object[] { color });
  146. return true;
  147. }
  148. static readonly int k_ThumbHash = "colorWheelThumb".GetHashCode();
  149. Vector3 GetInput(Rect bounds, Vector3 hsv, float radius)
  150. {
  151. var e = Event.current;
  152. var id = GUIUtility.GetControlID(k_ThumbHash, FocusType.Passive, bounds);
  153. var mousePos = e.mousePosition;
  154. var relativePos = mousePos - new Vector2(bounds.x, bounds.y);
  155. if (e.type == EventType.MouseDown && GUIUtility.hotControl == 0 && bounds.Contains(mousePos))
  156. {
  157. if (e.button == 0)
  158. {
  159. var center = new Vector2(bounds.x + radius, bounds.y + radius);
  160. float dist = Vector2.Distance(center, mousePos);
  161. if (dist <= radius)
  162. {
  163. e.Use();
  164. GetWheelHueSaturation(relativePos.x, relativePos.y, radius, out hsv.x, out hsv.y);
  165. GUIUtility.hotControl = id;
  166. GUI.changed = true;
  167. }
  168. }
  169. else if (e.button == 1)
  170. {
  171. e.Use();
  172. GUI.changed = true;
  173. m_ResetState = true;
  174. }
  175. }
  176. else if (e.type == EventType.MouseDrag && e.button == 0 && GUIUtility.hotControl == id)
  177. {
  178. e.Use();
  179. GUI.changed = true;
  180. GetWheelHueSaturation(relativePos.x, relativePos.y, radius, out hsv.x, out hsv.y);
  181. }
  182. else if (e.rawType == EventType.MouseUp && e.button == 0 && GUIUtility.hotControl == id)
  183. {
  184. e.Use();
  185. GUIUtility.hotControl = 0;
  186. }
  187. return hsv;
  188. }
  189. void GetWheelHueSaturation(float x, float y, float radius, out float hue, out float saturation)
  190. {
  191. float dx = (x - radius) / radius;
  192. float dy = (y - radius) / radius;
  193. float d = Mathf.Sqrt(dx * dx + dy * dy);
  194. hue = Mathf.Atan2(dx, -dy);
  195. hue = 1f - ((hue > 0) ? hue : (Mathf.PI * 2f) + hue) / (Mathf.PI * 2f);
  196. saturation = Mathf.Clamp01(d);
  197. }
  198. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  199. {
  200. return m_Size + 4f * 2f + EditorGUIUtility.singleLineHeight * 3f;
  201. }
  202. }
  203. }