diff --git a/UiRoundedCorners/IndependentRoundedCorners.shader b/UiRoundedCorners/IndependentRoundedCorners.shader index 761f018..30c65d7 100644 --- a/UiRoundedCorners/IndependentRoundedCorners.shader +++ b/UiRoundedCorners/IndependentRoundedCorners.shader @@ -59,39 +59,34 @@ float4 _r; float4 _halfSize; float4 _rect2props; - float4 _OuterUV; + half4 _OuterUV; sampler2D _MainTex; float4 _ClipRect; fixed4 _TextureSampleAdd; fixed4 frag (v2f i) : SV_Target { - float2 uvSample = i.uv; - uvSample.x = (uvSample.x - _OuterUV.x) / (_OuterUV.z - _OuterUV.x); - uvSample.y = (uvSample.y - _OuterUV.y) / (_OuterUV.w - _OuterUV.y); - - half4 color = (tex2D(_MainTex, i.uv) + _TextureSampleAdd) * i.color; + half4 textureColor = tex2D(_MainTex, i.uv); + half4 color = (textureColor + _TextureSampleAdd) * i.color; + // Apply standard UI clipping first #ifdef UNITY_UI_CLIP_RECT color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect); #endif - #ifdef UNITY_UI_ALPHACLIP - clip(color.a - 0.001); - #endif + half2 uvSample = (i.uv - _OuterUV.xy) / (_OuterUV.zw - _OuterUV.xy); - if (color.a <= 0) { - return color; - } + // Calculate the rounded corner alpha using an SDF for independent corners + half sdfAlpha = CalcAlphaForIndependentCorners(uvSample, _halfSize.xy, _rect2props, _r); - float alpha = CalcAlphaForIndependentCorners(uvSample, _halfSize.xy, _rect2props, _r); + // Combine the procedural alpha with the texture's alpha + color.a *= sdfAlpha; #ifdef UNITY_UI_ALPHACLIP - clip(alpha - 0.001); + clip(color.a - 0.001); #endif - return mixAlpha(tex2D(_MainTex, i.uv), i.color, alpha); - } - + return color; + } ENDCG } } diff --git a/UiRoundedCorners/RoundedCorners.shader b/UiRoundedCorners/RoundedCorners.shader index cb0e920..e00d29c 100644 --- a/UiRoundedCorners/RoundedCorners.shader +++ b/UiRoundedCorners/RoundedCorners.shader @@ -55,39 +55,36 @@ Shader "UI/RoundedCorners/RoundedCorners" { #pragma multi_compile_local _ UNITY_UI_ALPHACLIP float4 _WidthHeightRadius; - float4 _OuterUV; + half4 _OuterUV; sampler2D _MainTex; fixed4 _TextureSampleAdd; float4 _ClipRect; fixed4 frag (v2f i) : SV_Target { - float2 uvSample = i.uv; - uvSample.x = (uvSample.x - _OuterUV.x) / (_OuterUV.z - _OuterUV.x); - uvSample.y = (uvSample.y - _OuterUV.y) / (_OuterUV.w - _OuterUV.y); - - half4 color = (tex2D(_MainTex, i.uv) + _TextureSampleAdd) * i.color; + half4 textureColor = tex2D(_MainTex, i.uv); + half4 color = (textureColor + _TextureSampleAdd) * i.color; + // Apply standard UI clipping first #ifdef UNITY_UI_CLIP_RECT color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect); #endif - #ifdef UNITY_UI_ALPHACLIP - clip(color.a - 0.001); - #endif + // Remap UVs for the SDF calculation + half2 uvSample = i.uv; + uvSample = (uvSample - _OuterUV.xy) / (_OuterUV.zw - _OuterUV.xy); - if (color.a <= 0) { - return color; - } + // Calculate the rounded corner alpha using a Signed Distance Field + half sdfAlpha = CalcAlpha(uvSample, _WidthHeightRadius.xy, _WidthHeightRadius.z); - float alpha = CalcAlpha(uvSample, _WidthHeightRadius.xy, _WidthHeightRadius.z); + // Combine the procedural alpha with the texture's alpha + color.a *= sdfAlpha; #ifdef UNITY_UI_ALPHACLIP - clip(alpha - 0.001); + clip(color.a - 0.001); #endif - return mixAlpha(tex2D(_MainTex, i.uv), i.color, alpha); + return color; } - ENDCG } }