You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
7.4 KiB

  1. using UnityEngine;
  2. using UnityEngine.PostProcessing;
  3. namespace UnityEditor.PostProcessing
  4. {
  5. using Settings = MotionBlurModel.Settings;
  6. [PostProcessingModelEditor(typeof(MotionBlurModel))]
  7. public class MotionBlurModelEditor : PostProcessingModelEditor
  8. {
  9. SerializedProperty m_ShutterAngle;
  10. SerializedProperty m_SampleCount;
  11. SerializedProperty m_FrameBlending;
  12. GraphDrawer m_GraphDrawer;
  13. class GraphDrawer
  14. {
  15. const float k_Height = 32f;
  16. Texture m_BlendingIcon;
  17. GUIStyle m_LowerCenterStyle;
  18. GUIStyle m_MiddleCenterStyle;
  19. Color m_ColorDark;
  20. Color m_ColorGray;
  21. Vector3[] m_RectVertices = new Vector3[4];
  22. public GraphDrawer()
  23. {
  24. m_BlendingIcon = EditorResources.Load<Texture>("UI/MotionBlendingIcon.png");
  25. m_LowerCenterStyle = new GUIStyle(EditorStyles.miniLabel) { alignment = TextAnchor.LowerCenter };
  26. m_MiddleCenterStyle = new GUIStyle(EditorStyles.miniLabel) { alignment = TextAnchor.MiddleCenter };
  27. if (EditorGUIUtility.isProSkin)
  28. {
  29. m_ColorDark = new Color(0.18f, 0.18f, 0.18f);
  30. m_ColorGray = new Color(0.43f, 0.43f, 0.43f);
  31. }
  32. else
  33. {
  34. m_ColorDark = new Color(0.64f, 0.64f, 0.64f);
  35. m_ColorGray = new Color(0.92f, 0.92f, 0.92f);
  36. }
  37. }
  38. public void DrawShutterGraph(float angle)
  39. {
  40. var center = GUILayoutUtility.GetRect(128, k_Height).center;
  41. // Parameters used to make transitions smooth.
  42. var zeroWhenOff = Mathf.Min(1f, angle * 0.1f);
  43. var zeroWhenFull = Mathf.Min(1f, (360f - angle) * 0.02f);
  44. // Shutter angle graph
  45. var discCenter = center - new Vector2(k_Height * 2.4f, 0f);
  46. // - exposure duration indicator
  47. DrawDisc(discCenter, k_Height * Mathf.Lerp(0.5f, 0.38f, zeroWhenFull), m_ColorGray);
  48. // - shutter disc
  49. DrawDisc(discCenter, k_Height * 0.16f * zeroWhenFull, m_ColorDark);
  50. // - shutter blade
  51. DrawArc(discCenter, k_Height * 0.5f, 360f - angle, m_ColorDark);
  52. // - shutter axis
  53. DrawDisc(discCenter, zeroWhenOff, m_ColorGray);
  54. // Shutter label (off/full)
  55. var labelSize = new Vector2(k_Height, k_Height);
  56. var labelOrigin = discCenter - labelSize * 0.5f;
  57. var labelRect = new Rect(labelOrigin, labelSize);
  58. if (Mathf.Approximately(angle, 0f))
  59. GUI.Label(labelRect, "Off", m_MiddleCenterStyle);
  60. else if (Mathf.Approximately(angle, 360f))
  61. GUI.Label(labelRect, "Full", m_MiddleCenterStyle);
  62. // Exposure time bar graph
  63. var outerBarSize = new Vector2(4.75f, 0.5f) * k_Height;
  64. var innerBarSize = outerBarSize;
  65. innerBarSize.x *= angle / 360f;
  66. var barCenter = center + new Vector2(k_Height * 0.9f, 0f);
  67. var barOrigin = barCenter - outerBarSize * 0.5f;
  68. DrawRect(barOrigin, outerBarSize, m_ColorDark);
  69. DrawRect(barOrigin, innerBarSize, m_ColorGray);
  70. var barText = "Exposure time = " + (angle / 3.6f).ToString("0") + "% of ΔT";
  71. GUI.Label(new Rect(barOrigin, outerBarSize), barText, m_MiddleCenterStyle);
  72. }
  73. public void DrawBlendingGraph(float strength)
  74. {
  75. var center = GUILayoutUtility.GetRect(128, k_Height).center;
  76. var iconSize = new Vector2(k_Height, k_Height);
  77. var iconStride = new Vector2(k_Height * 0.9f, 0f);
  78. var iconOrigin = center - iconSize * 0.5f - iconStride * 2f;
  79. for (var i = 0; i < 5; i++)
  80. {
  81. var weight = BlendingWeight(strength, i / 60f);
  82. var rect = new Rect(iconOrigin + iconStride * i, iconSize);
  83. var color = m_ColorGray;
  84. color.a = weight;
  85. GUI.color = color;
  86. GUI.Label(rect, m_BlendingIcon);
  87. GUI.color = Color.white;
  88. GUI.Label(rect, (weight * 100).ToString("0") + "%", m_LowerCenterStyle);
  89. }
  90. // EditorGUIUtility.isProSkin
  91. }
  92. // Weight function for multi frame blending
  93. float BlendingWeight(float strength, float time)
  94. {
  95. if (strength > 0f || Mathf.Approximately(time, 0f))
  96. return Mathf.Exp(-time * Mathf.Lerp(80f, 10f, strength));
  97. return 0;
  98. }
  99. // Draw a solid disc in the graph rect.
  100. void DrawDisc(Vector2 center, float radius, Color fill)
  101. {
  102. Handles.color = fill;
  103. Handles.DrawSolidDisc(center, Vector3.forward, radius);
  104. }
  105. // Draw an arc in the graph rect.
  106. void DrawArc(Vector2 center, float radius, float angle, Color fill)
  107. {
  108. var start = new Vector2(
  109. -Mathf.Cos(Mathf.Deg2Rad * angle / 2f),
  110. Mathf.Sin(Mathf.Deg2Rad * angle / 2f)
  111. );
  112. Handles.color = fill;
  113. Handles.DrawSolidArc(center, Vector3.forward, start, angle, radius);
  114. }
  115. // Draw a rectangle in the graph rect.
  116. void DrawRect(Vector2 origin, Vector2 size, Color color)
  117. {
  118. var p0 = origin;
  119. var p1 = origin + size;
  120. m_RectVertices[0] = p0;
  121. m_RectVertices[1] = new Vector2(p1.x, p0.y);
  122. m_RectVertices[2] = p1;
  123. m_RectVertices[3] = new Vector2(p0.x, p1.y);
  124. Handles.color = Color.white;
  125. Handles.DrawSolidRectangleWithOutline(m_RectVertices, color, Color.clear);
  126. }
  127. }
  128. public override void OnEnable()
  129. {
  130. m_ShutterAngle = FindSetting((Settings x) => x.shutterAngle);
  131. m_SampleCount = FindSetting((Settings x) => x.sampleCount);
  132. m_FrameBlending = FindSetting((Settings x) => x.frameBlending);
  133. }
  134. public override void OnInspectorGUI()
  135. {
  136. if (m_GraphDrawer == null)
  137. m_GraphDrawer = new GraphDrawer();
  138. EditorGUILayout.LabelField("Shutter Speed Simulation", EditorStyles.boldLabel);
  139. EditorGUI.indentLevel++;
  140. m_GraphDrawer.DrawShutterGraph(m_ShutterAngle.floatValue);
  141. EditorGUILayout.PropertyField(m_ShutterAngle);
  142. EditorGUILayout.PropertyField(m_SampleCount);
  143. EditorGUI.indentLevel--;
  144. EditorGUILayout.Space();
  145. EditorGUILayout.LabelField("Multiple Frame Blending", EditorStyles.boldLabel);
  146. EditorGUI.indentLevel++;
  147. float fbValue = m_FrameBlending.floatValue;
  148. m_GraphDrawer.DrawBlendingGraph(fbValue);
  149. EditorGUILayout.PropertyField(m_FrameBlending);
  150. if (fbValue > 0f)
  151. EditorGUILayout.HelpBox("Multi-Frame Blending lowers precision of the final picture for optimization purposes.", MessageType.Info);
  152. EditorGUI.indentLevel--;
  153. }
  154. }
  155. }