Hello all. I’m getting started with alpha shaders. I decided to edit the default shader to learn how it works.
This code is supposed to take two textures, a Main Texture, and an Alpha Map. The Alpha Map just sets the alpha of the main texture.
Question 1: How does the program know how to hook 2D _MainTex with Sampler2D _MainTex? Is it ‘proper’ to have same names? Is uv_MainTex automatic as well?
Question 2: I’m passing in AlphaTexture’s rgba values, when all I need are the a values. How do I only pass the alpha values?
Question 3: What am I missing from here to make this work?
Having the same name (and appropriate type) is the only way to get your properties into your shader when it’s executing. The automatic UV calculation (taking scaling and offset into account) is done only in surface shaders like yours, when you add the appropriately named input structs (uv2 works too). In a regular vertex/fragment shader, you need to do the calculations yourself by declaring the automatically filled TextureName_ST fields, like in this example.
On real graphics cards (i.e. not mobile chipsets), reading a 4-component vector incurs no more cost than reading a single float. Also, the compiler is usually clever enough to ignore values that you aren’t using. However, you can make it explicit by changing your code to this:
I think what isn’t working is alpha blending, which is off by default in surface shaders.
To enable standard alpha blending change: #pragma surface surf Lambert
to #pragma surface surf Lambert alpha
then you should change the render queue to transparent, like this
Tags { “Queue” = “Transparent” }
And finally turn off ztest off, though that may be automatic with “alpha” flag, not sure.
ZTest is turned off by the alpha surface parameter. The only thing you need to add manually are tags, which should be the following for most transparent shaders:
Both #pragma surface surf Lambert alpha and the Tags were important for getting the effect I wanted. Looks like there’s quite a bit of research to be done!