TemplatePassSelectorHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. public class PassVisibleOptionsItems
  9. {
  10. public bool Visible;
  11. public string Name;
  12. public int Idx = -1;
  13. }
  14. [Serializable]
  15. public class TemplatePassSelectorHelper
  16. {
  17. private const string Label = " Available Passes";
  18. [SerializeField]
  19. private bool m_foldout;
  20. [SerializeField]
  21. private PassVisibleOptionsItems[] m_currentPasses;
  22. [NonSerialized]
  23. private Dictionary<string, PassVisibleOptionsItems> m_currentPassesDict;
  24. [SerializeField]
  25. private int m_mainPassId;
  26. public void CopyFrom( TemplatePassSelectorHelper from )
  27. {
  28. for( int i = 0; i < from.AvailablePasses.Length; i++ )
  29. {
  30. SetPassVisible( from.AvailablePasses[ i ].Name, from.AvailablePasses[ i ].Visible );
  31. }
  32. }
  33. public void Setup( TemplateSubShader subShader )
  34. {
  35. if( m_currentPasses == null )
  36. {
  37. m_currentPassesDict = new Dictionary<string, PassVisibleOptionsItems>();
  38. m_currentPasses = new PassVisibleOptionsItems[ subShader.Passes.Count ];
  39. for( int i = 0; i < m_currentPasses.Length; i++ )
  40. {
  41. if( subShader.Passes[ i ].IsMainPass )
  42. m_mainPassId = i;
  43. m_currentPasses[ i ] = new PassVisibleOptionsItems() { Name = subShader.Passes[ i ].PassNameContainer.Data, Visible = true, Idx = i };
  44. m_currentPassesDict.Add( m_currentPasses[ i ].Name, m_currentPasses[ i ] );
  45. }
  46. }
  47. }
  48. public void Clear()
  49. {
  50. m_currentPasses = null;
  51. if( m_currentPassesDict != null )
  52. m_currentPassesDict.Clear();
  53. m_currentPassesDict = null;
  54. }
  55. public void Reset()
  56. {
  57. for ( int i = 0; i < m_currentPasses.Length; i++ )
  58. {
  59. m_currentPasses[ i ].Visible = true;
  60. }
  61. }
  62. public void Destroy()
  63. {
  64. m_currentPasses = null;
  65. if( m_currentPassesDict != null )
  66. m_currentPassesDict.Clear();
  67. m_currentPassesDict = null;
  68. }
  69. public void Draw( TemplateMultiPassMasterNode owner )
  70. {
  71. if( m_currentPasses.Length < 2 )
  72. return;
  73. NodeUtils.DrawNestedPropertyGroup( ref m_foldout, Label, () =>
  74. {
  75. for( int i = 0; i < m_currentPasses.Length; i++ )
  76. {
  77. EditorGUI.BeginChangeCheck();
  78. m_currentPasses[ i ].Visible = owner.EditorGUILayoutToggleLeft( m_currentPasses[ i ].Name, m_currentPasses[ i ].Visible );
  79. if( EditorGUI.EndChangeCheck() )
  80. {
  81. owner.ContainerGraph.GetMultiPassMasterNodes( owner.LODIndex)[ m_currentPasses[ i ].Idx ].IsInvisible = !m_currentPasses[ i ].Visible;
  82. }
  83. }
  84. EditorGUILayout.Space();
  85. } );
  86. }
  87. public void ReadFromString( ref uint index, ref string[] nodeParams )
  88. {
  89. int passAmount = Convert.ToInt32( nodeParams[ index++ ] );
  90. for( int i = 0; i < passAmount; i++ )
  91. {
  92. bool value = Convert.ToBoolean( nodeParams[ index++ ] );
  93. if( i < m_currentPasses.Length )
  94. {
  95. m_currentPasses[ i ].Visible = value;
  96. }
  97. }
  98. }
  99. public void WriteToString( ref string nodeInfo )
  100. {
  101. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentPasses.Length );
  102. for( int i = 0; i < m_currentPasses.Length; i++ )
  103. {
  104. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentPasses[ i ].Visible );
  105. }
  106. }
  107. public void SetPassVisible( string passName, bool visible )
  108. {
  109. bool refresh = false;
  110. if( m_currentPassesDict == null )
  111. {
  112. m_currentPassesDict = new Dictionary<string, PassVisibleOptionsItems>();
  113. refresh = true;
  114. }
  115. else if( m_currentPassesDict.Count != m_currentPasses.Length )
  116. {
  117. refresh = true;
  118. }
  119. if( refresh )
  120. {
  121. for( int i = 0; i < m_currentPasses.Length; i++ )
  122. {
  123. m_currentPassesDict.Add( m_currentPasses[ i ].Name, m_currentPasses[ i ] );
  124. }
  125. }
  126. if( m_currentPassesDict.ContainsKey( passName ) )
  127. {
  128. m_currentPassesDict[ passName ].Visible = visible;
  129. }
  130. }
  131. public int LastActivePass
  132. {
  133. get
  134. {
  135. if( m_currentPasses != null )
  136. {
  137. for( int i = m_currentPasses.Length - 1; i > -1; i-- )
  138. {
  139. if( m_currentPasses[ i ].Visible )
  140. return i;
  141. }
  142. }
  143. m_currentPasses[ m_mainPassId ].Visible = true;
  144. return m_mainPassId;
  145. }
  146. }
  147. public bool IsVisible( int passId ) { return m_currentPasses[ passId ].Visible; }
  148. private PassVisibleOptionsItems[] AvailablePasses { get { return m_currentPasses; } }
  149. }
  150. }