Search Unity

Need help getting alpha in a simple shader

Discussion in 'Shaders' started by petey, Oct 13, 2010.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hi there,
    I have this simple shader that is just a solid color but I'd also like it to have transparency.

    Code (csharp):
    1. Shader "Petes/Solid Color" {
    2.  
    3.     properties{
    4.     _Color ("Solid Color",Color) = (0,0,0,0)   
    5.     }
    6.         subshader{
    7.         Cull Off
    8.         color [_Color] 
    9.         pass{}
    10.     }
    11. }
    Could someone help me add that in?
    Thanks, I'm still pretty average at the whole shader thing...

    Pete
     
  2. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    I'm still stuck with this one, does anyone know how I could get the transparency working here?
    I thought since I added the extra alpha section in here that it would allow me to use tranparency but it still doesnt work Color) = (0,0,0,0) :(
     
  3. arioch82

    arioch82

    Joined:
    Dec 3, 2009
    Posts:
    253
  4. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hey thanks arioch82
    The alpha is playing a part in the texture now but I it doesn't seem to play nice zdepth wise. Some objects appear in front but it seems okay with other objects... I tried the ztest option and I've included ZWrite but it cant get it to look right.
    Any ideas?

    Code (csharp):
    1.  
    2. Shader "Petes/Solid Color" {
    3.  
    4.     properties{
    5.     _Color ("Solid Color",color) = (0,0,0,0)   
    6.     }
    7.         subshader{
    8.         Cull Off
    9.         ZWrite On
    10.         color [_Color] 
    11.         pass{
    12.                 Blend SrcAlpha OneMinusSrcAlpha
    13.             }
    14.         }
    15. }

    Thanks
    Pete
     
  5. arioch82

    arioch82

    Joined:
    Dec 3, 2009
    Posts:
    253
    Hi Pete,

    I'm quite new to the shader programming (I'm approaching them for the first time just in these days), i know how to apply an alpha from a texture but not to a solid color from a float sorry
     
  6. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Try this;
    Code (csharp):
    1.  
    2. Shader "Petes/Solid Color"
    3. {
    4.     Properties
    5.     {
    6.         _Color ("Solid Color",color) = (0,0,0,0)   
    7.     }
    8.     Category
    9.     {
    10.    
    11.         Tags {"Queue"="Transparent"}
    12.         ZWrite Off
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.  
    15.         Subshader
    16.         {
    17.             color [_Color] 
    18.             Pass {}
    19.         }
    20.     }
    21. }
    I took the liberty of changing the indentation it a bit :p.

    The secret is the Tags {"Queue"="Transparent"} line, which tells unity to draw the object in the transparent queue. This makes sure it is drawn with the other transparent objects, in back-to-front order.
     
    Last edited: Oct 15, 2010
  7. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    cool thanks tomvds! That works great!
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    There's no need to use quotes in the Tag unless you're using a custom queue, and Category is only useful when you have multiple SubShaders.

    Code (csharp):
    1. Shader "Petes/Solid Transparent Color" {
    2.  
    3. Properties {
    4.     _Color ("Solid Color (A = Opacity)", Color) = (0,0,0,1)
    5. }
    6.  
    7. Subshader {
    8.     Tags {Queue = Transparent}
    9.     ZWrite Off
    10.     Blend SrcAlpha OneMinusSrcAlpha
    11.    
    12.     Color [_Color] 
    13.     Pass {}
    14. }
    15.  
    16. }
    More info about alpha blending / queues here, if you're interested.
     
    Last edited: Oct 16, 2010
  9. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Thanks Jessy, that's great!