Preview_TFHCFlipBookUVAnimation.shader 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. Shader "Hidden/TFHCFlipBookUVAnimation"
  2. {
  3. Properties
  4. {
  5. _A ("_UV", 2D) = "white" {}
  6. _B ("_Columns", 2D) = "white" {}
  7. _C ("_Rows", 2D) = "white" {}
  8. _D ("_Speed", 2D) = "white" {}
  9. _E ("_StartFrame", 2D) = "white" {}
  10. _F ("_Speed", 2D) = "white" {}
  11. _G( "_MaxFrame", 2D ) = "white" {}
  12. }
  13. SubShader
  14. {
  15. CGINCLUDE
  16. #include "UnityCG.cginc"
  17. #include "Preview.cginc"
  18. #pragma vertex vert_img
  19. #pragma fragment frag
  20. sampler2D _A;
  21. sampler2D _B;
  22. sampler2D _C;
  23. sampler2D _D;
  24. sampler2D _E;
  25. sampler2D _F;
  26. sampler2D _G;
  27. float _EditorTime;
  28. ENDCG
  29. //Time port disconnected
  30. Pass
  31. {
  32. CGPROGRAM
  33. float4 frag(v2f_img i) : SV_Target
  34. {
  35. float2 uv = tex2D( _A, i.uv ).rg;
  36. float col = tex2D( _B, i.uv ).r;
  37. float row = tex2D( _C, i.uv ).r;
  38. float spd = tex2D( _D, i.uv ).r;
  39. float str = tex2D( _E, i.uv ).r;
  40. float maxframe = tex2D( _G, i.uv ).r;
  41. float numframes = col * row;
  42. float fbtotaltiles = ( maxframe < 0 ) ? numframes : min( numframes, maxframe + 1 );
  43. float fbcolsoffset = 1.0f / col;
  44. float fbrowsoffset = 1.0f / row;
  45. float fbspeed = _EditorTime * spd;
  46. float2 fbtiling = float2(fbcolsoffset, fbrowsoffset);
  47. float fbcurrenttileindex = floor( fmod( fbspeed + str, fbtotaltiles) );
  48. fbcurrenttileindex += ( fbcurrenttileindex < 0) ? fbtotaltiles : 0;
  49. float fblinearindextox = round ( fmod ( fbcurrenttileindex, col ) );
  50. float fboffsetx = fblinearindextox * fbcolsoffset;
  51. float fblinearindextoy = round( fmod( ( fbcurrenttileindex - fblinearindextox ) / col, row ) );
  52. fblinearindextoy = (int)(row-1) - fblinearindextoy;
  53. float fboffsety = fblinearindextoy * fbrowsoffset;
  54. float2 fboffset = float2(fboffsetx, fboffsety);
  55. float2 fbuv = float4( uv, 0.0 , 0.0 ) * fbtiling + fboffset;
  56. return float4(fbuv, 0 , 0);
  57. }
  58. ENDCG
  59. }
  60. //Time port connected
  61. Pass
  62. {
  63. CGPROGRAM
  64. float4 frag(v2f_img i) : SV_Target
  65. {
  66. float2 uv = tex2D( _A, i.uv ).rg;
  67. float col = tex2D( _B, i.uv ).r;
  68. float row = tex2D( _C, i.uv ).r;
  69. float spd = tex2D( _D, i.uv ).r;
  70. float str = tex2D( _E, i.uv ).r;
  71. float time = tex2D( _F, i.uv ).r;
  72. float maxframe = tex2D( _G, i.uv ).r;
  73. float numframes = col * row;
  74. float fbtotaltiles = ( maxframe < 0 ) ? numframes : min( numframes, maxframe + 1 );
  75. float fbcolsoffset = 1.0f / col;
  76. float fbrowsoffset = 1.0f / row;
  77. float fbspeed = time * spd;
  78. float2 fbtiling = float2(fbcolsoffset, fbrowsoffset);
  79. float fbcurrenttileindex = floor( fmod( fbspeed + str, fbtotaltiles) );
  80. fbcurrenttileindex += ( fbcurrenttileindex < 0) ? fbtotaltiles : 0;
  81. float fblinearindextox = round ( fmod ( fbcurrenttileindex, col ) );
  82. float fboffsetx = fblinearindextox * fbcolsoffset;
  83. float fblinearindextoy = round( fmod( ( fbcurrenttileindex - fblinearindextox ) / col, row ) );
  84. fblinearindextoy = (int)(row-1) - fblinearindextoy;
  85. float fboffsety = fblinearindextoy * fbrowsoffset;
  86. float2 fboffset = float2(fboffsetx, fboffsety);
  87. float2 fbuv = float4( uv, 0.0 , 0.0 ) * fbtiling + fboffset;
  88. return float4(fbuv, 0 , 0);
  89. }
  90. ENDCG
  91. }
  92. }
  93. }