diff --git a/src/core/display/BoundsBuilder.js b/src/core/display/BoundsBuilder.js index f86d208..0fc6b6d 100644 --- a/src/core/display/BoundsBuilder.js +++ b/src/core/display/BoundsBuilder.js @@ -9,7 +9,7 @@ * @class * @memberof PIXI */ -function BoundsBuilder() +function Bounds() { /** * @member {number} @@ -34,18 +34,22 @@ * @default 0 */ this.maxY = -Infinity; + + this.rect = null; } -BoundsBuilder.prototype.constructor = BoundsBuilder; -module.exports = BoundsBuilder; +Bounds.prototype.constructor = Bounds; +module.exports = Bounds; -BoundsBuilder.prototype.isEmpty = function() +Bounds.prototype.isEmpty = function() { return this.minX > this.maxX || this.minY > this.maxY; }; -BoundsBuilder.prototype.clear = function() +Bounds.prototype.clear = function() { + this.updateID++; + this.minX = Infinity; this.minY = Infinity; this.maxX = -Infinity; @@ -58,24 +62,30 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -BoundsBuilder.prototype.getRectangle = function(tempRect) +Bounds.prototype.getRectangle = function() { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - tempRect = tempRect || new Rectangle(0, 0, 1, 1); - tempRect.x = this.minX; - tempRect.y = this.minY; - tempRect.width = this.maxX - this.minX; - tempRect.height = this.maxY - this.minY; - return tempRect; + + if(!this.rect) + { + this.rect = new Rectangle(0, 0, 1, 1); + } + + this.rect.x = this.minX; + this.rect.y = this.minY; + this.rect.width = this.maxX - this.minX; + this.rect.height = this.maxY - this.minY; + + return this.rect; }; /** * This function should be inlined when its possible * @param point {PIXI.Point} */ -BoundsBuilder.prototype.addPoint = function (point) +Bounds.prototype.addPoint = function (point) { this.minX = Math.min(this.minX, point.x); this.maxX = Math.max(this.maxX, point.x); @@ -86,9 +96,9 @@ /** * Adds a quad, not transformed * @param vertices {Float32Array} - * @returns {PIXI.BoundsBuilder} + * @returns {PIXI.Bounds} */ -BoundsBuilder.prototype.addQuad = function(vertices) +Bounds.prototype.addQuad = function(vertices) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; @@ -134,7 +144,7 @@ * @param x1 {number} * @param y1 {number} */ -BoundsBuilder.prototype.addFrame = function(transform, x0, y0, x1, y1) +Bounds.prototype.addFrame = function(transform, x0, y0, x1, y1) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -181,7 +191,7 @@ * @param beginOffset {number} * @param endOffset {number} */ -BoundsBuilder.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) +Bounds.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -205,7 +215,7 @@ this.maxY = maxY; }; -BoundsBuilder.prototype.addBounds = function(bounds) +Bounds.prototype.addBounds = function(bounds) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; diff --git a/src/core/display/BoundsBuilder.js b/src/core/display/BoundsBuilder.js index f86d208..0fc6b6d 100644 --- a/src/core/display/BoundsBuilder.js +++ b/src/core/display/BoundsBuilder.js @@ -9,7 +9,7 @@ * @class * @memberof PIXI */ -function BoundsBuilder() +function Bounds() { /** * @member {number} @@ -34,18 +34,22 @@ * @default 0 */ this.maxY = -Infinity; + + this.rect = null; } -BoundsBuilder.prototype.constructor = BoundsBuilder; -module.exports = BoundsBuilder; +Bounds.prototype.constructor = Bounds; +module.exports = Bounds; -BoundsBuilder.prototype.isEmpty = function() +Bounds.prototype.isEmpty = function() { return this.minX > this.maxX || this.minY > this.maxY; }; -BoundsBuilder.prototype.clear = function() +Bounds.prototype.clear = function() { + this.updateID++; + this.minX = Infinity; this.minY = Infinity; this.maxX = -Infinity; @@ -58,24 +62,30 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -BoundsBuilder.prototype.getRectangle = function(tempRect) +Bounds.prototype.getRectangle = function() { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - tempRect = tempRect || new Rectangle(0, 0, 1, 1); - tempRect.x = this.minX; - tempRect.y = this.minY; - tempRect.width = this.maxX - this.minX; - tempRect.height = this.maxY - this.minY; - return tempRect; + + if(!this.rect) + { + this.rect = new Rectangle(0, 0, 1, 1); + } + + this.rect.x = this.minX; + this.rect.y = this.minY; + this.rect.width = this.maxX - this.minX; + this.rect.height = this.maxY - this.minY; + + return this.rect; }; /** * This function should be inlined when its possible * @param point {PIXI.Point} */ -BoundsBuilder.prototype.addPoint = function (point) +Bounds.prototype.addPoint = function (point) { this.minX = Math.min(this.minX, point.x); this.maxX = Math.max(this.maxX, point.x); @@ -86,9 +96,9 @@ /** * Adds a quad, not transformed * @param vertices {Float32Array} - * @returns {PIXI.BoundsBuilder} + * @returns {PIXI.Bounds} */ -BoundsBuilder.prototype.addQuad = function(vertices) +Bounds.prototype.addQuad = function(vertices) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; @@ -134,7 +144,7 @@ * @param x1 {number} * @param y1 {number} */ -BoundsBuilder.prototype.addFrame = function(transform, x0, y0, x1, y1) +Bounds.prototype.addFrame = function(transform, x0, y0, x1, y1) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -181,7 +191,7 @@ * @param beginOffset {number} * @param endOffset {number} */ -BoundsBuilder.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) +Bounds.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -205,7 +215,7 @@ this.maxY = maxY; }; -BoundsBuilder.prototype.addBounds = function(bounds) +Bounds.prototype.addBounds = function(bounds) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index e69807a..eff729e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -363,7 +363,7 @@ */ Container.prototype.updateTransform = function () { - this._currentBounds = null; + this._boundsID++; if (!this.visible) { @@ -379,8 +379,6 @@ { this.children[i].updateTransform(); } - - }; // performance increase to avoid using call.. (10x faster) @@ -389,15 +387,13 @@ Container.prototype.calculateBounds = function () { - this._bounds_.clear(); - // if we have already done this on THIS frame. + this._bounds.clear(); if(!this.visible) { return; } - this._calculateBounds(); for (var i = 0; i < this.children.length; i++) @@ -406,8 +402,10 @@ child.calculateBounds(); - this._bounds_.addBounds(child._bounds_); + this._bounds.addBounds(child._bounds); } + + this._boundsID = this._lastBoundsID; }; Container.prototype._calculateBounds = function () diff --git a/src/core/display/BoundsBuilder.js b/src/core/display/BoundsBuilder.js index f86d208..0fc6b6d 100644 --- a/src/core/display/BoundsBuilder.js +++ b/src/core/display/BoundsBuilder.js @@ -9,7 +9,7 @@ * @class * @memberof PIXI */ -function BoundsBuilder() +function Bounds() { /** * @member {number} @@ -34,18 +34,22 @@ * @default 0 */ this.maxY = -Infinity; + + this.rect = null; } -BoundsBuilder.prototype.constructor = BoundsBuilder; -module.exports = BoundsBuilder; +Bounds.prototype.constructor = Bounds; +module.exports = Bounds; -BoundsBuilder.prototype.isEmpty = function() +Bounds.prototype.isEmpty = function() { return this.minX > this.maxX || this.minY > this.maxY; }; -BoundsBuilder.prototype.clear = function() +Bounds.prototype.clear = function() { + this.updateID++; + this.minX = Infinity; this.minY = Infinity; this.maxX = -Infinity; @@ -58,24 +62,30 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -BoundsBuilder.prototype.getRectangle = function(tempRect) +Bounds.prototype.getRectangle = function() { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - tempRect = tempRect || new Rectangle(0, 0, 1, 1); - tempRect.x = this.minX; - tempRect.y = this.minY; - tempRect.width = this.maxX - this.minX; - tempRect.height = this.maxY - this.minY; - return tempRect; + + if(!this.rect) + { + this.rect = new Rectangle(0, 0, 1, 1); + } + + this.rect.x = this.minX; + this.rect.y = this.minY; + this.rect.width = this.maxX - this.minX; + this.rect.height = this.maxY - this.minY; + + return this.rect; }; /** * This function should be inlined when its possible * @param point {PIXI.Point} */ -BoundsBuilder.prototype.addPoint = function (point) +Bounds.prototype.addPoint = function (point) { this.minX = Math.min(this.minX, point.x); this.maxX = Math.max(this.maxX, point.x); @@ -86,9 +96,9 @@ /** * Adds a quad, not transformed * @param vertices {Float32Array} - * @returns {PIXI.BoundsBuilder} + * @returns {PIXI.Bounds} */ -BoundsBuilder.prototype.addQuad = function(vertices) +Bounds.prototype.addQuad = function(vertices) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; @@ -134,7 +144,7 @@ * @param x1 {number} * @param y1 {number} */ -BoundsBuilder.prototype.addFrame = function(transform, x0, y0, x1, y1) +Bounds.prototype.addFrame = function(transform, x0, y0, x1, y1) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -181,7 +191,7 @@ * @param beginOffset {number} * @param endOffset {number} */ -BoundsBuilder.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) +Bounds.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -205,7 +215,7 @@ this.maxY = maxY; }; -BoundsBuilder.prototype.addBounds = function(bounds) +Bounds.prototype.addBounds = function(bounds) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index e69807a..eff729e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -363,7 +363,7 @@ */ Container.prototype.updateTransform = function () { - this._currentBounds = null; + this._boundsID++; if (!this.visible) { @@ -379,8 +379,6 @@ { this.children[i].updateTransform(); } - - }; // performance increase to avoid using call.. (10x faster) @@ -389,15 +387,13 @@ Container.prototype.calculateBounds = function () { - this._bounds_.clear(); - // if we have already done this on THIS frame. + this._bounds.clear(); if(!this.visible) { return; } - this._calculateBounds(); for (var i = 0; i < this.children.length; i++) @@ -406,8 +402,10 @@ child.calculateBounds(); - this._bounds_.addBounds(child._bounds_); + this._bounds.addBounds(child._bounds); } + + this._boundsID = this._lastBoundsID; }; Container.prototype._calculateBounds = function () diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index d140946..c63012e 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -1,9 +1,8 @@ -var math = require('../math'), - EventEmitter = require('eventemitter3'), +var EventEmitter = require('eventemitter3'), CONST = require('../const'), TransformStatic = require('./TransformStatic'), Transform = require('./Transform'), - BoundsBulder = require('./BoundsBuilder'), + Bounds = require('./BoundsBuilder'), _tempDisplayObjectParent = new DisplayObject(); /** @@ -80,20 +79,14 @@ this.filterArea = null; /** - * The original, cached bounds of the object + * The bounds object, this is used to calculate and store the bounds of the displayObject * * @member {PIXI.Rectangle} * @private */ - this._bounds = new math.Rectangle(0, 0, 1, 1); - - /** - * The most up-to-date bounds of the object - * - * @member {PIXI.Rectangle} - * @private - */ - this._currentBounds = null; + this._bounds = new Bounds(); + this._boundsID = 0; + this._lastBoundsID = -1; /** * The original, cached mask of the object @@ -103,7 +96,7 @@ */ this._mask = null; - this._bounds_ = new BoundsBulder(); + } // constructor @@ -346,6 +339,8 @@ this.transform.updateTransform(this.parent.transform); // multiply the alphas.. this.worldAlpha = this.alpha * this.parent.worldAlpha; + + this._bounds.updateID++; }; // performance increase to avoid using call.. (10x faster) @@ -392,13 +387,12 @@ } } - if(!this._currentBounds) + if(this._boundsID !== this._lastBoundsID) { this.calculateBounds(); - this._currentBounds = this._bounds_.getRectangle(this._bounds); } - return this._currentBounds; + return this._bounds.getRectangle(this._bounds); }; /** diff --git a/src/core/display/BoundsBuilder.js b/src/core/display/BoundsBuilder.js index f86d208..0fc6b6d 100644 --- a/src/core/display/BoundsBuilder.js +++ b/src/core/display/BoundsBuilder.js @@ -9,7 +9,7 @@ * @class * @memberof PIXI */ -function BoundsBuilder() +function Bounds() { /** * @member {number} @@ -34,18 +34,22 @@ * @default 0 */ this.maxY = -Infinity; + + this.rect = null; } -BoundsBuilder.prototype.constructor = BoundsBuilder; -module.exports = BoundsBuilder; +Bounds.prototype.constructor = Bounds; +module.exports = Bounds; -BoundsBuilder.prototype.isEmpty = function() +Bounds.prototype.isEmpty = function() { return this.minX > this.maxX || this.minY > this.maxY; }; -BoundsBuilder.prototype.clear = function() +Bounds.prototype.clear = function() { + this.updateID++; + this.minX = Infinity; this.minY = Infinity; this.maxX = -Infinity; @@ -58,24 +62,30 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -BoundsBuilder.prototype.getRectangle = function(tempRect) +Bounds.prototype.getRectangle = function() { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - tempRect = tempRect || new Rectangle(0, 0, 1, 1); - tempRect.x = this.minX; - tempRect.y = this.minY; - tempRect.width = this.maxX - this.minX; - tempRect.height = this.maxY - this.minY; - return tempRect; + + if(!this.rect) + { + this.rect = new Rectangle(0, 0, 1, 1); + } + + this.rect.x = this.minX; + this.rect.y = this.minY; + this.rect.width = this.maxX - this.minX; + this.rect.height = this.maxY - this.minY; + + return this.rect; }; /** * This function should be inlined when its possible * @param point {PIXI.Point} */ -BoundsBuilder.prototype.addPoint = function (point) +Bounds.prototype.addPoint = function (point) { this.minX = Math.min(this.minX, point.x); this.maxX = Math.max(this.maxX, point.x); @@ -86,9 +96,9 @@ /** * Adds a quad, not transformed * @param vertices {Float32Array} - * @returns {PIXI.BoundsBuilder} + * @returns {PIXI.Bounds} */ -BoundsBuilder.prototype.addQuad = function(vertices) +Bounds.prototype.addQuad = function(vertices) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; @@ -134,7 +144,7 @@ * @param x1 {number} * @param y1 {number} */ -BoundsBuilder.prototype.addFrame = function(transform, x0, y0, x1, y1) +Bounds.prototype.addFrame = function(transform, x0, y0, x1, y1) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -181,7 +191,7 @@ * @param beginOffset {number} * @param endOffset {number} */ -BoundsBuilder.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) +Bounds.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -205,7 +215,7 @@ this.maxY = maxY; }; -BoundsBuilder.prototype.addBounds = function(bounds) +Bounds.prototype.addBounds = function(bounds) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index e69807a..eff729e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -363,7 +363,7 @@ */ Container.prototype.updateTransform = function () { - this._currentBounds = null; + this._boundsID++; if (!this.visible) { @@ -379,8 +379,6 @@ { this.children[i].updateTransform(); } - - }; // performance increase to avoid using call.. (10x faster) @@ -389,15 +387,13 @@ Container.prototype.calculateBounds = function () { - this._bounds_.clear(); - // if we have already done this on THIS frame. + this._bounds.clear(); if(!this.visible) { return; } - this._calculateBounds(); for (var i = 0; i < this.children.length; i++) @@ -406,8 +402,10 @@ child.calculateBounds(); - this._bounds_.addBounds(child._bounds_); + this._bounds.addBounds(child._bounds); } + + this._boundsID = this._lastBoundsID; }; Container.prototype._calculateBounds = function () diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index d140946..c63012e 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -1,9 +1,8 @@ -var math = require('../math'), - EventEmitter = require('eventemitter3'), +var EventEmitter = require('eventemitter3'), CONST = require('../const'), TransformStatic = require('./TransformStatic'), Transform = require('./Transform'), - BoundsBulder = require('./BoundsBuilder'), + Bounds = require('./BoundsBuilder'), _tempDisplayObjectParent = new DisplayObject(); /** @@ -80,20 +79,14 @@ this.filterArea = null; /** - * The original, cached bounds of the object + * The bounds object, this is used to calculate and store the bounds of the displayObject * * @member {PIXI.Rectangle} * @private */ - this._bounds = new math.Rectangle(0, 0, 1, 1); - - /** - * The most up-to-date bounds of the object - * - * @member {PIXI.Rectangle} - * @private - */ - this._currentBounds = null; + this._bounds = new Bounds(); + this._boundsID = 0; + this._lastBoundsID = -1; /** * The original, cached mask of the object @@ -103,7 +96,7 @@ */ this._mask = null; - this._bounds_ = new BoundsBulder(); + } // constructor @@ -346,6 +339,8 @@ this.transform.updateTransform(this.parent.transform); // multiply the alphas.. this.worldAlpha = this.alpha * this.parent.worldAlpha; + + this._bounds.updateID++; }; // performance increase to avoid using call.. (10x faster) @@ -392,13 +387,12 @@ } } - if(!this._currentBounds) + if(this._boundsID !== this._lastBoundsID) { this.calculateBounds(); - this._currentBounds = this._bounds_.getRectangle(this._bounds); } - return this._currentBounds; + return this._bounds.getRectangle(this._bounds); }; /** diff --git a/src/core/graphics/Graphics.js b/src/core/graphics/Graphics.js index 4e37d2d..4394648 100644 --- a/src/core/graphics/Graphics.js +++ b/src/core/graphics/Graphics.js @@ -792,7 +792,7 @@ } var lb = this._localBounds; - this._bounds_.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); + this._bounds.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); }; /** diff --git a/src/core/display/BoundsBuilder.js b/src/core/display/BoundsBuilder.js index f86d208..0fc6b6d 100644 --- a/src/core/display/BoundsBuilder.js +++ b/src/core/display/BoundsBuilder.js @@ -9,7 +9,7 @@ * @class * @memberof PIXI */ -function BoundsBuilder() +function Bounds() { /** * @member {number} @@ -34,18 +34,22 @@ * @default 0 */ this.maxY = -Infinity; + + this.rect = null; } -BoundsBuilder.prototype.constructor = BoundsBuilder; -module.exports = BoundsBuilder; +Bounds.prototype.constructor = Bounds; +module.exports = Bounds; -BoundsBuilder.prototype.isEmpty = function() +Bounds.prototype.isEmpty = function() { return this.minX > this.maxX || this.minY > this.maxY; }; -BoundsBuilder.prototype.clear = function() +Bounds.prototype.clear = function() { + this.updateID++; + this.minX = Infinity; this.minY = Infinity; this.maxX = -Infinity; @@ -58,24 +62,30 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -BoundsBuilder.prototype.getRectangle = function(tempRect) +Bounds.prototype.getRectangle = function() { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - tempRect = tempRect || new Rectangle(0, 0, 1, 1); - tempRect.x = this.minX; - tempRect.y = this.minY; - tempRect.width = this.maxX - this.minX; - tempRect.height = this.maxY - this.minY; - return tempRect; + + if(!this.rect) + { + this.rect = new Rectangle(0, 0, 1, 1); + } + + this.rect.x = this.minX; + this.rect.y = this.minY; + this.rect.width = this.maxX - this.minX; + this.rect.height = this.maxY - this.minY; + + return this.rect; }; /** * This function should be inlined when its possible * @param point {PIXI.Point} */ -BoundsBuilder.prototype.addPoint = function (point) +Bounds.prototype.addPoint = function (point) { this.minX = Math.min(this.minX, point.x); this.maxX = Math.max(this.maxX, point.x); @@ -86,9 +96,9 @@ /** * Adds a quad, not transformed * @param vertices {Float32Array} - * @returns {PIXI.BoundsBuilder} + * @returns {PIXI.Bounds} */ -BoundsBuilder.prototype.addQuad = function(vertices) +Bounds.prototype.addQuad = function(vertices) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; @@ -134,7 +144,7 @@ * @param x1 {number} * @param y1 {number} */ -BoundsBuilder.prototype.addFrame = function(transform, x0, y0, x1, y1) +Bounds.prototype.addFrame = function(transform, x0, y0, x1, y1) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -181,7 +191,7 @@ * @param beginOffset {number} * @param endOffset {number} */ -BoundsBuilder.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) +Bounds.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -205,7 +215,7 @@ this.maxY = maxY; }; -BoundsBuilder.prototype.addBounds = function(bounds) +Bounds.prototype.addBounds = function(bounds) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index e69807a..eff729e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -363,7 +363,7 @@ */ Container.prototype.updateTransform = function () { - this._currentBounds = null; + this._boundsID++; if (!this.visible) { @@ -379,8 +379,6 @@ { this.children[i].updateTransform(); } - - }; // performance increase to avoid using call.. (10x faster) @@ -389,15 +387,13 @@ Container.prototype.calculateBounds = function () { - this._bounds_.clear(); - // if we have already done this on THIS frame. + this._bounds.clear(); if(!this.visible) { return; } - this._calculateBounds(); for (var i = 0; i < this.children.length; i++) @@ -406,8 +402,10 @@ child.calculateBounds(); - this._bounds_.addBounds(child._bounds_); + this._bounds.addBounds(child._bounds); } + + this._boundsID = this._lastBoundsID; }; Container.prototype._calculateBounds = function () diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index d140946..c63012e 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -1,9 +1,8 @@ -var math = require('../math'), - EventEmitter = require('eventemitter3'), +var EventEmitter = require('eventemitter3'), CONST = require('../const'), TransformStatic = require('./TransformStatic'), Transform = require('./Transform'), - BoundsBulder = require('./BoundsBuilder'), + Bounds = require('./BoundsBuilder'), _tempDisplayObjectParent = new DisplayObject(); /** @@ -80,20 +79,14 @@ this.filterArea = null; /** - * The original, cached bounds of the object + * The bounds object, this is used to calculate and store the bounds of the displayObject * * @member {PIXI.Rectangle} * @private */ - this._bounds = new math.Rectangle(0, 0, 1, 1); - - /** - * The most up-to-date bounds of the object - * - * @member {PIXI.Rectangle} - * @private - */ - this._currentBounds = null; + this._bounds = new Bounds(); + this._boundsID = 0; + this._lastBoundsID = -1; /** * The original, cached mask of the object @@ -103,7 +96,7 @@ */ this._mask = null; - this._bounds_ = new BoundsBulder(); + } // constructor @@ -346,6 +339,8 @@ this.transform.updateTransform(this.parent.transform); // multiply the alphas.. this.worldAlpha = this.alpha * this.parent.worldAlpha; + + this._bounds.updateID++; }; // performance increase to avoid using call.. (10x faster) @@ -392,13 +387,12 @@ } } - if(!this._currentBounds) + if(this._boundsID !== this._lastBoundsID) { this.calculateBounds(); - this._currentBounds = this._bounds_.getRectangle(this._bounds); } - return this._currentBounds; + return this._bounds.getRectangle(this._bounds); }; /** diff --git a/src/core/graphics/Graphics.js b/src/core/graphics/Graphics.js index 4e37d2d..4394648 100644 --- a/src/core/graphics/Graphics.js +++ b/src/core/graphics/Graphics.js @@ -792,7 +792,7 @@ } var lb = this._localBounds; - this._bounds_.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); + this._bounds.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); }; /** diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index d4ab057..2957e70 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -345,20 +345,23 @@ { this.calculateVertices(); // if we have already done this on THIS frame. - this._bounds_.addQuad(this.vertexData); + this._bounds.addQuad(this.vertexData); }; /** * Gets the local bounds of the sprite object. * */ + Sprite.prototype.getLocalBounds = function () { - this._bounds.x = -this._texture.orig.width * this.anchor.x; - this._bounds.y = -this._texture.orig.height * this.anchor.y; - this._bounds.width = this._texture.orig.width; - this._bounds.height = this._texture.orig.height; - return this._bounds; + + this._bounds.minX = -this._texture.orig.width * this.anchor.x; + this._bounds.minY = -this._texture.orig.height * this.anchor.y; + this._bounds.maxX = this._texture.orig.width; + this._bounds.maxY = this._texture.orig.height; + + return this._bounds.getRectangle(this._bounds); }; /** diff --git a/src/core/display/BoundsBuilder.js b/src/core/display/BoundsBuilder.js index f86d208..0fc6b6d 100644 --- a/src/core/display/BoundsBuilder.js +++ b/src/core/display/BoundsBuilder.js @@ -9,7 +9,7 @@ * @class * @memberof PIXI */ -function BoundsBuilder() +function Bounds() { /** * @member {number} @@ -34,18 +34,22 @@ * @default 0 */ this.maxY = -Infinity; + + this.rect = null; } -BoundsBuilder.prototype.constructor = BoundsBuilder; -module.exports = BoundsBuilder; +Bounds.prototype.constructor = Bounds; +module.exports = Bounds; -BoundsBuilder.prototype.isEmpty = function() +Bounds.prototype.isEmpty = function() { return this.minX > this.maxX || this.minY > this.maxY; }; -BoundsBuilder.prototype.clear = function() +Bounds.prototype.clear = function() { + this.updateID++; + this.minX = Infinity; this.minY = Infinity; this.maxX = -Infinity; @@ -58,24 +62,30 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -BoundsBuilder.prototype.getRectangle = function(tempRect) +Bounds.prototype.getRectangle = function() { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - tempRect = tempRect || new Rectangle(0, 0, 1, 1); - tempRect.x = this.minX; - tempRect.y = this.minY; - tempRect.width = this.maxX - this.minX; - tempRect.height = this.maxY - this.minY; - return tempRect; + + if(!this.rect) + { + this.rect = new Rectangle(0, 0, 1, 1); + } + + this.rect.x = this.minX; + this.rect.y = this.minY; + this.rect.width = this.maxX - this.minX; + this.rect.height = this.maxY - this.minY; + + return this.rect; }; /** * This function should be inlined when its possible * @param point {PIXI.Point} */ -BoundsBuilder.prototype.addPoint = function (point) +Bounds.prototype.addPoint = function (point) { this.minX = Math.min(this.minX, point.x); this.maxX = Math.max(this.maxX, point.x); @@ -86,9 +96,9 @@ /** * Adds a quad, not transformed * @param vertices {Float32Array} - * @returns {PIXI.BoundsBuilder} + * @returns {PIXI.Bounds} */ -BoundsBuilder.prototype.addQuad = function(vertices) +Bounds.prototype.addQuad = function(vertices) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; @@ -134,7 +144,7 @@ * @param x1 {number} * @param y1 {number} */ -BoundsBuilder.prototype.addFrame = function(transform, x0, y0, x1, y1) +Bounds.prototype.addFrame = function(transform, x0, y0, x1, y1) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -181,7 +191,7 @@ * @param beginOffset {number} * @param endOffset {number} */ -BoundsBuilder.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) +Bounds.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -205,7 +215,7 @@ this.maxY = maxY; }; -BoundsBuilder.prototype.addBounds = function(bounds) +Bounds.prototype.addBounds = function(bounds) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index e69807a..eff729e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -363,7 +363,7 @@ */ Container.prototype.updateTransform = function () { - this._currentBounds = null; + this._boundsID++; if (!this.visible) { @@ -379,8 +379,6 @@ { this.children[i].updateTransform(); } - - }; // performance increase to avoid using call.. (10x faster) @@ -389,15 +387,13 @@ Container.prototype.calculateBounds = function () { - this._bounds_.clear(); - // if we have already done this on THIS frame. + this._bounds.clear(); if(!this.visible) { return; } - this._calculateBounds(); for (var i = 0; i < this.children.length; i++) @@ -406,8 +402,10 @@ child.calculateBounds(); - this._bounds_.addBounds(child._bounds_); + this._bounds.addBounds(child._bounds); } + + this._boundsID = this._lastBoundsID; }; Container.prototype._calculateBounds = function () diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index d140946..c63012e 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -1,9 +1,8 @@ -var math = require('../math'), - EventEmitter = require('eventemitter3'), +var EventEmitter = require('eventemitter3'), CONST = require('../const'), TransformStatic = require('./TransformStatic'), Transform = require('./Transform'), - BoundsBulder = require('./BoundsBuilder'), + Bounds = require('./BoundsBuilder'), _tempDisplayObjectParent = new DisplayObject(); /** @@ -80,20 +79,14 @@ this.filterArea = null; /** - * The original, cached bounds of the object + * The bounds object, this is used to calculate and store the bounds of the displayObject * * @member {PIXI.Rectangle} * @private */ - this._bounds = new math.Rectangle(0, 0, 1, 1); - - /** - * The most up-to-date bounds of the object - * - * @member {PIXI.Rectangle} - * @private - */ - this._currentBounds = null; + this._bounds = new Bounds(); + this._boundsID = 0; + this._lastBoundsID = -1; /** * The original, cached mask of the object @@ -103,7 +96,7 @@ */ this._mask = null; - this._bounds_ = new BoundsBulder(); + } // constructor @@ -346,6 +339,8 @@ this.transform.updateTransform(this.parent.transform); // multiply the alphas.. this.worldAlpha = this.alpha * this.parent.worldAlpha; + + this._bounds.updateID++; }; // performance increase to avoid using call.. (10x faster) @@ -392,13 +387,12 @@ } } - if(!this._currentBounds) + if(this._boundsID !== this._lastBoundsID) { this.calculateBounds(); - this._currentBounds = this._bounds_.getRectangle(this._bounds); } - return this._currentBounds; + return this._bounds.getRectangle(this._bounds); }; /** diff --git a/src/core/graphics/Graphics.js b/src/core/graphics/Graphics.js index 4e37d2d..4394648 100644 --- a/src/core/graphics/Graphics.js +++ b/src/core/graphics/Graphics.js @@ -792,7 +792,7 @@ } var lb = this._localBounds; - this._bounds_.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); + this._bounds.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); }; /** diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index d4ab057..2957e70 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -345,20 +345,23 @@ { this.calculateVertices(); // if we have already done this on THIS frame. - this._bounds_.addQuad(this.vertexData); + this._bounds.addQuad(this.vertexData); }; /** * Gets the local bounds of the sprite object. * */ + Sprite.prototype.getLocalBounds = function () { - this._bounds.x = -this._texture.orig.width * this.anchor.x; - this._bounds.y = -this._texture.orig.height * this.anchor.y; - this._bounds.width = this._texture.orig.width; - this._bounds.height = this._texture.orig.height; - return this._bounds; + + this._bounds.minX = -this._texture.orig.width * this.anchor.x; + this._bounds.minY = -this._texture.orig.height * this.anchor.y; + this._bounds.maxX = this._texture.orig.width; + this._bounds.maxY = this._texture.orig.height; + + return this._bounds.getRectangle(this._bounds); }; /** diff --git a/src/core/text/Text.js b/src/core/text/Text.js index ee875de..697405f 100644 --- a/src/core/text/Text.js +++ b/src/core/text/Text.js @@ -641,7 +641,7 @@ this.updateText(true); this.calculateVertices(); // if we have already done this on THIS frame. - this._bounds_.addQuad(this.vertexData); + this._bounds.addQuad(this.vertexData); }; /** diff --git a/src/core/display/BoundsBuilder.js b/src/core/display/BoundsBuilder.js index f86d208..0fc6b6d 100644 --- a/src/core/display/BoundsBuilder.js +++ b/src/core/display/BoundsBuilder.js @@ -9,7 +9,7 @@ * @class * @memberof PIXI */ -function BoundsBuilder() +function Bounds() { /** * @member {number} @@ -34,18 +34,22 @@ * @default 0 */ this.maxY = -Infinity; + + this.rect = null; } -BoundsBuilder.prototype.constructor = BoundsBuilder; -module.exports = BoundsBuilder; +Bounds.prototype.constructor = Bounds; +module.exports = Bounds; -BoundsBuilder.prototype.isEmpty = function() +Bounds.prototype.isEmpty = function() { return this.minX > this.maxX || this.minY > this.maxY; }; -BoundsBuilder.prototype.clear = function() +Bounds.prototype.clear = function() { + this.updateID++; + this.minX = Infinity; this.minY = Infinity; this.maxX = -Infinity; @@ -58,24 +62,30 @@ * @param tempRect {PIXI.Rectangle} temporary object will be used if AABB is not empty * @returns {PIXI.Rectangle} */ -BoundsBuilder.prototype.getRectangle = function(tempRect) +Bounds.prototype.getRectangle = function() { if (this.minX > this.maxX || this.minY > this.maxY) { return Rectangle.EMPTY; } - tempRect = tempRect || new Rectangle(0, 0, 1, 1); - tempRect.x = this.minX; - tempRect.y = this.minY; - tempRect.width = this.maxX - this.minX; - tempRect.height = this.maxY - this.minY; - return tempRect; + + if(!this.rect) + { + this.rect = new Rectangle(0, 0, 1, 1); + } + + this.rect.x = this.minX; + this.rect.y = this.minY; + this.rect.width = this.maxX - this.minX; + this.rect.height = this.maxY - this.minY; + + return this.rect; }; /** * This function should be inlined when its possible * @param point {PIXI.Point} */ -BoundsBuilder.prototype.addPoint = function (point) +Bounds.prototype.addPoint = function (point) { this.minX = Math.min(this.minX, point.x); this.maxX = Math.max(this.maxX, point.x); @@ -86,9 +96,9 @@ /** * Adds a quad, not transformed * @param vertices {Float32Array} - * @returns {PIXI.BoundsBuilder} + * @returns {PIXI.Bounds} */ -BoundsBuilder.prototype.addQuad = function(vertices) +Bounds.prototype.addQuad = function(vertices) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; @@ -134,7 +144,7 @@ * @param x1 {number} * @param y1 {number} */ -BoundsBuilder.prototype.addFrame = function(transform, x0, y0, x1, y1) +Bounds.prototype.addFrame = function(transform, x0, y0, x1, y1) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -181,7 +191,7 @@ * @param beginOffset {number} * @param endOffset {number} */ -BoundsBuilder.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) +Bounds.prototype.addVertices = function(transform, vertices, beginOffset, endOffset) { var matrix = transform.worldTransform; var a = matrix.a, b = matrix.b, c = matrix.c, d = matrix.d, tx = matrix.tx, ty = matrix.ty; @@ -205,7 +215,7 @@ this.maxY = maxY; }; -BoundsBuilder.prototype.addBounds = function(bounds) +Bounds.prototype.addBounds = function(bounds) { var minX = this.minX, minY = this.minY, maxX = this.maxX, maxY = this.maxY; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index e69807a..eff729e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -363,7 +363,7 @@ */ Container.prototype.updateTransform = function () { - this._currentBounds = null; + this._boundsID++; if (!this.visible) { @@ -379,8 +379,6 @@ { this.children[i].updateTransform(); } - - }; // performance increase to avoid using call.. (10x faster) @@ -389,15 +387,13 @@ Container.prototype.calculateBounds = function () { - this._bounds_.clear(); - // if we have already done this on THIS frame. + this._bounds.clear(); if(!this.visible) { return; } - this._calculateBounds(); for (var i = 0; i < this.children.length; i++) @@ -406,8 +402,10 @@ child.calculateBounds(); - this._bounds_.addBounds(child._bounds_); + this._bounds.addBounds(child._bounds); } + + this._boundsID = this._lastBoundsID; }; Container.prototype._calculateBounds = function () diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index d140946..c63012e 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -1,9 +1,8 @@ -var math = require('../math'), - EventEmitter = require('eventemitter3'), +var EventEmitter = require('eventemitter3'), CONST = require('../const'), TransformStatic = require('./TransformStatic'), Transform = require('./Transform'), - BoundsBulder = require('./BoundsBuilder'), + Bounds = require('./BoundsBuilder'), _tempDisplayObjectParent = new DisplayObject(); /** @@ -80,20 +79,14 @@ this.filterArea = null; /** - * The original, cached bounds of the object + * The bounds object, this is used to calculate and store the bounds of the displayObject * * @member {PIXI.Rectangle} * @private */ - this._bounds = new math.Rectangle(0, 0, 1, 1); - - /** - * The most up-to-date bounds of the object - * - * @member {PIXI.Rectangle} - * @private - */ - this._currentBounds = null; + this._bounds = new Bounds(); + this._boundsID = 0; + this._lastBoundsID = -1; /** * The original, cached mask of the object @@ -103,7 +96,7 @@ */ this._mask = null; - this._bounds_ = new BoundsBulder(); + } // constructor @@ -346,6 +339,8 @@ this.transform.updateTransform(this.parent.transform); // multiply the alphas.. this.worldAlpha = this.alpha * this.parent.worldAlpha; + + this._bounds.updateID++; }; // performance increase to avoid using call.. (10x faster) @@ -392,13 +387,12 @@ } } - if(!this._currentBounds) + if(this._boundsID !== this._lastBoundsID) { this.calculateBounds(); - this._currentBounds = this._bounds_.getRectangle(this._bounds); } - return this._currentBounds; + return this._bounds.getRectangle(this._bounds); }; /** diff --git a/src/core/graphics/Graphics.js b/src/core/graphics/Graphics.js index 4e37d2d..4394648 100644 --- a/src/core/graphics/Graphics.js +++ b/src/core/graphics/Graphics.js @@ -792,7 +792,7 @@ } var lb = this._localBounds; - this._bounds_.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); + this._bounds.addFrame(this.transform, lb.minX, lb.minY, lb.maxX, lb.maxY); }; /** diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index d4ab057..2957e70 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -345,20 +345,23 @@ { this.calculateVertices(); // if we have already done this on THIS frame. - this._bounds_.addQuad(this.vertexData); + this._bounds.addQuad(this.vertexData); }; /** * Gets the local bounds of the sprite object. * */ + Sprite.prototype.getLocalBounds = function () { - this._bounds.x = -this._texture.orig.width * this.anchor.x; - this._bounds.y = -this._texture.orig.height * this.anchor.y; - this._bounds.width = this._texture.orig.width; - this._bounds.height = this._texture.orig.height; - return this._bounds; + + this._bounds.minX = -this._texture.orig.width * this.anchor.x; + this._bounds.minY = -this._texture.orig.height * this.anchor.y; + this._bounds.maxX = this._texture.orig.width; + this._bounds.maxY = this._texture.orig.height; + + return this._bounds.getRectangle(this._bounds); }; /** diff --git a/src/core/text/Text.js b/src/core/text/Text.js index ee875de..697405f 100644 --- a/src/core/text/Text.js +++ b/src/core/text/Text.js @@ -641,7 +641,7 @@ this.updateText(true); this.calculateVertices(); // if we have already done this on THIS frame. - this._bounds_.addQuad(this.vertexData); + this._bounds.addQuad(this.vertexData); }; /** diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index c895e8e..e3eedb7 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -449,7 +449,7 @@ Mesh.prototype._calculateBounds = function () { //TODO - we can cache local bounds and use them if they are dirty (like graphics) - this._bounds_.addVertices(this.transform, this.vertices, 0, this.vertices.length); + this._bounds.addVertices(this.transform, this.vertices, 0, this.vertices.length); }; /**