AnyOpNode.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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( "Any", "Logical Operators", "Determines if any components of the specified value are non-zero." )]
  8. public sealed class AnyOpNode : ParentNode
  9. {
  10. protected override void CommonInit( int uniqueId )
  11. {
  12. base.CommonInit( uniqueId );
  13. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue );
  14. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  15. m_useInternalPortData = true;
  16. m_previewShaderGUID = "a1e114ebddab3b347a7c5753c1cdb9cb";
  17. }
  18. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  19. {
  20. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  21. m_inputPorts[ 0 ].MatchPortToConnection();
  22. m_outputPorts[ 0 ].ChangeType( InputPorts[ 0 ].DataType, false );
  23. }
  24. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  25. {
  26. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  27. m_inputPorts[ 0 ].MatchPortToConnection();
  28. m_outputPorts[ 0 ].ChangeType( InputPorts[ 0 ].DataType, false );
  29. }
  30. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  31. {
  32. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  33. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  34. var inputValue = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  35. var outputType = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_outputPorts[ 0 ].DataType );
  36. string result = string.Format( "( ( {0} )( any( {1} ) ? 1 : 0 ) )", outputType, inputValue );
  37. return CreateOutputLocalVariable( 0, result, ref dataCollector );
  38. }
  39. }
  40. }