最近了解了一下模糊效果,顺便写了一下,下面是均值模糊算法。
Shader "Unlit/Average"
{
Properties
{
_MainTex("Main Texture", 2D) = "white" {}
_BlurSpread("Blur Spread", Range(1,20)) = 2
}
SubShader
{
CGINCLUDE
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float _BlurSpread;
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 = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_TARGET
{
fixed4 col = fixed4(0,0,0,0);
float a = tex2D(_MainTex, i.uv).a ;
float bx = i.uv.x - _BlurSpread*0.01 / 2;
float by = i.uv.y - _BlurSpread * 0.01 / 2;
bx = saturate(bx);
by = saturate(by);
for (int k = 0; k < _BlurSpread; k++)
{
for (int j = 0; j < _BlurSpread; j++)
{
col += tex2D(_MainTex, fixed2(saturate(bx + k * 0.01), saturate(by + j * 0.01)));
}
}
col = col / (_BlurSpread) / (_BlurSpread);
return fixed4(col.rgb,a);
}
ENDCG
Pass
{
Name "HORIZONTAL"
ZTest Always
ZWrite Off
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback Off
}
本人qq:344810449,欢迎探讨研究。
有unity,shader,小程序等需求也可以联系本人,非常乐于助人。
如果觉得还不错给博主来个小惊喜,纯属自愿,不强求:
作者:unity游侠
链接:https://blog.csdn.net/y90o08u28/article/details/88175352
来源:CSDN
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。