@carbonenginejs/runtime-utils 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/LICENSE +21 -0
- package/NOTICE +23 -0
- package/README.md +56 -0
- package/THIRD-PARTY-NOTICES.md +38 -0
- package/docs/README.md +81 -0
- package/docs/architecture.md +101 -0
- package/docs/concepts/foundation-consolidation.md +86 -0
- package/docs/const-kb.md +92 -0
- package/docs/core-types/DECORATOR-TODOS.md +25 -0
- package/docs/core-types/README.md +203 -0
- package/docs/reference/api.md +70 -0
- package/docs/reference/classes/README.md +115 -0
- package/package.json +132 -0
- package/src/arrays.js +5 -0
- package/src/audio/audioFormats.js +34 -0
- package/src/audio/index.js +1 -0
- package/src/box3.js +1385 -0
- package/src/bytes.js +56 -0
- package/src/compression.js +56 -0
- package/src/constants/index.js +6 -0
- package/src/constants.js +15 -0
- package/src/curve.js +419 -0
- package/src/d3d/dxgiFormats.js +46 -0
- package/src/d3d/index.js +2 -0
- package/src/d3d/primitiveTopology.js +11 -0
- package/src/document/CjsCarbonDocument.js +212 -0
- package/src/document/CjsClassRegistry.js +373 -0
- package/src/document/CjsDocumentDehydrator.js +142 -0
- package/src/document/CjsDocumentHydrator.js +156 -0
- package/src/document/CjsStructRegistry.js +348 -0
- package/src/document/hydrationAdapter.js +129 -0
- package/src/document/index.js +6 -0
- package/src/geometry/box.js +137 -0
- package/src/geometry/cylinder.js +244 -0
- package/src/geometry/helpers/LICENSE +15 -0
- package/src/geometry/helpers/earcut.js +766 -0
- package/src/geometry/helpers/misc.js +103 -0
- package/src/geometry/index.js +8 -0
- package/src/geometry/json.js +165 -0
- package/src/geometry/lathe.js +172 -0
- package/src/geometry/octahedron.js +0 -0
- package/src/geometry/plane.js +81 -0
- package/src/geometry/shape.js +95 -0
- package/src/geometry/sphere.js +123 -0
- package/src/geometry/torus.js +96 -0
- package/src/graphics/colorSpaces.js +22 -0
- package/src/graphics/index.js +4 -0
- package/src/graphics/pixelFormats.js +158 -0
- package/src/graphics/textureDimensions.js +22 -0
- package/src/graphics/trinityEnums.js +87 -0
- package/src/index.js +58 -0
- package/src/is.js +524 -0
- package/src/json.js +23 -0
- package/src/lifecycle/CjsLifecycleState.js +77 -0
- package/src/lifecycle/index.js +1 -0
- package/src/lne3.js +497 -0
- package/src/lookup.js +48 -0
- package/src/mat3.js +131 -0
- package/src/mat4.js +699 -0
- package/src/math/index.js +25 -0
- package/src/math/scalar.js +63 -0
- package/src/media/index.js +1 -0
- package/src/media/mediaTypes.js +50 -0
- package/src/mesh.js +394 -0
- package/src/model/CjsEventEmitter.js +333 -0
- package/src/model/CjsModel.js +1544 -0
- package/src/model/CjsModelState.js +72 -0
- package/src/model/index.js +4 -0
- package/src/model/sourceRecordUtils.js +54 -0
- package/src/noise.js +310 -0
- package/src/num.js +827 -0
- package/src/path.js +21 -0
- package/src/pln.js +762 -0
- package/src/pool.js +160 -0
- package/src/quat.js +144 -0
- package/src/ray3.js +1085 -0
- package/src/renderContext/formats.js +145 -0
- package/src/renderContext/index.js +5 -0
- package/src/renderContext/presentation.js +125 -0
- package/src/renderContext/resources.js +27 -0
- package/src/renderContext/upscaling.js +22 -0
- package/src/renderContext/window.js +20 -0
- package/src/runtime/CjsRuntimeState.js +50 -0
- package/src/schema/CjsSchema.js +1009 -0
- package/src/schema/index.js +17 -0
- package/src/shader/index.js +1 -0
- package/src/shader/shaderStages.js +37 -0
- package/src/sph3.js +754 -0
- package/src/tangent.js +288 -0
- package/src/text.js +40 -0
- package/src/tri3.js +650 -0
- package/src/types/carbonTypes.js +635 -0
- package/src/types/index.js +2 -0
- package/src/utils.js +58 -0
- package/src/validation.js +46 -0
- package/src/vec2.js +229 -0
- package/src/vec3.js +1172 -0
- package/src/vec4.js +347 -0
- package/src/vertex.js +108 -0
- package/src/webgpu/index.js +1 -0
- package/src/webgpu/textureFormats.js +121 -0
package/src/mat4.js
ADDED
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
import * as glMat4 from "gl-matrix/esm/mat4.js";
|
|
2
|
+
import {
|
|
3
|
+
cross as crossVec3,
|
|
4
|
+
dot as dotVec3,
|
|
5
|
+
normalize as normalizeVec3,
|
|
6
|
+
squaredLength as squaredLengthVec3,
|
|
7
|
+
subtract as subtractVec3
|
|
8
|
+
} from "gl-matrix/esm/vec3.js";
|
|
9
|
+
import { pool } from "./pool.js";
|
|
10
|
+
|
|
11
|
+
const mat4 = { ...glMat4 };
|
|
12
|
+
|
|
13
|
+
export { mat4 };
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @param {mat4} m
|
|
18
|
+
* @param {quat} rotation
|
|
19
|
+
* @param {vec3} translation
|
|
20
|
+
* @param {vec3} scaling
|
|
21
|
+
* @returns {mat4} m
|
|
22
|
+
*/
|
|
23
|
+
mat4.decompose = function (m, rotation, translation, scaling)
|
|
24
|
+
{
|
|
25
|
+
let
|
|
26
|
+
scaleX = Math.hypot(m[0], m[1], m[2]),
|
|
27
|
+
scaleY = Math.hypot(m[4], m[5], m[6]),
|
|
28
|
+
scaleZ = Math.hypot(m[8], m[9], m[10]);
|
|
29
|
+
|
|
30
|
+
if (mat4.determinant(m) < 0) scaleX = -scaleX;
|
|
31
|
+
|
|
32
|
+
translation[0] = m[12];
|
|
33
|
+
translation[1] = m[13];
|
|
34
|
+
translation[2] = m[14];
|
|
35
|
+
scaling[0] = scaleX;
|
|
36
|
+
scaling[1] = scaleY;
|
|
37
|
+
scaling[2] = scaleZ;
|
|
38
|
+
|
|
39
|
+
const nonZeroScaleCount = Number(scaleX !== 0) + Number(scaleY !== 0) + Number(scaleZ !== 0);
|
|
40
|
+
if (nonZeroScaleCount > 0)
|
|
41
|
+
{
|
|
42
|
+
const normalized = pool.allocF32(16);
|
|
43
|
+
mat4.copy(normalized, m);
|
|
44
|
+
|
|
45
|
+
if (scaleX !== 0)
|
|
46
|
+
{
|
|
47
|
+
normalized[0] /= scaleX;
|
|
48
|
+
normalized[1] /= scaleX;
|
|
49
|
+
normalized[2] /= scaleX;
|
|
50
|
+
}
|
|
51
|
+
if (scaleY !== 0)
|
|
52
|
+
{
|
|
53
|
+
normalized[4] /= scaleY;
|
|
54
|
+
normalized[5] /= scaleY;
|
|
55
|
+
normalized[6] /= scaleY;
|
|
56
|
+
}
|
|
57
|
+
if (scaleZ !== 0)
|
|
58
|
+
{
|
|
59
|
+
normalized[8] /= scaleZ;
|
|
60
|
+
normalized[9] /= scaleZ;
|
|
61
|
+
normalized[10] /= scaleZ;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (nonZeroScaleCount >= 2 && scaleX === 0)
|
|
65
|
+
{
|
|
66
|
+
normalized[0] = normalized[5] * normalized[10] - normalized[6] * normalized[9];
|
|
67
|
+
normalized[1] = normalized[6] * normalized[8] - normalized[4] * normalized[10];
|
|
68
|
+
normalized[2] = normalized[4] * normalized[9] - normalized[5] * normalized[8];
|
|
69
|
+
}
|
|
70
|
+
else if (nonZeroScaleCount >= 2 && scaleY === 0)
|
|
71
|
+
{
|
|
72
|
+
normalized[4] = normalized[9] * normalized[2] - normalized[10] * normalized[1];
|
|
73
|
+
normalized[5] = normalized[10] * normalized[0] - normalized[8] * normalized[2];
|
|
74
|
+
normalized[6] = normalized[8] * normalized[1] - normalized[9] * normalized[0];
|
|
75
|
+
}
|
|
76
|
+
else if (nonZeroScaleCount >= 2 && scaleZ === 0)
|
|
77
|
+
{
|
|
78
|
+
normalized[8] = normalized[1] * normalized[6] - normalized[2] * normalized[5];
|
|
79
|
+
normalized[9] = normalized[2] * normalized[4] - normalized[0] * normalized[6];
|
|
80
|
+
normalized[10] = normalized[0] * normalized[5] - normalized[1] * normalized[4];
|
|
81
|
+
}
|
|
82
|
+
else if (nonZeroScaleCount === 1)
|
|
83
|
+
{
|
|
84
|
+
const
|
|
85
|
+
x = normalized.subarray(0, 3),
|
|
86
|
+
y = normalized.subarray(4, 7),
|
|
87
|
+
z = normalized.subarray(8, 11);
|
|
88
|
+
|
|
89
|
+
if (scaleX !== 0)
|
|
90
|
+
{
|
|
91
|
+
const helper = Math.abs(x[1]) < 0.9 ? [ 0, 1, 0 ] : [ 0, 0, 1 ];
|
|
92
|
+
normalizeVec3(z, crossVec3(z, x, helper));
|
|
93
|
+
crossVec3(y, z, x);
|
|
94
|
+
}
|
|
95
|
+
else if (scaleY !== 0)
|
|
96
|
+
{
|
|
97
|
+
const helper = Math.abs(y[2]) < 0.9 ? [ 0, 0, 1 ] : [ 1, 0, 0 ];
|
|
98
|
+
normalizeVec3(x, crossVec3(x, y, helper));
|
|
99
|
+
crossVec3(z, x, y);
|
|
100
|
+
}
|
|
101
|
+
else
|
|
102
|
+
{
|
|
103
|
+
const helper = Math.abs(z[1]) < 0.9 ? [ 0, 1, 0 ] : [ 1, 0, 0 ];
|
|
104
|
+
normalizeVec3(x, crossVec3(x, helper, z));
|
|
105
|
+
crossVec3(y, z, x);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
mat4.getRotation(rotation, normalized);
|
|
110
|
+
pool.freeType(normalized);
|
|
111
|
+
}
|
|
112
|
+
else
|
|
113
|
+
{
|
|
114
|
+
rotation[0] = 0;
|
|
115
|
+
rotation[1] = 0;
|
|
116
|
+
rotation[2] = 0;
|
|
117
|
+
rotation[3] = 1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return m;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Allocates a pooled mat4
|
|
125
|
+
* @returns {Float32Array|mat4}
|
|
126
|
+
*/
|
|
127
|
+
mat4.alloc = function ()
|
|
128
|
+
{
|
|
129
|
+
return pool.allocF32(16);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Unallocates a pooled mat4
|
|
134
|
+
* @param {mat4|Float32Array} a
|
|
135
|
+
*/
|
|
136
|
+
mat4.unalloc = function (a)
|
|
137
|
+
{
|
|
138
|
+
pool.freeType(a);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Sets a mat4 from a bone joint mat
|
|
143
|
+
* @param {mat4} out
|
|
144
|
+
* @param {Float32Array} jointMat
|
|
145
|
+
* @param {Number} index
|
|
146
|
+
* @return {mat4}
|
|
147
|
+
*/
|
|
148
|
+
mat4.fromJointMatIndex = function (out, jointMat, index)
|
|
149
|
+
{
|
|
150
|
+
if (index >= 0)
|
|
151
|
+
{
|
|
152
|
+
const offset = index * 12;
|
|
153
|
+
out[0] = jointMat[offset];
|
|
154
|
+
out[1] = jointMat[offset + 4];
|
|
155
|
+
out[2] = jointMat[offset + 8];
|
|
156
|
+
out[3] = 0;
|
|
157
|
+
out[4] = jointMat[offset + 1];
|
|
158
|
+
out[5] = jointMat[offset + 5];
|
|
159
|
+
out[6] = jointMat[offset + 9];
|
|
160
|
+
out[7] = 0;
|
|
161
|
+
out[8] = jointMat[offset + 2];
|
|
162
|
+
out[9] = jointMat[offset + 6];
|
|
163
|
+
out[10] = jointMat[offset + 10];
|
|
164
|
+
out[11] = 0;
|
|
165
|
+
out[12] = jointMat[offset + 3];
|
|
166
|
+
out[13] = jointMat[offset + 7];
|
|
167
|
+
out[14] = jointMat[offset + 11];
|
|
168
|
+
out[15] = 1;
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return mat4.identity(out);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* arcFromForward
|
|
177
|
+
* @param {mat4} out
|
|
178
|
+
* @param {vec3} v
|
|
179
|
+
* @return {mat4} out
|
|
180
|
+
*/
|
|
181
|
+
mat4.arcFromForward = function (out, v)
|
|
182
|
+
{
|
|
183
|
+
const norm = normalizeVec3(pool.allocF32(3), v);
|
|
184
|
+
|
|
185
|
+
mat4.identity(out);
|
|
186
|
+
|
|
187
|
+
if (squaredLengthVec3(v) === 0)
|
|
188
|
+
{
|
|
189
|
+
pool.freeType(norm);
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (norm[2] < -0.99999)
|
|
194
|
+
{
|
|
195
|
+
pool.freeType(norm);
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (norm[2] > 0.99999)
|
|
200
|
+
{
|
|
201
|
+
out[5] = -1.0;
|
|
202
|
+
out[10] = -1.0;
|
|
203
|
+
pool.freeType(norm);
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const h = (1 + norm[2]) / (norm[0] * norm[0] + norm[1] * norm[1]);
|
|
208
|
+
|
|
209
|
+
out[0] = h * norm[1] * norm[1] - norm[2];
|
|
210
|
+
out[1] = -h * norm[0] * norm[1];
|
|
211
|
+
out[2] = norm[0];
|
|
212
|
+
|
|
213
|
+
out[4] = out[1];
|
|
214
|
+
out[5] = h * norm[0] * norm[0] - norm[2];
|
|
215
|
+
out[6] = norm[1];
|
|
216
|
+
|
|
217
|
+
out[8] = -norm[0];
|
|
218
|
+
out[9] = -norm[1];
|
|
219
|
+
out[10] = -norm[2];
|
|
220
|
+
|
|
221
|
+
pool.freeType(norm);
|
|
222
|
+
return out;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Copies the translation component from one mat4 to another
|
|
227
|
+
* @param {mat4} out
|
|
228
|
+
* @param {mat4} a
|
|
229
|
+
* @returns {mat4} out
|
|
230
|
+
*/
|
|
231
|
+
mat4.copyTranslation = function (out, a)
|
|
232
|
+
{
|
|
233
|
+
out[12] = a[12];
|
|
234
|
+
out[13] = a[13];
|
|
235
|
+
out[14] = a[14];
|
|
236
|
+
return out;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Sets a mat4 from a mat4
|
|
241
|
+
* @param {mat4} out
|
|
242
|
+
* @param {mat3} m
|
|
243
|
+
* @returns {mat4} out
|
|
244
|
+
*/
|
|
245
|
+
mat4.fromMat3 = function (out, m)
|
|
246
|
+
{
|
|
247
|
+
out[0] = m[0];
|
|
248
|
+
out[1] = m[1];
|
|
249
|
+
out[2] = m[2];
|
|
250
|
+
out[4] = m[3];
|
|
251
|
+
out[5] = m[4];
|
|
252
|
+
out[6] = m[5];
|
|
253
|
+
out[8] = m[6];
|
|
254
|
+
out[9] = m[7];
|
|
255
|
+
out[10] = m[8];
|
|
256
|
+
out[3] = out[7] = out[11] = out[12] = out[13] = out[14] = 0;
|
|
257
|
+
out[15] = 1;
|
|
258
|
+
return out;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
// D3D ortho, depth maps to [0..1]
|
|
262
|
+
mat4.orthoD3D = function(out, l, r, b, t, n, f)
|
|
263
|
+
{
|
|
264
|
+
const
|
|
265
|
+
lr = 1 / (r - l),
|
|
266
|
+
bt = 1 / (t - b),
|
|
267
|
+
nf = 1 / (f - n);
|
|
268
|
+
|
|
269
|
+
out[0] = 2 * lr;
|
|
270
|
+
out[4] = 0;
|
|
271
|
+
out[8] = 0;
|
|
272
|
+
out[12] = -(r + l) * lr;
|
|
273
|
+
out[1] = 0;
|
|
274
|
+
out[5] = 2 * bt;
|
|
275
|
+
out[9] = 0;
|
|
276
|
+
out[13] = -(t + b) * bt;
|
|
277
|
+
out[2] = 0;
|
|
278
|
+
out[6] = 0;
|
|
279
|
+
out[10] = nf;
|
|
280
|
+
out[14] = -n * nf;
|
|
281
|
+
out[3] = 0;
|
|
282
|
+
out[7] = 0;
|
|
283
|
+
out[11] = 0;
|
|
284
|
+
out[15] = 1;
|
|
285
|
+
return out;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Left-handed look-at (D3D-style): +Z forward
|
|
290
|
+
* Column-major (gl-matrix style)
|
|
291
|
+
*/
|
|
292
|
+
mat4.lookAtD3D = function (out, eye, center, up)
|
|
293
|
+
{
|
|
294
|
+
const x = pool.allocF32(3);
|
|
295
|
+
const y = pool.allocF32(3);
|
|
296
|
+
const z = pool.allocF32(3);
|
|
297
|
+
|
|
298
|
+
// z = forward = normalize(center - eye) (LH)
|
|
299
|
+
subtractVec3(z, center, eye);
|
|
300
|
+
|
|
301
|
+
if (squaredLengthVec3(z) === 0)
|
|
302
|
+
{
|
|
303
|
+
z[2] = 1;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
normalizeVec3(z, z);
|
|
307
|
+
|
|
308
|
+
// x = normalize(cross(up, z))
|
|
309
|
+
crossVec3(x, up, z);
|
|
310
|
+
|
|
311
|
+
if (squaredLengthVec3(x) === 0)
|
|
312
|
+
{
|
|
313
|
+
if (Math.abs(z[1]) < 0.999)
|
|
314
|
+
{
|
|
315
|
+
x[0] = z[2];
|
|
316
|
+
x[1] = 0;
|
|
317
|
+
x[2] = -z[0];
|
|
318
|
+
}
|
|
319
|
+
else
|
|
320
|
+
{
|
|
321
|
+
x[0] = 0;
|
|
322
|
+
x[1] = -z[2];
|
|
323
|
+
x[2] = z[1];
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
normalizeVec3(x, x);
|
|
328
|
+
|
|
329
|
+
// y = cross(z, x)
|
|
330
|
+
crossVec3(y, z, x);
|
|
331
|
+
|
|
332
|
+
// View rotation (camera axes in rows)
|
|
333
|
+
out[0] = x[0]; out[1] = y[0]; out[2] = z[0]; out[3] = 0;
|
|
334
|
+
out[4] = x[1]; out[5] = y[1]; out[6] = z[1]; out[7] = 0;
|
|
335
|
+
out[8] = x[2]; out[9] = y[2]; out[10] = z[2]; out[11] = 0;
|
|
336
|
+
|
|
337
|
+
// Translation
|
|
338
|
+
out[12] = -dotVec3(x, eye);
|
|
339
|
+
out[13] = -dotVec3(y, eye);
|
|
340
|
+
out[14] = -dotVec3(z, eye);
|
|
341
|
+
out[15] = 1;
|
|
342
|
+
|
|
343
|
+
pool.freeType(x);
|
|
344
|
+
pool.freeType(y);
|
|
345
|
+
pool.freeType(z);
|
|
346
|
+
|
|
347
|
+
return out;
|
|
348
|
+
|
|
349
|
+
// After calling lookAtD3D(out, eye, center, up):
|
|
350
|
+
// Transform center by out and it should land on +Z axis (x≈0, y≈0, z>0).
|
|
351
|
+
// Transform eye by out and it should land at the origin (0,0,0).
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Builds a rotation-only look-at basis
|
|
356
|
+
* OpenGL / RH convention
|
|
357
|
+
* −Z is forward
|
|
358
|
+
* Does NOT touch translation
|
|
359
|
+
* Copies translation (and row 3) from an existing matrix
|
|
360
|
+
* Safe for column-major, gl-matrix layout
|
|
361
|
+
*
|
|
362
|
+
* @param {mat4} out - result
|
|
363
|
+
* @param {mat4} m - source matrix
|
|
364
|
+
* @param {vec3} eye - Position of the viewer
|
|
365
|
+
* @param {vec3} center - Point the viewer is looking at
|
|
366
|
+
* @param {vec3} up - vec3 pointing up
|
|
367
|
+
* @returns {mat4} out
|
|
368
|
+
*/
|
|
369
|
+
mat4.setLookRotation = function (out, m, eye, center, up)
|
|
370
|
+
{
|
|
371
|
+
const
|
|
372
|
+
x = pool.allocF32(3),
|
|
373
|
+
y = pool.allocF32(3),
|
|
374
|
+
z = pool.allocF32(3),
|
|
375
|
+
u = pool.allocF32(3); // safeUp
|
|
376
|
+
|
|
377
|
+
// z axis = eye - center (camera backward); -z is forward
|
|
378
|
+
subtractVec3(z, eye, center);
|
|
379
|
+
|
|
380
|
+
if (squaredLengthVec3(z) === 0)
|
|
381
|
+
{
|
|
382
|
+
// arbitrary (back)
|
|
383
|
+
z[2] = 1;
|
|
384
|
+
}
|
|
385
|
+
normalizeVec3(z, z);
|
|
386
|
+
|
|
387
|
+
// Pick a stable up if the provided up is too aligned with z
|
|
388
|
+
normalizeVec3(u, up);
|
|
389
|
+
|
|
390
|
+
// if |dot(up, z)| is ~1 then up × z is unstable
|
|
391
|
+
const dz = Math.abs(u[0] * z[0] + u[1] * z[1] + u[2] * z[2]);
|
|
392
|
+
if (dz > 0.9995)
|
|
393
|
+
{
|
|
394
|
+
// choose an alternate up axis that is not parallel to z
|
|
395
|
+
// try Z axis first, then X axis if needed
|
|
396
|
+
u[0] = 0; u[1] = 0; u[2] = 1;
|
|
397
|
+
const dz2 = Math.abs(u[0] * z[0] + u[1] * z[1] + u[2] * z[2]);
|
|
398
|
+
if (dz2 > 0.9995)
|
|
399
|
+
{
|
|
400
|
+
u[0] = 1; u[1] = 0; u[2] = 0;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// x = up × z
|
|
405
|
+
crossVec3(x, u, z);
|
|
406
|
+
|
|
407
|
+
// Still degenerate? (can happen if 'up' was zero-length etc.)
|
|
408
|
+
if (squaredLengthVec3(x) === 0)
|
|
409
|
+
{
|
|
410
|
+
// fall back to a guaranteed-not-parallel up using z's dominant axis
|
|
411
|
+
if (Math.abs(z[1]) < 0.999)
|
|
412
|
+
{
|
|
413
|
+
u[0] = 0; u[1] = 1; u[2] = 0;
|
|
414
|
+
}
|
|
415
|
+
else
|
|
416
|
+
{
|
|
417
|
+
u[0] = 1; u[1] = 0; u[2] = 0;
|
|
418
|
+
}
|
|
419
|
+
crossVec3(x, u, z);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
normalizeVec3(x, x);
|
|
423
|
+
|
|
424
|
+
// y = z × x
|
|
425
|
+
crossVec3(y, z, x);
|
|
426
|
+
|
|
427
|
+
// write rotation (columns)
|
|
428
|
+
out[0] = x[0]; out[1] = x[1]; out[2] = x[2];
|
|
429
|
+
out[4] = y[0]; out[5] = y[1]; out[6] = y[2];
|
|
430
|
+
out[8] = z[0]; out[9] = z[1]; out[10] = z[2];
|
|
431
|
+
|
|
432
|
+
// copy the rest
|
|
433
|
+
if (out !== m)
|
|
434
|
+
{
|
|
435
|
+
out[3] = m[3];
|
|
436
|
+
out[7] = m[7];
|
|
437
|
+
out[11] = m[11];
|
|
438
|
+
out[12] = m[12];
|
|
439
|
+
out[13] = m[13];
|
|
440
|
+
out[14] = m[14];
|
|
441
|
+
out[15] = m[15];
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
pool.freeType(x);
|
|
445
|
+
pool.freeType(y);
|
|
446
|
+
pool.freeType(z);
|
|
447
|
+
pool.freeType(u);
|
|
448
|
+
|
|
449
|
+
return out;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Gets a mat4's maximum column axis scale
|
|
454
|
+
*
|
|
455
|
+
* @param {mat4} a - source mat4
|
|
456
|
+
* @returns {number} - maximum axis scale
|
|
457
|
+
*/
|
|
458
|
+
mat4.maxScaleOnAxis = function (a)
|
|
459
|
+
{
|
|
460
|
+
let x = a[0] * a[0] + a[1] * a[1] + a[2] * a[2],
|
|
461
|
+
y = a[4] * a[4] + a[5] * a[5] + a[6] * a[6],
|
|
462
|
+
z = a[8] * a[8] + a[9] * a[9] + a[10] * a[10];
|
|
463
|
+
|
|
464
|
+
return Math.sqrt(Math.max(x, y, z));
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Sets an OpenGL-style right-handed perspective with NDC depth in [-1, 1]
|
|
469
|
+
* @param {mat4} out - receiving mat4
|
|
470
|
+
* @param {number} fovY - Vertical field of view in radians
|
|
471
|
+
* @param {number} aspect - Aspect ratio. typically viewport width/height
|
|
472
|
+
* @param {number} near - Near bound of the frustum
|
|
473
|
+
* @param {number} far - Far bound of the frustum
|
|
474
|
+
* @returns {mat4} out - receiving mat4
|
|
475
|
+
*/
|
|
476
|
+
mat4.perspectiveGL = function (out, fovY, aspect, near, far)
|
|
477
|
+
{
|
|
478
|
+
let fH = Math.tan(fovY * 0.5) * near;
|
|
479
|
+
let fW = fH * aspect;
|
|
480
|
+
mat4.frustum(out, -fW, fW, -fH, fH, near, far);
|
|
481
|
+
return out;
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Projects a vector from 3d to 2d space, returning normalized screen space value
|
|
486
|
+
* m should be a projection matrix (or a VP or MVP)
|
|
487
|
+
* @author https://github.com/hughsk/from-3d-to-2d/blob/master/index.js
|
|
488
|
+
* @param {vec3} out - receiving vec3
|
|
489
|
+
* @param {mat4} m - Projection / View Projection
|
|
490
|
+
* @param {vec3} a - the point to project
|
|
491
|
+
* @returns {vec3} out - receiving vec3
|
|
492
|
+
*/
|
|
493
|
+
mat4.projectVec3 = function (out, m, a)
|
|
494
|
+
{
|
|
495
|
+
let
|
|
496
|
+
ix = a[0],
|
|
497
|
+
iy = a[1],
|
|
498
|
+
iz = a[2];
|
|
499
|
+
|
|
500
|
+
let ox = m[0] * ix + m[4] * iy + m[8] * iz + m[12],
|
|
501
|
+
oy = m[1] * ix + m[5] * iy + m[9] * iz + m[13],
|
|
502
|
+
oz = m[2] * ix + m[6] * iy + m[10] * iz + m[14],
|
|
503
|
+
ow = m[3] * ix + m[7] * iy + m[11] * iz + m[15];
|
|
504
|
+
|
|
505
|
+
out[0] = (ox / ow + 1) / 2;
|
|
506
|
+
out[1] = (oy / ow + 1) / 2;
|
|
507
|
+
out[2] = (oz / ow + 1) / 2;
|
|
508
|
+
return out;
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Sets the translation component of a mat4 from a vec3
|
|
514
|
+
* @param {mat4} out
|
|
515
|
+
* @param {vec3} v
|
|
516
|
+
* @returns {mat4} out
|
|
517
|
+
*/
|
|
518
|
+
mat4.setTranslation = function (out, v)
|
|
519
|
+
{
|
|
520
|
+
out[12] = v[0];
|
|
521
|
+
out[13] = v[1];
|
|
522
|
+
out[14] = v[2];
|
|
523
|
+
return out;
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Sets the translation component of a mat4 from values
|
|
528
|
+
* @param {mat4} out
|
|
529
|
+
* @param {number} x
|
|
530
|
+
* @param {number} y
|
|
531
|
+
* @param {number} z
|
|
532
|
+
* @returns {mat4} out
|
|
533
|
+
*/
|
|
534
|
+
mat4.setTranslationFromValues = function (out, x, y, z)
|
|
535
|
+
{
|
|
536
|
+
out[12] = x;
|
|
537
|
+
out[13] = y;
|
|
538
|
+
out[14] = z;
|
|
539
|
+
return out;
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* @author three.js authors
|
|
544
|
+
* @param out
|
|
545
|
+
* @param left
|
|
546
|
+
* @param right
|
|
547
|
+
* @param top
|
|
548
|
+
* @param bottom
|
|
549
|
+
* @param near
|
|
550
|
+
* @param far
|
|
551
|
+
* @returns {*}
|
|
552
|
+
*/
|
|
553
|
+
mat4.makePerspective = function (out, left, right, top, bottom, near, far)
|
|
554
|
+
{
|
|
555
|
+
let x = 2 * near / (right - left),
|
|
556
|
+
y = 2 * near / (top - bottom);
|
|
557
|
+
|
|
558
|
+
let a = (right + left) / (right - left),
|
|
559
|
+
b = (top + bottom) / (top - bottom),
|
|
560
|
+
c = -(far + near) / (far - near),
|
|
561
|
+
d = -2 * far * near / (far - near);
|
|
562
|
+
|
|
563
|
+
out[0] = x;
|
|
564
|
+
out[4] = 0;
|
|
565
|
+
out[8] = a;
|
|
566
|
+
out[12] = 0;
|
|
567
|
+
|
|
568
|
+
out[1] = 0;
|
|
569
|
+
out[5] = y;
|
|
570
|
+
out[9] = b;
|
|
571
|
+
out[13] = 0;
|
|
572
|
+
|
|
573
|
+
out[2] = 0;
|
|
574
|
+
out[6] = 0;
|
|
575
|
+
out[10] = c;
|
|
576
|
+
out[14] = d;
|
|
577
|
+
|
|
578
|
+
out[3] = 0;
|
|
579
|
+
out[7] = 0;
|
|
580
|
+
out[11] = -1;
|
|
581
|
+
out[15] = 0;
|
|
582
|
+
|
|
583
|
+
return out;
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* @author three.js authors
|
|
588
|
+
* @param out
|
|
589
|
+
* @param left
|
|
590
|
+
* @param right
|
|
591
|
+
* @param top
|
|
592
|
+
* @param bottom
|
|
593
|
+
* @param near
|
|
594
|
+
* @param far
|
|
595
|
+
* @returns {mat4}
|
|
596
|
+
*/
|
|
597
|
+
mat4.makeOrthographic = function (out, left, right, top, bottom, near, far)
|
|
598
|
+
{
|
|
599
|
+
let w = 1.0 / (right - left),
|
|
600
|
+
h = 1.0 / (top - bottom),
|
|
601
|
+
p = 1.0 / (far - near);
|
|
602
|
+
|
|
603
|
+
let x = (right + left) * w,
|
|
604
|
+
y = (top + bottom) * h,
|
|
605
|
+
z = (far + near) * p;
|
|
606
|
+
|
|
607
|
+
out[0] = 2 * w;
|
|
608
|
+
out[4] = 0;
|
|
609
|
+
out[8] = 0;
|
|
610
|
+
out[12] = -x;
|
|
611
|
+
|
|
612
|
+
out[1] = 0;
|
|
613
|
+
out[5] = 2 * h;
|
|
614
|
+
out[9] = 0;
|
|
615
|
+
out[13] = -y;
|
|
616
|
+
|
|
617
|
+
out[2] = 0;
|
|
618
|
+
out[6] = 0;
|
|
619
|
+
out[10] = -2 * p;
|
|
620
|
+
out[14] = -z;
|
|
621
|
+
|
|
622
|
+
out[3] = 0;
|
|
623
|
+
out[7] = 0;
|
|
624
|
+
out[11] = 0;
|
|
625
|
+
out[15] = 1;
|
|
626
|
+
|
|
627
|
+
return out;
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
export const {
|
|
631
|
+
add,
|
|
632
|
+
adjoint,
|
|
633
|
+
clone,
|
|
634
|
+
copy,
|
|
635
|
+
create,
|
|
636
|
+
decompose,
|
|
637
|
+
determinant,
|
|
638
|
+
equals,
|
|
639
|
+
exactEquals,
|
|
640
|
+
frob,
|
|
641
|
+
fromQuat,
|
|
642
|
+
fromQuat2,
|
|
643
|
+
fromRotation,
|
|
644
|
+
fromRotationTranslation,
|
|
645
|
+
fromRotationTranslationScale,
|
|
646
|
+
fromRotationTranslationScaleOrigin,
|
|
647
|
+
fromScaling,
|
|
648
|
+
fromTranslation,
|
|
649
|
+
fromValues,
|
|
650
|
+
fromXRotation,
|
|
651
|
+
fromYRotation,
|
|
652
|
+
fromZRotation,
|
|
653
|
+
frustum,
|
|
654
|
+
getRotation,
|
|
655
|
+
getScaling,
|
|
656
|
+
getTranslation,
|
|
657
|
+
identity,
|
|
658
|
+
invert,
|
|
659
|
+
lookAt,
|
|
660
|
+
mul,
|
|
661
|
+
multiply,
|
|
662
|
+
multiplyScalar,
|
|
663
|
+
multiplyScalarAndAdd,
|
|
664
|
+
ortho,
|
|
665
|
+
orthoNO,
|
|
666
|
+
orthoZO,
|
|
667
|
+
perspective,
|
|
668
|
+
perspectiveFromFieldOfView,
|
|
669
|
+
perspectiveNO,
|
|
670
|
+
perspectiveZO,
|
|
671
|
+
rotate,
|
|
672
|
+
rotateX,
|
|
673
|
+
rotateY,
|
|
674
|
+
rotateZ,
|
|
675
|
+
scale,
|
|
676
|
+
set,
|
|
677
|
+
str,
|
|
678
|
+
sub,
|
|
679
|
+
subtract,
|
|
680
|
+
targetTo,
|
|
681
|
+
translate,
|
|
682
|
+
transpose,
|
|
683
|
+
alloc,
|
|
684
|
+
unalloc,
|
|
685
|
+
fromJointMatIndex,
|
|
686
|
+
arcFromForward,
|
|
687
|
+
copyTranslation,
|
|
688
|
+
fromMat3,
|
|
689
|
+
orthoD3D,
|
|
690
|
+
lookAtD3D,
|
|
691
|
+
setLookRotation,
|
|
692
|
+
maxScaleOnAxis,
|
|
693
|
+
perspectiveGL,
|
|
694
|
+
projectVec3,
|
|
695
|
+
setTranslation,
|
|
696
|
+
setTranslationFromValues,
|
|
697
|
+
makePerspective,
|
|
698
|
+
makeOrthographic
|
|
699
|
+
} = mat4;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export * as num from "../num.js";
|
|
2
|
+
export * as vec2 from "../vec2.js";
|
|
3
|
+
export * as vec3 from "../vec3.js";
|
|
4
|
+
export * as vec4 from "../vec4.js";
|
|
5
|
+
export * as quat from "../quat.js";
|
|
6
|
+
export * as mat3 from "../mat3.js";
|
|
7
|
+
export * as mat4 from "../mat4.js";
|
|
8
|
+
|
|
9
|
+
export * as box3 from "../box3.js";
|
|
10
|
+
export * as tri3 from "../tri3.js";
|
|
11
|
+
export * as lne3 from "../lne3.js";
|
|
12
|
+
export * as pln from "../pln.js";
|
|
13
|
+
export * as ray3 from "../ray3.js";
|
|
14
|
+
export * as sph3 from "../sph3.js";
|
|
15
|
+
|
|
16
|
+
export * as pool from "../pool.js";
|
|
17
|
+
export * as noise from "../noise.js";
|
|
18
|
+
export * as curve from "../curve.js";
|
|
19
|
+
|
|
20
|
+
export * as geometry from "../geometry/index.js";
|
|
21
|
+
export * as vertex from "../vertex.js";
|
|
22
|
+
export * as mesh from "../mesh.js";
|
|
23
|
+
export * as tangent from "../tangent.js";
|
|
24
|
+
|
|
25
|
+
export * from "./scalar.js";
|