屏幕后处理效果
Bloom
Bloom特效是游戏中常见的一种屏幕效果。这种特效可以模拟真实摄像机的一种图像效果,它让画面中较亮的区域“扩散”到周围的区域中,造成一种朦胧的效果。
一、Bloom的实现原理
1、首先根据一个阀值提取出图像中较亮的区域,把它们存储在一张渲染纹理中
可以看见只有较亮的地方不是黑色,其余地方全是黑色(0.0.0)
2、然后对这个渲染纹理进行高斯模糊用来模拟扩散效果
上面的模糊不是很明显,因为像素采样距离比较短
3、最后把这个渲染纹理和源图像进行混合,得到最终结果
二、实现
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bloom : PostEffectsBase
{
public Shader bloomShader;
private Material bloomMaterial = null;
public Material material
{
get
{
bloomMaterial = CheckShaderAndCreateMaterial(bloomShader, bloomMaterial);
return bloomMaterial;
}
}
[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, 1.0f)] public float luminanceThreshold = 0.6f;
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (material != null)
{
material.SetFloat("_LuminanceThreshold",luminanceThreshold);
int rW = src.height / downSample;
int rH = src.width / downSample;
RenderTexture buffer0 = RenderTexture.GetTemporary(rW, rH, 0);
buffer0.filterMode = FilterMode.Bilinear;
//提取较亮的区域
Graphics.Blit(src,buffer0,material,0);
//Graphics.Blit(buffer0,dest);
//进行高斯模糊
for (int i = 0; i < iterations; i++)
{
material.SetFloat("_BlurSize",1.0f+i*blurSpread);
RenderTexture buffer1 = RenderTexture.GetTemporary(rW,rH,0);
Graphics.Blit(buffer0,buffer1,material,1);
RenderTexture.ReleaseTemporary(buffer0);
buffer0 = buffer1;
buffer1 = RenderTexture.GetTemporary(rW, rH, 0);
Graphics.Blit(buffer0,buffer1,material,2);
buffer0 = buffer1;
}
//进行Bloom效果
material.SetTexture("_Bloom",buffer0);
Graphics.Blit(src,dest,material,3);
RenderTexture.ReleaseTemporary(buffer0);
}
else
{
Graphics.Blit(src,dest);
}
}
}
Shader "Unlit/Bloom"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Bloom("Bloom Texture",2D)="black"{}
_LuminanceThreshold("Luminance Threshold",float)=0.5
_BlurSize("BlurSize",float) = 1.0
}
SubShader
{
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
half4 _MainTex_TexelSize;
sampler2D _Bloom;
float _LuminanceThreshold;
float _BlurSize;
struct v2f
{
float4 pos:SV_POSITION;
half2 uv:TEXCOORD0;
};
fixed luminance(fixed4 color)
{
return 0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b;
}
v2f vertExtraBright(appdata_img v)
{
v2f o;
o.pos = UnityViewToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
}
fixed4 fragExtraBright(v2f i):SV_TARGET
{
fixed4 texColor = tex2D(_MainTex,i.uv);
fixed luminanceVal = luminance(texColor);
fixed val = clamp(luminanceVal - _LuminanceThreshold,0,1);
//return fixed4(val,val,val,1);
return texColor * val;
}
v2f vertBloom(appdata_img v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
}
fixed4 fragBloom(v2f i):SV_TARGET
{
return tex2D(_MainTex,i.uv) + tex2D(_Bloom,i.uv);
}
ENDCG
ZTest Always Cull Off ZWrite Off
Pass
{
NAME "EXTRABRIGHT"
CGPROGRAM
#pragma vertex vertExtraBright
#pragma fragment fragExtraBright
ENDCG
}
UsePass "Unlit/GaussionBlur/GAUSSION_BLUR_VERTICAL"
UsePass "Unlit/GaussionBlur/GAUSSION_BLUR_HORZONTAL"
Pass
{
NAME "BLOOM"
CGPROGRAM
#pragma vertex vertBloom
#pragma fragment fragBloom
ENDCG
}
}
Fallback Off
}