• 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!

Legacy GM Blend two sprites by alpha

R

Rodimus

Guest
hello!

I want to draw a sprite, and on top of that another, but I want the second sprite drawn only where the fist sprite's has pixels, and cut where the first sprite is transulect

I look into the manual, but I became more confused.
 

GMWolf

aka fel666
I use GMS 1.4, so the functions are different. I looked at it, but don't help.
For most of those, just replace gpu_whatever with draw_whatever.
Do a little googling or browsing of the manual to find the equivalent functions.
 
R

Rodimus

Guest
For most of those, just replace gpu_whatever with draw_whatever.
I dod, not worrking. I did this:

draw_sprite(sprite1,0,x,y);
draw_set_blend_mode_ext(bm_dest_alpha, bm_inv_src_alpha);
draw_sprite(sprite0,0,x,y);
draw_set_blend_mode(bm_normal);

where I hope it draws part of sprite0 where sprite1 not transulect.
 

GMWolf

aka fel666
I dod, not worrking. I did this:

draw_sprite(sprite1,0,x,y);
draw_set_blend_mode_ext(bm_dest_alpha, bm_inv_src_alpha);
draw_sprite(sprite0,0,x,y);
draw_set_blend_mode(bm_normal);

where I hope it draws part of sprite0 where sprite1 not transulect.
yes. did you not read the blog post! its not just a blendmode...
you have to use draw_set_colour_write_enable, etc.

there is no such thing as free lunch. Read the post, understand what is going on, implement it.
 
R

Rodimus

Guest
yes. did you not read the blog post! its not just a blendmode...
you have to use draw_set_colour_write_enable, etc.
Yes i did, but in GMS 1.4 there's only draW_set_alpha_test() and that don't make any difference
 
R

Rodimus

Guest
yes. did you not read the blog post! its not just a blendmode...
you have to use draw_set_colour_write_enable, etc.

there is no such thing as free lunch. Read the post, understand what is going on, implement it.
"it's not a free lunch, simply not working what you linking. I posted the LAST thing i tried, but it doesn't mean that i didn't try what you suggested earlier."
 

GMWolf

aka fel666
"it's not a free lunch, simply not working what you linking. I posted the LAST thing i tried, but it doesn't mean that i didn't try what you suggested earlier."
Well, GMS does have draw_set_colour_write_enable, so when you claim it doesn't, IDK what you tried...
it is a somewhat recent addition, so make sure you update.
 
R

Rodimus

Guest
Well, GMS does have draw_set_colour_write_enable, so when you claim it doesn't, IDK what you tried...
it is a somewhat recent addition, so make sure you update.
I never saw this before, but I look forward it, thanks!
 
R

Rodimus

Guest
So you didn't type it into Game Maker then...
the feature has been there for a small while. You can already use it.
Yes I did after you mentioned it, but looks like it not working. I didn't know what i did wrong.
 

GMWolf

aka fel666
Yes I did after you mentioned it, but looks like it not working. I didn't know what i did wrong.
describe not working?
Does the function not show up? does it not render as expected?

To be fair, I highly doubt you had the time to study the blog post in the time between responses. There is quite a lot going on.
Can you confidently say you understand the reasoning behind the code? or did you just copy paste without giving it any thought?

note: be honest, I'm trying to help. Maybe I can explain it to you. (but note that the blog post already epxlains it better than i could)
 
R

Rodimus

Guest
describe not working?
Does the function not show up? does it not render as expected?

To be fair, I highly doubt you had the time to study the blog post in the time between responses. There is quite a lot going on.
Can you confidently say you understand the reasoning behind the code? or did you just copy paste without giving it any thought?

note: be honest, I'm trying to help. Maybe I can explain it to you. (but note that the blog post already epxlains it better than i could)
As a stated above, i found the blogpost earlier this day. I not checked in the manual the function you mentined, because I didn't even know it exists in GMS 1.4 I posted a snipet, and I know, what i doing. But it not working the way I expecting.

