Alpha behaviour when using drawAtlas with kPlus blending mode

25 views
Skip to first unread message

Ats

unread,
Sep 28, 2021, 12:47:19 PMSep 28
to skia-discuss
I'm using drawAtlas to draw a bunch of alpha blended sprites, I would like some of those sprites to be additively highlighted by certain colors. To achieve that I pass the colors to drawAtlas method along with kPlus blending mode. It mostly works, the sprites are brightened by the colors, but the problem is it also colors the transparent part of the sprites. When I use colors with the alpha bit set to 0, the sprite is not affect at all.

Here is a modified Skia Fiddle drawAtlas example: https://fiddle.skia.org/c/69cef0d64cd60f82a70ce85cfefe3ceb
On the top left you can see the result of using 0xffff0000 as a color, on the bottom right you can see the result of using 0x00ff0000 as a color. What I would like to achieve is top left, without the yellow background.

Can this be achieved with Skia?

Brian Osman

unread,
Sep 28, 2021, 12:53:09 PMSep 28
to skia-d...@googlegroups.com
I think that using kDstATop will get the effect you want. (In drawAtlas, the colors you pass in are the "destination", and the sprite color is the "source"). Does this look right? https://fiddle.skia.org/c/4e0667db1923c9de3464275872b90754

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/d63754b9-1d53-43c6-9bd9-b758400c2c94n%40googlegroups.com.

Ats

unread,
Sep 28, 2021, 1:42:10 PMSep 28
to skia-discuss
The problem with kDstATop is that it it's not additive and wipes out all detail from the sprite texture. I made another example that uses a better image to show the problem: https://fiddle.skia.org/c/6d22f44b3feb3d625450d8271a05723b
I would like to achieve the top left part without the red background. Bottom right is kDstATop.

kPlus seems to handle alpha similarly to kMultiply, while I would like it to handle alpha like kModulate, but there is no such option.

Is it possible to use custom shaders with drawAtlas?

Brian Osman

unread,
Sep 28, 2021, 3:09:35 PMSep 28
to skia-d...@googlegroups.com
Ah, I see. Hmm. There isn't a way to use a custom shader with drawAtlas today (it always assumes an SkImage). We've talked about offering an SkShader version of drawAtlas before - I don't remember if it was just omitted because there was no demand, or if there was a good reason (I can't think of any). I was going to suggest a tedious approach of using drawVertices, which does allow a shader. Unfortunately, the way that blend mode operates (for both atlas and vertices) is to combine the shader/image output after the shader runs. So even with a custom shader, you wouldn't be able to do what you want, unless/until we change how the per-sprite or per-vertex colors are fed into the custom shader.

Unfortunately, I don't think I have a great solution today.

Ats

unread,
Sep 28, 2021, 8:38:56 PMSep 28
to skia-discuss
At the very least I should be able to do a two pass solution, where the first pass draws the sprites and the second pass adds the highlights (using kDstATop for drawAtlas and kPlus for the Paint object).

But I would like to know more about the custom shader + drawVertices option, are there any samples? If I can access the colors and texture coordinates from the shader, I could do the highlight in the shader and use the default blend mode to just draw the result without additional blending.

Ats

unread,
Sep 29, 2021, 7:09:01 AMSep 29
to skia-discuss
Here's the example of what I want to achieve, but with two passes: https://fiddle.skia.org/c/8deeb33c6a08cc9ead78273ad2d4363e

I will try to figure out if this can be achieved this with drawVertices and a custom shader in one pass.
Reply all
Reply to author
Forward
0 new messages