Why the shader alpha value is not applied

I’m trying to get a shader work. But alpha value is not applied despite I set the value to 0 (or any other value). There probably some setting (blend setting?) that I need to enable but could not find where I can do it so I would appreciate it if. I can show it with this minimal code

#version 140

in highp vec4 var_position;
in mediump vec2 var_texcoord0;
in mediump vec3 var_normal;
in mediump vec4 var_light;

out vec4 out_fragColor;

uniform fs_uniforms
{
    mediump vec4 scanline_params;
    mediump vec4 time;
};

void main()
{
    float scanline_count = scanline_params.x;

    float scroll_speed = 0.02;
    float scroll_offset = mod(var_texcoord0.y + time.x * scroll_speed, 1.0); 

    float scanline_intensity = sin(scroll_offset * scanline_count * 3.14159 * 2.0);
    scanline_intensity = (scanline_intensity * 0.5 + 0.5) * 0.9 + 0.1;

    if (scanline_intensity < 0.75)
    discard;

    out_fragColor = vec4(vec3(0.2), 0);
}



1 Like

Thank you for the answer. But, I have already seen it, just could not figure out where or how to apply it. Do I modify the builtin renderer?
I’m relatively new to Defold so there are many things I’m unfamiliar with. I can handle game logic part but shaders are a mystery to me so I’d appreciate if you could give some tips on it

You can’t modify things inside the built-ins. You need to make your own copy of anything you need from them and place it outside the built-ins. In your case, make a copy of the renderer folder. Then, go to game.project > Bootstrap > Render and set it to the new copy of default.render. Also, modify your default.render to reference the new default.render_script. Once you do that, you’ll be ready to use what @selimanac mentioned inside your own default.render_script.

2 Likes

Yes, you have to modify the builtin renderer.
Copy, paste the build-in .render_script and .render to your project folder and set it .

You can place your model predicated after the sprites, tilemaps section.

Something like this:
(You might want to use graphics.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA instead of BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)

  -- render the other components: sprites, tilemaps, particles etc
    --
    render.enable_state(graphics.STATE_BLEND)
    render.draw(predicates.tile, draw_options_world)
    render.draw(predicates.particle)
    --render.disable_state(graphics.STATE_DEPTH_TEST)

    -- render last
    render.set_blend_func(graphics.BLEND_FACTOR_SRC_ALPHA, graphics.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA) -- <- You might want to use graphics.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
    render.draw(predicates.model_transparent, draw_options_world) -- <- model_transparent predicated
    render.disable_state(graphics.STATE_DEPTH_TEST)

Edit: You can set predicates in the init() function of the render script. Also, don’t forget to set the TAG to model_transparent in the .material file of the model.

  self.predicates = create_predicates("tile", "gui", "particle", "model", "model_transparent", "skybox", "debug_text")
3 Likes

It worked! Thank you so much. I wish there would be an easier way of doing thing with Defold, but at least its community makes things easier for newcomers

2 Likes

Yeah models should really have alpha settings, no idea why this hasn’t been added yet

1 Like