@vyr/engine 0.0.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 (95) hide show
  1. package/package.json +19 -0
  2. package/src/ArrayUtils.ts +65 -0
  3. package/src/AsyncTask.ts +72 -0
  4. package/src/Category.ts +119 -0
  5. package/src/Color.ts +111 -0
  6. package/src/Engine.ts +101 -0
  7. package/src/Generate.ts +40 -0
  8. package/src/InputSystem.ts +108 -0
  9. package/src/Listener.ts +59 -0
  10. package/src/ObjectPool.ts +84 -0
  11. package/src/ObjectUtils.ts +49 -0
  12. package/src/Scriptable.ts +27 -0
  13. package/src/Serialization.ts +49 -0
  14. package/src/Traverser.ts +39 -0
  15. package/src/actor/Actor.ts +28 -0
  16. package/src/actor/AnimationUnitActor.ts +289 -0
  17. package/src/actor/DivActor.ts +70 -0
  18. package/src/actor/FragmentActor.ts +56 -0
  19. package/src/actor/HTMActor.ts +166 -0
  20. package/src/actor/HTMServiceActor.ts +57 -0
  21. package/src/actor/HTMTransformControllerActor.ts +404 -0
  22. package/src/actor/StyleActor.ts +96 -0
  23. package/src/actor/index.ts +8 -0
  24. package/src/asset/Asset.ts +271 -0
  25. package/src/asset/AssetGraph.ts +246 -0
  26. package/src/asset/index.ts +2 -0
  27. package/src/descriptor/AnimationUnitDescriptor.ts +65 -0
  28. package/src/descriptor/CameraDescriptor.ts +12 -0
  29. package/src/descriptor/ControllerDescriptor.ts +16 -0
  30. package/src/descriptor/DatasetDescriptor.ts +92 -0
  31. package/src/descriptor/Descriptor.ts +415 -0
  32. package/src/descriptor/DivDescriptor.ts +18 -0
  33. package/src/descriptor/DynamicDescriptor.ts +27 -0
  34. package/src/descriptor/HTMLDescriptor.ts +87 -0
  35. package/src/descriptor/HTMLServiceDescriptor.ts +19 -0
  36. package/src/descriptor/HTMLTransformControllerDescriptor.ts +34 -0
  37. package/src/descriptor/NodeDescriptor.ts +32 -0
  38. package/src/descriptor/PrefabDescriptor.ts +53 -0
  39. package/src/descriptor/PrefabInstanceDescriptor.ts +32 -0
  40. package/src/descriptor/RoutineDescriptor.ts +54 -0
  41. package/src/descriptor/ServiceDescriptor.ts +32 -0
  42. package/src/descriptor/ServiceSchedulerDescriptor.ts +32 -0
  43. package/src/descriptor/StyleDescriptor.ts +213 -0
  44. package/src/descriptor/index.ts +17 -0
  45. package/src/graphics/Collection.ts +25 -0
  46. package/src/graphics/Compilation.ts +82 -0
  47. package/src/graphics/Graphics.ts +475 -0
  48. package/src/graphics/Observer.ts +36 -0
  49. package/src/graphics/Unit.ts +83 -0
  50. package/src/graphics/VariableProxy.ts +92 -0
  51. package/src/graphics/index.ts +5 -0
  52. package/src/index.ts +26 -0
  53. package/src/interpreter/AnimationUnitInterpreter.ts +53 -0
  54. package/src/interpreter/DatasetInterpreter.ts +11 -0
  55. package/src/interpreter/DivInterpreter.ts +44 -0
  56. package/src/interpreter/DynamicInterpreter.ts +207 -0
  57. package/src/interpreter/FragmentInterpreter.ts +34 -0
  58. package/src/interpreter/HTMLServiceInterpreter.ts +47 -0
  59. package/src/interpreter/HTMLTransformControllerInterpreter.ts +40 -0
  60. package/src/interpreter/Interpreter.ts +69 -0
  61. package/src/interpreter/PrefaInterpreter.ts +11 -0
  62. package/src/interpreter/PrefabInstanceInterpreter.ts +12 -0
  63. package/src/interpreter/RoutineInterpreter.ts +88 -0
  64. package/src/interpreter/ServiceInterpreter.ts +24 -0
  65. package/src/interpreter/ServiceSchedulerInterpreter.ts +42 -0
  66. package/src/interpreter/StyleInterpreter.ts +66 -0
  67. package/src/interpreter/index.ts +14 -0
  68. package/src/locale/Language.ts +10 -0
  69. package/src/locale/LanguageProvider.ts +48 -0
  70. package/src/locale/index.ts +2 -0
  71. package/src/math/Euler.ts +303 -0
  72. package/src/math/Matrix4.ts +1123 -0
  73. package/src/math/Quaternion.ts +737 -0
  74. package/src/math/Vector2.ts +680 -0
  75. package/src/math/Vector3.ts +1062 -0
  76. package/src/math/index.ts +5 -0
  77. package/src/math/utils.ts +17 -0
  78. package/src/preset/execute/dataset/index.ts +1 -0
  79. package/src/preset/execute/dataset/update.ts +52 -0
  80. package/src/preset/execute/graphics/index.ts +1 -0
  81. package/src/preset/execute/graphics/invoke.ts +49 -0
  82. package/src/preset/execute/index.ts +4 -0
  83. package/src/preset/execute/net/index.ts +1 -0
  84. package/src/preset/execute/net/request.ts +103 -0
  85. package/src/preset/execute/scheduler/index.ts +1 -0
  86. package/src/preset/execute/scheduler/switch.ts +46 -0
  87. package/src/preset/index.ts +7 -0
  88. package/src/preset/routine/graphics/index.ts +1 -0
  89. package/src/preset/routine/graphics/invoke.ts +27 -0
  90. package/src/preset/routine/index.ts +2 -0
  91. package/src/preset/routine/scheduler/index.ts +1 -0
  92. package/src/preset/routine/scheduler/switch.ts +27 -0
  93. package/src/setup/index.ts +17 -0
  94. package/src/utils/AssetProvider.ts +72 -0
  95. package/src/utils/index.ts +1 -0
