summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Menu/AmplifyShaderFunction.cs
blob: df83bedbd127f182beaa0373d62f5d037e47355e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>

using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using AmplifyShaderEditor;

[Serializable]
public class AmplifyShaderFunction : ScriptableObject
{
	[SerializeField]
	private string m_functionInfo = string.Empty;
	public string FunctionInfo
	{
		get { return m_functionInfo; }
		set { m_functionInfo = value; }
	}

	[SerializeField]
	private string m_functionName = string.Empty;
	public string FunctionName
	{
		get { if( m_functionName.Length == 0 ) return name; else return m_functionName; }
		set { m_functionName = value; }
	}

	[SerializeField]
	[TextArea( 5, 15 )]
	private string m_description = string.Empty;
	public string Description
	{
		get { return m_description; }
		set { m_description = value; }
	}

	[SerializeField]
	private AdditionalIncludesHelper m_additionalIncludes = new AdditionalIncludesHelper();
	//public AdditionalIncludesHelper AdditionalIncludes
	//{
	//	get { return m_additionalIncludes; }
	//	set { m_additionalIncludes = value; }
	//}

	[SerializeField]
	private AdditionalPragmasHelper m_additionalPragmas = new AdditionalPragmasHelper();
	//public AdditionalPragmasHelper AdditionalPragmas
	//{
	//	get { return m_additionalPragmas; }
	//	set { m_additionalPragmas = value; }
	//}

	[SerializeField]
	private TemplateAdditionalDirectivesHelper m_additionalDirectives = new TemplateAdditionalDirectivesHelper( " Additional Directives" );
	public TemplateAdditionalDirectivesHelper AdditionalDirectives
	{
		get { return m_additionalDirectives; }
		set { m_additionalDirectives = value; }
	}
	
	[SerializeField]
	private FunctionNodeCategories m_nodeCategory = FunctionNodeCategories.Functions;
	public FunctionNodeCategories NodeCategory
	{
		get { return m_nodeCategory; }
		set { m_nodeCategory = value; }
	}

	[SerializeField]
	private string m_customNodeCategory = string.Empty;
	public string CustomNodeCategory
	{
		get
		{
			if( m_nodeCategory == FunctionNodeCategories.Custom )
			{
				if( string.IsNullOrEmpty( m_customNodeCategory ) )
					return "Functions";
				else
					return m_customNodeCategory;
			}
			else
			{
				return UIUtils.CategoryPresets[ (int)m_nodeCategory ];
				//return new SerializedObject( this ).FindProperty( "m_nodeCategory" ).enumDisplayNames[ (int)m_nodeCategory ];
			}
		}
	}
	
	[SerializeField]
	private PreviewLocation m_previewPosition = PreviewLocation.Auto;
	public PreviewLocation PreviewPosition
	{
		get { return m_previewPosition; }
		set { m_previewPosition = value; }
	}

	[SerializeField]
	private bool m_hidden = false;
	public bool Hidden
	{
		get { return m_hidden; }
		set { m_hidden = value; }
	}

	public void UpdateDirectivesList()
	{
		m_additionalDirectives.CleanNullDirectives();
		m_additionalDirectives.UpdateDirectivesFromSaveItems();

		if( m_additionalIncludes.IncludeList.Count > 0 )
		{
			m_additionalDirectives.AddItems( AdditionalLineType.Include, m_additionalIncludes.IncludeList );
			m_additionalIncludes.IncludeList.Clear();
		}

		if( m_additionalPragmas.PragmaList.Count > 0 )
		{
			m_additionalDirectives.AddItems( AdditionalLineType.Pragma, m_additionalPragmas.PragmaList );
			m_additionalPragmas.PragmaList.Clear();
		}
	}

	public void ResetDirectivesOrigin()
	{
		//if( UIUtils.CurrentShaderVersion() < 16807 )
		// Although the correct version was 1.6.7 rev 07 this issue was only detected on v1.7.1. rev 00
		// So to avoid potential incorrect saves over shader functions, I decided to broaden up the version range
		if( UIUtils.CurrentShaderVersion() < 17101 )
		{
			m_additionalDirectives.ResetDirectivesOrigin();
		}
	}
}

public class ShaderFunctionDetector : AssetPostprocessor
{
	static void OnPostprocessAllAssets( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths )
	{
		if( UIUtils.CurrentWindow == null )
			return;

		bool markForRefresh = false;
		AmplifyShaderFunction function = null;
		for( int i = 0; i < importedAssets.Length; i++ )
		{
			function = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( importedAssets[ i ] );
			if( function != null )
			{
				markForRefresh = true;
				break;
			}
		}

		if( deletedAssets.Length > 0 )
			markForRefresh = true;

		for( int i = 0; i < movedAssets.Length; i++ )
		{
			function = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( movedAssets[ i ] );
			if( function != null )
			{
				markForRefresh = true;
				break;
			}
		}

		for( int i = 0; i < movedFromAssetPaths.Length; i++ )
		{
			function = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( movedFromAssetPaths[ i ] );
			if( function != null )
			{
				markForRefresh = true;
				break;
			}
		}

		if( markForRefresh )
		{
			markForRefresh = false;
			if( function != null )
			{
				IOUtils.UpdateSFandRefreshWindows( function );
			}
			UIUtils.CurrentWindow.LateRefreshAvailableNodes();
		}
	}
}