diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 596230d..17b009b 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -1,4 +1,6 @@ -var core = require('../core'); +var core = require('../core'), + tempPoint = new core.Point(), + tempPolygon = new core.Polygon(); /** * Base mesh class @@ -29,9 +31,9 @@ * @member {Float32Array} */ this.uvs = uvs || new Float32Array([0, 1, - 1, 1, - 1, 0, - 0, 1]); + 1, 1, + 1, 0, + 0, 1]); /** * An array of vertices @@ -39,9 +41,9 @@ * @member {Float32Array} */ this.vertices = vertices || new Float32Array([0, 0, - 100, 0, - 100, 100, - 0, 100]); + 100, 0, + 100, 100, + 0, 100]); /* * @member {Uint16Array} An array containing the indices of the vertices @@ -333,18 +335,18 @@ }; /* -Mesh.prototype.setTexture = function (texture) -{ - //TODO SET THE TEXTURES - //TODO VISIBILITY - //TODO SETTER + Mesh.prototype.setTexture = function (texture) + { + //TODO SET THE TEXTURES + //TODO VISIBILITY + //TODO SETTER - // stop current texture - this.texture = texture; - this.width = texture.frame.width; - this.height = texture.frame.height; - this.updateFrame = true; -}; + // stop current texture + this.texture = texture; + this.width = texture.frame.width; + this.height = texture.frame.height; + this.updateFrame = true; + }; */ /** @@ -366,55 +368,103 @@ */ Mesh.prototype.getBounds = function (matrix) { - var worldTransform = matrix || this.worldTransform; + if (!this._currentBounds) { + var worldTransform = matrix || this.worldTransform; - var a = worldTransform.a; - var b = worldTransform.b; - var c = worldTransform.c; - var d = worldTransform.d; - var tx = worldTransform.tx; - var ty = worldTransform.ty; + var a = worldTransform.a; + var b = worldTransform.b; + var c = worldTransform.c; + var d = worldTransform.d; + var tx = worldTransform.tx; + var ty = worldTransform.ty; - var maxX = -Infinity; - var maxY = -Infinity; + var maxX = -Infinity; + var maxY = -Infinity; - var minX = Infinity; - var minY = Infinity; + var minX = Infinity; + var minY = Infinity; - var vertices = this.vertices; - for (var i = 0, n = vertices.length; i < n; i += 2) - { - var rawX = vertices[i], rawY = vertices[i + 1]; - var x = (a * rawX) + (c * rawY) + tx; - var y = (d * rawY) + (b * rawX) + ty; + var vertices = this.vertices; + for (var i = 0, n = vertices.length; i < n; i += 2) { + var rawX = vertices[i], rawY = vertices[i + 1]; + var x = (a * rawX) + (c * rawY) + tx; + var y = (d * rawY) + (b * rawX) + ty; - minX = x < minX ? x : minX; - minY = y < minY ? y : minY; + minX = x < minX ? x : minX; + minY = y < minY ? y : minY; - maxX = x > maxX ? x : maxX; - maxY = y > maxY ? y : maxY; + maxX = x > maxX ? x : maxX; + maxY = y > maxY ? y : maxY; + } + + if (minX === -Infinity || maxY === Infinity) { + return core.Rectangle.EMPTY; + } + + var bounds = this._bounds; + + bounds.x = minX; + bounds.width = maxX - minX; + + bounds.y = minY; + bounds.height = maxY - minY; + + // store a reference so that if this function gets called again in the render cycle we do not have to recalculate + this._currentBounds = bounds; } - if (minX === -Infinity || maxY === Infinity) - { - return core.Rectangle.EMPTY; - } - - var bounds = this._bounds; - - bounds.x = minX; - bounds.width = maxX - minX; - - bounds.y = minY; - bounds.height = maxY - minY; - - // store a reference so that if this function gets called again in the render cycle we do not have to recalculate - this._currentBounds = bounds; - - return bounds; + return this._currentBounds; }; /** + * Tests if a point is inside this mesh. Works only for TRIANGLE_MESH + * + * @param point {Point} the point to test + * @return {boolean} the result of the test + */ +Mesh.prototype.containsPoint = function( point ) { + if (!this.getBounds().contains(point.x, point.y)) { + return false; + } + this.worldTransform.applyInverse(point, tempPoint); + + var vertices = this.vertices; + var points = tempPolygon.points; + var i, len; + + if (this.drawMode === Mesh.DRAW_MODES.TRIANGLES) { + var indices = this.indices; + len = this.indices.length; + //TODO: inline this. + for (i=0;i