Using colour math with additive blending

Discussion of hardware and software development for Super NES and Super Famicom. See the SNESdev wiki for more information.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
Post Reply
iNCEPTIONAL

Using colour math with additive blending

Post by iNCEPTIONAL »

If I had a boss that looked something like this https://youtu.be/9_xgZqhslSE?t=12913 could I use colour math for semi-transparency like this https://youtu.be/UlKH1_estpw?t=419 but with additive blending [or whatever makes most sense] to allow the whites of his eyes to remain [visually] opaque while the rest of his body would look semi-transparent?

I'm thinking in a similar way to how the beakers and test tubes and the like are done in Pugsley's Scavenger Hunt, where the glass and liquid is semi-transparent but the brightest/whitest highlight areas on the glass are [visibly] opaque: https://youtu.be/tVg8m_BYqcw?t=1249

Basically, I'd like to have a semi-transparent gelatinous boss in my game using one of the background layers, and I'd like to have some parts like his eyes opaque (I say the eyes because they can be pure white, which, if I understand how it works, works best with additive blending to make some parts of the image look opaque while others are semi-transparent), and I just want to know if I can do that using additive blending [or whatever colour math methods makes most sense here]. And I don't want to have to waste sprites to achieve the same effect if I don't need to [at least in some places].

Similarly, if I can do it for black parts of the boss, so those remain [visibly] opaque while everything else looks semi-transparent, then that would be great too, possibly even better. I mean doing it for blacks instead of whites here rather than both at the same time.

Lastly, could this actually be applied on a per tile basis on the layer, so I could mix and match with some parts using additive and some subtractive blending, and some parts with no colour math at all?
creaothceann
Posts: 611
Joined: Mon Jan 23, 2006 7:47 am
Location: Germany
Contact:

Re: Using colour math with additive blending

Post by creaothceann »

Did you understand how additive (or subtractive) blending works? Let's assume we have a base layer, a mainscreen row of red pixels like this:

[00 01 02 10 25 31 20 17 08 03]

The range of values is 5 bit, i.e. 0 (dark) to 2⁵-1 = 31 (bright). We can use color math addition with a subscreen row of red pixels like this:

[00 05 10 20 25 20 10 05 00 00]

As a result we get this:

[00 06 12 30 31 31 30 22 08 03]

Note that:
  • results larger than 31 get clamped to 31
  • any source value < 31 appears to be translucent because it can still be brightened
  • any source value = 31 appears to be opaque because it cannot be brightened further by the other source
The same works for subtraction, just in the other direction:
  • results smaller than 0 get clamped to 0
  • any source value > 0 appears to be translucent because it can still be darkened
  • any source value = 0 appears to be opaque because it cannot be darkened further by the other source
Therefore you can only have bright opaque pixels with addition, and only dark opaque pixels with subtraction.
(Color math works on each color channel (R/G/B) separately.)

A picture with "true" transparency is achieved by enabling the "divide each result by 2" feature.
My current setup:
Super Famicom ("2/1/3" SNS-CPU-GPM-02) → SCART → OSSC → StarTech USB3HDCAP → AmaRecTV 3.10
iNCEPTIONAL

Re: Using colour math with additive blending

Post by iNCEPTIONAL »

creaothceann wrote: Fri Jul 08, 2022 4:34 am Did you understand how additive (or subtractive) blending works? Let's assume we have a base layer, a mainscreen row of red pixels like this:

[00 01 02 10 25 31 20 17 08 03]

The range of values is 5 bit, i.e. 0 (dark) to 2⁵-1 = 31 (bright). We can use color math addition with a subscreen row of red pixels like this:

[00 05 10 20 25 20 10 05 00 00]

As a result we get this:

[00 06 12 30 31 31 30 22 08 03]

Note that:
  • results larger than 31 get clamped to 31
  • any source value < 31 appears to be translucent because it can still be brightened
  • any source value = 31 appears to be opaque because it cannot be brightened further by the other source
The same works for subtraction, just in the other direction:
  • results smaller than 0 get clamped to 0
  • any source value > 0 appears to be translucent because it can still be darkened
  • any source value = 0 appears to be opaque because it cannot be darkened further by the other source
Therefore you can only have bright opaque pixels with addition, and only dark opaque pixels with subtraction.
(Color math works on each color channel (R/G/B) separately.)

A picture with "true" transparency is achieved by enabling the "divide each result by 2" feature.
Well, I don't really understand it at all on any kind of technical and/or numbers level, but it sounds like you are saying I can indeed have parts of the image [visually] semi-transparent and parts of it [visually] opaque, if I use say white plus additive blending in my image for the opaque parts or black plus subtractive blending in my image for the opaque parts, correct?

If so, that's all I really wanted to know, so all is good. :)

And that last thing I want to check, assuming the above is correct, is if this can be applied differently on each tile (so some tiles could use additive and others would use subtractive)--meaning, if I draw the image using the tiles in a smart way, I could actually have opaque light parts in some tiles and opaque dark parts in other tiles, for an overall effect that makes it look like the boss is actually using both opaque white and black at the same time--or if you can only apply it across a whole layer?
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Using colour math with additive blending