I read up what is the source what is the destination, and i use the correct blend modes. I used alphatest, write, etc. but the result is the same! You right, I not descibe what went wrong. The result is not what I expected. I excepted the second sprite only draw where the first is not transulect, but instead they are on each other.
 

GMWolf

aka fel666
As a stated above, i found the blogpost earlier this day. I not checked in the manual the function you mentined, because I didn't even know it exists in GMS 1.4 I posted a snipet, and I know, what i doing. But it not working the way I expecting.

I read up what is the source what is the destination, and i use the correct blend modes. I used alphatest, write, etc. but the result is the same! You right, I not descibe what went wrong. The result is not what I expected. I excepted the second sprite only draw where the first is not transulect, but instead they are on each other.
you are going to have to post your code, along with perhaps a screenshot of the result so that we can help.
Its hard to tell where you went wrong, there are a multitude of possibilities.

Also, the posts above dont seem to agree with your latest statement, but eh, whatever.
 
R

Rodimus

Guest
upload_2018-4-22_14-9-29.png this is the image i want to "cut" whith the second sprite. (sprite0)
upload_2018-4-22_14-10-6.png this is the silhuette of the sprite i want to use as a mask In gms the black parts are removed in the sprite editor. (sprite1)

This is the LATEST code I used in the draw event of the test object:
draw_sprite(sprite1,0,x,y);
draw_set_blend_mode_ext(bm_src_color, bm_inv_dest_alpha);
draw_sprite(sprite0,0,x,y);
draw_set_blend_mode(bm_normal);

where i belive the destination part is sprite1 and the source is sprite0, so I grabbed sprite 1 color parts and blend with the inversed sprite1 alpha
 

Attachments

GMWolf

aka fel666
View attachment 17907 this is the image i want to "cut" whith the second sprite. (sprite0)
View attachment 17908 this is the silhuette of the sprite i want to use as a mask In gms the black parts are removed in the sprite editor. (sprite1)

This is the LATEST code I used in the draw event of the test object:
draw_sprite(sprite1,0,x,y);
draw_set_blend_mode_ext(bm_src_color, bm_inv_dest_alpha);
draw_sprite(sprite0,0,x,y);
draw_set_blend_mode(bm_normal);

where i belive the destination part is sprite1 and the source is sprite0, so I grabbed sprite 1 color parts and blend with the inversed sprite1 alpha
again, why dont you use the code in the blog post? blend modes wont do what you want!

Try the code from the blog post, and if it doesn't work post that code so i can help.
 
Last edited:

GMWolf

aka fel666
Hold up, I think I majorly derped.

You will need to make a few modifications to the code, so that you only draw the mask, without actually drawing the sprite.
This is importat so you get the stencil effect you are looking ofr.

Shaders may well be far easier, and its also a good introduction to glsl.
 
R

Rodimus

Guest
Hold up, I think I majorly derped.

You will need to make a few modifications to the code, so that you only draw the mask, without actually drawing the sprite.
This is importat so you get the stencil effect you are looking ofr.

Shaders may well be far easier, and its also a good introduction to glsl.
Okay, I didn't even look at shaders, but used them in the past.
 

GMWolf

aka fel666
Okay, I didn't even look at shaders, but used them in the past.
ok, in that case its pretty simple:
Add a sampler uniform for you alpha texture.

Then, discard any pixels that are dark from that texture:
Code:
if (texture(alpha_map, texcoord) < 0.5) {
 discard;
}
 
R

Rodimus

Guest
Okay, the example on the blog is now working, but I don't understand the part with the draw rectangle. It's looks diificult, and expensive, so I looking for a shader.
 
R

Rodimus

Guest
ok, in that case its pretty simple:
Add a sampler uniform for you alpha texture.

Then, discard any pixels that are dark from that texture:
Code:
if (texture(alpha_map, texcoord) < 0.5) {
 discard;
}
I'll try it looks better than blending.
 

GMWolf

aka fel666
Okay, the example on the blog is now working, but I don't understand the part with the draw rectangle. It's looks diificult, and expensive, so I looking for a shader.
This will depend on taget hardware. On older hardware, the blog post solution may be faster.
On newer hardware, a shader is the way to go.
have a look at my previous post to get you started, its pretty simple.
 

