You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.4 KiB

  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 LerpOp = "lerp({0},{1},{2})";
  15. [SerializeField]
  16. private string[] AvailableInputsLabels = { "In 0", "In 1" };
  17. [SerializeField]
  18. private int[] AvailableInputsValues = { 0, 1 };
  19. [SerializeField]
  20. private int m_currentSelectedInput = 0;
  21. [SerializeField]
  22. private WirePortDataType m_mainDataType = WirePortDataType.FLOAT;
  23. private int m_cachedPropertyId = -1;
  24. private GUIContent m_popContent;
  25. private Rect m_varRect;
  26. private Rect m_imgRect;
  27. private bool m_editing;
  28. protected override void CommonInit( int uniqueId )
  29. {
  30. base.CommonInit( uniqueId );
  31. AddInputPort( m_mainDataType, false, InputPortName + "0" );
  32. AddInputPort( m_mainDataType, false, InputPortName + "1" );
  33. AddOutputPort( m_mainDataType, " " );
  34. m_insideSize.Set( 80, 25 );
  35. m_currentParameterType = PropertyType.Property;
  36. m_customPrefix = "Toggle Switch";
  37. m_popContent = new GUIContent();
  38. m_popContent.image = UIUtils.PopupIcon;
  39. m_availableAttribs.Clear();
  40. m_availableAttribs.Add( new PropertyAttributes( "Toggle", "[Toggle]" ) );
  41. m_drawAttributes = false;
  42. m_freeType = false;
  43. m_useVarSubtitle = true;
  44. m_useInternalPortData = true;
  45. m_previewShaderGUID = "beeb138daeb592a4887454f81dba2b3f";
  46. m_allowPropertyDuplicates = true;
  47. m_showAutoRegisterUI = false;
  48. }
  49. protected override void OnUniqueIDAssigned()
  50. {
  51. base.OnUniqueIDAssigned();
  52. UIUtils.RegisterPropertyNode( this );
  53. }
  54. public override void SetPreviewInputs()
  55. {
  56. base.SetPreviewInputs();
  57. if ( m_cachedPropertyId == -1 )
  58. m_cachedPropertyId = Shader.PropertyToID( "_Current" );
  59. PreviewMaterial.SetInt( m_cachedPropertyId, m_currentSelectedInput );
  60. }
  61. public override void OnConnectedOutputNodeChanges( int portId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  62. {
  63. base.OnConnectedOutputNodeChanges( portId, otherNodeId, otherPortId, name, type );
  64. UpdateConnection();
  65. }
  66. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  67. {
  68. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  69. UpdateConnection();
  70. }
  71. public override void OnInputPortDisconnected( int portId )
  72. {
  73. base.OnInputPortDisconnected( portId );
  74. UpdateConnection();
  75. }
  76. void UpdateConnection()
  77. {
  78. WirePortDataType type1 = WirePortDataType.FLOAT;
  79. if( m_inputPorts[ 0 ].IsConnected )
  80. type1 = m_inputPorts[ 0 ].GetOutputConnection( 0 ).DataType;
  81. WirePortDataType type2 = WirePortDataType.FLOAT;
  82. if( m_inputPorts[ 1 ].IsConnected )
  83. type2 = m_inputPorts[ 1 ].GetOutputConnection( 0 ).DataType;
  84. m_mainDataType = UIUtils.GetPriority( type1 ) > UIUtils.GetPriority( type2 ) ? type1 : type2;
  85. m_inputPorts[ 0 ].ChangeType( m_mainDataType, false );
  86. m_inputPorts[ 1 ].ChangeType( m_mainDataType, false );
  87. //m_outputPorts[ 0 ].ChangeProperties( m_out, m_mainDataType, false );
  88. m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
  89. }
  90. public override void OnNodeLayout( DrawInfo drawInfo )
  91. {
  92. base.OnNodeLayout( drawInfo );
  93. m_varRect = m_remainingBox;
  94. m_varRect.width = 50 * drawInfo.InvertedZoom;
  95. m_varRect.height = 16 * drawInfo.InvertedZoom;
  96. m_varRect.x = m_remainingBox.xMax - m_varRect.width;
  97. m_varRect.y += 1 * drawInfo.InvertedZoom;
  98. m_imgRect = m_varRect;
  99. m_imgRect.x = m_varRect.xMax - 16 * drawInfo.InvertedZoom;
  100. m_imgRect.width = 16 * drawInfo.InvertedZoom;
  101. m_imgRect.height = m_imgRect.width;
  102. }
  103. public override void DrawGUIControls( DrawInfo drawInfo )
  104. {
  105. base.DrawGUIControls( drawInfo );
  106. if ( drawInfo.CurrentEventType != EventType.MouseDown )
  107. return;
  108. if ( m_varRect.Contains( drawInfo.MousePosition ) )
  109. {
  110. m_editing = true;
  111. }
  112. else if ( m_editing )
  113. {
  114. m_editing = false;
  115. }
  116. }
  117. public override void Draw( DrawInfo drawInfo )
  118. {
  119. base.Draw( drawInfo );
  120. if( m_editing )
  121. {
  122. EditorGUI.BeginChangeCheck();
  123. m_currentSelectedInput = EditorGUIIntPopup( m_varRect, m_currentSelectedInput, AvailableInputsLabels, AvailableInputsValues, UIUtils.SwitchNodePopUp );
  124. if ( EditorGUI.EndChangeCheck() )
  125. {
  126. UpdateConnection();
  127. m_requireMaterialUpdate = true;
  128. m_editing = false;
  129. }
  130. }
  131. }
  132. public override void OnNodeRepaint( DrawInfo drawInfo )
  133. {
  134. base.OnNodeRepaint( drawInfo );
  135. if ( !m_isVisible )
  136. return;
  137. if ( !m_editing && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  138. {
  139. GUI.Label( m_varRect, AvailableInputsLabels[ m_currentSelectedInput ], UIUtils.GraphDropDown );
  140. GUI.Label( m_imgRect, m_popContent, UIUtils.GraphButtonIcon );
  141. }
  142. }
  143. public override void DrawMainPropertyBlock()
  144. {
  145. base.DrawMainPropertyBlock();
  146. EditorGUILayout.Separator();
  147. EditorGUI.BeginChangeCheck();
  148. m_currentSelectedInput = EditorGUILayoutIntPopup( CurrSelectedStr, m_currentSelectedInput, AvailableInputsLabels, AvailableInputsValues );
  149. if ( EditorGUI.EndChangeCheck() )
  150. {
  151. UpdateConnection();
  152. m_requireMaterialUpdate = true;
  153. }
  154. }
  155. public override void DrawProperties()
  156. {
  157. base.DrawProperties();
  158. NodeUtils.DrawPropertyGroup( ref m_visibleCustomAttrFoldout, CustomAttrStr, DrawCustomAttributes, DrawCustomAttrAddRemoveButtons );
  159. }
  160. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  161. {
  162. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  163. m_precisionString = UIUtils.FinalPrecisionWirePortToCgType( m_currentPrecisionType, m_outputPorts[ 0 ].DataType );
  164. string resultA = m_inputPorts[ 0 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalvar, true );
  165. string resultB = m_inputPorts[ 1 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalvar, true );
  166. return string.Format( LerpOp, resultA, resultB, m_propertyName );
  167. }
  168. public override void ReadFromString( ref string[] nodeParams )
  169. {
  170. base.ReadFromString( ref nodeParams );
  171. m_currentSelectedInput = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  172. }
  173. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  174. {
  175. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  176. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentSelectedInput );
  177. }
  178. public override void RefreshExternalReferences()
  179. {
  180. base.RefreshExternalReferences();
  181. UpdateConnection();
  182. }
  183. public override string GetPropertyValue()
  184. {
  185. return PropertyAttributes + "[Toggle]" + m_propertyName + "(\"" + m_propertyInspectorName + "\", Float) = " + m_currentSelectedInput;
  186. }
  187. public override string GetUniformValue()
  188. {
  189. int index = m_containerGraph.IsSRP ? 1 : 0;
  190. return string.Format( Constants.UniformDec[ index ], UIUtils.FinalPrecisionWirePortToCgType( m_currentPrecisionType, WirePortDataType.FLOAT ), m_propertyName );
  191. }
  192. public override bool GetUniformData( out string dataType, out string dataName )
  193. {
  194. dataType = UIUtils.FinalPrecisionWirePortToCgType( m_currentPrecisionType, WirePortDataType.FLOAT );
  195. dataName = m_propertyName;
  196. return true;
  197. }
  198. public override void UpdateMaterial( Material mat )
  199. {
  200. base.UpdateMaterial( mat );
  201. if ( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
  202. {
  203. mat.SetFloat( m_propertyName, ( float ) m_currentSelectedInput );
  204. }
  205. }
  206. public override void SetMaterialMode( Material mat , bool fetchMaterialValues )
  207. {
  208. base.SetMaterialMode( mat , fetchMaterialValues );
  209. if ( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) && mat.HasProperty( m_propertyName ) )
  210. {
  211. m_currentSelectedInput = ( int ) mat.GetFloat( m_propertyName );
  212. }
  213. }
  214. public override void ForceUpdateFromMaterial( Material material )
  215. {
  216. if ( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( m_propertyName ) )
  217. m_currentSelectedInput = ( int ) material.GetFloat( m_propertyName );
  218. }
  219. public override string GetPropertyValStr()
  220. {
  221. return PropertyName; //return m_currentSelectedInput.ToString();
  222. }
  223. }
  224. }