Unity 支持UGUI Mask遮罩的PhotoShop混合模式特效Shader

这篇博客详细解析了Unity Shader中的BlendOperations,介绍了如何实现不同的透明度混合模式,如柔和叠加、正片叠底、滤色等,并展示了如何使用Stencil和AlphaScale属性进行更精细的控制。
摘要由CSDN通过智能技术生成

shader代码:

Shader "/BlendShader/Blend Operations" {
    Properties{
        _Color("Color Tint", Color) = (1, 1, 1, 1)
        _MainTex("Main Tex", 2D) = "white" {}
        _AlphaScale("Alpha Scale", Range(0, 1)) = 1

        //MASK SUPPORT ADD
        _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
        //MASK SUPPORT END


    }
        SubShader{
            Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}

            //MASK SUPPORT ADD
            Stencil
            {
                Ref[_Stencil]
                Comp[_StencilComp]
                Pass[_StencilOp]
                ReadMask[_StencilReadMask]
                WriteMask[_StencilWriteMask]
            }
            ColorMask[_ColorMask]
            //MASK SUPPORT END


            Pass {
                Tags { "LightMode" = "ForwardBase" }

                ZWrite Off
            //          正常,透明度混合
            //          // Normal
            //          Blend SrcAlpha OneMinusSrcAlpha

            //          柔和叠加
            //          // Soft Additive
            //          Blend OneMinusDstColor One

            //          正片叠底 相乘         
            //          // Multiply
            //            Blend DstColor Zero

            //          两倍叠加 相加
            //          // 2x Multiply
            //          Blend DstColor SrcColor

            //          变暗
            //          // Darken
            //            BlendOp Min
            //          Blend One One   // When using Min operation, these factors are ignored

            //          变亮
            //          //  Lighten
             //        BlendOp Max
             //         Blend One One // When using Max operation, these factors are ignored

            //          滤色
            //          // Screen
             //        Blend OneMinusDstColor One
                        // Or
                     Blend One OneMinusSrcColor

            //          线性减淡
            //          // Linear Dodge
            //          Blend One One

                        CGPROGRAM

                        #pragma vertex vert
                        #pragma fragment frag

                        #include "Lighting.cginc"

                        fixed4 _Color;
                        sampler2D _MainTex;
                        float4 _MainTex_ST;
                        fixed _AlphaScale;

                        struct a2v {
                            float4 vertex : POSITION;
                            float3 normal : NORMAL;
                            float4 texcoord : TEXCOORD0;
                        };

                        struct v2f {
                            float4 pos : SV_POSITION;
                            float2 uv : TEXCOORD0;
                        };

                        v2f vert(a2v v) {
                            v2f o;
                            o.pos = UnityObjectToClipPos(v.vertex);

                            o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                            return o;
                        }

                        fixed4 frag(v2f i) : SV_Target {
                            fixed4 texColor = tex2D(_MainTex, i.uv);

                            return fixed4(texColor.rgb * _Color.rgb, texColor.a * _AlphaScale);
                        }

                        ENDCG
                    }
        }
            FallBack "Transparent/VertexLit"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值