均值模糊,也使用卷积来实现,之不过卷积中每个值均相等,且相加等于1。
代码如下,
子类:
using UnityEngine; public class MeanBlurRenderer : PostEffectRenderer
{
protected override string ShaderName
{
get { return "Custom/Mean Blur"; }
}
}
MeanBlurRenderer
shader
Shader "Custom/Mean Blur"
{
Properties
{
_MainTex("Main Texture", 2D) = "white" {}
} SubShader
{
Pass
{
Cull Off
ZWrite Off
ZTest Always CGPROGRAM
#pragma vertex vert
#pragma fragment frag sampler2D _MainTex;
float4 _MainTex_TexelSize; struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
}; struct v2f
{
float4 pos : SV_POSITION;
float2 uv[] : TEXCOORD0;
}; v2f vert(appdata v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv[] = v.uv + _MainTex_TexelSize.xy * float2(-, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * float2(, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * float2(, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * float2(-, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * float2(, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * float2(, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * float2(-, -);
o.uv[] = v.uv + _MainTex_TexelSize.xy * float2(, -);
o.uv[] = v.uv + _MainTex_TexelSize.xy * float2(, -);
return o;
} fixed4 frag(v2f i) : SV_TARGET
{
float4 col = float4(, , , );
for(int k = ; k < ; k++)
{
float4 tex = tex2D(_MainTex, i.uv[k]);
col += tex;
}
return col * 0.1111;
} ENDCG
}
} Fallback Off
}
效果,左边为正常,右边为均值模糊: