Search Unity

Default shaders writing the texture alpha to the output.

Discussion in 'Shaders' started by Rob-Fireproof, Apr 3, 2014.

  1. Rob-Fireproof

    Rob-Fireproof

    Joined:
    Dec 18, 2012
    Posts:
    56
    All of the Unity default shaders that use the texture alpha for something other than alpha (e.g. the Specular shader, which uses it for gloss), still write out the alpha of the texture to the target buffer. This seems very odd - the "A" channel of the texture doesn't represent alpha in these cases, so why treat it as such when writing the pixel out?

    Here's the part in question, from the built-in Specular shader:
    Code (csharp):
    1.     o.Gloss = tex.a;                 //This is fine - we're using the alpha channel for gloss
    2.     o.Alpha = tex.a * _Color.a;     //Why not o.Alpha = 1; (or maybe just _Color.a)?
    This causes problems when we render an object to a render target and then draw that to the screen, since the result is that the object is transparent where it isn't specular. Is there a good way around it other than making custom versions of each shader?

    To give a concrete example, to display the inventory in our game we do a render of an object to a render target which is initially transparent, so that we should get an opaque objects on a transparent background, which we can then render to the HUD. What we actually end up with using Unity's default shaders is a semi-transparent object on a transparent background.

    I could do something fancy with alpha testing when I draw the resulting image to the screen, but a) that's expensive, and b) some of the objects might have genuinely semi-transparent parts.

    Is there actually a good reason it's done this way?

    Thanks,
    Rob.
     
    Last edited: Apr 3, 2014