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.

116 lines
3.9 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //
  4. // Custom Node Grayscale
  5. // Donated by The Four Headed Cat - @fourheadedcat
  6. using UnityEngine;
  7. using UnityEditor;
  8. using System;
  9. namespace AmplifyShaderEditor
  10. {
  11. [Serializable]
  12. [NodeAttributes( "Grayscale", "Image Effects", "Convert image colors to grayscale", null, KeyCode.None, true, false, null, null, "The Four Headed Cat - @fourheadedcat" )]
  13. public sealed class TFHCGrayscale : ParentNode
  14. {
  15. private const string GrayscaleStyleStr = "Grayscale Style";
  16. [SerializeField]
  17. private int m_grayscaleStyle;
  18. [SerializeField]
  19. private readonly string[] m_GrayscaleStyleValues = { "Luminance", "Natural Classic", "Old School" };
  20. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  21. protected override void CommonInit( int uniqueId )
  22. {
  23. base.CommonInit( uniqueId );
  24. AddInputPort( WirePortDataType.FLOAT3, false, "RGB" );
  25. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  26. m_textLabelWidth = 120;
  27. m_useInternalPortData = true;
  28. m_hasLeftDropdown = true;
  29. m_autoWrapProperties = true;
  30. SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr, m_GrayscaleStyleValues[ m_grayscaleStyle ] ) );
  31. m_previewShaderGUID = "56781cd022be9124597f0f396a46a35f";
  32. }
  33. public override void AfterCommonInit()
  34. {
  35. base.AfterCommonInit();
  36. if( PaddingTitleLeft == 0 )
  37. {
  38. PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  39. if( PaddingTitleRight == 0 )
  40. PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  41. }
  42. }
  43. public override void Destroy()
  44. {
  45. base.Destroy();
  46. m_upperLeftWidget = null;
  47. }
  48. void UpdateFromSelected()
  49. {
  50. m_previewMaterialPassId = m_grayscaleStyle;
  51. SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr, m_GrayscaleStyleValues[ m_grayscaleStyle ] ) );
  52. }
  53. public override void Draw( DrawInfo drawInfo )
  54. {
  55. base.Draw( drawInfo );
  56. EditorGUI.BeginChangeCheck();
  57. m_grayscaleStyle = m_upperLeftWidget.DrawWidget( this, m_grayscaleStyle, m_GrayscaleStyleValues );
  58. if( EditorGUI.EndChangeCheck() )
  59. {
  60. UpdateFromSelected();
  61. }
  62. }
  63. public override void DrawProperties()
  64. {
  65. base.DrawProperties();
  66. EditorGUI.BeginChangeCheck();
  67. m_grayscaleStyle = EditorGUILayoutPopup( GrayscaleStyleStr, m_grayscaleStyle, m_GrayscaleStyleValues );
  68. if( EditorGUI.EndChangeCheck() )
  69. {
  70. UpdateFromSelected();
  71. }
  72. EditorGUILayout.HelpBox( "Grayscale Old:\n\n - In: Image to convert.\n - Grayscale Style: Select the grayscale style.\n\n - Out: Grayscale version of the image.", MessageType.None );
  73. }
  74. public override void ReadFromString( ref string[] nodeParams )
  75. {
  76. base.ReadFromString( ref nodeParams );
  77. m_grayscaleStyle = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  78. UpdateFromSelected();
  79. }
  80. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  81. {
  82. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  83. IOUtils.AddFieldValueToString( ref nodeInfo, m_grayscaleStyle );
  84. }
  85. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  86. {
  87. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  88. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  89. string i = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  90. string grayscale = string.Empty;
  91. switch( m_grayscaleStyle )
  92. {
  93. case 1: { grayscale = "dot(" + i + ", float3(0.299,0.587,0.114))"; } break;
  94. case 2: { grayscale = "(" + i + ".r + " + i + ".g + " + i + ".b) / 3"; } break;
  95. default: { grayscale = "Luminance(" + i + ")"; } break;
  96. }
  97. RegisterLocalVariable( 0, grayscale, ref dataCollector, "grayscale" + OutputId );
  98. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  99. }
  100. }
  101. }