• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Surface alpha like without surface, (for region clip)

Joh

Member
Hello,

I would like to clip a portion of my screen, that is: cut off any sprite/text going above the header or below the footer (scrolling involved) in my menu.

Simple enough solution is using surfaces. draw the whole menu on a surface, remove the header & footer with bm_subtract and draw the surface.

It works well enough except the alphas are off. With some research I learned that apparently the clear of 0 alpha impacts the final alpha.
https://www.yoyogames.com/blog/57/explaining-blend-modes-part-2
see last part

I can't think of how to fix it. I tried different blend_modes but I cant wrap my head around which, if any would deliver the desired effect.
Essentially I just want the sprites to Ignore the clear Black alpha 0 background of surface, But still behave normally with respect to all other elements being drawn on it (with varying alphas)

Thank you
 

Joh

Member
You can read this about alpha and surfaces: https://www.yoyogames.com/blog/60/alpha-and-surfaces

Also you could do clipping without surfaces at all, here is some approaches:
Hi, thanks for the response,
The alpha page is pretty much my problem. I use large alpha gradients and the formula makes it impossible to keep the surface alpha up to 1. (always weighted down by 0 alpha back) Tried using opaque "non color" rectangle as suggested but it didn't work.
I feel like I'm missing something because the article is clearly about the issue I'm facing but the solutions wouldn't have the desired effect.
it also ends further proving the logic of why the issue happens, (and I see it, it makes sense) but no way to avoid it.

from the top of my head I can't see how the adjust the alphas because I either use the dest alpha (down weighting my alphas) or I attempt to bypass it which leads to all kinds of messed up drawings (opaque drawings, wrong opacities where theres overlap etc)

Just tried the dynamic rendering mask and it doesn't work because I use a lot of alpha & gradients; everything I draw has full opacity which just doesn't look nice. It also eats the gpu performance since its essentially full screen.
The shader approach I've used before and its not friendly with some native shape drawing for some reasons.
 

Joh

Member
@The Reverend
Thanks for the response and amazing tutorial. I actually had stumbled on it right after my first reply and it seemed like the solution. Extra credit for somehow finding me! Very well explained, supported my understanding and even clarified why some of the solutions that made no sense for me could work. (like fixing the alpha)
Unfortunately, the case that would be the solution for me was the last one. since it required a shader nonetheless (which I wanted to avoid since I'm not too familiar with them) I opted to simply use the Shader clipping from Yal, in the first post. It happened to be simpler.
Still great video and thanks a lot!
 

kraifpatrik

(edited)
GameMaker Dev.
Hey there, I also really needed this for a GUI system and in the end I came up with this blend mode, which allows you to draw transparent sprites, fonts etc. into surfaces without any issues:

GML:
surface_set_target(surface);
draw_clear_alpha(0, 0);
gpu_set_blendmode_ext_sepalpha(bm_src_alpha, bm_inv_src_alpha, bm_one, bm_inv_src_alpha);
// Draw surface content here...
gpu_set_blendmode(bm_normal);
surface_reset_target();
(I can't test it right now, so I hope it's this one. I just copied it from my GitHub.)
 

MusNik

Member
@kraifpatrik Yeah, came up with this recently too đź‘Ť

This will also make your surface alpha-premultiplied so you will need to draw it on screen with this blendmode:
GML:
gpu_set_blendmode_ext(bm_one, bm_inv_src_alpha);
 
  • Like
Reactions: Joh
Top