张掖网站制作/百度搜索排名与点击有关吗
0x00 序言
本节是在 高斯模糊的基础上进行的,稍加修改即可得到Bloom效果。
0x01 效果图
0x02 基本概念
首先根据一个阈值提取出图像中较亮的区域,把它们存储在一张渲染纹理中,再利用高斯模糊对这张渲染纹理进行模糊处理,模拟光线扩散的效果,最后再将其和原图像进行混合,得到最终的结果。
0x03 脚本代码
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, 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;//step1.提取较亮的区域,存储在buffer0Graphics.Blit(src, buffer0, material, 0);//step2.进行高斯模糊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;}//step3.把buffer0传递给材质中的_Bloom纹理属性material.SetTexture("_Bloom", buffer0);Graphics.Blit(src, dest, material, 3);RenderTexture.ReleaseTemporary(buffer0);}else{Graphics.Blit(src, dest);}}}
0x04 Shader代码
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'Shader "Bloom"{Properties{_MainTex("Base (RGB)", 2D) = "white" {}_BlurSize("Blur Size", Float) = 1.0_Bloom ("Bloom (RGB)", 2D) = "black" {}_LuminanceThreshold ("Luminance Threshold", Float) = 0.5}SubShader{CGINCLUDE#include "UnityCG.cginc"sampler2D _MainTex;half4 _MainTex_TexelSize;float _BlurSize;sampler2D _Bloom;float _LuminanceThreshold;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;}// 将采样得到的亮度值减去阈值,并把结果clamp到0~1范围内,然后把该值和原像素值相乘,得到提亮后的亮部区域。fixed4 fragExtractBright(v2f i) : SV_Target{fixed4 c = tex2D(_MainTex, i.uv);fixed val = clamp(luminance(c) - _LuminanceThreshold, 0.0, 1.0);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_TOPif (_MainTex_TexelSize.y < 0.0)o.uv.w = 1.0 - o.uv.w;#endifreturn o;}fixed4 fragBloom(v2fBloom i) : SV_Target{return tex2D(_MainTex, i.uv.xy) + tex2D(_Bloom, i.uv.zw);}ENDCGZTest Always Cull Off ZWrite OffPass{CGPROGRAM#pragma vertex vertExtractBright#pragma fragment fragExtractBrightENDCG}UsePass "Gaussian Blur/GAUSSIAN_BLUR_VERTICAL"UsePass "Gaussian Blur/GAUSSIAN_BLUR_HORIZONTAL"Pass{CGPROGRAM#pragma vertex vertBloom#pragma fragment fragBloomENDCG}}FallBack Off
}