RGBToHSVNode.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. namespace AmplifyShaderEditor
  3. {
  4. [Serializable]
  5. [NodeAttributes( "RGB to HSV", "Image Effects", "Converts from RGB to HSV color space" )]
  6. public sealed class RGBToHSVNode : ParentNode
  7. {
  8. public static readonly string RGBToHSVHeader = "RGBToHSV( {0} )";
  9. public static readonly string[] RGBToHSVFunction = { "{0}3 RGBToHSV({0}3 c)\n",
  10. "{\n",
  11. "\t{0}4 K = {0}4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n",
  12. "\t{0}4 p = lerp( {0}4( c.bg, K.wz ), {0}4( c.gb, K.xy ), step( c.b, c.g ) );\n",
  13. "\t{0}4 q = lerp( {0}4( p.xyw, c.r ), {0}4( c.r, p.yzx ), step( p.x, c.r ) );\n",
  14. "\t{0} d = q.x - min( q.w, q.y );\n",
  15. "\t{0} e = 1.0e-10;\n",
  16. "\treturn {0}3( abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n",
  17. "}"
  18. };
  19. public static readonly bool[] RGBToHSVFlags = { true,
  20. false,
  21. true,
  22. true,
  23. true,
  24. true,
  25. true,
  26. true,
  27. false};
  28. protected override void CommonInit( int uniqueId )
  29. {
  30. base.CommonInit( uniqueId );
  31. AddInputPort( WirePortDataType.FLOAT3, false, "RGB" );
  32. AddOutputPort( WirePortDataType.FLOAT3, "HSV" );
  33. AddOutputPort( WirePortDataType.FLOAT, "Hue" );
  34. AddOutputPort( WirePortDataType.FLOAT, "Saturation" );
  35. AddOutputPort( WirePortDataType.FLOAT, "Value" );
  36. m_previewShaderGUID = "0f2f09b49bf4954428aafa2dfe1a9a09";
  37. m_useInternalPortData = true;
  38. m_autoWrapProperties = true;
  39. m_customPrecision = true;
  40. }
  41. public override void DrawProperties()
  42. {
  43. base.DrawProperties();
  44. DrawPrecisionProperty();
  45. }
  46. public static void AddRGBToHSVFunction( ref MasterNodeDataCollector dataCollector, string precisionString )
  47. {
  48. if( !dataCollector.HasFunction( RGBToHSVHeader ) )
  49. {
  50. //Hack to be used util indent is properly used
  51. int currIndent = UIUtils.ShaderIndentLevel;
  52. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  53. {
  54. UIUtils.ShaderIndentLevel = 0;
  55. }
  56. else
  57. {
  58. UIUtils.ShaderIndentLevel = 1;
  59. UIUtils.ShaderIndentLevel++;
  60. }
  61. string finalFunction = string.Empty;
  62. for( int i = 0; i < RGBToHSVFunction.Length; i++ )
  63. {
  64. finalFunction += UIUtils.ShaderIndentTabs + ( RGBToHSVFlags[ i ] ? string.Format( RGBToHSVFunction[ i ], precisionString ) : RGBToHSVFunction[ i ] );
  65. }
  66. UIUtils.ShaderIndentLevel--;
  67. UIUtils.ShaderIndentLevel = currIndent;
  68. dataCollector.AddFunction( RGBToHSVHeader, finalFunction );
  69. }
  70. }
  71. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  72. {
  73. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  74. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  75. string precisionString = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, WirePortDataType.FLOAT );
  76. AddRGBToHSVFunction( ref dataCollector, precisionString );
  77. string rgbValue = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  78. RegisterLocalVariable( 0, string.Format( RGBToHSVHeader, rgbValue ), ref dataCollector, "hsvTorgb" + OutputId );
  79. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  80. }
  81. }
  82. }