use a destination parameter for bounds; add initFromMatrixRadius

- pass bounds as a destination parameter
- add initFromMatrixRadius
- use initFromMatrixRadius in getAABB
This commit is contained in:
Michael "Z" Goddard 2019-06-05 17:40:46 -04:00
parent 27c70a7542
commit bf47f69b04
No known key found for this signature in database
GPG key ID: 762CD40DD5349872
3 changed files with 37 additions and 17 deletions

View file

@ -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);
}
/**

View file

@ -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

View file

@ -148,8 +148,8 @@ class Skin extends EventEmitter {
* @param {Array<number>} 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);
}
/**