@@ -0,0 +1,1123 @@
1
+ import { Quaternion } from './Quaternion';
2
+ import { Vector3 } from './Vector3';
3
+ import { Euler } from './Euler';
4
+
5
+ const WebGLCoordinateSystem = 1, WebGPUCoordinateSystem = 2
6
+
7
+ type CoordinateSystem = typeof WebGLCoordinateSystem | typeof WebGPUCoordinateSystem;
8
+
9
+ /**
10
+ * Represents a 4x4 matrix.
11
+ *
12
+ * The most common use of a 4x4 matrix in 3D computer graphics is as a transformation matrix.
13
+ * For an introduction to transformation matrices as used in WebGL, check out [this tutorial]{@link https://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices}
14
+ *
15
+ * This allows a 3D vector representing a point in 3D space to undergo
16
+ * transformations such as translation, rotation, shear, scale, reflection,
17
+ * orthogonal or perspective projection and so on, by being multiplied by the
18
+ * matrix. This is known as `applying` the matrix to the vector.
19
+ *
20
+ * A Note on Row-Major and Column-Major Ordering:
21
+ *
22
+ * The constructor and {@link Matrix4#set} method take arguments in
23
+ * [row-major]{@link https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order}
24
+ * order, while internally they are stored in the {@link Matrix4#elements} array in column-major order.
25
+ * This means that calling:
26
+ * ```js
27
+ * const m = new Matrix4();
28
+ * m.set( 11, 12, 13, 14,
29
+ * 21, 22, 23, 24,
30
+ * 31, 32, 33, 34,
31
+ * 41, 42, 43, 44 );
32
+ * ```
33
+ * will result in the elements array containing:
34
+ * ```js
35
+ * m.elements = [ 11, 21, 31, 41,
36
+ * 12, 22, 32, 42,
37
+ * 13, 23, 33, 43,
38
+ * 14, 24, 34, 44 ];
39
+ * ```
40
+ * and internally all calculations are performed using column-major ordering.
41
+ * However, as the actual ordering makes no difference mathematically and
42
+ * most people are used to thinking about matrices in row-major order, the
43
+ * three.js documentation shows matrices in row-major order. Just bear in
44
+ * mind that if you are reading the source code, you'll have to take the
45
+ * transpose of any matrices outlined here to make sense of the calculations.
46
+ */
47
+ class Matrix4 {
48
+
49
+ /**
50
+ * A column-major list of matrix values.
51
+ */
52
+ elements: number[];
53
+
54
+ /**
55
+ * Constructs a new 4x4 matrix. The arguments are supposed to be
56
+ * in row-major order. If no arguments are provided, the constructor
57
+ * initializes the matrix as an identity matrix.
58
+ *
59
+ * @param n11 - 1-1 matrix element.
60
+ * @param n12 - 1-2 matrix element.
61
+ * @param n13 - 1-3 matrix element.
62
+ * @param n14 - 1-4 matrix element.
63
+ * @param n21 - 2-1 matrix element.
64
+ * @param n22 - 2-2 matrix element.
65
+ * @param n23 - 2-3 matrix element.
66
+ * @param n24 - 2-4 matrix element.
67
+ * @param n31 - 3-1 matrix element.
68
+ * @param n32 - 3-2 matrix element.
69
+ * @param n33 - 3-3 matrix element.
70
+ * @param n34 - 3-4 matrix element.
71
+ * @param n41 - 4-1 matrix element.
72
+ * @param n42 - 4-2 matrix element.
73
+ * @param n43 - 4-3 matrix element.
74
+ * @param n44 - 4-4 matrix element.
75
+ */
76
+ constructor(
77
+ n11?: number, n12?: number, n13?: number, n14?: number,
78
+ n21?: number, n22?: number, n23?: number, n24?: number,
79
+ n31?: number, n32?: number, n33?: number, n34?: number,
80
+ n41?: number, n42?: number, n43?: number, n44?: number
81
+ ) {
82
+ this.elements = [
83
+ 1, 0, 0, 0,
84
+ 0, 1, 0, 0,
85
+ 0, 0, 1, 0,
86
+ 0, 0, 0, 1
87
+ ];
88
+
89
+ if (n11 !== undefined) {
90
+ this.set(n11, n12!, n13!, n14!, n21!, n22!, n23!, n24!, n31!, n32!, n33!, n34!, n41!, n42!, n43!, n44!);
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Sets the elements of the matrix. The arguments are supposed to be
96
+ * in row-major order.
97
+ *
98
+ * @param n11 - 1-1 matrix element.
99
+ * @param n12 - 1-2 matrix element.
100
+ * @param n13 - 1-3 matrix element.
101
+ * @param n14 - 1-4 matrix element.
102
+ * @param n21 - 2-1 matrix element.
103
+ * @param n22 - 2-2 matrix element.
104
+ * @param n23 - 2-3 matrix element.
105
+ * @param n24 - 2-4 matrix element.
106
+ * @param n31 - 3-1 matrix element.
107
+ * @param n32 - 3-2 matrix element.
108
+ * @param n33 - 3-3 matrix element.
109
+ * @param n34 - 3-4 matrix element.
110
+ * @param n41 - 4-1 matrix element.
111
+ * @param n42 - 4-2 matrix element.
112
+ * @param n43 - 4-3 matrix element.
113
+ * @param n44 - 4-4 matrix element.
114
+ * @return A reference to this matrix.
115
+ */
116
+ set(
117
+ n11: number, n12: number, n13: number, n14: number,
118
+ n21: number, n22: number, n23: number, n24: number,
119
+ n31: number, n32: number, n33: number, n34: number,
120
+ n41: number, n42: number, n43: number, n44: number
121
+ ): Matrix4 {
122
+ const te = this.elements;
123
+
124
+ te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
125
+ te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
126
+ te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
127
+ te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
128
+
129
+ return this;
130
+ }
131
+
132
+ /**
133
+ * Sets this matrix to the 4x4 identity matrix.
134
+ *
135
+ * @return A reference to this matrix.
136
+ */
137
+ identity(): Matrix4 {
138
+ this.set(
139
+ 1, 0, 0, 0,
140
+ 0, 1, 0, 0,
141
+ 0, 0, 1, 0,
142
+ 0, 0, 0, 1
143
+ );
144
+ return this;
145
+ }
146
+
147
+ /**
148
+ * Returns a matrix with copied values from this instance.
149
+ *
150
+ * @return A clone of this instance.
151
+ */
152
+ clone(): Matrix4 {
153
+ return new Matrix4().fromArray(this.elements);
154
+ }
155
+
156
+ /**
157
+ * Copies the values of the given matrix to this instance.
158
+ *
159
+ * @param m - The matrix to copy.
160
+ * @return A reference to this matrix.
161
+ */
162
+ copy(m: Matrix4): Matrix4 {
163
+ const te = this.elements;
164
+ const me = m.elements;
165
+
166
+ te[0] = me[0]; te[1] = me[1]; te[2] = me[2]; te[3] = me[3];
167
+ te[4] = me[4]; te[5] = me[5]; te[6] = me[6]; te[7] = me[7];
168
+ te[8] = me[8]; te[9] = me[9]; te[10] = me[10]; te[11] = me[11];
169
+ te[12] = me[12]; te[13] = me[13]; te[14] = me[14]; te[15] = me[15];
170
+
171
+ return this;
172
+ }
173
+
174
+ /**
175
+ * Copies the translation component of the given matrix
176
+ * into this matrix's translation component.
177
+ *
178
+ * @param m - The matrix to copy the translation component.
179
+ * @return A reference to this matrix.
180
+ */
181
+ copyPosition(m: Matrix4): Matrix4 {
182
+ const te = this.elements, me = m.elements;
183
+
184
+ te[12] = me[12];
185
+ te[13] = me[13];
186
+ te[14] = me[14];
187
+
188
+ return this;
189
+ }
190
+
191
+ /**
192
+ * Extracts the basis of this matrix into the three axis vectors provided.
193
+ *
194
+ * @param xAxis - The basis's x axis.
195
+ * @param yAxis - The basis's y axis.
196
+ * @param zAxis - The basis's z axis.
197
+ * @return A reference to this matrix.
198
+ */
199
+ extractBasis(xAxis: Vector3, yAxis: Vector3, zAxis: Vector3): Matrix4 {
200
+ xAxis.setFromMatrixColumn(this, 0);
201
+ yAxis.setFromMatrixColumn(this, 1);
202
+ zAxis.setFromMatrixColumn(this, 2);
203
+
204
+ return this;
205
+ }
206
+
207
+ /**
208
+ * Sets the given basis vectors to this matrix.
209
+ *
210
+ * @param xAxis - The basis's x axis.
211
+ * @param yAxis - The basis's y axis.
212
+ * @param zAxis - The basis's z axis.
213
+ * @return A reference to this matrix.
214
+ */
215
+ makeBasis(xAxis: Vector3, yAxis: Vector3, zAxis: Vector3): Matrix4 {
216
+ this.set(
217
+ xAxis.x, yAxis.x, zAxis.x, 0,
218
+ xAxis.y, yAxis.y, zAxis.y, 0,
219
+ xAxis.z, yAxis.z, zAxis.z, 0,
220
+ 0, 0, 0, 1
221
+ );
222
+ return this;
223
+ }
224
+
225
+ /**
226
+ * Extracts the rotation component of the given matrix
227
+ * into this matrix's rotation component.
228
+ *
229
+ * Note: This method does not support reflection matrices.
230
+ *
231
+ * @param m - The matrix.
232
+ * @return A reference to this matrix.
233
+ */
234
+ extractRotation(m: Matrix4): Matrix4 {
235
+ const te = this.elements;
236
+ const me = m.elements;
237
+
238
+ const scaleX = 1 / _v1.setFromMatrixColumn(m, 0).length();
239
+ const scaleY = 1 / _v1.setFromMatrixColumn(m, 1).length();
240
+ const scaleZ = 1 / _v1.setFromMatrixColumn(m, 2).length();
241
+
242
+ te[0] = me[0] * scaleX;
243
+ te[1] = me[1] * scaleX;
244
+ te[2] = me[2] * scaleX;
245
+ te[3] = 0;
246
+
247
+ te[4] = me[4] * scaleY;
248
+ te[5] = me[5] * scaleY;
249
+ te[6] = me[6] * scaleY;
250
+ te[7] = 0;
251
+
252
+ te[8] = me[8] * scaleZ;
253
+ te[9] = me[9] * scaleZ;
254
+ te[10] = me[10] * scaleZ;
255
+ te[11] = 0;
256
+
257
+ te[12] = 0;
258
+ te[13] = 0;
259
+ te[14] = 0;
260
+ te[15] = 1;
261
+
262
+ return this;
263
+ }
264
+
265
+ /**
266
+ * Sets the rotation component (the upper left 3x3 matrix) of this matrix to
267
+ * the rotation specified by the given Euler angles. The rest of
268
+ * the matrix is set to the identity. Depending on the {@link Euler#order},
269
+ * there are six possible outcomes. See [this page]{@link https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix}
270
+ * for a complete list.
271
+ *
272
+ * @param euler - The Euler angles.
273
+ * @return A reference to this matrix.
274
+ */
275
+ makeRotationFromEuler(euler: Euler): Matrix4 {
276
+ const te = this.elements;
277
+
278
+ const x = euler.x, y = euler.y, z = euler.z;
279
+ const a = Math.cos(x), b = Math.sin(x);
280
+ const c = Math.cos(y), d = Math.sin(y);
281
+ const e = Math.cos(z), f = Math.sin(z);
282
+
283
+ if (euler.order === 'XYZ') {
284
+ const ae = a * e, af = a * f, be = b * e, bf = b * f;
285
+
286
+ te[0] = c * e;
287
+ te[4] = -c * f;
288
+ te[8] = d;
289
+
290
+ te[1] = af + be * d;
291
+ te[5] = ae - bf * d;
292
+ te[9] = -b * c;
293
+
294
+ te[2] = bf - ae * d;
295
+ te[6] = be + af * d;
296
+ te[10] = a * c;
297
+ } else if (euler.order === 'YXZ') {
298
+ const ce = c * e, cf = c * f, de = d * e, df = d * f;
299
+
300
+ te[0] = ce + df * b;
301
+ te[4] = de * b - cf;
302
+ te[8] = a * d;
303
+
304
+ te[1] = a * f;
305
+ te[5] = a * e;
306
+ te[9] = -b;
307
+
308
+ te[2] = cf * b - de;
309
+ te[6] = df + ce * b;
310
+ te[10] = a * c;
311
+ } else if (euler.order === 'ZXY') {
312
+ const ce = c * e, cf = c * f, de = d * e, df = d * f;
313
+
314
+ te[0] = ce - df * b;
315
+ te[4] = -a * f;
316
+ te[8] = de + cf * b;
317
+
318
+ te[1] = cf + de * b;
319
+ te[5] = a * e;
320
+ te[9] = df - ce * b;
321
+
322
+ te[2] = -a * d;
323
+ te[6] = b;
324
+ te[10] = a * c;
325
+ } else if (euler.order === 'ZYX') {
326
+ const ae = a * e, af = a * f, be = b * e, bf = b * f;
327
+
328
+ te[0] = c * e;
329
+ te[4] = be * d - af;
330
+ te[8] = ae * d + bf;
331
+
332
+ te[1] = c * f;
333
+ te[5] = bf * d + ae;
334
+ te[9] = af * d - be;
335
+
336
+ te[2] = -d;
337
+ te[6] = b * c;
338
+ te[10] = a * c;
339
+ } else if (euler.order === 'YZX') {
340
+ const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
341
+
342
+ te[0] = c * e;
343
+ te[4] = bd - ac * f;
344
+ te[8] = bc * f + ad;
345
+
346
+ te[1] = f;
347
+ te[5] = a * e;
348
+ te[9] = -b * e;
349
+
350
+ te[2] = -d * e;
351
+ te[6] = ad * f + bc;
352
+ te[10] = ac - bd * f;
353
+ } else if (euler.order === 'XZY') {
354
+ const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
355
+
356
+ te[0] = c * e;
357
+ te[4] = -f;
358
+ te[8] = d * e;
359
+
360
+ te[1] = ac * f + bd;
361
+ te[5] = a * e;
362
+ te[9] = ad * f - bc;
363
+
364
+ te[2] = bc * f - ad;
365
+ te[6] = b * e;
366
+ te[10] = bd * f + ac;
367
+ }
368
+
369
+ // bottom row
370
+ te[3] = 0;
371
+ te[7] = 0;
372
+ te[11] = 0;
373
+
374
+ // last column
375
+ te[12] = 0;
376
+ te[13] = 0;
377
+ te[14] = 0;
378
+ te[15] = 1;
379
+
380
+ return this;
381
+ }
382
+
383
+ /**
384
+ * Sets the rotation component of this matrix to the rotation specified by
385
+ * the given Quaternion as outlined [here]{@link https://en.wikipedia.org/wiki/Rotation_matrix#Quaternion}
386
+ * The rest of the matrix is set to the identity.
387
+ *
388
+ * @param q - The Quaternion.
389
+ * @return A reference to this matrix.
390
+ */
391
+ makeRotationFromQuaternion(q: Quaternion): Matrix4 {
392
+ return this.compose(_zero, q, _one);
393
+ }
394
+
395
+ /**
396
+ * Sets the rotation component of the transformation matrix, looking from `eye` towards
397
+ * `target`, and oriented by the up-direction.
398
+ *
399
+ * @param eye - The eye vector.
400
+ * @param target - The target vector.
401
+ * @param up - The up vector.
402
+ * @return A reference to this matrix.
403
+ */
404
+ lookAt(eye: Vector3, target: Vector3, up: Vector3): Matrix4 {
405
+ const te = this.elements;
406
+
407
+ _z.subVectors(eye, target);
408
+
409
+ if (_z.lengthSq() === 0) {
410
+ // eye and target are in the same position
411
+ _z.z = 1;
412
+ }
413
+
414
+ _z.normalize();
415
+ _x.crossVectors(up, _z);
416
+
417
+ if (_x.lengthSq() === 0) {
418
+ // up and z are parallel
419
+ if (Math.abs(up.z) === 1) {
420
+ _z.x += 0.0001;
421
+ } else {
422
+ _z.z += 0.0001;
423
+ }
424
+
425
+ _z.normalize();
426
+ _x.crossVectors(up, _z);
427
+ }
428
+
429
+ _x.normalize();
430
+ _y.crossVectors(_z, _x);
431
+
432
+ te[0] = _x.x; te[4] = _y.x; te[8] = _z.x;
433
+ te[1] = _x.y; te[5] = _y.y; te[9] = _z.y;
434
+ te[2] = _x.z; te[6] = _y.z; te[10] = _z.z;
435
+
436
+ return this;
437
+ }
438
+
439
+ /**
440
+ * Post-multiplies this matrix by the given 4x4 matrix.
441
+ *
442
+ * @param m - The matrix to multiply with.
443
+ * @return A reference to this matrix.
444
+ */
445
+ multiply(m: Matrix4): Matrix4 {
446
+ return this.multiplyMatrices(this, m);
447
+ }
448
+
449
+ /**
450
+ * Pre-multiplies this matrix by the given 4x4 matrix.
451
+ *
452
+ * @param m - The matrix to multiply with.
453
+ * @return A reference to this matrix.
454
+ */
455
+ premultiply(m: Matrix4): Matrix4 {
456
+ return this.multiplyMatrices(m, this);
457
+ }
458
+
459
+ /**
460
+ * Multiples the given 4x4 matrices and stores the result
461
+ * in this matrix.
462
+ *
463
+ * @param a - The first matrix.
464
+ * @param b - The second matrix.
465
+ * @return A reference to this matrix.
466
+ */
467
+ multiplyMatrices(a: Matrix4, b: Matrix4): Matrix4 {
468
+ const ae = a.elements;
469
+ const be = b.elements;
470
+ const te = this.elements;
471
+
472
+ const a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
473
+ const a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
474
+ const a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
475
+ const a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
476
+
477
+ const b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
478
+ const b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
479
+ const b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
480
+ const b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
481
+
482
+ te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
483
+ te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
484
+ te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
485
+ te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
486
+
487
+ te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
488
+ te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
489
+ te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
490
+ te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
491
+
492
+ te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
493
+ te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
494
+ te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
495
+ te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
496
+
497
+ te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
498
+ te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
499
+ te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
500
+ te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
501
+
502
+ return this;
503
+ }
504
+
505
+ /**
506
+ * Multiplies every component of the matrix by the given scalar.
507
+ *
508
+ * @param s - The scalar.
509
+ * @return A reference to this matrix.
510
+ */
511
+ multiplyScalar(s: number): Matrix4 {
512
+ const te = this.elements;
513
+
514
+ te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s;
515
+ te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s;
516
+ te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s;
517
+ te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s;
518
+
519
+ return this;
520
+ }
521
+
522
+ /**
523
+ * Computes and returns the determinant of this matrix.
524
+ *
525
+ * Based on the method outlined [here]{@link http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.html}.
526
+ *
527
+ * @return The determinant.
528
+ */
529
+ determinant(): number {
530
+ const te = this.elements;
531
+
532
+ const n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
533
+ const n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
534
+ const n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
535
+ const n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
536
+
537
+ return (
538
+ n41 * (
539
+ + n14 * n23 * n32
540
+ - n13 * n24 * n32
541
+ - n14 * n22 * n33
542
+ + n12 * n24 * n33
543
+ + n13 * n22 * n34
544
+ - n12 * n23 * n34
545
+ ) +
546
+ n42 * (
547
+ + n11 * n23 * n34
548
+ - n11 * n24 * n33
549
+ + n14 * n21 * n33
550
+ - n13 * n21 * n34
551
+ + n13 * n24 * n31
552
+ - n14 * n23 * n31
553
+ ) +
554
+ n43 * (
555
+ + n11 * n24 * n32
556
+ - n11 * n22 * n34
557
+ - n14 * n21 * n32
558
+ + n12 * n21 * n34
559
+ + n14 * n22 * n31
560
+ - n12 * n24 * n31
561
+ ) +
562
+ n44 * (
563
+ - n13 * n22 * n31
564
+ - n11 * n23 * n32
565
+ + n11 * n22 * n33
566
+ + n13 * n21 * n32
567
+ - n12 * n21 * n33
568
+ + n12 * n23 * n31
569
+ )
570
+ );
571
+ }
572
+
573
+ /**
574
+ * Transposes this matrix in place.
575
+ *
576
+ * @return A reference to this matrix.
577
+ */
578
+ transpose(): Matrix4 {
579
+ const te = this.elements;
580
+ let tmp: number;
581
+
582
+ tmp = te[1]; te[1] = te[4]; te[4] = tmp;
583
+ tmp = te[2]; te[2] = te[8]; te[8] = tmp;
584
+ tmp = te[6]; te[6] = te[9]; te[9] = tmp;
585
+
586
+ tmp = te[3]; te[3] = te[12]; te[12] = tmp;
587
+ tmp = te[7]; te[7] = te[13]; te[13] = tmp;
588
+ tmp = te[11]; te[11] = te[14]; te[14] = tmp;
589
+
590
+ return this;
591
+ }
592
+
593
+ /**
594
+ * Sets the position component for this matrix from the given vector,
595
+ * without affecting the rest of the matrix.
596
+ *
597
+ * @param x - The x component of the vector or alternatively the vector object.
598
+ * @param y - The y component of the vector.
599
+ * @param z - The z component of the vector.
600
+ * @return A reference to this matrix.
601
+ */
602
+ setPosition(x: number | Vector3, y?: number, z?: number): Matrix4 {
603
+ const te = this.elements;
604
+
605
+ if (typeof x === 'number') {
606
+ te[12] = x;
607
+ te[13] = y!;
608
+ te[14] = z!;
609
+ } else {
610
+ te[12] = x.x;
611
+ te[13] = x.y;
612
+ te[14] = x.z;
613
+ }
614
+
615
+ return this;
616
+ }
617
+
618
+ /**
619
+ * Inverts this matrix, using the [analytic method]{@link https://en.wikipedia.org/wiki/Invertible_matrix#Analytic_solution}.
620
+ * You can not invert with a determinant of zero. If you attempt this, the method produces
621
+ * a zero matrix instead.
622
+ *
623
+ * @return A reference to this matrix.
624
+ */
625
+ invert(): Matrix4 {
626
+ // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
627
+ const te = this.elements;
628
+
629
+ const n11 = te[0], n21 = te[1], n31 = te[2], n41 = te[3];
630
+ const n12 = te[4], n22 = te[5], n32 = te[6], n42 = te[7];
631
+ const n13 = te[8], n23 = te[9], n33 = te[10], n43 = te[11];
632
+ const n14 = te[12], n24 = te[13], n34 = te[14], n44 = te[15];
633
+
634
+ const t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44;
635
+ const t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44;
636
+ const t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44;
637
+ const t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
638
+
639
+ const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
640
+
641
+ if (det === 0) return this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
642
+
643
+ const detInv = 1 / det;
644
+
645
+ te[0] = t11 * detInv;
646
+ te[1] = (n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44) * detInv;
647
+ te[2] = (n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44) * detInv;
648
+ te[3] = (n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43) * detInv;
649
+
650
+ te[4] = t12 * detInv;
651
+ te[5] = (n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44) * detInv;
652
+ te[6] = (n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44) * detInv;
653
+ te[7] = (n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43) * detInv;
654
+
655
+ te[8] = t13 * detInv;
656
+ te[9] = (n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44) * detInv;
657
+ te[10] = (n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44) * detInv;
658
+ te[11] = (n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43) * detInv;
659
+
660
+ te[12] = t14 * detInv;
661
+ te[13] = (n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34) * detInv;
662
+ te[14] = (n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34) * detInv;
663
+ te[15] = (n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33) * detInv;
664
+
665
+ return this;
666
+ }
667
+
668
+ /**
669
+ * Multiplies the columns of this matrix by the given vector.
670
+ *
671
+ * @param v - The scale vector.
672
+ * @return A reference to this matrix.
673
+ */
674
+ scale(v: Vector3): Matrix4 {
675
+ const te = this.elements;
676
+ const x = v.x, y = v.y, z = v.z;
677
+
678
+ te[0] *= x; te[4] *= y; te[8] *= z;
679
+ te[1] *= x; te[5] *= y; te[9] *= z;
680
+ te[2] *= x; te[6] *= y; te[10] *= z;
681
+ te[3] *= x; te[7] *= y; te[11] *= z;
682
+
683
+ return this;
684
+ }
685
+
686
+ /**
687
+ * Gets the maximum scale value of the three axes.
688
+ *
689
+ * @return The maximum scale.
690
+ */
691
+ getMaxScaleOnAxis(): number {
692
+ const te = this.elements;
693
+
694
+ const scaleXSq = te[0] * te[0] + te[1] * te[1] + te[2] * te[2];
695
+ const scaleYSq = te[4] * te[4] + te[5] * te[5] + te[6] * te[6];
696
+ const scaleZSq = te[8] * te[8] + te[9] * te[9] + te[10] * te[10];
697
+
698
+ return Math.sqrt(Math.max(scaleXSq, scaleYSq, scaleZSq));
699
+ }
700
+
701
+ /**
702
+ * Sets this matrix as a translation transform from the given vector.
703
+ *
704
+ * @param x - The amount to translate in the X axis or alternatively a translation vector.
705
+ * @param y - The amount to translate in the Y axis.
706
+ * @param z - The amount to translate in the z axis.
707
+ * @return A reference to this matrix.
708
+ */
709
+ makeTranslation(x: number | Vector3, y?: number, z?: number): Matrix4 {
710
+ if (typeof x === 'number') {
711
+ this.set(
712
+ 1, 0, 0, x,
713
+ 0, 1, 0, y!,
714
+ 0, 0, 1, z!,
715
+ 0, 0, 0, 1
716
+ );
717
+ } else {
718
+ this.set(
719
+ 1, 0, 0, x.x,
720
+ 0, 1, 0, x.y,
721
+ 0, 0, 1, x.z,
722
+ 0, 0, 0, 1
723
+ );
724
+ }
725
+
726
+ return this;
727
+ }
728
+
729
+ /**
730
+ * Sets this matrix as a rotational transformation around the X axis by
731
+ * the given angle.
732
+ *
733
+ * @param theta - The rotation in radians.
734
+ * @return A reference to this matrix.
735
+ */
736
+ makeRotationX(theta: number): Matrix4 {
737
+ const c = Math.cos(theta), s = Math.sin(theta);
738
+
739
+ this.set(
740
+ 1, 0, 0, 0,
741
+ 0, c, -s, 0,
742
+ 0, s, c, 0,
743
+ 0, 0, 0, 1
744
+ );
745
+
746
+ return this;
747
+ }
748
+
749
+ /**
750
+ * Sets this matrix as a rotational transformation around the Y axis by
751
+ * the given angle.
752
+ *
753
+ * @param theta - The rotation in radians.
754
+ * @return A reference to this matrix.
755
+ */
756
+ makeRotationY(theta: number): Matrix4 {
757
+ const c = Math.cos(theta), s = Math.sin(theta);
758
+
759
+ this.set(
760
+ c, 0, s, 0,
761
+ 0, 1, 0, 0,
762
+ -s, 0, c, 0,
763
+ 0, 0, 0, 1
764
+ );
765
+
766
+ return this;
767
+ }
768
+
769
+ /**
770
+ * Sets this matrix as a rotational transformation around the Z axis by
771
+ * the given angle.
772
+ *
773
+ * @param theta - The rotation in radians.
774
+ * @return A reference to this matrix.
775
+ */
776
+ makeRotationZ(theta: number): Matrix4 {
777
+ const c = Math.cos(theta), s = Math.sin(theta);
778
+
779
+ this.set(
780
+ c, -s, 0, 0,
781
+ s, c, 0, 0,
782
+ 0, 0, 1, 0,
783
+ 0, 0, 0, 1
784
+ );
785
+
786
+ return this;
787
+ }
788
+
789
+ /**
790
+ * Sets this matrix as a rotational transformation around the given axis by
791
+ * the given angle.
792
+ *
793
+ * This is a somewhat controversial but mathematically sound alternative to
794
+ * rotating via Quaternions. See the discussion [here]{@link https://www.gamedev.net/articles/programming/math-and-physics/do-we-really-need-quaternions-r1199}.
795
+ *
796
+ * @param axis - The normalized rotation axis.
797
+ * @param angle - The rotation in radians.
798
+ * @return A reference to this matrix.
799
+ */
800
+ makeRotationAxis(axis: Vector3, angle: number): Matrix4 {
801
+ // Based on http://www.gamedev.net/reference/articles/article1199.asp
802
+ const c = Math.cos(angle);
803
+ const s = Math.sin(angle);
804
+ const t = 1 - c;
805
+ const x = axis.x, y = axis.y, z = axis.z;
806
+ const tx = t * x, ty = t * y;
807
+
808
+ this.set(
809
+ tx * x + c, tx * y - s * z, tx * z + s * y, 0,
810
+ tx * y + s * z, ty * y + c, ty * z - s * x, 0,
811
+ tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
812
+ 0, 0, 0, 1
813
+ );
814
+
815
+ return this;
816
+ }
817
+
818
+ /**
819
+ * Sets this matrix as a scale transformation.
820
+ *
821
+ * @param x - The amount to scale in the X axis.
822
+ * @param y - The amount to scale in the Y axis.
823
+ * @param z - The amount to scale in the Z axis.
824
+ * @return A reference to this matrix.
825
+ */
826
+ makeScale(x: number, y: number, z: number): Matrix4 {
827
+ this.set(
828
+ x, 0, 0, 0,
829
+ 0, y, 0, 0,
830
+ 0, 0, z, 0,
831
+ 0, 0, 0, 1
832
+ );
833
+
834
+ return this;
835
+ }
836
+
837
+ /**
838
+ * Sets this matrix as a shear transformation.
839
+ *
840
+ * @param xy - The amount to shear X by Y.
841
+ * @param xz - The amount to shear X by Z.
842
+ * @param yx - The amount to shear Y by X.
843
+ * @param yz - The amount to shear Y by Z.
844
+ * @param zx - The amount to shear Z by X.
845
+ * @param zy - The amount to shear Z by Y.
846
+ * @return A reference to this matrix.
847
+ */
848
+ makeShear(xy: number, xz: number, yx: number, yz: number, zx: number, zy: number): Matrix4 {
849
+ this.set(
850
+ 1, yx, zx, 0,
851
+ xy, 1, zy, 0,
852
+ xz, yz, 1, 0,
853
+ 0, 0, 0, 1
854
+ );
855
+
856
+ return this;
857
+ }
858
+
859
+ /**
860
+ * Sets this matrix to the transformation composed of the given position,
861
+ * rotation (Quaternion) and scale.
862
+ *
863
+ * @param position - The position vector.
864
+ * @param quaternion - The rotation as a Quaternion.
865
+ * @param scale - The scale vector.
866
+ * @return A reference to this matrix.
867
+ */
868
+ compose(position: Vector3, quaternion: Quaternion, scale: Vector3): Matrix4 {
869
+ const te = this.elements;
870
+
871
+ const x = quaternion.x, y = quaternion.y, z = quaternion.z, w = quaternion.w;
872
+ const x2 = x + x, y2 = y + y, z2 = z + z;
873
+ const xx = x * x2, xy = x * y2, xz = x * z2;
874
+ const yy = y * y2, yz = y * z2, zz = z * z2;
875
+ const wx = w * x2, wy = w * y2, wz = w * z2;
876
+
877
+ const sx = scale.x, sy = scale.y, sz = scale.z;
878
+
879
+ te[0] = (1 - (yy + zz)) * sx;
880
+ te[1] = (xy + wz) * sx;
881
+ te[2] = (xz - wy) * sx;
882
+ te[3] = 0;
883
+
884
+ te[4] = (xy - wz) * sy;
885
+ te[5] = (1 - (xx + zz)) * sy;
886
+ te[6] = (yz + wx) * sy;
887
+ te[7] = 0;
888
+
889
+ te[8] = (xz + wy) * sz;
890
+ te[9] = (yz - wx) * sz;
891
+ te[10] = (1 - (xx + yy)) * sz;
892
+ te[11] = 0;
893
+
894
+ te[12] = position.x;
895
+ te[13] = position.y;
896
+ te[14] = position.z;
897
+ te[15] = 1;
898
+
899
+ return this;
900
+ }
901
+
902
+ /**
903
+ * Decomposes this matrix into its position, rotation and scale components
904
+ * and provides the result in the given objects.
905
+ *
906
+ * Note: Not all matrices are decomposable in this way. For example, if an
907
+ * object has a non-uniformly scaled parent, then the object's world matrix
908
+ * may not be decomposable, and this method may not be appropriate.
909
+ *
910
+ * @param position - The position vector.
911
+ * @param quaternion - The rotation as a Quaternion.
912
+ * @param scale - The scale vector.
913
+ * @return A reference to this matrix.
914
+ */
915
+ decompose(position: Vector3, quaternion: Quaternion, scale: Vector3): Matrix4 {
916
+ const te = this.elements;
917
+
918
+ let sx = _v1.set(te[0], te[1], te[2]).length();
919
+ const sy = _v1.set(te[4], te[5], te[6]).length();
920
+ const sz = _v1.set(te[8], te[9], te[10]).length();
921
+
922
+ // if determine is negative, we need to invert one scale
923
+ const det = this.determinant();
924
+ if (det < 0) sx = -sx;
925
+
926
+ position.x = te[12];
927
+ position.y = te[13];
928
+ position.z = te[14];
929
+
930
+ // scale the rotation part
931
+ _m1.copy(this);
932
+
933
+ const invSX = 1 / sx;
934
+ const invSY = 1 / sy;
935
+ const invSZ = 1 / sz;
936
+
937
+ _m1.elements[0] *= invSX;
938
+ _m1.elements[1] *= invSX;
939
+ _m1.elements[2] *= invSX;
940
+
941
+ _m1.elements[4] *= invSY;
942
+ _m1.elements[5] *= invSY;
943
+ _m1.elements[6] *= invSY;
944
+
945
+ _m1.elements[8] *= invSZ;
946
+ _m1.elements[9] *= invSZ;
947
+ _m1.elements[10] *= invSZ;
948
+
949
+ quaternion.setFromRotationMatrix(_m1);
950
+
951
+ scale.x = sx;
952
+ scale.y = sy;
953
+ scale.z = sz;
954
+
955
+ return this;
956
+ }
957
+
958
+ /**
959
+ * Creates a perspective projection matrix. This is used internally by
960
+ * {@link PerspectiveCamera#updateProjectionMatrix}.
961
+ *
962
+ * @param left - Left boundary of the viewing frustum at the near plane.
963
+ * @param right - Right boundary of the viewing frustum at the near plane.
964
+ * @param top - Top boundary of the viewing frustum at the near plane.
965
+ * @param bottom - Bottom boundary of the viewing frustum at the near plane.
966
+ * @param near - The distance from the camera to the near plane.
967
+ * @param far - The distance from the camera to the far plane.
968
+ * @param coordinateSystem - The coordinate system.
969
+ * @return A reference to this matrix.
970
+ */
971
+ makePerspective(
972
+ left: number, right: number, top: number, bottom: number,
973
+ near: number, far: number, coordinateSystem: CoordinateSystem = WebGLCoordinateSystem
974
+ ): Matrix4 {
975
+ const te = this.elements;
976
+ const x = 2 * near / (right - left);
977
+ const y = 2 * near / (top - bottom);
978
+
979
+ const a = (right + left) / (right - left);
980
+ const b = (top + bottom) / (top - bottom);
981
+
982
+ let c: number, d: number;
983
+
984
+ if (coordinateSystem === WebGLCoordinateSystem) {
985
+ c = -(far + near) / (far - near);
986
+ d = (-2 * far * near) / (far - near);
987
+ } else if (coordinateSystem === WebGPUCoordinateSystem) {
988
+ c = -far / (far - near);
989
+ d = (-far * near) / (far - near);
990
+ } else {
991
+ throw new Error('Matrix4.makePerspective(): Invalid coordinate system: ' + coordinateSystem);
992
+ }
993
+
994
+ te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
995
+ te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
996
+ te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
997
+ te[3] = 0; te[7] = 0; te[11] = -1; te[15] = 0;
998
+
999
+ return this;
1000
+ }
1001
+
1002
+ /**
1003
+ * Creates a orthographic projection matrix. This is used internally by
1004
+ * {@link OrthographicCamera#updateProjectionMatrix}.
1005
+ *
1006
+ * @param left - Left boundary of the viewing frustum at the near plane.
1007
+ * @param right - Right boundary of the viewing frustum at the near plane.
1008
+ * @param top - Top boundary of the viewing frustum at the near plane.
1009
+ * @param bottom - Bottom boundary of the viewing frustum at the near plane.
1010
+ * @param near - The distance from the camera to the near plane.
1011
+ * @param far - The distance from the camera to the far plane.
1012
+ * @param coordinateSystem - The coordinate system.
1013
+ * @return A reference to this matrix.
1014
+ */
1015
+ makeOrthographic(
1016
+ left: number, right: number, top: number, bottom: number,
1017
+ near: number, far: number, coordinateSystem: CoordinateSystem = WebGLCoordinateSystem
1018
+ ): Matrix4 {
1019
+ const te = this.elements;
1020
+ const w = 1.0 / (right - left);
1021
+ const h = 1.0 / (top - bottom);
1022
+ const p = 1.0 / (far - near);
1023
+
1024
+ const x = (right + left) * w;
1025
+ const y = (top + bottom) * h;
1026
+
1027
+ let z: number, zInv: number;
1028
+
1029
+ if (coordinateSystem === WebGLCoordinateSystem) {
1030
+ z = (far + near) * p;
1031
+ zInv = -2 * p;
1032
+ } else if (coordinateSystem === WebGPUCoordinateSystem) {
1033
+ z = near * p;
1034
+ zInv = -1 * p;
1035
+ } else {
1036
+ throw new Error('Matrix4.makeOrthographic(): Invalid coordinate system: ' + coordinateSystem);
1037
+ }
1038
+
1039
+ te[0] = 2 * w; te[4] = 0; te[8] = 0; te[12] = -x;
1040
+ te[1] = 0; te[5] = 2 * h; te[9] = 0; te[13] = -y;
1041
+ te[2] = 0; te[6] = 0; te[10] = zInv; te[14] = -z;
1042
+ te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1;
1043
+
1044
+ return this;
1045
+ }
1046
+
1047
+ /**
1048
+ * Returns `true` if this matrix is equal with the given one.
1049
+ *
1050
+ * @param matrix - The matrix to test for equality.
1051
+ * @return Whether this matrix is equal with the given one.
1052
+ */
1053
+ equals(matrix: Matrix4): boolean {
1054
+ const te = this.elements;
1055
+ const me = matrix.elements;
1056
+
1057
+ for (let i = 0; i < 16; i++) {
1058
+ if (te[i] !== me[i]) return false;
1059
+ }
1060
+
1061
+ return true;
1062
+ }
1063
+
1064
+ /**
1065
+ * Sets the elements of the matrix from the given array.
1066
+ *
1067
+ * @param array - The matrix elements in column-major order.
1068
+ * @param offset - Index of the first element in the array.
1069
+ * @return A reference to this matrix.
1070
+ */
1071
+ fromArray(array: number[], offset: number = 0): Matrix4 {
1072
+ for (let i = 0; i < 16; i++) {
1073
+ this.elements[i] = array[i + offset];
1074
+ }
1075
+
1076
+ return this;
1077
+ }
1078
+
1079
+ /**
1080
+ * Writes the elements of this matrix to the given array. If no array is provided,
1081
+ * the method returns a new instance.
1082
+ *
1083
+ * @param array - The target array holding the matrix elements in column-major order.
1084
+ * @param offset - Index of the first element in the array.
1085
+ * @return The matrix elements in column-major order.
1086
+ */
1087
+ toArray(array: number[] = [], offset: number = 0): number[] {
1088
+ const te = this.elements;
1089
+
1090
+ array[offset] = te[0];
1091
+ array[offset + 1] = te[1];
1092
+ array[offset + 2] = te[2];
1093
+ array[offset + 3] = te[3];
1094
+
1095
+ array[offset + 4] = te[4];
1096
+ array[offset + 5] = te[5];
1097
+ array[offset + 6] = te[6];
1098
+ array[offset + 7] = te[7];
1099
+
1100
+ array[offset + 8] = te[8];
1101
+ array[offset + 9] = te[9];
1102
+ array[offset + 10] = te[10];
1103
+ array[offset + 11] = te[11];
1104
+
1105
+ array[offset + 12] = te[12];
1106
+ array[offset + 13] = te[13];
1107
+ array[offset + 14] = te[14];
1108
+ array[offset + 15] = te[15];
1109
+
1110
+ return array;
1111
+ }
1112
+ }
1113
+
1114
+ // 全局辅助变量
1115
+ const _v1 = /*@__PURE__*/ new Vector3();
1116
+ const _m1 = /*@__PURE__*/ new Matrix4();
1117
+ const _zero = /*@__PURE__*/ new Vector3(0, 0, 0);
1118
+ const _one = /*@__PURE__*/ new Vector3(1, 1, 1);
1119
+ const _x = /*@__PURE__*/ new Vector3();
1120
+ const _y = /*@__PURE__*/ new Vector3();
1121
+ const _z = /*@__PURE__*/ new Vector3();
1122
+
1123
+ export { Matrix4 };