GMS 2 alpha not working with blend modes?

Discussion in 'Programming' started by bdonohue, Apr 9, 2019.

  1. bdonohue

    bdonohue Member

    Joined:
    Oct 25, 2016
    Posts:
    16
    Hi,

    Is an image's alpha not adjustable when you use blend modes? For example:

    Code:
    gpu_set_blendmode_ext(bm_subtract,bm_src_color);
    draw_sprite_ext(spr_water1, 0, x, y, image_xscale, image_yscale, image_angle, image_blend, 0.2);
    gpu_set_blendmode(bm_normal);
    No matter what value I change the 0.2 to, nothing looks any different.

    Thanks!
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,858
    Binsk likes this.
  3. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,692
    You may also want to review this reference for a good explanation of how blendmodes work in general.

    Blendmodes GML Reference
     
  4. bdonohue

    bdonohue Member

    Joined:
    Oct 25, 2016
    Posts:
    16
    Thank you for the replies :)

    Hmm... ok so bm_subtract is not valid there? I'm confused because the table on that page does not list bm_subtract, but the corresponding graphic below it does list bm_subtract. In fact I see two inconsistencies between the table and graphic... typos maybe?

    Anyway, I tried different values in the gpu_set_blendmode_ext(src,dest) and none of them seem to let me then adjust the alpha of the image.
    Code:
    gpu_set_blendmode_ext(bm_zero,bm_src_color);
    draw_sprite_ext(spr_water1, 0, x, y, image_xscale, image_yscale, image_angle, image_blend, 0.2);
    gpu_set_blendmode(bm_normal);
    Is there any way I can do this?
     
  5. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,692
    You'll probably have to read up to understand why what you're doing won't work.

    Blendmodes are not super complicated, but they aren't intuitive. Essentially they're a series of basic math equations (addition and multiplication) done in a specified order. What you're doing is working, it just isn't what you expected.

    The first paragraph of the link I posted is this:

    "Saying that some modes "do not take the alpha value fully into account" seems a bit misleading. The ways in which alpha is taken into account simply may not be what the user desires or expects."
     
  6. Binsk

    Binsk Member

    Joined:
    Jun 22, 2016
    Posts:
    505
    You don't fully understand how blend modes work.

    When you render something, math happens and you are left with the colors that you want to draw as a result. However, on the screen already are some colors from the last render. How should we mix the colors? That is what the blend mode defines.

    Now, realize that the alpha channel IS CONSIDERED AS PART OF THE COLOR DATA. This means that when you change the blend mode you are changing HOW the alpha is used. It is simply another channel just like red is a channel, green is one, and blue is one. The computer doesn't care what they're for and simply uses them as the blend mode says it should (which, as you are finding out, is not what you'd expect).

    Take a look at the blend mode definitions. Notice how neither of the ones you are using tell the alpha to affect the color channels?

    In case you don't know the math that blend modes do, this is what happens:

    Final color on screen = rendered_color * arg0_blend_command + old_color * arg1_blend_command

    As an example, you are using bm_zero and bm_src_color. Let's assume you are drawing a white square on top of a black background. The math is a follows:

    [1, 1, 1, 0.2] * [0, 0, 0, 0] + [0, 0, 0, 1] * [1, 1, 1, 0.2] = [0, 0, 0, 0.2]

    That would be a black square with 0.2 transparency.
     
    Last edited: Apr 10, 2019
  7. bdonohue

    bdonohue Member

    Joined:
    Oct 25, 2016
    Posts:
    16
    Ok, thank you all very much for the assistance with this! I think I do understand better now. I guess the alpha is technically working, just not as I would expect it to. My background is graphics/Photoshop, so I guess I was expecting blend modes to work the same way they do in Photoshop.

    Thanks!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice