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.

78 lines
3.3 KiB

  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( "Scale And Offset", "Math Operators", "Scales and offsets an input value\n( ( <b>Value</b> * <b>Scale</b> ) + <b>Offset</b> )" )]
  8. public sealed class ScaleAndOffsetNode : ParentNode
  9. {
  10. private const string ScaleOffsetOpStr = "({0}*{1} + {2})";
  11. protected override void CommonInit( int uniqueId )
  12. {
  13. base.CommonInit( uniqueId );
  14. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue );
  15. AddInputPort( WirePortDataType.FLOAT, false, "Scale" );
  16. m_inputPorts[ 1 ].FloatInternalData = 1;
  17. AddInputPort( WirePortDataType.FLOAT, false, "Offset" );
  18. AddOutputPort( WirePortDataType.FLOAT, " " );
  19. m_useInternalPortData = true;
  20. m_previewShaderGUID = "a1f1053d4d9c3be439e0382038b74771";
  21. }
  22. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  23. {
  24. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  25. UpdateConnection( 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. UpdateConnection( inputPortId );
  31. }
  32. public override void OnInputPortDisconnected( int portId )
  33. {
  34. base.OnInputPortDisconnected( portId );
  35. m_inputPorts[ portId ].ChangeType( WirePortDataType.FLOAT, false );
  36. if( portId == 0 )
  37. {
  38. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT, false );
  39. }
  40. }
  41. void UpdateConnection( int portId )
  42. {
  43. if( portId == 0 )
  44. {
  45. m_inputPorts[ 0 ].MatchPortToConnection();
  46. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  47. }
  48. else
  49. {
  50. WirePortDataType newDataType = m_inputPorts[ portId ].ConnectionType() == WirePortDataType.FLOAT ? WirePortDataType.FLOAT : m_outputPorts[ 0 ].DataType;
  51. m_inputPorts[ portId ].ChangeType( newDataType, false );
  52. }
  53. }
  54. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  55. {
  56. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  57. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  58. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  59. // If scale or offset ports are floats then there's no need to cast them to any other type since they can be multiplied with everything
  60. WirePortDataType scaleType = ( m_inputPorts[ 1 ].ConnectionType() == WirePortDataType.FLOAT ) ? WirePortDataType.FLOAT : m_outputPorts[ 0 ].DataType;
  61. string scale = m_inputPorts[ 1 ].GenerateShaderForOutput( ref dataCollector, scaleType, ignoreLocalvar , true );
  62. WirePortDataType offsetType = ( m_inputPorts[ 2 ].ConnectionType() == WirePortDataType.FLOAT ) ? WirePortDataType.FLOAT : m_outputPorts[ 0 ].DataType;
  63. string offset = m_inputPorts[ 2 ].GenerateShaderForOutput( ref dataCollector, offsetType, ignoreLocalvar, true );
  64. string result = string.Format( ScaleOffsetOpStr, value, scale, offset );
  65. return CreateOutputLocalVariable( 0, result, ref dataCollector );
  66. }
  67. }
  68. }