RenderStateDataEditor.cs 4.3 KB

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