Advertisement
dnnkeeper

Unity Blur shader

Feb 28th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/BlurredImage"
  2. {
  3.     Properties
  4.     {
  5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  6.         _Radius("Radius", Range(1, 255)) = 1
  7.     }
  8.  
  9.     Category
  10.     {
  11.         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
  12.      
  13.         Blend SrcAlpha OneMinusSrcAlpha
  14.  
  15.         SubShader
  16.         {
  17.             GrabPass
  18.             {
  19.                 Tags{ "LightMode" = "Always" }
  20.             }
  21.  
  22.             Pass
  23.             {
  24.                 Tags{ "LightMode" = "Always" }
  25.  
  26.                 CGPROGRAM
  27.                 #pragma vertex vert
  28.                 #pragma fragment frag
  29.                 #pragma fragmentoption ARB_precision_hint_fastest
  30.                 #include "UnityCG.cginc"
  31.  
  32.                 struct appdata_t
  33.                 {
  34.                     float4 vertex : POSITION;
  35.                     float2 texcoord: TEXCOORD0;
  36.                 };
  37.  
  38.                 struct v2f
  39.                 {
  40.                     float4 vertex : POSITION;
  41.                     float4 uvgrab : TEXCOORD0;
  42.                     float2 texcoord: TEXCOORD1;
  43.                 };
  44.  
  45.                 v2f vert(appdata_t v)
  46.                 {
  47.                     v2f o;
  48.                     o.vertex = UnityObjectToClipPos(v.vertex);
  49.                     #if UNITY_UV_STARTS_AT_TOP
  50.                     float scale = -1.0;
  51.                     #else
  52.                     float scale = 1.0;
  53.                     #endif
  54.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  55.                     o.uvgrab.zw = o.vertex.zw;
  56.                     o.texcoord = v.texcoord;
  57.                     return o;
  58.                 }
  59.  
  60.                 sampler2D _MainTex;
  61.                 sampler2D _GrabTexture;
  62.                 float4 _GrabTexture_TexelSize;
  63.                 float _Radius;
  64.  
  65.                 half4 frag(v2f i) : COLOR
  66.                 {
  67.                     half4 sum = half4(0,0,0,0);
  68.  
  69.                     #define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))
  70.  
  71.                     sum += GRABXYPIXEL(0.0, 0.0);
  72.                     int measurments = 1;
  73.  
  74.                     for (float range = 0.1f; range <= _Radius; range += 0.1f)
  75.                     {
  76.                         sum += GRABXYPIXEL(range, range);
  77.                         sum += GRABXYPIXEL(range, -range);
  78.                         sum += GRABXYPIXEL(-range, range);
  79.                         sum += GRABXYPIXEL(-range, -range);
  80.                         measurments += 4;
  81.                     }
  82.  
  83.                     half4 color = (sum / measurments);
  84.                     color.a = tex2D(_MainTex, i.texcoord).a;
  85.  
  86.                     return color;
  87.                 }
  88.                 ENDCG
  89.             }
  90.             GrabPass
  91.             {
  92.                 Tags{ "LightMode" = "Always" }
  93.             }
  94.  
  95.            
  96.  
  97.             Pass
  98.             {
  99.                 Tags{ "LightMode" = "Always" }
  100.  
  101.                 CGPROGRAM
  102.                 #pragma vertex vert
  103.                 #pragma fragment frag
  104.                 #pragma fragmentoption ARB_precision_hint_fastest
  105.                 #include "UnityCG.cginc"
  106.  
  107.                 struct appdata_t
  108.                 {
  109.                     float4 vertex : POSITION;
  110.                     float2 texcoord: TEXCOORD0;
  111.                 };
  112.  
  113.                 struct v2f
  114.                 {
  115.                     float4 vertex : POSITION;
  116.                     float4 uvgrab : TEXCOORD0;
  117.                     float2 texcoord: TEXCOORD1;
  118.                 };
  119.  
  120.                 v2f vert(appdata_t v)
  121.                 {
  122.                     v2f o;
  123.                     o.vertex = UnityObjectToClipPos(v.vertex);
  124.                     #if UNITY_UV_STARTS_AT_TOP
  125.                     float scale = -1.0;
  126.                     #else
  127.                     float scale = 1.0;
  128.                     #endif
  129.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  130.                     o.uvgrab.zw = o.vertex.zw;
  131.                     o.texcoord = v.texcoord;
  132.                     return o;
  133.                 }
  134.  
  135.                 sampler2D _MainTex;
  136.                 sampler2D _GrabTexture;
  137.                 float4 _GrabTexture_TexelSize;
  138.                 float _Radius;
  139.  
  140.                 half4 frag(v2f i) : COLOR
  141.                 {
  142.                     half4 sum = half4(0,0,0,0);
  143.                     float radius = 1.41421356237 * _Radius;
  144.  
  145.                     #define GRABXYPIXEL(kernelx, kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely, i.uvgrab.z, i.uvgrab.w)))
  146.  
  147.                     half4 originalSample = GRABXYPIXEL(0.0, 0.0);
  148.  
  149.                     sum += originalSample;
  150.                     int measurments = 1;
  151.  
  152.                     for (float range = 1.41421356237f; range <= radius * 1.41; range += 1.41421356237f)
  153.                     {
  154.                         sum += GRABXYPIXEL(range, 0);
  155.                         sum += GRABXYPIXEL(-range, 0);
  156.                         sum += GRABXYPIXEL(0, range);
  157.                         sum += GRABXYPIXEL(0, -range);
  158.                         measurments += 4;
  159.                     }
  160.                    
  161.                     half4 color = (sum / measurments);
  162.                     color.a = tex2D(_MainTex, i.texcoord).a;
  163.  
  164.                     return color;
  165.                 }
  166.                 ENDCG
  167.             }
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement