TemplateIdManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. public class TemplatePassId
  10. {
  11. public string PassId;
  12. public bool RemoveFromShader;
  13. }
  14. [Serializable]
  15. public class TemplateTag
  16. {
  17. public string Tag = string.Empty;
  18. public string Replacement = string.Empty;
  19. public string Output = string.Empty;
  20. public TemplateTag( string tag, string replacement = null )
  21. {
  22. Tag = tag;
  23. if( replacement != null )
  24. {
  25. Replacement = replacement;
  26. Output = replacement;
  27. }
  28. }
  29. }
  30. [Serializable]
  31. public class TemplateId
  32. {
  33. public int StartIdx = -1;
  34. public string UniqueID;
  35. public string Tag;
  36. public string ReplacementText;
  37. public bool IsReplaced = false;
  38. public bool EmptyReplacer = false;
  39. public TemplateId( int bodyIdx, string uniqueID, string tag, bool emptyReplacer = false )
  40. {
  41. StartIdx = bodyIdx;
  42. UniqueID = uniqueID;
  43. Tag = tag;
  44. EmptyReplacer = emptyReplacer;
  45. ReplacementText = emptyReplacer ? string.Empty : tag;
  46. }
  47. public void SetReplacementText( string replacementText )
  48. {
  49. ReplacementText = replacementText;
  50. IsReplaced = true;
  51. }
  52. public void Reset()
  53. {
  54. ReplacementText = EmptyReplacer?string.Empty:Tag;
  55. IsReplaced = false;
  56. }
  57. }
  58. [Serializable]
  59. public class TemplateIdManager
  60. {
  61. [SerializeField]
  62. private bool m_isSorted = false;
  63. [SerializeField]
  64. private string m_shaderBody;
  65. [SerializeField]
  66. private List<TemplateId> m_registeredIds = new List<TemplateId>();
  67. [SerializeField]
  68. private List<TemplateTag> m_registeredTags = new List<TemplateTag>();
  69. [SerializeField]
  70. private List<TemplatePassId> m_registeredPassIds = new List<TemplatePassId>();
  71. private Dictionary<string, TemplateId> m_registeredIdsDict = new Dictionary<string, TemplateId>();
  72. public TemplateIdManager( string shaderBody )
  73. {
  74. m_shaderBody = shaderBody;
  75. }
  76. public void Destroy()
  77. {
  78. m_registeredPassIds.Clear();
  79. m_registeredPassIds = null;
  80. m_registeredTags.Clear();
  81. m_registeredTags = null;
  82. m_registeredIds.Clear();
  83. m_registeredIds = null;
  84. if( m_registeredIdsDict != null )
  85. {
  86. m_registeredIdsDict.Clear();
  87. m_registeredIdsDict = null;
  88. }
  89. }
  90. void RefreshIds()
  91. {
  92. if( m_registeredIdsDict == null )
  93. {
  94. m_registeredIdsDict = new Dictionary<string, TemplateId>();
  95. }
  96. if( m_registeredIdsDict.Count != m_registeredIds.Count )
  97. {
  98. m_registeredIdsDict.Clear();
  99. int count = m_registeredIds.Count;
  100. for( int i = 0; i < count; i++ )
  101. {
  102. m_registeredIdsDict.Add( m_registeredIds[ i ].UniqueID, m_registeredIds[ i ] );
  103. }
  104. }
  105. }
  106. public void RegisterId( int bodyIdx, string uniqueID, string tag, bool emptyReplacer = false )
  107. {
  108. if( bodyIdx < 0 )
  109. return;
  110. RefreshIds();
  111. TemplateId templateId = new TemplateId( bodyIdx, uniqueID, tag, emptyReplacer );
  112. m_registeredIds.Add( templateId );
  113. m_registeredIdsDict.Add( uniqueID, templateId );
  114. }
  115. public void RegisterTag( string tag, string replacement = null )
  116. {
  117. m_registeredTags.Add( new TemplateTag( tag, replacement ) );
  118. }
  119. public void RegisterPassId( string passId )
  120. {
  121. m_registeredPassIds.Add( new TemplatePassId() { PassId = passId, RemoveFromShader = false } );
  122. }
  123. public void SetPassIdUsage( int idx , bool removeFromShader )
  124. {
  125. m_registeredPassIds[ idx ].RemoveFromShader = removeFromShader;
  126. }
  127. public void SetReplacementText( string uniqueId, string replacementText )
  128. {
  129. RefreshIds();
  130. if( m_registeredIdsDict.ContainsKey( uniqueId ) && m_registeredIdsDict[ uniqueId ].StartIdx >= 0 )
  131. m_registeredIdsDict[ uniqueId ].SetReplacementText( replacementText );
  132. }
  133. public string BuildShader()
  134. {
  135. if( !m_isSorted )
  136. {
  137. m_registeredIds.Sort( ( x, y ) => { return x.StartIdx.CompareTo( y.StartIdx ); } );
  138. }
  139. int idCount = m_registeredIds.Count;
  140. int offset = 0;
  141. string finalShaderBody = m_shaderBody;
  142. for( int i = 0; i < idCount; i++ )
  143. {
  144. if( m_registeredIds[ i ].StartIdx >= 0 && m_registeredIds[ i ].IsReplaced )
  145. {
  146. finalShaderBody = finalShaderBody.ReplaceAt( m_registeredIds[ i ].Tag, m_registeredIds[ i ].ReplacementText, offset + m_registeredIds[ i ].StartIdx );
  147. offset += ( m_registeredIds[ i ].ReplacementText.Length - m_registeredIds[ i ].Tag.Length );
  148. }
  149. }
  150. int count = m_registeredPassIds.Count;
  151. for( int i = 0; i < count; i++ )
  152. {
  153. if( m_registeredPassIds[ i ].RemoveFromShader )
  154. finalShaderBody = finalShaderBody.Replace( m_registeredPassIds[ i ].PassId, string.Empty );
  155. }
  156. for( int i = 0; i < idCount; i++ )
  157. {
  158. if( !m_registeredIds[ i ].IsReplaced && !m_registeredIds[ i ].Tag.Equals( m_registeredIds[ i ].ReplacementText ) )
  159. {
  160. finalShaderBody = finalShaderBody.Replace( m_registeredIds[ i ].Tag, m_registeredIds[ i ].ReplacementText );
  161. }
  162. }
  163. count = m_registeredTags.Count;
  164. for( int i = 0; i < count; i++ )
  165. {
  166. TemplateTag tag = m_registeredTags[ i ];
  167. finalShaderBody = finalShaderBody.Replace( tag.Tag, tag.Replacement );
  168. tag.Replacement = tag.Output;
  169. }
  170. //finalShaderBody = finalShaderBody.Replace( TemplatesManager.TemplateExcludeFromGraphTag, string.Empty );
  171. //finalShaderBody = finalShaderBody.Replace( TemplatesManager.TemplateMainPassTag, string.Empty );
  172. return finalShaderBody;
  173. }
  174. public void ResetRegistersState()
  175. {
  176. int count = m_registeredIds.Count;
  177. for( int i = 0; i < count; i++ )
  178. {
  179. m_registeredIds[ i ].Reset();
  180. }
  181. }
  182. public void Reset()
  183. {
  184. m_registeredIds.Clear();
  185. if( m_registeredIdsDict == null )
  186. {
  187. m_registeredIdsDict = new Dictionary<string, TemplateId>();
  188. }
  189. else
  190. {
  191. m_registeredIdsDict.Clear();
  192. }
  193. }
  194. public string ShaderBody
  195. {
  196. get { return m_shaderBody; }
  197. set { m_shaderBody = value; }
  198. }
  199. public List<TemplateTag> RegisteredTags { get { return m_registeredTags; } set { m_registeredTags = value; } }
  200. }
  201. }