summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Utils/NodeExporterUtils.cs
blob: 0f2ca737fe5ff7160132e19ee73e50fdfa414c35 (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
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace AmplifyShaderEditor
{
	public enum DebugScreenShotNodeState
	{
		CreateNode,
		FocusOnNode,
		TakeScreenshot,
		WaitFrame,
		DeleteNode
	};

	public enum DebugUndoNodeState
	{
		CreateNode,
		FocusOnNode,
		WaitFrameCreate,
		DeleteNode,
		WaitFrameDelete,
		UndoNode,
		WaitFrameUndo,
		PrepareForNext
	};


	public class NodeExporterUtils
	{
		//Auto-Screenshot nodes
		private RenderTexture m_screenshotRT;
		private Texture2D m_screenshotTex2D;
		private List<ContextMenuItem> m_screenshotList = new List<ContextMenuItem>();
		private DebugScreenShotNodeState m_screenShotState;
		private bool m_takingShots = false;

		private DebugUndoNodeState m_undoState;
		private bool m_testingUndo = false;


		private AmplifyShaderEditorWindow m_window;
		private ParentNode m_node;


		private string m_pathname;

		public NodeExporterUtils( AmplifyShaderEditorWindow window )
		{
			m_window = window;
			Undo.undoRedoPerformed += OnUndoRedoPerformed;
		}

		public void OnUndoRedoPerformed()
		{
			if( m_testingUndo && m_undoState == DebugUndoNodeState.WaitFrameUndo )
			{
				m_undoState = DebugUndoNodeState.PrepareForNext;
			}
		}

		public void CalculateShaderInstructions( Shader shader )
		{
			//Type shaderutilType = Type.GetType( "UnityEditor.ShaderUtil, UnityEditor" );
			//shaderutilType.InvokeMember( "OpenCompiledShader", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { shader, mode, customPlatformsMask, includeAllVariants } );
		}

		public void ActivateAutoScreenShot( string pathname, int from, int to )
		{

			m_pathname = pathname;
			if( !System.IO.Directory.Exists( m_pathname ) )
			{
				System.IO.Directory.CreateDirectory( m_pathname );
			}

			m_screenshotRT = new RenderTexture( (int)m_window.position.width, (int)m_window.position.height, 0 );
			m_screenshotTex2D = new Texture2D( (int)m_window.position.width, (int)m_window.position.height, TextureFormat.RGB24, false );

			RenderTexture.active = m_screenshotRT;
			m_window.CurrentPaletteWindow.FillList( ref m_screenshotList, true );
			m_window.CurrentGraph.ClearGraph();
			if( m_window.IsShaderFunctionWindow )
			{
				m_window.CurrentGraph.CurrentOutputNode.Vec2Position = new Vector2( 1500, 0 );
			}
			else
			{
				m_window.CurrentGraph.CurrentMasterNode.Vec2Position = new Vector2( 1500, 0 );
			}
			m_window.ResetCameraSettings();

			m_takingShots = true;
			m_screenShotState = DebugScreenShotNodeState.CreateNode;

		}

		public void ActivateNodesURL( int from , int to )
		{
			m_window.CurrentPaletteWindow.FillList( ref m_screenshotList, true );
			
			if( to < 0 || to > m_screenshotList.Count )
				to  = m_screenshotList.Count;

			if( from >= to )
				return;

			for( int i = from; i < to; i++ )
			{
				if( m_screenshotList[ i ].NodeType != typeof( FunctionNode ) )
				{
					Application.OpenURL( m_screenshotList[ i ].NodeAttributes.NodeUrl );
				}
			}
		}

		public void ActivateAutoUndo()
		{
			m_window.CurrentPaletteWindow.FillList( ref m_screenshotList, true );
			m_window.CurrentGraph.ClearGraph();
			m_window.CurrentGraph.CurrentMasterNode.Vec2Position = new Vector2( 1500, 0 );
			m_window.ResetCameraSettings();

			m_testingUndo = true;
			m_undoState = DebugUndoNodeState.CreateNode;
		}


		public void Update()
		{
			if( m_testingUndo )
			{
				if( Event.current.type == EventType.Repaint )
				{
					m_window.Focus();
					switch( m_undoState )
					{
						case DebugUndoNodeState.CreateNode:
						{
							m_window.CurrentGraph.DeSelectAll();
							m_node = m_window.CreateNode( m_screenshotList[ 0 ].NodeType, Vector2.zero, null, true );
							m_node.RefreshExternalReferences();
							m_undoState = DebugUndoNodeState.FocusOnNode;
							Debug.Log( "Created " + m_node.Attributes.Name );
						}
						break;
						case DebugUndoNodeState.FocusOnNode:
						{
							m_window.FocusOnPoint( m_node.TruePosition.center, 1, false );
							m_undoState = DebugUndoNodeState.WaitFrameCreate;
							Debug.Log( "Focused " + m_node.Attributes.Name );
						}
						break;
						case DebugUndoNodeState.WaitFrameCreate:
						{
							m_undoState = DebugUndoNodeState.DeleteNode;
							Debug.Log( "Waiting on Create" );
						}
						break;
						case DebugUndoNodeState.DeleteNode:
						{
							Debug.Log( "Deleting " + m_node.Attributes.Name );
							m_window.DeleteSelectedNodeWithRepaint();
							m_undoState = DebugUndoNodeState.WaitFrameDelete;
						}
						break;
						case DebugUndoNodeState.WaitFrameDelete:
						{
							m_undoState = DebugUndoNodeState.UndoNode;
							Debug.Log( "Waiting on Delete" );
						}
						break;
						case DebugUndoNodeState.UndoNode:
						{
							Debug.Log( "Performing Undo" );
							m_undoState = DebugUndoNodeState.WaitFrameUndo;
							Undo.PerformUndo();
						}
						break;
						case DebugUndoNodeState.WaitFrameUndo: { } break;
						case DebugUndoNodeState.PrepareForNext:
						{
							m_screenshotList.RemoveAt( 0 );
							Debug.Log( "Undo Performed. Nodes Left " + m_screenshotList.Count );
							m_testingUndo = m_screenshotList.Count > 0;
							if( m_testingUndo )
							{
								m_undoState = DebugUndoNodeState.CreateNode;
								Debug.Log( "Going to next node" );
							}
							else
							{
								Debug.Log( "Finished Undo Test" );
							}
						}
						break;

					}
				}
			}


			if( m_takingShots )
			{
				m_window.Focus();
				switch( m_screenShotState )
				{
					case DebugScreenShotNodeState.CreateNode:
					{
						m_node = m_window.CreateNode( m_screenshotList[ 0 ].NodeType, Vector2.zero, null, false );
						m_node.RefreshExternalReferences();
						m_screenShotState = DebugScreenShotNodeState.FocusOnNode;

						
					}
					break;
					case DebugScreenShotNodeState.FocusOnNode:
					{
						//m_window.FocusOnNode( m_node, 1, false );
						m_window.FocusOnPoint( m_node.TruePosition.center, 1, false );
						m_screenShotState = DebugScreenShotNodeState.TakeScreenshot;
					}
					break;
					case DebugScreenShotNodeState.TakeScreenshot:
					{
						if( m_screenshotRT != null && Event.current.type == EventType.Repaint )
						{
							m_screenshotTex2D.ReadPixels( new Rect( 0, 0, m_screenshotRT.width, m_screenshotRT.height ), 0, 0 );
							m_screenshotTex2D.Apply();

							byte[] bytes = m_screenshotTex2D.EncodeToPNG();
							string pictureFilename = UIUtils.ReplaceInvalidStrings( m_screenshotList[ 0 ].Name );
							pictureFilename = UIUtils.RemoveInvalidCharacters( pictureFilename );

							System.IO.File.WriteAllBytes( m_pathname + pictureFilename + ".png", bytes );
							m_screenShotState = DebugScreenShotNodeState.WaitFrame;
						}
					}
					break;
					case DebugScreenShotNodeState.WaitFrame: { Debug.Log( "Wait Frame" ); m_screenShotState = DebugScreenShotNodeState.DeleteNode; } break;
					case DebugScreenShotNodeState.DeleteNode:
					{
						m_window.DestroyNode( m_node );
						m_screenshotList.RemoveAt( 0 );
						m_takingShots = m_screenshotList.Count > 0;
						Debug.Log( "Destroy Node " + m_screenshotList.Count );

						if( m_takingShots )
						{
							m_screenShotState = DebugScreenShotNodeState.CreateNode;
						}
						else
						{
							RenderTexture.active = null;
							m_screenshotRT.Release();
							UnityEngine.Object.DestroyImmediate( m_screenshotRT );
							m_screenshotRT = null;
							UnityEngine.Object.DestroyImmediate( m_screenshotTex2D );
							m_screenshotTex2D = null;
						}
					}
					break;
				};
			}
		}

		public void Destroy()
		{
			m_window = null;
			if( m_screenshotRT != null )
			{
				m_screenshotRT.Release();
				UnityEngine.Object.DestroyImmediate( m_screenshotRT );
				m_screenshotRT = null;
			}

			if( m_screenshotTex2D != null )
			{
				UnityEngine.Object.DestroyImmediate( m_screenshotTex2D );
				m_screenshotTex2D = null;
			}
		}
	}
}