HSVToRGBNode.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. namespace AmplifyShaderEditor
  3. {
  4. [Serializable]
  5. [NodeAttributes( "HSV to RGB", "Image Effects", "Converts from HSV to RGB color space" )]
  6. public sealed class HSVToRGBNode : ParentNode
  7. {
  8. public static readonly string HSVToRGBHeader = "HSVToRGB( {0}3({1},{2},{3}) )";
  9. public static readonly string[] HSVToRGBFunction = { "{0}3 HSVToRGB( {0}3 c )\n",
  10. "{\n",
  11. "\t{0}4 K = {0}4( 1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0 );\n",
  12. "\t{0}3 p = abs( frac( c.xxx + K.xyz ) * 6.0 - K.www );\n",
  13. "\treturn c.z * lerp( K.xxx, saturate( p - K.xxx ), c.y );\n",
  14. "}\n"};
  15. public static readonly bool[] HSVToRGBFlags = { true,
  16. false,
  17. true,
  18. true,
  19. false,
  20. false};
  21. protected override void CommonInit( int uniqueId )
  22. {
  23. base.CommonInit( uniqueId );
  24. AddInputPort( WirePortDataType.FLOAT, false, "Hue" );
  25. AddInputPort( WirePortDataType.FLOAT, false, "Saturation" );
  26. AddInputPort( WirePortDataType.FLOAT, false, "Value" );
  27. AddOutputColorPorts( "RGB", false );
  28. m_previewShaderGUID = "fab445eb945d63047822a7a6b81b959d";
  29. m_useInternalPortData = true;
  30. m_autoWrapProperties = true;
  31. m_customPrecision = true;
  32. }
  33. public override void DrawProperties()
  34. {
  35. base.DrawProperties();
  36. DrawPrecisionProperty();
  37. }
  38. public static void AddHSVToRGBFunction( ref MasterNodeDataCollector dataCollector , string precisionString )
  39. {
  40. if( !dataCollector.HasFunction( HSVToRGBHeader ) )
  41. {
  42. //Hack to be used util indent is properly used
  43. int currIndent = UIUtils.ShaderIndentLevel;
  44. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  45. {
  46. UIUtils.ShaderIndentLevel = 0;
  47. }
  48. else
  49. {
  50. UIUtils.ShaderIndentLevel = 1;
  51. UIUtils.ShaderIndentLevel++;
  52. }
  53. string finalFunction = string.Empty;
  54. for( int i = 0; i < HSVToRGBFunction.Length; i++ )
  55. {
  56. finalFunction += UIUtils.ShaderIndentTabs + ( HSVToRGBFlags[ i ] ? string.Format( HSVToRGBFunction[ i ], precisionString ) : HSVToRGBFunction[ i ] );
  57. }
  58. UIUtils.ShaderIndentLevel = currIndent;
  59. dataCollector.AddFunction( HSVToRGBHeader, finalFunction );
  60. }
  61. }
  62. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  63. {
  64. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  65. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  66. string precisionString = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, WirePortDataType.FLOAT );
  67. AddHSVToRGBFunction( ref dataCollector , precisionString );
  68. string hue = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  69. string saturation = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  70. string value = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  71. RegisterLocalVariable( 0, string.Format( HSVToRGBHeader, precisionString, hue, saturation, value ), ref dataCollector, "hsvTorgb" + OutputId );
  72. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  73. }
  74. }
  75. }