NandNode.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. namespace AmplifyShaderEditor
  5. {
  6. [Serializable]
  7. [NodeAttributes( "Nand", "Logical Operators", "Returns 1 if both the inputs A and B are 0.", tags: "not and" )]
  8. public sealed class NandNode : ParentNode
  9. {
  10. [UnityEngine.SerializeField]
  11. private WirePortDataType m_mainDataType = WirePortDataType.FLOAT;
  12. protected override void CommonInit( int uniqueId )
  13. {
  14. base.CommonInit( uniqueId );
  15. AddInputPort( WirePortDataType.FLOAT, false, "A" );
  16. AddInputPort( WirePortDataType.FLOAT, false, "B" );
  17. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  18. m_useInternalPortData = true;
  19. m_previewShaderGUID = "04fb2bacb454a424d8cfeff7d95cca52";
  20. }
  21. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  22. {
  23. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  24. UpdateConnection( portId );
  25. }
  26. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  27. {
  28. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  29. UpdateConnection( inputPortId );
  30. }
  31. public override void OnInputPortDisconnected( int portId )
  32. {
  33. base.OnInputPortDisconnected( portId );
  34. UpdateConnection( portId );
  35. }
  36. void UpdateConnection( int portId )
  37. {
  38. WirePortDataType type1 = WirePortDataType.FLOAT;
  39. if ( m_inputPorts[ 0 ].IsConnected )
  40. type1 = m_inputPorts[ 0 ].GetOutputConnection( 0 ).DataType;
  41. WirePortDataType type2 = WirePortDataType.FLOAT;
  42. if ( m_inputPorts[ 1 ].IsConnected )
  43. type2 = m_inputPorts[ 1 ].GetOutputConnection( 0 ).DataType;
  44. m_mainDataType = UIUtils.GetPriority( type1 ) > UIUtils.GetPriority( type2 ) ? type1 : type2;
  45. if ( !m_inputPorts[ 0 ].IsConnected && !m_inputPorts[ 1 ].IsConnected && m_inputPorts[ 2 ].IsConnected )
  46. m_mainDataType = m_inputPorts[ 2 ].GetOutputConnection( 0 ).DataType;
  47. m_inputPorts[ 0 ].ChangeType( m_mainDataType, false );
  48. m_inputPorts[ 1 ].ChangeType( m_mainDataType, false );
  49. m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
  50. }
  51. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  52. {
  53. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  54. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  55. var A = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  56. var B = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  57. var outputType = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_outputPorts[ 0 ].DataType );
  58. string result = string.Format( "( ( {0} )( !{1} && !{2} ) )", outputType, A, B );
  59. return CreateOutputLocalVariable( 0, result, ref dataCollector );
  60. }
  61. }
  62. }