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.

98 lines
3.2 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //https://www.shadertoy.com/view/ldX3D4
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. [NodeAttributes( "Posterize", "Image Effects", "Converts a continuous gradation of tones to multiple regions of fewer tones" )]
  11. public sealed class PosterizeNode : ParentNode
  12. {
  13. private const string PosterizationPowerStr = "Power";
  14. [SerializeField]
  15. private int m_posterizationPower = 1;
  16. protected override void CommonInit( int uniqueId )
  17. {
  18. base.CommonInit( uniqueId );
  19. AddInputPort( WirePortDataType.COLOR, false, "RGBA", -1, MasterNodePortCategory.Fragment, 1 );
  20. AddInputPort( WirePortDataType.INT, false, "Power", -1, MasterNodePortCategory.Fragment, 0 );
  21. m_inputPorts[ 1 ].AutoDrawInternalData = true;
  22. AddOutputPort( WirePortDataType.COLOR, Constants.EmptyPortValue );
  23. m_textLabelWidth = 60;
  24. m_autoWrapProperties = true;
  25. m_previewShaderGUID = "ecb3048ef0eec1645bad1d72a98d8279";
  26. }
  27. public override void DrawProperties()
  28. {
  29. base.DrawProperties();
  30. if( !m_inputPorts[ 1 ].IsConnected )
  31. {
  32. EditorGUILayout.BeginVertical();
  33. {
  34. EditorGUI.BeginChangeCheck();
  35. m_posterizationPower = EditorGUILayoutIntSlider( PosterizationPowerStr, m_posterizationPower, 1, 256 );
  36. if( EditorGUI.EndChangeCheck() )
  37. {
  38. GetInputPortByUniqueId( 0 ).IntInternalData = m_posterizationPower;
  39. }
  40. }
  41. EditorGUILayout.EndVertical();
  42. }
  43. else
  44. {
  45. EditorGUILayout.Space();
  46. }
  47. }
  48. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  49. {
  50. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  51. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  52. string posterizationPower = "1";
  53. if( m_inputPorts[ 1 ].IsConnected )
  54. {
  55. posterizationPower = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  56. }
  57. else
  58. {
  59. posterizationPower = m_posterizationPower.ToString();
  60. }
  61. string colorTarget = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  62. string divVar = "div" + OutputId;
  63. dataCollector.AddLocalVariable( UniqueId, "float " + divVar + "=256.0/float(" + posterizationPower + ");" );
  64. string result = "( floor( " + colorTarget + " * " + divVar + " ) / " + divVar + " )";
  65. RegisterLocalVariable( 0, result, ref dataCollector, "posterize" + OutputId );
  66. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  67. }
  68. public override void RefreshExternalReferences()
  69. {
  70. base.RefreshExternalReferences();
  71. m_inputPorts[ 0 ].ChangeType( WirePortDataType.COLOR, false );
  72. m_inputPorts[ 1 ].ChangeType( WirePortDataType.INT, false );
  73. }
  74. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  75. {
  76. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  77. IOUtils.AddFieldValueToString( ref nodeInfo, m_posterizationPower );
  78. }
  79. public override void ReadFromString( ref string[] nodeParams )
  80. {
  81. base.ReadFromString( ref nodeParams );
  82. m_posterizationPower = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  83. }
  84. }
  85. }