summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Utils/ASEPPSHelperTool.cs
blob: e009c04f41705307cdb647998c606769ab81a3c3 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>
using System;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
//using UnityEngine.Rendering.PostProcessing;


namespace AmplifyShaderEditor
{
	public enum ASEPostProcessEvent
	{
		BeforeTransparent = 0,
		BeforeStack = 1,
		AfterStack = 2
	}

	[Serializable]
	public class ASEPPSHelperBuffer
	{
		public string Name;
		public string Tooltip;
	}

	[Serializable]
	public class ASEPPSHelperTool : EditorWindow
	{
		private const string PPSFullTemplate =
		"// Amplify Shader Editor - Visual Shader Editing Tool\n" +
		"// Copyright (c) Amplify Creations, Lda <info@amplify.pt>\n" +
		"#if UNITY_POST_PROCESSING_STACK_V2\n" +
		"using System;\n" +
		"using UnityEngine;\n" +
		"using UnityEngine.Rendering.PostProcessing;\n" +
		"\n" +
		"[Serializable]\n" +
		"[PostProcess( typeof( /*PPSRendererClass*/ ), PostProcessEvent./*PPSEventType*/, \"/*PPSMenuEntry*/\", /*AllowInSceneView*/ )]\n" +
		"public sealed class /*PPSSettingsClass*/ : PostProcessEffectSettings\n" +
		"{\n" +
		"/*PPSPropertiesDeclaration*/" +
		"}\n" +
		"\n" +
		"public sealed class /*PPSRendererClass*/ : PostProcessEffectRenderer</*PPSSettingsClass*/>\n" +
		"{\n" +
		"\tpublic override void Render( PostProcessRenderContext context )\n" +
		"\t{\n" +
		"\t\tvar sheet = context.propertySheets.Get( Shader.Find( \"/*PPSShader*/\" ) );\n" +
		"/*PPSPropertySet*/" +
		"\t\tcontext.command.BlitFullscreenTriangle( context.source, context.destination, sheet, 0 );\n" +
		"\t}\n" +
		"}\n" +
		"#endif\n";

		private const string PPSEventType = "/*PPSEventType*/";
		private const string PPSRendererClass = "/*PPSRendererClass*/";
		private const string PPSSettingsClass = "/*PPSSettingsClass*/";
		private const string PPSMenuEntry = "/*PPSMenuEntry*/";
		private const string PPSAllowInSceneView = "/*AllowInSceneView*/";
		private const string PPSShader = "/*PPSShader*/";
		private const string PPSPropertiesDecl = "/*PPSPropertiesDeclaration*/";
		private const string PPSPropertySet = "/*PPSPropertySet*/";

		public static readonly string PPSPropertySetFormat = "\t\tsheet.properties.{0}( \"{1}\", settings.{1} );\n";
		public static readonly string PPSPropertySetNullPointerCheckFormat = "\t\tif(settings.{1}.value != null) sheet.properties.{0}( \"{1}\", settings.{1} );\n";
		public static readonly string PPSPropertyDecFormat =
		"\t[{0}Tooltip( \"{1}\" )]\n" +
		"\tpublic {2} {3} = new {2} {{ {4} }};\n";
		public static readonly Dictionary<WirePortDataType, string> WireToPPSType = new Dictionary<WirePortDataType, string>()
		{
			{ WirePortDataType.FLOAT,"FloatParameter"},
			{ WirePortDataType.FLOAT2,"Vector4Parameter"},
			{ WirePortDataType.FLOAT3,"Vector4Parameter"},
			{ WirePortDataType.FLOAT4,"Vector4Parameter"},
			{ WirePortDataType.COLOR,"ColorParameter"},
			{ WirePortDataType.SAMPLER1D,"TextureParameter"},
			{ WirePortDataType.SAMPLER2D,"TextureParameter"},
			{ WirePortDataType.SAMPLER3D,"TextureParameter"},
			{ WirePortDataType.SAMPLERCUBE,"TextureParameter"}
		};

		public static readonly Dictionary<WirePortDataType, string> WireToPPSValueSet = new Dictionary<WirePortDataType, string>()
		{
			{ WirePortDataType.FLOAT,"SetFloat"},
			{ WirePortDataType.FLOAT2,"SetVector"},
			{ WirePortDataType.FLOAT3,"SetVector"},
			{ WirePortDataType.FLOAT4,"SetVector"},
			{ WirePortDataType.COLOR,"SetColor"},
			{ WirePortDataType.SAMPLER1D,  "SetTexture"},
			{ WirePortDataType.SAMPLER2D,  "SetTexture"},
			{ WirePortDataType.SAMPLER3D,  "SetTexture"},
			{ WirePortDataType.SAMPLERCUBE,"SetTexture"}
		};

