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.

103 lines
3.7 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( "Clamp", "Math Operators", "Value clamped to the range [min,max]" )]
  8. public sealed class ClampOpNode : ParentNode
  9. {
  10. protected override void CommonInit( int uniqueId )
  11. {
  12. base.CommonInit( uniqueId );
  13. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue );
  14. AddInputPort( WirePortDataType.FLOAT, false, "Min" );
  15. AddInputPort( WirePortDataType.FLOAT, false, "Max" );
  16. m_inputPorts[ m_inputPorts.Count - 1 ].FloatInternalData = 1;
  17. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  18. m_useInternalPortData = true;
  19. m_textLabelWidth = 55;
  20. m_previewShaderGUID = "ab6163c4b10bfc84da8e3c486520490a";
  21. }
  22. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  23. {
  24. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  25. if ( portId == 0 )
  26. {
  27. m_inputPorts[ 0 ].MatchPortToConnection();
  28. m_inputPorts[ 1 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  29. m_inputPorts[ 2 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  30. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  31. }
  32. //else
  33. //{
  34. // _inputPorts[ portId ].MatchPortToConnection();
  35. //}
  36. }
  37. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  38. {
  39. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  40. if ( outputPortId == 0 )
  41. {
  42. m_inputPorts[ 0 ].MatchPortToConnection();
  43. m_inputPorts[ 1 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  44. m_inputPorts[ 2 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  45. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  46. }
  47. }
  48. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  49. {
  50. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  51. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  52. WirePortDataType valueType = m_inputPorts[ 0 ].ConnectionType();
  53. WirePortDataType minType = m_inputPorts[ 1 ].ConnectionType();
  54. WirePortDataType maxType = m_inputPorts[ 2 ].ConnectionType();
  55. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  56. string min = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  57. if ( minType != valueType )
  58. {
  59. min = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), null, m_inputPorts[ 1 ].DataType, m_inputPorts[ 0 ].DataType, min );
  60. }
  61. string max = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  62. if ( maxType != valueType )
  63. {
  64. max = UIUtils.CastPortType( ref dataCollector, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), null, m_inputPorts[ 2 ].DataType, m_inputPorts[ 0 ].DataType, max );
  65. }
  66. string result = string.Empty;
  67. switch ( valueType )
  68. {
  69. case WirePortDataType.FLOAT:
  70. case WirePortDataType.FLOAT2:
  71. case WirePortDataType.FLOAT3:
  72. case WirePortDataType.FLOAT4:
  73. case WirePortDataType.INT:
  74. case WirePortDataType.COLOR:
  75. case WirePortDataType.OBJECT:
  76. {
  77. result = "clamp( " + value + " , " + min + " , " + max + " )";
  78. }
  79. break;
  80. case WirePortDataType.FLOAT3x3:
  81. case WirePortDataType.FLOAT4x4:
  82. {
  83. return UIUtils.InvalidParameter( this );
  84. }
  85. }
  86. RegisterLocalVariable( 0, result, ref dataCollector, "clampResult" + OutputId );
  87. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  88. }
  89. }
  90. }