reze-engine 0.1.0

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/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { Engine, type EngineStats } from "./engine"
package/src/math.ts ADDED
@@ -0,0 +1,505 @@
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
+ }