@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.
Files changed (101) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +23 -0
  3. package/README.md +56 -0
  4. package/THIRD-PARTY-NOTICES.md +38 -0
  5. package/docs/README.md +81 -0
  6. package/docs/architecture.md +101 -0
  7. package/docs/concepts/foundation-consolidation.md +86 -0
  8. package/docs/const-kb.md +92 -0
  9. package/docs/core-types/DECORATOR-TODOS.md +25 -0
  10. package/docs/core-types/README.md +203 -0
  11. package/docs/reference/api.md +70 -0
  12. package/docs/reference/classes/README.md +115 -0
  13. package/package.json +132 -0
  14. package/src/arrays.js +5 -0
  15. package/src/audio/audioFormats.js +34 -0
  16. package/src/audio/index.js +1 -0
  17. package/src/box3.js +1385 -0
  18. package/src/bytes.js +56 -0
  19. package/src/compression.js +56 -0
  20. package/src/constants/index.js +6 -0
  21. package/src/constants.js +15 -0
  22. package/src/curve.js +419 -0
  23. package/src/d3d/dxgiFormats.js +46 -0
  24. package/src/d3d/index.js +2 -0
  25. package/src/d3d/primitiveTopology.js +11 -0
  26. package/src/document/CjsCarbonDocument.js +212 -0
  27. package/src/document/CjsClassRegistry.js +373 -0
  28. package/src/document/CjsDocumentDehydrator.js +142 -0
  29. package/src/document/CjsDocumentHydrator.js +156 -0
  30. package/src/document/CjsStructRegistry.js +348 -0
  31. package/src/document/hydrationAdapter.js +129 -0
  32. package/src/document/index.js +6 -0
  33. package/src/geometry/box.js +137 -0
  34. package/src/geometry/cylinder.js +244 -0
  35. package/src/geometry/helpers/LICENSE +15 -0
  36. package/src/geometry/helpers/earcut.js +766 -0
  37. package/src/geometry/helpers/misc.js +103 -0
  38. package/src/geometry/index.js +8 -0
  39. package/src/geometry/json.js +165 -0
  40. package/src/geometry/lathe.js +172 -0
  41. package/src/geometry/octahedron.js +0 -0
  42. package/src/geometry/plane.js +81 -0
  43. package/src/geometry/shape.js +95 -0
  44. package/src/geometry/sphere.js +123 -0
  45. package/src/geometry/torus.js +96 -0
  46. package/src/graphics/colorSpaces.js +22 -0
  47. package/src/graphics/index.js +4 -0
  48. package/src/graphics/pixelFormats.js +158 -0
  49. package/src/graphics/textureDimensions.js +22 -0
  50. package/src/graphics/trinityEnums.js +87 -0
  51. package/src/index.js +58 -0
  52. package/src/is.js +524 -0
  53. package/src/json.js +23 -0
  54. package/src/lifecycle/CjsLifecycleState.js +77 -0
  55. package/src/lifecycle/index.js +1 -0
  56. package/src/lne3.js +497 -0
  57. package/src/lookup.js +48 -0
  58. package/src/mat3.js +131 -0
  59. package/src/mat4.js +699 -0
  60. package/src/math/index.js +25 -0
  61. package/src/math/scalar.js +63 -0
  62. package/src/media/index.js +1 -0
  63. package/src/media/mediaTypes.js +50 -0
  64. package/src/mesh.js +394 -0
  65. package/src/model/CjsEventEmitter.js +333 -0
  66. package/src/model/CjsModel.js +1544 -0
  67. package/src/model/CjsModelState.js +72 -0
  68. package/src/model/index.js +4 -0
  69. package/src/model/sourceRecordUtils.js +54 -0
  70. package/src/noise.js +310 -0
  71. package/src/num.js +827 -0
  72. package/src/path.js +21 -0
  73. package/src/pln.js +762 -0
  74. package/src/pool.js +160 -0
  75. package/src/quat.js +144 -0
  76. package/src/ray3.js +1085 -0
  77. package/src/renderContext/formats.js +145 -0
  78. package/src/renderContext/index.js +5 -0
  79. package/src/renderContext/presentation.js +125 -0
  80. package/src/renderContext/resources.js +27 -0
  81. package/src/renderContext/upscaling.js +22 -0
  82. package/src/renderContext/window.js +20 -0
  83. package/src/runtime/CjsRuntimeState.js +50 -0
  84. package/src/schema/CjsSchema.js +1009 -0
  85. package/src/schema/index.js +17 -0
  86. package/src/shader/index.js +1 -0
  87. package/src/shader/shaderStages.js +37 -0
  88. package/src/sph3.js +754 -0
  89. package/src/tangent.js +288 -0
  90. package/src/text.js +40 -0
  91. package/src/tri3.js +650 -0
  92. package/src/types/carbonTypes.js +635 -0
  93. package/src/types/index.js +2 -0
  94. package/src/utils.js +58 -0
  95. package/src/validation.js +46 -0
  96. package/src/vec2.js +229 -0
  97. package/src/vec3.js +1172 -0
  98. package/src/vec4.js +347 -0
  99. package/src/vertex.js +108 -0
  100. package/src/webgpu/index.js +1 -0
  101. package/src/webgpu/textureFormats.js +121 -0