		public static readonly Dictionary<UnityEditor.ShaderUtil.ShaderPropertyType, string> ShaderPropertyToPPSType = new Dictionary<UnityEditor.ShaderUtil.ShaderPropertyType, string>()
		{
			{ UnityEditor.ShaderUtil.ShaderPropertyType.Float,"FloatParameter"},
			{ UnityEditor.ShaderUtil.ShaderPropertyType.Range,"FloatParameter"},
			{ UnityEditor.ShaderUtil.ShaderPropertyType.Vector,"Vector4Parameter"},
			{ UnityEditor.ShaderUtil.ShaderPropertyType.Color,"ColorParameter"},
			{ UnityEditor.ShaderUtil.ShaderPropertyType.TexEnv,"TextureParameter"}
		};


		public static readonly Dictionary<UnityEditor.ShaderUtil.ShaderPropertyType, string> ShaderPropertyToPPSSet = new Dictionary<UnityEditor.ShaderUtil.ShaderPropertyType, string>()
		{
			{ UnityEditor.ShaderUtil.ShaderPropertyType.Float,"SetFloat"},
			{ UnityEditor.ShaderUtil.ShaderPropertyType.Range,"SetFloat"},
			{ UnityEditor.ShaderUtil.ShaderPropertyType.Vector,"SetVector"},
			{ UnityEditor.ShaderUtil.ShaderPropertyType.Color,"SetColor"},
			{ UnityEditor.ShaderUtil.ShaderPropertyType.TexEnv,"SetTexture"}
		};

		private Dictionary<string, bool> m_excludedProperties = new Dictionary<string, bool>
		{
			{ "_texcoord",true },
			{ "__dirty",true}
		};

		private Material m_dummyMaterial = null;

		private DragAndDropTool m_dragAndDropTool;
		private Rect m_draggableArea;

		[SerializeField]
		private string m_rendererClassName = "PPSRenderer";

		[SerializeField]
		private string m_settingsClassName = "PPSSettings";

		[SerializeField]
		private string m_folderPath = "Assets/";

		[SerializeField]
		private string m_menuEntry = string.Empty;

		[SerializeField]
		private bool m_allowInSceneView = true;

		[SerializeField]
		private ASEPostProcessEvent m_eventType = ASEPostProcessEvent.AfterStack;

		[SerializeField]
		private Shader m_currentShader = null;

		[SerializeField]
		private List<ASEPPSHelperBuffer> m_tooltips = new List<ASEPPSHelperBuffer>();

		[SerializeField]
		private bool m_tooltipsFoldout = true;

		private GUIStyle m_contentStyle = null;
		private GUIStyle m_pathButtonStyle = null;
		private GUIContent m_pathButtonContent = new GUIContent();
		private Vector2 m_scrollPos = Vector2.zero;

		[MenuItem( "Window/Amplify Shader Editor/Post-Processing Stack Tool", false, 1001 )]
		static void ShowWindow()
		{
			ASEPPSHelperTool window = EditorWindow.GetWindow<ASEPPSHelperTool>();
			window.titleContent.text = "Post-Processing Stack Tool";
			window.minSize = new Vector2( 302, 350 );
			window.Show();
		}

		void FetchTooltips()
		{
			m_tooltips.Clear();
			int propertyCount = UnityEditor.ShaderUtil.GetPropertyCount( m_currentShader );
			for( int i = 0; i < propertyCount; i++ )
			{
				//UnityEditor.ShaderUtil.ShaderPropertyType type = UnityEditor.ShaderUtil.GetPropertyType( m_currentShader, i );
				string name = UnityEditor.ShaderUtil.GetPropertyName( m_currentShader, i );
				string description = UnityEditor.ShaderUtil.GetPropertyDescription( m_currentShader, i );

				if( m_excludedProperties.ContainsKey( name ))
					continue;

				m_tooltips.Add( new ASEPPSHelperBuffer { Name = name, Tooltip = description } );
			}
		}

