@math.gl/core 4.0.0 → 4.1.0-alpha.1

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.
Files changed (147) hide show
  1. package/dist/classes/base/math-array.d.ts +1 -2
  2. package/dist/classes/base/math-array.js +223 -266
  3. package/dist/classes/base/matrix.d.ts +1 -2
  4. package/dist/classes/base/matrix.js +56 -53
  5. package/dist/classes/base/vector.d.ts +1 -2
  6. package/dist/classes/base/vector.js +125 -135
  7. package/dist/classes/euler.d.ts +2 -3
  8. package/dist/classes/euler.js +464 -533
  9. package/dist/classes/matrix3.d.ts +9 -4
  10. package/dist/classes/matrix3.js +191 -190
  11. package/dist/classes/matrix4.d.ts +8 -4
  12. package/dist/classes/matrix4.js +521 -452
  13. package/dist/classes/pose.d.ts +4 -5
  14. package/dist/classes/pose.js +106 -119
  15. package/dist/classes/quaternion.d.ts +1 -2
  16. package/dist/classes/quaternion.js +261 -233
  17. package/dist/classes/spherical-coordinates.d.ts +3 -4
  18. package/dist/classes/spherical-coordinates.js +142 -147
  19. package/dist/classes/vector2.d.ts +6 -5
  20. package/dist/classes/vector2.js +103 -85
  21. package/dist/classes/vector3.d.ts +6 -5
  22. package/dist/classes/vector3.js +129 -136
  23. package/dist/classes/vector4.d.ts +7 -6
  24. package/dist/classes/vector4.js +108 -114
  25. package/dist/gl-matrix/common.d.ts +0 -1
  26. package/dist/gl-matrix/common.js +38 -6
  27. package/dist/gl-matrix/mat3.d.ts +0 -1
  28. package/dist/gl-matrix/mat3.js +699 -425
  29. package/dist/gl-matrix/mat4.d.ts +0 -1
  30. package/dist/gl-matrix/mat4.js +1879 -1339
  31. package/dist/gl-matrix/quat.d.ts +0 -1
  32. package/dist/gl-matrix/quat.js +655 -257
  33. package/dist/gl-matrix/vec2.d.ts +0 -1
  34. package/dist/gl-matrix/vec2.js +529 -212
  35. package/dist/gl-matrix/vec3.d.ts +0 -1
  36. package/dist/gl-matrix/vec3.js +703 -335
  37. package/dist/gl-matrix/vec4.d.ts +0 -1
  38. package/dist/gl-matrix/vec4.js +579 -293
  39. package/dist/gl-matrix/wip/index.d.ts +3 -4
  40. package/dist/gl-matrix/wip/index.js +5 -1
  41. package/dist/gl-matrix/wip/mat2.d.ts +1 -2
  42. package/dist/gl-matrix/wip/mat2.js +336 -157
  43. package/dist/gl-matrix/wip/mat2d.d.ts +0 -1
  44. package/dist/gl-matrix/wip/mat2d.js +380 -202
  45. package/dist/gl-matrix/wip/quat2.d.ts +0 -1
  46. package/dist/gl-matrix/wip/quat2.js +650 -409
  47. package/dist/index.cjs +60 -100
  48. package/dist/index.cjs.map +7 -0
  49. package/dist/index.d.ts +27 -23
  50. package/dist/index.js +25 -16
  51. package/dist/lib/assert.d.ts +0 -1
  52. package/dist/lib/assert.js +6 -4
  53. package/dist/lib/common.d.ts +2 -3
  54. package/dist/lib/common.js +165 -134
  55. package/dist/lib/gl-matrix-extras.d.ts +0 -1
  56. package/dist/lib/gl-matrix-extras.js +42 -36
  57. package/dist/lib/math-utils.d.ts +0 -1
  58. package/dist/lib/math-utils.js +5 -1
  59. package/dist/lib/validators.d.ts +0 -1
  60. package/dist/lib/validators.js +25 -25
  61. package/package.json +5 -6
  62. package/src/classes/base/math-array.ts +4 -1
  63. package/src/classes/base/matrix.ts +4 -1
  64. package/src/classes/base/vector.ts +4 -1
  65. package/src/classes/euler.ts +4 -1
  66. package/src/classes/matrix3.ts +12 -3
  67. package/src/classes/matrix4.ts +11 -3
  68. package/src/classes/pose.ts +4 -1
  69. package/src/classes/quaternion.ts +4 -1
  70. package/src/classes/spherical-coordinates.ts +4 -1
  71. package/src/classes/vector2.ts +11 -4
  72. package/src/classes/vector3.ts +10 -4
  73. package/src/classes/vector4.ts +9 -4
  74. package/src/gl-matrix/wip/index.js +4 -0
  75. package/src/gl-matrix/wip/mat2.js +4 -0
  76. package/src/gl-matrix/wip/mat2d.js +4 -0
  77. package/src/gl-matrix/wip/quat2.ts +4 -0
  78. package/src/index.ts +10 -2
  79. package/src/lib/assert.ts +4 -0
  80. package/src/lib/common.ts +3 -1
  81. package/src/lib/gl-matrix-extras.ts +4 -0
  82. package/src/lib/gl-matrix.d.ts +4 -0
  83. package/src/lib/math-utils.ts +4 -0
  84. package/src/lib/validators.ts +4 -19
  85. package/dist/classes/base/math-array.d.ts.map +0 -1
  86. package/dist/classes/base/math-array.js.map +0 -1
  87. package/dist/classes/base/matrix.d.ts.map +0 -1
  88. package/dist/classes/base/matrix.js.map +0 -1
  89. package/dist/classes/base/vector.d.ts.map +0 -1
  90. package/dist/classes/base/vector.js.map +0 -1
  91. package/dist/classes/euler.d.ts.map +0 -1
  92. package/dist/classes/euler.js.map +0 -1
  93. package/dist/classes/matrix3.d.ts.map +0 -1
  94. package/dist/classes/matrix3.js.map +0 -1
  95. package/dist/classes/matrix4.d.ts.map +0 -1
  96. package/dist/classes/matrix4.js.map +0 -1
  97. package/dist/classes/pose.d.ts.map +0 -1
  98. package/dist/classes/pose.js.map +0 -1
  99. package/dist/classes/quaternion.d.ts.map +0 -1
  100. package/dist/classes/quaternion.js.map +0 -1
  101. package/dist/classes/spherical-coordinates.d.ts.map +0 -1
  102. package/dist/classes/spherical-coordinates.js.map +0 -1
  103. package/dist/classes/vector2.d.ts.map +0 -1
  104. package/dist/classes/vector2.js.map +0 -1
  105. package/dist/classes/vector3.d.ts.map +0 -1
  106. package/dist/classes/vector3.js.map +0 -1
  107. package/dist/classes/vector4.d.ts.map +0 -1
  108. package/dist/classes/vector4.js.map +0 -1
  109. package/dist/gl-matrix/common.d.ts.map +0 -1
  110. package/dist/gl-matrix/common.js.map +0 -1
  111. package/dist/gl-matrix/mat3.d.ts.map +0 -1
  112. package/dist/gl-matrix/mat3.js.map +0 -1
  113. package/dist/gl-matrix/mat4.d.ts.map +0 -1
  114. package/dist/gl-matrix/mat4.js.map +0 -1
  115. package/dist/gl-matrix/quat.d.ts.map +0 -1
  116. package/dist/gl-matrix/quat.js.map +0 -1
  117. package/dist/gl-matrix/vec2.d.ts.map +0 -1
  118. package/dist/gl-matrix/vec2.js.map +0 -1
  119. package/dist/gl-matrix/vec3.d.ts.map +0 -1
  120. package/dist/gl-matrix/vec3.js.map +0 -1
  121. package/dist/gl-matrix/vec4.d.ts.map +0 -1
  122. package/dist/gl-matrix/vec4.js.map +0 -1
  123. package/dist/gl-matrix/wip/index.d.ts.map +0 -1
  124. package/dist/gl-matrix/wip/index.js.map +0 -1
  125. package/dist/gl-matrix/wip/mat2.d.ts.map +0 -1
  126. package/dist/gl-matrix/wip/mat2.js.map +0 -1
  127. package/dist/gl-matrix/wip/mat2d.d.ts.map +0 -1
  128. package/dist/gl-matrix/wip/mat2d.js.map +0 -1
  129. package/dist/gl-matrix/wip/quat2.d.ts.map +0 -1
  130. package/dist/gl-matrix/wip/quat2.js.map +0 -1
  131. package/dist/gl-matrix/wip/types.d.js +0 -2
  132. package/dist/gl-matrix/wip/types.d.js.map +0 -1
  133. package/dist/index.d.ts.map +0 -1
  134. package/dist/index.js.map +0 -1
  135. package/dist/lib/assert.d.ts.map +0 -1
  136. package/dist/lib/assert.js.map +0 -1
  137. package/dist/lib/common.d.ts.map +0 -1
  138. package/dist/lib/common.js.map +0 -1
  139. package/dist/lib/gl-matrix-extras.d.ts.map +0 -1
  140. package/dist/lib/gl-matrix-extras.js.map +0 -1
  141. package/dist/lib/gl-matrix.d.js +0 -2
  142. package/dist/lib/gl-matrix.d.js.map +0 -1
  143. package/dist/lib/math-utils.d.ts.map +0 -1
  144. package/dist/lib/math-utils.js.map +0 -1
  145. package/dist/lib/validators.d.ts.map +0 -1
  146. package/dist/lib/validators.js.map +0 -1
  147. package/src/gl-matrix/wip/types.d.ts +0 -93
@@ -1,478 +1,547 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Copyright (c) 2017 Uber Technologies, Inc.
1
5
  import { Matrix } from "./base/matrix.js";
2
6
  import { checkVector } from "../lib/validators.js";
7
+ /* eslint-disable camelcase */
3
8
  import { vec2_transformMat4AsVector, vec3_transformMat4AsVector } from "../lib/gl-matrix-extras.js";
9
+ // @ts-ignore gl-matrix types...
4
10
  import { fromQuat as mat4_fromQuat, frustum as mat4_frustum, lookAt as mat4_lookAt, ortho as mat4_ortho, perspective as mat4_perspective, determinant as mat4_determinant, transpose as mat4_transpose, invert as mat4_invert, multiply as mat4_multiply, rotateX as mat4_rotateX, rotateY as mat4_rotateY, rotateZ as mat4_rotateZ, rotate as mat4_rotate, scale as mat4_scale, translate as mat4_translate } from "../gl-matrix/mat4.js";
5
11
  import { transformMat4 as vec2_transformMat4 } from "../gl-matrix/vec2.js";
6
12
  import { transformMat4 as vec3_transformMat4 } from "../gl-matrix/vec3.js";
7
13
  import { transformMat4 as vec4_transformMat4 } from "../gl-matrix/vec4.js";
8
14
  var INDICES;
