fand / notan-alpha-on-render-texture-test

https://amagi.dev/notan-alpha-on-render-texture-test/
0 stars 1 forks source link

Blending modes and the right behavior #2

Open Nazariglez opened 1 year ago

Nazariglez commented 1 year ago

Hey @fand ! I just found this demo, and I think that this is not the right behavior at all right? The 2nd image and the 3rd should look the same.

I am thinking that this is an issue with the alpha blending and color_blending modes applied. You can change the color mode with draw.set_blend_mode but the Draw API doesn't support yet .set_alpha_mode. It should be easy to add though.

I am just wondering if you know how to get the right result using WebGL or OpenGL, because I am doing some test and I cannot find the right combination yet. Any help with this will be very welcome!

Thank you so much to put this demo together.

BTW: Notan 0.8.0 will not require to scale * -1.0 the height for render textures if you're using the Draw API. I'll release it soon.

Nazariglez commented 1 year ago

This is what I found if you don't clear at all the FBO you get the same result as the 2nd image.

But, if you want to clear with Color::TRANSPARENT you will need to set the alpha mode as BlendMode::OVER to achieve the same behavior. I'll expose a method to set the alpha blending more on the Draw2D API, and I'll also apply the mode Over automatically for this case:

This should fix the issue. Let me know if you have any insight or questions!

fand commented 1 year ago

Hi @Nazariglez ! Thanks, I was going to ask you on the Discord about this, I wonder how you could find this repo...😂

I thought it's a gamma correction problem, since I noticed adding a pipeline that modifies the alpha like color.a = pow(color.a, 1.0 / 2.2); kinda mitigates the issue... though the RGB color is still broken, alpha seems close to the original alpha. (I tried this here https://github.com/fand/notan-alpha-on-render-texture-test/pull/3#issue-1465463184)

But I'm not sure, I haven't read notan impl and I have no idea what's going on under the hood. If BlendMode::OVER solves the problem it's great, I'll try it soon 🙏

Nazariglez commented 1 year ago

Hey!

Hi @Nazariglez ! Thanks, I was going to ask you on the Discord about this, I wonder how you could find this repo...😂

I saw that some people are importing notan sub-crates directly instead of enabling the features and I was checking the uses of notan on github and found your repo, with the image. :P Sorry for just jumping here and opening an issue, but the image looked wrong to me and I was worried that it was a bug.

I made a PR (#4) addressing the issue. I found the issue and two workarounds, one of them is the code on the PR, and the second one is written down in the PR description.

The issue is mainly because we're using a non-premultiplied alpha image on a premultiplied alpha context (RenderTexture) using the incorrect blend mode. It's kind of a complex topic, and I found thanks to you a lot of information related.

This link explains it really well: https://www.adriancourreges.com/blog/2017/05/09/beware-of-transparent-pixels/

Anyway, I need to think about how to address this on notan itself, the options are:

Both options have the downside that the user needs to do something, the workaround, or load/convert images with premultiplied alpha colors. Right now, 2nd option looks less confusing to me, but I guess I need to do some tests before doing nothing because it's a breaking change.

I hope this helps you. Thank you so much for taking the time to put together this test and investigate it.