Files
bansonic_beta_main/Assets/Shaders/UI/BansonicUIBlurBehind.shader
T
2026-06-30 21:21:30 +08:00

200 lines
6.9 KiB
Plaintext

Shader "UI/Bansonic/Blur Behind"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_BlurRadius ("Blur Radius", Range(0,4)) = 1.2
_BlurSpread ("Blur Spread", Range(0.25,3)) = 1
_BackgroundOpacity ("Background Opacity", Range(0,1)) = 1
_TintStrength ("Tint Strength", Range(0,1)) = 0
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
Comp [_StencilComp]
Pass [_StencilOp]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend One OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
Name "Default"
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#ifndef fixed
#define fixed half
#endif
#ifndef fixed2
#define fixed2 half2
#endif
#ifndef fixed3
#define fixed3 half3
#endif
#ifndef fixed4
#define fixed4 half4
#endif
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
float4 mask : TEXCOORD2;
float4 screenPosition : TEXCOORD3;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
TEXTURE2D_X(_BansonicUIBlurSourceTex);
SAMPLER(sampler_BansonicUIBlurSourceTex);
fixed4 _Color;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
float4 _MainTex_ST;
float4 _BansonicUIBlurSourceTex_TexelSize;
float _UIMaskSoftnessX;
float _UIMaskSoftnessY;
float _BlurRadius;
float _BlurSpread;
float _BackgroundOpacity;
float _TintStrength;
v2f vert(appdata_t v)
{
v2f OUT;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
OUT.worldPosition = v.vertex;
OUT.vertex = TransformObjectToHClip(v.vertex.xyz);
OUT.screenPosition = OUT.vertex;
OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
float2 pixelSize = OUT.vertex.w;
pixelSize /= float2(1, 1) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
OUT.mask = float4(
v.vertex.xy * 2 - clampedRect.xy - clampedRect.zw,
0.25 / (0.25 * half2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy))
);
OUT.color = v.color * _Color;
return OUT;
}
half4 SampleBlurredBackground(float2 screenUV, float blurRadius)
{
screenUV = UnityStereoTransformScreenSpaceTex(screenUV);
screenUV = saturate(screenUV);
float2 texel = _BansonicUIBlurSourceTex_TexelSize.xy * blurRadius * _BlurSpread;
float2 clampMin = _BansonicUIBlurSourceTex_TexelSize.xy * 0.5;
float2 clampMax = 1.0 - clampMin;
#define SAMPLE_BG(uv) SAMPLE_TEXTURE2D_X(_BansonicUIBlurSourceTex, sampler_BansonicUIBlurSourceTex, clamp((uv), clampMin, clampMax))
half4 sum = SAMPLE_BG(screenUV) * 4.0h;
sum += SAMPLE_BG(screenUV + float2( texel.x, 0)) * 2.0h;
sum += SAMPLE_BG(screenUV + float2(-texel.x, 0)) * 2.0h;
sum += SAMPLE_BG(screenUV + float2(0, texel.y)) * 2.0h;
sum += SAMPLE_BG(screenUV + float2(0, -texel.y)) * 2.0h;
sum += SAMPLE_BG(screenUV + float2( texel.x, texel.y));
sum += SAMPLE_BG(screenUV + float2(-texel.x, texel.y));
sum += SAMPLE_BG(screenUV + float2( texel.x, -texel.y));
sum += SAMPLE_BG(screenUV + float2(-texel.x, -texel.y));
float2 texel2 = texel * 2.0;
sum += SAMPLE_BG(screenUV + float2( texel2.x, 0));
sum += SAMPLE_BG(screenUV + float2(-texel2.x, 0));
sum += SAMPLE_BG(screenUV + float2(0, texel2.y));
sum += SAMPLE_BG(screenUV + float2(0, -texel2.y));
#undef SAMPLE_BG
return sum / 20.0h;
}
fixed4 frag(v2f IN) : SV_Target
{
const half alphaPrecision = half(0xff);
const half invAlphaPrecision = half(1.0 / alphaPrecision);
IN.color.a = round(IN.color.a * alphaPrecision) * invAlphaPrecision;
fixed4 spriteSample = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
float2 screenUV = GetNormalizedScreenSpaceUV(IN.vertex);
half4 blurred = SampleBlurredBackground(screenUV, max(_BlurRadius, 0.0001));
half3 tint = lerp(half3(1, 1, 1), saturate(spriteSample.rgb), _TintStrength);
half4 color;
color.rgb = blurred.rgb * tint;
color.a = spriteSample.a * _BackgroundOpacity;
#ifdef UNITY_UI_CLIP_RECT
half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
color.a *= m.x * m.y;
#endif
#ifdef UNITY_UI_ALPHACLIP
clip(color.a - 0.001);
#endif
color.rgb *= color.a;
return color;
}
ENDHLSL
}
}
Fallback Off
}