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.

204 lines
6.9 KiB

  1. using UnityEngine;
  2. using UnityEngine.PostProcessing;
  3. namespace UnityEditor.PostProcessing
  4. {
  5. using Settings = BloomModel.Settings;
  6. [PostProcessingModelEditor(typeof(BloomModel))]
  7. public class BloomModelEditor : PostProcessingModelEditor
  8. {
  9. struct BloomSettings
  10. {
  11. public SerializedProperty intensity;
  12. public SerializedProperty threshold;
  13. public SerializedProperty softKnee;
  14. public SerializedProperty radius;
  15. public SerializedProperty antiFlicker;
  16. }
  17. struct LensDirtSettings
  18. {
  19. public SerializedProperty texture;
  20. public SerializedProperty intensity;
  21. }
  22. BloomSettings m_Bloom;
  23. LensDirtSettings m_LensDirt;
  24. public override void OnEnable()
  25. {
  26. m_Bloom = new BloomSettings
  27. {
  28. intensity = FindSetting((Settings x) => x.bloom.intensity),
  29. threshold = FindSetting((Settings x) => x.bloom.threshold),
  30. softKnee = FindSetting((Settings x) => x.bloom.softKnee),
  31. radius = FindSetting((Settings x) => x.bloom.radius),
  32. antiFlicker = FindSetting((Settings x) => x.bloom.antiFlicker)
  33. };
  34. m_LensDirt = new LensDirtSettings
  35. {
  36. texture = FindSetting((Settings x) => x.lensDirt.texture),
  37. intensity = FindSetting((Settings x) => x.lensDirt.intensity)
  38. };
  39. }
  40. public override void OnInspectorGUI()
  41. {
  42. EditorGUILayout.Space();
  43. PrepareGraph();
  44. DrawGraph();
  45. EditorGUILayout.Space();
  46. EditorGUILayout.PropertyField(m_Bloom.intensity);
  47. EditorGUILayout.PropertyField(m_Bloom.threshold, EditorGUIHelper.GetContent("Threshold (Gamma)"));
  48. EditorGUILayout.PropertyField(m_Bloom.softKnee);
  49. EditorGUILayout.PropertyField(m_Bloom.radius);
  50. EditorGUILayout.PropertyField(m_Bloom.antiFlicker);
  51. EditorGUILayout.Space();
  52. EditorGUILayout.LabelField("Dirt", EditorStyles.boldLabel);
  53. EditorGUI.indentLevel++;
  54. EditorGUILayout.PropertyField(m_LensDirt.texture);
  55. EditorGUILayout.PropertyField(m_LensDirt.intensity);
  56. EditorGUI.indentLevel--;
  57. }
  58. #region Graph
  59. float m_GraphThreshold;
  60. float m_GraphKnee;
  61. float m_GraphIntensity;
  62. // Number of vertices in curve
  63. const int k_CurveResolution = 48;
  64. // Vertex buffers
  65. Vector3[] m_RectVertices = new Vector3[4];
  66. Vector3[] m_LineVertices = new Vector3[2];
  67. Vector3[] m_CurveVertices = new Vector3[k_CurveResolution];
  68. Rect m_RectGraph;
  69. float m_RangeX;
  70. float m_RangeY;
  71. float ResponseFunction(float x)
  72. {
  73. var rq = Mathf.Clamp(x - m_GraphThreshold + m_GraphKnee, 0, m_GraphKnee * 2);
  74. rq = rq * rq * 0.25f / m_GraphKnee;
  75. return Mathf.Max(rq, x - m_GraphThreshold) * m_GraphIntensity;
  76. }
  77. // Transform a point into the graph rect
  78. Vector3 PointInRect(float x, float y)
  79. {
  80. x = Mathf.Lerp(m_RectGraph.x, m_RectGraph.xMax, x / m_RangeX);
  81. y = Mathf.Lerp(m_RectGraph.yMax, m_RectGraph.y, y / m_RangeY);
  82. return new Vector3(x, y, 0);
  83. }
  84. // Draw a line in the graph rect
  85. void DrawLine(float x1, float y1, float x2, float y2, float grayscale)
  86. {
  87. m_LineVertices[0] = PointInRect(x1, y1);
  88. m_LineVertices[1] = PointInRect(x2, y2);
  89. Handles.color = Color.white * grayscale;
  90. Handles.DrawAAPolyLine(2.0f, m_LineVertices);
  91. }
  92. // Draw a rect in the graph rect
  93. void DrawRect(float x1, float y1, float x2, float y2, float fill, float line)
  94. {
  95. m_RectVertices[0] = PointInRect(x1, y1);
  96. m_RectVertices[1] = PointInRect(x2, y1);
  97. m_RectVertices[2] = PointInRect(x2, y2);
  98. m_RectVertices[3] = PointInRect(x1, y2);
  99. Handles.DrawSolidRectangleWithOutline(
  100. m_RectVertices,
  101. fill < 0 ? Color.clear : Color.white * fill,
  102. line < 0 ? Color.clear : Color.white * line
  103. );
  104. }
  105. // Update internal state with a given bloom instance
  106. public void PrepareGraph()
  107. {
  108. var bloom = (BloomModel)target;
  109. m_RangeX = 5f;
  110. m_RangeY = 2f;
  111. m_GraphThreshold = bloom.settings.bloom.thresholdLinear;
  112. m_GraphKnee = bloom.settings.bloom.softKnee * m_GraphThreshold + 1e-5f;
  113. // Intensity is capped to prevent sampling errors
  114. m_GraphIntensity = Mathf.Min(bloom.settings.bloom.intensity, 10f);
  115. }
  116. // Draw the graph at the current position
  117. public void DrawGraph()
  118. {
  119. using (new GUILayout.HorizontalScope())
  120. {
  121. GUILayout.Space(EditorGUI.indentLevel * 15f);
  122. m_RectGraph = GUILayoutUtility.GetRect(128, 80);
  123. }
  124. // Background
  125. DrawRect(0, 0, m_RangeX, m_RangeY, 0.1f, 0.4f);
  126. // Soft-knee range
  127. DrawRect(m_GraphThreshold - m_GraphKnee, 0, m_GraphThreshold + m_GraphKnee, m_RangeY, 0.25f, -1);
  128. // Horizontal lines
  129. for (var i = 1; i < m_RangeY; i++)
  130. DrawLine(0, i, m_RangeX, i, 0.4f);
  131. // Vertical lines
  132. for (var i = 1; i < m_RangeX; i++)
  133. DrawLine(i, 0, i, m_RangeY, 0.4f);
  134. // Label
  135. Handles.Label(
  136. PointInRect(0, m_RangeY) + Vector3.right,
  137. "Brightness Response (linear)", EditorStyles.miniLabel
  138. );
  139. // Threshold line
  140. DrawLine(m_GraphThreshold, 0, m_GraphThreshold, m_RangeY, 0.6f);
  141. // Response curve
  142. var vcount = 0;
  143. while (vcount < k_CurveResolution)
  144. {
  145. var x = m_RangeX * vcount / (k_CurveResolution - 1);
  146. var y = ResponseFunction(x);
  147. if (y < m_RangeY)
  148. {
  149. m_CurveVertices[vcount++] = PointInRect(x, y);
  150. }
  151. else
  152. {
  153. if (vcount > 1)
  154. {
  155. // Extend the last segment to the top edge of the rect.
  156. var v1 = m_CurveVertices[vcount - 2];
  157. var v2 = m_CurveVertices[vcount - 1];
  158. var clip = (m_RectGraph.y - v1.y) / (v2.y - v1.y);
  159. m_CurveVertices[vcount - 1] = v1 + (v2 - v1) * clip;
  160. }
  161. break;
  162. }
  163. }
  164. if (vcount > 1)
  165. {
  166. Handles.color = Color.white * 0.9f;
  167. Handles.DrawAAPolyLine(2.0f, vcount, m_CurveVertices);
  168. }
  169. }
  170. #endregion
  171. }
  172. }