diff --git a/src/Drawable.js b/src/Drawable.js index 1d90a29..8c87033 100644 --- a/src/Drawable.js +++ b/src/Drawable.js @@ -453,7 +453,7 @@ class Drawable { * Before calling this, ensure the renderer has updated convex hull points. * @return {!Rectangle} Bounds for a tight box around the Drawable. */ - getBounds () { + getBounds (bounds) { if (this.needsConvexHullPoints()) { throw new Error('Needs updated convex hull points before bounds calculation.'); } @@ -462,7 +462,7 @@ class Drawable { } const transformedHullPoints = this._getTransformedHullPoints(); // Search through transformed points to generate box on axes. - const bounds = new Rectangle(); + bounds = bounds || new Rectangle(); bounds.initFromPointsAABB(transformedHullPoints); return bounds; } @@ -473,7 +473,7 @@ class Drawable { * Before calling this, ensure the renderer has updated convex hull points. * @return {!Rectangle} Bounds for a tight box around a slice of the Drawable. */ - getBoundsForBubble () { + getBoundsForBubble (bounds) { if (this.needsConvexHullPoints()) { throw new Error('Needs updated convex hull points before bubble bounds calculation.'); } @@ -485,7 +485,7 @@ 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. - const bounds = new Rectangle(); + bounds = bounds || new Rectangle(); bounds.initFromPointsAABB(filteredHullPoints); return bounds; } @@ -499,18 +499,13 @@ class Drawable { * faster to calculate so may be desired for quick checks/optimizations. * @return {!Rectangle} Rough axis-aligned bounding box for Drawable. */ - getAABB () { + getAABB (bounds) { if (this._transformDirty) { this._calculateTransform(); } const tm = this._uniforms.u_modelMatrix; - const bounds = new Rectangle(); - bounds.initFromPointsAABB([ - twgl.m4.transformPoint(tm, [-0.5, -0.5, 0]), - twgl.m4.transformPoint(tm, [0.5, -0.5, 0]), - twgl.m4.transformPoint(tm, [-0.5, 0.5, 0]), - twgl.m4.transformPoint(tm, [0.5, 0.5, 0]) - ]); + bounds = bounds || new Rectangle(); + bounds.initFromMatrixRadius(tm, 0.5); return bounds; } @@ -520,12 +515,12 @@ class Drawable { * known, but otherwise return the rough AABB of the Drawable. * @return {!Rectangle} Bounds for the Drawable. */ - getFastBounds () { + getFastBounds (bounds) { this.updateMatrix(); if (!this.needsConvexHullPoints()) { - return this.getBounds(); + return this.getBounds(bounds); } - return this.getAABB(); + return this.getAABB(bounds); } /** diff --git a/src/Rectangle.js b/src/Rectangle.js index 7659e22..726a151 100644 --- a/src/Rectangle.js +++ b/src/Rectangle.js @@ -54,6 +54,31 @@ class Rectangle { } } + initFromMatrixRadius (m, r) { + // const v0 = r; + // const v1 = r; + // const v2 = r; + const m00 = m[(0 * 4) + 0]; + const m01 = m[(0 * 4) + 1]; + const m10 = m[(1 * 4) + 0]; + const m11 = m[(1 * 4) + 1]; + const m30 = m[(3 * 4) + 0]; + const m31 = m[(3 * 4) + 1]; + // var d = v0 * m03 + v1 * m13 + v2 * m23 + m33; + // dst[0] = ( + const x = Math.abs(r * m00) + Math.abs(r * m10); + // + v2 * m20 + m30) / d; + // dst[1] = ( + const y = Math.abs(r * m01) + Math.abs(r * m11); + // + v2 * m21 + m31) / d; + // dst[2] = (v0 * m02 + v1 * m12 + v2 * m22 + m32) / d; + + this.left = -x + m30; + this.right = x + m30; + this.top = y + m31; + this.bottom = -y + m31; + } + /** * Determine if this Rectangle intersects some other. * Note that this is a comparison assuming the Rectangle was diff --git a/src/Skin.js b/src/Skin.js index e0e7413..8d124be 100644 --- a/src/Skin.js +++ b/src/Skin.js @@ -148,8 +148,8 @@ class Skin extends EventEmitter { * @param {Array} drawable - The Drawable instance this skin is using. * @return {!Rectangle} The drawable's bounds. */ - getFenceBounds (drawable) { - return drawable.getFastBounds(); + getFenceBounds (drawable, bounds) { + return drawable.getFastBounds(bounds); } /**