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.

236 lines
5.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 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. finalShaderBody = finalShaderBody.Replace( m_registeredTags[ i ].Tag, m_registeredTags[ i ].Replacement );
  167. m_registeredTags[ i ].Replacement = m_registeredTags[ i ].Output;
  168. }
  169. //finalShaderBody = finalShaderBody.Replace( TemplatesManager.TemplateExcludeFromGraphTag, string.Empty );
  170. //finalShaderBody = finalShaderBody.Replace( TemplatesManager.TemplateMainPassTag, string.Empty );
  171. return finalShaderBody;
  172. }
  173. public void ResetRegistersState()
  174. {
  175. int count = m_registeredIds.Count;
  176. for( int i = 0; i < count; i++ )
  177. {
  178. m_registeredIds[ i ].Reset();
  179. }
  180. }
  181. public void Reset()
  182. {
  183. m_registeredIds.Clear();
  184. if( m_registeredIdsDict == null )
  185. {
  186. m_registeredIdsDict = new Dictionary<string, TemplateId>();
  187. }
  188. else
  189. {
  190. m_registeredIdsDict.Clear();
  191. }
  192. }
  193. public string ShaderBody
  194. {
  195. get { return m_shaderBody; }
  196. set { m_shaderBody = value; }
  197. }
  198. public List<TemplateTag> RegisteredTags { get { return m_registeredTags; } set { m_registeredTags = value; } }
  199. }
  200. }