- Home /
Shader question: Alpha value of 0 not entirely transparent
Hi
I'm writing a shader for terrain surface, which supports multitexture blending according to the local vertex color. Now, the alpha value of the vertex color should render areas of the texture completely transparent, revealing surfaces below if needed, like a water stream for example.
So far this works out well, save from the transparent part. Here a screenshot:
the left half of the material has an vertex color alpha value of 0, the right side has an alpha value of 255. Behind are some test objects and the problem is that I can't get the blending right. I want the left area to be invisible completely, not just a bit translucent.
Here is the shadercode:
Shader "fornax/test/VertexMultiBlend" {
Properties {
_MainTex0 ("Layer0", 2D) = "white" {}
_MainTex1 ("Layer1", 2D) = "white" {}
_MainTex2 ("Layer2", 2D) = "white" {}
}
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "ForceNoShadowCasting"="True" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha
sampler2D _MainTex0;
sampler2D _MainTex1;
sampler2D _MainTex2;
struct Input {
float2 uv_MainTex0;
float4 color: Color; // Vertex color
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c0 = tex2D (_MainTex0, IN.uv_MainTex0);
half4 c1 = tex2D (_MainTex1, IN.uv_MainTex0);
half4 c2 = tex2D (_MainTex2, IN.uv_MainTex0);
o.Albedo = IN.color.r * c0.rgb + IN.color.g * c1.rgb + IN.color.b * c2.rgb; // vertex RGB
o.Alpha = IN.color.a; // vertex Alpha
}
ENDCG
}
FallBack "Diffuse"
}
I am not very vell with shaders and I can't find how I adjust the blending type, if that's what I need to do anyway. Any help is apreciated!
// edit: To add onto this: I experimented with the renderqueue and rendertype a lot. Most sense to me makes rendertype="TransparentCutout" and renderqueue="Background" since the ground is always the first rendered object, nothing needs to be displayed behind it, save from other background shaders of the same type. I couldn't see any difference tho which confuses me. All Rendertypes and queues seem to create the same result.
Current status: (Note how the plane with the grass texture is in front of those 3 objects but rendered behind.)
Answer by WILEz1975 · May 16, 2015 at 03:45 PM
Same problem...
Tags {
"Queue"="Transparent"
"IgnoreProjector"="false"
"RenderType"="Transparent"
}
Blend SrcAlpha OneMinusSrcAlpha // use alpha blending
ZWrite Off // don't write to depth buffer
.... ....
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Emission = c.rgb*_Color.a;
o.Alpha =0 ; //Try to set 0
}
I put o.Alpha =0 but does not work. It is not completely transparent (invisible). Why?
Answer by LexDeKogel · Sep 01, 2019 at 12:14 AM
Late answer but multiplying the rgb values by the alpha value seems to completely solve the problem. So c.rgb *= c.a; for a color c.
I still don't entirely understand why it works this way.