Transparent part of texture is shown as black

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TobiLa

Hi,

I have a mesh instance imported from blender which has a texture with transparent pixels.

In blender these pixels are correctly shown as transparent, in Godot they are painted black.

What can I do to show them as transparent pixels?

I am using a shader material which I converted from a spatial material.
The texture is a .png with 4096*4096 pixels and 32 bits.

The godot version is 3.0.6 stable.

Thanks in advance,
Tobi

Does your shader have the blend_mix render mode?

Zylann | 2018-11-08 19:06

Yes, it does. What should it be set to?

TobiLa | 2018-11-08 19:13

For the texture to show as transparent, you need to set the ALPHA variable in the fragment shader as well. Not just ALBEDO. Have you done this?

SIsilicon | 2018-11-08 20:12

No, I haven’t. You mean to set the ALPHA vaulue in the fragment shader? What should I assign to ALPHA?
Btw, my shader code looks like this:

shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx;
uniform vec4 albedo : hint_color;
uniform sampler2D texture_albedo : hint_albedo;
uniform float specular;
uniform float metallic;
uniform float roughness : hint_range(0,1);
uniform float point_size : hint_range(0,128);
uniform sampler2D texture_metallic : hint_white;
uniform vec4 metallic_texture_channel;
uniform sampler2D texture_roughness : hint_white;
uniform vec4 roughness_texture_channel;
uniform vec3 uv1_scale;
uniform vec3 uv1_offset;
uniform vec3 uv2_scale;
uniform vec3 uv2_offset;


void vertex() {
	UV=UV*uv1_scale.xy+uv1_offset.xy;
}




void fragment() {
	vec2 base_uv = UV;
	vec4 albedo_tex = texture(texture_albedo,base_uv);
	ALBEDO = albedo.rgb * albedo_tex.rgb;
	float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);
	METALLIC = metallic_tex * metallic;
	float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);
	ROUGHNESS = roughness_tex * roughness;
	SPECULAR = specular;
}

TobiLa | 2018-11-08 20:19

:bust_in_silhouette: Reply From: SIsilicon

How about you just make a new SpatialMaterial, set the transparent flag, then convert that into a ShaderMaterial.

But I might as well tell you what’s going on.
You see, ALPHA value determines the transparency of the currently processed pixel. ALBEDO determines the color(along with some other variables). How this shader got the colour from the texture is from this line of code.

vec4 albedo_tex = texture(texture_albedo, base_uv);

The first three components of this vector contain the colour itself (.rgb), which is used here.

ALBEDO = albedo.rgb * albedo_tex.rgb;

The last component contains the alpha channel (.a), which you can use by adding this line of code.

//This is what you add in the shader.
ALPHA = albedo.a * albedo_tex.a;

And like that, you have transparency.

Hi, that basically seems to work.
At least as long as I don’t set a custom albedo color as an addition to the texture.

If I do that the transparent part of the texture is painted black again. Unfortunately, I need the albedo color for an animation I do with the object.

Do you have any idea how to solve that?

TobiLa | 2018-11-09 15:57

Use ALPHA = albedo_tex.a; to get just the alpha from the texture, instead of mixing it from the color.

MysteryGM | 2018-11-09 16:42

Thanks again.
It seems to have something to do with my depth draw setting.
If I set it to depth_draw_alpha_prepass and using ALPHA = albedo_tex.a; it works, but I can’t use that setting for my material because it leads to graphical glitches at most of my meshes.
If I set it to depth_draw_always everything else looks fine, but my texture is still drawn black instead of transparent, even when using ALPHA = albedo_tex.a;.
What else could be the mistake? Or could this be a bug of godot?

TobiLa | 2018-11-09 18:33