RenderStateDataEditor.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Rendering.Universal;
  4. namespace UnityEditor.Rendering.Universal
  5. {
  6. [CustomPropertyDrawer(typeof(StencilStateData), true)]
  7. internal class StencilStateDataDrawer : PropertyDrawer
  8. {
  9. class Styles
  10. {
  11. public static readonly GUIContent overrideStencil =
  12. new GUIContent("Stencil", "Override stencil rendering.");
  13. public static readonly GUIContent stencilValue = new GUIContent("Value",
  14. "The stencil index to write to.");
  15. public static readonly GUIContent stencilFunction = new GUIContent("Compare Function",
  16. "Choose the comparison function against the stencil value on screen.");
  17. public static readonly GUIContent stencilPass =
  18. new GUIContent("Pass", "What happens to the stencil value when passing.");
  19. public static readonly GUIContent stencilFail =
  20. new GUIContent("Fail", "What happens to the stencil value when failing.");
  21. public static readonly GUIContent stencilZFail =
  22. new GUIContent("Z Fail", "What happens to the stencil value when failing Z testing.");
  23. }
  24. //Stencil rendering
  25. private const int stencilBits = 4;
  26. private const int minStencilValue = 0;
  27. private const int maxStencilValue = (1 << stencilBits) - 1;
  28. //Stencil props
  29. private SerializedProperty m_OverrideStencil;
  30. private SerializedProperty m_StencilIndex;
  31. private SerializedProperty m_StencilFunction;
  32. private SerializedProperty m_StencilPass;
  33. private SerializedProperty m_StencilFail;
  34. private SerializedProperty m_StencilZFail;
  35. private List<SerializedObject> m_properties = new List<SerializedObject>();
  36. void Init(SerializedProperty property)
  37. {
  38. //Stencil
  39. m_OverrideStencil = property.FindPropertyRelative("overrideStencilState");
  40. m_StencilIndex = property.FindPropertyRelative("stencilReference");
  41. m_StencilFunction = property.FindPropertyRelative("stencilCompareFunction");
  42. m_StencilPass = property.FindPropertyRelative("passOperation");
  43. m_StencilFail = property.FindPropertyRelative("failOperation");
  44. m_StencilZFail = property.FindPropertyRelative("zFailOperation");
  45. m_properties.Add(property.serializedObject);
  46. }
  47. public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
  48. {
  49. if(!m_properties.Contains(property.serializedObject))
  50. Init(property);
  51. rect.height = EditorGUIUtility.singleLineHeight;
  52. EditorGUI.PropertyField(rect, m_OverrideStencil, Styles.overrideStencil);
  53. if (m_OverrideStencil.boolValue)
  54. {
  55. EditorGUI.indentLevel++;
  56. rect.y += EditorUtils.Styles.defaultLineSpace;
  57. //Stencil value
  58. EditorGUI.BeginChangeCheck();
  59. var stencilVal = m_StencilIndex.intValue;
  60. stencilVal = EditorGUI.IntSlider(rect, Styles.stencilValue, stencilVal, minStencilValue, maxStencilValue);
  61. if (EditorGUI.EndChangeCheck())
  62. m_StencilIndex.intValue = stencilVal;
  63. rect.y += EditorUtils.Styles.defaultLineSpace;
  64. //Stencil compare options
  65. EditorGUI.PropertyField(rect, m_StencilFunction, Styles.stencilFunction);
  66. rect.y += EditorUtils.Styles.defaultLineSpace;
  67. EditorGUI.indentLevel++;
  68. EditorGUI.PropertyField(rect, m_StencilPass, Styles.stencilPass);
  69. rect.y += EditorUtils.Styles.defaultLineSpace;
  70. EditorGUI.PropertyField(rect, m_StencilFail, Styles.stencilFail);
  71. rect.y += EditorUtils.Styles.defaultLineSpace;
  72. EditorGUI.indentLevel--;
  73. //Stencil compare options
  74. EditorGUI.PropertyField(rect, m_StencilZFail, Styles.stencilZFail);
  75. EditorGUI.indentLevel--;
  76. }
  77. }
  78. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  79. {
  80. if (m_properties.Contains(property.serializedObject) && m_OverrideStencil != null && m_OverrideStencil.boolValue)
  81. return EditorUtils.Styles.defaultLineSpace * 6;
  82. return EditorUtils.Styles.defaultLineSpace * 1;
  83. }
  84. }
  85. }