/
Handles.cs
1768 lines (1534 loc) · 79.5 KB
/
Handles.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Internal;
namespace UnityEditor
{
// Grid drawing params for Handles.DrawCamera.
[StructLayout(LayoutKind.Sequential)]
struct DrawGridParameters
{
public int gridID;
public Vector3 pivot;
public Color color;
public Vector2 size;
}
public sealed partial class Handles
{
// Color of the X axis handle
internal static PrefColor s_XAxisColor = new PrefColor("Scene/X Axis", 219f / 255, 62f / 255, 29f / 255, .93f);
public static Color xAxisColor { get { return s_XAxisColor; } }
// Color of the Y axis handle
internal static PrefColor s_YAxisColor = new PrefColor("Scene/Y Axis", 154f / 255, 243f / 255, 72f / 255, .93f);
public static Color yAxisColor { get { return s_YAxisColor; } }
// Color of the Z axis handle
internal static PrefColor s_ZAxisColor = new PrefColor("Scene/Z Axis", 58f / 255, 122f / 255, 248f / 255, .93f);
public static Color zAxisColor { get { return s_ZAxisColor; } }
// Color of the Constrain Proportions scale handles
internal static PrefColor constrainProportionsScaleHandleColor = new PrefColor("Scene/Constrain Proportions Scale Handle", 190f / 255, 190f / 255, 190f / 255, 1f);
// Color of the center handle
internal static PrefColor s_CenterColor = new PrefColor("Scene/Center Axis", .8f, .8f, .8f, .93f);
public static Color centerColor { get { return s_CenterColor; } }
// color for handles the currently active handle
internal static PrefColor s_SelectedColor = new PrefColor("Scene/Selected Axis", 246f / 255, 242f / 255, 50f / 255, .89f);
public static Color selectedColor { get { return s_SelectedColor; } }
// color for handles the currently hovered handle
internal static PrefColor s_PreselectionColor = new PrefColor("Scene/Preselection Highlight", 201f / 255, 200f / 255, 144f / 255, 0.89f);
public static Color preselectionColor { get { return s_PreselectionColor; } }
// soft color for general stuff - used to draw e.g. the arc selection while dragging
internal static PrefColor s_SecondaryColor = new PrefColor("Scene/Guide Line", .5f, .5f, .5f, .2f);
public static Color secondaryColor { get { return s_SecondaryColor; } }
// internal color for static handles
internal static Color staticColor = new Color(.5f, .5f, .5f, 0f);
// internal blend ratio for static colors
internal static float staticBlend = 0.6f;
static PrefColor s_ElementColor => new PrefColor("Scene/Element Default", 0f, 224f / 255f, 1f, 1f);
static PrefColor s_ElementPreselectionColor => new PrefColor("Scene/Element Preselection", 1f, 207f / 255f, 112f / 255f, 1f);
static PrefColor s_ElementSelectionColor => new PrefColor("Scene/Element Selection", 1f, 182f / 255f, 40f / 255f, 1f);
public static Color elementColor => s_ElementColor;
public static Color elementPreselectionColor => s_ElementPreselectionColor;
public static Color elementSelectionColor => s_ElementSelectionColor;
internal static float backfaceAlphaMultiplier = 0.2f;
internal static Color s_ColliderHandleColor = new Color(145f, 244f, 139f, 210f) / 255;
internal static Color s_ColliderHandleColorDisabled = new Color(84, 200f, 77f, 140f) / 255;
internal static Color s_BoundingBoxHandleColor = new Color(255, 255, 255, 150) / 255;
// Should match s_ColliderHandleColor to start unless the user overrides the color. As Gizmos are drawn from CPP there is no way currently to hook the ColliderHandleColor up properly.
public static Color UIColliderHandleColor { get { return s_UIColliderHandleColor; } }
internal static PrefColor s_UIColliderHandleColor = new PrefColor("Scene/UI Collider Handle", 145f / 255, 244f / 255, 139f / 255, 210f / 255);
internal readonly static GUIContent s_StaticLabel = EditorGUIUtility.TrTextContent("Static");
internal readonly static GUIContent s_PrefabLabel = EditorGUIUtility.TrTextContent("Prefab");
internal static int s_SliderHash = "SliderHash".GetHashCode();
internal static int s_Slider2DHash = "Slider2DHash".GetHashCode();
internal static int s_FreeRotateHandleHash = "FreeRotateHandleHash".GetHashCode();
internal static int s_RadiusHandleHash = "RadiusHandleHash".GetHashCode();
internal static int s_xAxisMoveHandleHash = "xAxisFreeMoveHandleHash".GetHashCode();
internal static int s_yAxisMoveHandleHash = "yAxisFreeMoveHandleHash".GetHashCode();
internal static int s_zAxisMoveHandleHash = "zAxisFreeMoveHandleHash".GetHashCode();
internal static int s_FreeMoveHandleHash = "FreeMoveHandleHash".GetHashCode();
internal static int s_xzAxisMoveHandleHash = "xzAxisFreeMoveHandleHash".GetHashCode();
internal static int s_xyAxisMoveHandleHash = "xyAxisFreeMoveHandleHash".GetHashCode();
internal static int s_yzAxisMoveHandleHash = "yzAxisFreeMoveHandleHash".GetHashCode();
internal static int s_xAxisScaleHandleHash = "xAxisScaleHandleHash".GetHashCode();
internal static int s_yAxisScaleHandleHash = "yAxisScaleHandleHash".GetHashCode();
internal static int s_zAxisScaleHandleHash = "zAxisScaleHandleHash".GetHashCode();
internal static int s_ScaleSliderHash = "ScaleSliderHash".GetHashCode();
internal static int s_ScaleValueHandleHash = "ScaleValueHandleHash".GetHashCode();
internal static int s_DiscHash = "DiscHash".GetHashCode();
internal static int s_ButtonHash = "ButtonHash".GetHashCode();
static readonly int kPropUseGuiClip = Shader.PropertyToID("_UseGUIClip");
static readonly int kPropHandleZTest = Shader.PropertyToID("_HandleZTest");
static readonly int kPropColor = Shader.PropertyToID("_Color");
static readonly int kPropArcCenterRadius = Shader.PropertyToID("_ArcCenterRadius");
static readonly int kPropArcNormalAngle = Shader.PropertyToID("_ArcNormalAngle");
static readonly int kPropArcFromCount = Shader.PropertyToID("_ArcFromCount");
static readonly int kPropArcThicknessSides = Shader.PropertyToID("_ArcThicknessSides");
static readonly int kPropHandlesMatrix = Shader.PropertyToID("_HandlesMatrix");
public struct DrawingScope : IDisposable
{
private bool m_Disposed;
public Color originalColor { get { return m_OriginalColor; } }
private Color m_OriginalColor;
public Matrix4x4 originalMatrix { get { return m_OriginalMatrix; } }
private Matrix4x4 m_OriginalMatrix;
public DrawingScope(Color color) : this(color, Handles.matrix) {}
public DrawingScope(Matrix4x4 matrix) : this(Handles.color, matrix) {}
public DrawingScope(Color color, Matrix4x4 matrix)
{
m_Disposed = false;
m_OriginalColor = Handles.color;
m_OriginalMatrix = Handles.matrix;
Handles.matrix = matrix;
Handles.color = color;
}
public void Dispose()
{
if (m_Disposed)
return;
m_Disposed = true;
Handles.color = m_OriginalColor;
Handles.matrix = m_OriginalMatrix;
}
}
internal static Mesh cubeMesh
{
get
{
if (s_CubeMesh == null)
Init();
return s_CubeMesh;
}
}
internal static Mesh coneMesh
{
get
{
if (s_ConeMesh == null)
Init();
return s_ConeMesh;
}
}
internal static Mesh cylinderMesh
{
get
{
if (s_CylinderMesh == null)
Init();
return s_CylinderMesh;
}
}
internal static Mesh sphereMesh
{
get
{
if (s_SphereMesh == null)
Init();
return s_SphereMesh;
}
}
internal static Mesh quadMesh
{
get
{
if (s_QuadMesh == null)
Init();
return s_QuadMesh;
}
}
internal static int s_xRotateHandleHash = "xRotateHandleHash".GetHashCode();
internal static int s_yRotateHandleHash = "yRotateHandleHash".GetHashCode();
internal static int s_zRotateHandleHash = "zRotateHandleHash".GetHashCode();
internal static int s_cameraAxisRotateHandleHash = "cameraAxisRotateHandleHash".GetHashCode();
internal static int s_xyzRotateHandleHash = "xyzRotateHandleHash".GetHashCode();
internal static int s_xScaleHandleHash = "xScaleHandleHash".GetHashCode();
internal static int s_yScaleHandleHash = "yScaleHandleHash".GetHashCode();
internal static int s_zScaleHandleHash = "zScaleHandleHash".GetHashCode();
internal static int s_xyzScaleHandleHash = "xyzScaleHandleHash".GetHashCode();
private static Color lineTransparency = new Color(1, 1, 1, 0.75f);
internal static SavedFloat s_LineThickness = new SavedFloat("SceneView.handleLineThickness", 2.0f);
public static float lineThickness => s_LineThickness.value;
// When hovering over some handle axis/control, this is the indication that it would
// get picked on mouse press:
// Color gets a bit more bright and less opaque,
internal static Color s_HoverIntensity = new Color(1.0f, 1.0f, 1.0f, 1.33f);
// Handle lines get more thick,
internal static float s_HoverExtraThickness = 1.0f;
// 3D handle elements (caps) get slightly larger.
internal static float s_HoverExtraScale = 1.05f;
// When axis is looking away from camera, fade it out along 25 -> 15 degrees range
static readonly float kCameraViewLerpStart1 = Mathf.Cos(Mathf.Deg2Rad * 25.0f);
static readonly float kCameraViewLerpEnd1 = Mathf.Cos(Mathf.Deg2Rad * 15.0f);
// When axis is looking towards the camera, fade it out along 170 -> 175 degrees range
static readonly float kCameraViewLerpStart2 = Mathf.Cos(Mathf.Deg2Rad * 170.0f);
static readonly float kCameraViewLerpEnd2 = Mathf.Cos(Mathf.Deg2Rad * 175.0f);
// Hide & disable axis if they have faded out more than 60%
internal const float kCameraViewThreshold = 0.6f;
// The function for calling AddControl in Layout event and draw the handle in Repaint event.
public delegate void CapFunction(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType);
public delegate float SizeFunction(Vector3 position);
static PrefColor[] s_AxisColor = { s_XAxisColor, s_YAxisColor, s_ZAxisColor };
static Vector3[] s_AxisVector = { Vector3.right, Vector3.up, Vector3.forward };
internal static Color s_DisabledHandleColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
internal static Color GetColorByAxis(int axis)
{
return s_AxisColor[axis];
}
internal static Color ToActiveColorSpace(Color color)
{
return (QualitySettings.activeColorSpace == ColorSpace.Linear) ? color.linear : color;
}
static Vector3 GetAxisVector(int axis)
{
return s_AxisVector[axis];
}
internal static bool IsHovering(int controlID, Event evt)
{
return controlID == HandleUtility.nearestControl && GUIUtility.hotControl == 0 && !Tools.viewToolActive;
}
static internal void SetupHandleColor(int controlID, Event evt, out Color prevColor, out float thickness)
{
prevColor = Handles.color;
thickness = Handles.lineThickness;
if (controlID == GUIUtility.hotControl)
{
Handles.color = Handles.selectedColor;
}
else if (IsHovering(controlID, evt))
{
var col = Handles.color * s_HoverIntensity;
// make sure colors never go outside of 0..1 range
col.r = Mathf.Clamp01(col.r);
col.g = Mathf.Clamp01(col.g);
col.b = Mathf.Clamp01(col.b);
col.a = Mathf.Clamp01(col.a);
Handles.color = col;
thickness += s_HoverExtraThickness;
}
}
static void Swap(ref Vector3 v, int[] indices, int a, int b)
{
var f = v[a];
v[a] = v[b];
v[b] = f;
var t = indices[a];
indices[a] = indices[b];
indices[b] = t;
}
// Given view direction in handle space, calculate
// back-to-front order in which handle axes should be drawn.
// The array should be [3] size, and will contain axis indices
// from (0,1,2) set.
static void CalcDrawOrder(Vector3 viewDir, int[] ordering)
{
ordering[0] = 0;
ordering[1] = 1;
ordering[2] = 2;
// essentially an unrolled bubble sort for 3 elements
if (viewDir.y > viewDir.x) Swap(ref viewDir, ordering, 1, 0);
if (viewDir.z > viewDir.y) Swap(ref viewDir, ordering, 2, 1);
if (viewDir.y > viewDir.x) Swap(ref viewDir, ordering, 1, 0);
}
private static bool BeginLineDrawing(Matrix4x4 matrix, bool dottedLines, int mode)
{
if (Event.current.type != EventType.Repaint)
return false;
Color col = color * lineTransparency;
if (dottedLines)
HandleUtility.ApplyDottedWireMaterial(zTest);
else
HandleUtility.ApplyWireMaterial(zTest);
GL.PushMatrix();
GL.MultMatrix(matrix);
GL.Begin(mode);
GL.Color(col);
return true;
}
private static void EndLineDrawing()
{
GL.End();
GL.PopMatrix();
}
public static void DrawPolyLine(params Vector3[] points)
{
if (!BeginLineDrawing(matrix, false, GL.LINE_STRIP))
return;
for (int i = 0; i < points.Length; i++)
{
GL.Vertex(points[i]);
}
EndLineDrawing();
}
[ExcludeFromDocs]
public static void DrawLine(Vector3 p1, Vector3 p2)
{
DrawLine(p1, p2, false);
}
internal static void DrawLine(Vector3 p1, Vector3 p2, bool dottedLine)
{
if (!BeginLineDrawing(matrix, dottedLine, GL.LINES))
return;
GL.Vertex(p1);
GL.Vertex(p2);
EndLineDrawing();
}
static float ThicknessToPixels(float thickness)
{
var halfThicknessPixels = thickness * EditorGUIUtility.pixelsPerPoint * 0.5f;
if (halfThicknessPixels < 0.9f)
halfThicknessPixels = 0;
return halfThicknessPixels;
}
public static void DrawLine(Vector3 p1, Vector3 p2, [DefaultValue("0.0f")] float thickness)
{
if (Event.current.type != EventType.Repaint)
return;
thickness = ThicknessToPixels(thickness);
if (thickness <= 0)
{
DrawLine(p1, p2);
return;
}
var mat = SetupArcMaterial();
if (mat == null) // can't do thick lines
{
DrawLine(p1, p2);
return;
}
mat.SetVector(kPropArcCenterRadius, new Vector4(p1.x, p1.y, p1.z, 0));
mat.SetVector(kPropArcFromCount, new Vector4(p2.x, p2.y, p2.z, 0));
mat.SetVector(kPropArcThicknessSides, new Vector4(thickness, kArcSides, 0, 0));
mat.SetPass(1);
var indexBuffer = HandleUtility.GetArcIndexBuffer(kArcSegments, kArcSides);
Graphics.DrawProceduralNow(MeshTopology.Triangles, indexBuffer, kArcSides * 6);
}
public static void DrawLines(Vector3[] lineSegments)
{
if (!BeginLineDrawing(matrix, false, GL.LINES))
return;
for (int i = 0; i < lineSegments.Length; i += 2)
{
var p1 = lineSegments[i + 0];
var p2 = lineSegments[i + 1];
GL.Vertex(p1);
GL.Vertex(p2);
}
EndLineDrawing();
}
public static void DrawLines(Vector3[] points, int[] segmentIndices)
{
if (!BeginLineDrawing(matrix, false, GL.LINES))
return;
for (int i = 0; i < segmentIndices.Length; i += 2)
{
var p1 = points[segmentIndices[i + 0]];
var p2 = points[segmentIndices[i + 1]];
GL.Vertex(p1);
GL.Vertex(p2);
}
EndLineDrawing();
}
public static void DrawDottedLine(Vector3 p1, Vector3 p2, float screenSpaceSize)
{
if (!BeginLineDrawing(matrix, true, GL.LINES))
return;
var dashSize = screenSpaceSize * EditorGUIUtility.pixelsPerPoint;
GL.MultiTexCoord(1, p1); GL.MultiTexCoord2(2, dashSize, 0); GL.Vertex(p1);
GL.MultiTexCoord(1, p1); GL.MultiTexCoord2(2, dashSize, 0); GL.Vertex(p2);
EndLineDrawing();
}
public static void DrawDottedLines(Vector3[] lineSegments, float screenSpaceSize)
{
if (!BeginLineDrawing(matrix, true, GL.LINES))
return;
var dashSize = screenSpaceSize * EditorGUIUtility.pixelsPerPoint;
for (int i = 0; i < lineSegments.Length; i += 2)
{
var p1 = lineSegments[i + 0];
var p2 = lineSegments[i + 1];
GL.MultiTexCoord(1, p1); GL.MultiTexCoord2(2, dashSize, 0); GL.Vertex(p1);
GL.MultiTexCoord(1, p1); GL.MultiTexCoord2(2, dashSize, 0); GL.Vertex(p2);
}
EndLineDrawing();
}
public static void DrawDottedLines(Vector3[] points, int[] segmentIndices, float screenSpaceSize)
{
if (!BeginLineDrawing(matrix, true, GL.LINES))
return;
var dashSize = screenSpaceSize * EditorGUIUtility.pixelsPerPoint;
for (int i = 0; i < segmentIndices.Length; i += 2)
{
var p1 = points[segmentIndices[i + 0]];
var p2 = points[segmentIndices[i + 1]];
GL.MultiTexCoord(1, p1); GL.MultiTexCoord2(2, dashSize, 0); GL.Vertex(p1);
GL.MultiTexCoord(1, p1); GL.MultiTexCoord2(2, dashSize, 0); GL.Vertex(p2);
}
EndLineDrawing();
}
public static void DrawWireCube(Vector3 center, Vector3 size)
{
if (Event.current.type != EventType.Repaint || lineTransparency.a <= 0)
return;
HandleUtility.ApplyWireMaterial(zTest);
GL.PushMatrix();
GL.MultMatrix(matrix);
GL.Begin(GL.LINE_STRIP);
GL.Color(color * lineTransparency);
Vector3 p1, p2, p3, p6, p7, p8;
{
Vector3 halfsize = size * 0.5f;
GL.Vertex(center + new Vector3(-halfsize.x, -halfsize.y, -halfsize.z));
p1 = center + new Vector3(-halfsize.x, halfsize.y, -halfsize.z);
GL.Vertex(p1);
p2 = center + new Vector3(halfsize.x, halfsize.y, -halfsize.z);
GL.Vertex(p2);
p3 = center + new Vector3(halfsize.x, -halfsize.y, -halfsize.z);
GL.Vertex(p3);
GL.Vertex(center + new Vector3(-halfsize.x, -halfsize.y, -halfsize.z));
GL.Vertex(center + new Vector3(-halfsize.x, -halfsize.y, halfsize.z));
p6 = center + new Vector3(-halfsize.x, halfsize.y, halfsize.z);
GL.Vertex(p6);
p7 = center + new Vector3(halfsize.x, halfsize.y, halfsize.z);
GL.Vertex(p7);
p8 = center + new Vector3(halfsize.x, -halfsize.y, halfsize.z);
GL.Vertex(p8);
GL.Vertex(center + new Vector3(-halfsize.x, -halfsize.y, halfsize.z));
}
GL.End();
GL.Begin(GL.LINES);
GL.Color(color * lineTransparency);
GL.Vertex(p1);
GL.Vertex(p6);
GL.Vertex(p2);
GL.Vertex(p7);
GL.Vertex(p3);
GL.Vertex(p8);
GL.End();
GL.PopMatrix();
}
public static bool ShouldRenderGizmos()
{
var playModeView = PlayModeView.GetRenderingView();
SceneView sv = SceneView.currentDrawingSceneView;
if (playModeView != null)
return playModeView.IsShowingGizmos();
if (sv != null)
return sv.drawGizmos;
return false;
}
public static void DrawGizmos(Camera camera)
{
if (ShouldRenderGizmos())
Internal_DoDrawGizmos(camera);
}
// Make a 3D slider
public static Vector3 Slider(Vector3 position, Vector3 direction)
{
return Slider(position, direction, HandleUtility.GetHandleSize(position), ArrowHandleCap, -1);
}
public static Vector3 Slider(Vector3 position, Vector3 direction, float size, CapFunction capFunction, float snap)
{
int id = GUIUtility.GetControlID(s_SliderHash, FocusType.Passive);
return UnityEditorInternal.Slider1D.Do(id, position, direction, size, capFunction, snap);
}
public static Vector3 Slider(int controlID, Vector3 position, Vector3 direction, float size, CapFunction capFunction, float snap)
{
return UnityEditorInternal.Slider1D.Do(controlID, position, direction, size, capFunction, snap);
}
public static Vector3 Slider(int controlID, Vector3 position, Vector3 offset, Vector3 direction, float size, CapFunction capFunction, float snap)
{
return UnityEditorInternal.Slider1D.Do(controlID, position, offset, direction, direction, size, capFunction, snap);
}
[Obsolete("Rotation parameter is obsolete. (UnityUpgradable) -> !1")]
public static Vector3 FreeMoveHandle(Vector3 position, Quaternion rotation, float size, Vector3 snap, CapFunction capFunction)
{
int id = GUIUtility.GetControlID(s_FreeMoveHandleHash, FocusType.Passive);
return UnityEditorInternal.FreeMove.Do(id, position, rotation, size, snap, capFunction);
}
[Obsolete("Rotation parameter is obsolete. (UnityUpgradable) -> !2")]
public static Vector3 FreeMoveHandle(int controlID, Vector3 position, Quaternion rotation, float size, Vector3 snap, CapFunction capFunction)
{
return UnityEditorInternal.FreeMove.Do(controlID, position, rotation, size, snap, capFunction);
}
public static Vector3 FreeMoveHandle(Vector3 position, float size, Vector3 snap, CapFunction capFunction)
{
int id = GUIUtility.GetControlID(s_FreeMoveHandleHash, FocusType.Passive);
return UnityEditorInternal.FreeMove.Do(id, position, size, snap, capFunction);
}
public static Vector3 FreeMoveHandle(int controlID, Vector3 position, float size, Vector3 snap, CapFunction capFunction)
{
return UnityEditorInternal.FreeMove.Do(controlID, position, size, snap, capFunction);
}
// Make a single-float draggable handle.
public static float ScaleValueHandle(float value, Vector3 position, Quaternion rotation, float size, CapFunction capFunction, float snap)
{
int id = GUIUtility.GetControlID(s_ScaleValueHandleHash, FocusType.Passive);
return UnityEditorInternal.SliderScale.DoCenter(id, value, position, rotation, size, capFunction, snap);
}
public static float ScaleValueHandle(int controlID, float value, Vector3 position, Quaternion rotation, float size, CapFunction capFunction, float snap)
{
return UnityEditorInternal.SliderScale.DoCenter(controlID, value, position, rotation, size, capFunction, snap);
}
// Make a 3D Button.
public static bool Button(Vector3 position, Quaternion direction, float size, float pickSize, CapFunction capFunction)
{
int id = GUIUtility.GetControlID(s_ButtonHash, FocusType.Passive);
return UnityEditorInternal.Button.Do(id, position, direction, size, pickSize, capFunction);
}
internal static bool Button(int controlID, Vector3 position, Quaternion direction, float size, float pickSize, CapFunction capFunction)
{
return UnityEditorInternal.Button.Do(controlID, position, direction, size, pickSize, capFunction);
}
internal static bool Button(int controlID, Vector3 position, Quaternion direction, float size, float pickSize, CapFunction capFunction, bool checkMouseProximity)
{
return UnityEditorInternal.Button.Do(controlID, position, direction, size, pickSize, capFunction, checkMouseProximity);
}
// Draw a cube. Pass this into handle functions.
public static void CubeHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
switch (eventType)
{
case EventType.Layout:
case EventType.MouseMove:
HandleUtility.AddControl(controlID, HandleUtility.DistanceToCube(position, rotation, size));
break;
case (EventType.Repaint):
Graphics.DrawMeshNow(cubeMesh, StartCapDraw(position, rotation, size));
break;
}
}
// Draw a Sphere. Pass this into handle functions.
public static void SphereHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
switch (eventType)
{
case EventType.Layout:
case EventType.MouseMove:
// TODO: Create DistanceToCube
HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(position, size));
break;
case (EventType.Repaint):
Graphics.DrawMeshNow(sphereMesh, StartCapDraw(position, rotation, size));
break;
}
}
// Draw a Cone. Pass this into handle functions.
public static void ConeHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
switch (eventType)
{
case EventType.Layout:
case EventType.MouseMove:
HandleUtility.AddControl(controlID, HandleUtility.DistanceToCone(position, rotation, size));
break;
case EventType.Repaint:
Graphics.DrawMeshNow(coneMesh, StartCapDraw(position, rotation, size));
break;
}
}
// Draw a Cylinder. Pass this into handle functions.
public static void CylinderHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
switch (eventType)
{
case EventType.Layout:
case EventType.MouseMove:
// TODO: Create DistanceToCylinder
HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(position, size));
break;
case (EventType.Repaint):
Graphics.DrawMeshNow(cylinderMesh, StartCapDraw(position, rotation, size));
break;
}
}
// Draw a camera-facing Rectangle. Pass this into handle functions.
static Vector3[] s_RectangleHandlePointsCache = new Vector3[5];
public static void RectangleHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
RectangleHandleCap(controlID, position, rotation, new Vector2(size, size), eventType);
}
internal static void RectangleHandleCap(int controlID, Vector3 position, Quaternion rotation, Vector2 size, EventType eventType)
{
switch (eventType)
{
case EventType.Layout:
case EventType.MouseMove:
// TODO: Create DistanceToRectangle
HandleUtility.AddControl(controlID, HandleUtility.DistanceToRectangleInternal(position, rotation, size));
break;
case (EventType.Repaint):
Vector3 sideways = rotation * new Vector3(size.x, 0, 0);
Vector3 up = rotation * new Vector3(0, size.y, 0);
s_RectangleHandlePointsCache[0] = position + sideways + up;
s_RectangleHandlePointsCache[1] = position + sideways - up;
s_RectangleHandlePointsCache[2] = position - sideways - up;
s_RectangleHandlePointsCache[3] = position - sideways + up;
s_RectangleHandlePointsCache[4] = position + sideways + up;
Handles.DrawPolyLine(s_RectangleHandlePointsCache);
break;
}
}
internal static void RectangleHandleCapWorldSpace(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
RectangleHandleCapWorldSpace(controlID, position, rotation, new Vector2(size, size), eventType);
}
internal static void RectangleHandleCapWorldSpace(int controlID, Vector3 position, Quaternion rotation, Vector2 size, EventType eventType)
{
switch (eventType)
{
case EventType.Layout:
case EventType.MouseMove:
HandleUtility.AddControl(controlID, HandleUtility.DistanceToRectangleInternalWorldSpace(position, rotation, size));
break;
case (EventType.Repaint):
Vector3 sideways = rotation * new Vector3(size.x, 0, 0);
Vector3 up = rotation * new Vector3(0, size.y, 0);
s_RectangleHandlePointsCache[0] = position + sideways + up;
s_RectangleHandlePointsCache[1] = position + sideways - up;
s_RectangleHandlePointsCache[2] = position - sideways - up;
s_RectangleHandlePointsCache[3] = position - sideways + up;
s_RectangleHandlePointsCache[4] = position + sideways + up;
Handles.DrawPolyLine(s_RectangleHandlePointsCache);
break;
}
}
// Draw a camera-facing dot. Pass this into handle functions.
public static void DotHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
switch (eventType)
{
case EventType.Layout:
case EventType.MouseMove:
HandleUtility.AddControl(controlID, HandleUtility.DistanceToRectangle(position, rotation, size));
break;
case (EventType.Repaint):
// Only apply matrix to the position because DotCap is camera facing
position = matrix.MultiplyPoint(position);
Vector3 sideways = (Camera.current == null ? Vector3.right : Camera.current.transform.right) * size;
Vector3 up = (Camera.current == null ? Vector3.up : Camera.current.transform.up) * size;
Color col = color * new Color(1, 1, 1, 0.99f);
HandleUtility.ApplyWireMaterial(Handles.zTest);
GL.Begin(GL.QUADS);
GL.Color(col);
GL.Vertex(position + sideways + up);
GL.Vertex(position + sideways - up);
GL.Vertex(position - sideways - up);
GL.Vertex(position - sideways + up);
GL.End();
break;
}
}
// Draw a camera-facing Circle. Pass this into handle functions.
public static void CircleHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
switch (eventType)
{
case EventType.Layout:
case EventType.MouseMove:
HandleUtility.AddControl(controlID, HandleUtility.DistanceToRectangle(position, rotation, size));
break;
case (EventType.Repaint):
StartCapDraw(position, rotation, size);
Vector3 forward = rotation * new Vector3(0, 0, 1);
Handles.DrawWireDisc(position, forward, size);
break;
}
}
// Draw an arrow like those used by the move tool.
public static void ArrowHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
ArrowHandleCap(controlID, position, rotation, size, eventType, Vector3.zero);
}
internal static void ArrowHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType, Vector3 coneOffset)
{
switch (eventType)
{
case EventType.Layout:
case EventType.MouseMove:
{
Vector3 direction = rotation * Vector3.forward;
HandleUtility.AddControl(controlID, HandleUtility.DistanceToLine(position, position + (direction + coneOffset) * (size * .9f)));
HandleUtility.AddControl(controlID, HandleUtility.DistanceToCone(position + (direction + coneOffset) * size, rotation, size * .2f));
break;
}
case EventType.Repaint:
{
Vector3 direction = rotation * Vector3.forward;
float thickness = Handles.lineThickness;
float coneSize = size * .2f;
if (IsHovering(controlID, Event.current))
{
thickness += s_HoverExtraThickness;
coneSize *= s_HoverExtraScale;
}
var camera = Camera.current;
var viewDir = camera != null ? camera.transform.forward : -direction;
var facingAway = Vector3.Dot(viewDir, direction) < 0.0f;
var conePos = position + (direction + coneOffset) * size;
var linePos = position + (direction + coneOffset) * (size * .9f);
// draw line vs cone in the appropriate order based on viewing
// direction, for correct transparency sorting
if (facingAway)
{
DrawLine(position, linePos, thickness);
ConeHandleCap(controlID, conePos, rotation, coneSize, eventType);
}
else
{
ConeHandleCap(controlID, conePos, rotation, coneSize, eventType);
DrawLine(position, linePos, thickness);
}
break;
}
}
}
// Draw a camera facing selection frame.
public static void DrawSelectionFrame(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
{
if (eventType != EventType.Repaint)
return;
Handles.StartCapDraw(position, rotation, size);
Vector3 sideways = rotation * new Vector3(size, 0, 0);
Vector3 up = rotation * new Vector3(0, size, 0);
var point1 = position - sideways + up;
var point2 = position + sideways + up;
var point3 = position + sideways - up;
var point4 = position - sideways - up;
Handles.DrawLine(point1, point2);
Handles.DrawLine(point2, point3);
Handles.DrawLine(point3, point4);
Handles.DrawLine(point4, point1);
}
internal static Color GetFadedAxisColor(Color col, float fade, int id)
{
// never fade out axes that are being hover-highlighted or currently interacted with
if (id != 0 && id == GUIUtility.hotControl || id == HandleUtility.nearestControl)
fade = 0;
col = Color.Lerp(col, Color.clear, fade);
return col;
}
internal static float GetCameraViewLerpForWorldAxis(Vector3 viewVector, Vector3 axis)
{
var dot = Vector3.Dot(viewVector, axis);
var l1 = Mathf.InverseLerp(kCameraViewLerpStart1, kCameraViewLerpEnd1, dot);
var l2 = Mathf.InverseLerp(kCameraViewLerpStart2, kCameraViewLerpEnd2, dot);
return Mathf.Max(l1, l2);
}
internal static Vector3 GetCameraViewFrom(Vector3 position, Matrix4x4 matrix)
{
Camera camera = Camera.current;
return camera.orthographic
? matrix.MultiplyVector(camera.transform.forward).normalized
: matrix.MultiplyVector(position - camera.transform.position).normalized;
}
// Make a 3D Scene view position handle.
public static Vector3 PositionHandle(Vector3 position, Quaternion rotation)
{
return DoPositionHandle(position, rotation);
}
public static Vector3 PositionHandle(PositionHandleIds ids, Vector3 position, Quaternion rotation)
{
return DoPositionHandle(ids, position, rotation);
}
// Make a Scene view rotation handle.
public static Quaternion RotationHandle(Quaternion rotation, Vector3 position)
{
return DoRotationHandle(rotation, position);
}
public static Quaternion RotationHandle(RotationHandleIds ids, Quaternion rotation, Vector3 position)
{
return DoRotationHandle(ids, rotation, position, RotationHandleParam.Default);
}
// Make a Scene view scale handle
public static Vector3 ScaleHandle(Vector3 scale, Vector3 position, Quaternion rotation)
{
return DoScaleHandle(scale, position, rotation, HandleUtility.GetHandleSize(position));
}
// Make a Scene view scale handle
public static Vector3 ScaleHandle(Vector3 scale, Vector3 position, Quaternion rotation, float size)
{
return DoScaleHandle(scale, position, rotation, size, false);
}
internal static Vector3 ScaleHandle(Vector3 scale, Vector3 position, Quaternion rotation, float size, bool isProportionalScale)
{
return DoScaleHandle(scale, position, rotation, size, isProportionalScale);
}
///*listonly*
public static float RadiusHandle(Quaternion rotation, Vector3 position, float radius, bool handlesOnly)
{
return DoRadiusHandle(rotation, position, radius, handlesOnly);
}
// Make a Scene view radius handle
public static float RadiusHandle(Quaternion rotation, Vector3 position, float radius)
{
return DoRadiusHandle(rotation, position, radius, false);
}
// Make a Scene View cone handle
internal static Vector2 ConeHandle(Quaternion rotation, Vector3 position, Vector2 angleAndRange, float angleScale, float rangeScale, bool handlesOnly)
{
return DoConeHandle(rotation, position, angleAndRange, angleScale, rangeScale, handlesOnly);
}
// Make a Scene View cone frustrum handle
internal static Vector3 ConeFrustrumHandle(Quaternion rotation, Vector3 position, Vector3 radiusAngleRange, ConeHandles showHandles = ConeHandles.All)
{
return DoConeFrustrumHandle(rotation, position, radiusAngleRange, showHandles);
}
// Slide a handle in a 2D plane
public static Vector3 Slider2D(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CapFunction capFunction, Vector2 snap)
{
return Slider2D(id, handlePos, offset, handleDir, slideDir1, slideDir2, handleSize, capFunction, snap, false);
}
public static Vector3 Slider2D(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CapFunction capFunction, Vector2 snap, [DefaultValue("false")] bool drawHelper)
{
return UnityEditorInternal.Slider2D.Do(id, handlePos, offset, handleDir, slideDir1, slideDir2, handleSize, capFunction, snap, drawHelper);
}
/// *listonly*
public static Vector3 Slider2D(Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CapFunction capFunction, Vector2 snap)
{
return Slider2D(handlePos, handleDir, slideDir1, slideDir2, handleSize, capFunction, snap, false);
}
public static Vector3 Slider2D(Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CapFunction capFunction, Vector2 snap, [DefaultValue("false")] bool drawHelper)
{
int id = GUIUtility.GetControlID(s_Slider2DHash, FocusType.Passive);
return UnityEditorInternal.Slider2D.Do(id, handlePos, new Vector3(0, 0, 0), handleDir, slideDir1, slideDir2, handleSize, capFunction, snap, drawHelper);
}
/// *listonly*
public static Vector3 Slider2D(int id, Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CapFunction capFunction, Vector2 snap)
{
return Slider2D(id, handlePos, handleDir, slideDir1, slideDir2, handleSize, capFunction, snap, false);
}
public static Vector3 Slider2D(int id, Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CapFunction capFunction, Vector2 snap, [DefaultValue("false")] bool drawHelper)
{
return UnityEditorInternal.Slider2D.Do(id, handlePos, new Vector3(0, 0, 0), handleDir, slideDir1, slideDir2, handleSize, capFunction, snap, drawHelper);
}
/// *listonly*
public static Vector3 Slider2D(Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CapFunction capFunction, float snap)
{
return Slider2D(handlePos, handleDir, slideDir1, slideDir2, handleSize, capFunction, snap, false);
}
public static Vector3 Slider2D(Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CapFunction capFunction, float snap, [DefaultValue("false")] bool drawHelper)
{
int id = GUIUtility.GetControlID(s_Slider2DHash, FocusType.Passive);
return Slider2D(id, handlePos, new Vector3(0, 0, 0), handleDir, slideDir1, slideDir2, handleSize, capFunction, new Vector2(snap, snap), drawHelper);
}
// Make an unconstrained rotation handle.
public static Quaternion FreeRotateHandle(int id, Quaternion rotation, Vector3 position, float size)
{
return UnityEditorInternal.FreeRotate.Do(id, rotation, position, size);
}
public static Quaternion FreeRotateHandle(Quaternion rotation, Vector3 position, float size)
{
int id = GUIUtility.GetControlID(s_FreeRotateHandleHash, FocusType.Passive);
return UnityEditorInternal.FreeRotate.Do(id, rotation, position, size);
}
// Make a directional scale slider
public static float ScaleSlider(int id, float scale, Vector3 position, Vector3 direction, Quaternion rotation, float size, float snap)
{
return UnityEditorInternal.SliderScale.DoAxis(id, scale, position, direction, rotation, size, snap);
}
public static float ScaleSlider(float scale, Vector3 position, Vector3 direction, Quaternion rotation, float size, float snap)
{
int id = GUIUtility.GetControlID(s_ScaleSliderHash, FocusType.Passive);
return UnityEditorInternal.SliderScale.DoAxis(id, scale, position, direction, rotation, size, snap);
}
// Make a 3D disc that can be dragged with the mouse
public static Quaternion Disc(int id, Quaternion rotation, Vector3 position, Vector3 axis, float size, bool cutoffPlane, float snap)
{
return UnityEditorInternal.Disc.Do(id, rotation, position, axis, size, cutoffPlane, snap);
}
public static Quaternion Disc(Quaternion rotation, Vector3 position, Vector3 axis, float size, bool cutoffPlane, float snap)
{
int id = GUIUtility.GetControlID(s_DiscHash, FocusType.Passive);
return UnityEditorInternal.Disc.Do(id, rotation, position, axis, size, cutoffPlane, snap);
}
internal static void SetupIgnoreRaySnapObjects()
{
HandleUtility.ignoreRaySnapObjects = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.Deep);
}
// If snapping is active, return a new value rounded to the nearest increment of snap.
public static float SnapValue(float value, float snap)
{
if (EditorSnapSettings.incrementalSnapActive)