ToggleSwitchNode.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Toggle Switch", "Logical Operators", "Switch between any of its input ports" )]
  10. public class ToggleSwitchNode : PropertyNode
  11. {
  12. private const string InputPortName = "In ";
  13. private const string CurrSelectedStr = "Toggle Value";
  14. private const string GenerateKeywordStr = "Generate Keyword";
  15. //private const string LerpOp = "lerp({0},{1},{2})";
  16. private const string LerpOp = "(( {2} )?( {1} ):( {0} ))";
  17. [SerializeField]
  18. private string[] AvailableInputsLabels = { "In 0", "In 1" };
  19. [SerializeField]
  20. private int[] AvailableInputsValues = { 0, 1 };
  21. [SerializeField]
  22. private int m_currentSelectedInput = 0;
  23. [SerializeField]
  24. private WirePortDataType m_mainDataType = WirePortDataType.FLOAT;
  25. [SerializeField]
  26. private bool m_generateKeyword = true;
  27. private int m_cachedPropertyId = -1;
  28. private GUIContent m_popContent;
  29. private Rect m_varRect;
  30. private Rect m_imgRect;
  31. private bool m_editing;
  32. protected override void CommonInit( int uniqueId )
  33. {
  34. base.CommonInit( uniqueId );
  35. AddInputPort( m_mainDataType, false, InputPortName + "0" );
  36. AddInputPort( m_mainDataType, false, InputPortName + "1" );
  37. AddOutputPort( m_mainDataType, " " );
  38. m_insideSize.Set( 80, 25 );
  39. m_currentParameterType = PropertyType.Property;
  40. m_customPrefix = "Toggle Switch";
  41. m_popContent = new GUIContent();
  42. m_popContent.image = UIUtils.PopupIcon;
  43. m_availableAttribs.Clear();
  44. //Need to maintain this because of retrocompatibility reasons
  45. m_availableAttribs.Add( new PropertyAttributes( "Toggle", "[Toggle]" ) );
  46. m_drawAttributes = false;
  47. m_freeType = false;
  48. m_useVarSubtitle = true;
  49. m_useInternalPortData = true;
  50. m_previewShaderGUID = "beeb138daeb592a4887454f81dba2b3f";
  51. m_allowPropertyDuplicates = true;
  52. m_showAutoRegisterUI = false;
  53. m_srpBatcherCompatible = true;
  54. }
  55. protected override void OnUniqueIDAssigned()
  56. {
  57. base.OnUniqueIDAssigned();
  58. UIUtils.RegisterPropertyNode( this );
  59. }
  60. public override void SetPreviewInputs()
  61. {
  62. base.SetPreviewInputs();
  63. if ( m_cachedPropertyId == -1 )
  64. m_cachedPropertyId = Shader.PropertyToID( "_Current" );
  65. PreviewMaterial.SetInt( m_cachedPropertyId, m_currentSelectedInput );
  66. }
  67. public override void OnConnectedOutputNodeChanges( int portId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  68. {
  69. base.OnConnectedOutputNodeChanges( portId, otherNodeId, otherPortId, name, type );
  70. UpdateConnection();
  71. }
  72. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  73. {
  74. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  75. UpdateConnection();
  76. }
  77. public override void OnInputPortDisconnected( int portId )
  78. {
  79. base.OnInputPortDisconnected( portId );
  80. UpdateConnection();
  81. }
  82. void UpdateConnection()
  83. {
  84. WirePortDataType type1 = WirePortDataType.FLOAT;
  85. if( m_inputPorts[ 0 ].IsConnected )
  86. type1 = m_inputPorts[ 0 ].GetOutputConnection( 0 ).DataType;
  87. WirePortDataType type2 = WirePortDataType.FLOAT;
  88. if( m_inputPorts[ 1 ].IsConnected )
  89. type2 = m_inputPorts[ 1 ].GetOutputConnection( 0 ).DataType;
  90. m_mainDataType = UIUtils.GetPriority( type1 ) > UIUtils.GetPriority( type2 ) ? type1 : type2;
  91. m_inputPorts[ 0 ].ChangeType( m_mainDataType, false );
  92. m_inputPorts[ 1 ].ChangeType( m_mainDataType, false );
  93. //m_outputPorts[ 0 ].ChangeProperties( m_out, m_mainDataType, false );
  94. m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
  95. }
  96. public override void OnNodeLayout( DrawInfo drawInfo )
  97. {
  98. base.OnNodeLayout( drawInfo );
  99. m_varRect = m_remainingBox;
  100. m_varRect.width = 50 * drawInfo.InvertedZoom;
  101. m_varRect.height = 16 * drawInfo.InvertedZoom;
  102. m_varRect.x = m_remainingBox.xMax - m_varRect.width;
  103. m_varRect.y += 1 * drawInfo.InvertedZoom;
  104. m_imgRect = m_varRect;
  105. m_imgRect.x = m_varRect.xMax - 16 * drawInfo.InvertedZoom;
  106. m_imgRect.width = 16 * drawInfo.InvertedZoom;
  107. m_imgRect.height = m_imgRect.width;
  108. }
  109. public override void DrawGUIControls( DrawInfo drawInfo )
  110. {
  111. base.DrawGUIControls( drawInfo );
  112. if ( drawInfo.CurrentEventType != EventType.MouseDown )
  113. return;
  114. if ( m_varRect.Contains( drawInfo.MousePosition ) )
  115. {
  116. m_editing = true;
  117. }
  118. else if ( m_editing )
  119. {
  120. m_editing = false;
  121. }
  122. }
  123. public override void Draw( DrawInfo drawInfo )
  124. {
  125. base.Draw( drawInfo );
  126. if( m_editing )
  127. {
  128. EditorGUI.BeginChangeCheck();
  129. m_currentSelectedInput = EditorGUIIntPopup( m_varRect, m_currentSelectedInput, AvailableInputsLabels, AvailableInputsValues, UIUtils.SwitchNodePopUp );
  130. if ( EditorGUI.EndChangeCheck() )
  131. {
  132. PreviewIsDirty = true;
  133. UpdateConnection();
  134. m_requireMaterialUpdate = true;
  135. m_editing = false;
  136. }
  137. }
  138. }
  139. public override void OnNodeRepaint( DrawInfo drawInfo )
  140. {
  141. base.OnNodeRepaint( drawInfo );
  142. if ( !m_isVisible )
  143. return;
  144. if ( !m_editing && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  145. {
  146. GUI.Label( m_varRect, AvailableInputsLabels[ m_currentSelectedInput ], UIUtils.GraphDropDown );
  147. GUI.Label( m_imgRect, m_popContent, UIUtils.GraphButtonIcon );
  148. }
  149. }
  150. public override void DrawMainPropertyBlock()
  151. {
  152. EditorGUILayout.BeginVertical();
  153. {
  154. ShowPropertyInspectorNameGUI();
  155. ShowPropertyNameGUI( true );
  156. ShowVariableMode();
  157. ShowHybridInstanced();
  158. ShowAutoRegister();
  159. ShowPrecision();
  160. m_generateKeyword = EditorGUILayoutToggle( GenerateKeywordStr, m_generateKeyword );
  161. ShowToolbar();
  162. }
  163. EditorGUILayout.EndVertical();
  164. EditorGUILayout.Separator();
  165. EditorGUI.BeginChangeCheck();
  166. m_currentSelectedInput = EditorGUILayoutIntPopup( CurrSelectedStr, m_currentSelectedInput, AvailableInputsLabels, AvailableInputsValues );
  167. if ( EditorGUI.EndChangeCheck() )
  168. {
  169. UpdateConnection();
  170. m_requireMaterialUpdate = true;
  171. }
  172. }
  173. public override void DrawProperties()
  174. {
  175. base.DrawProperties();
  176. NodeUtils.DrawPropertyGroup( ref m_visibleCustomAttrFoldout, CustomAttrStr, DrawCustomAttributes, DrawCustomAttrAddRemoveButtons );
  177. }
  178. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  179. {
  180. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  181. m_precisionString = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_outputPorts[ 0 ].DataType );
  182. string resultA = m_inputPorts[ 0 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalvar, true );
  183. string resultB = m_inputPorts[ 1 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalvar, true );
  184. return string.Format( LerpOp, resultA, resultB, m_propertyName );
  185. }
  186. public override void ReadFromString( ref string[] nodeParams )
  187. {
  188. base.ReadFromString( ref nodeParams );
  189. m_currentSelectedInput = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  190. if( UIUtils.CurrentShaderVersion() > 18806 )
  191. {
  192. m_generateKeyword = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  193. }
  194. }
  195. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  196. {
  197. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  198. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentSelectedInput );
  199. IOUtils.AddFieldValueToString( ref nodeInfo, m_generateKeyword );
  200. }
  201. public override void RefreshExternalReferences()
  202. {
  203. base.RefreshExternalReferences();
  204. m_selectedAttribs.Clear();
  205. UpdateConnection();
  206. }
  207. public override string GetPropertyValue()
  208. {
  209. string toggleAttribute = ( m_generateKeyword ) ? "[Toggle]":"[ToggleUI]";
  210. return PropertyAttributes + toggleAttribute + m_propertyName + "(\"" + m_propertyInspectorName + "\", Float) = " + m_currentSelectedInput;
  211. }
  212. public override string GetUniformValue()
  213. {
  214. int index = m_containerGraph.IsSRP ? 1 : 0;
  215. return string.Format( Constants.UniformDec[ index ], UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, WirePortDataType.FLOAT ), m_propertyName );
  216. }
  217. public override bool GetUniformData( out string dataType, out string dataName, ref bool fullValue )
  218. {
  219. dataType = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, WirePortDataType.FLOAT );
  220. dataName = m_propertyName;
  221. return true;
  222. }
  223. public override void UpdateMaterial( Material mat )
  224. {
  225. base.UpdateMaterial( mat );
  226. if ( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
  227. {
  228. mat.SetFloat( m_propertyName, ( float ) m_currentSelectedInput );
  229. }
  230. }
  231. public override void SetMaterialMode( Material mat , bool fetchMaterialValues )
  232. {
  233. base.SetMaterialMode( mat , fetchMaterialValues );
  234. if ( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) && mat.HasProperty( m_propertyName ) )
  235. {
  236. m_currentSelectedInput = ( int ) mat.GetFloat( m_propertyName );
  237. }
  238. }
  239. public override void ForceUpdateFromMaterial( Material material )
  240. {
  241. if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( m_propertyName ) )
  242. {
  243. m_currentSelectedInput = (int)material.GetFloat( m_propertyName );
  244. PreviewIsDirty = true;
  245. }
  246. }
  247. public override string GetPropertyValStr()
  248. {
  249. return PropertyName; //return m_currentSelectedInput.ToString();
  250. }
  251. }
  252. }