-
Notifications
You must be signed in to change notification settings - Fork 350
Set PenSkin silhouette data directly #475
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
Merged
fsih
merged 1 commit into
scratchfoundation:develop
from
adroitwhiz:penskin-silhouette-from-data
Apr 2, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,12 @@ class PenSkin extends Skin { | |
/** @type {boolean} */ | ||
this._silhouetteDirty = false; | ||
|
||
/** @type {Uint8Array} */ | ||
this._silhouettePixels = null; | ||
|
||
/** @type {ImageData} */ | ||
this._silhouetteImageData = null; | ||
|
||
/** @type {object} */ | ||
this._lineOnBufferDrawRegionId = { | ||
enter: () => this._enterDrawLineOnBuffer(), | ||
|
@@ -597,6 +603,9 @@ class PenSkin extends Skin { | |
gl.clearColor(0, 0, 0, 0); | ||
gl.clear(gl.COLOR_BUFFER_BIT); | ||
|
||
this._silhouettePixels = new Uint8Array(Math.floor(width * height * 4)); | ||
this._silhouetteImageData = this._canvas.getContext('2d').createImageData(width, height); | ||
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. Yes. We should definitely reuse these. |
||
|
||
this._silhouetteDirty = true; | ||
} | ||
|
||
|
@@ -634,24 +643,17 @@ class PenSkin extends Skin { | |
// Render export texture to another framebuffer | ||
const gl = this._renderer.gl; | ||
|
||
const bounds = this._bounds; | ||
|
||
this._renderer.enterDrawRegion(this._toBufferDrawRegionId); | ||
|
||
// Sample the framebuffer's pixels into the silhouette instance | ||
const skinPixels = new Uint8Array(Math.floor(this._canvas.width * this._canvas.height * 4)); | ||
gl.readPixels(0, 0, this._canvas.width, this._canvas.height, gl.RGBA, gl.UNSIGNED_BYTE, skinPixels); | ||
|
||
const skinCanvas = this._canvas; | ||
skinCanvas.width = bounds.width; | ||
skinCanvas.height = bounds.height; | ||
|
||
const skinContext = skinCanvas.getContext('2d'); | ||
const skinImageData = skinContext.createImageData(bounds.width, bounds.height); | ||
skinImageData.data.set(skinPixels); | ||
skinContext.putImageData(skinImageData, 0, 0); | ||
|
||
this._silhouette.update(this._canvas, true /* isPremultiplied */); | ||
gl.readPixels( | ||
0, 0, | ||
this._canvas.width, this._canvas.height, | ||
gl.RGBA, gl.UNSIGNED_BYTE, this._silhouettePixels | ||
); | ||
|
||
this._silhouetteImageData.data.set(this._silhouettePixels); | ||
this._silhouette.update(this._silhouetteImageData, true /* isPremultiplied */); | ||
|
||
this._silhouetteDirty = false; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: the
math.floor
isn't necessary hereThere 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.
It seems to be used in every other place in the codebase that constructs a
Uint8Array
, specifically to prevent issues with Edge.If I removed it here, I don't think it would cause any trouble (
width
andheight
should be integers), and newer versions of Edge (I tested it on Microsoft Edge 44.18362.449.0, Microsoft EdgeHTML 18.18363) have no trouble with floating-point values in typed array constructors, but it'd break the convention that the rest of the codebase is using. Is that okay?(P.S. it may be worth revisiting this in the future by either testing whether Edge 15, the minimum version Scratch officially supports, supports floating-point values in
TypedArray
constructors, or by bumping the minimum supported Edge version in the FAQ to something newer.)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.
Oops - thanks for the reminder! I had completely forgotten about that.