@aethervtt/shard-core 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 (81) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/dist/ecs/commands.d.ts +35 -0
  4. package/dist/ecs/commands.js +96 -0
  5. package/dist/ecs/entity.d.ts +11 -0
  6. package/dist/ecs/entity.js +12 -0
  7. package/dist/ecs/events.d.ts +34 -0
  8. package/dist/ecs/events.js +65 -0
  9. package/dist/ecs/hierarchy.d.ts +7 -0
  10. package/dist/ecs/hierarchy.js +29 -0
  11. package/dist/ecs/index.d.ts +8 -0
  12. package/dist/ecs/index.js +38 -0
  13. package/dist/ecs/observers.d.ts +40 -0
  14. package/dist/ecs/observers.js +27 -0
  15. package/dist/ecs/query.d.ts +44 -0
  16. package/dist/ecs/query.js +67 -0
  17. package/dist/ecs/table.d.ts +91 -0
  18. package/dist/ecs/table.js +286 -0
  19. package/dist/ecs/world.d.ts +127 -0
  20. package/dist/ecs/world.js +513 -0
  21. package/dist/error.d.ts +26 -0
  22. package/dist/error.js +29 -0
  23. package/dist/index.d.ts +6 -0
  24. package/dist/index.js +9 -0
  25. package/dist/json.d.ts +15 -0
  26. package/dist/json.js +22 -0
  27. package/dist/math/aabb.d.ts +13 -0
  28. package/dist/math/aabb.js +78 -0
  29. package/dist/math/affine.d.ts +28 -0
  30. package/dist/math/affine.js +159 -0
  31. package/dist/math/affine64.d.ts +11 -0
  32. package/dist/math/affine64.js +58 -0
  33. package/dist/math/frustum.d.ts +11 -0
  34. package/dist/math/frustum.js +59 -0
  35. package/dist/math/hash.d.ts +14 -0
  36. package/dist/math/hash.js +38 -0
  37. package/dist/math/index.d.ts +14 -0
  38. package/dist/math/index.js +33 -0
  39. package/dist/math/mat3.d.ts +6 -0
  40. package/dist/math/mat3.js +37 -0
  41. package/dist/math/mat4.d.ts +23 -0
  42. package/dist/math/mat4.js +224 -0
  43. package/dist/math/quat.d.ts +22 -0
  44. package/dist/math/quat.js +233 -0
  45. package/dist/math/ray.d.ts +10 -0
  46. package/dist/math/ray.js +54 -0
  47. package/dist/math/rng.d.ts +41 -0
  48. package/dist/math/rng.js +102 -0
  49. package/dist/math/types.d.ts +8 -0
  50. package/dist/math/types.js +4 -0
  51. package/dist/math/vec2.d.ts +11 -0
  52. package/dist/math/vec2.js +52 -0
  53. package/dist/math/vec3.d.ts +23 -0
  54. package/dist/math/vec3.js +135 -0
  55. package/dist/math/vec4.d.ts +8 -0
  56. package/dist/math/vec4.js +47 -0
  57. package/dist/scheduler/index.d.ts +3 -0
  58. package/dist/scheduler/index.js +39 -0
  59. package/dist/scheduler/profiler.d.ts +19 -0
  60. package/dist/scheduler/profiler.js +44 -0
  61. package/dist/scheduler/schedule.d.ts +52 -0
  62. package/dist/scheduler/schedule.js +235 -0
  63. package/dist/scheduler/system.d.ts +74 -0
  64. package/dist/scheduler/system.js +106 -0
  65. package/dist/schema/color.d.ts +7 -0
  66. package/dist/schema/color.js +34 -0
  67. package/dist/schema/component.d.ts +71 -0
  68. package/dist/schema/component.js +120 -0
  69. package/dist/schema/field.d.ts +145 -0
  70. package/dist/schema/field.js +591 -0
  71. package/dist/schema/index.d.ts +6 -0
  72. package/dist/schema/index.js +40 -0
  73. package/dist/schema/names.d.ts +27 -0
  74. package/dist/schema/names.js +39 -0
  75. package/dist/schema/registry.d.ts +38 -0
  76. package/dist/schema/registry.js +58 -0
  77. package/dist/schema/resource.d.ts +48 -0
  78. package/dist/schema/resource.js +79 -0
  79. package/dist/test-env.d.ts +31 -0
  80. package/dist/test-env.js +37 -0
  81. package/package.json +38 -0
