【Unity Shader入门精要学习】高级(五)

2019-11-04  本文已影响0人  小王子称号发放NPC

屏幕后处理效果

运动模糊

运动模糊是真实世界中摄像机的一种效果。如果摄像机曝光时,拍摄场景发生了变化,就会产生模糊的画面。运动模糊效果可以让物体运动起来更加真实平滑,但在计算机产生的图像中,由于不存在曝光这一物理现象,渲染出来的图像往往棱角分明缺少运动模糊。

一、运动模糊的实现方法

1、累计缓存(accumulation buffer),使用一块累计缓存来混合多张连续的图像,当物体快速运动产生多张图像后,取它们之间的平均值作为最后的运动模糊图像。这种方式比较暴力。
2、速度缓存(velocity buffer),这个缓存中存储了各个像素当前的运动速度,然后利用该值来决定模糊的方向和大小。
本节实现使用的是类似于第1种方式,但是不需要多次渲染,只需要保存之前的渲染结果,不断的把当前的渲染图像叠加到之前的渲染图像中,从而产生一种运动轨迹的视觉效果。

二、实现

原图像
运动后
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MotionBlur : PostEffectsBase
{
    public Shader motionBlurShader;
    private Material motionBlurMaterial = null;

    public Material material {  
        get {
            motionBlurMaterial = CheckShaderAndCreateMaterial(motionBlurShader, motionBlurMaterial);
            return motionBlurMaterial;
        }  
    }

    [Range(0.0f, 0.9f)]
    public float blurAmount = 0.5f;
    
    private RenderTexture accumulationTexture;

    void OnDisable() {
        DestroyImmediate(accumulationTexture);
    }

    void OnRenderImage (RenderTexture src, RenderTexture dest) {
        if (material != null) {
            if (accumulationTexture == null || 
                accumulationTexture.width != src.width || 
                accumulationTexture.height != src.height) {
                
                DestroyImmediate(accumulationTexture);
                accumulationTexture = new RenderTexture(src.width, src.height, 0);
                accumulationTexture.hideFlags = HideFlags.HideAndDontSave;
                Graphics.Blit(src, accumulationTexture);
                
            }
            //不要将上次混合后的渲染结果清空,accumulationTexture保存了之前的混合结果
            //恢复操作发生在渲染到纹理而该纹理有没有被提前清空或销毁的情况下。
            accumulationTexture.MarkRestoreExpected();

            material.SetFloat("_BlurAmount", blurAmount);
            Graphics.Blit (src, accumulationTexture, material);
            Graphics.Blit (accumulationTexture, dest);
        } else {
            Graphics.Blit(src, dest);
        }
    }
}
Shader "Unlit/MotionBlur"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurAmount("BlurAmount",float)=1
    }
    SubShader
    {
        CGINCLUDE
        #include "UnityCG.cginc"

        sampler2D _MainTex;
        fixed _BlurAmount;

        struct v2f
        {
            float4 pos:SV_POSITION;
            half2 uv:TEXCOORD0;
        };

        v2f vert(appdata_img v)
        {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);
            o.uv = v.texcoord;
            return o;
        }

        fixed4 fragRGB(v2f i):SV_TARGET
        {
            return fixed4(tex2D(_MainTex,i.uv).rgb,_BlurAmount);
        }

        fixed4 fragA(v2f i):SV_TARGET
        {
            return fixed4(tex2D(_MainTex,i.uv));
        }

        ENDCG

        ZTest Always Cull Off ZWrite off
        Pass
        {
            //第一步是将当前图像的RGB与之前图像(也就是accumulationTexture)进行混合
            //当图像运动起来,两张图片的同一位置有不同的颜色,就会产生混合的效果,从而看起来变模糊了
            //当图像不运动时,同一位置的颜色相同,混合起来也是一样的颜色
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMask RGB
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment fragRGB
            ENDCG 
        }

        Pass
        {
            //保持当前图像的Alpha值不变,因为只需要在第一个Pass用混合操作,不能影响写入dest的Alpha值
            Blend One Zero
            ColorMask A
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment fragA
            ENDCG
        }
    }
    Fallback Off
}
上一篇下一篇

猜你喜欢

热点阅读