How to make a shader’s alpha/transparency work?

Alpha/transparency should be easy, right? :slight_smile: I don’t know what I’m doing wrong here…
I couldn’t figure out how alpha works in a shader. Whatever I do, it blends with the clear color(even its alpha is 0) and never becomes transparent. I know it’s possible (even semi-transparent) because @d954mas has it working in Cashchubbies Island for the windows.

  • I set clear color to 0,0,0,0 on render script(to be sure)
  • I don’t want to use discard
  • Using default texture_profiles(Rgba)

Shader is very simple:

#version 140

out vec4 FragColor;

void main()
{
    vec3 color = vec3(1.0);                 // White color
    vec3 premultiplied_color = color * 0.5; // Premultiply alpha
    FragColor = vec4(premultiplied_color, 0.5);
}

Nothing special in render.script, but I also tried using different blend modes (FACTOR_ZERO, FACTOR_ONE) while making the proper changes in the shader as well but no luck.

    render.enable_state(graphics.STATE_DEPTH_TEST)
    render.enable_state(graphics.STATE_BLEND)
    render.set_blend_func(graphics.BLEND_FACTOR_SRC_ALPHA, graphics.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA)

    render.enable_state(graphics.STATE_CULL_FACE)
    render.draw(predicates.model, draw_options_world)
    render.set_depth_mask(false)
    render.disable_state(graphics.STATE_CULL_FACE)

I’ll be glad if anyone can point me in the right direction!

For transparency, render it last. And don’t write in depth buffer.

render.set_depth_mask(false) --don't write to depth
render.enable_state(graphics.STATE_DEPTH_TEST)
render.draw(self.predicates.model_transparent, self.draw_opts)
2 Likes

No, transparency is not easy at gpu:(
We can render millions of polygons but transparency is always complex with some limits)

1 Like

Ah! Thank you. So, do you know why this is necessary?

Sure, but I meant that just setting alpha should be easy. :slight_smile: I’m just experimenting, thank you

When rendering 3D models, they write their depth to the depth buffer. This ensures that pixels further away are not rendered over closer ones. However, if you render a transparent object and write it to the depth buffer, any pixels rendered after it that are deeper will not be visible. This causes issues with transparency, as objects behind the transparent surface may not render correctly.

3 Likes

I see, thank you very much :pray:

1 Like