		void OnGUI()
		{
			if( m_pathButtonStyle == null )
				m_pathButtonStyle = "minibutton";

			m_scrollPos = EditorGUILayout.BeginScrollView( m_scrollPos, GUILayout.Height( position.height ) );

			EditorGUILayout.BeginVertical( m_contentStyle );
			EditorGUI.BeginChangeCheck();
			m_currentShader = EditorGUILayout.ObjectField( "Shader", m_currentShader, typeof( Shader ), false ) as Shader;
			if( EditorGUI.EndChangeCheck() )
			{
				GetInitialInfo( m_currentShader );
			}

			EditorGUILayout.Separator();
			EditorGUILayout.LabelField( "Path and Filename" );
			EditorGUILayout.BeginHorizontal();
			m_pathButtonContent.text = m_folderPath;
			Vector2 buttonSize = m_pathButtonStyle.CalcSize( m_pathButtonContent );
			if( GUILayout.Button( m_pathButtonContent, m_pathButtonStyle, GUILayout.MaxWidth( Mathf.Min( position.width * 0.5f, buttonSize.x ) ) ) )
			{
				string folderpath = EditorUtility.OpenFolderPanel( "Save Texture Array to folder", "Assets/", "" );
				folderpath = FileUtil.GetProjectRelativePath( folderpath );
				if( string.IsNullOrEmpty( folderpath ) )
					m_folderPath = "Assets/";
				else
					m_folderPath = folderpath + "/";
			}

			m_settingsClassName = EditorGUILayout.TextField( m_settingsClassName, GUILayout.ExpandWidth( true ) );

			EditorGUILayout.LabelField( ".cs", GUILayout.MaxWidth( 40 ) );
			EditorGUILayout.EndHorizontal();
			EditorGUILayout.Separator();

			m_menuEntry = EditorGUILayout.TextField( "Name", m_menuEntry );

			EditorGUILayout.Separator();

			m_allowInSceneView = EditorGUILayout.Toggle( "Allow In Scene View", m_allowInSceneView );

			EditorGUILayout.Separator();

			m_eventType = (ASEPostProcessEvent)EditorGUILayout.EnumPopup( "Event Type", m_eventType );

			EditorGUILayout.Separator();

			m_tooltipsFoldout = EditorGUILayout.Foldout( m_tooltipsFoldout, "Tooltips" );
			if( m_tooltipsFoldout )
			{
				EditorGUI.indentLevel++;
				for( int i = 0; i < m_tooltips.Count; i++ )
				{
					m_tooltips[ i ].Tooltip = EditorGUILayout.TextField( m_tooltips[ i ].Name, m_tooltips[ i ].Tooltip );
				}
				EditorGUI.indentLevel--;
			}

			EditorGUILayout.Separator();

			if( GUILayout.Button( "Build" ) )
			{
				System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
				string propertiesDecl = string.Empty;
				string propertiesSet = string.Empty;
				GetShaderInfoFromShaderAsset( ref propertiesDecl, ref propertiesSet );
				string template = PPSFullTemplate;
				template = template.Replace( PPSRendererClass, m_rendererClassName );
				template = template.Replace( PPSSettingsClass, m_settingsClassName );
				template = template.Replace( PPSEventType, m_eventType.ToString() );
				template = template.Replace( PPSPropertiesDecl, propertiesDecl );
				template = template.Replace( PPSPropertySet, propertiesSet );
				template = template.Replace( PPSMenuEntry, m_menuEntry );
				template = template.Replace( PPSAllowInSceneView, m_allowInSceneView?"true":"false" );
				template = template.Replace( PPSShader, m_currentShader.name );
				string path = m_folderPath + m_settingsClassName + ".cs";
				IOUtils.SaveTextfileToDisk( template, path, false );
				System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
				AssetDatabase.Refresh();
			}

			EditorGUILayout.EndVertical();
			EditorGUILayout.EndScrollView();
			m_draggableArea.size = position.size;
			m_dragAndDropTool.TestDragAndDrop( m_draggableArea );
		}

		public void GetShaderInfoFromASE( ref string propertiesDecl, ref string propertiesSet )
		{
			List<PropertyNode> properties = UIUtils.CurrentWindow.OutsideGraph.PropertyNodes.NodesList;
			int propertyCount = properties.Count;
			for( int i = 0; i < propertyCount; i++ )
			{
				properties[ i ].GeneratePPSInfo( ref propertiesDecl, ref propertiesSet );
			}
		}

