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