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.4 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. // http://stackoverflow.com/questions/9320953/what-algorithm-does-photoshop-use-to-desaturate-an-image
  4. // https://www.shadertoy.com/view/lsdXDH
  5. namespace AmplifyShaderEditor
  6. {
  7. [System.Serializable]
  8. [NodeAttributes( "Desaturate", "Image Effects", "Generic desaturation operation" )]
  9. public sealed class DesaturateOpNode : ParentNode
  10. {
  11. private const string GenericDesaturateOp0 = "dot( {0}, float3( 0.299, 0.587, 0.114 ))";
  12. private const string GenericDesaturateOp1 = "lerp( {0}, {1}.xxx, {2} )";
  13. //private const string GenericDesaturateOp = "lerp( {0},dot({0},float3(0.299,0.587,0.114)).xxx,{1})";
  14. protected override void CommonInit( int uniqueId )
  15. {
  16. base.CommonInit( uniqueId );
  17. AddInputPort( WirePortDataType.FLOAT3, false, "RGB" );
  18. AddInputPort( WirePortDataType.FLOAT, false, "Fraction" );
  19. AddOutputPort( WirePortDataType.FLOAT3, Constants.EmptyPortValue );
  20. m_useInternalPortData = true;
  21. m_previewShaderGUID = "faabe9efdf44b9648a523f1742abdfd3";
  22. }
  23. void UpdatePorts( int portId )
  24. {
  25. if ( portId == 0 )
  26. {
  27. m_inputPorts[ 0 ].MatchPortToConnection();
  28. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  29. }
  30. }
  31. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  32. {
  33. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  34. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  35. string initalColorValue = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  36. string fraction = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  37. string initialColorVarName = "desaturateInitialColor" + OutputId;
  38. dataCollector.AddLocalVariable( UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, initialColorVarName, initalColorValue );
  39. string dotVarName = "desaturateDot" + OutputId;
  40. string dotVarValue = string.Format( GenericDesaturateOp0, initialColorVarName );
  41. dataCollector.AddLocalVariable( UniqueId, m_currentPrecisionType,WirePortDataType.FLOAT, dotVarName, dotVarValue );
  42. RegisterLocalVariable( 0, string.Format( GenericDesaturateOp1, initialColorVarName, dotVarName,fraction ), ref dataCollector, "desaturateVar" + OutputId );
  43. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  44. }
  45. }
  46. }