- Home /
Transparent/Diffuse-shader always transparent by default (alpha)?
I'm currently learning Unity and I've had some problems with the Transparent/Diffuse-shader. I made a script that, using iTween, fades objects when you go behind them and for that I needed a shader that supports transparency.
This one was recommended, but when I apply it to my test-objects they act like the alpha is set to a low amount, even though it isn't mentioned anywhere in the script or settings (most of the objects aren't even related to the script, they just share the same texture which is one of Unity's default ones). If I use the normal diffuse-shader there aren't any problems, but I can't use the script either (due to the lack of transparency).
How can I make the objects stay at 255 alpha by default, or alternatively, how can I change all the affected objects at startup? I've already tried with one and got it to work, but that would require scripts for all the similar objects which seems pretty unnecessary.
First question and post here btw, so hi me!
Your textures either have alpha information or you've got it created by default in the texture in Unity. Click the texture and check out its settings.
After some random testing, it's on all the default textures and even without a texture it's still the same. I'm not sure how I can check if it gets created by default in the texture though
Did you try clicking the color selector and make sure the alpha is at 255?
If you mean the small color-selection above the texture (in the bottom-right corner of the pic), then yes. I can't find any other type of color-selector (like I said, I started using Unity just lately)
Answer by CHPedersen · May 03, 2013 at 11:17 AM
I downloaded the source for the built-in shaders some time ago, and can reveal that Transparent/Diffuse is implemented like so:
Shader "Transparent/Diffuse" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha
sampler2D _MainTex;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/VertexLit"
}
As you can see, the color that ends up getting written is the product of the color you select in the editor, and the color that gets read from the texture. They get multiplied together:
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
I can see in your screenshot that your _Color variable is all white and alpha 1. (It would have a partially black bar underneath if you had its alpha lowered). So the texture color alone is actually displayed, because it gets multiplied with (1,1,1,1) in the shader. Therefore, the only other option, as @Loius suggested, is that your texture has alpha values below 1 in its pixels. It is the only other place an alpha value < 1 could possibly come from.
Open your texture in a bitmap editor (Photoshop, GIMP, Paint(dot)Net) and examine its alpha channel.
I tried to make a new texture completely and it seems like it's solved now. I still don't get how I even had the problem when I didn't have a texture attached, but I can live with that.
Thanks for the answer and detailed explanation
The Properties section in the shader is where the editor-accessible values are defined, i.e. the $$anonymous$$ain Color and the texture. The stuff to the right in each definition after the = is the default value. That is, the color is "born" as white until the user changes it, and if the user doesn't supply a texture, it uses some built-in standard one called "white". There are other built-in textures one can choose; they're defined on this page:
http://docs.unity3d.com/Documentation/Components/SL-Properties.html
When I test it, my objects become solidly colored if I don't supply a texture, and set the Color's alpha to 1. This indicates that Unity's builtin "white" texture has alpha = 1 written in all its pixels. Why it behaved differently on your machine shall remain a mystery, it seems. :)