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.

272 lines
8.1 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace AmplifyShaderEditor
  6. {
  7. public enum ZWriteMode
  8. {
  9. On,
  10. Off
  11. }
  12. public enum ZTestMode
  13. {
  14. Less,
  15. Greater,
  16. LEqual,
  17. GEqual,
  18. Equal,
  19. NotEqual,
  20. Always
  21. }
  22. [Serializable]
  23. class ZBufferOpHelper
  24. {
  25. public static readonly string DepthParametersStr = " Depth";
  26. public static readonly string ZWriteModeStr = "ZWrite Mode";
  27. public static readonly string ZTestModeStr = "ZTest Mode";
  28. public static readonly string OffsetStr = "Offset";
  29. public static readonly string OffsetFactorStr = "Factor";
  30. public static readonly string OffsetUnitsStr = "Units";
  31. private const string ExtraDepthPassStr = "Extra Depth Pass";
  32. private const string DepthZTestStr = "Depth ZTest";
  33. public static readonly string[] ZTestModeLabels =
  34. {
  35. "<Default>",
  36. "Less",
  37. "Greater",
  38. "Less or Equal",
  39. "Greater or Equal",
  40. "Equal",
  41. "Not Equal",
  42. "Always"
  43. };
  44. public static readonly string[] ZTestModeValues =
  45. {
  46. "<Default>",
  47. "Less",
  48. "Greater",
  49. "LEqual",
  50. "GEqual",
  51. "Equal",
  52. "NotEqual",
  53. "Always"
  54. };
  55. public static readonly string[] ZWriteModeValues =
  56. {
  57. "<Default>",
  58. "On",
  59. "Off"
  60. };
  61. public static readonly Dictionary<ZTestMode, int> ZTestModeDict = new Dictionary<ZTestMode, int>
  62. {
  63. {ZTestMode.Less,1 },
  64. {ZTestMode.Greater,2},
  65. {ZTestMode.LEqual,3},
  66. {ZTestMode.GEqual,4},
  67. {ZTestMode.Equal,5},
  68. {ZTestMode.NotEqual,6},
  69. {ZTestMode.Always,7}
  70. };
  71. public static readonly Dictionary<ZWriteMode, int> ZWriteModeDict = new Dictionary<ZWriteMode, int>
  72. {
  73. { ZWriteMode.On,1},
  74. { ZWriteMode.Off,2}
  75. };
  76. [SerializeField]
  77. private InlineProperty m_zTestMode = new InlineProperty();
  78. [SerializeField]
  79. private InlineProperty m_zWriteMode = new InlineProperty();
  80. [SerializeField]
  81. private InlineProperty m_offsetFactor = new InlineProperty();
  82. [SerializeField]
  83. private InlineProperty m_offsetUnits = new InlineProperty();
  84. [SerializeField]
  85. private bool m_offsetEnabled;
  86. [SerializeField]
  87. private bool m_extraDepthPass;
  88. [SerializeField]
  89. private int m_extrazTestMode = 0;
  90. [SerializeField]
  91. private StandardSurfaceOutputNode m_parentSurface;
  92. public string CreateDepthInfo( bool outlineZWrite, bool outlineZTest )
  93. {
  94. string result = string.Empty;
  95. if( m_zWriteMode.IntValue != 0 || m_zWriteMode.Active )
  96. {
  97. MasterNode.AddRenderState( ref result, "ZWrite", m_zWriteMode.GetValueOrProperty( ZWriteModeValues[ m_zWriteMode.IntValue ] ) );
  98. }
  99. else if( outlineZWrite )
  100. {
  101. MasterNode.AddRenderState( ref result, "ZWrite", ZWriteModeValues[ 1 ] );
  102. }
  103. if( m_zTestMode.IntValue != 0 || m_zTestMode.Active )
  104. {
  105. MasterNode.AddRenderState( ref result, "ZTest", m_zTestMode.GetValueOrProperty( ZTestModeValues[ m_zTestMode.IntValue ] ) );
  106. }
  107. else if( outlineZTest )
  108. {
  109. MasterNode.AddRenderState( ref result, "ZTest", ZTestModeValues[ 3 ] );
  110. }
  111. if( m_offsetEnabled )
  112. {
  113. MasterNode.AddRenderState( ref result, "Offset ", m_offsetFactor.GetValueOrProperty() + " , " + m_offsetUnits.GetValueOrProperty() );
  114. }
  115. return result;
  116. }
  117. public void Draw( UndoParentNode owner, GUIStyle toolbarstyle, bool customBlendAvailable )
  118. {
  119. Color cachedColor = GUI.color;
  120. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, 0.5f );
  121. EditorGUILayout.BeginHorizontal( toolbarstyle );
  122. GUI.color = cachedColor;
  123. EditorGUI.BeginChangeCheck();
  124. m_parentSurface.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDepth = owner.GUILayoutToggle( m_parentSurface.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDepth, DepthParametersStr, UIUtils.MenuItemToggleStyle );
  125. if( EditorGUI.EndChangeCheck() )
  126. {
  127. EditorPrefs.SetBool( "ExpandedDepth", m_parentSurface.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDepth );
  128. }
  129. EditorGUILayout.EndHorizontal();
  130. if( m_parentSurface.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDepth )
  131. {
  132. cachedColor = GUI.color;
  133. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  134. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  135. GUI.color = cachedColor;
  136. EditorGUI.indentLevel++;
  137. if( !customBlendAvailable )
  138. EditorGUILayout.HelpBox( "Depth Writing is only available for Opaque or Custom blend modes", MessageType.Warning );
  139. EditorGUILayout.Separator();
  140. EditorGUI.BeginDisabledGroup( !customBlendAvailable );
  141. m_zWriteMode.EnumTypePopup( ref owner, ZWriteModeStr, ZWriteModeValues );
  142. m_zTestMode.EnumTypePopup( ref owner, ZTestModeStr, ZTestModeLabels );
  143. //m_zWriteMode = owner.EditorGUILayoutPopup( ZWriteModeStr, m_zWriteMode, ZWriteModeValues );
  144. //m_zTestMode = owner.EditorGUILayoutPopup( ZTestModeStr, m_zTestMode, ZTestModeLabels );
  145. m_offsetEnabled = owner.EditorGUILayoutToggle( OffsetStr, m_offsetEnabled );
  146. if( m_offsetEnabled )
  147. {
  148. EditorGUI.indentLevel++;
  149. m_offsetFactor.FloatField( ref owner , OffsetFactorStr );
  150. m_offsetUnits.FloatField( ref owner , OffsetUnitsStr );
  151. EditorGUI.indentLevel--;
  152. }
  153. m_extraDepthPass = owner.EditorGUILayoutToggle( ExtraDepthPassStr, m_extraDepthPass );
  154. if( m_extraDepthPass )
  155. {
  156. EditorGUI.indentLevel++;
  157. m_extrazTestMode = owner.EditorGUILayoutPopup( DepthZTestStr, m_extrazTestMode, ZTestModeLabels );
  158. EditorGUI.indentLevel--;
  159. }
  160. EditorGUILayout.Separator();
  161. EditorGUI.indentLevel--;
  162. EditorGUI.EndDisabledGroup();
  163. EditorGUILayout.EndVertical();
  164. }
  165. EditorGUI.EndDisabledGroup();
  166. }
  167. public void DrawExtraDepthPass( ref string shaderBody )
  168. {
  169. if( m_extraDepthPass )
  170. {
  171. shaderBody += "\t\tPass\n";
  172. shaderBody += "\t\t{\n";
  173. shaderBody += "\t\t\tColorMask 0\n";
  174. if( m_extrazTestMode != 0 )
  175. shaderBody += "\t\t\tZTest " + ZTestModeValues[ m_extrazTestMode ] + "\n";
  176. shaderBody += "\t\t\tZWrite On\n";
  177. shaderBody += "\t\t}\n\n";
  178. }
  179. }
  180. public void ReadFromString( ref uint index, ref string[] nodeParams )
  181. {
  182. if( UIUtils.CurrentShaderVersion() < 2502 )
  183. {
  184. string zWriteMode = nodeParams[ index++ ];
  185. m_zWriteMode.IntValue = zWriteMode.Equals( "Off" ) ? 2 : 0;
  186. string zTestMode = nodeParams[ index++ ];
  187. for( int i = 0; i < ZTestModeValues.Length; i++ )
  188. {
  189. if( zTestMode.Equals( ZTestModeValues[ i ] ) )
  190. {
  191. m_zTestMode.IntValue = i;
  192. break;
  193. }
  194. }
  195. }
  196. else
  197. {
  198. if( UIUtils.CurrentShaderVersion() > 14501 )
  199. {
  200. m_zWriteMode.ReadFromString( ref index, ref nodeParams );
  201. m_zTestMode.ReadFromString( ref index, ref nodeParams );
  202. }
  203. else
  204. {
  205. m_zWriteMode.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
  206. m_zTestMode.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
  207. }
  208. m_offsetEnabled = Convert.ToBoolean( nodeParams[ index++ ] );
  209. if( UIUtils.CurrentShaderVersion() > 15303 )
  210. {
  211. m_offsetFactor.ReadFromString( ref index, ref nodeParams );
  212. m_offsetUnits.ReadFromString( ref index, ref nodeParams );
  213. }
  214. else
  215. {
  216. m_offsetFactor.FloatValue = Convert.ToSingle( nodeParams[ index++ ] );
  217. m_offsetUnits.FloatValue = Convert.ToSingle( nodeParams[ index++ ] );
  218. }
  219. if( UIUtils.CurrentShaderVersion() > 14202 )
  220. {
  221. m_extraDepthPass = Convert.ToBoolean( nodeParams[ index++ ] );
  222. m_extrazTestMode = Convert.ToInt32( nodeParams[ index++ ] );
  223. }
  224. }
  225. }
  226. public void WriteToString( ref string nodeInfo )
  227. {
  228. m_zWriteMode.WriteToString( ref nodeInfo );
  229. m_zTestMode.WriteToString( ref nodeInfo );
  230. IOUtils.AddFieldValueToString( ref nodeInfo, m_offsetEnabled );
  231. m_offsetFactor.WriteToString( ref nodeInfo );
  232. m_offsetUnits.WriteToString( ref nodeInfo );
  233. IOUtils.AddFieldValueToString( ref nodeInfo, m_extraDepthPass );
  234. IOUtils.AddFieldValueToString( ref nodeInfo, m_extrazTestMode );
  235. }
  236. public bool IsActive { get { return m_zTestMode.IntValue != 0 || m_zWriteMode.IntValue != 0 || m_offsetEnabled || m_zTestMode.Active || m_zWriteMode.Active; } }
  237. public StandardSurfaceOutputNode ParentSurface { get { return m_parentSurface; } set { m_parentSurface = value; } }
  238. }
  239. }