Compare commits

...

36 Commits

Author SHA1 Message Date
greenkeeper[bot]
c4489095b1 chore(package): update jsdoc to version 3.6.2 2019-05-12 16:52:14 +00:00
Chris Willis-Ford
4a55d63ada Merge pull request #424 from ktbee/limit-mosaic-effect
Only check position against effect transform if it falls within the Drawable's space
2019-04-29 17:19:51 -07:00
Chris Willis-Ford
c9c780aa69 Merge pull request #418 from peabrainiac/develop
pen transparency fix
2019-04-29 17:04:15 -07:00
Paul Kaplan
590c2ca084 Merge pull request #440 from LLK/revert-419-coordinates-fixups-2
Revert "Adjust CPU `isTouchingColor` to match GPU results (again)"
2019-04-19 16:14:10 -04:00
Paul Kaplan
757d7e3c96 Revert "Adjust CPU isTouchingColor to match GPU results (again)" 2019-04-19 16:13:47 -04:00
Paul Kaplan
e365a909dc Merge pull request #439 from LLK/greenkeeper/scratch-svg-renderer-0.2.0-prerelease.20190419183947
fix(package): update scratch-svg-renderer to version 0.2.0-prerelease…
2019-04-19 16:03:53 -04:00
greenkeeper[bot]
bffe80086e fix(package): update scratch-svg-renderer to version 0.2.0-prerelease.20190419183947
Closes #430
2019-04-19 18:45:16 +00:00
Chris Willis-Ford
008dc5b15b Merge pull request #419 from cwillisf/coordinates-fixups-2
Adjust CPU `isTouchingColor` to match GPU results (again)
2019-04-10 11:35:25 -07:00
Michael "Z" Goddard
9177705e04 Merge pull request #414 from mzgoddard/image-data-texture
ImageData WebGL Textures
2019-03-26 12:12:57 -04:00
Christopher Willis-Ford
b304ea8fdf Make touching-color test more robust against GPU imprecision
Previously, the `color-touching-tests.sb2` test "touches a color that
doesn't actually exist right now" would use a sprite with ghost 50,
blended against another sprite, to create the color that "doesn't
actually exist" when the query sprite is skipped. Unfortunately the
blend result was near a bit-boundary and, depending on the specific
hardware used, that test could fail on the GPU. When the renderer uses
the CPU path this test works fine, though, so the existing problem went
unnoticed.

To fix the problem I changed the project to use ghost 30 instead, which
results in a color that is less near a bit boundary and is therefore
less likely to fail on specific hardware.

