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.

339 lines
9.8 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. public sealed class TemplateDepthModule : TemplateModuleParent
  11. {
  12. private const string ZWriteFormatter = "ZWrite {0}\n";
  13. private const string ZTestFormatter = "ZTest {0}\n";
  14. [ SerializeField]
  15. private bool m_validZTest = false;
  16. [SerializeField]
  17. private InlineProperty m_zTestMode = new InlineProperty(0);
  18. [SerializeField]
  19. private bool m_validZWrite = false;
  20. [SerializeField]
  21. private InlineProperty m_zWriteMode = new InlineProperty(0);
  22. [SerializeField]
  23. private InlineProperty m_offsetFactor = new InlineProperty(0);
  24. [SerializeField]
  25. private InlineProperty m_offsetUnits = new InlineProperty(0);
  26. [SerializeField]
  27. private bool m_offsetEnabled = false;
  28. [SerializeField]
  29. private bool m_validOffset = false;
  30. public TemplateDepthModule() : base( "Depth" ) { }
  31. public void CopyFrom( TemplateDepthModule other , bool allData )
  32. {
  33. if( allData )
  34. {
  35. m_independentModule = other.IndependentModule;
  36. m_validZTest = other.ValidZTest;
  37. m_validZWrite = other.ValidZWrite;
  38. m_validOffset = other.ValidOffset;
  39. }
  40. m_zTestMode.CopyFrom( other.ZTestMode );
  41. m_zWriteMode.CopyFrom( other.ZWriteMode );
  42. m_offsetFactor.CopyFrom( other.OffsetFactor );
  43. m_offsetUnits.CopyFrom( other.OffsetUnits );
  44. m_offsetEnabled = other.OffsetEnabled;
  45. }
  46. public void ConfigureFromTemplateData( TemplateDepthData depthData )
  47. {
  48. m_independentModule = depthData.IndependentModule;
  49. if( depthData.ValidZTest && m_validZTest != depthData.ValidZTest )
  50. {
  51. if( string.IsNullOrEmpty( depthData.ZTestInlineValue ) )
  52. {
  53. m_zTestMode.IntValue = ZBufferOpHelper.ZTestModeDict[ depthData.ZTestModeValue ];
  54. m_zTestMode.ResetProperty();
  55. }
  56. else
  57. {
  58. m_zTestMode.SetInlineByName( depthData.ZTestInlineValue );
  59. }
  60. }
  61. if( depthData.ValidZWrite && m_validZWrite != depthData.ValidZWrite )
  62. {
  63. if( string.IsNullOrEmpty( depthData.ZWriteInlineValue ) )
  64. {
  65. m_zWriteMode.IntValue = ZBufferOpHelper.ZWriteModeDict[ depthData.ZWriteModeValue ];
  66. m_zWriteMode.ResetProperty();
  67. }
  68. else
  69. {
  70. m_zWriteMode.SetInlineByName( depthData.ZWriteInlineValue );
  71. }
  72. }
  73. if( depthData.ValidOffset && m_validOffset != depthData.ValidOffset )
  74. {
  75. if( string.IsNullOrEmpty( depthData.OffsetFactorInlineValue ) )
  76. {
  77. m_offsetFactor.FloatValue = depthData.OffsetFactor;
  78. m_offsetFactor.ResetProperty();
  79. }
  80. else
  81. {
  82. m_offsetFactor.SetInlineByName( depthData.OffsetFactorInlineValue );
  83. }
  84. if( string.IsNullOrEmpty( depthData.OffsetUnitsInlineValue ) )
  85. {
  86. m_offsetUnits.FloatValue = depthData.OffsetUnits;
  87. m_offsetUnits.ResetProperty();
  88. }
  89. else
  90. {
  91. m_offsetUnits.SetInlineByName( depthData.OffsetUnitsInlineValue );
  92. }
  93. m_offsetEnabled = depthData.ValidOffset;
  94. }
  95. m_validZTest = depthData.ValidZTest;
  96. m_validZWrite = depthData.ValidZWrite;
  97. m_validOffset = depthData.ValidOffset;
  98. m_validData = m_validZTest || m_validZWrite || m_validOffset;
  99. }
  100. public override void ShowUnreadableDataMessage( ParentNode owner )
  101. {
  102. bool foldoutValue = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDepth;
  103. NodeUtils.DrawPropertyGroup( ref foldoutValue, ZBufferOpHelper.DepthParametersStr, base.ShowUnreadableDataMessage );
  104. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDepth = foldoutValue;
  105. }
  106. public override void Draw( UndoParentNode owner , bool style = true)
  107. {
  108. bool foldout = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDepth;
  109. if( style )
  110. {
  111. NodeUtils.DrawPropertyGroup( ref foldout, ZBufferOpHelper.DepthParametersStr, () =>
  112. {
  113. EditorGUI.indentLevel++;
  114. DrawBlock( owner );
  115. EditorGUI.indentLevel--;
  116. } );
  117. }
  118. else
  119. {
  120. NodeUtils.DrawNestedPropertyGroup( ref foldout, ZBufferOpHelper.DepthParametersStr, () =>
  121. {
  122. DrawBlock( owner );
  123. } );
  124. }
  125. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDepth = foldout;
  126. }
  127. void DrawBlock( UndoParentNode owner )
  128. {
  129. EditorGUI.BeginChangeCheck();
  130. Color cachedColor = GUI.color;
  131. GUI.color = new Color( cachedColor.r, cachedColor.g, cachedColor.b, ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f ) );
  132. //EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  133. GUI.color = cachedColor;
  134. EditorGUILayout.Separator();
  135. if( m_validZWrite )
  136. m_zWriteMode.EnumTypePopup( ref owner, ZBufferOpHelper.ZWriteModeStr, ZBufferOpHelper.ZWriteModeValues );
  137. if( m_validZTest )
  138. m_zTestMode.EnumTypePopup( ref owner, ZBufferOpHelper.ZTestModeStr, ZBufferOpHelper.ZTestModeLabels );
  139. if( m_validOffset )
  140. {
  141. m_offsetEnabled = owner.EditorGUILayoutToggle( ZBufferOpHelper.OffsetStr, m_offsetEnabled );
  142. if( m_offsetEnabled )
  143. {
  144. EditorGUI.indentLevel++;
  145. m_offsetFactor.FloatField( ref owner , ZBufferOpHelper.OffsetFactorStr );
  146. m_offsetUnits.FloatField( ref owner , ZBufferOpHelper.OffsetUnitsStr);
  147. EditorGUI.indentLevel--;
  148. }
  149. }
  150. EditorGUILayout.Separator();
  151. //EditorGUILayout.EndVertical();
  152. if( EditorGUI.EndChangeCheck() )
  153. {
  154. m_isDirty = true;
  155. }
  156. }
  157. public void ReadZWriteFromString( ref uint index, ref string[] nodeParams )
  158. {
  159. bool validDataOnMeta = m_validZWrite;
  160. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  161. {
  162. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  163. }
  164. if( validDataOnMeta )
  165. {
  166. if( UIUtils.CurrentShaderVersion() < 15304 )
  167. {
  168. m_zWriteMode.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
  169. }
  170. else
  171. {
  172. m_zWriteMode.ReadFromString( ref index, ref nodeParams );
  173. }
  174. }
  175. }
  176. public void ReadZTestFromString( ref uint index, ref string[] nodeParams )
  177. {
  178. bool validDataOnMeta = m_validZTest;
  179. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  180. {
  181. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  182. }
  183. if( validDataOnMeta )
  184. {
  185. if( UIUtils.CurrentShaderVersion() < 15304 )
  186. {
  187. m_zTestMode.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
  188. }
  189. else
  190. {
  191. m_zTestMode.ReadFromString( ref index, ref nodeParams );
  192. }
  193. }
  194. }
  195. public void ReadOffsetFromString( ref uint index, ref string[] nodeParams )
  196. {
  197. bool validDataOnMeta = m_validOffset;
  198. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  199. {
  200. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  201. }
  202. if( validDataOnMeta )
  203. {
  204. m_offsetEnabled = Convert.ToBoolean( nodeParams[ index++ ] );
  205. if( UIUtils.CurrentShaderVersion() < 15304 )
  206. {
  207. m_offsetFactor.FloatValue = Convert.ToSingle( nodeParams[ index++ ] );
  208. m_offsetUnits.FloatValue = Convert.ToSingle( nodeParams[ index++ ] );
  209. }
  210. else
  211. {
  212. m_offsetFactor.ReadFromString( ref index, ref nodeParams );
  213. m_offsetUnits.ReadFromString( ref index, ref nodeParams );
  214. }
  215. }
  216. }
  217. public override void ReadFromString( ref uint index, ref string[] nodeParams )
  218. {
  219. ReadZWriteFromString( ref index, ref nodeParams );
  220. ReadZTestFromString( ref index, ref nodeParams );
  221. ReadOffsetFromString( ref index, ref nodeParams );
  222. }
  223. public void WriteZWriteToString( ref string nodeInfo )
  224. {
  225. IOUtils.AddFieldValueToString( ref nodeInfo, m_validZWrite );
  226. if( m_validZWrite )
  227. m_zWriteMode.WriteToString( ref nodeInfo );
  228. }
  229. public void WriteZTestToString( ref string nodeInfo )
  230. {
  231. IOUtils.AddFieldValueToString( ref nodeInfo, m_validZTest );
  232. if( m_validZTest )
  233. m_zTestMode.WriteToString( ref nodeInfo );
  234. }
  235. public void WriteOffsetToString( ref string nodeInfo )
  236. {
  237. IOUtils.AddFieldValueToString( ref nodeInfo, m_validOffset );
  238. if( m_validOffset )
  239. {
  240. IOUtils.AddFieldValueToString( ref nodeInfo, m_offsetEnabled );
  241. m_offsetFactor.WriteToString(ref nodeInfo);
  242. m_offsetUnits.WriteToString( ref nodeInfo );
  243. }
  244. }
  245. public override void WriteToString( ref string nodeInfo )
  246. {
  247. WriteZWriteToString( ref nodeInfo );
  248. WriteZTestToString( ref nodeInfo );
  249. WriteOffsetToString( ref nodeInfo );
  250. }
  251. public bool IsActive { get { return ( m_zTestMode.IsValid || m_zTestMode.IntValue != 0) || ( m_zWriteMode .IsValid || m_zWriteMode.IntValue != 0) || m_offsetEnabled; } }
  252. public string CurrentZWriteMode
  253. {
  254. get
  255. {
  256. if( m_zWriteMode.IsValid )
  257. {
  258. return string.Format( ZWriteFormatter, m_zWriteMode.GetValueOrProperty() ); ;
  259. }
  260. int finalZWrite = ( m_zWriteMode.IntValue == 0 ) ? 1 : m_zWriteMode.IntValue;
  261. return string.Format( ZWriteFormatter, ZBufferOpHelper.ZWriteModeValues[ finalZWrite ] ); ;
  262. }
  263. }
  264. public string CurrentZTestMode
  265. {
  266. get
  267. {
  268. if( m_zTestMode.IsValid )
  269. return string.Format( ZTestFormatter, m_zTestMode.GetValueOrProperty() );
  270. int finalZTestMode = ( m_zTestMode.IntValue == 0 )?3 : m_zTestMode.IntValue;
  271. return string.Format( ZTestFormatter, ZBufferOpHelper.ZTestModeValues[ finalZTestMode ] );
  272. }
  273. }
  274. public string CurrentOffset
  275. {
  276. get
  277. {
  278. if( m_offsetEnabled )
  279. return "Offset " + m_offsetFactor.GetValueOrProperty() + " , " + m_offsetUnits.GetValueOrProperty() + "\n";
  280. else
  281. return "Offset 0,0\n";
  282. }
  283. }
  284. public bool ValidZTest { get { return m_validZTest; } }
  285. public bool ValidZWrite { get { return m_validZWrite; } }
  286. public bool ValidOffset { get { return m_validOffset; } }
  287. public InlineProperty ZTestMode { get { return m_zTestMode; } }
  288. public InlineProperty ZWriteMode { get { return m_zWriteMode; } }
  289. public InlineProperty OffsetFactor { get { return m_offsetFactor; } }
  290. public InlineProperty OffsetUnits { get { return m_offsetUnits; } }
  291. public bool OffsetEnabled { get { return m_offsetEnabled; } }
  292. }
  293. }