PannerNode.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using System;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Panner", "UV Coordinates", "Pans UV texture coordinates according to its inputs" )]
  9. public sealed class PannerNode : ParentNode
  10. {
  11. private const string _speedXStr = "Speed X";
  12. private const string _speedYStr = "Speed Y";
  13. private int m_cachedUsingEditorId = -1;
  14. protected override void CommonInit( int uniqueId )
  15. {
  16. base.CommonInit( uniqueId );
  17. AddInputPort( WirePortDataType.FLOAT2, false, "UV" ,-1,MasterNodePortCategory.Fragment,0);
  18. AddInputPort( WirePortDataType.FLOAT2, false, "Speed", -1, MasterNodePortCategory.Fragment, 2 );
  19. AddInputPort( WirePortDataType.FLOAT, false, "Time", -1, MasterNodePortCategory.Fragment, 1 );
  20. AddOutputPort( WirePortDataType.FLOAT2, "Out" );
  21. m_textLabelWidth = 70;
  22. m_useInternalPortData = true;
  23. m_previewShaderGUID = "6f89a5d96bdad114b9bbd0c236cac622";
  24. m_inputPorts[ 2 ].FloatInternalData = 1;
  25. m_continuousPreviewRefresh = true;
  26. }
  27. public override void SetPreviewInputs()
  28. {
  29. base.SetPreviewInputs();
  30. if ( m_cachedUsingEditorId == -1 )
  31. m_cachedUsingEditorId = Shader.PropertyToID( "_UsingEditor" );
  32. PreviewMaterial.SetFloat( m_cachedUsingEditorId, ( m_inputPorts[ 2 ].IsConnected ? 0 : 1 ) );
  33. }
  34. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  35. {
  36. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  37. if( portId == 1 )
  38. {
  39. m_continuousPreviewRefresh = false;
  40. }
  41. }
  42. public override void OnInputPortDisconnected( int portId )
  43. {
  44. base.OnInputPortDisconnected( portId );
  45. if( portId == 1 )
  46. {
  47. m_continuousPreviewRefresh = true;
  48. }
  49. }
  50. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  51. {
  52. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  53. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  54. string timePort = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  55. if( !m_inputPorts[ 2 ].IsConnected )
  56. {
  57. if( !( dataCollector.IsTemplate && dataCollector.IsSRP ) )
  58. dataCollector.AddToIncludes( UniqueId, Constants.UnityShaderVariables );
  59. timePort += " * _Time.y";
  60. }
  61. string speed = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  62. string result = "( " + timePort + " * " + speed + " + " + m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector ) + ")";
  63. RegisterLocalVariable( 0, result, ref dataCollector, "panner" + OutputId );
  64. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  65. }
  66. public override void ReadFromString( ref string[] nodeParams )
  67. {
  68. base.ReadFromString( ref nodeParams );
  69. if( UIUtils.CurrentShaderVersion() < 13107 )
  70. {
  71. // The internal data for the new port can be set in here since it didn't existed
  72. // on older shader versions
  73. float speedX = Convert.ToSingle( GetCurrentParam( ref nodeParams ) );
  74. float speedY = Convert.ToSingle( GetCurrentParam( ref nodeParams ) );
  75. m_inputPorts[ 1 ].Vector2InternalData = new Vector2( speedX, speedY );
  76. }
  77. }
  78. public override void ReadInputDataFromString( ref string[] nodeParams )
  79. {
  80. base.ReadInputDataFromString( ref nodeParams );
  81. if( UIUtils.CurrentShaderVersion() < 13107 )
  82. {
  83. //Time Port must be rewritten after internal data is read
  84. // already existed in previous shaders
  85. m_inputPorts[ 2 ].FloatInternalData = 1;
  86. }
  87. }
  88. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  89. {
  90. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  91. }
  92. }
  93. }