RotatorNode.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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( "Rotator", "UV Coordinates", "Rotates UVs or any Vector2 value from an Anchor point for a specified Time value")]
  9. public sealed class RotatorNode : ParentNode
  10. {
  11. private int m_cachedUsingEditorId = -1;
  12. protected override void CommonInit( int uniqueId )
  13. {
  14. base.CommonInit( uniqueId );
  15. AddInputPort( WirePortDataType.FLOAT2, false, "UV" );
  16. AddInputPort( WirePortDataType.FLOAT2, false, "Anchor" );
  17. AddInputPort( WirePortDataType.FLOAT, false, "Time" );
  18. AddOutputPort( WirePortDataType.FLOAT2, "Out" );
  19. m_useInternalPortData = true;
  20. m_inputPorts[ 2 ].FloatInternalData = 1;
  21. m_textLabelWidth = 50;
  22. m_previewShaderGUID = "e21408a1c7f12f14bbc2652f69bce1fc";
  23. }
  24. public override void SetPreviewInputs()
  25. {
  26. base.SetPreviewInputs();
  27. if ( m_cachedUsingEditorId == -1 )
  28. m_cachedUsingEditorId = Shader.PropertyToID( "_UsingEditor" );
  29. PreviewMaterial.SetFloat( m_cachedUsingEditorId, (m_inputPorts[ 2 ].IsConnected ? 0 : 1 ) );
  30. }
  31. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  32. {
  33. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  34. if( portId == 2 )
  35. {
  36. m_continuousPreviewRefresh = false;
  37. }
  38. }
  39. public override void OnInputPortDisconnected( int portId )
  40. {
  41. base.OnInputPortDisconnected( portId );
  42. if( portId == 2 )
  43. {
  44. m_continuousPreviewRefresh = true;
  45. }
  46. }
  47. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  48. {
  49. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  50. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  51. string result = string.Empty;
  52. string uv = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  53. string anchor = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  54. string time = 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. time += " * _Time.y";
  60. }
  61. result += uv;
  62. string cosVar = "cos" + OutputId;
  63. string sinVar = "sin" + OutputId;
  64. dataCollector.AddLocalVariable( UniqueId, "float " + cosVar + " = cos( "+time+" );");
  65. dataCollector.AddLocalVariable( UniqueId, "float " + sinVar + " = sin( "+time+" );");
  66. string value = "mul( " + result + " - " + anchor + " , float2x2( "+cosVar+" , -"+sinVar+" , "+sinVar+" , "+cosVar+" )) + "+anchor;
  67. RegisterLocalVariable( 0, value, ref dataCollector, "rotator" + OutputId );
  68. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  69. }
  70. public override void RefreshExternalReferences()
  71. {
  72. base.RefreshExternalReferences();
  73. if( UIUtils.CurrentShaderVersion() < 13107 )
  74. {
  75. m_inputPorts[ 2 ].FloatInternalData = 1;
  76. }
  77. }
  78. }
  79. }