@@ -0,0 +1,224 @@
1
+ const create = () => identity(new Float32Array(16));
2
+ function identity(out) {
3
+ for (let i = 0; i < 16; i++) out[i] = i % 5 === 0 ? 1 : 0;
4
+ return out;
5
+ }
6
+ function copy(out, a) {
7
+ for (let i = 0; i < 16; i++) out[i] = a[i];
8
+ return out;
9
+ }
10
+ function multiply(out, a, b) {
11
+ const a00 = a[0];
12
+ const a01 = a[1];
13
+ const a02 = a[2];
14
+ const a03 = a[3];
15
+ const a10 = a[4];
16
+ const a11 = a[5];
17
+ const a12 = a[6];
18
+ const a13 = a[7];
19
+ const a20 = a[8];
20
+ const a21 = a[9];
21
+ const a22 = a[10];
22
+ const a23 = a[11];
23
+ const a30 = a[12];
24
+ const a31 = a[13];
25
+ const a32 = a[14];
26
+ const a33 = a[15];
27
+ for (let c = 0; c < 4; c++) {
28
+ const b0 = b[c * 4];
29
+ const b1 = b[c * 4 + 1];
30
+ const b2 = b[c * 4 + 2];
31
+ const b3 = b[c * 4 + 3];
32
+ out[c * 4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
33
+ out[c * 4 + 1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
34
+ out[c * 4 + 2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
35
+ out[c * 4 + 3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
36
+ }
37
+ return out;
38
+ }
39
+ function transpose(out, a) {
40
+ if (out === a) {
41
+ for (let r = 0; r < 4; r++) {
42
+ for (let c = r + 1; c < 4; c++) {
43
+ const tmp = out[c * 4 + r];
44
+ out[c * 4 + r] = out[r * 4 + c];
45
+ out[r * 4 + c] = tmp;
46
+ }
47
+ }
48
+ return out;
49
+ }
50
+ for (let r = 0; r < 4; r++) for (let c = 0; c < 4; c++) out[c * 4 + r] = a[r * 4 + c];
51
+ return out;
52
+ }
53
+ function invert(out, a) {
54
+ const a00 = a[0];
55
+ const a01 = a[1];
56
+ const a02 = a[2];
57
+ const a03 = a[3];
58
+ const a10 = a[4];
59
+ const a11 = a[5];
60
+ const a12 = a[6];
61
+ const a13 = a[7];
62
+ const a20 = a[8];
63
+ const a21 = a[9];
64
+ const a22 = a[10];
65
+ const a23 = a[11];
66
+ const a30 = a[12];
67
+ const a31 = a[13];
68
+ const a32 = a[14];
69
+ const a33 = a[15];
70
+ const b00 = a00 * a11 - a01 * a10;
71
+ const b01 = a00 * a12 - a02 * a10;
72
+ const b02 = a00 * a13 - a03 * a10;
73
+ const b03 = a01 * a12 - a02 * a11;
74
+ const b04 = a01 * a13 - a03 * a11;
75
+ const b05 = a02 * a13 - a03 * a12;
76
+ const b06 = a20 * a31 - a21 * a30;
77
+ const b07 = a20 * a32 - a22 * a30;
78
+ const b08 = a20 * a33 - a23 * a30;
79
+ const b09 = a21 * a32 - a22 * a31;
80
+ const b10 = a21 * a33 - a23 * a31;
81
+ const b11 = a22 * a33 - a23 * a32;
82
+ let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
83
+ if (!det) return null;
84
+ det = 1 / det;
85
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
86
+ out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
87
+ out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
88
+ out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
89
+ out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
90
+ out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
91
+ out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
92
+ out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
93
+ out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
94
+ out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
95
+ out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
96
+ out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
97
+ out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
98
+ out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
99
+ out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
100
+ out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
101
+ return out;
102
+ }
103
+ function fromTRS(out, t, q, s) {
104
+ const x = q[0];
105
+ const y = q[1];
106
+ const z = q[2];
107
+ const w = q[3];
108
+ const sx = s[0];
109
+ const sy = s[1];
110
+ const sz = s[2];
111
+ out[0] = (1 - 2 * (y * y + z * z)) * sx;
112
+ out[1] = 2 * (x * y + w * z) * sx;
113
+ out[2] = 2 * (x * z - w * y) * sx;
114
+ out[3] = 0;
115
+ out[4] = 2 * (x * y - w * z) * sy;
116
+ out[5] = (1 - 2 * (x * x + z * z)) * sy;
117
+ out[6] = 2 * (y * z + w * x) * sy;
118
+ out[7] = 0;
119
+ out[8] = 2 * (x * z + w * y) * sz;
120
+ out[9] = 2 * (y * z - w * x) * sz;
121
+ out[10] = (1 - 2 * (x * x + y * y)) * sz;
122
+ out[11] = 0;
123
+ out[12] = t[0];
124
+ out[13] = t[1];
125
+ out[14] = t[2];
126
+ out[15] = 1;
127
+ return out;
128
+ }
129
+ function getTranslation(out, m) {
130
+ out[0] = m[12];
131
+ out[1] = m[13];
132
+ out[2] = m[14];
133
+ return out;
134
+ }
135
+ function clear(out) {
136
+ for (let i = 0; i < 16; i++) out[i] = 0;
137
+ }
138
+ function perspective(out, fovY, aspect, near, far) {
139
+ const f = 1 / Math.tan(fovY / 2);
140
+ clear(out);
141
+ out[0] = f / aspect;
142
+ out[5] = f;
143
+ out[10] = far / (near - far);
144
+ out[11] = -1;
145
+ out[14] = far * near / (near - far);
146
+ return out;
147
+ }
148
+ function perspectiveReversedZ(out, fovY, aspect, near) {
149
+ const f = 1 / Math.tan(fovY / 2);
150
+ clear(out);
151
+ out[0] = f / aspect;
152
+ out[5] = f;
153
+ out[11] = -1;
154
+ out[14] = near;
155
+ return out;
156
+ }
157
+ function orthographic(out, left, right, bottom, top, near, far) {
158
+ clear(out);
159
+ out[0] = 2 / (right - left);
160
+ out[5] = 2 / (top - bottom);
161
+ out[10] = 1 / (near - far);
162
+ out[12] = (left + right) / (left - right);
163
+ out[13] = (top + bottom) / (bottom - top);
164
+ out[14] = near / (near - far);
165
+ out[15] = 1;
166
+ return out;
167
+ }
168
+ function orthographicReversedZ(out, left, right, bottom, top, near, far) {
169
+ orthographic(out, left, right, bottom, top, near, far);
170
+ out[10] = 1 / (far - near);
171
+ out[14] = far / (far - near);
172
+ return out;
173
+ }
174
+ function lookAt(out, eye, target, up) {
175
+ let zx = eye[0] - target[0];
176
+ let zy = eye[1] - target[1];
177
+ let zz = eye[2] - target[2];
178
+ let len = Math.sqrt(zx * zx + zy * zy + zz * zz) || 1;
179
+ zx /= len;
180
+ zy /= len;
181
+ zz /= len;
182
+ let xx = up[1] * zz - up[2] * zy;
183
+ let xy = up[2] * zx - up[0] * zz;
184
+ let xz = up[0] * zy - up[1] * zx;
185
+ len = Math.sqrt(xx * xx + xy * xy + xz * xz) || 1;
186
+ xx /= len;
187
+ xy /= len;
188
+ xz /= len;
189
+ const yx = zy * xz - zz * xy;
190
+ const yy = zz * xx - zx * xz;
191
+ const yz = zx * xy - zy * xx;
192
+ out[0] = xx;
193
+ out[1] = yx;
194
+ out[2] = zx;
195
+ out[3] = 0;
196
+ out[4] = xy;
197
+ out[5] = yy;
198
+ out[6] = zy;
199
+ out[7] = 0;
200
+ out[8] = xz;
201
+ out[9] = yz;
202
+ out[10] = zz;
203
+ out[11] = 0;
204
+ out[12] = -(xx * eye[0] + xy * eye[1] + xz * eye[2]);
205
+ out[13] = -(yx * eye[0] + yy * eye[1] + yz * eye[2]);
206
+ out[14] = -(zx * eye[0] + zy * eye[1] + zz * eye[2]);
207
+ out[15] = 1;
208
+ return out;
209
+ }
210
+ export {
211
+ copy,
212
+ create,
213
+ fromTRS,
214
+ getTranslation,
215
+ identity,
216
+ invert,
217
+ lookAt,
218
+ multiply,
219
+ orthographic,
220
+ orthographicReversedZ,
221
+ perspective,
222
+ perspectiveReversedZ,
223
+ transpose
224
+ };
@@ -0,0 +1,22 @@
1
+ import type { Readable, Writable } from './types.js';
2
+ /** Quaternions are [x, y, z, w]. */
3
+ export declare const create: () => Float32Array;
4
+ export declare function identity<T extends Writable>(out: T): T;
5
+ export declare function set<T extends Writable>(out: T, x: number, y: number, z: number, w: number): T;
6
+ export declare function copy<T extends Writable>(out: T, a: Readable): T;
7
+ /** Rotation of `radians` around a unit `axis`. */
8
+ export declare function fromAxisAngle<T extends Writable>(out: T, axis: Readable, radians: number): T;
9
+ /** Euler angles in radians, applied X first, then Y, then Z (q = qz · qy · qx). */
10
+ export declare function fromEuler<T extends Writable>(out: T, x: number, y: number, z: number): T;
11
+ /** out = a · b (apply b, then a). */
12
+ export declare function multiply<T extends Writable>(out: T, a: Readable, b: Readable): T;
13
+ /** Inverse of a unit quaternion. */
14
+ export declare function conjugate<T extends Writable>(out: T, a: Readable): T;
15
+ export declare function normalize<T extends Writable>(out: T, a: Readable): T;
16
+ export declare const dot: (a: Readable, b: Readable) => number;
17
+ /** Spherical interpolation along the shortest arc. */
18
+ export declare function slerp<T extends Writable>(out: T, a: Readable, b: Readable, t: number): T;
19
+ /** Rotation from the columns of an orthonormal basis (x, y, z axes). */
20
+ export declare function fromBasis<T extends Writable>(out: T, x: Readable, y: Readable, z: Readable): T;
21
+ /** Rotation that points -Z along `forward`, keeping +Y as close to `up` as possible. */
22
+ export declare function lookRotation<T extends Writable>(out: T, forward: Readable, up: Readable): T;
@@ -0,0 +1,233 @@
1
+ const create = () => new Float32Array([0, 0, 0, 1]);
2
+ function identity(out) {
3
+ out[0] = 0;
4
+ out[1] = 0;
5
+ out[2] = 0;
6
+ out[3] = 1;
7
+ return out;
8
+ }
9
+ function set(out, x, y, z, w) {
10
+ out[0] = x;
11
+ out[1] = y;
12
+ out[2] = z;
13
+ out[3] = w;
14
+ return out;
15
+ }
16
+ function copy(out, a) {
17
+ out[0] = a[0];
18
+ out[1] = a[1];
19
+ out[2] = a[2];
20
+ out[3] = a[3];
21
+ return out;
22
+ }
23
+ function fromAxisAngle(out, axis, radians) {
24
+ const s = Math.sin(radians / 2);
25
+ out[0] = axis[0] * s;
26
+ out[1] = axis[1] * s;
27
+ out[2] = axis[2] * s;
28
+ out[3] = Math.cos(radians / 2);
29
+ return out;
30
+ }
31
+ function fromEuler(out, x, y, z) {
32
+ const sx = Math.sin(x / 2);
33
+ const cx = Math.cos(x / 2);
34
+ const sy = Math.sin(y / 2);
35
+ const cy = Math.cos(y / 2);
36
+ const sz = Math.sin(z / 2);
37
+ const cz = Math.cos(z / 2);
38
+ const bx = cy * sx;
39
+ const by = sy * cx;
40
+ const bz = -sy * sx;
41
+ const bw = cy * cx;
42
+ out[0] = cz * bx - sz * by;
43
+ out[1] = cz * by + sz * bx;
44
+ out[2] = sz * bw + cz * bz;
45
+ out[3] = cz * bw - sz * bz;
46
+ return out;
47
+ }
48
+ function multiply(out, a, b) {
49
+ const ax = a[0];
50
+ const ay = a[1];
51
+ const az = a[2];
52
+ const aw = a[3];
53
+ const bx = b[0];
54
+ const by = b[1];
55
+ const bz = b[2];
56
+ const bw = b[3];
57
+ out[0] = ax * bw + aw * bx + ay * bz - az * by;
58
+ out[1] = ay * bw + aw * by + az * bx - ax * bz;
59
+ out[2] = az * bw + aw * bz + ax * by - ay * bx;
60
+ out[3] = aw * bw - ax * bx - ay * by - az * bz;
61
+ return out;
62
+ }
63
+ function conjugate(out, a) {
64
+ out[0] = -a[0];
65
+ out[1] = -a[1];
66
+ out[2] = -a[2];
67
+ out[3] = a[3];
68
+ return out;
69
+ }
70
+ function normalize(out, a) {
71
+ const x = a[0];
72
+ const y = a[1];
73
+ const z = a[2];
74
+ const w = a[3];
75
+ const len = Math.sqrt(x * x + y * y + z * z + w * w);
76
+ const inv = len > 0 ? 1 / len : 0;
77
+ out[0] = x * inv;
78
+ out[1] = y * inv;
79
+ out[2] = z * inv;
80
+ out[3] = w * inv;
81
+ return out;
82
+ }
83
+ const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
84
+ function slerp(out, a, b, t) {
85
+ const ax = a[0];
86
+ const ay = a[1];
87
+ const az = a[2];
88
+ const aw = a[3];
89
+ let bx = b[0];
90
+ let by = b[1];
91
+ let bz = b[2];
92
+ let bw = b[3];
93
+ let cos = ax * bx + ay * by + az * bz + aw * bw;
94
+ if (cos < 0) {
95
+ cos = -cos;
96
+ bx = -bx;
97
+ by = -by;
98
+ bz = -bz;
99
+ bw = -bw;
100
+ }
101
+ let s0;
102
+ let s1;
103
+ if (1 - cos > 1e-6) {
104
+ const omega = Math.acos(cos);
105
+ const sin = Math.sin(omega);
106
+ s0 = Math.sin((1 - t) * omega) / sin;
107
+ s1 = Math.sin(t * omega) / sin;
108
+ } else {
109
+ s0 = 1 - t;
110
+ s1 = t;
111
+ }
112
+ out[0] = s0 * ax + s1 * bx;
113
+ out[1] = s0 * ay + s1 * by;
114
+ out[2] = s0 * az + s1 * bz;
115
+ out[3] = s0 * aw + s1 * bw;
116
+ return out;
117
+ }
118
+ function fromBasis(out, x, y, z) {
119
+ const m00 = x[0];
120
+ const m10 = x[1];
121
+ const m20 = x[2];
122
+ const m01 = y[0];
123
+ const m11 = y[1];
124
+ const m21 = y[2];
125
+ const m02 = z[0];
126
+ const m12 = z[1];
127
+ const m22 = z[2];
128
+ const trace = m00 + m11 + m22;
129
+ if (trace > 0) {
130
+ const s = 0.5 / Math.sqrt(trace + 1);
131
+ out[3] = 0.25 / s;
132
+ out[0] = (m21 - m12) * s;
133
+ out[1] = (m02 - m20) * s;
134
+ out[2] = (m10 - m01) * s;
135
+ } else if (m00 > m11 && m00 > m22) {
136
+ const s = 2 * Math.sqrt(1 + m00 - m11 - m22);
137
+ out[3] = (m21 - m12) / s;
138
+ out[0] = 0.25 * s;
139
+ out[1] = (m01 + m10) / s;
140
+ out[2] = (m02 + m20) / s;
141
+ } else if (m11 > m22) {
142
+ const s = 2 * Math.sqrt(1 + m11 - m00 - m22);
143
+ out[3] = (m02 - m20) / s;
144
+ out[0] = (m01 + m10) / s;
145
+ out[1] = 0.25 * s;
146
+ out[2] = (m12 + m21) / s;
147
+ } else {
148
+ const s = 2 * Math.sqrt(1 + m22 - m00 - m11);
149
+ out[3] = (m10 - m01) / s;
150
+ out[0] = (m02 + m20) / s;
151
+ out[1] = (m12 + m21) / s;
152
+ out[2] = 0.25 * s;
153
+ }
154
+ return out;
155
+ }
156
+ function lookRotation(out, forward, up) {
157
+ let zx = -forward[0];
158
+ let zy = -forward[1];
159
+ let zz = -forward[2];
160
+ let len = Math.sqrt(zx * zx + zy * zy + zz * zz) || 1;
161
+ zx /= len;
162
+ zy /= len;
163
+ zz /= len;
164
+ let xx = up[1] * zz - up[2] * zy;
165
+ let xy = up[2] * zx - up[0] * zz;
166
+ let xz = up[0] * zy - up[1] * zx;
167
+ len = Math.sqrt(xx * xx + xy * xy + xz * xz);
168
+ if (len < 1e-6) {
169
+ const hx = Math.abs(zy) < 0.99 ? 0 : 1;
170
+ const hy = Math.abs(zy) < 0.99 ? 1 : 0;
171
+ xx = hy * zz;
172
+ xy = -hx * zz;
173
+ xz = hx * zy - hy * zx;
174
+ len = Math.sqrt(xx * xx + xy * xy + xz * xz);
175
+ }
176
+ xx /= len;
177
+ xy /= len;
178
+ xz /= len;
179
+ const yx = zy * xz - zz * xy;
180
+ const yy = zz * xx - zx * xz;
181
+ const yz = zx * xy - zy * xx;
182
+ const m00 = xx;
183
+ const m10 = xy;
184
+ const m20 = xz;
185
+ const m01 = yx;
186
+ const m11 = yy;
187
+ const m21 = yz;
188
+ const m02 = zx;
189
+ const m12 = zy;
190
+ const m22 = zz;
191
+ const trace = m00 + m11 + m22;
192
+ if (trace > 0) {
193
+ const s = 0.5 / Math.sqrt(trace + 1);
194
+ out[3] = 0.25 / s;
195
+ out[0] = (m21 - m12) * s;
196
+ out[1] = (m02 - m20) * s;
197
+ out[2] = (m10 - m01) * s;
198
+ } else if (m00 > m11 && m00 > m22) {
199
+ const s = 2 * Math.sqrt(1 + m00 - m11 - m22);
200
+ out[3] = (m21 - m12) / s;
201
+ out[0] = 0.25 * s;
202
+ out[1] = (m01 + m10) / s;
203
+ out[2] = (m02 + m20) / s;
204
+ } else if (m11 > m22) {
205
+ const s = 2 * Math.sqrt(1 + m11 - m00 - m22);
206
+ out[3] = (m02 - m20) / s;
207
+ out[0] = (m01 + m10) / s;
208
+ out[1] = 0.25 * s;
209
+ out[2] = (m12 + m21) / s;
210
+ } else {
211
+ const s = 2 * Math.sqrt(1 + m22 - m00 - m11);
212
+ out[3] = (m10 - m01) / s;
213
+ out[0] = (m02 + m20) / s;
214
+ out[1] = (m12 + m21) / s;
215
+ out[2] = 0.25 * s;
216
+ }
217
+ return out;
218
+ }
219
+ export {
220
+ conjugate,
221
+ copy,
222
+ create,
223
+ dot,
224
+ fromAxisAngle,
225
+ fromBasis,
226
+ fromEuler,
227
+ identity,
228
+ lookRotation,
229
+ multiply,
230
+ normalize,
231
+ set,
232
+ slerp
233
+ };
@@ -0,0 +1,10 @@
1
+ import type { Readable, Writable } from './types.js';
2
+ /** Ray as 6 floats: `[originX, originY, originZ, dirX, dirY, dirZ]` (direction normalized). */
3
+ export declare const create: () => Float32Array;
4
+ /**
5
+ * Ray through a pixel. `x`, `y` in pixels from the top-left; `inverseViewProjection` is the inverse
6
+ * of projection · view. `reversedZ` must match the projection (the engine default is true).
7
+ */
8
+ export declare function fromScreen<T extends Writable>(out: T, x: number, y: number, width: number, height: number, inverseViewProjection: Readable, reversedZ?: boolean): T;
9
+ /** Distance along the ray to the box, or -1 on a miss. 0 if the origin is inside. */
10
+ export declare function intersectAabb(ray: Readable, box: Readable): number;
@@ -0,0 +1,54 @@
1
+ const create = () => new Float32Array([0, 0, 0, 0, 0, -1]);
2
+ function fromScreen(out, x, y, width, height, inverseViewProjection, reversedZ = true) {
3
+ const nx = x / width * 2 - 1;
4
+ const ny = 1 - y / height * 2;
5
+ const m = inverseViewProjection;
6
+ const nearZ = reversedZ ? 0.75 : 0.25;
7
+ const farZ = reversedZ ? 0.25 : 0.75;
8
+ const wn = m[3] * nx + m[7] * ny + m[11] * nearZ + m[15];
9
+ const ox = (m[0] * nx + m[4] * ny + m[8] * nearZ + m[12]) / wn;
10
+ const oy = (m[1] * nx + m[5] * ny + m[9] * nearZ + m[13]) / wn;
11
+ const oz = (m[2] * nx + m[6] * ny + m[10] * nearZ + m[14]) / wn;
12
+ const wf = m[3] * nx + m[7] * ny + m[11] * farZ + m[15];
13
+ const dx = (m[0] * nx + m[4] * ny + m[8] * farZ + m[12]) / wf - ox;
14
+ const dy = (m[1] * nx + m[5] * ny + m[9] * farZ + m[13]) / wf - oy;
15
+ const dz = (m[2] * nx + m[6] * ny + m[10] * farZ + m[14]) / wf - oz;
16
+ const len = Math.sqrt(dx * dx + dy * dy + dz * dz) || 1;
17
+ out[0] = ox;
18
+ out[1] = oy;
19
+ out[2] = oz;
20
+ out[3] = dx / len;
21
+ out[4] = dy / len;
22
+ out[5] = dz / len;
23
+ return out;
24
+ }
25
+ function intersectAabb(ray, box) {
26
+ let tMin = 0;
27
+ let tMax = Infinity;
28
+ for (let i = 0; i < 3; i++) {
29
+ const o = ray[i];
30
+ const d = ray[i + 3];
31
+ const lo = box[i];
32
+ const hi = box[i + 3];
33
+ if (Math.abs(d) < 1e-12) {
34
+ if (o < lo || o > hi) return -1;
35
+ continue;
36
+ }
37
+ let t0 = (lo - o) / d;
38
+ let t1 = (hi - o) / d;
39
+ if (t0 > t1) {
40
+ const tmp = t0;
41
+ t0 = t1;
42
+ t1 = tmp;
43
+ }
44
+ if (t0 > tMin) tMin = t0;
45
+ if (t1 < tMax) tMax = t1;
46
+ if (tMin > tMax) return -1;
47
+ }
48
+ return tMin;
49
+ }
50
+ export {
51
+ create,
52
+ fromScreen,
53
+ intersectAabb
54
+ };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Seeded random numbers (xoshiro128**: 32-bit operations only, fast in JS, good statistical
3
+ * quality). Same seed, same sequence, on every platform.
4
+ */
5
+ export declare class Rng {
6
+ readonly seed: number;
7
+ private a;
8
+ private b;
9
+ private c;
10
+ private d;
11
+ private named;
12
+ constructor(seed?: number);
13
+ /** Uniform 32-bit unsigned integer. */
14
+ nextU32(): number;
15
+ /** Uniform in [0, 1). */
16
+ float(): number;
17
+ /** Uniform in [min, max). */
18
+ range(min: number, max: number): number;
19
+ /** Uniform integer in [min, max] (inclusive). */
20
+ int(min: number, max: number): number;
21
+ bool(probability?: number): boolean;
22
+ pick<T>(items: readonly T[]): T;
23
+ /** Standard normal sample (Box-Muller). */
24
+ gaussian(mean?: number, stdDev?: number): number;
25
+ /**
26
+ * An independent stream derived from this generator's seed and a label. It doesn't depend on how
27
+ * many numbers this generator has produced, so adding draws elsewhere never changes a fork.
28
+ */
29
+ fork(label: string): Rng;
30
+ /**
31
+ * A named fork this generator keeps: the same object on every call with the label. Saves record
32
+ * each stream's state (0038), so draws after a load match draws without one.
33
+ */
34
+ stream(label: string): Rng;
35
+ /** The streams made with `stream`, by label. */
36
+ streams(): ReadonlyMap<string, Rng>;
37
+ /** The four state words: where the sequence is now. */
38
+ getState(): [number, number, number, number];
39
+ /** Continues from a state `getState` returned. */
40
+ setState(state: readonly number[]): void;
41
+ }
@@ -0,0 +1,102 @@
1
+ import { hashSeed } from "./hash.js";
2
+ function splitmix32(state) {
3
+ state.s = state.s + 2654435769 | 0;
4
+ let z = state.s;
5
+ z = Math.imul(z ^ z >>> 16, 2246822507);
6
+ z = Math.imul(z ^ z >>> 13, 3266489909);
7
+ return (z ^ z >>> 16) >>> 0;
8
+ }
9
+ class Rng {
10
+ seed;
11
+ a;
12
+ b;
13
+ c;
14
+ d;
15
+ named;
16
+ constructor(seed = 0) {
17
+ this.seed = seed >>> 0;
18
+ const state = { s: this.seed };
19
+ this.a = splitmix32(state);
20
+ this.b = splitmix32(state);
21
+ this.c = splitmix32(state);
22
+ this.d = splitmix32(state);
23
+ }
24
+ /** Uniform 32-bit unsigned integer. */
25
+ nextU32() {
26
+ const result = Math.imul(rotl(Math.imul(this.b, 5), 7), 9) >>> 0;
27
+ const t = this.b << 9;
28
+ this.c ^= this.a;
29
+ this.d ^= this.b;
30
+ this.b ^= this.c;
31
+ this.a ^= this.d;
32
+ this.c ^= t;
33
+ this.d = rotl(this.d, 11);
34
+ return result;
35
+ }
36
+ /** Uniform in [0, 1). */
37
+ float() {
38
+ return this.nextU32() / 4294967296;
39
+ }
40
+ /** Uniform in [min, max). */
41
+ range(min, max) {
42
+ return min + (max - min) * this.float();
43
+ }
44
+ /** Uniform integer in [min, max] (inclusive). */
45
+ int(min, max) {
46
+ return min + Math.floor(this.float() * (max - min + 1));
47
+ }
48
+ bool(probability = 0.5) {
49
+ return this.float() < probability;
50
+ }
51
+ pick(items) {
52
+ return items[Math.floor(this.float() * items.length)];
53
+ }
54
+ /** Standard normal sample (Box-Muller). */
55
+ gaussian(mean = 0, stdDev = 1) {
56
+ const u = 1 - this.float();
57
+ const v = this.float();
58
+ return mean + stdDev * Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
59
+ }
60
+ /**
61
+ * An independent stream derived from this generator's seed and a label. It doesn't depend on how
62
+ * many numbers this generator has produced, so adding draws elsewhere never changes a fork.
63
+ */
64
+ fork(label) {
65
+ return new Rng(hashSeed(this.seed, label));
66
+ }
67
+ /**
68
+ * A named fork this generator keeps: the same object on every call with the label. Saves record
69
+ * each stream's state (0038), so draws after a load match draws without one.
70
+ */
71
+ stream(label) {
72
+ this.named ??= /* @__PURE__ */ new Map();
73
+ let rng = this.named.get(label);
74
+ if (!rng) {
75
+ rng = this.fork(label);
76
+ this.named.set(label, rng);
77
+ }
78
+ return rng;
79
+ }
80
+ /** The streams made with `stream`, by label. */
81
+ streams() {
82
+ return this.named ?? EMPTY;
83
+ }
84
+ /** The four state words: where the sequence is now. */
85
+ getState() {
86
+ return [this.a >>> 0, this.b >>> 0, this.c >>> 0, this.d >>> 0];
87
+ }
88
+ /** Continues from a state `getState` returned. */
89
+ setState(state) {
90
+ this.a = state[0] | 0;
91
+ this.b = state[1] | 0;
92
+ this.c = state[2] | 0;
93
+ this.d = state[3] | 0;
94
+ }
95
+ }
96
+ const EMPTY = /* @__PURE__ */ new Map();
97
+ function rotl(x, k) {
98
+ return x << k | x >>> 32 - k;
99
+ }
100
+ export {
101
+ Rng
102
+ };
@@ -0,0 +1,8 @@
1
+ /** Anything readable by index: tuples, TypedArrays, column views. */
2
+ export type Readable = ArrayLike<number>;
3
+ /** Anything writable by index. Math functions write results into one of these. */
4
+ export interface Writable {
5
+ readonly length: number;
6
+ [index: number]: number;
7
+ }
8
+ export declare const EPSILON = 0.000001;
@@ -0,0 +1,4 @@
1
+ const EPSILON = 1e-6;
2
+ export {
3
+ EPSILON
4
+ };