9
-
10
15
  (function (INDICES) {
11
- INDICES[INDICES["COL0ROW0"] = 0] = "COL0ROW0";
12
- INDICES[INDICES["COL0ROW1"] = 1] = "COL0ROW1";
13
- INDICES[INDICES["COL0ROW2"] = 2] = "COL0ROW2";
14
- INDICES[INDICES["COL0ROW3"] = 3] = "COL0ROW3";
15
- INDICES[INDICES["COL1ROW0"] = 4] = "COL1ROW0";
16
- INDICES[INDICES["COL1ROW1"] = 5] = "COL1ROW1";
17
- INDICES[INDICES["COL1ROW2"] = 6] = "COL1ROW2";
18
- INDICES[INDICES["COL1ROW3"] = 7] = "COL1ROW3";
19
- INDICES[INDICES["COL2ROW0"] = 8] = "COL2ROW0";
20
- INDICES[INDICES["COL2ROW1"] = 9] = "COL2ROW1";
21
- INDICES[INDICES["COL2ROW2"] = 10] = "COL2ROW2";
22
- INDICES[INDICES["COL2ROW3"] = 11] = "COL2ROW3";
23
- INDICES[INDICES["COL3ROW0"] = 12] = "COL3ROW0";
24
- INDICES[INDICES["COL3ROW1"] = 13] = "COL3ROW1";
25
- INDICES[INDICES["COL3ROW2"] = 14] = "COL3ROW2";
26
- INDICES[INDICES["COL3ROW3"] = 15] = "COL3ROW3";
16
+ INDICES[INDICES["COL0ROW0"] = 0] = "COL0ROW0";
17
+ INDICES[INDICES["COL0ROW1"] = 1] = "COL0ROW1";
18
+ INDICES[INDICES["COL0ROW2"] = 2] = "COL0ROW2";
19
+ INDICES[INDICES["COL0ROW3"] = 3] = "COL0ROW3";
20
+ INDICES[INDICES["COL1ROW0"] = 4] = "COL1ROW0";
21
+ INDICES[INDICES["COL1ROW1"] = 5] = "COL1ROW1";
22
+ INDICES[INDICES["COL1ROW2"] = 6] = "COL1ROW2";
23
+ INDICES[INDICES["COL1ROW3"] = 7] = "COL1ROW3";
24
+ INDICES[INDICES["COL2ROW0"] = 8] = "COL2ROW0";
25
+ INDICES[INDICES["COL2ROW1"] = 9] = "COL2ROW1";
26
+ INDICES[INDICES["COL2ROW2"] = 10] = "COL2ROW2";
27
+ INDICES[INDICES["COL2ROW3"] = 11] = "COL2ROW3";
28
+ INDICES[INDICES["COL3ROW0"] = 12] = "COL3ROW0";
29
+ INDICES[INDICES["COL3ROW1"] = 13] = "COL3ROW1";
30
+ INDICES[INDICES["COL3ROW2"] = 14] = "COL3ROW2";
31
+ INDICES[INDICES["COL3ROW3"] = 15] = "COL3ROW3";
27
32
  })(INDICES || (INDICES = {}));
28
-
29
- const DEFAULT_FOVY = 45 * Math.PI / 180;
33
+ const DEFAULT_FOVY = (45 * Math.PI) / 180;
30
34
  const DEFAULT_ASPECT = 1;
31
35
  const DEFAULT_NEAR = 0.1;
32
36
  const DEFAULT_FAR = 500;
