Skip to content

Commit d38cf0b

Browse files
committed
add mat3/4.add, mat3/4.multiplyScalar
1 parent 0b6c699 commit d38cf0b

File tree

4 files changed

+193
-54
lines changed

4 files changed

+193
-54
lines changed

src/mat3-impl.ts

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,49 @@ function negate<T extends Mat3Arg = MatType>(m: Mat3Arg, dst?: T) {
193193
return newDst;
194194
}
195195

196+
/**
197+
* multiply a matrix by a scalar matrix.
198+
* @param m - The matrix.
199+
* @param s - the scalar
200+
* @param dst - matrix to hold result. If not passed a new one is created.
201+
* @returns m * s.
202+
*/
203+
function multiplyScalar<T extends Mat3Arg = MatType>(m: Mat3Arg, s: number, dst?: T) {
204+
const newDst = (dst ?? new Ctor(12)) as T;
205+
206+
newDst[ 0] = m[ 0] * s; newDst[ 1] = m[ 1] * s; newDst[ 2] = m[ 2] * s;
207+
newDst[ 4] = m[ 4] * s; newDst[ 5] = m[ 5] * s; newDst[ 6] = m[ 6] * s;
208+
newDst[ 8] = m[ 8] * s; newDst[ 9] = m[ 9] * s; newDst[10] = m[10] * s;
209+
210+
return newDst;
211+
}
212+
213+
/**
214+
* multiply a matrix by a scalar matrix.
215+
* @param m - The matrix.
216+
* @param s - the scalar
217+
* @param dst - matrix to hold result. If not passed a new one is created.
218+
* @returns m * s.
219+
*/
220+
const mulScalar = multiplyScalar;
221+
222+
/**
223+
* add 2 matrices.
224+
* @param a - matrix 1.
225+
* @param b - matrix 2.
226+
* @param dst - matrix to hold result. If not passed a new one is created.
227+
* @returns a + b.
228+
*/
229+
function add<T extends Mat3Arg = MatType>(a: Mat3Arg, b: Mat3Arg, dst?: T) {
230+
const newDst = (dst ?? new Ctor(12)) as T;
231+
232+
newDst[ 0] = a[ 0] + b[ 0]; newDst[ 1] = a[ 1] + b[ 1]; newDst[ 2] = a[ 2] + b[ 2];
233+
newDst[ 4] = a[ 4] + b[ 4]; newDst[ 5] = a[ 5] + b[ 5]; newDst[ 6] = a[ 6] + b[ 6];
234+
newDst[ 8] = a[ 8] + b[ 8]; newDst[ 9] = a[ 9] + b[ 9]; newDst[10] = a[10] + b[10];
235+
236+
return newDst;
237+
}
238+
196239
/**
197240
* Copies a matrix. (same as {@link mat3.clone})
198241
* Also see {@link mat3.create} and {@link mat3.set}
@@ -980,46 +1023,49 @@ function uniformScale3D<T extends Mat3Arg = MatType>(m: Mat3Arg, s: number, dst?
9801023
}
9811024

9821025
return {
1026+
add,
9831027
clone,
1028+
copy,
9841029
create,
985-
set,
1030+
determinant,
1031+
equals,
1032+
equalsApproximately,
9861033
fromMat4,
9871034
fromQuat,
988-
negate,
989-
copy,
990-
equalsApproximately,
991-
equals,
1035+
get3DScaling,
1036+
getAxis,
1037+
getScaling,
1038+
getTranslation,
9921039
identity,
993-
transpose,
9941040
inverse,
9951041
invert,
996-
determinant,
9971042
mul,
1043+
mulScalar,
9981044
multiply,
999-
setTranslation,
1000-
getTranslation,
1001-
getAxis,
1002-
setAxis,
1003-
getScaling,
1004-
get3DScaling,
1005-
translation,
1006-
translate,
1007-
rotation,
1045+
multiplyScalar,
1046+
negate,
10081047
rotate,
1009-
rotationX,
10101048
rotateX,
1011-
rotationY,
10121049
rotateY,
1013-
rotationZ,
10141050
rotateZ,
1015-
scaling,
1051+
rotation,
1052+
rotationX,
1053+
rotationY,
1054+
rotationZ,
10161055
scale,
1017-
uniformScaling,
1018-
uniformScale,
1019-
scaling3D,
10201056
scale3D,
1021-
uniformScaling3D,
1057+
scaling,
1058+
scaling3D,
1059+
set,
1060+
setAxis,
1061+
setTranslation,
1062+
translate,
1063+
translation,
1064+
transpose,
1065+
uniformScale,
10221066
uniformScale3D,
1067+
uniformScaling,
1068+
uniformScaling3D,
10231069
};
10241070

10251071
}

src/mat4-impl.ts

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,51 @@ function negate<T extends Mat4Arg = MatType>(m: Mat4Arg, dst?: T) {
231231
return newDst;
232232
}
233233

234+
/**
235+
* add 2 matrices.
236+
* @param a - matrix 1.
237+
* @param b - matrix 2.
238+
* @param dst - matrix to hold result. If not passed a new one is created.
239+
* @returns a + b.
240+
*/
241+
function add<T extends Mat4Arg = MatType>(a: Mat4Arg, b: Mat4Arg, dst?: T) {
242+
const newDst = (dst ?? new Ctor(16)) as T;
243+
244+
newDst[ 0] = a[ 0] + b[ 0]; newDst[ 1] = a[ 1] + b[ 1]; newDst[ 2] = a[ 2] + b[ 2]; newDst[ 3] = a[ 3] + b[ 3];
245+
newDst[ 4] = a[ 4] + b[ 4]; newDst[ 5] = a[ 5] + b[ 5]; newDst[ 6] = a[ 6] + b[ 6]; newDst[ 7] = a[ 7] + b[ 7];
246+
newDst[ 8] = a[ 8] + b[ 8]; newDst[ 9] = a[ 9] + b[ 9]; newDst[10] = a[10] + b[10]; newDst[11] = a[11] + b[11];
247+
newDst[12] = a[12] + b[12]; newDst[13] = a[13] + b[13]; newDst[14] = a[14] + b[14]; newDst[15] = a[15] + b[15];
248+
249+
return newDst;
250+
}
251+
252+
/**
253+
* Multiplies a matrix by a scalar
254+
* @param m - The matrix.
255+
* @param s - The scalar
256+
* @param dst - matrix to hold result. If not passed a new one is created.
257+
* @returns m * s.
258+
*/
259+
function multiplyScalar<T extends Mat4Arg = MatType>(m: Mat4Arg, s: number, dst?: T) {
260+
const newDst = (dst ?? new Ctor(16)) as T;
261+
262+
newDst[ 0] = m[ 0] * s; newDst[ 1] = m[ 1] * s; newDst[ 2] = m[ 2] * s; newDst[ 3] = m[ 3] * s;
263+
newDst[ 4] = m[ 4] * s; newDst[ 5] = m[ 5] * s; newDst[ 6] = m[ 6] * s; newDst[ 7] = m[ 7] * s;
264+
newDst[ 8] = m[ 8] * s; newDst[ 9] = m[ 9] * s; newDst[10] = m[10] * s; newDst[11] = m[11] * s;
265+
newDst[12] = m[12] * s; newDst[13] = m[13] * s; newDst[14] = m[14] * s; newDst[15] = m[15] * s;
266+
267+
return newDst;
268+
}
269+
270+
/**
271+
* Multiplies a matrix by a scalar
272+
* @param m - The matrix.
273+
* @param s - The scalar
274+
* @param dst - matrix to hold result. If not passed a new one is created.
275+
* @returns m * s.
276+
*/
277+
const mulScalar = multiplyScalar;
278+
234279
/**
235280
* Copies a matrix. (same as {@link mat4.clone})
236281
* Also see {@link mat4.create} and {@link mat4.set}
@@ -1579,51 +1624,54 @@ function uniformScale<T extends Mat4Arg = MatType>(m: Mat4Arg, s: number, dst?:
15791624
}
15801625

15811626
return {
1627+
add,
1628+
aim,
1629+
axisRotate,
1630+
axisRotation,
1631+
cameraAim,
1632+
clone,
1633+
copy,
15821634
create,
1583-
set,
1635+
determinant,
1636+
equals,
1637+
equalsApproximately,
15841638
fromMat3,
15851639
fromQuat,
1586-
negate,
1587-
copy,
1588-
clone,
1589-
equalsApproximately,
1590-
equals,
1640+
frustum,
1641+
frustumReverseZ,
1642+
getAxis,
1643+
getScaling,
1644+
getTranslation,
15911645
identity,
1592-
transpose,
15931646
inverse,
1594-
determinant,
15951647
invert,
1596-
multiply,
1648+
lookAt,
15971649
mul,
1598-
setTranslation,
1599-
getTranslation,
1600-
getAxis,
1601-
setAxis,
1602-
getScaling,
1650+
mulScalar,
1651+
multiply,
1652+
multiplyScalar,
1653+
negate,
1654+
ortho,
16031655
perspective,
16041656
perspectiveReverseZ,
1605-
ortho,
1606-
frustum,
1607-
frustumReverseZ,
1608-
aim,
1609-
cameraAim,
1610-
lookAt,
1611-
translation,
1612-
translate,
1613-
rotationX,
1657+
rotate,
16141658
rotateX,
1615-
rotationY,
16161659
rotateY,
1617-
rotationZ,
16181660
rotateZ,
1619-
axisRotation,
16201661
rotation,
1621-
axisRotate,
1622-
rotate,
1623-
scaling,
1662+
rotationX,
1663+
rotationY,
1664+
rotationZ,
16241665
scale,
1625-
uniformScaling,
1666+
scaling,
1667+
set,
1668+
setAxis,
1669+
setTranslation,
1670+
translate,
1671+
translation,
1672+
transpose,
16261673
uniformScale,
1674+
uniformScaling,
16271675
};
16281676

16291677
}

test/tests/mat3-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ function check(mat3, Type) {
119119
}, expected);
120120
});
121121

122+
it('should add', () => {
123+
const expected = [
124+
0, 2, 4, 6,
125+
8, 10, 12, 14,
126+
16, 18, 20, 22,
127+
];
128+
testMat3WithAndWithoutDest((newDst) => {
129+
return mat3.add(m, m, newDst);
130+
}, expected);
131+
});
132+
133+
it('should multiplyScalar', () => {
134+
const expected = [
135+
0, 2, 4, 6,
136+
8, 10, 12, 14,
137+
16, 18, 20, 22,
138+
];
139+
testMat3WithAndWithoutDest((newDst) => {
140+
return mat3.multiplyScalar(m, 2, newDst);
141+
}, expected);
142+
});
143+
122144
it('should copy', () => {
123145
const expected = m;
124146
testMat3WithAndWithoutDest((newDst) => {

test/tests/mat4-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ function check(mat4, Type) {
9494
}, expected);
9595
});
9696

97+
it('should add', () => {
98+
const expected = [
99+
0, 2, 4, 6,
100+
8, 10, 12, 14,
101+
16, 18, 20, 22,
102+
24, 26, 28, 30,
103+
];
104+
testMat4WithAndWithoutDest((newDst) => {
105+
return mat4.add(m, m, newDst);
106+
}, expected);
107+
});
108+
109+
it('should multiplyScalar', () => {
110+
const expected = [
111+
0, 2, 4, 6,
112+
8, 10, 12, 14,
113+
16, 18, 20, 22,
114+
24, 26, 28, 30,
115+
];
116+
testMat4WithAndWithoutDest((newDst) => {
117+
return mat4.multiplyScalar(m, 2, newDst);
118+
}, expected);
119+
});
97120
it('should copy', () => {
98121
const expected = m;
99122
testMat4WithAndWithoutDest((newDst) => {

0 commit comments

Comments
 (0)