GMWolf

aka fel666
If you need to actually use the alpha of the texture, trather than have a clear cut edge, you can use this:
Code:
gl_fragColor.rgb = texture(whatever_it_is_using_gm, texcoord);
gl_fragColor.a = texture(alpha_map, texcoord);
 
R

Rodimus

Guest
okay texture() not working in GLSL ES on my pc and GLSL not compiling. under GLSL ES it's not find the overloaded function which using two parameters.
 
R

Rodimus

Guest
okay texture() not working in GLSL ES on my pc and GLSL not compiling. under GLSL ES it's not find the overloaded function which using two parameters.
okay texture() is the old version I need texture2D
 

GMWolf

aka fel666
okay texture() is the old version I need texture2D
actually, you will find that texture2D is the old version, and glsl now uses texture().
GameMaker still uses old versions of GLSL. I completely forgot about that :rolleyes:

you will also find that 'whatever_it_is_using_gm' is not valid either. I just cannot remember what arbitrary name GM decided on.
 
R

Rodimus

Guest
actually, you will find that texture2D is the old version, and glsl now uses texture().
GameMaker still uses old versions of GLSL. I completely forgot about that :rolleyes:

you will also find that 'whatever_it_is_using_gm' is not valid either. I just cannot remember what arbitrary name GM decided on.
gm_BaseTexture
GLSL ES not know texture() GLSL not compile on my computer. I double checked everything, texture sampler not working too, so i think, t's time to admit this is as far I can go.

I give up. Thanks for your time, help and patiency.
 
R

Rodimus

Guest
Fragment shader
//
//Shader combine
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform sampler2D texture1;

void main()
{
gl_FragColor = vec4(texture2D( texture1, v_vTexcoord ).r,texture2D( texture1, v_vTexcoord ).g,texture2D( texture1, v_vTexcoord ).b,texture2D( gm_BaseTexture, v_vTexcoord ).a);
}

Create event
_tex1 = shader_get_sampler_index(shader_skin,"texture1");

Draw event
if (shader_is_compiled(shader_skin))
{
_spr = sprite_get_texture(sprite0,0);
shader_set(shader_skin);
texture_set_stage(_tex1,_spr);
draw_sprite(sprite0,0,x,y);
shader_reset();
}

That's all.
 

GMWolf

aka fel666
Fragment shader
//
//Shader combine
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform sampler2D texture1;

void main()
{
gl_FragColor = vec4(texture2D( texture1, v_vTexcoord ).r,texture2D( texture1, v_vTexcoord ).g,texture2D( texture1, v_vTexcoord ).b,texture2D( gm_BaseTexture, v_vTexcoord ).a);
}

Create event
_tex1 = shader_get_sampler_index(shader_skin,"texture1");

Draw event
if (shader_is_compiled(shader_skin))
{
_spr = sprite_get_texture(sprite0,0);
shader_set(shader_skin);
texture_set_stage(_tex1,_spr);
draw_sprite(sprite0,0,x,y);
shader_reset();
}

That's all.
the code looks ok.
you say the shader isnt compiling? what graphics hardware do you have? If its too old for shaders, I'm affraid you will have to use the method outlined in the blogpost.
That being said, there is very little hardware these days that doesn't support shaders.

oh, also, remember that your UV's wont match between sprite, so you have to set each sprite to use a separate texture page / use for 3D.


######
if (shader_is_compiled(shader_skin))
{
_spr = sprite_get_texture(sprite0,0);
shader_set(shader_skin);
texture_set_stage(_tex1,_spr);
draw_sprite(sprite0,0,x,y);
shader_reset();
}
Notice how you are using sprite0 twice?
you should use sprite_get_texture with the color map, and use draw_sprite with you alpha mask.
 
R

Rodimus

Guest
Oh, I did wrong. My hardware works fine with GLSL ES shaders, but I get an error message with GLSL setting. I do the UV.
 
Top