From 5be561133da590ce2e6c822a2042bf6f8aea498a Mon Sep 17 00:00:00 2001 From: DD Liu Date: Sat, 5 Jan 2019 00:43:55 -0500 Subject: [PATCH 1/2] Increase maximum texture size and back it off if GL fails to render --- src/SVGSkin.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/SVGSkin.js b/src/SVGSkin.js index b99998c..0c0937d 100644 --- a/src/SVGSkin.js +++ b/src/SVGSkin.js @@ -3,7 +3,7 @@ const twgl = require('twgl.js'); const Skin = require('./Skin'); const SvgRenderer = require('scratch-svg-renderer').SVGRenderer; -const MAX_TEXTURE_DIMENSION = 2048; +const MAX_TEXTURE_DIMENSION = 10240; class SVGSkin extends Skin { /** @@ -68,21 +68,28 @@ class SVGSkin extends Skin { getTexture (scale) { // 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); + const requestedScale = scaleMax / 100; 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 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); - } - }); - } + + let error; + const callback = () => { + if (this._textureScale === newScale) { + 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); + error = gl.getError(); + } + }; + do { + if (this._textureScale !== newScale) { + this._textureScale = newScale; + this._svgRenderer._draw(this._textureScale, callback); + } + newScale /= 2; + } while (error && newScale > 1); return this._texture; } From 0676e0f54af521831b0da54060a973641259e68e Mon Sep 17 00:00:00 2001 From: DD Liu Date: Mon, 7 Jan 2019 10:00:31 -0500 Subject: [PATCH 2/2] Use callback for loop, and update maxTextureScale during callback --- src/SVGSkin.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/SVGSkin.js b/src/SVGSkin.js index 0c0937d..bc4ab1e 100644 --- a/src/SVGSkin.js +++ b/src/SVGSkin.js @@ -81,15 +81,18 @@ class SVGSkin extends Skin { gl.bindTexture(gl.TEXTURE_2D, this._texture); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._svgRenderer.canvas); error = gl.getError(); + if (error && newScale > 1) { + newScale /= 2; + this._textureScale = newScale; + this._maxTextureScale = newScale; + this._svgRenderer._draw(this._textureScale, callback); + } } }; - do { - if (this._textureScale !== newScale) { - this._textureScale = newScale; - this._svgRenderer._draw(this._textureScale, callback); - } - newScale /= 2; - } while (error && newScale > 1); + if (this._textureScale !== newScale) { + this._textureScale = newScale; + this._svgRenderer._draw(this._textureScale, callback); + } return this._texture; }