AmplifyShaderFunction.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. public class AmplifyShaderFunction : ScriptableObject
  10. {
  11. [SerializeField]
  12. private string m_functionInfo = string.Empty;
  13. public string FunctionInfo
  14. {
  15. get { return m_functionInfo; }
  16. set { m_functionInfo = value; }
  17. }
  18. [SerializeField]
  19. private string m_functionName = string.Empty;
  20. public string FunctionName
  21. {
  22. get { if( m_functionName.Length == 0 ) return name; else return m_functionName; }
  23. set { m_functionName = value; }
  24. }
  25. [SerializeField]
  26. [TextArea( 5, 15 )]
  27. private string m_description = string.Empty;
  28. public string Description
  29. {
  30. get { return m_description; }
  31. set { m_description = value; }
  32. }
  33. [SerializeField]
  34. private AdditionalIncludesHelper m_additionalIncludes = new AdditionalIncludesHelper();
  35. //public AdditionalIncludesHelper AdditionalIncludes
  36. //{
  37. // get { return m_additionalIncludes; }
  38. // set { m_additionalIncludes = value; }
  39. //}
  40. [SerializeField]
  41. private AdditionalPragmasHelper m_additionalPragmas = new AdditionalPragmasHelper();
  42. //public AdditionalPragmasHelper AdditionalPragmas
  43. //{
  44. // get { return m_additionalPragmas; }
  45. // set { m_additionalPragmas = value; }
  46. //}
  47. [SerializeField]
  48. private TemplateAdditionalDirectivesHelper m_additionalDirectives = new TemplateAdditionalDirectivesHelper( " Additional Directives" );
  49. public TemplateAdditionalDirectivesHelper AdditionalDirectives
  50. {
  51. get { return m_additionalDirectives; }
  52. set { m_additionalDirectives = value; }
  53. }
  54. [SerializeField]
  55. private FunctionNodeCategories m_nodeCategory = FunctionNodeCategories.Functions;
  56. public FunctionNodeCategories NodeCategory
  57. {
  58. get { return m_nodeCategory; }
  59. set { m_nodeCategory = value; }
  60. }
  61. public enum HeaderStyle
  62. {
  63. Default,
  64. Category,
  65. Custom
  66. }
  67. [SerializeField]
  68. private HeaderStyle m_headerStyle = HeaderStyle.Default;
  69. public HeaderStyle Header
  70. {
  71. get { return m_headerStyle; }
  72. set { m_headerStyle = value; }
  73. }
  74. [SerializeField]
  75. private Color m_headerColor = new Color( 1.00f, 0.4f, 0.0f, 1.0f );
  76. public Color HeaderColor
  77. {
  78. get { return m_headerColor; }
  79. set { m_headerColor = value; }
  80. }
  81. [SerializeField]
  82. private string m_customNodeCategory = string.Empty;
  83. public string CustomNodeCategory
  84. {
  85. get
  86. {
  87. if( m_nodeCategory == FunctionNodeCategories.Custom )
  88. {
  89. if( string.IsNullOrEmpty( m_customNodeCategory ) )
  90. return "Functions";
  91. else
  92. return m_customNodeCategory;
  93. }
  94. else
  95. {
  96. return UIUtils.CategoryPresets[ (int)m_nodeCategory ];
  97. //return new SerializedObject( this ).FindProperty( "m_nodeCategory" ).enumDisplayNames[ (int)m_nodeCategory ];
  98. }
  99. }
  100. }
  101. [SerializeField]
  102. private PreviewLocation m_previewPosition = PreviewLocation.Auto;
  103. public PreviewLocation PreviewPosition
  104. {
  105. get { return m_previewPosition; }
  106. set { m_previewPosition = value; }
  107. }
  108. [SerializeField]
  109. private bool m_hidden = false;
  110. public bool Hidden
  111. {
  112. get { return m_hidden; }
  113. set { m_hidden = value; }
  114. }
  115. [SerializeField]
  116. private string m_url = string.Empty;
  117. public string URL
  118. {
  119. get { return m_url; }
  120. set { m_url = value; }
  121. }
  122. public void UpdateDirectivesList()
  123. {
  124. m_additionalDirectives.CleanNullDirectives();
  125. m_additionalDirectives.UpdateDirectivesFromSaveItems();
  126. if( m_additionalIncludes.IncludeList.Count > 0 )
  127. {
  128. m_additionalDirectives.AddItems( AdditionalLineType.Include, m_additionalIncludes.IncludeList );
  129. m_additionalIncludes.IncludeList.Clear();
  130. }
  131. if( m_additionalPragmas.PragmaList.Count > 0 )
  132. {
  133. m_additionalDirectives.AddItems( AdditionalLineType.Pragma, m_additionalPragmas.PragmaList );
  134. m_additionalPragmas.PragmaList.Clear();
  135. }
  136. }
  137. public void ResetDirectivesOrigin()
  138. {
  139. //if( UIUtils.CurrentShaderVersion() < 16807 )
  140. // Although the correct version was 1.6.7 rev 07 this issue was only detected on v1.7.1. rev 00
  141. // So to avoid potential incorrect saves over shader functions, I decided to broaden up the version range
  142. if( UIUtils.CurrentShaderVersion() < 17101 )
  143. {
  144. m_additionalDirectives.ResetDirectivesOrigin();
  145. }
  146. }
  147. }
  148. public class ShaderFunctionDetector : AssetPostprocessor
  149. {
  150. static void OnPostprocessAllAssets( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths )
  151. {
  152. if( UIUtils.CurrentWindow == null )
  153. return;
  154. bool markForRefresh = false;
  155. AmplifyShaderFunction function = null;
  156. for( int i = 0; i < importedAssets.Length; i++ )
  157. {
  158. function = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( importedAssets[ i ] );
  159. if( function != null )
  160. {
  161. markForRefresh = true;
  162. break;
  163. }
  164. }
  165. if( deletedAssets.Length > 0 )
  166. markForRefresh = true;
  167. for( int i = 0; i < movedAssets.Length; i++ )
  168. {
  169. function = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( movedAssets[ i ] );
  170. if( function != null )
  171. {
  172. markForRefresh = true;
  173. break;
  174. }
  175. }
  176. for( int i = 0; i < movedFromAssetPaths.Length; i++ )
  177. {
  178. function = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( movedFromAssetPaths[ i ] );
  179. if( function != null )
  180. {
  181. markForRefresh = true;
  182. break;
  183. }
  184. }
  185. if( markForRefresh )
  186. {
  187. markForRefresh = false;
  188. if( function != null )
  189. {
  190. IOUtils.UpdateSFandRefreshWindows( function );
  191. }
  192. UIUtils.CurrentWindow.LateRefreshAvailableNodes();
  193. }
  194. }
  195. }
  196. }