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.

86 lines
3.2 KiB

  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. }
  32. public override void DrawProperties()
  33. {
  34. base.DrawProperties();
  35. DrawPrecisionProperty();
  36. }
  37. public static void AddHSVToRGBFunction( ref MasterNodeDataCollector dataCollector , string precisionString )
  38. {
  39. if( !dataCollector.HasFunction( HSVToRGBHeader ) )
  40. {
  41. //Hack to be used util indent is properly used
  42. int currIndent = UIUtils.ShaderIndentLevel;
  43. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  44. {
  45. UIUtils.ShaderIndentLevel = 0;
  46. }
  47. else
  48. {
  49. UIUtils.ShaderIndentLevel = 1;
  50. UIUtils.ShaderIndentLevel++;
  51. }
  52. string finalFunction = string.Empty;
  53. for( int i = 0; i < HSVToRGBFunction.Length; i++ )
  54. {
  55. finalFunction += UIUtils.ShaderIndentTabs + ( HSVToRGBFlags[ i ] ? string.Format( HSVToRGBFunction[ i ], precisionString ) : HSVToRGBFunction[ i ] );
  56. }
  57. UIUtils.ShaderIndentLevel = currIndent;
  58. dataCollector.AddFunction( HSVToRGBHeader, finalFunction );
  59. }
  60. }
  61. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  62. {
  63. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  64. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  65. string precisionString = UIUtils.FinalPrecisionWirePortToCgType( m_currentPrecisionType, WirePortDataType.FLOAT );
  66. AddHSVToRGBFunction( ref dataCollector , precisionString );
  67. string hue = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  68. string saturation = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  69. string value = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  70. RegisterLocalVariable( 0, string.Format( HSVToRGBHeader, precisionString, hue, saturation, value ), ref dataCollector, "hsvTorgb" + OutputId );
  71. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  72. }
  73. }
  74. }