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.9 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //
  4. // Custom Node If
  5. // Donated by The Four Headed Cat - @fourheadedcat
  6. using UnityEngine;
  7. using System;
  8. namespace AmplifyShaderEditor
  9. {
  10. [Serializable]
  11. [NodeAttributes( "If [Community]", "Logical Operators", "Compare A with B. If A is greater than B output the value of A > B port. If A is equal to B output the value of A == B port. If A is lower than B output the value of A < B port. Equal Threshold parameter will be used to check A == B adding and subtracting this value to A.", null, KeyCode.None, true, false, null, null, "The Four Headed Cat - @fourheadedcat" )]
  12. public sealed class TFHCIf : ParentNode
  13. {
  14. private WirePortDataType m_inputMainDataType = WirePortDataType.FLOAT;
  15. private WirePortDataType m_outputMainDataType = WirePortDataType.FLOAT;
  16. protected override void CommonInit( int uniqueId )
  17. {
  18. base.CommonInit( uniqueId );
  19. AddInputPort( WirePortDataType.FLOAT, false, "A" );
  20. AddInputPort( WirePortDataType.FLOAT, false, "B" );
  21. AddInputPort( WirePortDataType.FLOAT, false, "A > B" );
  22. AddInputPort( WirePortDataType.FLOAT, false, "A == B" );
  23. AddInputPort( WirePortDataType.FLOAT, false, "A < B" );
  24. AddInputPort( WirePortDataType.FLOAT, false, "Equal Threshold" );
  25. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  26. m_textLabelWidth = 110;
  27. m_useInternalPortData = true;
  28. m_previewShaderGUID = "5c7bc7e3cab81da499e4864ace0d86c5";
  29. }
  30. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  31. {
  32. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  33. UpdateConnection( inputPortId );
  34. }
  35. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  36. {
  37. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  38. UpdateConnection( portId );
  39. }
  40. public override void OnInputPortDisconnected( int portId )
  41. {
  42. UpdateConnection( portId );
  43. }
  44. void TestMainInputDataType()
  45. {
  46. WirePortDataType newType = WirePortDataType.FLOAT;
  47. if( m_inputPorts[ 0 ].IsConnected && UIUtils.GetPriority( m_inputPorts[ 0 ].DataType ) > UIUtils.GetPriority( newType ) )
  48. {
  49. newType = m_inputPorts[ 0 ].DataType;
  50. }
  51. if( m_inputPorts[ 1 ].IsConnected && ( UIUtils.GetPriority( m_inputPorts[ 1 ].DataType ) > UIUtils.GetPriority( newType ) ) )
  52. {
  53. newType = m_inputPorts[ 1 ].DataType;
  54. }
  55. if( m_inputPorts[ 5 ].IsConnected && ( UIUtils.GetPriority( m_inputPorts[ 5 ].DataType ) > UIUtils.GetPriority( newType ) ) )
  56. {
  57. newType = m_inputPorts[ 5 ].DataType;
  58. }
  59. m_inputMainDataType = newType;
  60. }
  61. void TestMainOutputDataType()
  62. {
  63. WirePortDataType newType = WirePortDataType.FLOAT;
  64. for( int i = 2; i < 5; i++ )
  65. {
  66. if( m_inputPorts[ i ].IsConnected && ( UIUtils.GetPriority( m_inputPorts[ i ].DataType ) > UIUtils.GetPriority( newType ) ) )
  67. {
  68. newType = m_inputPorts[ i ].DataType;
  69. }
  70. }
  71. if( newType != m_outputMainDataType )
  72. {
  73. m_outputMainDataType = newType;
  74. m_outputPorts[ 0 ].ChangeType( m_outputMainDataType, false );
  75. }
  76. }
  77. public void UpdateConnection( int portId )
  78. {
  79. m_inputPorts[ portId ].MatchPortToConnection();
  80. switch( portId )
  81. {
  82. case 0:
  83. case 1:
  84. case 5:
  85. {
  86. TestMainInputDataType();
  87. }
  88. break;
  89. case 2:
  90. case 3:
  91. case 4:
  92. {
  93. TestMainOutputDataType();
  94. }
  95. break;
  96. }
  97. }
  98. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  99. {
  100. string a = m_inputPorts[ 0 ].GenerateShaderForOutput( ref dataCollector, m_inputMainDataType, ignoreLocalvar, true );
  101. string b = m_inputPorts[ 1 ].GenerateShaderForOutput( ref dataCollector, m_inputMainDataType, ignoreLocalvar, true );
  102. string r1 = m_inputPorts[ 2 ].GenerateShaderForOutput( ref dataCollector, m_outputMainDataType, ignoreLocalvar, true );
  103. string r2 = m_inputPorts[ 3 ].GenerateShaderForOutput( ref dataCollector, m_outputMainDataType, ignoreLocalvar, true );
  104. string r3 = m_inputPorts[ 4 ].GenerateShaderForOutput( ref dataCollector, m_outputMainDataType, ignoreLocalvar, true );
  105. string tr = m_inputPorts[ 5 ].GenerateShaderForOutput( ref dataCollector, m_inputMainDataType, ignoreLocalvar, true );
  106. // No Equal Threshold parameter
  107. //(a > b ? r1 : a == b ? r2 : r3 )
  108. //string strout = " ( " + a + " > " + b + " ? " + r1 + " : " + a + " == " + b + " ? " + r2 + " : " + r3 + " ) ";
  109. // With Equal Threshold parameter
  110. // ( a - tr > b ? r1 : a - tr <= b && a + tr >= b ? r2 : r3 )
  111. string strout = " ( " + a + " - " + tr + " > " + b + " ? " + r1 + " : " + a + " - " + tr + " <= " + b + " && " + a + " + " + tr + " >= " + b + " ? " + r2 + " : " + r3 + " ) ";
  112. //Debug.Log( strout );
  113. return CreateOutputLocalVariable( 0, strout, ref dataCollector );
  114. }
  115. }
  116. }