As an example of what was happening: the `touching color` block was
checking for `RGB(127,101,216)` with a mask of `RGB(0xF8,0xF8,0xF0)`. On
the CPU it would find `RGB(120,99,215)`, which is in range, but on some
GPUs the closest color it could find was `RGB(119,98,215)` which
mismatches on all four of the least significant bits -- one of which is
enabled in the mask.
2019-03-20 22:58:36 -07:00
Christopher Willis-Ford
f9428ee096 Run test projects in each GPU usage mode 2019-03-20 11:21:05 -07:00
Christopher Willis-Ford
9526612d79 Add touching-color test to verify stencil use 2019-03-20 11:21:05 -07:00
Christopher Willis-Ford
fb767b7553 Fix exception on first button click 2019-03-20 11:21:05 -07:00
Christopher Willis-Ford
e864018d87 Iterate drawables in the same order on CPU & GPU 2019-03-20 11:21:05 -07:00
Christopher Willis-Ford
e0b420a183 Use alpha test to avoid false touching-color 2019-03-20 11:21:05 -07:00
Christopher Willis-Ford
a24b853af6 Fix (x,y) => point[] conversion comments 2019-03-20 11:21:05 -07:00
Christopher Willis-Ford
73896b6f32 Fix direction for Y iteration on CPU path
For some reason the JavaScript engine insists on running the code
instead of doing what the comment says. I guess they should match.
2019-03-20 11:21:05 -07:00
Christopher Willis-Ford
80630a64da Adjust CPU isTouchingColor to match GPU results 2019-03-20 11:21:05 -07:00
Michael "Z" Goddard
e31934f6a9 update Skin textures with ImageData
When possible pass ImageData to texture creation and updating to help
remove chance of references that keep canvas and underlying data from
being garbage collected.
2019-03-19 17:52:21 -04:00
Katie Broida
8f007c0986 Only check local position against mosaic effect transform if it falls within the drawable's space 2019-03-18 15:25:40 -04:00
Paul Kaplan
3c79a5562e Merge pull request #421 from LLK/greenkeeper/scratch-svg-renderer-0.2.0-prerelease.20190304180800
Update scratch-svg-renderer to the latest version 🚀
2019-03-12 08:46:47 -04:00
greenkeeper[bot]
d59d45b6c8 fix(package): update scratch-svg-renderer to version 0.2.0-prerelease.20190304180800 2019-03-04 18:09:20 +00:00
Michael "Z" Goddard
19ee8e8eaa Merge pull request #415 from mzgoddard/drop-silhouette-alpha-buffer
Replace Silhouette._data with Silhouette._colorData
2019-03-04 12:56:58 -05:00
peabrainiac
fe01fea9d0 Update RenderWebGL.js 2019-03-03 18:49:04 +01:00
peabrainiac
5fb9346036 Update RenderWebGL.js 2019-03-03 18:42:10 +01:00
peabrainiac
3d373571f8 Update PenSkin.js 2019-03-03 18:40:09 +01:00
peabrainiac
152cf028cc Update Skin.js 2019-03-03 18:39:40 +01:00
peabrainiac
147b79d319 Update RenderWebGL.js 2019-03-03 18:12:13 +01:00
peabrainiac
f2a7085492 Update RenderWebGL.js 2019-03-03 17:58:12 +01:00
peabrainiac
996a1d6cf7 Update sprite.frag 2019-03-03 17:55:55 +01:00
peabrainiac
61bf4c84c3 Update RenderWebGL.js 2019-03-02 22:35:42 +01:00
peabrainiac
7628c1e7f9 Update RenderWebGL.js
Modified blend function in `_drawThese` to blend skins with premultiplied alpha correctly
2019-03-02 20:59:00 +01:00
peabrainiac
9f7bd971c9 Update PenSkin.js
changed clearColor on `_setCanvasSize`
2019-03-02 00:29:21 +01:00
peabrainiac
44d2fdeba8 Update PenSkin.js 2019-02-27 08:48:45 +01:00
Michael "Z" Goddard
e022222365 replace Silhouette._data with Silhouette._colorData
_colorData holds the same (and more) data that _data holds. Dropping
the _data array saves a lot of memory for a tiny performance
degradation in regards to touching object.
2019-02-22 17:08:53 -05:00
Michael "Z" Goddard
be5ab2e689 receive ImageData directly in Silhouette.update
Given ImageData we can skip drawing the input and getting image data.
This can help if update's color can also use the ImageData directly.
2019-02-22 17:08:04 -05:00
9 changed files with 88 additions and 43 deletions

View File

@@ -35,7 +35,7 @@
"eslint": "^4.6.1",
"eslint-config-scratch": "^5.0.0",
"gh-pages": "^1.0.0",
"jsdoc": "^3.5.5",
"jsdoc": "^3.6.2",
"json": "^9.0.4",
"scratch-vm": "0.2.0-prerelease.20190207224121",
"tap": "^11.0.0",
@@ -53,7 +53,7 @@
"minilog": "3.1.0",
"raw-loader": "^0.5.1",
"scratch-storage": "^1.0.0",
"scratch-svg-renderer": "0.2.0-prerelease.20190125192231",
"scratch-svg-renderer": "0.2.0-prerelease.20190419183947",
"twgl.js": "4.4.0"
}
}

View File

