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.

198 lines
4.9 KiB

  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 TemplateTag
  10. {
  11. public string Tag = string.Empty;
  12. public string Replacement = string.Empty;
  13. public TemplateTag( string tag, string replacement = null )
  14. {
  15. Tag = tag;
  16. if( replacement != null )
  17. Replacement = replacement;
  18. }
  19. }
  20. [Serializable]
  21. public class TemplateId
  22. {
  23. public int StartIdx = -1;
  24. public string UniqueID;
  25. public string Tag;
  26. public string ReplacementText;
  27. public bool IsReplaced = false;
  28. public bool EmptyReplacer = false;
  29. public TemplateId( int bodyIdx, string uniqueID, string tag, bool emptyReplacer = false )
  30. {
  31. StartIdx = bodyIdx;
  32. UniqueID = uniqueID;
  33. Tag = tag;
  34. EmptyReplacer = emptyReplacer;
  35. ReplacementText = emptyReplacer ? string.Empty : tag;
  36. }
  37. public void SetReplacementText( string replacementText )
  38. {
  39. ReplacementText = replacementText;
  40. IsReplaced = true;
  41. }
  42. public void Reset()
  43. {
  44. ReplacementText = EmptyReplacer?string.Empty:Tag;
  45. IsReplaced = false;
  46. }
  47. }
  48. [Serializable]
  49. public class TemplateIdManager
  50. {
  51. [SerializeField]
  52. private bool m_isSorted = false;
  53. [SerializeField]
  54. private string m_shaderBody;
  55. [SerializeField]
  56. private List<TemplateId> m_registeredIds = new List<TemplateId>();
  57. [SerializeField]
  58. private List<TemplateTag> m_registeredTags = new List<TemplateTag>();
  59. private Dictionary<string, TemplateId> m_registeredIdsDict = new Dictionary<string, TemplateId>();
  60. public TemplateIdManager( string shaderBody )
  61. {
  62. m_shaderBody = shaderBody;
  63. }
  64. public void Destroy()
  65. {
  66. m_registeredTags.Clear();
  67. m_registeredTags = null;
  68. m_registeredIds.Clear();
  69. m_registeredIds = null;
  70. if( m_registeredIdsDict != null )
  71. {
  72. m_registeredIdsDict.Clear();
  73. m_registeredIdsDict = null;
  74. }
  75. }
  76. void RefreshIds()
  77. {
  78. if( m_registeredIdsDict == null )
  79. {
  80. m_registeredIdsDict = new Dictionary<string, TemplateId>();
  81. }
  82. if( m_registeredIdsDict.Count != m_registeredIds.Count )
  83. {
  84. m_registeredIdsDict.Clear();
  85. int count = m_registeredIds.Count;
  86. for( int i = 0; i < count; i++ )
  87. {
  88. m_registeredIdsDict.Add( m_registeredIds[ i ].UniqueID, m_registeredIds[ i ] );
  89. }
  90. }
  91. }
  92. public void RegisterId( int bodyIdx, string uniqueID, string tag, bool emptyReplacer = false )
  93. {
  94. if( bodyIdx < 0 )
  95. return;
  96. RefreshIds();
  97. TemplateId templateId = new TemplateId( bodyIdx, uniqueID, tag, emptyReplacer );
  98. m_registeredIds.Add( templateId );
  99. m_registeredIdsDict.Add( uniqueID, templateId );
  100. }
  101. public void RegisterTag( string tag, string replacement = null )
  102. {
  103. m_registeredTags.Add( new TemplateTag( tag, replacement ) );
  104. }
  105. public void SetReplacementText( string uniqueId, string replacementText )
  106. {
  107. RefreshIds();
  108. if( m_registeredIdsDict.ContainsKey( uniqueId ) && m_registeredIdsDict[ uniqueId ].StartIdx >= 0 )
  109. m_registeredIdsDict[ uniqueId ].SetReplacementText( replacementText );
  110. }
  111. public string BuildShader()
  112. {
  113. if( !m_isSorted )
  114. {
  115. m_registeredIds.Sort( ( x, y ) => { return x.StartIdx.CompareTo( y.StartIdx ); } );
  116. }
  117. int idCount = m_registeredIds.Count;
  118. int offset = 0;
  119. string finalShaderBody = m_shaderBody;
  120. for( int i = 0; i < idCount; i++ )
  121. {
  122. if( m_registeredIds[ i ].StartIdx >= 0 && m_registeredIds[ i ].IsReplaced )
  123. {
  124. finalShaderBody = finalShaderBody.ReplaceAt( m_registeredIds[ i ].Tag, m_registeredIds[ i ].ReplacementText, offset + m_registeredIds[ i ].StartIdx );
  125. offset += ( m_registeredIds[ i ].ReplacementText.Length - m_registeredIds[ i ].Tag.Length );
  126. }
  127. }
  128. for( int i = 0; i < idCount; i++ )
  129. {
  130. if( !m_registeredIds[ i ].IsReplaced && !m_registeredIds[ i ].Tag.Equals( m_registeredIds[ i ].ReplacementText ) )
  131. {
  132. finalShaderBody = finalShaderBody.Replace( m_registeredIds[ i ].Tag, m_registeredIds[ i ].ReplacementText );
  133. }
  134. }
  135. int tagCount = m_registeredTags.Count;
  136. for( int i = 0; i < tagCount; i++ )
  137. {
  138. finalShaderBody = finalShaderBody.Replace( m_registeredTags[ i ].Tag, m_registeredTags[ i ].Replacement );
  139. }
  140. //finalShaderBody = finalShaderBody.Replace( TemplatesManager.TemplateExcludeFromGraphTag, string.Empty );
  141. finalShaderBody = finalShaderBody.Replace( TemplatesManager.TemplateMainPassTag, string.Empty );
  142. return finalShaderBody;
  143. }
  144. public void ResetRegistersState()
  145. {
  146. int count = m_registeredIds.Count;
  147. for( int i = 0; i < count; i++ )
  148. {
  149. m_registeredIds[ i ].Reset();
  150. }
  151. }
  152. public void Reset()
  153. {
  154. m_registeredIds.Clear();
  155. if( m_registeredIdsDict == null )
  156. {
  157. m_registeredIdsDict = new Dictionary<string, TemplateId>();
  158. }
  159. else
  160. {
  161. m_registeredIdsDict.Clear();
  162. }
  163. }
  164. public string ShaderBody
  165. {
  166. get { return m_shaderBody; }
  167. set { m_shaderBody = value; }
  168. }
  169. }
  170. }