Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question about basic ShaderLab, albedo and alpha concept

Discussion in 'Shaders' started by Daybreaker32, Dec 18, 2015.

  1. Daybreaker32

    Daybreaker32

    Joined:
    Jun 11, 2014
    Posts:
    73
    Hello, I have some basic question about transparent surface shader.

    Code (CSharp):
    1.     void surf (Input IN, inout SurfaceOutput o) {
    2.         fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
    3.         fixed4 t = _Time;
    4.         o.Albedo = c.rgb * c.a;
    5.         o.Alpha = c.a;
    6.     }  
    1. Why do we need to multiply c.rgb with c.a for o.Albedo? If I remove the * c.a, why does the supposed to be transparent pixel became white? Isn't there already o.Alpha = c.a?
    2. Why if I put o.Alpha = 0 some pixels are still visible?

    You can check my shader file and use this image for testing.

    Thank you.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    I'm not 100% certain, but I could imagine that it is related to this line in your shader:
    Code (CSharp):
    1. #pragma surface surf BlinnPhong alpha
    According to the documentation...

    If Unity picks premultiplied, it could explain why it turns white. You could try this theory if you change that line to alpha:fade as shown below:
    Code (CSharp):
    1. #pragma surface surf BlinnPhong alpha:fade

    I just got curious before posting and gave it a try. Here is a screenshot of not doing albedo*alpha with the different alpha modes. Is this what you're seeing too?




    As a side note, the shader is using the BlinnPhong lighting model, but does not provide gloss and specular properties in the surface shader. But this might be just because it's an example.
     
    Daybreaker32 likes this.
  3. Daybreaker32

    Daybreaker32

    Joined:
    Jun 11, 2014
    Posts:
    73
    Yep that's what I'm seeing.

    Ah! I see. I haven't check the optional parameters and just find out that there is an alpha:fade parameter. Great explanation on your link btw.

    But isn't BlinnPhong one of the simple lighting function? Why does Unity pick the alpha: premul instead of alpha:fade?

    edit: alpha:premul