Shaders Looking for a Multiply Shader

R

Rackover

Guest
Hi,
The artist i'm working with applied a cool effect on his photoshop mockup using a layer set with the blend mode "Multiply".

As I already managed to create an Overlay blend mode shader, I'm looking forward to have a multiply one as well ; problem is, finding good documentation about it seems much more complicated.

May someone please lend me a Multiply blend mode shader or take me to a page where I can at least get some informations about it ?

I'm not willing to use the draw_set_blend_mode stuff, as I must stay with GLSL shaders for now.

Thanks
 

Binsk

Member
I don't understand why you would give up the easier and more effective way of multiplying two colors together and instead demand to use the complexity of a shader. You do realize blend modes would only require two lines of code? One to set it, one to reset it after drawing.

Regardless, if you want to mimic the "multiply" affect the formula is as simple as the name: final_color = first_color * second_color
When you write a shader, you are provided with gm_BaseTexture (or whatever the variable name is) which is the texture from the layer you are trying to draw. Next you would just need to pass in a sampler2D uniform to the fragment shader that contains the other layer. In the case of the multiply effect, you can swap the two and it doesn't matter.

In the fragment, just sample the point from the sampler relevant to the current fragment and multiply the values together to get your final result. This should be all channels, including alpha. That's it, really. I assume you know how to do the majority of this since you already wrote the overlay shader.
 

obscene

Member
I don't understand why you would give up the easier and more effective way of multiplying two colors together and instead demand to use the complexity of a shader. You do realize blend modes would only require two lines of code? One to set it, one to reset it after drawing.

Regardless, if you want to mimic the "multiply" affect the formula is as simple as the name: final_color = first_color * second_color
When you write a shader, you are provided with gm_BaseTexture (or whatever the variable name is) which is the texture from the layer you are trying to draw. Next you would just need to pass in a sampler2D uniform to the fragment shader that contains the other layer. In the case of the multiply effect, you can swap the two and it doesn't matter.

In the fragment, just sample the point from the sampler relevant to the current fragment and multiply the values together to get your final result. This should be all channels, including alpha. That's it, really. I assume you know how to do the majority of this since you already wrote the overlay shader.
Is there a way to get a multiply effect from blend modes? I thought there wasn't!
 
Is there a way to get a multiply effect from blend modes? I thought there wasn't!
Code:
draw_set_blend_mode_ext(bm_dest_colour,bm_zero);
(Rs,Gs,Bs,As)*(Rd,Gd,Bd,Ad)+(Rd,Gd,Bd,Ad)*(0,0,0,0)
Or
Code:
draw_set_blend_mode_ext(bm_zero,bm_src_colour);
(Rs,Gs,Bs,As)*(0,0,0,0)+(Rd,Gd,Bd,Ad)*(Rs,Gs,Bs,As)
 
Code:
draw_set_blend_mode_ext(bm_dest_colour,bm_zero);
(Rs,Gs,Bs,As)*(Rd,Gd,Bd,Ad)+(Rd,Gd,Bd,Ad)*(0,0,0,0)
Or
Code:
draw_set_blend_mode_ext(bm_zero,bm_src_colour);
(Rs,Gs,Bs,As)*(0,0,0,0)+(Rd,Gd,Bd,Ad)*(Rs,Gs,Bs,As)
When the source (or probably also the destination) contain partial alpha, both of these produce different results from what you get in photoshop. Also, they produce different results depending on whether you use (bm_dest_colour,bm_zero) or (bm_zero,bm_src_colour).
 
Last edited:
When the source (or probably also the destination) contain partial alpha, both of these produce different results from what you get in photoshop.
There will be some setup involved depending on what effect you want...

I don't have Photoshop but I do have Gimp and to recreate the effect, all I had to do was this:
Code:
//already verified surfaces exist and stuff

//clear surface black with 0 alpha (0,0,0,0). surfaces will allow editing alpha to help simulate layers.
surface_set_target(layer0);
draw_clear_alpha(c_black,0);

//draw sprite to bottom layer
draw_sprite(background,0,0,0);

//setup sprite for multiplying
surface_set_target(layer1);
draw_clear_alpha(c_white,0);
draw_sprite(sprite,0,0,0);
surface_reset_target();

//set blend mode and stop editing alpha
draw_set_blend_mode_ext(bm_zero,bm_src_colour); //or draw_set_blend_mode_ext(bm_dest_colour,bm_zero); both work the same.
draw_set_colour_write_enable(true, true, true, false);

//merge layers
draw_surface(layer1,0,0);

//reset
draw_set_blend_mode(bm_normal);
draw_set_colour_write_enable(true, true, true, true);
surface_reset_target();

//draw surface to screen
draw_surface(layer0,0,0);
Really though, if I were going to use the multiply effect, I personally would choose between only working on transparency or only working with color.

Also, they produce different results depending on whether you use (bm_dest_colour,bm_zero) or (bm_zero,bm_src_colour).
I tested this and this is false (at least for me). They produce exactly the same result. With underlying blend mode math, it doesn't make sense that they would give different results.

-----------------------------------------------------------------------------

@Rackover - this is for you. Sorry for going off topic.

Looking at what I think is the Photoshop formulas, my head hurts a little bit but I guess it works.
Formulas start at Pg 322 - 326 and color space affects results.
 
Last edited:
Top