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.

54 lines
2.3 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //
  4. // Custom Node Pixelate UV
  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( "Pixelate UV", "UV Coordinates", "Pixelate Texture Modifying UV.", null, KeyCode.None, true, false, null, null, "The Four Headed Cat - @fourheadedcat" )]
  13. public sealed class TFHCPixelate : ParentNode
  14. {
  15. protected override void CommonInit( int uniqueId )
  16. {
  17. base.CommonInit( uniqueId );
  18. AddInputPort( WirePortDataType.FLOAT2, true, "UV" );
  19. AddInputPort( WirePortDataType.FLOAT, false, "Pixels X" );
  20. AddInputPort( WirePortDataType.FLOAT, false, "Pixels Y" );
  21. AddOutputPort( WirePortDataType.FLOAT2, "Out" );
  22. m_useInternalPortData = true;
  23. m_previewShaderGUID = "e2f7e3c513ed18340868b8cbd0d85cfb";
  24. }
  25. public override void DrawProperties()
  26. {
  27. base.DrawProperties ();
  28. EditorGUILayout.HelpBox ("Pixelate UV.\n\n - UV is the Texture Coordinates to pixelate.\n - Pixels X is the number of horizontal pixels\n - Pixels Y is the number of vertical pixels.", MessageType.None);
  29. }
  30. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  31. {
  32. string uv = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  33. string PixelCount_X = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  34. string PixelCount_Y = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  35. string pixelWidth = "float pixelWidth" + OutputId + " = 1.0f / " + PixelCount_X + ";";
  36. string pixelHeight = "float pixelHeight" + OutputId + " = 1.0f / " + PixelCount_Y + ";";
  37. string pixelatedUV = "half2 pixelateduv" + OutputId + " = half2((int)(" + uv + ".x / pixelWidth" + OutputId + ") * pixelWidth" + OutputId + ", (int)(" + uv + ".y / pixelHeight" + OutputId + ") * pixelHeight" + OutputId + ");";
  38. string result = "pixelateduv" + OutputId;
  39. dataCollector.AddLocalVariable( UniqueId, pixelWidth );
  40. dataCollector.AddLocalVariable( UniqueId, pixelHeight );
  41. dataCollector.AddLocalVariable( UniqueId, pixelatedUV );
  42. return GetOutputVectorItem( 0, outputId, result);
  43. }
  44. }
  45. }