Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Alpha Value in Fragment Shader

Discussion in 'Shaders' started by Pouleto, Nov 9, 2015.

  1. Pouleto

    Pouleto

    Joined:
    Oct 10, 2015
    Posts:
    3
    Hi!

    I'm currently trying to do something pretty simple : modify the alpha value into a fragment shader. This is the code of my shader :

    Code (CSharp):
    1. Shader "UI/GreenBackgroundRemover" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.    
    6.     SubShader {
    7.  
    8.         Pass {
    9.             CGPROGRAM
    10.             #pragma vertex vert_img
    11.             #pragma fragment frag
    12.  
    13.             #include "UnityCG.cginc"
    14.            
    15.             uniform sampler2D _MainTex;
    16.  
    17.             fixed4 frag(v2f_img i) : SV_Target {
    18.                 fixed4 r = tex2D(_MainTex, i.uv);
    19.                
    20.                 if(r.g >= 0.7f)
    21.                 {
    22.                     r.a = 0;
    23.                     r.g = r.r = r.b = 0;
    24.                 }
    25.                 return r;
    26.             }
    27.             ENDCG
    28.         }
    29.     }
    30. }
    I use this shader into a material.
    Here is my initial texture:
    test.png
    And here is the result of my shader:
    Capture.PNG

    As you can see the pixels are set to black but the alpha value is not modified. Does anyone have an idea about what I'm missing here?

    Thanks in advance for your help!
     
  2. Fuegan

    Fuegan

    Joined:
    Dec 5, 2012
    Posts:
    20
    You are missing a blending method, I don't know how you can add it directly in the fragment shader but you can do it with shaderlab (cf : http://docs.unity3d.com/Manual/SL-Blend.html). In general you just have to add it (
    Blend SrcAlpha OneMinusSrcAlpha) wich is the basic alpha blending.

    Code (CSharp):
    1.     Shader "UI/GreenBackgroundRemover" {
    2.         Properties {
    3.             _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         }
    5.      
    6.         SubShader {
    7.             Blend SrcAlpha OneMinusSrcAlpha
    8.  
    9.             Pass {
    10.                 CGPROGRAM
    11.                 #pragma vertex vert_img
    12.                 #pragma fragment frag
    13.    
    14.                 #include "UnityCG.cginc"
    15.              
    16.                 uniform sampler2D _MainTex;
    17.    
    18.                 fixed4 frag(v2f_img i) : SV_Target {
    19.                     fixed4 r = tex2D(_MainTex, i.uv);
    20.                  
    21.                     if(r.g >= 0.7f)
    22.                     {
    23.                         r.a = 0;
    24.                         r.g = r.r = r.b = 0;
    25.                     }
    26.                     return r;
    27.                 }
    28.                 ENDCG
    29.             }
    30.         }
    31.     }
    32.  
     
    eDmitriy and Zihaben like this.
  3. Pouleto

    Pouleto

    Joined:
    Oct 10, 2015
    Posts:
    3
    @Fuegan Thanks a lot for your answer! Although it seems there is a rendering order problem, instead of seeing the object behing my plane I see the color of the background of my camera. And what is funny is that it is the good result in the Scene Tab but not in the Game Tab.

    In Scene Tab : Capture.PNG
    In the Game Tab : Capture.PNG

    I'll look further into the link you gave me!
     
  4. Fuegan

    Fuegan

    Joined:
    Dec 5, 2012
    Posts:
    20
    As you don't have any tags set it seems Unity doesn't render your objects in the right order yes.

    Adding this

    Code (CSharp):
    1. Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    between the SubShader and Blend lignes should resolve the problem.

    The main part is "Queue"="Transparent" wich is the order in the queue (Transparent are renderer after opaque objects).

    The RenderType part is for Unity (it will be treated as a transparent object) I don't know exactly what it does but I suppose it has to do with some optimisation.

    IgnoreProjector is usually added to transparent shaders as projectors don't work well with transparency. (But it is useless if you don't use projectors).


    PS : without tags unity sets the objects as "Queue"="Geometry" which means if your background have a standard shader without transparency your plane should have still be rendered last so be sure your objects are not too close (as it can cause flickering).
     
  5. Pouleto

    Pouleto

    Joined:
    Oct 10, 2015
    Posts:
    3
    Thanks for the explanation!
    I finally had to invert the order of my camera and my objects in z to make it work.
     
  6. X28

    X28

    Joined:
    Jul 15, 2015
    Posts:
    5
    Thank you. ;)
     
unityunity