ReciprocalOpNode.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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( "Reciprocal", "Math Operators", "Reciprocal of scalars and vectors", tags: "rcp recip reciprocal" )]
  8. public sealed class ReciprocalOpNode : 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 = "51c79938d491c8244a633fe407c49327";
  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. protected override void OnUniqueIDAssigned()
  31. {
  32. base.OnUniqueIDAssigned();
  33. }
  34. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  35. {
  36. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  37. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  38. var inputValue = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  39. var localVarType = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_outputPorts[ 0 ].DataType );
  40. var localVarName = "recip" + OutputId;
  41. dataCollector.AddLocalVariable( UniqueId, "#if ( SHADER_TARGET >= 50 )", true );
  42. dataCollector.AddLocalVariable( UniqueId, string.Format( "{0} {1} = rcp( {2} );", localVarType, localVarName, inputValue ) );
  43. dataCollector.AddLocalVariable( UniqueId, "#else", true );
  44. dataCollector.AddLocalVariable( UniqueId, string.Format( "{0} {1} = 1.0 / {2};", localVarType, localVarName, inputValue ) );
  45. dataCollector.AddLocalVariable( UniqueId, "#endif", true );
  46. m_outputPorts[ 0 ].SetLocalValue( localVarName, dataCollector.PortCategory );
  47. return localVarName;
  48. }
  49. }
  50. }