Jump to content


Photo

Subtractive Blending Isn't Quite Subtractive ?


  • Please log in to reply
13 replies to this topic

#1 ragarnak

ragarnak

    GMC Member

  • Retired Staff
  • 19468 posts
  • Version:GM8

Posted 21 November 2007 - 12:10 PM

Well, I thought I had a nice idea : Subtract one image from the other, and you should get, when the images are the same, a full black image.

Alas, when I tried it the image looked black, but wasn't. A fact that becaume clear when I did, additive, draw a gray rectangle to the "black" result, and I saw al kinds of distinctive "ghost" colors.

Question : is this a known issue, or is this normal behaviour for blending ?

Edited by ragarnak, 21 November 2007 - 11:14 PM.

  • 0

#2 torigara

torigara

    GMC Member

  • GMC Member
  • 6507 posts

Posted 21 November 2007 - 02:45 PM

Subtracting and maximum blending do not take the alpha value into account. It means that the transparent mask would have gone as well. Therefore the sprite is drawn with no transparency, "subtracting" its background color around the image. You have to paint the background of the sprite black to make it work properly.
  • 0

#3 Tarik

Tarik

    GMC Member

  • GMC Member
  • 2729 posts
  • Version:GM:Studio

Posted 21 November 2007 - 04:14 PM

For a totally black sprite, as far as I know you can simply do this.

draw_sprite_ext(sprite_index,-1,x,y,1,1,0,c_black,1)
  • 0

#4 grandhighgamer

grandhighgamer

    Village Idiot

  • GMC Member
  • 3439 posts
  • Version:GM:Studio

Posted 21 November 2007 - 04:50 PM

Sounds more like a DirectX issue.
  • 0

#5 ragarnak

ragarnak

    GMC Member

  • Retired Staff
  • 19468 posts
  • Version:GM8

Posted 21 November 2007 - 05:47 PM

Subtracting and maximum blending do not take the alpha value into account.

<{POST_SNAPBACK}>

Well, I used (as far as I can tell) a "background" image that was not transparent, nor drawn as such. Either of them should get rid of that problem ?

You have to paint the background of the sprite black to make it work properly.

Trying ... Nope, that does not seem to be it.

@Tarik:
The black image is not the deseired result, but just a (possible) "half-product" : It should be that color when the two images are the same.
  • 0

#6 EyeGuy

EyeGuy

    GMC Member

  • New Member
  • 832 posts

Posted 21 November 2007 - 06:12 PM

I don't believe subtractive blending is truly subtractive, but rather is equivilant to the blend mode:
draw_set_blend_mode_ext(bm_zero,bm_inv_src_color);

  • 0

#7 ragarnak

ragarnak

    GMC Member

  • Retired Staff
  • 19468 posts
  • Version:GM8

Posted 21 November 2007 - 06:40 PM

I don't believe subtractive blending is truly subtractive, but rather is equivilant to the blend mode:

draw_set_blend_mode_ext(bm_zero,bm_inv_src_color);

<{POST_SNAPBACK}>

I just checked, and there is visually no difference between the results of two.

But I do not quite understand the relevance of your statement : is there something the matter with the way "bm_inv_src_color" works ?
  • 0

#8 EyeGuy

EyeGuy

    GMC Member

  • New Member
  • 832 posts

Posted 21 November 2007 - 07:03 PM

See, true subtraction would be {(nr,nb,ng) are the color resulting from the blend, (or,ob,og) are the old colors, (sr,sb,sg) are the source colors}:
nr = or - sr
nb = ob - sb
ng = og - sg

However, that blend mode will give you:
nr = sr * 0 + or * (1 - sr) = or - or*sr
nb = sb * 0 + ob * (1 - sb) = ob - ob*sb
ng = sg * 0 + og * (1 - sg) = og - og*sg

The difference is subtle, but it makes a difference. Basically, the color of the old image effects how much will be subtracted from it, so though white will subtract all of white, 1/2 grey will only subtract 1/2 of 1/2, so it'll result in 1/4.

Edited by EyeGuy, 21 November 2007 - 07:05 PM.

  • 0

#9 ragarnak

ragarnak

    GMC Member

  • Retired Staff
  • 19468 posts
  • Version:GM8

Posted 21 November 2007 - 11:13 PM

Basically, the color of the old image effects how much will be subtracted from it, so though white will subtract all of white, 1/2 grey will only subtract 1/2 of 1/2, so it'll result in 1/4.

<{POST_SNAPBACK}>

Thanks for that explanation. Yes, that I did not expect.

Well, there goes a perfectly good idea down the drain ! :D :D
  • 0

#10 paul23

paul23

    GMC Member

  • Global Moderators
  • 3684 posts
  • Version:GM:Studio

Posted 22 November 2007 - 12:15 AM

well this seems very much like a problem xot had a few weeks ago: gm automatically adds a few "extra" pixels around the current sprite to blend nicely. However the colour of those pixels isn't retrievable..
  • 0

#11 EyeGuy

EyeGuy

    GMC Member

  • New Member
  • 832 posts

Posted 22 November 2007 - 06:40 PM

Actually, that's completely unrelated to this problem.

Yea, it sucks bm_subtract doesn't work that way, it'd be pretty powerful. I was experimenting with it when someone (GearGod?) was looking for a way to store several alpha channels on a single image (i.e. applied real-time), but it couldn't be used the way I wanted cause of it.
  • 0

#12 ws25

ws25

    Gaming For Geeks

  • New Member
  • 724 posts

Posted 22 November 2007 - 06:51 PM

I find that subtraction is pretty glitchy for anything other than shades of grey.
  • 0

#13 GearGOD

GearGOD

    Deus Verus

  • GMC Member
  • 2153 posts

Posted 24 November 2007 - 12:28 AM

Yeah. This is the sort of thing where shaders really shine.

return float4(dest.rgb-source.rgb, max(source.a, dest.a));
  • 0

#14 ws25

ws25

    Gaming For Geeks

  • New Member
  • 724 posts

Posted 24 November 2007 - 01:53 AM

In a more positive light:

From a different perspective, this trait of blending is good for reducing/increasing contrast. Ex: take image A. You subtract image A from A, and it darkens all the dark sections to really dark and keeps the light sections fairly light. Or, say you want to draw a white sphere glow onto the screen. You could use a glow sprite as an alpha map for a white square, or simply bm_add the sprite onto the screen. This also makes nice meta-ball-ish effects that don't happen with alpha masks. I think GearGOD used a multi-pass blend_mode_ext() to do a metaball effect? Am I right? I'd check, but MessyMind appears to be down right now.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users