Skip to content

Instantly share code, notes, and snippets.

@ParkingLotGames
Created March 23, 2023 20:36
Show Gist options
  • Save ParkingLotGames/f96e6d3edf8fd4b13373bc07328b81e5 to your computer and use it in GitHub Desktop.
Save ParkingLotGames/f96e6d3edf8fd4b13373bc07328b81e5 to your computer and use it in GitHub Desktop.
Shader "BlurShader" {
Properties{
_MainTex("Texture", 2D) = "white" {}
_BlurAmount("Blur Amount", Range(0, 5)) = 1
[Toggle]_HALF_TEXEL_OFFSET("Use Half Texel Offset", Float) = 0
}
SubShader{
Tags { "RenderType" = "Opaque" }
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _HALF_TEXEL_OFFSET_ON
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float4 _MainTex_ST;
sampler2D _MainTex;
float _BlurAmount;
v2f vert(appdata v) {
v2f o = (v2f)0;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
#if _HALF_TEXEL_OFFSET_ON
o.uv.zw = o.uv + 0.5 * float2(1.0 / _ScreenParams.x, 1.0 / _ScreenParams.y);
#endif
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 col = fixed4(0, 0, 0, 0);
float blurSize = _BlurAmount / _ScreenParams.y;
#if _HALF_TEXEL_OFFSET_ON
float weight = 0.04;
#else
float weight = 0.1111111;
#endif
float2 offsets[4] = { float2(0, 1), float2(1, 0), float2(1, 1), float2(1, -1) }
float2 inverseOffset = float2(i.uv.z, i.uv.w - 1 * (1 / _ScreenParams.x));
for (int j = 0; j < 4; j++) {
float2 offset = offsets[j] * blurSize;
#if _HALF_TEXEL_OFFSET_ON
col += tex2D(_MainTex, i.uv.zw + offset) * weight;
col += tex2D(_MainTex, i.uv.zw - offset) * weight;
col += tex2D(_MainTex, inverseOffset + offset) * weight;
col += tex2D(_MainTex, inverseOffset - offset) * weight;
#endif
col += tex2D(_MainTex, i.uv.xy + offset) * weight;
col += tex2D(_MainTex, i.uv.xy - offset) * weight;
}
col += tex2D(_MainTex, i.uv.xy) * weight;
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment