PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/Library/PackageCache/com.unity.render-pipelines.universal@10.8.1/Editor/Shadow/ShadowCascadeSplitGUI.cs

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