unity 2d遮挡透明显示整理

unity 2d遮挡透明显示整理

游戏里面常常用到几种人物遮挡的透明显示 在这里也给大家整理一下几种的方式。

这里写图片描述原图

图片中火枪手被黑色的图片遮挡了一边,现在想让它被遮挡的另一边也显示出来。

Shader "Custom/Mask VisibleColor" {
     Properties {
            _NotVisibleColor ("NotVisibleColor (RGB)", Color) = (0.3,0.3,0.3,1)
            _MainTex ("Base (RGB)", 2D) = "white" {}
        }
        SubShader {
            Tags { "Queue" = "Geometry+500" "RenderType"="Opaque" }
            LOD 200

            Pass {
                ZTest Greater
                Lighting Off
                ZWrite Off
                Color [_NotVisibleColor]
                Blend SrcAlpha OneMinusSrcAlpha
                SetTexture [_MainTex] { ConstantColor [_NotVisibleColor] combine constant * texture }

            }

            Pass {
                ZTest LEqual
                Material {
                    Diffuse (1,1,1,1)
                    Ambient (1,1,1,1)
                }
                Lighting Off
                 Blend SrcAlpha OneMinusSrcAlpha
                SetTexture [_MainTex] { combine texture } 
            }

        } 
        FallBack "Diffuse"
}

效果图如下:
这里写图片描述

亦可用顶点渲染的方式

Shader "Custom/Mask Color" {
    Properties {
        _NotVisibleColor ("NotVisibleColor (RGB)", Color) = (0.3,0.3,0.3,1)
        _MainTex ("Back Texture", 2D) = "white" {}
    }

    SubShader {
        Tags { "Queue" = "Transparent" }


        Pass {

            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            Offset -1, -1
            ZTest   Greater
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata_vert {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
            };

            uniform sampler2D _MainTex;
            uniform float4 _NotVisibleColor;

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

            v2f vert (appdata_vert v) {
                v2f o;
                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                o.uv=v.texcoord;
                return o;
            }

            float4 frag (v2f i) : COLOR {
                half4 texcol = tex2D( _MainTex, i.uv );
                if(texcol.a != 0){

                    texcol.xyzw = _NotVisibleColor.xyzw;
                }
                return texcol;
            }
        ENDCG
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            ZTest LEqual
            Fog { Mode Off }
            Offset -1, -1
            ColorMask RGB
            AlphaTest Greater .01
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMaterial AmbientAndDiffuse

            SetTexture [_MainTex]
            {
                Combine Texture * Primary
            }
        }

    }
}

如图:
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值