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.

2481 lines
86 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using UnityEngine;
  7. using UnityEditor;
  8. using System.Collections.Generic;
  9. namespace AmplifyShaderEditor
  10. {
  11. public enum CustomTemplatePropertyUIEnum
  12. {
  13. None,
  14. HDPBR
  15. }
  16. public enum TemplateSemantics
  17. {
  18. NONE,
  19. POSITION,
  20. SV_POSITION,
  21. COLOR,
  22. COLOR0,
  23. COLOR1,
  24. TEXCOORD0,
  25. TEXCOORD1,
  26. TEXCOORD2,
  27. TEXCOORD3,
  28. TEXCOORD4,
  29. TEXCOORD5,
  30. TEXCOORD6,
  31. TEXCOORD7,
  32. TEXCOORD8,
  33. TEXCOORD9,
  34. TEXCOORD10,
  35. TEXCOORD11,
  36. TEXCOORD12,
  37. TEXCOORD13,
  38. TEXCOORD14,
  39. TEXCOORD15,
  40. NORMAL,
  41. TANGENT,
  42. VFACE,
  43. SV_VertexID,
  44. SV_PrimitiveID,
  45. SV_InstanceID,
  46. INTERNALTESSPOS,
  47. INSTANCEID_SEMANTIC
  48. }
  49. public enum TemplateInfoOnSematics
  50. {
  51. NONE,
  52. POSITION,
  53. CLIP_POS,
  54. SCREEN_POSITION,
  55. SCREEN_POSITION_NORMALIZED,
  56. COLOR,
  57. TEXTURE_COORDINATES0,
  58. TEXTURE_COORDINATES1,
  59. TEXTURE_COORDINATES2,
  60. TEXTURE_COORDINATES3,
  61. TEXTURE_COORDINATES4,
  62. TEXTURE_COORDINATES5,
  63. TEXTURE_COORDINATES6,
  64. TEXTURE_COORDINATES7,
  65. NORMAL,
  66. TANGENT,
  67. WORLD_NORMAL,
  68. WORLD_TANGENT,
  69. WORLD_BITANGENT,
  70. WORLD_VIEW_DIR,
  71. WORLD_POSITION,
  72. RELATIVE_WORLD_POS,
  73. INSTANCE_ID,
  74. OTHER,
  75. VFACE,
  76. SHADOWCOORDS,
  77. VERTEXID
  78. }
  79. public enum TemplateShaderPropertiesIdx
  80. {
  81. Identation = 1,
  82. Name = 3,
  83. InspectorName,
  84. Type
  85. }
  86. public enum TemplateShaderGlobalsIdx
  87. {
  88. Type = 1,
  89. Name = 2
  90. }
  91. public enum TemplateDataCheck
  92. {
  93. Valid,
  94. Invalid
  95. }
  96. public enum InvisibleOptionsEnum
  97. {
  98. SyncProperties = 1 << 0
  99. }
  100. public enum TemplateSpecialTags
  101. {
  102. RenderType,
  103. Queue,
  104. DisableBatching,
  105. None
  106. }
  107. public class TemplateReplaceHelper
  108. {
  109. public TemplateMultiPassMasterNode MasterNode = null;
  110. public bool Used = false;
  111. public TemplateReplaceHelper( TemplateMultiPassMasterNode masterNode ) { MasterNode = masterNode; }
  112. }
  113. [Serializable]
  114. public class TemplatesTagData
  115. {
  116. public string Name;
  117. public string Value;
  118. public TemplatesTagData( string name, string value )
  119. {
  120. Name = name;
  121. Value = value;
  122. }
  123. }
  124. [Serializable]
  125. public class TemplateModuleData
  126. {
  127. public bool IndependentModule = true;
  128. public TemplateDataCheck DataCheck = TemplateDataCheck.Invalid;
  129. public string InlineData = string.Empty;
  130. public int StartIdx;
  131. public bool IsValid { get { return DataCheck == TemplateDataCheck.Valid; } }
  132. public virtual void SetAllModulesDefault() { IndependentModule = false; DataCheck = TemplateDataCheck.Valid; }
  133. }
  134. [Serializable]
  135. public sealed class TemplateTagsModuleData : TemplateModuleData
  136. {
  137. public string TagsId;
  138. public List<TemplatesTagData> Tags = new List<TemplatesTagData>();
  139. public void Destroy()
  140. {
  141. Tags.Clear();
  142. Tags = null;
  143. }
  144. public void Reset()
  145. {
  146. Tags.Clear();
  147. }
  148. public void Dump()
  149. {
  150. string dump = string.Empty;
  151. for( int i = 0; i < Tags.Count; i++ )
  152. {
  153. dump += string.Format( "[{0}] Name: {1} Value: {2}\n", i, Tags[ i ].Name, Tags[ i ].Value );
  154. }
  155. Debug.Log( dump );
  156. }
  157. }
  158. [Serializable]
  159. public class TemplateShaderModelData : TemplateModuleData
  160. {
  161. public string Id = string.Empty;
  162. public string Value = "2.5";
  163. public int InterpolatorAmount = 8;
  164. public bool Encapsulate = false;
  165. public override void SetAllModulesDefault()
  166. {
  167. base.SetAllModulesDefault();
  168. Id = string.Empty;
  169. Value = "3.0";
  170. InterpolatorAmount = 10;
  171. Encapsulate = true;
  172. }
  173. }
  174. [Serializable]
  175. public sealed class TemplateDepthData : TemplateModuleData
  176. {
  177. public bool ValidZWrite;
  178. public string ZWriteModeId;
  179. public ZWriteMode ZWriteModeValue;
  180. public int ZWriteStartIndex;
  181. public string ZWriteInlineValue;
  182. public bool ValidZTest;
  183. public string ZTestModeId;
  184. public ZTestMode ZTestModeValue;
  185. public int ZTestStartIndex;
  186. public string ZTestInlineValue;
  187. public bool ValidOffset;
  188. public string OffsetId;
  189. public float OffsetFactor;
  190. public float OffsetUnits;
  191. public int OffsetStartIndex;
  192. public string OffsetFactorInlineValue;
  193. public string OffsetUnitsInlineValue;
  194. public override void SetAllModulesDefault()
  195. {
  196. base.SetAllModulesDefault();
  197. ValidZWrite = true;
  198. ZWriteModeId = string.Empty;
  199. ZWriteModeValue = ZWriteMode.On;
  200. ZWriteStartIndex = -1;
  201. ZWriteInlineValue = string.Empty;
  202. ValidZTest = true;
  203. ZTestModeId = string.Empty;
  204. ZTestModeValue = ZTestMode.LEqual;
  205. ZTestStartIndex = -1;
  206. ZTestInlineValue = string.Empty;
  207. ValidOffset = true;
  208. OffsetId = string.Empty;
  209. OffsetFactor = 0;
  210. OffsetUnits = 0;
  211. OffsetStartIndex = -1;
  212. OffsetFactorInlineValue = string.Empty;
  213. OffsetUnitsInlineValue = string.Empty;
  214. }
  215. public void SetDataCheck()
  216. {
  217. DataCheck = ( ValidZWrite || ValidZTest || ValidOffset )?TemplateDataCheck.Valid:TemplateDataCheck.Invalid;
  218. }
  219. }
  220. [Serializable]
  221. public sealed class TemplateStencilData : TemplateModuleData
  222. {
  223. public string StencilBufferId;
  224. public bool Active = true;
  225. public int Reference;
  226. public string ReferenceInline;
  227. public int ReadMask = 255;
  228. public string ReadMaskInline;
  229. public int WriteMask = 255;
  230. public string WriteMaskInline;
  231. public string ComparisonFront;
  232. public string ComparisonFrontInline;
  233. public string PassFront;
  234. public string PassFrontInline;
  235. public string FailFront;
  236. public string FailFrontInline;
  237. public string ZFailFront;
  238. public string ZFailFrontInline;
  239. public string ComparisonBack;
  240. public string ComparisonBackInline;
  241. public string PassBack;
  242. public string PassBackInline;
  243. public string FailBack;
  244. public string FailBackInline;
  245. public string ZFailBack;
  246. public string ZFailBackInline;
  247. public void SetDefaultValues()
  248. {
  249. Active = false;
  250. StencilBufferId = string.Empty;
  251. Reference = 255;
  252. ReferenceInline = string.Empty;
  253. ReadMask = 255;
  254. ReadMaskInline = string.Empty;
  255. WriteMask = 255;
  256. WriteMaskInline = string.Empty;
  257. ComparisonFront = "always";
  258. ComparisonFrontInline = string.Empty;
  259. PassFront = "keep";
  260. PassFrontInline = string.Empty;
  261. FailFront = "keep";
  262. FailFrontInline = string.Empty;
  263. ZFailFront = "keep";
  264. ZFailFrontInline = string.Empty;
  265. ComparisonBack = "always";
  266. ComparisonBackInline = string.Empty;
  267. PassBack = "keep";
  268. PassBackInline = string.Empty;
  269. FailBack = "keep";
  270. FailBackInline = string.Empty;
  271. ZFailBack = "keep";
  272. ZFailBackInline = string.Empty;
  273. }
  274. public void SetIndependentDefault()
  275. {
  276. IndependentModule = true;
  277. DataCheck = TemplateDataCheck.Valid;
  278. SetDefaultValues();
  279. }
  280. public override void SetAllModulesDefault()
  281. {
  282. base.SetAllModulesDefault();
  283. SetDefaultValues();
  284. }
  285. }
  286. [Serializable]
  287. public sealed class TemplateBlendData : TemplateModuleData
  288. {
  289. public bool ValidBlendMode = false;
  290. public bool BlendModeOff = true;
  291. public string Target = string.Empty;
  292. public string BlendModeId;
  293. public bool SeparateBlendFactors = false;
  294. public AvailableBlendFactor SourceFactorRGB = AvailableBlendFactor.One;
  295. public string SourceFactorRGBInline;
  296. public AvailableBlendFactor DestFactorRGB = AvailableBlendFactor.Zero;
  297. public string DestFactorRGBInline;
  298. public int BlendModeStartIndex;
  299. public AvailableBlendFactor SourceFactorAlpha = AvailableBlendFactor.One;
  300. public string SourceFactorAlphaInline;
  301. public AvailableBlendFactor DestFactorAlpha = AvailableBlendFactor.Zero;
  302. public string DestFactorAlphaInline;
  303. public bool ValidBlendOp = false;
  304. public string BlendOpId;
  305. public bool SeparateBlendOps = false;
  306. public AvailableBlendOps BlendOpRGB = AvailableBlendOps.OFF;
  307. public string BlendOpRGBInline;
  308. public AvailableBlendOps BlendOpAlpha = AvailableBlendOps.OFF;
  309. public string BlendOpAlphaInline;
  310. public int BlendOpStartIndex;
  311. public override void SetAllModulesDefault()
  312. {
  313. base.SetAllModulesDefault();
  314. if( !ValidBlendMode )
  315. {
  316. ValidBlendMode = true;
  317. BlendModeOff = true;
  318. BlendModeId = string.Empty;
  319. SeparateBlendFactors = false;
  320. SourceFactorRGB = AvailableBlendFactor.One;
  321. SourceFactorRGBInline = string.Empty;
  322. DestFactorRGB = AvailableBlendFactor.Zero;
  323. DestFactorRGBInline = string.Empty;
  324. BlendModeStartIndex = -1;
  325. SourceFactorAlpha = AvailableBlendFactor.One;
  326. SourceFactorAlphaInline = string.Empty;
  327. DestFactorAlpha = AvailableBlendFactor.Zero;
  328. DestFactorAlphaInline = string.Empty;
  329. }
  330. if( !ValidBlendOp )
  331. {
  332. ValidBlendOp = true;
  333. BlendOpId = string.Empty;
  334. SeparateBlendOps = false;
  335. BlendOpRGB = AvailableBlendOps.OFF;
  336. BlendOpRGBInline = string.Empty;
  337. BlendOpAlpha = AvailableBlendOps.OFF;
  338. BlendOpAlphaInline = string.Empty;
  339. BlendOpStartIndex = -1;
  340. }
  341. DataCheck = TemplateDataCheck.Valid;
  342. }
  343. }
  344. [Serializable]
  345. public sealed class TemplateAlphaToMaskData : TemplateModuleData
  346. {
  347. public string AlphaToMaskId;
  348. public bool AlphaToMaskData = false;
  349. public override void SetAllModulesDefault()
  350. {
  351. base.SetAllModulesDefault();
  352. AlphaToMaskId = string.Empty;
  353. AlphaToMaskData = false;
  354. }
  355. }
  356. [Serializable]
  357. public sealed class TemplateCullModeData : TemplateModuleData
  358. {
  359. public string CullModeId;
  360. public CullMode CullModeData = CullMode.Back;
  361. public override void SetAllModulesDefault()
  362. {
  363. base.SetAllModulesDefault();
  364. CullModeId = string.Empty;
  365. CullModeData = CullMode.Back;
  366. }
  367. }
  368. [Serializable]
  369. public sealed class TemplateColorMaskData : TemplateModuleData
  370. {
  371. public string ColorMaskId;
  372. public bool[] ColorMaskData = { true, true, true, true };
  373. public string Target = string.Empty;
  374. public override void SetAllModulesDefault()
  375. {
  376. base.SetAllModulesDefault();
  377. ColorMaskId = string.Empty;
  378. Target = string.Empty;
  379. for( int i = 0; i < ColorMaskData.Length; i++ )
  380. {
  381. ColorMaskData[ i ] = true;
  382. }
  383. }
  384. }
  385. public static class TemplateHelperFunctions
  386. {
  387. /*
  388. struct DirectionalLightData
  389. {
  390. uint lightLayers;
  391. float3 positionRWS;
  392. float3 color;
  393. int cookieIndex;
  394. float volumetricDimmer;
  395. float3 right;
  396. float3 up;
  397. float3 forward;
  398. int tileCookie;
  399. int shadowIndex;
  400. int contactShadowIndex;
  401. float4 shadowMaskSelector;
  402. int nonLightmappedOnly;
  403. float diffuseScale;
  404. float specularScale;
  405. };
  406. */
  407. public static string HDLightInfoFormat = "_DirectionalLightDatas[{0}].{1}";
  408. public static string[] VectorSwizzle = { "x", "y", "z", "w" };
  409. public static string[] ColorSwizzle = { "r", "g", "b", "a" };
  410. public static readonly Dictionary<string, CustomTemplatePropertyUIEnum> CustomTemplatePropertyUI = new Dictionary<string, CustomTemplatePropertyUIEnum>
  411. {
  412. { "None", CustomTemplatePropertyUIEnum.None},
  413. { "HDPBR", CustomTemplatePropertyUIEnum.HDPBR}
  414. };
  415. public static readonly Dictionary<string, InvisibleOptionsEnum> InvisibleOptions = new Dictionary<string, InvisibleOptionsEnum>()
  416. {
  417. { "SyncP", InvisibleOptionsEnum.SyncProperties }
  418. };
  419. public static readonly Dictionary<string, TemplateSpecialTags> StringToReservedTags = new Dictionary<string, TemplateSpecialTags>()
  420. {
  421. { TemplateSpecialTags.RenderType.ToString(), TemplateSpecialTags.RenderType},
  422. { TemplateSpecialTags.Queue.ToString(), TemplateSpecialTags.Queue},
  423. { TemplateSpecialTags.DisableBatching.ToString(), TemplateSpecialTags.DisableBatching},
  424. };
  425. public static readonly Dictionary<string, DisableBatching> StringToDisableBatching = new Dictionary<string, DisableBatching>
  426. {
  427. {"true",DisableBatching.True},
  428. {"True",DisableBatching.True},
  429. {"false",DisableBatching.False},
  430. {"False",DisableBatching.False},
  431. {"LOD Fading",DisableBatching.LODFading},
  432. {"LODFading",DisableBatching.LODFading}
  433. };
  434. public static readonly Dictionary<string, RenderType> StringToRenderType = new Dictionary<string, RenderType>
  435. {
  436. {"Opaque",RenderType.Opaque},
  437. {"Transparent",RenderType.Transparent},
  438. {"TransparentCutout",RenderType.TransparentCutout},
  439. {"Background",RenderType.Background},
  440. {"Overlay",RenderType.Overlay},
  441. {"TreeOpaque",RenderType.TreeOpaque},
  442. {"TreeTransparentCutout",RenderType.TreeTransparentCutout},
  443. {"TreeBillboard",RenderType.TreeBillboard},
  444. {"Grass",RenderType.Grass},
  445. {"GrassBillboard",RenderType.GrassBillboard}
  446. };
  447. public static readonly Dictionary<string, RenderQueue> StringToRenderQueue = new Dictionary<string, RenderQueue>
  448. {
  449. {"Background",RenderQueue.Background },
  450. {"Geometry",RenderQueue.Geometry },
  451. {"AlphaTest",RenderQueue.AlphaTest },
  452. {"Transparent",RenderQueue.Transparent },
  453. {"Overlay",RenderQueue.Overlay }
  454. };
  455. public static readonly Dictionary<string, WirePortDataType> PropertyToWireType = new Dictionary<string, WirePortDataType>
  456. {
  457. {"Float",WirePortDataType.FLOAT},
  458. {"Range",WirePortDataType.FLOAT},
  459. {"Int",WirePortDataType.INT},
  460. {"Color",WirePortDataType.COLOR},
  461. {"Vector",WirePortDataType.FLOAT4},
  462. {"2D",WirePortDataType.SAMPLER2D},
  463. {"3D",WirePortDataType.SAMPLER3D},
  464. {"Cube",WirePortDataType.SAMPLERCUBE},
  465. {"2DArray",WirePortDataType.SAMPLER2DARRAY},
  466. };
  467. public static readonly Dictionary<WirePortDataType, int> DataTypeChannelUsage = new Dictionary<WirePortDataType, int>
  468. {
  469. {WirePortDataType.OBJECT,0 },
  470. {WirePortDataType.FLOAT,1 },
  471. {WirePortDataType.FLOAT2,2 },
  472. {WirePortDataType.FLOAT3,3 },
  473. {WirePortDataType.FLOAT4,4 },
  474. {WirePortDataType.FLOAT3x3,0 },
  475. {WirePortDataType.FLOAT4x4,0 },
  476. {WirePortDataType.COLOR,4 },
  477. {WirePortDataType.INT,1 },
  478. {WirePortDataType.UINT,1 },
  479. {WirePortDataType.SAMPLER1D,0 },
  480. {WirePortDataType.SAMPLER2D,0 },
  481. {WirePortDataType.SAMPLER3D,0 },
  482. {WirePortDataType.SAMPLERCUBE,0 },
  483. {WirePortDataType.SAMPLER2DARRAY,0 },
  484. {WirePortDataType.SAMPLERSTATE,0 }
  485. };
  486. public static readonly Dictionary<int, WirePortDataType> ChannelToDataType = new Dictionary<int, WirePortDataType>
  487. {
  488. {1,WirePortDataType.FLOAT},
  489. {2,WirePortDataType.FLOAT2},
  490. {3,WirePortDataType.FLOAT3},
  491. {4,WirePortDataType.FLOAT4}
  492. };
  493. public static readonly Dictionary<TemplateSemantics, string> SemanticsDefaultName = new Dictionary<TemplateSemantics, string>
  494. {
  495. {TemplateSemantics.COLOR ,"ase_color"},
  496. {TemplateSemantics.NORMAL ,"ase_normal"},
  497. {TemplateSemantics.POSITION ,"ase_position"},
  498. {TemplateSemantics.SV_POSITION ,"ase_sv_position"},
  499. {TemplateSemantics.TANGENT ,"ase_tangent"},
  500. {TemplateSemantics.VFACE ,"ase_vface"},
  501. {TemplateSemantics.SV_VertexID ,"ase_vertexId"},
  502. {TemplateSemantics.SV_PrimitiveID ,"ase_primitiveId"},
  503. {TemplateSemantics.INTERNALTESSPOS ,"ase_internalTessPos"},
  504. {TemplateSemantics.TEXCOORD0 ,"ase_tex_coord0"},
  505. {TemplateSemantics.TEXCOORD1 ,"ase_tex_coord1"},
  506. {TemplateSemantics.TEXCOORD2 ,"ase_tex_coord2"},
  507. {TemplateSemantics.TEXCOORD3 ,"ase_tex_coord3"},
  508. {TemplateSemantics.TEXCOORD4 ,"ase_tex_coord4"},
  509. {TemplateSemantics.TEXCOORD5 ,"ase_tex_coord5"},
  510. {TemplateSemantics.TEXCOORD6 ,"ase_tex_coord6"},
  511. {TemplateSemantics.TEXCOORD7 ,"ase_tex_coord7"},
  512. {TemplateSemantics.TEXCOORD8 ,"ase_tex_coord8"},
  513. {TemplateSemantics.TEXCOORD9 ,"ase_tex_coord9"},
  514. {TemplateSemantics.TEXCOORD10 ,"ase_tex_coord10"},
  515. {TemplateSemantics.TEXCOORD11 ,"ase_tex_coord11"},
  516. {TemplateSemantics.TEXCOORD12 ,"ase_tex_coord12"},
  517. {TemplateSemantics.TEXCOORD13 ,"ase_tex_coord13"},
  518. {TemplateSemantics.TEXCOORD14 ,"ase_tex_coord14"},
  519. {TemplateSemantics.TEXCOORD15 ,"ase_tex_coord15"},
  520. };
  521. public static readonly Dictionary<int, TemplateInfoOnSematics> IntToInfo = new Dictionary<int, TemplateInfoOnSematics>
  522. {
  523. {0,TemplateInfoOnSematics.TEXTURE_COORDINATES0 },
  524. {1,TemplateInfoOnSematics.TEXTURE_COORDINATES1 },
  525. {2,TemplateInfoOnSematics.TEXTURE_COORDINATES2 },
  526. {3,TemplateInfoOnSematics.TEXTURE_COORDINATES3 },
  527. {4,TemplateInfoOnSematics.TEXTURE_COORDINATES4 },
  528. {5,TemplateInfoOnSematics.TEXTURE_COORDINATES5 },
  529. {6,TemplateInfoOnSematics.TEXTURE_COORDINATES6 },
  530. {7,TemplateInfoOnSematics.TEXTURE_COORDINATES7 },
  531. };
  532. public static readonly Dictionary<string, TemplateInfoOnSematics> ShortcutToInfo = new Dictionary<string, TemplateInfoOnSematics>
  533. {
  534. {"p" ,TemplateInfoOnSematics.POSITION },
  535. {"sp" ,TemplateInfoOnSematics.CLIP_POS },
  536. {"spu" ,TemplateInfoOnSematics.SCREEN_POSITION },
  537. {"spn" ,TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED },
  538. {"c" ,TemplateInfoOnSematics.COLOR },
  539. {"uv0" ,TemplateInfoOnSematics.TEXTURE_COORDINATES0 },
  540. {"uv1" ,TemplateInfoOnSematics.TEXTURE_COORDINATES1 },
  541. {"uv2" ,TemplateInfoOnSematics.TEXTURE_COORDINATES2 },
  542. {"uv3" ,TemplateInfoOnSematics.TEXTURE_COORDINATES3 },
  543. {"uv4" ,TemplateInfoOnSematics.TEXTURE_COORDINATES4 },
  544. {"uv5" ,TemplateInfoOnSematics.TEXTURE_COORDINATES5 },
  545. {"uv6" ,TemplateInfoOnSematics.TEXTURE_COORDINATES6 },
  546. {"uv7" ,TemplateInfoOnSematics.TEXTURE_COORDINATES7 },
  547. {"n" ,TemplateInfoOnSematics.NORMAL },
  548. {"t" ,TemplateInfoOnSematics.TANGENT },
  549. {"wn" ,TemplateInfoOnSematics.WORLD_NORMAL},
  550. {"wt" ,TemplateInfoOnSematics.WORLD_TANGENT},
  551. {"wbt" ,TemplateInfoOnSematics.WORLD_BITANGENT},
  552. {"wvd" ,TemplateInfoOnSematics.WORLD_VIEW_DIR},
  553. {"wp" ,TemplateInfoOnSematics.WORLD_POSITION},
  554. {"rwp" ,TemplateInfoOnSematics.RELATIVE_WORLD_POS},
  555. {"vf" ,TemplateInfoOnSematics.VFACE},
  556. {"sc" ,TemplateInfoOnSematics.SHADOWCOORDS}
  557. };
  558. public static readonly Dictionary<TemplateInfoOnSematics, string> InfoToDefineFrag = new Dictionary<TemplateInfoOnSematics, string>
  559. {
  560. {TemplateInfoOnSematics.POSITION ,"ASE_NEEDS_FRAG_POSITION"},
  561. {TemplateInfoOnSematics.CLIP_POS ,"ASE_NEEDS_FRAG_CLIP_POS"},
  562. {TemplateInfoOnSematics.SCREEN_POSITION,"ASE_NEEDS_FRAG_SCREEN_POSITION" },
  563. {TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED,"ASE_NEEDS_FRAG_SCREEN_POSITION_NORMALIZED" },
  564. {TemplateInfoOnSematics.COLOR, "ASE_NEEDS_FRAG_COLOR"},
  565. {TemplateInfoOnSematics.TEXTURE_COORDINATES0,"ASE_NEEDS_FRAG_TEXTURE_COORDINATES0" },
  566. {TemplateInfoOnSematics.TEXTURE_COORDINATES1,"ASE_NEEDS_FRAG_TEXTURE_COORDINATES1" },
  567. {TemplateInfoOnSematics.TEXTURE_COORDINATES2,"ASE_NEEDS_FRAG_TEXTURE_COORDINATES2" },
  568. {TemplateInfoOnSematics.TEXTURE_COORDINATES3,"ASE_NEEDS_FRAG_TEXTURE_COORDINATES3" },
  569. {TemplateInfoOnSematics.TEXTURE_COORDINATES4,"ASE_NEEDS_FRAG_TEXTURE_COORDINATES4" },
  570. {TemplateInfoOnSematics.TEXTURE_COORDINATES5,"ASE_NEEDS_FRAG_TEXTURE_COORDINATES5" },
  571. {TemplateInfoOnSematics.TEXTURE_COORDINATES6,"ASE_NEEDS_FRAG_TEXTURE_COORDINATES6" },
  572. {TemplateInfoOnSematics.TEXTURE_COORDINATES7,"ASE_NEEDS_FRAG_TEXTURE_COORDINATES7" },
  573. {TemplateInfoOnSematics.NORMAL,"ASE_NEEDS_FRAG_NORMAL" },
  574. {TemplateInfoOnSematics.TANGENT ,"ASE_NEEDS_FRAG_TANGENT"},
  575. {TemplateInfoOnSematics.WORLD_NORMAL,"ASE_NEEDS_FRAG_WORLD_NORMAL"},
  576. {TemplateInfoOnSematics.WORLD_TANGENT,"ASE_NEEDS_FRAG_WORLD_TANGENT"},
  577. {TemplateInfoOnSematics.WORLD_BITANGENT,"ASE_NEEDS_FRAG_WORLD_BITANGENT"},
  578. {TemplateInfoOnSematics.WORLD_VIEW_DIR,"ASE_NEEDS_FRAG_WORLD_VIEW_DIR"},
  579. {TemplateInfoOnSematics.WORLD_POSITION,"ASE_NEEDS_FRAG_WORLD_POSITION"},
  580. {TemplateInfoOnSematics.RELATIVE_WORLD_POS,"ASE_NEEDS_FRAG_RELATIVE_WORLD_POS"},
  581. {TemplateInfoOnSematics.VFACE,"ASE_NEEDS_FRAG_VFACE"},
  582. {TemplateInfoOnSematics.SHADOWCOORDS,"ASE_NEEDS_FRAG_SHADOWCOORDS"}
  583. };
  584. public static readonly Dictionary<TemplateInfoOnSematics, string> InfoToDefineVertex = new Dictionary<TemplateInfoOnSematics, string>
  585. {
  586. {TemplateInfoOnSematics.POSITION ,"ASE_NEEDS_VERT_POSITION"},
  587. {TemplateInfoOnSematics.CLIP_POS ,"ASE_NEEDS_VERT_CLIP_POS"},
  588. {TemplateInfoOnSematics.SCREEN_POSITION,"ASE_NEEDS_VERT_SCREEN_POSITION" },
  589. {TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED,"ASE_NEEDS_VERT_SCREEN_POSITION_NORMALIZED" },
  590. {TemplateInfoOnSematics.COLOR, "ASE_NEEDS_VERT_COLOR"},
  591. {TemplateInfoOnSematics.TEXTURE_COORDINATES0,"ASE_NEEDS_VERT_TEXTURE_COORDINATES0" },
  592. {TemplateInfoOnSematics.TEXTURE_COORDINATES1,"ASE_NEEDS_VERT_TEXTURE_COORDINATES1" },
  593. {TemplateInfoOnSematics.TEXTURE_COORDINATES2,"ASE_NEEDS_VERT_TEXTURE_COORDINATES2" },
  594. {TemplateInfoOnSematics.TEXTURE_COORDINATES3,"ASE_NEEDS_VERT_TEXTURE_COORDINATES3" },
  595. {TemplateInfoOnSematics.TEXTURE_COORDINATES4,"ASE_NEEDS_VERT_TEXTURE_COORDINATES4" },
  596. {TemplateInfoOnSematics.TEXTURE_COORDINATES5,"ASE_NEEDS_VERT_TEXTURE_COORDINATES5" },
  597. {TemplateInfoOnSematics.TEXTURE_COORDINATES6,"ASE_NEEDS_VERT_TEXTURE_COORDINATES6" },
  598. {TemplateInfoOnSematics.TEXTURE_COORDINATES7,"ASE_NEEDS_VERT_TEXTURE_COORDINATES7" },
  599. {TemplateInfoOnSematics.NORMAL,"ASE_NEEDS_VERT_NORMAL" },
  600. {TemplateInfoOnSematics.TANGENT ,"ASE_NEEDS_VERT_TANGENT"},
  601. {TemplateInfoOnSematics.WORLD_NORMAL,"ASE_NEEDS_VERT_WORLD_NORMAL"},
  602. {TemplateInfoOnSematics.WORLD_TANGENT,"ASE_NEEDS_VERT_WORLD_TANGENT"},
  603. {TemplateInfoOnSematics.WORLD_BITANGENT,"ASE_NEEDS_VERT_WORLD_BITANGENT"},
  604. {TemplateInfoOnSematics.WORLD_VIEW_DIR,"ASE_NEEDS_VERT_WORLD_VIEW_DIR"},
  605. {TemplateInfoOnSematics.WORLD_POSITION,"ASE_NEEDS_VERT_WORLD_POSITION"},
  606. {TemplateInfoOnSematics.RELATIVE_WORLD_POS,"ASE_NEEDS_VERT_RELATIVE_WORLD_POS"},
  607. {TemplateInfoOnSematics.VFACE,"ASE_NEEDS_VERT_VFACE"},
  608. {TemplateInfoOnSematics.SHADOWCOORDS,"ASE_NEEDS_VERT_SHADOWCOORDS"}
  609. };
  610. public static readonly Dictionary<TemplateInfoOnSematics, string> InfoToLocalVar = new Dictionary<TemplateInfoOnSematics, string>
  611. {
  612. {TemplateInfoOnSematics.POSITION,GeneratorUtils.VertexPosition4Str },
  613. {TemplateInfoOnSematics.CLIP_POS,GeneratorUtils.ClipPositionStr },
  614. {TemplateInfoOnSematics.SCREEN_POSITION,GeneratorUtils.ScreenPositionStr },
  615. {TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED,GeneratorUtils.ScreenPositionNormalizedStr },
  616. {TemplateInfoOnSematics.COLOR, "ase_color" },
  617. {TemplateInfoOnSematics.TEXTURE_COORDINATES0, "ase_uv0" },
  618. {TemplateInfoOnSematics.TEXTURE_COORDINATES1, "ase_uv1" },
  619. {TemplateInfoOnSematics.TEXTURE_COORDINATES2, "ase_uv2" },
  620. {TemplateInfoOnSematics.TEXTURE_COORDINATES3, "ase_uv3" },
  621. {TemplateInfoOnSematics.NORMAL, GeneratorUtils.VertexNormalStr },
  622. {TemplateInfoOnSematics.TANGENT, GeneratorUtils.VertexTangentStr },
  623. {TemplateInfoOnSematics.WORLD_NORMAL, GeneratorUtils.WorldNormalStr},
  624. {TemplateInfoOnSematics.WORLD_TANGENT, GeneratorUtils.WorldTangentStr},
  625. {TemplateInfoOnSematics.WORLD_BITANGENT, GeneratorUtils.WorldBitangentStr},
  626. {TemplateInfoOnSematics.WORLD_VIEW_DIR, GeneratorUtils.WorldViewDirectionStr},
  627. {TemplateInfoOnSematics.WORLD_POSITION, GeneratorUtils.WorldPositionStr},
  628. {TemplateInfoOnSematics.RELATIVE_WORLD_POS, GeneratorUtils.RelativeWorldPositionStr},
  629. {TemplateInfoOnSematics.VFACE, GeneratorUtils.VFaceStr},
  630. {TemplateInfoOnSematics.SHADOWCOORDS, GeneratorUtils.ShadowCoordsStr}
  631. };
  632. public static readonly Dictionary<TemplateInfoOnSematics, WirePortDataType> InfoToWirePortType = new Dictionary<TemplateInfoOnSematics, WirePortDataType>
  633. {
  634. {TemplateInfoOnSematics.POSITION,WirePortDataType.FLOAT4 },
  635. {TemplateInfoOnSematics.CLIP_POS,WirePortDataType.FLOAT4 },
  636. {TemplateInfoOnSematics.SCREEN_POSITION,WirePortDataType.FLOAT4 },
  637. {TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED,WirePortDataType.FLOAT4 },
  638. {TemplateInfoOnSematics.COLOR, WirePortDataType.COLOR },
  639. {TemplateInfoOnSematics.TEXTURE_COORDINATES0, WirePortDataType.FLOAT4 },
  640. {TemplateInfoOnSematics.TEXTURE_COORDINATES1, WirePortDataType.FLOAT4 },
  641. {TemplateInfoOnSematics.TEXTURE_COORDINATES2, WirePortDataType.FLOAT4 },
  642. {TemplateInfoOnSematics.TEXTURE_COORDINATES3, WirePortDataType.FLOAT4 },
  643. {TemplateInfoOnSematics.NORMAL, WirePortDataType.FLOAT3 },
  644. {TemplateInfoOnSematics.TANGENT, WirePortDataType.FLOAT4 },
  645. {TemplateInfoOnSematics.WORLD_NORMAL, WirePortDataType.FLOAT3},
  646. {TemplateInfoOnSematics.WORLD_TANGENT, WirePortDataType.FLOAT3},
  647. {TemplateInfoOnSematics.WORLD_BITANGENT, WirePortDataType.FLOAT3},
  648. {TemplateInfoOnSematics.WORLD_VIEW_DIR, WirePortDataType.FLOAT3},
  649. {TemplateInfoOnSematics.WORLD_POSITION, WirePortDataType.FLOAT3},
  650. {TemplateInfoOnSematics.RELATIVE_WORLD_POS, WirePortDataType.FLOAT3},
  651. {TemplateInfoOnSematics.VFACE, WirePortDataType.FLOAT},
  652. {TemplateInfoOnSematics.SHADOWCOORDS, WirePortDataType.FLOAT4},
  653. };
  654. public static readonly Dictionary<int, TemplateInfoOnSematics> IntToUVChannelInfo = new Dictionary<int, TemplateInfoOnSematics>
  655. {
  656. {0,TemplateInfoOnSematics.TEXTURE_COORDINATES0 },
  657. {1,TemplateInfoOnSematics.TEXTURE_COORDINATES1 },
  658. {2,TemplateInfoOnSematics.TEXTURE_COORDINATES2 },
  659. {3,TemplateInfoOnSematics.TEXTURE_COORDINATES3 },
  660. {4,TemplateInfoOnSematics.TEXTURE_COORDINATES4 },
  661. {5,TemplateInfoOnSematics.TEXTURE_COORDINATES5 },
  662. {6,TemplateInfoOnSematics.TEXTURE_COORDINATES6 },
  663. {7,TemplateInfoOnSematics.TEXTURE_COORDINATES7 }
  664. };
  665. public static readonly Dictionary<int, TemplateSemantics> IntToSemantic = new Dictionary<int, TemplateSemantics>
  666. {
  667. { 0,TemplateSemantics.TEXCOORD0 },
  668. { 1,TemplateSemantics.TEXCOORD1 },
  669. { 2,TemplateSemantics.TEXCOORD2 },
  670. { 3,TemplateSemantics.TEXCOORD3 },
  671. { 4,TemplateSemantics.TEXCOORD4 },
  672. { 5,TemplateSemantics.TEXCOORD5 },
  673. { 6,TemplateSemantics.TEXCOORD6 },
  674. { 7,TemplateSemantics.TEXCOORD7 },
  675. { 8,TemplateSemantics.TEXCOORD8 },
  676. { 9,TemplateSemantics.TEXCOORD9 },
  677. { 10,TemplateSemantics.TEXCOORD10 },
  678. { 11,TemplateSemantics.TEXCOORD11 },
  679. { 12,TemplateSemantics.TEXCOORD12 },
  680. { 13,TemplateSemantics.TEXCOORD13 },
  681. { 14,TemplateSemantics.TEXCOORD14 },
  682. { 15,TemplateSemantics.TEXCOORD15 }
  683. };
  684. public static readonly Dictionary<TemplateSemantics, int> SemanticToInt = new Dictionary<TemplateSemantics, int>
  685. {
  686. { TemplateSemantics.TEXCOORD0,0 },
  687. { TemplateSemantics.TEXCOORD1,1 },
  688. { TemplateSemantics.TEXCOORD2,2 },
  689. { TemplateSemantics.TEXCOORD3,3 },
  690. { TemplateSemantics.TEXCOORD4,4 },
  691. { TemplateSemantics.TEXCOORD5,5 },
  692. { TemplateSemantics.TEXCOORD6,6 },
  693. { TemplateSemantics.TEXCOORD7,7 },
  694. { TemplateSemantics.TEXCOORD8,8 },
  695. { TemplateSemantics.TEXCOORD9,9 },
  696. { TemplateSemantics.TEXCOORD10,10 },
  697. { TemplateSemantics.TEXCOORD11,11 },
  698. { TemplateSemantics.TEXCOORD12,12 },
  699. { TemplateSemantics.TEXCOORD13,13 },
  700. { TemplateSemantics.TEXCOORD14,14 },
  701. { TemplateSemantics.TEXCOORD15,15 },
  702. };
  703. public static readonly Dictionary<string, TemplateSemantics> ShortcutToSemantic = new Dictionary<string, TemplateSemantics>
  704. {
  705. { "p" ,TemplateSemantics.POSITION },
  706. { "sp" ,TemplateSemantics.SV_POSITION },
  707. { "c" ,TemplateSemantics.COLOR },
  708. { "n" ,TemplateSemantics.NORMAL },
  709. { "t" ,TemplateSemantics.TANGENT },
  710. { "tc0" ,TemplateSemantics.TEXCOORD0 },
  711. { "tc1" ,TemplateSemantics.TEXCOORD1 },
  712. { "tc2" ,TemplateSemantics.TEXCOORD2 },
  713. { "tc3" ,TemplateSemantics.TEXCOORD3 },
  714. { "tc4" ,TemplateSemantics.TEXCOORD4 },
  715. { "tc5" ,TemplateSemantics.TEXCOORD5 },
  716. { "tc6" ,TemplateSemantics.TEXCOORD6 },
  717. { "tc7" ,TemplateSemantics.TEXCOORD7 },
  718. { "tc8" ,TemplateSemantics.TEXCOORD8 },
  719. { "tc9" ,TemplateSemantics.TEXCOORD9 },
  720. { "tc10" ,TemplateSemantics.TEXCOORD10 },
  721. { "tc11" ,TemplateSemantics.TEXCOORD11 },
  722. { "tc12" ,TemplateSemantics.TEXCOORD12 },
  723. { "tc13" ,TemplateSemantics.TEXCOORD13 },
  724. { "tc14" ,TemplateSemantics.TEXCOORD14 },
  725. { "tc15" ,TemplateSemantics.TEXCOORD15 }
  726. };
  727. public static readonly Dictionary<string, WirePortDataType> CgToWirePortType = new Dictionary<string, WirePortDataType>()
  728. {
  729. {"float" ,WirePortDataType.FLOAT},
  730. {"float2" ,WirePortDataType.FLOAT2},
  731. {"float3" ,WirePortDataType.FLOAT3},
  732. {"float4" ,WirePortDataType.FLOAT4},
  733. {"float3x3" ,WirePortDataType.FLOAT3x3},
  734. {"float4x4" ,WirePortDataType.FLOAT4x4},
  735. {"half" ,WirePortDataType.FLOAT},
  736. {"half2" ,WirePortDataType.FLOAT2},
  737. {"half3" ,WirePortDataType.FLOAT3},
  738. {"half4" ,WirePortDataType.FLOAT4},
  739. {"half3x3" ,WirePortDataType.FLOAT3x3},
  740. {"half4x4" ,WirePortDataType.FLOAT4x4},
  741. {"fixed" ,WirePortDataType.FLOAT},
  742. {"fixed2" ,WirePortDataType.FLOAT2},
  743. {"fixed3" ,WirePortDataType.FLOAT3},
  744. {"fixed4" ,WirePortDataType.FLOAT4},
  745. {"fixed3x3" ,WirePortDataType.FLOAT3x3},
  746. {"fixed4x4" ,WirePortDataType.FLOAT4x4},
  747. {"int" ,WirePortDataType.INT},
  748. {"uint" ,WirePortDataType.INT},
  749. {"sampler1D" ,WirePortDataType.SAMPLER1D},
  750. {"sampler2D" ,WirePortDataType.SAMPLER2D},
  751. {"sampler2D_float" ,WirePortDataType.SAMPLER2D},
  752. {"sampler3D" ,WirePortDataType.SAMPLER3D},
  753. {"samplerCUBE" ,WirePortDataType.SAMPLERCUBE},
  754. {"sampler2DArray" ,WirePortDataType.SAMPLER2DARRAY},
  755. {"SamplerState" ,WirePortDataType.SAMPLERSTATE}
  756. };
  757. public static readonly Dictionary<string, int> AvailableInterpolators = new Dictionary<string, int>()
  758. {
  759. {"2.0",8 },
  760. {"2.5",8 },
  761. {"3.0",10},
  762. {"3.5",10},
  763. {"4.0",16},
  764. {"4.5",16},
  765. {"4.6",16},
  766. {"5.0",16}
  767. };
  768. public static readonly string[] AvailableShaderModels =
  769. { "2.0", "2.5", "3.0", "3.5", "4.0", "4.5", "4.6", "5.0" };
  770. public static readonly Dictionary<string, int> ShaderModelToArrayIdx = new Dictionary<string, int>()
  771. {
  772. {"2.0",0},
  773. {"2.5",1},
  774. {"3.0",2},
  775. {"3.5",3},
  776. {"4.0",4},
  777. {"4.5",5},
  778. {"4.6",6},
  779. {"5.0",7}
  780. };
  781. public static readonly string HDPBRTag = "UNITY_MATERIAL_LIT";
  782. public static readonly Dictionary<string, TemplateSRPType> TagToRenderPipeline = new Dictionary<string, TemplateSRPType>()
  783. {
  784. { "UniversalPipeline",TemplateSRPType.Lightweight },
  785. { "LightweightPipeline",TemplateSRPType.Lightweight },
  786. { "HDRenderPipeline",TemplateSRPType.HD }
  787. };
  788. #if UNITY_2018_3_OR_NEWER
  789. public static string CoreColorLib = "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl";
  790. public static string CoreCommonLib = "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl";
  791. #else
  792. public static string CoreCommonLib = "CoreRP/ShaderLibrary/Common.hlsl";
  793. public static string CoreColorLib = "CoreRP/ShaderLibrary/Color.hlsl";
  794. #endif
  795. public static string FetchSubShaderBody = @"(SubShader.*)\/\*ase_lod\*\/";
  796. public static string TemplateCustomUI = @"\/\*CustomNodeUI:(\w*)\*\/";
  797. public static string HidePassPattern = @"\/\*ase_hide_pass[:]*([a-zA-Z:]*)\*\/";
  798. public static string ASEPassPattern = @"\/\*ase_pass[:]*([a-zA-Z:]*)\*\/";
  799. public static string BlendWholeWordPattern = @"\bBlend\b";
  800. public static string BlendOpWholeWordPattern = @"\bBlendOp\b";
  801. public static string AlphaToMaskPattern = @"\bAlphaToMask\s+(\[*\w+\]*)";
  802. public static string CullWholeWordPattern = @"\bCull\b";
  803. public static string ColorMaskWholeWordPattern = @"\bColorMask\b";
  804. public static string StencilWholeWordPattern = @"\bStencil\b";
  805. public static string ZWriteWholeWordPattern = @"\bZWrite\b";
  806. public static string ZTestWholeWordPattern = @"\bZTest\b";
  807. public static string ZOffsetWholeWordPattern = @"\bOffset\b";
  808. public static string TagsWholeWordPattern = @"\bTags\b";
  809. public static string CustomInspectorPattern = "^\\s*CustomEditor\\s+\\\"([\\w\\.]*)\\\"";
  810. public static string FallbackPattern = "^\\s*Fallback\\s+\\\"([\\w\\/\\\\]*)\\\"";
  811. public static string DefinesPattern = @"^\s*#define\s+([\w .]*)";
  812. public static string PragmasPattern = @"^\s*#pragma\s+([\w .]*)";
  813. public static string IncludesPattern = "^\\s*#include\\s+\"([\\w.\\/]*)\"";
  814. public static string GlobalDirectivesPattern = "[#]+(define|pragma|include)\\s+([\\w .\\/\\\"]*)";
  815. public static string BeforePragmaPattern = @"(?:CGPROGRAM|HLSLPROGRAM|GLSLPROGRAM).*?\n(\s*)(.)";
  816. public static string GlobalTOPDirectivesPattern = @"(CGPROGRAM|CGINCLUDE|HLSLPROGRAM|HLSLINCLUDE).*?\n\s*(.)";
  817. public static string VertexPragmaPattern = @"#pragma vertex\s+(\w+)";
  818. public static string FragmentPragmaPattern = @"#pragma fragment\s+(\w+)";
  819. public static string FunctionBodyStartPattern = @"\s+{0}\s*\(";
  820. public static string ShaderModelPattern = @"#pragma\s+target\s+([0-9]*[.]*[0-9]*)";
  821. public static readonly string LocalVarPattern = @"\/\*ase_local_var[:]*(\w*)\*\/\s*(\w*)\s+(\w*)";
  822. public static readonly string InlinePattern = @"\/\*ase_inline_begin\*\/(.*?)\/\*ase_inline_end\*\/";
  823. public static readonly string SubShaderLODPattern = @"\sLOD\s+(\d+)";
  824. public static readonly string PassNamePattern = "Name\\s+\\\"([\\w\\+\\-\\*\\/\\(\\) ]*)\\\"";
  825. public static readonly string TagsPattern = "\"(\\w+)\"\\s*=\\s*\"(\\w+\\+*\\w*)\"";
  826. public static readonly string ZTestPattern = @"^\s*ZTest\s+(\[*\w+\]*)";
  827. public static readonly string ZWritePattern = @"^\s*ZWrite\s+(\[*\w+\]*)";
  828. //public static readonly string ZOffsetPattern = @"\s*Offset\s+([-+]?[0-9]*\.?[0-9]+)\s*,\s*([-+]?[0-9]*\.?[0-9]+)";
  829. public static readonly string ZOffsetPattern = @"^\s*Offset\s+([-+]?[0-9]*\.?[0-9]+|\[*\w+\]*)\s*,\s*([-+]?[0-9]*\.?[0-9]+|\[*\w+\]*)\s*";
  830. public static readonly string VertexDataPattern = @"([a-z0-9D_]+|samplerCUBE|sampler2DArray)\s+(\w+)\s*:\s*([A-Z0-9_]+);";
  831. public static readonly string InterpRangePattern = @"ase_interp\((\d\.{0,1}\w{0,4}),(\d*)\)";
  832. //public static readonly string PropertiesPatternB = "(\\w*)\\s*\\(\\s*\"([\\w ]*)\"\\s*\\,\\s*(\\w*)\\s*.*\\)";
  833. //public static readonly string PropertiesPatternC = "^\\s*(\\w*)\\s*\\(\\s*\"([\\w\\(\\)\\+\\-\\\\* ]*)\"\\s*\\,\\s*(\\w*)\\s*.*\\)";
  834. //public static readonly string PropertiesPatternD = "(\\/\\/\\s*)*(\\w*)\\s*\\(\\s*\"([\\w\\(\\)\\+\\-\\\\* ]*)\"\\s*\\,\\s*(\\w*)\\s*.*\\)";
  835. //public static readonly string PropertiesPatternE = "(\\/\\/\\s*)*(\\w*)\\s*\\(\\s*\"([\\w\\(\\)\\+\\-\\\\* ]*)\"\\s*\\,\\s*(\\w*)\\s*.*\\)\\s*=\\s*[\\w,()\" {}]*";
  836. //public static readonly string PropertiesPatternF = "^(\\/\\/)*\\s*(\\[[\\[\\]\\w\\s\\(\\)\\_\\,]*\\])*\\s*(\\w*)\\s*\\(\\s*\"([\\w\\(\\)\\+\\-\\\\* ]*)\"\\s*\\,\\s*(\\w*)\\s*.*\\)\\s*=\\s*[\\w,()\" {}]*";
  837. //public static readonly string PropertiesPatternG = "^(\\s*)(\\[[\\[\\]\\w\\s\\(\\)\\_\\,]*\\])*\\s*(\\w*)\\s*\\(\\s*\"([\\w\\(\\)\\+\\-\\\\* ]*)\"\\s*\\,\\s*(\\w*)\\s*.*\\)\\s*=\\s*[\\w,()\" {}]*";
  838. public static readonly string PropertiesPatternG = "^(\\s*)(\\[[\\[\\]\\w\\s\\(\\)_,\\.]*\\])*[\\s\\/]*(\\w*)\\s*\\(\\s*\"([\\w\\(\\)\\+\\-\\\\* ]*)\"\\s*\\,\\s*(\\w*)\\s*.*\\)\\s*=\\s*[\\w,()\" {}\\.]*";
  839. public static readonly string CullModePattern = @"^\s*Cull\s+(\[*\w+\]*)";
  840. public static readonly string ColorMaskPatternFirst = @"\bColorMask\s+([\d\w\[\]]+)(\s+0)*";
  841. public static readonly string ColorMaskPattern = @"\bColorMask\s+([\d\w\[\]]+)(\s+0)";
  842. public static readonly string ColorMask1Pattern = @"\bColorMask\s+([\d\w\[\]]+)(\s+1)";
  843. public static readonly string ColorMask2Pattern = @"\bColorMask\s+([\d\w\[\]]+)(\s+2)";
  844. public static readonly string ColorMask3Pattern = @"\bColorMask\s+([\d\w\[\]]+)(\s+3)";
  845. //public static readonly string BlendModePattern = @"\s*Blend\s+(\w+)\s+(\w+)(?:[\s,]+(\w+)\s+(\w+)|)";
  846. //public static readonly string BlendModePattern = @"\s*Blend\s+(\[*\w+\]*)\s+(\[*\w+\]*)(?:[\s,]+(\[*\w+\]*)\s+(\[*\w+\]*)|)";
  847. //public static readonly string BlendModePattern = @"^\s*Blend\s+(?:(?=\d)|(\[*\w+\]*)\s+(\[*\w+\]*)(?:[\s,]+(\[*\w+\]*)\s+(\[*\w+\]*)|))";
  848. public static readonly string BlendModePatternFirst = @"\bBlend([ \t]+0)*[ \t]+(?:Off|(\[*\w+\]*)[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|(?:[ \t,]+(\[*\w+\]*)[ \t]+(\[*\w+\]*)|)))";
  849. public static readonly string BlendModePattern = @"\bBlend([ \t]+0)[ \t]+(?:Off|(\[*\w+\]*)[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|(?:[ \t,]+(\[*\w+\]*)[ \t]+(\[*\w+\]*)|)))";
  850. public static readonly string BlendModePattern1 = @"\bBlend([ \t]+1)[ \t]+(?:Off|(\[*\w+\]*)[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|(?:[ \t,]+(\[*\w+\]*)[ \t]+(\[*\w+\]*)|)))";
  851. public static readonly string BlendModePattern2 = @"\bBlend([ \t]+2)[ \t]+(?:Off|(\[*\w+\]*)[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|(?:[ \t,]+(\[*\w+\]*)[ \t]+(\[*\w+\]*)|)))";
  852. public static readonly string BlendModePattern3 = @"\bBlend([ \t]+3)[ \t]+(?:Off|(\[*\w+\]*)[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|(?:[ \t,]+(\[*\w+\]*)[ \t]+(\[*\w+\]*)|)))";
  853. //public static readonly string BlendOpPattern = @"\s*BlendOp\s+(\w+)[\s,]*(?:(\w+)|)";
  854. //public static readonly string BlendOpPattern = @"\s*BlendOp\s+(\[*\w+\]*)[\s,]*(?:(\[*\w+\]*)|)";
  855. //public static readonly string BlendOpPattern = @"^\s*BlendOp\s+(?:(?=\d)|(\[*\w+\]*)[\s,]*(?:(\[*\w+\]*)|))";
  856. public static readonly string BlendOpPatternFirst = @"\bBlendOp([ \t]+0)*[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|[ \t,]*(?:(\[*\w+\]*)|))";
  857. public static readonly string BlendOpPattern = @"\bBlendOp([ \t]+0)[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|[ \t,]*(?:(\[*\w+\]*)|))";
  858. public static readonly string BlendOpPattern1 = @"\bBlendOp([ \t]+1)[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|[ \t,]*(?:(\[*\w+\]*)|))";
  859. public static readonly string BlendOpPattern2 = @"\bBlendOp([ \t]+2)[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|[ \t,]*(?:(\[*\w+\]*)|))";
  860. public static readonly string BlendOpPattern3 = @"\bBlendOp([ \t]+3)[ \t]+(\[*\w+\]*)(?:(?=[ \t]+Blend)|[ \t,]*(?:(\[*\w+\]*)|))";
  861. public static readonly string StencilOpGlobalPattern = @"Stencil\s*{([\w\W\s]*)}";
  862. public static readonly string StencilOpLinePattern = @"(\w+)\s*(\[*\w+\]*)";
  863. public static readonly string ShaderGlobalsOverallPattern = @"(?:\/\*ase_pragma\*\/|[\}\#])[\#\&\|\!\<\>\(\)\=\w\s\;\/\*\.\\""]*\/\*ase_globals\*\/";
  864. public static readonly string ShaderGlobalsMultilinePattern = @"^\s*(?:uniform\s*)*(\w*)\s*(\w*);$";
  865. public static readonly string TexSemantic = "float4 {0} : TEXCOORD{1};";
  866. public static readonly string TexFullSemantic = "float4 {0} : {1};";
  867. public static readonly string InterpFullSemantic = "{0} {1} : {2};";
  868. public static readonly string BaseInterpolatorName = "ase_texcoord";
  869. public static readonly string TexUVFullSemantic = "float4 ase_texcoord{0} : TEXCOORD{0};";
  870. public static readonly string InterpMacro = "{0}({1})";
  871. public static readonly string InterpolatorDecl = Constants.VertexShaderOutputStr + ".{0} = " + Constants.VertexShaderInputStr + ".{0};";
  872. public static readonly string TemplateVariableDecl = "{0} = {1};";
  873. public static readonly string TemplateVarFormat = "{0}.{1}";
  874. //public static readonly string StructsRemoval = @"struct\s+\w+\s+{[\s\w;\/\*]+};";
  875. public static readonly string StructsRemoval = @"struct\s+\w+\s+{[\#\&\|\!\<\>\s\w\(\).;:=,\/\*]+};";
  876. public static readonly string SRPBatcherFindTag = @"CBUFFER_START\s*\(\s*UnityPerMaterial\s*\)\s*\n(\s*)";
  877. public static string ReplaceAt( this string body, string oldStr, string newStr, int startIndex )
  878. {
  879. return body.Remove( startIndex, oldStr.Length ).Insert( startIndex, newStr );
  880. }
  881. public static bool GetPassUniqueId( TemplateTagData tagData, TemplatePropertyContainer propertyContainer, TemplateIdManager idManager, string uniquePrefix, int offsetIdx, string subBody, ref string passUniqueID )
  882. {
  883. Match match = Regex.Match( subBody, ASEPassPattern );
  884. if( match.Success && match.Groups.Count > 1 && match.Groups[1].Length > 0 )
  885. {
  886. passUniqueID = match.Groups[ 1 ].Value;
  887. tagData.StartIdx = offsetIdx + match.Index;
  888. tagData.Id = match.Value;
  889. idManager.RegisterId( tagData.StartIdx, uniquePrefix + tagData.Id, tagData.Id );
  890. propertyContainer.AddId( subBody, tagData.Id, tagData.SearchIndentation );
  891. return true;
  892. }
  893. return false;
  894. }
  895. public static CustomTemplatePropertyUIEnum FetchCustomUI( string data )
  896. {
  897. Match match = Regex.Match( data, TemplateCustomUI );
  898. if( match.Success && CustomTemplatePropertyUI.ContainsKey( match.Groups[ 1 ].Value ) )
  899. {
  900. return CustomTemplatePropertyUI[ match.Groups[ 1 ].Value ];
  901. }
  902. return CustomTemplatePropertyUIEnum.None;
  903. }
  904. public static bool FetchInvisibleInfo( string input, ref int optionsArr, ref string id, ref int idIndex )
  905. {
  906. Match match = Regex.Match( input, HidePassPattern );
  907. if( match.Success )
  908. {
  909. id = match.Value;
  910. idIndex = match.Index;
  911. if( match.Groups.Count > 1 )
  912. {
  913. string[] properties = match.Groups[ 1 ].Value.Split( ':' );
  914. for( int i = 0; i < properties.Length; i++ )
  915. {
  916. if( InvisibleOptions.ContainsKey( properties[ i ] ) )
  917. {
  918. optionsArr |= (int)InvisibleOptions[ properties[ i ] ];
  919. }
  920. }
  921. }
  922. }
  923. return match.Success;
  924. }
  925. static public string GenerateTextureSemantic( ref MasterNodeDataCollector dataCollector, int uv )
  926. {
  927. string texCoordName = BaseInterpolatorName;
  928. if( uv > 0 )
  929. {
  930. texCoordName += uv.ToString();
  931. }
  932. string texCoordData = string.Format( TexSemantic, texCoordName, uv );
  933. dataCollector.AddToVertexInput( texCoordData );
  934. dataCollector.AddToInterpolators( texCoordData );
  935. dataCollector.AddToVertexInterpolatorsDecl( string.Format( InterpolatorDecl, texCoordName ) );
  936. return texCoordName;
  937. }
  938. public static void CreatePragmaIncludeList( string data, TemplateIncludePragmaContainter includePragmaContainer )
  939. {
  940. // this finds the topmost position for including directives
  941. int topIndex = -1;
  942. foreach( Match match in Regex.Matches( data, GlobalTOPDirectivesPattern, RegexOptions.Singleline ) )
  943. {
  944. if( match.Groups.Count == 3 )
  945. {
  946. topIndex = match.Groups[ 2 ].Index;
  947. }
  948. }
  949. foreach( Match match in Regex.Matches( data, GlobalDirectivesPattern, RegexOptions.Multiline ) )
  950. {
  951. if( match.Success )
  952. {
  953. includePragmaContainer.AddNativeDirective( match.Groups[ 0 ].Value, topIndex );
  954. }
  955. }
  956. foreach( Match match in Regex.Matches( data, PragmasPattern, RegexOptions.Multiline ) )
  957. {
  958. if( match.Groups.Count == 2 )
  959. {
  960. includePragmaContainer.AddPragma( match.Groups[ 1 ].Value );
  961. }
  962. }
  963. foreach( Match match in Regex.Matches( data, DefinesPattern, RegexOptions.Multiline ) )
  964. {
  965. if( match.Groups.Count == 2 )
  966. {
  967. includePragmaContainer.AddDefine( match.Groups[ 1 ].Value );
  968. }
  969. }
  970. foreach( Match match in Regex.Matches( data, IncludesPattern, RegexOptions.Multiline ) )
  971. {
  972. if( match.Groups.Count == 2 )
  973. {
  974. includePragmaContainer.AddInclude( match.Groups[ 1 ].Value );
  975. }
  976. }
  977. }
  978. public static void CreateShaderPropertiesList( string propertyData, ref List<TemplateShaderPropertyData> propertiesList, ref Dictionary<string, TemplateShaderPropertyData> duplicatesHelper )
  979. {
  980. int identationIdx = (int)TemplateShaderPropertiesIdx.Identation;
  981. int nameIdx = (int)TemplateShaderPropertiesIdx.Name;
  982. int typeIdx = (int)TemplateShaderPropertiesIdx.Type;
  983. int inspectorNameIdx = (int)TemplateShaderPropertiesIdx.InspectorName;
  984. foreach( Match match in Regex.Matches( propertyData, PropertiesPatternG,RegexOptions.Multiline ) )
  985. {
  986. if( match.Groups.Count > 1 )
  987. {
  988. if( !duplicatesHelper.ContainsKey( match.Groups[ nameIdx ].Value ) && PropertyToWireType.ContainsKey( match.Groups[ typeIdx ].Value ) )
  989. {
  990. TemplateShaderPropertyData newData = new TemplateShaderPropertyData( match.Index,
  991. match.Value,
  992. match.Groups[ identationIdx ].Value,
  993. match.Groups[ inspectorNameIdx ].Value,
  994. match.Groups[ nameIdx ].Value,
  995. PropertyToWireType[ match.Groups[ typeIdx ].Value ],
  996. PropertyType.Property );
  997. propertiesList.Add( newData );
  998. duplicatesHelper.Add( newData.PropertyName, newData );
  999. }
  1000. }
  1001. }
  1002. }
  1003. public const string DepthMacroDeclRegex = @"UNITY_DECLARE_DEPTH_TEXTURE\(\s*_CameraDepthTexture";
  1004. public static void CheckUnityBuiltinGlobalMacros( string propertyData, ref List<TemplateShaderPropertyData> propertiesList, ref Dictionary<string, TemplateShaderPropertyData> duplicatesHelper )
  1005. {
  1006. Match match = Regex.Match( propertyData, DepthMacroDeclRegex );
  1007. if( match.Success )
  1008. {
  1009. TemplateShaderPropertyData newData = new TemplateShaderPropertyData( -1,
  1010. string.Empty,
  1011. string.Empty,
  1012. string.Empty,
  1013. Constants.CameraDepthTextureValue,
  1014. WirePortDataType.SAMPLER2D,
  1015. PropertyType.Global,
  1016. true );
  1017. duplicatesHelper.Add( newData.PropertyName, newData );
  1018. propertiesList.Add( newData );
  1019. }
  1020. }
  1021. public static void CreateShaderGlobalsList( string propertyData, ref List<TemplateShaderPropertyData> propertiesList, ref Dictionary<string, TemplateShaderPropertyData> duplicatesHelper )
  1022. {
  1023. int typeIdx = (int)TemplateShaderGlobalsIdx.Type;
  1024. int nameIdx = (int)TemplateShaderGlobalsIdx.Name;
  1025. // removes structs
  1026. propertyData = Regex.Replace( propertyData, StructsRemoval, "" );
  1027. MatchCollection matchCollection = Regex.Matches( propertyData, ShaderGlobalsOverallPattern );
  1028. string value = ( matchCollection.Count > 0 ) ? matchCollection[ 0 ].Groups[ 0 ].Value : propertyData;
  1029. foreach( Match lineMatch in Regex.Matches( value, ShaderGlobalsMultilinePattern, RegexOptions.Multiline ) )
  1030. {
  1031. if( lineMatch.Groups.Count > 1 )
  1032. {
  1033. if( !duplicatesHelper.ContainsKey( lineMatch.Groups[ nameIdx ].Value ) && CgToWirePortType.ContainsKey( lineMatch.Groups[ typeIdx ].Value ) )
  1034. {
  1035. TemplateShaderPropertyData newData = new TemplateShaderPropertyData( -1,
  1036. string.Empty,
  1037. string.Empty,
  1038. string.Empty,
  1039. lineMatch.Groups[ nameIdx ].Value,
  1040. CgToWirePortType[ lineMatch.Groups[ typeIdx ].Value ],
  1041. PropertyType.Global );
  1042. duplicatesHelper.Add( newData.PropertyName, newData );
  1043. propertiesList.Add( newData );
  1044. }
  1045. }
  1046. }
  1047. }
  1048. public static void CreateStencilOps( string stencilData, ref TemplateStencilData stencilDataObj )
  1049. {
  1050. stencilDataObj.DataCheck = TemplateDataCheck.Invalid;
  1051. MatchCollection overallGlobalMatch = Regex.Matches( stencilData, StencilOpGlobalPattern );
  1052. if( overallGlobalMatch.Count == 1 && overallGlobalMatch[ 0 ].Groups.Count == 2 )
  1053. {
  1054. string property = string.Empty;
  1055. string value = overallGlobalMatch[ 0 ].Groups[ 1 ].Value;
  1056. foreach( Match match in Regex.Matches( value, StencilOpLinePattern ) )
  1057. {
  1058. stencilDataObj.DataCheck = TemplateDataCheck.Valid;
  1059. if( match.Groups.Count == 3 )
  1060. {
  1061. switch( match.Groups[ 1 ].Value )
  1062. {
  1063. default:
  1064. {
  1065. stencilDataObj.DataCheck = TemplateDataCheck.Invalid;
  1066. return;
  1067. }
  1068. case "Ref":
  1069. {
  1070. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1071. {
  1072. stencilDataObj.ReferenceInline = property;
  1073. }
  1074. else
  1075. {
  1076. try
  1077. {
  1078. stencilDataObj.Reference = Convert.ToInt32( match.Groups[ 2 ].Value );
  1079. }
  1080. catch( Exception e )
  1081. {
  1082. Debug.LogException( e );
  1083. stencilDataObj.DataCheck = TemplateDataCheck.Invalid;
  1084. return;
  1085. }
  1086. }
  1087. }
  1088. break;
  1089. case "ReadMask":
  1090. {
  1091. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1092. {
  1093. stencilDataObj.ReadMaskInline = property;
  1094. }
  1095. else
  1096. {
  1097. try
  1098. {
  1099. stencilDataObj.ReadMask = Convert.ToInt32( match.Groups[ 2 ].Value );
  1100. }
  1101. catch( Exception e )
  1102. {
  1103. Debug.LogException( e );
  1104. stencilDataObj.DataCheck = TemplateDataCheck.Invalid;
  1105. return;
  1106. }
  1107. }
  1108. }
  1109. break;
  1110. case "WriteMask":
  1111. {
  1112. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1113. {
  1114. stencilDataObj.WriteMaskInline = property;
  1115. }
  1116. else
  1117. {
  1118. try
  1119. {
  1120. stencilDataObj.WriteMask = Convert.ToInt32( match.Groups[ 2 ].Value );
  1121. }
  1122. catch( Exception e )
  1123. {
  1124. Debug.LogException( e );
  1125. stencilDataObj.DataCheck = TemplateDataCheck.Invalid;
  1126. return;
  1127. }
  1128. }
  1129. }
  1130. break;
  1131. case "CompFront":
  1132. case "Comp":
  1133. {
  1134. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1135. {
  1136. stencilDataObj.ComparisonFrontInline = property;
  1137. }
  1138. else
  1139. {
  1140. stencilDataObj.ComparisonFront = match.Groups[ 2 ].Value;
  1141. }
  1142. }
  1143. break;
  1144. case "PassFront":
  1145. case "Pass":
  1146. {
  1147. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1148. {
  1149. stencilDataObj.PassFrontInline = property;
  1150. }
  1151. else
  1152. {
  1153. stencilDataObj.PassFront = match.Groups[ 2 ].Value;
  1154. }
  1155. }
  1156. break;
  1157. case "FailFront":
  1158. case "Fail":
  1159. {
  1160. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1161. {
  1162. stencilDataObj.FailFrontInline = property;
  1163. }
  1164. else
  1165. {
  1166. stencilDataObj.FailFront = match.Groups[ 2 ].Value;
  1167. }
  1168. }
  1169. break;
  1170. case "ZFail":
  1171. case "ZFailFront":
  1172. {
  1173. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1174. {
  1175. stencilDataObj.ZFailFrontInline = property;
  1176. }
  1177. else
  1178. {
  1179. stencilDataObj.ZFailFront = match.Groups[ 2 ].Value;
  1180. }
  1181. }
  1182. break;
  1183. case "CompBack":
  1184. {
  1185. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1186. {
  1187. stencilDataObj.ComparisonBackInline = property;
  1188. }
  1189. else
  1190. {
  1191. stencilDataObj.ComparisonBack = match.Groups[ 2 ].Value;
  1192. }
  1193. }
  1194. break;
  1195. case "PassBack":
  1196. {
  1197. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1198. {
  1199. stencilDataObj.PassBackInline = property;
  1200. }
  1201. else
  1202. {
  1203. stencilDataObj.PassBack = match.Groups[ 2 ].Value;
  1204. }
  1205. }
  1206. break;
  1207. case "FailBack":
  1208. {
  1209. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1210. {
  1211. stencilDataObj.FailBackInline = property;
  1212. }
  1213. else
  1214. {
  1215. stencilDataObj.FailBack = match.Groups[ 2 ].Value;
  1216. }
  1217. }
  1218. break;
  1219. case "ZFailBack":
  1220. {
  1221. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1222. {
  1223. stencilDataObj.ZFailBackInline = property;
  1224. }
  1225. else
  1226. {
  1227. stencilDataObj.ZFailBack = match.Groups[ 2 ].Value;
  1228. }
  1229. }
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. }
  1235. }
  1236. public static void CreateColorMask( string colorMaskData, ref TemplateColorMaskData colorMaskObj, string pattern )
  1237. {
  1238. colorMaskObj.DataCheck = TemplateDataCheck.Invalid;
  1239. Match match = Regex.Match( colorMaskData, pattern );
  1240. //if( match.Groups.Count == 3 /*&& !match.Groups[ 2 ].Success*/ ) // second group is the colormask MRT which isn't implemented yet
  1241. {
  1242. string property = string.Empty;
  1243. if( match.Groups[ 1 ].Success && IsInlineProperty( match.Groups[ 1 ].Value, ref property ) )
  1244. {
  1245. colorMaskObj.InlineData = property;
  1246. colorMaskObj.DataCheck = TemplateDataCheck.Valid;
  1247. colorMaskObj.Target = match.Groups[ 2 ].Value;
  1248. }
  1249. else
  1250. {
  1251. for( int i = 0; i < 4; i++ )
  1252. {
  1253. colorMaskObj.ColorMaskData[ i ] = false;
  1254. }
  1255. colorMaskObj.Target = match.Groups[ 2 ].Value;
  1256. colorMaskObj.DataCheck = TemplateDataCheck.Valid;
  1257. try
  1258. {
  1259. for( int i = 0; i < match.Groups[ 1 ].Value.Length; i++ )
  1260. {
  1261. switch( Char.ToLower( match.Groups[ 1 ].Value[ i ] ) )
  1262. {
  1263. case 'r': colorMaskObj.ColorMaskData[ 0 ] = true; break;
  1264. case 'g': colorMaskObj.ColorMaskData[ 1 ] = true; break;
  1265. case 'b': colorMaskObj.ColorMaskData[ 2 ] = true; break;
  1266. case 'a': colorMaskObj.ColorMaskData[ 3 ] = true; break;
  1267. case '0':
  1268. {
  1269. for( int j = 0; j < 4; j++ )
  1270. {
  1271. colorMaskObj.ColorMaskData[ j ] = false;
  1272. }
  1273. return;
  1274. }
  1275. default:
  1276. {
  1277. colorMaskObj.DataCheck = TemplateDataCheck.Invalid;
  1278. return;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. catch( Exception e )
  1284. {
  1285. Debug.LogException( e );
  1286. colorMaskObj.DataCheck = TemplateDataCheck.Invalid;
  1287. return;
  1288. }
  1289. }
  1290. }
  1291. }
  1292. public static void CreateAlphaToMask( string alphaToMaskData, ref TemplateAlphaToMaskData alphaToMaskObj )
  1293. {
  1294. alphaToMaskObj.DataCheck = TemplateDataCheck.Invalid;
  1295. Match match = Regex.Match( alphaToMaskData, AlphaToMaskPattern );
  1296. if( match.Groups.Count == 2 )
  1297. {
  1298. string property = string.Empty;
  1299. if( match.Groups[ 1 ].Success && IsInlineProperty( match.Groups[ 1 ].Value, ref property ) )
  1300. {
  1301. alphaToMaskObj.InlineData = property;
  1302. alphaToMaskObj.DataCheck = TemplateDataCheck.Valid;
  1303. }
  1304. else
  1305. {
  1306. try
  1307. {
  1308. alphaToMaskObj.AlphaToMaskData = match.Groups[ 1 ].Value == "On" ? true : false;
  1309. alphaToMaskObj.DataCheck = TemplateDataCheck.Valid;
  1310. }
  1311. catch( Exception e )
  1312. {
  1313. alphaToMaskObj.DataCheck = TemplateDataCheck.Invalid;
  1314. Debug.LogException( e );
  1315. return;
  1316. }
  1317. }
  1318. }
  1319. }
  1320. public static void CreateCullMode( string cullModeData, ref TemplateCullModeData cullDataObj )
  1321. {
  1322. cullDataObj.DataCheck = TemplateDataCheck.Invalid;
  1323. Match match = Regex.Match( cullModeData, CullModePattern );
  1324. if( match.Groups.Count == 2 )
  1325. {
  1326. string property = string.Empty;
  1327. if( match.Groups[ 1 ].Success && IsInlineProperty( match.Groups[ 1 ].Value, ref property ) )
  1328. {
  1329. cullDataObj.InlineData = property;
  1330. cullDataObj.DataCheck = TemplateDataCheck.Valid;
  1331. }
  1332. else
  1333. {
  1334. try
  1335. {
  1336. cullDataObj.CullModeData = (CullMode)Enum.Parse( typeof( CullMode ), match.Groups[ 1 ].Value );
  1337. cullDataObj.DataCheck = TemplateDataCheck.Valid;
  1338. }
  1339. catch( Exception e )
  1340. {
  1341. cullDataObj.DataCheck = TemplateDataCheck.Invalid;
  1342. Debug.LogException( e );
  1343. return;
  1344. }
  1345. }
  1346. }
  1347. }
  1348. public static void CreateBlendMode( string blendModeData, ref TemplateBlendData blendDataObj, string pattern )
  1349. {
  1350. blendDataObj.ValidBlendMode = true;
  1351. string property = string.Empty;
  1352. bool noMatches = true;
  1353. // TODO: OPTIMIZE REGEX EXPRESSIONS TO NOT CATCH EMPTY GROUPS
  1354. Match match = Regex.Match( blendModeData, pattern );
  1355. //Debug.Log( blendModeData + " | " + match.Groups.Count + "|" + match.Groups[ 0 ].Value + " " + match.Groups[ 0 ].Success + " " + match.Groups[ 1 ].Success + " " + match.Groups[ 2 ].Success + " " + match.Groups[ 3 ].Success + " " + match.Groups[ 4 ].Success + " " + match.Groups[ 5 ].Success );
  1356. {
  1357. if( match.Groups.Count <= 4 && match.Groups.Count >= 3 )
  1358. {
  1359. if( match.Groups[ 0 ].Success && match.Groups[ 2 ].Success )
  1360. {
  1361. try
  1362. {
  1363. if( IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1364. {
  1365. blendDataObj.SourceFactorRGBInline = property;
  1366. }
  1367. else
  1368. {
  1369. AvailableBlendFactor sourceAll = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), match.Groups[ 2 ].Value );
  1370. blendDataObj.SourceFactorRGB = sourceAll;
  1371. }
  1372. if( match.Groups[ 3 ].Success && IsInlineProperty( match.Groups[ 3 ].Value, ref property ) )
  1373. {
  1374. blendDataObj.DestFactorRGBInline = property;
  1375. }
  1376. else
  1377. {
  1378. AvailableBlendFactor destAll = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), match.Groups[ 3 ].Value );
  1379. blendDataObj.DestFactorRGB = destAll;
  1380. }
  1381. blendDataObj.Target = match.Groups[ 1 ].Value;
  1382. blendDataObj.SeparateBlendFactors = false;
  1383. blendDataObj.BlendModeOff = false;
  1384. noMatches = false;
  1385. }
  1386. catch( Exception e )
  1387. {
  1388. Debug.LogException( e );
  1389. blendDataObj.DataCheck = TemplateDataCheck.Invalid;
  1390. blendDataObj.ValidBlendMode = false;
  1391. return;
  1392. }
  1393. }
  1394. }
  1395. else if( match.Groups.Count >= 5 )
  1396. {
  1397. if( match.Groups[ 1 ].Success )
  1398. blendDataObj.Target = match.Groups[ 1 ].Value;
  1399. if( !match.Groups[ 2 ].Success && !match.Groups[ 3 ].Success )
  1400. {
  1401. if( match.Groups[ 0 ].Value.IndexOf("Off") > -1 )
  1402. {
  1403. blendDataObj.BlendModeOff = true;
  1404. noMatches = false;
  1405. }
  1406. }
  1407. if( match.Groups[ 0 ].Success && match.Groups[ 2 ].Success )
  1408. {
  1409. try
  1410. {
  1411. if( IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1412. {
  1413. blendDataObj.SourceFactorRGBInline = property;
  1414. }
  1415. else
  1416. {
  1417. AvailableBlendFactor sourceRGB = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), match.Groups[ 2 ].Value );
  1418. blendDataObj.SourceFactorRGB = sourceRGB;
  1419. }
  1420. if( match.Groups[ 3 ].Success && IsInlineProperty( match.Groups[ 3 ].Value, ref property ) )
  1421. {
  1422. blendDataObj.DestFactorRGBInline = property;
  1423. }
  1424. else
  1425. {
  1426. AvailableBlendFactor destRGB = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), match.Groups[ 3 ].Value );
  1427. blendDataObj.DestFactorRGB = destRGB;
  1428. }
  1429. if( match.Groups[ 4 ].Success && match.Groups[ 5 ].Success )
  1430. {
  1431. if( IsInlineProperty( match.Groups[ 4 ].Value, ref property ) )
  1432. {
  1433. blendDataObj.SourceFactorAlphaInline = property;
  1434. }
  1435. else
  1436. {
  1437. AvailableBlendFactor sourceA = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), match.Groups[ 4 ].Value );
  1438. blendDataObj.SourceFactorAlpha = sourceA;
  1439. }
  1440. if( IsInlineProperty( match.Groups[ 5 ].Value, ref property ) )
  1441. {
  1442. blendDataObj.DestFactorAlphaInline = property;
  1443. }
  1444. else
  1445. {
  1446. AvailableBlendFactor destA = (AvailableBlendFactor)Enum.Parse( typeof( AvailableBlendFactor ), match.Groups[ 5 ].Value );
  1447. blendDataObj.DestFactorAlpha = destA;
  1448. }
  1449. blendDataObj.SeparateBlendFactors = true;
  1450. }
  1451. else
  1452. {
  1453. blendDataObj.SeparateBlendFactors = false;
  1454. }
  1455. blendDataObj.Target = match.Groups[ 1 ].Value;
  1456. blendDataObj.BlendModeOff = false;
  1457. noMatches = false;
  1458. }
  1459. catch( Exception e )
  1460. {
  1461. Debug.LogException( e );
  1462. blendDataObj.DataCheck = TemplateDataCheck.Invalid;
  1463. blendDataObj.ValidBlendMode = false;
  1464. return;
  1465. }
  1466. }
  1467. }
  1468. }
  1469. if( noMatches )
  1470. blendDataObj.ValidBlendMode = false;
  1471. }
  1472. public static void CreateBlendOp( string blendOpData, ref TemplateBlendData blendDataObj, string pattern )
  1473. {
  1474. bool noMatches = true;
  1475. blendDataObj.ValidBlendOp = true;
  1476. string property = string.Empty;
  1477. // TODO: OPTIMIZE REGEX EXPRESSIONS TO NOT CATCH EMPTY GROUPS
  1478. Match match = Regex.Match( blendOpData, pattern );
  1479. {
  1480. if( match.Groups.Count == 3 )
  1481. {
  1482. if( match.Groups[ 0 ].Success && match.Groups[ 2 ].Success )
  1483. {
  1484. try
  1485. {
  1486. if( IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1487. {
  1488. blendDataObj.BlendOpRGBInline = property;
  1489. }
  1490. else
  1491. {
  1492. AvailableBlendOps blendOpsAll = (AvailableBlendOps)Enum.Parse( typeof( AvailableBlendOps ), match.Groups[ 2 ].Value );
  1493. blendDataObj.BlendOpRGB = blendOpsAll;
  1494. }
  1495. blendDataObj.SeparateBlendOps = false;
  1496. blendDataObj.Target = match.Groups[ 1 ].Value;
  1497. noMatches = false;
  1498. }
  1499. catch( Exception e )
  1500. {
  1501. Debug.LogException( e );
  1502. blendDataObj.DataCheck = TemplateDataCheck.Invalid;
  1503. blendDataObj.ValidBlendOp = false;
  1504. return;
  1505. }
  1506. }
  1507. }
  1508. else if( match.Groups.Count == 4 )
  1509. {
  1510. if( match.Groups[ 0 ].Success && match.Groups[ 2 ].Success )
  1511. {
  1512. try
  1513. {
  1514. if( IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1515. {
  1516. blendDataObj.BlendOpRGBInline = property;
  1517. }
  1518. else
  1519. {
  1520. AvailableBlendOps blendOpsRGB = (AvailableBlendOps)Enum.Parse( typeof( AvailableBlendOps ), match.Groups[ 2 ].Value );
  1521. blendDataObj.BlendOpRGB = blendOpsRGB;
  1522. }
  1523. if( match.Groups[ 3 ].Success )
  1524. {
  1525. if( IsInlineProperty( match.Groups[ 3 ].Value, ref property ) )
  1526. {
  1527. blendDataObj.BlendOpAlphaInline = property;
  1528. }
  1529. else
  1530. {
  1531. AvailableBlendOps blendOpsA = (AvailableBlendOps)Enum.Parse( typeof( AvailableBlendOps ), match.Groups[ 3 ].Value );
  1532. blendDataObj.BlendOpAlpha = blendOpsA;
  1533. }
  1534. blendDataObj.SeparateBlendOps = true;
  1535. }
  1536. else
  1537. {
  1538. blendDataObj.SeparateBlendOps = false;
  1539. }
  1540. blendDataObj.Target = match.Groups[ 1 ].Value;
  1541. noMatches = false;
  1542. }
  1543. catch( Exception e )
  1544. {
  1545. Debug.LogException( e );
  1546. blendDataObj.DataCheck = TemplateDataCheck.Invalid;
  1547. blendDataObj.ValidBlendOp = false;
  1548. return;
  1549. }
  1550. }
  1551. }
  1552. }
  1553. if( noMatches )
  1554. blendDataObj.ValidBlendOp = false;
  1555. }
  1556. public static void FetchLocalVars( string body, ref List<TemplateLocalVarData> localVarList, TemplateFunctionData vertexFunction, TemplateFunctionData fragFunction )
  1557. {
  1558. foreach( Match match in Regex.Matches( body, LocalVarPattern ) )
  1559. {
  1560. if( match.Groups.Count == 4 )
  1561. {
  1562. if( CgToWirePortType.ContainsKey( match.Groups[ 2 ].Value ) )
  1563. {
  1564. MasterNodePortCategory category;
  1565. if( fragFunction.MainBodyLocalIdx > vertexFunction.MainBodyLocalIdx )
  1566. {
  1567. if( match.Index < fragFunction.MainBodyLocalIdx )
  1568. {
  1569. category = MasterNodePortCategory.Vertex;
  1570. }
  1571. else
  1572. {
  1573. category = MasterNodePortCategory.Fragment;
  1574. }
  1575. }
  1576. else
  1577. {
  1578. if( match.Index < vertexFunction.MainBodyLocalIdx )
  1579. {
  1580. category = MasterNodePortCategory.Fragment;
  1581. }
  1582. else
  1583. {
  1584. category = MasterNodePortCategory.Vertex;
  1585. }
  1586. }
  1587. if( !string.IsNullOrEmpty( match.Groups[ 1 ].Value ) && ShortcutToInfo.ContainsKey( match.Groups[ 1 ].Value ) )
  1588. {
  1589. string id = match.Groups[ 0 ].Value.Substring( 0, match.Groups[ 0 ].Value.IndexOf( "*/" ) + 2 );
  1590. TemplateLocalVarData data = new TemplateLocalVarData( ShortcutToInfo[ match.Groups[ 1 ].Value ], id, CgToWirePortType[ match.Groups[ 2 ].Value ], category, match.Groups[ 3 ].Value, match.Index );
  1591. localVarList.Add( data );
  1592. }
  1593. else
  1594. {
  1595. TemplateLocalVarData data = new TemplateLocalVarData( CgToWirePortType[ match.Groups[ 2 ].Value ], category, match.Groups[ 3 ].Value, match.Index );
  1596. localVarList.Add( data );
  1597. }
  1598. }
  1599. }
  1600. }
  1601. }
  1602. public static void FetchInlineVars( string body, ref TemplateIdManager idManager )
  1603. {
  1604. foreach( Match match in Regex.Matches( body, InlinePattern ) )
  1605. {
  1606. if( match.Success && match.Groups.Count == 2 )
  1607. {
  1608. string id = match.Groups[ 0 ].Value;
  1609. string prop = match.Groups[ 1 ].Value;
  1610. idManager.RegisterTag( id, prop );
  1611. }
  1612. }
  1613. }
  1614. public static TemplateSRPType CreateTags( ref TemplateTagsModuleData tagsObj, bool isSubShader )
  1615. {
  1616. TemplateSRPType srpType = TemplateSRPType.BuiltIn;
  1617. MatchCollection matchColl = Regex.Matches( tagsObj.TagsId, TagsPattern, RegexOptions.IgnorePatternWhitespace );
  1618. int count = matchColl.Count;
  1619. if( count > 0 )
  1620. {
  1621. for( int i = 0; i < count; i++ )
  1622. {
  1623. if( matchColl[ i ].Groups.Count == 3 )
  1624. {
  1625. if( isSubShader && matchColl[ i ].Groups[ 1 ].Value.Equals( "RenderPipeline" ) )
  1626. {
  1627. if( TagToRenderPipeline.ContainsKey( matchColl[ i ].Groups[ 2 ].Value ) )
  1628. srpType = TagToRenderPipeline[ matchColl[ i ].Groups[ 2 ].Value ];
  1629. }
  1630. tagsObj.Tags.Add( new TemplatesTagData( matchColl[ i ].Groups[ 1 ].Value, matchColl[ i ].Groups[ 2 ].Value ) );
  1631. }
  1632. }
  1633. }
  1634. return srpType;
  1635. }
  1636. public static void CreateZWriteMode( string zWriteData, ref TemplateDepthData depthDataObj )
  1637. {
  1638. depthDataObj.DataCheck = TemplateDataCheck.Invalid;
  1639. Match match = Regex.Match( zWriteData, ZWritePattern );
  1640. if( match.Groups.Count == 2 )
  1641. {
  1642. string property = string.Empty;
  1643. if( match.Groups[ 1 ].Success && IsInlineProperty( match.Groups[ 1 ].Value, ref property ) )
  1644. {
  1645. depthDataObj.ZWriteInlineValue = property;
  1646. depthDataObj.DataCheck = TemplateDataCheck.Valid;
  1647. depthDataObj.ValidZWrite = true;
  1648. }
  1649. else
  1650. {
  1651. try
  1652. {
  1653. depthDataObj.ZWriteModeValue = (ZWriteMode)Enum.Parse( typeof( ZWriteMode ), match.Groups[ 1 ].Value );
  1654. depthDataObj.DataCheck = TemplateDataCheck.Valid;
  1655. depthDataObj.ValidZWrite = true;
  1656. }
  1657. catch
  1658. {
  1659. depthDataObj.DataCheck = TemplateDataCheck.Invalid;
  1660. }
  1661. }
  1662. }
  1663. }
  1664. public static void CreateZTestMode( string zTestData, ref TemplateDepthData depthDataObj )
  1665. {
  1666. depthDataObj.DataCheck = TemplateDataCheck.Invalid;
  1667. Match match = Regex.Match( zTestData, ZTestPattern );
  1668. if( match.Groups.Count == 2 )
  1669. {
  1670. string property = string.Empty;
  1671. if( match.Groups[ 1 ].Success && IsInlineProperty( match.Groups[ 1 ].Value, ref property ) )
  1672. {
  1673. depthDataObj.ZTestInlineValue = property;
  1674. depthDataObj.DataCheck = TemplateDataCheck.Valid;
  1675. depthDataObj.ValidZTest = true;
  1676. }
  1677. else
  1678. {
  1679. try
  1680. {
  1681. depthDataObj.ZTestModeValue = (ZTestMode)Enum.Parse( typeof( ZTestMode ), match.Groups[ 1 ].Value );
  1682. depthDataObj.DataCheck = TemplateDataCheck.Valid;
  1683. depthDataObj.ValidZTest = true;
  1684. }
  1685. catch
  1686. {
  1687. depthDataObj.DataCheck = TemplateDataCheck.Invalid;
  1688. }
  1689. }
  1690. }
  1691. }
  1692. public static void CreateZOffsetMode( string zOffsetData, ref TemplateDepthData depthDataObj )
  1693. {
  1694. depthDataObj.DataCheck = TemplateDataCheck.Invalid;
  1695. Match match = Regex.Match( zOffsetData, ZOffsetPattern );
  1696. if( match.Groups.Count == 3 )
  1697. {
  1698. try
  1699. {
  1700. string property = string.Empty;
  1701. if( match.Groups[ 1 ].Success && IsInlineProperty( match.Groups[ 1 ].Value, ref property ) )
  1702. {
  1703. depthDataObj.OffsetFactorInlineValue = property;
  1704. }
  1705. else
  1706. {
  1707. depthDataObj.OffsetFactor = Convert.ToSingle( match.Groups[ 1 ].Value );
  1708. }
  1709. if( match.Groups[ 2 ].Success && IsInlineProperty( match.Groups[ 2 ].Value, ref property ) )
  1710. {
  1711. depthDataObj.OffsetUnitsInlineValue = property;
  1712. }
  1713. else
  1714. {
  1715. depthDataObj.OffsetUnits = Convert.ToSingle( match.Groups[ 2 ].Value );
  1716. }
  1717. depthDataObj.ValidOffset = true;
  1718. depthDataObj.DataCheck = TemplateDataCheck.Valid;
  1719. }
  1720. catch
  1721. {
  1722. depthDataObj.DataCheck = TemplateDataCheck.Invalid;
  1723. }
  1724. }
  1725. }
  1726. public static List<TemplateVertexData> CreateVertexDataList( string vertexData, string parametersBody )
  1727. {
  1728. List<TemplateVertexData> vertexDataList = null;
  1729. Dictionary<TemplateSemantics, TemplateVertexData> vertexDataDict = null;
  1730. foreach( Match match in Regex.Matches( vertexData, VertexDataPattern ) )
  1731. {
  1732. if( match.Groups.Count > 1 )
  1733. {
  1734. if( vertexDataList == null )
  1735. {
  1736. vertexDataList = new List<TemplateVertexData>();
  1737. vertexDataDict = new Dictionary<TemplateSemantics, TemplateVertexData>();
  1738. }
  1739. WirePortDataType dataType = CgToWirePortType[ match.Groups[ 1 ].Value ];
  1740. string varName = match.Groups[ 2 ].Value;
  1741. TemplateSemantics semantics = TemplateSemantics.NONE;
  1742. try
  1743. {
  1744. semantics = (TemplateSemantics)Enum.Parse( typeof( TemplateSemantics ), match.Groups[ 3 ].Value );
  1745. }
  1746. catch(Exception e)
  1747. {
  1748. Debug.LogException( e );
  1749. }
  1750. TemplateVertexData templateVertexData = new TemplateVertexData( semantics, dataType, varName );
  1751. vertexDataList.Add( templateVertexData );
  1752. vertexDataDict.Add( semantics, templateVertexData );
  1753. }
  1754. }
  1755. if( vertexData.Contains( Constants.InstanceIdMacro ) )
  1756. {
  1757. TemplateVertexData templateVertexData = new TemplateVertexData( TemplateSemantics.SV_InstanceID, WirePortDataType.UINT, Constants.InstanceIdVariable );
  1758. templateVertexData.DataInfo = TemplateInfoOnSematics.INSTANCE_ID;
  1759. templateVertexData.Available = true;
  1760. templateVertexData.ExcludeStructPrefix = true;
  1761. vertexDataList.Add( templateVertexData );
  1762. vertexDataDict.Add( TemplateSemantics.SV_InstanceID, templateVertexData );
  1763. }
  1764. if( !string.IsNullOrEmpty( parametersBody ) )
  1765. {
  1766. string[] paramsArray = parametersBody.Split( IOUtils.FIELD_SEPARATOR );
  1767. if( paramsArray.Length > 0 )
  1768. {
  1769. for( int i = 0; i < paramsArray.Length; i++ )
  1770. {
  1771. string[] paramDataArr = paramsArray[ i ].Split( IOUtils.VALUE_SEPARATOR );
  1772. if( paramDataArr.Length == 2 )
  1773. {
  1774. string[] swizzleInfoArr = paramDataArr[ 1 ].Split( IOUtils.FLOAT_SEPARATOR );
  1775. TemplateSemantics semantic = ShortcutToSemantic[ swizzleInfoArr[ 0 ] ];
  1776. if( vertexDataDict.ContainsKey( semantic ) )
  1777. {
  1778. TemplateVertexData templateVertexData = vertexDataDict[ semantic ];
  1779. if( templateVertexData != null )
  1780. {
  1781. if( swizzleInfoArr.Length > 1 )
  1782. {
  1783. templateVertexData.DataSwizzle = "." + swizzleInfoArr[ 1 ];
  1784. }
  1785. templateVertexData.DataInfo = ShortcutToInfo[ paramDataArr[ 0 ] ];
  1786. templateVertexData.Available = true;
  1787. }
  1788. }
  1789. }
  1790. }
  1791. }
  1792. }
  1793. if( vertexDataDict != null )
  1794. {
  1795. vertexDataDict.Clear();
  1796. vertexDataDict = null;
  1797. }
  1798. return vertexDataList;
  1799. }
  1800. public static TemplateInterpData CreateInterpDataList( string interpData, string fullLine, int maxInterpolators )
  1801. {
  1802. TemplateInterpData interpDataObj = null;
  1803. List<TemplateVertexData> interpDataList = null;
  1804. Dictionary<TemplateSemantics, TemplateVertexData> interpDataDict = null;
  1805. Match rangeMatch = Regex.Match( fullLine, InterpRangePattern );
  1806. if( rangeMatch.Groups.Count > 0 )
  1807. {
  1808. interpDataObj = new TemplateInterpData();
  1809. // Get range of available interpolators
  1810. int minVal = 0;
  1811. int maxVal = 0;
  1812. try
  1813. {
  1814. string[] minValArgs = rangeMatch.Groups[ 1 ].Value.Split( IOUtils.FLOAT_SEPARATOR );
  1815. minVal = Convert.ToInt32( minValArgs[ 0 ] );
  1816. if( string.IsNullOrEmpty( rangeMatch.Groups[ 2 ].Value ) )
  1817. {
  1818. maxVal = maxInterpolators - 1;
  1819. interpDataObj.DynamicMax = true;
  1820. }
  1821. else
  1822. {
  1823. maxVal = Convert.ToInt32( rangeMatch.Groups[ 2 ].Value );
  1824. }
  1825. if( minVal > maxVal )
  1826. {
  1827. //int aux = minVal;
  1828. //minVal = maxVal;
  1829. //maxVal = aux;
  1830. maxVal = minVal;
  1831. }
  1832. for( int i = minVal; i <= maxVal; i++ )
  1833. {
  1834. interpDataObj.AvailableInterpolators.Add( new TemplateInterpElement( IntToSemantic[ i ] ) );
  1835. }
  1836. if( minValArgs.Length > 1 )
  1837. {
  1838. interpDataObj.AvailableInterpolators[ 0 ].SetAvailableChannelsFromString( minValArgs[ 1 ] );
  1839. }
  1840. }
  1841. catch( Exception e )
  1842. {
  1843. Debug.LogException( e );
  1844. }
  1845. interpDataList = new List<TemplateVertexData>();
  1846. interpDataDict = new Dictionary<TemplateSemantics, TemplateVertexData>();
  1847. //Get Current interpolators
  1848. int parametersBeginIdx = fullLine.IndexOf( ":" ) + 1;
  1849. int parametersEnd = fullLine.IndexOf( TemplatesManager.TemplateEndOfLine );
  1850. string parametersBody = fullLine.Substring( parametersBeginIdx, parametersEnd - parametersBeginIdx );
  1851. foreach( Match match in Regex.Matches( interpData, VertexDataPattern ) )
  1852. {
  1853. if( match.Groups.Count > 1 )
  1854. {
  1855. WirePortDataType dataType = CgToWirePortType[ match.Groups[ 1 ].Value ];
  1856. string varName = match.Groups[ 2 ].Value;
  1857. TemplateSemantics semantics = TemplateSemantics.NONE;
  1858. try
  1859. {
  1860. semantics = (TemplateSemantics)Enum.Parse( typeof( TemplateSemantics ), match.Groups[ 3 ].Value );
  1861. }
  1862. catch( Exception e )
  1863. {
  1864. Debug.LogException( e );
  1865. }
  1866. TemplateVertexData templateVertexData = new TemplateVertexData( semantics, dataType, varName );
  1867. //interpDataList.Add( templateVertexData );
  1868. interpDataDict.Add( semantics, templateVertexData );
  1869. interpDataObj.RawInterpolators.Add( templateVertexData );
  1870. //Check if they are also on the free channels list and update their names
  1871. interpDataObj.ReplaceNameOnInterpolator( semantics, varName );
  1872. }
  1873. }
  1874. if( interpData.Contains( Constants.InstanceIdMacro ) )
  1875. {
  1876. TemplateVertexData templateInterpData = new TemplateVertexData( TemplateSemantics.SV_InstanceID, WirePortDataType.UINT, Constants.InstanceIdVariable );
  1877. templateInterpData.DataInfo = TemplateInfoOnSematics.INSTANCE_ID;
  1878. templateInterpData.Available = true;
  1879. templateInterpData.ExcludeStructPrefix = true;
  1880. interpDataList.Add( templateInterpData );
  1881. interpDataDict.Add( TemplateSemantics.SV_InstanceID, templateInterpData );
  1882. }
  1883. Dictionary<string, TemplateVertexData> auxDict = new Dictionary<string, TemplateVertexData>();
  1884. // Get info for available interpolators
  1885. string[] paramsArray = parametersBody.Split( IOUtils.FIELD_SEPARATOR );
  1886. if( paramsArray.Length > 0 )
  1887. {
  1888. for( int i = 0; i < paramsArray.Length; i++ )
  1889. {
  1890. string[] paramDataArr = paramsArray[ i ].Split( IOUtils.VALUE_SEPARATOR );
  1891. if( paramDataArr.Length == 2 )
  1892. {
  1893. string[] swizzleInfoArr = paramDataArr[ 1 ].Split( IOUtils.FLOAT_SEPARATOR );
  1894. TemplateSemantics semantic = ShortcutToSemantic[ swizzleInfoArr[ 0 ] ];
  1895. if( interpDataDict.ContainsKey( semantic ) )
  1896. {
  1897. if( interpDataDict[ semantic ] != null )
  1898. {
  1899. string[] multiComponent = paramDataArr[ 0 ].Split( IOUtils.FLOAT_SEPARATOR );
  1900. if( multiComponent.Length > 1 )
  1901. {
  1902. TemplateVertexData templateInterpData = null;
  1903. if( auxDict.ContainsKey( multiComponent[ 0 ] ) )
  1904. {
  1905. templateInterpData = auxDict[ multiComponent[ 0 ] ];
  1906. }
  1907. else
  1908. {
  1909. templateInterpData = new TemplateVertexData( interpDataDict[ semantic ] );
  1910. //if( swizzleInfoArr.Length > 1 )
  1911. //{
  1912. // templateInterpData.DataSwizzle = "." + swizzleInfoArr[ 1 ];
  1913. //}
  1914. templateInterpData.DataInfo = ShortcutToInfo[ multiComponent[ 0 ] ];
  1915. templateInterpData.Available = true;
  1916. interpDataList.Add( templateInterpData );
  1917. auxDict.Add( multiComponent[ 0 ], templateInterpData );
  1918. }
  1919. if( swizzleInfoArr[ 1 ].Length == multiComponent[ 1 ].Length )
  1920. {
  1921. for( int channelIdx = 0; channelIdx < swizzleInfoArr[ 1 ].Length; channelIdx++ )
  1922. {
  1923. templateInterpData.RegisterComponent( multiComponent[ 1 ][ channelIdx ], interpDataDict[ semantic ].VarName + "." + swizzleInfoArr[ 1 ][ channelIdx ] );
  1924. }
  1925. }
  1926. }
  1927. else
  1928. {
  1929. TemplateVertexData templateInterpData = new TemplateVertexData( interpDataDict[ semantic ] );
  1930. if( swizzleInfoArr.Length > 1 )
  1931. {
  1932. templateInterpData.DataSwizzle = "." + swizzleInfoArr[ 1 ];
  1933. }
  1934. templateInterpData.DataInfo = ShortcutToInfo[ paramDataArr[ 0 ] ];
  1935. templateInterpData.Available = true;
  1936. interpDataList.Add( templateInterpData );
  1937. }
  1938. }
  1939. }
  1940. }
  1941. }
  1942. }
  1943. /*TODO:
  1944. 1) Remove interpDataList.Add( templateVertexData ); from initial foreach
  1945. 2) When looping though each foreach array element, create a new TemplateVertexData
  1946. from the one containted on the interpDataDict and add it to interpDataList
  1947. */
  1948. for( int i = 0; i < interpDataList.Count; i++ )
  1949. {
  1950. interpDataList[ i ].BuildVar();
  1951. }
  1952. auxDict.Clear();
  1953. auxDict = null;
  1954. interpDataObj.Interpolators = interpDataList;
  1955. interpDataDict.Clear();
  1956. interpDataDict = null;
  1957. }
  1958. return interpDataObj;
  1959. }
  1960. public static void FetchDependencies( TemplateInfoContainer dependencies, ref string body )
  1961. {
  1962. int index = body.IndexOf( TemplatesManager.TemplateDependenciesListTag );
  1963. if( index > 0 )
  1964. {
  1965. dependencies.Index = index;
  1966. dependencies.Id = TemplatesManager.TemplateDependenciesListTag;
  1967. dependencies.Data = TemplatesManager.TemplateDependenciesListTag;
  1968. }
  1969. else
  1970. {
  1971. int lastIndex = body.LastIndexOf( '}' );
  1972. if( lastIndex > 0 )
  1973. {
  1974. body = body.Insert( lastIndex, "\t" + TemplatesManager.TemplateDependenciesListTag + "\n" );
  1975. FetchDependencies( dependencies, ref body );
  1976. }
  1977. }
  1978. }
  1979. public static void FetchCustomInspector( TemplateInfoContainer inspectorContainer, ref string body )
  1980. {
  1981. Match match = Regex.Match( body, CustomInspectorPattern, RegexOptions.Multiline );
  1982. if( match != null && match.Groups.Count > 1 )
  1983. {
  1984. inspectorContainer.Index = match.Index;
  1985. inspectorContainer.Id = match.Groups[ 0 ].Value;
  1986. inspectorContainer.Data = match.Groups[ 1 ].Value;
  1987. #if UNITY_2019_3_OR_NEWER
  1988. if( ASEPackageManagerHelper.CurrentHDVersion > ASESRPVersions.ASE_SRP_6_9_1 )
  1989. {
  1990. if( inspectorContainer.Data.Equals( "UnityEditor.Experimental.Rendering.HDPipeline.HDLitGUI" ) )
  1991. inspectorContainer.Data = "UnityEditor.Rendering.HighDefinition.HDLitGUI";
  1992. }
  1993. #endif
  1994. }
  1995. else
  1996. {
  1997. int index = body.LastIndexOf( '}' );
  1998. if( index > 0 )
  1999. {
  2000. body = body.Insert( index, string.Format( "\tCustomEditor \"{0}\"\n", Constants.DefaultCustomInspector ) );
  2001. FetchCustomInspector( inspectorContainer, ref body );
  2002. }
  2003. }
  2004. }
  2005. public static void FetchFallback( TemplateInfoContainer fallbackContainer, ref string body )
  2006. {
  2007. Match match = Regex.Match( body, FallbackPattern, RegexOptions.Multiline | RegexOptions.IgnoreCase );
  2008. if( match != null && match.Groups.Count > 1 )
  2009. {
  2010. fallbackContainer.Index = match.Index;
  2011. fallbackContainer.Id = match.Groups[ 0 ].Value;
  2012. fallbackContainer.Data = match.Groups[ 1 ].Value;
  2013. }
  2014. else
  2015. {
  2016. int index = body.LastIndexOf( '}' );
  2017. if( index > 0 )
  2018. {
  2019. body = body.Insert( index, "\tFallback \"\"\n" );
  2020. FetchFallback( fallbackContainer, ref body );
  2021. }
  2022. }
  2023. }
  2024. public static string AutoSwizzleData( string dataVar, WirePortDataType from, WirePortDataType to, bool isPosition )
  2025. {
  2026. switch( from )
  2027. {
  2028. case WirePortDataType.COLOR:
  2029. case WirePortDataType.FLOAT4:
  2030. {
  2031. switch( to )
  2032. {
  2033. case WirePortDataType.FLOAT3: dataVar += ".xyz"; break;
  2034. case WirePortDataType.FLOAT2: dataVar += ".xy"; break;
  2035. case WirePortDataType.INT:
  2036. case WirePortDataType.FLOAT: dataVar += ".x"; break;
  2037. }
  2038. }
  2039. break;
  2040. case WirePortDataType.FLOAT3:
  2041. {
  2042. switch( to )
  2043. {
  2044. case WirePortDataType.FLOAT4: dataVar = string.Format( "float4({0},{1})", dataVar,(isPosition?1:0) ); break;
  2045. case WirePortDataType.FLOAT2: dataVar += ".xy"; break;
  2046. case WirePortDataType.INT:
  2047. case WirePortDataType.FLOAT: dataVar += ".x"; break;
  2048. }
  2049. }
  2050. break;
  2051. case WirePortDataType.FLOAT2:
  2052. {
  2053. switch( to )
  2054. {
  2055. case WirePortDataType.FLOAT4: dataVar = string.Format( "float4({0},0,{1})", dataVar , (isPosition ? 1 : 0) ); break;
  2056. case WirePortDataType.FLOAT3: dataVar = string.Format( "float3({0},0)", dataVar ); break;
  2057. case WirePortDataType.INT:
  2058. case WirePortDataType.FLOAT: dataVar += ".x"; break;
  2059. }
  2060. }
  2061. break;
  2062. case WirePortDataType.FLOAT:
  2063. {
  2064. switch( to )
  2065. {
  2066. case WirePortDataType.FLOAT4: dataVar = string.Format( "float4({0},0,0,{1})", dataVar, ( isPosition ? 1 : 0 ) ); break;
  2067. case WirePortDataType.FLOAT3: dataVar = string.Format( "float3({0},0,0)", dataVar ); break;
  2068. case WirePortDataType.FLOAT2: dataVar = string.Format( "float2({0},0)", dataVar ); break;
  2069. }
  2070. }
  2071. break;
  2072. }
  2073. return dataVar;
  2074. }
  2075. public static bool CheckIfTemplate( string assetPath )
  2076. {
  2077. if( assetPath.EndsWith( ".shader" ) )
  2078. {
  2079. if( File.Exists( assetPath ) )
  2080. {
  2081. string body = IOUtils.LoadTextFileFromDisk( assetPath );
  2082. return ( body.IndexOf( TemplatesManager.TemplateShaderNameBeginTag ) > -1 );
  2083. }
  2084. }
  2085. return false;
  2086. }
  2087. public static bool CheckIfCompatibles( WirePortDataType first, WirePortDataType second )
  2088. {
  2089. switch( first )
  2090. {
  2091. case WirePortDataType.OBJECT:
  2092. return true;
  2093. case WirePortDataType.FLOAT:
  2094. case WirePortDataType.FLOAT2:
  2095. case WirePortDataType.FLOAT3:
  2096. case WirePortDataType.FLOAT4:
  2097. case WirePortDataType.COLOR:
  2098. case WirePortDataType.INT:
  2099. {
  2100. switch( second )
  2101. {
  2102. case WirePortDataType.FLOAT3x3:
  2103. case WirePortDataType.FLOAT4x4:
  2104. case WirePortDataType.SAMPLER1D:
  2105. case WirePortDataType.SAMPLER2D:
  2106. case WirePortDataType.SAMPLER3D:
  2107. case WirePortDataType.SAMPLERCUBE:
  2108. case WirePortDataType.SAMPLER2DARRAY:
  2109. case WirePortDataType.SAMPLERSTATE:
  2110. return false;
  2111. }
  2112. }
  2113. break;
  2114. case WirePortDataType.FLOAT3x3:
  2115. case WirePortDataType.FLOAT4x4:
  2116. {
  2117. switch( second )
  2118. {
  2119. case WirePortDataType.FLOAT:
  2120. case WirePortDataType.FLOAT2:
  2121. case WirePortDataType.FLOAT3:
  2122. case WirePortDataType.FLOAT4:
  2123. case WirePortDataType.COLOR:
  2124. case WirePortDataType.INT:
  2125. case WirePortDataType.SAMPLER1D:
  2126. case WirePortDataType.SAMPLER2D:
  2127. case WirePortDataType.SAMPLER3D:
  2128. case WirePortDataType.SAMPLERCUBE:
  2129. case WirePortDataType.SAMPLER2DARRAY:
  2130. case WirePortDataType.SAMPLERSTATE:
  2131. return false;
  2132. }
  2133. }
  2134. break;
  2135. case WirePortDataType.SAMPLER1D:
  2136. case WirePortDataType.SAMPLER2D:
  2137. case WirePortDataType.SAMPLER3D:
  2138. case WirePortDataType.SAMPLERCUBE:
  2139. case WirePortDataType.SAMPLER2DARRAY:
  2140. {
  2141. switch( second )
  2142. {
  2143. case WirePortDataType.FLOAT:
  2144. case WirePortDataType.FLOAT2:
  2145. case WirePortDataType.FLOAT3:
  2146. case WirePortDataType.FLOAT4:
  2147. case WirePortDataType.FLOAT3x3:
  2148. case WirePortDataType.FLOAT4x4:
  2149. case WirePortDataType.COLOR:
  2150. case WirePortDataType.INT:
  2151. case WirePortDataType.SAMPLERSTATE:
  2152. return false;
  2153. }
  2154. }
  2155. break;
  2156. case WirePortDataType.SAMPLERSTATE:
  2157. {
  2158. switch( second )
  2159. {
  2160. default:
  2161. return false;
  2162. case WirePortDataType.SAMPLERSTATE:
  2163. case WirePortDataType.OBJECT:
  2164. break;
  2165. }
  2166. }
  2167. break;
  2168. }
  2169. return true;
  2170. }
  2171. // Lightweight <-> Default functions
  2172. public static string WorldSpaceViewDir( MasterNodeDataCollector dataCollector, string worldPosVec3, bool normalize )
  2173. {
  2174. string value = string.Empty;
  2175. if( dataCollector.IsTemplate && dataCollector.IsSRP )
  2176. {
  2177. value = string.Format( "_WorldSpaceCameraPos.xyz - {0}", worldPosVec3 );
  2178. }
  2179. else
  2180. {
  2181. value = string.Format( "UnityWorldSpaceViewDir( {0} )", worldPosVec3 );
  2182. }
  2183. if( normalize )
  2184. {
  2185. value = SafeNormalize( dataCollector, value );
  2186. }
  2187. return value;
  2188. }
  2189. public static string SafeNormalize( MasterNodeDataCollector dataCollector, string value )
  2190. {
  2191. if( dataCollector.IsTemplate && dataCollector.IsSRP )
  2192. {
  2193. value = string.Format( "SafeNormalize( {0} )", value );
  2194. }
  2195. else
  2196. {
  2197. dataCollector.AddToIncludes( -1, Constants.UnityBRDFLib );
  2198. value = string.Format( "Unity_SafeNormalize( {0} )", value );
  2199. }
  2200. return value;
  2201. }
  2202. public static bool IsInlineProperty( string data, ref string property )
  2203. {
  2204. if( data.Length > 0 && data[ 0 ] == '[' && data[ data.Length - 1 ] == ']' )
  2205. {
  2206. property = data.Substring( 1, data.Length - 2 );
  2207. return true;
  2208. }
  2209. return false;
  2210. }
  2211. // public static readonly string FetchDefaultDepthFormat = "UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture,UNITY_PROJ_COORD( {0} )))";
  2212. public static readonly string FetchDefaultDepthFormat = "SAMPLE_DEPTH_TEXTURE( _CameraDepthTexture, {0}.xy )";
  2213. public static readonly string FetchDefaultDepthFormatVertex = "SAMPLE_DEPTH_TEXTURE_LOD( _CameraDepthTexture, float4( {0}.xy, 0, 0 ) )";
  2214. public static readonly string FetchLWDepthFormat = "SHADERGRAPH_SAMPLE_SCENE_DEPTH( {0}.xy )";
  2215. public static readonly string FetchLWDepthFormatVertex = "SHADERGRAPH_SAMPLE_SCENE_DEPTH_LOD( {0}.xy )";
  2216. #if UNITY_2018_3_OR_NEWER
  2217. public static readonly string FetchHDDepthFormat = "SampleCameraDepth( {0}.xy )";
  2218. #else
  2219. public static readonly string FetchHDDepthFormat = "SAMPLE_TEXTURE2D( _CameraDepthTexture, s_point_clamp_sampler, {0}.xy ).r";
  2220. #endif
  2221. public static string CreateDepthFetch( MasterNodeDataCollector dataCollector, string screenPos )
  2222. {
  2223. string screenDepthInstruction = string.Empty;
  2224. if( dataCollector.IsTemplate && dataCollector.IsSRP )
  2225. {
  2226. if( dataCollector.TemplateDataCollectorInstance.CurrentSRPType == TemplateSRPType.Lightweight )
  2227. {
  2228. if( dataCollector.PortCategory == MasterNodePortCategory.Vertex )
  2229. {
  2230. string m_functionBody = string.Empty;
  2231. GenerateLW( ref m_functionBody );
  2232. dataCollector.AddFunctions( FetchLWDepthFormatVertex, m_functionBody, "0" );
  2233. screenDepthInstruction = string.Format( FetchLWDepthFormatVertex, screenPos );
  2234. }
  2235. else
  2236. screenDepthInstruction = string.Format( FetchLWDepthFormat, screenPos );
  2237. }
  2238. else if( dataCollector.TemplateDataCollectorInstance.CurrentSRPType == TemplateSRPType.HD )
  2239. screenDepthInstruction = string.Format( FetchHDDepthFormat, screenPos );
  2240. }
  2241. else
  2242. {
  2243. if( dataCollector.PortCategory == MasterNodePortCategory.Vertex )
  2244. screenDepthInstruction = string.Format( FetchDefaultDepthFormatVertex, screenPos );
  2245. else
  2246. screenDepthInstruction = string.Format( FetchDefaultDepthFormat, screenPos );
  2247. }
  2248. return screenDepthInstruction;
  2249. }
  2250. public static void GenerateLW( ref string body )
  2251. {
  2252. body = string.Empty;
  2253. IOUtils.AddFunctionHeader( ref body, "float SHADERGRAPH_SAMPLE_SCENE_DEPTH_LOD(float2 uv)" );
  2254. IOUtils.AddFunctionLine( ref body, "#if defined(REQUIRE_DEPTH_TEXTURE)" );
  2255. IOUtils.AddFunctionLine( ref body, "#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)" );
  2256. IOUtils.AddFunctionLine( ref body, " \tfloat rawDepth = SAMPLE_TEXTURE2D_ARRAY_LOD(_CameraDepthTexture, sampler_CameraDepthTexture, uv, unity_StereoEyeIndex, 0).r;" );
  2257. IOUtils.AddFunctionLine( ref body, "#else" );
  2258. IOUtils.AddFunctionLine( ref body, " \tfloat rawDepth = SAMPLE_DEPTH_TEXTURE_LOD(_CameraDepthTexture, sampler_CameraDepthTexture, uv, 0);" );
  2259. IOUtils.AddFunctionLine( ref body, "#endif" );
  2260. IOUtils.AddFunctionLine( ref body, "return rawDepth;" );
  2261. IOUtils.AddFunctionLine( ref body, "#endif // REQUIRE_DEPTH_TEXTURE" );
  2262. IOUtils.AddFunctionLine( ref body, "return 0;" );
  2263. IOUtils.CloseFunctionBody( ref body );
  2264. }
  2265. public static bool GetShaderModelForInterpolatorAmount( int interpAmount, ref string shaderModel )
  2266. {
  2267. for( int i = 0; i < AvailableShaderModels.Length; i++ )
  2268. {
  2269. if( AvailableInterpolators[ AvailableShaderModels[ i ] ] >= interpAmount )
  2270. {
  2271. shaderModel = AvailableShaderModels[ i ];
  2272. return true;
  2273. }
  2274. }
  2275. return false;
  2276. }
  2277. public static string GetSubShaderFrom( string shaderBody )
  2278. {
  2279. Match match = Regex.Match( shaderBody, FetchSubShaderBody, RegexOptions.Singleline );
  2280. if( match.Success && match.Groups.Count > 1 )
  2281. {
  2282. return match.Groups[ 1 ].Value;
  2283. }
  2284. return string.Empty;
  2285. }
  2286. }
  2287. }