Skip to content

Commit ab0bea7

Browse files
committed
fix mat3 not getting extra element for number[]
1 parent affb147 commit ab0bea7

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

src/wgpu-matrix.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ function wgpuMatrixAPI<
7474
Vec4Ctor: BaseCtor<Vec4>,
7575
) {
7676
return {
77-
/** @namespace mat4 */
78-
mat4: getMat4API<Mat3>(Mat3Ctor),
7977
/** @namespace mat3 */
80-
mat3: getMat3API<Mat4>(Mat4Ctor),
78+
mat3: getMat3API<Mat3>(Mat3Ctor),
79+
/** @namespace mat4 */
80+
mat4: getMat4API<Mat4>(Mat4Ctor),
8181
/** @namespace quat */
8282
quat: getQuatAPI<Quat>(QuatCtor),
8383
/** @namespace vec2 */
@@ -91,15 +91,15 @@ function wgpuMatrixAPI<
9191

9292
export const {
9393
/**
94-
* 4x4 Matrix functions that default to returning `Float32Array`
94+
* 3x3 Matrix functions that default to returning `Float32Array`
9595
* @namespace
9696
*/
97-
mat4,
97+
mat3,
9898
/**
99-
* 3x3 Matrix functions that default to returning `Float32Array`
99+
* 4x4 Matrix functions that default to returning `Float32Array`
100100
* @namespace
101101
*/
102-
mat3,
102+
mat4,
103103
/**
104104
* Quaternion functions that default to returning `Float32Array`
105105
* @namespace
@@ -126,15 +126,15 @@ export const {
126126

127127
export const {
128128
/**
129-
* 4x4 Matrix functions that default to returning `Float64Array`
129+
* 3x3 Matrix functions that default to returning `Float64Array`
130130
* @namespace
131131
*/
132-
mat4: mat4d,
132+
mat3: mat3d,
133133
/**
134-
* 3x3 Matrix functions that default to returning `Float64Array`
134+
* 4x4 Matrix functions that default to returning `Float64Array`
135135
* @namespace
136136
*/
137-
mat3: mat3d,
137+
mat4: mat4d,
138138
/**
139139
* Quaternion functions that default to returning `Float64Array`
140140
* @namespace
@@ -161,15 +161,15 @@ export const {
161161

162162
export const {
163163
/**
164-
* 4x4 Matrix functions that default to returning `number[]`
164+
* 3x3 Matrix functions that default to returning `number[]`
165165
* @namespace
166166
*/
167-
mat4: mat4n,
167+
mat3: mat3n,
168168
/**
169-
* 3x3 Matrix functions that default to returning `number[]`
169+
* 4x4 Matrix functions that default to returning `number[]`
170170
* @namespace
171171
*/
172-
mat3: mat3n,
172+
mat4: mat4n,
173173
/**
174174
* Quaternion functions that default to returning `number[]`
175175
* @namespace

test/tests/mat3-test.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,33 @@ function assertMat3EqualApproximately(a, b) {
2121
}
2222
}
2323

24-
function check(Type) {
24+
function check(mat3, Type) {
2525
describe('using ' + Type, () => {
2626
const m = [
2727
0, 1, 2, 0,
2828
4, 5, 6, 0,
2929
8, 9, 10, 0,
3030
];
3131

32+
function createCopyOfType(v) {
33+
return Type === Array ? new Type(...v) : new Type(v);
34+
}
35+
3236
function testM3WithoutDest(func, expected, ...args) {
3337
const d = func(...args);
3438
assertMat3EqualApproximately(d, expected);
3539
}
3640

3741
function testM3WithDest(func, expected, ...args) {
38-
expected = new Float32Array(expected);
39-
const d = new Float32Array(12);
42+
expected = createCopyOfType(expected);
43+
const d = new Type(12).fill(0);
4044
const c = func(...args, d);
4145
assertStrictEqual(c, d);
4246
assertMat3EqualApproximately(c, expected);
4347
}
4448

4549
function testMat3WithAndWithoutDest(func, expected, ...args) {
46-
if (Type === Float32Array) {
47-
expected = new Float32Array(expected);
48-
}
50+
expected = createCopyOfType(expected);
4951
testM3WithoutDest(func, expected, ...args);
5052
testM3WithDest(func, expected, ...args);
5153
}
@@ -56,14 +58,14 @@ function check(Type) {
5658
}
5759

5860
function testV2WithDest(func, expected) {
59-
const d = new Float32Array(2);
61+
const d = new Type(2).fill(0);
6062
const c = func(d);
6163
assertStrictEqual(c, d);
6264
assertEqual(c, expected);
6365
}
6466

6567
function testV2WithAndWithoutDest(func, expected) {
66-
expected = new Float32Array(expected);
68+
expected = createCopyOfType(expected);
6769
testV2WithoutDest(func, expected);
6870
testV2WithDest(func, expected);
6971
}
@@ -90,9 +92,9 @@ function check(Type) {
9092

9193
it('should negate', () => {
9294
const expected = [
93-
-0, -1, -2, -3,
94-
-4, -5, -6, -7,
95-
-8, -9, -10, -11,
95+
-0, -1, -2, 0,
96+
-4, -5, -6, 0,
97+
-8, -9, -10, 0,
9698
];
9799
testMat3WithAndWithoutDest((newDst) => {
98100
return mat3.negate(m, newDst);
@@ -350,12 +352,12 @@ function check(Type) {
350352

351353
it('should get scaling', () => {
352354
const m = [
353-
1, 2, 3, 0,
355+
2, 8, 3, 0,
354356
5, 6, 7, 0,
355357
9, 10, 11, 0,
356358
];
357359
const expected = [
358-
Math.sqrt(1 * 1 + 2 * 2),
360+
Math.sqrt(2 * 2 + 8 * 8),
359361
Math.sqrt(5 * 5 + 6 * 6),
360362
];
361363
testV2WithAndWithoutDest((newDst) => {
@@ -491,8 +493,8 @@ function check(Type) {
491493
}
492494

493495
describe('mat3', () => {
494-
check(mat3n);
495-
check(mat3);
496-
check(mat3d);
496+
check(mat3n, Array);
497+
check(mat3, Float32Array);
498+
check(mat3d, Float64Array);
497499
});
498500

test/tests/mat4-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {describe, it} from '../mocha-support.js';
1212

1313

1414
function check(mat4, Type) {
15-
1615
describe('using ' + Type, () => {
1716
const m = [
1817
0, 1, 2, 3,

0 commit comments

Comments
 (0)