Assignment for RMIT Mixed Reality in 2020
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.

394 lines
11 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. var cache = EditorGUIUtility.labelWidth;
  131. EditorGUIUtility.labelWidth = EditorGUIUtility.labelWidth - 20;
  132. Color 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. EditorGUILayout.Separator();
  137. if( m_validZWrite )
  138. m_zWriteMode.EnumTypePopup( ref owner, ZBufferOpHelper.ZWriteModeStr, ZBufferOpHelper.ZWriteModeValues );
  139. if( m_validZTest )
  140. m_zTestMode.EnumTypePopup( ref owner, ZBufferOpHelper.ZTestModeStr, ZBufferOpHelper.ZTestModeLabels );
  141. if( m_validOffset )
  142. {
  143. m_offsetEnabled = owner.EditorGUILayoutToggle( ZBufferOpHelper.OffsetStr, m_offsetEnabled );
  144. if( m_offsetEnabled )
  145. {
  146. EditorGUI.indentLevel++;
  147. m_offsetFactor.FloatField( ref owner, ZBufferOpHelper.OffsetFactorStr );
  148. m_offsetUnits.FloatField( ref owner, ZBufferOpHelper.OffsetUnitsStr );
  149. EditorGUI.indentLevel--;
  150. }
  151. }
  152. EditorGUILayout.Separator();
  153. EditorGUIUtility.labelWidth = cache;
  154. //EditorGUILayout.EndVertical();
  155. if( EditorGUI.EndChangeCheck() )
  156. {
  157. m_isDirty = true;
  158. }
  159. }
  160. public void ReadZWriteFromString( ref uint index, ref string[] nodeParams )
  161. {
  162. bool validDataOnMeta = m_validZWrite;
  163. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  164. {
  165. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  166. }
  167. if( validDataOnMeta )
  168. {
  169. if( UIUtils.CurrentShaderVersion() < 15304 )
  170. {
  171. m_zWriteMode.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
  172. }
  173. else
  174. {
  175. m_zWriteMode.ReadFromString( ref index, ref nodeParams );
  176. }
  177. }
  178. }
  179. public void ReadZTestFromString( ref uint index, ref string[] nodeParams )
  180. {
  181. bool validDataOnMeta = m_validZTest;
  182. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  183. {
  184. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  185. }
  186. if( validDataOnMeta )
  187. {
  188. if( UIUtils.CurrentShaderVersion() < 15304 )
  189. {
  190. m_zTestMode.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
  191. }
  192. else
  193. {
  194. m_zTestMode.ReadFromString( ref index, ref nodeParams );
  195. }
  196. }
  197. }
  198. public void ReadOffsetFromString( ref uint index, ref string[] nodeParams )
  199. {
  200. bool validDataOnMeta = m_validOffset;
  201. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  202. {
  203. validDataOnMeta = Convert.ToBoolean( nodeParams[ index++ ] );
  204. }
  205. if( validDataOnMeta )
  206. {
  207. m_offsetEnabled = Convert.ToBoolean( nodeParams[ index++ ] );
  208. if( UIUtils.CurrentShaderVersion() < 15304 )
  209. {
  210. m_offsetFactor.FloatValue = Convert.ToSingle( nodeParams[ index++ ] );
  211. m_offsetUnits.FloatValue = Convert.ToSingle( nodeParams[ index++ ] );
  212. }
  213. else
  214. {
  215. m_offsetFactor.ReadFromString( ref index, ref nodeParams, false );
  216. m_offsetUnits.ReadFromString( ref index, ref nodeParams, false );
  217. }
  218. }
  219. }
  220. public override void ReadFromString( ref uint index, ref string[] nodeParams )
  221. {
  222. ReadZWriteFromString( ref index, ref nodeParams );
  223. ReadZTestFromString( ref index, ref nodeParams );
  224. ReadOffsetFromString( ref index, ref nodeParams );
  225. }
  226. public void WriteZWriteToString( ref string nodeInfo )
  227. {
  228. IOUtils.AddFieldValueToString( ref nodeInfo, m_validZWrite );
  229. if( m_validZWrite )
  230. m_zWriteMode.WriteToString( ref nodeInfo );
  231. }
  232. public void WriteZTestToString( ref string nodeInfo )
  233. {
  234. IOUtils.AddFieldValueToString( ref nodeInfo, m_validZTest );
  235. if( m_validZTest )
  236. m_zTestMode.WriteToString( ref nodeInfo );
  237. }
  238. public void WriteOffsetToString( ref string nodeInfo )
  239. {
  240. IOUtils.AddFieldValueToString( ref nodeInfo, m_validOffset );
  241. if( m_validOffset )
  242. {
  243. IOUtils.AddFieldValueToString( ref nodeInfo, m_offsetEnabled );
  244. m_offsetFactor.WriteToString( ref nodeInfo );
  245. m_offsetUnits.WriteToString( ref nodeInfo );
  246. }
  247. }
  248. public override void WriteToString( ref string nodeInfo )
  249. {
  250. WriteZWriteToString( ref nodeInfo );
  251. WriteZTestToString( ref nodeInfo );
  252. WriteOffsetToString( ref nodeInfo );
  253. }
  254. public bool IsActive { get { return ( m_zTestMode.IsValid || m_zTestMode.IntValue != 0 ) || ( m_zWriteMode.IsValid || m_zWriteMode.IntValue != 0 ) || m_offsetEnabled; } }
  255. public string CurrentZWriteMode
  256. {
  257. get
  258. {
  259. if( m_zWriteMode.IsValid )
  260. {
  261. return string.Format( ZWriteFormatter, m_zWriteMode.GetValueOrProperty() ); ;
  262. }
  263. int finalZWrite = ( m_zWriteMode.IntValue == 0 ) ? 1 : m_zWriteMode.IntValue;
  264. return string.Format( ZWriteFormatter, ZBufferOpHelper.ZWriteModeValues[ finalZWrite ] ); ;
  265. }
  266. }
  267. public string CurrentZTestMode
  268. {
  269. get
  270. {
  271. if( m_zTestMode.IsValid )
  272. return string.Format( ZTestFormatter, m_zTestMode.GetValueOrProperty() );
  273. int finalZTestMode = ( m_zTestMode.IntValue == 0 ) ? 3 : m_zTestMode.IntValue;
  274. return string.Format( ZTestFormatter, ZBufferOpHelper.ZTestModeValues[ finalZTestMode ] );
  275. }
  276. }
  277. public string CurrentOffset
  278. {
  279. get
  280. {
  281. if( m_offsetEnabled )
  282. return "Offset " + m_offsetFactor.GetValueOrProperty() + " , " + m_offsetUnits.GetValueOrProperty() + "\n";
  283. else
  284. return "Offset 0,0\n";
  285. }
  286. }
  287. public bool ValidZTest { get { return m_validZTest; } }
  288. public bool ValidZWrite { get { return m_validZWrite; } }
  289. public bool ValidOffset { get { return m_validOffset; } }
  290. public InlineProperty ZTestMode { get { return m_zTestMode; } }
  291. public InlineProperty ZWriteMode { get { return m_zWriteMode; } }
  292. public InlineProperty OffsetFactor { get { return m_offsetFactor; } }
  293. public InlineProperty OffsetUnits { get { return m_offsetUnits; } }
  294. public bool OffsetEnabled { get { return m_offsetEnabled; } }
  295. public ZTestMode ZTestModeValue
  296. {
  297. set
  298. {
  299. m_zTestMode.IntValue = ZBufferOpHelper.ZTestModeDict[ value ];
  300. m_zTestMode.Active = false;
  301. }
  302. get
  303. {
  304. return (ZTestMode)( m_zTestMode.IntValue - 1 );
  305. }
  306. }
  307. public ZWriteMode ZWriteModeValue
  308. {
  309. set
  310. {
  311. m_zWriteMode.IntValue = ZBufferOpHelper.ZWriteModeDict[ value ];
  312. m_zWriteMode.Active = false;
  313. }
  314. get
  315. {
  316. return (ZWriteMode)( m_zWriteMode.IntValue - 1 );
  317. }
  318. }
  319. public float OffsetFactorValue
  320. {
  321. set
  322. {
  323. m_offsetEnabled = true;
  324. m_offsetFactor.FloatValue = value;
  325. m_offsetFactor.Active = false;
  326. }
  327. get
  328. {
  329. return m_offsetFactor.FloatValue;
  330. }
  331. }
  332. public float OffsetUnitsValue
  333. {
  334. set
  335. {
  336. m_offsetEnabled = true;
  337. m_offsetUnits.FloatValue = value;
  338. m_offsetUnits.Active = false;
  339. }
  340. get
  341. {
  342. return m_offsetUnits.FloatValue;
  343. }
  344. }
  345. }
  346. }