Guest User

UISpriteswapping.shader

a guest
Nov 13th, 2020
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "UI/SpritesSwap"
  3. {
  4.     Properties
  5.     {
  6.         [PerRendererData]_MainTex("Sprite Texture", 2D) = "black" {}
  7.         [PerRendererData]_XOffset("XOffset", Float) = 8
  8.         [PerRendererData]_Offset("Offset", Float) = 8
  9.         _MainTex2("Sprite Texture Swapped", 2D) = "black" {}
  10.         _Color("Tint", Color) = (1,1,1,1)
  11.         [Toggle(DISSOLVE)] _DISSOLVE("Dissolve", Float) = 0
  12.         _Noise("Noise", 2D) = "white" {}
  13.         _NoiseStrength("Noise Strength", Range(0,1)) = 0.3
  14.         [Toggle(FLIPX)] _FLIPX("Flip X", Float) = 0
  15.         [Toggle(FLIPY)] _FLIPY("Flip Y", Float) = 0
  16.         _FlipTransition("Flip Transition", Range(0,1)) = 1
  17.         [Toggle(GRADIENTHOR)] _GRADIENTHOR("Horizontal Gradient", Float) = 1
  18.         [Toggle(GRADIENTVERT)] _GRADIENTVERT("Vertical Gradient", Float) = 0
  19.         _Transition("Transition", Range(0,1.4)) = 1  
  20.         _Edgewidth("Edge Width", Range(0,1)) = 0.1
  21.         _EdgeCol("Edge Color", Color) = (1,1,1,1)
  22.  
  23.         [HideInInspector]_StencilComp("Stencil Comparison", Float) = 8
  24.         [HideInInspector]  _Stencil("Stencil ID", Float) = 0
  25.         [HideInInspector] _StencilOp("Stencil Operation", Float) = 0
  26.         [HideInInspector] _StencilWriteMask("Stencil Write Mask", Float) = 255
  27.         [HideInInspector] _StencilReadMask("Stencil Read Mask", Float) = 255
  28.         [HideInInspector] _ColorMask("Color Mask", Float) = 15
  29.     }
  30.  
  31.         SubShader
  32.         {
  33.             Tags
  34.             {
  35.                 "Queue" = "Transparent"
  36.                 "IgnoreProjector" = "True"
  37.                 "RenderType" = "Transparent"
  38.                 "PreviewType" = "Plane"
  39.            
  40.              
  41.             }
  42.  
  43.             Stencil
  44.             {
  45.                 Ref[_Stencil]
  46.                 Comp[_StencilComp]
  47.                 Pass[_StencilOp]
  48.                 ReadMask[_StencilReadMask]
  49.                 WriteMask[_StencilWriteMask]
  50.             }
  51.  
  52.             Cull Off
  53.             Lighting Off
  54.             ZWrite Off
  55.             ZTest[unity_GUIZTestMode]
  56.             Blend SrcAlpha OneMinusSrcAlpha
  57.             ColorMask[_ColorMask]
  58.  
  59.             Pass
  60.             {
  61.                 Name "Default"
  62.             CGPROGRAM
  63.                 #pragma vertex vert
  64.                 #pragma fragment frag
  65.                 #pragma target 2.0
  66.                 #pragma shader_feature FLIPX
  67.                 #pragma shader_feature FLIPY
  68.                 #pragma shader_feature GRADIENTHOR
  69.                 #pragma shader_feature GRADIENTVERT
  70.                 #pragma shader_feature DISSOLVE
  71.                 #include "UnityCG.cginc"
  72.                 #include "UnityUI.cginc"
  73.  
  74.                 struct appdata_t
  75.                 {
  76.                     float4 vertex   : POSITION;
  77.                     float4 color    : COLOR;
  78.                     float4 texcoord : TEXCOORD0;
  79.                     UNITY_VERTEX_INPUT_INSTANCE_ID
  80.                 };
  81.  
  82.                 struct v2f
  83.                 {
  84.                     float4 vertex   : SV_POSITION;
  85.                     fixed4 color : COLOR;
  86.                     float2 texcoord  : TEXCOORD0;
  87.                     float4 worldPosition : TEXCOORD1;
  88.                     UNITY_VERTEX_OUTPUT_STEREO
  89.                 };
  90.  
  91.                 sampler2D _MainTex, _MainTex2, _Noise;
  92.                 fixed4 _Color;  
  93.                 float4 _MainTex_ST;
  94.  
  95.                 float _XOffset, _YOffset;
  96.  
  97.                 float _Transition, _Edgewidth, _NoiseStrength, _FlipTransition;
  98.                 float4 _EdgeCol;
  99.  
  100.                 float3 RotateAroundYInDegrees(float3 vertex, float degrees)
  101.                 {
  102.                     float alpha = degrees * UNITY_PI / 180.0;
  103.                     float sina, cosa;
  104.                     sincos(alpha, sina, cosa);
  105.                     float2x2 m = float2x2(cosa, -sina, sina, cosa);
  106.                     return float3(mul(vertex.xz, m), vertex.y).xzy;                  
  107.                 }
  108.                 float3 RotateAroundXInDegrees(float3 vertex, float degrees)
  109.                 {
  110.                     float alpha = degrees * UNITY_PI / 180.0;
  111.                     float sina, cosa;
  112.                     sincos(alpha, sina, cosa);
  113.                     float2x2 m = float2x2(cosa, -sina, sina, cosa);
  114.                     return float3(mul(vertex.yz, m), vertex.x).xzy;
  115.                 }
  116.            
  117.                 v2f vert(appdata_t v)
  118.                 {
  119.                     v2f OUT;
  120.                     UNITY_SETUP_INSTANCE_ID(v);
  121.                     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  122.                              
  123. #if FLIPX
  124.                     // return to 0
  125.                     v.vertex.x -= _XOffset;
  126.                     // rotate
  127.                     v.vertex.x = RotateAroundYInDegrees(v.vertex.xyz, _FlipTransition * 360).x;
  128.                     //set offset back
  129.                     v.vertex.x += _XOffset;
  130.                
  131. #endif
  132. #if FLIPY
  133.                     v.vertex.y -= _YOffset;
  134.                     v.vertex.y = RotateAroundXInDegrees(v.vertex.xyz, _FlipTransition * 360).x;
  135.                     v.vertex.y += _YOffset;
  136.  
  137. #endif
  138.                     OUT.worldPosition = v.vertex;
  139.                     OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);                  
  140.                     OUT.texcoord = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
  141.                     OUT.color = v.color * _Color;
  142.                     return OUT;
  143.                 }
  144.  
  145.                 fixed4 frag(v2f IN,fixed facing : VFACE) : SV_Target
  146.                 {
  147.                     // get the textures
  148.                     half4 noise = tex2D(_Noise, IN.texcoord) * IN.color * _NoiseStrength;
  149.                     half4 color = tex2D(_MainTex, IN.texcoord) * IN.color;
  150.                     half4 color2 = tex2D(_MainTex2, IN.texcoord) * IN.color;
  151.  
  152.                     float gradient = 1;
  153.                     float gradientEdge = 0;
  154.  
  155.                     // combine noise with uv
  156.                     float2 noiseGrad = IN.texcoord + noise;
  157. #if GRADIENTHOR
  158.                     // create cutoff
  159.                     gradient = step(_Transition, noiseGrad.x);
  160.                     // create line
  161.                     gradientEdge = step(_Transition - _Edgewidth, noiseGrad.x) - gradient;
  162.                     // only show texture 1
  163.                     color *= (gradient + gradientEdge);
  164.                     // only show texture 2 on inverted
  165.                     color2 *= 1 - (gradient);
  166. #endif
  167.  
  168. #if GRADIENTVERT
  169.                     gradient = step(_Transition, noiseGrad.y);
  170.                     gradientEdge = step(_Transition - _Edgewidth, noiseGrad.y) - gradient;
  171.                     color *= (gradient + gradientEdge);
  172.                     color2 *= 1 - (gradient);
  173. #endif
  174.                  
  175. #if DISSOLVE
  176.                     gradient = step(_Transition, noise.r);
  177.                     gradientEdge = step(_Transition - _Edgewidth, noise.r) - gradient;
  178.                     color *= (gradient + gradientEdge);
  179.                     color2 *= 1 - (gradient);
  180. #endif
  181.                     // add color to the edge, and multiply with alpha for a nicer transition
  182.                     float4 coloredEdge = gradientEdge * _EdgeCol * (color.a + color2.a);
  183.                     // combine everything
  184.                     float4 final = (color + color2 + coloredEdge);
  185. #if FLIPX || FLIPY
  186.                     //VFACE returns positive for front facing, negative for backfacing
  187.                     final =  facing > 0 ? color : color2 ;
  188. #endif
  189.                     return final;                  
  190.                 }
  191.             ENDCG
  192.             }
  193.         }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment