diff --git a/src/mat3-impl.ts b/src/mat3-impl.ts index dbf35fd..74c97f9 100644 --- a/src/mat3-impl.ts +++ b/src/mat3-impl.ts @@ -735,3 +735,46 @@ export function scale(m: Mat3, v: Vec2, dst?: Mat3): Mat3 { return dst; } +/** + * Creates a 3-by-3 matrix which scales uniformly in each dimension + * @param s - Amount to scale + * @param dst - matrix to hold result. If not passed a new one is created. + * @returns The scaling matrix. + */ +export function uniformScaling(s: number, dst?: Mat3): Mat3 { + dst = dst || newMat3(); + + dst[ 0] = s; dst[ 1] = 0; dst[ 2] = 0; + dst[ 4] = 0; dst[ 5] = s; dst[ 6] = 0; + dst[ 8] = 0; dst[ 9] = 0; dst[10] = 1; + + return dst; +} + +/** + * Scales the given 3-by-3 matrix in each dimension by an amount + * given. + * @param m - The matrix to be modified. + * @param s - Amount to scale. + * @param dst - matrix to hold result. If not passed a new one is created. + * @returns The scaled matrix. + */ +export function uniformScale(m: Mat3, s: number, dst?: Mat3): Mat3 { + dst = dst || newMat3(); + + dst[ 0] = s * m[0 * 4 + 0]; + dst[ 1] = s * m[0 * 4 + 1]; + dst[ 2] = s * m[0 * 4 + 2]; + + dst[ 4] = s * m[1 * 4 + 0]; + dst[ 5] = s * m[1 * 4 + 1]; + dst[ 6] = s * m[1 * 4 + 2]; + + if (m !== dst) { + dst[ 8] = m[ 8]; + dst[ 9] = m[ 9]; + dst[10] = m[10]; + } + + return dst; +} diff --git a/src/mat4-impl.ts b/src/mat4-impl.ts index 131d6e5..205ac65 100644 --- a/src/mat4-impl.ts +++ b/src/mat4-impl.ts @@ -1449,3 +1449,53 @@ export function scale(m: Mat4, v: Vec3, dst?: Mat4): Mat4 { return dst; } + +/** + * Creates a 4-by-4 matrix which scales a uniform amount in each dimension. + * @param s - the amount to scale + * @param dst - matrix to hold result. If not passed a new one is created. + * @returns The scaling matrix. + */ +export function uniformScaling(s: number, dst?: Mat4): Mat4 { + dst = dst || new MatType(16); + + dst[ 0] = s; dst[ 1] = 0; dst[ 2] = 0; dst[ 3] = 0; + dst[ 4] = 0; dst[ 5] = s; dst[ 6] = 0; dst[ 7] = 0; + dst[ 8] = 0; dst[ 9] = 0; dst[10] = s; dst[11] = 0; + dst[12] = 0; dst[13] = 0; dst[14] = 0; dst[15] = 1; + + return dst; +} + +/** + * Scales the given 4-by-4 matrix in each dimension by a uniform scale. + * @param m - The matrix to be modified. + * @param s - The amount to scale. + * @param dst - matrix to hold result. If not passed a new one is created. + * @returns The scaled matrix. + */ +export function uniformScale(m: Mat4, s: number, dst?: Mat4): Mat4 { + dst = dst || new MatType(16); + + dst[ 0] = s * m[0 * 4 + 0]; + dst[ 1] = s * m[0 * 4 + 1]; + dst[ 2] = s * m[0 * 4 + 2]; + dst[ 3] = s * m[0 * 4 + 3]; + dst[ 4] = s * m[1 * 4 + 0]; + dst[ 5] = s * m[1 * 4 + 1]; + dst[ 6] = s * m[1 * 4 + 2]; + dst[ 7] = s * m[1 * 4 + 3]; + dst[ 8] = s * m[2 * 4 + 0]; + dst[ 9] = s * m[2 * 4 + 1]; + dst[10] = s * m[2 * 4 + 2]; + dst[11] = s * m[2 * 4 + 3]; + + if (m !== dst) { + dst[12] = m[12]; + dst[13] = m[13]; + dst[14] = m[14]; + dst[15] = m[15]; + } + + return dst; +} \ No newline at end of file diff --git a/test/tests/mat3-test.js b/test/tests/mat3-test.js index c523013..49f8ada 100644 --- a/test/tests/mat3-test.js +++ b/test/tests/mat3-test.js @@ -441,6 +441,28 @@ function check(Type) { }, expected); }); + it('should make uniform scaling matrix', () => { + const expected = [ + 2, 0, 0, 0, + 0, 2, 0, 0, + 0, 0, 1, 0, + ]; + testMat3WithAndWithoutDest((dst) => { + return mat3.uniformScaling(2, dst); + }, expected); + }); + + it('should uniformly scale', () => { + const expected = [ + 0, 2, 4, 0, + 8, 10, 12, 0, + 8, 9, 10, 0, + ]; + testMat3WithAndWithoutDest((dst) => { + return mat3.uniformScale(m, 2, dst); + }, expected); + }); + it('should make a mat3 from mat4', () => { const expected = [ 1, 2, 3, 0, diff --git a/test/tests/mat4-test.js b/test/tests/mat4-test.js index 3ff47dd..d4494ad 100644 --- a/test/tests/mat4-test.js +++ b/test/tests/mat4-test.js @@ -909,6 +909,30 @@ function check(Type) { }, expected); }); + it('should make uniform scaling matrix', () => { + const expected = [ + 2, 0, 0, 0, + 0, 2, 0, 0, + 0, 0, 2, 0, + 0, 0, 0, 1, + ]; + testMat4WithAndWithoutDest((dst) => { + return mat4.uniformScaling(2, dst); + }, expected); + }); + + it('should uniformly scale', () => { + const expected = [ + 0, 2, 4, 6, + 8, 10, 12, 14, + 16, 18, 20, 22, + 12, 13, 14, 15, + ]; + testMat4WithAndWithoutDest((dst) => { + return mat4.uniformScale(m, 2, dst); + }, expected); + }); + it('should make a mat4 from mat3', () => { const expected = [ 1, 2, 3, 0,