-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathPrimitiveBoundsHandle.cs
More file actions
319 lines (269 loc) · 12 KB
/
PrimitiveBoundsHandle.cs
File metadata and controls
319 lines (269 loc) · 12 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using System;
namespace UnityEditor.IMGUI.Controls
{
public abstract class PrimitiveBoundsHandle
{
[Flags]
public enum Axes
{
None = 0,
X = 1 << 0,
Y = 1 << 1,
Z = 1 << 2,
All = X | Y | Z
}
protected enum HandleDirection
{
PositiveX,
NegativeX,
PositiveY,
NegativeY,
PositiveZ,
NegativeZ
}
private static readonly float s_DefaultMidpointHandleSize = 0.03f;
private static readonly int[] s_NextAxis = new[] { 1, 2, 0 };
internal static GUIContent editModeButton
{
get
{
if (s_EditModeButton == null)
{
s_EditModeButton = new GUIContent(
EditorGUIUtility.IconContent("EditCollider").image,
EditorGUIUtility.TrTextContent("Edit bounding volume.\n\n - Hold Alt after clicking control handle to pin center in place.\n - Hold Shift after clicking control handle to scale uniformly.").text
);
}
return s_EditModeButton;
}
}
private static GUIContent s_EditModeButton;
public static float DefaultMidpointHandleSizeFunction(Vector3 position)
{
return HandleUtility.GetHandleSize(position) * s_DefaultMidpointHandleSize;
}
private int[] m_ControlIDs = new int[6] { 0, 0, 0, 0, 0, 0 };
private Bounds m_Bounds;
private Bounds m_BoundsOnClick;
public Vector3 center
{
get { return m_Bounds.center; }
set { m_Bounds.center = value; }
}
public Axes axes { get; set; }
public Color handleColor { get; set; }
public Color wireframeColor { get; set; }
public Handles.CapFunction midpointHandleDrawFunction { get; set; }
public Handles.SizeFunction midpointHandleSizeFunction { get; set; }
[Obsolete("Use parameterless constructor instead.")]
public PrimitiveBoundsHandle(int controlIDHint) : this() {}
public PrimitiveBoundsHandle()
{
handleColor = Color.white;
wireframeColor = Color.white;
axes = Axes.X | Axes.Y | Axes.Z;
midpointHandleDrawFunction = Handles.DotHandleCap;
midpointHandleSizeFunction = DefaultMidpointHandleSizeFunction;
}
public void SetColor(Color color)
{
handleColor = color;
wireframeColor = color;
}
public void DrawHandle()
{
for (int i = 0, count = m_ControlIDs.Length; i < count; ++i)
m_ControlIDs[i] = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);
// wireframe (before handles so handles are rendered top most)
using (new Handles.DrawingScope(Handles.color * wireframeColor))
DrawWireframe();
// unless holding alt to pin center, exit before drawing control handles when holding alt, since alt-click will rotate scene view
if (Event.current.alt)
{
bool exit = true;
foreach (var id in m_ControlIDs)
{
if (id == GUIUtility.hotControl)
{
exit = false;
break;
}
}
if (exit)
return;
}
// bounding box extents
Vector3 minPos = m_Bounds.min;
Vector3 maxPos = m_Bounds.max;
// handles
int prevHotControl = GUIUtility.hotControl;
bool isCameraInsideBox = Camera.current != null
&& m_Bounds.Contains(Handles.inverseMatrix.MultiplyPoint(Camera.current.transform.position));
EditorGUI.BeginChangeCheck();
using (new Handles.DrawingScope(Handles.color * handleColor))
MidpointHandles(ref minPos, ref maxPos, isCameraInsideBox);
bool changed = EditorGUI.EndChangeCheck();
// detect if any handles got hotControl
if (prevHotControl != GUIUtility.hotControl && GUIUtility.hotControl != 0)
{
m_BoundsOnClick = m_Bounds;
}
// update if changed
if (changed)
{
// determine which handle changed to apply any further modifications
m_Bounds.center = (maxPos + minPos) * 0.5f;
m_Bounds.size = maxPos - minPos;
for (int i = 0, count = m_ControlIDs.Length; i < count; ++i)
{
if (GUIUtility.hotControl == m_ControlIDs[i])
m_Bounds = OnHandleChanged((HandleDirection)i, m_BoundsOnClick, m_Bounds);
}
// shift scales uniformly
if (Event.current.shift)
{
int hotControl = GUIUtility.hotControl;
Vector3 size = m_Bounds.size;
int scaleAxis = 0;
if (hotControl == m_ControlIDs[(int)HandleDirection.PositiveY] || hotControl == m_ControlIDs[(int)HandleDirection.NegativeY])
{
scaleAxis = 1;
}
if (hotControl == m_ControlIDs[(int)HandleDirection.PositiveZ] || hotControl == m_ControlIDs[(int)HandleDirection.NegativeZ])
{
scaleAxis = 2;
}
float scaleFactor = Mathf.Approximately(m_BoundsOnClick.size[scaleAxis], 0f) ?
1f : size[scaleAxis] / m_BoundsOnClick.size[scaleAxis];
int nextAxis = s_NextAxis[scaleAxis];
size[nextAxis] = scaleFactor * m_BoundsOnClick.size[nextAxis];
nextAxis = s_NextAxis[nextAxis];
size[nextAxis] = scaleFactor * m_BoundsOnClick.size[nextAxis];
m_Bounds.size = size;
}
// alt scales from the center
if (Event.current.alt)
m_Bounds.center = m_BoundsOnClick.center;
}
}
protected abstract void DrawWireframe();
protected virtual Bounds OnHandleChanged(HandleDirection handle, Bounds boundsOnClick, Bounds newBounds)
{
return newBounds;
}
protected Vector3 GetSize()
{
Vector3 size = m_Bounds.size;
// zero out size on disabled axes
for (int axis = 0; axis < 3; ++axis)
{
if (!IsAxisEnabled(axis))
size[axis] = 0f;
}
return size;
}
protected void SetSize(Vector3 size)
{
m_Bounds.size = new Vector3(Mathf.Abs(size.x), Mathf.Abs(size.y), Mathf.Abs(size.z));
}
protected bool IsAxisEnabled(Axes axis)
{
return (axes & axis) == axis;
}
protected bool IsAxisEnabled(int vector3Axis)
{
switch (vector3Axis)
{
case 0:
return IsAxisEnabled(Axes.X);
case 1:
return IsAxisEnabled(Axes.Y);
case 2:
return IsAxisEnabled(Axes.Z);
default:
throw new ArgumentOutOfRangeException("vector3Axis", "Must be 0, 1, or 2");
}
}
private void MidpointHandles(ref Vector3 minPos, ref Vector3 maxPos, bool isCameraInsideBox)
{
Vector3 xAxis = Vector3.right;
Vector3 yAxis = Vector3.up;
Vector3 zAxis = Vector3.forward;
Vector3 middle = (maxPos + minPos) * 0.5f;
Vector3 localPos, newPos;
if (IsAxisEnabled(Axes.X))
{
// +X
localPos = new Vector3(maxPos.x, middle.y, middle.z);
newPos = MidpointHandle(m_ControlIDs[(int)HandleDirection.PositiveX], localPos, yAxis, zAxis, isCameraInsideBox);
maxPos.x = Mathf.Max(newPos.x, minPos.x);
// -X
localPos = new Vector3(minPos.x, middle.y, middle.z);
newPos = MidpointHandle(m_ControlIDs[(int)HandleDirection.NegativeX], localPos, yAxis, -zAxis, isCameraInsideBox);
minPos.x = Mathf.Min(newPos.x, maxPos.x);
}
if (IsAxisEnabled(Axes.Y))
{
// +Y
localPos = new Vector3(middle.x, maxPos.y, middle.z);
newPos = MidpointHandle(m_ControlIDs[(int)HandleDirection.PositiveY], localPos, xAxis, -zAxis, isCameraInsideBox);
maxPos.y = Mathf.Max(newPos.y, minPos.y);
// -Y
localPos = new Vector3(middle.x, minPos.y, middle.z);
newPos = MidpointHandle(m_ControlIDs[(int)HandleDirection.NegativeY], localPos, xAxis, zAxis, isCameraInsideBox);
minPos.y = Mathf.Min(newPos.y, maxPos.y);
}
if (IsAxisEnabled(Axes.Z))
{
// +Z
localPos = new Vector3(middle.x, middle.y, maxPos.z);
newPos = MidpointHandle(m_ControlIDs[(int)HandleDirection.PositiveZ], localPos, yAxis, -xAxis, isCameraInsideBox);
maxPos.z = Mathf.Max(newPos.z, minPos.z);
// -Z
localPos = new Vector3(middle.x, middle.y, minPos.z);
newPos = MidpointHandle(m_ControlIDs[(int)HandleDirection.NegativeZ], localPos, yAxis, xAxis, isCameraInsideBox);
minPos.z = Mathf.Min(newPos.z, maxPos.z);
}
}
private Vector3 MidpointHandle(int id, Vector3 localPos, Vector3 localTangent, Vector3 localBinormal, bool isCameraInsideBox)
{
Color oldColor = Handles.color;
AdjustMidpointHandleColor(localPos, localTangent, localBinormal, isCameraInsideBox);
if (Handles.color.a > 0f && midpointHandleDrawFunction != null)
{
Vector3 localDir = Vector3.Cross(localTangent, localBinormal).normalized;
var size = midpointHandleSizeFunction == null ? 0f : midpointHandleSizeFunction(localPos);
localPos = UnityEditorInternal.Slider1D.Do(
id, localPos, localDir, size, midpointHandleDrawFunction, SnapSettings.scale
);
}
Handles.color = oldColor;
return localPos;
}
private void AdjustMidpointHandleColor(Vector3 localPos, Vector3 localTangent, Vector3 localBinormal, bool isCameraInsideBox)
{
float alphaMultiplier = 1f;
// if inside the box then ignore backfacing alpha multiplier (otherwise all handles will look disabled)
if (!isCameraInsideBox && axes == (Axes.X | Axes.Y | Axes.Z))
{
// use tangent and binormal to calculate normal in case handle matrix is skewed
Vector3 worldTangent = Handles.matrix.MultiplyVector(localTangent);
Vector3 worldBinormal = Handles.matrix.MultiplyVector(localBinormal);
Vector3 worldDir = Vector3.Cross(worldTangent, worldBinormal).normalized;
// adjust color if handle is backfacing
float cosV;
if (Camera.current.orthographic)
cosV = Vector3.Dot(-Camera.current.transform.forward, worldDir);
else
cosV = Vector3.Dot((Camera.current.transform.position - Handles.matrix.MultiplyPoint(localPos)).normalized, worldDir);
if (cosV < -0.0001f)
alphaMultiplier *= Handles.backfaceAlphaMultiplier;
}
Handles.color *= new Color(1f, 1f, 1f, alphaMultiplier);
}
}
}