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 = mul(UNITY_MATRIX_MVP, 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);
// 亮度值-阈值(得到的值在0~1之间)
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 = mul (UNITY_MATRIX_MVP, 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
// 1.提取高亮区域
Pass {
CGPROGRAM
#pragma vertex vertExtractBright
#pragma fragment fragExtractBright
ENDCG
}
// 2.纵向滤波
UsePass "高斯模糊中定义的pass/GAUSSIAN_BLUR_VERTICAL"
// 3.横向滤波
UsePass "高斯模糊中定义的pass/GAUSSIAN_BLUR_HORIZONTAL"
// 4.将高亮图+原图像 进行混合
Pass {
CGPROGRAM
#pragma vertex vertBloom
#pragma fragment fragBloom
ENDCG
}
}
FallBack Off
}
【Shader】通过高斯模糊实现Bloom效果
最新推荐文章于 2024-06-13 02:51:51 发布