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.

92 lines
3.3 KiB

  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. }
  40. public override void DrawProperties()
  41. {
  42. base.DrawProperties();
  43. DrawPrecisionProperty();
  44. }
  45. public void AddRGBToHSVFunction( ref MasterNodeDataCollector dataCollector, string precisionString )
  46. {
  47. if( !dataCollector.HasFunction( RGBToHSVHeader ) )
  48. {
  49. //Hack to be used util indent is properly used
  50. int currIndent = UIUtils.ShaderIndentLevel;
  51. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  52. {
  53. UIUtils.ShaderIndentLevel = 0;
  54. }
  55. else
  56. {
  57. UIUtils.ShaderIndentLevel = 1;
  58. UIUtils.ShaderIndentLevel++;
  59. }
  60. string finalFunction = string.Empty;
  61. for( int i = 0; i < RGBToHSVFunction.Length; i++ )
  62. {
  63. finalFunction += UIUtils.ShaderIndentTabs + ( RGBToHSVFlags[ i ] ? string.Format( RGBToHSVFunction[ i ], precisionString ) : RGBToHSVFunction[ i ] );
  64. }
  65. UIUtils.ShaderIndentLevel--;
  66. UIUtils.ShaderIndentLevel = currIndent;
  67. dataCollector.AddFunction( RGBToHSVHeader, finalFunction );
  68. }
  69. }
  70. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  71. {
  72. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  73. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  74. string precisionString = UIUtils.PrecisionWirePortToCgType( m_currentPrecisionType, WirePortDataType.FLOAT );
  75. AddRGBToHSVFunction( ref dataCollector, precisionString );
  76. string rgbValue = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  77. RegisterLocalVariable( 0, string.Format( RGBToHSVHeader, rgbValue ), ref dataCollector, "hsvTorgb" + OutputId );
  78. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  79. }
  80. }
  81. }