Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

916 lines
28 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEditor;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using System.Text.RegularExpressions;
  8. namespace AmplifyShaderEditor
  9. {
  10. [Serializable]
  11. public class TemplateInputData
  12. {
  13. public string PortName;
  14. public WirePortDataType DataType;
  15. public MasterNodePortCategory PortCategory;
  16. public int PortUniqueId;
  17. public int OrderId;
  18. public int TagGlobalStartIdx;
  19. public int TagLocalStartIdx;
  20. public string TagId;
  21. public string DefaultValue;
  22. public string LinkId;
  23. public TemplateInputData( int tagLocalStartIdx, int tagGlobalStartIdx, string tagId, string portName, string defaultValue, WirePortDataType dataType, MasterNodePortCategory portCategory, int portUniqueId, int orderId, string linkId )
  24. {
  25. DefaultValue = defaultValue;
  26. PortName = portName;
  27. DataType = dataType;
  28. PortCategory = portCategory;
  29. PortUniqueId = portUniqueId;
  30. OrderId = orderId;
  31. TagId = tagId;
  32. TagGlobalStartIdx = tagGlobalStartIdx;
  33. TagLocalStartIdx = tagLocalStartIdx;
  34. LinkId = linkId;
  35. }
  36. public TemplateInputData( TemplateInputData other )
  37. {
  38. DefaultValue = other.DefaultValue;
  39. PortName = other.PortName;
  40. DataType = other.DataType;
  41. PortCategory = other.PortCategory;
  42. PortUniqueId = other.PortUniqueId;
  43. OrderId = other.OrderId;
  44. TagId = other.TagId;
  45. TagGlobalStartIdx = other.TagGlobalStartIdx;
  46. LinkId = other.LinkId;
  47. }
  48. }
  49. [Serializable]
  50. public class TemplatePropertyContainer
  51. {
  52. [SerializeField]
  53. private List<TemplateProperty> m_propertyList = new List<TemplateProperty>();
  54. private Dictionary<string, TemplateProperty> m_propertyDict = new Dictionary<string, TemplateProperty>();
  55. public void AddId( TemplateProperty templateProperty )
  56. {
  57. BuildInfo();
  58. m_propertyList.Add( templateProperty );
  59. m_propertyDict.Add( templateProperty.Id, templateProperty );
  60. }
  61. public void AddId( string body, string ID, bool searchIndentation = true )
  62. {
  63. AddId( body, ID, searchIndentation, string.Empty );
  64. }
  65. public void AddId( string body, string ID, bool searchIndentation, string customIndentation )
  66. {
  67. BuildInfo();
  68. int propertyIndex = body.IndexOf( ID );
  69. if( propertyIndex > -1 )
  70. {
  71. if( searchIndentation )
  72. {
  73. int identationIndex = -1;
  74. for( int i = propertyIndex; i >= 0; i-- )
  75. {
  76. if( body[ i ] == TemplatesManager.TemplateNewLine )
  77. {
  78. identationIndex = i + 1;
  79. break;
  80. }
  81. if( i == 0 )
  82. {
  83. identationIndex = 0;
  84. }
  85. }
  86. if( identationIndex > -1 )
  87. {
  88. int length = propertyIndex - identationIndex;
  89. string indentation = ( length > 0 ) ? body.Substring( identationIndex, length ) : string.Empty;
  90. TemplateProperty templateProperty = new TemplateProperty( ID, indentation, false );
  91. m_propertyList.Add( templateProperty );
  92. m_propertyDict.Add( templateProperty.Id, templateProperty );
  93. }
  94. else
  95. {
  96. TemplateProperty templateProperty = new TemplateProperty( ID, string.Empty, false );
  97. m_propertyList.Add( templateProperty );
  98. m_propertyDict.Add( templateProperty.Id, templateProperty );
  99. }
  100. }
  101. else
  102. {
  103. TemplateProperty templateProperty = new TemplateProperty( ID, customIndentation, true );
  104. m_propertyList.Add( templateProperty );
  105. m_propertyDict.Add( templateProperty.Id, templateProperty );
  106. }
  107. }
  108. }
  109. public void AddId( string body, string ID, int propertyIndex, bool searchIndentation )
  110. {
  111. AddId( body, ID, propertyIndex, searchIndentation, string.Empty );
  112. }
  113. public void AddId( string body, string ID, int propertyIndex, bool searchIndentation, string customIndentation )
  114. {
  115. if( body == null || string.IsNullOrEmpty( body ) )
  116. return;
  117. BuildInfo();
  118. if( searchIndentation && propertyIndex > -1 && propertyIndex < body.Length )
  119. {
  120. int indentationIndex = -1;
  121. for( int i = propertyIndex; i > 0; i-- )
  122. {
  123. if( body[ i ] == TemplatesManager.TemplateNewLine )
  124. {
  125. indentationIndex = i + 1;
  126. break;
  127. }
  128. }
  129. if( indentationIndex > -1 )
  130. {
  131. int length = propertyIndex - indentationIndex;
  132. string indentation = ( length > 0 ) ? body.Substring( indentationIndex, length ) : string.Empty;
  133. TemplateProperty templateProperty = new TemplateProperty( ID, indentation, false );
  134. m_propertyList.Add( templateProperty );
  135. m_propertyDict.Add( templateProperty.Id, templateProperty );
  136. }
  137. }
  138. else
  139. {
  140. TemplateProperty templateProperty = new TemplateProperty( ID, customIndentation, true );
  141. m_propertyList.Add( templateProperty );
  142. m_propertyDict.Add( templateProperty.Id, templateProperty );
  143. }
  144. }
  145. public void BuildInfo()
  146. {
  147. if( m_propertyDict == null )
  148. {
  149. m_propertyDict = new Dictionary<string, TemplateProperty>();
  150. }
  151. if( m_propertyList.Count != m_propertyDict.Count )
  152. {
  153. m_propertyDict.Clear();
  154. for( int i = 0; i < m_propertyList.Count; i++ )
  155. {
  156. m_propertyDict.Add( m_propertyList[ i ].Id, m_propertyList[ i ] );
  157. }
  158. }
  159. }
  160. public void ResetTemplateUsageData()
  161. {
  162. BuildInfo();
  163. for( int i = 0; i < m_propertyList.Count; i++ )
  164. {
  165. m_propertyList[ i ].Used = false;
  166. }
  167. }
  168. public void Reset()
  169. {
  170. m_propertyList.Clear();
  171. m_propertyDict.Clear();
  172. }
  173. public void Destroy()
  174. {
  175. m_propertyList.Clear();
  176. m_propertyList = null;
  177. m_propertyDict.Clear();
  178. m_propertyDict = null;
  179. }
  180. public Dictionary<string, TemplateProperty> PropertyDict
  181. {
  182. get
  183. {
  184. BuildInfo();
  185. return m_propertyDict;
  186. }
  187. }
  188. public List<TemplateProperty> PropertyList { get { return m_propertyList; } }
  189. }
  190. [Serializable]
  191. public class TemplateProperty
  192. {
  193. public bool UseIndentationAtStart = false;
  194. public string Indentation;
  195. public bool UseCustomIndentation;
  196. public string Id;
  197. public bool AutoLineFeed;
  198. public bool Used;
  199. public TemplateProperty( string id, string indentation, bool useCustomIndentation )
  200. {
  201. Id = id;
  202. Indentation = indentation;
  203. UseCustomIndentation = useCustomIndentation;
  204. AutoLineFeed = !string.IsNullOrEmpty( indentation );
  205. Used = false;
  206. }
  207. }
  208. [Serializable]
  209. public class TemplateTessVControlTag
  210. {
  211. public string Id;
  212. public int StartIdx;
  213. public TemplateTessVControlTag()
  214. {
  215. StartIdx = -1;
  216. }
  217. public bool IsValid { get { return StartIdx >= 0; } }
  218. }
  219. [Serializable]
  220. public class TemplateTessControlData
  221. {
  222. public string Id;
  223. public int StartIdx;
  224. public string InVarType;
  225. public string InVarName;
  226. public string OutVarType;
  227. public string OutVarName;
  228. public bool IsValid { get { return StartIdx >= 0; } }
  229. public TemplateTessControlData()
  230. {
  231. StartIdx = -1;
  232. }
  233. public TemplateTessControlData( int startIdx, string id, string inVarInfo, string outVarInfo )
  234. {
  235. StartIdx = startIdx;
  236. Id = id;
  237. string[] inVarInfoArr = inVarInfo.Split( IOUtils.VALUE_SEPARATOR );
  238. if( inVarInfoArr.Length > 1 )
  239. {
  240. InVarType = inVarInfoArr[ 1 ];
  241. InVarName = inVarInfoArr[ 0 ];
  242. }
  243. string[] outVarInfoArr = outVarInfo.Split( IOUtils.VALUE_SEPARATOR );
  244. if( outVarInfoArr.Length > 1 )
  245. {
  246. OutVarType = outVarInfoArr[ 1 ];
  247. OutVarName = outVarInfoArr[ 0 ];
  248. }
  249. }
  250. public string[] GenerateControl( Dictionary<TemplateSemantics, TemplateVertexData> vertexData, List<string> inputList )
  251. {
  252. List<string> value = new List<string>();
  253. if( vertexData != null && vertexData.Count > 0 )
  254. {
  255. foreach( var item in vertexData )
  256. {
  257. if( inputList.FindIndex( x => { return x.Contains( item.Value.VarName ); } ) > -1 )
  258. value.Add( string.Format( "{0}.{1} = {2}.{1};", OutVarName, item.Value.VarName, InVarName ) );
  259. }
  260. }
  261. return value.ToArray();
  262. }
  263. }
  264. [Serializable]
  265. public class TemplateTessDomainData
  266. {
  267. public string Id;
  268. public int StartIdx;
  269. public string InVarType;
  270. public string InVarName;
  271. public string OutVarType;
  272. public string OutVarName;
  273. public string BaryVarType;
  274. public string BaryVarName;
  275. public bool IsValid { get { return StartIdx >= 0; } }
  276. public TemplateTessDomainData()
  277. {
  278. StartIdx = -1;
  279. }
  280. public TemplateTessDomainData( int startIdx, string id, string inVarInfo, string outVarInfo, string baryVarInfo )
  281. {
  282. StartIdx = startIdx;
  283. Id = id;
  284. string[] inVarInfoArr = inVarInfo.Split( IOUtils.VALUE_SEPARATOR );
  285. if( inVarInfoArr.Length > 1 )
  286. {
  287. InVarType = inVarInfoArr[ 1 ];
  288. InVarName = inVarInfoArr[ 0 ];
  289. }
  290. string[] outVarInfoArr = outVarInfo.Split( IOUtils.VALUE_SEPARATOR );
  291. if( outVarInfoArr.Length > 1 )
  292. {
  293. OutVarType = outVarInfoArr[ 1 ];
  294. OutVarName = outVarInfoArr[ 0 ];
  295. }
  296. string[] baryVarInfoArr = baryVarInfo.Split( IOUtils.VALUE_SEPARATOR );
  297. if( baryVarInfoArr.Length > 1 )
  298. {
  299. BaryVarType = baryVarInfoArr[ 1 ];
  300. BaryVarName = baryVarInfoArr[ 0 ];
  301. }
  302. }
  303. public string[] GenerateDomain( Dictionary<TemplateSemantics, TemplateVertexData> vertexData, List<string> inputList )
  304. {
  305. List<string> value = new List<string>();
  306. if( vertexData != null && vertexData.Count > 0 )
  307. {
  308. foreach( var item in vertexData )
  309. {
  310. //o.ase_normal = patch[0].ase_normal * bary.x + patch[1].ase_normal * bary.y + patch[2].ase_normal * bary.z;
  311. if( inputList.FindIndex( x => { return x.Contains( item.Value.VarName ); } ) > -1 )
  312. value.Add( string.Format( "{0}.{1} = {2}[0].{1} * {3}.x + {2}[1].{1} * {3}.y + {2}[2].{1} * {3}.z;", OutVarName, item.Value.VarName, InVarName, BaryVarName ) );
  313. }
  314. }
  315. return value.ToArray();
  316. }
  317. }
  318. [Serializable]
  319. public class TemplateFunctionData
  320. {
  321. public int MainBodyLocalIdx;
  322. public string MainBodyName;
  323. public string Id;
  324. public int Position;
  325. public string InVarType;
  326. public string InVarName;
  327. public string OutVarType;
  328. public string OutVarName;
  329. public MasterNodePortCategory Category;
  330. public TemplateFunctionData( int mainBodyLocalIdx, string mainBodyName, string id, int position, string inVarInfo, string outVarInfo, MasterNodePortCategory category )
  331. {
  332. MainBodyLocalIdx = mainBodyLocalIdx;
  333. MainBodyName = mainBodyName;
  334. Id = id;
  335. Position = position;
  336. {
  337. string[] inVarInfoArr = inVarInfo.Split( IOUtils.VALUE_SEPARATOR );
  338. if( inVarInfoArr.Length > 1 )
  339. {
  340. InVarType = inVarInfoArr[ 1 ];
  341. InVarName = inVarInfoArr[ 0 ];
  342. }
  343. }
  344. {
  345. string[] outVarInfoArr = outVarInfo.Split( IOUtils.VALUE_SEPARATOR );
  346. if( outVarInfoArr.Length > 1 )
  347. {
  348. OutVarType = outVarInfoArr[ 1 ];
  349. OutVarName = outVarInfoArr[ 0 ];
  350. }
  351. }
  352. Category = category;
  353. }
  354. }
  355. [Serializable]
  356. public class TemplateTagData
  357. {
  358. public int StartIdx = -1;
  359. public string Id;
  360. public bool SearchIndentation;
  361. public string CustomIndentation;
  362. public TemplateTagData( int startIdx, string id, bool searchIndentation )
  363. {
  364. StartIdx = startIdx;
  365. Id = id;
  366. SearchIndentation = searchIndentation;
  367. CustomIndentation = string.Empty;
  368. }
  369. public TemplateTagData( string id, bool searchIndentation )
  370. {
  371. Id = id;
  372. SearchIndentation = searchIndentation;
  373. CustomIndentation = string.Empty;
  374. }
  375. public TemplateTagData( string id, bool searchIndentation, string customIndentation )
  376. {
  377. Id = id;
  378. SearchIndentation = searchIndentation;
  379. CustomIndentation = customIndentation;
  380. }
  381. public bool IsValid { get { return StartIdx >= 0; } }
  382. }
  383. public enum TemplatePortIds
  384. {
  385. Name = 0,
  386. DataType,
  387. UniqueId,
  388. OrderId,
  389. Link
  390. }
  391. public enum TemplateCommonTagId
  392. {
  393. Property = 0,
  394. Global = 1,
  395. Function = 2,
  396. Tag = 3,
  397. Pragmas = 4,
  398. Pass = 5,
  399. Params_Vert = 6,
  400. Params_Frag = 7
  401. //CullMode = 8,
  402. //BlendMode = 9,
  403. //BlendOp = 10,
  404. //ColorMask = 11,
  405. //StencilOp = 12
  406. }
  407. [Serializable]
  408. public class TemplatesManager : ScriptableObject
  409. {
  410. public static int MPShaderVersion = 14503;
  411. public static readonly string TemplateShaderNameBeginTag = "/*ase_name*/";
  412. public static readonly string TemplateStencilTag = "/*ase_stencil*/\n";
  413. public static readonly string TemplateAllModulesTag = "/*ase_all_modules*/\n";
  414. public static readonly string TemplateMPSubShaderTag = "\\bSubShader\\b\\s*{";
  415. //public static readonly string TemplateMPPassTag = "^\\s*Pass\b\\s*{";//"\\bPass\\b\\s*{";
  416. public static readonly string TemplateMPPassTag = "\\bPass\\b\\s*{";
  417. public static readonly string TemplateLocalVarTag = "/*ase_local_var*/";
  418. public static readonly string TemplateDependenciesListTag = "/*ase_dependencies_list*/";
  419. public static readonly string TemplatePragmaBeforeTag = "/*ase_pragma_before*/";
  420. public static readonly string TemplatePragmaTag = "/*ase_pragma*/";
  421. public static readonly string TemplatePassTag = "/*ase_pass*/";
  422. public static readonly string TemplatePassesEndTag = "/*ase_pass_end*/";
  423. public static readonly string TemplateLODsTag = "/*ase_lod*/";
  424. //public static readonly string TemplatePassTagPattern = @"\s\/\*ase_pass\*\/";
  425. public static readonly string TemplatePassTagPattern = @"\s\/\*ase_pass[:\*]+";
  426. public static readonly string TemplatePropertyTag = "/*ase_props*/";
  427. public static readonly string TemplateGlobalsTag = "/*ase_globals*/";
  428. public static readonly string TemplateSRPBatcherTag = "/*ase_srp_batcher*/\n";
  429. public static readonly string TemplateInterpolatorBeginTag = "/*ase_interp(";
  430. public static readonly string TemplateVertexDataTag = "/*ase_vdata:";
  431. public static readonly string TemplateTessVControlTag = "/*ase_vcontrol*/";
  432. public static readonly string TemplateTessControlCodeArea = "/*ase_control_code:";
  433. public static readonly string TemplateTessDomainCodeArea = "/*ase_domain_code:";
  434. //public static readonly string TemplateExcludeFromGraphTag = "/*ase_hide_pass*/";
  435. public static readonly string TemplateMainPassTag = "/*ase_main_pass*/";
  436. public static readonly string TemplateFunctionsTag = "/*ase_funcs*/\n";
  437. //public static readonly string TemplateTagsTag = "/*ase_tags*/";
  438. //public static readonly string TemplateCullModeTag = "/*ase_cull_mode*/";
  439. //public static readonly string TemplateBlendModeTag = "/*ase_blend_mode*/";
  440. //public static readonly string TemplateBlendOpTag = "/*ase_blend_op*/";
  441. //public static readonly string TemplateColorMaskTag = "/*ase_color_mask*/";
  442. //public static readonly string TemplateStencilOpTag = "/*ase_stencil*/";
  443. public static readonly string TemplateCodeSnippetAttribBegin = "#CODE_SNIPPET_ATTRIBS_BEGIN#";
  444. public static readonly string TemplateCodeSnippetAttribEnd = "#CODE_SNIPPET_ATTRIBS_END#\n";
  445. public static readonly string TemplateCodeSnippetEnd = "#CODE_SNIPPET_END#\n";
  446. public static readonly char TemplateNewLine = '\n';
  447. // INPUTS AREA
  448. public static readonly string TemplateInputsVertBeginTag = "/*ase_vert_out:";
  449. public static readonly string TemplateInputsFragBeginTag = "/*ase_frag_out:";
  450. public static readonly string TemplateInputsVertParamsTag = "/*ase_vert_input*/";
  451. public static readonly string TemplateInputsFragParamsTag = "/*ase_frag_input*/";
  452. // CODE AREA
  453. public static readonly string TemplateVertexCodeBeginArea = "/*ase_vert_code:";
  454. public static readonly string TemplateFragmentCodeBeginArea = "/*ase_frag_code:";
  455. public static readonly string TemplateEndOfLine = "*/\n";
  456. public static readonly string TemplateEndSectionTag = "*/";
  457. public static readonly string TemplateFullEndTag = "/*end*/";
  458. public static readonly string NameFormatter = "\"{0}\"";
  459. public static readonly TemplateTagData[] CommonTags = { new TemplateTagData( TemplatePropertyTag,true),
  460. new TemplateTagData( TemplateGlobalsTag,true),
  461. new TemplateTagData( TemplateSRPBatcherTag,true),
  462. new TemplateTagData( TemplateFunctionsTag,true),
  463. //new TemplateTagData( TemplateTagsTag,false," "),
  464. new TemplateTagData( TemplatePragmaBeforeTag,true),
  465. new TemplateTagData( TemplatePragmaTag,true),
  466. new TemplateTagData( TemplatePassTag,true),
  467. new TemplateTagData( TemplateInputsVertParamsTag,false),
  468. new TemplateTagData( TemplateInputsFragParamsTag,false),
  469. new TemplateTagData( TemplateLODsTag,true)
  470. //new TemplateTagData( TemplateCullModeTag,false),
  471. //new TemplateTagData( TemplateBlendModeTag,false),
  472. //new TemplateTagData( TemplateBlendOpTag,false),
  473. //new TemplateTagData( TemplateColorMaskTag,false),
  474. //new TemplateTagData( TemplateStencilOpTag,true),
  475. };
  476. public static string LightweigthPBRGUID = "1976390536c6c564abb90fe41f6ee334";
  477. public static string LightweigthUnlitGUID = "e2514bdcf5e5399499a9eb24d175b9db";
  478. public static string UniversalPBRGUID = "94348b07e5e8bab40bd6c8a1e3df54cd";
  479. public static string UniversalUnlitGUID = "2992e84f91cbeb14eab234972e07ea9d";
  480. public static string HDNewLitGUID = "53b46d85872c5b24c8f4f0a1c3fe4c87";
  481. public static string HDNewPBRGUID = "41e04be03f2c20941bc749271be1c937";
  482. public static string HDNewUnlitGUID = "7f5cb9c3ea6481f469fdd856555439ef";
  483. public static string HDLitGUID = "091c43ba8bd92c9459798d59b089ce4e";
  484. public static string HDPBRGUID = "bb308bce79762c34e823049efce65141";
  485. public static string HDUnlitGUID = "dfe2f27ac20b08c469b2f95c236be0c3";
  486. public static Dictionary<string, string> OfficialTemplates = new Dictionary<string, string>()
  487. {
  488. { "0770190933193b94aaa3065e307002fa","Legacy/Unlit"},
  489. { "32139be9c1eb75640a847f011acf3bcf","Legacy/Post-Processing Stack"},
  490. { "6ce779933eb99f049b78d6163735e06f","Legacy/Custom RT Init"},
  491. { "32120270d1b3a8746af2aca8bc749736","Legacy/Custom RT Update"},
  492. { LightweigthPBRGUID,"LW/PBR"},
  493. { LightweigthUnlitGUID,"LW/Unlit"},
  494. { UniversalPBRGUID,"Universal/PBR"},
  495. { UniversalUnlitGUID,"Universal/Unlit"},
  496. { "53b46d85872c5b24c8f4f0a1c3fe4c87","HD/Lit"},
  497. { HDLitGUID,"Deprecated/HD/Lit"},
  498. { HDPBRGUID,"Deprecated/HD/PBR"},
  499. { HDUnlitGUID,"Deprecated/HD/Unlit"},
  500. { "c71b220b631b6344493ea3cf87110c93","Legacy/Post Process" },
  501. { "6e114a916ca3e4b4bb51972669d463bf","Deprecated/Legacy/Default Unlit" },
  502. { "5056123faa0c79b47ab6ad7e8bf059a4","Legacy/Default UI" },
  503. { "899e609c083c74c4ca567477c39edef0","Legacy/Unlit Lightmap" },
  504. { "0f8ba0101102bb14ebf021ddadce9b49","Legacy/Default Sprites" },
  505. { "0b6a9f8b4f707c74ca64c0be8e590de0","Legacy/Particles Alpha Blended" },
  506. { "e1de45c0d41f68c41b2cc20c8b9c05ef","Legacy/Multi Pass Unlit" }
  507. };
  508. public static readonly string TemplateMenuItemsFileGUID = "da0b931bd234a1e43b65f684d4b59bfb";
  509. private Dictionary<string, TemplateDataParent> m_availableTemplates = new Dictionary<string, TemplateDataParent>();
  510. [SerializeField]
  511. private List<TemplateDataParent> m_sortedTemplates = new List<TemplateDataParent>();
  512. [SerializeField]
  513. public string[] AvailableTemplateNames;
  514. [SerializeField]
  515. public bool Initialized = false;
  516. private Dictionary<string, bool> m_optionsInitialSetup = new Dictionary<string, bool>();
  517. public static string CurrTemplateGUIDLoaded = string.Empty;
  518. public static bool IsTestTemplate { get { return CurrTemplateGUIDLoaded.Equals( "a95a019bbc760714bb8228af04c291d1" ); } }
  519. public static bool ShowDebugMessages = false;
  520. public void RefreshAvailableTemplates()
  521. {
  522. if( m_availableTemplates.Count != m_sortedTemplates.Count )
  523. {
  524. m_availableTemplates.Clear();
  525. int count = m_sortedTemplates.Count;
  526. for( int i = 0; i < count; i++ )
  527. {
  528. m_availableTemplates.Add( m_sortedTemplates[ i ].GUID, m_sortedTemplates[ i ] );
  529. }
  530. }
  531. }
  532. public void Init()
  533. {
  534. if( !Initialized )
  535. {
  536. if( ShowDebugMessages )
  537. Debug.Log( "Initialize" );
  538. string templateMenuItems = IOUtils.LoadTextFileFromDisk( AssetDatabase.GUIDToAssetPath( TemplateMenuItemsFileGUID ) );
  539. bool refreshTemplateMenuItems = false;
  540. foreach( KeyValuePair<string, string> kvp in OfficialTemplates )
  541. {
  542. if( !string.IsNullOrEmpty( AssetDatabase.GUIDToAssetPath( kvp.Key ) ) )
  543. {
  544. TemplateMultiPass template = ScriptableObject.CreateInstance<TemplateMultiPass>();
  545. template.Init( kvp.Value, kvp.Key, false );
  546. AddTemplate( template );
  547. if( !refreshTemplateMenuItems && templateMenuItems.IndexOf( kvp.Value ) < 0 )
  548. refreshTemplateMenuItems = true;
  549. }
  550. }
  551. // Search for other possible templates on the project
  552. string[] allShaders = AssetDatabase.FindAssets( "t:shader" );
  553. for( int i = 0; i < allShaders.Length; i++ )
  554. {
  555. if( !m_availableTemplates.ContainsKey( allShaders[ i ] ) )
  556. {
  557. CheckAndLoadTemplate( allShaders[ i ] );
  558. }
  559. }
  560. // TODO: Sort list alphabeticaly
  561. AvailableTemplateNames = new string[ m_sortedTemplates.Count + 1 ];
  562. AvailableTemplateNames[ 0 ] = "Custom";
  563. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  564. {
  565. m_sortedTemplates[ i ].OrderId = i;
  566. AvailableTemplateNames[ i + 1 ] = m_sortedTemplates[ i ].Name;
  567. }
  568. if( refreshTemplateMenuItems )
  569. CreateTemplateMenuItems();
  570. Initialized = true;
  571. }
  572. }
  573. //[MenuItem( "Window/Amplify Shader Editor/Create Menu Items", false, 1000 )]
  574. //public static void ForceCreateTemplateMenuItems()
  575. //{
  576. // UIUtils.CurrentWindow.TemplatesManagerInstance.CreateTemplateMenuItems();
  577. //}
  578. public void CreateTemplateMenuItems()
  579. {
  580. if( m_sortedTemplates == null || m_sortedTemplates.Count == 0 )
  581. return;
  582. // change names for duplicates
  583. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  584. {
  585. for( int j = 0; j < i; j++ )
  586. {
  587. if( m_sortedTemplates[ i ].Name == m_sortedTemplates[ j ].Name )
  588. {
  589. var match = Regex.Match( m_sortedTemplates[ i ].Name, @".+(\d+)" );
  590. if( match.Success )
  591. {
  592. string strNumber = match.Groups[ 1 ].Value;
  593. int number = int.Parse( strNumber ) + 1;
  594. string firstPart = m_sortedTemplates[ i ].Name.Substring( 0, match.Groups[ 1 ].Index );
  595. string secondPart = m_sortedTemplates[ i ].Name.Substring( match.Groups[ 1 ].Index + strNumber.Length );
  596. m_sortedTemplates[ i ].Name = firstPart + number + secondPart;
  597. }
  598. else
  599. {
  600. m_sortedTemplates[ i ].Name += " 1";
  601. }
  602. }
  603. }
  604. }
  605. System.Text.StringBuilder fileContents = new System.Text.StringBuilder();
  606. fileContents.Append( "// Amplify Shader Editor - Visual Shader Editing Tool\n" );
  607. fileContents.Append( "// Copyright (c) Amplify Creations, Lda <info@amplify.pt>\n" );
  608. fileContents.Append( "using UnityEditor;\n" );
  609. fileContents.Append( "\n" );
  610. fileContents.Append( "namespace AmplifyShaderEditor\n" );
  611. fileContents.Append( "{\n" );
  612. fileContents.Append( "\tpublic class TemplateMenuItems\n" );
  613. fileContents.Append( "\t{\n" );
  614. int fixedPriority = 85;
  615. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  616. {
  617. fileContents.AppendFormat( "\t\t[MenuItem( \"Assets/Create/Amplify Shader/{0}\", false, {1} )]\n", m_sortedTemplates[ i ].Name, fixedPriority );
  618. string itemName = UIUtils.RemoveInvalidCharacters( m_sortedTemplates[ i ].Name );
  619. fileContents.AppendFormat( "\t\tpublic static void ApplyTemplate{0}()\n", itemName/*i*/ );
  620. fileContents.Append( "\t\t{\n" );
  621. //fileContents.AppendFormat( "\t\t\tAmplifyShaderEditorWindow.CreateNewTemplateShader( \"{0}\" );\n", m_sortedTemplates[ i ].GUID );
  622. fileContents.AppendFormat( "\t\t\tAmplifyShaderEditorWindow.CreateConfirmationTemplateShader( \"{0}\" );\n", m_sortedTemplates[ i ].GUID );
  623. fileContents.Append( "\t\t}\n" );
  624. }
  625. fileContents.Append( "\t}\n" );
  626. fileContents.Append( "}\n" );
  627. string filePath = AssetDatabase.GUIDToAssetPath( TemplateMenuItemsFileGUID );
  628. IOUtils.SaveTextfileToDisk( fileContents.ToString(), filePath, false );
  629. m_filepath = filePath;
  630. //AssetDatabase.ImportAsset( filePath );
  631. }
  632. string m_filepath = string.Empty;
  633. public void ReimportMenuItems()
  634. {
  635. if( !string.IsNullOrEmpty( m_filepath ) )
  636. {
  637. AssetDatabase.ImportAsset( m_filepath );
  638. m_filepath = string.Empty;
  639. }
  640. }
  641. public int GetIdForTemplate( TemplateData templateData )
  642. {
  643. if( templateData == null )
  644. return -1;
  645. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  646. {
  647. if( m_sortedTemplates[ i ].GUID.Equals( templateData.GUID ) )
  648. return m_sortedTemplates[ i ].OrderId;
  649. }
  650. return -1;
  651. }
  652. public void AddTemplate( TemplateDataParent templateData )
  653. {
  654. if( templateData == null || !templateData.IsValid )
  655. return;
  656. RefreshAvailableTemplates();
  657. if( !m_availableTemplates.ContainsKey( templateData.GUID ) )
  658. {
  659. m_sortedTemplates.Add( templateData );
  660. m_availableTemplates.Add( templateData.GUID, templateData );
  661. }
  662. }
  663. public void RemoveTemplate( string guid )
  664. {
  665. TemplateDataParent templateData = GetTemplate( guid );
  666. if( templateData != null )
  667. {
  668. RemoveTemplate( templateData );
  669. }
  670. }
  671. public void RemoveTemplate( TemplateDataParent templateData )
  672. {
  673. RefreshAvailableTemplates();
  674. if( m_availableTemplates != null )
  675. m_availableTemplates.Remove( templateData.GUID );
  676. m_sortedTemplates.Remove( templateData );
  677. templateData.Destroy();
  678. }
  679. public void Destroy()
  680. {
  681. if( TemplatesManager.ShowDebugMessages )
  682. Debug.Log( "Destroy Manager" );
  683. if( m_availableTemplates != null )
  684. {
  685. foreach( KeyValuePair<string, TemplateDataParent> kvp in m_availableTemplates )
  686. {
  687. kvp.Value.Destroy();
  688. }
  689. m_availableTemplates.Clear();
  690. m_availableTemplates = null;
  691. }
  692. int count = m_sortedTemplates.Count;
  693. for( int i = 0; i < count; i++ )
  694. {
  695. ScriptableObject.DestroyImmediate( m_sortedTemplates[ i ] );
  696. }
  697. m_sortedTemplates.Clear();
  698. m_sortedTemplates = null;
  699. AvailableTemplateNames = null;
  700. Initialized = false;
  701. }
  702. public TemplateDataParent GetTemplate( int id )
  703. {
  704. if( id < m_sortedTemplates.Count )
  705. return m_sortedTemplates[ id ];
  706. return null;
  707. }
  708. public TemplateDataParent GetTemplate( string guid )
  709. {
  710. RefreshAvailableTemplates();
  711. if( m_availableTemplates == null && m_sortedTemplates != null )
  712. {
  713. m_availableTemplates = new Dictionary<string, TemplateDataParent>();
  714. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  715. {
  716. m_availableTemplates.Add( m_sortedTemplates[ i ].GUID, m_sortedTemplates[ i ] );
  717. }
  718. }
  719. if( m_availableTemplates.ContainsKey( guid ) )
  720. return m_availableTemplates[ guid ];
  721. return null;
  722. }
  723. public TemplateDataParent GetTemplateByName( string name )
  724. {
  725. RefreshAvailableTemplates();
  726. if( m_availableTemplates == null && m_sortedTemplates != null )
  727. {
  728. m_availableTemplates = new Dictionary<string, TemplateDataParent>();
  729. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  730. {
  731. m_availableTemplates.Add( m_sortedTemplates[ i ].GUID, m_sortedTemplates[ i ] );
  732. }
  733. }
  734. foreach( KeyValuePair<string, TemplateDataParent> kvp in m_availableTemplates )
  735. {
  736. if( kvp.Value.DefaultShaderName.Equals( name ) )
  737. {
  738. return kvp.Value;
  739. }
  740. }
  741. return null;
  742. }
  743. public TemplateDataParent CheckAndLoadTemplate( string guid )
  744. {
  745. TemplateDataParent templateData = GetTemplate( guid );
  746. if( templateData == null )
  747. {
  748. string datapath = AssetDatabase.GUIDToAssetPath( guid );
  749. string body = IOUtils.LoadTextFileFromDisk( datapath );
  750. if( body.IndexOf( TemplatesManager.TemplateShaderNameBeginTag ) > -1 )
  751. {
  752. templateData = ScriptableObject.CreateInstance<TemplateMultiPass>();
  753. templateData.Init( string.Empty, guid, true );
  754. if( templateData.IsValid )
  755. {
  756. AddTemplate( templateData );
  757. return templateData;
  758. }
  759. }
  760. }
  761. return null;
  762. }
  763. private void OnEnable()
  764. {
  765. if( !Initialized )
  766. {
  767. Init();
  768. }
  769. else
  770. {
  771. RefreshAvailableTemplates();
  772. }
  773. hideFlags = HideFlags.HideAndDontSave;
  774. if( ShowDebugMessages )
  775. Debug.Log( "On Enable Manager: " + this.GetInstanceID() );
  776. }
  777. public void ResetOptionsSetupData()
  778. {
  779. if( ShowDebugMessages )
  780. Debug.Log( "Reseting options setup data" );
  781. m_optionsInitialSetup.Clear();
  782. }
  783. public bool SetOptionsValue( string optionId, bool value )
  784. {
  785. if( m_optionsInitialSetup.ContainsKey( optionId ) )
  786. {
  787. m_optionsInitialSetup[ optionId ] = m_optionsInitialSetup[ optionId ] || value;
  788. }
  789. else
  790. {
  791. m_optionsInitialSetup.Add( optionId, value );
  792. }
  793. return m_optionsInitialSetup[ optionId ];
  794. }
  795. public int TemplateCount { get { return m_sortedTemplates.Count; } }
  796. }
  797. }