运动模糊后处理 - Shader

本文介绍了一种在Unity中实现运动模糊效果的方法,通过使用深度纹理和后处理技术,能够根据场景的运动情况生成平滑的模糊效果。该技术特别适用于提高游戏画面的真实感。
  • 脚本
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
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值