NodeExporterUtils.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.IO;
  9. namespace AmplifyShaderEditor
  10. {
  11. public enum DebugScreenShotNodeState
  12. {
  13. CreateNode,
  14. FocusOnNode,
  15. TakeScreenshot,
  16. WaitFrame,
  17. DeleteNode
  18. };
  19. public enum DebugUndoNodeState
  20. {
  21. CreateNode,
  22. FocusOnNode,
  23. WaitFrameCreate,
  24. DeleteNode,
  25. WaitFrameDelete,
  26. UndoNode,
  27. WaitFrameUndo,
  28. PrepareForNext
  29. };
  30. public class NodeExporterUtils
  31. {
  32. //Auto-Screenshot nodes
  33. private RenderTexture m_screenshotRT;
  34. private Texture2D m_screenshotTex2D;
  35. private List<ContextMenuItem> m_screenshotList = new List<ContextMenuItem>();
  36. private DebugScreenShotNodeState m_screenShotState;
  37. private bool m_takingShots = false;
  38. private DebugUndoNodeState m_undoState;
  39. private bool m_testingUndo = false;
  40. private AmplifyShaderEditorWindow m_window;
  41. private ParentNode m_node;
  42. private string m_pathname;
  43. public NodeExporterUtils( AmplifyShaderEditorWindow window )
  44. {
  45. m_window = window;
  46. UndoUtils.RegisterUndoRedoCallback( OnUndoRedoPerformed );
  47. }
  48. public void OnUndoRedoPerformed()
  49. {
  50. if( m_testingUndo && m_undoState == DebugUndoNodeState.WaitFrameUndo )
  51. {
  52. m_undoState = DebugUndoNodeState.PrepareForNext;
  53. }
  54. }
  55. public void CalculateShaderInstructions( Shader shader )
  56. {
  57. //Type shaderutilType = Type.GetType( "UnityEditor.ShaderUtil, UnityEditor" );
  58. //shaderutilType.InvokeMember( "OpenCompiledShader", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { shader, mode, customPlatformsMask, includeAllVariants } );
  59. }
  60. public void ActivateAutoScreenShot( string pathname, int from, int to )
  61. {
  62. m_pathname = pathname;
  63. if( !System.IO.Directory.Exists( m_pathname ) )
  64. {
  65. System.IO.Directory.CreateDirectory( m_pathname );
  66. }
  67. m_screenshotRT = new RenderTexture( (int)m_window.position.width, (int)m_window.position.height, 0 );
  68. m_screenshotTex2D = new Texture2D( (int)m_window.position.width, (int)m_window.position.height, TextureFormat.RGB24, false );
  69. RenderTexture.active = m_screenshotRT;
  70. m_window.CurrentPaletteWindow.FillList( ref m_screenshotList, true );
  71. m_window.CurrentGraph.ClearGraph();
  72. if( m_window.IsShaderFunctionWindow )
  73. {
  74. m_window.CurrentGraph.CurrentOutputNode.Vec2Position = new Vector2( 1500, 0 );
  75. }
  76. else
  77. {
  78. m_window.CurrentGraph.CurrentMasterNode.Vec2Position = new Vector2( 1500, 0 );
  79. }
  80. m_window.ResetCameraSettings();
  81. m_takingShots = true;
  82. m_screenShotState = DebugScreenShotNodeState.CreateNode;
  83. }
  84. public void ActivateNodesURL( int from , int to )
  85. {
  86. m_window.CurrentPaletteWindow.FillList( ref m_screenshotList, true );
  87. if( to < 0 || to > m_screenshotList.Count )
  88. to = m_screenshotList.Count;
  89. if( from >= to )
  90. return;
  91. for( int i = from; i < to; i++ )
  92. {
  93. if( m_screenshotList[ i ].NodeType != typeof( FunctionNode ) )
  94. {
  95. Application.OpenURL( m_screenshotList[ i ].NodeAttributes.NodeUrl );
  96. }
  97. }
  98. }
  99. public void ActivateAutoUndo()
  100. {
  101. m_window.CurrentPaletteWindow.FillList( ref m_screenshotList, true );
  102. m_window.CurrentGraph.ClearGraph();
  103. m_window.CurrentGraph.CurrentMasterNode.Vec2Position = new Vector2( 1500, 0 );
  104. m_window.ResetCameraSettings();
  105. m_testingUndo = true;
  106. m_undoState = DebugUndoNodeState.CreateNode;
  107. }
  108. private Type[] GetTypesInNamespace( Assembly assembly , string nameSpace )
  109. {
  110. return assembly.GetTypes().Where( t => String.Equals( t.Namespace , nameSpace , StringComparison.Ordinal ) ).ToArray();
  111. }
  112. public void GenerateNodesCSV( string path )
  113. {
  114. path += "AvailableNodesCSV.txt";
  115. StringBuilder result = new StringBuilder();
  116. result.AppendLine( "Nodes" );
  117. result.AppendLine( "Name,Updated" );
  118. try
  119. {
  120. //IEnumerable<System.Type> availableTypes = AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany( type => type.GetTypes() );
  121. var mainAssembly = Assembly.GetExecutingAssembly();
  122. Type[] availableTypes = GetTypesInNamespace( mainAssembly , "AmplifyShaderEditor" );
  123. foreach( System.Type type in availableTypes )
  124. {
  125. foreach( NodeAttributes attribute in Attribute.GetCustomAttributes( type ).OfType<NodeAttributes>() )
  126. {
  127. if( attribute.Available && !attribute.Deprecated )
  128. {
  129. result.AppendLine( attribute.Name + ", false" );
  130. }
  131. }
  132. }
  133. }
  134. catch( ReflectionTypeLoadException exception )
  135. {
  136. Debug.LogException( exception );
  137. }
  138. result.AppendLine();
  139. result.AppendLine( "Shader Functions" );
  140. result.AppendLine( "Name,Updated" );
  141. string[] guids = AssetDatabase.FindAssets( "t:AmplifyShaderFunction" );
  142. for( int i = 0 ; i < guids.Length ; i++ )
  143. {
  144. AmplifyShaderFunction sf = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( AssetDatabase.GUIDToAssetPath( guids[ i ] ) );
  145. result.AppendLine( sf.FunctionName + ",false" );
  146. }
  147. IOUtils.SaveTextfileToDisk( result.ToString() , path , false );
  148. Debug.Log( "Available nodes CSV saved to: " + path );
  149. }
  150. public void Update()
  151. {
  152. if( m_testingUndo )
  153. {
  154. if( Event.current.type == EventType.Repaint )
  155. {
  156. m_window.Focus();
  157. switch( m_undoState )
  158. {
  159. case DebugUndoNodeState.CreateNode:
  160. {
  161. m_window.CurrentGraph.DeSelectAll();
  162. m_node = m_window.CreateNode( m_screenshotList[ 0 ].NodeType, Vector2.zero, null, true );
  163. m_node.RefreshExternalReferences();
  164. m_undoState = DebugUndoNodeState.FocusOnNode;
  165. Debug.Log( "Created " + m_node.Attributes.Name );
  166. }
  167. break;
  168. case DebugUndoNodeState.FocusOnNode:
  169. {
  170. m_window.FocusOnPoint( m_node.TruePosition.center, 1, false );
  171. m_undoState = DebugUndoNodeState.WaitFrameCreate;
  172. Debug.Log( "Focused " + m_node.Attributes.Name );
  173. }
  174. break;
  175. case DebugUndoNodeState.WaitFrameCreate:
  176. {
  177. m_undoState = DebugUndoNodeState.DeleteNode;
  178. Debug.Log( "Waiting on Create" );
  179. }
  180. break;
  181. case DebugUndoNodeState.DeleteNode:
  182. {
  183. Debug.Log( "Deleting " + m_node.Attributes.Name );
  184. m_window.DeleteSelectedNodeWithRepaint();
  185. m_undoState = DebugUndoNodeState.WaitFrameDelete;
  186. }
  187. break;
  188. case DebugUndoNodeState.WaitFrameDelete:
  189. {
  190. m_undoState = DebugUndoNodeState.UndoNode;
  191. Debug.Log( "Waiting on Delete" );
  192. }
  193. break;
  194. case DebugUndoNodeState.UndoNode:
  195. {
  196. Debug.Log( "Performing Undo" );
  197. m_undoState = DebugUndoNodeState.WaitFrameUndo;
  198. UndoUtils.PerformUndo();
  199. }
  200. break;
  201. case DebugUndoNodeState.WaitFrameUndo: { } break;
  202. case DebugUndoNodeState.PrepareForNext:
  203. {
  204. m_screenshotList.RemoveAt( 0 );
  205. Debug.Log( "Undo Performed. Nodes Left " + m_screenshotList.Count );
  206. m_testingUndo = m_screenshotList.Count > 0;
  207. if( m_testingUndo )
  208. {
  209. m_undoState = DebugUndoNodeState.CreateNode;
  210. Debug.Log( "Going to next node" );
  211. }
  212. else
  213. {
  214. Debug.Log( "Finished Undo Test" );
  215. }
  216. }
  217. break;
  218. }
  219. }
  220. }
  221. if( m_takingShots )
  222. {
  223. m_window.Focus();
  224. switch( m_screenShotState )
  225. {
  226. case DebugScreenShotNodeState.CreateNode:
  227. {
  228. m_node = m_window.CreateNode( m_screenshotList[ 0 ].NodeType, Vector2.zero, null, false );
  229. m_node.RefreshExternalReferences();
  230. m_screenShotState = DebugScreenShotNodeState.FocusOnNode;
  231. }
  232. break;
  233. case DebugScreenShotNodeState.FocusOnNode:
  234. {
  235. //m_window.FocusOnNode( m_node, 1, false );
  236. m_window.FocusOnPoint( m_node.TruePosition.center, 1, false );
  237. m_screenShotState = DebugScreenShotNodeState.TakeScreenshot;
  238. }
  239. break;
  240. case DebugScreenShotNodeState.TakeScreenshot:
  241. {
  242. if( m_screenshotRT != null && Event.current.type == EventType.Repaint )
  243. {
  244. m_screenshotTex2D.ReadPixels( new Rect( 0, 0, m_screenshotRT.width, m_screenshotRT.height ), 0, 0 );
  245. m_screenshotTex2D.Apply();
  246. byte[] bytes = m_screenshotTex2D.EncodeToPNG();
  247. string pictureFilename = UIUtils.ReplaceInvalidStrings( m_screenshotList[ 0 ].Name );
  248. pictureFilename = UIUtils.RemoveInvalidCharacters( pictureFilename );
  249. System.IO.File.WriteAllBytes( m_pathname + pictureFilename + ".png", bytes );
  250. m_screenShotState = DebugScreenShotNodeState.WaitFrame;
  251. }
  252. }
  253. break;
  254. case DebugScreenShotNodeState.WaitFrame: { Debug.Log( "Wait Frame" ); m_screenShotState = DebugScreenShotNodeState.DeleteNode; } break;
  255. case DebugScreenShotNodeState.DeleteNode:
  256. {
  257. m_window.DestroyNode( m_node );
  258. m_screenshotList.RemoveAt( 0 );
  259. m_takingShots = m_screenshotList.Count > 0;
  260. Debug.Log( "Destroy Node " + m_screenshotList.Count );
  261. if( m_takingShots )
  262. {
  263. m_screenShotState = DebugScreenShotNodeState.CreateNode;
  264. }
  265. else
  266. {
  267. RenderTexture.active = null;
  268. m_screenshotRT.Release();
  269. UnityEngine.Object.DestroyImmediate( m_screenshotRT );
  270. m_screenshotRT = null;
  271. UnityEngine.Object.DestroyImmediate( m_screenshotTex2D );
  272. m_screenshotTex2D = null;
  273. }
  274. }
  275. break;
  276. };
  277. }
  278. }
  279. public void Destroy()
  280. {
  281. m_window = null;
  282. if( m_screenshotRT != null )
  283. {
  284. m_screenshotRT.Release();
  285. UnityEngine.Object.DestroyImmediate( m_screenshotRT );
  286. m_screenshotRT = null;
  287. }
  288. if( m_screenshotTex2D != null )
  289. {
  290. UnityEngine.Object.DestroyImmediate( m_screenshotTex2D );
  291. m_screenshotTex2D = null;
  292. }
  293. }
  294. }
  295. }