ClipNode.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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( "Clip", "Miscellaneous", "Conditionally kill a pixel before output" )]
  8. public sealed class ClipNode : ParentNode
  9. {
  10. private const string ClipOpFormat = "clip( {0} );";
  11. private const string ClipSubOpFormat = "clip( {0} - {1});";
  12. protected override void CommonInit( int uniqueId )
  13. {
  14. base.CommonInit( uniqueId );
  15. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue );
  16. AddInputPort( WirePortDataType.FLOAT, false, "Alpha" );
  17. AddInputPort( WirePortDataType.FLOAT, false, "Threshold" );
  18. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  19. m_useInternalPortData = true;
  20. m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.OBJECT,
  21. WirePortDataType.FLOAT ,
  22. WirePortDataType.FLOAT2,
  23. WirePortDataType.FLOAT3,
  24. WirePortDataType.FLOAT4,
  25. WirePortDataType.COLOR ,
  26. WirePortDataType.INT);
  27. m_previewShaderGUID = "1fca7774f364aee4d8c64e8634ef4be4";
  28. }
  29. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  30. {
  31. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  32. m_inputPorts[ portId ].MatchPortToConnection();
  33. UpdatePortConnection( portId );
  34. }
  35. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  36. {
  37. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  38. m_inputPorts[ outputPortId ].MatchPortToConnection();
  39. UpdatePortConnection( outputPortId );
  40. }
  41. void UpdatePortConnection( int portId )
  42. {
  43. if( portId == 0 )
  44. {
  45. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  46. }
  47. else
  48. {
  49. int otherPortId = portId == 1 ? 2 : 1;
  50. if( m_inputPorts[ otherPortId ].IsConnected )
  51. {
  52. WirePortDataType type1 = m_inputPorts[ portId ].DataType;
  53. WirePortDataType type2 = m_inputPorts[ otherPortId ].DataType;
  54. WirePortDataType mainType = UIUtils.GetPriority( type1 ) > UIUtils.GetPriority( type2 ) ? type1 : type2;
  55. m_inputPorts[ portId ].ChangeType( mainType, false );
  56. m_inputPorts[ otherPortId ].ChangeType( mainType , false );
  57. }
  58. else
  59. {
  60. m_inputPorts[ otherPortId ].ChangeType( m_inputPorts[ portId ].DataType,false );
  61. }
  62. }
  63. }
  64. public override void OnInputPortDisconnected( int portId )
  65. {
  66. base.OnInputPortDisconnected( portId );
  67. if( portId == 0 )
  68. return;
  69. int otherPortId = portId == 1 ? 2 : 1;
  70. if( m_inputPorts[ otherPortId ].IsConnected )
  71. {
  72. m_inputPorts[ portId ].ChangeType( m_inputPorts[ otherPortId ].DataType, false );
  73. }
  74. else
  75. {
  76. m_inputPorts[ portId ].ChangeType( WirePortDataType.FLOAT, false );
  77. m_inputPorts[ otherPortId ].ChangeType( WirePortDataType.FLOAT, false );
  78. }
  79. }
  80. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  81. {
  82. if( dataCollector.PortCategory == MasterNodePortCategory.Vertex ||
  83. dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  84. {
  85. UIUtils.ShowMessage( UniqueId, "Clip can only be used in fragment functions", MessageSeverity.Warning );
  86. return GenerateErrorValue();
  87. }
  88. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  89. string alpha = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  90. if( m_inputPorts[ 2 ].IsConnected )
  91. {
  92. string threshold = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  93. dataCollector.AddLocalVariable( UniqueId, string.Format( ClipSubOpFormat, alpha , threshold) );
  94. }
  95. else
  96. {
  97. if( m_inputPorts[ 2 ].IsZeroInternalData )
  98. {
  99. dataCollector.AddLocalVariable( UniqueId, string.Format( ClipOpFormat, alpha ) );
  100. }
  101. else
  102. {
  103. string threshold = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  104. dataCollector.AddLocalVariable( UniqueId, string.Format( ClipSubOpFormat, alpha, threshold ) );
  105. }
  106. }
  107. return value;
  108. }
  109. }
  110. }