diff --git a/src/core/textures/Texture.js b/src/core/textures/Texture.js index 2508070..5376b0f 100644 --- a/src/core/textures/Texture.js +++ b/src/core/textures/Texture.js @@ -17,7 +17,7 @@ * @param [crop] {Rectangle} The area of original texture * @param [trim] {Rectangle} Trimmed texture rectangle */ -function Texture(baseTexture, frame, crop, trim) +function Texture(baseTexture, frame, crop, trim, rotate) { /** * Does this Texture have any frame data assigned to it? @@ -111,7 +111,7 @@ * @private * @member {number} */ - this._rotation = 0; + this.rotate = !!rotate; if (baseTexture.hasLoaded) { @@ -149,7 +149,7 @@ this.crop = frame; - if (!this.trim && (frame.x + frame.width > this.baseTexture.width || frame.y + frame.height > this.baseTexture.height)) + if (!this.trim && !this.rotate && (frame.x + frame.width > this.baseTexture.width || frame.y + frame.height > this.baseTexture.height)) { throw new Error('Texture Error: frame does not fit inside the base Texture dimensions ' + this); } @@ -169,17 +169,6 @@ this._updateUvs(); } } - }, - rotation: { - get: function () - { - return this._rotation; - }, - set: function (val) { - this._rotation = val; - - this._updateUvs(); - } } }); @@ -243,23 +232,7 @@ this._uvs = new TextureUvs(); } - var frame = this.crop; - var tw = this.baseTexture.width; - var th = this.baseTexture.height; - - this._uvs.x0 = frame.x / tw; - this._uvs.y0 = frame.y / th; - - this._uvs.x1 = (frame.x + frame.width) / tw; - this._uvs.y1 = frame.y / th; - - this._uvs.x2 = (frame.x + frame.width) / tw; - this._uvs.y2 = (frame.y + frame.height) / th; - - this._uvs.x3 = frame.x / tw; - this._uvs.y3 = (frame.y + frame.height) / th; - - this._uvs.rotate(this.rotation); + this._uvs.set(this.crop, this.baseTexture, this.rotate); }; /** diff --git a/src/core/textures/Texture.js b/src/core/textures/Texture.js index 2508070..5376b0f 100644 --- a/src/core/textures/Texture.js +++ b/src/core/textures/Texture.js @@ -17,7 +17,7 @@ * @param [crop] {Rectangle} The area of original texture * @param [trim] {Rectangle} Trimmed texture rectangle */ -function Texture(baseTexture, frame, crop, trim) +function Texture(baseTexture, frame, crop, trim, rotate) { /** * Does this Texture have any frame data assigned to it? @@ -111,7 +111,7 @@ * @private * @member {number} */ - this._rotation = 0; + this.rotate = !!rotate; if (baseTexture.hasLoaded) { @@ -149,7 +149,7 @@ this.crop = frame; - if (!this.trim && (frame.x + frame.width > this.baseTexture.width || frame.y + frame.height > this.baseTexture.height)) + if (!this.trim && !this.rotate && (frame.x + frame.width > this.baseTexture.width || frame.y + frame.height > this.baseTexture.height)) { throw new Error('Texture Error: frame does not fit inside the base Texture dimensions ' + this); } @@ -169,17 +169,6 @@ this._updateUvs(); } } - }, - rotation: { - get: function () - { - return this._rotation; - }, - set: function (val) { - this._rotation = val; - - this._updateUvs(); - } } }); @@ -243,23 +232,7 @@ this._uvs = new TextureUvs(); } - var frame = this.crop; - var tw = this.baseTexture.width; - var th = this.baseTexture.height; - - this._uvs.x0 = frame.x / tw; - this._uvs.y0 = frame.y / th; - - this._uvs.x1 = (frame.x + frame.width) / tw; - this._uvs.y1 = frame.y / th; - - this._uvs.x2 = (frame.x + frame.width) / tw; - this._uvs.y2 = (frame.y + frame.height) / th; - - this._uvs.x3 = frame.x / tw; - this._uvs.y3 = (frame.y + frame.height) / th; - - this._uvs.rotate(this.rotation); + this._uvs.set(this.crop, this.baseTexture, this.rotate); }; /** diff --git a/src/core/textures/TextureUvs.js b/src/core/textures/TextureUvs.js index f06eddb..99d8df2 100644 --- a/src/core/textures/TextureUvs.js +++ b/src/core/textures/TextureUvs.js @@ -18,62 +18,38 @@ module.exports = TextureUvs; -TextureUvs.prototype.rotate = function (angle) +TextureUvs.prototype.set = function (frame, baseFrame, rotate) { - if (!angle) - { - return; - } + var tw = baseFrame.width; + var th = baseFrame.height; - // if not a multiple of (PI/2) - if (angle % halfPI) + if(rotate) { - // TODO: Not a multiple of (PI/2)... + this.x0 = (frame.x + frame.height) / tw; + this.y0 = frame.y / th; + + this.x1 = (frame.x + frame.height) / tw; + this.y1 = (frame.y + frame.width) / th; + + this.x2 = frame.x / tw; + this.y2 = (frame.y + frame.width) / th; + + this.x3 = frame.x / tw; + this.y3 = frame.y / th; } - // 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.x0 = frame.x / tw; + this.y0 = frame.y / th; - this.x2 = this.x1; - this.y2 = this.y1; + this.x1 = (frame.x + frame.width) / tw; + this.y1 = frame.y / th; - this.x1 = this.x0; - this.y1 = this.y0; + this.x2 = (frame.x + frame.width) / tw; + this.y2 = (frame.y + frame.height) / th; - 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; - } - } + this.x3 = frame.x / tw; + this.y3 = (frame.y + frame.height) / th; } -}; +} diff --git a/src/core/textures/Texture.js b/src/core/textures/Texture.js index 2508070..5376b0f 100644 --- a/src/core/textures/Texture.js +++ b/src/core/textures/Texture.js @@ -17,7 +17,7 @@ * @param [crop] {Rectangle} The area of original texture * @param [trim] {Rectangle} Trimmed texture rectangle */ -function Texture(baseTexture, frame, crop, trim) +function Texture(baseTexture, frame, crop, trim, rotate) { /** * Does this Texture have any frame data assigned to it? @@ -111,7 +111,7 @@ * @private * @member {number} */ - this._rotation = 0; + this.rotate = !!rotate; if (baseTexture.hasLoaded) { @@ -149,7 +149,7 @@ this.crop = frame; - if (!this.trim && (frame.x + frame.width > this.baseTexture.width || frame.y + frame.height > this.baseTexture.height)) + if (!this.trim && !this.rotate && (frame.x + frame.width > this.baseTexture.width || frame.y + frame.height > this.baseTexture.height)) { throw new Error('Texture Error: frame does not fit inside the base Texture dimensions ' + this); } @@ -169,17 +169,6 @@ this._updateUvs(); } } - }, - rotation: { - get: function () - { - return this._rotation; - }, - set: function (val) { - this._rotation = val; - - this._updateUvs(); - } } }); @@ -243,23 +232,7 @@ this._uvs = new TextureUvs(); } - var frame = this.crop; - var tw = this.baseTexture.width; - var th = this.baseTexture.height; - - this._uvs.x0 = frame.x / tw; - this._uvs.y0 = frame.y / th; - - this._uvs.x1 = (frame.x + frame.width) / tw; - this._uvs.y1 = frame.y / th; - - this._uvs.x2 = (frame.x + frame.width) / tw; - this._uvs.y2 = (frame.y + frame.height) / th; - - this._uvs.x3 = frame.x / tw; - this._uvs.y3 = (frame.y + frame.height) / th; - - this._uvs.rotate(this.rotation); + this._uvs.set(this.crop, this.baseTexture, this.rotate); }; /** diff --git a/src/core/textures/TextureUvs.js b/src/core/textures/TextureUvs.js index f06eddb..99d8df2 100644 --- a/src/core/textures/TextureUvs.js +++ b/src/core/textures/TextureUvs.js @@ -18,62 +18,38 @@ module.exports = TextureUvs; -TextureUvs.prototype.rotate = function (angle) +TextureUvs.prototype.set = function (frame, baseFrame, rotate) { - if (!angle) - { - return; - } + var tw = baseFrame.width; + var th = baseFrame.height; - // if not a multiple of (PI/2) - if (angle % halfPI) + if(rotate) { - // TODO: Not a multiple of (PI/2)... + this.x0 = (frame.x + frame.height) / tw; + this.y0 = frame.y / th; + + this.x1 = (frame.x + frame.height) / tw; + this.y1 = (frame.y + frame.width) / th; + + this.x2 = frame.x / tw; + this.y2 = (frame.y + frame.width) / th; + + this.x3 = frame.x / tw; + this.y3 = frame.y / th; } - // 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.x0 = frame.x / tw; + this.y0 = frame.y / th; - this.x2 = this.x1; - this.y2 = this.y1; + this.x1 = (frame.x + frame.width) / tw; + this.y1 = frame.y / th; - this.x1 = this.x0; - this.y1 = this.y0; + this.x2 = (frame.x + frame.width) / tw; + this.y2 = (frame.y + frame.height) / th; - 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; - } - } + this.x3 = frame.x / tw; + this.y3 = (frame.y + frame.height) / th; } -}; +} diff --git a/src/loaders/spritesheetParser.js b/src/loaders/spritesheetParser.js index f97a477..032028b 100644 --- a/src/loaders/spritesheetParser.js +++ b/src/loaders/spritesheetParser.js @@ -50,13 +50,16 @@ ); } - resource.textures[i] = new core.Texture(res.texture.baseTexture, size, size.clone(), trim); - - if (frames[i].rotated) { - // resource.textures[i].pivot.x = frames[i].pivot.x; - // resource.textures[i].pivot.y = frames[i].pivot.y; - resource.textures[i].rotation = -Math.PI / 2; + // flip the width and height! + if (frames[i].rotated) + { + var temp = size.width; + size.width = size.height; + size.height = temp; } + + resource.textures[i] = new core.Texture(res.texture.baseTexture, size, size.clone(), trim, frames[i].rotated); + } }