shader 均值模糊

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/y90o08u28/article/details/88175352

最近了解了一下告诉模糊效果,顺便写了一下。

junzhimohu1junzhimohu2

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,欢迎探讨研究。

猜你喜欢

转载自blog.csdn.net/y90o08u28/article/details/88175352