Skip to content

Commit b9c21a7

Browse files
committed
add dst to m3
1 parent 235049e commit b9c21a7

File tree

1 file changed

+159
-66
lines changed

1 file changed

+159
-66
lines changed

webgl/resources/m3.js

Lines changed: 159 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 GFXFundamentals.
2+
* Copyright 2021, GFXFundamentals.
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -46,20 +46,42 @@
4646
"use strict";
4747

4848
/**
49-
* An array or typed array with 9 values.
49+
* An array or typed array with 2 values.
50+
* @typedef {number[]|TypedArray} Vector2
51+
* @memberOf module:webgl-2d-math
52+
*/
53+
54+
/**
55+
* An array or typed array with 16 values.
5056
* @typedef {number[]|TypedArray} Matrix3
5157
* @memberOf module:webgl-2d-math
5258
*/
5359

60+
61+
let MatType = Float32Array;
62+
63+
/**
64+
* Sets the type this library creates for a Mat3
65+
* @param {constructor} Ctor the constructor for the type. Either `Float32Array` or `Array`
66+
* @return {constructor} previous constructor for Mat3
67+
*/
68+
function setDefaultType(Ctor) {
69+
const OldType = MatType;
70+
MatType = Ctor;
71+
return OldType;
72+
}
73+
5474
/**
5575
* Takes two Matrix3s, a and b, and computes the product in the order
5676
* that pre-composes b with a. In other words, the matrix returned will
5777
* @param {module:webgl-2d-math.Matrix3} a A matrix.
5878
* @param {module:webgl-2d-math.Matrix3} b A matrix.
79+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
5980
* @return {module:webgl-2d-math.Matrix3} the result.
6081
* @memberOf module:webgl-2d-math
6182
*/
62-
function multiply(a, b) {
83+
function multiply(a, b, dst) {
84+
dst = dst || new MatType(9);
6385
var a00 = a[0 * 3 + 0];
6486
var a01 = a[0 * 3 + 1];
6587
var a02 = a[0 * 3 + 2];
@@ -79,139 +101,188 @@
79101
var b21 = b[2 * 3 + 1];
80102
var b22 = b[2 * 3 + 2];
81103

82-
return [
83-
b00 * a00 + b01 * a10 + b02 * a20,
84-
b00 * a01 + b01 * a11 + b02 * a21,
85-
b00 * a02 + b01 * a12 + b02 * a22,
86-
b10 * a00 + b11 * a10 + b12 * a20,
87-
b10 * a01 + b11 * a11 + b12 * a21,
88-
b10 * a02 + b11 * a12 + b12 * a22,
89-
b20 * a00 + b21 * a10 + b22 * a20,
90-
b20 * a01 + b21 * a11 + b22 * a21,
91-
b20 * a02 + b21 * a12 + b22 * a22,
92-
];
104+
dst[0] = b00 * a00 + b01 * a10 + b02 * a20;
105+
dst[1] = b00 * a01 + b01 * a11 + b02 * a21;
106+
dst[2] = b00 * a02 + b01 * a12 + b02 * a22;
107+
dst[3] = b10 * a00 + b11 * a10 + b12 * a20;
108+
dst[4] = b10 * a01 + b11 * a11 + b12 * a21;
109+
dst[5] = b10 * a02 + b11 * a12 + b12 * a22;
110+
dst[6] = b20 * a00 + b21 * a10 + b22 * a20;
111+
dst[7] = b20 * a01 + b21 * a11 + b22 * a21;
112+
dst[8] = b20 * a02 + b21 * a12 + b22 * a22;
113+
114+
return dst;
93115
}
94116

95117

96118
/**
97119
* Creates a 3x3 identity matrix
120+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
98121
* @return {module:webgl2-2d-math.Matrix3} an identity matrix
99122
*/
100-
function identity() {
101-
return [
102-
1, 0, 0,
103-
0, 1, 0,
104-
0, 0, 1,
105-
];
123+
function identity(dst) {
124+
dst = dst || new MatType(9);
125+
dst[0] = 1;
126+
dst[1] = 0;
127+
dst[2] = 0;
128+
dst[3] = 0;
129+
dst[4] = 1;
130+
dst[5] = 0;
131+
dst[6] = 0;
132+
dst[7] = 0;
133+
dst[8] = 1;
134+
135+
return dst;
106136
}
107137

108138
/**
109139
* Creates a 2D projection matrix
110140
* @param {number} width width in pixels
111141
* @param {number} height height in pixels
142+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
112143
* @return {module:webgl-2d-math.Matrix3} a projection matrix that converts from pixels to clipspace with Y = 0 at the top.
113144
* @memberOf module:webgl-2d-math
114145
*/
115-
function projection(width, height) {
146+
function projection(width, height, dst) {
147+
dst = dst || new MatType(9);
116148
// Note: This matrix flips the Y axis so 0 is at the top.
117-
return [
118-
2 / width, 0, 0,
119-
0, -2 / height, 0,
120-
-1, 1, 1,
121-
];
149+
150+
dst[0] = 2 / width;
151+
dst[1] = 0;
152+
dst[2] = 0;
153+
dst[3] = 0;
154+
dst[4] = -2 / height;
155+
dst[5] = 0;
156+
dst[6] = -1;
157+
dst[7] = 1;
158+
dst[8] = 1;
159+
160+
return dst;
122161
}
123162

124163
/**
125164
* Multiplies by a 2D projection matrix
126165
* @param {module:webgl-2d-math.Matrix3} the matrix to be multiplied
127166
* @param {number} width width in pixels
128167
* @param {number} height height in pixels
168+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
129169
* @return {module:webgl-2d-math.Matrix3} the result
130170
* @memberOf module:webgl-2d-math
131171
*/
132-
function project(m, width, height) {
133-
return multiply(m, projection(width, height));
172+
function project(m, width, height, dst) {
173+
return multiply(m, projection(width, height), dst);
134174
}
135175

136176
/**
137177
* Creates a 2D translation matrix
138178
* @param {number} tx amount to translate in x
139179
* @param {number} ty amount to translate in y
180+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
140181
* @return {module:webgl-2d-math.Matrix3} a translation matrix that translates by tx and ty.
141182
* @memberOf module:webgl-2d-math
142183
*/
143-
function translation(tx, ty) {
144-
return [
145-
1, 0, 0,
146-
0, 1, 0,
147-
tx, ty, 1,
148-
];
184+
function translation(tx, ty, dst) {
185+
dst = dst || new MatType(9);
186+
187+
dst[0] = 1;
188+
dst[1] = 0;
189+
dst[2] = 0;
190+
dst[3] = 0;
191+
dst[4] = 1;
192+
dst[5] = 0;
193+
dst[6] = tx;
194+
dst[7] = ty;
195+
dst[8] = 1;
196+
197+
return dst;
149198
}
150199

151200
/**
152201
* Multiplies by a 2D translation matrix
153202
* @param {module:webgl-2d-math.Matrix3} the matrix to be multiplied
154203
* @param {number} tx amount to translate in x
155204
* @param {number} ty amount to translate in y
205+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
156206
* @return {module:webgl-2d-math.Matrix3} the result
157207
* @memberOf module:webgl-2d-math
158208
*/
159-
function translate(m, tx, ty) {
160-
return multiply(m, translation(tx, ty));
209+
function translate(m, tx, ty, dst) {
210+
return multiply(m, translation(tx, ty), dst);
161211
}
162212

163213
/**
164214
* Creates a 2D rotation matrix
165215
* @param {number} angleInRadians amount to rotate in radians
216+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
166217
* @return {module:webgl-2d-math.Matrix3} a rotation matrix that rotates by angleInRadians
167218
* @memberOf module:webgl-2d-math
168219
*/
169-
function rotation(angleInRadians) {
220+
function rotation(angleInRadians, dst) {
170221
var c = Math.cos(angleInRadians);
171222
var s = Math.sin(angleInRadians);
172-
return [
173-
c, -s, 0,
174-
s, c, 0,
175-
0, 0, 1,
176-
];
223+
224+
dst = dst || new MatType(9);
225+
226+
dst[0] = c;
227+
dst[1] = -s;
228+
dst[2] = 0;
229+
dst[3] = s;
230+
dst[4] = c;
231+
dst[5] = 0;
232+
dst[6] = 0;
233+
dst[7] = 0;
234+
dst[8] = 1;
235+
236+
return dst;
177237
}
178238

179239
/**
180240
* Multiplies by a 2D rotation matrix
181241
* @param {module:webgl-2d-math.Matrix3} the matrix to be multiplied
182242
* @param {number} angleInRadians amount to rotate in radians
243+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
183244
* @return {module:webgl-2d-math.Matrix3} the result
184245
* @memberOf module:webgl-2d-math
185246
*/
186-
function rotate(m, angleInRadians) {
187-
return multiply(m, rotation(angleInRadians));
247+
function rotate(m, angleInRadians, dst) {
248+
return multiply(m, rotation(angleInRadians), dst);
188249
}
189250

190251
/**
191252
* Creates a 2D scaling matrix
192253
* @param {number} sx amount to scale in x
193254
* @param {number} sy amount to scale in y
255+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
194256
* @return {module:webgl-2d-math.Matrix3} a scale matrix that scales by sx and sy.
195257
* @memberOf module:webgl-2d-math
196258
*/
197-
function scaling(sx, sy) {
198-
return [
199-
sx, 0, 0,
200-
0, sy, 0,
201-
0, 0, 1,
202-
];
259+
function scaling(sx, sy, dst) {
260+
dst = dst || new MatType(9);
261+
262+
dst[0] = sx;
263+
dst[1] = 0;
264+
dst[2] = 0;
265+
dst[3] = 0;
266+
dst[4] = sy;
267+
dst[5] = 0;
268+
dst[6] = 0;
269+
dst[7] = 0;
270+
dst[8] = 1;
271+
272+
return dst;
203273
}
204274

205275
/**
206276
* Multiplies by a 2D scaling matrix
207277
* @param {module:webgl-2d-math.Matrix3} the matrix to be multiplied
208278
* @param {number} sx amount to scale in x
209279
* @param {number} sy amount to scale in y
280+
* @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result
210281
* @return {module:webgl-2d-math.Matrix3} the result
211282
* @memberOf module:webgl-2d-math
212283
*/
213-
function scale(m, sx, sy) {
214-
return multiply(m, scaling(sx, sy));
284+
function scale(m, sx, sy, dst) {
285+
return multiply(m, scaling(sx, sy), dst);
215286
}
216287

217288
function dot(x1, y1, x2, y2) {
@@ -262,20 +333,42 @@
262333
];
263334
}
264335

265-
function inverse(m) {
266-
var t00 = m[1 * 3 + 1] * m[2 * 3 + 2] - m[1 * 3 + 2] * m[2 * 3 + 1];
267-
var t10 = m[0 * 3 + 1] * m[2 * 3 + 2] - m[0 * 3 + 2] * m[2 * 3 + 1];
268-
var t20 = m[0 * 3 + 1] * m[1 * 3 + 2] - m[0 * 3 + 2] * m[1 * 3 + 1];
269-
var d = 1.0 / (m[0 * 3 + 0] * t00 - m[1 * 3 + 0] * t10 + m[2 * 3 + 0] * t20);
270-
return [
271-
d * t00, -d * t10, d * t20,
272-
-d * (m[1 * 3 + 0] * m[2 * 3 + 2] - m[1 * 3 + 2] * m[2 * 3 + 0]),
273-
d * (m[0 * 3 + 0] * m[2 * 3 + 2] - m[0 * 3 + 2] * m[2 * 3 + 0]),
274-
-d * (m[0 * 3 + 0] * m[1 * 3 + 2] - m[0 * 3 + 2] * m[1 * 3 + 0]),
275-
d * (m[1 * 3 + 0] * m[2 * 3 + 1] - m[1 * 3 + 1] * m[2 * 3 + 0]),
276-
-d * (m[0 * 3 + 0] * m[2 * 3 + 1] - m[0 * 3 + 1] * m[2 * 3 + 0]),
277-
d * (m[0 * 3 + 0] * m[1 * 3 + 1] - m[0 * 3 + 1] * m[1 * 3 + 0]),
278-
];
336+
function inverse(m, dst) {
337+
dst = dst || new MatType(9);
338+
339+
const m00 = m[0 * 4 + 0];
340+
const m01 = m[0 * 4 + 1];
341+
const m02 = m[0 * 4 + 2];
342+
const m10 = m[1 * 4 + 0];
343+
const m11 = m[1 * 4 + 1];
344+
const m12 = m[1 * 4 + 2];
345+
const m20 = m[2 * 4 + 0];
346+
const m21 = m[2 * 4 + 1];
347+
const m22 = m[2 * 4 + 2];
348+
349+
const m11_x_m22 = m11 * m22;
350+
const m21_x_m12 = m21 * m12;
351+
const m01_x_m22 = m01 * m22;
352+
const m21_x_m02 = m21 * m02;
353+
const m01_x_m12 = m01 * m12;
354+
const m11_x_m02 = m11 * m02;
355+
356+
const invDet = 1 / (
357+
m00 * (m11_x_m22 - m21_x_m12) -
358+
m10 * (m01_x_m22 - m21_x_m02) +
359+
m20 * (m01_x_m12 - m11_x_m02));
360+
361+
dst[ 0] = +(m11_x_m22 - m21_x_m12) * invDet;
362+
dst[ 1] = -(m10 * m22 - m20 * m12) * invDet;
363+
dst[ 2] = +(m10 * m21 - m20 * m11) * invDet;
364+
dst[ 3] = -(m01_x_m22 - m21_x_m02) * invDet;
365+
dst[ 4] = +(m00 * m22 - m20 * m02) * invDet;
366+
dst[ 5] = -(m00 * m21 - m20 * m01) * invDet;
367+
dst[ 6] = +(m01_x_m12 - m11_x_m02) * invDet;
368+
dst[ 7] = -(m00 * m12 - m10 * m02) * invDet;
369+
dst[ 8] = +(m00 * m11 - m10 * m01) * invDet;
370+
371+
return dst;
279372
}
280373

281374
return {

0 commit comments

Comments
 (0)