Search Unity

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

Discussion in 'General Graphics' started by Nhauste, Jan 8, 2015.

  1. Nhauste

    Nhauste

    Joined:
    Mar 21, 2014
    Posts:
    7
    Hi everyone !

    I've searched a lot of things on the web but anyone of them fit to my request. Here's my problem :
    OrbRuns_Problem.png
    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 :
    MapBorders_Problem2.png

    instead of this

    MapBorders_Problem1.png

    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
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    multiply alpha channel with another texture's alpha channel.
     
  3. Nhauste

    Nhauste

    Joined:
    Mar 21, 2014
    Posts:
    7
    For sure, but how could I add an alpha mask (texture) to the shader I'm using ?
     
  4. MaxDesRoches

    MaxDesRoches

    Joined:
    Nov 5, 2014
    Posts:
    7
    You could use this shader:

    Code (CSharp):
    1. // Unlit alpha-blended shader.
    2. // - no lighting
    3. // - no lightmap support
    4. // - no per-material color
    5.  
    6. Shader "Unlit/TransparentAlphaMask" {
    7. Properties {
    8.     _MainTex ("Base (RGB)", 2D) = "white" {}
    9.     _AlphaTex ("Alpha mask (R)", 2D) = "white" {}
    10. }
    11.  
    12. SubShader {
    13.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    14.     LOD 100
    15.  
    16.     Blend SrcAlpha OneMinusSrcAlpha
    17.  
    18.     Pass {
    19.         CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.          
    23.             #include "UnityCG.cginc"
    24.          
    25.  
    26.             struct appdata_t {
    27.                 float4 vertex : POSITION;
    28.                 float2 texcoord : TEXCOORD0;
    29.             };
    30.  
    31.             struct v2f {
    32.                 float4 vertex : SV_POSITION;
    33.                 half2 texcoord : TEXCOORD0;
    34.             };
    35.  
    36.             sampler2D _MainTex;
    37.             sampler2D _AlphaTex;
    38.          
    39.             float4 _MainTex_ST;
    40.          
    41.             v2f vert (appdata_t v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    45.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    46.                 return o;
    47.             }
    48.          
    49.             fixed4 frag (v2f i) : SV_Target
    50.             {
    51.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    52.                 fixed4 col2 = tex2D(_AlphaTex, i.texcoord);
    53.              
    54.                 return fixed4(col.r, col.g, col.b, col2.r);
    55.             }
    56.         ENDCG
    57.     }
    58. }
    59.  
    60. }
    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.
     
    hugogfadams likes this.
  5. Nhauste

    Nhauste

    Joined:
    Mar 21, 2014
    Posts:
    7
    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.
     
    Last edited: Jan 8, 2015
  6. MaxDesRoches

    MaxDesRoches

    Joined:
    Nov 5, 2014
    Posts:
    7
    I checked the Shader, everything seems to be in order here. Here is a screenshot of what I get in Unity :

     
  7. Nhauste

    Nhauste

    Joined:
    Mar 21, 2014
    Posts:
    7
    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.

    OrbRuns_Problem2.png

    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 ?
     
    Last edited: Jan 10, 2015
  8. Nhauste

    Nhauste

    Joined:
    Mar 21, 2014
    Posts:
    7
    I've found a solution to my problem.

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

    Code (CSharp):
    1. Shader "Custom/Map Borders" ////////// Shader renamed
    2. {
    3.    Properties
    4.    {
    5.       _Color ("Mask Color", Color) = (1, 1, 1, 1) ////////// _Color property added to this shader
    6.       _MainTex ("Base (RGB)", 2D) = "white" {}
    7.       _Mask ("Culling Mask", 2D) = "white" {}
    8.       _Cutoff ("Alpha cutoff", Range (0,1)) = 0.1
    9.    }
    10.    SubShader
    11.    {
    12.       Tags {"Queue"="Transparent"}
    13.       Cull Off ////////// I added this line to have the both sides of my plane mesh visible
    14.       Lighting Off
    15.       ZWrite Off
    16.       Blend SrcAlpha OneMinusSrcAlpha
    17.       AlphaTest GEqual [_Cutoff]
    18.       Pass
    19.       {
    20.          SetTexture [_Mask] {
    21.              constantColor [_Color]
    22.              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)
    23.             }
    24.          SetTexture [_MainTex] {combine texture, previous}
    25.       }
    26.    }
    27. }
    Now it works fine, here's a fresh new screenshot with my problem solved :

    OrbRuns_ProblemSolved.png
     
    Last edited: Jan 18, 2015
    shacharoz likes this.
  9. thisiskctan

    thisiskctan

    Joined:
    Jan 2, 2016
    Posts:
    2
    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.
     
  10. Batigol

    Batigol

    Joined:
    Oct 17, 2012
    Posts:
    234
    Is there any way to add it to Fx/Water shader? I need to make soft blend edge for water