From 5aff2b480aea781f222ab46f63ea04d94b81b5af Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Wed, 24 Jun 2026 12:02:42 +0900 Subject: [PATCH] fix: `UI/Additive` shader does not support RectMask2D softness. --- Shaders/UIAdditive.shader | 53 +++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/Shaders/UIAdditive.shader b/Shaders/UIAdditive.shader index 9910dcf..3f7b735 100644 --- a/Shaders/UIAdditive.shader +++ b/Shaders/UIAdditive.shader @@ -40,12 +40,8 @@ Lighting Off ZWrite Off ZTest [unity_GUIZTestMode] - Fog - { - Mode Off - } + Fog { Mode Off } Blend One One - ColorMask [_ColorMask] Pass @@ -76,6 +72,7 @@ fixed4 color : COLOR; float2 texcoord : TEXCOORD0; float4 worldPosition : TEXCOORD1; + float4 mask : TEXCOORD2; UNITY_VERTEX_OUTPUT_STEREO }; @@ -84,16 +81,43 @@ float4 _MainTex_ST; fixed4 _TextureSampleAdd; float4 _ClipRect; + float _UIMaskSoftnessX; + float _UIMaskSoftnessY; + int _UIVertexColorAlwaysGammaSpace; + + half3 _UIGammaToLinear(half3 value) + { + half3 low = 0.0849710 * value - 0.000163029; + half3 high = value * (value * (value * 0.265885 + 0.736584) - 0.00980184) + 0.00319697; + + // We should be 0.5 away from any actual gamma value stored in an 8 bit channel + const half3 split = (half3)0.0725490; // Equals 18.5 / 255 + return (value < split) ? low : high; + } v2f vert(appdata_t v) { v2f OUT; UNITY_SETUP_INSTANCE_ID(v); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT); + float4 vPosition = UnityObjectToClipPos(v.vertex); OUT.worldPosition = v.vertex; - OUT.vertex = UnityObjectToClipPos(OUT.worldPosition); + OUT.vertex = vPosition; - OUT.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); + float2 maskUV = (v.vertex.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy); + OUT.texcoord = TRANSFORM_TEX(v.texcoord.xy, _MainTex); + OUT.mask = float4(v.vertex.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy))); + + if (_UIVertexColorAlwaysGammaSpace) + { + #ifndef UNITY_COLORSPACE_GAMMA + v.color.rgb = _UIGammaToLinear(v.color.rgb); + #endif + } OUT.color = v.color * _Color; return OUT; @@ -101,17 +125,26 @@ fixed4 frag(v2f IN) : SV_Target { - half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color; + //Round up the alpha color coming from the interpolator (to 1.0/256.0 steps) + //The incoming alpha could have numerical instability, which makes it very sensible to + //HDR color transparency blend, when it blends with the world's texture. + const half alphaPrecision = half(0xff); + const half invAlphaPrecision = half(1.0 / alphaPrecision); + IN.color.a = round(IN.color.a * alphaPrecision) * invAlphaPrecision; + + half4 color = IN.color * (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd); #ifdef UNITY_UI_CLIP_RECT - color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect); + 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); + clip(color.a - 0.001); #endif color.rgb *= color.a; + return color; } ENDCG