Advertisement
Coguelin

UI-AdditiveFlow

Aug 11th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/UI/AdditiveFlow"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Sprite Texture", 2D) = "white" {}
  6.         _Color ("Tint", Color) = (1,1,1,1)
  7.        
  8.         _StencilComp ("Stencil Comparison", Float) = 8
  9.         _Stencil ("Stencil ID", Float) = 0
  10.         _StencilOp ("Stencil Operation", Float) = 0
  11.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
  12.         _StencilReadMask ("Stencil Read Mask", Float) = 255
  13.  
  14.         _ColorMask ("Color Mask", Float) = 15
  15.  
  16.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
  17.  
  18.         _FlowTex("Flow Texture", 2D) = "grey" {}
  19.         _XMul ("UV.x mul", float) = 0.25
  20.         _YMul ("UV.y mul", float) = 0.25
  21.         _Speed ("Speed", float) = 0.05
  22.     }
  23.  
  24.     SubShader
  25.     {
  26.         Tags
  27.         {
  28.             "Queue"="Transparent"
  29.             "IgnoreProjector"="True"
  30.             "RenderType"="Transparent"
  31.             "PreviewType"="Plane"
  32.             "CanUseSpriteAtlas"="True"
  33.         }
  34.        
  35.         Stencil
  36.         {
  37.             Ref [_Stencil]
  38.             Comp [_StencilComp]
  39.             Pass [_StencilOp]
  40.             ReadMask [_StencilReadMask]
  41.             WriteMask [_StencilWriteMask]
  42.         }
  43.  
  44.         Cull Off
  45.         Lighting Off
  46.         ZWrite Off
  47.         ZTest [unity_GUIZTestMode]
  48.         Blend SrcAlpha OneMinusSrcAlpha
  49.         ColorMask [_ColorMask]
  50.  
  51.         Pass
  52.         {
  53.         CGPROGRAM
  54.             #pragma vertex vert
  55.             #pragma fragment frag
  56.  
  57.             #include "UnityCG.cginc"
  58.             #include "UnityUI.cginc"
  59.  
  60.             #pragma multi_compile __ UNITY_UI_ALPHACLIP
  61.            
  62.             struct appdata_t
  63.             {
  64.                 float4 vertex   : POSITION;
  65.                 float4 color    : COLOR;
  66.                 float2 texcoord : TEXCOORD0;
  67.                 float2 flowcoord : TEXCOORD1;
  68.             };
  69.  
  70.             struct v2f
  71.             {
  72.                 float4 vertex   : SV_POSITION;
  73.                 fixed4 color    : COLOR;
  74.                 half2 texcoord  : TEXCOORD0;
  75.                 float2 flowcoord : TEXCOORD1;
  76.                 float4 worldPosition : TEXCOORD2;
  77.             };
  78.            
  79.             fixed4 _Color;
  80.             fixed4 _TextureSampleAdd;
  81.             float4 _ClipRect;
  82.  
  83.             v2f vert(appdata_t IN)
  84.             {
  85.                 v2f OUT;
  86.                 OUT.worldPosition = IN.vertex;
  87.                 OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);
  88.  
  89.                 //OUT.texcoord = TRANSFORM_TEX(IN.texcoord, _MainTex);
  90.                 OUT.texcoord = IN.texcoord;
  91.                
  92.                 #ifdef UNITY_HALF_TEXEL_OFFSET
  93.                 OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
  94.                 #endif
  95.  
  96.                 //OUT.flowcoord = TRANSFORM_TEX(IN.flowcoord, _FlowTex);
  97.                 OUT.flowcoord = IN.flowcoord;
  98.  
  99.                 OUT.color = IN.color * _Color;
  100.                 return OUT;
  101.             }
  102.  
  103.             uniform sampler2D _MainTex;
  104.             uniform float4 _MainTex_ST;
  105.             uniform sampler2D _FlowTex;
  106.             uniform float4 _FlowTex_ST;
  107.             uniform fixed _XMul;
  108.             uniform fixed _YMul;
  109.             uniform fixed _Speed;
  110.  
  111.             fixed4 frag(v2f IN) : SV_Target
  112.             {
  113.                 fixed phase = _Time[1];
  114.                 fixed2 flowDir = fixed2(_XMul, _YMul) * _Speed;
  115.                 fixed2 flowUV = IN.flowcoord + flowDir * phase;
  116.                 fixed4 tex1 = tex2D(_FlowTex, flowUV);
  117.                 fixed4 tex0 = tex2D(_MainTex, IN.texcoord);
  118.  
  119.                 fixed4 add = tex1 < .5 ? 2.0 * tex1 * tex0 : 1.0 - 2.0 * (1.0 - tex1) * (1.0 - tex0); //additive calc
  120.  
  121.                 fixed4 c = (add + _TextureSampleAdd) * IN.color;
  122.  
  123.                 c.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  124.                
  125.                 #ifdef UNITY_UI_ALPHACLIP
  126.                 clip (c.a - 0.001);
  127.                 #endif
  128.  
  129.                 return c;
  130.             }
  131.         ENDCG
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement