Skip to content
Advertisement

OpenGL Alpha values having no effect in shader

I have a simple shader to make scanlines in my 2d game which works fine and is as follows:

JavaScript

But if I change the alpha value in the vec4 for the fragment colour, it has no effect, the scanlines are equally black at 0.1 as they are at 1.0. I saw some other questions regarding this which advised to enable blending, but I tried that to no avail.

This is my render method with blending enabled.

JavaScript

Advertisement

Answer

This isn’t a blending problem. The issue is your shader only draws either a translucent black pixel, or the color of whatever texture region is being drawn. These black pixels are just getting blended with what’s already on the screen (in this case the black clear color).

I assume what you actually want here is for the scan lines not to be pure black. So you should be drawing the color of the texture region everywhere, and just darken it slightly where the scan lines are. You don’t want to be modifying the alpha that the shader outputs, or when you have overlapping sprites, the scanline will appear darker in that area.

So change your shader like this:

JavaScript

However, if/else should be avoided in fragment shaders because it performs significantly worse in most cases. (Exception is if the if statement evaluates to the same value for many pixels in a row, such as with if (u_someUniformBoolean).) You could rewrite it like this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement