glmaths 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/mat3.ts CHANGED
@@ -12,9 +12,9 @@ import { Mat4 } from './mat4'
12
12
  */
13
13
  export class Mat3 extends Float32Array {
14
14
 
15
- static get identity() { return new Mat3(1, 0, 0, 0, 1, 0, 0, 0, 1) }
16
- static get Identity() { return new Mat3(1, 0, 0, 0, 1, 0, 0, 0, 1) }
17
- static get IDENTITY() { return new Mat3(1, 0, 0, 0, 1, 0, 0, 0, 1) }
15
+ static get identity() { return mat3(1, 0, 0, 0, 1, 0, 0, 0, 1) }
16
+ static get Identity() { return mat3(1, 0, 0, 0, 1, 0, 0, 0, 1) }
17
+ static get IDENTITY() { return mat3(1, 0, 0, 0, 1, 0, 0, 0, 1) }
18
18
 
19
19
  /**
20
20
  * Create a new mat3 with the given values
@@ -48,16 +48,16 @@ export class Mat3 extends Float32Array {
48
48
  * @returns {Mat3} a new Mat3
49
49
  */
50
50
  clone() {
51
- return new Mat3(this[0], this[1], this[2], this[3], this[4], this[5], this[6], this[7], this[8])
51
+ return mat3(this[0], this[1], this[2], this[3], this[4], this[5], this[6], this[7], this[8])
52
52
  }
53
53
 
54
54
  /**
55
55
  * Transposes a mat3
56
56
  *
57
- * @param {Mat3} out the receiving matrix, defaults to this
57
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
58
58
  * @returns {Mat3} out
59
59
  */
60
- transpose(out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
60
+ transpose(out = glmaths.ALWAYS_COPY ? mat3() : this) {
61
61
  if (out === this) {
62
62
  const a01 = this[1], a02 = this[2], a12 = this[5]
63
63
  out[1] = this[3]
@@ -83,10 +83,10 @@ export class Mat3 extends Float32Array {
83
83
  /**
84
84
  * Inverts a mat3
85
85
  *
86
- * @param {Mat3} out the receiving matrix, defaults to this
86
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
87
87
  * @returns {Mat3|null} out, or null if not invertible
88
88
  */
89
- invert(out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
89
+ invert(out = glmaths.ALWAYS_COPY ? mat3() : this) {
90
90
  const a00 = this[0], a01 = this[1], a02 = this[2]
91
91
  const a10 = this[3], a11 = this[4], a12 = this[5]
92
92
  const a20 = this[6], a21 = this[7], a22 = this[8]
@@ -114,10 +114,10 @@ export class Mat3 extends Float32Array {
114
114
  /**
115
115
  * Calculates the adjugate of a mat3
116
116
  *
117
- * @param {Mat3} out the receiving matrix, defaults to this
117
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
118
118
  * @returns {Mat3} out
119
119
  */
120
- adjoint(out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
120
+ adjoint(out = glmaths.ALWAYS_COPY ? mat3() : this) {
121
121
  const a00 = this[0], a01 = this[1], a02 = this[2]
122
122
  const a10 = this[3], a11 = this[4], a12 = this[5]
123
123
  const a20 = this[6], a21 = this[7], a22 = this[8]
@@ -150,13 +150,13 @@ export class Mat3 extends Float32Array {
150
150
  * Multiplies two mat3's
151
151
  *
152
152
  * @param {Mat3} b the second operand
153
- * @param {Mat3} out the receiving matrix, defaults to this
153
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
154
154
  * @returns {Mat3} out
155
155
  */
156
156
  multiply(b: Vec2): Vec2
157
157
  multiply(b: Vec3): Vec3
158
158
  multiply(b: Mat3, out?: Mat3): Mat3
159
- multiply(b: Mat3 | Vec3 | Vec2, out: Mat3 = glmaths.ALWAYS_COPY ? new Mat3() : this) {
159
+ multiply(b: Mat3 | Vec3 | Vec2, out: Mat3 = glmaths.ALWAYS_COPY ? mat3() : this) {
160
160
  if (b instanceof Vec2)
161
161
  return b.transformMat3(this)
162
162
  if (b instanceof Vec3)
@@ -186,10 +186,10 @@ export class Mat3 extends Float32Array {
186
186
  * Translates a mat3 by the given vector
187
187
  *
188
188
  * @param {Vec2} v vector to translate by
189
- * @param {Mat3} out the receiving matrix, defaults to this
189
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
190
190
  * @returns {Mat3} out
191
191
  */
192
- translate(v: Vec2, out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
192
+ translate(v: Vec2, out = glmaths.ALWAYS_COPY ? mat3() : this) {
193
193
  const a00 = this[0], a01 = this[1], a02 = this[2]
194
194
  const a10 = this[3], a11 = this[4], a12 = this[5]
195
195
  const a20 = this[6], a21 = this[7], a22 = this[8]
@@ -211,10 +211,10 @@ export class Mat3 extends Float32Array {
211
211
  * Rotates a mat3 by the given angle
212
212
  *
213
213
  * @param {Number} rad the angle to rotate the matrix by
214
- * @param {Mat3} out the receiving matrix, defaults to this
214
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
215
215
  * @returns {Mat3} out
216
216
  */
217
- rotate(rad: number, out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
217
+ rotate(rad: number, out = glmaths.ALWAYS_COPY ? mat3() : this) {
218
218
  const a00 = this[0], a01 = this[1], a02 = this[2]
219
219
  const a10 = this[3], a11 = this[4], a12 = this[5]
220
220
  const a20 = this[6], a21 = this[7], a22 = this[8]
@@ -236,10 +236,10 @@ export class Mat3 extends Float32Array {
236
236
  * Scales a mat3 by the given vector
237
237
  *
238
238
  * @param {Vec2} v the vector to scale by
239
- * @param {Mat3} out the receiving matrix, defaults to this
239
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
240
240
  * @returns {Mat3} out
241
241
  */
242
- scale(v: Vec2, out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
242
+ scale(v: Vec2, out = glmaths.ALWAYS_COPY ? mat3() : this) {
243
243
  const x = v[0], y = v[1]
244
244
  out[0] = x * this[0]
245
245
  out[1] = x * this[1]
@@ -257,10 +257,10 @@ export class Mat3 extends Float32Array {
257
257
  * Creates a matrix from a translation vector
258
258
  *
259
259
  * @param {Vec2} v translation vector
260
- * @param {Mat3} out the receiving matrix, defaults to new Mat3()
260
+ * @param {Mat3} out the receiving matrix, defaults to mat3()
261
261
  * @returns {Mat3} out
262
262
  */
263
- static fromTranslation(v: Vec2, out = new Mat3()) {
263
+ static fromTranslation(v: Vec2, out = mat3()) {
264
264
  out[0] = 1
265
265
  out[1] = 0
266
266
  out[2] = 0
@@ -277,10 +277,10 @@ export class Mat3 extends Float32Array {
277
277
  * Creates a matrix from a given angle
278
278
  *
279
279
  * @param {Number} rad the angle to rotate the matrix by
280
- * @param {Mat3} out the receiving matrix, defaults to new Mat3()
280
+ * @param {Mat3} out the receiving matrix, defaults to mat3()
281
281
  * @returns {Mat3} out
282
282
  */
283
- static fromRotation(rad: number, out = new Mat3()) {
283
+ static fromRotation(rad: number, out = mat3()) {
284
284
  const s = Math.sin(rad), c = Math.cos(rad)
285
285
  out[0] = c
286
286
  out[1] = s
@@ -298,10 +298,10 @@ export class Mat3 extends Float32Array {
298
298
  * Creates a matrix from a scaling vector
299
299
  *
300
300
  * @param {Vec2} v scaling vector
301
- * @param {Mat3} out the receiving matrix, defaults to new Mat3()
301
+ * @param {Mat3} out the receiving matrix, defaults to mat3()
302
302
  * @returns {Mat3} out
303
303
  */
304
- static fromScaling(v: Vec2, out = new Mat3()) {
304
+ static fromScaling(v: Vec2, out = mat3()) {
305
305
  out[0] = v[0]
306
306
  out[1] = 0
307
307
  out[2] = 0
@@ -318,10 +318,10 @@ export class Mat3 extends Float32Array {
318
318
  * Creates a mat3 from a Mat2x3
319
319
  *
320
320
  * @param {Mat2x3} a the Mat2x3 to convert
321
- * @param {Mat3} out the receiving matrix, defaults to new Mat3()
321
+ * @param {Mat3} out the receiving matrix, defaults to mat3()
322
322
  * @returns {Mat3} out
323
323
  */
324
- static fromMat2x3(a: Mat2x3, out = new Mat3()) {
324
+ static fromMat2x3(a: Mat2x3, out = mat3()) {
325
325
  out[0] = a[0]
326
326
  out[1] = a[1]
327
327
  out[2] = 0
@@ -339,10 +339,10 @@ export class Mat3 extends Float32Array {
339
339
  * Calculates a mat3 from the given quaternion
340
340
  *
341
341
  * @param {Quat} q quaternion to create matrix from
342
- * @param {Mat3} out the receiving matrix, defaults to new Mat3()
342
+ * @param {Mat3} out the receiving matrix, defaults to mat3()
343
343
  * @returns {Mat3} out
344
344
  */
345
- static fromQuat(q: Quat, out = new Mat3()) {
345
+ static fromQuat(q: Quat, out = mat3()) {
346
346
  const x = q[0], y = q[1], z = q[2], w = q[3]
347
347
  const x2 = x + x, y2 = y + y, z2 = z + z
348
348
  const xx = x * x2, yx = y * x2, yy = y * y2
@@ -367,10 +367,10 @@ export class Mat3 extends Float32Array {
367
367
  * Calculates a mat3 normal matrix (transpose inverse) from a mat4
368
368
  *
369
369
  * @param {Mat4} a the source mat4 to derive the normal matrix from
370
- * @param {Mat3} out the receiving matrix, defaults to new Mat3()
370
+ * @param {Mat3} out the receiving matrix, defaults to mat3()
371
371
  * @returns {Mat3|null} out, or null if not invertible
372
372
  */
373
- static normalFromMat4(a: Mat4, out = new Mat3()) {
373
+ static normalFromMat4(a: Mat4, out = mat3()) {
374
374
  const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]
375
375
  const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]
376
376
  const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]
@@ -409,10 +409,10 @@ export class Mat3 extends Float32Array {
409
409
  * Copies the upper-left 3x3 values of a mat4 into a mat3
410
410
  *
411
411
  * @param {Mat4} a the source mat4
412
- * @param {Mat3} out the receiving matrix, defaults to new Mat3()
412
+ * @param {Mat3} out the receiving matrix, defaults to mat3()
413
413
  * @returns {Mat3} out
414
414
  */
415
- static fromMat4(a: Mat4, out = new Mat3()) {
415
+ static fromMat4(a: Mat4, out = mat3()) {
416
416
  out[0] = a[0]
417
417
  out[1] = a[1]
418
418
  out[2] = a[2]
@@ -431,10 +431,10 @@ export class Mat3 extends Float32Array {
431
431
  *
432
432
  * @param {Number} width width of the projection
433
433
  * @param {Number} height height of the projection
434
- * @param {Mat3} out the receiving matrix, defaults to new Mat3()
434
+ * @param {Mat3} out the receiving matrix, defaults to mat3()
435
435
  * @returns {Mat3} out
436
436
  */
437
- static projection(width: number, height: number, out = new Mat3()) {
437
+ static projection(width: number, height: number, out = mat3()) {
438
438
  out[0] = 2 / width
439
439
  out[1] = 0
440
440
  out[2] = 0
@@ -464,10 +464,10 @@ export class Mat3 extends Float32Array {
464
464
  * Adds two mat3's
465
465
  *
466
466
  * @param {Mat3} b the second operand
467
- * @param {Mat3} out the receiving matrix, defaults to this
467
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
468
468
  * @returns {Mat3} out
469
469
  */
470
- plus(b: Mat3, out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
470
+ plus(b: Mat3, out = glmaths.ALWAYS_COPY ? mat3() : this) {
471
471
  out[0] = this[0] + b[0]
472
472
  out[1] = this[1] + b[1]
473
473
  out[2] = this[2] + b[2]
@@ -484,10 +484,10 @@ export class Mat3 extends Float32Array {
484
484
  * Subtracts matrix b from this
485
485
  *
486
486
  * @param {Mat3} b the second operand
487
- * @param {Mat3} out the receiving matrix, defaults to this
487
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
488
488
  * @returns {Mat3} out
489
489
  */
490
- minus(b: Mat3, out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
490
+ minus(b: Mat3, out = glmaths.ALWAYS_COPY ? mat3() : this) {
491
491
  out[0] = this[0] - b[0]
492
492
  out[1] = this[1] - b[1]
493
493
  out[2] = this[2] - b[2]
@@ -504,10 +504,10 @@ export class Mat3 extends Float32Array {
504
504
  * Multiplies each element of a mat3 by a scalar number
505
505
  *
506
506
  * @param {Number} b amount to scale the matrix's elements by
507
- * @param {Mat3} out the receiving matrix, defaults to this
507
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
508
508
  * @returns {Mat3} out
509
509
  */
510
- scaleScalar(b: number, out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
510
+ scaleScalar(b: number, out = glmaths.ALWAYS_COPY ? mat3() : this) {
511
511
  out[0] = this[0] * b
512
512
  out[1] = this[1] * b
513
513
  out[2] = this[2] * b
@@ -525,10 +525,10 @@ export class Mat3 extends Float32Array {
525
525
  *
526
526
  * @param {Mat3} b the second operand
527
527
  * @param {Number} scale the amount to scale b's elements by before adding
528
- * @param {Mat3} out the receiving matrix, defaults to this
528
+ * @param {Mat3} out the receiving matrix, defaults to new mat3()
529
529
  * @returns {Mat3} out
530
530
  */
531
- multiplyScalarAndAdd(b: Mat3, scale: number, out = glmaths.ALWAYS_COPY ? new Mat3() : this) {
531
+ multiplyScalarAndAdd(b: Mat3, scale: number, out = glmaths.ALWAYS_COPY ? mat3() : this) {
532
532
  out[0] = this[0] + b[0] * scale
533
533
  out[1] = this[1] + b[1] * scale
534
534
  out[2] = this[2] + b[2] * scale
@@ -614,16 +614,15 @@ Mat3.prototype.times = Mat3.prototype.multiply
614
614
  Mat3.prototype.str = Mat3.prototype.toString
615
615
  Mat3.prototype.multiplyScalar = Mat3.prototype.scaleScalar
616
616
 
617
- export const mat3 = Object.assign(
618
- (...args: (number | Float32Array)[]): Mat3 => {
619
- const out = new Mat3()
620
- let i = 0
621
- for (const a of args) {
622
- if (typeof a === 'number') out[i++] = a
623
- else for (const v of a) out[i++] = v
624
- }
625
- return out
626
- },
627
- Mat3
628
- )
617
+ const createMat3 = (...args: (number | Float32Array)[]): Mat3 => {
618
+ const out = new Mat3()
619
+ let i = 0
620
+ for (const a of args) {
621
+ if (typeof a === 'number') out[i++] = a
622
+ else for (const v of a) out[i++] = v
623
+ }
624
+ return out
625
+ }
626
+ Object.setPrototypeOf(createMat3, Mat3)
627
+ export const mat3 = createMat3 as typeof createMat3 & typeof Mat3
629
628
  export const mat3x3 = mat3