33
37
  const IDENTITY_MATRIX = Object.freeze([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
38
+ /**
39
+ * A 4x4 matrix with common linear algebra operations
40
+ * Subclass of Array<number> meaning that it is highly compatible with other libraries
41
+ */
34
42
  export class Matrix4 extends Matrix {
35
- static get IDENTITY() {
36
- return getIdentityMatrix();
37
- }
38
-
39
- static get ZERO() {
40
- return getZeroMatrix();
41
- }
42
-
43
- get ELEMENTS() {
44
- return 16;
45
- }
46
-
47
- get RANK() {
48
- return 4;
49
- }
50
-
51
- get INDICES() {
52
- return INDICES;
53
- }
54
-
55
- constructor(array) {
56
- super(-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0);
57
-
58
- if (arguments.length === 1 && Array.isArray(array)) {
59
- this.copy(array);
60
- } else {
61
- this.identity();
62
- }
63
- }
64
-
65
- copy(array) {
66
- this[0] = array[0];
67
- this[1] = array[1];
68
- this[2] = array[2];
69
- this[3] = array[3];
70
- this[4] = array[4];
71
- this[5] = array[5];
72
- this[6] = array[6];
73
- this[7] = array[7];
74
- this[8] = array[8];
75
- this[9] = array[9];
76
- this[10] = array[10];
77
- this[11] = array[11];
78
- this[12] = array[12];
79
- this[13] = array[13];
80
- this[14] = array[14];
81
- this[15] = array[15];
82
- return this.check();
83
- }
84
-
85
- set(m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33) {
86
- this[0] = m00;
87
- this[1] = m10;
88
- this[2] = m20;
89
- this[3] = m30;
90
- this[4] = m01;
91
- this[5] = m11;
92
- this[6] = m21;
93
- this[7] = m31;
94
- this[8] = m02;
95
- this[9] = m12;
96
- this[10] = m22;
97
- this[11] = m32;
98
- this[12] = m03;
99
- this[13] = m13;
100
- this[14] = m23;
101
- this[15] = m33;
102
- return this.check();
103
- }
104
-
105
- setRowMajor(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
106
- this[0] = m00;
107
- this[1] = m10;
108
- this[2] = m20;
109
- this[3] = m30;
110
- this[4] = m01;
111
- this[5] = m11;
112
- this[6] = m21;
113
- this[7] = m31;
114
- this[8] = m02;
115
- this[9] = m12;
116
- this[10] = m22;
117
- this[11] = m32;
118
- this[12] = m03;
119
- this[13] = m13;
120
- this[14] = m23;
121
- this[15] = m33;
122
- return this.check();
123
- }
124
-
125
- toRowMajor(result) {
126
- result[0] = this[0];
127
- result[1] = this[4];
128
- result[2] = this[8];
129
- result[3] = this[12];
130
- result[4] = this[1];
131
- result[5] = this[5];
132
- result[6] = this[9];
133
- result[7] = this[13];
134
- result[8] = this[2];
135
- result[9] = this[6];
136
- result[10] = this[10];
137
- result[11] = this[14];
138
- result[12] = this[3];
139
- result[13] = this[7];
140
- result[14] = this[11];
141
- result[15] = this[15];
142
- return result;
143
- }
144
-
145
- identity() {
146
- return this.copy(IDENTITY_MATRIX);
147
- }
148
-
149
- fromObject(object) {
150
- return this.check();
151
- }
152
-
153
- fromQuaternion(quaternion) {
154
- mat4_fromQuat(this, quaternion);
155
- return this.check();
156
- }
157
-
158
- frustum(view) {
159
- const {
160
- left,
161
- right,
162
- bottom,
163
- top,
164
- near = DEFAULT_NEAR,
165
- far = DEFAULT_FAR
166
- } = view;
167
-
168
- if (far === Infinity) {
169
- computeInfinitePerspectiveOffCenter(this, left, right, bottom, top, near);
170
- } else {
171
- mat4_frustum(this, left, right, bottom, top, near, far);
172
- }
173
-
174
- return this.check();
175
- }
176
-
177
- lookAt(view) {
178
- const {
179
- eye,
180
- center = [0, 0, 0],
181
- up = [0, 1, 0]
182
- } = view;
183
- mat4_lookAt(this, eye, center, up);
184
- return this.check();
185
- }
186
-
187
- ortho(view) {
188
- const {
189
- left,
190
- right,
191
- bottom,
192
- top,
193
- near = DEFAULT_NEAR,
194
- far = DEFAULT_FAR
195
- } = view;
196
- mat4_ortho(this, left, right, bottom, top, near, far);
197
- return this.check();
198
- }
199
-
200
- orthographic(view) {
201
- const {
202
- fovy = DEFAULT_FOVY,
203
- aspect = DEFAULT_ASPECT,
204
- focalDistance = 1,
205
- near = DEFAULT_NEAR,
206
- far = DEFAULT_FAR
207
- } = view;
208
- checkRadians(fovy);
209
- const halfY = fovy / 2;
210
- const top = focalDistance * Math.tan(halfY);
211
- const right = top * aspect;
212
- return this.ortho({
213
- left: -right,
214
- right,
215
- bottom: -top,
216
- top,
217
- near,
218
- far
219
- });
220
- }
221
-
222
- perspective(view) {
223
- const {
224
- fovy = 45 * Math.PI / 180,
225
- aspect = 1,
226
- near = 0.1,
227
- far = 500
228
- } = view;
229
- checkRadians(fovy);
230
- mat4_perspective(this, fovy, aspect, near, far);
231
- return this.check();
232
- }
233
-
234
- determinant() {
235
- return mat4_determinant(this);
236
- }
237
-
238
- getScale(result = [-0, -0, -0]) {
239
- result[0] = Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2]);
240
- result[1] = Math.sqrt(this[4] * this[4] + this[5] * this[5] + this[6] * this[6]);
241
- result[2] = Math.sqrt(this[8] * this[8] + this[9] * this[9] + this[10] * this[10]);
242
- return result;
243
- }
244
-
245
- getTranslation(result = [-0, -0, -0]) {
246
- result[0] = this[12];
247
- result[1] = this[13];
248
- result[2] = this[14];
249
- return result;
250
- }
251
-
252
- getRotation(result, scaleResult) {
253
- result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0];
254
- scaleResult = scaleResult || [-0, -0, -0];
255
- const scale = this.getScale(scaleResult);
256
- const inverseScale0 = 1 / scale[0];
257
- const inverseScale1 = 1 / scale[1];
258
- const inverseScale2 = 1 / scale[2];
259
- result[0] = this[0] * inverseScale0;
260
- result[1] = this[1] * inverseScale1;
261
- result[2] = this[2] * inverseScale2;
262
- result[3] = 0;
263
- result[4] = this[4] * inverseScale0;
264
- result[5] = this[5] * inverseScale1;
265
- result[6] = this[6] * inverseScale2;
266
- result[7] = 0;
267
- result[8] = this[8] * inverseScale0;
268
- result[9] = this[9] * inverseScale1;
269
- result[10] = this[10] * inverseScale2;
270
- result[11] = 0;
271
- result[12] = 0;
272
- result[13] = 0;
273
- result[14] = 0;
274
- result[15] = 1;
275
- return result;
276
- }
277
-
278
- getRotationMatrix3(result, scaleResult) {
279
- result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0];
280
- scaleResult = scaleResult || [-0, -0, -0];
281
- const scale = this.getScale(scaleResult);
282
- const inverseScale0 = 1 / scale[0];
283
- const inverseScale1 = 1 / scale[1];
284
- const inverseScale2 = 1 / scale[2];
285
- result[0] = this[0] * inverseScale0;
286
- result[1] = this[1] * inverseScale1;
287
- result[2] = this[2] * inverseScale2;
288
- result[3] = this[4] * inverseScale0;
289
- result[4] = this[5] * inverseScale1;
290
- result[5] = this[6] * inverseScale2;
291
- result[6] = this[8] * inverseScale0;
292
- result[7] = this[9] * inverseScale1;
293
- result[8] = this[10] * inverseScale2;
294
- return result;
295
- }
296
-
297
- transpose() {
298
- mat4_transpose(this, this);
299
- return this.check();
300
- }
301
-
302
- invert() {
303
- mat4_invert(this, this);
304
- return this.check();
305
- }
306
-
307
- multiplyLeft(a) {
308
- mat4_multiply(this, a, this);
309
- return this.check();
310
- }
311
-
312
- multiplyRight(a) {
313
- mat4_multiply(this, this, a);
314
- return this.check();
315
- }
316
-
317
- rotateX(radians) {
318
- mat4_rotateX(this, this, radians);
319
- return this.check();
320
- }
321
-
322
- rotateY(radians) {
323
- mat4_rotateY(this, this, radians);
324
- return this.check();
325
- }
326
-
327
- rotateZ(radians) {
328
- mat4_rotateZ(this, this, radians);
329
- return this.check();
330
- }
331
-
332
- rotateXYZ(angleXYZ) {
333
- return this.rotateX(angleXYZ[0]).rotateY(angleXYZ[1]).rotateZ(angleXYZ[2]);
334
- }
335
-
336
- rotateAxis(radians, axis) {
337
- mat4_rotate(this, this, radians, axis);
338
- return this.check();
339
- }
340
-
341
- scale(factor) {
342
- mat4_scale(this, this, Array.isArray(factor) ? factor : [factor, factor, factor]);
343
- return this.check();
344
- }
345
-
346
- translate(vector) {
347
- mat4_translate(this, this, vector);
348
- return this.check();
349
- }
350
-
351
- transform(vector, result) {
352
- if (vector.length === 4) {
353
- result = vec4_transformMat4(result || [-0, -0, -0, -0], vector, this);
354
- checkVector(result, 4);
355
- return result;
356
- }
357
-
358
- return this.transformAsPoint(vector, result);
359
- }
360
-
361
- transformAsPoint(vector, result) {
362
- const {
363
- length
364
- } = vector;
365
- let out;
366
-
367
- switch (length) {
368
- case 2:
369
- out = vec2_transformMat4(result || [-0, -0], vector, this);
370
- break;
371
-
372
- case 3:
373
- out = vec3_transformMat4(result || [-0, -0, -0], vector, this);
374
- break;
375
-
376
- default:
377
- throw new Error('Illegal vector');
378
- }
379
-
380
- checkVector(out, vector.length);
381
- return out;
382
- }
383
-
384
- transformAsVector(vector, result) {
385
- let out;
386
-
387
- switch (vector.length) {
388
- case 2:
389
- out = vec2_transformMat4AsVector(result || [-0, -0], vector, this);
390
- break;
391
-
392
- case 3:
393
- out = vec3_transformMat4AsVector(result || [-0, -0, -0], vector, this);
394
- break;
395
-
396
- default:
397
- throw new Error('Illegal vector');
398
- }
399
-
400
- checkVector(out, vector.length);
401
- return out;
402
- }
403
-
404
- transformPoint(vector, result) {
405
- return this.transformAsPoint(vector, result);
406
- }
407
-
408
- transformVector(vector, result) {
409
- return this.transformAsPoint(vector, result);
410
- }
411
-
412
- transformDirection(vector, result) {
413
- return this.transformAsVector(vector, result);
414
- }
415
-
416
- makeRotationX(radians) {
417
- return this.identity().rotateX(radians);
418
- }
419
-
420
- makeTranslation(x, y, z) {
421
- return this.identity().translate([x, y, z]);
422
- }
423
-
43
+ static get IDENTITY() {
44
+ return getIdentityMatrix();
45
+ }
46
+ static get ZERO() {
47
+ return getZeroMatrix();
48
+ }
49
+ get ELEMENTS() {
50
+ return 16;
51
+ }
52
+ get RANK() {
53
+ return 4;
54
+ }
55
+ get INDICES() {
56
+ return INDICES;
57
+ }
58
+ constructor(array) {
59
+ // PERF NOTE: initialize elements as double precision numbers
60
+ super(-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0);
61
+ if (arguments.length === 1 && Array.isArray(array)) {
62
+ this.copy(array);
63
+ }
64
+ else {
65
+ this.identity();
66
+ }
67
+ }
68
+ copy(array) {
69
+ this[0] = array[0];
70
+ this[1] = array[1];
71
+ this[2] = array[2];
72
+ this[3] = array[3];
73
+ this[4] = array[4];
74
+ this[5] = array[5];
75
+ this[6] = array[6];
76
+ this[7] = array[7];
77
+ this[8] = array[8];
78
+ this[9] = array[9];
79
+ this[10] = array[10];
80
+ this[11] = array[11];
81
+ this[12] = array[12];
82
+ this[13] = array[13];
83
+ this[14] = array[14];
84
+ this[15] = array[15];
85
+ return this.check();
86
+ }
87
+ // eslint-disable-next-line max-params
88
+ set(m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33) {
89
+ this[0] = m00;
90
+ this[1] = m10;
91
+ this[2] = m20;
92
+ this[3] = m30;
93
+ this[4] = m01;
94
+ this[5] = m11;
95
+ this[6] = m21;
96
+ this[7] = m31;
97
+ this[8] = m02;
98
+ this[9] = m12;
99
+ this[10] = m22;
100
+ this[11] = m32;
101
+ this[12] = m03;
102
+ this[13] = m13;
103
+ this[14] = m23;
104
+ this[15] = m33;
105
+ return this.check();
106
+ }
107
+ // accepts row major order, stores as column major
108
+ // eslint-disable-next-line max-params
109
+ setRowMajor(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
110
+ this[0] = m00;
111
+ this[1] = m10;
112
+ this[2] = m20;
113
+ this[3] = m30;
114
+ this[4] = m01;
115
+ this[5] = m11;
116
+ this[6] = m21;
117
+ this[7] = m31;
118
+ this[8] = m02;
119
+ this[9] = m12;
120
+ this[10] = m22;
121
+ this[11] = m32;
122
+ this[12] = m03;
123
+ this[13] = m13;
124
+ this[14] = m23;
125
+ this[15] = m33;
126
+ return this.check();
127
+ }
128
+ toRowMajor(result) {
129
+ result[0] = this[0];
130
+ result[1] = this[4];
131
+ result[2] = this[8];
132
+ result[3] = this[12];
133
+ result[4] = this[1];
134
+ result[5] = this[5];
135
+ result[6] = this[9];
136
+ result[7] = this[13];
137
+ result[8] = this[2];
138
+ result[9] = this[6];
139
+ result[10] = this[10];
140
+ result[11] = this[14];
141
+ result[12] = this[3];
142
+ result[13] = this[7];
143
+ result[14] = this[11];
144
+ result[15] = this[15];
145
+ return result;
146
+ }
147
+ // Constructors
148
+ /** Set to identity matrix */
149
+ identity() {
150
+ return this.copy(IDENTITY_MATRIX);
151
+ }
152
+ /**
153
+ *
154
+ * @param object
155
+ * @returns self
156
+ */
157
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
158
+ fromObject(object) {
159
+ return this.check();
160
+ }
161
+ /**
162
+ * Calculates a 4x4 matrix from the given quaternion
163
+ * @param quaternion Quaternion to create matrix from
164
+ * @returns self
165
+ */
166
+ fromQuaternion(quaternion) {
167
+ mat4_fromQuat(this, quaternion);
168
+ return this.check();
169
+ }
170
+ /**
171
+ * Generates a frustum matrix with the given bounds
172
+ * @param view.left - Left bound of the frustum
173
+ * @param view.right - Right bound of the frustum
174
+ * @param view.bottom - Bottom bound of the frustum
175
+ * @param view.top - Top bound of the frustum
176
+ * @param view.near - Near bound of the frustum
177
+ * @param view.far - Far bound of the frustum. Can be set to Infinity.
178
+ * @returns self
179
+ */
180
+ frustum(view) {
181
+ const { left, right, bottom, top, near = DEFAULT_NEAR, far = DEFAULT_FAR } = view;
182
+ if (far === Infinity) {
183
+ computeInfinitePerspectiveOffCenter(this, left, right, bottom, top, near);
184
+ }
185
+ else {
186
+ mat4_frustum(this, left, right, bottom, top, near, far);
187
+ }
188
+ return this.check();
189
+ }
190
+ /**
191
+ * Generates a look-at matrix with the given eye position, focal point,
192
+ * and up axis
193
+ * @param view.eye - (vector) Position of the viewer
194
+ * @param view.center - (vector) Point the viewer is looking at
195
+ * @param view.up - (vector) Up axis
196
+ * @returns self
197
+ */
198
+ lookAt(view) {
199
+ const { eye, center = [0, 0, 0], up = [0, 1, 0] } = view;
200
+ mat4_lookAt(this, eye, center, up);
201
+ return this.check();
202
+ }
203
+ /**
204
+ * Generates a orthogonal projection matrix with the given bounds
205
+ * from "traditional" view space parameters
206
+ * @param view.left - Left bound of the frustum
207
+ * @param view.right number Right bound of the frustum
208
+ * @param view.bottom - Bottom bound of the frustum
209
+ * @param view.top number Top bound of the frustum
210
+ * @param view.near - Near bound of the frustum
211
+ * @param view.far number Far bound of the frustum
212
+ * @returns self
213
+ */
214
+ ortho(view) {
215
+ const { left, right, bottom, top, near = DEFAULT_NEAR, far = DEFAULT_FAR } = view;
216
+ mat4_ortho(this, left, right, bottom, top, near, far);
217
+ return this.check();
218
+ }
219
+ /**
220
+ * Generates an orthogonal projection matrix with the same parameters
221
+ * as a perspective matrix (plus focalDistance)
222
+ * @param view.fovy Vertical field of view in radians
223
+ * @param view.aspect Aspect ratio. Typically viewport width / viewport height
224
+ * @param view.focalDistance Distance in the view frustum used for extent calculations
225
+ * @param view.near Near bound of the frustum
226
+ * @param view.far Far bound of the frustum
227
+ * @returns self
228
+ */
229
+ orthographic(view) {
230
+ const { fovy = DEFAULT_FOVY, aspect = DEFAULT_ASPECT, focalDistance = 1, near = DEFAULT_NEAR, far = DEFAULT_FAR } = view;
231
+ checkRadians(fovy);
232
+ const halfY = fovy / 2;
233
+ const top = focalDistance * Math.tan(halfY); // focus_plane is the distance from the camera
234
+ const right = top * aspect;
235
+ return this.ortho({
236
+ left: -right,
237
+ right,
238
+ bottom: -top,
239
+ top,
240
+ near,
241
+ far
242
+ });
243
+ }
244
+ /**
245
+ * Generates a perspective projection matrix with the given bounds
246
+ * @param view.fovy Vertical field of view in radians
247
+ * @param view.aspect Aspect ratio. typically viewport width/height
248
+ * @param view.near Near bound of the frustum
249
+ * @param view.far Far bound of the frustum
250
+ * @returns self
251
+ */
252
+ perspective(view) {
253
+ const { fovy = (45 * Math.PI) / 180, aspect = 1, near = 0.1, far = 500 } = view;
254
+ checkRadians(fovy);
255
+ mat4_perspective(this, fovy, aspect, near, far);
256
+ return this.check();
257
+ }
258
+ // Accessors
259
+ determinant() {
260
+ return mat4_determinant(this);
261
+ }
262
+ /**
263
+ * Extracts the non-uniform scale assuming the matrix is an affine transformation.
264
+ * The scales are the "lengths" of the column vectors in the upper-left 3x3 matrix.
265
+ * @param result
266
+ * @returns self
267
+ */
268
+ getScale(result = [-0, -0, -0]) {
269
+ // explicit is faster than hypot...
270
+ result[0] = Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2]);
271
+ result[1] = Math.sqrt(this[4] * this[4] + this[5] * this[5] + this[6] * this[6]);
272
+ result[2] = Math.sqrt(this[8] * this[8] + this[9] * this[9] + this[10] * this[10]);
273
+ // result[0] = Math.hypot(this[0], this[1], this[2]);
274
+ // result[1] = Math.hypot(this[4], this[5], this[6]);
275
+ // result[2] = Math.hypot(this[8], this[9], this[10]);
276
+ return result;
277
+ }
278
+ /**
279
+ * Gets the translation portion, assuming the matrix is a affine transformation matrix.
280
+ * @param result
281
+ * @returns self
282
+ */
283
+ getTranslation(result = [-0, -0, -0]) {
284
+ result[0] = this[12];
285
+ result[1] = this[13];
286
+ result[2] = this[14];
287
+ return result;
288
+ }
289
+ /**
290
+ * Gets upper left 3x3 pure rotation matrix (non-scaling), assume affine transformation matrix
291
+ * @param result
292
+ * @param scaleResult
293
+ * @returns self
294
+ */
295
+ getRotation(result, scaleResult) {
296
+ result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0];
297
+ scaleResult = scaleResult || [-0, -0, -0];
298
+ const scale = this.getScale(scaleResult);
299
+ const inverseScale0 = 1 / scale[0];
300
+ const inverseScale1 = 1 / scale[1];
301
+ const inverseScale2 = 1 / scale[2];
302
+ result[0] = this[0] * inverseScale0;
303
+ result[1] = this[1] * inverseScale1;
304
+ result[2] = this[2] * inverseScale2;
305
+ result[3] = 0;
306
+ result[4] = this[4] * inverseScale0;
307
+ result[5] = this[5] * inverseScale1;
308
+ result[6] = this[6] * inverseScale2;
309
+ result[7] = 0;
310
+ result[8] = this[8] * inverseScale0;
311
+ result[9] = this[9] * inverseScale1;
312
+ result[10] = this[10] * inverseScale2;
313
+ result[11] = 0;
314
+ result[12] = 0;
315
+ result[13] = 0;
316
+ result[14] = 0;
317
+ result[15] = 1;
318
+ return result;
319
+ }
320
+ /**
321
+ *
322
+ * @param result
323
+ * @param scaleResult
324
+ * @returns self
325
+ */
326
+ getRotationMatrix3(result, scaleResult) {
327
+ result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0];
328
+ scaleResult = scaleResult || [-0, -0, -0];
329
+ const scale = this.getScale(scaleResult);
330
+ const inverseScale0 = 1 / scale[0];
331
+ const inverseScale1 = 1 / scale[1];
332
+ const inverseScale2 = 1 / scale[2];
333
+ result[0] = this[0] * inverseScale0;
334
+ result[1] = this[1] * inverseScale1;
335
+ result[2] = this[2] * inverseScale2;
336
+ result[3] = this[4] * inverseScale0;
337
+ result[4] = this[5] * inverseScale1;
338
+ result[5] = this[6] * inverseScale2;
339
+ result[6] = this[8] * inverseScale0;
340
+ result[7] = this[9] * inverseScale1;
341
+ result[8] = this[10] * inverseScale2;
342
+ return result;
343
+ }
344
+ // Modifiers
345
+ transpose() {
346
+ mat4_transpose(this, this);
347
+ return this.check();
348
+ }
349
+ invert() {
350
+ mat4_invert(this, this);
351
+ return this.check();
352
+ }
353
+ // Operations
354
+ multiplyLeft(a) {
355
+ mat4_multiply(this, a, this);
356
+ return this.check();
357
+ }
358
+ multiplyRight(a) {
359
+ mat4_multiply(this, this, a);
360
+ return this.check();
361
+ }
362
+ // Rotates a matrix by the given angle around the X axis
363
+ rotateX(radians) {
364
+ mat4_rotateX(this, this, radians);
365
+ // mat4_rotate(this, this, radians, [1, 0, 0]);
366
+ return this.check();
367
+ }
368
+ // Rotates a matrix by the given angle around the Y axis.
369
+ rotateY(radians) {
370
+ mat4_rotateY(this, this, radians);
371
+ // mat4_rotate(this, this, radians, [0, 1, 0]);
372
+ return this.check();
373
+ }
374
+ /**
375
+ * Rotates a matrix by the given angle around the Z axis.
376
+ * @param radians
377
+ * @returns self
378
+ */
379
+ rotateZ(radians) {
380
+ mat4_rotateZ(this, this, radians);
381
+ // mat4_rotate(this, this, radians, [0, 0, 1]);
382
+ return this.check();
383
+ }
384
+ /**
385
+ *
386
+ * @param param0
387
+ * @returns self
388
+ */
389
+ rotateXYZ(angleXYZ) {
390
+ return this.rotateX(angleXYZ[0]).rotateY(angleXYZ[1]).rotateZ(angleXYZ[2]);
391
+ }
392
+ /**
393
+ *
394
+ * @param radians
395
+ * @param axis
396
+ * @returns self
397
+ */
398
+ rotateAxis(radians, axis) {
399
+ mat4_rotate(this, this, radians, axis);
400
+ return this.check();
401
+ }
402
+ /**
403
+ *
404
+ * @param factor
405
+ * @returns self
406
+ */
407
+ scale(factor) {
408
+ mat4_scale(this, this, Array.isArray(factor) ? factor : [factor, factor, factor]);
409
+ return this.check();
410
+ }
411
+ /**
412
+ *
413
+ * @param vec
414
+ * @returns self
415
+ */
416
+ translate(vector) {
417
+ mat4_translate(this, this, vector);
418
+ return this.check();
419
+ }
420
+ // Transforms
421
+ /**
422
+ * Transforms any 2, 3 or 4 element vector. 2 and 3 elements are treated as points
423
+ * @param vector
424
+ * @param result
425
+ * @returns self
426
+ */
427
+ transform(vector, result) {
428
+ if (vector.length === 4) {
429
+ result = vec4_transformMat4(result || [-0, -0, -0, -0], vector, this);
430
+ checkVector(result, 4);
431
+ return result;
432
+ }
433
+ return this.transformAsPoint(vector, result);
434
+ }
435
+ /**
436
+ * Transforms any 2 or 3 element array as point (w implicitly 1)
437
+ * @param vector
438
+ * @param result
439
+ * @returns self
440
+ */
441
+ transformAsPoint(vector, result) {
442
+ const { length } = vector;
443
+ let out;
444
+ switch (length) {
445
+ case 2:
446
+ out = vec2_transformMat4(result || [-0, -0], vector, this);
447
+ break;
448
+ case 3:
449
+ out = vec3_transformMat4(result || [-0, -0, -0], vector, this);
450
+ break;
451
+ default:
452
+ throw new Error('Illegal vector');
453
+ }
454
+ checkVector(out, vector.length);
455
+ return out;
456
+ }
457
+ /**
458
+ * Transforms any 2 or 3 element array as vector (w implicitly 0)
459
+ * @param vector
460
+ * @param result
461
+ * @returns self
462
+ */
463
+ transformAsVector(vector, result) {
464
+ let out;
465
+ switch (vector.length) {
466
+ case 2:
467
+ out = vec2_transformMat4AsVector(result || [-0, -0], vector, this);
468
+ break;
469
+ case 3:
470
+ out = vec3_transformMat4AsVector(result || [-0, -0, -0], vector, this);
471
+ break;
472
+ default:
473
+ throw new Error('Illegal vector');
474
+ }
475
+ checkVector(out, vector.length);
476
+ return out;
477
+ }
478
+ /** @deprecated */
479
+ transformPoint(vector, result) {
480
+ return this.transformAsPoint(vector, result);
481
+ }
482
+ /** @deprecated */
483
+ transformVector(vector, result) {
484
+ return this.transformAsPoint(vector, result);
485
+ }
486
+ /** @deprecated */
487
+ transformDirection(vector, result) {
488
+ return this.transformAsVector(vector, result);
489
+ }
490
+ // three.js math API compatibility
491
+ makeRotationX(radians) {
492
+ return this.identity().rotateX(radians);
493
+ }
494
+ makeTranslation(x, y, z) {
495
+ return this.identity().translate([x, y, z]);
496
+ }
424
497
  }
