Unity Shader学习:动态模糊(shutter angle方式)

本文探讨了Unity中使用Shader实现动态模糊的方法,重点在于利用motion vector技术。通过对比关闭动态模糊的效果,阐述了动态模糊在物体高速旋转或运动时增加曝光时间效果的重要性。并提供了Keijiro的相关源码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unity Shader学习:动态模糊

动态模糊一般有帧混合和motion vector两种,这里主要介绍motion vector的方法。
Keijiro源码:https://github.com/keijiro/KinoMotion
当物体快速旋转或者运动时:
有曝光时间的效果
有曝光时间的效果

关闭动态模糊,虽然物体在高速旋转,没有曝光时间
关闭动态模糊,虽然物体在高速旋转,没有曝光时间

c#部分:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ImageEffectAllowedInSceneView]
public class ZzcMotionBlur : MonoBehaviour {
   
    public Material _material;

    [Range(0,100)]
    /// <summary>
    /// 动态模糊最大长度(值为屏幕高度的百分比)
    /// </summary>
    public float kMaxBlurRadius = 5f;

    [Range(1,360)]
    /// <summary>
    /// 曝光时间
    /// </summary>
    public float shutterAngle = 270f;

    [Range(1,32)]
    /// <summary>
    /// 采样数
    /// </summary>
    public int sampleCount = 8;

    // 速度图格式
    RenderTextureFormat _vectorRTFormat = RenderTextureFormat.RGHalf;

    // 速度、深度图格式
    RenderTextureFormat _packedRTFormat = RenderTextureFormat.ARGB2101010;

	void Update () {
   
        if (shutterAngle > 0)
            GetComponent<Camera>().depthTextureMode |=
                DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
    }

    //新建rt
    RenderTexture GetTemporaryRT(
                Texture source, int divider, RenderTextureFormat format
            )
    {
   
        var w = source.width / divider;
        var h = source.height / divider;
        var linear = RenderTextureReadWrite.Linear;
        var rt = RenderTexture.GetTemporary(w, h, 0, format, linear);
        rt.filterMode = FilterMode.Point;
        return rt;
    }

    void ReleaseTemporaryRT(RenderTexture rt)
    {
   
        RenderTexture.ReleaseTemporary(rt);
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
   
        if (!_material||shutterAngle == 0)
        {
   
            return;
        }

        // 最大模糊像素(屏幕百分比)
        var maxBlurPixels = (int)(kMaxBlurRadius * source.height * 0.01f);

        // 根据最大模糊像素降分辨率
        // 需要为8的倍数并大于maxBlurPixels
        var tileSize = ((maxBlurPixels - 1) / 8 + 1) * 8;

        // 1st pass - 获取像素速度和深度
        // 快门开角跟曝光速度的关系如下:
        // 开角 / 360度 = 曝光时间 / 画幅停顿时间------------(像素速度缩放)
        var velocityScale = shutterAngle / 360;
        _material.SetFloat("_VelocityScale", velocityScale);
        _material.SetFloat("_MaxBlurRadius", maxBlurPixels);
        _material.SetFloat("_RcpMaxBlurRadius", 1.0f / maxBlurPixels);

        var vbuffer = GetTemporaryRT(source, 1, _packedRTFormat);
        Graphics.Blit(null, vbuffer, _material, 0);

        // 2nd pass - 1/2 获取周围像素最大速度滤镜
        var tile2 = GetTemporaryRT(source, 2, _vectorRTFormat);
        Graphics.Blit(vbuffer, tile2, _material, 1);

        // 3rd pass - 1/4 获取周围像素最大速度滤镜
        var tile4 = GetTemporaryRT(source, 4, _vectorRTFormat);
        Graphics.Blit(tile2, tile4, _material, 2);
        ReleaseTemporaryRT(tile2);

        // 4th pass - 1/8 获取周围像素最大速度滤镜
        var tile8 = GetTemporaryRT
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值