Advertisement
Muk99

UIBlur2

Mar 12th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "UI/Blur"{
  2.     Properties{
  3.         [PerRendererData]_MainTex("Sprite Texture", 2D) = "white" {}
  4.         _Color("Tint", Color) = (1,1,1,1)
  5.         _Horizontal("Horizontal Deviation", Range(1, 50)) = 30
  6.         _Vertical("Vertical Deviation", Range(1, 50)) = 30
  7.         [Toggle]_Fix("(BETA)Fix Y Position", Float) = 0
  8.  
  9.         [HideInInspector]_StencilComp("Stencil Comparison", Float) = 8
  10.         [HideInInspector]_Stencil("Stencil ID", Float) = 0
  11.         [HideInInspector]_StencilOp("Stencil Operation", Float) = 0
  12.         [HideInInspector]_StencilWriteMask("Stencil Write Mask", Float) = 255
  13.         [HideInInspector]_StencilReadMask("Stencil Read Mask", Float) = 255
  14.         [HideInInspector]_UseUIAlphaClip("Use Alpha Clip", Float) = 0
  15.         [HideInInspector]_ColorMask("Color Mask", Float) = 15
  16.     }
  17.  
  18.     SubShader{
  19.         Tags {
  20.         "Queue" = "Transparent"
  21.         "IgnoreProjector" = "True"
  22.         "RenderType" = "Transparent"
  23.         "PreviewType" = "Plane"
  24.         "CanUseSpriteAtlas" = "True"
  25.         }
  26.  
  27.         Stencil{
  28.             Ref[_Stencil]
  29.             Comp[_StencilComp]
  30.             Pass[_StencilOp]
  31.             ReadMask[_StencilReadMask]
  32.             WriteMask[_StencilWriteMask]
  33.         }
  34.  
  35.         Cull Off
  36.         Lighting Off
  37.         ZWrite Off
  38.         ZTest[unity_GUIZTestMode]
  39.         Blend SrcAlpha OneMinusSrcAlpha
  40.         ColorMask[_ColorMask]
  41.                
  42.         GrabPass {
  43.             Tags{
  44.                 "LightMode" = "Always"
  45.             }
  46.         }
  47.  
  48.         //Horizontal blur pass
  49.         Pass {
  50.             Tags{
  51.                 "LightMode" = "Always"
  52.             }
  53.  
  54.             CGPROGRAM
  55.             #pragma vertex vert
  56.             #pragma fragment frag
  57.             #pragma fragmentoption ARB_precision_hint_fastest
  58.             #include "UnityCG.cginc"
  59.  
  60.             struct appdata_t {
  61.                 half4 vertex : POSITION;
  62.                 half2 texcoord: TEXCOORD0;
  63.                 half4 worldPosition : TEXCOORD1;
  64.                 half4 color : COLOR;
  65.             };
  66.  
  67.             uint _Fix;
  68.  
  69.             struct v2f {
  70.                 half4 vertex : SV_POSITION;
  71.                 half4 uvgrab : TEXCOORD0;
  72.                 half2 position : TEXCOORD1;
  73.                 half4 color : COLOR;
  74.             };
  75.  
  76.             v2f vert(appdata_t v) {
  77. #if UNITY_UV_STARTS_AT_TOP
  78.                 float scale = -1.0;
  79. #else
  80.                 float scale = 1.0;
  81. #endif
  82.                 v2f o;
  83.  
  84.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  85.                 o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  86.                 o.uvgrab.zw = 0;
  87.                 o.position = v.texcoord;
  88.                 o.color = v.color;
  89.  
  90.                 if (_Fix)
  91.                     o.uvgrab.y += 0.115 / 2;
  92.  
  93.                 return o;
  94.             }
  95.  
  96.             sampler2D _MainTex;
  97.             sampler2D _GrabTexture;
  98.             half4 _Color;
  99.             half _Horizontal;
  100.  
  101.             half4 frag(v2f i) : COLOR{
  102.                 half4 sum = half4(0,0,0,0);
  103.                 half iterations = _Horizontal * 3;
  104.  
  105.                 #define GFunc(x,d) (1/(sqrt(2*3.14*d*d))) * exp(-(x*x / (2*d*d)))
  106.                
  107.                 for(int j = -iterations /2; j<iterations /2; j++)
  108.                     sum += tex2D(_GrabTexture, float2(i.uvgrab.x + j * _Horizontal / 500.0 / iterations, i.uvgrab.y)) * GFunc(j, _Horizontal);
  109.  
  110.                 sum.a = tex2D(_MainTex, i.position).a;
  111.  
  112.                 return sum * _Color;
  113.             }
  114.  
  115.             ENDCG
  116.         }
  117.                    
  118.         GrabPass{
  119.             Tags{
  120.                 "LightMode" = "Always"
  121.             }
  122.          }
  123.        
  124.         //Vertical blur pass
  125.         Pass{
  126.             Tags{
  127.                 "LightMode" = "Always"
  128.             }
  129.  
  130.                 CGPROGRAM
  131.                 #pragma vertex vert
  132.                 #pragma fragment frag
  133.                 #pragma fragmentoption ARB_precision_hint_fastest
  134.                 #include "UnityCG.cginc"
  135.  
  136.             struct appdata_t {
  137.                 half4 vertex : POSITION;
  138.                 half2 texcoord: TEXCOORD0;
  139.                 half4 worldPosition : TEXCOORD1;
  140.                 half4 color : COLOR;
  141.             };
  142.  
  143.             struct v2f {
  144.                 half4 vertex : SV_POSITION;
  145.                 half4 uvgrab : TEXCOORD0;
  146.                 half2 position : TEXCOORD1;
  147.                 half4 color : COLOR;
  148.             };
  149.  
  150.             uint _Fix;
  151.  
  152.             v2f vert(appdata_t v) {
  153. #if UNITY_UV_STARTS_AT_TOP
  154.                 float scale = -1.0;
  155. #else
  156.                 float scale = 1.0;
  157. #endif
  158.                 v2f o;
  159.  
  160.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  161.                 o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  162.                 o.uvgrab.zw = 0;
  163.                 o.position = v.texcoord;
  164.                 o.color = v.color;
  165.  
  166.                 if (_Fix)
  167.                     o.uvgrab.y += 0.115 / 2;
  168.  
  169.                 return o;
  170.             }
  171.  
  172.             sampler2D _MainTex;
  173.             sampler2D _GrabTexture;
  174.             half4 _Color;
  175.             half _Vertical;
  176.  
  177.             half4 frag(v2f i) : COLOR{
  178.                 half4 sum = half4(0,0,0,0);
  179.                 half iterations = _Vertical * 3;
  180.  
  181.                 #define GFunc(x,d) (1/(sqrt(2*3.14*d*d))) * exp(-(x*x / (2*d*d)))
  182.  
  183.                 for (int j = -iterations / 2; j < iterations / 2; j++)
  184.                     sum += tex2D(_GrabTexture, float2(i.uvgrab.x, i.uvgrab.y + j * _Vertical / 500.0 / iterations)) * GFunc(j, _Vertical);
  185.  
  186.                 sum.a = tex2D(_MainTex, i.position).a;
  187.  
  188.                 return sum * _Color;
  189.             }
  190.            
  191.             ENDCG
  192.         }
  193.  
  194.         //Sprite pass
  195.         Pass {
  196.             CGPROGRAM
  197.             #pragma vertex vert
  198.             #pragma fragment frag
  199.  
  200.             #include "UnityCG.cginc"
  201.             #include "UnityUI.cginc"
  202.  
  203.             #pragma multi_compile __ UNITY_UI_ALPHACLIP
  204.  
  205.             struct appdata_t
  206.             {
  207.                 float4 vertex   : POSITION;
  208.                 float4 color    : COLOR;
  209.                 float2 texcoord : TEXCOORD0;
  210.             };
  211.  
  212.             struct v2f
  213.             {
  214.                 float4 vertex   : SV_POSITION;
  215.                 fixed4 color : COLOR;
  216.                 half2 texcoord  : TEXCOORD0;
  217.                 float4 worldPosition : TEXCOORD1;
  218.             };
  219.  
  220.             fixed4 _Color;
  221.             fixed4 _TextureSampleAdd;
  222.             float4 _ClipRect;
  223.  
  224.             v2f vert(appdata_t v) {
  225.                 v2f o;
  226.                 o.worldPosition = v.vertex;
  227.                 o.vertex = mul(UNITY_MATRIX_MVP, o.worldPosition);
  228.  
  229.                 o.texcoord = v.texcoord;
  230.  
  231. #ifdef UNITY_HALF_TEXEL_OFFSET
  232.                 o.vertex.xy += (_ScreenParams.zw - 1.0)*float2(-1,1);
  233. #endif
  234.  
  235.                 o.color = v.color * _Color;
  236.                 return o;
  237.             }
  238.  
  239.             sampler2D _MainTex;
  240.  
  241.             fixed4 frag(v2f i) : SV_Target{
  242.                 half4 color = (tex2D(_MainTex, i.texcoord) + _TextureSampleAdd) * i.color;
  243.                 color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
  244. #ifdef UNITY_UI_ALPHACLIP
  245.                 clip(color.a - 0.001);
  246. #endif
  247.                 return color;
  248.             }
  249.             ENDCG
  250.         }
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement