【Unity Shader入門精要學(xué)習(xí)】高級(四)

屏幕后處理效果

Bloom

Bloom特效是游戲中常見的一種屏幕效果。這種特效可以模擬真實攝像機(jī)的一種圖像效果,它讓畫面中較亮的區(qū)域“擴(kuò)散”到周圍的區(qū)域中,造成一種朦朧的效果。


大象之夢

一、Bloom的實現(xiàn)原理

1、首先根據(jù)一個閥值提取出圖像中較亮的區(qū)域,把它們存儲在一張渲染紋理中

像素亮度閥值為0.5

可以看見只有較亮的地方不是黑色,其余地方全是黑色(0.0.0)
2、然后對這個渲染紋理進(jìn)行高斯模糊用來模擬擴(kuò)散效果
高斯模糊

上面的模糊不是很明顯,因為像素采樣距離比較短
3、最后把這個渲染紋理和源圖像進(jìn)行混合,得到最終結(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);
            
            //進(jìn)行高斯模糊
            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;
            }
            //進(jìn)行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
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容