参考书籍:《Unity Shader 入门精要》
实现原理:先根据一个阈值获取图像中较亮的区域,存到一张纹理,再用高斯模糊渲染这个纹理,最后混合原图即可。
Bloom类:和高斯模糊类似,只是处理的不是整个屏幕,而是较亮区域。
using UnityEngine;
public class Bloom : PostEffectsBase
{
[Range(0, 4)]
public int iterations = 3; // 模糊迭代次数
[Range(0.2f, 3.0f)]
public float blurSpread = 0.6f; // 模糊跨度
[Range(1, 8)]
public int downSample = 2; // 模糊大小
[Range(0.0f, 4.0f)]
public float luminanceThreshold = 0.6f; // 亮度阈值(一般不超过1,开了HDR可以存更高精度)
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (TargetMaterial != null)
{
TargetMaterial.SetFloat("_LuminanceThreshold", luminanceThreshold);
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, TargetMaterial, 0); // 第一个Pass把较亮区域存到buffer0
// 迭代高斯模糊
for (int i = 0; i < iterations; i++)
{
TargetMaterial.SetFloat("_BlurSize", 1.0f + i * blurSpread);
RenderTexture buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);
Graphics.Blit(buffer0, buffer1, TargetMaterial, 1); // 第二个Pass,高斯模糊
RenderTexture.ReleaseTemporary(buffer0);
buffer0 = buffer1;
buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);
Graphics.Blit(buffer0, buffer1, TargetMaterial, 2); // 第三个Pass
RenderTexture.ReleaseTemporary(buffer0);
buffer0 = buffer1;
}
TargetMaterial.SetTexture("_Bloom", buffer0); // 较亮区域存到_Bloom
Graphics.Blit(src, dest, TargetMaterial, 3); // 第四个Pass最后混合
RenderTexture.ReleaseTemporary(buffer0);
}
else
Graphics.Blit(src, dest);
}
}
Shader:
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Bloom ("Bloom (RGB)", 2D) = "black" {}
_LuminanceThreshold ("Luminance Threshold", Float) = 0.5 // 亮度阈值
_BlurSize ("Blur Size", Float) = 1.0
}
SubShader {
CGINCLUDE
#include "UnityCG.cginc"
...
v2f vertExtractBright(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
}
...
fixed4 fragExtractBright(v2f i) : SV_Target {
fixed4 c = tex2D(_MainTex, i.uv); // 原像素值
fixed val = clamp(luminance(c) - _LuminanceThreshold, 0.0, 1.0); // 亮度减去阈值,再截取到0~1
return c * val; // 得到提取后的亮部区域
}
// --- 上面是提取较亮部分 --- 下面是融合原图和较亮部分 --- //
struct v2fBloom {
float4 pos : SV_POSITION;
half4 uv : TEXCOORD0; // uv.xy:原图像的纹理坐标。uv.zw:_Bloom,较亮区域纹理坐标
};
v2fBloom vertBloom(appdata_img v) {
v2fBloom o;
o.pos = UnityObjectToClipPos (v.vertex);
o.uv.xy = v.texcoord;
o.uv.zw = v.texcoord;
#if UNITY_UV_STARTS_AT_TOP // 起始坐标在上面的话,就反过来
if (_MainTex_TexelSize.y < 0.0)
o.uv.w = 1.0 - o.uv.w;
#endif
return o;
}
fixed4 fragBloom(v2fBloom i) : SV_Target {
return tex2D(_MainTex, i.uv.xy) + tex2D(_Bloom, i.uv.zw);
}
ENDCG
ZTest Always Cull Off ZWrite Off
// 第一个Pass:提取出较亮部分
Pass {
CGPROGRAM
#pragma vertex vertExtractBright
#pragma fragment fragExtractBright
ENDCG
}
// 第二个Pass:对较亮部分做高斯模糊,直接调用前面定义的Pass
UsePass "Custom/Chapter 12/Gaussian Blur/GAUSSIAN_BLUR_VERTICAL"
// 第三个Pass:同上。高斯模糊的第二个Pass
UsePass "Custom/Chapter 12/Gaussian Blur/GAUSSIAN_BLUR_HORIZONTAL"
// 第四个Pass:融合原图和经高斯模糊后的较亮部分
Pass {
CGPROGRAM
#pragma vertex vertBloom
#pragma fragment fragBloom
ENDCG
}
}