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.

800 lines
35 KiB

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