forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneView.cs
More file actions
4255 lines (3634 loc) · 166 KB
/
Copy pathSceneView.cs
File metadata and controls
4255 lines (3634 loc) · 166 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
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;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using UnityEditor.AnimatedValues;
using UnityEditor.Rendering;
using UnityEditor.SceneManagement;
using UnityEditor.ShortcutManagement;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
using RequiredByNativeCodeAttribute = UnityEngine.Scripting.RequiredByNativeCodeAttribute;
using UnityEditor.EditorTools;
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.Profiling;
using UnityEditor.Snap;
using UnityEngine.Serialization;
using UnityEngine.UIElements;
using Component = UnityEngine.Component;
using FrameCapture = UnityEngine.Apple.FrameCapture;
using FrameCaptureDestination = UnityEngine.Apple.FrameCaptureDestination;
namespace UnityEditor
{
[EditorWindowTitle(title = "Scene", useTypeNameAsIconName = true)]
public class SceneView : SearchableEditorWindow, IHasCustomMenu
{
[Serializable]
public struct CameraMode
{
internal CameraMode(DrawCameraMode drawMode, string name, string section)
{
this.drawMode = drawMode;
this.name = name;
this.section = section;
}
public DrawCameraMode drawMode;
public string name;
public string section;
public static bool operator==(CameraMode a, CameraMode z)
{
return a.drawMode == z.drawMode && a.name == z.name && a.section == z.section;
}
public static bool operator!=(CameraMode a, CameraMode z)
{
return !(a == z);
}
public override bool Equals(System.Object otherObject)
{
if (ReferenceEquals(otherObject, null))
return false;
if (!(otherObject is CameraMode))
return false;
return this == (CameraMode)otherObject;
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public override string ToString()
{
return UnityString.Format("{0}||{1}||{2}", drawMode, name, section);
}
}
private static SceneView s_LastActiveSceneView;
private static SceneView s_CurrentDrawingSceneView;
public static SceneView lastActiveSceneView
{
get
{
if (s_LastActiveSceneView == null && s_SceneViews.Count > 0)
s_LastActiveSceneView = s_SceneViews[0] as SceneView;
return s_LastActiveSceneView;
}
}
public static SceneView currentDrawingSceneView { get { return s_CurrentDrawingSceneView; } }
internal static readonly PrefColor kSceneViewBackground = new PrefColor("Scene/Background", 0.278431f, 0.278431f, 0.278431f, 0);
internal static readonly PrefColor kSceneViewPrefabBackground = new PrefColor("Scene/Background for Prefabs", 0.132f, 0.231f, 0.330f, 0);
static readonly PrefColor kSceneViewWire = new PrefColor("Scene/Wireframe", 0.0f, 0.0f, 0.0f, 0.5f);
static readonly PrefColor kSceneViewWireOverlay = new PrefColor("Scene/Wireframe Overlay", 0.0f, 0.0f, 0.0f, 0.25f);
static readonly PrefColor kSceneViewSelectedOutline = new PrefColor("Scene/Selected Outline", 255.0f / 255.0f, 102.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f);
static readonly PrefColor kSceneViewSelectedChildrenOutline = new PrefColor("Scene/Selected Children Outline", 94.0f / 255.0f, 119.0f / 255.0f, 155.0f / 255.0f, 0.0f / 255.0f);
static readonly PrefColor kSceneViewSelectedWire = new PrefColor("Scene/Wireframe Selected", 94.0f / 255.0f, 119.0f / 255.0f, 155.0f / 255.0f, 64.0f / 255.0f);
static readonly PrefColor kSceneViewMaterialValidateLow = new PrefColor("Scene/Material Validator Value Too Low", 255.0f / 255.0f, 0.0f, 0.0f, 1.0f);
static readonly PrefColor kSceneViewMaterialValidateHigh = new PrefColor("Scene/Material Validator Value Too High", 0.0f, 0.0f, 255.0f / 255.0f, 1.0f);
static readonly PrefColor kSceneViewMaterialValidatePureMetal = new PrefColor("Scene/Material Validator Pure Metal", 255.0f / 255.0f, 255.0f / 255.0f, 0.0f, 1.0f);
static readonly PrefColor kSceneViewMaterialNoContributeGI = new PrefColor("Scene/Contribute GI Off", 229.0f / 255.0f, 203.0f / 255.0f, 132.0f / 255.0f, 1.0f);
static readonly PrefColor kSceneViewMaterialReceiveGILightmaps = new PrefColor("Scene/Contribute GI / Receive GI Lightmaps", 89.0f / 255.0f, 148.0f / 255.0f, 161.0f / 255.0f, 1.0f);
static readonly PrefColor kSceneViewMaterialReceiveGILightProbes = new PrefColor("Scene/Contribute GI / Receive GI Light Probes", 221.0f / 255.0f, 115.0f / 255.0f, 91.0f / 255.0f, 1.0f);
internal static Color kSceneViewFrontLight = new Color(0.769f, 0.769f, 0.769f, 1);
internal static Color kSceneViewUpLight = new Color(0.212f, 0.227f, 0.259f, 1);
internal static Color kSceneViewMidLight = new Color(0.114f, 0.125f, 0.133f, 1);
internal static Color kSceneViewDownLight = new Color(0.047f, 0.043f, 0.035f, 1);
public static Color selectedOutlineColor => kSceneViewSelectedOutline.Color;
[SerializeField]
bool m_ShowContextualTools;
internal static SavedBool s_PreferenceEnableFilteringWhileSearching = new SavedBool("SceneView.enableFilteringWhileSearching", true);
internal static SavedBool s_PreferenceEnableFilteringWhileLodGroupEditing = new SavedBool("SceneView.enableFilteringWhileLodGroupEditing", true);
internal bool displayToolModes
{
get { return m_ShowContextualTools; }
set { m_ShowContextualTools = value; }
}
internal static Transform GetDefaultParentObjectIfSet()
{
Transform parentObject = null;
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
string activeSceneGUID = prefabStage != null ? prefabStage.scene.guid : EditorSceneManager.GetActiveScene().guid;
int id = SceneHierarchy.GetDefaultParentForSession(activeSceneGUID);
if (id != 0)
{
var objectFromInstanceID = EditorUtility.InstanceIDToObject(id) as GameObject;
parentObject = objectFromInstanceID?.gameObject?.transform;
}
return parentObject;
}
static void OnSelectedObjectWasDestroyed(int unused)
{
s_ActiveEditorsDirty = true;
}
static void OnEditorTrackerRebuilt()
{
s_ActiveEditorsDirty = true;
}
static List<Editor> s_ActiveEditors = new List<Editor>();
static bool s_ActiveEditorsDirty;
internal static IEnumerable<Editor> activeEditors
{
get
{
CollectActiveEditors();
return s_ActiveEditors;
}
}
static void CollectActiveEditors()
{
if (!s_ActiveEditorsDirty)
return;
s_ActiveEditorsDirty = false;
s_ActiveEditors.Clear();
if (s_SharedTracker == null)
s_SharedTracker = ActiveEditorTracker.sharedTracker;
foreach (var editor in s_SharedTracker.activeEditors)
s_ActiveEditors.Add(editor);
foreach (var inspector in InspectorWindow.GetInspectors())
{
if (inspector.isLocked)
{
foreach (var editor in inspector.tracker.activeEditors)
s_ActiveEditors.Add(editor);
}
}
}
bool m_SkipFadingPending;
internal void SkipFading()
{
m_SkipFadingPending = true;
}
[SerializeField]
string m_WindowGUID;
internal string windowGUID => m_WindowGUID;
[SerializeField] bool m_Gizmos = true;
public bool drawGizmos
{
get { return m_Gizmos; }
set { m_Gizmos = value; }
}
internal bool showToolbar { get; set; } = true;
Scene m_CustomScene;
protected internal Scene customScene
{
get { return m_CustomScene; }
set
{
m_CustomScene = value;
m_Camera.scene = m_CustomScene;
var stage = StageUtility.GetStageHandle(m_CustomScene);
StageUtility.SetSceneToRenderInStage(m_CustomLightsScene, stage);
}
}
[SerializeField] ulong m_OverrideSceneCullingMask;
internal ulong overrideSceneCullingMask
{
get { return m_OverrideSceneCullingMask; }
set
{
m_OverrideSceneCullingMask = value;
m_Camera.overrideSceneCullingMask = value;
}
}
SceneViewStageHandling m_StageHandling;
float toolbarHeight
{
get
{
var _toolbarHeight = showToolbar ? EditorGUI.kWindowToolbarHeight.value : 0;
return (m_StageHandling != null && m_StageHandling.isShowingBreadcrumbBar)
? m_StageHandling.breadcrumbHeight + _toolbarHeight
: _toolbarHeight;
}
}
float sceneViewHeight => position.height - toolbarHeight;
// Returns the calculated rect where we render the camera in the SceneView (in window space coordinates)
internal Rect cameraRect
{
get { return new Rect(0, toolbarHeight, position.width, sceneViewHeight); }
}
Transform m_CustomParentForNewGameObjects;
protected internal Transform customParentForDraggedObjects
{
get { return customParentForNewGameObjects; }
set { customParentForNewGameObjects = value; }
}
internal Transform customParentForNewGameObjects
{
get { return m_CustomParentForNewGameObjects; }
set { m_CustomParentForNewGameObjects = value; }
}
[NonSerialized]
static readonly Quaternion kDefaultRotation = Quaternion.LookRotation(new Vector3(-1, -.7f, -1));
const float kDefaultViewSize = 10f;
[NonSerialized]
static readonly Vector3 kDefaultPivot = Vector3.zero;
const float kOrthoThresholdAngle = 3f;
const float kOneOverSqrt2 = 0.707106781f;
// Don't allow scene view zoom/size to go to crazy high values, or otherwise various
// operations will start going to infinities etc.
internal const float k_MaxSceneViewSize = 3.2e34f;
// Limit the max draw distance to Sqrt(float.MaxValue) because transparent sorting function uses dist^2, and
// Asserts that values are finite.
internal const float k_MaxCameraFarClip = 1.844674E+19f;
internal const float k_MinCameraNearClip = 1e-5f;
[NonSerialized]
static ActiveEditorTracker s_SharedTracker;
[SerializeField]
bool m_SceneIsLit = true;
[Obsolete("m_SceneLighting has been deprecated. Use sceneLighting instead (UnityUpgradable) -> UnityEditor.SceneView.sceneLighting", true)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public bool m_SceneLighting = true;
public bool sceneLighting
{
get { return m_SceneIsLit; }
set { m_SceneIsLit = value; }
}
public event Func<CameraMode, bool> onValidateCameraMode;
public event Action<CameraMode> onCameraModeChanged;
public event Action<bool> gridVisibilityChanged;
private bool m_WasFocused = false;
[Serializable]
public class SceneViewState
{
[SerializeField, FormerlySerializedAs("showMaterialUpdate")]
bool m_AlwaysRefresh;
public bool showFog = true;
// marked obsolete by @karlh 2020/4/14
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Obsolete msg (UnityUpgradable) -> alwaysRefresh")]
public bool showMaterialUpdate
{
get => m_AlwaysRefresh;
set => m_AlwaysRefresh = value;
}
public bool alwaysRefresh
{
get => m_AlwaysRefresh;
set => m_AlwaysRefresh = value;
}
public bool showSkybox = true;
public bool showFlares = true;
public bool showImageEffects = true;
public bool showParticleSystems = true;
public bool showVisualEffectGraphs = true;
public bool fogEnabled => fxEnabled && showFog;
// marked obsolete by @karlh 2020/4/14
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Obsolete msg (UnityUpgradable) -> alwaysRefreshEnabled")]
public bool materialUpdateEnabled => alwaysRefreshEnabled;
public bool alwaysRefreshEnabled => fxEnabled && alwaysRefresh;
public bool skyboxEnabled => fxEnabled && showSkybox;
public bool flaresEnabled => fxEnabled && showFlares;
public bool imageEffectsEnabled => fxEnabled && showImageEffects;
public bool particleSystemsEnabled => fxEnabled && showParticleSystems;
public bool visualEffectGraphsEnabled => fxEnabled && showVisualEffectGraphs;
[SerializeField]
bool m_FxEnabled = true;
public SceneViewState()
{
}
public SceneViewState(SceneViewState other)
{
fxEnabled = other.fxEnabled;
showFog = other.showFog;
alwaysRefresh = other.alwaysRefresh;
showSkybox = other.showSkybox;
showFlares = other.showFlares;
showImageEffects = other.showImageEffects;
showParticleSystems = other.showParticleSystems;
showVisualEffectGraphs = other.showVisualEffectGraphs;
}
[Obsolete("IsAllOn() has been deprecated. Use allEnabled instead (UnityUpgradable) -> allEnabled")]
public bool IsAllOn()
{
return allEnabled;
}
public bool allEnabled
{
get
{
bool all = showFog && alwaysRefresh && showSkybox && showFlares && showImageEffects && showParticleSystems;
if (UnityEngine.VFX.VFXManager.activateVFX)
all = all && showVisualEffectGraphs;
return all;
}
}
[Obsolete("Toggle() has been deprecated. Use SetAllEnabled() instead (UnityUpgradable) -> SetAllEnabled(*)")]
public void Toggle(bool value)
{
SetAllEnabled(value);
}
public void SetAllEnabled(bool value)
{
showFog = value;
alwaysRefresh = value;
showSkybox = value;
showFlares = value;
showImageEffects = value;
showParticleSystems = value;
showVisualEffectGraphs = value;
}
public bool fxEnabled
{
get { return m_FxEnabled; }
set { m_FxEnabled = value; }
}
}
[SerializeField]
private bool m_2DMode;
public bool in2DMode
{
get { return m_2DMode; }
set
{
if (m_2DMode != value)
{
m_2DMode = value;
On2DModeChange();
}
}
}
[SerializeField]
bool m_isRotationLocked = false;
public bool isRotationLocked
{
get { return m_isRotationLocked; }
set { m_isRotationLocked = value; }
}
internal static List<CameraMode> userDefinedModes { get; } = new List<CameraMode>();
[SerializeField]
bool m_PlayAudio = false;
[Obsolete("m_AudioPlay has been deprecated. Use audioPlay instead (UnityUpgradable) -> audioPlay", true)]
public bool m_AudioPlay = false;
public bool audioPlay
{
get { return m_PlayAudio; }
set
{
if (value == m_PlayAudio)
return;
m_PlayAudio = value;
RefreshAudioPlay();
}
}
static SceneView s_AudioSceneView;
[SerializeField]
AnimVector3 m_Position = new AnimVector3(kDefaultPivot);
#pragma warning disable 618
[Obsolete("OnSceneFunc() has been deprecated. Use System.Action instead.")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public delegate void OnSceneFunc(SceneView sceneView);
// Marked obsolete 2018-11-28
[Obsolete("onSceneGUIDelegate has been deprecated. Use duringSceneGui instead.")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static OnSceneFunc onSceneGUIDelegate;
#pragma warning restore 618
public static event Action<SceneView> beforeSceneGui;
public static event Action<SceneView> duringSceneGui;
internal static event Func<SceneView, VisualElement> addCustomVisualElementToSceneView;
// Used for performance tests
internal static event Action<SceneView> onGUIStarted;
internal static event Action<SceneView> onGUIEnded;
[Obsolete("Use cameraMode instead", false)]
public DrawCameraMode m_RenderMode = 0;
[Obsolete("Use cameraMode instead", false)]
public DrawCameraMode renderMode
{
get
{
return m_CameraMode.drawMode;
}
set
{
if (value == DrawCameraMode.UserDefined)
throw new ArgumentException("Use cameraMode to set user-defined modes");
cameraMode = SceneRenderModeWindow.GetBuiltinCameraMode(value);
}
}
[SerializeField]
CameraMode m_CameraMode;
public CameraMode cameraMode
{
get
{
// fix for case 969889 where the toolbar is empty when we haven't fully initialized the value
if (string.IsNullOrEmpty(m_CameraMode.name))
{
m_CameraMode = SceneRenderModeWindow.GetBuiltinCameraMode(m_CameraMode.drawMode);
}
return m_CameraMode;
}
set
{
if (!IsValidCameraMode(value))
{
throw new ArgumentException(string.Format("The provided camera mode {0} is not registered!", value));
}
m_CameraMode = value;
if (onCameraModeChanged != null)
onCameraModeChanged(m_CameraMode);
}
}
[Obsolete("m_ValidateTrueMetals has been deprecated. Use validateTrueMetals instead (UnityUpgradable) -> validateTrueMetals", true)]
public bool m_ValidateTrueMetals = false;
[SerializeField]
bool m_DoValidateTrueMetals = false;
public bool validateTrueMetals
{
get { return m_DoValidateTrueMetals; }
set
{
if (m_DoValidateTrueMetals == value)
return;
m_DoValidateTrueMetals = value;
Shader.SetGlobalInt("_CheckPureMetal", m_DoValidateTrueMetals ? 1 : 0);
}
}
[SerializeField]
float m_ExposureSliderValue = 0.0f;
// this value can be altered by the user
float m_ExposureSliderMax = 16f;
Texture2D m_ExposureTexture = null;
Texture2D m_EmptyExposureTexture = null;
internal bool showExposureSettings
{
get
{
return (this.cameraMode.drawMode == DrawCameraMode.BakedEmissive || this.cameraMode.drawMode == DrawCameraMode.BakedLightmap ||
this.cameraMode.drawMode == DrawCameraMode.RealtimeEmissive || this.cameraMode.drawMode == DrawCameraMode.RealtimeIndirect);
}
}
[SerializeField]
SceneViewState m_SceneViewState;
public SceneViewState sceneViewState
{
get { return m_SceneViewState; }
set { m_SceneViewState = value; }
}
[SerializeField]
SceneViewGrid m_Grid;
public bool showGrid
{
get { return sceneViewGrids.showGrid; }
set { sceneViewGrids.showGrid = value; }
}
[SerializeField]
internal SceneViewRotation svRot;
[SerializeField]
internal AnimQuaternion m_Rotation = new AnimQuaternion(kDefaultRotation);
// How large an area the scene view covers (measured vertically)
[SerializeField]
AnimFloat m_Size = new AnimFloat(kDefaultViewSize);
[SerializeField]
internal AnimBool m_Ortho = new AnimBool();
[NonSerialized]
Camera m_Camera;
VisualElement m_CameraViewVisualElement;
static readonly string s_CameraRectVisualElementName = "unity-scene-view-camera-rect";
internal VisualElement cameraViewVisualElement => m_CameraViewVisualElement;
[Serializable]
public class CameraSettings
{
const float defaultEasingDuration = .4f;
const float kAbsoluteSpeedMin = .001f;
const float kAbsoluteSpeedMax = 99f;
const float kAbsoluteEasingDurationMin = .1f;
const float kAbsoluteEasingDurationMax = 2f;
const float kMinSpeedMinMaxRange = .001f;
[SerializeField]
float m_Speed;
[SerializeField]
float m_SpeedNormalized;
[SerializeField]
float m_SpeedMin;
[SerializeField]
float m_SpeedMax;
[SerializeField]
bool m_EasingEnabled;
[SerializeField]
float m_EasingDuration;
[SerializeField]
bool m_AccelerationEnabled;
[SerializeField]
float m_FieldOfViewHorizontalOrVertical; // either horizontal or vertical depending on aspect ratio
[SerializeField]
float m_NearClip;
[SerializeField]
float m_FarClip;
[SerializeField]
bool m_DynamicClip;
[SerializeField]
bool m_OcclusionCulling;
public CameraSettings()
{
m_Speed = 1f;
m_SpeedNormalized = .5f;
m_SpeedMin = kAbsoluteSpeedMin;
m_SpeedMax = 2f;
m_EasingEnabled = true;
m_EasingDuration = defaultEasingDuration;
fieldOfView = kDefaultPerspectiveFov;
m_DynamicClip = true;
m_OcclusionCulling = false;
m_NearClip = .03f;
m_FarClip = 10000f;
m_AccelerationEnabled = true;
}
public float speed
{
get
{
return m_Speed;
}
set
{
speedNormalized = Mathf.InverseLerp(m_SpeedMin, m_SpeedMax, value);
}
}
public float speedNormalized
{
get
{
return m_SpeedNormalized;
}
set
{
m_SpeedNormalized = Mathf.Clamp01(value);
m_Speed = Mathf.Lerp(m_SpeedMin, m_SpeedMax, m_SpeedNormalized);
}
}
public float speedMin
{
get
{
return m_SpeedMin;
}
set
{
float[] m_Vector2Floats = { value, m_SpeedMax };
SetSpeedMinMax(m_Vector2Floats);
}
}
public float speedMax
{
get
{
return m_SpeedMax;
}
set
{
float[] m_Vector2Floats = { m_SpeedMin, value };
SetSpeedMinMax(m_Vector2Floats);
}
}
// Easing is applied when starting and stopping movement. When enabled, the camera will lerp from it's
// current speed to the target speed over the course of `CameraSettings.easingDuration` seconds.
public bool easingEnabled
{
get { return m_EasingEnabled; }
set { m_EasingEnabled = value; }
}
// How many seconds should the camera take to go from stand-still to initial full speed. When setting an animated value
// speed, use `1 / duration`.
public float easingDuration
{
get
{
return m_EasingDuration;
}
set
{
// Clamp and round to 1 decimal point
m_EasingDuration = (float)Math.Round(Mathf.Clamp(value, kAbsoluteEasingDurationMin, kAbsoluteEasingDurationMax), 1);
}
}
// When acceleration is enabled, camera speed is continuously increased while in motion. When acceleration
// is disabled, speed is a constant value defined by `CameraSettings.speed`
public bool accelerationEnabled
{
get { return m_AccelerationEnabled; }
set { m_AccelerationEnabled = value; }
}
// this ensures that the resolution the slider snaps to is sufficient given the minimum speed, and the
// range of appropriate values
internal float RoundSpeedToNearestSignificantDecimal(float value)
{
if (value <= speedMin)
return speedMin;
if (value >= speedMax)
return speedMax;
float rng = speedMax - speedMin;
int min_rnd = speedMin < .01f ? 3 : speedMin < .1f ? 2 : speedMin < 1f ? 1 : 0;
int rng_rnd = rng < 1f ? 2 : rng < 10f ? 1 : 0;
return (float)Math.Round(value, Mathf.Max(min_rnd, rng_rnd));
}
internal void SetSpeedMinMax(float[] floatValues)
{
// Clamp min to valid ranges
float min = Mathf.Clamp(floatValues[0], kAbsoluteSpeedMin, kAbsoluteSpeedMax - kMinSpeedMinMaxRange);
float minRange = min < .1f ? .01f : min < 1f ? .1f : 1f;
float max = Mathf.Clamp(floatValues[1], min + minRange, kAbsoluteSpeedMax);
m_SpeedMin = min;
m_SpeedMax = max;
// This will clamp the speed to the new range
speed = m_Speed;
}
internal void SetClipPlanes(float near, float far)
{
farClip = Mathf.Clamp(far, float.Epsilon, k_MaxCameraFarClip);
nearClip = Mathf.Max(k_MinCameraNearClip, near);
}
public float fieldOfView
{
get { return m_FieldOfViewHorizontalOrVertical; }
set { m_FieldOfViewHorizontalOrVertical = value; }
}
public float nearClip
{
get { return m_NearClip; }
set { m_NearClip = value; }
}
public float farClip
{
get { return m_FarClip; }
set { m_FarClip = value; }
}
public bool dynamicClip
{
get { return m_DynamicClip; }
set { m_DynamicClip = value; }
}
public bool occlusionCulling
{
get { return m_OcclusionCulling; }
set { m_OcclusionCulling = value; }
}
}
[SerializeField]
private CameraSettings m_CameraSettings;
public CameraSettings cameraSettings
{
get { return m_CameraSettings; }
set { m_CameraSettings = value; }
}
internal Vector2 GetDynamicClipPlanes()
{
float farClip = Mathf.Clamp(2000f * size, 1000f, k_MaxCameraFarClip);
return new Vector2(farClip * 0.000005f, farClip);
}
internal SceneViewGrid sceneViewGrids
{
get { return m_Grid; }
}
public void ResetCameraSettings()
{
m_CameraSettings = new CameraSettings();
}
// Thomas Tu: 2019-06-20. Will be marked as Obsolete.
// We need to deal with code dependency in packages first.
internal bool showGlobalGrid { get { return showGrid; } set { showGrid = value; } }
[SerializeField]
private Quaternion m_LastSceneViewRotation;
public Quaternion lastSceneViewRotation
{
get
{
if (m_LastSceneViewRotation == new Quaternion(0f, 0f, 0f, 0f))
m_LastSceneViewRotation = Quaternion.identity;
return m_LastSceneViewRotation;
}
set { m_LastSceneViewRotation = value; }
}
[SerializeField]
private bool m_LastSceneViewOrtho;
// Cursor rect handling
private struct CursorRect
{
public Rect rect;
public MouseCursor cursor;
public CursorRect(Rect rect, MouseCursor cursor)
{
this.rect = rect;
this.cursor = cursor;
}
}
private static MouseCursor s_LastCursor = MouseCursor.Arrow;
private static readonly List<CursorRect> s_MouseRects = new List<CursorRect>();
private bool s_DraggingCursorIsCached;
internal static void AddCursorRect(Rect rect, MouseCursor cursor)
{
var eventType = Event.current.type;
if (eventType == EventType.Repaint || eventType == EventType.MouseMove)
s_MouseRects.Add(new CursorRect(rect, cursor));
}
static float GetPerspectiveCameraDistance(float objectSize, float fov)
{
// A
// |\ We want to place camera at a
// | \ distance that, at the given FOV,
// | \ would enclose a sphere of radius
// _..+.._\ "size". Here |BC|=size, and we
// .' | '\ need to find |AB|. ACB is a right
// / | _C angle, andBAC is half the FOV. So
// | | _- | that gives: sin(BAC)=|BC|/|AB|,
// | B | and thus |AB|=|BC|/sin(BAC).
// | |
// \ /
// '._ _.'
// `````
return objectSize / Mathf.Sin(fov * 0.5f * Mathf.Deg2Rad);
}
public float cameraDistance
{
get
{
float res;
if (!camera.orthographic)
{
float fov = m_Ortho.Fade(perspectiveFov, 0);
res = GetPerspectiveCameraDistance(size, fov);
}
else
res = size * 2f;
// clamp to allowed range in case scene view size was huge
return Mathf.Clamp(res, -k_MaxSceneViewSize, k_MaxSceneViewSize);
}
}
[System.NonSerialized]
Scene m_CustomLightsScene;
[System.NonSerialized]
Light[] m_Light = new Light[3];
RectSelection m_RectSelection;
const float kDefaultPerspectiveFov = 60;
static ArrayList s_SceneViews = new ArrayList();
public static ArrayList sceneViews { get { return s_SceneViews; } }
static List<Camera> s_AllSceneCameraList = new List<Camera>();
static Camera[] s_AllSceneCameras = new Camera[] {};
static Material s_AlphaOverlayMaterial;
static Material s_DeferredOverlayMaterial;
static Shader s_ShowOverdrawShader;
static Shader s_ShowMipsShader;
static Shader s_ShowTextureStreamingShader;
static Shader s_AuraShader;
static Material s_FadeMaterial;
static Material s_ApplyFilterMaterial;
static Texture2D s_MipColorsTexture;
// Handle Dragging of stuff over scene view
//static ArrayList s_DraggedEditors = null;
//static GameObject[] s_PickedObject = { null };
internal static class Styles
{
public static GUIContent toolsContent = EditorGUIUtility.TrIconContent("SceneViewTools", "Hide or show the Component Editor Tools panel in the Scene view.");
public static GUIContent lighting = EditorGUIUtility.TrIconContent("SceneviewLighting", "When toggled on, the Scene lighting is used. When toggled off, a light attached to the Scene view camera is used.");
public static GUIContent fx = EditorGUIUtility.TrIconContent("SceneviewFx", "Toggle skybox, fog, and various other effects.");
public static GUIContent audioPlayContent = EditorGUIUtility.TrIconContent("SceneviewAudio", "Toggle audio on or off.");
public static GUIContent gizmosContent = EditorGUIUtility.TrTextContent("Gizmos", "Toggle visibility of all Gizmos in the Scene view");
public static GUIContent gizmosDropDownContent = EditorGUIUtility.TrTextContent("", "Toggle the visibility of different Gizmos in the Scene view.");
public static GUIContent mode2DContent = EditorGUIUtility.TrIconContent("SceneView2D", "When toggled on, the Scene is in 2D view. When toggled off, the Scene is in 3D view.");
public static GUIContent gridXToolbarContent = EditorGUIUtility.TrIconContent("GridAxisX", "Toggle the visibility of the grid");
public static GUIContent gridYToolbarContent = EditorGUIUtility.TrIconContent("GridAxisY", "Toggle the visibility of the grid");
public static GUIContent gridZToolbarContent = EditorGUIUtility.TrIconContent("GridAxisZ", "Toggle the visibility of the grid");
public static GUIContent isolationModeExitButton = EditorGUIUtility.TrTextContent("Exit", "Exit isolation mode");
public static GUIContent renderDocContent;
public static GUIContent metalFrameCaptureContent = EditorGUIUtility.TrIconContent("FrameCapture", "Capture the current view and open in Xcode frame debugger");
public static GUIContent sceneVisToolbarButtonContent = EditorGUIUtility.TrIconContent("SceneViewVisibility", "Number of hidden objects, click to toggle scene visibility");
public static GUIStyle gizmoButtonStyle;
public static GUIContent sceneViewCameraContent = EditorGUIUtility.TrIconContent("SceneViewCamera", "Settings for the Scene view camera.");
public static GUIContent contributeGIOff = EditorGUIUtility.TrTextContent("Contribute GI Off");
public static GUIContent receiveGILightmaps = EditorGUIUtility.TrTextContent("Contribute GI / Receive GI Lightmaps");
public static GUIContent receiveGILightProbes = EditorGUIUtility.TrTextContent("Contribute GI / Receive GI Light Probes");
static Styles()
{
gizmoButtonStyle = "GV Gizmo DropDown";
renderDocContent = EditorGUIUtility.TrIconContent("FrameCapture", UnityEditor.RenderDocUtil.openInRenderDocLabel);
}
}
double m_StartSearchFilterTime = -1;
RenderTexture m_SceneTargetTexture;
int m_MainViewControlID;
public Camera camera { get { return m_Camera; } }
[SerializeField]
private Shader m_ReplacementShader;
[SerializeField]
private string m_ReplacementString;
[SerializeField]
private bool m_SceneVisActive = true;
private string m_SceneVisHiddenCount = "0";
OverlayWindow m_SceneVisOverlayWindow;
OverlayWindow m_PBRSettingsOverlayWindow;
OverlayWindow m_LightingExposureSettingsOverlayWindow;
OverlayWindow m_GIContributorsReceiversOverlayWindow;
OverlayWindow m_EditorToolsOverlayWindow;
public void SetSceneViewShaderReplace(Shader shader, string replaceString)
{
m_ReplacementShader = shader;
m_ReplacementString = replaceString;
}
internal bool m_ShowSceneViewWindows = false;
SceneViewOverlay m_SceneViewOverlay;
internal EditorCache m_DragEditorCache;
// While Locking the view to object, we have different behaviour for different scenarios:
// Smooth camera behaviour: User dragging the handles
// Instant camera behaviour: Position changed externally (via inspector, physics or scripts etc.)