Windows [Resolved] Not sure how to use image alpha channel for transparency.

Discussion in 'Programming' started by Nik, Jul 15, 2019.

  1. Nik

    Nik Member

    Joined:
    Jan 21, 2017
    Posts:
    52
    Just need some quick help on how to use the alpha channel in an image to function as the opacity mask in the draw event using gpu_set_blendmode_ext()? I haven't tried every combination but I'm either seeing the entire image (as if the alpha channel is all white), or as a translucent blend, or all black. :bash:
     
  2. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,169
    Please explain what you're trying to do in more detail.
     
  3. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    You should use gpu_set_blendmode_ext_sepalpha(source, destination, source alpha, destination alpha). As you could glean from the name, this function works exactly like gpu_set_blendmode_ext but gives you even finer control by letting you specify separate blend modes for the alpha values.
    More specifically, the combination of blendmodes you are looking for is

    Code:
    gpu_set_blendmode_ext_sepalpha(bm_zero, bm_one, bm_one, bm_zero);
    
    Because, from your description, you want the already existing (destination) color to be filtered by the mask's (source) alpha. So this means that each resulting pixel should be (Rd, Gd, Bd, As).
    The first bm_zero discards the source colour values, while the first bm_one keeps the destination color. Likewise, the second bm_one keeps the source alpha value while bm_zero discards the destination alpha value.
    Thus...
    (0, 0, 0, As) + (Rd, Gd, Bd, 0) = (Rd, Gd, Bd, As)
     
  4. Nik

    Nik Member

    Joined:
    Jan 21, 2017
    Posts:
    52
    I have a 32-bit BMP image (8-bit R, 8-bit G, 8-bit B, 8-bit A) and I am trying to use the alpha channel (8-bit A) as a transparency mask.

    You can see the alpha channel as bright red when selected in Photoshop. I'm trying to make the red area transparent and the white center of the image opaque.

    [​IMG]


    So I tried the code above and the image was completely transparent - no black edges, but also no galaxy at the center. I'm starting to wonder if GMS2 strips the alpha channel out of images?

    EDIT 1: So far the closest I've come to getting this thing to appear even remotely correct is to use gpu_set_blendmode(bm_add) - I just wish it wasn't so bright and glowy.

    EDIT 2: I'm still trying to mix and match blend modes and I found one that is *ALMOST* correct...
    Code:
    gpu_set_blendmode_ext_sepalpha(bm_src_color,bm_inv_dest_color,bm_src_alpha,bm_inv_dest_alpha);
    [​IMG]
    This renders the image with normal brightness and a very faint translucent black background - if I can get it to maintain brightness and eliminate the black background altogether, it will be a win.

    EDIT 3: I've included a screenshot of how the files are being saved and links to download the two assets that I am using in case anyone wants to try and figure out how to render this properly.[​IMG]
    http://nik.mohilchock.com/images/BG.bmp [Background] and http://nik.mohilchock.com/images/SG.bmp [Spiral Galaxy]
     
    Last edited: Jul 16, 2019
  5. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,169
    You mention alpha masks, but it sounds like what you are trying to do is to draw the picture normally, but to reduce the background alpha to zero. If that is the case, it sounds like a job for an image editor.
     
  6. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    Could you post some of your actual code, also, what are you trying to mask?
     
  7. Nik

    Nik Member

    Joined:
    Jan 21, 2017
    Posts:
    52
    Aside from the X/Y Scale and Alpha variables in the create event, this is the ONLY code:
    Code:
    /* STEP EVENT */
    if f_SpiralGalaxy_Scale < 1.0 { f_SpiralGalaxy_Scale += 0.01 }
    if f_SpiralGalaxy_Alpha < 1.0 { f_SpiralGalaxy_Alpha += 0.01 }
    /* DRAW EVENT */
    // Render Galaxy Map
    if !gpu_get_blendenable() { gpu_set_blendenable(true) }
    // gpu_set_blendmode_ext_sepalpha(bm_src_color,bm_inv_dest_color,bm_src_alpha,bm_inv_dest_alpha);
    // gpu_set_blendmode(bm_add);
    gpu_set_blendmode_ext_sepalpha(bm_one,bm_one,bm_zero,bm_one);
    // Fade Galaxy into view and expand to 1.0 Scale
    draw_sprite_ext(spr_SpiralGalaxy,0,room_width/2,room_height/2-10,f_SpiralGalaxy_Scale,f_SpiralGalaxy_Scale,0,c_white,f_SpiralGalaxy_Alpha);
    gpu_set_blendmode(bm_normal);
    
    EDIT: The project is literally just a room (rm_MapRoom) and an object (obj_Map) right now. 'rm_MapRoom' is using bkg_Space (BG.bmp) as the background, and obj_Map is using spr_SpiralGalaxy (SG.bmp) in the DRAW event (the object has not been assigned a sprite).
     
    Last edited: Jul 16, 2019
  8. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    So what you're looking to do is just draw the sprite... you don't need blend modes to do that. You just draw the sprite. If you have issues with that, it's because you're not saving the image correctly. In fact, if it is true that the image you have linked above is the same exact image you are using in your project, then that image DOES NOT have an alpha channel at all.

    Look:
    [​IMG]
    I then add an alpha channel and erase part of the picture just for illustrative purposes. Notice I am using the same options you showed.

    [​IMG]

    I can then import this file to GameMaker with no issues:
    [​IMG]
    I can then draw it with no issues:

    [​IMG]
    So I believe the problem is with the way you export your image, there must be a step you're messing up.
     
  9. Nik

    Nik Member

    Joined:
    Jan 21, 2017
    Posts:
    52
    Ok, I'll try playing around with the file settings and see if I can figure out what I'm doing wrong.
     
  10. Biosyn

    Biosyn Member

    Joined:
    Oct 17, 2017
    Posts:
    43
    General tip: if you're trying to extract an image with a single solid background and partial transparency around the edges preserved, use Greenfish icon editor. Check its Remove matte option which allows you to pick the background color you want removed while maintaining transparent edges. Then Export as png. No blending needed.
     
    Nik likes this.

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