TemplateModulesData.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Text.RegularExpressions;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. namespace AmplifyShaderEditor
  8. {
  9. public enum TemplateModuleDataType
  10. {
  11. ModuleShaderModel,
  12. ModuleBlendMode,
  13. ModuleBlendMode1,
  14. ModuleBlendMode2,
  15. ModuleBlendMode3,
  16. ModuleBlendOp,
  17. ModuleBlendOp1,
  18. ModuleBlendOp2,
  19. ModuleBlendOp3,
  20. ModuleAlphaToMask,
  21. ModuleCullMode,
  22. ModuleColorMask,
  23. ModuleColorMask1,
  24. ModuleColorMask2,
  25. ModuleColorMask3,
  26. ModuleStencil,
  27. ModuleZwrite,
  28. ModuleZTest,
  29. ModuleZOffset,
  30. ModuleTag,
  31. ModuleGlobals,
  32. ModuleSRPBatcher,
  33. ModuleFunctions,
  34. ModulePragma,
  35. ModulePragmaBefore,
  36. ModulePass,
  37. ModuleInputVert,
  38. ModuleInputFrag,
  39. PassVertexFunction,
  40. PassFragmentFunction,
  41. PassVertexData,
  42. PassInterpolatorData,
  43. PassNameData,
  44. AllModules,
  45. VControl,
  46. ControlData,
  47. DomainData,
  48. ModuleRenderPlatforms
  49. //EndPass
  50. }
  51. public enum TemplateSRPType
  52. {
  53. BiRP,
  54. HDRP,
  55. URP
  56. }
  57. [Serializable]
  58. public class TemplateModulesData
  59. {
  60. [SerializeField]
  61. TemplateRenderPlatformHelper m_renderPlatformHelper;
  62. [SerializeField]
  63. private TemplateBlendData m_blendData = new TemplateBlendData();
  64. [SerializeField]
  65. private TemplateBlendData m_blendData1 = new TemplateBlendData();
  66. [SerializeField]
  67. private TemplateBlendData m_blendData2 = new TemplateBlendData();
  68. [SerializeField]
  69. private TemplateBlendData m_blendData3 = new TemplateBlendData();
  70. [SerializeField]
  71. private TemplateAlphaToMaskData m_alphaToMaskData = new TemplateAlphaToMaskData();
  72. [SerializeField]
  73. private TemplateCullModeData m_cullModeData = new TemplateCullModeData();
  74. [SerializeField]
  75. private TemplateColorMaskData m_colorMaskData = new TemplateColorMaskData();
  76. [SerializeField]
  77. private TemplateColorMaskData m_colorMaskData1 = new TemplateColorMaskData();
  78. [SerializeField]
  79. private TemplateColorMaskData m_colorMaskData2 = new TemplateColorMaskData();
  80. [SerializeField]
  81. private TemplateColorMaskData m_colorMaskData3 = new TemplateColorMaskData();
  82. [SerializeField]
  83. private TemplateStencilData m_stencilData = new TemplateStencilData();
  84. [SerializeField]
  85. private TemplateDepthData m_depthData = new TemplateDepthData();
  86. [SerializeField]
  87. private TemplateTagsModuleData m_tagData = new TemplateTagsModuleData();
  88. [SerializeField]
  89. private TemplateTagData m_globalsTag = new TemplateTagData( TemplatesManager.TemplateGlobalsTag, true );
  90. [SerializeField]
  91. private TemplateTagData m_srpBatcherTag = new TemplateTagData( TemplatesManager.TemplateSRPBatcherTag, true );
  92. [SerializeField]
  93. private TemplateTagData m_allModulesTag = new TemplateTagData( TemplatesManager.TemplateAllModulesTag, true );
  94. [SerializeField]
  95. private TemplateTagData m_functionsTag = new TemplateTagData( TemplatesManager.TemplateFunctionsTag, true );
  96. [SerializeField]
  97. private TemplateTagData m_pragmaTag = new TemplateTagData( TemplatesManager.TemplatePragmaTag, true );
  98. [SerializeField]
  99. private TemplateTagData m_pragmaBeforeTag = new TemplateTagData( TemplatesManager.TemplatePragmaBeforeTag, true );
  100. [SerializeField]
  101. private TemplateTagData m_passTag = new TemplateTagData( TemplatesManager.TemplatePassTag, true );
  102. [SerializeField]
  103. private TemplateTagData m_inputsVertTag = new TemplateTagData( TemplatesManager.TemplateInputsVertParamsTag, false );
  104. [SerializeField]
  105. private TemplateTagData m_inputsFragTag = new TemplateTagData( TemplatesManager.TemplateInputsFragParamsTag, false );
  106. [SerializeField]
  107. private TemplateShaderModelData m_shaderModel = new TemplateShaderModelData();
  108. [SerializeField]
  109. private TemplateSRPType m_srpType = TemplateSRPType.BiRP;
  110. [SerializeField]
  111. private bool m_srpIsPBR = false;
  112. [SerializeField]
  113. private string m_uniquePrefix;
  114. [SerializeField]
  115. private TemplateIncludePragmaContainter m_includePragmaContainer = new TemplateIncludePragmaContainter();
  116. [SerializeField]
  117. private bool m_allModulesMode = false;
  118. [SerializeField]
  119. private string m_passUniqueName = string.Empty;
  120. public void Destroy()
  121. {
  122. m_renderPlatformHelper.Destroy();
  123. m_renderPlatformHelper = null;
  124. m_blendData = null;
  125. m_blendData1 = null;
  126. m_blendData2 = null;
  127. m_blendData3 = null;
  128. m_alphaToMaskData = null;
  129. m_cullModeData = null;
  130. m_colorMaskData = null;
  131. m_colorMaskData1 = null;
  132. m_colorMaskData2 = null;
  133. m_colorMaskData3 = null;
  134. m_stencilData = null;
  135. m_depthData = null;
  136. m_tagData.Destroy();
  137. m_tagData = null;
  138. m_globalsTag = null;
  139. m_srpBatcherTag = null;
  140. m_allModulesTag = null;
  141. m_functionsTag = null;
  142. m_pragmaTag = null;
  143. m_pragmaBeforeTag = null;
  144. m_passTag = null;
  145. m_inputsVertTag = null;
  146. m_inputsFragTag = null;
  147. m_includePragmaContainer.Destroy();
  148. m_includePragmaContainer = null;
  149. }
  150. public void ConfigureCommonTag( TemplateTagData tagData, TemplatePropertyContainer propertyContainer, TemplateIdManager idManager, string uniquePrefix, int offsetIdx, string subBody )
  151. {
  152. int id = subBody.IndexOf( tagData.Id );
  153. if ( id >= 0 )
  154. {
  155. tagData.StartIdx = offsetIdx + id;
  156. idManager.RegisterId( tagData.StartIdx, uniquePrefix + tagData.Id, tagData.Id );
  157. propertyContainer.AddId( subBody, tagData.Id, tagData.SearchIndentation );
  158. }
  159. }
  160. public TemplateModulesData( TemplateOptionsContainer optionsContainer, TemplateIdManager idManager, TemplatePropertyContainer propertyContainer, string uniquePrefix, int offsetIdx, string subBody, bool isSubShader )
  161. {
  162. if ( string.IsNullOrEmpty( subBody ) )
  163. return;
  164. m_uniquePrefix = uniquePrefix;
  165. //RENDERING PLATFORMS
  166. m_renderPlatformHelper = new TemplateRenderPlatformHelper();
  167. TemplateHelperFunctions.FillRenderingPlatform( m_renderPlatformHelper , subBody );
  168. if( m_renderPlatformHelper.IsValid )
  169. {
  170. m_renderPlatformHelper.Index = offsetIdx + m_renderPlatformHelper.Index;
  171. idManager.RegisterId( m_renderPlatformHelper.Index , uniquePrefix + m_renderPlatformHelper.ID , m_renderPlatformHelper.ID );
  172. }
  173. //PRAGMAS AND INCLUDES
  174. TemplateHelperFunctions.CreatePragmaIncludeList( subBody, m_includePragmaContainer );
  175. //COMMON TAGS
  176. ConfigureCommonTag( m_globalsTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody );
  177. ConfigureCommonTag( m_srpBatcherTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody );
  178. ConfigureCommonTag( m_functionsTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody );
  179. ConfigureCommonTag( m_pragmaTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody );
  180. ConfigureCommonTag( m_pragmaBeforeTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody );
  181. if( !TemplateHelperFunctions.GetPassUniqueId( m_passTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody, ref m_passUniqueName ) )
  182. {
  183. ConfigureCommonTag( m_passTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody );
  184. }
  185. ConfigureCommonTag( m_inputsVertTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody );
  186. ConfigureCommonTag( m_inputsFragTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody );
  187. // If Options are enabled then remove them so they won't influence Regex matches
  188. if( optionsContainer.Enabled && optionsContainer.EndIndex > 0 )
  189. {
  190. offsetIdx += optionsContainer.EndIndex;
  191. subBody = subBody.Substring( optionsContainer.EndIndex );
  192. }
  193. //BlEND MODE
  194. {
  195. Match blendModeMatch = Regex.Match( subBody, TemplateHelperFunctions.BlendModePattern1 );
  196. if( blendModeMatch.Success )
  197. {
  198. int blendModeIdx = blendModeMatch.Index;
  199. int end = blendModeMatch.Length + blendModeIdx;
  200. string blendParams = subBody.Substring( blendModeIdx, end - blendModeIdx );
  201. m_blendData1.BlendModeId = blendParams;
  202. m_blendData1.BlendModeStartIndex = offsetIdx + blendModeIdx;
  203. idManager.RegisterId( m_blendData1.BlendModeStartIndex, uniquePrefix + m_blendData1.BlendModeId, m_blendData1.BlendModeId );
  204. TemplateHelperFunctions.CreateBlendMode( blendParams, ref m_blendData1, TemplateHelperFunctions.BlendModePattern1 );
  205. if( m_blendData1.ValidBlendMode )
  206. {
  207. propertyContainer.AddId( subBody, blendParams, false );
  208. }
  209. }
  210. }
  211. {
  212. Match blendModeMatch = Regex.Match( subBody, TemplateHelperFunctions.BlendModePattern2 );
  213. if( blendModeMatch.Success )
  214. {
  215. int blendModeIdx = blendModeMatch.Index;
  216. int end = blendModeMatch.Length + blendModeIdx;
  217. string blendParams = subBody.Substring( blendModeIdx, end - blendModeIdx );
  218. m_blendData2.BlendModeId = blendParams;
  219. m_blendData2.BlendModeStartIndex = offsetIdx + blendModeIdx;
  220. idManager.RegisterId( m_blendData2.BlendModeStartIndex, uniquePrefix + m_blendData2.BlendModeId, m_blendData2.BlendModeId );
  221. TemplateHelperFunctions.CreateBlendMode( blendParams, ref m_blendData2, TemplateHelperFunctions.BlendModePattern2 );
  222. if( m_blendData2.ValidBlendMode )
  223. {
  224. propertyContainer.AddId( subBody, blendParams, false );
  225. }
  226. }
  227. }
  228. {
  229. Match blendModeMatch = Regex.Match( subBody, TemplateHelperFunctions.BlendModePattern3 );
  230. if( blendModeMatch.Success )
  231. {
  232. int blendModeIdx = blendModeMatch.Index;
  233. int end = blendModeMatch.Length + blendModeIdx;
  234. string blendParams = subBody.Substring( blendModeIdx, end - blendModeIdx );
  235. m_blendData3.BlendModeId = blendParams;
  236. m_blendData3.BlendModeStartIndex = offsetIdx + blendModeIdx;
  237. idManager.RegisterId( m_blendData3.BlendModeStartIndex, uniquePrefix + m_blendData3.BlendModeId, m_blendData3.BlendModeId );
  238. TemplateHelperFunctions.CreateBlendMode( blendParams, ref m_blendData3, TemplateHelperFunctions.BlendModePattern3 );
  239. if( m_blendData3.ValidBlendMode )
  240. {
  241. propertyContainer.AddId( subBody, blendParams, false );
  242. }
  243. }
  244. }
  245. {
  246. string pattern = TemplateHelperFunctions.BlendModePattern;
  247. Match blendModeMatch = Regex.Match( subBody, pattern );
  248. if( !blendModeMatch.Success && !m_blendData1.ValidBlendMode && !m_blendData2.ValidBlendMode && !m_blendData3.ValidBlendMode )
  249. {
  250. pattern = TemplateHelperFunctions.BlendModePatternFirst;
  251. blendModeMatch = Regex.Match( subBody, pattern );
  252. }
  253. if( blendModeMatch.Success )
  254. {
  255. int blendModeIdx = blendModeMatch.Index;
  256. int end = blendModeMatch.Length + blendModeIdx;
  257. string blendParams = subBody.Substring( blendModeIdx, end - blendModeIdx );
  258. m_blendData.BlendModeId = blendParams;
  259. m_blendData.BlendModeStartIndex = offsetIdx + blendModeIdx;
  260. idManager.RegisterId( m_blendData.BlendModeStartIndex, uniquePrefix + m_blendData.BlendModeId, m_blendData.BlendModeId );
  261. TemplateHelperFunctions.CreateBlendMode( blendParams, ref m_blendData, pattern );
  262. if( m_blendData.ValidBlendMode )
  263. {
  264. propertyContainer.AddId( subBody, blendParams, false );
  265. }
  266. }
  267. }
  268. //BLEND OP
  269. {
  270. Match blendOpMatch = Regex.Match( subBody, TemplateHelperFunctions.BlendOpPattern1 );
  271. if( blendOpMatch.Success )
  272. {
  273. int blendOpIdx = blendOpMatch.Index;
  274. int end = blendOpMatch.Length + blendOpIdx;
  275. string blendOpParams = subBody.Substring( blendOpIdx, end - blendOpIdx );
  276. m_blendData1.BlendOpId = blendOpParams;
  277. m_blendData1.BlendOpStartIndex = offsetIdx + blendOpIdx;
  278. idManager.RegisterId( m_blendData1.BlendOpStartIndex, uniquePrefix + m_blendData1.BlendOpId, m_blendData1.BlendOpId );
  279. TemplateHelperFunctions.CreateBlendOp( blendOpParams, ref m_blendData1, TemplateHelperFunctions.BlendOpPattern1 );
  280. if( m_blendData1.ValidBlendOp )
  281. {
  282. propertyContainer.AddId( subBody, blendOpParams, false );
  283. }
  284. }
  285. m_blendData1.DataCheck = ( m_blendData1.ValidBlendMode || m_blendData1.ValidBlendOp ) ? TemplateDataCheck.Valid : TemplateDataCheck.Invalid;
  286. }
  287. {
  288. Match blendOpMatch = Regex.Match( subBody, TemplateHelperFunctions.BlendOpPattern2 );
  289. if( blendOpMatch.Success )
  290. {
  291. int blendOpIdx = blendOpMatch.Index;
  292. int end = blendOpMatch.Length + blendOpIdx;
  293. string blendOpParams = subBody.Substring( blendOpIdx, end - blendOpIdx );
  294. m_blendData2.BlendOpId = blendOpParams;
  295. m_blendData2.BlendOpStartIndex = offsetIdx + blendOpIdx;
  296. idManager.RegisterId( m_blendData2.BlendOpStartIndex, uniquePrefix + m_blendData2.BlendOpId, m_blendData2.BlendOpId );
  297. TemplateHelperFunctions.CreateBlendOp( blendOpParams, ref m_blendData2, TemplateHelperFunctions.BlendOpPattern2 );
  298. if( m_blendData2.ValidBlendOp )
  299. {
  300. propertyContainer.AddId( subBody, blendOpParams, false );
  301. }
  302. }
  303. m_blendData2.DataCheck = ( m_blendData2.ValidBlendMode || m_blendData2.ValidBlendOp ) ? TemplateDataCheck.Valid : TemplateDataCheck.Invalid;
  304. }
  305. {
  306. Match blendOpMatch = Regex.Match( subBody, TemplateHelperFunctions.BlendOpPattern3 );
  307. if( blendOpMatch.Success )
  308. {
  309. int blendOpIdx = blendOpMatch.Index;
  310. int end = blendOpMatch.Length + blendOpIdx;
  311. string blendOpParams = subBody.Substring( blendOpIdx, end - blendOpIdx );
  312. m_blendData3.BlendOpId = blendOpParams;
  313. m_blendData3.BlendOpStartIndex = offsetIdx + blendOpIdx;
  314. idManager.RegisterId( m_blendData3.BlendOpStartIndex, uniquePrefix + m_blendData3.BlendOpId, m_blendData3.BlendOpId );
  315. TemplateHelperFunctions.CreateBlendOp( blendOpParams, ref m_blendData3, TemplateHelperFunctions.BlendOpPattern3 );
  316. if( m_blendData3.ValidBlendOp )
  317. {
  318. propertyContainer.AddId( subBody, blendOpParams, false );
  319. }
  320. }
  321. m_blendData3.DataCheck = ( m_blendData3.ValidBlendMode || m_blendData3.ValidBlendOp ) ? TemplateDataCheck.Valid : TemplateDataCheck.Invalid;
  322. }
  323. {
  324. string pattern = TemplateHelperFunctions.BlendOpPattern;
  325. Match blendOpMatch = Regex.Match( subBody, pattern );
  326. if( !blendOpMatch.Success && !m_blendData1.ValidBlendOp && !m_blendData2.ValidBlendOp && !m_blendData3.ValidBlendOp )
  327. {
  328. pattern = TemplateHelperFunctions.BlendOpPatternFirst;
  329. blendOpMatch = Regex.Match( subBody, pattern );
  330. }
  331. if( blendOpMatch.Success )
  332. {
  333. int blendOpIdx = blendOpMatch.Index;
  334. int end = blendOpMatch.Length + blendOpIdx;
  335. string blendOpParams = subBody.Substring( blendOpIdx, end - blendOpIdx );
  336. m_blendData.BlendOpId = blendOpParams;
  337. m_blendData.BlendOpStartIndex = offsetIdx + blendOpIdx;
  338. idManager.RegisterId( m_blendData.BlendOpStartIndex, uniquePrefix + m_blendData.BlendOpId, m_blendData.BlendOpId );
  339. TemplateHelperFunctions.CreateBlendOp( blendOpParams, ref m_blendData, pattern );
  340. if( m_blendData.ValidBlendOp )
  341. {
  342. propertyContainer.AddId( subBody, blendOpParams, false );
  343. }
  344. }
  345. m_blendData.DataCheck = ( m_blendData.ValidBlendMode || m_blendData.ValidBlendOp ) ? TemplateDataCheck.Valid : TemplateDataCheck.Invalid;
  346. }
  347. //ALPHA TO MASK
  348. {
  349. Match alphaToMaskMatch = Regex.Match( subBody, TemplateHelperFunctions.AlphaToMaskPattern );
  350. if( alphaToMaskMatch.Success )
  351. {
  352. int alphaIdx = alphaToMaskMatch.Index;
  353. int end = subBody.IndexOf( TemplatesManager.TemplateNewLine, alphaIdx );
  354. string alphaParams = subBody.Substring( alphaIdx, end - alphaIdx );
  355. m_alphaToMaskData.AlphaToMaskId = alphaParams;
  356. m_alphaToMaskData.StartIdx = offsetIdx + alphaIdx;
  357. idManager.RegisterId( m_alphaToMaskData.StartIdx, uniquePrefix + m_alphaToMaskData.AlphaToMaskId, m_alphaToMaskData.AlphaToMaskId );
  358. TemplateHelperFunctions.CreateAlphaToMask( alphaParams, ref m_alphaToMaskData );
  359. if( m_alphaToMaskData.DataCheck == TemplateDataCheck.Valid )
  360. propertyContainer.AddId( subBody, alphaParams, false, string.Empty );
  361. }
  362. }
  363. //CULL MODE
  364. {
  365. Match cullMatch = Regex.Match( subBody, TemplateHelperFunctions.CullWholeWordPattern );
  366. if( cullMatch.Success )
  367. {
  368. int cullIdx = cullMatch.Index;
  369. int end = subBody.IndexOf( TemplatesManager.TemplateNewLine, cullIdx );
  370. string cullParams = subBody.Substring( cullIdx, end - cullIdx );
  371. m_cullModeData.CullModeId = cullParams;
  372. m_cullModeData.StartIdx = offsetIdx + cullIdx;
  373. idManager.RegisterId( m_cullModeData.StartIdx, uniquePrefix + m_cullModeData.CullModeId, m_cullModeData.CullModeId );
  374. TemplateHelperFunctions.CreateCullMode( cullParams, ref m_cullModeData );
  375. if( m_cullModeData.DataCheck == TemplateDataCheck.Valid )
  376. propertyContainer.AddId( subBody, cullParams, false, string.Empty );
  377. }
  378. }
  379. //COLOR MASK
  380. {
  381. Match colorMaskMatch = Regex.Match( subBody, TemplateHelperFunctions.ColorMask1Pattern );
  382. if( colorMaskMatch.Success )
  383. {
  384. int colorMaskIdx = colorMaskMatch.Index;
  385. int end = colorMaskMatch.Length + colorMaskIdx;// subBody.IndexOf( TemplatesManager.TemplateNewLine, colorMaskIdx );
  386. string colorMaskParams = subBody.Substring( colorMaskIdx, end - colorMaskIdx );
  387. m_colorMaskData1.ColorMaskId = colorMaskParams;
  388. m_colorMaskData1.StartIdx = offsetIdx + colorMaskIdx;
  389. idManager.RegisterId( m_colorMaskData1.StartIdx, uniquePrefix + m_colorMaskData1.ColorMaskId, m_colorMaskData1.ColorMaskId );
  390. TemplateHelperFunctions.CreateColorMask( colorMaskParams, ref m_colorMaskData1, TemplateHelperFunctions.ColorMask1Pattern );
  391. if( m_colorMaskData1.DataCheck == TemplateDataCheck.Valid )
  392. propertyContainer.AddId( subBody, colorMaskParams, false );
  393. }
  394. }
  395. {
  396. Match colorMaskMatch = Regex.Match( subBody, TemplateHelperFunctions.ColorMask2Pattern );
  397. if( colorMaskMatch.Success )
  398. {
  399. int colorMaskIdx = colorMaskMatch.Index;
  400. int end = colorMaskMatch.Length + colorMaskIdx;// subBody.IndexOf( TemplatesManager.TemplateNewLine, colorMaskIdx );
  401. string colorMaskParams = subBody.Substring( colorMaskIdx, end - colorMaskIdx );
  402. m_colorMaskData2.ColorMaskId = colorMaskParams;
  403. m_colorMaskData2.StartIdx = offsetIdx + colorMaskIdx;
  404. idManager.RegisterId( m_colorMaskData2.StartIdx, uniquePrefix + m_colorMaskData2.ColorMaskId, m_colorMaskData2.ColorMaskId );
  405. TemplateHelperFunctions.CreateColorMask( colorMaskParams, ref m_colorMaskData2, TemplateHelperFunctions.ColorMask2Pattern );
  406. if( m_colorMaskData2.DataCheck == TemplateDataCheck.Valid )
  407. propertyContainer.AddId( subBody, colorMaskParams, false );
  408. }
  409. }
  410. {
  411. Match colorMaskMatch = Regex.Match( subBody, TemplateHelperFunctions.ColorMask3Pattern );
  412. if( colorMaskMatch.Success )
  413. {
  414. int colorMaskIdx = colorMaskMatch.Index;
  415. int end = colorMaskMatch.Length + colorMaskIdx;// subBody.IndexOf( TemplatesManager.TemplateNewLine, colorMaskIdx );
  416. string colorMaskParams = subBody.Substring( colorMaskIdx, end - colorMaskIdx );
  417. m_colorMaskData3.ColorMaskId = colorMaskParams;
  418. m_colorMaskData3.StartIdx = offsetIdx + colorMaskIdx;
  419. idManager.RegisterId( m_colorMaskData3.StartIdx, uniquePrefix + m_colorMaskData3.ColorMaskId, m_colorMaskData3.ColorMaskId );
  420. TemplateHelperFunctions.CreateColorMask( colorMaskParams, ref m_colorMaskData3, TemplateHelperFunctions.ColorMask3Pattern );
  421. if( m_colorMaskData3.DataCheck == TemplateDataCheck.Valid )
  422. propertyContainer.AddId( subBody, colorMaskParams, false );
  423. }
  424. }
  425. {
  426. string pattern = TemplateHelperFunctions.ColorMaskPattern;
  427. Match colorMaskMatch = Regex.Match( subBody, pattern );
  428. if( !colorMaskMatch.Success && m_colorMaskData1.DataCheck == TemplateDataCheck.Invalid && m_colorMaskData2.DataCheck == TemplateDataCheck.Invalid && m_colorMaskData3.DataCheck == TemplateDataCheck.Invalid )
  429. {
  430. pattern = TemplateHelperFunctions.ColorMaskPatternFirst;
  431. colorMaskMatch = Regex.Match( subBody, pattern );
  432. }
  433. if( colorMaskMatch.Success )
  434. {
  435. int colorMaskIdx = colorMaskMatch.Index;
  436. int end = colorMaskMatch.Length + colorMaskIdx; //subBody.IndexOf( TemplatesManager.TemplateNewLine, colorMaskIdx );
  437. string colorMaskParams = subBody.Substring( colorMaskIdx, end - colorMaskIdx );
  438. m_colorMaskData.ColorMaskId = colorMaskParams;
  439. m_colorMaskData.StartIdx = offsetIdx + colorMaskIdx;
  440. idManager.RegisterId( m_colorMaskData.StartIdx, uniquePrefix + m_colorMaskData.ColorMaskId, m_colorMaskData.ColorMaskId );
  441. TemplateHelperFunctions.CreateColorMask( colorMaskParams, ref m_colorMaskData, pattern );
  442. if( m_colorMaskData.DataCheck == TemplateDataCheck.Valid )
  443. propertyContainer.AddId( subBody, colorMaskParams, false );
  444. }
  445. }
  446. //STENCIL
  447. {
  448. Match stencilMatch = Regex.Match( subBody, TemplateHelperFunctions.StencilWholeWordPattern );
  449. if( stencilMatch.Success )
  450. {
  451. int stencilIdx = stencilMatch.Index;
  452. int stencilEndIdx = subBody.IndexOf( "}", stencilIdx );
  453. if( stencilEndIdx > 0 )
  454. {
  455. string stencilParams = subBody.Substring( stencilIdx, stencilEndIdx + 1 - stencilIdx );
  456. m_stencilData.StencilBufferId = stencilParams;
  457. m_stencilData.StartIdx = offsetIdx + stencilIdx;
  458. idManager.RegisterId( m_stencilData.StartIdx, uniquePrefix + m_stencilData.StencilBufferId, m_stencilData.StencilBufferId );
  459. TemplateHelperFunctions.CreateStencilOps( stencilParams, ref m_stencilData );
  460. if( m_stencilData.DataCheck == TemplateDataCheck.Valid )
  461. {
  462. propertyContainer.AddId( subBody, stencilParams, true );
  463. }
  464. }
  465. }
  466. else
  467. {
  468. int stencilTagIdx = subBody.IndexOf( TemplatesManager.TemplateStencilTag );
  469. if( stencilTagIdx > -1 )
  470. {
  471. m_stencilData.SetIndependentDefault();
  472. m_stencilData.StencilBufferId = TemplatesManager.TemplateStencilTag;
  473. m_stencilData.StartIdx = offsetIdx + stencilTagIdx;
  474. idManager.RegisterId( m_stencilData.StartIdx, uniquePrefix + m_stencilData.StencilBufferId, m_stencilData.StencilBufferId );
  475. propertyContainer.AddId( subBody, m_stencilData.StencilBufferId, true );
  476. }
  477. }
  478. }
  479. //ZWRITE
  480. {
  481. Match zWriteMatch = Regex.Match( subBody, TemplateHelperFunctions.ZWriteWholeWordPattern );
  482. if( zWriteMatch.Success )
  483. {
  484. int zWriteOpIdx = zWriteMatch.Index;
  485. int zWriteEndIdx = subBody.IndexOf( TemplatesManager.TemplateNewLine, zWriteOpIdx );
  486. if( zWriteEndIdx > 0 )
  487. {
  488. m_depthData.ZWriteModeId = subBody.Substring( zWriteOpIdx, zWriteEndIdx + 1 - zWriteOpIdx );
  489. m_depthData.ZWriteStartIndex = offsetIdx + zWriteOpIdx;
  490. idManager.RegisterId( m_depthData.ZWriteStartIndex, uniquePrefix + m_depthData.ZWriteModeId, m_depthData.ZWriteModeId );
  491. TemplateHelperFunctions.CreateZWriteMode( m_depthData.ZWriteModeId, ref m_depthData );
  492. if( m_depthData.DataCheck == TemplateDataCheck.Valid )
  493. {
  494. propertyContainer.AddId( subBody, m_depthData.ZWriteModeId, true );
  495. }
  496. }
  497. }
  498. }
  499. //ZTEST
  500. {
  501. Match zTestMatch = Regex.Match( subBody, TemplateHelperFunctions.ZTestWholeWordPattern );
  502. if( zTestMatch.Success )
  503. {
  504. int zTestOpIdx = zTestMatch.Index;
  505. int zTestEndIdx = subBody.IndexOf( TemplatesManager.TemplateNewLine, zTestOpIdx );
  506. if( zTestEndIdx > 0 )
  507. {
  508. m_depthData.ZTestModeId = subBody.Substring( zTestOpIdx, zTestEndIdx + 1 - zTestOpIdx );
  509. m_depthData.ZTestStartIndex = offsetIdx + zTestOpIdx;
  510. idManager.RegisterId( m_depthData.ZTestStartIndex, uniquePrefix + m_depthData.ZTestModeId, m_depthData.ZTestModeId );
  511. TemplateHelperFunctions.CreateZTestMode( m_depthData.ZTestModeId, ref m_depthData );
  512. if( m_depthData.DataCheck == TemplateDataCheck.Valid )
  513. {
  514. propertyContainer.AddId( subBody, m_depthData.ZTestModeId, true );
  515. }
  516. }
  517. }
  518. }
  519. //ZOFFSET
  520. {
  521. Match zOffsetMatch = Regex.Match( subBody, TemplateHelperFunctions.ZOffsetWholeWordPattern );
  522. if( zOffsetMatch.Success )
  523. {
  524. int zOffsetIdx = zOffsetMatch.Index;
  525. int zOffsetEndIdx = subBody.IndexOf( TemplatesManager.TemplateNewLine, zOffsetIdx );
  526. if( zOffsetEndIdx > 0 )
  527. {
  528. m_depthData.OffsetId = subBody.Substring( zOffsetIdx, zOffsetEndIdx + 1 - zOffsetIdx );
  529. m_depthData.OffsetStartIndex = offsetIdx + zOffsetIdx;
  530. idManager.RegisterId( m_depthData.OffsetStartIndex, uniquePrefix + m_depthData.OffsetId, m_depthData.OffsetId );
  531. TemplateHelperFunctions.CreateZOffsetMode( m_depthData.OffsetId, ref m_depthData );
  532. if( m_depthData.DataCheck == TemplateDataCheck.Valid )
  533. {
  534. propertyContainer.AddId( subBody, m_depthData.OffsetId, true );
  535. }
  536. }
  537. }
  538. m_depthData.SetDataCheck();
  539. }
  540. //TAGS
  541. {
  542. Match tagsMatch = Regex.Match( subBody, TemplateHelperFunctions.TagsWholeWordPattern );
  543. if ( tagsMatch.Success )
  544. {
  545. int tagsIdx = tagsMatch.Index;
  546. int tagsEndIdx = subBody.IndexOf( "}", tagsIdx );
  547. if ( tagsEndIdx > -1 )
  548. {
  549. m_tagData.Reset();
  550. m_tagData.TagsId = subBody.Substring( tagsIdx, tagsEndIdx + 1 - tagsIdx );
  551. m_tagData.StartIdx = offsetIdx + tagsIdx;
  552. idManager.RegisterId( m_tagData.StartIdx, uniquePrefix + m_tagData.TagsId, m_tagData.TagsId );
  553. m_srpType = TemplateHelperFunctions.CreateTags( ref m_tagData, isSubShader );
  554. propertyContainer.AddId( subBody, m_tagData.TagsId, false );
  555. m_tagData.DataCheck = TemplateDataCheck.Valid;
  556. }
  557. else
  558. {
  559. m_tagData.DataCheck = TemplateDataCheck.Invalid;
  560. }
  561. }
  562. else
  563. {
  564. m_tagData.DataCheck = TemplateDataCheck.Invalid;
  565. }
  566. }
  567. //SHADER MODEL
  568. {
  569. Match match = Regex.Match( subBody, TemplateHelperFunctions.ShaderModelPattern );
  570. if ( match != null && match.Groups.Count > 1 )
  571. {
  572. if ( TemplateHelperFunctions.AvailableInterpolators.ContainsKey( match.Groups[ 1 ].Value ) )
  573. {
  574. m_shaderModel.Id = match.Groups[ 0 ].Value;
  575. m_shaderModel.StartIdx = offsetIdx + match.Index;
  576. m_shaderModel.Value = match.Groups[ 1 ].Value;
  577. m_shaderModel.InterpolatorAmount = TemplateHelperFunctions.AvailableInterpolators[ match.Groups[ 1 ].Value ];
  578. m_shaderModel.DataCheck = TemplateDataCheck.Valid;
  579. idManager.RegisterId( m_shaderModel.StartIdx, uniquePrefix + m_shaderModel.Id, m_shaderModel.Id );
  580. }
  581. else
  582. {
  583. m_shaderModel.DataCheck = TemplateDataCheck.Invalid;
  584. }
  585. }
  586. }
  587. // ALL MODULES
  588. int allModulesIndex = subBody.IndexOf( TemplatesManager.TemplateAllModulesTag );
  589. if( allModulesIndex > 0 )
  590. {
  591. //ONLY REGISTER MISSING TAGS
  592. ConfigureCommonTag( m_allModulesTag, propertyContainer, idManager, uniquePrefix, offsetIdx, subBody );
  593. m_allModulesMode = true;
  594. m_blendData.SetAllModulesDefault();
  595. if( !m_alphaToMaskData.IsValid )
  596. m_alphaToMaskData.SetAllModulesDefault();
  597. if( !m_cullModeData.IsValid )
  598. m_cullModeData.SetAllModulesDefault();
  599. if( !m_colorMaskData.IsValid )
  600. m_colorMaskData.SetAllModulesDefault();
  601. if( !m_stencilData.IsValid )
  602. m_stencilData.SetAllModulesDefault();
  603. if( !m_depthData.IsValid )
  604. m_depthData.SetAllModulesDefault();
  605. if( !m_shaderModel.IsValid )
  606. m_shaderModel.SetAllModulesDefault();
  607. }
  608. }
  609. public void TestPropertyInternalName( string name, ref List<TemplateShaderPropertyData> availableShaderProperties, ref Dictionary<string, TemplateShaderPropertyData> duplicatesHelper )
  610. {
  611. if( !string.IsNullOrEmpty( name ) && !duplicatesHelper.ContainsKey( name ))
  612. {
  613. TemplateShaderPropertyData newData = new TemplateShaderPropertyData( -1, string.Empty, string.Empty, name, name, WirePortDataType.INT, PropertyType.Property,-1,-1 );
  614. availableShaderProperties.Add( newData );
  615. duplicatesHelper.Add( newData.PropertyName , newData );
  616. }
  617. }
  618. public void RegisterInternalUnityInlines( ref List<TemplateShaderPropertyData> availableShaderProperties, ref Dictionary<string, TemplateShaderPropertyData> duplicatesHelper )
  619. {
  620. TestPropertyInternalName( m_depthData.ZWriteInlineValue, ref availableShaderProperties , ref duplicatesHelper);
  621. TestPropertyInternalName( m_depthData.ZTestInlineValue, ref availableShaderProperties, ref duplicatesHelper );
  622. TestPropertyInternalName( m_depthData.OffsetFactorInlineValue, ref availableShaderProperties, ref duplicatesHelper );
  623. TestPropertyInternalName( m_depthData.OffsetUnitsInlineValue, ref availableShaderProperties, ref duplicatesHelper );
  624. TestPropertyInternalName( m_blendData.SourceFactorRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  625. TestPropertyInternalName( m_blendData.DestFactorRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  626. TestPropertyInternalName( m_blendData.SourceFactorAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  627. TestPropertyInternalName( m_blendData.DestFactorAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  628. TestPropertyInternalName( m_blendData.BlendOpRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  629. TestPropertyInternalName( m_blendData.BlendOpAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  630. TestPropertyInternalName( m_blendData1.SourceFactorRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  631. TestPropertyInternalName( m_blendData1.DestFactorRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  632. TestPropertyInternalName( m_blendData1.SourceFactorAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  633. TestPropertyInternalName( m_blendData1.DestFactorAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  634. TestPropertyInternalName( m_blendData1.BlendOpRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  635. TestPropertyInternalName( m_blendData1.BlendOpAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  636. TestPropertyInternalName( m_blendData2.SourceFactorRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  637. TestPropertyInternalName( m_blendData2.DestFactorRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  638. TestPropertyInternalName( m_blendData2.SourceFactorAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  639. TestPropertyInternalName( m_blendData2.DestFactorAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  640. TestPropertyInternalName( m_blendData2.BlendOpRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  641. TestPropertyInternalName( m_blendData2.BlendOpAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  642. TestPropertyInternalName( m_blendData3.SourceFactorRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  643. TestPropertyInternalName( m_blendData3.DestFactorRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  644. TestPropertyInternalName( m_blendData3.SourceFactorAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  645. TestPropertyInternalName( m_blendData3.DestFactorAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  646. TestPropertyInternalName( m_blendData3.BlendOpRGBInline, ref availableShaderProperties, ref duplicatesHelper );
  647. TestPropertyInternalName( m_blendData3.BlendOpAlphaInline, ref availableShaderProperties, ref duplicatesHelper );
  648. TestPropertyInternalName( m_alphaToMaskData.InlineData, ref availableShaderProperties, ref duplicatesHelper );
  649. TestPropertyInternalName( m_stencilData.ReferenceInline, ref availableShaderProperties, ref duplicatesHelper );
  650. TestPropertyInternalName( m_stencilData.ReadMaskInline, ref availableShaderProperties, ref duplicatesHelper );
  651. TestPropertyInternalName( m_stencilData.WriteMaskInline, ref availableShaderProperties, ref duplicatesHelper );
  652. TestPropertyInternalName( m_stencilData.ComparisonFrontInline, ref availableShaderProperties, ref duplicatesHelper );
  653. TestPropertyInternalName( m_stencilData.PassFrontInline, ref availableShaderProperties, ref duplicatesHelper );
  654. TestPropertyInternalName( m_stencilData.FailFrontInline, ref availableShaderProperties, ref duplicatesHelper );
  655. TestPropertyInternalName( m_stencilData.ZFailFrontInline, ref availableShaderProperties, ref duplicatesHelper );
  656. TestPropertyInternalName( m_stencilData.ComparisonBackInline, ref availableShaderProperties, ref duplicatesHelper );
  657. TestPropertyInternalName( m_stencilData.PassBackInline, ref availableShaderProperties, ref duplicatesHelper );
  658. TestPropertyInternalName( m_stencilData.FailBackInline, ref availableShaderProperties, ref duplicatesHelper );
  659. TestPropertyInternalName( m_stencilData.ZFailBackInline, ref availableShaderProperties, ref duplicatesHelper );
  660. TestPropertyInternalName( m_cullModeData.InlineData, ref availableShaderProperties, ref duplicatesHelper );
  661. TestPropertyInternalName( m_colorMaskData.InlineData, ref availableShaderProperties, ref duplicatesHelper );
  662. TestPropertyInternalName( m_colorMaskData1.InlineData, ref availableShaderProperties, ref duplicatesHelper );
  663. TestPropertyInternalName( m_colorMaskData2.InlineData, ref availableShaderProperties, ref duplicatesHelper );
  664. TestPropertyInternalName( m_colorMaskData3.InlineData, ref availableShaderProperties, ref duplicatesHelper );
  665. }
  666. public void SetPassUniqueNameIfUndefined( string value )
  667. {
  668. if( string.IsNullOrEmpty( m_passUniqueName ) )
  669. m_passUniqueName = value;
  670. }
  671. public bool HasValidData
  672. {
  673. get
  674. {
  675. return m_blendData.DataCheck == TemplateDataCheck.Valid ||
  676. m_blendData1.DataCheck == TemplateDataCheck.Valid ||
  677. m_blendData2.DataCheck == TemplateDataCheck.Valid ||
  678. m_blendData3.DataCheck == TemplateDataCheck.Valid ||
  679. m_alphaToMaskData.DataCheck == TemplateDataCheck.Valid ||
  680. m_cullModeData.DataCheck == TemplateDataCheck.Valid ||
  681. m_colorMaskData.DataCheck == TemplateDataCheck.Valid ||
  682. m_colorMaskData1.DataCheck == TemplateDataCheck.Valid ||
  683. m_colorMaskData2.DataCheck == TemplateDataCheck.Valid ||
  684. m_colorMaskData3.DataCheck == TemplateDataCheck.Valid ||
  685. m_stencilData.DataCheck == TemplateDataCheck.Valid ||
  686. m_depthData.DataCheck == TemplateDataCheck.Valid ||
  687. m_tagData.DataCheck == TemplateDataCheck.Valid ||
  688. m_shaderModel.DataCheck == TemplateDataCheck.Valid ||
  689. m_globalsTag.IsValid ||
  690. m_srpBatcherTag.IsValid ||
  691. m_allModulesTag.IsValid ||
  692. m_functionsTag.IsValid ||
  693. m_pragmaTag.IsValid ||
  694. m_pragmaBeforeTag.IsValid ||
  695. m_passTag.IsValid ||
  696. m_inputsVertTag.IsValid ||
  697. m_inputsFragTag.IsValid ||
  698. m_renderPlatformHelper.IsValid;
  699. }
  700. }
  701. public TemplateBlendData BlendData { get { return m_blendData; } }
  702. public TemplateBlendData BlendData1 { get { return m_blendData1; } }
  703. public TemplateBlendData BlendData2 { get { return m_blendData2; } }
  704. public TemplateBlendData BlendData3 { get { return m_blendData3; } }
  705. public TemplateAlphaToMaskData AlphaToMaskData { get { return m_alphaToMaskData; } }
  706. public TemplateCullModeData CullModeData { get { return m_cullModeData; } }
  707. public TemplateColorMaskData ColorMaskData { get { return m_colorMaskData; } }
  708. public TemplateColorMaskData ColorMaskData1 { get { return m_colorMaskData1; } }
  709. public TemplateColorMaskData ColorMaskData2 { get { return m_colorMaskData2; } }
  710. public TemplateColorMaskData ColorMaskData3 { get { return m_colorMaskData3; } }
  711. public TemplateStencilData StencilData { get { return m_stencilData; } }
  712. public TemplateDepthData DepthData { get { return m_depthData; } }
  713. public TemplateTagsModuleData TagData { get { return m_tagData; } }
  714. public TemplateTagData GlobalsTag { get { return m_globalsTag; } }
  715. public TemplateTagData SRPBatcherTag { get { return m_srpBatcherTag; } }
  716. public TemplateTagData AllModulesTag { get { return m_allModulesTag; } }
  717. public TemplateTagData FunctionsTag { get { return m_functionsTag; } }
  718. public TemplateTagData PragmaTag { get { return m_pragmaTag; } }
  719. public TemplateTagData PragmaBeforeTag { get { return m_pragmaBeforeTag; } }
  720. public TemplateTagData PassTag { get { return m_passTag; } }
  721. public TemplateTagData InputsVertTag { get { return m_inputsVertTag; } }
  722. public TemplateTagData InputsFragTag { get { return m_inputsFragTag; } }
  723. public TemplateShaderModelData ShaderModel { get { return m_shaderModel; } }
  724. public TemplateSRPType SRPType { get { return m_srpType; } set { m_srpType = value; } }
  725. public bool SRPIsPBR { get { return m_srpIsPBR; } set { m_srpIsPBR = value; } }
  726. public bool SRPIsPBRHD { get { return m_srpIsPBR && m_srpType == TemplateSRPType.HDRP; } }
  727. public string UniquePrefix { get { return m_uniquePrefix; } }
  728. public string PassUniqueName { get { return m_passUniqueName; } }
  729. public bool HasPassUniqueName { get { return !string.IsNullOrEmpty( m_passUniqueName ); } }
  730. public TemplateIncludePragmaContainter IncludePragmaContainer { get { return m_includePragmaContainer; } }
  731. public TemplateRenderPlatformHelper RenderPlatformHelper { get { return m_renderPlatformHelper; } }
  732. public bool AllModulesMode { get { return m_allModulesMode; } }
  733. }
  734. }