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.

122 lines
5.1 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( "Smoothstep", "Math Operators", "Returns a smooth Hermite interpolation between 0 and 1, if input is in the range [min, max]." )]
  8. public sealed class SmoothstepOpNode : ParentNode
  9. {
  10. //[UnityEngine.SerializeField]
  11. //private WirePortDataType m_mainDataType = WirePortDataType.FLOAT;
  12. private int m_alphaPortId = 0;
  13. private int m_minPortId = 0;
  14. private int m_maxPortId = 0;
  15. private const string SmoothstepOpFormat = "smoothstep( {0} , {1} , {2})";//min max alpha
  16. protected override void CommonInit( int uniqueId )
  17. {
  18. base.CommonInit( uniqueId );
  19. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue, -1, MasterNodePortCategory.Fragment, 0 );
  20. m_alphaPortId = m_inputPorts.Count - 1;
  21. AddInputPort( WirePortDataType.FLOAT, false, "Min", -1, MasterNodePortCategory.Fragment, 1 );
  22. m_minPortId = m_inputPorts.Count - 1;
  23. AddInputPort( WirePortDataType.FLOAT, false, "Max", -1, MasterNodePortCategory.Fragment, 2 );
  24. m_maxPortId = m_inputPorts.Count - 1;
  25. GetInputPortByUniqueId( m_maxPortId ).FloatInternalData = 1;
  26. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  27. m_useInternalPortData = true;
  28. m_textLabelWidth = 55;
  29. m_previewShaderGUID = "954cdd40a7a528344a0a4d3ff1db5176";
  30. }
  31. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  32. {
  33. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  34. if( portId == 0 )
  35. {
  36. m_inputPorts[ 0 ].MatchPortToConnection();
  37. m_inputPorts[ 1 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  38. m_inputPorts[ 2 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  39. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  40. }
  41. }
  42. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  43. {
  44. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  45. if( outputPortId == 0 )
  46. {
  47. m_inputPorts[ 0 ].MatchPortToConnection();
  48. m_inputPorts[ 1 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  49. m_inputPorts[ 2 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  50. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  51. }
  52. }
  53. //public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  54. //{
  55. // base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  56. // UpdateConnection( portId );
  57. //}
  58. //public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  59. //{
  60. // base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  61. // UpdateConnection( inputPortId );
  62. //}
  63. //public override void OnInputPortDisconnected( int portId )
  64. //{
  65. // base.OnInputPortDisconnected( portId );
  66. // UpdateConnection( portId );
  67. //}
  68. //void UpdateConnection( int portId )
  69. //{
  70. // WirePortDataType type1 = WirePortDataType.FLOAT;
  71. // if( m_inputPorts[ m_minPortId ].IsConnected )
  72. // type1 = m_inputPorts[ m_minPortId ].GetOutputConnection( 0 ).DataType;
  73. // WirePortDataType type2 = WirePortDataType.FLOAT;
  74. // if( m_inputPorts[ m_maxPortId ].IsConnected )
  75. // type2 = m_inputPorts[ m_maxPortId ].GetOutputConnection( 0 ).DataType;
  76. // m_mainDataType = UIUtils.GetPriority( type1 ) > UIUtils.GetPriority( type2 ) ? type1 : type2;
  77. // if( !m_inputPorts[ m_minPortId ].IsConnected && !m_inputPorts[ m_maxPortId ].IsConnected && m_inputPorts[ m_alphaPortId ].IsConnected )
  78. // m_mainDataType = m_inputPorts[ m_alphaPortId ].GetOutputConnection( 0 ).DataType;
  79. // m_inputPorts[ m_minPortId ].ChangeType( m_mainDataType, false );
  80. // m_inputPorts[ m_maxPortId ].ChangeType( m_mainDataType, false );
  81. // if( m_inputPorts[ m_alphaPortId ].IsConnected && m_inputPorts[ m_alphaPortId ].GetOutputConnection( 0 ).DataType == WirePortDataType.FLOAT )
  82. // m_inputPorts[ m_alphaPortId ].ChangeType( WirePortDataType.FLOAT, false );
  83. // else
  84. // m_inputPorts[ m_alphaPortId ].ChangeType( m_mainDataType, false );
  85. // m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
  86. //}
  87. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  88. {
  89. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  90. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  91. string aValue = m_inputPorts[ m_minPortId ].GeneratePortInstructions( ref dataCollector );
  92. string bValue = m_inputPorts[ m_maxPortId ].GeneratePortInstructions( ref dataCollector );
  93. string interp = m_inputPorts[ m_alphaPortId ].GeneratePortInstructions( ref dataCollector );
  94. string result = string.Format( SmoothstepOpFormat, aValue, bValue, interp );
  95. RegisterLocalVariable( 0, result, ref dataCollector, "smoothstepResult" + OutputId );
  96. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  97. }
  98. }
  99. }