Skip to content

Instantly share code, notes, and snippets.

@baraujo
Created July 20, 2017 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baraujo/9c6b02374aea28a14831b0689b835c82 to your computer and use it in GitHub Desktop.
Save baraujo/9c6b02374aea28a14831b0689b835c82 to your computer and use it in GitHub Desktop.
// Unity shader for blur image effect
// Based on https://www.youtube.com/watch?v=kpBnIAPtsj8 and http://danjohnmoran.com/Shaders/102/SimpleBoxBlur.shader
// Add on a material and use the blur radius slider to increase/decrease the effect
Shader "Custom/BlurImageEffect"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BlurRadius("Blur radius", Range(0, 5)) = 1
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float4 _MainTex_TexelSize;
int _BlurRadius;
fixed4 frag (v2f i) : SV_Target
{
float4 size = _MainTex_TexelSize;
float4 col = float4(0, 0, 0, 0);
int fragCount = 0;
for(int x = -_BlurRadius; x <= _BlurRadius; x++){
for(int y = -_BlurRadius; y <= _BlurRadius; y++){
col += tex2D(_MainTex, i.uv + float2(size.x * x, size.y * y));
fragCount++;
}
}
return col / fragCount;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment