在 Unity3D 中将 alpha 添加到着色器

Add alpha to shader in Unity3D

我对着色器编程一无所知,但现在我需要将 alpha 添加到我想使用的着色器中。其实我想淡入和淡出我的精灵,但它不在我使用的着色器中。

着色器:

Shader "Sprites/ClipArea2Sides"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
        _Length ("Length", Range(0.0, 1.0)) = 1.0
        _Width ("Width", Range(0.0, 1.0)) = 1.0
     }

    SubShader
    {
        LOD 200

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

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Offset -1, -1
            Fog { Mode Off }
            ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Length;
            float _Width;

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

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

            half4 frag (v2f IN) : COLOR
            {
                if ((IN.texcoord.x<0) || (IN.texcoord.x>_Width) || (IN.texcoord.y<0) || (IN.texcoord.y>_Length))
                {
                    half4 colorTransparent = half4(0,0,0,0) ;
                    return colorTransparent ;
                }
                else
                    return tex2D(_MainTex, IN.texcoord) ;
            }
            ENDCG
        }
    }
}

像这个着色器:http://wiki.unity3d.com/index.php/UnlitAlphaWithFade

由于性能原因,我需要针对移动设备优化 alpha。

回答

非常感谢 Anas iqbal。

这是一个具有裁剪区域 + 色调的着色器:

Shader "Sprites/TestShader"
{
    Properties
    {
        _Color ("Color Tint", Color) = (1,1,1,1)
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
        _Length ("Length", Range(0.0, 1.0)) = 1.0
        _Width ("Width", Range(0.0, 1.0)) = 1.0
     }

    SubShader
    {
        LOD 200

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

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Offset -1, -1
            Fog { Mode Off }
            ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Length;
            float _Width;
            half4 _Color;

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                half4 color : COLOR;
            };

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = v.texcoord;
                o.color = _Color;
                return o;
            }

            half4 frag (v2f IN) : COLOR
            {
                if ((IN.texcoord.x<0) || (IN.texcoord.x>_Width) || (IN.texcoord.y<0) || (IN.texcoord.y>_Length))
                {
                    half4 colorTransparent = half4(0,0,0,0) ;
                    return colorTransparent;
                }
                else
                {
                    half4 tex = tex2D(_MainTex, IN.texcoord);
                    tex.a = IN.color.a;
                    return tex;
                }
            }
            ENDCG
        }
    }
}

将此行添加到您的 Properties

_Color ("Color Tint", Color) = (1,1,1,1)

然后在 float _Width;

正下方添加这一行
half4 _Color;

更新您的 struct v2f 并在其中添加一个颜色变量。

struct v2f
{
    float4 vertex : POSITION;
    float2 texcoord : TEXCOORD0;
    half4 color : COLOR;
};

然后您可以像这样在 v2f vert 中使用它:

o.color = _Color

或者如果你只想单独玩 rgb 和 alpha

o.color.rgb = _Color.rgb
o.color.a = _Color.a

o.color.r = _Color.r
o.color.g = _Color.g
o.color.b = _Color.b
o.color.a = _Color.a

之后你可以return你的half4 frag (v2f IN) : COLOR方法中的颜色值

// do something with your color if you want
// you can also play with alpha here
return IN.color;