[Solved] How to add an alpha mask over the existing Transparent/Cutout/Soft Edge Unlit shader ?

Hi everyone !

I’ve searched a lot of things on the web but anyone of them fit to my request. Here’s my problem :


If the player goes too far on the x axis, it will be out of bounds and will have to retry the level. The purple grid symbolizes the x limit of the level. It is actually a simple plane stuck on a certain x value but follows the y and z positions of the ball. To give the impression that the grid is “infinite”, I’ve attached to my plane a script changing the offset of the texture depending on the y and z positions of the ball. It works fine, but the square shape of the grid is not so aesthetic.

I would prefer having this :

instead of this

The plane with the grid uses the Transparent/Cutout/SoftEdgeUnlit for the following reasons :

  • The material.color.a changes depending on the distance between the ball and the grid, making it dissapear if the ball is too far, so I need a material with the “Color” component ;
  • Both sides are visible ;
  • I need a material which understand the alpha channel of my .png file.

How could I have this result, knowing that the _MainTex (and its own alpha channel) offset depends on another object ?

Thanks for the future answers,

Nhauste

multiply alpha channel with another texture’s alpha channel.

For sure, but how could I add an alpha mask (texture) to the shader I’m using ?

You could use this shader:

// Unlit alpha-blended shader.
// - no lighting
// - no lightmap support
// - no per-material color

Shader "Unlit/TransparentAlphaMask" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _AlphaTex ("Alpha mask (R)", 2D) = "white" {}
}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 100
  
    Blend SrcAlpha OneMinusSrcAlpha
  
    Pass {
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
          
            #include "UnityCG.cginc"
          

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

            struct v2f {
                float4 vertex : SV_POSITION;
                half2 texcoord : TEXCOORD0;
            };

            sampler2D _MainTex;
            sampler2D _AlphaTex;
          
            float4 _MainTex_ST;
          
            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }
          
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.texcoord);
                fixed4 col2 = tex2D(_AlphaTex, i.texcoord);
              
                return fixed4(col.r, col.g, col.b, col2.r);
            }
        ENDCG
    }
}

}

And use a .png file where white is alpha = 1 and black is alpha = 0. So a radial gradient from white to black would do the effect.

1 Like

There is probably an error, when I select your shader in the inspector, I can’t add any texture to the “Alpha mask” property. It continues saying “None” (Texture). The console tells me :

Material doesn’t have a texture property ‘_AlphaTex’
UnityEditor.DockArea:OnGUI()

And even with a texture in the Base (RGB), my mesh keeps this annoying pink colour.

I checked the Shader, everything seems to be in order here. Here is a screenshot of what I get in Unity :

Ok, I tried another time. I’ve just created a new shader, and I’ve pasted the code you wrote up there.

Returning back to the editor, I got these two errors. Is really my graphic card a problem ? I have a Nvidia Geforce GT 630M.

Sorry about these questions that could seem absurd, I’m not really good at coding shaders, especially in csharp.

I don’t know how will look my grid when I’ll manage to use your shader, but I would also need the “Color” property (as I said I change the renderer.material.color.a depending on the distance between the ball and the grid) and an alpha mask directly in the “_MainTex” property (or the squares between the grid lines will be opaque).

NB : I forgot to mention, but as you can see it in my screenshot I’m using the free license of Unity. Could it be a limitation problem ?

I’ve found a solution to my problem.

I modified some things in the Texture Mask Shader available on the web :

Shader "Custom/Map Borders" ////////// Shader renamed
{
   Properties
   {
      _Color ("Mask Color", Color) = (1, 1, 1, 1) ////////// _Color property added to this shader
      _MainTex ("Base (RGB)", 2D) = "white" {}
      _Mask ("Culling Mask", 2D) = "white" {}
      _Cutoff ("Alpha cutoff", Range (0,1)) = 0.1
   }
   SubShader
   {
      Tags {"Queue"="Transparent"}
      Cull Off ////////// I added this line to have the both sides of my plane mesh visible
      Lighting Off
      ZWrite Off
      Blend SrcAlpha OneMinusSrcAlpha
      AlphaTest GEqual [_Cutoff]
      Pass
      {
         SetTexture [_Mask] {
             constantColor [_Color]
             Combine texture * constant ////////// The alpha component of the _Color property defines the Culling Mask opacity (depending on the distance between the player and the grid)
            }
         SetTexture [_MainTex] {combine texture, previous}
      }
   }
}

Now it works fine, here’s a fresh new screenshot with my problem solved :

1 Like

Hi, could you please explain further on how you do it? I want to do something similar as well. But I still did not understand about it. Sorry, unity3d newbie here. Thanks.

Is there any way to add it to Fx/Water shader? I need to make soft blend edge for water