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.

57 lines
2.1 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Power", "Math Operators", "Base to the Exp-th power of scalars and vectors", null, KeyCode.E )]
  9. public sealed class PowerNode : ParentNode
  10. {
  11. protected override void CommonInit( int uniqueId )
  12. {
  13. base.CommonInit( uniqueId );
  14. AddInputPort( WirePortDataType.FLOAT, false, "Base" );
  15. AddInputPort( WirePortDataType.FLOAT, false, "Exp" );
  16. m_inputPorts[ 1 ].FloatInternalData = 1;
  17. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  18. m_useInternalPortData = true;
  19. m_textLabelWidth = 50;
  20. m_previewShaderGUID = "758cc2f8b537b4e4b93d9833075d138c";
  21. }
  22. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  23. {
  24. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  25. UpdateConnections( portId );
  26. }
  27. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  28. {
  29. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  30. UpdateConnections( inputPortId );
  31. }
  32. void UpdateConnections( int inputPort )
  33. {
  34. m_inputPorts[ inputPort ].MatchPortToConnection();
  35. if ( inputPort == 0 )
  36. {
  37. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  38. }
  39. }
  40. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  41. {
  42. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  43. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  44. string x = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  45. string y = m_inputPorts[ 1 ].GenerateShaderForOutput( ref dataCollector, m_inputPorts[ 0 ].DataType, ignoreLocalvar, true );
  46. string result = "pow( " + x + " , " + y + " )";
  47. return CreateOutputLocalVariable( 0, result, ref dataCollector );
  48. }
  49. }
  50. }