在Unity3D中为着色器添加alpha

5

我对着色器编程毫无头绪,但现在我需要给我想要使用的着色器添加透明度。实际上,我想让我的精灵淡入和淡出,但这并不包含在我正在使用的着色器中。

着色器:

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
        }
    }
}
1个回答

3

将此行添加到您的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

然后,在您的half4 frag (v2f IN) : COLOR方法中,您可以返回颜色值。

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

非常感谢您的解决方案,但是我不知道如何在返回half4 frag (v2f IN) : COLOR函数中同时返回IN.color。正如您所看到的return colorTransparentreturn tex2D(_MainTex, IN.texcoord),我想在我的脚本中编辑材质的alpha通道。 - ATHellboy
1
return colorTransparent; 将返回没有 alpha(透明/无颜色)的黑色。而 return tex2D(_MainTex, IN.texcoord) 将返回纹理的颜色。因此,如果您想调整纹理的 alpha 值,则可以执行 half4 tex = tex2D(_MainTex, IN.texcoord); tex.a = IN.color.a; return tex; 这将把纹理的 alpha 值更改为您设置的颜色的 alpha 值。 - Anas iqbal
关于这个着色器,只有一个问题,它能在移动设备上使用吗? - ATHellboy
着色器在我看来(用于移动设备)看起来很好,但我不是着色器编写专家,因此不太擅长着色器优化。您可以通过在移动设备上运行它并进行一些分析来测试它,以查看着色器是否会出现任何峰值。 - Anas iqbal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接