Skip to content

Instantly share code, notes, and snippets.

@dogles
Created March 20, 2014 20:22
Show Gist options
  • Save dogles/9672991 to your computer and use it in GitHub Desktop.
Save dogles/9672991 to your computer and use it in GitHub Desktop.
Shader "Hidden/FastBlur" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Bloom ("Bloom (RGB)", 2D) = "black" {}
}
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _Bloom;
uniform half4 _MainTex_TexelSize;
uniform half4 _Parameter;
struct v2f_tap
{
float4 pos : SV_POSITION;
half2 uv20 : TEXCOORD0;
half2 uv21 : TEXCOORD1;
half2 uv22 : TEXCOORD2;
half2 uv23 : TEXCOORD3;
};
v2f_tap vert4Tap ( appdata_img v )
{
v2f_tap o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv20 = v.texcoord + _MainTex_TexelSize.xy;
o.uv21 = v.texcoord + _MainTex_TexelSize.xy * half2(-0.5h,-0.5h);
o.uv22 = v.texcoord + _MainTex_TexelSize.xy * half2(0.5h,-0.5h);
o.uv23 = v.texcoord + _MainTex_TexelSize.xy * half2(-0.5h,0.5h);
return o;
}
fixed4 fragDownsample ( v2f_tap i ) : COLOR
{
fixed4 color = tex2D (_MainTex, i.uv20);
color += tex2D (_MainTex, i.uv21);
color += tex2D (_MainTex, i.uv22);
color += tex2D (_MainTex, i.uv23);
return color / 4;
}
// weight curves
static const half curve[7] = { 0.0205, 0.0855, 0.232, 0.324, 0.232, 0.0855, 0.0205 }; // gauss'ish blur weights
static const half4 curve4[7] = { half4(0.0205,0.0205,0.0205,0), half4(0.0855,0.0855,0.0855,0), half4(0.232,0.232,0.232,0),
half4(0.324,0.324,0.324,1), half4(0.232,0.232,0.232,0), half4(0.0855,0.0855,0.0855,0), half4(0.0205,0.0205,0.0205,0) };
struct v2f_withBlurCoords8
{
float4 pos : SV_POSITION;
half4 uv : TEXCOORD0;
half2 offs : TEXCOORD1;
};
struct v2f_withBlurCoordsSGX
{
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
half2 offs[6] : TEXCOORD1;
};
v2f_withBlurCoords8 vertBlurHorizontal (appdata_img v)
{
v2f_withBlurCoords8 o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = half4(v.texcoord.xy,1,1);
o.offs = _MainTex_TexelSize.xy * half2(1.0, 0.0) * _Parameter.x;
return o;
}
v2f_withBlurCoords8 vertBlurVertical (appdata_img v)
{
v2f_withBlurCoords8 o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = half4(v.texcoord.xy,1,1);
o.offs = _MainTex_TexelSize.xy * half2(0.0, 1.0) * _Parameter.x;
return o;
}
half4 fragBlur8 ( v2f_withBlurCoords8 i ) : COLOR
{
half2 uv = i.uv.xy;
half2 netFilterWidth = i.offs;
half2 coords = uv - netFilterWidth * 3.0;
half4 color = 0;
for( int l = 0; l < 7; l++ )
{
half4 tap = tex2D(_MainTex, coords);
color += tap * curve4[l];
coords += netFilterWidth;
}
return color;
}
v2f_withBlurCoordsSGX vertBlurHorizontalSGX (appdata_img v)
{
v2f_withBlurCoordsSGX o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
half2 netFilterWidth = _MainTex_TexelSize.xy * half2(1.0, 0.0) * _Parameter.x;
half2 coords = -netFilterWidth.xy * 3.0;
o.offs[0] = v.texcoord.xy + coords * half2(1.0h,1.0h);
o.offs[1] = v.texcoord.xy + coords * half2(-1.0h,-1.0h);
coords += netFilterWidth;
o.offs[2] = v.texcoord.xy + coords * half2(1.0h,1.0h);
o.offs[3] = v.texcoord.xy + coords * half2(-1.0h,-1.0h);
coords += netFilterWidth;
o.offs[4] = v.texcoord.xy + coords * half2(1.0h,1.0h);
o.offs[5] = v.texcoord.xy + coords * half2(-1.0h,-1.0h);
return o;
}
v2f_withBlurCoordsSGX vertBlurVerticalSGX (appdata_img v)
{
v2f_withBlurCoordsSGX o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
half2 netFilterWidth = _MainTex_TexelSize.xy * half2(0.0, 1.0) * _Parameter.x;
half2 coords = -netFilterWidth.xy * 3.0;
o.offs[0] = v.texcoord.xy + coords * half2(1.0h,1.0h);
o.offs[1] = v.texcoord.xy + coords * half2(-1.0h,-1.0h);
coords += netFilterWidth;
o.offs[2] = v.texcoord.xy + coords * half2(1.0h,1.0h);
o.offs[3] = v.texcoord.xy + coords * half2(-1.0h,-1.0h);
coords += netFilterWidth;
o.offs[4] = v.texcoord.xy + coords * half2(1.0h,1.0h);
o.offs[5] = v.texcoord.xy + coords * half2(-1.0h,-1.0h);
return o;
}
half4 fragBlurSGX ( v2f_withBlurCoordsSGX i ) : COLOR
{
half4 color = tex2D(_MainTex, i.uv) * curve4[3];
half4 tapA_1 = tex2D(_MainTex, i.offs[0]);
half4 tapB_1 = tex2D(_MainTex, i.offs[1]);
half4 tapA_2 = tex2D(_MainTex, i.offs[2]);
half4 tapB_2 = tex2D(_MainTex, i.offs[3]);
half4 tapA_3 = tex2D(_MainTex, i.offs[4]);
half4 tapB_3 = tex2D(_MainTex, i.offs[5]);
color += (tapA_1 + tapB_1) * curve4[0];
color += (tapA_2 + tapB_2) * curve4[1];
color += (tapA_3 + tapB_3) * curve4[2];
return color;
}
ENDCG
SubShader {
ZTest Off Cull Off ZWrite Off Blend Off
Fog { Mode off }
// 0
Pass {
CGPROGRAM
#pragma vertex vert4Tap
#pragma fragment fragDownsample
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
// 1
Pass {
CGPROGRAM
#pragma vertex vertBlurVertical
#pragma fragment fragBlur8
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
// 2
Pass {
CGPROGRAM
#pragma vertex vertBlurHorizontal
#pragma fragment fragBlur8
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
// alternate blur
// 3
Pass {
CGPROGRAM
#pragma vertex vertBlurVerticalSGX
#pragma fragment fragBlurSGX
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
// 4
Pass {
CGPROGRAM
#pragma vertex vertBlurHorizontalSGX
#pragma fragment fragBlurSGX
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
}
FallBack Off
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment