I am having what seems like a simple problem. I am trying write a fragment shader that takes one texture as the base, then another texture as the alpha layer. I came up with:
My base texture has four different quadrants of four different colors. The alpha texture has similarly mapped quadrants, except the top right and the bottom left quadrants have an alpha of 0. Mapped to a cube, there aren’t any transparent quadrants rendered in Unity. The whole cube is visible. Why are the pink and purple quadrants showing?
As you can see, it is picking up the alpha layer correctly… it’s just not showing it correctly in the original shader. I’ve put together a basic package that you can download which shows this problem.
Works perfectly fine here (well, aside from needing to clamp your alpha texture, to avoid the pink artifact). All I did was open your package, and voila:
Also for transparent shaders in most cases you should not write to Z buffer.
ZWrite Off
And just to be picky, this is a plain cg shader wrapped in shaderlab syntax, not what is called “Surface Shader” in Unity so it won’t get all the lighting goodness. Depending on what you want to achive one can be better than the other and vice-versa.
A surface shader would look something like this
Jessy,
are you using the latest version of Unity? I just upgraded to 4.1.2 from 4.1 hoping to see a difference but I didn’t. In any case I’ve filed a bug report (535343) to see what Unity has to say about it.
cician,
I am trying to use the alpha channel of the alpha texture. It works correctly when I switch it to red, but why isn’t it working correctly when I use alpha? Especially since Jessy said that it works correctly on his end. Thanks for the surface shader - I’m finally starting to wrap my head around Cg so I haven’t even tried looking at surface shaders yet.