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.

238 lines
12 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //
  4. // Custom Node Flipbook UV Animation
  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( "Flipbook UV Animation", "UV Coordinates", "Animate a Flipbook Texture Modifying UV Coordinates.", null, KeyCode.None, true, false, null, null, "The Four Headed Cat - @fourheadedcat" )]
  13. public sealed class TFHCFlipBookUVAnimation : ParentNode
  14. {
  15. private const string TextureVerticalDirectionStr = "Texture Direction";
  16. private const string NegativeSpeedBehaviorStr = "If Negative Speed";
  17. [SerializeField]
  18. private int m_selectedTextureVerticalDirection = 0;
  19. [SerializeField]
  20. private int m_negativeSpeedBehavior = 0;
  21. [SerializeField]
  22. private readonly string[] m_textureVerticalDirectionValues = { "Top To Bottom", "Bottom To Top" };
  23. [SerializeField]
  24. private readonly string[] m_negativeSpeedBehaviorValues = { "Switch to Positive", "Reverse Animation" };
  25. protected override void CommonInit( int uniqueId )
  26. {
  27. base.CommonInit( uniqueId );
  28. AddInputPort( WirePortDataType.FLOAT2, false, "UV" );
  29. AddInputPort( WirePortDataType.FLOAT, false, "Columns" );
  30. AddInputPort( WirePortDataType.FLOAT, false, "Rows" );
  31. AddInputPort( WirePortDataType.FLOAT, false, "Speed" );
  32. AddInputPort( WirePortDataType.FLOAT, false, "Start Frame" );
  33. AddInputPort( WirePortDataType.FLOAT, false, "Time" );
  34. AddOutputVectorPorts( WirePortDataType.FLOAT2, "UV" );
  35. m_outputPorts[ 1 ].Name = "U";
  36. m_outputPorts[ 2 ].Name = "V";
  37. m_textLabelWidth = 125;
  38. m_useInternalPortData = true;
  39. m_autoWrapProperties = true;
  40. m_previewShaderGUID = "04fe24be792bfd5428b92132d7cf0f7d";
  41. }
  42. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  43. {
  44. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  45. if( portId == 5 )
  46. {
  47. m_previewMaterialPassId = 1;
  48. }
  49. }
  50. public override void OnInputPortDisconnected( int portId )
  51. {
  52. base.OnInputPortDisconnected( portId );
  53. if( portId == 5 )
  54. {
  55. m_previewMaterialPassId = 0;
  56. }
  57. }
  58. public override void DrawProperties()
  59. {
  60. base.DrawProperties();
  61. EditorGUILayout.BeginVertical();
  62. m_selectedTextureVerticalDirection = EditorGUILayoutPopup( TextureVerticalDirectionStr, m_selectedTextureVerticalDirection, m_textureVerticalDirectionValues );
  63. m_negativeSpeedBehavior = EditorGUILayoutPopup( NegativeSpeedBehaviorStr, m_negativeSpeedBehavior, m_negativeSpeedBehaviorValues );
  64. EditorGUILayout.EndVertical();
  65. EditorGUILayout.HelpBox( "Flipbook UV Animation:\n\n - UV: Texture Coordinates to Flipbook.\n - Columns: number of Columns (X) of the Flipbook Texture.\n - Rows: number of Rows (Y) of the Flipbook Textures.\n - Speed: speed of the animation.\n - Texture Direction: set the vertical order of the texture tiles.\n - If Negative Speed: set the behavior when speed is negative.\n\n - Out: UV Coordinates.", MessageType.None );
  66. }
  67. public override void ReadFromString( ref string[] nodeParams )
  68. {
  69. base.ReadFromString( ref nodeParams );
  70. m_selectedTextureVerticalDirection = ( int ) int.Parse( GetCurrentParam( ref nodeParams ) );
  71. m_negativeSpeedBehavior = ( int ) int.Parse( GetCurrentParam( ref nodeParams ) );
  72. }
  73. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  74. {
  75. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  76. IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedTextureVerticalDirection );
  77. IOUtils.AddFieldValueToString( ref nodeInfo, m_negativeSpeedBehavior );
  78. }
  79. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  80. {
  81. // OPTIMIZATION NOTES
  82. //
  83. // round( fmod( x, y ) ) can be replaced with a faster
  84. // floor( frac( x / y ) * y + 0.5 ) => div can be muls with 1/y, almost always static/constant
  85. //
  86. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  87. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  88. string uv = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  89. string columns = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  90. if ( !m_inputPorts[ 1 ].IsConnected )
  91. columns = ( float.Parse( columns ) == 0f ? "1" : columns );
  92. string rows = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  93. if ( !m_inputPorts[ 2 ].IsConnected )
  94. rows = ( float.Parse( rows ) == 0f ? "1" : rows );
  95. string speed = m_inputPorts[ 3 ].GeneratePortInstructions( ref dataCollector );
  96. string startframe = m_inputPorts[ 4 ].GeneratePortInstructions( ref dataCollector );
  97. string timer = m_inputPorts[ 5 ].IsConnected ? m_inputPorts[ 5 ].GeneratePortInstructions( ref dataCollector ) : "_Time[ 1 ]";
  98. string vcomment1 = "// *** BEGIN Flipbook UV Animation vars ***";
  99. string vcomment2 = "// Total tiles of Flipbook Texture";
  100. string vtotaltiles = "float fbtotaltiles" + OutputId + " = " + columns + " * " + rows + ";";
  101. string vcomment3 = "// Offsets for cols and rows of Flipbook Texture";
  102. string vcolsoffset = "float fbcolsoffset" + OutputId + " = 1.0f / " + columns + ";";
  103. string vrowssoffset = "float fbrowsoffset" + OutputId + " = 1.0f / " + rows + ";";
  104. string vcomment4 = "// Speed of animation";
  105. string vspeed = string.Format( "float fbspeed{0} = {1} * {2};", OutputId,timer,speed);
  106. string vcomment5 = "// UV Tiling (col and row offset)";
  107. string vtiling = "float2 fbtiling" + OutputId + " = float2(fbcolsoffset" + OutputId + ", fbrowsoffset" + OutputId + ");";
  108. string vcomment6 = "// UV Offset - calculate current tile linear index, and convert it to (X * coloffset, Y * rowoffset)";
  109. string vcomment7 = "// Calculate current tile linear index";
  110. //float fbcurrenttileindex1 = round( fmod( fbspeed1 + _Float0, fbtotaltiles1 ) );
  111. string vcurrenttileindex = "float fbcurrenttileindex" + OutputId + " = round( fmod( fbspeed" + OutputId + " + " + startframe + ", fbtotaltiles" + OutputId + ") );";
  112. string vcurrenttileindex1 = "fbcurrenttileindex" + OutputId + " += ( fbcurrenttileindex" + OutputId + " < 0) ? fbtotaltiles" + OutputId + " : 0;";
  113. //fbcurrenttileindex1 += ( fbcurrenttileindex1 < 0 ) ? fbtotaltiles1 : 0;
  114. //string vcurrenttileindex = "int fbcurrenttileindex" + m_uniqueId + " = (int)fmod( fbspeed" + m_uniqueId + ", fbtotaltiles" + m_uniqueId + ") + " + startframe + ";";
  115. string vcomment8 = "// Obtain Offset X coordinate from current tile linear index";
  116. //float fblinearindextox1 = round( fmod( fbcurrenttileindex1, 5.0 ) );
  117. //string voffsetx1 = "int fblinearindextox" + m_uniqueId + " = fbcurrenttileindex" + m_uniqueId + " % (int)" + columns + ";";
  118. string voffsetx1 = "float fblinearindextox" + OutputId + " = round ( fmod ( fbcurrenttileindex" + OutputId + ", " + columns + " ) );";
  119. string vcomment9 = String.Empty;
  120. string voffsetx2 = String.Empty;
  121. if ( m_negativeSpeedBehavior != 0 )
  122. {
  123. vcomment9 = "// Reverse X animation if speed is negative";
  124. voffsetx2 = "fblinearindextox" + OutputId + " = (" + speed + " > 0 ? fblinearindextox" + OutputId + " : (int)" + columns + " - fblinearindextox" + OutputId + ");";
  125. }
  126. string vcomment10 = "// Multiply Offset X by coloffset";
  127. string voffsetx3 = "float fboffsetx" + OutputId + " = fblinearindextox" + OutputId + " * fbcolsoffset" + OutputId + ";";
  128. string vcomment11 = "// Obtain Offset Y coordinate from current tile linear index";
  129. //float fblinearindextoy1 = round( fmod( ( fbcurrenttileindex1 - fblinearindextox1 ) / 5.0, 5.0 ) );
  130. string voffsety1 = "float fblinearindextoy" + OutputId + " = round( fmod( ( fbcurrenttileindex" + OutputId + " - fblinearindextox" + OutputId + " ) / " + columns + ", " + rows + " ) );";
  131. //string voffsety1 = "int fblinearindextoy" + m_uniqueId + " = (int)( ( fbcurrenttileindex" + m_uniqueId + " - fblinearindextox" + m_uniqueId + " ) / " + columns + " ) % (int)" + rows + ";";
  132. //string vcomment10 = "// Reverse Y to get from Top to Bottom";
  133. //string voffsety2 = "fblinearindextoy" + m_uniqueId + " = (int)" + rows + " - fblinearindextoy" + m_uniqueId + ";";
  134. string vcomment12 = String.Empty;
  135. string voffsety2 = String.Empty;
  136. if ( m_negativeSpeedBehavior == 0 )
  137. {
  138. if ( m_selectedTextureVerticalDirection == 0 )
  139. {
  140. vcomment12 = "// Reverse Y to get tiles from Top to Bottom";
  141. voffsety2 = "fblinearindextoy" + OutputId + " = (int)(" + rows + "-1) - fblinearindextoy" + OutputId + ";";
  142. }
  143. }
  144. else
  145. {
  146. string reverseanimationoperator = String.Empty;
  147. if ( m_selectedTextureVerticalDirection == 0 )
  148. {
  149. vcomment12 = "// Reverse Y to get tiles from Top to Bottom and Reverse Y animation if speed is negative";
  150. reverseanimationoperator = " < ";
  151. }
  152. else
  153. {
  154. vcomment12 = "// Reverse Y animation if speed is negative";
  155. reverseanimationoperator = " > ";
  156. }
  157. voffsety2 = "fblinearindextoy" + OutputId + " = (" + speed + reverseanimationoperator + " 0 ? fblinearindextoy" + OutputId + " : (int)" + rows + " - fblinearindextoy" + OutputId + ");";
  158. }
  159. string vcomment13 = "// Multiply Offset Y by rowoffset";
  160. string voffsety3 = "float fboffsety" + OutputId + " = fblinearindextoy" + OutputId + " * fbrowsoffset" + OutputId + ";";
  161. string vcomment14 = "// UV Offset";
  162. string voffset = "float2 fboffset" + OutputId + " = float2(fboffsetx" + OutputId + ", fboffsety" + OutputId + ");";
  163. //string voffset = "float2 fboffset" + m_uniqueId + " = float2( ( ( (int)fmod( fbspeed" + m_uniqueId + " , fbtotaltiles" + m_uniqueId + ") % (int)" + columns + " ) * fbcolsoffset" + m_OutputId + " ) , ( ( (int)" + rows + " - ( (int)( ( (int)fmod( fbspeed" + m_uniqueId + " , fbtotaltiles" + m_uniqueId + " ) - ( (int)fmod( fbspeed" + m_uniqueId + " , fbtotaltiles" + m_uniqueId + " ) % (int)" + columns + " ) ) / " + columns + " ) % (int)" + rows + " ) ) * fbrowsoffset" + m_uniqueId + " ) );";
  164. string vcomment15 = "// Flipbook UV";
  165. string vfbuv = "half2 fbuv" + OutputId + " = " + uv + " * fbtiling" + OutputId + " + fboffset" + OutputId + ";";
  166. string vcomment16 = "// *** END Flipbook UV Animation vars ***";
  167. string result = "fbuv" + OutputId;
  168. dataCollector.AddLocalVariable( UniqueId, vcomment1 );
  169. dataCollector.AddLocalVariable( UniqueId, vcomment2 );
  170. dataCollector.AddLocalVariable( UniqueId, vtotaltiles );
  171. dataCollector.AddLocalVariable( UniqueId, vcomment3 );
  172. dataCollector.AddLocalVariable( UniqueId, vcolsoffset );
  173. dataCollector.AddLocalVariable( UniqueId, vrowssoffset );
  174. dataCollector.AddLocalVariable( UniqueId, vcomment4 );
  175. dataCollector.AddLocalVariable( UniqueId, vspeed );
  176. dataCollector.AddLocalVariable( UniqueId, vcomment5 );
  177. dataCollector.AddLocalVariable( UniqueId, vtiling );
  178. dataCollector.AddLocalVariable( UniqueId, vcomment6 );
  179. dataCollector.AddLocalVariable( UniqueId, vcomment7 );
  180. dataCollector.AddLocalVariable( UniqueId, vcurrenttileindex );
  181. dataCollector.AddLocalVariable( UniqueId, vcurrenttileindex1 );
  182. dataCollector.AddLocalVariable( UniqueId, vcomment8 );
  183. dataCollector.AddLocalVariable( UniqueId, voffsetx1 );
  184. if ( m_negativeSpeedBehavior != 0 )
  185. {
  186. dataCollector.AddLocalVariable( UniqueId, vcomment9 );
  187. dataCollector.AddLocalVariable( UniqueId, voffsetx2 );
  188. }
  189. dataCollector.AddLocalVariable( UniqueId, vcomment10 );
  190. dataCollector.AddLocalVariable( UniqueId, voffsetx3 );
  191. dataCollector.AddLocalVariable( UniqueId, vcomment11 );
  192. dataCollector.AddLocalVariable( UniqueId, voffsety1 );
  193. if ( m_selectedTextureVerticalDirection == 0 || m_negativeSpeedBehavior != 0 )
  194. {
  195. dataCollector.AddLocalVariable( UniqueId, vcomment12 );
  196. dataCollector.AddLocalVariable( UniqueId, voffsety2 );
  197. }
  198. dataCollector.AddLocalVariable( UniqueId, vcomment13 );
  199. dataCollector.AddLocalVariable( UniqueId, voffsety3 );
  200. dataCollector.AddLocalVariable( UniqueId, vcomment14 );
  201. dataCollector.AddLocalVariable( UniqueId, voffset );
  202. dataCollector.AddLocalVariable( UniqueId, vcomment15 );
  203. dataCollector.AddLocalVariable( UniqueId, vfbuv );
  204. dataCollector.AddLocalVariable( UniqueId, vcomment16 );
  205. m_outputPorts[ 0 ].SetLocalValue( result, dataCollector.PortCategory );
  206. return GetOutputVectorItem( 0, outputId, result );
  207. }
  208. }
  209. }