|
1 | 1 | /* |
2 | | - * Copyright 2021 GFXFundamentals. |
| 2 | + * Copyright 2021, GFXFundamentals. |
3 | 3 | * All rights reserved. |
4 | 4 | * |
5 | 5 | * Redistribution and use in source and binary forms, with or without |
|
46 | 46 | "use strict"; |
47 | 47 |
|
48 | 48 | /** |
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. |
50 | 56 | * @typedef {number[]|TypedArray} Matrix3 |
51 | 57 | * @memberOf module:webgl-2d-math |
52 | 58 | */ |
53 | 59 |
|
| 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 | + |
54 | 74 | /** |
55 | 75 | * Takes two Matrix3s, a and b, and computes the product in the order |
56 | 76 | * that pre-composes b with a. In other words, the matrix returned will |
57 | 77 | * @param {module:webgl-2d-math.Matrix3} a A matrix. |
58 | 78 | * @param {module:webgl-2d-math.Matrix3} b A matrix. |
| 79 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
59 | 80 | * @return {module:webgl-2d-math.Matrix3} the result. |
60 | 81 | * @memberOf module:webgl-2d-math |
61 | 82 | */ |
62 | | - function multiply(a, b) { |
| 83 | + function multiply(a, b, dst) { |
| 84 | + dst = dst || new MatType(9); |
63 | 85 | var a00 = a[0 * 3 + 0]; |
64 | 86 | var a01 = a[0 * 3 + 1]; |
65 | 87 | var a02 = a[0 * 3 + 2]; |
|
79 | 101 | var b21 = b[2 * 3 + 1]; |
80 | 102 | var b22 = b[2 * 3 + 2]; |
81 | 103 |
|
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; |
93 | 115 | } |
94 | 116 |
|
95 | 117 |
|
96 | 118 | /** |
97 | 119 | * Creates a 3x3 identity matrix |
| 120 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
98 | 121 | * @return {module:webgl2-2d-math.Matrix3} an identity matrix |
99 | 122 | */ |
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; |
106 | 136 | } |
107 | 137 |
|
108 | 138 | /** |
109 | 139 | * Creates a 2D projection matrix |
110 | 140 | * @param {number} width width in pixels |
111 | 141 | * @param {number} height height in pixels |
| 142 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
112 | 143 | * @return {module:webgl-2d-math.Matrix3} a projection matrix that converts from pixels to clipspace with Y = 0 at the top. |
113 | 144 | * @memberOf module:webgl-2d-math |
114 | 145 | */ |
115 | | - function projection(width, height) { |
| 146 | + function projection(width, height, dst) { |
| 147 | + dst = dst || new MatType(9); |
116 | 148 | // 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; |
122 | 161 | } |
123 | 162 |
|
124 | 163 | /** |
125 | 164 | * Multiplies by a 2D projection matrix |
126 | 165 | * @param {module:webgl-2d-math.Matrix3} the matrix to be multiplied |
127 | 166 | * @param {number} width width in pixels |
128 | 167 | * @param {number} height height in pixels |
| 168 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
129 | 169 | * @return {module:webgl-2d-math.Matrix3} the result |
130 | 170 | * @memberOf module:webgl-2d-math |
131 | 171 | */ |
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); |
134 | 174 | } |
135 | 175 |
|
136 | 176 | /** |
137 | 177 | * Creates a 2D translation matrix |
138 | 178 | * @param {number} tx amount to translate in x |
139 | 179 | * @param {number} ty amount to translate in y |
| 180 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
140 | 181 | * @return {module:webgl-2d-math.Matrix3} a translation matrix that translates by tx and ty. |
141 | 182 | * @memberOf module:webgl-2d-math |
142 | 183 | */ |
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; |
149 | 198 | } |
150 | 199 |
|
151 | 200 | /** |
152 | 201 | * Multiplies by a 2D translation matrix |
153 | 202 | * @param {module:webgl-2d-math.Matrix3} the matrix to be multiplied |
154 | 203 | * @param {number} tx amount to translate in x |
155 | 204 | * @param {number} ty amount to translate in y |
| 205 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
156 | 206 | * @return {module:webgl-2d-math.Matrix3} the result |
157 | 207 | * @memberOf module:webgl-2d-math |
158 | 208 | */ |
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); |
161 | 211 | } |
162 | 212 |
|
163 | 213 | /** |
164 | 214 | * Creates a 2D rotation matrix |
165 | 215 | * @param {number} angleInRadians amount to rotate in radians |
| 216 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
166 | 217 | * @return {module:webgl-2d-math.Matrix3} a rotation matrix that rotates by angleInRadians |
167 | 218 | * @memberOf module:webgl-2d-math |
168 | 219 | */ |
169 | | - function rotation(angleInRadians) { |
| 220 | + function rotation(angleInRadians, dst) { |
170 | 221 | var c = Math.cos(angleInRadians); |
171 | 222 | 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; |
177 | 237 | } |
178 | 238 |
|
179 | 239 | /** |
180 | 240 | * Multiplies by a 2D rotation matrix |
181 | 241 | * @param {module:webgl-2d-math.Matrix3} the matrix to be multiplied |
182 | 242 | * @param {number} angleInRadians amount to rotate in radians |
| 243 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
183 | 244 | * @return {module:webgl-2d-math.Matrix3} the result |
184 | 245 | * @memberOf module:webgl-2d-math |
185 | 246 | */ |
186 | | - function rotate(m, angleInRadians) { |
187 | | - return multiply(m, rotation(angleInRadians)); |
| 247 | + function rotate(m, angleInRadians, dst) { |
| 248 | + return multiply(m, rotation(angleInRadians), dst); |
188 | 249 | } |
189 | 250 |
|
190 | 251 | /** |
191 | 252 | * Creates a 2D scaling matrix |
192 | 253 | * @param {number} sx amount to scale in x |
193 | 254 | * @param {number} sy amount to scale in y |
| 255 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
194 | 256 | * @return {module:webgl-2d-math.Matrix3} a scale matrix that scales by sx and sy. |
195 | 257 | * @memberOf module:webgl-2d-math |
196 | 258 | */ |
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; |
203 | 273 | } |
204 | 274 |
|
205 | 275 | /** |
206 | 276 | * Multiplies by a 2D scaling matrix |
207 | 277 | * @param {module:webgl-2d-math.Matrix3} the matrix to be multiplied |
208 | 278 | * @param {number} sx amount to scale in x |
209 | 279 | * @param {number} sy amount to scale in y |
| 280 | + * @param {module:webgl-2d-math.Matrix4} [dst] optional matrix to store result |
210 | 281 | * @return {module:webgl-2d-math.Matrix3} the result |
211 | 282 | * @memberOf module:webgl-2d-math |
212 | 283 | */ |
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); |
215 | 286 | } |
216 | 287 |
|
217 | 288 | function dot(x1, y1, x2, y2) { |
|
262 | 333 | ]; |
263 | 334 | } |
264 | 335 |
|
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; |
279 | 372 | } |
280 | 373 |
|
281 | 374 | return { |
|
0 commit comments