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.

64 lines
2.9 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //
  4. // Custom Node Remap
  5. // Donated by The Four Headed Cat - @fourheadedcat
  6. using UnityEngine;
  7. using System;
  8. namespace AmplifyShaderEditor
  9. {
  10. [Serializable]
  11. [NodeAttributes( "Remap", "Math Operators", "Remap value from old min - max range to new min - max range", null, KeyCode.None, true, false, null, null, "The Four Headed Cat - @fourheadedcat" )]
  12. public sealed class TFHCRemapNode : DynamicTypeNode
  13. {
  14. protected override void CommonInit( int uniqueId )
  15. {
  16. base.CommonInit( uniqueId );
  17. m_inputPorts[ 0 ].Name = Constants.EmptyPortValue;
  18. m_inputPorts[ 1 ].Name = "Min Old";
  19. AddInputPort( WirePortDataType.FLOAT, false, "Max Old" );
  20. m_inputPorts[ 2 ].FloatInternalData = 1;
  21. AddInputPort( WirePortDataType.FLOAT, false, "Min New" );
  22. AddInputPort( WirePortDataType.FLOAT, false, "Max New" );
  23. m_inputPorts[ 4 ].FloatInternalData = 1;
  24. m_textLabelWidth = 100;
  25. m_useInternalPortData = true;
  26. m_previewShaderGUID = "72dd1cbea889fa047b929d5191e360c0";
  27. }
  28. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  29. {
  30. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  31. UpdateConnections();
  32. }
  33. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  34. {
  35. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  36. UpdateConnections();
  37. }
  38. void UpdateConnections()
  39. {
  40. m_inputPorts[ 0 ].MatchPortToConnection();
  41. m_inputPorts[ 1 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  42. m_inputPorts[ 2 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  43. m_inputPorts[ 3 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  44. m_inputPorts[ 4 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  45. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  46. }
  47. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  48. {
  49. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  50. string oldMin = m_inputPorts[ 1 ].GenerateShaderForOutput( ref dataCollector, m_inputPorts[ 0 ].DataType, ignoreLocalvar, true );
  51. string oldMax = m_inputPorts[ 2 ].GenerateShaderForOutput( ref dataCollector, m_inputPorts[ 0 ].DataType, ignoreLocalvar, true );
  52. string newMin = m_inputPorts[ 3 ].GenerateShaderForOutput( ref dataCollector, m_inputPorts[ 0 ].DataType, ignoreLocalvar, true );
  53. string newMax = m_inputPorts[ 4 ].GenerateShaderForOutput( ref dataCollector, m_inputPorts[ 0 ].DataType, ignoreLocalvar, true );
  54. string strout = "(" + newMin + " + (" + value + " - " + oldMin + ") * (" + newMax + " - " + newMin + ") / (" + oldMax + " - " + oldMin + "))";
  55. return CreateOutputLocalVariable( 0, strout, ref dataCollector );
  56. }
  57. }
  58. }