174 lines
5.3 KiB
GLSL
174 lines
5.3 KiB
GLSL
Shader "UI/UIGaussianBlur"
|
|
{
|
|
Properties
|
|
{
|
|
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
|
|
_Color ("Tint", Color) = (1,1,1,1)
|
|
_BlurRadius ("Blur Radius (Pixels)", Range(0, 64)) = 2
|
|
|
|
_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
|
|
|
|
[HideInInspector] _TextureSampleAdd ("Texture Sample Add", Color) = (0,0,0,0)
|
|
[HideInInspector] _MainTex_ST ("MainTex ST", Vector) = (1,1,0,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"
|
|
CGPROGRAM
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
#pragma target 3.0
|
|
|
|
#include "UnityCG.cginc"
|
|
#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;
|
|
UNITY_VERTEX_OUTPUT_STEREO
|
|
};
|
|
|
|
sampler2D _MainTex;
|
|
fixed4 _Color;
|
|
fixed4 _TextureSampleAdd;
|
|
float4 _ClipRect;
|
|
float4 _MainTex_ST;
|
|
float4 _MainTex_TexelSize;
|
|
float _UIMaskSoftnessX;
|
|
float _UIMaskSoftnessY;
|
|
float _BlurRadius;
|
|
|
|
v2f vert(appdata_t v)
|
|
{
|
|
v2f o;
|
|
UNITY_SETUP_INSTANCE_ID(v);
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
|
|
|
|
float4 vPosition = UnityObjectToClipPos(v.vertex);
|
|
o.worldPosition = v.vertex;
|
|
o.vertex = vPosition;
|
|
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
|
|
|
|
float2 pixelSize = vPosition.w;
|
|
pixelSize /= float2(1, 1) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
|
|
|
|
float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
|
|
o.mask = float4(
|
|
v.vertex.xy * 2 - clampedRect.xy - clampedRect.zw,
|
|
0.25 / (0.25 * half2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy))
|
|
);
|
|
|
|
o.color = v.color * _Color;
|
|
return o;
|
|
}
|
|
|
|
fixed4 SampleBlur(float2 uv)
|
|
{
|
|
float blur = max(_BlurRadius, 0.0);
|
|
if (blur <= 0.0001)
|
|
{
|
|
return tex2D(_MainTex, uv);
|
|
}
|
|
|
|
float2 texel = _MainTex_TexelSize.xy * blur;
|
|
const int sampleCount = 32;
|
|
const float goldenAngle = 2.39996323;
|
|
|
|
fixed4 c = tex2D(_MainTex, uv);
|
|
float weightSum = 1.0;
|
|
|
|
[unroll]
|
|
for (int i = 0; i < sampleCount; i++)
|
|
{
|
|
float fi = (float)i + 0.5;
|
|
float r = sqrt(fi / (float)sampleCount);
|
|
float a = fi * goldenAngle;
|
|
float2 dir = float2(cos(a), sin(a));
|
|
float2 offset = dir * r * texel;
|
|
float w = exp(-r * r * 3.5);
|
|
c += tex2D(_MainTex, uv + offset) * w;
|
|
weightSum += w;
|
|
}
|
|
|
|
return c / max(weightSum, 0.0001);
|
|
}
|
|
|
|
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;
|
|
|
|
float4 tex = SampleBlur(IN.texcoord.xy);
|
|
tex += _TextureSampleAdd;
|
|
|
|
fixed4 color = tex * IN.color;
|
|
|
|
#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;
|
|
}
|
|
ENDCG
|
|
}
|
|
}
|
|
}
|