diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index 0105965..669bdb5 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -197,12 +197,6 @@ { this.scale.y = this._height / this.texture.frame.height; } - - if (this.texture.rotation) { - this.rotation = this.texture.rotation; - // this.pivot.x = this.texture.spritePivot.x; - // this.pivot.y = this.texture.spritePivot.y; - } }; Sprite.prototype._renderWebGL = function (renderer) diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index 0105965..669bdb5 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -197,12 +197,6 @@ { this.scale.y = this._height / this.texture.frame.height; } - - if (this.texture.rotation) { - this.rotation = this.texture.rotation; - // this.pivot.x = this.texture.spritePivot.x; - // this.pivot.y = this.texture.spritePivot.y; - } }; Sprite.prototype._renderWebGL = function (renderer) diff --git a/src/core/textures/Texture.js b/src/core/textures/Texture.js index 3198a53..2508070 100644 --- a/src/core/textures/Texture.js +++ b/src/core/textures/Texture.js @@ -106,18 +106,12 @@ this.crop = crop || frame;//new math.Rectangle(0, 0, 1, 1); /** - * The pivot point to used for a sprite this texture belongs to. + * The rotation value of the texture. * - * @member {Point} - */ - this.spritePivot = new math.Point(); - - /** - * The rotation value of the texture, copied to a sprite when assigned to it. - * + * @private * @member {number} */ - this.rotation = 0; + this._rotation = 0; if (baseTexture.hasLoaded) { @@ -175,6 +169,17 @@ this._updateUvs(); } } + }, + rotation: { + get: function () + { + return this._rotation; + }, + set: function (val) { + this._rotation = val; + + this._updateUvs(); + } } }); @@ -253,6 +258,8 @@ this._uvs.x3 = frame.x / tw; this._uvs.y3 = (frame.y + frame.height) / th; + + this._uvs.rotate(this.rotation); }; /** diff --git a/src/core/sprites/Sprite.js b/src/core/sprites/Sprite.js index 0105965..669bdb5 100644 --- a/src/core/sprites/Sprite.js +++ b/src/core/sprites/Sprite.js @@ -197,12 +197,6 @@ { this.scale.y = this._height / this.texture.frame.height; } - - if (this.texture.rotation) { - this.rotation = this.texture.rotation; - // this.pivot.x = this.texture.spritePivot.x; - // this.pivot.y = this.texture.spritePivot.y; - } }; Sprite.prototype._renderWebGL = function (renderer) diff --git a/src/core/textures/Texture.js b/src/core/textures/Texture.js index 3198a53..2508070 100644 --- a/src/core/textures/Texture.js +++ b/src/core/textures/Texture.js @@ -106,18 +106,12 @@ this.crop = crop || frame;//new math.Rectangle(0, 0, 1, 1); /** - * The pivot point to used for a sprite this texture belongs to. + * The rotation value of the texture. * - * @member {Point} - */ - this.spritePivot = new math.Point(); - - /** - * The rotation value of the texture, copied to a sprite when assigned to it. - * + * @private * @member {number} */ - this.rotation = 0; + this._rotation = 0; if (baseTexture.hasLoaded) { @@ -175,6 +169,17 @@ this._updateUvs(); } } + }, + rotation: { + get: function () + { + return this._rotation; + }, + set: function (val) { + this._rotation = val; + + this._updateUvs(); + } } }); @@ -253,6 +258,8 @@ this._uvs.x3 = frame.x / tw; this._uvs.y3 = (frame.y + frame.height) / th; + + this._uvs.rotate(this.rotation); }; /** diff --git a/src/core/textures/TextureUvs.js b/src/core/textures/TextureUvs.js index 5424d52..98db19a 100644 --- a/src/core/textures/TextureUvs.js +++ b/src/core/textures/TextureUvs.js @@ -1,3 +1,6 @@ +var halfPI = Math.PI / 2, + x, y; + function TextureUvs() { this.x0 = 0; @@ -14,3 +17,59 @@ } module.exports = TextureUvs; + +TextureUvs.prototype.rotate = function (angle) +{ + if (!angle) + { + return; + } + + // if not a multiple of (PI/2) + if (angle % halfPI) + { + // TODO: Not a multiple of (PI/2)... + } + // shift values for multiples of (PI/2) + else + { + // rotate the uvs by (PI/2) however many times are needed + if (angle > 0) { + for (var i = angle / halfPI; i > 0; --i) { + x = this.x3; + y = this.y3; + + this.x3 = this.x2; + this.y3 = this.y2; + + this.x2 = this.x1; + this.y2 = this.y1; + + this.x1 = this.x0; + this.y1 = this.y0; + + this.x0 = x; + this.y0 = y; + } + } + // rotate the uvs by -(PI/2) however many times are needed + else { + for (var i = angle / halfPI; i < 0; ++i) { + x = this.x0; + y = this.y0; + + this.x0 = this.x1; + this.y0 = this.y1; + + this.x1 = this.x2; + this.y1 = this.y2; + + this.x2 = this.x3; + this.y2 = this.y3; + + this.x3 = x; + this.y3 = y; + } + } + } +};