TemplateHelperFunctions.cs 94 KB

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