Compare commits

...

40 Commits

Author SHA1 Message Date
greenkeeper[bot]
1d7a60d6a2 chore(package): update scratch-vm to version 0.2.0-prerelease.20200310202129
Closes #560
2020-03-10 20:28:46 +00:00
DD Liu
a28753fb5a Merge pull request #559 from LLK/revert-438-pen-shader
Revert "Draw pen lines via fragment shader"
2020-02-28 10:19:45 -05:00
DD Liu
adf6fd6dae Revert "Draw pen lines via fragment shader" 2020-02-28 10:00:40 -05:00
DD Liu
8139afdb52 Merge pull request #438 from adroitwhiz/pen-shader
Draw pen lines via fragment shader
2020-02-27 15:55:53 -05:00
adroitwhiz
7d652b44d3 Move pen line bounds calculation to vertex shader 2020-02-11 17:46:12 -05:00
adroitwhiz
6bc1c32cf8 Explain pen line shader better 2020-02-07 07:11:35 -05:00
adroitwhiz
936afeef60 Tighten pen bounds 2020-02-07 07:11:28 -05:00
adroitwhiz
9e12f093f6 Cleanup 2020-02-05 03:18:47 -05:00
adroitwhiz
24851f28f8 Draw pen lines via fragment shader 2020-02-05 03:18:47 -05:00
DD Liu
2a36dadf21 Merge pull request #554 from LLK/greenkeeper/scratch-vm-0.2.0-prerelease.20191227164934
chore(package): update scratch-vm to version 0.2.0-prerelease.2019122…
2020-02-04 20:11:03 -05:00
DD Liu
490f0a4ecb Merge pull request #553 from LLK/greenkeeper/scratch-svg-renderer-0.2.0-prerelease.20200205003400
fix(package): update scratch-svg-renderer to version 0.2.0-prerelease…
2020-02-04 20:00:29 -05:00
greenkeeper[bot]
c55344d55b fix(package): update scratch-svg-renderer to version 0.2.0-prerelease.20200205003400
Closes #552
2020-02-05 00:35:17 +00:00
Chris Willis-Ford
d0d98de38f Merge pull request #515 from adroitwhiz/premultiply-the-sequel
Use premultiplied alpha everywhere, take 2
2020-01-28 12:49:27 -08:00
adroitwhiz
0cb97a202a Address review feedback 2020-01-24 05:28:20 -05:00
adroitwhiz
10a6d87eb6 Premultiplied alpha, take 2 2020-01-23 14:41:53 -05:00
Chris Willis-Ford
fed02ca582 Merge pull request #544 from adroitwhiz/fix-vector-silhouette
Fix SVGSkin silhouettes
2020-01-13 14:34:52 -05:00
adroitwhiz
2985f2ac54 Fix SVGSkin silhouettes 2020-01-10 06:24:01 -05:00
DD Liu
a67cd48ef6 Merge pull request #485 from adroitwhiz/effect-transform-optimizations
Optimize effect transforms
2020-01-09 14:52:14 -05:00
DD Liu
ed889bce72 Merge pull request #539 from adroitwhiz/zero-size
Set zero-sized SVG MIPs to empty texture
2020-01-09 02:29:30 -05:00
DD Liu
570fa9880f Merge pull request #540 from fsih/updateSvgRend
Update svg renderer
2020-01-09 02:28:51 -05:00
DD Liu
ad9b90805f Update svg renderer 2020-01-09 02:17:25 -05:00
adroitwhiz
6d9cec8222 Set zero-sized SVG MIPs to empty texture 2020-01-09 01:09:47 -05:00
adroitwhiz
8d1117b36a Simplify EffectTransform call signatures 2020-01-07 16:11:53 -05:00
adroitwhiz
dd2d54ed13 Optimize effect transforms 2020-01-07 16:11:39 -05:00
DD Liu
5215c99544 Merge pull request #536 from adroitwhiz/update-svg-renderer
Update scratch-svg-renderer to required version
2020-01-07 15:48:33 -05:00
adroitwhiz
d8db4b5c08 Update scratch-svg-renderer to required version 2020-01-07 14:48:34 -05:00
Chris Willis-Ford
478698cf12 Merge pull request #527 from adroitwhiz/svgmip-synchronous
Synchronous SVG mipmaps
2020-01-07 08:54:00 -08:00
DD Liu
8409de7684 Merge pull request #534 from LLK/greenkeeper/scratch-svg-renderer-0.2.0-prerelease.20200103191258
Update scratch-svg-renderer to the latest version 🚀
2020-01-07 11:48:23 -05:00
adroitwhiz
6662a7d556 Address review feedback
Co-Authored-By: Chris Willis-Ford <cwillisf@users.noreply.github.com>
2020-01-07 10:47:10 -05:00
greenkeeper[bot]
5dc809014e fix(package): update scratch-svg-renderer to version 0.2.0-prerelease.20200103191258 2020-01-03 19:18:09 +00:00
greenkeeper[bot]
53374ac77f chore(package): update scratch-vm to version 0.2.0-prerelease.20191227164934
Closes #525
2019-12-27 16:51:50 +00:00
picklesrus
932846f484 Merge pull request #531 from LLK/greenkeeper/scratch-svg-renderer-0.2.0-prerelease.20191217211338
Update scratch-svg-renderer to the latest version 🚀
2019-12-17 16:21:56 -05:00
greenkeeper[bot]
db1c7b3b2d fix(package): update scratch-svg-renderer to version 0.2.0-prerelease.20191217211338 2019-12-17 21:14:57 +00:00
DD Liu
e12646871e Merge pull request #487 from adroitwhiz/pen-stamp-to-framebuffer
penStamp() directly to the PenSkin's framebuffer
2019-12-11 15:30:28 -05:00
adroitwhiz
bf5d666737 Synchronous SVG mipmaps 2019-11-26 16:48:45 -05:00
Chris Willis-Ford
b1274d5820 Merge pull request #431 from ktbee/javascript-scaled-textures
Add JavaScript MIPs for scaling textures larger and smaller
2019-11-26 12:57:51 -08:00
Katie Broida
f17252624e Add SVGMIP as a wrapper for WebGL textures to handle creating textures at a different scale and updating them
Check scale for largest MIP
2019-10-28 13:53:10 -04:00
adroitwhiz
b1419d0468 set silhouette dirty 2019-08-04 06:43:59 -04:00
adroitwhiz
a8320b9ebc remove magic numbers 2019-08-01 22:28:49 -04:00
adroitwhiz
9f15b86672 penStamp directly to PenSkin's framebuffer 2019-07-25 07:59:52 -04:00
12 changed files with 313 additions and 227 deletions

