Bez popisu
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.

FullscreenShaderGUI.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using RenderQueue = UnityEngine.Rendering.RenderQueue;
  5. using UnityEditor.ShaderGraph.Drawing;
  6. namespace UnityEditor.Rendering.Fullscreen.ShaderGraph
  7. {
  8. /// <summary>
  9. /// The base class to implement the fullscreen Material GUI in a render pipeline.
  10. /// </summary>
  11. public class FullscreenShaderGUI : ShaderGUI
  12. {
  13. /// <summary>Enum used to store the expanded state of the drawer in the material GUI</summary>
  14. [Flags]
  15. protected enum Expandable
  16. {
  17. /// <summary>Surface Option key for the MaterialHeaderScopeList.RegisterHeaderScope call</summary>
  18. SurfaceOptions = 1 << 0,
  19. /// <summary>Surface Inputs key for the MaterialHeaderScopeList.RegisterHeaderScope call</summary>
  20. SurfaceInputs = 1 << 1,
  21. }
  22. protected class Styles
  23. {
  24. // Categories
  25. /// <summary>Surface Option header name</summary>
  26. public static readonly GUIContent SurfaceOptions =
  27. EditorGUIUtility.TrTextContent("Surface Options", "Controls the rendering states of the fullscreen material.");
  28. /// <summary>Surface Inputs header name</summary>
  29. public static readonly GUIContent SurfaceInputs = EditorGUIUtility.TrTextContent("Surface Inputs",
  30. "These settings describe the look and feel of the surface itself.");
  31. /// <summary>Name and tooltip for the blending mode property in the material GUI</summary>
  32. public static readonly GUIContent blendingMode = EditorGUIUtility.TrTextContent("Blending Mode",
  33. "Controls how the color of the Transparent surface blends with the Material color in the background.");
  34. /// <summary>Name and tooltip for the source color blend mode property in the material GUI</summary>
  35. public static readonly GUIContent srcColorBlendMode = EditorGUIUtility.TrTextContent("Src Color",
  36. "Describes how the input color will be blended.");
  37. /// <summary>Name and tooltip for the destination color blend mode property in the material GUI</summary>
  38. public static readonly GUIContent dstColorBlendMode = EditorGUIUtility.TrTextContent("Dst Color",
  39. "Describes how the destination color will be blended.");
  40. /// <summary>Name and tooltip for the color blend operation property in the material GUI</summary>
  41. public static readonly GUIContent colorBlendOperation = EditorGUIUtility.TrTextContent("Color Blend Op",
  42. "Tell which operation to use when blending the colors. Default is Add.");
  43. /// <summary>Name and tooltip for the source alpha blend mode property in the material GUI</summary>
  44. public static readonly GUIContent srcAlphaBlendMode = EditorGUIUtility.TrTextContent("Src Alpha",
  45. "Describes how the input alpha will be blended.");
  46. /// <summary>Name and tooltip for the destination alpha blend mode property in the material GUI</summary>
  47. public static readonly GUIContent dstAlphaBlendMode = EditorGUIUtility.TrTextContent("Dst Alpha",
  48. "Describes how the input alpha will be blended.");
  49. /// <summary>Name and tooltip for the alpha blend operation property in the material GUI</summary>
  50. public static readonly GUIContent alphaBlendOperation = EditorGUIUtility.TrTextContent("Alpha Blend Op",
  51. "Tell which operation to use when blending the alpha channel. Default is Add.");
  52. /// <summary>Name and tooltip for the depth write property in the material GUI</summary>
  53. public static readonly GUIContent depthWrite = EditorGUIUtility.TrTextContent("Depth Write",
  54. "Controls whether the shader writes depth.");
  55. /// <summary>Name and tooltip for the depth test property in the material GUI</summary>
  56. public static readonly GUIContent depthTest = EditorGUIUtility.TrTextContent("Depth Test",
  57. "Specifies the depth test mode. The default is Always.");
  58. /// <summary>Name and tooltip for the stencil override property in the material GUI</summary>
  59. public static readonly GUIContent stencil = EditorGUIUtility.TrTextContent("Stencil Override", "Enable the stencil block in the shader.");
  60. /// <summary>Name and tooltip for the stencil reference property in the material GUI</summary>
  61. public static readonly GUIContent stencilRef = EditorGUIUtility.TrTextContent("Reference", "Reference value use for comparison and operations.");
  62. /// <summary>Name and tooltip for the stencil read mask property in the material GUI</summary>
  63. public static readonly GUIContent stencilReadMask = EditorGUIUtility.TrTextContent("Read Mask", "Tells which bit are allowed to be read during the stencil test.");
  64. /// <summary>Name and tooltip for the stencil write mask property in the material GUI</summary>
  65. public static readonly GUIContent stencilWriteMask = EditorGUIUtility.TrTextContent("Write Mask", "Tells which bit are allowed to be written during the stencil test.");
  66. /// <summary>Name and tooltip for the stencil comparison property in the material GUI</summary>
  67. public static readonly GUIContent stencilComparison = EditorGUIUtility.TrTextContent("Comparison", "Tells which function to use when doing the stencil test.");
  68. /// <summary>Name and tooltip for the stencil pass operation property in the material GUI</summary>
  69. public static readonly GUIContent stencilPass = EditorGUIUtility.TrTextContent("Pass", "Tells what to do when the stencil test succeed.");
  70. /// <summary>Name and tooltip for the stencil fail operation property in the material GUI</summary>
  71. public static readonly GUIContent stencilFail = EditorGUIUtility.TrTextContent("Fail", "Tells what to do when the stencil test fails.");
  72. /// <summary>Name and tooltip for the stencil depth fail operation property in the material GUI</summary>
  73. public static readonly GUIContent stencilDepthFail = EditorGUIUtility.TrTextContent("Depth Fail", "Tells what to do when the depth test fails.");
  74. }
  75. bool m_FirstTimeApply = true;
  76. // By default, everything is expanded
  77. readonly MaterialHeaderScopeList m_MaterialScopeList = new MaterialHeaderScopeList(uint.MaxValue);
  78. // These have to be stored due to how MaterialHeaderScopeList callbacks work (they don't provide this data in the callbacks)
  79. MaterialEditor m_MaterialEditor;
  80. MaterialProperty[] m_Properties;
  81. private const int queueOffsetRange = 50;
  82. /// <summary>
  83. /// Unity calls this function when it displays the GUI. To implement your custom GUI, override this function..
  84. /// </summary>
  85. /// <param name="materialEditor">Material editor instance.</param>
  86. /// <param name="properties">The list of properties in the inspected material(s).</param>
  87. override public void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
  88. {
  89. m_MaterialEditor = materialEditor;
  90. m_Properties = properties;
  91. Material targetMat = materialEditor.target as Material;
  92. if (m_FirstTimeApply)
  93. {
  94. OnOpenGUI(targetMat, materialEditor, properties);
  95. m_FirstTimeApply = false;
  96. }
  97. ShaderPropertiesGUI(materialEditor, targetMat, properties);
  98. }
  99. /// <summary>
  100. /// Unity calls this function when it displays the GUI. To implement your custom GUI, override this function..
  101. /// </summary>
  102. /// <param name="materialEditor">Material editor instance.</param>
  103. /// <param name="properties">The list of properties in the inspected material(s).</param>
  104. /// <param name="material">The target materials for this GUI.</param>
  105. public virtual void OnOpenGUI(Material material, MaterialEditor materialEditor, MaterialProperty[] properties)
  106. {
  107. // Generate the foldouts
  108. m_MaterialScopeList.RegisterHeaderScope(Styles.SurfaceOptions, (uint)Expandable.SurfaceOptions, DrawSurfaceOptions);
  109. m_MaterialScopeList.RegisterHeaderScope(Styles.SurfaceInputs, (uint)Expandable.SurfaceInputs, DrawSurfaceInputs);
  110. }
  111. /// <summary>
  112. /// Assign a new FullscreenShader to the target material.
  113. /// </summary>
  114. /// <param name="material">A valid material using a Fullscreen Shader Graph.</param>
  115. /// <param name="oldShader"></param>
  116. /// <param name="newShader"></param>
  117. public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
  118. {
  119. // Clear all keywords for fresh start
  120. // Note: this will nuke user-selected custom keywords when they change shaders
  121. material.shaderKeywords = null;
  122. base.AssignNewShaderToMaterial(material, oldShader, newShader);
  123. // Setup keywords based on the new shader
  124. ValidateMaterial(material);
  125. }
  126. void ShaderPropertiesGUI(MaterialEditor materialEditor, Material material, MaterialProperty[] properties)
  127. {
  128. m_MaterialScopeList.DrawHeaders(materialEditor, material);
  129. }
  130. /// <summary>
  131. /// Draw the Surface Options section of the fullscreen shader GUI.
  132. /// </summary>
  133. /// <param name="material">A valid material using a Fullscreen Shader Graph.</param>
  134. protected virtual void DrawSurfaceOptions(Material material)
  135. {
  136. var materialEditor = m_MaterialEditor;
  137. var properties = m_Properties;
  138. var blendMode = FindProperty(FullscreenUniforms.blendModeProperty, properties, false);
  139. var srcColorBlend = FindProperty(FullscreenUniforms.srcColorBlendProperty, properties, false);
  140. var dstColorBlend = FindProperty(FullscreenUniforms.dstColorBlendProperty, properties, false);
  141. var srcAlphaBlend = FindProperty(FullscreenUniforms.srcAlphaBlendProperty, properties, false);
  142. var dstAlphaBlend = FindProperty(FullscreenUniforms.dstAlphaBlendProperty, properties, false);
  143. var colorBlendOp = FindProperty(FullscreenUniforms.colorBlendOperationProperty, properties, false);
  144. var alphaBlendOp = FindProperty(FullscreenUniforms.alphaBlendOperationProperty, properties, false);
  145. var depthWrite = FindProperty(FullscreenUniforms.depthWriteProperty, properties, false);
  146. var depthTest = FindProperty(FullscreenUniforms.depthTestProperty, properties, false);
  147. var stencilEnable = FindProperty(FullscreenUniforms.stencilEnableProperty, properties, false);
  148. var stencilRef = FindProperty(FullscreenUniforms.stencilReferenceProperty, properties, false);
  149. var stencilReadMask = FindProperty(FullscreenUniforms.stencilReadMaskProperty, properties, false);
  150. var stencilWriteMask = FindProperty(FullscreenUniforms.stencilWriteMaskProperty, properties, false);
  151. var stencilComp = FindProperty(FullscreenUniforms.stencilComparisonProperty, properties, false);
  152. var stencilPass = FindProperty(FullscreenUniforms.stencilPassProperty, properties, false);
  153. var stencilFail = FindProperty(FullscreenUniforms.stencilFailProperty, properties, false);
  154. var stencilDepthFail = FindProperty(FullscreenUniforms.stencilDepthFailProperty, properties, false);
  155. if (material.HasProperty(FullscreenUniforms.blendModeProperty))
  156. {
  157. EditorGUI.BeginChangeCheck();
  158. m_MaterialEditor.ShaderProperty(blendMode, Styles.blendingMode);
  159. FullscreenBlendMode blendModeValue = (FullscreenBlendMode)blendMode.floatValue;
  160. if (EditorGUI.EndChangeCheck())
  161. SetBlendMode(blendModeValue);
  162. if (blendModeValue == FullscreenBlendMode.Custom)
  163. {
  164. m_MaterialEditor.ShaderProperty(srcColorBlend, Styles.srcColorBlendMode, 1);
  165. m_MaterialEditor.ShaderProperty(dstColorBlend, Styles.dstColorBlendMode, 1);
  166. m_MaterialEditor.ShaderProperty(colorBlendOp, Styles.colorBlendOperation, 1);
  167. m_MaterialEditor.ShaderProperty(srcAlphaBlend, Styles.srcAlphaBlendMode, 1);
  168. m_MaterialEditor.ShaderProperty(dstAlphaBlend, Styles.dstAlphaBlendMode, 1);
  169. m_MaterialEditor.ShaderProperty(alphaBlendOp, Styles.alphaBlendOperation, 1);
  170. }
  171. }
  172. if (material.HasProperty(FullscreenUniforms.depthWriteProperty))
  173. m_MaterialEditor.ShaderProperty(depthWrite, Styles.depthWrite);
  174. if (material.HasProperty(FullscreenUniforms.depthTestProperty))
  175. m_MaterialEditor.ShaderProperty(depthTest, Styles.depthTest);
  176. if (material.HasProperty(FullscreenUniforms.stencilEnableProperty))
  177. {
  178. EditorGUI.BeginChangeCheck();
  179. m_MaterialEditor.ShaderProperty(stencilEnable, Styles.stencil);
  180. bool stencilEnableValue = stencilEnable.floatValue > 0.5f;
  181. if (EditorGUI.EndChangeCheck())
  182. SetStencilEnable(stencilEnableValue);
  183. if (stencilEnableValue)
  184. {
  185. m_MaterialEditor.ShaderProperty(stencilRef, Styles.stencilRef, 1);
  186. m_MaterialEditor.ShaderProperty(stencilReadMask, Styles.stencilReadMask, 1);
  187. m_MaterialEditor.ShaderProperty(stencilWriteMask, Styles.stencilWriteMask, 1);
  188. m_MaterialEditor.ShaderProperty(stencilComp, Styles.stencilComparison, 1);
  189. m_MaterialEditor.ShaderProperty(stencilPass, Styles.stencilPass, 1);
  190. m_MaterialEditor.ShaderProperty(stencilFail, Styles.stencilFail, 1);
  191. m_MaterialEditor.ShaderProperty(stencilDepthFail, Styles.stencilDepthFail, 1);
  192. }
  193. }
  194. void SetStencilEnable(bool enabled)
  195. {
  196. if (!enabled)
  197. {
  198. stencilComp.floatValue = (float)CompareFunction.Always;
  199. stencilPass.floatValue = (float)StencilOp.Keep;
  200. stencilFail.floatValue = (float)StencilOp.Keep;
  201. stencilDepthFail.floatValue = (float)StencilOp.Keep;
  202. }
  203. }
  204. void SetBlendMode(FullscreenBlendMode blendMode)
  205. {
  206. // Note that we can't disable the blend mode from here
  207. if (blendMode == FullscreenBlendMode.Alpha || blendMode == FullscreenBlendMode.Disabled)
  208. {
  209. srcColorBlend.floatValue = (float)BlendMode.SrcAlpha;
  210. dstColorBlend.floatValue = (float)BlendMode.OneMinusSrcAlpha;
  211. srcAlphaBlend.floatValue = (float)BlendMode.One;
  212. dstAlphaBlend.floatValue = (float)BlendMode.OneMinusSrcAlpha;
  213. }
  214. else if (blendMode == FullscreenBlendMode.Premultiply)
  215. {
  216. srcColorBlend.floatValue = (float)BlendMode.One;
  217. dstColorBlend.floatValue = (float)BlendMode.OneMinusSrcAlpha;
  218. srcAlphaBlend.floatValue = (float)BlendMode.One;
  219. dstAlphaBlend.floatValue = (float)BlendMode.OneMinusSrcAlpha;
  220. }
  221. else if (blendMode == FullscreenBlendMode.Additive)
  222. {
  223. srcColorBlend.floatValue = (float)BlendMode.SrcAlpha;
  224. dstColorBlend.floatValue = (float)BlendMode.One;
  225. srcAlphaBlend.floatValue = (float)BlendMode.One;
  226. dstAlphaBlend.floatValue = (float)BlendMode.One;
  227. }
  228. else if (blendMode == FullscreenBlendMode.Multiply)
  229. {
  230. srcColorBlend.floatValue = (float)BlendMode.DstColor;
  231. dstColorBlend.floatValue = (float)BlendMode.Zero;
  232. srcAlphaBlend.floatValue = (float)BlendMode.One;
  233. dstAlphaBlend.floatValue = (float)BlendMode.OneMinusSrcAlpha;
  234. }
  235. colorBlendOp.floatValue = (float)BlendOp.Add;
  236. alphaBlendOp.floatValue = (float)BlendOp.Add;
  237. }
  238. }
  239. /// <summary>
  240. /// Draw the Surface Inputs section of the fullscreen shader GUI.
  241. /// </summary>
  242. /// <param name="material">A valid material using a Fullscreen Shader Graph.</param>
  243. protected virtual void DrawSurfaceInputs(Material material)
  244. {
  245. DrawShaderGraphProperties(m_MaterialEditor, material, m_Properties);
  246. }
  247. static void DrawShaderGraphProperties(MaterialEditor materialEditor, Material material, MaterialProperty[] properties)
  248. {
  249. if (properties == null)
  250. return;
  251. ShaderGraphPropertyDrawers.DrawShaderGraphGUI(materialEditor, properties);
  252. }
  253. /// <summary>
  254. /// Ensures that the material is correctly setup.
  255. /// </summary>
  256. /// <param name="material">A valid material using a Fullscreen Shader Graph.</param>
  257. public override void ValidateMaterial(Material material) => SetupSurface(material);
  258. /// <summary>
  259. /// Setup the fullscreen shader keywords from the material properties.
  260. /// </summary>
  261. /// <param name="material">A valid material using a Fullscreen Shader Graph.</param>
  262. public static void SetupSurface(Material material)
  263. {
  264. // For now there is no keyword in FullScreenShader.
  265. }
  266. }
  267. }