Deprecated.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace UnityEditor.Rendering.LWRP
  5. {
  6. // This is to keep the namespace UnityEditor.Rendering.LWRP alive,
  7. // so that user scripts that have "using UnityEditor.Rendering.LWRP" in them still compile.
  8. internal class Deprecated
  9. {
  10. }
  11. }
  12. namespace UnityEditor.Rendering.Universal
  13. {
  14. static partial class EditorUtils
  15. {
  16. [Obsolete("This is obsolete, please use DrawCascadeSplitGUI<T>(ref SerializedProperty shadowCascadeSplit, float distance, int cascadeCount, Unit unit) instead.", false)]
  17. public static void DrawCascadeSplitGUI<T>(ref SerializedProperty shadowCascadeSplit)
  18. {
  19. float[] cascadePartitionSizes = null;
  20. Type type = typeof(T);
  21. if (type == typeof(float))
  22. {
  23. cascadePartitionSizes = new float[] { shadowCascadeSplit.floatValue };
  24. }
  25. else if (type == typeof(Vector3))
  26. {
  27. Vector3 splits = shadowCascadeSplit.vector3Value;
  28. cascadePartitionSizes = new float[]
  29. {
  30. Mathf.Clamp(splits[0], 0.0f, 1.0f),
  31. Mathf.Clamp(splits[1] - splits[0], 0.0f, 1.0f),
  32. Mathf.Clamp(splits[2] - splits[1], 0.0f, 1.0f)
  33. };
  34. }
  35. if (cascadePartitionSizes != null)
  36. {
  37. EditorGUI.BeginChangeCheck();
  38. ShadowCascadeSplitGUI.HandleCascadeSliderGUI(ref cascadePartitionSizes);
  39. if (EditorGUI.EndChangeCheck())
  40. {
  41. if (type == typeof(float))
  42. shadowCascadeSplit.floatValue = cascadePartitionSizes[0];
  43. else
  44. {
  45. Vector3 updatedValue = new Vector3();
  46. updatedValue[0] = cascadePartitionSizes[0];
  47. updatedValue[1] = updatedValue[0] + cascadePartitionSizes[1];
  48. updatedValue[2] = updatedValue[1] + cascadePartitionSizes[2];
  49. shadowCascadeSplit.vector3Value = updatedValue;
  50. }
  51. }
  52. }
  53. }
  54. }
  55. static partial class ShadowCascadeSplitGUI
  56. {
  57. [Obsolete("This is obsolete, please use HandleCascadeSliderGUI(ref float[] normalizedCascadePartitions, float distance, EditorUtils.Unit unit) instead.", false)]
  58. public static void HandleCascadeSliderGUI(ref float[] normalizedCascadePartitions)
  59. {
  60. EditorGUILayout.BeginHorizontal();
  61. GUILayout.Space(EditorGUI.indentLevel * 15f);
  62. // get the inspector width since we need it while drawing the partition rects.
  63. // Only way currently is to reserve the block in the layout using GetRect(), and then immediately drawing the empty box
  64. // to match the call to GetRect.
  65. // From this point on, we move to non-layout based code.
  66. var sliderRect = GUILayoutUtility.GetRect(GUIContent.none
  67. , s_CascadeSliderBG
  68. , GUILayout.Height(kSliderbarTopMargin + kSliderbarHeight + kSliderbarBottomMargin)
  69. , GUILayout.ExpandWidth(true));
  70. GUI.Box(sliderRect, GUIContent.none);
  71. EditorGUILayout.EndHorizontal();
  72. float currentX = sliderRect.x;
  73. float cascadeBoxStartY = sliderRect.y + kSliderbarTopMargin;
  74. float cascadeSliderWidth = sliderRect.width - (normalizedCascadePartitions.Length * kPartitionHandleWidth);
  75. Color origTextColor = GUI.color;
  76. Color origBackgroundColor = GUI.backgroundColor;
  77. int colorIndex = -1;
  78. // setup the array locally with the last partition
  79. float[] adjustedCascadePartitions = new float[normalizedCascadePartitions.Length + 1];
  80. System.Array.Copy(normalizedCascadePartitions, adjustedCascadePartitions, normalizedCascadePartitions.Length);
  81. adjustedCascadePartitions[adjustedCascadePartitions.Length - 1] = 1.0f - normalizedCascadePartitions.Sum();
  82. // check for user input on any of the partition handles
  83. // this mechanism gets the current event in the queue... make sure that the mouse is over our control before consuming the event
  84. int sliderControlId = GUIUtility.GetControlID(s_CascadeSliderId, FocusType.Passive);
  85. Event currentEvent = Event.current;
  86. int hotPartitionHandleIndex = -1; // the index of any partition handle that we are hovering over or dragging
  87. // draw each cascade partition
  88. for (int i = 0; i < adjustedCascadePartitions.Length; ++i)
  89. {
  90. float currentPartition = adjustedCascadePartitions[i];
  91. colorIndex = (colorIndex + 1) % kCascadeColors.Length;
  92. GUI.backgroundColor = kCascadeColors[colorIndex];
  93. float boxLength = (cascadeSliderWidth * currentPartition);
  94. // main cascade box
  95. Rect partitionRect = new Rect(currentX, cascadeBoxStartY, boxLength, kSliderbarHeight);
  96. GUI.Box(partitionRect, GUIContent.none, s_CascadeSliderBG);
  97. currentX += boxLength;
  98. // cascade box percentage text
  99. GUI.color = Color.white;
  100. Rect textRect = partitionRect;
  101. var cascadeText = string.Format("{0}\n{1:F1}%", i, currentPartition * 100.0f);
  102. GUI.Label(textRect, cascadeText, s_TextCenteredStyle);
  103. // no need to draw the partition handle for last box
  104. if (i == adjustedCascadePartitions.Length - 1)
  105. break;
  106. // partition handle
  107. GUI.backgroundColor = Color.black;
  108. Rect handleRect = partitionRect;
  109. handleRect.x = currentX;
  110. handleRect.width = kPartitionHandleWidth;
  111. GUI.Box(handleRect, GUIContent.none, s_CascadeSliderBG);
  112. // we want a thin handle visually (since wide black bar looks bad), but a slightly larger
  113. // hit area for easier manipulation
  114. Rect handleHitRect = handleRect;
  115. handleHitRect.xMin -= kPartitionHandleExtraHitAreaWidth;
  116. handleHitRect.xMax += kPartitionHandleExtraHitAreaWidth;
  117. if (handleHitRect.Contains(currentEvent.mousePosition))
  118. hotPartitionHandleIndex = i;
  119. // add regions to slider where the cursor changes to Resize-Horizontal
  120. if (s_DragCache == null)
  121. {
  122. EditorGUIUtility.AddCursorRect(handleHitRect, MouseCursor.ResizeHorizontal, sliderControlId);
  123. }
  124. currentX += kPartitionHandleWidth;
  125. }
  126. GUI.color = origTextColor;
  127. GUI.backgroundColor = origBackgroundColor;
  128. EventType eventType = currentEvent.GetTypeForControl(sliderControlId);
  129. switch (eventType)
  130. {
  131. case EventType.MouseDown:
  132. if (hotPartitionHandleIndex >= 0)
  133. {
  134. s_DragCache = new DragCache(hotPartitionHandleIndex, normalizedCascadePartitions[hotPartitionHandleIndex], currentEvent.mousePosition);
  135. if (GUIUtility.hotControl == 0)
  136. GUIUtility.hotControl = sliderControlId;
  137. currentEvent.Use();
  138. // Switch active scene view into shadow cascades visualization mode, once we start
  139. // tweaking cascade splits.
  140. if (s_RestoreSceneView == null)
  141. {
  142. s_RestoreSceneView = SceneView.lastActiveSceneView;
  143. if (s_RestoreSceneView != null)
  144. {
  145. s_OldSceneDrawMode = s_RestoreSceneView.cameraMode;
  146. #if UNITY_2019_1_OR_NEWER
  147. s_OldSceneLightingMode = s_RestoreSceneView.sceneLighting;
  148. #else
  149. s_OldSceneLightingMode = s_RestoreSceneView.m_SceneLighting;
  150. #endif
  151. s_RestoreSceneView.cameraMode = SceneView.GetBuiltinCameraMode(DrawCameraMode.ShadowCascades);
  152. }
  153. }
  154. }
  155. break;
  156. case EventType.MouseUp:
  157. // mouseUp event anywhere should release the hotcontrol (if it belongs to us), drags (if any)
  158. if (GUIUtility.hotControl == sliderControlId)
  159. {
  160. GUIUtility.hotControl = 0;
  161. currentEvent.Use();
  162. }
  163. s_DragCache = null;
  164. // Restore previous scene view drawing mode once we stop tweaking cascade splits.
  165. if (s_RestoreSceneView != null)
  166. {
  167. s_RestoreSceneView.cameraMode = s_OldSceneDrawMode;
  168. #if UNITY_2019_1_OR_NEWER
  169. s_RestoreSceneView.sceneLighting = s_OldSceneLightingMode;
  170. #else
  171. s_RestoreSceneView.m_SceneLighting = s_OldSceneLightingMode;
  172. #endif
  173. s_RestoreSceneView = null;
  174. }
  175. break;
  176. case EventType.MouseDrag:
  177. if (GUIUtility.hotControl != sliderControlId)
  178. break;
  179. // convert the mouse movement to normalized cascade width. Make sure that we are safe to apply the delta before using it.
  180. float delta = (currentEvent.mousePosition - s_DragCache.m_LastCachedMousePosition).x / cascadeSliderWidth;
  181. bool isLeftPartitionHappy = ((adjustedCascadePartitions[s_DragCache.m_ActivePartition] + delta) > 0.0f);
  182. bool isRightPartitionHappy = ((adjustedCascadePartitions[s_DragCache.m_ActivePartition + 1] - delta) > 0.0f);
  183. if (isLeftPartitionHappy && isRightPartitionHappy)
  184. {
  185. s_DragCache.m_NormalizedPartitionSize += delta;
  186. normalizedCascadePartitions[s_DragCache.m_ActivePartition] = s_DragCache.m_NormalizedPartitionSize;
  187. if (s_DragCache.m_ActivePartition < normalizedCascadePartitions.Length - 1)
  188. normalizedCascadePartitions[s_DragCache.m_ActivePartition + 1] -= delta;
  189. GUI.changed = true;
  190. }
  191. s_DragCache.m_LastCachedMousePosition = currentEvent.mousePosition;
  192. currentEvent.Use();
  193. break;
  194. }
  195. }
  196. }
  197. }