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