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.

497 lines
14 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. public class TemplateModulesHelper
  9. {
  10. [SerializeField]
  11. internal bool Foldout = false;
  12. private bool m_isDirty = false;
  13. [SerializeField]
  14. private TemplatesBlendModule m_blendOpHelper = new TemplatesBlendModule();
  15. [SerializeField]
  16. private TemplateCullModeModule m_cullModeHelper = new TemplateCullModeModule();
  17. [SerializeField]
  18. private TemplateColorMaskModule m_colorMaskHelper = new TemplateColorMaskModule();
  19. [SerializeField]
  20. private TemplatesStencilBufferModule m_stencilBufferHelper = new TemplatesStencilBufferModule();
  21. [SerializeField]
  22. private TemplateDepthModule m_depthOphelper = new TemplateDepthModule();
  23. [SerializeField]
  24. private TemplateTagsModule m_tagsHelper = new TemplateTagsModule();
  25. [SerializeField]
  26. private TemplateShaderModelModule m_shaderModelHelper = new TemplateShaderModelModule();
  27. [SerializeField]
  28. private TemplateAdditionalIncludesHelper m_additionalIncludes = new TemplateAdditionalIncludesHelper();
  29. [SerializeField]
  30. private TemplateAdditionalDefinesHelper m_additionalDefines = new TemplateAdditionalDefinesHelper();
  31. [SerializeField]
  32. private TemplateAdditionalPragmasHelper m_additionalPragmas = new TemplateAdditionalPragmasHelper();
  33. [SerializeField]
  34. private TemplateAdditionalDirectivesHelper m_additionalDirectives = new TemplateAdditionalDirectivesHelper(" Additional Directives");
  35. [SerializeField]
  36. private bool m_hasValidData = false;
  37. [SerializeField]
  38. private bool m_allModulesMode = false;
  39. public void CopyFrom( TemplateModulesHelper other )
  40. {
  41. m_allModulesMode = other.AllModulesMode;
  42. if( other.BlendOpHelper.IsDirty )
  43. {
  44. m_blendOpHelper.CopyFrom( other.BlendOpHelper, true );
  45. }
  46. if( other.CullModeHelper.IsDirty )
  47. {
  48. m_cullModeHelper.CopyFrom( other.CullModeHelper , true );
  49. }
  50. if( other.ColorMaskHelper.IsDirty )
  51. {
  52. m_colorMaskHelper.CopyFrom( other.ColorMaskHelper , true);
  53. }
  54. if( other.StencilBufferHelper.IsDirty )
  55. {
  56. m_stencilBufferHelper.CopyFrom( other.StencilBufferHelper,true );
  57. }
  58. if( other.DepthOphelper.IsDirty )
  59. {
  60. m_depthOphelper.CopyFrom( other.DepthOphelper,true );
  61. }
  62. if( other.TagsHelper.IsDirty )
  63. {
  64. m_tagsHelper.CopyFrom( other.TagsHelper );
  65. }
  66. if( other.ShaderModelHelper.IsDirty )
  67. {
  68. m_shaderModelHelper.CopyFrom( other.ShaderModelHelper, true );
  69. }
  70. }
  71. public void SyncWith( TemplateModulesHelper other )
  72. {
  73. if( m_blendOpHelper.ValidData && other.BlendOpHelper.ValidData )
  74. {
  75. m_blendOpHelper.CopyFrom( other.BlendOpHelper, false );
  76. }
  77. if( m_cullModeHelper.ValidData && other.CullModeHelper.ValidData )
  78. {
  79. m_cullModeHelper.CopyFrom( other.CullModeHelper, false );
  80. }
  81. if( m_colorMaskHelper.ValidData && other.ColorMaskHelper.ValidData )
  82. {
  83. m_colorMaskHelper.CopyFrom( other.ColorMaskHelper , false );
  84. }
  85. if( m_stencilBufferHelper.ValidData && other.StencilBufferHelper.ValidData )
  86. {
  87. m_stencilBufferHelper.CopyFrom( other.StencilBufferHelper, false );
  88. }
  89. if( m_depthOphelper.ValidData && other.DepthOphelper.ValidData )
  90. {
  91. m_depthOphelper.CopyFrom( other.DepthOphelper, false );
  92. }
  93. if( m_shaderModelHelper.ValidData && other.ShaderModelHelper.ValidData )
  94. {
  95. m_shaderModelHelper.CopyFrom( other.ShaderModelHelper , false);
  96. }
  97. }
  98. public void FetchDataFromTemplate( TemplateModulesData module )
  99. {
  100. m_allModulesMode = module.AllModulesMode;
  101. if( module.PragmaTag.IsValid )
  102. {
  103. m_hasValidData = true;
  104. //m_additionalPragmas.IsValid = true;
  105. //m_additionalPragmas.FillNativeItems( module.IncludePragmaContainer.PragmasList );
  106. //m_additionalIncludes.IsValid = true;
  107. //m_additionalIncludes.FillNativeItems( module.IncludePragmaContainer.IncludesList );
  108. //m_additionalDefines.IsValid = true;
  109. //m_additionalDefines.FillNativeItems( module.IncludePragmaContainer.DefinesList );
  110. m_additionalDirectives.FillNativeItems( module.IncludePragmaContainer.NativeDirectivesList );
  111. m_additionalDirectives.IsValid = true;
  112. }
  113. else
  114. {
  115. //m_additionalPragmas.IsValid = false;
  116. //m_additionalIncludes.IsValid = false;
  117. //m_additionalDefines.IsValid = false;
  118. m_additionalDirectives.IsValid = false;
  119. }
  120. m_blendOpHelper.ConfigureFromTemplateData( module.BlendData );
  121. if( module.BlendData.DataCheck == TemplateDataCheck.Valid )
  122. {
  123. m_hasValidData = true;
  124. }
  125. m_cullModeHelper.ConfigureFromTemplateData( module.CullModeData );
  126. if( module.CullModeData.DataCheck == TemplateDataCheck.Valid )
  127. {
  128. m_hasValidData = true;
  129. }
  130. m_colorMaskHelper.ConfigureFromTemplateData( module.ColorMaskData );
  131. if( module.ColorMaskData.DataCheck == TemplateDataCheck.Valid )
  132. {
  133. m_hasValidData = true;
  134. }
  135. m_stencilBufferHelper.ConfigureFromTemplateData( module.StencilData );
  136. if( module.StencilData.DataCheck == TemplateDataCheck.Valid )
  137. {
  138. m_hasValidData = true;
  139. }
  140. m_depthOphelper.ConfigureFromTemplateData( module.DepthData );
  141. if( module.DepthData.DataCheck == TemplateDataCheck.Valid )
  142. {
  143. m_hasValidData = true;
  144. }
  145. m_tagsHelper.ConfigureFromTemplateData( module.TagData );
  146. if( module.TagData.DataCheck == TemplateDataCheck.Valid )
  147. {
  148. m_hasValidData = true;
  149. }
  150. m_shaderModelHelper.ConfigureFromTemplateData( module.ShaderModel );
  151. if( module.ShaderModel.DataCheck == TemplateDataCheck.Valid )
  152. {
  153. m_hasValidData = true;
  154. }
  155. }
  156. public void OnLogicUpdate( TemplateModulesData currentModule )
  157. {
  158. if( currentModule.TagData.DataCheck == TemplateDataCheck.Valid )
  159. m_tagsHelper.OnLogicUpdate();
  160. }
  161. public void Draw( ParentNode owner, TemplateModulesData currentModule , TemplateModulesHelper parent = null )
  162. {
  163. if( currentModule.ShaderModel.DataCheck == TemplateDataCheck.Valid )
  164. m_shaderModelHelper.Draw( owner );
  165. m_isDirty = m_shaderModelHelper.IsDirty;
  166. if( currentModule.CullModeData.DataCheck == TemplateDataCheck.Valid )
  167. m_cullModeHelper.Draw( owner );
  168. m_isDirty = m_isDirty || m_cullModeHelper.IsDirty;
  169. if( currentModule.ColorMaskData.DataCheck == TemplateDataCheck.Valid )
  170. m_colorMaskHelper.Draw( owner );
  171. m_isDirty = m_isDirty || m_colorMaskHelper.IsDirty;
  172. if( currentModule.DepthData.DataCheck == TemplateDataCheck.Valid )
  173. m_depthOphelper.Draw( owner, false );
  174. m_isDirty = m_isDirty || m_depthOphelper.IsDirty;
  175. if( currentModule.BlendData.DataCheck == TemplateDataCheck.Valid )
  176. m_blendOpHelper.Draw( owner, false );
  177. m_isDirty = m_isDirty || m_blendOpHelper.IsDirty;
  178. if( currentModule.StencilData.DataCheck == TemplateDataCheck.Valid )
  179. {
  180. CullMode cullMode = CullMode.Back;
  181. if( currentModule.CullModeData.DataCheck == TemplateDataCheck.Valid )
  182. {
  183. cullMode = m_cullModeHelper.CurrentCullMode;
  184. }
  185. else if( parent != null && parent.CullModeHelper.ValidData )
  186. {
  187. cullMode = parent.CullModeHelper.CurrentCullMode;
  188. }
  189. m_stencilBufferHelper.Draw( owner, cullMode, false );
  190. }
  191. m_isDirty = m_isDirty || m_stencilBufferHelper.IsDirty;
  192. if( currentModule.TagData.DataCheck == TemplateDataCheck.Valid )
  193. m_tagsHelper.Draw( owner, false );
  194. m_isDirty = m_isDirty || m_tagsHelper.IsDirty;
  195. if( currentModule.PragmaTag.IsValid )
  196. {
  197. //m_additionalDefines.Draw( owner );
  198. //m_additionalIncludes.Draw( owner );
  199. //m_additionalPragmas.Draw( owner );
  200. m_additionalDirectives.Draw( owner , false);
  201. }
  202. m_isDirty = m_isDirty ||
  203. //m_additionalDefines.IsDirty ||
  204. //m_additionalIncludes.IsDirty ||
  205. //m_additionalPragmas.IsDirty ||
  206. m_additionalDirectives.IsDirty;
  207. }
  208. public void Destroy()
  209. {
  210. m_shaderModelHelper = null;
  211. m_blendOpHelper = null;
  212. m_cullModeHelper = null;
  213. m_colorMaskHelper.Destroy();
  214. m_colorMaskHelper = null;
  215. m_stencilBufferHelper.Destroy();
  216. m_stencilBufferHelper = null;
  217. m_tagsHelper.Destroy();
  218. m_tagsHelper = null;
  219. m_additionalDefines.Destroy();
  220. m_additionalDefines = null;
  221. m_additionalIncludes.Destroy();
  222. m_additionalIncludes = null;
  223. m_additionalPragmas.Destroy();
  224. m_additionalPragmas = null;
  225. m_additionalDirectives.Destroy();
  226. m_additionalDirectives = null;
  227. }
  228. public string GenerateAllModulesString( bool isSubShader )
  229. {
  230. string moduleBody = string.Empty;
  231. if( !ShaderModelHelper.IndependentModule )
  232. {
  233. moduleBody += ShaderModelHelper.GenerateShaderData( isSubShader ) + "\n";
  234. }
  235. if( !BlendOpHelper.IndependentModule )
  236. {
  237. if( BlendOpHelper.BlendModeEnabled )
  238. moduleBody += BlendOpHelper.CurrentBlendFactor + "\n";
  239. if( BlendOpHelper.BlendOpActive )
  240. moduleBody += BlendOpHelper.CurrentBlendOp + "\n";
  241. }
  242. if( !BlendOpHelper.AlphaToMaskIndependent )
  243. {
  244. if( BlendOpHelper.ValidAlphaToMask && BlendOpHelper.AlphaToMaskValue )
  245. moduleBody += BlendOpHelper.CurrentAlphaToMask + "\n";
  246. }
  247. if( !CullModeHelper.IndependentModule )
  248. moduleBody += CullModeHelper.GenerateShaderData( isSubShader ) + "\n";
  249. if( !ColorMaskHelper.IndependentModule )
  250. moduleBody += ColorMaskHelper.GenerateShaderData( isSubShader ) + "\n";
  251. if( !DepthOphelper.IndependentModule )
  252. {
  253. moduleBody += DepthOphelper.CurrentZWriteMode;
  254. moduleBody += DepthOphelper.CurrentZTestMode;
  255. if( DepthOphelper.OffsetEnabled )
  256. moduleBody += DepthOphelper.CurrentOffset;
  257. }
  258. if( !StencilBufferHelper.IndependentModule && StencilBufferHelper.Active )
  259. {
  260. CullMode cullMode = ( CullModeHelper.ValidData ) ? CullModeHelper.CurrentCullMode : CullMode.Back;
  261. moduleBody += StencilBufferHelper.CreateStencilOp( cullMode );
  262. }
  263. return moduleBody;
  264. }
  265. public void ReadFromString( ref uint index, ref string[] nodeParams )
  266. {
  267. try
  268. {
  269. m_blendOpHelper.ReadFromString( ref index, ref nodeParams );
  270. }
  271. catch( Exception e )
  272. {
  273. Debug.LogException( e );
  274. }
  275. try
  276. {
  277. m_cullModeHelper.ReadFromString( ref index, ref nodeParams );
  278. }
  279. catch( Exception e )
  280. {
  281. Debug.LogException( e );
  282. }
  283. try
  284. {
  285. m_colorMaskHelper.ReadFromString( ref index, ref nodeParams );
  286. }
  287. catch( Exception e )
  288. {
  289. Debug.LogException( e );
  290. }
  291. try
  292. {
  293. m_stencilBufferHelper.ReadFromString( ref index, ref nodeParams );
  294. }
  295. catch( Exception e )
  296. {
  297. Debug.LogException( e );
  298. }
  299. try
  300. {
  301. m_depthOphelper.ReadFromString( ref index, ref nodeParams );
  302. }
  303. catch( Exception e )
  304. {
  305. Debug.LogException( e );
  306. }
  307. try
  308. {
  309. m_tagsHelper.ReadFromString( ref index, ref nodeParams );
  310. }
  311. catch( Exception e )
  312. {
  313. Debug.LogException( e );
  314. }
  315. try
  316. {
  317. m_shaderModelHelper.ReadFromString( ref index, ref nodeParams );
  318. }
  319. catch( Exception e )
  320. {
  321. Debug.LogException( e );
  322. }
  323. if( UIUtils.CurrentShaderVersion() < 15312 )
  324. {
  325. try
  326. {
  327. m_additionalDefines.ReadFromString( ref index, ref nodeParams );
  328. }
  329. catch( Exception e )
  330. {
  331. Debug.LogException( e );
  332. }
  333. try
  334. {
  335. m_additionalPragmas.ReadFromString( ref index, ref nodeParams );
  336. }
  337. catch( Exception e )
  338. {
  339. Debug.LogException( e );
  340. }
  341. try
  342. {
  343. m_additionalIncludes.ReadFromString( ref index, ref nodeParams );
  344. }
  345. catch( Exception e )
  346. {
  347. Debug.LogException( e );
  348. }
  349. m_additionalDirectives.AddItems( AdditionalLineType.Include, m_additionalIncludes.ItemsList );
  350. m_additionalDirectives.AddItems( AdditionalLineType.Define, m_additionalDefines.ItemsList );
  351. m_additionalDirectives.AddItems( AdditionalLineType.Pragma, m_additionalPragmas.ItemsList );
  352. }
  353. else
  354. {
  355. try
  356. {
  357. m_additionalDirectives.ReadFromString( ref index, ref nodeParams );
  358. }
  359. catch( Exception e )
  360. {
  361. Debug.LogException( e );
  362. }
  363. }
  364. }
  365. public void WriteToString( ref string nodeInfo )
  366. {
  367. m_blendOpHelper.WriteToString( ref nodeInfo );
  368. m_cullModeHelper.WriteToString( ref nodeInfo );
  369. m_colorMaskHelper.WriteToString( ref nodeInfo );
  370. m_stencilBufferHelper.WriteToString( ref nodeInfo );
  371. m_depthOphelper.WriteToString( ref nodeInfo );
  372. m_tagsHelper.WriteToString( ref nodeInfo );
  373. m_shaderModelHelper.WriteToString( ref nodeInfo );
  374. //m_additionalDefines.WriteToString( ref nodeInfo );
  375. //m_additionalPragmas.WriteToString( ref nodeInfo );
  376. //m_additionalIncludes.WriteToString( ref nodeInfo );
  377. m_additionalDirectives.WriteToString( ref nodeInfo );
  378. }
  379. public TemplatesBlendModule BlendOpHelper { get { return m_blendOpHelper; } }
  380. public TemplateCullModeModule CullModeHelper { get { return m_cullModeHelper; } }
  381. public TemplateColorMaskModule ColorMaskHelper { get { return m_colorMaskHelper; } }
  382. public TemplatesStencilBufferModule StencilBufferHelper { get { return m_stencilBufferHelper; } }
  383. public TemplateDepthModule DepthOphelper { get { return m_depthOphelper; } }
  384. public TemplateTagsModule TagsHelper { get { return m_tagsHelper; } }
  385. public TemplateShaderModelModule ShaderModelHelper { get { return m_shaderModelHelper; } }
  386. //public TemplateAdditionalIncludesHelper AdditionalIncludes { get { return m_additionalIncludes; } }
  387. //public TemplateAdditionalDefinesHelper AdditionalDefines { get { return m_additionalDefines; } }
  388. //public TemplateAdditionalPragmasHelper AdditionalPragmas { get { return m_additionalPragmas; } }
  389. public TemplateAdditionalDirectivesHelper AdditionalDirectives { get { return m_additionalDirectives; } }
  390. public bool AllModulesMode { get { return m_allModulesMode; } }
  391. public bool HasValidData { get { return m_hasValidData; } }
  392. public bool IsDirty
  393. {
  394. get { return m_isDirty; }
  395. set
  396. {
  397. m_isDirty = value;
  398. if( !value )
  399. {
  400. m_blendOpHelper.IsDirty = false;
  401. m_cullModeHelper.IsDirty = false;
  402. m_colorMaskHelper.IsDirty = false;
  403. m_stencilBufferHelper.IsDirty = false;
  404. m_tagsHelper.IsDirty = false;
  405. m_shaderModelHelper.IsDirty = false;
  406. //m_additionalDefines.IsDirty = false;
  407. //m_additionalPragmas.IsDirty = false;
  408. //m_additionalIncludes.IsDirty = false;
  409. m_additionalDirectives.IsDirty = false;
  410. }
  411. }
  412. }
  413. // public bool Foldout { get { return m_foldout; } set { m_foldout = value; } }
  414. }
  415. }