屏幕后處理效果
Bloom
Bloom特效是游戲中常見的一種屏幕效果酥郭。這種特效可以模擬真實攝像機的一種圖像效果衙传,它讓畫面中較亮的區(qū)域“擴散”到周圍的區(qū)域中,造成一種朦朧的效果。
大象之夢
一盖桥、Bloom的實現(xiàn)原理
1、首先根據(jù)一個閥值提取出圖像中較亮的區(qū)域题翻,把它們存儲在一張渲染紋理中
像素亮度閥值為0.5
可以看見只有較亮的地方不是黑色揩徊,其余地方全是黑色(0.0.0)
2、然后對這個渲染紋理進行高斯模糊用來模擬擴散效果
高斯模糊
上面的模糊不是很明顯嵌赠,因為像素采樣距離比較短
3塑荒、最后把這個渲染紋理和源圖像進行混合,得到最終結(jié)果
原圖像
Bloom
二姜挺、實現(xiàn)
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;
//提取較亮的區(qū)域
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
}