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.

117 lines
4.1 KiB

  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. //[SerializeField]
  14. //private float m_speedX = 1f;
  15. //[SerializeField]
  16. //private float m_speedY = 1f;
  17. private int m_cachedUsingEditorId = -1;
  18. //private int m_cachedSpeedXId = -1;
  19. //private int m_cachedSpeedYId = -1;
  20. protected override void CommonInit( int uniqueId )
  21. {
  22. base.CommonInit( uniqueId );
  23. AddInputPort( WirePortDataType.FLOAT2, false, "UV" ,-1,MasterNodePortCategory.Fragment,0);
  24. AddInputPort( WirePortDataType.FLOAT2, false, "Speed", -1, MasterNodePortCategory.Fragment, 2 );
  25. AddInputPort( WirePortDataType.FLOAT, false, "Time", -1, MasterNodePortCategory.Fragment, 1 );
  26. AddOutputPort( WirePortDataType.FLOAT2, "Out" );
  27. m_textLabelWidth = 70;
  28. // m_autoWrapProperties = true;
  29. m_useInternalPortData = true;
  30. m_previewShaderGUID = "6f89a5d96bdad114b9bbd0c236cac622";
  31. m_inputPorts[ 2 ].FloatInternalData = 1;
  32. }
  33. //public override void DrawProperties()
  34. //{
  35. // base.DrawProperties();
  36. // m_speedX = EditorGUILayoutFloatField( _speedXStr, m_speedX );
  37. // m_speedY = EditorGUILayoutFloatField( _speedYStr, m_speedY );
  38. //}
  39. public override void SetPreviewInputs()
  40. {
  41. base.SetPreviewInputs();
  42. if ( m_cachedUsingEditorId == -1 )
  43. m_cachedUsingEditorId = Shader.PropertyToID( "_UsingEditor" );
  44. //if ( m_cachedSpeedXId == -1 )
  45. // m_cachedSpeedXId = Shader.PropertyToID( "_SpeedX" );
  46. //if ( m_cachedSpeedYId == -1 )
  47. // m_cachedSpeedYId = Shader.PropertyToID( "_SpeedY" );
  48. PreviewMaterial.SetFloat( m_cachedUsingEditorId, ( m_inputPorts[ 2 ].IsConnected ? 0 : 1 ) );
  49. //PreviewMaterial.SetFloat( m_cachedSpeedXId, m_speedX );
  50. //PreviewMaterial.SetFloat( m_cachedSpeedYId, m_speedY );
  51. }
  52. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  53. {
  54. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  55. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  56. string timePort = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  57. if( !m_inputPorts[ 2 ].IsConnected )
  58. {
  59. if( !( dataCollector.IsTemplate && dataCollector.IsSRP ) )
  60. dataCollector.AddToIncludes( UniqueId, Constants.UnityShaderVariables );
  61. timePort += " * _Time.y";
  62. }
  63. string speed = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  64. string result = "( " + timePort + " * " + speed + " + " + m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector ) + ")";
  65. RegisterLocalVariable( 0, result, ref dataCollector, "panner" + OutputId );
  66. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  67. }
  68. public override void ReadFromString( ref string[] nodeParams )
  69. {
  70. base.ReadFromString( ref nodeParams );
  71. if( UIUtils.CurrentShaderVersion() < 13107 )
  72. {
  73. // The internal data for the new port can be set in here since it didn't existed
  74. // on older shader versions
  75. float speedX = Convert.ToSingle( GetCurrentParam( ref nodeParams ) );
  76. float speedY = Convert.ToSingle( GetCurrentParam( ref nodeParams ) );
  77. m_inputPorts[ 1 ].Vector2InternalData = new Vector2( speedX, speedY );
  78. }
  79. }
  80. public override void ReadInputDataFromString( ref string[] nodeParams )
  81. {
  82. base.ReadInputDataFromString( ref nodeParams );
  83. if( UIUtils.CurrentShaderVersion() < 13107 )
  84. {
  85. //Time Port must be rewritten after internal data is read
  86. // already existed in previous shaders
  87. m_inputPorts[ 2 ].FloatInternalData = 1;
  88. }
  89. }
  90. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  91. {
  92. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  93. //IOUtils.AddFieldValueToString( ref nodeInfo, m_speedX );
  94. //IOUtils.AddFieldValueToString( ref nodeInfo, m_speedY );
  95. }
  96. }
  97. }