运动模糊后处理 - Shader

  • 脚本
using System;
using UnityEngine;
using UnityEngine.Serialization;

namespace DefaultNamespace
{
    
    
    public class MotionBlurWithDepthTexture : PostEffectBase
    {
    
    
        [SerializeField] private Shader _motionBlurShader;
        private Material _motionBlurMaterial;
        private Camera _camera;

        [Range(0.0f, 1.0f)] public float blurSize = 0.5f;
        private Matrix4x4 _previousViewProjectionMatrix;

        public Camera MyCamera
        {
    
    
            get
            {
    
    
                if (_camera == null)
                    _camera = GetComponent<Camera>();
                return _camera;
            }
        }

        public Material MotionBlurMaterial
        {
    
    
            get
            {
    
    
                _motionBlurMaterial = CheckShaderAndCreateMaterial(_motionBlurShader, _motionBlurMaterial);
                return _motionBlurMaterial;
            }
        }

        private void OnEnable()
        {
    
    
            MyCamera.depthTextureMode |= DepthTextureMode.Depth;
            _previousViewProjectionMatrix = MyCamera.projectionMatrix * MyCamera.worldToCameraMatrix;
        }
        

        private void OnRenderImage(RenderTexture src, RenderTexture dest)
        {
    
    
            if (MotionBlurMaterial != null)
            {
    
    
                MotionBlurMaterial.SetFloat("_BlurSize", blurSize);
                
                MotionBlurMaterial.SetMatrix("_PreviousViewProjectionMatrix", _previousViewProjectionMatrix);
                var currentViewProjectionMatrix = MyCamera.projectionMatrix * MyCamera.worldToCameraMatrix;
                var currentViewProjectionInverseMatrix = currentViewProjectionMatrix.inverse;
                MotionBlurMaterial.SetMatrix("_CurrentViewProjectionInverseMatrix", currentViewProjectionInverseMatrix);
                _previousViewProjectionMatrix = currentViewProjectionMatrix;
                
                Graphics.Blit(src, dest, MotionBlurMaterial);
            }
            else
            {
    
    
                Graphics.Blit(src, dest);
            }
        }
    }
}
  • shader
Shader "Hidden/MotionBlurWithDepthTextrue"
{
    
    
    Properties
    {
    
    
        _MainTex ("Texture", 2D) = "white" {
    
    }
        _BlurSize ("Blur Amount", Float) = 1.0
    }
    SubShader
    {
    
    
        CGINCLUDE

        #include <UnityCG.cginc>

        sampler2D _MainTex;
        half4 _MainTex_TexelSize;
        sampler2D _CameraDepthTexture;
        float4x4 _CurrentViewProjectionInverseMatrix;
        float4x4 _PreviousViewProjectionMatrix;
        float _BlurSize;

        struct a2v
        {
    
    
            float4 vertex : POSITION;
            float2 texcoord : TEXCOORD0;
        };

        struct v2f
        {
    
    
            float4 pos : SV_POSITION;
            float2 uv : TEXCOORD0;
            float2 uv_depth : TEXCOORD1;
        };

        v2f vert(a2v v)
        {
    
    
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);

            o.uv = v.texcoord.xy;
            o.uv_depth = v.texcoord.xy;

            #if UNITY_UV_STARTS_AT_TOP
            if (_MainTex_TexelSize.y < 0)
                o.uv_depth.y = 1 - o.uv_depth.y;
            #endif

            return o;
        }

        fixed4 frag(v2f i) : SV_Target
        {
    
    
            float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv_depth);
            float4 H = float4(i.uv.x * 2 - 1, i.uv.y * 2 - 1, d * 2 - 1, 1);
            float4 D = mul(_CurrentViewProjectionInverseMatrix, H);
            float4 worldPos = D / D.w;

            float4 currentPos = H;
            float4 previousPos = mul(_PreviousViewProjectionMatrix, worldPos);
            previousPos /= previousPos.w;

            float2 velocity = (currentPos.xy - previousPos.xy) / 2;

            float2 uv = i.uv;
            float4 color = tex2D(_MainTex, uv);
            uv += velocity * _BlurSize;
            for (int it = 1; it < 3; it++)
            {
    
    
                float4 currentColor = tex2D(_MainTex, uv);
                color += currentColor;

                uv += velocity * _BlurSize;
            }

            color /= 3; 

            return fixed4(color.rgb, 1.0);
        }
        
        ENDCG
        
        Pass
        {
    
    
            Cull Off ZWrite Off ZTest Always
            
            CGPROGRAM
            
            #pragma vertex vert
            #pragma fragment frag
            
            ENDCG
        }
    }
    
    FallBack off
}

猜你喜欢

转载自blog.csdn.net/m0_52361859/article/details/128268411
今日推荐