-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Fix WebGL blending bugs #5794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix WebGL blending bugs #5794
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -437,7 +437,7 @@ suite('p5.RendererGL', function() { | |
| test('blendModes change pixel colors as expected', function(done) { | ||
| myp5.createCanvas(10, 10, myp5.WEBGL); | ||
| myp5.noStroke(); | ||
| assert.deepEqual([133, 69, 191, 255], mixAndReturn(myp5.ADD, 255)); | ||
| assert.deepEqual([122, 0, 122, 255], mixAndReturn(myp5.ADD, 0)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was updated to use a black background because previously, the background was white, and blending anything onto white with ADD results in more white once ADD works the same way as in 2D mode. |
||
| assert.deepEqual([0, 0, 255, 255], mixAndReturn(myp5.REPLACE, 255)); | ||
| assert.deepEqual([133, 255, 133, 255], mixAndReturn(myp5.SUBTRACT, 255)); | ||
| assert.deepEqual([255, 0, 255, 255], mixAndReturn(myp5.SCREEN, 0)); | ||
|
|
@@ -447,6 +447,68 @@ suite('p5.RendererGL', function() { | |
| assert.deepEqual([0, 0, 0, 255], mixAndReturn(myp5.DARKEST, 255)); | ||
| done(); | ||
| }); | ||
|
|
||
| test('blendModes match 2D mode', function(done) { | ||
| myp5.createCanvas(10, 10, myp5.WEBGL); | ||
| myp5.setAttributes({ alpha: true }); | ||
| const ref = myp5.createGraphics(myp5.width, myp5.height); | ||
| ref.translate(ref.width / 2, ref.height / 2); // Match WebGL mode | ||
|
|
||
| const testBlend = function(target, colorA, colorB, mode) { | ||
| target.clear(); | ||
| target.push(); | ||
| target.background(colorA); | ||
| target.blendMode(mode); | ||
| target.noStroke(); | ||
| target.fill(colorB); | ||
| target.rectMode(target.CENTER); | ||
| target.rect(0, 0, target.width, target.height); | ||
| target.pop(); | ||
| return target.get(0, 0); | ||
| }; | ||
|
|
||
| const assertSameIn2D = function(colorA, colorB, mode) { | ||
| const refColor = testBlend(myp5, colorA, colorB, mode); | ||
| const webglColor = testBlend(ref, colorA, colorB, mode); | ||
| if (refColor[3] === 0) { | ||
| assert.equal(webglColor[3], 0); | ||
| } else { | ||
| assert.deepEqual( | ||
| refColor, | ||
| webglColor, | ||
| `Blending ${colorA} with ${colorB} using ${mode}` | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| const red = '#F53'; | ||
| const blue = '#13F'; | ||
| assertSameIn2D(red, blue, myp5.BLEND); | ||
| assertSameIn2D(red, blue, myp5.ADD); | ||
| assertSameIn2D(red, blue, myp5.DARKEST); | ||
| assertSameIn2D(red, blue, myp5.LIGHTEST); | ||
| assertSameIn2D(red, blue, myp5.EXCLUSION); | ||
| assertSameIn2D(red, blue, myp5.MULTIPLY); | ||
| assertSameIn2D(red, blue, myp5.SCREEN); | ||
| assertSameIn2D(red, blue, myp5.REPLACE); | ||
| assertSameIn2D(red, blue, myp5.REMOVE); | ||
| done(); | ||
| }); | ||
|
|
||
| test('blendModes are included in push/pop', function(done) { | ||
| myp5.createCanvas(10, 10, myp5.WEBGL); | ||
| myp5.blendMode(myp5.MULTIPLY); | ||
| myp5.push(); | ||
| myp5.blendMode(myp5.ADD); | ||
| assert.equal(myp5._renderer.curBlendMode, myp5.ADD, 'Changed to ADD'); | ||
| myp5.pop(); | ||
| assert.equal( | ||
| myp5._renderer.curBlendMode, | ||
| myp5.MULTIPLY, | ||
| 'Resets to MULTIPLY' | ||
| ); | ||
| done(); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a great set of unit tests. This is a great safeguard against unexpected divergence between the renderers down the road. |
||
| }); | ||
|
|
||
| suite('BufferDef', function() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great and important change! I remember when we first added blendmodes to WebGL, we were getting a bunch of issues that all involved transparency or textures. It was easy to forget that people blend fully opaque colors as well 🤦♀️ . Great job finding a small improvement that makes a big difference!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dread every transparency/blending bug I encounter because it's so easy to mess up in WebGL 😅 I really appreciate your work setting all of it up so that transparency Just Works in p5!