SwitchNode.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Debug Switch", "Logical Operators", "Hard Switch between any of its input ports" )]
  10. public class SwitchNode : ParentNode
  11. {
  12. private const string Info = "This is a Debug node which only generates the source for the selected port. This means that no properties are generated for other ports and information might be lost.";
  13. private const string InputPortName = "In ";
  14. private const string CurrSelectedStr = "Current";
  15. private const string MaxAmountStr = "Max Amount";
  16. private const int MaxAllowedAmount = 8;
  17. [SerializeField]
  18. private string[] m_availableInputsLabels = { "In 0", "In 1" };
  19. [SerializeField]
  20. private int[] m_availableInputsValues = { 0, 1 };
  21. [SerializeField]
  22. private int m_currentSelectedInput = 0;
  23. [SerializeField]
  24. private int m_maxAmountInputs = 2;
  25. private int m_cachedPropertyId = -1;
  26. private GUIContent m_popContent;
  27. private Rect m_varRect;
  28. private Rect m_imgRect;
  29. private bool m_editing;
  30. protected override void CommonInit( int uniqueId )
  31. {
  32. base.CommonInit( uniqueId );
  33. for( int i = 0; i < MaxAllowedAmount; i++ )
  34. {
  35. AddInputPort( WirePortDataType.FLOAT, false, InputPortName + i );
  36. m_inputPorts[ i ].Visible = ( i < 2 );
  37. }
  38. AddOutputPort( WirePortDataType.FLOAT, " " );
  39. m_popContent = new GUIContent();
  40. m_popContent.image = UIUtils.PopupIcon;
  41. m_insideSize.Set( 50, 25 );
  42. m_textLabelWidth = 100;
  43. m_autoWrapProperties = false;
  44. m_previewShaderGUID = "a58e46feaa5e3d14383bfeac24d008bc";
  45. }
  46. public override void SetPreviewInputs()
  47. {
  48. base.SetPreviewInputs();
  49. if( m_cachedPropertyId == -1 )
  50. m_cachedPropertyId = Shader.PropertyToID( "_Current" );
  51. PreviewMaterial.SetInt( m_cachedPropertyId, m_currentSelectedInput );
  52. }
  53. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  54. {
  55. m_inputPorts[ inputPortId ].MatchPortToConnection();
  56. if( inputPortId == m_currentSelectedInput )
  57. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ inputPortId ].DataType, false );
  58. }
  59. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  60. {
  61. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  62. m_inputPorts[ portId ].MatchPortToConnection();
  63. if( portId == m_currentSelectedInput )
  64. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ portId ].DataType, false );
  65. }
  66. public void UpdateLabels()
  67. {
  68. m_availableInputsLabels = new string[ m_maxAmountInputs ];
  69. m_availableInputsValues = new int[ m_maxAmountInputs ];
  70. for( int i = 0; i < m_maxAmountInputs; i++ )
  71. {
  72. m_availableInputsLabels[ i ] = InputPortName + i;
  73. m_availableInputsValues[ i ] = i;
  74. }
  75. }
  76. //void UpdateOutput()
  77. //{
  78. // m_outputPorts[ 0 ].ChangeProperties( m_inputPorts[ m_currentSelectedInput ].Name, m_inputPorts[ m_currentSelectedInput ].DataType, false );
  79. // m_sizeIsDirty = true;
  80. //}
  81. public override void OnNodeLayout( DrawInfo drawInfo )
  82. {
  83. base.OnNodeLayout( drawInfo );
  84. m_varRect = m_remainingBox;
  85. m_varRect.width = 50 * drawInfo.InvertedZoom;
  86. m_varRect.height = 16 * drawInfo.InvertedZoom;
  87. m_varRect.x = m_remainingBox.xMax - m_varRect.width;
  88. //m_varRect.x += m_remainingBox.width * 0.5f - m_varRect.width * 0.5f;
  89. m_varRect.y += 1 * drawInfo.InvertedZoom;
  90. m_imgRect = m_varRect;
  91. m_imgRect.x = m_varRect.xMax - 16 * drawInfo.InvertedZoom;
  92. m_imgRect.width = 16 * drawInfo.InvertedZoom;
  93. m_imgRect.height = m_imgRect.width;
  94. }
  95. public override void DrawGUIControls( DrawInfo drawInfo )
  96. {
  97. base.DrawGUIControls( drawInfo );
  98. if( drawInfo.CurrentEventType != EventType.MouseDown )
  99. return;
  100. if( m_varRect.Contains( drawInfo.MousePosition ) )
  101. {
  102. m_editing = true;
  103. }
  104. else if( m_editing )
  105. {
  106. m_editing = false;
  107. }
  108. }
  109. public override void Draw( DrawInfo drawInfo )
  110. {
  111. base.Draw( drawInfo );
  112. if( m_editing )
  113. {
  114. EditorGUI.BeginChangeCheck();
  115. m_currentSelectedInput = EditorGUIIntPopup( m_varRect, m_currentSelectedInput, m_availableInputsLabels, m_availableInputsValues, UIUtils.GraphDropDown );
  116. if( EditorGUI.EndChangeCheck() )
  117. {
  118. PreviewIsDirty = true;
  119. m_editing = false;
  120. }
  121. }
  122. }
  123. public override void OnNodeRepaint( DrawInfo drawInfo )
  124. {
  125. base.OnNodeRepaint( drawInfo );
  126. if( !m_isVisible )
  127. return;
  128. if( !m_editing && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  129. {
  130. GUI.Label( m_varRect, m_availableInputsLabels[ m_currentSelectedInput ], UIUtils.GraphDropDown );
  131. GUI.Label( m_imgRect, m_popContent, UIUtils.GraphButtonIcon );
  132. }
  133. }
  134. public override void DrawProperties()
  135. {
  136. base.DrawProperties();
  137. NodeUtils.DrawPropertyGroup( ref m_propertiesFoldout, Constants.ParameterLabelStr, DrawDebugOptions );
  138. EditorGUILayout.HelpBox( Info, MessageType.Warning );
  139. }
  140. void DrawDebugOptions()
  141. {
  142. EditorGUI.BeginChangeCheck();
  143. m_maxAmountInputs = EditorGUILayoutIntSlider( MaxAmountStr, m_maxAmountInputs, 2, MaxAllowedAmount );
  144. if( EditorGUI.EndChangeCheck() )
  145. {
  146. for( int i = 0; i < MaxAllowedAmount; i++ )
  147. {
  148. m_inputPorts[ i ].Visible = ( i < m_maxAmountInputs );
  149. }
  150. if( m_currentSelectedInput >= m_maxAmountInputs )
  151. {
  152. m_currentSelectedInput = m_maxAmountInputs - 1;
  153. }
  154. UpdateLabels();
  155. m_sizeIsDirty = true;
  156. }
  157. m_currentSelectedInput = EditorGUILayoutIntPopup( CurrSelectedStr, m_currentSelectedInput, m_availableInputsLabels, m_availableInputsValues );
  158. }
  159. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  160. {
  161. return m_inputPorts[ m_currentSelectedInput ].GeneratePortInstructions( ref dataCollector );
  162. }
  163. public override void ReadFromString( ref string[] nodeParams )
  164. {
  165. base.ReadFromString( ref nodeParams );
  166. m_currentSelectedInput = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  167. m_maxAmountInputs = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  168. for( int i = 0; i < MaxAllowedAmount; i++ )
  169. {
  170. m_inputPorts[ i ].Visible = ( i < m_maxAmountInputs );
  171. }
  172. if( m_currentSelectedInput >= m_maxAmountInputs )
  173. {
  174. m_currentSelectedInput = m_maxAmountInputs - 1;
  175. }
  176. UpdateLabels();
  177. m_sizeIsDirty = true;
  178. }
  179. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  180. {
  181. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  182. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentSelectedInput );
  183. IOUtils.AddFieldValueToString( ref nodeInfo, m_maxAmountInputs );
  184. }
  185. }
  186. }