0
点赞
收藏
分享

微信扫一扫

Unity Shader学习笔记(二)偏导数实现图片模糊

其生 2022-04-23 阅读 169
效果图展示:

原图
模糊后效果

直接贴代码:
Shader "ShaderLearn/ddxddyApply"
{
    Properties
    {
        _MainTex("MainTex",2D) = ""{}
        _BlurLength("BlurLength",Range(0,3)) = 1
    }

    SubShader
    {
        //URP渲染管线切换 Tags;
        //Tags{"LightMode"="UniversalForward" "rendertype"="opaque" "Queue"="Geometry"}
        Tags{"rendertype"="opaque" "Queue"="Geometry"}

        pass
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _BlurLength;

            //struct appdata_full
            //{
            //    float4 vertex : POSITION;
            //    float2 uv : TEXCOORD0;
            //};

            struct v2f
            {
              float4 vertex : SV_POSITION;
              float2 uv : TEXCOORD0;
            };

            v2f vert (appdata_base v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_TARGET
            {
                float2 uv = i.uv;
                
                float dx = ddx(uv.x) * _BlurLength;
                float dy = ddy(uv.y) * _BlurLength;

                fixed4 col = tex2D(_MainTex,uv,float2(dx,dx),float2(dy,dy));
                return col;
            }

            ENDCG
        }
    }
}
举报

相关推荐

0 条评论