0 votes

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

in Engine by (184 points)

Does your shader have the blend_mix render mode?

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

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?

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;
}

1 Answer

+1 vote

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.

by (3,875 points)

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?

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

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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.