reze-engine 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/math.ts CHANGED
@@ -1,505 +1,546 @@
1
- export class Vec3 {
2
- x: number
3
- y: number
4
- z: number
5
-
6
- constructor(x: number, y: number, z: number) {
7
- this.x = x
8
- this.y = y
9
- this.z = z
10
- }
11
-
12
- add(other: Vec3): Vec3 {
13
- return new Vec3(this.x + other.x, this.y + other.y, this.z + other.z)
14
- }
15
-
16
- subtract(other: Vec3): Vec3 {
17
- return new Vec3(this.x - other.x, this.y - other.y, this.z - other.z)
18
- }
19
-
20
- length(): number {
21
- return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z)
22
- }
23
-
24
- normalize(): Vec3 {
25
- const len = this.length()
26
- if (len === 0) return new Vec3(0, 0, 0)
27
- return new Vec3(this.x / len, this.y / len, this.z / len)
28
- }
29
-
30
- cross(other: Vec3): Vec3 {
31
- return new Vec3(
32
- this.y * other.z - this.z * other.y,
33
- this.z * other.x - this.x * other.z,
34
- this.x * other.y - this.y * other.x
35
- )
36
- }
37
-
38
- dot(other: Vec3): number {
39
- return this.x * other.x + this.y * other.y + this.z * other.z
40
- }
41
-
42
- scale(scalar: number): Vec3 {
43
- return new Vec3(this.x * scalar, this.y * scalar, this.z * scalar)
44
- }
45
-
46
- clone(): Vec3 {
47
- return new Vec3(this.x, this.y, this.z)
48
- }
49
- }
50
-
51
- export class Quat {
52
- x: number
53
- y: number
54
- z: number
55
- w: number
56
-
57
- constructor(x: number, y: number, z: number, w: number) {
58
- this.x = x
59
- this.y = y
60
- this.z = z
61
- this.w = w
62
- }
63
-
64
- add(other: Quat): Quat {
65
- return new Quat(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w)
66
- }
67
-
68
- clone(): Quat {
69
- return new Quat(this.x, this.y, this.z, this.w)
70
- }
71
-
72
- multiply(other: Quat): Quat {
73
- // Proper quaternion multiplication (not component-wise)
74
- return new Quat(
75
- this.w * other.x + this.x * other.w + this.y * other.z - this.z * other.y,
76
- this.w * other.y - this.x * other.z + this.y * other.w + this.z * other.x,
77
- this.w * other.z + this.x * other.y - this.y * other.x + this.z * other.w,
78
- this.w * other.w - this.x * other.x - this.y * other.y - this.z * other.z
79
- )
80
- }
81
-
82
- conjugate(): Quat {
83
- // Conjugate (inverse for unit quaternions): (x, y, z, w) -> (-x, -y, -z, w)
84
- return new Quat(-this.x, -this.y, -this.z, this.w)
85
- }
86
-
87
- length(): number {
88
- return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w)
89
- }
90
-
91
- normalize(): Quat {
92
- const len = this.length()
93
- if (len === 0) return new Quat(0, 0, 0, 1)
94
- return new Quat(this.x / len, this.y / len, this.z / len, this.w / len)
95
- }
96
-
97
- // Rotate a vector by this quaternion: result = q * v * q^-1
98
- rotateVec(v: Vec3): Vec3 {
99
- // Treat v as pure quaternion (x, y, z, 0)
100
- const qx = this.x,
101
- qy = this.y,
102
- qz = this.z,
103
- qw = this.w
104
- const vx = v.x,
105
- vy = v.y,
106
- vz = v.z
107
-
108
- // t = 2 * cross(q.xyz, v)
109
- const tx = 2 * (qy * vz - qz * vy)
110
- const ty = 2 * (qz * vx - qx * vz)
111
- const tz = 2 * (qx * vy - qy * vx)
112
-
113
- // result = v + q.w * t + cross(q.xyz, t)
114
- return new Vec3(
115
- vx + qw * tx + (qy * tz - qz * ty),
116
- vy + qw * ty + (qz * tx - qx * tz),
117
- vz + qw * tz + (qx * ty - qy * tx)
118
- )
119
- }
120
-
121
- // Rotate a vector by this quaternion (Babylon.js style naming)
122
- rotate(v: Vec3): Vec3 {
123
- const qv = new Vec3(this.x, this.y, this.z)
124
- const uv = qv.cross(v)
125
- const uuv = qv.cross(uv)
126
- return v.add(uv.scale(2 * this.w)).add(uuv.scale(2))
127
- }
128
-
129
- // Static method: create quaternion that rotates from one direction to another
130
- static fromTo(from: Vec3, to: Vec3): Quat {
131
- const dot = from.dot(to)
132
- if (dot > 0.999999) return new Quat(0, 0, 0, 1) // Already aligned
133
- if (dot < -0.999999) {
134
- // 180 degrees
135
- let axis = from.cross(new Vec3(1, 0, 0))
136
- if (axis.length() < 0.001) axis = from.cross(new Vec3(0, 1, 0))
137
- return new Quat(axis.x, axis.y, axis.z, 0).normalize()
138
- }
139
-
140
- const axis = from.cross(to)
141
- const w = Math.sqrt((1 + dot) * 2)
142
- const invW = 1 / w
143
- return new Quat(axis.x * invW, axis.y * invW, axis.z * invW, w * 0.5).normalize()
144
- }
145
-
146
- toArray(): [number, number, number, number] {
147
- return [this.x, this.y, this.z, this.w]
148
- }
149
-
150
- // Convert Euler angles to quaternion (ZXY order, left-handed, PMX format)
151
- static fromEuler(rotX: number, rotY: number, rotZ: number): Quat {
152
- const cx = Math.cos(rotX * 0.5)
153
- const sx = Math.sin(rotX * 0.5)
154
- const cy = Math.cos(rotY * 0.5)
155
- const sy = Math.sin(rotY * 0.5)
156
- const cz = Math.cos(rotZ * 0.5)
157
- const sz = Math.sin(rotZ * 0.5)
158
-
159
- const w = cy * cx * cz + sy * sx * sz
160
- const x = cy * sx * cz + sy * cx * sz
161
- const y = sy * cx * cz - cy * sx * sz
162
- const z = cy * cx * sz - sy * sx * cz
163
-
164
- return new Quat(x, y, z, w).normalize()
165
- }
166
-
167
- // Convert quaternion to Euler angles (ZXY order, inverse of fromEuler)
168
- toEuler(): Vec3 {
169
- const qx = this.x
170
- const qy = this.y
171
- const qz = this.z
172
- const qw = this.w
173
-
174
- // ZXY order (left-handed)
175
- // Roll (X): rotation around X axis
176
- const sinr_cosp = 2 * (qw * qx + qy * qz)
177
- const cosr_cosp = 1 - 2 * (qx * qx + qy * qy)
178
- const rotX = Math.atan2(sinr_cosp, cosr_cosp)
179
-
180
- // Pitch (Y): rotation around Y axis
181
- const sinp = 2 * (qw * qy - qz * qx)
182
- const rotY = Math.abs(sinp) >= 1 ? (sinp >= 0 ? Math.PI / 2 : -Math.PI / 2) : Math.asin(sinp)
183
-
184
- // Yaw (Z): rotation around Z axis
185
- const siny_cosp = 2 * (qw * qz + qx * qy)
186
- const cosy_cosp = 1 - 2 * (qy * qy + qz * qz)
187
- const rotZ = Math.atan2(siny_cosp, cosy_cosp)
188
-
189
- return new Vec3(rotX, rotY, rotZ)
190
- }
191
- }
192
-
193
- export class Mat4 {
194
- values: Float32Array
195
-
196
- constructor(values: Float32Array) {
197
- this.values = values
198
- }
199
-
200
- static identity(): Mat4 {
201
- return new Mat4(new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]))
202
- }
203
-
204
- // Perspective matrix for LEFT-HANDED coordinate system (Z+ forward)
205
- // For left-handed: Z goes from 0 (near) to 1 (far), +Z is forward
206
- static perspective(fov: number, aspect: number, near: number, far: number): Mat4 {
207
- const f = 1.0 / Math.tan(fov / 2)
208
- const rangeInv = 1.0 / (far - near) // Positive for left-handed
209
-
210
- return new Mat4(
211
- new Float32Array([
212
- f / aspect,
213
- 0,
214
- 0,
215
- 0,
216
- 0,
217
- f,
218
- 0,
219
- 0,
220
- 0,
221
- 0,
222
- (far + near) * rangeInv,
223
- 1, // Positive for left-handed (Z+ forward)
224
- 0,
225
- 0,
226
- -near * far * rangeInv * 2, // Negated for left-handed
227
- 0,
228
- ])
229
- )
230
- }
231
-
232
- // LookAt matrix for LEFT-HANDED coordinate system (Z+ forward)
233
- // For left-handed: camera looks along +Z direction
234
- static lookAt(eye: Vec3, target: Vec3, up: Vec3): Mat4 {
235
- // In left-handed: forward = target - eye (Z+ direction)
236
- const forward = target.subtract(eye).normalize()
237
- const right = up.cross(forward).normalize() // X+ is right
238
- const upVec = forward.cross(right).normalize() // Y+ is up
239
-
240
- return new Mat4(
241
- new Float32Array([
242
- right.x,
243
- upVec.x,
244
- forward.x,
245
- 0,
246
- right.y,
247
- upVec.y,
248
- forward.y,
249
- 0,
250
- right.z,
251
- upVec.z,
252
- forward.z,
253
- 0,
254
- -right.dot(eye),
255
- -upVec.dot(eye),
256
- -forward.dot(eye),
257
- 1,
258
- ])
259
- )
260
- }
261
-
262
- multiply(other: Mat4): Mat4 {
263
- // Column-major multiplication (matches WGSL/GLSL convention):
264
- // result = a * b
265
- const out = new Float32Array(16)
266
- const a = this.values
267
- const b = other.values
268
- for (let c = 0; c < 4; c++) {
269
- const b0 = b[c * 4 + 0]
270
- const b1 = b[c * 4 + 1]
271
- const b2 = b[c * 4 + 2]
272
- const b3 = b[c * 4 + 3]
273
- out[c * 4 + 0] = a[0] * b0 + a[4] * b1 + a[8] * b2 + a[12] * b3
274
- out[c * 4 + 1] = a[1] * b0 + a[5] * b1 + a[9] * b2 + a[13] * b3
275
- out[c * 4 + 2] = a[2] * b0 + a[6] * b1 + a[10] * b2 + a[14] * b3
276
- out[c * 4 + 3] = a[3] * b0 + a[7] * b1 + a[11] * b2 + a[15] * b3
277
- }
278
- return new Mat4(out)
279
- }
280
-
281
- // Static method to multiply two matrix array segments directly into output array (no object creation)
282
- // Column-major multiplication: result = a * b
283
- static multiplyArrays(
284
- a: Float32Array,
285
- aOffset: number,
286
- b: Float32Array,
287
- bOffset: number,
288
- out: Float32Array,
289
- outOffset: number
290
- ): void {
291
- for (let c = 0; c < 4; c++) {
292
- const b0 = b[bOffset + c * 4 + 0]
293
- const b1 = b[bOffset + c * 4 + 1]
294
- const b2 = b[bOffset + c * 4 + 2]
295
- const b3 = b[bOffset + c * 4 + 3]
296
- out[outOffset + c * 4 + 0] =
297
- a[aOffset + 0] * b0 + a[aOffset + 4] * b1 + a[aOffset + 8] * b2 + a[aOffset + 12] * b3
298
- out[outOffset + c * 4 + 1] =
299
- a[aOffset + 1] * b0 + a[aOffset + 5] * b1 + a[aOffset + 9] * b2 + a[aOffset + 13] * b3
300
- out[outOffset + c * 4 + 2] =
301
- a[aOffset + 2] * b0 + a[aOffset + 6] * b1 + a[aOffset + 10] * b2 + a[aOffset + 14] * b3
302
- out[outOffset + c * 4 + 3] =
303
- a[aOffset + 3] * b0 + a[aOffset + 7] * b1 + a[aOffset + 11] * b2 + a[aOffset + 15] * b3
304
- }
305
- }
306
-
307
- clone(): Mat4 {
308
- return new Mat4(this.values.slice())
309
- }
310
-
311
- static fromQuat(x: number, y: number, z: number, w: number): Mat4 {
312
- // Column-major rotation matrix from quaternion (matches glMatrix/WGSL)
313
- const out = new Float32Array(16)
314
- const x2 = x + x,
315
- y2 = y + y,
316
- z2 = z + z
317
- const xx = x * x2,
318
- xy = x * y2,
319
- xz = x * z2
320
- const yy = y * y2,
321
- yz = y * z2,
322
- zz = z * z2
323
- const wx = w * x2,
324
- wy = w * y2,
325
- wz = w * z2
326
- out[0] = 1 - (yy + zz)
327
- out[1] = xy + wz
328
- out[2] = xz - wy
329
- out[3] = 0
330
- out[4] = xy - wz
331
- out[5] = 1 - (xx + zz)
332
- out[6] = yz + wx
333
- out[7] = 0
334
- out[8] = xz + wy
335
- out[9] = yz - wx
336
- out[10] = 1 - (xx + yy)
337
- out[11] = 0
338
- out[12] = 0
339
- out[13] = 0
340
- out[14] = 0
341
- out[15] = 1
342
- return new Mat4(out)
343
- }
344
-
345
- // Create transform matrix from position and rotation
346
- static fromPositionRotation(position: Vec3, rotation: Quat): Mat4 {
347
- const rotMat = Mat4.fromQuat(rotation.x, rotation.y, rotation.z, rotation.w)
348
- rotMat.values[12] = position.x
349
- rotMat.values[13] = position.y
350
- rotMat.values[14] = position.z
351
- return rotMat
352
- }
353
-
354
- // Extract position from transform matrix
355
- getPosition(): Vec3 {
356
- return new Vec3(this.values[12], this.values[13], this.values[14])
357
- }
358
-
359
- // Extract quaternion rotation from this matrix (upper-left 3x3 rotation part)
360
- toQuat(): Quat {
361
- return Mat4.toQuatFromArray(this.values, 0)
362
- }
363
-
364
- // Static method to extract quaternion from matrix array (avoids creating Mat4 object)
365
- static toQuatFromArray(m: Float32Array, offset: number): Quat {
366
- const m00 = m[offset + 0],
367
- m01 = m[offset + 4],
368
- m02 = m[offset + 8]
369
- const m10 = m[offset + 1],
370
- m11 = m[offset + 5],
371
- m12 = m[offset + 9]
372
- const m20 = m[offset + 2],
373
- m21 = m[offset + 6],
374
- m22 = m[offset + 10]
375
- const trace = m00 + m11 + m22
376
- let x = 0,
377
- y = 0,
378
- z = 0,
379
- w = 1
380
- if (trace > 0) {
381
- const s = Math.sqrt(trace + 1.0) * 2
382
- w = 0.25 * s
383
- x = (m21 - m12) / s
384
- y = (m02 - m20) / s
385
- z = (m10 - m01) / s
386
- } else if (m00 > m11 && m00 > m22) {
387
- const s = Math.sqrt(1.0 + m00 - m11 - m22) * 2
388
- w = (m21 - m12) / s
389
- x = 0.25 * s
390
- y = (m01 + m10) / s
391
- z = (m02 + m20) / s
392
- } else if (m11 > m22) {
393
- const s = Math.sqrt(1.0 + m11 - m00 - m22) * 2
394
- w = (m02 - m20) / s
395
- x = (m01 + m10) / s
396
- y = 0.25 * s
397
- z = (m12 + m21) / s
398
- } else {
399
- const s = Math.sqrt(1.0 + m22 - m00 - m11) * 2
400
- w = (m10 - m01) / s
401
- x = (m02 + m20) / s
402
- y = (m12 + m21) / s
403
- z = 0.25 * s
404
- }
405
- const invLen = 1 / Math.hypot(x, y, z, w)
406
- return new Quat(x * invLen, y * invLen, z * invLen, w * invLen)
407
- }
408
-
409
- // Reset matrix to identity in place
410
- setIdentity(): this {
411
- const v = this.values
412
- v[0] = 1
413
- v[1] = 0
414
- v[2] = 0
415
- v[3] = 0
416
- v[4] = 0
417
- v[5] = 1
418
- v[6] = 0
419
- v[7] = 0
420
- v[8] = 0
421
- v[9] = 0
422
- v[10] = 1
423
- v[11] = 0
424
- v[12] = 0
425
- v[13] = 0
426
- v[14] = 0
427
- v[15] = 1
428
- return this
429
- }
430
-
431
- translateInPlace(tx: number, ty: number, tz: number): this {
432
- this.values[12] += tx
433
- this.values[13] += ty
434
- this.values[14] += tz
435
- return this
436
- }
437
-
438
- // Full 4x4 matrix inverse using adjugate method
439
- // This works for any invertible matrix, not just orthonormal transforms
440
- // The previous implementation assumed orthonormal rotation matrices, which fails
441
- // when matrices have scaling or are not perfectly orthonormal (e.g., after
442
- // bone hierarchy transformations)
443
- inverse(): Mat4 {
444
- const m = this.values
445
- const out = new Float32Array(16)
446
-
447
- const a00 = m[0],
448
- a01 = m[1],
449
- a02 = m[2],
450
- a03 = m[3]
451
- const a10 = m[4],
452
- a11 = m[5],
453
- a12 = m[6],
454
- a13 = m[7]
455
- const a20 = m[8],
456
- a21 = m[9],
457
- a22 = m[10],
458
- a23 = m[11]
459
- const a30 = m[12],
460
- a31 = m[13],
461
- a32 = m[14],
462
- a33 = m[15]
463
-
464
- const b00 = a00 * a11 - a01 * a10
465
- const b01 = a00 * a12 - a02 * a10
466
- const b02 = a00 * a13 - a03 * a10
467
- const b03 = a01 * a12 - a02 * a11
468
- const b04 = a01 * a13 - a03 * a11
469
- const b05 = a02 * a13 - a03 * a12
470
- const b06 = a20 * a31 - a21 * a30
471
- const b07 = a20 * a32 - a22 * a30
472
- const b08 = a20 * a33 - a23 * a30
473
- const b09 = a21 * a32 - a22 * a31
474
- const b10 = a21 * a33 - a23 * a31
475
- const b11 = a22 * a33 - a23 * a32
476
-
477
- let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06
478
-
479
- if (Math.abs(det) < 1e-10) {
480
- console.warn("Matrix is not invertible (determinant near zero)")
481
- return Mat4.identity()
482
- }
483
-
484
- det = 1.0 / det
485
-
486
- out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det
487
- out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det
488
- out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det
489
- out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det
490
- out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det
491
- out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det
492
- out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det
493
- out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det
494
- out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det
495
- out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det
496
- out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det
497
- out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det
498
- out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det
499
- out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det
500
- out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det
501
- out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det
502
-
503
- return new Mat4(out)
504
- }
505
- }
1
+ // Easing function: ease-in-out quadratic
2
+ export function easeInOut(t: number): number {
3
+ return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2
4
+ }
5
+
6
+ export class Vec3 {
7
+ x: number
8
+ y: number
9
+ z: number
10
+
11
+ constructor(x: number, y: number, z: number) {
12
+ this.x = x
13
+ this.y = y
14
+ this.z = z
15
+ }
16
+
17
+ add(other: Vec3): Vec3 {
18
+ return new Vec3(this.x + other.x, this.y + other.y, this.z + other.z)
19
+ }
20
+
21
+ subtract(other: Vec3): Vec3 {
22
+ return new Vec3(this.x - other.x, this.y - other.y, this.z - other.z)
23
+ }
24
+
25
+ length(): number {
26
+ return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z)
27
+ }
28
+
29
+ normalize(): Vec3 {
30
+ const len = this.length()
31
+ if (len === 0) return new Vec3(0, 0, 0)
32
+ return new Vec3(this.x / len, this.y / len, this.z / len)
33
+ }
34
+
35
+ cross(other: Vec3): Vec3 {
36
+ return new Vec3(
37
+ this.y * other.z - this.z * other.y,
38
+ this.z * other.x - this.x * other.z,
39
+ this.x * other.y - this.y * other.x
40
+ )
41
+ }
42
+
43
+ dot(other: Vec3): number {
44
+ return this.x * other.x + this.y * other.y + this.z * other.z
45
+ }
46
+
47
+ scale(scalar: number): Vec3 {
48
+ return new Vec3(this.x * scalar, this.y * scalar, this.z * scalar)
49
+ }
50
+
51
+ clone(): Vec3 {
52
+ return new Vec3(this.x, this.y, this.z)
53
+ }
54
+ }
55
+
56
+ export class Quat {
57
+ x: number
58
+ y: number
59
+ z: number
60
+ w: number
61
+
62
+ constructor(x: number, y: number, z: number, w: number) {
63
+ this.x = x
64
+ this.y = y
65
+ this.z = z
66
+ this.w = w
67
+ }
68
+
69
+ add(other: Quat): Quat {
70
+ return new Quat(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w)
71
+ }
72
+
73
+ clone(): Quat {
74
+ return new Quat(this.x, this.y, this.z, this.w)
75
+ }
76
+
77
+ multiply(other: Quat): Quat {
78
+ // Proper quaternion multiplication (not component-wise)
79
+ return new Quat(
80
+ this.w * other.x + this.x * other.w + this.y * other.z - this.z * other.y,
81
+ this.w * other.y - this.x * other.z + this.y * other.w + this.z * other.x,
82
+ this.w * other.z + this.x * other.y - this.y * other.x + this.z * other.w,
83
+ this.w * other.w - this.x * other.x - this.y * other.y - this.z * other.z
84
+ )
85
+ }
86
+
87
+ conjugate(): Quat {
88
+ // Conjugate (inverse for unit quaternions): (x, y, z, w) -> (-x, -y, -z, w)
89
+ return new Quat(-this.x, -this.y, -this.z, this.w)
90
+ }
91
+
92
+ length(): number {
93
+ return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w)
94
+ }
95
+
96
+ normalize(): Quat {
97
+ const len = this.length()
98
+ if (len === 0) return new Quat(0, 0, 0, 1)
99
+ return new Quat(this.x / len, this.y / len, this.z / len, this.w / len)
100
+ }
101
+
102
+ // Rotate a vector by this quaternion: result = q * v * q^-1
103
+ rotateVec(v: Vec3): Vec3 {
104
+ // Treat v as pure quaternion (x, y, z, 0)
105
+ const qx = this.x,
106
+ qy = this.y,
107
+ qz = this.z,
108
+ qw = this.w
109
+ const vx = v.x,
110
+ vy = v.y,
111
+ vz = v.z
112
+
113
+ // t = 2 * cross(q.xyz, v)
114
+ const tx = 2 * (qy * vz - qz * vy)
115
+ const ty = 2 * (qz * vx - qx * vz)
116
+ const tz = 2 * (qx * vy - qy * vx)
117
+
118
+ // result = v + q.w * t + cross(q.xyz, t)
119
+ return new Vec3(
120
+ vx + qw * tx + (qy * tz - qz * ty),
121
+ vy + qw * ty + (qz * tx - qx * tz),
122
+ vz + qw * tz + (qx * ty - qy * tx)
123
+ )
124
+ }
125
+
126
+ // Rotate a vector by this quaternion (Babylon.js style naming)
127
+ rotate(v: Vec3): Vec3 {
128
+ const qv = new Vec3(this.x, this.y, this.z)
129
+ const uv = qv.cross(v)
130
+ const uuv = qv.cross(uv)
131
+ return v.add(uv.scale(2 * this.w)).add(uuv.scale(2))
132
+ }
133
+
134
+ // Static method: create quaternion that rotates from one direction to another
135
+ static fromTo(from: Vec3, to: Vec3): Quat {
136
+ const dot = from.dot(to)
137
+ if (dot > 0.999999) return new Quat(0, 0, 0, 1) // Already aligned
138
+ if (dot < -0.999999) {
139
+ // 180 degrees
140
+ let axis = from.cross(new Vec3(1, 0, 0))
141
+ if (axis.length() < 0.001) axis = from.cross(new Vec3(0, 1, 0))
142
+ return new Quat(axis.x, axis.y, axis.z, 0).normalize()
143
+ }
144
+
145
+ const axis = from.cross(to)
146
+ const w = Math.sqrt((1 + dot) * 2)
147
+ const invW = 1 / w
148
+ return new Quat(axis.x * invW, axis.y * invW, axis.z * invW, w * 0.5).normalize()
149
+ }
150
+
151
+ toArray(): [number, number, number, number] {
152
+ return [this.x, this.y, this.z, this.w]
153
+ }
154
+
155
+ // Spherical linear interpolation between two quaternions
156
+ static slerp(a: Quat, b: Quat, t: number): Quat {
157
+ let cos = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
158
+ let bx = b.x,
159
+ by = b.y,
160
+ bz = b.z,
161
+ bw = b.w
162
+
163
+ // If dot product is negative, negate one quaternion to take shorter path
164
+ if (cos < 0) {
165
+ cos = -cos
166
+ bx = -bx
167
+ by = -by
168
+ bz = -bz
169
+ bw = -bw
170
+ }
171
+
172
+ // If quaternions are very close, use linear interpolation
173
+ if (cos > 0.9995) {
174
+ const x = a.x + t * (bx - a.x)
175
+ const y = a.y + t * (by - a.y)
176
+ const z = a.z + t * (bz - a.z)
177
+ const w = a.w + t * (bw - a.w)
178
+ const invLen = 1 / Math.hypot(x, y, z, w)
179
+ return new Quat(x * invLen, y * invLen, z * invLen, w * invLen)
180
+ }
181
+
182
+ // Standard SLERP
183
+ const theta0 = Math.acos(cos)
184
+ const sinTheta0 = Math.sin(theta0)
185
+ const theta = theta0 * t
186
+ const s0 = Math.sin(theta0 - theta) / sinTheta0
187
+ const s1 = Math.sin(theta) / sinTheta0
188
+ return new Quat(s0 * a.x + s1 * bx, s0 * a.y + s1 * by, s0 * a.z + s1 * bz, s0 * a.w + s1 * bw)
189
+ }
190
+
191
+ // Convert Euler angles to quaternion (ZXY order, left-handed, PMX format)
192
+ static fromEuler(rotX: number, rotY: number, rotZ: number): Quat {
193
+ const cx = Math.cos(rotX * 0.5)
194
+ const sx = Math.sin(rotX * 0.5)
195
+ const cy = Math.cos(rotY * 0.5)
196
+ const sy = Math.sin(rotY * 0.5)
197
+ const cz = Math.cos(rotZ * 0.5)
198
+ const sz = Math.sin(rotZ * 0.5)
199
+
200
+ const w = cy * cx * cz + sy * sx * sz
201
+ const x = cy * sx * cz + sy * cx * sz
202
+ const y = sy * cx * cz - cy * sx * sz
203
+ const z = cy * cx * sz - sy * sx * cz
204
+
205
+ return new Quat(x, y, z, w).normalize()
206
+ }
207
+
208
+ // Convert quaternion to Euler angles (ZXY order, inverse of fromEuler)
209
+ toEuler(): Vec3 {
210
+ const qx = this.x
211
+ const qy = this.y
212
+ const qz = this.z
213
+ const qw = this.w
214
+
215
+ // ZXY order (left-handed)
216
+ // Roll (X): rotation around X axis
217
+ const sinr_cosp = 2 * (qw * qx + qy * qz)
218
+ const cosr_cosp = 1 - 2 * (qx * qx + qy * qy)
219
+ const rotX = Math.atan2(sinr_cosp, cosr_cosp)
220
+
221
+ // Pitch (Y): rotation around Y axis
222
+ const sinp = 2 * (qw * qy - qz * qx)
223
+ const rotY = Math.abs(sinp) >= 1 ? (sinp >= 0 ? Math.PI / 2 : -Math.PI / 2) : Math.asin(sinp)
224
+
225
+ // Yaw (Z): rotation around Z axis
226
+ const siny_cosp = 2 * (qw * qz + qx * qy)
227
+ const cosy_cosp = 1 - 2 * (qy * qy + qz * qz)
228
+ const rotZ = Math.atan2(siny_cosp, cosy_cosp)
229
+
230
+ return new Vec3(rotX, rotY, rotZ)
231
+ }
232
+ }
233
+
234
+ export class Mat4 {
235
+ values: Float32Array
236
+
237
+ constructor(values: Float32Array) {
238
+ this.values = values
239
+ }
240
+
241
+ static identity(): Mat4 {
242
+ return new Mat4(new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]))
243
+ }
244
+
245
+ // Perspective matrix for LEFT-HANDED coordinate system (Z+ forward)
246
+ // For left-handed: Z goes from 0 (near) to 1 (far), +Z is forward
247
+ static perspective(fov: number, aspect: number, near: number, far: number): Mat4 {
248
+ const f = 1.0 / Math.tan(fov / 2)
249
+ const rangeInv = 1.0 / (far - near) // Positive for left-handed
250
+
251
+ return new Mat4(
252
+ new Float32Array([
253
+ f / aspect,
254
+ 0,
255
+ 0,
256
+ 0,
257
+ 0,
258
+ f,
259
+ 0,
260
+ 0,
261
+ 0,
262
+ 0,
263
+ (far + near) * rangeInv,
264
+ 1, // Positive for left-handed (Z+ forward)
265
+ 0,
266
+ 0,
267
+ -near * far * rangeInv * 2, // Negated for left-handed
268
+ 0,
269
+ ])
270
+ )
271
+ }
272
+
273
+ // LookAt matrix for LEFT-HANDED coordinate system (Z+ forward)
274
+ // For left-handed: camera looks along +Z direction
275
+ static lookAt(eye: Vec3, target: Vec3, up: Vec3): Mat4 {
276
+ // In left-handed: forward = target - eye (Z+ direction)
277
+ const forward = target.subtract(eye).normalize()
278
+ const right = up.cross(forward).normalize() // X+ is right
279
+ const upVec = forward.cross(right).normalize() // Y+ is up
280
+
281
+ return new Mat4(
282
+ new Float32Array([
283
+ right.x,
284
+ upVec.x,
285
+ forward.x,
286
+ 0,
287
+ right.y,
288
+ upVec.y,
289
+ forward.y,
290
+ 0,
291
+ right.z,
292
+ upVec.z,
293
+ forward.z,
294
+ 0,
295
+ -right.dot(eye),
296
+ -upVec.dot(eye),
297
+ -forward.dot(eye),
298
+ 1,
299
+ ])
300
+ )
301
+ }
302
+
303
+ multiply(other: Mat4): Mat4 {
304
+ // Column-major multiplication (matches WGSL/GLSL convention):
305
+ // result = a * b
306
+ const out = new Float32Array(16)
307
+ const a = this.values
308
+ const b = other.values
309
+ for (let c = 0; c < 4; c++) {
310
+ const b0 = b[c * 4 + 0]
311
+ const b1 = b[c * 4 + 1]
312
+ const b2 = b[c * 4 + 2]
313
+ const b3 = b[c * 4 + 3]
314
+ out[c * 4 + 0] = a[0] * b0 + a[4] * b1 + a[8] * b2 + a[12] * b3
315
+ out[c * 4 + 1] = a[1] * b0 + a[5] * b1 + a[9] * b2 + a[13] * b3
316
+ out[c * 4 + 2] = a[2] * b0 + a[6] * b1 + a[10] * b2 + a[14] * b3
317
+ out[c * 4 + 3] = a[3] * b0 + a[7] * b1 + a[11] * b2 + a[15] * b3
318
+ }
319
+ return new Mat4(out)
320
+ }
321
+
322
+ // Static method to multiply two matrix array segments directly into output array (no object creation)
323
+ // Column-major multiplication: result = a * b
324
+ static multiplyArrays(
325
+ a: Float32Array,
326
+ aOffset: number,
327
+ b: Float32Array,
328
+ bOffset: number,
329
+ out: Float32Array,
330
+ outOffset: number
331
+ ): void {
332
+ for (let c = 0; c < 4; c++) {
333
+ const b0 = b[bOffset + c * 4 + 0]
334
+ const b1 = b[bOffset + c * 4 + 1]
335
+ const b2 = b[bOffset + c * 4 + 2]
336
+ const b3 = b[bOffset + c * 4 + 3]
337
+ out[outOffset + c * 4 + 0] =
338
+ a[aOffset + 0] * b0 + a[aOffset + 4] * b1 + a[aOffset + 8] * b2 + a[aOffset + 12] * b3
339
+ out[outOffset + c * 4 + 1] =
340
+ a[aOffset + 1] * b0 + a[aOffset + 5] * b1 + a[aOffset + 9] * b2 + a[aOffset + 13] * b3
341
+ out[outOffset + c * 4 + 2] =
342
+ a[aOffset + 2] * b0 + a[aOffset + 6] * b1 + a[aOffset + 10] * b2 + a[aOffset + 14] * b3
343
+ out[outOffset + c * 4 + 3] =
344
+ a[aOffset + 3] * b0 + a[aOffset + 7] * b1 + a[aOffset + 11] * b2 + a[aOffset + 15] * b3
345
+ }
346
+ }
347
+
348
+ clone(): Mat4 {
349
+ return new Mat4(this.values.slice())
350
+ }
351
+
352
+ static fromQuat(x: number, y: number, z: number, w: number): Mat4 {
353
+ // Column-major rotation matrix from quaternion (matches glMatrix/WGSL)
354
+ const out = new Float32Array(16)
355
+ const x2 = x + x,
356
+ y2 = y + y,
357
+ z2 = z + z
358
+ const xx = x * x2,
359
+ xy = x * y2,
360
+ xz = x * z2
361
+ const yy = y * y2,
362
+ yz = y * z2,
363
+ zz = z * z2
364
+ const wx = w * x2,
365
+ wy = w * y2,
366
+ wz = w * z2
367
+ out[0] = 1 - (yy + zz)
368
+ out[1] = xy + wz
369
+ out[2] = xz - wy
370
+ out[3] = 0
371
+ out[4] = xy - wz
372
+ out[5] = 1 - (xx + zz)
373
+ out[6] = yz + wx
374
+ out[7] = 0
375
+ out[8] = xz + wy
376
+ out[9] = yz - wx
377
+ out[10] = 1 - (xx + yy)
378
+ out[11] = 0
379
+ out[12] = 0
380
+ out[13] = 0
381
+ out[14] = 0
382
+ out[15] = 1
383
+ return new Mat4(out)
384
+ }
385
+
386
+ // Create transform matrix from position and rotation
387
+ static fromPositionRotation(position: Vec3, rotation: Quat): Mat4 {
388
+ const rotMat = Mat4.fromQuat(rotation.x, rotation.y, rotation.z, rotation.w)
389
+ rotMat.values[12] = position.x
390
+ rotMat.values[13] = position.y
391
+ rotMat.values[14] = position.z
392
+ return rotMat
393
+ }
394
+
395
+ // Extract position from transform matrix
396
+ getPosition(): Vec3 {
397
+ return new Vec3(this.values[12], this.values[13], this.values[14])
398
+ }
399
+
400
+ // Extract quaternion rotation from this matrix (upper-left 3x3 rotation part)
401
+ toQuat(): Quat {
402
+ return Mat4.toQuatFromArray(this.values, 0)
403
+ }
404
+
405
+ // Static method to extract quaternion from matrix array (avoids creating Mat4 object)
406
+ static toQuatFromArray(m: Float32Array, offset: number): Quat {
407
+ const m00 = m[offset + 0],
408
+ m01 = m[offset + 4],
409
+ m02 = m[offset + 8]
410
+ const m10 = m[offset + 1],
411
+ m11 = m[offset + 5],
412
+ m12 = m[offset + 9]
413
+ const m20 = m[offset + 2],
414
+ m21 = m[offset + 6],
415
+ m22 = m[offset + 10]
416
+ const trace = m00 + m11 + m22
417
+ let x = 0,
418
+ y = 0,
419
+ z = 0,
420
+ w = 1
421
+ if (trace > 0) {
422
+ const s = Math.sqrt(trace + 1.0) * 2
423
+ w = 0.25 * s
424
+ x = (m21 - m12) / s
425
+ y = (m02 - m20) / s
426
+ z = (m10 - m01) / s
427
+ } else if (m00 > m11 && m00 > m22) {
428
+ const s = Math.sqrt(1.0 + m00 - m11 - m22) * 2
429
+ w = (m21 - m12) / s
430
+ x = 0.25 * s
431
+ y = (m01 + m10) / s
432
+ z = (m02 + m20) / s
433
+ } else if (m11 > m22) {
434
+ const s = Math.sqrt(1.0 + m11 - m00 - m22) * 2
435
+ w = (m02 - m20) / s
436
+ x = (m01 + m10) / s
437
+ y = 0.25 * s
438
+ z = (m12 + m21) / s
439
+ } else {
440
+ const s = Math.sqrt(1.0 + m22 - m00 - m11) * 2
441
+ w = (m10 - m01) / s
442
+ x = (m02 + m20) / s
443
+ y = (m12 + m21) / s
444
+ z = 0.25 * s
445
+ }
446
+ const invLen = 1 / Math.hypot(x, y, z, w)
447
+ return new Quat(x * invLen, y * invLen, z * invLen, w * invLen)
448
+ }
449
+
450
+ // Reset matrix to identity in place
451
+ setIdentity(): this {
452
+ const v = this.values
453
+ v[0] = 1
454
+ v[1] = 0
455
+ v[2] = 0
456
+ v[3] = 0
457
+ v[4] = 0
458
+ v[5] = 1
459
+ v[6] = 0
460
+ v[7] = 0
461
+ v[8] = 0
462
+ v[9] = 0
463
+ v[10] = 1
464
+ v[11] = 0
465
+ v[12] = 0
466
+ v[13] = 0
467
+ v[14] = 0
468
+ v[15] = 1
469
+ return this
470
+ }
471
+
472
+ translateInPlace(tx: number, ty: number, tz: number): this {
473
+ this.values[12] += tx
474
+ this.values[13] += ty
475
+ this.values[14] += tz
476
+ return this
477
+ }
478
+
479
+ // Full 4x4 matrix inverse using adjugate method
480
+ // This works for any invertible matrix, not just orthonormal transforms
481
+ // The previous implementation assumed orthonormal rotation matrices, which fails
482
+ // when matrices have scaling or are not perfectly orthonormal (e.g., after
483
+ // bone hierarchy transformations)
484
+ inverse(): Mat4 {
485
+ const m = this.values
486
+ const out = new Float32Array(16)
487
+
488
+ const a00 = m[0],
489
+ a01 = m[1],
490
+ a02 = m[2],
491
+ a03 = m[3]
492
+ const a10 = m[4],
493
+ a11 = m[5],
494
+ a12 = m[6],
495
+ a13 = m[7]
496
+ const a20 = m[8],
497
+ a21 = m[9],
498
+ a22 = m[10],
499
+ a23 = m[11]
500
+ const a30 = m[12],
501
+ a31 = m[13],
502
+ a32 = m[14],
503
+ a33 = m[15]
504
+
505
+ const b00 = a00 * a11 - a01 * a10
506
+ const b01 = a00 * a12 - a02 * a10
507
+ const b02 = a00 * a13 - a03 * a10
508
+ const b03 = a01 * a12 - a02 * a11
509
+ const b04 = a01 * a13 - a03 * a11
510
+ const b05 = a02 * a13 - a03 * a12
511
+ const b06 = a20 * a31 - a21 * a30
512
+ const b07 = a20 * a32 - a22 * a30
513
+ const b08 = a20 * a33 - a23 * a30
514
+ const b09 = a21 * a32 - a22 * a31
515
+ const b10 = a21 * a33 - a23 * a31
516
+ const b11 = a22 * a33 - a23 * a32
517
+
518
+ let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06
519
+
520
+ if (Math.abs(det) < 1e-10) {
521
+ console.warn("Matrix is not invertible (determinant near zero)")
522
+ return Mat4.identity()
523
+ }
524
+
525
+ det = 1.0 / det
526
+
527
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det
528
+ out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det
529
+ out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det
530
+ out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det
531
+ out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det
532
+ out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det
533
+ out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det
534
+ out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det
535
+ out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det
536
+ out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det
537
+ out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det
538
+ out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det
539
+ out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det
540
+ out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det
541
+ out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det
542
+ out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det
543
+
544
+ return new Mat4(out)
545
+ }
546
+ }