@@ -79,20 +79,31 @@ class BitmapSkin extends Skin {
setBitmap (bitmapData, costumeResolution, rotationCenter) {
const gl = this._renderer.gl;
// Preferably bitmapData is ImageData. ImageData speeds up updating
// Silhouette and is better handled by more browsers in regards to
// memory.
let textureData = bitmapData;
if (bitmapData instanceof HTMLCanvasElement) {
// Given a HTMLCanvasElement get the image data to pass to webgl and
// Silhouette.
const context = bitmapData.getContext('2d');
textureData = context.getImageData(0, 0, bitmapData.width, bitmapData.height);
}
if (this._texture) {
gl.bindTexture(gl.TEXTURE_2D, this._texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmapData);
this._silhouette.update(bitmapData);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData);
this._silhouette.update(textureData);
} else {
// TODO: mipmaps?
const textureOptions = {
auto: true,
wrap: gl.CLAMP_TO_EDGE,
src: bitmapData
src: textureData
};
this._texture = twgl.createTexture(gl, textureOptions);
this._silhouette.update(bitmapData);
this._silhouette.update(textureData);
}
// Do these last in case any of the above throws an exception

View File

@@ -36,8 +36,10 @@ const getLocalPosition = (drawable, vec) => {
// localPosition matches that transformation.
localPosition[0] = 0.5 - (((v0 * m[0]) + (v1 * m[4]) + m[12]) / d);
localPosition[1] = (((v0 * m[1]) + (v1 * m[5]) + m[13]) / d) + 0.5;
// Apply texture effect transform.
EffectTransform.transformPoint(drawable, localPosition, localPosition);
// Apply texture effect transform if the localPosition is within the drawable's space.
if ((localPosition[0] >= 0 && localPosition[0] < 1) && (localPosition[1] >= 0 && localPosition[1] < 1)) {
EffectTransform.transformPoint(drawable, localPosition, localPosition);
}
return localPosition;
};

View File

@@ -154,6 +154,13 @@ class PenSkin extends Skin {
return true;
}
/**
* @returns {boolean} true if alpha is premultiplied, false otherwise
*/
get hasPremultipliedAlpha () {
return true;
}
/**
* @return {Array<number>} the "native" size, in texels, of this skin. [width, height]
*/
@@ -181,8 +188,9 @@ class PenSkin extends Skin {
clear () {
const gl = this._renderer.gl;
twgl.bindFramebufferInfo(gl, this._framebuffer);
gl.clearColor(1, 1, 1, 0);
/* Reset framebuffer to transparent black */
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
const ctx = this._canvas.getContext('2d');
@@ -598,7 +606,7 @@ class PenSkin extends Skin {
this._silhouetteBuffer = twgl.createFramebufferInfo(gl, [{format: gl.RGBA}], width, height);
}
gl.clearColor(1, 1, 1, 0);
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
this._silhouetteDirty = true;

View File

@@ -1620,7 +1620,14 @@ class RenderWebGL extends EventEmitter {
}
twgl.setUniforms(currentShader, uniforms);
/* adjust blend function for this skin */
if (drawable.skin.hasPremultipliedAlpha){
gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
} else {
gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
}
twgl.drawBufferInfo(gl, this._bufferInfo, gl.TRIANGLES);
}

View File

@@ -77,10 +77,14 @@ class SVGSkin extends Skin {
this._textureScale = newScale;
this._svgRenderer._draw(this._textureScale, () => {
if (this._textureScale === newScale) {
const canvas = this._svgRenderer.canvas;
const context = canvas.getContext('2d');
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);
const gl = this._renderer.gl;
gl.bindTexture(gl.TEXTURE_2D, this._texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._svgRenderer.canvas);
this._silhouette.update(this._svgRenderer.canvas);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData);
this._silhouette.update(textureData);
}
});
}
@@ -99,20 +103,28 @@ class SVGSkin extends Skin {
this._svgRenderer.fromString(svgData, 1, () => {
const gl = this._renderer.gl;
this._textureScale = this._maxTextureScale = 1;
// Pull out the ImageData from the canvas. ImageData speeds up
// updating Silhouette and is better handled by more browsers in
// regards to memory.
const canvas = this._svgRenderer.canvas;
const context = canvas.getContext('2d');
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);
if (this._texture) {
gl.bindTexture(gl.TEXTURE_2D, this._texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._svgRenderer.canvas);
this._silhouette.update(this._svgRenderer.canvas);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData);
this._silhouette.update(textureData);
} else {
// TODO: mipmaps?
const textureOptions = {
auto: true,
wrap: gl.CLAMP_TO_EDGE,
src: this._svgRenderer.canvas
src: textureData
};
this._texture = twgl.createTexture(gl, textureOptions);
this._silhouette.update(this._svgRenderer.canvas);
this._silhouette.update(textureData);
}
const maxDimension = Math.max(this._svgRenderer.canvas.width, this._svgRenderer.canvas.height);

View File

@@ -19,12 +19,12 @@ let __SilhouetteUpdateCanvas;
* @param {number} y - y
* @return {number} Alpha value for x/y position
*/
const getPoint = ({_width: width, _height: height, _data: data}, x, y) => {
const getPoint = ({_width: width, _height: height, _colorData: data}, x, y) => {
// 0 if outside bouds, otherwise read from data.
if (x >= width || y >= height || x < 0 || y < 0) {
return 0;
}
return data[(y * width) + x];
return data[(((y * width) + x) * 4) + 3];
};
/**
@@ -76,7 +76,6 @@ class Silhouette {
* The data representing a skin's silhouette shape.
* @type {Uint8ClampedArray}
*/
this._data = null;
this._colorData = null;
this.colorAtNearest = this.colorAtLinear = (_, dst) => dst.fill(0);
@@ -88,28 +87,33 @@ class Silhouette {
* rendering can be queried from.
*/
update (bitmapData) {
const canvas = Silhouette._updateCanvas();
const width = this._width = canvas.width = bitmapData.width;
const height = this._height = canvas.height = bitmapData.height;
const ctx = canvas.getContext('2d');
let imageData;
if (bitmapData instanceof ImageData) {
// If handed ImageData directly, use it directly.
imageData = bitmapData;
this._width = bitmapData.width;
this._height = bitmapData.height;
} else {
// Draw about anything else to our update canvas and poll image data
// from that.
const canvas = Silhouette._updateCanvas();
const width = this._width = canvas.width = bitmapData.width;
const height = this._height = canvas.height = bitmapData.height;
const ctx = canvas.getContext('2d');
if (!(width && height)) {
return;
if (!(width && height)) {
return;
}
ctx.clearRect(0, 0, width, height);
ctx.drawImage(bitmapData, 0, 0, width, height);
imageData = ctx.getImageData(0, 0, width, height);
}
ctx.clearRect(0, 0, width, height);
ctx.drawImage(bitmapData, 0, 0, width, height);
const imageData = ctx.getImageData(0, 0, width, height);
this._data = new Uint8ClampedArray(imageData.data.length / 4);
this._colorData = imageData.data;
// delete our custom overriden "uninitalized" color functions
// let the prototype work for itself
delete this.colorAtNearest;
delete this.colorAtLinear;
for (let i = 0; i < imageData.data.length; i += 4) {
this._data[i / 4] = imageData.data[i + 3];
}
}
/**
@@ -166,7 +170,7 @@ class Silhouette {
* @return {boolean} If the nearest pixel has an alpha value.
*/
isTouchingNearest (vec) {
if (!this._data) return;
if (!this._colorData) return;
return getPoint(
this,
Math.floor(vec[0] * (this._width - 1)),
@@ -181,7 +185,7 @@ class Silhouette {
* @return {boolean} Any of the pixels have some alpha.
*/
isTouchingLinear (vec) {
if (!this._data) return;
if (!this._colorData) return;
const x = Math.floor(vec[0] * (this._width - 1));
const y = Math.floor(vec[1] * (this._height - 1));
return getPoint(this, x, y) > 0 ||

View File

@@ -76,6 +76,13 @@ class Skin extends EventEmitter {
return false;
}
/**
* @returns {boolean} true if alpha is premultiplied, false otherwise
*/
get hasPremultipliedAlpha () {
return false;
}
/**
* @return {int} the unique ID for this Skin.
*/

View File

@@ -194,12 +194,6 @@ void main()
discard;
}
#endif // DRAW_MODE_colorMask
// WebGL defaults to premultiplied alpha
#ifndef DRAW_MODE_stamp
gl_FragColor.rgb *= gl_FragColor.a;
#endif // DRAW_MODE_stamp
#endif // DRAW_MODE_silhouette
#else // DRAW_MODE_lineSample