Skip to content
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 "touching color" for background color #489

Merged
merged 7 commits into from
Jul 14, 2020

Conversation

adroitwhiz
Copy link
Contributor

@adroitwhiz adroitwhiz commented Aug 2, 2019

Resolves

Resolves #488

Proposed Changes

This PR makes several changes that are all necessary to properly fix "touching color" for background colors:

  • RenderWebGL._backgroundColor has been replaced with RenderWebGL._backgroundColor4f and RenderWebGL._backgroundColor3b. This makes it easier to call colorMatches on the background color.
  • RenderWebGL.isTouchingColor now checks whether the color to be checked colorMatches the background color, and if it does, will not limit the check to "candidate" drawables' bounds, as the background spans the entire stage and should always be checked.
  • A new background draw mode and draw region has been added, which draws the background color as a quad. This is necessary to support the next change.
    • As part of adding a new draw mode, I cleaned up the #ifdef logic in the shader code a bit.
  • The GPU path for isTouchingColor now clears to transparent black, and only draws in the opaque background for pixels masked in by the stencil buffer (which can only be done if the background is drawn as a quad--gl.clear will fill the entire buffer). The loop in _isTouchingColorGpuFin that checks for a pixel match will no longer check transparent pixels. This is the correct behavior, as only masked-in pixels should be checked.
  • _candidatesTouching, used for "touching sprite" queries, expands the bounding boxes of all candidate drawables to integer coordinates, as is already done for the query drawable in _touchingBounds.

Reason for Changes

These changes are all necessary to make "touching color" behave properly when checking if the background color is being touched.

Test Coverage

An "is touching background color" test has been added.

@thisandagain
Copy link
Contributor

/cc @BryceLTaylor @kchadha

@@ -842,11 +875,16 @@ class RenderWebGL extends EventEmitter {
};
}

// TODO: figure out why this is all in a try block?
Copy link
Contributor Author

@adroitwhiz adroitwhiz Nov 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this try-finally dates back to cwillisf's initial implementation of the code. What error is it supposed to catch?

@adroitwhiz
Copy link
Contributor Author

This is actually failing the test I wrote on the GPU mode. At least we know the test works...

When we construct bounds inside which we should loop over pixels, which
is done in _candidatesBounds, we expect their dimensions+position to be
integers when looping over them. If they aren't, weird things will occur
(e.g. "phantom" touching-color results depending on the precise subpixel
bounds position) that are not very fun to debug.
@adroitwhiz
Copy link
Contributor Author

Fixed it! I'm surprised we didn't see issues earlier-- in some places we were initializing loop counters to floating-point values!

/** @type {Uint8ClampedArray}
* @readonly */
this._backgroundColor3b = new Uint8ClampedArray(3);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't remove this._backgroundColor because it never existed here-- it's first defined when setBackgroundColor is called

@@ -244,7 +258,12 @@ class RenderWebGL extends EventEmitter {
* @param {number} blue The blue component for the background.
*/
setBackgroundColor (red, green, blue) {
this._backgroundColor = [red, green, blue, 1];
this._backgroundColor4f = [red, green, blue, 1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might generate less GC work:

Suggested change
this._backgroundColor4f = [red, green, blue, 1];
this._backgroundColor4f[0] = red;
this._backgroundColor4f[1] = green;
this._backgroundColor4f[2] = blue;
this._backgroundColor4f[3] = 1;

@@ -244,7 +258,12 @@ class RenderWebGL extends EventEmitter {
* @param {number} blue The blue component for the background.
*/
setBackgroundColor (red, green, blue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's consider removing this function entirely in a future PR

Copy link
Contributor

@cwillisf cwillisf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks!

@adroitwhiz adroitwhiz merged commit a1ca2eb into scratchfoundation:develop Jul 14, 2020
adroitwhiz added a commit to adroitwhiz/scratch-render that referenced this pull request Jul 16, 2020
…hing-white-fixes"

This reverts commit a1ca2eb, reversing
changes made to fcdcffe.
adroitwhiz added a commit that referenced this pull request Jul 16, 2020
Revert "Merge pull request #489 from adroitwhiz/touching-white-fixes"
fsih added a commit that referenced this pull request Aug 4, 2020
fsih added a commit that referenced this pull request Aug 4, 2020
Revert "Revert "Merge pull request #489 from adroitwhiz/touching-white-fixes""
fsih added a commit that referenced this pull request Aug 5, 2020
fsih added a commit that referenced this pull request Aug 6, 2020
Revert "Revert "Revert "Merge pull request #489 from adroitwhiz/touching-white-fixes"""
fsih added a commit that referenced this pull request Aug 11, 2020
fsih added a commit that referenced this pull request Aug 26, 2020
…ert-489

Put back in "Merge pull request #489 from adroitwhiz/touching-white-fixes"
fsih added a commit that referenced this pull request Aug 27, 2020
fsih added a commit that referenced this pull request Aug 27, 2020
…ert-660-revert-489

Revert "Put back in "Merge pull request #489 from adroitwhiz/touching-white-fixes""
fsih added a commit that referenced this pull request Aug 27, 2020
fsih added a commit that referenced this pull request Aug 27, 2020
fsih added a commit that referenced this pull request Aug 27, 2020
…ert-673-revert-660-revert-489

Put back in "Merge pull request #489 from adroitwhiz/touching-white-fixes", fix touching color issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Touching color (white)" behaves inconsistently
5 participants