Shader "Hide/FullBlur" { Properties { _MainTex("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" } LOD 100 HLSLINCLUDE #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" sampler2D _MainTex; half4 _MainTex_TexelSize; float _BlurSizeX; float _BlurSizeY; //URP下没有appdata_img结构 要自定义 struct a2v { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; half2 uv[5] : TEXCOORD0; }; //我们只需要appdata_img内置结构的数据传入即可(有顶点、纹理坐标) v2f vertBlurVertical(a2v v) { v2f o; //o.pos = UnityObjectToClipPos(v.vertex); //CG VertexPositionInputs vertexInputs = GetVertexPositionInputs(v.vertex.xyz); o.pos = vertexInputs.positionCS; half2 uv = v.texcoord; //纵向的5个像素点纹理坐标 o.uv[0] = uv; o.uv[1] = uv + float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSizeY; o.uv[2] = uv - float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSizeY; o.uv[3] = uv + float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSizeY; o.uv[4] = uv - float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSizeY; return o; } v2f vertBlurHorizontal(a2v v) { v2f o; //o.pos = UnityObjectToClipPos(v.vertex); //CG VertexPositionInputs vertexInputs = GetVertexPositionInputs(v.vertex.xyz); o.pos = vertexInputs.positionCS; half2 uv = v.texcoord; //横向的5个像素点纹理坐标 o.uv[0] = uv; o.uv[1] = uv + float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSizeX; o.uv[2] = uv - float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSizeX; o.uv[3] = uv + float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSizeX; o.uv[4] = uv - float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSizeX; return o; } //无论是纵向还是横向,它们都会使用这个片元着色器,处理手法一样 half4 fragBlur(v2f i) : SV_Target{ float weight[3] = {0.4026, 0.2442, 0.0545}; //采样RGB然后进行乘以对应的权重累加到sum half3 sum = tex2D(_MainTex, i.uv[0]).rgb * weight[0]; for (int it = 1; it < 3; it++) { sum += tex2D(_MainTex, i.uv[it * 2 - 1]).rgb * weight[it]; sum += tex2D(_MainTex, i.uv[it * 2]).rgb * weight[it]; } //是的这样就完成了,模糊。。。。 return half4(sum, 1.0); } ENDHLSL //上面都是INCLUDE内容即下面Pass都可使用的内容 //标配写法 ZTest Always Cull Off ZWrite Off //第一个PASS,纵向模糊处理 Pass { NAME "GAUSSIAN_BLUR_VERTICAL" //CGPROGRAM //CG HLSLPROGRAM //纵向的顶点着色器 #pragma vertex vertBlurVertical //片元着色器 #pragma fragment fragBlur //ENDCG //CG ENDHLSL } //第二个Pass 横向模糊处理 Pass { NAME "GAUSSIAN_BLUR_HORIZONTAL" //CGPROGRAM HLSLPROGRAM //横向的顶点着色器 #pragma vertex vertBlurHorizontal //片元着色器 #pragma fragment fragBlur //ENDCG ENDHLSL } }//完成SubShader Fallback Off }