一个简单的屏幕模糊shader

如下:

Shader "TA/Unlit/ScreenBlur"
{
    Properties
    {
        [MainTexture]_MainTex ("Texture", 2D) = "white" {}
        _pixelOffset("_pixelOffset",Float) = 3
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
             HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            TEXTURE2D(_CameraColorTexture);
            SAMPLER(sampler_CameraColorTexture);
             
            uniform float4 _CameraColorTexture_TexelSize;
            half _pixelOffset;
            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 screenPos : TEXCOORD0;
      
                float4 vertex : SV_POSITION;
            };
             

            v2f vert (appdata v)
            {
                v2f o;
                float4 posCS = TransformObjectToHClip(v.vertex);
                o.vertex = posCS;
                o.screenPos = ComputeScreenPos(posCS);
 
                return o;
            }
            half4 KawaseBlur(TEXTURE2D_PARAM(tex, samplerTex), float2 uv, float2 texelSize, half pixelOffset)
            {
                half4 o = 0;
                o += SAMPLE_TEXTURE2D(tex, samplerTex, uv + float2(pixelOffset +0.5, pixelOffset +0.5) * texelSize); 
                o += SAMPLE_TEXTURE2D(tex, samplerTex, uv + float2(-pixelOffset -0.5, pixelOffset +0.5) * texelSize); 
                o += SAMPLE_TEXTURE2D(tex, samplerTex, uv + float2(-pixelOffset -0.5, -pixelOffset -0.5) * texelSize); 
                o += SAMPLE_TEXTURE2D(tex, samplerTex, uv + float2(pixelOffset +0.5, -pixelOffset -0.5) * texelSize); 
                return o * 0.25;
            }
            float4 frag (v2f i) : SV_Target
            {
                // sample the texture
                float2 screenUV = i.screenPos.xy/i.screenPos.w;
                //float4 screenColor = SAMPLE_TEXTURE2D(_CameraColorTexture,sampler_CameraColorTexture,screenUV);
                // blur
                float4 blur = KawaseBlur(_CameraColorTexture,sampler_CameraColorTexture,screenUV,_CameraColorTexture_TexelSize,_pixelOffset);
                return blur*.5;
            }
            ENDHLSL
        }
    }
}

 

posted @ 2025-03-19 17:11  太乙_真人  阅读(20)  评论(0)    收藏  举报