Transparency Problem

Hello,

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:

Shader "Blend"
{
	Properties
	{
		_MainTex ("Main Texture", 2D) = "clear" {}
		_AlphaTex ("Alpha Texture", 2D) = "clear" {}
	}
    SubShader
	{
        Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
        Blend SrcAlpha OneMinusSrcAlpha     // Alpha blending
        
        Pass {
			CGPROGRAM
			#pragma vertex vert_img
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest
			#include "UnityCG.cginc"
			
			uniform sampler2D _MainTex;
			uniform sampler2D _AlphaTex;
			
			fixed4 frag(v2f_img i) : COLOR
			{
				return fixed4(tex2D(_MainTex, i.uv).rgb, tex2D(_AlphaTex, i.uv).a);
			}
			ENDCG
        } // end pass
    } // end SubShader
	
	Fallback "Diffuse"
}

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?

Here’s a view of my alpha texture:

White represents an alpha value of 1, black represents an alpha value of 0. This was made by changing the fragment shader to:

return fixed4(tex2D(_AlphaTex, i.uv).a,tex2D(_AlphaTex, i.uv).a,tex2D(_AlphaTex, i.uv).a,1);

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.

1207230–48745–$BlendingProblem.unitypackage (125 KB)

Any ideas?

Thanks,
Justin

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:

Oh, and here’s a tip to decrease your verbosity:

return fixed4(tex2D(_AlphaTex, i.uv).aaa, 1);

1207309--48758--$Screen Shot 2013-04-01 at 10.08.07 PM.png

Your issue is probably that you’re using alpha channel of the alpha texture.

What you most like want here is to change

return fixed4(tex2D(_MainTex, i.uv).rgb, tex2D(_AlphaTex, i.uv).a);

to

return fixed4(tex2D(_MainTex, i.uv).rgb, tex2D(_AlphaTex, i.uv).r);

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

Shader "Blend" {
    Properties {
        _MainTex ("Main Texture", 2D) = "clear" {}
        _AlphaTex ("Alpha Texture", 2D) = "clear" {}
    }

    SubShader {
        Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }

        CGPROGRAM
	        #pragma surface surf Lambert alpha
	        
	        uniform sampler2D _MainTex;
	        uniform sampler2D _AlphaTex;
	
			struct Input {
				float2 uv_MainTex;
				float2 uv_AlphaTex;
			};
	
	        void surf(Input IN, inout SurfaceOutput o) {
				o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
				o.Alpha = tex2D(_AlphaTex, IN.uv_AlphaTex).r;
	        }
        ENDCG
    }

    Fallback "Diffuse"
}

Cheers :smile:

Thanks guys.

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.

I tested on 4.1.1. It could be a platform-dependent thing. I’m on OS X.