package/src/vec3.js ADDED
@@ -0,0 +1,1172 @@
1
+ import * as glVec3 from "gl-matrix/esm/vec3.js";
2
+ import {
3
+ fromQuat as mat4FromQuat,
4
+ getRotation as getMat4Rotation
5
+ } from "gl-matrix/esm/mat4.js";
6
+ import { transformMat4 as transformVec4Mat4 } from "gl-matrix/esm/vec4.js";
7
+ import { num } from "./num.js";
8
+ import { pool } from "./pool.js";
9
+
10
+ const vec3 = { ...glVec3 };
11
+
12
+ /**
13
+ * Vector 3
14
+ * @typedef {Float32Array} vec3
15
+ */
16
+
17
+ /**
18
+ * Allocates a pooled vec3
19
+ * @returns {Float32Array|vec3}
20
+ */
21
+ vec3.alloc = function ()
22
+ {
23
+ return pool.allocF32(3);
24
+ };
25
+
26
+ /**
27
+ * Unallocates a pooled vec3
28
+ * @param {vec3|Float32Array} a
29
+ */
30
+ vec3.unalloc = function (a)
31
+ {
32
+ pool.freeType(a);
33
+ };
34
+
35
+ vec3.unallocMany = function(arr)
36
+ {
37
+ for (let i = 0; i < arr.length; i++)
38
+ {
39
+ pool.freeType(arr[i]);
40
+ }
41
+ };
42
+
43
+ /**
44
+ * X_AXIS
45
+ * @type {vec3}
46
+ */
47
+ vec3.X_AXIS = vec3.fromValues(1, 0, 0);
48
+
49
+ /**
50
+ * Y Axis
51
+ * @type {vec3}
52
+ */
53
+ vec3.Y_AXIS = vec3.fromValues(0, 1, 0);
54
+
55
+ /**
56
+ * Z Axis
57
+ * @type {vec3}
58
+ */
59
+ vec3.Z_AXIS = vec3.fromValues(0, 0, 1);
60
+
61
+
62
+ /**
63
+ * Adds a scalar to a vec3
64
+ *
65
+ * @param {vec3} out
66
+ * @param {vec3} a
67
+ * @param {Number} s
68
+ * @returns {vec3} out
69
+ */
70
+ vec3.addScalar = function (out, a, s)
71
+ {
72
+ out[0] = a[0] + s;
73
+ out[1] = a[1] + s;
74
+ out[2] = a[2] + s;
75
+ return out;
76
+ };
77
+
78
+ /**
79
+ * Converts radians to degrees
80
+ *
81
+ * @param {vec3} out
82
+ * @param {vec3} a
83
+ * @returns {vec3} out
84
+ */
85
+ vec3.degrees = function (out, a)
86
+ {
87
+ out[0] = num.degrees(a[0]);
88
+ out[1] = num.degrees(a[1]);
89
+ out[2] = num.degrees(a[2]);
90
+ return out;
91
+ };
92
+
93
+ /**
94
+ * Converts radians to unwrapped degrees
95
+ *
96
+ * @param {vec3} out
97
+ * @param {vec3} a
98
+ * @returns {vec3} out
99
+ */
100
+ vec3.degreesUnwrapped = function (out, a)
101
+ {
102
+ out[0] = num.degreesUnwrapped(a[0]);
103
+ out[1] = num.degreesUnwrapped(a[1]);
104
+ out[2] = num.degreesUnwrapped(a[2]);
105
+ return out;
106
+ };
107
+
108
+ /**
109
+ * Gets normalized direction between two points
110
+ * @param {vec3} out
111
+ * @param {vec3} a
112
+ * @param {vec3} b
113
+ * @returns {vec3}
114
+ */
115
+ vec3.direction = function (out, a, b)
116
+ {
117
+ vec3.subtract(out, a, b);
118
+ vec3.normalize(out, out);
119
+ return out;
120
+ };
121
+
122
+ /**
123
+ * Gets the direction from a quat
124
+ * @param {vec3} out
125
+ * @param {vec3} axis
126
+ * @param {quat} q
127
+ * @returns {vec3} out
128
+ */
129
+ vec3.directionFromQuat = function (out, axis, q)
130
+ {
131
+ return vec3.transformQuat(out, axis, q);
132
+ };
133
+
134
+ /**
135
+ * Gets the direction from a mat4's axis
136
+ * @param {vec3} out
137
+ * @param {vec3} axis
138
+ * @param {mat4} m
139
+ * @returns {vec3} out
140
+ */
141
+ vec3.directionFromMat4 = function (out, axis, m)
142
+ {
143
+ const quat_0 = getMat4Rotation(pool.allocF32(4), m);
144
+ vec3.transformQuat(out, axis, quat_0);
145
+ pool.freeType(quat_0);
146
+ return out;
147
+ };
148
+
149
+ /**
150
+ * Divides a vec3 by a scalar
151
+ *
152
+ * @param {vec3} out
153
+ * @param {vec3} a
154
+ * @param {Number} s
155
+ * @returns {vec3} out
156
+ */
157
+ vec3.divideScalar = function (out, a, s)
158
+ {
159
+ return vec3.multiplyScalar(out, a, 1 / s);
160
+ };
161
+
162
+ /**
163
+ * Euler functions
164
+ * @type {{*}}
165
+ */
166
+ vec3.euler = {};
167
+
168
+ /**
169
+ * Default euler order
170
+ * @type {string}
171
+ */
172
+ vec3.euler.DEFAULT_ORDER = "XYZ";
173
+
174
+
175
+ /**
176
+ * Sets a euler from a quat
177
+ *
178
+ * @param {vec3} out
179
+ * @param {quat} q
180
+ * @param {string} [order=vec3.euler.DEFAULT_ORDER]
181
+ * @returns {vec3} out
182
+ */
183
+ vec3.euler.fromQuat = function (out, q, order = vec3.euler.DEFAULT_ORDER)
184
+ {
185
+ // mat4.alloc
186
+ const mat4_0 = mat4FromQuat(pool.allocF32(16), q);
187
+ vec3.euler.fromMat4(out, mat4_0, order);
188
+ pool.unalloc(mat4_0);
189
+ return out;
190
+ };
191
+
192
+ /**
193
+ * Sets a euler from a mat4
194
+ *
195
+ * @author three.js (converted)
196
+ * @param {vec3} out
197
+ * @param {mat4} m
198
+ * @param {string} [order=vec3.euler.DEFAULT_ORDER]
199
+ * @returns {vec3} out
200
+ */
201
+ vec3.euler.fromMat4 = function (out, m, order = vec3.euler.DEFAULT_ORDER)
202
+ {
203
+ const
204
+ m11 = m[0], m12 = m[4], m13 = m[8],
205
+ m21 = m[1], m22 = m[5], m23 = m[9],
206
+ m31 = m[2], m32 = m[6], m33 = m[10];
207
+
208
+ const clamp = num.clamp;
209
+
210
+ if (order === "XYZ")
211
+ {
212
+ out[1] = Math.asin(clamp(m13, -1, 1));
213
+ if (Math.abs(m13) < 0.99999)
214
+ {
215
+ out[0] = Math.atan2(-m23, m33);
216
+ out[2] = Math.atan2(-m12, m11);
217
+ }
218
+ else
219
+ {
220
+ out[0] = Math.atan2(m32, m22);
221
+ out[2] = 0;
222
+ }
223
+ }
224
+ else if (order === "YXZ")
225
+ {
226
+ out[0] = Math.asin(-clamp(m23, -1, 1));
227
+ if (Math.abs(m23) < 0.99999)
228
+ {
229
+ out[1] = Math.atan2(m13, m33);
230
+ out[2] = Math.atan2(m21, m22);
231
+ }
232
+ else
233
+ {
234
+ out[1] = Math.atan2(-m31, m11);
235
+ out[2] = 0;
236
+ }
237
+ }
238
+ else if (order === "ZXY")
239
+ {
240
+ out[0] = Math.asin(clamp(m32, -1, 1));
241
+ if (Math.abs(m32) < 0.99999)
242
+ {
243
+ out[1] = Math.atan2(-m31, m33);
244
+ out[2] = Math.atan2(-m12, m22);
245
+ }
246
+ else
247
+ {
248
+ out[1] = 0;
249
+ out[2] = Math.atan2(m21, m11);
250
+ }
251
+ }
252
+ else if (order === "ZYX")
253
+ {
254
+ out[1] = Math.asin(-clamp(m31, -1, 1));
255
+ if (Math.abs(m31) < 0.99999)
256
+ {
257
+ out[0] = Math.atan2(m32, m33);
258
+ out[2] = Math.atan2(m21, m11);
259
+ }
260
+ else
261
+ {
262
+ out[0] = 0;
263
+ out[2] = Math.atan2(-m12, m22);
264
+ }
265
+ }
266
+ else if (order === "YZX")
267
+ {
268
+ out[2] = Math.asin(clamp(m21, -1, 1));
269
+ if (Math.abs(m21) < 0.99999)
270
+ {
271
+ out[0] = Math.atan2(-m23, m22);
272
+ out[1] = Math.atan2(-m31, m11);
273
+ }
274
+ else
275
+ {
276
+ out[0] = 0;
277
+ out[1] = Math.atan2(m13, m33);
278
+ }
279
+ }
280
+ else if (order === "XZY")
281
+ {
282
+ out[2] = Math.asin(-clamp(m12, -1, 1));
283
+ if (Math.abs(m12) < 0.99999)
284
+ {
285
+ out[0] = Math.atan2(m32, m22);
286
+ out[1] = Math.atan2(m13, m11);
287
+ }
288
+ else
289
+ {
290
+ out[0] = Math.atan2(-m23, m33);
291
+ out[1] = 0;
292
+ }
293
+ }
294
+ else
295
+ {
296
+ out[0] = 0;
297
+ out[1] = 0;
298
+ out[2] = 0;
299
+ throw new Error("Unrecognised euler order: " + order);
300
+ }
301
+
302
+ return out;
303
+ };
304
+
305
+ /**
306
+ * Gets a quat from a euler
307
+ * - Differs from quat.getEuler as it allows for different euler ordering
308
+ *
309
+ * - http://www.mathworks.com/matlabcentral/fileexchange/
310
+ * - 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
311
+ * - content/SpinCalc.m
312
+ *
313
+ * @param {quat} out
314
+ * @param {vec3} euler
315
+ * @param [order=vec3.euler.DEFAULT_ORDER]
316
+ * @returns {quat} out
317
+ */
318
+ vec3.euler.getQuat = function (out, euler, order = vec3.euler.DEFAULT_ORDER)
319
+ {
320
+ const
321
+ x = euler[0],
322
+ y = euler[1],
323
+ z = euler[2];
324
+
325
+ const
326
+ cosYaw = Math.cos(x / 2),
327
+ cosPitch = Math.cos(y / 2),
328
+ cosRoll = Math.cos(z / 2),
329
+ sinYaw = Math.sin(x / 2),
330
+ sinPitch = Math.sin(y / 2),
331
+ sinRoll = Math.sin(z / 2);
332
+
333
+ if (order === "XYZ")
334
+ {
335
+ out[0] = sinYaw * cosPitch * cosRoll + cosYaw * sinPitch * sinRoll;
336
+ out[1] = cosYaw * sinPitch * cosRoll - sinYaw * cosPitch * sinRoll;
337
+ out[2] = cosYaw * cosPitch * sinRoll + sinYaw * sinPitch * cosRoll;
338
+ out[3] = cosYaw * cosPitch * cosRoll - sinYaw * sinPitch * sinRoll;
339
+ }
340
+ else if (order === "YXZ")
341
+ {
342
+ out[0] = sinYaw * cosPitch * cosRoll + cosYaw * sinPitch * sinRoll;
343
+ out[1] = cosYaw * sinPitch * cosRoll - sinYaw * cosPitch * sinRoll;
344
+ out[2] = cosYaw * cosPitch * sinRoll - sinYaw * sinPitch * cosRoll;
345
+ out[3] = cosYaw * cosPitch * cosRoll + sinYaw * sinPitch * sinRoll;
346
+ }
347
+ else if (order === "ZXY")
348
+ {
349
+ out[0] = sinYaw * cosPitch * cosRoll - cosYaw * sinPitch * sinRoll;
350
+ out[1] = cosYaw * sinPitch * cosRoll + sinYaw * cosPitch * sinRoll;
351
+ out[2] = cosYaw * cosPitch * sinRoll + sinYaw * sinPitch * cosRoll;
352
+ out[3] = cosYaw * cosPitch * cosRoll - sinYaw * sinPitch * sinRoll;
353
+ }
354
+ else if (order === "ZYX")
355
+ {
356
+ out[0] = sinYaw * cosPitch * cosRoll - cosYaw * sinPitch * sinRoll;
357
+ out[1] = cosYaw * sinPitch * cosRoll + sinYaw * cosPitch * sinRoll;
358
+ out[2] = cosYaw * cosPitch * sinRoll - sinYaw * sinPitch * cosRoll;
359
+ out[3] = cosYaw * cosPitch * cosRoll + sinYaw * sinPitch * sinRoll;
360
+ }
361
+ else if (order === "YZX")
362
+ {
363
+ out[0] = sinYaw * cosPitch * cosRoll + cosYaw * sinPitch * sinRoll;
364
+ out[1] = cosYaw * sinPitch * cosRoll + sinYaw * cosPitch * sinRoll;
365
+ out[2] = cosYaw * cosPitch * sinRoll - sinYaw * sinPitch * cosRoll;
366
+ out[3] = cosYaw * cosPitch * cosRoll - sinYaw * sinPitch * sinRoll;
367
+ }
368
+ else if (order === "XZY")
369
+ {
370
+ out[0] = sinYaw * cosPitch * cosRoll - cosYaw * sinPitch * sinRoll;
371
+ out[1] = cosYaw * sinPitch * cosRoll - sinYaw * cosPitch * sinRoll;
372
+ out[2] = cosYaw * cosPitch * sinRoll + sinYaw * sinPitch * cosRoll;
373
+ out[3] = cosYaw * cosPitch * cosRoll + sinYaw * sinPitch * sinRoll;
374
+ }
375
+ else
376
+ {
377
+ out[0] = 0;
378
+ out[1] = 0;
379
+ out[2] = 0;
380
+ out[3] = 1;
381
+ throw new Error("Unrecognised euler order: " + order);
382
+ }
383
+ return out;
384
+ };
385
+
386
+
387
+ /**
388
+ * Gets a quat from a euler values
389
+ *
390
+ * @param {quat} out
391
+ * @param {Number} x
392
+ * @param {Number} y
393
+ * @param {Number} z
394
+ * @param {String} [order]
395
+ * @return {quat} out
396
+ */
397
+ vec3.euler.getQuatFromValues = function (out, x, y, z, order)
398
+ {
399
+ const vec3_0 = vec3.set(vec3.alloc(), x, y, z);
400
+ vec3.euler.getQuat(out, vec3_0, order);
401
+ vec3.unalloc(vec3_0);
402
+ return out;
403
+ };
404
+
405
+ /**
406
+ * Gets a quat from a euler that uses degrees
407
+ *
408
+ * @param {quat} out
409
+ * @param {vec3} v
410
+ * @param {String} [order]
411
+ * @return {quat} out
412
+ */
413
+ vec3.euler.getQuatFromDegrees = function (out, v, order)
414
+ {
415
+ const vec3_0 = vec3.radians(vec3.alloc(), v);
416
+ vec3.euler.getQuat(out, vec3_0, order);
417
+ vec3.unalloc(vec3_0);
418
+ return out;
419
+ };
420
+
421
+ /**
422
+ * Gets a quat from euler degree values
423
+ *
424
+ * @param {quat} out
425
+ * @param {Number} x
426
+ * @param {Number} y
427
+ * @param {Number} z
428
+ * @param {String} [order]
429
+ * @return {quat} out
430
+ */
431
+ vec3.euler.getQuatFromDegreeValues = function (out, x, y, z, order)
432
+ {
433
+ const vec3_0 = vec3.set(vec3.alloc(), x, y, z);
434
+ vec3.radians(vec3_0, vec3_0);
435
+ vec3.euler.getQuat(out, vec3_0, order);
436
+ vec3.unalloc(vec3_0);
437
+ return out;
438
+ };
439
+
440
+ /**
441
+ * Sets a euler from quat, and stores in degrees
442
+ *
443
+ * @param {vec3} out
444
+ * @param {quat} q
445
+ * @param {String} [order]
446
+ * @return {vec3} out
447
+ */
448
+ vec3.euler.fromQuatInDegrees = function (out, q, order)
449
+ {
450
+ vec3.euler.fromQuat(out, q, order);
451
+ return vec3.degrees(out, out);
452
+ };
453
+
454
+ /**
455
+ * Exponential decay
456
+ *
457
+ * @param {vec3} out
458
+ * @param {vec3} omega0
459
+ * @param {vec3} torque
460
+ * @param {number} I
461
+ * @param {number} drag
462
+ * @param {number} time
463
+ * @returns {vec3} out
464
+ */
465
+ vec3.exponentialDecay = function (out, omega0, torque, I, drag, time)
466
+ {
467
+ out[0] = num.exponentialDecay(omega0[0], torque[0], I, drag, time);
468
+ out[1] = num.exponentialDecay(omega0[1], torque[1], I, drag, time);
469
+ out[2] = num.exponentialDecay(omega0[2], torque[2], I, drag, time);
470
+ return out;
471
+ };
472
+
473
+ /**
474
+ * Creates a spherical
475
+ * @returns {vec3}
476
+ */
477
+ vec3.createSpherical = function ()
478
+ {
479
+ return vec3.fromValues(0, 0, 1);
480
+ };
481
+
482
+ /**
483
+ * Sets a vec3 with cartesian coordinates from spherical coordinates and an optional center point
484
+ * @param {vec3} out - receiving vec3
485
+ * @param {vec3} spherical - source vec3 with spherical coordinates (phi, theta, radius)
486
+ * @param {vec3} [center] - Optional center
487
+ * @returns {vec3} out - receiving vec3
488
+ */
489
+ vec3.fromSpherical = function (out, spherical, center)
490
+ {
491
+ const
492
+ phi = spherical[0],
493
+ theta = spherical[1],
494
+ radius = spherical[2];
495
+
496
+ const sinPhi = Math.sin(phi);
497
+
498
+ out[0] = radius * sinPhi * Math.sin(theta); // x
499
+ out[1] = radius * Math.cos(phi); // y
500
+ out[2] = radius * sinPhi * Math.cos(theta); // z
501
+
502
+ if (center)
503
+ {
504
+ out[0] += center[0];
505
+ out[1] += center[1];
506
+ out[2] += center[2];
507
+ }
508
+
509
+ return out;
510
+ };
511
+
512
+
513
+
514
+ /**
515
+ * Gets spherical coordinates from a vector
516
+ * @param {vec3} out
517
+ * @param {vec3} a
518
+ * @returns {vec3} out
519
+ */
520
+ vec3.getSpherical = function (out, a)
521
+ {
522
+ let phi = 0,
523
+ theta = 0,
524
+ radius = vec3.length(a);
525
+
526
+ if (radius !== 0)
527
+ {
528
+ phi = Math.acos(num.clamp(a[1] / radius, -1, 1));
529
+ theta = Math.atan2(a[0], a[2]);
530
+ }
531
+
532
+ out[0] = phi;
533
+ out[1] = theta;
534
+ out[2] = radius;
535
+ return out;
536
+ };
537
+
538
+ /**
539
+ * Makes a spherical value "safe"
540
+ * @param {vec3} out
541
+ * @param {vec3} a
542
+ * @returns {vec3} out
543
+ */
544
+ vec3.makeSphericalSafe = function (out, a)
545
+ {
546
+ out[0] = Math.max(num.EPSILON, Math.min(Math.PI - num.EPSILON, a[0]));
547
+ out[1] = a[1];
548
+ out[2] = a[2];
549
+ return out;
550
+ };
551
+
552
+ /**
553
+ * Checks if all elements are 0
554
+ * @param {vec3} a
555
+ * @returns {boolean}
556
+ */
557
+ vec3.isEmpty = function (a)
558
+ {
559
+ return a[0] === 0 && a[1] === 0 && a[2] === 0;
560
+ };
561
+
562
+ /**
563
+ * Multiplies a vec3 by a scalar
564
+ *
565
+ * @param {vec3} out
566
+ * @param {vec3} a
567
+ * @param {Number} s
568
+ * @returns {vec3} out
569
+ */
570
+ vec3.multiplyScalar = function (out, a, s)
571
+ {
572
+ out[0] = a[0] * s;
573
+ out[1] = a[1] * s;
574
+ out[2] = a[2] * s;
575
+ return out;
576
+ };
577
+
578
+ /**
579
+ * Converts from long, lat and radius to a vector
580
+ * @param {vec3} out
581
+ * @param {Number} radius
582
+ * @param {Number} latitude
583
+ * @param {Number} longitude
584
+ * @returns {vec3} out
585
+ */
586
+ vec3.polarToCartesian = function (out, radius, latitude, longitude)
587
+ {
588
+ out[0] = radius * Math.cos(latitude) * Math.sin(longitude);
589
+ out[1] = radius * Math.sin(latitude);
590
+ out[2] = radius * Math.cos(latitude) * Math.cos(longitude);
591
+ return out;
592
+ };
593
+
594
+ /**
595
+ * Projects a world vec3 to screen space with viewport settings
596
+ * @param {vec3} out - receiving vec3
597
+ * @param {vec3} a - local vec3
598
+ * @param {mat4} m - model view projection matrix
599
+ * @param {vec4} viewport - view port settings (x, y, width, height)
600
+ * @returns {vec3} out - receiving vec3 (x, y, perspectiveDivide)
601
+ */
602
+ vec3.project = function (out, a, m, viewport)
603
+ {
604
+ let x = a[0],
605
+ y = a[1],
606
+ z = a[2];
607
+
608
+ let outX = m[0] * x + m[4] * y + m[8] * z + m[12],
609
+ outY = m[1] * x + m[5] * y + m[9] * z + m[13],
610
+ perD = m[3] * x + m[7] * y + m[11] * z + m[15];
611
+
612
+ let projectionX = (outX / perD + 1) / 2;
613
+ let projectionY = 1 - (outY / perD + 1) / 2;
614
+
615
+ out[0] = projectionX * viewport[2] + viewport[0];
616
+ out[1] = projectionY * viewport[3] + viewport[1];
617
+ out[2] = perD;
618
+ return out;
619
+ };
620
+
621
+ /**
622
+ * Converts degrees to radians
623
+ *
624
+ * @param {vec3} out
625
+ * @param {vec3} a
626
+ * @returns {vec3} out
627
+ */
628
+ vec3.radians = function (out, a)
629
+ {
630
+ out[0] = num.radians(a[0]);
631
+ out[1] = num.radians(a[1]);
632
+ out[2] = num.radians(a[2]);
633
+ return out;
634
+ };
635
+
636
+ /**
637
+ * Converts degrees to unwrapped radians
638
+ *
639
+ * @param {vec3} out
640
+ * @param {vec3} a
641
+ * @returns {vec3} out
642
+ */
643
+ vec3.radiansUnwrapped = function (out, a)
644
+ {
645
+ out[0] = num.radiansUnwrapped(a[0]);
646
+ out[1] = num.radiansUnwrapped(a[1]);
647
+ out[2] = num.radiansUnwrapped(a[2]);
648
+ return out;
649
+ };
650
+
651
+ /**
652
+ * Sets a vec3 from a scalar
653
+ *
654
+ * @param {vec3} out
655
+ * @param {Number} s
656
+ * @returns {vec3} out
657
+ */
658
+ vec3.setScalar = function (out, s)
659
+ {
660
+ out[0] = s;
661
+ out[1] = s;
662
+ out[2] = s;
663
+ return out;
664
+ };
665
+
666
+ /**
667
+ * Subtracts a scalar from a vec3
668
+ *
669
+ * @param {vec3} out
670
+ * @param {vec3} a
671
+ * @param {Number} s
672
+ * @returns {vec3} out
673
+ */
674
+ vec3.subtractScalar = function (out, a, s)
675
+ {
676
+ out[0] = a[0] - s;
677
+ out[1] = a[1] - s;
678
+ out[2] = a[2] - s;
679
+ return out;
680
+ };
681
+
682
+ /**
683
+ * Unprojects a vec3 with canvas coordinates to world space
684
+ *
685
+ * @param {vec3} out - receiving vec3
686
+ * @param {vec3} a - vec3 to unproject
687
+ * @param {mat4} invViewProj - inverse view projection matrix
688
+ * @param {vec4|Array} viewport - [ x, y, width, height ]
689
+ * @returns {vec3} out
690
+ * @throw On perspective divide error
691
+ */
692
+ vec3.unproject = function (out, a, invViewProj, viewport)
693
+ {
694
+ const vec4_0 = pool.allocF32(4);
695
+
696
+ let x = a[0],
697
+ y = a[1],
698
+ z = a[2];
699
+
700
+ vec4_0[0] = (x - viewport[0]) * 2.0 / viewport[2] - 1.0;
701
+ vec4_0[1] = (y - viewport[1]) * 2.0 / viewport[3] - 1.0;
702
+ vec4_0[2] = 2.0 * z - 1.0;
703
+ vec4_0[3] = 1.0;
704
+
705
+ transformVec4Mat4(vec4_0, vec4_0, invViewProj);
706
+
707
+ if (vec4_0[3] === 0.0)
708
+ {
709
+ pool.freeType(vec4_0);
710
+ out[0] = 0;
711
+ out[1] = 0;
712
+ out[2] = 0;
713
+ throw new Error("Perspective divide error");
714
+ }
715
+
716
+ out[0] = vec4_0[0] / vec4_0[3];
717
+ out[1] = vec4_0[1] / vec4_0[3];
718
+ out[2] = vec4_0[2] / vec4_0[3];
719
+
720
+ pool.freeType(vec4_0);
721
+
722
+ return out;
723
+ };
724
+
725
+ /**
726
+ * Unwraps degrees
727
+ *
728
+ * @param {vec3} out
729
+ * @param {vec3} a
730
+ * @returns {vec3}
731
+ */
732
+ vec3.unwrapDegrees = function (out, a)
733
+ {
734
+ out[0] = num.unwrapDegrees(a[0]);
735
+ out[1] = num.unwrapDegrees(a[1]);
736
+ out[2] = num.unwrapDegrees(a[2]);
737
+ return out;
738
+ };
739
+
740
+ /**
741
+ * Unwraps radians
742
+ *
743
+ * @param {vec3} out
744
+ * @param {vec3} a
745
+ * @returns {vec3}
746
+ */
747
+ vec3.unwrapRadians = function (out, a)
748
+ {
749
+ out[0] = num.unwrapRadians(a[0]);
750
+ out[1] = num.unwrapRadians(a[1]);
751
+ out[2] = num.unwrapRadians(a[2]);
752
+ return out;
753
+ };
754
+
755
+ /**
756
+ * Converts from linear color to rgb
757
+ * @param {vec3} out
758
+ * @param {vec3} linear
759
+ * @returns {vec3}
760
+ */
761
+ vec3.toRGB = function (out, linear)
762
+ {
763
+ out[0] = num.colorFromLinear(linear[0]);
764
+ out[1] = num.colorFromLinear(linear[1]);
765
+ out[2] = num.colorFromLinear(linear[2]);
766
+ return out;
767
+ };
768
+
769
+ /**
770
+ * Converts from linear color to rgba
771
+ * @param {vec4} out
772
+ * @param {vec3} linear
773
+ * @param {Number} [linearAlpha=1]
774
+ * @return {vec4} out
775
+ */
776
+ vec3.toRGBA = function (out, linear, linearAlpha = 1)
777
+ {
778
+ out[0] = num.colorFromLinear(linear[0]);
779
+ out[1] = num.colorFromLinear(linear[1]);
780
+ out[2] = num.colorFromLinear(linear[2]);
781
+ out[3] = num.colorFromLinear(linearAlpha);
782
+ return out;
783
+ };
784
+
785
+ /**
786
+ * Converts to linear color from rgba
787
+ * @param {vec3} out
788
+ * @param {vec3} rgb
789
+ * @returns {vec3} out
790
+ */
791
+ vec3.fromRGB = function (out, rgb)
792
+ {
793
+ out[0] = num.linearFromColor(rgb[0]);
794
+ out[1] = num.linearFromColor(rgb[1]);
795
+ out[2] = num.linearFromColor(rgb[2]);
796
+ return out;
797
+ };
798
+
799
+ /**
800
+ * Gets hex value from linear color
801
+ * @param {vec4} linear
802
+ * @returns {string} hex value
803
+ */
804
+ vec3.toHex = function (linear)
805
+ {
806
+ return "#" +
807
+ num.hexFromLinear(linear[0]) +
808
+ num.hexFromLinear(linear[1]) +
809
+ num.hexFromLinear(linear[2]);
810
+ };
811
+
812
+ /**
813
+ * Gets hex rgba from linear colour
814
+ * @param {vec3} linear
815
+ * @param {Number} [linearAlpha=1]
816
+ * @return {string}
817
+ */
818
+ vec3.toHexA = function (linear, linearAlpha = 1)
819
+ {
820
+ return "#" +
821
+ num.hexFromLinear(linear[0]) +
822
+ num.hexFromLinear(linear[1]) +
823
+ num.hexFromLinear(linear[2]) +
824
+ num.hexFromLinear(linearAlpha);
825
+ };
826
+
827
+ /**
828
+ * Gets RGB from hex
829
+ * @param {vec3} out
830
+ * @param {String} hex
831
+ * @return {vec3} out
832
+ */
833
+ vec3.fromHex = function (out, hex)
834
+ {
835
+ // Set empty color in case of error
836
+ out[0] = 0;
837
+ out[1] = 0;
838
+ out[2] = 0;
839
+
840
+ if (typeof hex !== "string" || !/^#(?:[\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i.test(hex))
841
+ {
842
+ throw new TypeError("Invalid hex");
843
+ }
844
+
845
+ if (hex.length === 4 || hex.length === 5)
846
+ {
847
+ out[0] = ("0x" + hex[1] + hex[1]) / 255;
848
+ out[1] = ("0x" + hex[2] + hex[2]) / 255;
849
+ out[2] = ("0x" + hex[3] + hex[3]) / 255;
850
+ }
851
+ // RGB hex
852
+ else if (hex.length === 7 || hex.length === 9)
853
+ {
854
+ out[0] = ("0x" + hex[1] + hex[2]) / 255;
855
+ out[1] = ("0x" + hex[3] + hex[4]) / 255;
856
+ out[2] = ("0x" + hex[5] + hex[6]) / 255;
857
+ }
858
+ return out;
859
+ };
860
+
861
+
862
+ vec3.linearToHSV = function (out, linear)
863
+ {
864
+ let rabs,
865
+ gabs,
866
+ babs, rr,
867
+ gg, bb,
868
+ h,
869
+ s,
870
+ v,
871
+ diff,
872
+ diffc,
873
+ percentRoundFn;
874
+
875
+ rabs = linear[0];
876
+ gabs = linear[1];
877
+ babs = linear[2];
878
+
879
+ v = Math.max(rabs, gabs, babs);
880
+ diff = v - Math.min(rabs, gabs, babs);
881
+ diffc = c => (v - c) / 6 / diff + 1 / 2;
882
+ percentRoundFn = num => Math.round(num * 100) / 100;
883
+
884
+ if (diff == 0)
885
+ {
886
+ h = s = 0;
887
+ }
888
+ else
889
+ {
890
+ s = diff / v;
891
+ rr = diffc(rabs);
892
+ gg = diffc(gabs);
893
+ bb = diffc(babs);
894
+
895
+ if (rabs === v)
896
+ {
897
+ h = bb - gg;
898
+ }
899
+ else if (gabs === v)
900
+ {
901
+ h = (1 / 3) + rr - bb;
902
+ }
903
+ else if (babs === v)
904
+ {
905
+ h = (2 / 3) + gg - rr;
906
+ }
907
+
908
+ if (h < 0)
909
+ {
910
+ h += 1;
911
+ }
912
+ else if (h > 1)
913
+ {
914
+ h -= 1;
915
+ }
916
+ }
917
+
918
+ out[0] = Math.round(h * 360);
919
+ out[1] = percentRoundFn(s * 100);
920
+ out[2] = percentRoundFn(v * 100);
921
+ return out;
922
+ };
923
+
924
+ /**
925
+ * Sets a vec3 from an array with an optional offset
926
+ * @param {vec3} out
927
+ * @param {TypedArray|Array} array
928
+ * @param {Number} [offset=0]
929
+ * @returns {vec3} out
930
+ */
931
+ vec3.fromArray = function (out, array, offset = 0)
932
+ {
933
+ out[0] = array[offset];
934
+ out[1] = array[offset + 1];
935
+ out[2] = array[offset + 2];
936
+ return out;
937
+ };
938
+
939
+ /**
940
+ * Sets a vec3 from a mat4 column
941
+ * @param {vec3} out
942
+ * @param {mat4} m
943
+ * @param {Number} index
944
+ * @returns {vec3} out
945
+ */
946
+ vec3.fromMat4Column = function (out, m, index)
947
+ {
948
+ return vec3.fromArray(out, m, index * 4);
949
+ };
950
+
951
+ /**
952
+ * Sets a vec3 from a mat3 column
953
+ * @param {vec3} out
954
+ * @param {mat4} m
955
+ * @param {Number} index
956
+ * @returns {vec3} out
957
+ */
958
+ vec3.fromMat3Column = function (out, m, index)
959
+ {
960
+ return vec3.fromArray(out, m, index * 3);
961
+ };
962
+
963
+ /**
964
+ * Converts srgb to linear colour
965
+ * @param {vec3} out
966
+ * @param {vec3} srgb
967
+ * @returns {vec3} out
968
+ */
969
+ vec3.fromSRGB = function (out, srgb)
970
+ {
971
+ out[0] = num.linearFromSRGB(srgb[0]);
972
+ out[1] = num.linearFromSRGB(srgb[1]);
973
+ out[2] = num.linearFromSRGB(srgb[2]);
974
+ return out;
975
+ };
976
+
977
+ /**
978
+ * Converts srgb to linear colour
979
+ * @param {vec3} out
980
+ * @param {vec3} srgb
981
+ * @returns {vec3} out
982
+ */
983
+ vec3.linearFromSRGB = function (out, srgb)
984
+ {
985
+ return vec3.fromSRGB(out, srgb);
986
+ };
987
+
988
+ /**
989
+ * Converts linear colour to srgb
990
+ * @param {vec3} out
991
+ * @param {vec3} srgb
992
+ * @returns {vec3} out
993
+ */
994
+ vec3.toSRGB = function (out, linear)
995
+ {
996
+ out[0] = num.srgbFromLinear(linear[0]);
997
+ out[1] = num.srgbFromLinear(linear[1]);
998
+ out[2] = num.srgbFromLinear(linear[2]);
999
+ return out;
1000
+ };
1001
+
1002
+ /**
1003
+ * Converts linear colour to srgb
1004
+ * @param {vec3} out
1005
+ * @param {vec3} linear
1006
+ * @returns {vec3} out
1007
+ */
1008
+ vec3.srgbFromLinear = function (out, linear)
1009
+ {
1010
+ return vec3.toSRGB(out, linear);
1011
+ };
1012
+
1013
+ /**
1014
+ * Converts from linear color space to Carbon gamma 2.2 color space
1015
+ * @param {vec3} out
1016
+ * @param {vec3} linear
1017
+ * @returns {vec3} out
1018
+ */
1019
+ vec3.linearToGamma = function (out, linear)
1020
+ {
1021
+ out[0] = num.linearToGamma(linear[0]);
1022
+ out[1] = num.linearToGamma(linear[1]);
1023
+ out[2] = num.linearToGamma(linear[2]);
1024
+ return out;
1025
+ };
1026
+
1027
+ /**
1028
+ * Converts from Carbon gamma 2.2 color space to linear color space
1029
+ * @param {vec3} out
1030
+ * @param {vec3} gamma
1031
+ * @returns {vec3} out
1032
+ */
1033
+ vec3.gammaToLinear = function (out, gamma)
1034
+ {
1035
+ out[0] = num.gammaToLinear(gamma[0]);
1036
+ out[1] = num.gammaToLinear(gamma[1]);
1037
+ out[2] = num.gammaToLinear(gamma[2]);
1038
+ return out;
1039
+ };
1040
+
1041
+ /**
1042
+ * Three js
1043
+ * Todo: replace with glMatrix
1044
+ */
1045
+ vec3.applyQuaternion = function (out, a, q)
1046
+ {
1047
+
1048
+ const
1049
+ x = a[0],
1050
+ y = a[1],
1051
+ z = a[2],
1052
+ qx = q[0],
1053
+ qy = q[1],
1054
+ qz = q[2],
1055
+ qw = q[3];
1056
+
1057
+ // calculate quat * vector
1058
+ const
1059
+ ix = qw * x + qy * z - qz * y,
1060
+ iy = qw * y + qz * x - qx * z,
1061
+ iz = qw * z + qx * y - qy * x,
1062
+ iw = -qx * x - qy * y - qz * z;
1063
+
1064
+ // calculate result * inverse quat
1065
+ out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
1066
+ out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
1067
+ out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
1068
+
1069
+ return out;
1070
+ };
1071
+
1072
+
1073
+ export { vec3 };
1074
+
1075
+ export const {
1076
+ add,
1077
+ angle,
1078
+ bezier,
1079
+ ceil,
1080
+ clone,
1081
+ copy,
1082
+ create,
1083
+ cross,
1084
+ dist,
1085
+ distance,
1086
+ div,
1087
+ divide,
1088
+ dot,
1089
+ equals,
1090
+ exactEquals,
1091
+ floor,
1092
+ forEach,
1093
+ fromValues,
1094
+ hermite,
1095
+ inverse,
1096
+ len,
1097
+ length,
1098
+ lerp,
1099
+ max,
1100
+ min,
1101
+ mul,
1102
+ multiply,
1103
+ negate,
1104
+ normalize,
1105
+ random,
1106
+ rotateX,
1107
+ rotateY,
1108
+ rotateZ,
1109
+ round,
1110
+ scale,
1111
+ scaleAndAdd,
1112
+ set,
1113
+ slerp,
1114
+ sqrDist,
1115
+ sqrLen,
1116
+ squaredDistance,
1117
+ squaredLength,
1118
+ str,
1119
+ sub,
1120
+ subtract,
1121
+ transformMat3,
1122
+ transformMat4,
1123
+ transformQuat,
1124
+ zero,
1125
+ alloc,
1126
+ unalloc,
1127
+ unallocMany,
1128
+ X_AXIS,
1129
+ Y_AXIS,
1130
+ Z_AXIS,
1131
+ addScalar,
1132
+ degrees,
1133
+ degreesUnwrapped,
1134
+ direction,
1135
+ directionFromQuat,
1136
+ directionFromMat4,
1137
+ divideScalar,
1138
+ euler,
1139
+ exponentialDecay,
1140
+ createSpherical,
1141
+ fromSpherical,
1142
+ getSpherical,
1143
+ makeSphericalSafe,
1144
+ isEmpty,
1145
+ multiplyScalar,
1146
+ polarToCartesian,
1147
+ project,
1148
+ radians,
1149
+ radiansUnwrapped,
1150
+ setScalar,
1151
+ subtractScalar,
1152
+ unproject,
1153
+ unwrapDegrees,
1154
+ unwrapRadians,
1155
+ toRGB,
1156
+ toRGBA,
1157
+ fromRGB,
1158
+ toHex,
1159
+ toHexA,
1160
+ fromHex,
1161
+ linearToHSV,
1162
+ fromArray,
1163
+ fromMat4Column,
1164
+ fromMat3Column,
1165
+ fromSRGB,
1166
+ linearFromSRGB,
1167
+ toSRGB,
1168
+ srgbFromLinear,
1169
+ linearToGamma,
1170
+ gammaToLinear,
1171
+ applyQuaternion
1172
+ } = vec3;