hch
2025-09-09 e6e30e457af97c4a2cd6319a7009d4836d5dc3f9
Shader/MaskedFlowEffect.shader
@@ -16,10 +16,18 @@
    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" }
        Tags {
            "Queue"="Transparent"
            "RenderType"="Transparent"
            "IgnoreProjector"="True"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }
        AlphaToMask On
        LOD 100
        Blend One OneMinusSrcAlpha
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off
        Cull Off
        Pass
        {
@@ -27,17 +35,23 @@
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #include "UnityUI.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR;
            };
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                fixed4 color : COLOR;
                //Add for RectMask2D
                float4 worldPosition : TEXCOORD4;
                //End for RectMask2D
            };
            sampler2D _MainTex;
@@ -48,12 +62,17 @@
        float _FlowThickness;
            float _FlowWidth;
            float4 _FlowDirection;
            float4 _ClipRect;
            v2f vert (appdata v)
            {
                v2f o;
                //Add for RectMask2D
                o.worldPosition = v.vertex;
                //End for RectMask2D
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.color = v.color;
                return o;
            }
@@ -62,6 +81,10 @@
                fixed4 base = tex2D(_MainTex, i.uv);
                fixed mask = tex2D(_MaskTex, i.uv).a;
                
                // 透明部分不显示流光
                if (base.a <= 0)
                    return fixed4(0, 0, 0, 0);
                float2 dir = normalize(_FlowDirection.xy);
                float flowPos = dot(i.uv, dir) / _FlowSpacing;
                float flow = (sin(_Time.y * _FlowSpeed + flowPos * 10) * 0.5 + 0.5) * _FlowThickness * 2;
@@ -69,6 +92,15 @@
                
                fixed4 final = base;
                final.rgb = lerp(base.rgb, _FlowColor.rgb, flowMask * _FlowColor.a);
                //final.a = base.a; // 保留原始透明度
                final.a = base.a * flowMask; // 非流光部分透明度为0,流光部分保留原始透明度
                //Add for RectMask2D
                final.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
#ifdef UNITY_UI_ALPHACLIP
                clip(final.a - 0.001);
#endif
                //End for RectMask2D
                return final;
            }
            ENDCG