|
高斯模糊效果实现依然使用卷积操作,使用的卷积核为高斯核。具体计算流程书中解释如下:
这里为了提高性能,我们将一个5x5的高斯核拆分为两个长度为5的一维高斯核,从而降低采样次数已达到提升性能的目的。
挂载在相机上的后处理脚本实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GaussianBlur : PostEffectsBase
{
public Shader GaussianBlurShader;
[Range(0,4)]
public int _Iterations = 3; //模糊迭代(越大越模糊)
[Range(0.2f, 3.0f)]
public float _BlurSpread = 0.6f;
[Range(1,8)]
public int _DownSample = 2;
private Material _GaussianBlurMaterial;
public Material material
{
get
{
_GaussianBlurMaterial = CheckShaderAndCreateMaterial(GaussianBlurShader, _GaussianBlurMaterial);
return _GaussianBlurMaterial;
}
}
////方案一(使用临时缓冲区存储过渡纹理)
// private void OnRenderImage(RenderTexture src, RenderTexture dest)
// {
// if (material != null)
// {
// int rtW = src.width;
// int rtH = src.height;
// RenderTexture buffer = RenderTexture.GetTemporary(rtW, rtH, 0);
//
// //渲染垂直方向的Pass
// Graphics.Blit(src,buffer,material,0);
// //渲染水平方向的Pass
// Graphics.Blit(src,dest,material,1);
//
// RenderTexture.ReleaseTemporary(buffer);
// }
// else
// {
// Graphics.Blit(src,dest);
// }
// }
////方案二(在方案一的基础上,利用缩放对图像进行降采样,从而减少处理的像素个数提高性能)
// private void OnRenderImage(RenderTexture src, RenderTexture dest)
// {
// if (material != null)
// {
// int rtW = src.width/_DownSample;
// int rtH = src.height/_DownSample;
// RenderTexture buffer = RenderTexture.GetTemporary(rtW, rtH, 0);
// buffer.filterMode = FilterMode.Bilinear;
// //渲染垂直方向的Pass
// Graphics.Blit(src,buffer,material,0);
// //渲染水平方向的Pass
// Graphics.Blit(src,dest,material,1);
//
// RenderTexture.ReleaseTemporary(buffer);
// }
// else
// {
// Graphics.Blit(src,dest);
// }
// }
////方案三(在方案二的基础上,增加了高斯模糊的迭代次数以得到更好的效果)
private void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (material != null)
{
int rtW = src.width/_DownSample;
int rtH = src.height/_DownSample;
RenderTexture buffer0 = RenderTexture.GetTemporary(rtW, rtH, 0);
buffer0.filterMode = FilterMode.Bilinear;
Graphics.Blit(src,buffer0);
for (int i = 0; i < _Iterations; i++)
{
material.SetFloat(&#34;_BlurSize&#34;,1.0f + i * _BlurSpread);
RenderTexture buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);
//渲染垂直方向的Pass
Graphics.Blit(src,buffer1,material,0);
RenderTexture.ReleaseTemporary(buffer0);
buffer0 = buffer1;
buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);
//渲染水平方向的Pass
Graphics.Blit(src,buffer1,material,1);
RenderTexture.ReleaseTemporary(buffer0);
buffer0 = buffer1;
}
Graphics.Blit(buffer0,dest);
RenderTexture.ReleaseTemporary(buffer0);
}
else
{
Graphics.Blit(src,dest);
}
}
}
用来渲染后处理效果的Shader实现如下:
Shader &#34;Unlit/GaussianBlur&#34;
{
Properties
{
_MainTex (&#34;Texture&#34;, 2D) = &#34;white&#34; {}
_BlurSize (&#34;Blur Size&#34;, Float) = 1.0
}
SubShader
{
CGINCLUDE
#include &#34;UnityCG.cginc&#34;
sampler2D _MainTex;
float4 _MainTex_TexelSize;
float _BlurSize;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
half2 uv[5] : TEXCOORD0;
};
//垂直方向的顶点着色器
v2f vertBlurVertical(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.uv;
o.uv[0] = uv;
o.uv[1] = uv + float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSize;
o.uv[2] = uv + float2(0.0, _MainTex_TexelSize.y * -1.0) * _BlurSize;
o.uv[3] = uv + float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSize;
o.uv[4] = uv + float2(0.0, _MainTex_TexelSize.y * -2.0) * _BlurSize;
return o;
}
//水平方向的顶点着色器
v2f vertBlurHorizontal(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.uv;
o.uv[0] = uv;
o.uv[1] = uv + float2( _MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;
o.uv[2] = uv + float2( _MainTex_TexelSize.x * -1.0, 0.0) * _BlurSize;
o.uv[3] = uv + float2( _MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;
o.uv[4] = uv + float2( _MainTex_TexelSize.x * -2.0, 0.0) * _BlurSize;
return o;
}
fixed4 fragBlur(v2f i) : SV_Target
{
//{0.0545, 0.2442, 0.4026, 0.2442, 0.0545} 高斯核 ,写成下边这样以提升性能
float weight[3] = {0.4026, 0.2442, 0.0545};
//根据高斯核计算五个点的采样之和
fixed3 sum = tex2D(_MainTex,i.uv[0]).rgb * weight[0];
for(int it = 1; it < 3; it++)
{
sum += tex2D(_MainTex,i.uv[it]).rgb * weight[it];
sum += tex2D(_MainTex,i.uv[it*2]).rgb * weight[it];
}
return fixed4(sum,1.0);
}
ENDCG
//后处理标配
ZWrite Off
ZTest Always
Cull Off
Pass
{
NAME &#34;GAUSSIAN_BLUR_VERTICAL&#34;
CGPROGRAM
#pragma vertex vertBlurVertical
#pragma fragment fragBlur
ENDCG
}
Pass
{
NAME &#34;GAUSSIAN_BLUR_HORIZONTAL&#34;
CGPROGRAM
#pragma vertex vertBlurHorizontal
#pragma fragment fragBlur
ENDCG
}
}
Fallback Off
}
渲染结果如下:
此篇文章是在学习《Unity Shader入门精要》一书的笔记,加入了一些个人理解,如有错误之处,还请各位不吝赐教。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|