glmaths 0.0.3 → 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/mat4.ts CHANGED
@@ -11,9 +11,9 @@ import { Vec4 } from './vec4'
11
11
  */
12
12
  export class Mat4 extends Float32Array {
13
13
 
14
- static get identity() { return new Mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) }
15
- static get Identity() { return new Mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) }
16
- static get IDENTITY() { return new Mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) }
14
+ static get identity() { return mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) }
15
+ static get Identity() { return mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) }
16
+ static get IDENTITY() { return mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) }
17
17
 
18
18
  /**
19
19
  * Creates a new 4x4 matrix
@@ -66,7 +66,7 @@ export class Mat4 extends Float32Array {
66
66
  * @returns {Mat4} a new 4x4 matrix
67
67
  */
68
68
  clone() {
69
- return new Mat4(
69
+ return mat4(
70
70
  this[0], this[1], this[2], this[3],
71
71
  this[4], this[5], this[6], this[7],
72
72
  this[8], this[9], this[10], this[11],
@@ -77,10 +77,10 @@ export class Mat4 extends Float32Array {
77
77
  /**
78
78
  * Transposes a mat4
79
79
  *
80
- * @param {Mat4} out the receiving matrix, defaults to this
80
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
81
81
  * @returns {Mat4} out
82
82
  */
83
- transpose(out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
83
+ transpose(out = glmaths.ALWAYS_COPY ? mat4() : this) {
84
84
  if (out === this) {
85
85
  const a01 = this[1], a02 = this[2], a03 = this[3]
86
86
  const a12 = this[6], a13 = this[7], a23 = this[11]
@@ -108,10 +108,10 @@ export class Mat4 extends Float32Array {
108
108
  /**
109
109
  * Inverts a mat4
110
110
  *
111
- * @param {Mat4} out the receiving matrix, defaults to this
111
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
112
112
  * @returns {Mat4} out
113
113
  */
114
- invert(out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
114
+ invert(out = glmaths.ALWAYS_COPY ? mat4() : this) {
115
115
  const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3]
116
116
  const a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7]
117
117
  const a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11]
@@ -156,10 +156,10 @@ export class Mat4 extends Float32Array {
156
156
  /**
157
157
  * Calculates the adjugate of a mat4
158
158
  *
159
- * @param {Mat4} out the receiving matrix, defaults to this
159
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
160
160
  * @returns {Mat4} out
161
161
  */
162
- adjoint(out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
162
+ adjoint(out = glmaths.ALWAYS_COPY ? mat4() : this) {
163
163
  const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3]
164
164
  const a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7]
165
165
  const a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11]
@@ -221,14 +221,14 @@ export class Mat4 extends Float32Array {
221
221
  * Multiplies with another matrix, or transforms a vector
222
222
  *
223
223
  * @param {Vec2 | Vec3 | Vec4 | Mat4} b the second operand
224
- * @param {Mat4} out the receiving matrix, defaults to this
224
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
225
225
  * @returns {Mat4} out
226
226
  */
227
227
  multiply(b: Vec2): Vec2
228
228
  multiply(b: Vec3): Vec3
229
229
  multiply(b: Vec4): Vec4
230
230
  multiply(b: Mat4, out?: Mat4): Mat4
231
- multiply(b: Mat4 | Vec2 | Vec3 | Vec4, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
231
+ multiply(b: Mat4 | Vec2 | Vec3 | Vec4, out = glmaths.ALWAYS_COPY ? mat4() : this) {
232
232
  if (b instanceof Vec2)
233
233
  return b.transformMat4(this)
234
234
  if (b instanceof Vec3)
@@ -271,10 +271,10 @@ export class Mat4 extends Float32Array {
271
271
  * Translates a mat4 by the given Vec3
272
272
  *
273
273
  * @param {Vec3} v vector to translate by
274
- * @param {Mat4} out the receiving matrix, defaults to this
274
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
275
275
  * @returns {Mat4} out
276
276
  */
277
- translate(v: Vec3, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
277
+ translate(v: Vec3, out = glmaths.ALWAYS_COPY ? mat4() : this) {
278
278
  const x = v[0], y = v[1], z = v[2]
279
279
  if (out === this) {
280
280
  out[12] = this[0] * x + this[4] * y + this[8] * z + this[12]
@@ -300,10 +300,10 @@ export class Mat4 extends Float32Array {
300
300
  * Scales a mat4 by the dimensions in the given Vec3 not using vectorization
301
301
  *
302
302
  * @param {Vec3} v the Vec3 to scale the matrix by
303
- * @param {Mat4} out the receiving matrix, defaults to this
303
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
304
304
  * @returns {Mat4} out
305
305
  */
306
- scale(v: Vec3, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
306
+ scale(v: Vec3, out = glmaths.ALWAYS_COPY ? mat4() : this) {
307
307
  const x = v[0], y = v[1], z = v[2]
308
308
  out[0] = this[0] * x; out[1] = this[1] * x; out[2] = this[2] * x; out[3] = this[3] * x
309
309
  out[4] = this[4] * y; out[5] = this[5] * y; out[6] = this[6] * y; out[7] = this[7] * y
@@ -317,10 +317,10 @@ export class Mat4 extends Float32Array {
317
317
  *
318
318
  * @param {Number} rad the angle to rotate the matrix by
319
319
  * @param {Vec3} axis the axis to rotate around
320
- * @param {Mat4} out the receiving matrix, defaults to this
320
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
321
321
  * @returns {Mat4} out
322
322
  */
323
- rotate(rad: number, axis: Vec3, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
323
+ rotate(rad: number, axis: Vec3, out = glmaths.ALWAYS_COPY ? mat4() : this) {
324
324
  let x = axis[0], y = axis[1], z = axis[2]
325
325
  let len = Math.sqrt(x * x + y * y + z * z)
326
326
  if (len < glmaths.EPSILON) return null
@@ -356,10 +356,10 @@ export class Mat4 extends Float32Array {
356
356
  * Rotates a mat4 by the given angle around the X axis
357
357
  *
358
358
  * @param {Number} rad the angle to rotate the matrix by
359
- * @param {Mat4} out the receiving matrix, defaults to this
359
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
360
360
  * @returns {Mat4} out
361
361
  */
362
- rotateX(rad: number, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
362
+ rotateX(rad: number, out = glmaths.ALWAYS_COPY ? mat4() : this) {
363
363
  const s = Math.sin(rad), c = Math.cos(rad)
364
364
  const a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7]
365
365
  const a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11]
@@ -380,10 +380,10 @@ export class Mat4 extends Float32Array {
380
380
  * Rotates a mat4 by the given angle around the Y axis
381
381
  *
382
382
  * @param {Number} rad the angle to rotate the matrix by
383
- * @param {Mat4} out the receiving matrix, defaults to this
383
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
384
384
  * @returns {Mat4} out
385
385
  */
386
- rotateY(rad: number, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
386
+ rotateY(rad: number, out = glmaths.ALWAYS_COPY ? mat4() : this) {
387
387
  const s = Math.sin(rad), c = Math.cos(rad)
388
388
  const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3]
389
389
  const a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11]
@@ -404,10 +404,10 @@ export class Mat4 extends Float32Array {
404
404
  * Rotates a mat4 by the given angle around the Z axis
405
405
  *
406
406
  * @param {Number} rad the angle to rotate the matrix by
407
- * @param {Mat4} out the receiving matrix, defaults to this
407
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
408
408
  * @returns {Mat4} out
409
409
  */
410
- rotateZ(rad: number, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
410
+ rotateZ(rad: number, out = glmaths.ALWAYS_COPY ? mat4() : this) {
411
411
  const s = Math.sin(rad), c = Math.cos(rad)
412
412
  const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3]
413
413
  const a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7]
@@ -559,10 +559,10 @@ export class Mat4 extends Float32Array {
559
559
  * Creates a matrix from a vector translation
560
560
  *
561
561
  * @param {Vec3} v translation vector
562
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
562
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
563
563
  * @returns {Mat4} out
564
564
  */
565
- static fromTranslation(v: Vec3, out = new Mat4()) {
565
+ static fromTranslation(v: Vec3, out = mat4()) {
566
566
  out[0] = out[5] = out[10] = out[15] = 1
567
567
  out[1] = out[2] = out[3] = out[4] = out[6] = out[7] = out[8] = out[9] = out[11] = 0
568
568
  out[12] = v[0]
@@ -575,10 +575,10 @@ export class Mat4 extends Float32Array {
575
575
  * Creates a matrix from a vector scaling
576
576
  *
577
577
  * @param {Vec3} v scaling vector
578
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
578
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
579
579
  * @returns {Mat4} out
580
580
  */
581
- static fromScaling(v: Vec3, out = new Mat4()) {
581
+ static fromScaling(v: Vec3, out = mat4()) {
582
582
  out[0] = v[0]
583
583
  out[5] = v[1]
584
584
  out[10] = v[2]
@@ -592,10 +592,10 @@ export class Mat4 extends Float32Array {
592
592
  *
593
593
  * @param {Number} rad the angle to rotate the matrix by
594
594
  * @param {Vec3} axis the axis to rotate around
595
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
595
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
596
596
  * @returns {Mat4} out
597
597
  */
598
- static fromRotation(rad: number, axis: Vec3, out = new Mat4()) {
598
+ static fromRotation(rad: number, axis: Vec3, out = mat4()) {
599
599
  let x = axis[0], y = axis[1], z = axis[2]
600
600
  let len = Math.sqrt(x * x + y * y + z * z)
601
601
  if (len < glmaths.EPSILON) return null
@@ -616,10 +616,10 @@ export class Mat4 extends Float32Array {
616
616
  * Creates a matrix from the given angle around the X axis
617
617
  *
618
618
  * @param {Number} rad the angle to rotate the matrix by
619
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
619
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
620
620
  * @returns {Mat4} out
621
621
  */
622
- static fromXRotation(rad: number, out = new Mat4()) {
622
+ static fromXRotation(rad: number, out = mat4()) {
623
623
  const s = Math.sin(rad), c = Math.cos(rad)
624
624
  out[0] = 1
625
625
  out[1] = out[2] = out[3] = out[4] = out[7] = out[8] = out[11] = out[12] = out[13] = out[14] = 0
@@ -635,10 +635,10 @@ export class Mat4 extends Float32Array {
635
635
  * Creates a matrix from the given angle around the Y axis
636
636
  *
637
637
  * @param {Number} rad the angle to rotate the matrix by
638
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
638
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
639
639
  * @returns {Mat4} out
640
640
  */
641
- static fromYRotation(rad: number, out = new Mat4()) {
641
+ static fromYRotation(rad: number, out = mat4()) {
642
642
  const s = Math.sin(rad), c = Math.cos(rad)
643
643
  out[0] = c
644
644
  out[1] = out[3] = out[4] = out[6] = out[7] = out[9] = out[11] = out[12] = out[13] = out[14] = 0
@@ -653,10 +653,10 @@ export class Mat4 extends Float32Array {
653
653
  * Creates a matrix from the given angle around the Z axis
654
654
  *
655
655
  * @param {Number} rad the angle to rotate the matrix by
656
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
656
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
657
657
  * @returns {Mat4} out
658
658
  */
659
- static fromZRotation(rad: number, out = new Mat4()) {
659
+ static fromZRotation(rad: number, out = mat4()) {
660
660
  const s = Math.sin(rad), c = Math.cos(rad)
661
661
  out[0] = c
662
662
  out[1] = s
@@ -672,10 +672,10 @@ export class Mat4 extends Float32Array {
672
672
  *
673
673
  * @param {Quat} q rotation quaternion
674
674
  * @param {Vec3} v translation vector
675
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
675
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
676
676
  * @returns {Mat4} out
677
677
  */
678
- static fromRotationTranslation(q: Quat, v: Vec3, out = new Mat4()) {
678
+ static fromRotationTranslation(q: Quat, v: Vec3, out = mat4()) {
679
679
  const x = q[0], y = q[1], z = q[2], w = q[3]
680
680
  const x2 = x + x, y2 = y + y, z2 = z + z
681
681
  const xx = x * x2, xy = x * y2, xz = x * z2
@@ -696,10 +696,10 @@ export class Mat4 extends Float32Array {
696
696
  * @param {Quat} q rotation quaternion
697
697
  * @param {Vec3} v translation vector
698
698
  * @param {Vec3} s scaling vector
699
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
699
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
700
700
  * @returns {Mat4} out
701
701
  */
702
- static fromRotationTranslationScale(q: Quat, v: Vec3, s: Vec3, out = new Mat4()) {
702
+ static fromRotationTranslationScale(q: Quat, v: Vec3, s: Vec3, out = mat4()) {
703
703
  const x = q[0], y = q[1], z = q[2], w = q[3]
704
704
  const x2 = x + x, y2 = y + y, z2 = z + z
705
705
  const xx = x * x2, xy = x * y2, xz = x * z2
@@ -722,10 +722,10 @@ export class Mat4 extends Float32Array {
722
722
  * @param {Vec3} v translation vector
723
723
  * @param {Vec3} s scaling vector
724
724
  * @param {Vec3} o the origin vector
725
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
725
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
726
726
  * @returns {Mat4} out
727
727
  */
728
- static fromRotationTranslationScaleOrigin(q: Quat, v: Vec3, s: Vec3, o: Vec3, out = new Mat4()) {
728
+ static fromRotationTranslationScaleOrigin(q: Quat, v: Vec3, s: Vec3, o: Vec3, out = mat4()) {
729
729
  const x = q[0], y = q[1], z = q[2], w = q[3]
730
730
  const x2 = x + x, y2 = y + y, z2 = z + z
731
731
  const xx = x * x2, xy = x * y2, xz = x * z2
@@ -759,10 +759,10 @@ export class Mat4 extends Float32Array {
759
759
  * Calculates a 4x4 matrix from the given quaternion
760
760
  *
761
761
  * @param {Quat} q quaternion to create matrix from
762
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
762
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
763
763
  * @returns {Mat4} out
764
764
  */
765
- static fromQuat(q: Quat, out = new Mat4()) {
765
+ static fromQuat(q: Quat, out = mat4()) {
766
766
  const x = q[0], y = q[1], z = q[2], w = q[3]
767
767
  const x2 = x + x, y2 = y + y, z2 = z + z
768
768
  const xx = x * x2, yx = y * x2, yy = y * y2
@@ -786,10 +786,10 @@ export class Mat4 extends Float32Array {
786
786
  * @param {Number} top top bound of the frustum
787
787
  * @param {Number} near near bound of the frustum
788
788
  * @param {Number} far far bound of the frustum
789
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
789
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
790
790
  * @returns {Mat4} out
791
791
  */
792
- static frustum(left: number, right: number, bottom: number, top: number, near: number, far: number, out = new Mat4()) {
792
+ static frustum(left: number, right: number, bottom: number, top: number, near: number, far: number, out = mat4()) {
793
793
  const rl = 1 / (right - left)
794
794
  const tb = 1 / (top - bottom)
795
795
  const nf = 1 / (near - far)
@@ -811,10 +811,10 @@ export class Mat4 extends Float32Array {
811
811
  * @param {Number} aspect aspect ratio, typically viewport width / height
812
812
  * @param {Number} near near bound of the frustum
813
813
  * @param {Number | null} far far bound of the frustum, can be null or Infinity
814
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
814
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
815
815
  * @returns {Mat4} out
816
816
  */
817
- static perspectiveNO(fovy: number, aspect: number, near: number, far: number | null, out = new Mat4()) {
817
+ static perspectiveNO(fovy: number, aspect: number, near: number, far: number | null, out = mat4()) {
818
818
  const f = 1.0 / Math.tan(fovy / 2)
819
819
  const lh = glmaths.LEFT_HANDED
820
820
  out[0] = f / aspect
@@ -842,10 +842,10 @@ export class Mat4 extends Float32Array {
842
842
  * @param {Number} aspect aspect ratio, typically viewport width / height
843
843
  * @param {Number} near near bound of the frustum
844
844
  * @param {Number | null} far far bound of the frustum, can be null or Infinity
845
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
845
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
846
846
  * @returns {Mat4} out
847
847
  */
848
- static perspectiveZO(fovy: number, aspect: number, near: number, far: number | null, out = new Mat4()) {
848
+ static perspectiveZO(fovy: number, aspect: number, near: number, far: number | null, out = mat4()) {
849
849
  const f = 1.0 / Math.tan(fovy / 2)
850
850
  const lh = glmaths.LEFT_HANDED
851
851
  out[0] = f / aspect; out[1] = 0; out[2] = 0; out[3] = 0
@@ -869,10 +869,10 @@ export class Mat4 extends Float32Array {
869
869
  * @param {Object} fov object containing upDegrees, downDegrees, leftDegrees, rightDegrees
870
870
  * @param {Number} near near bound of the frustum
871
871
  * @param {Number} far far bound of the frustum
872
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
872
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
873
873
  * @returns {Mat4} out
874
874
  */
875
- static perspectiveFromFieldOfView(fov: { upDegrees: number, downDegrees: number, leftDegrees: number, rightDegrees: number }, near: number, far: number, out = new Mat4()) {
875
+ static perspectiveFromFieldOfView(fov: { upDegrees: number, downDegrees: number, leftDegrees: number, rightDegrees: number }, near: number, far: number, out = mat4()) {
876
876
  const upTan = Math.tan((fov.upDegrees * Math.PI) / 180.0)
877
877
  const downTan = Math.tan((fov.downDegrees * Math.PI) / 180.0)
878
878
  const leftTan = Math.tan((fov.leftDegrees * Math.PI) / 180.0)
@@ -904,10 +904,10 @@ export class Mat4 extends Float32Array {
904
904
  * @param {Number} top top bound of the frustum
905
905
  * @param {Number} near near bound of the frustum
906
906
  * @param {Number} far far bound of the frustum
907
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
907
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
908
908
  * @returns {Mat4} out
909
909
  */
910
- static orthoNO(left: number, right: number, bottom: number, top: number, near: number, far: number, out = new Mat4()) {
910
+ static orthoNO(left: number, right: number, bottom: number, top: number, near: number, far: number, out = mat4()) {
911
911
  const lr = 1 / (left - right)
912
912
  const bt = 1 / (bottom - top)
913
913
  const nf = 1 / (near - far)
@@ -931,10 +931,10 @@ export class Mat4 extends Float32Array {
931
931
  * @param {Number} top top bound of the frustum
932
932
  * @param {Number} near near bound of the frustum
933
933
  * @param {Number} far far bound of the frustum
934
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
934
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
935
935
  * @returns {Mat4} out
936
936
  */
937
- static orthoZO(left: number, right: number, bottom: number, top: number, near: number, far: number, out = new Mat4()) {
937
+ static orthoZO(left: number, right: number, bottom: number, top: number, near: number, far: number, out = mat4()) {
938
938
  const lr = 1 / (left - right)
939
939
  const bt = 1 / (bottom - top)
940
940
  const nf = 1 / (near - far)
@@ -953,10 +953,10 @@ export class Mat4 extends Float32Array {
953
953
  * @param {Vec3} eye position of the viewer
954
954
  * @param {Vec3} center point the viewer is looking at
955
955
  * @param {Vec3} up vector pointing up
956
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
956
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
957
957
  * @returns {Mat4} out
958
958
  */
959
- static lookAt(eye: Vec3, center: Vec3, up: Vec3, out = new Mat4()) {
959
+ static lookAt(eye: Vec3, center: Vec3, up: Vec3, out = mat4()) {
960
960
  let x0, x1, x2, y0, y1, y2, z0, z1, z2, len
961
961
 
962
962
  const eyex = eye[0], eyey = eye[1], eyez = eye[2]
@@ -1009,10 +1009,10 @@ export class Mat4 extends Float32Array {
1009
1009
  * @param {Vec3} eye position of the viewer
1010
1010
  * @param {Vec3} target point the viewer is looking at
1011
1011
  * @param {Vec3} up vector pointing up
1012
- * @param {Mat4} out the receiving matrix, defaults to new Mat4()
1012
+ * @param {Mat4} out the receiving matrix, defaults to mat4()
1013
1013
  * @returns {Mat4} out
1014
1014
  */
1015
- static targetTo(eye: Vec3, target: Vec3, up: Vec3, out = new Mat4()) {
1015
+ static targetTo(eye: Vec3, target: Vec3, up: Vec3, out = mat4()) {
1016
1016
  const eyex = eye[0], eyey = eye[1], eyez = eye[2]
1017
1017
  const upx = up[0], upy = up[1], upz = up[2]
1018
1018
 
@@ -1054,7 +1054,7 @@ export class Mat4 extends Float32Array {
1054
1054
  * @param {Mat4} out the receiving matrix, defaults to a new Mat4
1055
1055
  * @returns {Mat4} out
1056
1056
  */
1057
- static infinitePerspective(fovy: number, aspect: number, near: number, out = new Mat4()) {
1057
+ static infinitePerspective(fovy: number, aspect: number, near: number, out = mat4()) {
1058
1058
  const f = 1.0 / Math.tan(fovy / 2)
1059
1059
  const lh = glmaths.LEFT_HANDED
1060
1060
  out[0] = f / aspect
@@ -1115,7 +1115,7 @@ export class Mat4 extends Float32Array {
1115
1115
  const a20 = model[8], a21 = model[9], a22 = model[10], a23 = model[11]
1116
1116
  const a30 = model[12], a31 = model[13], a32 = model[14], a33 = model[15]
1117
1117
 
1118
- const pm = new Mat4()
1118
+ const pm = mat4()
1119
1119
  let b0 = proj[0], b1 = proj[1], b2 = proj[2], b3 = proj[3]
1120
1120
  pm[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30
1121
1121
  pm[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31
@@ -1173,10 +1173,10 @@ export class Mat4 extends Float32Array {
1173
1173
  * Adds two mat4's
1174
1174
  *
1175
1175
  * @param {Mat4} b the second operand
1176
- * @param {Mat4} out the receiving matrix, defaults to this
1176
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
1177
1177
  * @returns {Mat4} out
1178
1178
  */
1179
- plus(b: Mat4, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
1179
+ plus(b: Mat4, out = glmaths.ALWAYS_COPY ? mat4() : this) {
1180
1180
  out[0] = this[0] + b[0]; out[1] = this[1] + b[1]; out[2] = this[2] + b[2]; out[3] = this[3] + b[3]
1181
1181
  out[4] = this[4] + b[4]; out[5] = this[5] + b[5]; out[6] = this[6] + b[6]; out[7] = this[7] + b[7]
1182
1182
  out[8] = this[8] + b[8]; out[9] = this[9] + b[9]; out[10] = this[10] + b[10]; out[11] = this[11] + b[11]
@@ -1188,10 +1188,10 @@ export class Mat4 extends Float32Array {
1188
1188
  * Subtracts matrix b from a mat4
1189
1189
  *
1190
1190
  * @param {Mat4} b the second operand
1191
- * @param {Mat4} out the receiving matrix, defaults to this
1191
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
1192
1192
  * @returns {Mat4} out
1193
1193
  */
1194
- minus(b: Mat4, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
1194
+ minus(b: Mat4, out = glmaths.ALWAYS_COPY ? mat4() : this) {
1195
1195
  out[0] = this[0] - b[0]; out[1] = this[1] - b[1]; out[2] = this[2] - b[2]; out[3] = this[3] - b[3]
1196
1196
  out[4] = this[4] - b[4]; out[5] = this[5] - b[5]; out[6] = this[6] - b[6]; out[7] = this[7] - b[7]
1197
1197
  out[8] = this[8] - b[8]; out[9] = this[9] - b[9]; out[10] = this[10] - b[10]; out[11] = this[11] - b[11]
@@ -1203,10 +1203,10 @@ export class Mat4 extends Float32Array {
1203
1203
  * Multiplies each element of a mat4 by a scalar number
1204
1204
  *
1205
1205
  * @param {Number} b amount to scale the matrix's elements by
1206
- * @param {Mat4} out the receiving matrix, defaults to this
1206
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
1207
1207
  * @returns {Mat4} out
1208
1208
  */
1209
- scaleScalar(b: number, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
1209
+ scaleScalar(b: number, out = glmaths.ALWAYS_COPY ? mat4() : this) {
1210
1210
  out[0] = this[0] * b; out[1] = this[1] * b; out[2] = this[2] * b; out[3] = this[3] * b
1211
1211
  out[4] = this[4] * b; out[5] = this[5] * b; out[6] = this[6] * b; out[7] = this[7] * b
1212
1212
  out[8] = this[8] * b; out[9] = this[9] * b; out[10] = this[10] * b; out[11] = this[11] * b
@@ -1219,10 +1219,10 @@ export class Mat4 extends Float32Array {
1219
1219
  *
1220
1220
  * @param {Mat4} b the second operand
1221
1221
  * @param {Number} scale the amount to scale b's elements by before adding
1222
- * @param {Mat4} out the receiving matrix, defaults to this
1222
+ * @param {Mat4} out the receiving matrix, defaults to new mat4()
1223
1223
  * @returns {Mat4} out
1224
1224
  */
1225
- multiplyScalarAndAdd(b: Mat4, scale: number, out = glmaths.ALWAYS_COPY ? new Mat4() : this) {
1225
+ multiplyScalarAndAdd(b: Mat4, scale: number, out = glmaths.ALWAYS_COPY ? mat4() : this) {
1226
1226
  out[0] = this[0] + b[0] * scale; out[1] = this[1] + b[1] * scale
1227
1227
  out[2] = this[2] + b[2] * scale; out[3] = this[3] + b[3] * scale
1228
1228
  out[4] = this[4] + b[4] * scale; out[5] = this[5] + b[5] * scale
@@ -1304,16 +1304,15 @@ Mat4.prototype.times = Mat4.prototype.multiply
1304
1304
  Mat4.prototype.str = Mat4.prototype.toString
1305
1305
  Mat4.prototype.multiplyScalar = Mat4.prototype.scaleScalar
1306
1306
 
1307
- export const mat4 = Object.assign(
1308
- (...args: (number | Float32Array)[]): Mat4 => {
1309
- const out = new Mat4()
1310
- let i = 0
1311
- for (const a of args) {
1312
- if (typeof a === 'number') out[i++] = a
1313
- else for (const v of a) out[i++] = v
1314
- }
1315
- return out
1316
- },
1317
- Mat4
1318
- )
1307
+ const createMat4 = (...args: (number | Float32Array)[]): Mat4 => {
1308
+ const out = new Mat4()
1309
+ let i = 0
1310
+ for (const a of args) {
1311
+ if (typeof a === 'number') out[i++] = a
1312
+ else for (const v of a) out[i++] = v
1313
+ }
1314
+ return out
1315
+ }
1316
+ Object.setPrototypeOf(createMat4, Mat4)
1317
+ export const mat4 = createMat4 as typeof createMat4 & typeof Mat4
1319
1318
  export const mat4x4 = mat4
package/src/quat.ts CHANGED
@@ -10,9 +10,9 @@ import { Mat4 } from './mat4'
10
10
  */
11
11
  export class Quat extends Float32Array {
12
12
 
13
- static get identity() { return new Quat(0, 0, 0, 1) }
14
- static get Identity() { return new Quat(0, 0, 0, 1) }
15
- static get IDENTITY() { return new Quat(0, 0, 0, 1) }
13
+ static get identity() { return quat(0, 0, 0, 1) }
14
+ static get Identity() { return quat(0, 0, 0, 1) }
15
+ static get IDENTITY() { return quat(0, 0, 0, 1) }
16
16
 
17
17
  /**
18
18
  * Creates a new quaternion
@@ -34,10 +34,10 @@ export class Quat extends Float32Array {
34
34
  * Calculates the Hamilton product of two quaternions
35
35
  *
36
36
  * @param {Quat} b the second operand
37
- * @param {Quat} out the receiving quaternion, defaults to this
37
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
38
38
  * @returns {Quat} out
39
39
  */
40
- multiply(b: Quat | number, out: Quat = glmaths.ALWAYS_COPY ? new Quat() : this): Quat {
40
+ multiply(b: Quat | number, out: Quat = glmaths.ALWAYS_COPY ? quat() : this): Quat {
41
41
  if (typeof b === 'number') {
42
42
  out[0] = this[0] * b
43
43
  out[1] = this[1] * b
@@ -77,7 +77,7 @@ export class Quat extends Float32Array {
77
77
  *
78
78
  * @param {Vec3} axis the axis around which to rotate
79
79
  * @param {Number} rad the angle in radians
80
- * @param {Quat} out the receiving quaternion, defaults to this
80
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
81
81
  * @returns {Quat} out
82
82
  */
83
83
  setAxisAngle(axis: Vec3, rad: number, out = glmaths.ALWAYS_COPY ? quat() : this): Quat {
@@ -143,7 +143,7 @@ export class Quat extends Float32Array {
143
143
  * Rotates a quaternion by the given angle about the X axis
144
144
  *
145
145
  * @param {Number} rad angle in radians to rotate
146
- * @param {Quat} out the receiving quaternion, defaults to this
146
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
147
147
  * @returns {Quat} out
148
148
  */
149
149
  rotateX(rad: number, out = glmaths.ALWAYS_COPY ? quat() : this) {
@@ -161,7 +161,7 @@ export class Quat extends Float32Array {
161
161
  * Rotates a quaternion by the given angle about the Y axis
162
162
  *
163
163
  * @param {Number} rad angle in radians to rotate
164
- * @param {Quat} out the receiving quaternion, defaults to this
164
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
165
165
  * @returns {Quat} out
166
166
  */
167
167
  rotateY(rad: number, out = glmaths.ALWAYS_COPY ? quat() : this) {
@@ -178,7 +178,7 @@ export class Quat extends Float32Array {
178
178
  * Rotates a quaternion by the given angle about the Z axis
179
179
  *
180
180
  * @param {Number} rad angle in radians to rotate
181
- * @param {Quat} out the receiving quaternion, defaults to this
181
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
182
182
  * @returns {Quat} out
183
183
  */
184
184
  rotateZ(rad: number, out = glmaths.ALWAYS_COPY ? quat() : this) {
@@ -223,7 +223,7 @@ export class Quat extends Float32Array {
223
223
  /**
224
224
  * Calculates the exponential of the unit quaternion
225
225
  *
226
- * @param {Quat} out the receiving quaternion, defaults to this
226
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
227
227
  * @returns {Quat} out
228
228
  */
229
229
  exp(out = glmaths.ALWAYS_COPY ? quat() : this): Quat {
@@ -258,7 +258,7 @@ export class Quat extends Float32Array {
258
258
  /**
259
259
  * Calculates the natural logarithm of the unit quaternion
260
260
  *
261
- * @param {Quat} out the receiving quaternion, defaults to this
261
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
262
262
  * @returns {Quat} out
263
263
  */
264
264
  ln(out = glmaths.ALWAYS_COPY ? quat() : this): Quat {
@@ -334,7 +334,7 @@ export class Quat extends Float32Array {
334
334
  *
335
335
  * @param {Quat} b the second operand
336
336
  * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
337
- * @param {Quat} out the receiving quaternion, defaults to this
337
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
338
338
  * @returns {Quat} out
339
339
  */
340
340
  slerp(b: Quat, t: number, out = glmaths.ALWAYS_COPY ? quat() : this): Quat {
@@ -415,7 +415,7 @@ export class Quat extends Float32Array {
415
415
  /**
416
416
  * Calculates the inverse of a quaternion
417
417
  *
418
- * @param {Quat} out the receiving quaternion, defaults to this
418
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
419
419
  * @returns {Quat} out
420
420
  */
421
421
  invert(out = glmaths.ALWAYS_COPY ? quat() : this) {
@@ -447,7 +447,7 @@ export class Quat extends Float32Array {
447
447
  /**
448
448
  * Calculates the conjugate of a quaternion
449
449
  *
450
- * @param {Quat} out the receiving quaternion, defaults to this
450
+ * @param {Quat} out the receiving quaternion, defaults to new quat()
451
451
  * @returns {Quat} out
452
452
  */
453
453
  conjugate(out = glmaths.ALWAYS_COPY ? quat() : this): Quat {
@@ -675,15 +675,16 @@ export class Quat extends Float32Array {
675
675
 
676
676
  return Quat.fromMat3(Quat.tmpMat3, out).normalize()
677
677
  }
678
-
678
+
679
679
  /**
680
680
  * Normalizes a quaternion
681
681
  *
682
- * @param {Quat} out the receiving vector, defaults to this
682
+ * @param {Quat} q the quaternion to normalize
683
+ * @param {Quat} out the receiving vector, defaults to new quat()
683
684
  * @returns {Quat} out
684
685
  */
685
- normalize(out = glmaths.ALWAYS_COPY ? new Quat() : this): Quat {
686
- const x = this[0], y = this[1], z = this[2], w = this[3]
686
+ static normalize(q: Quat, out = quat()): Quat {
687
+ const x = q[0], y = q[1], z = q[2], w = q[3]
687
688
  let len = x * x + y * y + z * z + w * w
688
689
  if (len > 0) {
689
690
  len = 1.0 / Math.sqrt(len)
@@ -694,6 +695,16 @@ export class Quat extends Float32Array {
694
695
  out[3] = w * len
695
696
  return out
696
697
  }
698
+
699
+ /**
700
+ * Normalizes a quaternion
701
+ *
702
+ * @param {Quat} out the receiving vector, defaults to new quat()
703
+ * @returns {Quat} out
704
+ */
705
+ normalize(out = glmaths.ALWAYS_COPY ? quat() : this): Quat {
706
+ return Quat.normalize(this, out)
707
+ }
697
708
 
698
709
  /**
699
710
  * Creates a quaternion that looks along the given direction vector
@@ -703,7 +714,7 @@ export class Quat extends Float32Array {
703
714
  * @param {Quat} out the receiving quaternion, defaults to quat()
704
715
  * @returns {Quat} out
705
716
  */
706
- static quatLookAt(direction: Vec3, up: Vec3, out = new Quat()): Quat {
717
+ static quatLookAt(direction: Vec3, up: Vec3, out = quat()): Quat {
707
718
  const f = vec3(direction[0], direction[1], direction[2]).normalize()
708
719
  const s = Vec3.cross(f, up).normalize()
709
720
  const u = Vec3.cross(s, f)
@@ -803,6 +814,7 @@ export interface Quat {
803
814
  scale: (b: Quat | number, out?: Quat) => Quat
804
815
  times: (b: Quat | number, out?: Quat) => Quat
805
816
  str: () => string
817
+ normalized: (out?: Quat) => Quat
806
818
  }
807
819
 
808
820
  // @aliases
@@ -812,8 +824,9 @@ Quat.prototype.mul = Quat.prototype.multiply
812
824
  Quat.prototype.scale = Quat.prototype.multiply
813
825
  Quat.prototype.times = Quat.prototype.multiply
814
826
  Quat.prototype.str = Quat.prototype.toString
827
+ Quat.prototype.normalized = Quat.prototype.normalize
815
828
 
816
- export const quat = Object.assign(
817
- (x = 0, y = 0, z = 0, w = 1) => new Quat(x, y, z, w),
818
- Quat
819
- )
829
+ const createQuat =
830
+ (x = 0, y = 0, z = 0, w = 1) => new Quat(x, y, z, w)
831
+ Object.setPrototypeOf(createQuat, Quat)
832
+ export const quat = createQuat as typeof createQuat & typeof Quat