		public void GetShaderInfoFromShaderAsset( ref string propertiesDecl, ref string propertiesSet )
		{
			bool fetchInitialInfo = false;
			if( m_currentShader == null )
			{
				Material mat = Selection.activeObject as Material;
				if( mat != null )
				{
					m_currentShader = mat.shader;
				}
				else
				{
					m_currentShader = Selection.activeObject as Shader;
				}
				fetchInitialInfo = true;
			}

			if( m_currentShader != null )
			{
				if( fetchInitialInfo )
					GetInitialInfo( m_currentShader );

				if( m_dummyMaterial == null )
				{
					m_dummyMaterial = new Material( m_currentShader );
				}
				else
				{
					m_dummyMaterial.shader = m_currentShader;
				}

				int propertyCount = UnityEditor.ShaderUtil.GetPropertyCount( m_currentShader );
				//string allProperties = string.Empty;
				int validIds = 0;
				for( int i = 0; i < propertyCount; i++ )
				{
					UnityEditor.ShaderUtil.ShaderPropertyType type = UnityEditor.ShaderUtil.GetPropertyType( m_currentShader, i );
					string name = UnityEditor.ShaderUtil.GetPropertyName( m_currentShader, i );
					//string description = UnityEditor.ShaderUtil.GetPropertyDescription( m_currentShader, i );
					if( m_excludedProperties.ContainsKey( name ))
						continue;

					string defaultValue = string.Empty;
					bool nullPointerCheck = false;
					switch( type )
					{
						case UnityEditor.ShaderUtil.ShaderPropertyType.Color:
						{
							Color value = m_dummyMaterial.GetColor( name );
							defaultValue = string.Format( "value = new Color({0}f,{1}f,{2}f,{3}f)", value.r, value.g, value.b, value.a );
						}
						break;
						case UnityEditor.ShaderUtil.ShaderPropertyType.Vector:
						{
							Vector4 value = m_dummyMaterial.GetVector( name );
							defaultValue = string.Format( "value = new Vector4({0}f,{1}f,{2}f,{3}f)", value.x, value.y, value.z, value.w );
						}
						break;
						case UnityEditor.ShaderUtil.ShaderPropertyType.Float:
						{
							float value = m_dummyMaterial.GetFloat( name );
							defaultValue = "value = " + value + "f";
						}
						break;
						case UnityEditor.ShaderUtil.ShaderPropertyType.Range:
						{
							float value = m_dummyMaterial.GetFloat( name );
							defaultValue = "value = " + value + "f";
						}
						break;
						case UnityEditor.ShaderUtil.ShaderPropertyType.TexEnv:
						{
							nullPointerCheck = true;
						}
						break;
					}

					propertiesDecl += string.Format( PPSPropertyDecFormat, string.Empty, m_tooltips[ validIds ].Tooltip, ShaderPropertyToPPSType[ type ], name, defaultValue );
					propertiesSet += string.Format( nullPointerCheck ? PPSPropertySetNullPointerCheckFormat : PPSPropertySetFormat, ShaderPropertyToPPSSet[ type ], name );
					validIds++;
				}

			}
		}

		private void GetInitialInfo()
		{
			MasterNode masterNode = UIUtils.CurrentWindow.OutsideGraph.CurrentMasterNode;
			m_menuEntry = masterNode.ShaderName.Replace( "Hidden/", string.Empty ).Replace( ".shader", string.Empty );
			string name = m_menuEntry;
			m_rendererClassName = name + "PPSRenderer";
			m_settingsClassName = name + "PPSSettings";
			m_folderPath = "Assets/";
		}

		private void GetInitialInfo( Shader shader )
		{
			if( shader == null )
			{
				m_scrollPos = Vector2.zero;
				m_menuEntry = string.Empty;
				m_rendererClassName = "PPSRenderer";
				m_settingsClassName = "PPSSettings";
				m_folderPath = "Assets/";
				m_tooltips.Clear();
				return;
			}

			m_menuEntry = shader.name.Replace( "Hidden/", string.Empty ).Replace( ".shader", string.Empty );
			m_menuEntry = UIUtils.RemoveInvalidCharacters( m_menuEntry );
			string name = m_menuEntry.Replace( "/", string.Empty );
			m_rendererClassName = name + "PPSRenderer";
			m_settingsClassName = name + "PPSSettings";
			m_folderPath = AssetDatabase.GetAssetPath( shader );
			m_folderPath = m_folderPath.Replace( System.IO.Path.GetFileName( m_folderPath ), string.Empty );

			FetchTooltips();
		}

		public void OnValidObjectsDropped( UnityEngine.Object[] droppedObjs )
		{
			for( int objIdx = 0; objIdx < droppedObjs.Length; objIdx++ )
			{
				Material mat = droppedObjs[ objIdx ] as Material;
				if( mat != null )
				{
					m_currentShader = mat.shader;
					GetInitialInfo( mat.shader );
					return;
				}
				else
				{
					Shader shader = droppedObjs[ objIdx ] as Shader;
					if( shader != null )
					{
						m_currentShader = shader;
						GetInitialInfo( shader );
						return;
					}
				}
			}
		}

		private void OnEnable()
		{
			m_draggableArea = new Rect( 0, 0, 1, 1 );
			m_dragAndDropTool = new DragAndDropTool();
			m_dragAndDropTool.OnValidDropObjectEvt += OnValidObjectsDropped;

			if( m_contentStyle == null )
			{
				m_contentStyle = new GUIStyle( GUIStyle.none );
				m_contentStyle.margin = new RectOffset( 6, 4, 5, 5 );
			}

			m_pathButtonStyle = null;

			//GetInitialInfo();
		}

		private void OnDestroy()
		{
			if( m_dummyMaterial != null )
			{
				GameObject.DestroyImmediate( m_dummyMaterial );
				m_dummyMaterial = null;
			}

			m_dragAndDropTool.Destroy();
			m_dragAndDropTool = null;

			m_tooltips.Clear();
			m_tooltips = null;

			m_contentStyle = null;
			m_pathButtonStyle = null;
			m_currentShader = null;
		}
	}
}