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.

133 lines
4.8 KiB

  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( "Keyword Switch", "Logical Operators", "Attributes a value according to the existance of a selected keyword", Deprecated = true, DeprecatedAlternativeType = typeof(StaticSwitch), DeprecatedAlternative = "Static Switch" )]
  10. public sealed class KeywordSwitchNode : ParentNode
  11. {
  12. private const string KeywordStr = "Keyword";
  13. private const string CustomStr = "Custom";
  14. [SerializeField]
  15. private string m_currentKeyword = string.Empty;
  16. [SerializeField]
  17. private int m_currentKeywordId = 0;
  18. [SerializeField]
  19. private WirePortDataType m_mainPortType = WirePortDataType.FLOAT;
  20. protected override void CommonInit( int uniqueId )
  21. {
  22. base.CommonInit( uniqueId );
  23. AddInputPort( WirePortDataType.FLOAT, false, "True" );
  24. AddInputPort( WirePortDataType.FLOAT, false, "False" );
  25. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  26. m_textLabelWidth = 65;
  27. }
  28. public override void DrawProperties()
  29. {
  30. base.DrawProperties();
  31. EditorGUI.BeginChangeCheck();
  32. m_currentKeywordId = EditorGUILayoutPopup( KeywordStr, m_currentKeywordId, UIUtils.AvailableKeywords );
  33. if ( EditorGUI.EndChangeCheck() )
  34. {
  35. if ( m_currentKeywordId != 0 )
  36. {
  37. m_currentKeyword = UIUtils.AvailableKeywords[ m_currentKeywordId ];
  38. }
  39. }
  40. if ( m_currentKeywordId == 0 )
  41. {
  42. m_currentKeyword = EditorGUILayoutTextField( CustomStr, m_currentKeyword );
  43. }
  44. }
  45. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  46. {
  47. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  48. UpdateConnected( portId );
  49. }
  50. public override void OnConnectedOutputNodeChanges( int portId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  51. {
  52. base.OnConnectedOutputNodeChanges( portId, otherNodeId, otherPortId, name, type );
  53. UpdateConnected( portId );
  54. }
  55. public override void OnInputPortDisconnected( int portId )
  56. {
  57. base.OnInputPortDisconnected( portId );
  58. UpdateDisconnected( portId );
  59. }
  60. void UpdateConnected( int portId )
  61. {
  62. m_inputPorts[ portId ].MatchPortToConnection();
  63. int otherPortId = ( portId + 1 ) % 2;
  64. if ( m_inputPorts[ otherPortId ].IsConnected )
  65. {
  66. m_mainPortType = ( UIUtils.GetPriority( m_inputPorts[ portId ].DataType ) > UIUtils.GetPriority( m_inputPorts[ otherPortId ].DataType ) ) ?
  67. m_inputPorts[ portId ].DataType :
  68. m_inputPorts[ otherPortId ].DataType;
  69. }
  70. else
  71. {
  72. m_mainPortType = m_inputPorts[ portId ].DataType;
  73. m_inputPorts[ otherPortId ].ChangeType( m_mainPortType, false );
  74. }
  75. m_outputPorts[ 0 ].ChangeType( m_mainPortType, false );
  76. }
  77. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  78. {
  79. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  80. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  81. string trueCode = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  82. string falseCode = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  83. string localVarName = "simpleKeywordVar"+OutputId;
  84. string outType = UIUtils.PrecisionWirePortToCgType( m_currentPrecisionType, m_outputPorts[ 0 ].DataType );
  85. dataCollector.AddLocalVariable( UniqueId, "#ifdef " + m_currentKeyword, true );
  86. dataCollector.AddLocalVariable( UniqueId, outType + " " + localVarName + " = " + trueCode + ";", true );
  87. dataCollector.AddLocalVariable( UniqueId, "#else", true );
  88. dataCollector.AddLocalVariable( UniqueId, outType + " " + localVarName + " = " + falseCode + ";", true );
  89. dataCollector.AddLocalVariable( UniqueId, "#endif", true );
  90. m_outputPorts[ 0 ].SetLocalValue( localVarName, dataCollector.PortCategory );
  91. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  92. }
  93. void UpdateDisconnected( int portId )
  94. {
  95. int otherPortId = ( portId + 1 ) % 2;
  96. if ( m_inputPorts[ otherPortId ].IsConnected )
  97. {
  98. m_mainPortType = m_inputPorts[ otherPortId ].DataType;
  99. m_inputPorts[ portId ].ChangeType( m_mainPortType, false );
  100. }
  101. m_outputPorts[ 0 ].ChangeType( m_mainPortType, false );
  102. }
  103. public override void ReadFromString( ref string[] nodeParams )
  104. {
  105. base.ReadFromString( ref nodeParams );
  106. m_currentKeyword = GetCurrentParam( ref nodeParams );
  107. m_currentKeywordId = UIUtils.GetKeywordId( m_currentKeyword );
  108. }
  109. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  110. {
  111. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  112. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentKeyword );
  113. }
  114. }
  115. }