Unity Shader屏幕后处理 Bloom效果

本文介绍了Unity中使用Shader实现Bloom屏幕后处理技术的原理和步骤,包括提取图像亮点、高斯模糊处理及与原图像混合,通过4个Pass通道完成光线扩散效果。
摘要由CSDN通过智能技术生成

原理:

首先根据一个阈值提取出图像中较亮的区域,把他们存储在一张渲染纹理中,再利用高斯模糊对这张渲染纹理进行模糊处理,模拟光线扩散效果,最后将其和原图像进行混合,得到最终的结果

脚本实现:

我们要使用4个Pass通道
第一个Pass通道提取较亮的区域,存储起来
第二个Pass通道进行垂直模糊
第三个Pass通道进行水平模糊
第四个Pass通道进行混合

public class Bloom : PostEffectsBase
{

    public Shader bloomShader;
    private Material bloomMaterial = null;
    public Material material
    {
        get
        {
            bloomMaterial = CheckShaderAndCreateMaterial(bloomShader, bloomMaterial);
            return bloomMaterial;
        }
    }


    [Range(0, 10)]
    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;//控制提取较亮区域时使用的阈值

    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (material != null)
        {
            material.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, material, 0);
            //Graphics.Blit使用shader中的第一个Pass提取图像中的较亮区域,然后存储在buffer0中

            for (int i = 0; i < iterations; i++) //对较亮区域进行高斯模糊迭代处理
            {
                material.SetFloat("_BlurSize", 1.0f + i * blurSpread);

                RenderTexture buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);

                Graphics.Blit(buffer0, buffer1, material, 1);//垂直模糊

                RenderTexture.ReleaseTemporary(buffer0);
                buffer0 = buffer1;
                buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);


                Graphics.Blit(buffer0, buffer1, material, 2);//水平模糊

                RenderTexture.ReleaseTemporary(buffer0);
                buffer0 = buffer1;
            }

            material.SetTexture("_Bloom", buffer0);
            //把buffer0传递给材质中的_Bloom纹理属性
            Graphics.Blit(src, dest, material, 3);//混合
            //调用Graphics.Blit使用第四个Pass进行最后的混合,将结果存储在目标渲染纹理dest中

            RenderTexture.ReleaseTemporary(buffer0);//释放临时缓存
        }
        else
        {
            Graphics.Blit(src, dest);
        }
    }
}

Shader实现如下:

Shader "Bloom" {
	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"
		
		sampler2D _MainTex;
		half4 _MainTex_TexelSize;
		sampler2D _Bloom;
		float _LuminanceThreshold;
		float _BlurSize;
		
		struct v2f {
			float4 pos : SV_POSITION; 
			half2 uv : TEXCOORD0;
		};	
		
		v2f vertExtractBright(appdata_img v) {
			v2f o;
			
			o.pos = UnityObjectToClipPos(v.vertex);
			
			o.uv = v.texcoord;
					 
			return o;
		}
		
		fixed luminance(fixed4 color) {
			return  0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b; 
		}
		
		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;
		};
		
		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 {  
			CGPROGRAM  
			#pragma vertex vertExtractBright  
			#pragma fragment fragExtractBright  
			
			ENDCG  
		}
		
		UsePass "Gaussian Blur/GAUSSIAN_BLUR_VERTICAL"
		
		UsePass "Gaussian Blur/GAUSSIAN_BLUR_HORIZONTAL"
		
		Pass {  
			CGPROGRAM  
			#pragma vertex vertBloom  
			#pragma fragment fragBloom  
			
			ENDCG  
		}
	}
	FallBack Off
}

效果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值