Skip to content

Commit

Permalink
add uniform scale
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Aug 4, 2023
1 parent a64ceb7 commit bbd7fb0
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/mat3-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
50 changes: 50 additions & 0 deletions src/mat4-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
22 changes: 22 additions & 0 deletions test/tests/mat3-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions test/tests/mat4-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit bbd7fb0

Please sign in to comment.