498
+ // TODO initializing static members directly is an option, but make sure no tree-shaking issues
425
499
  let ZERO;
426
500
  let IDENTITY;
427
-
428
501
  function getZeroMatrix() {
429
- if (!ZERO) {
430
- ZERO = new Matrix4([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
431
- Object.freeze(ZERO);
432
- }
433
-
434
- return ZERO;
502
+ if (!ZERO) {
503
+ ZERO = new Matrix4([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
504
+ Object.freeze(ZERO);
505
+ }
506
+ return ZERO;
435
507
  }
436
-
437
508
  function getIdentityMatrix() {
438
- if (!IDENTITY) {
439
- IDENTITY = new Matrix4();
440
- Object.freeze(IDENTITY);
441
- }
442
-
443
- return IDENTITY;
509
+ if (!IDENTITY) {
510
+ IDENTITY = new Matrix4();
511
+ Object.freeze(IDENTITY);
512
+ }
513
+ return IDENTITY;
444
514
  }
445
-
515
+ // HELPER FUNCTIONS
446
516
  function checkRadians(possiblyDegrees) {
447
- if (possiblyDegrees > Math.PI * 2) {
448
- throw Error('expected radians');
449
- }
517
+ if (possiblyDegrees > Math.PI * 2) {
518
+ throw Error('expected radians');
519
+ }
450
520
  }
451
-
521
+ // eslint-disable-next-line max-params
452
522
  function computeInfinitePerspectiveOffCenter(result, left, right, bottom, top, near) {
453
- const column0Row0 = 2 * near / (right - left);
454
- const column1Row1 = 2 * near / (top - bottom);
455
- const column2Row0 = (right + left) / (right - left);
456
- const column2Row1 = (top + bottom) / (top - bottom);
457
- const column2Row2 = -1;
458
- const column2Row3 = -1;
459
- const column3Row2 = -2 * near;
460
- result[0] = column0Row0;
461
- result[1] = 0;
462
- result[2] = 0;
463
- result[3] = 0;
464
- result[4] = 0;
465
- result[5] = column1Row1;
466
- result[6] = 0;
467
- result[7] = 0;
468
- result[8] = column2Row0;
469
- result[9] = column2Row1;
470
- result[10] = column2Row2;
471
- result[11] = column2Row3;
472
- result[12] = 0;
473
- result[13] = 0;
474
- result[14] = column3Row2;
475
- result[15] = 0;
476
- return result;
523
+ const column0Row0 = (2 * near) / (right - left);
524
+ const column1Row1 = (2 * near) / (top - bottom);
525
+ const column2Row0 = (right + left) / (right - left);
526
+ const column2Row1 = (top + bottom) / (top - bottom);
527
+ const column2Row2 = -1;
528
+ const column2Row3 = -1;
529
+ const column3Row2 = -2 * near;
530
+ result[0] = column0Row0;
531
+ result[1] = 0;
532
+ result[2] = 0;
533
+ result[3] = 0;
534
+ result[4] = 0;
535
+ result[5] = column1Row1;
536
+ result[6] = 0;
537
+ result[7] = 0;
538
+ result[8] = column2Row0;
539
+ result[9] = column2Row1;
540
+ result[10] = column2Row2;
541
+ result[11] = column2Row3;
542
+ result[12] = 0;
543
+ result[13] = 0;
544
+ result[14] = column3Row2;
545
+ result[15] = 0;
546
+ return result;
477
547
  }
478
- //# sourceMappingURL=matrix4.js.map