diff --git a/src/BitmapSkin.js b/src/BitmapSkin.js index b4b03d3..94f2984 100644 --- a/src/BitmapSkin.js +++ b/src/BitmapSkin.js @@ -62,10 +62,11 @@ class BitmapSkin extends Skin { /** * Get the bounds of the drawable for determining its fenced position. * @param {Array} drawable - The Drawable instance this skin is using. + * @param {?Rectangle} result - Optional destination for bounds calculation. * @return {!Rectangle} The drawable's bounds. For compatibility with Scratch 2, we always use getAABB for bitmaps. */ - getFenceBounds (drawable) { - return drawable.getAABB(); + getFenceBounds (drawable, result) { + return drawable.getAABB(result); } /** diff --git a/src/Drawable.js b/src/Drawable.js index 8c87033..6ab8a1c 100644 --- a/src/Drawable.js +++ b/src/Drawable.js @@ -451,9 +451,10 @@ class Drawable { * This function applies the transform matrix to the known convex hull, * and then finds the minimum box along the axes. * Before calling this, ensure the renderer has updated convex hull points. + * @param {?Rectangle} result optional destination for bounds calculation * @return {!Rectangle} Bounds for a tight box around the Drawable. */ - getBounds (bounds) { + getBounds (result) { if (this.needsConvexHullPoints()) { throw new Error('Needs updated convex hull points before bounds calculation.'); } @@ -462,18 +463,19 @@ class Drawable { } const transformedHullPoints = this._getTransformedHullPoints(); // Search through transformed points to generate box on axes. - bounds = bounds || new Rectangle(); - bounds.initFromPointsAABB(transformedHullPoints); - return bounds; + result = result || new Rectangle(); + result.initFromPointsAABB(transformedHullPoints); + return result; } /** * Get the precise bounds for the upper 8px slice of the Drawable. * Used for calculating where to position a text bubble. * Before calling this, ensure the renderer has updated convex hull points. + * @param {?Rectangle} result optional destination for bounds calculation * @return {!Rectangle} Bounds for a tight box around a slice of the Drawable. */ - getBoundsForBubble (bounds) { + getBoundsForBubble (result) { if (this.needsConvexHullPoints()) { throw new Error('Needs updated convex hull points before bubble bounds calculation.'); } @@ -485,9 +487,9 @@ class Drawable { const maxY = Math.max.apply(null, transformedHullPoints.map(p => p[1])); const filteredHullPoints = transformedHullPoints.filter(p => p[1] > maxY - slice); // Search through filtered points to generate box on axes. - bounds = bounds || new Rectangle(); - bounds.initFromPointsAABB(filteredHullPoints); - return bounds; + result = result || new Rectangle(); + result.initFromPointsAABB(filteredHullPoints); + return result; } /** @@ -497,30 +499,32 @@ class Drawable { * which is tightly snapped to account for a Drawable's transparent regions. * `getAABB` returns a much less accurate bounding box, but will be much * faster to calculate so may be desired for quick checks/optimizations. + * @param {?Rectangle} result optional destination for bounds calculation * @return {!Rectangle} Rough axis-aligned bounding box for Drawable. */ - getAABB (bounds) { + getAABB (result) { if (this._transformDirty) { this._calculateTransform(); } const tm = this._uniforms.u_modelMatrix; - bounds = bounds || new Rectangle(); - bounds.initFromMatrixRadius(tm, 0.5); - return bounds; + result = result || new Rectangle(); + result.initFromMatrixRadius(tm, 0.5); + return result; } /** * Return the best Drawable bounds possible without performing graphics queries. * I.e., returns the tight bounding box when the convex hull points are already * known, but otherwise return the rough AABB of the Drawable. + * @param {?Rectangle} result optional destination for bounds calculation * @return {!Rectangle} Bounds for the Drawable. */ - getFastBounds (bounds) { + getFastBounds (result) { this.updateMatrix(); if (!this.needsConvexHullPoints()) { - return this.getBounds(bounds); + return this.getBounds(result); } - return this.getAABB(bounds); + return this.getAABB(result); } /** diff --git a/src/RenderWebGL.js b/src/RenderWebGL.js index 4782455..ba629ea 100644 --- a/src/RenderWebGL.js +++ b/src/RenderWebGL.js @@ -16,6 +16,7 @@ const log = require('./util/log'); const __isTouchingDrawablesPoint = twgl.v3.create(); const __candidatesBounds = new Rectangle(); +const __fenceBounds = new Rectangle(); const __touchingColor = new Uint8ClampedArray(4); const __blendColor = new Uint8ClampedArray(4); @@ -1357,7 +1358,7 @@ class RenderWebGL extends EventEmitter { const dx = x - drawable._position[0]; const dy = y - drawable._position[1]; - const aabb = drawable._skin.getFenceBounds(drawable); + const aabb = drawable._skin.getFenceBounds(drawable, __fenceBounds); const inset = Math.floor(Math.min(aabb.width, aabb.height) / 2); const sx = this._xRight - Math.min(FENCE_WIDTH, inset); @@ -1627,14 +1628,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); } diff --git a/src/Skin.js b/src/Skin.js index 8d124be..473c70b 100644 --- a/src/Skin.js +++ b/src/Skin.js @@ -146,10 +146,11 @@ class Skin extends EventEmitter { /** * Get the bounds of the drawable for determining its fenced position. * @param {Array} drawable - The Drawable instance this skin is using. + * @param {?Rectangle} result - Optional destination for bounds calculation. * @return {!Rectangle} The drawable's bounds. */ - getFenceBounds (drawable, bounds) { - return drawable.getFastBounds(bounds); + getFenceBounds (drawable, result) { + return drawable.getFastBounds(result); } /**