5

I don't have any idea about shader programming but right now I need to add alpha to the shader that I want to use. Actually I want to fade in and fade out my sprite but it's not in the shader that I use.

Shader :

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
        }
    }
}

Something like this shader : http://wiki.unity3d.com/index.php/UnlitAlphaWithFade

And I need optimized alpha for mobile because of performance.

Answer

Many thanks to Anas iqbal.

This is a shader which has clip area + color tint :

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
        }
    }
}
ATHellboy
  • 704
  • 3
  • 15
  • 30

1 Answers1

3

Add this line to your Properties

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

then add this line right below float _Width;

half4 _Color;

Update your struct v2f and add a colour variable in it.

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

then you can use it in your v2f vert like this:

o.color = _Color

OR if you only want to play with rgb and alpha seperatly

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

OR

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

after that you can return the color value in your half4 frag (v2f IN) : COLOR method

// do something with your color if you want
// you can also play with alpha here
return IN.color;
Anas iqbal
  • 1,036
  • 8
  • 23
  • Many thanks for solution but I don't know how to return IN.color when I return another things in half4 frag (v2f IN) : COLOR as you see `return colorTransparent` and `return tex2D(_MainTex, IN.texcoord)` and I want to edit alpha channel of material in my scripts – ATHellboy Sep 02 '15 at 07:50
  • 1
    `return colorTransparent;` will return black color with no alpha (transparent/ no color). while `return tex2D(_MainTex, IN.texcoord)` will return the color of the texture. Therefore, if you want to play with texture alpha; you can do `half4 tex = tex2D(_MainTex, IN.texcoord); tex.a = IN.color.a; return tex;` this will change the alpha of the texture to the alpha of color you set. – Anas iqbal Sep 02 '15 at 10:24
  • Just one thing about this shader, Is it ok to use it on mobile ? – ATHellboy Sep 02 '15 at 12:29
  • The Shader looks fine (for use on mobile) to me but I am not expert in shader writing and so not very good a shader optimisations. You can test it by running it on a mobile and do some profiling to see if the shader is giving any spikes or not. – Anas iqbal Sep 03 '15 at 06:20