drawing only on transparent pixels using blendmode() ?

Is there a way to draw only on transparent parts of an image using blendmode()?

I found the blendmodes thread by Jvm38, but I still don’t quite understand how the multiple parameters work together. Big thanks to jvm38 for the thread though, I found a much faster erase function than my own manual solution!

this shoul do what you want


note that


color = sum of the 2 colors after multiplication by the coefficients above
alpha = opacity = sum of the 2 alphas after multiplication by the coefficients above

@Jmv38 What would the blendmode be for setting black pixels (0,0,0,255) to alpha zero?

the back pixels of the SOURCE image or the DESTINATION image?

The image that you’re about to draw (is that source or destination?)

Edit: it’s the source

I’m not sure the effect I’m after is possible with blendMode, not without messing up the colour values of the other pixels. At the moment, I use a shader to make black pixels invisible, adding this line to the frag:

col.a = smoothstep(0.,0.1, (col.r + col.g + col.b)); //set alpha to zero for black

i.e. effectively treating black and near-black as an alpha channel.

But I was curious as to whether the same effect was possible just with blendMode.

Thanks jvm! The suggested blendmode didn’t do what I wanted (or I may have used it wrong), but thanks to your second post I was able to figure out (or at least stumble upon) the effect I was after!


This appears to draw only on the transparent pixels of the DST image and draws the SRC colors as they are given.

In my understanding it’s:

SRC = the image you are drawing
DST = the image you are drawing over

Which then together make the output color, which is the eventual resulting color made by multiplying/blending SRC and DST in the way you specify. This will show on the screen.

I’m sure I will be corrected in case I am completely wrong. =)

@kirl good to know you could manage it yourself with my little summary!
When i first decided to use blendmode(), it took me weeks before i felt i finally understood it.

just curious: is there any chane of faking floodfill with blendmodes, so one color gets replaced by another?

I remember, I tried to make myself a spritesheet with all blendmodes available, but noone had worked as a solution… (spritesheet was done with using every blendmode with every other, like with a for loop, kinda bruteforce method but it worked for a overview)

can you give a pecise example of what you mean by floodfill ? If adding colors is involved then yes. If comparison is involved then no. You have to write a shader for this.

If you’re talking about the kind of floodfill used in image programs, which fills just the current area and stops when it hits a different colour, I don’t think shaders would do, you have to iterate. I have code for that.