diff --git a/packages/math/src/Matrix.js b/packages/math/src/Matrix.js index bf1d0e0..5f81325 100644 --- a/packages/math/src/Matrix.js +++ b/packages/math/src/Matrix.js @@ -1,4 +1,5 @@ import Point from './Point'; +import { PI_2 } from './const'; /** * The PixiJS Matrix class as an object, which makes it a lot faster, @@ -294,25 +295,13 @@ */ setTransform(x, y, pivotX, pivotY, scaleX, scaleY, rotation, skewX, skewY) { - const sr = Math.sin(rotation); - const cr = Math.cos(rotation); - const cy = Math.cos(skewY); - const sy = Math.sin(skewY); - const nsx = -Math.sin(skewX); - const cx = Math.cos(skewX); + this.a = Math.cos(rotation + skewY) * scaleX; + this.b = Math.sin(rotation + skewY) * scaleX; + this.c = -Math.sin(rotation - skewX) * scaleY; + this.d = Math.cos(rotation - skewX) * scaleY; - const a = cr * scaleX; - const b = sr * scaleX; - const c = -sr * scaleY; - const d = cr * scaleY; - - this.a = (cy * a) + (sy * c); - this.b = (cy * b) + (sy * d); - this.c = (nsx * a) + (cx * c); - this.d = (nsx * b) + (cx * d); - - this.tx = x + ((pivotX * a) + (pivotY * c)); - this.ty = y + ((pivotX * b) + (pivotY * d)); + this.tx = x - ((pivotX * this.a) + (pivotY * this.c)); + this.ty = y - ((pivotX * this.b) + (pivotY * this.d)); return this; } @@ -363,7 +352,7 @@ const delta = Math.abs(skewX + skewY); - if (delta < 0.00001) + if (delta < 0.00001 || Math.abs(PI_2 - delta) < 0.00001) { transform.rotation = skewY; @@ -376,6 +365,7 @@ } else { + transform.rotation = 0; transform.skew.x = skewX; transform.skew.y = skewY; } diff --git a/packages/math/src/Matrix.js b/packages/math/src/Matrix.js index bf1d0e0..5f81325 100644 --- a/packages/math/src/Matrix.js +++ b/packages/math/src/Matrix.js @@ -1,4 +1,5 @@ import Point from './Point'; +import { PI_2 } from './const'; /** * The PixiJS Matrix class as an object, which makes it a lot faster, @@ -294,25 +295,13 @@ */ setTransform(x, y, pivotX, pivotY, scaleX, scaleY, rotation, skewX, skewY) { - const sr = Math.sin(rotation); - const cr = Math.cos(rotation); - const cy = Math.cos(skewY); - const sy = Math.sin(skewY); - const nsx = -Math.sin(skewX); - const cx = Math.cos(skewX); + this.a = Math.cos(rotation + skewY) * scaleX; + this.b = Math.sin(rotation + skewY) * scaleX; + this.c = -Math.sin(rotation - skewX) * scaleY; + this.d = Math.cos(rotation - skewX) * scaleY; - const a = cr * scaleX; - const b = sr * scaleX; - const c = -sr * scaleY; - const d = cr * scaleY; - - this.a = (cy * a) + (sy * c); - this.b = (cy * b) + (sy * d); - this.c = (nsx * a) + (cx * c); - this.d = (nsx * b) + (cx * d); - - this.tx = x + ((pivotX * a) + (pivotY * c)); - this.ty = y + ((pivotX * b) + (pivotY * d)); + this.tx = x - ((pivotX * this.a) + (pivotY * this.c)); + this.ty = y - ((pivotX * this.b) + (pivotY * this.d)); return this; } @@ -363,7 +352,7 @@ const delta = Math.abs(skewX + skewY); - if (delta < 0.00001) + if (delta < 0.00001 || Math.abs(PI_2 - delta) < 0.00001) { transform.rotation = skewY; @@ -376,6 +365,7 @@ } else { + transform.rotation = 0; transform.skew.x = skewX; transform.skew.y = skewY; } diff --git a/packages/math/test/Matrix.js b/packages/math/test/Matrix.js index c0be31e..5aeed05 100644 --- a/packages/math/test/Matrix.js +++ b/packages/math/test/Matrix.js @@ -1,4 +1,4 @@ -const { Matrix } = require('../'); +const { Matrix, Transform } = require('../'); describe('PIXI.Matrix', function () { @@ -163,4 +163,38 @@ expect(matrix.tx).to.equal(0); expect(matrix.ty).to.equal(0); }); + + it('should have the same transform after decompose', function () + { + const matrix = new Matrix(); + const transformInitial = new Transform(); + const transformDecomposed = new Transform(); + + for (let x = 0; x < 50; ++x) + { + transformInitial.position.x = (Math.random() * 1000) - 2000; + transformInitial.position.y = (Math.random() * 1000) - 2000; + transformInitial.scale.x = (Math.random() * 5) - 10; + transformInitial.scale.y = (Math.random() * 5) - 10; + transformInitial.rotation = (Math.random() - 2) * Math.PI; + transformInitial.skew.x = (Math.random() - 2) * Math.PI; + transformInitial.skew.y = (Math.random() - 2) * Math.PI; + + matrix.setTransform( + transformInitial.x, transformInitial.y, + 0, 0, + transformInitial.scale.x, transformInitial.scale.y, + transformInitial.rotation, + transformInitial.skew.x, transformInitial.skew.y + ); + matrix.decompose(transformDecomposed); + + expect(transformInitial.a).to.equal(transformDecomposed.a); + expect(transformInitial.b).to.equal(transformDecomposed.b); + expect(transformInitial.c).to.equal(transformDecomposed.c); + expect(transformInitial.d).to.equal(transformDecomposed.d); + expect(transformInitial.tx).to.equal(transformDecomposed.tx); + expect(transformInitial.ty).to.equal(transformDecomposed.ty); + } + }); });