Advertisement
Domukas64

Scrolling multiplied texture UI shader

Aug 25th, 2018
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. Shader "UI/Scrolling Textures"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  6. [HDR]_Color ("Tint", Color) = (1,1,1,1)
  7.  
  8. _UnscaledTimeScale("Unscaled Time Scale", float) = 1
  9. _Tex1("Layer 1", 2D) = "white" {}
  10. _Tex2("Layer 2", 2D) = "white" {}
  11. _Tex3("Layer 3", 2D) = "white" {}
  12. _Tex4("Layer 4", 2D) = "white" {}
  13.  
  14. _StencilComp ("Stencil Comparison", Float) = 8
  15. _Stencil ("Stencil ID", Float) = 0
  16. _StencilOp ("Stencil Operation", Float) = 0
  17. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  18. _StencilReadMask ("Stencil Read Mask", Float) = 255
  19.  
  20. _ColorMask ("Color Mask", Float) = 15
  21.  
  22. [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
  23. }
  24.  
  25. SubShader
  26. {
  27. Tags
  28. {
  29. "Queue"="Transparent"
  30. "IgnoreProjector"="True"
  31. "RenderType"="Transparent"
  32. "PreviewType"="Plane"
  33. "CanUseSpriteAtlas"="True"
  34. }
  35.  
  36. Stencil
  37. {
  38. Ref [_Stencil]
  39. Comp [_StencilComp]
  40. Pass [_StencilOp]
  41. ReadMask [_StencilReadMask]
  42. WriteMask [_StencilWriteMask]
  43. }
  44.  
  45. Cull Off
  46. Lighting Off
  47. ZWrite Off
  48. ZTest [unity_GUIZTestMode]
  49. Blend SrcAlpha OneMinusSrcAlpha
  50. ColorMask [_ColorMask]
  51.  
  52. Pass
  53. {
  54. Name "Default"
  55. CGPROGRAM
  56. #pragma vertex vert
  57. #pragma fragment frag
  58. #pragma target 2.0
  59.  
  60. #include "UnityCG.cginc"
  61. #include "UnityUI.cginc"
  62.  
  63. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  64. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  65.  
  66. struct appdata_t
  67. {
  68. float4 vertex : POSITION;
  69. float4 color : COLOR;
  70. float2 texcoord : TEXCOORD0;
  71. float2 texcoord2 : TEXCOORD1; // This can also be used as timer set from UI Vertex Effect script
  72. UNITY_VERTEX_INPUT_INSTANCE_ID
  73. };
  74.  
  75. struct v2f
  76. {
  77. float4 vertex : SV_POSITION;
  78. fixed4 color : COLOR;
  79. float2 texcoord : TEXCOORD0;
  80. float4 worldPosition : TEXCOORD1;
  81. float4 texcoordScrolling12 : TEXCOORD2;
  82. float4 texcoordScrolling34 : TEXCOORD3;
  83.  
  84. UNITY_VERTEX_OUTPUT_STEREO
  85. };
  86.  
  87. fixed4 _Color;
  88. fixed4 _TextureSampleAdd;
  89. float4 _ClipRect;
  90.  
  91. //Scrolling Textures - Begin:
  92. sampler2D _Tex1;
  93. float4 _Tex1_ST;
  94. sampler2D _Tex2;
  95. float4 _Tex2_ST;
  96. sampler2D _Tex3;
  97. float4 _Tex3_ST;
  98. sampler2D _Tex4;
  99. float4 _Tex4_ST;
  100.  
  101. float4 _UnscaledTime; // This is something you would like to set if you want your animation to continue if time scale is zero
  102. float _UnscaledTimeScale;
  103. //Scrolling Textures - End:
  104.  
  105. v2f vert(appdata_t v)
  106. {
  107. v2f OUT;
  108. UNITY_SETUP_INSTANCE_ID(v);
  109. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  110. OUT.worldPosition = v.vertex;
  111. OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  112.  
  113. OUT.texcoord = v.texcoord;
  114.  
  115. //Scrolling Textures - Begin:
  116. float time = _Time.y + v.texcoord2.x + _UnscaledTime.w * _UnscaledTimeScale;
  117. OUT.texcoordScrolling12.xy = v.texcoord.xy * _Tex1_ST.xy + _Tex1_ST.zw * time;
  118. OUT.texcoordScrolling12.zw = v.texcoord.xy * _Tex2_ST.xy + _Tex2_ST.zw * time;
  119. OUT.texcoordScrolling34.xy = v.texcoord.xy * _Tex3_ST.xy + _Tex3_ST.zw * time;
  120. OUT.texcoordScrolling34.zw = v.texcoord.xy * _Tex4_ST.xy + _Tex4_ST.zw * time;
  121. //Scrolling Textures - End:
  122.  
  123. OUT.color = v.color * _Color;
  124. return OUT;
  125. }
  126.  
  127. sampler2D _MainTex;
  128.  
  129. fixed4 frag(v2f IN) : SV_Target
  130. {
  131. half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
  132.  
  133. //Scrolling Textures - Begin:
  134. float4 tex1 = tex2D(_Tex1, IN.texcoordScrolling12.xy);
  135. float4 tex2 = tex2D(_Tex2, IN.texcoordScrolling12.zw);
  136. float4 tex3 = tex2D(_Tex3, IN.texcoordScrolling34.xy);
  137. float4 tex4 = tex2D(_Tex4, IN.texcoordScrolling34.zw);
  138.  
  139. color *= tex1 * tex2 * tex3 * tex4 * 8;
  140. //Scrolling Textures - End:
  141.  
  142.  
  143. #ifdef UNITY_UI_CLIP_RECT
  144. color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  145. #endif
  146.  
  147. #ifdef UNITY_UI_ALPHACLIP
  148. clip (color.a - 0.001);
  149. #endif
  150.  
  151. return color;
  152. }
  153. ENDCG
  154. }
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement