hch
5 天以前 9d6170fdbfaf314f58e09212e00044c2c7144405
0312 优化嵌套RectMask2D无法裁剪问题
2个文件已修改
31 ■■■■ 已修改文件
Shader/GUI_EllipseMask.shader 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Shader/GUI_EllipseMaskedContent.shader 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Shader/GUI_EllipseMask.shader
@@ -94,15 +94,20 @@
                float ellipseValue = dx * dx + dy * dy;
                
                // 软边缘处理 - 调整椭圆边界
                float adjustedEllipseValue = ellipseValue;
                float alpha = 1.0;
                if (_Softness > 0)
                {
                    // 使用smoothstep创建软边缘过渡
                    adjustedEllipseValue = smoothstep(1.0 - _Softness, 1.0 + _Softness, ellipseValue);
                    alpha = 1.0 - smoothstep(1.0 - _Softness, 1.0 + _Softness, ellipseValue);
                }
                else
                {
                    // 硬边缘:椭圆内为1,椭圆外为0
                    alpha = ellipseValue <= 1.0 ? 1.0 : 0.0;
                }
                
                // 如果不在椭圆内,则丢弃像素
                clip(1.0 - adjustedEllipseValue - 0.01);
                // 如果alpha为0,则丢弃像素(用于模板测试)
                clip(alpha - 0.01);
                return fixed4(0, 0, 0, 0);
            }
            ENDCG
Shader/GUI_EllipseMaskedContent.shader
@@ -5,6 +5,11 @@
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        
        [Header(Ellipse Settings)]
        _EllipseCenter ("Ellipse Center", Vector) = (0.5, 0.5, 0, 0)
        _EllipseRadius ("Ellipse Radius", Vector) = (0.5, 0.5, 0, 0)
        _Softness ("Softness", Range(0, 0.5)) = 0.1
        [Header(Stencil Settings)]
        _StencilComp ("Stencil Comparison", Float) = 3  // Equal
        _Stencil ("Stencil ID", Float) = 1
@@ -49,6 +54,7 @@
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #include "UnityUI.cginc"
            
            struct appdata_t
            {
@@ -62,11 +68,14 @@
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
                float4 worldPosition : TEXCOORD1;
            };
            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            float4 _ClipRect;
            
            v2f vert(appdata_t v)
            {
@@ -74,12 +83,21 @@
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.color = v.color * _Color;
                o.worldPosition = v.vertex;
                return o;
            }
            
            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 color = tex2D(_MainTex, i.texcoord) * i.color;
                // 应用RectMask2D裁剪
                color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
                // 确保alpha通道正确,以便与RectMask2D正确交互
                // 如果颜色完全透明,则丢弃像素,避免影响RectMask2D的裁剪
                clip(color.a - 0.001);
                return color;
            }
            ENDCG