屏幕后处理-高斯模糊
using UnityEngine;

public class Mohua : MonoBehaviour {

    public Material mat;

    [Range(0, 4)]
    public int iterations = 3; //处理次数,越多次越模糊

    [Range(0.2f, 3.0f)]
    public float blurSpread = 0.6f;  //模糊程度

    [Range(1, 8)]
    public int downSample = 2; //取样缩放

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (mat != null)
        {
            int rtW = source.width / downSample;
            int rtH = source.height / downSample;

            RenderTexture buffer0 = RenderTexture.GetTemporary(rtW, rtH, 0);
            buffer0.filterMode = FilterMode.Bilinear;

            Graphics.Blit(source, buffer0);

            for (int i = 0; i < iterations; i++)//处理的次数
            {
                mat.SetFloat("_BlurSize", 1.0f + i * blurSpread);
                RenderTexture buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);
                Graphics.Blit(buffer0, buffer1, mat, 0);//使用第一个pass做垂直方向的处理

                RenderTexture.ReleaseTemporary(buffer0);

                buffer0 = buffer1;
                buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);
                Graphics.Blit(buffer0, buffer1, mat, 1); //使用第二个pass做水平方向的处理

                RenderTexture.ReleaseTemporary(buffer0);
                buffer0 = buffer1;
            }

            Graphics.Blit(buffer0, destination);//显示处理后的图像
            RenderTexture.ReleaseTemporary(buffer0);
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }
}


Shader "Unlit/gsMohu"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_BlurSize ("Blur Size",Float) = 1.0
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		CGINCLUDE //声明一块公用的
			
		#include "UnityCG.cginc"

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

		sampler2D _MainTex;
		half4 _MainTex_TexelSize;
		float _BlurSize;
			
		v2f vertBlurVertical (appdata_img v)
		{
			v2f o;
			o.vertex = UnityObjectToClipPos(v.vertex);
			half2 uv = v.texcoord;

			//竖直方向的5个点取样,_BlurSize越大,取样的间距越大,模糊就越大
			o.uv[0] = uv;
			o.uv[1] = uv + float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSize;
			o.uv[2] = uv - float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSize;
			o.uv[3] = uv + float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSize;
			o.uv[4] = uv - float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSize;

			return o;
		}

		v2f vertBlurHorizontal(appdata_img v)
		{
			v2f o;
			o.vertex = UnityObjectToClipPos(v.vertex);
			half2 uv = v.texcoord;

			//水平方向的5个点取样
			o.uv[0] = uv;
			o.uv[1] = uv + float2(0.0, _MainTex_TexelSize.x * 1.0) * _BlurSize;
			o.uv[2] = uv - float2(0.0, _MainTex_TexelSize.x * 1.0) * _BlurSize;
			o.uv[3] = uv + float2(0.0, _MainTex_TexelSize.x * 2.0) * _BlurSize;
			o.uv[4] = uv - float2(0.0, _MainTex_TexelSize.x * 2.0) * _BlurSize;
				
			return o;
		}
			
		fixed4 frag(v2f i) : SV_Target
		{
			float weight[3] = {0.4026,0.2442,0.0545};  //根据高斯方程计算处理啊的,标准方差为1,每个值相加为1
			fixed3 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 fixed4(sum, 1.0);
		}
		ENDCG

		ZTest Always Cull Off ZWrite Off

		Pass
		{
			NAME "GAUSSING_BLUR_VERTICAL" //为Pass命名,方便在其他SubShader中重用
			CGPROGRAM
			#pragma vertex vertBlurVertical
			#pragma fragment frag

			ENDCG
		}

		Pass
		{
			NAME "GAUSSING_BLUR_GORIZONTAL"
			CGPROGRAM
			#pragma vertex vertBlurHorizontal
			#pragma fragment frag
			ENDCG
		}
	}
	FallBack "Diffuse"
}



高斯方程


blob.png

blob.png


单通道 单独处理单个方向 传offset参数

Shader "PostEffect/SeparableGlassBlur" {
   Properties {
      _MainTex ("Base (RGB)", 2D) = "" {}
   }

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

   struct appdata_img {
      float4 vertex:POSITION;
      float2 texcoord:TEXCOORD0;
   };
   struct v2f {
      float4 pos : POSITION;
      float2 uv : TEXCOORD0;

      float4 uv01 : TEXCOORD1;
      float4 uv23 : TEXCOORD2;
      float4 uv45 : TEXCOORD3;
   };
   
   float4 offsets;
   
   sampler2D _MainTex;
   
   v2f vert (appdata_img v) {
      v2f o;
      VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
      o.pos = vertexInput.positionCS;
      o.uv.xy = v.texcoord.xy;

      o.uv01 =  v.texcoord.xyxy + offsets.xyxy * float4(1,1, -1,-1);
      o.uv23 =  v.texcoord.xyxy + offsets.xyxy * float4(1,1, -1,-1) * 2.0;
      o.uv45 =  v.texcoord.xyxy + offsets.xyxy * float4(1,1, -1,-1) * 3.0;
      return o;
   }
   
   half4 frag (v2f i) : COLOR {
      half4 color = float4 (0,0,0,0);

      color += 0.40 * tex2D (_MainTex, i.uv);
      color += 0.15 * tex2D (_MainTex, i.uv01.xy);
      color += 0.15 * tex2D (_MainTex, i.uv01.zw);
      color += 0.10 * tex2D (_MainTex, i.uv23.xy);
      color += 0.10 * tex2D (_MainTex, i.uv23.zw);
      color += 0.05 * tex2D (_MainTex, i.uv45.xy);
      color += 0.05 * tex2D (_MainTex, i.uv45.zw);
      
      return color;
   }

   ENDHLSL
   
Subshader {
 Pass {
     ZTest Always Cull Off ZWrite Off

      HLSLPROGRAM
      #pragma vertex vert
      #pragma fragment frag
      ENDHLSL
  }
}

}



首页 我的博客
粤ICP备17103704号