Jump to content

Issue with shaders and alpha


mateuszJS
 Share

Recommended Posts

Hey!

I'm trying to make calculations in a shader, and use all 4 channels, but unfortunately the alpha channel does not work as expected. I was thinking that if I will set
gl.blendFunc(gl.ONE, gl.ZERO)
then the output should be only just the source, without any blending, but it's not. So I have tried to combine it also with premultipling alpha (and keep gl.blendFunc(gl.ONE, gl.ZERO)), I get sort of closer results to the expected, but still behaviour is strange.

In the demo below I'm just filling mesh with one color, and console.log values of the first pixel, was expecting [25, 178, 229, 25] but I got [255, 255, 255, 25] (without premultipled alpha) and [31, 184, 235, 25] (with premultipled alpha)
https://www.pixiplayground.com/#/edit/785nGjqG5wSZoa9r2Xn7I
 

Thank you in advance for a response!

Link to comment
Share on other sites

extract assumes that data in buffer is premultiplied, so it postdivides it :)

premultiplied

ok, so, your R*A = 0.1 * 0.1 , that's 0.01, that's only 2.55 if interval is 0..255, it gets rounded to just 2. Then it divides it by alpha, which is 0.1, and that's how I get red=20 in my console.

Welcome to 8-bit per channel premultiplied hell

Edited by ivan.popelyshev
Link to comment
Share on other sites

Awesome! Thank you! Just in case if someone else will run into that issue, here is the fixed code sample

https://www.pixiplayground.com/#/edit/JBJ_NTNOIwcxv64JdNMZE

  1. Output from the fragment shader are not premultipled by alpha
  2. Created function extractPixelsWithoutPostdivide, it's just copy of https://github.com/pixijs/pixi.js/blob/068e0af7f2851c51618b463b7fccc555e6ffb4b0/packages/extract/src/Extract.ts#L192 but with removed Extract.arrayPostDivide(webglPixels, webglPixels);
Edited by mateuszJS
Typo
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...