@geastack/gea3d 0.0.0-stage → 0.1.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.
@@ -0,0 +1,1228 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ // Typed port of the three.js math core (API-compatible subset).
3
+ // Semantics follow three.js: column-major Matrix4.elements, Euler/Quaternion
4
+ // kept in sync by their owning Object3D, right-handed coordinates.
5
+
6
+ export const DEG2RAD = Math.PI / 180
7
+
8
+ export function clamp(value: number, min: number, max: number): number {
9
+ return Math.max(min, Math.min(max, value))
10
+ }
11
+
12
+ export class Vector2 {
13
+ x: number
14
+ y: number
15
+
16
+ constructor(x: number = 0, y: number = 0) {
17
+ this.x = x
18
+ this.y = y
19
+ }
20
+
21
+ set(x: number, y: number): Vector2 {
22
+ this.x = x
23
+ this.y = y
24
+ return this
25
+ }
26
+
27
+ copy(v: Vector2): Vector2 {
28
+ this.x = v.x
29
+ this.y = v.y
30
+ return this
31
+ }
32
+
33
+ clone(): Vector2 {
34
+ return new Vector2(this.x, this.y)
35
+ }
36
+ }
37
+
38
+ export class Vector3 {
39
+ x: number
40
+ y: number
41
+ z: number
42
+
43
+ constructor(x: number = 0, y: number = 0, z: number = 0) {
44
+ this.x = x
45
+ this.y = y
46
+ this.z = z
47
+ }
48
+
49
+ set(x: number, y: number, z: number): Vector3 {
50
+ this.x = x
51
+ this.y = y
52
+ this.z = z
53
+ return this
54
+ }
55
+
56
+ setScalar(s: number): Vector3 {
57
+ this.x = s
58
+ this.y = s
59
+ this.z = s
60
+ return this
61
+ }
62
+
63
+ copy(v: Vector3): Vector3 {
64
+ this.x = v.x
65
+ this.y = v.y
66
+ this.z = v.z
67
+ return this
68
+ }
69
+
70
+ clone(): Vector3 {
71
+ return new Vector3(this.x, this.y, this.z)
72
+ }
73
+
74
+ add(v: Vector3): Vector3 {
75
+ this.x += v.x
76
+ this.y += v.y
77
+ this.z += v.z
78
+ return this
79
+ }
80
+
81
+ addVectors(a: Vector3, b: Vector3): Vector3 {
82
+ this.x = a.x + b.x
83
+ this.y = a.y + b.y
84
+ this.z = a.z + b.z
85
+ return this
86
+ }
87
+
88
+ addScaledVector(v: Vector3, s: number): Vector3 {
89
+ this.x += v.x * s
90
+ this.y += v.y * s
91
+ this.z += v.z * s
92
+ return this
93
+ }
94
+
95
+ sub(v: Vector3): Vector3 {
96
+ this.x -= v.x
97
+ this.y -= v.y
98
+ this.z -= v.z
99
+ return this
100
+ }
101
+
102
+ subVectors(a: Vector3, b: Vector3): Vector3 {
103
+ this.x = a.x - b.x
104
+ this.y = a.y - b.y
105
+ this.z = a.z - b.z
106
+ return this
107
+ }
108
+
109
+ multiplyScalar(s: number): Vector3 {
110
+ this.x *= s
111
+ this.y *= s
112
+ this.z *= s
113
+ return this
114
+ }
115
+
116
+ divideScalar(s: number): Vector3 {
117
+ return this.multiplyScalar(1 / s)
118
+ }
119
+
120
+ negate(): Vector3 {
121
+ return this.multiplyScalar(-1)
122
+ }
123
+
124
+ dot(v: Vector3): number {
125
+ return this.x * v.x + this.y * v.y + this.z * v.z
126
+ }
127
+
128
+ lengthSq(): number {
129
+ return this.x * this.x + this.y * this.y + this.z * this.z
130
+ }
131
+
132
+ length(): number {
133
+ return Math.sqrt(this.lengthSq())
134
+ }
135
+
136
+ normalize(): Vector3 {
137
+ const len = this.length()
138
+ return this.divideScalar(len > 0 ? len : 1)
139
+ }
140
+
141
+ cross(v: Vector3): Vector3 {
142
+ return this.crossVectors(this, v)
143
+ }
144
+
145
+ crossVectors(a: Vector3, b: Vector3): Vector3 {
146
+ const ax = a.x
147
+ const ay = a.y
148
+ const az = a.z
149
+ const bx = b.x
150
+ const by = b.y
151
+ const bz = b.z
152
+ this.x = ay * bz - az * by
153
+ this.y = az * bx - ax * bz
154
+ this.z = ax * by - ay * bx
155
+ return this
156
+ }
157
+
158
+ distanceTo(v: Vector3): number {
159
+ return Math.sqrt(this.distanceToSquared(v))
160
+ }
161
+
162
+ distanceToSquared(v: Vector3): number {
163
+ const dx = this.x - v.x
164
+ const dy = this.y - v.y
165
+ const dz = this.z - v.z
166
+ return dx * dx + dy * dy + dz * dz
167
+ }
168
+
169
+ lerp(v: Vector3, alpha: number): Vector3 {
170
+ this.x += (v.x - this.x) * alpha
171
+ this.y += (v.y - this.y) * alpha
172
+ this.z += (v.z - this.z) * alpha
173
+ return this
174
+ }
175
+
176
+ applyMatrix4(m: Matrix4): Vector3 {
177
+ const x = this.x
178
+ const y = this.y
179
+ const z = this.z
180
+ const e = m.elements
181
+ const w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15])
182
+ this.x = (e[0] * x + e[4] * y + e[8] * z + e[12]) * w
183
+ this.y = (e[1] * x + e[5] * y + e[9] * z + e[13]) * w
184
+ this.z = (e[2] * x + e[6] * y + e[10] * z + e[14]) * w
185
+ return this
186
+ }
187
+
188
+ applyQuaternion(q: Quaternion): Vector3 {
189
+ const vx = this.x
190
+ const vy = this.y
191
+ const vz = this.z
192
+ const qx = q.x
193
+ const qy = q.y
194
+ const qz = q.z
195
+ const qw = q.w
196
+ // t = 2 * cross(q.xyz, v)
197
+ const tx = 2 * (qy * vz - qz * vy)
198
+ const ty = 2 * (qz * vx - qx * vz)
199
+ const tz = 2 * (qx * vy - qy * vx)
200
+ // v + q.w * t + cross(q.xyz, t)
201
+ this.x = vx + qw * tx + qy * tz - qz * ty
202
+ this.y = vy + qw * ty + qz * tx - qx * tz
203
+ this.z = vz + qw * tz + qx * ty - qy * tx
204
+ return this
205
+ }
206
+
207
+ // Rotates by the upper 3x3 of m (no translation), then normalizes.
208
+ transformDirection(m: Matrix4): Vector3 {
209
+ const x = this.x
210
+ const y = this.y
211
+ const z = this.z
212
+ const e = m.elements
213
+ this.x = e[0] * x + e[4] * y + e[8] * z
214
+ this.y = e[1] * x + e[5] * y + e[9] * z
215
+ this.z = e[2] * x + e[6] * y + e[10] * z
216
+ return this.normalize()
217
+ }
218
+
219
+ setFromMatrixPosition(m: Matrix4): Vector3 {
220
+ const e = m.elements
221
+ this.x = e[12]
222
+ this.y = e[13]
223
+ this.z = e[14]
224
+ return this
225
+ }
226
+
227
+ setFromMatrixColumn(m: Matrix4, index: number): Vector3 {
228
+ const e = m.elements
229
+ const off = index * 4
230
+ this.x = e[off]
231
+ this.y = e[off + 1]
232
+ this.z = e[off + 2]
233
+ return this
234
+ }
235
+
236
+ fromArray(array: ArrayLike<number>, offset: number = 0): Vector3 {
237
+ this.x = array[offset]
238
+ this.y = array[offset + 1]
239
+ this.z = array[offset + 2]
240
+ return this
241
+ }
242
+ }
243
+
244
+ export class Vector4 {
245
+ x: number
246
+ y: number
247
+ z: number
248
+ w: number
249
+
250
+ constructor(x: number = 0, y: number = 0, z: number = 0, w: number = 1) {
251
+ this.x = x
252
+ this.y = y
253
+ this.z = z
254
+ this.w = w
255
+ }
256
+
257
+ set(x: number, y: number, z: number, w: number): Vector4 {
258
+ this.x = x
259
+ this.y = y
260
+ this.z = z
261
+ this.w = w
262
+ return this
263
+ }
264
+
265
+ copy(v: Vector4): Vector4 {
266
+ this.x = v.x
267
+ this.y = v.y
268
+ this.z = v.z
269
+ this.w = v.w
270
+ return this
271
+ }
272
+ }
273
+
274
+ // Quaternion and Euler notify their owner through two callbacks: onChange
275
+ // after every write and onRead before every read. Object3D links its pair
276
+ // lazily through them (a write only marks the other view stale; it is
277
+ // recomputed when read), so rotation writes cost no trigonometry.
278
+ export class Quaternion {
279
+ _x: number
280
+ _y: number
281
+ _z: number
282
+ _w: number
283
+ _onChangeCallback: () => void
284
+ _onReadCallback: () => void
285
+
286
+ constructor(x: number = 0, y: number = 0, z: number = 0, w: number = 1) {
287
+ this._x = x
288
+ this._y = y
289
+ this._z = z
290
+ this._w = w
291
+ this._onChangeCallback = () => {}
292
+ this._onReadCallback = () => {}
293
+ }
294
+
295
+ get x(): number {
296
+ this._onReadCallback()
297
+ return this._x
298
+ }
299
+
300
+ set x(value: number) {
301
+ this._onReadCallback()
302
+ this._x = value
303
+ this._onChangeCallback()
304
+ }
305
+
306
+ get y(): number {
307
+ this._onReadCallback()
308
+ return this._y
309
+ }
310
+
311
+ set y(value: number) {
312
+ this._onReadCallback()
313
+ this._y = value
314
+ this._onChangeCallback()
315
+ }
316
+
317
+ get z(): number {
318
+ this._onReadCallback()
319
+ return this._z
320
+ }
321
+
322
+ set z(value: number) {
323
+ this._onReadCallback()
324
+ this._z = value
325
+ this._onChangeCallback()
326
+ }
327
+
328
+ get w(): number {
329
+ this._onReadCallback()
330
+ return this._w
331
+ }
332
+
333
+ set w(value: number) {
334
+ this._onReadCallback()
335
+ this._w = value
336
+ this._onChangeCallback()
337
+ }
338
+
339
+ set(x: number, y: number, z: number, w: number): Quaternion {
340
+ this._x = x
341
+ this._y = y
342
+ this._z = z
343
+ this._w = w
344
+ this._onChangeCallback()
345
+ return this
346
+ }
347
+
348
+ copy(q: Quaternion): Quaternion {
349
+ q._onReadCallback()
350
+ return this.set(q._x, q._y, q._z, q._w)
351
+ }
352
+
353
+ clone(): Quaternion {
354
+ this._onReadCallback()
355
+ return new Quaternion(this._x, this._y, this._z, this._w)
356
+ }
357
+
358
+ identity(): Quaternion {
359
+ return this.set(0, 0, 0, 1)
360
+ }
361
+
362
+ setFromEuler(euler: Euler, update: boolean = true): Quaternion {
363
+ euler._onReadCallback()
364
+ const x = euler._x
365
+ const y = euler._y
366
+ const z = euler._z
367
+ const order = euler._order
368
+
369
+ const c1 = Math.cos(x / 2)
370
+ const c2 = Math.cos(y / 2)
371
+ const c3 = Math.cos(z / 2)
372
+ const s1 = Math.sin(x / 2)
373
+ const s2 = Math.sin(y / 2)
374
+ const s3 = Math.sin(z / 2)
375
+
376
+ if (order === 'XYZ') {
377
+ this._x = s1 * c2 * c3 + c1 * s2 * s3
378
+ this._y = c1 * s2 * c3 - s1 * c2 * s3
379
+ this._z = c1 * c2 * s3 + s1 * s2 * c3
380
+ this._w = c1 * c2 * c3 - s1 * s2 * s3
381
+ } else if (order === 'YXZ') {
382
+ this._x = s1 * c2 * c3 + c1 * s2 * s3
383
+ this._y = c1 * s2 * c3 - s1 * c2 * s3
384
+ this._z = c1 * c2 * s3 - s1 * s2 * c3
385
+ this._w = c1 * c2 * c3 + s1 * s2 * s3
386
+ } else if (order === 'ZXY') {
387
+ this._x = s1 * c2 * c3 - c1 * s2 * s3
388
+ this._y = c1 * s2 * c3 + s1 * c2 * s3
389
+ this._z = c1 * c2 * s3 + s1 * s2 * c3
390
+ this._w = c1 * c2 * c3 - s1 * s2 * s3
391
+ } else if (order === 'ZYX') {
392
+ this._x = s1 * c2 * c3 - c1 * s2 * s3
393
+ this._y = c1 * s2 * c3 + s1 * c2 * s3
394
+ this._z = c1 * c2 * s3 - s1 * s2 * c3
395
+ this._w = c1 * c2 * c3 + s1 * s2 * s3
396
+ } else if (order === 'YZX') {
397
+ this._x = s1 * c2 * c3 + c1 * s2 * s3
398
+ this._y = c1 * s2 * c3 + s1 * c2 * s3
399
+ this._z = c1 * c2 * s3 - s1 * s2 * c3
400
+ this._w = c1 * c2 * c3 - s1 * s2 * s3
401
+ } else if (order === 'XZY') {
402
+ this._x = s1 * c2 * c3 - c1 * s2 * s3
403
+ this._y = c1 * s2 * c3 - s1 * c2 * s3
404
+ this._z = c1 * c2 * s3 + s1 * s2 * c3
405
+ this._w = c1 * c2 * c3 + s1 * s2 * s3
406
+ }
407
+
408
+ if (update) this._onChangeCallback()
409
+ return this
410
+ }
411
+
412
+ setFromAxisAngle(axis: Vector3, angle: number): Quaternion {
413
+ const halfAngle = angle / 2
414
+ const s = Math.sin(halfAngle)
415
+ return this.set(axis.x * s, axis.y * s, axis.z * s, Math.cos(halfAngle))
416
+ }
417
+
418
+ setFromRotationMatrix(m: Matrix4): Quaternion {
419
+ // Assumes the upper 3x3 of m is a pure (unscaled) rotation.
420
+ const te = m.elements
421
+ return this.setFromRotationElements(te[0], te[4], te[8], te[1], te[5], te[9], te[2], te[6], te[10])
422
+ }
423
+
424
+ // Rotation from the rows of a pure 3x3 rotation matrix.
425
+ setFromRotationElements(
426
+ m11: number, m12: number, m13: number,
427
+ m21: number, m22: number, m23: number,
428
+ m31: number, m32: number, m33: number,
429
+ ): Quaternion {
430
+ const trace = m11 + m22 + m33
431
+
432
+ if (trace > 0) {
433
+ const s = 0.5 / Math.sqrt(trace + 1.0)
434
+ return this.set((m32 - m23) * s, (m13 - m31) * s, (m21 - m12) * s, 0.25 / s)
435
+ }
436
+ if (m11 > m22 && m11 > m33) {
437
+ const s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33)
438
+ return this.set(0.25 * s, (m12 + m21) / s, (m13 + m31) / s, (m32 - m23) / s)
439
+ }
440
+ if (m22 > m33) {
441
+ const s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33)
442
+ return this.set((m12 + m21) / s, 0.25 * s, (m23 + m32) / s, (m13 - m31) / s)
443
+ }
444
+ const s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22)
445
+ return this.set((m13 + m31) / s, (m23 + m32) / s, 0.25 * s, (m21 - m12) / s)
446
+ }
447
+
448
+ // Rotation whose -Z axis points from eye to target (three.js lookAt
449
+ // convention for cameras); swap eye and target for objects that face +Z.
450
+ setFromLookAt(
451
+ eyeX: number, eyeY: number, eyeZ: number,
452
+ targetX: number, targetY: number, targetZ: number,
453
+ upX: number, upY: number, upZ: number,
454
+ ): Quaternion {
455
+ let zx = eyeX - targetX
456
+ let zy = eyeY - targetY
457
+ let zz = eyeZ - targetZ
458
+ const zLength = Math.sqrt(zx * zx + zy * zy + zz * zz)
459
+ if (zLength === 0) {
460
+ zx = 0
461
+ zy = 0
462
+ zz = 1
463
+ } else {
464
+ zx /= zLength
465
+ zy /= zLength
466
+ zz /= zLength
467
+ }
468
+ let xx = upY * zz - upZ * zy
469
+ let xy = upZ * zx - upX * zz
470
+ let xz = upX * zy - upY * zx
471
+ let xLength = Math.sqrt(xx * xx + xy * xy + xz * xz)
472
+ if (xLength === 0) {
473
+ // up and z are parallel: nudge z.
474
+ if (Math.abs(upZ) === 1) zx += 0.0001
475
+ else zz += 0.0001
476
+ const inv = 1 / Math.sqrt(zx * zx + zy * zy + zz * zz)
477
+ zx *= inv
478
+ zy *= inv
479
+ zz *= inv
480
+ xx = upY * zz - upZ * zy
481
+ xy = upZ * zx - upX * zz
482
+ xz = upX * zy - upY * zx
483
+ xLength = Math.sqrt(xx * xx + xy * xy + xz * xz)
484
+ }
485
+ const inv = xLength > 0 ? 1 / xLength : 1
486
+ xx *= inv
487
+ xy *= inv
488
+ xz *= inv
489
+ const yx = zy * xz - zz * xy
490
+ const yy = zz * xx - zx * xz
491
+ const yz = zx * xy - zy * xx
492
+ return this.setFromRotationElements(xx, yx, zx, xy, yy, zy, xz, yz, zz)
493
+ }
494
+
495
+ multiply(q: Quaternion): Quaternion {
496
+ return this.multiplyQuaternions(this, q)
497
+ }
498
+
499
+ premultiply(q: Quaternion): Quaternion {
500
+ return this.multiplyQuaternions(q, this)
501
+ }
502
+
503
+ multiplyQuaternions(a: Quaternion, b: Quaternion): Quaternion {
504
+ a._onReadCallback()
505
+ b._onReadCallback()
506
+ return this.setProduct(a._x, a._y, a._z, a._w, b._x, b._y, b._z, b._w)
507
+ }
508
+
509
+ // this = this * (x, y, z, w), without a temporary quaternion.
510
+ multiplyComponents(x: number, y: number, z: number, w: number): Quaternion {
511
+ this._onReadCallback()
512
+ return this.setProduct(this._x, this._y, this._z, this._w, x, y, z, w)
513
+ }
514
+
515
+ private setProduct(
516
+ qax: number, qay: number, qaz: number, qaw: number,
517
+ qbx: number, qby: number, qbz: number, qbw: number,
518
+ ): Quaternion {
519
+ return this.set(
520
+ qax * qbw + qaw * qbx + qay * qbz - qaz * qby,
521
+ qay * qbw + qaw * qby + qaz * qbx - qax * qbz,
522
+ qaz * qbw + qaw * qbz + qax * qby - qay * qbx,
523
+ qaw * qbw - qax * qbx - qay * qby - qaz * qbz,
524
+ )
525
+ }
526
+
527
+ lengthSq(): number {
528
+ this._onReadCallback()
529
+ return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w
530
+ }
531
+
532
+ normalize(): Quaternion {
533
+ const l = Math.sqrt(this.lengthSq())
534
+ if (l === 0) return this.set(0, 0, 0, 1)
535
+
536
+ const inv = 1 / l
537
+ return this.set(this._x * inv, this._y * inv, this._z * inv, this._w * inv)
538
+ }
539
+
540
+ _onChange(callback: () => void): Quaternion {
541
+ this._onChangeCallback = callback
542
+ return this
543
+ }
544
+
545
+ _onRead(callback: () => void): Quaternion {
546
+ this._onReadCallback = callback
547
+ return this
548
+ }
549
+ }
550
+
551
+ export class Euler {
552
+ _x: number
553
+ _y: number
554
+ _z: number
555
+ _order: string
556
+ _onChangeCallback: () => void
557
+ _onReadCallback: () => void
558
+
559
+ constructor(x: number = 0, y: number = 0, z: number = 0, order: string = 'XYZ') {
560
+ this._x = x
561
+ this._y = y
562
+ this._z = z
563
+ this._order = order
564
+ this._onChangeCallback = () => {}
565
+ this._onReadCallback = () => {}
566
+ }
567
+
568
+ get x(): number {
569
+ this._onReadCallback()
570
+ return this._x
571
+ }
572
+
573
+ set x(value: number) {
574
+ this._onReadCallback()
575
+ this._x = value
576
+ this._onChangeCallback()
577
+ }
578
+
579
+ get y(): number {
580
+ this._onReadCallback()
581
+ return this._y
582
+ }
583
+
584
+ set y(value: number) {
585
+ this._onReadCallback()
586
+ this._y = value
587
+ this._onChangeCallback()
588
+ }
589
+
590
+ get z(): number {
591
+ this._onReadCallback()
592
+ return this._z
593
+ }
594
+
595
+ set z(value: number) {
596
+ this._onReadCallback()
597
+ this._z = value
598
+ this._onChangeCallback()
599
+ }
600
+
601
+ get order(): string {
602
+ this._onReadCallback()
603
+ return this._order
604
+ }
605
+
606
+ set order(value: string) {
607
+ this._onReadCallback()
608
+ this._order = value
609
+ this._onChangeCallback()
610
+ }
611
+
612
+ set(x: number, y: number, z: number, order: string = ''): Euler {
613
+ this._x = x
614
+ this._y = y
615
+ this._z = z
616
+ if (order !== '') this._order = order
617
+ this._onChangeCallback()
618
+ return this
619
+ }
620
+
621
+ copy(e: Euler): Euler {
622
+ e._onReadCallback()
623
+ return this.set(e._x, e._y, e._z, e._order)
624
+ }
625
+
626
+ clone(): Euler {
627
+ this._onReadCallback()
628
+ return new Euler(this._x, this._y, this._z, this._order)
629
+ }
630
+
631
+ setFromRotationMatrix(m: Matrix4, order: string = '', update: boolean = true): Euler {
632
+ // Assumes the upper 3x3 of m is a pure (unscaled) rotation.
633
+ const te = m.elements
634
+ return this.setFromRotationElements(te[0], te[4], te[8], te[1], te[5], te[9], te[2], te[6], te[10], order, update)
635
+ }
636
+
637
+ setFromRotationElements(
638
+ m11: number, m12: number, m13: number,
639
+ m21: number, m22: number, m23: number,
640
+ m31: number, m32: number, m33: number,
641
+ order: string = '', update: boolean = true,
642
+ ): Euler {
643
+ const ord = order !== '' ? order : this._order
644
+
645
+ if (ord === 'XYZ') {
646
+ this._y = Math.asin(clamp(m13, -1, 1))
647
+ if (Math.abs(m13) < 0.9999999) {
648
+ this._x = Math.atan2(-m23, m33)
649
+ this._z = Math.atan2(-m12, m11)
650
+ } else {
651
+ this._x = Math.atan2(m32, m22)
652
+ this._z = 0
653
+ }
654
+ } else if (ord === 'YXZ') {
655
+ this._x = Math.asin(-clamp(m23, -1, 1))
656
+ if (Math.abs(m23) < 0.9999999) {
657
+ this._y = Math.atan2(m13, m33)
658
+ this._z = Math.atan2(m21, m22)
659
+ } else {
660
+ this._y = Math.atan2(-m31, m11)
661
+ this._z = 0
662
+ }
663
+ } else if (ord === 'ZXY') {
664
+ this._x = Math.asin(clamp(m32, -1, 1))
665
+ if (Math.abs(m32) < 0.9999999) {
666
+ this._y = Math.atan2(-m31, m33)
667
+ this._z = Math.atan2(-m12, m22)
668
+ } else {
669
+ this._y = 0
670
+ this._z = Math.atan2(m21, m11)
671
+ }
672
+ } else if (ord === 'ZYX') {
673
+ this._y = Math.asin(-clamp(m31, -1, 1))
674
+ if (Math.abs(m31) < 0.9999999) {
675
+ this._x = Math.atan2(m32, m33)
676
+ this._z = Math.atan2(m21, m11)
677
+ } else {
678
+ this._x = 0
679
+ this._z = Math.atan2(-m12, m22)
680
+ }
681
+ } else if (ord === 'YZX') {
682
+ this._z = Math.asin(clamp(m21, -1, 1))
683
+ if (Math.abs(m21) < 0.9999999) {
684
+ this._x = Math.atan2(-m23, m22)
685
+ this._y = Math.atan2(-m31, m11)
686
+ } else {
687
+ this._x = 0
688
+ this._y = Math.atan2(m13, m33)
689
+ }
690
+ } else if (ord === 'XZY') {
691
+ this._z = Math.asin(-clamp(m12, -1, 1))
692
+ if (Math.abs(m12) < 0.9999999) {
693
+ this._x = Math.atan2(m32, m22)
694
+ this._y = Math.atan2(m13, m11)
695
+ } else {
696
+ this._x = Math.atan2(-m23, m33)
697
+ this._y = 0
698
+ }
699
+ }
700
+
701
+ this._order = ord
702
+ if (update) this._onChangeCallback()
703
+ return this
704
+ }
705
+
706
+ setFromQuaternion(q: Quaternion, order: string = '', update: boolean = true): Euler {
707
+ // Rotation-matrix elements computed directly from the quaternion —
708
+ // no Matrix4 temporary (see the no-scratch note above).
709
+ q._onReadCallback()
710
+ const x = q._x
711
+ const y = q._y
712
+ const z = q._z
713
+ const w = q._w
714
+ const x2 = x + x
715
+ const y2 = y + y
716
+ const z2 = z + z
717
+ const xx = x * x2
718
+ const xy = x * y2
719
+ const xz = x * z2
720
+ const yy = y * y2
721
+ const yz = y * z2
722
+ const zz = z * z2
723
+ const wx = w * x2
724
+ const wy = w * y2
725
+ const wz = w * z2
726
+ const m11 = 1 - (yy + zz)
727
+ const m12 = xy - wz
728
+ const m13 = xz + wy
729
+ const m21 = xy + wz
730
+ const m22 = 1 - (xx + zz)
731
+ const m23 = yz - wx
732
+ const m31 = xz - wy
733
+ const m32 = yz + wx
734
+ const m33 = 1 - (xx + yy)
735
+ return this.setFromRotationElements(m11, m12, m13, m21, m22, m23, m31, m32, m33, order, update)
736
+ }
737
+
738
+ _onChange(callback: () => void): Euler {
739
+ this._onChangeCallback = callback
740
+ return this
741
+ }
742
+
743
+ _onRead(callback: () => void): Euler {
744
+ this._onReadCallback = callback
745
+ return this
746
+ }
747
+ }
748
+
749
+ // Elements are a Float32Array (the precision the renderer uses): math on
750
+ // them runs in hardware float, and the renderer takes them without a copy.
751
+ export class Matrix4 {
752
+ readonly elements: Float32Array
753
+
754
+ constructor() {
755
+ this.elements = new Float32Array(16)
756
+ this.elements[0] = 1
757
+ this.elements[5] = 1
758
+ this.elements[10] = 1
759
+ this.elements[15] = 1
760
+ }
761
+
762
+ // Row-major argument order, stored column-major (three.js convention).
763
+ set(
764
+ n11: number, n12: number, n13: number, n14: number,
765
+ n21: number, n22: number, n23: number, n24: number,
766
+ n31: number, n32: number, n33: number, n34: number,
767
+ n41: number, n42: number, n43: number, n44: number,
768
+ ): Matrix4 {
769
+ const te = this.elements
770
+ te[0] = n11
771
+ te[4] = n12
772
+ te[8] = n13
773
+ te[12] = n14
774
+ te[1] = n21
775
+ te[5] = n22
776
+ te[9] = n23
777
+ te[13] = n24
778
+ te[2] = n31
779
+ te[6] = n32
780
+ te[10] = n33
781
+ te[14] = n34
782
+ te[3] = n41
783
+ te[7] = n42
784
+ te[11] = n43
785
+ te[15] = n44
786
+ return this
787
+ }
788
+
789
+ identity(): Matrix4 {
790
+ return this.set(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)
791
+ }
792
+
793
+ copy(m: Matrix4): Matrix4 {
794
+ const te = this.elements
795
+ const me = m.elements
796
+ for (let i = 0; i < 16; i++) te[i] = me[i]
797
+ return this
798
+ }
799
+
800
+ clone(): Matrix4 {
801
+ return new Matrix4().copy(this)
802
+ }
803
+
804
+ multiply(m: Matrix4): Matrix4 {
805
+ return this.multiplyMatrices(this, m)
806
+ }
807
+
808
+ premultiply(m: Matrix4): Matrix4 {
809
+ return this.multiplyMatrices(m, this)
810
+ }
811
+
812
+ multiplyMatrices(a: Matrix4, b: Matrix4): Matrix4 {
813
+ const ae = a.elements
814
+ const be = b.elements
815
+ const te = this.elements
816
+
817
+ const a11 = ae[0]
818
+ const a12 = ae[4]
819
+ const a13 = ae[8]
820
+ const a14 = ae[12]
821
+ const a21 = ae[1]
822
+ const a22 = ae[5]
823
+ const a23 = ae[9]
824
+ const a24 = ae[13]
825
+ const a31 = ae[2]
826
+ const a32 = ae[6]
827
+ const a33 = ae[10]
828
+ const a34 = ae[14]
829
+ const a41 = ae[3]
830
+ const a42 = ae[7]
831
+ const a43 = ae[11]
832
+ const a44 = ae[15]
833
+
834
+ const b11 = be[0]
835
+ const b12 = be[4]
836
+ const b13 = be[8]
837
+ const b14 = be[12]
838
+ const b21 = be[1]
839
+ const b22 = be[5]
840
+ const b23 = be[9]
841
+ const b24 = be[13]
842
+ const b31 = be[2]
843
+ const b32 = be[6]
844
+ const b33 = be[10]
845
+ const b34 = be[14]
846
+ const b41 = be[3]
847
+ const b42 = be[7]
848
+ const b43 = be[11]
849
+ const b44 = be[15]
850
+
851
+ te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41
852
+ te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42
853
+ te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43
854
+ te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44
855
+ te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41
856
+ te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42
857
+ te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43
858
+ te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44
859
+ te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41
860
+ te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42
861
+ te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43
862
+ te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44
863
+ te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41
864
+ te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42
865
+ te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43
866
+ te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44
867
+ return this
868
+ }
869
+
870
+ makeRotationFromQuaternion(q: Quaternion): Matrix4 {
871
+ // compose(zero, q, one) without the temporary vectors
872
+ q._onReadCallback()
873
+ const te = this.elements
874
+ const x = q._x
875
+ const y = q._y
876
+ const z = q._z
877
+ const w = q._w
878
+ const x2 = x + x
879
+ const y2 = y + y
880
+ const z2 = z + z
881
+ const xx = x * x2
882
+ const xy = x * y2
883
+ const xz = x * z2
884
+ const yy = y * y2
885
+ const yz = y * z2
886
+ const zz = z * z2
887
+ const wx = w * x2
888
+ const wy = w * y2
889
+ const wz = w * z2
890
+ te[0] = 1 - (yy + zz)
891
+ te[1] = xy + wz
892
+ te[2] = xz - wy
893
+ te[3] = 0
894
+ te[4] = xy - wz
895
+ te[5] = 1 - (xx + zz)
896
+ te[6] = yz + wx
897
+ te[7] = 0
898
+ te[8] = xz + wy
899
+ te[9] = yz - wx
900
+ te[10] = 1 - (xx + yy)
901
+ te[11] = 0
902
+ te[12] = 0
903
+ te[13] = 0
904
+ te[14] = 0
905
+ te[15] = 1
906
+ return this
907
+ }
908
+
909
+ compose(position: Vector3, quaternion: Quaternion, scale: Vector3): Matrix4 {
910
+ quaternion._onReadCallback()
911
+ const te = this.elements
912
+ const x = quaternion._x
913
+ const y = quaternion._y
914
+ const z = quaternion._z
915
+ const w = quaternion._w
916
+ const x2 = x + x
917
+ const y2 = y + y
918
+ const z2 = z + z
919
+ const xx = x * x2
920
+ const xy = x * y2
921
+ const xz = x * z2
922
+ const yy = y * y2
923
+ const yz = y * z2
924
+ const zz = z * z2
925
+ const wx = w * x2
926
+ const wy = w * y2
927
+ const wz = w * z2
928
+ const sx = scale.x
929
+ const sy = scale.y
930
+ const sz = scale.z
931
+
932
+ te[0] = (1 - (yy + zz)) * sx
933
+ te[1] = (xy + wz) * sx
934
+ te[2] = (xz - wy) * sx
935
+ te[3] = 0
936
+ te[4] = (xy - wz) * sy
937
+ te[5] = (1 - (xx + zz)) * sy
938
+ te[6] = (yz + wx) * sy
939
+ te[7] = 0
940
+ te[8] = (xz + wy) * sz
941
+ te[9] = (yz - wx) * sz
942
+ te[10] = (1 - (xx + yy)) * sz
943
+ te[11] = 0
944
+ te[12] = position.x
945
+ te[13] = position.y
946
+ te[14] = position.z
947
+ te[15] = 1
948
+ return this
949
+ }
950
+
951
+ invert(): Matrix4 {
952
+ // Full 4x4 inverse (three.js implementation).
953
+ const te = this.elements
954
+ const n11 = te[0]
955
+ const n21 = te[1]
956
+ const n31 = te[2]
957
+ const n41 = te[3]
958
+ const n12 = te[4]
959
+ const n22 = te[5]
960
+ const n32 = te[6]
961
+ const n42 = te[7]
962
+ const n13 = te[8]
963
+ const n23 = te[9]
964
+ const n33 = te[10]
965
+ const n43 = te[11]
966
+ const n14 = te[12]
967
+ const n24 = te[13]
968
+ const n34 = te[14]
969
+ const n44 = te[15]
970
+
971
+ const t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44
972
+ const t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44
973
+ const t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44
974
+ const t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34
975
+
976
+ const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14
977
+ if (det === 0) return this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
978
+
979
+ const detInv = 1 / det
980
+ te[0] = t11 * detInv
981
+ te[1] = (n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44) * detInv
982
+ te[2] = (n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44) * detInv
983
+ te[3] = (n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43) * detInv
984
+ te[4] = t12 * detInv
985
+ te[5] = (n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44) * detInv
986
+ te[6] = (n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44) * detInv
987
+ te[7] = (n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43) * detInv
988
+ te[8] = t13 * detInv
989
+ te[9] = (n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44) * detInv
990
+ te[10] = (n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44) * detInv
991
+ te[11] = (n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43) * detInv
992
+ te[12] = t14 * detInv
993
+ te[13] = (n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34) * detInv
994
+ te[14] = (n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34) * detInv
995
+ te[15] = (n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33) * detInv
996
+ return this
997
+ }
998
+
999
+ setPosition(x: number, y: number, z: number): Matrix4 {
1000
+ const te = this.elements
1001
+ te[12] = x
1002
+ te[13] = y
1003
+ te[14] = z
1004
+ return this
1005
+ }
1006
+
1007
+ // Rotation-only look-at (three.js Matrix4.lookAt), scalar-style.
1008
+ lookAt(eye: Vector3, target: Vector3, up: Vector3): Matrix4 {
1009
+ return this.lookAtScalar(eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z)
1010
+ }
1011
+
1012
+ lookAtScalar(
1013
+ eyeX: number, eyeY: number, eyeZ: number,
1014
+ targetX: number, targetY: number, targetZ: number,
1015
+ upX: number, upY: number, upZ: number,
1016
+ ): Matrix4 {
1017
+ const te = this.elements
1018
+ let zx = eyeX - targetX
1019
+ let zy = eyeY - targetY
1020
+ let zz = eyeZ - targetZ
1021
+ let zLenSq = zx * zx + zy * zy + zz * zz
1022
+ if (zLenSq === 0) {
1023
+ zz = 1
1024
+ zLenSq = 1
1025
+ }
1026
+ let inv = 1 / Math.sqrt(zLenSq)
1027
+ zx *= inv
1028
+ zy *= inv
1029
+ zz *= inv
1030
+ let xx = upY * zz - upZ * zy
1031
+ let xy = upZ * zx - upX * zz
1032
+ let xz = upX * zy - upY * zx
1033
+ let xLenSq = xx * xx + xy * xy + xz * xz
1034
+ if (xLenSq === 0) {
1035
+ // up and z are parallel
1036
+ if (Math.abs(upZ) === 1) zx += 0.0001
1037
+ else zz += 0.0001
1038
+ inv = 1 / Math.sqrt(zx * zx + zy * zy + zz * zz)
1039
+ zx *= inv
1040
+ zy *= inv
1041
+ zz *= inv
1042
+ xx = upY * zz - upZ * zy
1043
+ xy = upZ * zx - upX * zz
1044
+ xz = upX * zy - upY * zx
1045
+ xLenSq = xx * xx + xy * xy + xz * xz
1046
+ }
1047
+ inv = xLenSq > 0 ? 1 / Math.sqrt(xLenSq) : 1
1048
+ xx *= inv
1049
+ xy *= inv
1050
+ xz *= inv
1051
+ const yx = zy * xz - zz * xy
1052
+ const yy = zz * xx - zx * xz
1053
+ const yz = zx * xy - zy * xx
1054
+ te[0] = xx
1055
+ te[4] = yx
1056
+ te[8] = zx
1057
+ te[1] = xy
1058
+ te[5] = yy
1059
+ te[9] = zy
1060
+ te[2] = xz
1061
+ te[6] = yz
1062
+ te[10] = zz
1063
+ return this
1064
+ }
1065
+
1066
+ makePerspective(left: number, right: number, top: number, bottom: number, near: number, far: number): Matrix4 {
1067
+ const te = this.elements
1068
+ const x = (2 * near) / (right - left)
1069
+ const y = (2 * near) / (top - bottom)
1070
+ const a = (right + left) / (right - left)
1071
+ const b = (top + bottom) / (top - bottom)
1072
+ const c = -(far + near) / (far - near)
1073
+ const d = (-2 * far * near) / (far - near)
1074
+ te[0] = x
1075
+ te[4] = 0
1076
+ te[8] = a
1077
+ te[12] = 0
1078
+ te[1] = 0
1079
+ te[5] = y
1080
+ te[9] = b
1081
+ te[13] = 0
1082
+ te[2] = 0
1083
+ te[6] = 0
1084
+ te[10] = c
1085
+ te[14] = d
1086
+ te[3] = 0
1087
+ te[7] = 0
1088
+ te[11] = -1
1089
+ te[15] = 0
1090
+ return this
1091
+ }
1092
+
1093
+ makeOrthographic(left: number, right: number, top: number, bottom: number, near: number, far: number): Matrix4 {
1094
+ const te = this.elements
1095
+ const w = 1.0 / (right - left)
1096
+ const h = 1.0 / (top - bottom)
1097
+ const p = 1.0 / (far - near)
1098
+ te[0] = 2 * w
1099
+ te[4] = 0
1100
+ te[8] = 0
1101
+ te[12] = -(right + left) * w
1102
+ te[1] = 0
1103
+ te[5] = 2 * h
1104
+ te[9] = 0
1105
+ te[13] = -(top + bottom) * h
1106
+ te[2] = 0
1107
+ te[6] = 0
1108
+ te[10] = -2 * p
1109
+ te[14] = -(far + near) * p
1110
+ te[3] = 0
1111
+ te[7] = 0
1112
+ te[11] = 0
1113
+ te[15] = 1
1114
+ return this
1115
+ }
1116
+
1117
+ getMaxScaleOnAxis(): number {
1118
+ const te = this.elements
1119
+ const sqX = te[0] * te[0] + te[1] * te[1] + te[2] * te[2]
1120
+ const sqY = te[4] * te[4] + te[5] * te[5] + te[6] * te[6]
1121
+ const sqZ = te[8] * te[8] + te[9] * te[9] + te[10] * te[10]
1122
+ return Math.sqrt(Math.max(sqX, Math.max(sqY, sqZ)))
1123
+ }
1124
+ }
1125
+
1126
+ // NOTE: no module-scope scratch singletons and no lazy scratch containers —
1127
+ // both patterns miscompile in embedded geatsc today (module-scope instances
1128
+ // lower by-value; nullable class-typed globals box and fail to round-trip).
1129
+ // Hot conversions are written scalar-style instead; cold paths allocate
1130
+ // small temporaries per call.
1131
+
1132
+ export class Color {
1133
+ r: number
1134
+ g: number
1135
+ b: number
1136
+
1137
+ constructor(r: number = -1, g: number = -1, b: number = -1) {
1138
+ this.r = 1
1139
+ this.g = 1
1140
+ this.b = 1
1141
+ if (r < 0) return // new Color() = white
1142
+ if (g < 0 || b < 0) this.setHex(Math.floor(r))
1143
+ else this.setRGB(r, g, b)
1144
+ }
1145
+
1146
+ set(hex: number): Color {
1147
+ return this.setHex(hex)
1148
+ }
1149
+
1150
+ setHex(hex: number): Color {
1151
+ const h = Math.floor(hex)
1152
+ this.r = ((h >> 16) & 255) / 255
1153
+ this.g = ((h >> 8) & 255) / 255
1154
+ this.b = (h & 255) / 255
1155
+ return this
1156
+ }
1157
+
1158
+ setRGB(r: number, g: number, b: number): Color {
1159
+ this.r = r
1160
+ this.g = g
1161
+ this.b = b
1162
+ return this
1163
+ }
1164
+
1165
+ setHSL(h: number, s: number, l: number): Color {
1166
+ // h,s,l in [0,1] (three.js semantics)
1167
+ let hue = h % 1
1168
+ if (hue < 0) hue += 1
1169
+ if (s === 0) {
1170
+ this.r = l
1171
+ this.g = l
1172
+ this.b = l
1173
+ return this
1174
+ }
1175
+ const q = l <= 0.5 ? l * (1 + s) : l + s - l * s
1176
+ const p = 2 * l - q
1177
+ this.r = hue2rgb(p, q, hue + 1 / 3)
1178
+ this.g = hue2rgb(p, q, hue)
1179
+ this.b = hue2rgb(p, q, hue - 1 / 3)
1180
+ return this
1181
+ }
1182
+
1183
+ copy(c: Color): Color {
1184
+ this.r = c.r
1185
+ this.g = c.g
1186
+ this.b = c.b
1187
+ return this
1188
+ }
1189
+
1190
+ clone(): Color {
1191
+ const c = new Color(0xffffff)
1192
+ c.r = this.r
1193
+ c.g = this.g
1194
+ c.b = this.b
1195
+ return c
1196
+ }
1197
+
1198
+ multiply(c: Color): Color {
1199
+ this.r *= c.r
1200
+ this.g *= c.g
1201
+ this.b *= c.b
1202
+ return this
1203
+ }
1204
+
1205
+ multiplyScalar(s: number): Color {
1206
+ this.r *= s
1207
+ this.g *= s
1208
+ this.b *= s
1209
+ return this
1210
+ }
1211
+
1212
+ getHex(): number {
1213
+ const r = Math.round(clamp(this.r * 255, 0, 255))
1214
+ const g = Math.round(clamp(this.g * 255, 0, 255))
1215
+ const b = Math.round(clamp(this.b * 255, 0, 255))
1216
+ return (r << 16) | (g << 8) | b
1217
+ }
1218
+ }
1219
+
1220
+ function hue2rgb(p: number, q: number, t: number): number {
1221
+ let tt = t
1222
+ if (tt < 0) tt += 1
1223
+ if (tt > 1) tt -= 1
1224
+ if (tt < 1 / 6) return p + (q - p) * 6 * tt
1225
+ if (tt < 1 / 2) return q
1226
+ if (tt < 2 / 3) return p + (q - p) * 6 * (2 / 3 - tt)
1227
+ return p
1228
+ }