Post by dougeff »

The effect you described would be achieved by using sprite palettes 0-3 for opaque portions and sprite palettes 4-7 for transparent portions. (And set color math to add and divide half)
nesdoug.com -- blog/tutorial on programming for the NES
creaothceann
Posts: 611
Joined: Mon Jan 23, 2006 7:47 am
Location: Germany
Contact:

Re: Using colour math with additive blending

Post by creaothceann »

You can also use HDMA to change the color math settings on a scanline-by-scanline basis.
My current setup:
Super Famicom ("2/1/3" SNS-CPU-GPM-02) → SCART → OSSC → StarTech USB3HDCAP → AmaRecTV 3.10
iNCEPTIONAL

Re: Using colour math with additive blending

Post by iNCEPTIONAL »

Well these last two answers sound pretty good to me. Seems like the colour math is more versatile than I thought. I always thought it was just applied to the whole layer or nothing (and same with a whole sprite or nothing, but I presume even there I could use a few objects next to each other to build it and have some of them use the palettes that work with colour math and the others not).
93143
Posts: 1717
Joined: Fri Jul 04, 2014 9:31 pm

Re: Using colour math with additive blending

Post by 93143 »

iNCEPTIONAL wrote: Fri Jul 08, 2022 5:35 amso some tiles could use additive and others would use subtractive
You can't have both additive and subtractive blending at the same time. You can switch it between scanlines with HDMA (and perhaps within a single scanline with an H-IRQ, although this tends to be imprecise/jittery and gets expensive fast), but there's no way to set up different elements of the image to have different types of colour math because the type of colour math (additive/subtractive, halved result yes/no) is a global setting.

The only thing like this that you can do is have some tiles (for a BG layer) or hardware OBJs (for sprites) use colour math and some not. In addition to the palette number thing with OBJs, you can manipulate BG tile priority and sprite priority vs. BG layers to have some elements appear on the main screen and some on the subscreen (among other things, you can do effects kinda like the whole-tile priority shadow/highlight effect on Mega Drive, though not the per-pixel shadow/highlight effect). But this is only to control whether or not colour math is used at all per-element; you can't have more than one type of colour math without dynamically changing the global setting.
Last edited by 93143 on Fri Jul 08, 2022 11:35 am, edited 1 time in total.
iNCEPTIONAL

Re: Using colour math with additive blending

Post by iNCEPTIONAL »

93143 wrote: Fri Jul 08, 2022 11:23 am
iNCEPTIONAL wrote: Fri Jul 08, 2022 5:35 amso some tiles could use additive and others would use subtractive
You can't have both additive and subtractive blending at the same time. You can switch it between scanlines with HDMA (and perhaps within a single scanline with an H-IRQ, although this tends to be imprecise/jittery and gets expensive fast), but there's no way to set up different elements of the image to have different types of colour math because the type of colour math (additive/subtractive, halved result yes/no) is a global setting.

The only thing like this that you can do is have some tiles (for a BG layer) or hardware OBJs (for sprites) use colour math and some not. In addition to the palette number thing with OBJs, you can manipulate BG tile priority and sprite priority vs. BG layers to have some elements appear on the main screen and some on the subscreen (among other things, you can do effects kinda like the whole-tile priority shadow/highlight effect on Mega Drive, thouugh not the per-pixel s/h effect). But this is only to control whether or not colour math is used at all per-element; you can't have more than one type of colour math without dynamically changing the global setting.
Well that's still cool if I can have only parts of a sprite or background using the colour math for semi-transparency while other parts of it don't and are opaque. And, just to be clear, that means I could do something like this but with basically any mixed colour(s) of transparency rather than just shadow/highlight versions of whatever colours are already there, correct: https://youtu.be/SF6s8TB_dJU?t=493
93143
Posts: 1717
Joined: Fri Jul 04, 2014 9:31 pm

Re: Using colour math with additive blending

Post by 93143 »

iNCEPTIONAL wrote: Fri Jul 08, 2022 11:35 amAnd, just to be clear, that means I could do something like this but with basically any mixed colour(s) of transparency rather than just shadow/highlight versions of whatever colours are already there, correct: https://youtu.be/SF6s8TB_dJU?t=493
As was said before, you'd need separate sprites for the opaque part and the transparent part. You don't get per-pixel priority on sprites.

It might help a bit that the SNES has slightly more sprite coverage than the Mega Drive (106.25% rather than 100%), but the lack of flexible sizing per-sprite can really hurt it here too...
iNCEPTIONAL

Re: Using colour math with additive blending

Post by iNCEPTIONAL »

93143 wrote: Fri Jul 08, 2022 11:40 am
iNCEPTIONAL wrote: Fri Jul 08, 2022 11:35 amAnd, just to be clear, that means I could do something like this but with basically any mixed colour(s) of transparency rather than just shadow/highlight versions of whatever colours are already there, correct: https://youtu.be/SF6s8TB_dJU?t=493
As was said before, you'd need separate sprites for the opaque part and the transparent part. You don't get per-pixel priority on sprites.