View File

@@ -37,7 +37,7 @@
"gh-pages": "^1.0.0",
"jsdoc": "^3.5.5",
"json": "^9.0.4",
"scratch-vm": "0.2.0-prerelease.20190213162739",
"scratch-vm": "0.2.0-prerelease.20200310202129",
"tap": "^11.0.0",
"travis-after-all": "^1.4.4",
"uglifyjs-webpack-plugin": "^1.2.5",
@@ -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.20191104164753",
"scratch-svg-renderer": "0.2.0-prerelease.20200205003400",
"twgl.js": "4.4.0"
}
}

View File

@@ -18,9 +18,6 @@ class BitmapSkin extends Skin {
/** @type {!RenderWebGL} */
this._renderer = renderer;
/** @type {WebGLTexture} */
this._texture = null;
/** @type {Array<int>} */
this._textureSize = [0, 0];
}
@@ -95,22 +92,17 @@ class BitmapSkin extends Skin {
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, textureData);
this._silhouette.update(textureData);
} else {
// TODO: mipmaps?
if (this._texture === null) {
const textureOptions = {
auto: true,
wrap: gl.CLAMP_TO_EDGE,
src: textureData
auto: false,
wrap: gl.CLAMP_TO_EDGE
};
this._texture = twgl.createTexture(gl, textureOptions);
this._silhouette.update(textureData);
}
this._setTexture(textureData);
// Do these last in case any of the above throws an exception
this._costumeResolution = costumeResolution || 2;
this._textureSize = BitmapSkin._getBitmapSize(bitmapData);

View File

@@ -36,8 +36,12 @@ 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 if the localPosition is within the drawable's space.
if ((localPosition[0] >= 0 && localPosition[0] < 1) && (localPosition[1] >= 0 && localPosition[1] < 1)) {
// Apply texture effect transform if the localPosition is within the drawable's space,
// and any effects are currently active.
if (drawable.enabledEffects !== 0 &&
(localPosition[0] >= 0 && localPosition[0] < 1) &&
(localPosition[1] >= 0 && localPosition[1] < 1)) {
EffectTransform.transformPoint(drawable, localPosition, localPosition);
}
return localPosition;
@@ -96,7 +100,11 @@ class Drawable {
this._inverseMatrix = twgl.m4.identity();
this._inverseTransformDirty = true;
this._visible = true;
this._effectBits = 0;
/** A bitmask identifying which effects are currently in use.
* @readonly
* @type {int} */
this.enabledEffects = 0;
/** @todo move convex hull functionality, maybe bounds functionality overall, to Skin classes */
this._convexHullPoints = null;
@@ -159,13 +167,6 @@ class Drawable {
return [this._scale[0], this._scale[1]];
}
/**
* @returns {int} A bitmask identifying which effects are currently in use.
*/
getEnabledEffects () {
return this._effectBits;
}
/**
* @returns {object.<string, *>} the shader uniforms to be used when rendering this Drawable.
*/
@@ -242,9 +243,9 @@ class Drawable {
updateEffect (effectName, rawValue) {
const effectInfo = ShaderManager.EFFECT_INFO[effectName];
if (rawValue) {
this._effectBits |= effectInfo.mask;
this.enabledEffects |= effectInfo.mask;
} else {
this._effectBits &= ~effectInfo.mask;
this.enabledEffects &= ~effectInfo.mask;
}
const converter = effectInfo.converter;
this._uniforms[effectInfo.uniformName] = converter(rawValue);
@@ -481,7 +482,7 @@ class Drawable {
}
// If the effect bits for mosaic, pixelate, whirl, or fisheye are set, use linear
if ((this._effectBits & (
if ((this.enabledEffects & (
ShaderManager.EFFECT_INFO.fisheye.mask |
ShaderManager.EFFECT_INFO.whirl.mask |
ShaderManager.EFFECT_INFO.pixelate.mask |
@@ -684,6 +685,9 @@ class Drawable {
const localPosition = getLocalPosition(drawable, vec);
if (localPosition[0] < 0 || localPosition[1] < 0 ||
localPosition[0] > 1 || localPosition[1] > 1) {
dst[0] = 0;
dst[1] = 0;
dst[2] = 0;
dst[3] = 0;
return dst;
}
@@ -692,7 +696,9 @@ class Drawable {
// drawable.useNearest() ?
drawable.skin._silhouette.colorAtNearest(localPosition, dst);
// : drawable.skin._silhouette.colorAtLinear(localPosition, dst);
return EffectTransform.transformColor(drawable, textColor, textColor);
if (drawable.enabledEffects === 0) return textColor;
return EffectTransform.transformColor(drawable, textColor);
}
}

View File

@@ -114,36 +114,39 @@ const hslToRgb = ([h, s, l]) => {
class EffectTransform {
/**
* Transform a color given the drawables effect uniforms. Will apply
* Transform a color in-place given the drawable's effect uniforms. Will apply
* Ghost and Color and Brightness effects.
* @param {Drawable} drawable The drawable to get uniforms from.
* @param {Uint8ClampedArray} color4b The initial color.
* @param {Uint8ClampedArary} [dst] Working space to save the color in (is returned)
* @param {number} [effectMask] A bitmask for which effects to use. Optional.
* @param {Uint8ClampedArray} inOutColor The color to transform.
* @returns {Uint8ClampedArray} dst filled with the transformed color
*/
static transformColor (drawable, color4b, dst, effectMask) {
dst = dst || new Uint8ClampedArray(4);
effectMask = effectMask || 0xffffffff;
dst.set(color4b);
if (dst[3] === 0) {
return dst;
static transformColor (drawable, inOutColor) {
// If the color is fully transparent, don't bother attempting any transformations.
if (inOutColor[3] === 0) {
return inOutColor;
}
const effects = drawable.enabledEffects;
const uniforms = drawable.getUniforms();
const effects = drawable.getEnabledEffects() & effectMask;
if ((effects & ShaderManager.EFFECT_INFO.ghost.mask) !== 0) {
// gl_FragColor.a *= u_ghost
dst[3] *= uniforms.u_ghost;
}
const enableColor = (effects & ShaderManager.EFFECT_INFO.color.mask) !== 0;
const enableBrightness = (effects & ShaderManager.EFFECT_INFO.brightness.mask) !== 0;
if (enableColor || enableBrightness) {
// gl_FragColor.rgb /= gl_FragColor.a + epsilon;
// Here, we're dividing by the (previously pre-multiplied) alpha to ensure HSV is properly calculated
// for partially transparent pixels.
// epsilon is present in the shader because dividing by 0 (fully transparent pixels) messes up calculations.
// We're doing this with a Uint8ClampedArray here, so dividing by 0 just gives 255. We're later multiplying
// by 0 again, so it won't affect results.
const alpha = inOutColor[3] / 255;
inOutColor[0] /= alpha;
inOutColor[1] /= alpha;
inOutColor[2] /= alpha;
// vec3 hsl = convertRGB2HSL(gl_FragColor.xyz);
const hsl = rgbToHsl(dst);
const hsl = rgbToHsl(inOutColor);
if (enableColor) {
// this code forces grayscale values to be slightly saturated
@@ -173,25 +176,38 @@ class EffectTransform {
hsl[2] = Math.min(1, hsl[2] + uniforms.u_brightness);
}
// gl_FragColor.rgb = convertHSL2RGB(hsl);
dst.set(hslToRgb(hsl));
inOutColor.set(hslToRgb(hsl));
// gl_FragColor.rgb *= gl_FragColor.a + epsilon;
// Now we're doing the reverse, premultiplying by the alpha once again.
inOutColor[0] *= alpha;
inOutColor[1] *= alpha;
inOutColor[2] *= alpha;
}
return dst;
if ((effects & ShaderManager.EFFECT_INFO.ghost.mask) !== 0) {
// gl_FragColor *= u_ghost
inOutColor[0] *= uniforms.u_ghost;
inOutColor[1] *= uniforms.u_ghost;
inOutColor[2] *= uniforms.u_ghost;
inOutColor[3] *= uniforms.u_ghost;
}
return inOutColor;
}
/**
* Transform a texture coordinate to one that would be select after applying shader effects.
* @param {Drawable} drawable The drawable whose effects to emulate.
* @param {twgl.v3} vec The texture coordinate to transform.
* @param {?twgl.v3} dst A place to store the output coordinate.
* @param {twgl.v3} dst A place to store the output coordinate.
* @return {twgl.v3} dst - The coordinate after being transform by effects.
*/
static transformPoint (drawable, vec, dst = twgl.v3.create()) {
static transformPoint (drawable, vec, dst) {
twgl.v3.copy(vec, dst);
const effects = drawable.enabledEffects;
const uniforms = drawable.getUniforms();
const effects = drawable.getEnabledEffects();
if ((effects & ShaderManager.EFFECT_INFO.mosaic.mask) !== 0) {
// texcoord0 = fract(u_mosaic * texcoord0);
dst[0] = uniforms.u_mosaic * dst[0] % 1;

View File

@@ -25,6 +25,12 @@ const DefaultPenAttributes = {
diameter: 1
};
/**
* Reused memory location for storing a premultiplied pen color.
* @type {FloatArray}
*/
const __premultipliedColor = [0, 0, 0, 0];
/**
* Reused memory location for projection matrices.
@@ -88,9 +94,6 @@ class PenSkin extends Skin {
/** @type {HTMLCanvasElement} */
this._canvas = document.createElement('canvas');
/** @type {WebGLTexture} */
this._texture = null;
/** @type {WebGLTexture} */
this._exportTexture = null;
@@ -123,7 +126,7 @@ class PenSkin extends Skin {
const NO_EFFECTS = 0;
/** @type {twgl.ProgramInfo} */
this._stampShader = this._renderer._shaderManager.getShader(ShaderManager.DRAW_MODE.stamp, NO_EFFECTS);
this._stampShader = this._renderer._shaderManager.getShader(ShaderManager.DRAW_MODE.default, NO_EFFECTS);
/** @type {twgl.ProgramInfo} */
this._lineShader = this._renderer._shaderManager.getShader(ShaderManager.DRAW_MODE.lineSample, NO_EFFECTS);
@@ -154,13 +157,6 @@ 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]
*/
@@ -188,7 +184,7 @@ class PenSkin extends Skin {
clear () {
const gl = this._renderer.gl;
twgl.bindFramebufferInfo(gl, this._framebuffer);
/* Reset framebuffer to transparent black */
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
@@ -317,13 +313,6 @@ class PenSkin extends Skin {
twgl.bindFramebufferInfo(gl, this._framebuffer);
// Needs a blend function that blends a destination that starts with
// no alpha.
gl.blendFuncSeparate(
gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA,
gl.ONE, gl.ONE_MINUS_SRC_ALPHA
);
gl.viewport(0, 0, bounds.width, bounds.height);
gl.useProgram(currentShader.program);
@@ -344,8 +333,6 @@ class PenSkin extends Skin {
_exitDrawLineOnBuffer () {
const gl = this._renderer.gl;
gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE);
twgl.bindFramebufferInfo(gl, null);
}
@@ -384,6 +371,13 @@ class PenSkin extends Skin {
const radius = diameter / 2;
const yScalar = (0.50001 - (radius / (length + diameter)));
// Premultiply pen color by pen transparency
const penColor = penAttributes.color4f || DefaultPenAttributes.color4f;
__premultipliedColor[0] = penColor[0] * penColor[3];
__premultipliedColor[1] = penColor[1] * penColor[3];
__premultipliedColor[2] = penColor[2] * penColor[3];
__premultipliedColor[3] = penColor[3];
const uniforms = {
u_positionScalar: yScalar,
u_capScale: diameter,
@@ -397,7 +391,7 @@ class PenSkin extends Skin {
twgl.m4.scaling(scalingVector, __modelScalingMatrix),
__modelMatrix
),
u_lineColor: penAttributes.color4f || DefaultPenAttributes.color4f
u_lineColor: __premultipliedColor
};
twgl.setUniforms(currentShader, uniforms);
@@ -490,8 +484,6 @@ class PenSkin extends Skin {
twgl.bindFramebufferInfo(gl, this._framebuffer);
gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
this._drawRectangleRegionEnter(this._stampShader, this._bounds);
}
@@ -501,8 +493,6 @@ class PenSkin extends Skin {
_exitDrawToBuffer () {
const gl = this._renderer.gl;
gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE);
twgl.bindFramebufferInfo(gl, null);
}
@@ -661,7 +651,7 @@ class PenSkin extends Skin {
skinImageData.data.set(skinPixels);
skinContext.putImageData(skinImageData, 0, 0);
this._silhouette.update(this._canvas);
this._silhouette.update(this._canvas, true /* isPremultiplied */);
this._silhouetteDirty = false;
}

View File

@@ -196,7 +196,7 @@ class RenderWebGL extends EventEmitter {
gl.disable(gl.DEPTH_TEST);
/** @todo disable when no partial transparency? */
gl.enable(gl.BLEND);
gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
}
/**
@@ -834,7 +834,8 @@ class RenderWebGL extends EventEmitter {
projection,
{
extraUniforms,
ignoreVisibility: true // Touching color ignores sprite visibility
ignoreVisibility: true, // Touching color ignores sprite visibility,
effectMask: ~ShaderManager.EFFECT_INFO.ghost.mask
});
gl.stencilFunc(gl.EQUAL, 1, 1);
@@ -972,7 +973,7 @@ class RenderWebGL extends EventEmitter {
drawable.updateMatrix();
if (drawable.skin) {
drawable.skin.updateSilhouette();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
} else {
log.warn(`Could not find skin for drawable with id: ${drawableID}`);
}
@@ -1008,7 +1009,7 @@ class RenderWebGL extends EventEmitter {
if (drawable.getVisible() && drawable.getUniforms().u_ghost !== 0) {
drawable.updateMatrix();
if (drawable.skin) {
drawable.skin.updateSilhouette();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
} else {
log.warn(`Could not find skin for drawable with id: ${id}`);
}
@@ -1243,7 +1244,7 @@ class RenderWebGL extends EventEmitter {
if (!drawable.skin || !drawable.skin.getTexture([100, 100])) return null;
drawable.updateMatrix();
drawable.skin.updateSilhouette();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
const bounds = drawable.getFastBounds();
// Limit queries to the stage size.
@@ -1281,7 +1282,7 @@ class RenderWebGL extends EventEmitter {
if (drawable.skin && drawable._visible) {
// Update the CPU position data
drawable.updateMatrix();
drawable.skin.updateSilhouette();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
const candidateBounds = drawable.getFastBounds();
if (bounds.intersects(candidateBounds)) {
result.push({
@@ -1542,23 +1543,20 @@ class RenderWebGL extends EventEmitter {
const skin = /** @type {PenSkin} */ this._allSkins[penSkinID];
const gl = this._gl;
twgl.bindFramebufferInfo(gl, this._queryBufferInfo);
twgl.bindFramebufferInfo(gl, skin._framebuffer);
// Limit size of viewport to the bounds around the stamp Drawable and create the projection matrix for the draw.
gl.viewport(0, 0, bounds.width, bounds.height);
gl.viewport(
(this._nativeSize[0] * 0.5) + bounds.left,
(this._nativeSize[1] * 0.5) - bounds.top,
bounds.width,
bounds.height
);
const projection = twgl.m4.ortho(bounds.left, bounds.right, bounds.top, bounds.bottom, -1, 1);
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
try {
gl.disable(gl.BLEND);
this._drawThese([stampID], ShaderManager.DRAW_MODE.stamp, projection, {ignoreVisibility: true});
} finally {
gl.enable(gl.BLEND);
}
skin._drawToBuffer(this._queryBufferInfo.attachments[0], bounds.left, bounds.top);
// Draw the stamped sprite onto the PenSkin's framebuffer.
this._drawThese([stampID], ShaderManager.DRAW_MODE.default, projection, {ignoreVisibility: true});
skin._silhouetteDirty = true;
}
/* ******
@@ -1662,6 +1660,18 @@ class RenderWebGL extends EventEmitter {
this._regionId = null;
}
/**
* Get the screen-space scale of a drawable, as percentages of the drawable's "normal" size.
* @param {Drawable} drawable The drawable whose screen-space scale we're fetching.
* @returns {Array<number>} The screen-space X and Y dimensions of the drawable's scale, as percentages.
*/
_getDrawableScreenSpaceScale (drawable) {
return [
drawable.scale[0] * this._gl.canvas.width / this._nativeSize[0],
drawable.scale[1] * this._gl.canvas.height / this._nativeSize[1]
];
}
/**
* Draw a set of Drawables, by drawable ID
* @param {Array<int>} drawables The Drawable IDs to draw, possibly this._drawList.
@@ -1694,17 +1704,14 @@ class RenderWebGL extends EventEmitter {
if (!drawable.getVisible() && !opts.ignoreVisibility) continue;
// Combine drawable scale with the native vs. backing pixel ratio
const drawableScale = [
drawable.scale[0] * this._gl.canvas.width / this._nativeSize[0],
drawable.scale[1] * this._gl.canvas.height / this._nativeSize[1]
];
const drawableScale = this._getDrawableScreenSpaceScale(drawable);
// If the skin or texture isn't ready yet, skip it.
if (!drawable.skin || !drawable.skin.getTexture(drawableScale)) continue;
const uniforms = {};
let effectBits = drawable.getEnabledEffects();
let effectBits = drawable.enabledEffects;
effectBits &= opts.hasOwnProperty('effectMask') ? opts.effectMask : effectBits;
const newShader = this._shaderManager.getShader(drawMode, effectBits);
@@ -1738,14 +1745,6 @@ 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);
}
@@ -1896,13 +1895,11 @@ class RenderWebGL extends EventEmitter {
}
*/
Drawable.sampleColor4b(vec, drawables[index].drawable, __blendColor);
// if we are fully transparent, go to the next one "down"
const sampleAlpha = __blendColor[3] / 255;
// premultiply alpha
dst[0] += __blendColor[0] * blendAlpha * sampleAlpha;
dst[1] += __blendColor[1] * blendAlpha * sampleAlpha;
dst[2] += __blendColor[2] * blendAlpha * sampleAlpha;
blendAlpha *= (1 - sampleAlpha);
// Equivalent to gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
dst[0] += __blendColor[0] * blendAlpha;
dst[1] += __blendColor[1] * blendAlpha;
dst[2] += __blendColor[2] * blendAlpha;
blendAlpha *= (1 - (__blendColor[3] / 255));
}
// Backdrop could be transparent, so we need to go to the "clear color" of the
// draw scene (white) as a fallback if everything was alpha

View File

@@ -4,6 +4,14 @@ const Skin = require('./Skin');
const SvgRenderer = require('scratch-svg-renderer').SVGRenderer;
const MAX_TEXTURE_DIMENSION = 2048;
const MIN_TEXTURE_SCALE = 1 / 256;
/**
* All scaled renderings of the SVG are stored in an array. The 1.0 scale of
* the SVG is stored at the 8th index. The smallest possible 1 / 256 scale
* rendering is stored at the 0th index.
* @const {number}
*/
const INDEX_OFFSET = 8;
class SVGSkin extends Skin {
/**
@@ -22,24 +30,24 @@ class SVGSkin extends Skin {
/** @type {SvgRenderer} */
this._svgRenderer = new SvgRenderer();
/** @type {WebGLTexture} */
this._texture = null;
/** @type {Array<WebGLTexture>} */
this._scaledMIPs = [];
/** @type {number} */
this._textureScale = 1;
this._largestMIPScale = 0;
/** @type {Number} */
this._maxTextureScale = 0;
/**
* Ratio of the size of the SVG and the max size of the WebGL texture
* @type {Number}
*/
this._maxTextureScale = 1;
}
/**
* Dispose of this object. Do not use it after calling this method.
*/
dispose () {
if (this._texture) {
this._renderer.gl.deleteTexture(this._texture);
this._texture = null;
}
this.resetMIPs();
super.dispose();
}
@@ -60,89 +68,111 @@ class SVGSkin extends Skin {
super.setRotationCenter(x - viewOffset[0], y - viewOffset[1]);
}
/**
* Create a MIP for a given scale.
* @param {number} scale - The relative size of the MIP
* @return {SVGMIP} An object that handles creating and updating SVG textures.
*/
createMIP (scale) {
this._svgRenderer.draw(scale);
// 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;
// If one of the canvas dimensions is 0, set this MIP to an empty image texture.
// This avoids an IndexSizeError from attempting to getImageData when one of the dimensions is 0.
if (canvas.width === 0 || canvas.height === 0) return super.getTexture();
const context = canvas.getContext('2d');
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);
const textureOptions = {
auto: false,
wrap: this._renderer.gl.CLAMP_TO_EDGE,
src: textureData,
premultiplyAlpha: true
};
const mip = twgl.createTexture(this._renderer.gl, textureOptions);
// Check if this is the largest MIP created so far. Currently, silhouettes only get scaled up.
if (this._largestMIPScale < scale) {
this._silhouette.update(textureData);
this._largestMIPScale = scale;
}
return mip;
}
updateSilhouette (scale = 1) {
// Ensure a silhouette exists.
this.getTexture(scale);
}
/**
* @param {Array<number>} scale - The scaling factors to be used, each in the [0,100] range.
* @return {WebGLTexture} The GL texture representation of this skin when drawing at the given scale.
*/
// eslint-disable-next-line no-unused-vars
getTexture (scale) {
if (!this._svgRenderer.canvas.width || !this._svgRenderer.canvas.height) {
return super.getTexture();
}
// The texture only ever gets uniform scale. Take the larger of the two axes.
const scaleMax = scale ? Math.max(Math.abs(scale[0]), Math.abs(scale[1])) : 100;
const requestedScale = Math.min(scaleMax / 100, this._maxTextureScale);
let newScale = this._textureScale;
while ((newScale < this._maxTextureScale) && (requestedScale >= 1.5 * newScale)) {
newScale *= 2;
}
if (this._textureScale !== newScale) {
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);
let newScale = 1;
let textureIndex = 0;
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, textureData);
this._silhouette.update(textureData);
}
});
if (requestedScale < 1) {
while ((newScale > MIN_TEXTURE_SCALE) && (requestedScale <= newScale * .75)) {
newScale /= 2;
textureIndex -= 1;
}
} else {
while ((newScale < this._maxTextureScale) && (requestedScale >= 1.5 * newScale)) {
newScale *= 2;
textureIndex += 1;
}
}
return this._texture;
if (this._svgRenderer.loaded && !this._scaledMIPs[textureIndex + INDEX_OFFSET]) {
this._scaledMIPs[textureIndex + INDEX_OFFSET] = this.createMIP(newScale);
}
return this._scaledMIPs[textureIndex + INDEX_OFFSET] || super.getTexture();
}
/**
* Do a hard reset of the existing MIPs by deleting them.
* @param {Array<number>} [rotationCenter] - Optional rotation center for the SVG. If not supplied, it will be
* calculated from the bounding box
* @fires Skin.event:WasAltered
*/
resetMIPs () {
this._scaledMIPs.forEach(oldMIP => this._renderer.gl.deleteTexture(oldMIP));
this._scaledMIPs.length = 0;
this._largestMIPScale = 0;
}
/**
* Set the contents of this skin to a snapshot of the provided SVG data.
* @param {string} svgData - new SVG to use.
* @param {Array<number>} [rotationCenter] - Optional rotation center for the SVG. If not supplied, it will be
* calculated from the bounding box
* @fires Skin.event:WasAltered
* @param {Array<number>} [rotationCenter] - Optional rotation center for the SVG.
*/
setSVG (svgData, rotationCenter) {
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;
if (!canvas.width || !canvas.height) {
this._svgRenderer.loadSVG(svgData, false, () => {
const svgSize = this._svgRenderer.size;
if (svgSize[0] === 0 || svgSize[1] === 0) {
super.setEmptyImageData();
return;
}
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, textureData);
this._silhouette.update(textureData);
} else {
// TODO: mipmaps?
const textureOptions = {
auto: true,
wrap: gl.CLAMP_TO_EDGE,
src: textureData
};
this._texture = twgl.createTexture(gl, textureOptions);
this._silhouette.update(textureData);
}
const maxDimension = Math.max(this._svgRenderer.canvas.width, this._svgRenderer.canvas.height);
const maxDimension = Math.ceil(Math.max(this.size[0], this.size[1]));
let testScale = 2;
for (testScale; maxDimension * testScale <= MAX_TEXTURE_DIMENSION; testScale *= 2) {
this._maxTextureScale = testScale;
}
this.resetMIPs();
if (typeof rotationCenter === 'undefined') rotationCenter = this.calculateRotationCenter();
this.setRotationCenter.apply(this, rotationCenter);
this.emit(Skin.Events.WasAltered);

View File

@@ -171,12 +171,7 @@ ShaderManager.DRAW_MODE = {
/**
* Sample a "texture" to draw a line with caps.
*/
lineSample: 'lineSample',
/**
* Draw normally except for pre-multiplied alpha
*/
stamp: 'stamp'
lineSample: 'lineSample'
};
module.exports = ShaderManager;

View File

@@ -20,7 +20,7 @@ let __SilhouetteUpdateCanvas;
* @return {number} Alpha value for x/y position
*/
const getPoint = ({_width: width, _height: height, _colorData: data}, x, y) => {
// 0 if outside bouds, otherwise read from data.
// 0 if outside bounds, otherwise read from data.
if (x >= width || y >= height || x < 0 || y < 0) {
return 0;
}
@@ -39,6 +39,7 @@ const __cornerWork = [
/**
* Get the color from a given silhouette at an x/y local texture position.
* Multiply color values by alpha for proper blending.
* @param {Silhouette} The silhouette to sample.
* @param {number} x X position of texture (0-1).
* @param {number} y Y position of texture (0-1).
@@ -46,7 +47,31 @@ const __cornerWork = [
* @return {Uint8ClampedArray} The dst vector.
*/
const getColor4b = ({_width: width, _height: height, _colorData: data}, x, y, dst) => {
// 0 if outside bouds, otherwise read from data.
// 0 if outside bounds, otherwise read from data.
if (x >= width || y >= height || x < 0 || y < 0) {
return dst.fill(0);
}
const offset = ((y * width) + x) * 4;
// premultiply alpha
const alpha = data[offset + 3] / 255;
dst[0] = data[offset] * alpha;
dst[1] = data[offset + 1] * alpha;
dst[2] = data[offset + 2] * alpha;
dst[3] = data[offset + 3];
return dst;
};
/**
* Get the color from a given silhouette at an x/y local texture position.
* Do not multiply color values by alpha, as it has already been done.
* @param {Silhouette} The silhouette to sample.
* @param {number} x X position of texture (0-1).
* @param {number} y Y position of texture (0-1).
* @param {Uint8ClampedArray} dst A color 4b space.
* @return {Uint8ClampedArray} The dst vector.
*/
const getPremultipliedColor4b = ({_width: width, _height: height, _colorData: data}, x, y, dst) => {
// 0 if outside bounds, otherwise read from data.
if (x >= width || y >= height || x < 0 || y < 0) {
return dst.fill(0);
}
@@ -78,15 +103,21 @@ class Silhouette {
*/
this._colorData = null;
// By default, silhouettes are assumed not to contain premultiplied image data,
// so when we get a color, we want to multiply it by its alpha channel.
// Point `_getColor` to the version of the function that multiplies.
this._getColor = getColor4b;
this.colorAtNearest = this.colorAtLinear = (_, dst) => dst.fill(0);
}
/**
* Update this silhouette with the bitmapData for a skin.
* @param {*} bitmapData An image, canvas or other element that the skin
* @param {ImageData|HTMLCanvasElement|HTMLImageElement} bitmapData An image, canvas or other element that the skin
* @param {boolean} isPremultiplied True if the source bitmap data comes premultiplied (e.g. from readPixels).
* rendering can be queried from.
*/
update (bitmapData) {
update (bitmapData, isPremultiplied = false) {
let imageData;
if (bitmapData instanceof ImageData) {
// If handed ImageData directly, use it directly.
@@ -109,6 +140,12 @@ class Silhouette {
imageData = ctx.getImageData(0, 0, width, height);
}
if (isPremultiplied) {
this._getColor = getPremultipliedColor4b;
} else {
this._getColor = getColor4b;
}
this._colorData = imageData.data;
// delete our custom overriden "uninitalized" color functions
// let the prototype work for itself
@@ -124,7 +161,7 @@ class Silhouette {
* @returns {Uint8ClampedArray} dst
*/
colorAtNearest (vec, dst) {
return getColor4b(
return this._getColor(
this,
Math.floor(vec[0] * (this._width - 1)),
Math.floor(vec[1] * (this._height - 1)),
@@ -151,10 +188,10 @@ class Silhouette {
const xFloor = Math.floor(x);
const yFloor = Math.floor(y);
const x0y0 = getColor4b(this, xFloor, yFloor, __cornerWork[0]);
const x1y0 = getColor4b(this, xFloor + 1, yFloor, __cornerWork[1]);
const x0y1 = getColor4b(this, xFloor, yFloor + 1, __cornerWork[2]);
const x1y1 = getColor4b(this, xFloor + 1, yFloor + 1, __cornerWork[3]);
const x0y0 = this._getColor(this, xFloor, yFloor, __cornerWork[0]);
const x1y0 = this._getColor(this, xFloor + 1, yFloor, __cornerWork[1]);
const x0y1 = this._getColor(this, xFloor, yFloor + 1, __cornerWork[2]);
const x1y1 = this._getColor(this, xFloor + 1, yFloor + 1, __cornerWork[3]);
dst[0] = (x0y0[0] * x0D * y0D) + (x0y1[0] * x0D * y1D) + (x1y0[0] * x1D * y0D) + (x1y1[0] * x1D * y1D);
dst[1] = (x0y0[1] * x0D * y0D) + (x0y1[1] * x0D * y1D) + (x1y0[1] * x1D * y0D) + (x1y1[1] * x1D * y1D);

View File

@@ -33,6 +33,9 @@ class Skin extends EventEmitter {
/** @type {Vec3} */
this._rotationCenter = twgl.v3.create(0, 0);
/** @type {WebGLTexture} */
this._texture = null;
/**
* The uniforms to be used by the vertex and pixel shaders.
* Some of these are used by other parts of the renderer as well.
@@ -76,13 +79,6 @@ 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.
*/
@@ -171,6 +167,23 @@ class Skin extends EventEmitter {
*/
updateSilhouette () {}
/**
* Set this skin's texture to the given image.
* @param {ImageData|HTMLCanvasElement} textureData - The canvas or image data to set the texture to.
*/
_setTexture (textureData) {
const gl = this._renderer.gl;
gl.bindTexture(gl.TEXTURE_2D, this._texture);
// Premultiplied alpha is necessary for proper blending.
// See http://www.realtimerendering.com/blog/gpus-prefer-premultiplication/
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
this._silhouette.update(textureData);
}
/**
* Set the contents of this skin to an empty skin.
* @fires Skin.event:WasAltered

View File

@@ -42,9 +42,6 @@ class TextBubbleSkin extends Skin {
/** @type {HTMLCanvasElement} */
this._canvas = document.createElement('canvas');
/** @type {WebGLTexture} */
this._texture = null;
/** @type {Array<number>} */
this._size = [0, 0];
@@ -272,9 +269,7 @@ class TextBubbleSkin extends Skin {
this._texture = twgl.createTexture(gl, textureOptions);
}
gl.bindTexture(gl.TEXTURE_2D, this._texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData);
this._silhouette.update(textureData);
this._setTexture(textureData);
}
return this._texture;

View File

@@ -43,14 +43,16 @@ uniform sampler2D u_skin;
varying vec2 v_texCoord;
// Add this to divisors to prevent division by 0, which results in NaNs propagating through calculations.
// Smaller values can cause problems on some mobile devices.
const float epsilon = 1e-3;
#if !defined(DRAW_MODE_silhouette) && (defined(ENABLE_color))
// Branchless color conversions based on code from:
// http://www.chilliant.com/rgb2hsv.html by Ian Taylor
// Based in part on work by Sam Hocevar and Emil Persson
// See also: https://en.wikipedia.org/wiki/HSL_and_HSV#Formal_derivation
// Smaller values can cause problems on some mobile devices
const float epsilon = 1e-3;
// Convert an RGB color to Hue, Saturation, and Value.
// All components of input and output are expected to be in the [0,1] range.
@@ -153,16 +155,12 @@ void main()
gl_FragColor = texture2D(u_skin, texcoord0);
#ifdef ENABLE_ghost
gl_FragColor.a *= u_ghost;
#endif // ENABLE_ghost
#if defined(ENABLE_color) || defined(ENABLE_brightness)
// Divide premultiplied alpha values for proper color processing
// Add epsilon to avoid dividing by 0 for fully transparent pixels
gl_FragColor.rgb = clamp(gl_FragColor.rgb / (gl_FragColor.a + epsilon), 0.0, 1.0);
#ifdef DRAW_MODE_silhouette
// switch to u_silhouetteColor only AFTER the alpha test
gl_FragColor = u_silhouetteColor;
#else // DRAW_MODE_silhouette
#if defined(ENABLE_color)
#ifdef ENABLE_color
{
vec3 hsv = convertRGB2HSV(gl_FragColor.xyz);
@@ -178,11 +176,29 @@ void main()
gl_FragColor.rgb = convertHSV2RGB(hsv);
}
#endif // defined(ENABLE_color)
#endif // ENABLE_color
#if defined(ENABLE_brightness)
#ifdef ENABLE_brightness
gl_FragColor.rgb = clamp(gl_FragColor.rgb + vec3(u_brightness), vec3(0), vec3(1));
#endif // defined(ENABLE_brightness)
#endif // ENABLE_brightness
// Re-multiply color values
gl_FragColor.rgb *= gl_FragColor.a + epsilon;
#endif // defined(ENABLE_color) || defined(ENABLE_brightness)
#ifdef ENABLE_ghost
gl_FragColor *= u_ghost;
#endif // ENABLE_ghost
#ifdef DRAW_MODE_silhouette
// Discard fully transparent pixels for stencil test
if (gl_FragColor.a == 0.0) {
discard;
}
// switch to u_silhouetteColor only AFTER the alpha test
gl_FragColor = u_silhouetteColor;
#else // DRAW_MODE_silhouette
#ifdef DRAW_MODE_colorMask
vec3 maskDistance = abs(gl_FragColor.rgb - u_colorMask);
@@ -195,8 +211,7 @@ void main()
#endif // DRAW_MODE_silhouette
#else // DRAW_MODE_lineSample
gl_FragColor = u_lineColor;
gl_FragColor.a *= clamp(
gl_FragColor = u_lineColor * clamp(
// Scale the capScale a little to have an aliased region.
(u_capScale + u_aliasAmount -
u_capScale * 2.0 * distance(v_texCoord, vec2(0.5, 0.5))