It might help a bit that the SNES has slightly more sprite coverage than the Mega Drive (106.25% rather than 100%), but the lack of flexible sizing per-sprite really hurts it here too...
"The only thing like this that you can do is have some tiles (for a BG layer) or hardware OBJs (for sprites) use colour math and some not."

But isn't that ^^^ what I am saying, so some parts would use colour math (for semi-transparency) and some not (so they'd be [visibly] opaque), just on a per tile basis rather than per pixel? And then you'd just have to be really good at drawing within the lines of each tile and with tiles next to each other to get some decent shapes out of that (rather than just a bunch of patent opaque and semi-transparent square tiles next to each other).

And isn't the beaker/test tube effect in Pugsley's Scavenger Hunt using the white highlights parts such that they are [visibly] opaque (while the other parts are semi-transparent), which is based on where the pure white pixels are (so that actually is down to the pixel)?

So, can't I combine the two effects by having say one object be simply opaque next to another object that's semi-transparent but with the white pixels using additive blending to actually appear opaque in places too?

What am either missing or being misunderstood on here?

I feel like I'm going to have to quickly mock something up in Photoshop because I don't think people are understanding what I'm actually suggesting.

Give me a minute. . . .
Last edited by iNCEPTIONAL on Fri Jul 08, 2022 12:40 pm, edited 1 time in total.
iNCEPTIONAL

Re: Using colour math with additive blending

Post by iNCEPTIONAL »

OK, so, follow me on this:
TilesColourMathVariations.png
The pink is the default transparent palette colour that is never shown.
Any tiles with purple in them are going to have no colour math applied, so they are fully opaque.
Any tiles with blue in them are going to have colour math applied for semi-transparency, but the one with additional white [plus blue] will use the additive blending so the white actually looks opaque (to the eye), and the ones with black [plus blue] will use subtractive blending so the black also looks opaque (to the eye).

And when placed in/over and actual level, it would look something like this:
TilesColourMathVariationsInLevel.png
TilesColourMathVariationsInLevel.png (16.44 KiB) Viewed 623 times
And look at the top image, I'm not mixing different blending methods in the same tile. I'm using some tiles with no colour math and some with colour math (applied in two different ways: additive in some tiles, subtractive in others), and I'm sticking them altogether to form one large character. But there a no tiles placed on top of other tiles or sprites placed on top of other sprites here (depending on if it's made out of a background or sprites), so no waste.

Does that make sense?

Is that possible?
Last edited by iNCEPTIONAL on Fri Jul 08, 2022 12:55 pm, edited 3 times in total.
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Using colour math with additive blending

Post by Dwedit »

This example appears to be fixed 50% transparency, plus overlays for the other parts. So 50% mode would work, then you overlay the opaque parts on top of it.

Which image editor are you using? Many of them let you use Add or Subtract blending modes.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
iNCEPTIONAL

Re: Using colour math with additive blending

Post by iNCEPTIONAL »

Dwedit wrote: Fri Jul 08, 2022 12:40 pm This example is one level of fixed transparency, plus overlays for the other parts. 50% mode would work, then you overlay the opaque parts on top of it.
No, the idea I'm trying to propose involves NOT overlaying other sprites on top. It's just tiles next to other tiles but with different blending done on different tiles, all placed next to each other to form one larger character, so the illusion is that the one character uses multiple types of blending.

Can you read it one more time, please, because I'm think I'm explaining what I'm asking clearly.

I used Photoshop to quickly mock it up.

The exact look is not important. I'm just asking if I can have a tile that uses plain add+half next to a tile that uses add+additive (or however that works to make the white bits opaque but the rest not) and next to tile that uses add+subtractive (or however that works to make the black bits opaque but the rest not), and then moving them all around together like one object.
Last edited by iNCEPTIONAL on Fri Jul 08, 2022 12:52 pm, edited 2 times in total.
Joe
Posts: 650
Joined: Mon Apr 01, 2013 11:17 pm

Re: Using colour math with additive blending

Post by Joe »

Photoshop should be able to approximate the SNES blending modes if you change the layer's blend mode to "linear dodge" for additive blending and "subtract" for subtractive blending. Leave the layer's opacity at 100%. You'll probably want to invert the colors on the layer you set to subtractive blending.

Keep in mind the blend mode is set per line, not per tile.
iNCEPTIONAL

Re: Using colour math with additive blending

Post by iNCEPTIONAL »

Joe wrote: Fri Jul 08, 2022 12:47 pm Photoshop should be able to approximate the SNES blending modes if you change the layer's blend mode to "linear dodge" for additive blending and "subtract" for subtractive blending. Leave the layer's opacity at 100%. You'll probably want to invert the colors on the layer you set to subtractive blending.

Keep in mind the blend mode is set per line, not per tile.
Again, I'm not trying to get the exact look; that's not my concern right now. I'm just asking if I can build a single character out of multiple tiles or objects next to each other (without using objects on top of one another), where some of them use simple add+half, some use additive blending to make the white appear [visibly] opaque and some use subtractive blending to make the black parts appear [visibly] opaque, and then moving them around altogether to create the illusion of a single large character that has multiple types of blending going on.
Post Reply