@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/box3.js ADDED
@@ -0,0 +1,1385 @@
1
+ import { num } from "./num.js";
2
+ import { vec3 } from "./vec3.js";
3
+ import { pool } from "./pool.js";
4
+
5
+ const FLOAT32_MAX = 3.4028234663852886e38;
6
+
7
+
8
+ /**
9
+ * 3D Box
10
+ * @typedef {Float32Array} box3
11
+ */
12
+
13
+ export const box3 = { bounds: {} };
14
+
15
+ /**
16
+ * Allocates a pooled box3
17
+ * @returns {box3|Float32Array}
18
+ */
19
+ box3.alloc = function()
20
+ {
21
+ return pool.allocF32(6);
22
+ };
23
+
24
+ /**
25
+ * Frees a pooled box3
26
+ * @param {box3|Float32Array} b
27
+ */
28
+ box3.unalloc = function(b)
29
+ {
30
+ pool.freeType(b);
31
+ };
32
+
33
+ /**
34
+ * Gets a subarray of a box3's min vector
35
+ * @property {box3} a
36
+ * @returns {TypedArray}
37
+ */
38
+ box3.$min = function (a)
39
+ {
40
+ return a.subarray(0, 3);
41
+ };
42
+
43
+ /**
44
+ * Gets a subarray of a box3's max vector
45
+ * @property {box3} a
46
+ * @returns {TypedArray}
47
+ */
48
+ box3.$max = function (a)
49
+ {
50
+ return a.subarray(3, 6);
51
+ };
52
+
53
+ /**
54
+ * Adds a point to a box3
55
+ *
56
+ * @param {box3} out - receiving box3
57
+ * @param {box3} a - source box3
58
+ * @param {vec3} p - point to add
59
+ * @returns {box3} out - receiving box3
60
+ */
61
+ box3.addPoint = function (out, a, p)
62
+ {
63
+ if (box3.isEmpty(a))
64
+ {
65
+ out[0] = out[3] = p[0];
66
+ out[1] = out[4] = p[1];
67
+ out[2] = out[5] = p[2];
68
+ return out;
69
+ }
70
+
71
+ out[0] = Math.min(a[0], p[0]);
72
+ out[1] = Math.min(a[1], p[1]);
73
+ out[2] = Math.min(a[2], p[2]);
74
+ out[3] = Math.max(a[3], p[0]);
75
+ out[4] = Math.max(a[4], p[1]);
76
+ out[5] = Math.max(a[5], p[2]);
77
+ return out;
78
+ };
79
+
80
+ /**
81
+ * Sets a box3 from a box3 with added points
82
+ *
83
+ * @param {box3} out - receiving box3
84
+ * @param {box3} a - source box3
85
+ * @param {Array.<number>} points - points to add
86
+ * @returns {box3} out - receiving box3
87
+ */
88
+ box3.addPoints = function (out, a, points)
89
+ {
90
+ if (box3.isEmpty(a))
91
+ {
92
+ return points.length ? box3.fromPoints(out, points) : box3.empty(out);
93
+ }
94
+
95
+ let minX = a[0],
96
+ minY = a[1],
97
+ minZ = a[2],
98
+ maxX = a[3],
99
+ maxY = a[4],
100
+ maxZ = a[5];
101
+
102
+ for (let i = 0; i < points.length; i++)
103
+ {
104
+ minX = Math.min(minX, points[i][0]);
105
+ minY = Math.min(minY, points[i][1]);
106
+ minZ = Math.min(minZ, points[i][2]);
107
+ maxX = Math.max(maxX, points[i][0]);
108
+ maxY = Math.max(maxY, points[i][1]);
109
+ maxZ = Math.max(maxZ, points[i][2]);
110
+ }
111
+
112
+ out[0] = minX;
113
+ out[1] = minY;
114
+ out[2] = minZ;
115
+ out[3] = maxX;
116
+ out[4] = maxY;
117
+ out[5] = maxZ;
118
+ return out;
119
+ };
120
+
121
+ /**
122
+ * Clones a box3
123
+ *
124
+ * @param {box3} a - source box3
125
+ * @returns {box3} - a new box3
126
+ */
127
+ box3.clone = function (a)
128
+ {
129
+ let out = new Float32Array(6);
130
+ out[0] = a[0];
131
+ out[1] = a[1];
132
+ out[2] = a[2];
133
+ out[3] = a[3];
134
+ out[4] = a[4];
135
+ out[5] = a[5];
136
+ return out;
137
+ };
138
+
139
+ /**
140
+ * Checks if a box3 contains another box3
141
+ *
142
+ * @param {box3} a - source box3
143
+ * @param {box3} b - box3 to compare
144
+ * @returns {boolean} - true if the source box3 contains the second
145
+ */
146
+ box3.contains = function (a, b)
147
+ {
148
+ return (
149
+ (a[0] <= b[0]) && (b[3] <= a[3]) &&
150
+ (a[1] <= b[1]) && (b[4] <= a[4]) &&
151
+ (a[2] <= b[2]) && (b[5] <= a[5])
152
+ );
153
+ };
154
+
155
+ /**
156
+ * Checks if the box3 contains min and max bounds
157
+ *
158
+ * @param {box3} a - source box3
159
+ * @param {vec3} min - min bounds
160
+ * @param {vec3} max - max bounds
161
+ * @returns {boolean} - true if the source box3 contains the bounds
162
+ */
163
+ box3.containsBounds = function (a, min, max)
164
+ {
165
+ return (
166
+ (a[0] <= min[0]) && (max[0] <= a[3]) &&
167
+ (a[1] <= min[1]) && (max[1] <= a[4]) &&
168
+ (a[2] <= min[2]) && (max[2] <= a[5])
169
+ );
170
+ };
171
+
172
+ /**
173
+ * Checks if a box3 contains a point
174
+ *
175
+ * @param {box3} a - source box3
176
+ * @param {vec3} p - point to compare
177
+ * @returns {boolean} - true if the source box contains the point
178
+ */
179
+ box3.containsPoint = function (a, p)
180
+ {
181
+ return !(
182
+ p[0] < a[0] || p[0] > a[3] ||
183
+ p[1] < a[1] || p[1] > a[4] ||
184
+ p[2] < a[2] || p[2] > a[5]
185
+ );
186
+ };
187
+
188
+ /**
189
+ * Checks if a box3 contains a point's values
190
+ *
191
+ * @param {box3} a - source box3
192
+ * @param {Number} px - point X
193
+ * @param {Number} py - point Y
194
+ * @param {Number} pz - point Z
195
+ * @returns {boolean} - true if the source box3 contains the point
196
+ */
197
+ box3.containsValue = function (a, px, py, pz)
198
+ {
199
+ return !(
200
+ px < a[0] || px > a[3] ||
201
+ py < a[1] || py > a[4] ||
202
+ pz < a[2] || pz > a[5]
203
+ );
204
+ };
205
+
206
+ /**
207
+ * Copies the values from one box3 into another
208
+ *
209
+ * @param {box3} out - receiving box3
210
+ * @param {box3} a - source box3
211
+ * @returns {box3} out - receiving box3
212
+ */
213
+ box3.copy = function (out, a)
214
+ {
215
+ out[0] = a[0];
216
+ out[1] = a[1];
217
+ out[2] = a[2];
218
+ out[3] = a[3];
219
+ out[4] = a[4];
220
+ out[5] = a[5];
221
+ return out;
222
+ };
223
+
224
+ /**
225
+ * Copies the min vector of one box3 to another
226
+ *
227
+ * @param {box3} out
228
+ * @param {box3} a
229
+ * @returns {box3}
230
+ */
231
+ box3.copyMin = vec3.copy;
232
+
233
+ /**
234
+ * Copies the max vector of one box3 to another
235
+ *
236
+ * @param {box3} out
237
+ * @param {box3} a
238
+ * @returns {box3}
239
+ */
240
+ box3.copyMax = function (out, a)
241
+ {
242
+ out[3] = a[3];
243
+ out[4] = a[4];
244
+ out[5] = a[5];
245
+ return out;
246
+ };
247
+
248
+ /**
249
+ * Creates a box3
250
+ *
251
+ * @returns {box3} - The new box3
252
+ */
253
+ box3.create = function ()
254
+ {
255
+ return box3.empty(new Float32Array(6));
256
+ };
257
+
258
+ /**
259
+ * Gets the distance from a box3 to a point
260
+ *
261
+ * @param {box3} a - source box3
262
+ * @param {vec3} p - point
263
+ * @returns {number} - distance between the source box3 and point
264
+ */
265
+ box3.distanceToPoint = function (a, p)
266
+ {
267
+ let x = Math.max(a[0], Math.min(a[3], p[0])) - p[0],
268
+ y = Math.max(a[1], Math.min(a[4], p[1])) - p[1],
269
+ z = Math.max(a[2], Math.min(a[5], p[2])) - p[2];
270
+
271
+ return Math.sqrt(x * x + y * y + z * z);
272
+ };
273
+
274
+ /**
275
+ * Gets the distance from a box3 to a point's values
276
+ *
277
+ * @param {box3} a - source box3
278
+ * @param {Number} px - point x
279
+ * @param {Number} py - point y
280
+ * @param {Number} pz - point z
281
+ * @returns {number} - distance between the source box3 and point
282
+ */
283
+ box3.distanceToValues = function (a, px, py, pz)
284
+ {
285
+ let x = Math.max(a[0], Math.min(a[3], px)) - px,
286
+ y = Math.max(a[1], Math.min(a[4], py)) - py,
287
+ z = Math.max(a[2], Math.min(a[5], pz)) - pz;
288
+
289
+ return Math.sqrt(x * x + y * y + z * z);
290
+ };
291
+
292
+ /**
293
+ * Empties a box3
294
+ *
295
+ * @param {box3} a - box3 to empty
296
+ * @returns {box3} - the empty box3
297
+ */
298
+ box3.empty = function (a)
299
+ {
300
+ a[0] = FLOAT32_MAX;
301
+ a[1] = FLOAT32_MAX;
302
+ a[2] = FLOAT32_MAX;
303
+ a[3] = -FLOAT32_MAX;
304
+ a[4] = -FLOAT32_MAX;
305
+ a[5] = -FLOAT32_MAX;
306
+ return a;
307
+ };
308
+
309
+ /**
310
+ * Empties bounds
311
+ *
312
+ * @param {vec3} min - min bounds
313
+ * @param {vec3} max - max bounds
314
+ */
315
+ box3.bounds.empty = function (min, max)
316
+ {
317
+ min[0] = FLOAT32_MAX;
318
+ min[1] = FLOAT32_MAX;
319
+ min[2] = FLOAT32_MAX;
320
+ max[0] = -FLOAT32_MAX;
321
+ max[1] = -FLOAT32_MAX;
322
+ max[2] = -FLOAT32_MAX;
323
+ };
324
+
325
+ /**
326
+ * Checks two box3's for equality
327
+ *
328
+ * @param {box3} a - box3 to compare
329
+ * @param {box3} b - box3 to compare
330
+ * @returns {boolean} - true if box3s are equal
331
+ */
332
+ box3.equals = function (a, b)
333
+ {
334
+ let a0 = a[0],
335
+ a1 = a[1],
336
+ a2 = a[2],
337
+ a3 = a[3],
338
+ a4 = a[4],
339
+ a5 = a[5];
340
+
341
+ let b0 = b[0],
342
+ b1 = b[1],
343
+ b2 = b[2],
344
+ b3 = b[3],
345
+ b4 = b[4],
346
+ b5 = b[5];
347
+
348
+ return (
349
+ (a0 === b0 || Math.abs(a0 - b0) <= num.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0))) &&
350
+ (a1 === b1 || Math.abs(a1 - b1) <= num.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1))) &&
351
+ (a2 === b2 || Math.abs(a2 - b2) <= num.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2))) &&
352
+ (a3 === b3 || Math.abs(a3 - b3) <= num.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))) &&
353
+ (a4 === b4 || Math.abs(a4 - b4) <= num.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4))) &&
354
+ (a5 === b5 || Math.abs(a5 - b5) <= num.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)))
355
+ );
356
+ };
357
+
358
+ /**
359
+ * Checks a box3 and box3 components for equality
360
+ *
361
+ * @param {box3} a - box3 to compare
362
+ * @param {vec3} min - min bounds to compare
363
+ * @param {vec3} max - max bounds to compare
364
+ * @returns {boolean} - true if the box3 and bounds are equal
365
+ */
366
+ box3.equalsBounds = function (a, min, max)
367
+ {
368
+ let a0 = a[0],
369
+ a1 = a[1],
370
+ a2 = a[2],
371
+ a3 = a[3],
372
+ a4 = a[4],
373
+ a5 = a[5];
374
+
375
+ let b0 = min[0],
376
+ b1 = min[1],
377
+ b2 = min[2],
378
+ b3 = max[0],
379
+ b4 = max[1],
380
+ b5 = max[2];
381
+
382
+ return (
383
+ (a0 === b0 || Math.abs(a0 - b0) <= num.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0))) &&
384
+ (a1 === b1 || Math.abs(a1 - b1) <= num.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1))) &&
385
+ (a2 === b2 || Math.abs(a2 - b2) <= num.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2))) &&
386
+ (a3 === b3 || Math.abs(a3 - b3) <= num.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))) &&
387
+ (a4 === b4 || Math.abs(a4 - b4) <= num.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4))) &&
388
+ (a5 === b5 || Math.abs(a5 - b5) <= num.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)))
389
+ );
390
+ };
391
+
392
+ /**
393
+ * Checks for vec6 exact equality
394
+ *
395
+ * @param {box3} a - box3 to compare
396
+ * @param {box3} b - box3 to compare
397
+ * @returns {boolean} - true if both box3s are exactly equal
398
+ */
399
+ box3.exactEquals = function (a, b)
400
+ {
401
+ return (
402
+ a[0] === b[0] && a[1] === b[1] && a[2] === b[2] &&
403
+ a[3] === b[3] && a[4] === b[4] && a[5] === b[5]
404
+ );
405
+ };
406
+
407
+ /**
408
+ * Checks for exact equality between a box3 and components
409
+ *
410
+ * @param {box3} a - box3 to compare
411
+ * @param {vec3} min - min bounds to compare
412
+ * @param {vec3} max - max bounds to compare
413
+ * @returns {boolean} - true if the box3 and bounds are exactly equal
414
+ */
415
+ box3.exactEqualsBounds = function (a, min, max)
416
+ {
417
+ return (
418
+ a[0] === min[0] && a[1] === min[1] && a[2] === min[2] &&
419
+ a[3] === max[0] && a[4] === max[1] && a[5] === max[2]
420
+ );
421
+ };
422
+
423
+ /**
424
+ * Sets a box3 from the expansion of a box3 and a given scalar
425
+ *
426
+ * @param {box3} out - receiving box3
427
+ * @param {box3} a - source box3
428
+ * @param {number} s - scalar to expand by
429
+ * @returns {box3} out - receiving box3
430
+ */
431
+ box3.expandScalar = function (out, a, s)
432
+ {
433
+ out[0] = a[0] - s;
434
+ out[1] = a[1] - s;
435
+ out[2] = a[2] - s;
436
+ out[3] = a[3] + s;
437
+ out[4] = a[4] + s;
438
+ out[5] = a[5] + s;
439
+ return out;
440
+ };
441
+
442
+ /**
443
+ * Sets a box3 from the expansion of a box3 and a vector's values
444
+ *
445
+ * @param {box3} out - receiving box3
446
+ * @param {box3} a - source box3
447
+ * @param {number} vx - vector x
448
+ * @param {number} vy - vector y
449
+ * @param {number} vz - vector z
450
+ * @returns {box3} out - receiving box3
451
+ */
452
+ box3.expandValues = function (out, a, vx, vy, vz)
453
+ {
454
+ out[0] = a[0] - vx;
455
+ out[1] = a[1] - vy;
456
+ out[2] = a[2] - vz;
457
+ out[3] = a[3] + vx;
458
+ out[4] = a[4] + vy;
459
+ out[5] = a[5] + vz;
460
+ return out;
461
+ };
462
+
463
+ /**
464
+ * Expands a box3 by a vec3
465
+ *
466
+ * @param {box3} out - receiving box3
467
+ * @param {box3} a - source box3
468
+ * @param {vec3} v - vector to expand by
469
+ * @returns {box3} out - receiving box3
470
+ */
471
+ box3.expandVec3 = function (out, a, v)
472
+ {
473
+ out[0] = a[0] - v[0];
474
+ out[1] = a[1] - v[1];
475
+ out[2] = a[2] - v[2];
476
+ out[3] = a[3] + v[0];
477
+ out[4] = a[4] + v[1];
478
+ out[5] = a[5] + v[2];
479
+ return out;
480
+ };
481
+
482
+ /**
483
+ * Sets a box3 from an array at an optional offset
484
+ *
485
+ * @param {box3} out - receiving box3
486
+ * @param {Array} arr - source array
487
+ * @param {number} [index=0] - optional array index
488
+ * @returns {box3} - receiving box3
489
+ */
490
+ box3.fromArray = function (out, arr, index = 0)
491
+ {
492
+ out[0] = arr[index];
493
+ out[1] = arr[index + 1];
494
+ out[2] = arr[index + 2];
495
+ out[3] = arr[index + 3];
496
+ out[4] = arr[index + 4];
497
+ out[5] = arr[index + 5];
498
+ return out;
499
+ };
500
+
501
+ /**
502
+ * Sets a box3 from bounds
503
+ *
504
+ * @param {box3} out - receiving box3
505
+ * @param {vec3} min - source min bounds
506
+ * @param {vec3} max - source max bounds
507
+ * @returns {box3} out - receiving box3
508
+ */
509
+ box3.fromBounds = function (out, min, max)
510
+ {
511
+ out[0] = min[0];
512
+ out[1] = min[1];
513
+ out[2] = min[2];
514
+ out[3] = max[0];
515
+ out[4] = max[1];
516
+ out[5] = max[2];
517
+ return out;
518
+ };
519
+
520
+ box3.from = box3.fromBounds;
521
+
522
+
523
+ /**
524
+ * Sets a box3 from position and size
525
+ *
526
+ * @param {box3} out - receiving box3
527
+ * @param {vec3} position - source position
528
+ * @param {vec3} size - source size
529
+ * @returns {box3} out - receiving box3
530
+ */
531
+ box3.fromPositionSize = function (out, position, size)
532
+ {
533
+ out[0] = position[0] - size[0] * 0.5;
534
+ out[1] = position[1] - size[1] * 0.5;
535
+ out[2] = position[2] - size[2] * 0.5;
536
+ out[3] = position[0] + size[0] * 0.5;
537
+ out[4] = position[1] + size[1] * 0.5;
538
+ out[5] = position[2] + size[2] * 0.5;
539
+ return out;
540
+ };
541
+
542
+ /**
543
+ * Sets a box3 from an array of points
544
+ *
545
+ * @param {box3} out - receiving box3
546
+ * @param {Array.<vec3>} points - array of points
547
+ * @returns {box3} out - receiving box3
548
+ */
549
+ box3.fromPoints = function (out, points)
550
+ {
551
+ if (!points.length) return box3.empty(out);
552
+
553
+ out[0] = out[3] = points[0][0];
554
+ out[1] = out[4] = points[0][1];
555
+ out[2] = out[5] = points[0][2];
556
+
557
+ for (let i = 1; i < points.length; i++)
558
+ {
559
+ out[0] = Math.min(out[0], points[i][0]);
560
+ out[1] = Math.min(out[1], points[i][1]);
561
+ out[2] = Math.min(out[2], points[i][2]);
562
+ out[3] = Math.max(out[3], points[i][0]);
563
+ out[4] = Math.max(out[4], points[i][1]);
564
+ out[5] = Math.max(out[5], points[i][2]);
565
+ }
566
+
567
+ return out;
568
+ };
569
+
570
+ box3.setPoints = box3.fromPoints;
571
+
572
+ /**
573
+ * Sets a box3 from a sphere's components
574
+ *
575
+ * @param {box3} out - receiving box3
576
+ * @param {vec3} position - position
577
+ * @param {number} radius - radius
578
+ * @returns {box3} - receiving box3
579
+ */
580
+ box3.fromPositionRadius = function (out, position, radius)
581
+ {
582
+ out[0] = position[0] - radius;
583
+ out[1] = position[1] - radius;
584
+ out[2] = position[2] - radius;
585
+ out[3] = position[0] + radius;
586
+ out[4] = position[1] + radius;
587
+ out[5] = position[2] + radius;
588
+ return out;
589
+ };
590
+
591
+ /**
592
+ * Sets a box3 from a Float32Array(4) sphere
593
+ *
594
+ * @param {box3} out - receiving box3
595
+ * @param {sph3} sphere - source sphere
596
+ * @returns {box3} out - receiving box3
597
+ */
598
+ box3.fromSph3 = function (out, sphere)
599
+ {
600
+ if (sphere[3] < 0) return box3.empty(out);
601
+
602
+ out[0] = sphere[0] - sphere[3];
603
+ out[1] = sphere[1] - sphere[3];
604
+ out[2] = sphere[2] - sphere[3];
605
+ out[3] = sphere[0] + sphere[3];
606
+ out[4] = sphere[1] + sphere[3];
607
+ out[5] = sphere[2] + sphere[3];
608
+ return out;
609
+ };
610
+
611
+ /**
612
+ * Creates a box3 from a transform
613
+ * @param {box3} out
614
+ * @param {mat4} m
615
+ * @param {Number} [initialScale=1]
616
+ * @returns {box3} out
617
+ */
618
+ box3.fromTransform = function(out, m, initialScale=1)
619
+ {
620
+ out[0] = out[1] = out[2] = -initialScale / 2;
621
+ out[3] = out[4] = out[5] = initialScale / 2;
622
+ return box3.transformMat4(out, out, m);
623
+ };
624
+
625
+ /**
626
+ * Creates a box3 from values
627
+ * @param {Number} minx
628
+ * @param {Number} miny
629
+ * @param {Number} minz
630
+ * @param {Number} maxx
631
+ * @param {Number} maxy
632
+ * @param {Number} maxz
633
+ * @returns {box3}
634
+ */
635
+ box3.fromValues = function (minx, miny, minz, maxx, maxy, maxz)
636
+ {
637
+ const out = box3.create();
638
+ out[0] = minx;
639
+ out[1] = miny;
640
+ out[2] = minz;
641
+ out[3] = maxx;
642
+ out[4] = maxy;
643
+ out[5] = maxz;
644
+ return out;
645
+ };
646
+
647
+ /**
648
+ * Gets a box3's center
649
+ *
650
+ * @param {vec3} out
651
+ * @param {box3} a
652
+ * @return {vec3} out
653
+ */
654
+ box3.getCenter = function (out, a)
655
+ {
656
+ if (box3.isEmpty(a))
657
+ {
658
+ out[0] = 0;
659
+ out[1] = 0;
660
+ out[2] = 0;
661
+ return out;
662
+ }
663
+
664
+ out[0] = (a[0] + a[3]) * 0.5;
665
+ out[1] = (a[1] + a[4]) * 0.5;
666
+ out[2] = (a[2] + a[5]) * 0.5;
667
+ return out;
668
+ };
669
+
670
+ /**
671
+ * Sets a vec3 from a point clamped to a box3
672
+ *
673
+ * @param {vec3} out - receiving vec3
674
+ * @param {box3} a - source box
675
+ * @param {vec3} p - the point to clamp
676
+ * @returns {vec3} [out] - receiving vec3
677
+ */
678
+ box3.getClampedPoint = function (out, a, p)
679
+ {
680
+ if (box3.isEmpty(a))
681
+ {
682
+ out[0] = p[0];
683
+ out[1] = p[1];
684
+ out[2] = p[2];
685
+ return out;
686
+ }
687
+
688
+ out[0] = Math.max(a[0], Math.min(a[3], p[0]));
689
+ out[1] = Math.max(a[1], Math.min(a[4], p[1]));
690
+ out[2] = Math.max(a[2], Math.min(a[5], p[2]));
691
+ return out;
692
+ };
693
+
694
+ /**
695
+ * Sets a vec3 with the box3's max bounds
696
+ *
697
+ * @param {vec3} out - receiving vec3
698
+ * @param {box3} a - source box
699
+ * @returns {vec3} [out] - receiving vec3
700
+ */
701
+ box3.getMax = function (out, a)
702
+ {
703
+ out[0] = a[3];
704
+ out[1] = a[4];
705
+ out[2] = a[5];
706
+ return out;
707
+ };
708
+
709
+ /**
710
+ * Sets a vec3 with the box3's min bounds
711
+ *
712
+ * @param {vec3} out - receiving vec3
713
+ * @param {box3} a - source box
714
+ * @returns {vec3} [out] - receiving vec3
715
+ */
716
+ box3.getMin = function (out, a)
717
+ {
718
+ out[0] = a[0];
719
+ out[1] = a[1];
720
+ out[2] = a[2];
721
+ return out;
722
+ };
723
+
724
+ /**
725
+ * Sets a vec3 with a box3's size
726
+ *
727
+ * @param {vec3} out - receiving vec3
728
+ * @param {box3} a - source box
729
+ * @returns {vec3} [out] - receiving vec3
730
+ */
731
+ box3.getSize = function (out, a)
732
+ {
733
+ if (box3.isEmpty(a))
734
+ {
735
+ out[0] = 0;
736
+ out[1] = 0;
737
+ out[2] = 0;
738
+ return out;
739
+ }
740
+
741
+ out[0] = a[3] - a[0];
742
+ out[1] = a[4] - a[1];
743
+ out[2] = a[5] - a[2];
744
+ return out;
745
+ };
746
+
747
+ /**
748
+ * Sets a vec3 with the box3's position
749
+ *
750
+ * @param {vec3} out - receiving vec3
751
+ * @param {box3} a - source box
752
+ * @returns {vec3} [out] - receiving vec3
753
+ */
754
+ box3.getPosition = function (out, a)
755
+ {
756
+ return box3.getCenter(out, a);
757
+ };
758
+
759
+ /**
760
+ * Gets the effective radius of a bounding box
761
+ * @param {box3} a
762
+ * @returns {number}
763
+ */
764
+ box3.radius = function (a)
765
+ {
766
+ if (box3.isEmpty(a)) return 0;
767
+
768
+ let sX = a[3] - a[0],
769
+ sY = a[4] - a[1],
770
+ sZ = a[5] - a[2];
771
+
772
+ return Math.sqrt(sX * sX + sY * sY + sZ * sZ) * 0.5;
773
+ };
774
+
775
+ /**
776
+ * Sets a box3 from the intersect of two box3s
777
+ *
778
+ * @param {box3} out - receiving box3
779
+ * @param {box3} a - first box3
780
+ * @param {box3} b - second box3
781
+ * @returns {box3} out - receiving box3
782
+ */
783
+ box3.intersect = function (out, a, b)
784
+ {
785
+ out[0] = Math.max(a[0], b[0]);
786
+ out[1] = Math.max(a[1], b[1]);
787
+ out[2] = Math.max(a[2], b[2]);
788
+ out[3] = Math.min(a[3], b[3]);
789
+ out[4] = Math.min(a[4], b[4]);
790
+ out[5] = Math.min(a[5], b[5]);
791
+ return box3.isEmpty(out) ? box3.empty(out) : out;
792
+ };
793
+
794
+ /**
795
+ * Sets a box3 from the intersect of a box3 and min and max bounds
796
+ *
797
+ * @param {box3} out - receiving box3
798
+ * @param {box3} a - box3
799
+ * @param {vec3} min - min bounds
800
+ * @param {vec3} max - max bounds
801
+ * @returns {box3} out - receiving box3
802
+ */
803
+ box3.intersectBounds = function (out, a, min, max)
804
+ {
805
+ out[0] = Math.max(a[0], min[0]);
806
+ out[1] = Math.max(a[1], min[1]);
807
+ out[2] = Math.max(a[2], min[2]);
808
+ out[3] = Math.min(a[3], max[0]);
809
+ out[4] = Math.min(a[4], max[1]);
810
+ out[5] = Math.min(a[5], max[2]);
811
+ return box3.isEmpty(out) ? box3.empty(out) : out;
812
+ };
813
+
814
+ /**
815
+ * Checks for box3 intersection with another box3
816
+ *
817
+ * @param a - first box3 to compare
818
+ * @param b - second box3 to compare
819
+ * @returns {boolean} - true if intersection occurred
820
+ */
821
+ box3.intersects = function (a, b)
822
+ {
823
+ return !(
824
+ b[3] < a[0] || b[0] > a[3] ||
825
+ b[4] < a[1] || b[1] > a[4] ||
826
+ b[5] < a[2] || b[2] > a[5]
827
+ );
828
+ };
829
+
830
+ /**
831
+ * Checks for box3 intersection with min and max bounds
832
+ *
833
+ * @param {box3} a - box3 to compare
834
+ * @param {vec3} min - min bounds to compare
835
+ * @param {vec3} max - max bounds to compare
836
+ * @returns {boolean} - true if intersection occurred
837
+ */
838
+ box3.intersectsBounds = function (a, min, max)
839
+ {
840
+ return !(
841
+ max[0] < a[0] || min[0] > a[3] ||
842
+ max[1] < a[1] || min[1] > a[4] ||
843
+ max[2] < a[2] || min[2] > a[5]
844
+ );
845
+ };
846
+
847
+ /**
848
+ * Checks for box3 intersection with a plane normal and constant
849
+ *
850
+ * @param {box3} a - source box3
851
+ * @param {vec3} normal - plane normal vec3
852
+ * @param {number} constant - plane constant
853
+ * @returns {boolean} - true if intersection occurs
854
+ */
855
+ box3.intersectsNormalConstant = function (a, normal, constant)
856
+ {
857
+ let tMin = 0,
858
+ tMax = 0;
859
+
860
+ if (normal[0] > 0)
861
+ {
862
+ tMin += normal[0] * a[0];
863
+ tMax += normal[0] * a[3];
864
+ }
865
+ else
866
+ {
867
+ tMin += normal[0] * a[3];
868
+ tMax += normal[0] * a[0];
869
+ }
870
+
871
+ if (normal[1] > 0)
872
+ {
873
+ tMin += normal[1] * a[1];
874
+ tMax += normal[1] * a[4];
875
+ }
876
+ else
877
+ {
878
+ tMin += normal[1] * a[4];
879
+ tMax += normal[1] * a[1];
880
+ }
881
+
882
+ if (normal[2] > 0)
883
+ {
884
+ tMin += normal[2] * a[2];
885
+ tMax += normal[2] * a[5];
886
+ }
887
+ else
888
+ {
889
+ tMin += normal[2] * a[5];
890
+ tMax += normal[2] * a[2];
891
+ }
892
+
893
+ return (tMin <= -constant && tMax >= -constant);
894
+ };
895
+
896
+ /**
897
+ * Checks for box3 intersection with a Float32Array(4) plane
898
+ *
899
+ * @param {box3} a - box3 to compare
900
+ * @param {(pln|Float32Array)} p - plane to compare
901
+ * @returns {boolean} - true if intersection occurs
902
+ */
903
+ box3.intersectsPln = function (a, p)
904
+ {
905
+ // const x = p.subarray(0, 3);
906
+ return box3.intersectsNormalConstant(a, p, p[3]);
907
+ };
908
+
909
+ /**
910
+ * Checks for box3 intersection with a point
911
+ *
912
+ * @param {box3} a - box3 to compare
913
+ * @param {vec3} p - point to compare
914
+ * @returns {boolean} - true if intersection occurs
915
+ */
916
+ box3.intersectsPoint = function (a, p)
917
+ {
918
+ return p[0] >= a[0] && p[0] <= a[3] && p[1] >= a[1] && p[1] <= a[4] && p[2] >= a[2] && p[2] <= a[5];
919
+ };
920
+
921
+ /**
922
+ * Checks for box3 intersection with a point's values
923
+ *
924
+ * @param {box3} a - box3 to compare
925
+ * @param {Number} px - point x to compare
926
+ * @param {Number} py - point y to compare
927
+ * @param {Number} pz - point z to compare
928
+ * @returns {boolean} - true if intersection occurs
929
+ */
930
+ box3.intersectsValues = function (a, px, py, pz)
931
+ {
932
+ return px >= a[0] && px <= a[3] && py >= a[1] && py <= a[4] && pz >= a[2] && pz <= a[5];
933
+ };
934
+
935
+ /**
936
+ * Checks for box3 intersection with a sphere's components
937
+ *
938
+ * @param {box3} a - box3 to compare
939
+ * @param {vec3} position - sphere position to compare
940
+ * @param {number} radius - sphere radius to compare
941
+ * @returns {boolean} - true if intersection occurs
942
+ */
943
+ box3.intersectsPositionRadius = function (a, position, radius)
944
+ {
945
+ if (box3.isEmpty(a) || radius < 0) return false;
946
+
947
+ let x = Math.max(a[0], Math.min(a[3], position[0])) - position[0],
948
+ y = Math.max(a[1], Math.min(a[4], position[1])) - position[1],
949
+ z = Math.max(a[2], Math.min(a[5], position[2])) - position[2];
950
+
951
+ return (x * x + y * y + z * z) <= radius * radius;
952
+ };
953
+
954
+ /**
955
+ * Checks for box3 intersection with a Float32Array(4) sphere
956
+ *
957
+ * @param {box3} a - box3 to compare
958
+ * @param {sph3} sphere - sph3 to compare
959
+ * @returns {boolean} - true if intersection occurs
960
+ */
961
+ box3.intersectsSph3 = function (a, sphere)
962
+ {
963
+ if (box3.isEmpty(a) || sphere[3] < 0) return false;
964
+
965
+ let x = Math.max(a[0], Math.min(a[3], sphere[0])) - sphere[0],
966
+ y = Math.max(a[1], Math.min(a[4], sphere[1])) - sphere[1],
967
+ z = Math.max(a[2], Math.min(a[5], sphere[2])) - sphere[2];
968
+
969
+ return (x * x + y * y + z * z) <= sphere[3] * sphere[3];
970
+ };
971
+
972
+ /**
973
+ * Checks if a box3 is empty
974
+ *
975
+ * @param {box3} a - source box3
976
+ * @returns {boolean} - true if empty
977
+ */
978
+ box3.isEmpty = function (a)
979
+ {
980
+ return (a[3] < a[0]) || (a[4] < a[1]) || (a[5] < a[2]);
981
+ };
982
+
983
+ /**
984
+ * Checks if bounds are empty
985
+ *
986
+ * @param {vec3} min
987
+ * @param {vec3} max
988
+ * @returns {boolean}
989
+ */
990
+ box3.bounds.isEmpty = function (min, max)
991
+ {
992
+ return (max[0] < min[0]) || (max[1] < min[1]) || (max[2] < min[2]);
993
+ };
994
+
995
+ /**
996
+ * Sets a box3 from values
997
+ *
998
+ * @param {box3} out - receiving box3
999
+ * @param {Number} aX
1000
+ * @param {Number} aY
1001
+ * @param {Number} aZ
1002
+ * @param {Number} bX
1003
+ * @param {Number} bY
1004
+ * @param {Number} bZ
1005
+ * @returns {box3}
1006
+ */
1007
+ box3.set = function (out, aX, aY, aZ, bX, bY, bZ)
1008
+ {
1009
+ out[0] = aX;
1010
+ out[1] = aY;
1011
+ out[2] = aZ;
1012
+ out[3] = bX;
1013
+ out[4] = bY;
1014
+ out[5] = bZ;
1015
+ return out;
1016
+ };
1017
+
1018
+ /**
1019
+ * Sets the box's min bounds
1020
+ * @param {box3} out
1021
+ * @param {vec3} v
1022
+ * @returns {box3} out
1023
+ */
1024
+ box3.setMinBounds = vec3.copy;
1025
+
1026
+ /**
1027
+ * Sets the box's max bounds
1028
+ * @param {box3} out
1029
+ * @param {vec3} v
1030
+ * @returns {box3} out
1031
+ */
1032
+ box3.setMaxBounds = function (out, v)
1033
+ {
1034
+ out[3] = v[0];
1035
+ out[4] = v[1];
1036
+ out[5] = v[2];
1037
+ return out;
1038
+ };
1039
+
1040
+ /**
1041
+ * Gets the distance from a box3 to a point
1042
+ *
1043
+ * @param {box3} a - source box3
1044
+ * @param {vec3} p - point
1045
+ * @returns {number} - distance
1046
+ */
1047
+ box3.squaredDistanceToPoint = function (a, p)
1048
+ {
1049
+ let x = Math.max(a[0], Math.min(a[3], p[0])) - p[0],
1050
+ y = Math.max(a[1], Math.min(a[4], p[1])) - p[1],
1051
+ z = Math.max(a[2], Math.min(a[5], p[2])) - p[2];
1052
+
1053
+ return x * x + y * y + z * z;
1054
+ };
1055
+
1056
+ /**
1057
+ * Gets the surface area of a box3
1058
+ *
1059
+ * @param {box3} a - source box3
1060
+ * @returns {number} - surface area
1061
+ */
1062
+ box3.surfaceArea = function (a)
1063
+ {
1064
+ if (box3.isEmpty(a)) return 0;
1065
+
1066
+ let aa = a[3] - a[0],
1067
+ h = a[4] - a[1],
1068
+ d = a[5] - a[2];
1069
+
1070
+ return 2 * (aa * (h + d) + h * d);
1071
+ };
1072
+
1073
+ /**
1074
+ * Converts the box3 into an array
1075
+ *
1076
+ * @param {box3} a - receiving box3
1077
+ * @param {Array} arr - source array
1078
+ * @param {number} [offset=0] - optional offset
1079
+ * @returns {box3} a - receiving box3
1080
+ */
1081
+ box3.toArray = function (a, arr, offset = 0)
1082
+ {
1083
+ arr[offset] = a[0];
1084
+ arr[offset + 1] = a[1];
1085
+ arr[offset + 2] = a[2];
1086
+ arr[offset + 3] = a[3];
1087
+ arr[offset + 4] = a[4];
1088
+ arr[offset + 5] = a[5];
1089
+ return a;
1090
+ };
1091
+
1092
+ /**
1093
+ * Converts a box3 to bounds
1094
+ *
1095
+ * @param {vec3} outMin - receiving vector for min bounds
1096
+ * @param {vec3} outMax - receiving vector for max bounds
1097
+ * @param {box3} a - source box3
1098
+ */
1099
+ box3.toBounds = function (a, outMin, outMax)
1100
+ {
1101
+ outMin[0] = a[0];
1102
+ outMin[1] = a[1];
1103
+ outMin[2] = a[2];
1104
+ outMax[0] = a[3];
1105
+ outMax[1] = a[4];
1106
+ outMax[2] = a[5];
1107
+ return a;
1108
+ };
1109
+
1110
+ /**
1111
+ * Converts a box3 to an array of points
1112
+ * @param {box3} a
1113
+ * @param {Array} [points]
1114
+ * @returns {Array<Array>} points
1115
+ */
1116
+ box3.toPoints = function (a, points = [])
1117
+ {
1118
+ if (box3.isEmpty(a)) return points;
1119
+
1120
+ const
1121
+ ax = a[0],
1122
+ ay = a[1],
1123
+ az = a[2],
1124
+ bx = a[3],
1125
+ by = a[4],
1126
+ bz = a[5];
1127
+
1128
+ points.push([ bx, by, bz ]);
1129
+ points.push([ ax, by, bz ]);
1130
+ points.push([ bx, by, az ]);
1131
+ points.push([ ax, by, az ]);
1132
+ points.push([ bx, ay, bz ]);
1133
+ points.push([ ax, ay, bz ]);
1134
+ points.push([ bx, ay, az ]);
1135
+ points.push([ ax, ay, az ]);
1136
+
1137
+ return points;
1138
+ };
1139
+
1140
+ /**
1141
+ * Converts a box3 to position radius
1142
+ * @param a
1143
+ * @param outCenter
1144
+ * @returns {number}
1145
+ */
1146
+ box3.toPositionRadius = function (a, outCenter)
1147
+ {
1148
+ if (box3.isEmpty(a))
1149
+ {
1150
+ outCenter[0] = 0;
1151
+ outCenter[1] = 0;
1152
+ outCenter[2] = 0;
1153
+ return 0;
1154
+ }
1155
+
1156
+ let sX = a[3] - a[0],
1157
+ sY = a[4] - a[1],
1158
+ sZ = a[5] - a[2];
1159
+
1160
+ outCenter[0] = (a[0] + a[3]) * 0.5;
1161
+ outCenter[1] = (a[1] + a[4]) * 0.5;
1162
+ outCenter[2] = (a[2] + a[5]) * 0.5;
1163
+
1164
+ return Math.sqrt(sX * sX + sY * sY + sZ * sZ) * 0.5;
1165
+ };
1166
+
1167
+ /**
1168
+ * Converts bounds to position radius
1169
+ * @param {vec3} minBounds
1170
+ * @param {vec3} maxBounds
1171
+ * @param {vec3} outCenter
1172
+ * @returns {number}
1173
+ */
1174
+ box3.bounds.toPositionRadius = function (minBounds, maxBounds, outCenter)
1175
+ {
1176
+ if (box3.bounds.isEmpty(minBounds, maxBounds))
1177
+ {
1178
+ outCenter[0] = 0;
1179
+ outCenter[1] = 0;
1180
+ outCenter[2] = 0;
1181
+ return 0;
1182
+ }
1183
+
1184
+ let sX = maxBounds[0] - minBounds[0],
1185
+ sY = maxBounds[1] - minBounds[1],
1186
+ sZ = maxBounds[2] - minBounds[2];
1187
+
1188
+ outCenter[0] = (minBounds[0] + maxBounds[0]) * 0.5;
1189
+ outCenter[1] = (minBounds[1] + maxBounds[1]) * 0.5;
1190
+ outCenter[2] = (minBounds[2] + maxBounds[2]) * 0.5;
1191
+
1192
+ return Math.sqrt(sX * sX + sY * sY + sZ * sZ) * 0.5;
1193
+ };
1194
+
1195
+ /**
1196
+ * Sets a receiving box3 from the translation of a box3 and a vec3
1197
+ *
1198
+ * @param {box3} out - receiving box3
1199
+ * @param {box3} a - source box3
1200
+ * @param {vec3} v - vec3 to translate by
1201
+ * @returns {box3} out - receiving box3
1202
+ */
1203
+ box3.translate = function (out, a, v)
1204
+ {
1205
+ out[0] = a[0] + v[0];
1206
+ out[1] = a[1] + v[1];
1207
+ out[2] = a[2] + v[2];
1208
+ out[3] = a[3] + v[0];
1209
+ out[4] = a[4] + v[1];
1210
+ out[5] = a[5] + v[2];
1211
+ return out;
1212
+ };
1213
+
1214
+ /**
1215
+ * Sets a receiving box3 from the transformation of a box3 with a mat4
1216
+ *
1217
+ * @param {box3} out - receiving box3
1218
+ * @param {box3} a - source box3
1219
+ * @param {mat4} m - mat4 to transform with
1220
+ * @returns {box3} out - receiving box3
1221
+ */
1222
+ box3.transformMat4 = function (out, a, m)
1223
+ {
1224
+ if (box3.isEmpty(a)) return box3.empty(out);
1225
+
1226
+ const
1227
+ ax = a[0],
1228
+ ay = a[1],
1229
+ az = a[2],
1230
+ bx = a[3],
1231
+ by = a[4],
1232
+ bz = a[5],
1233
+ point = vec3.alloc();
1234
+
1235
+ out[0] = out[1] = out[2] = Infinity;
1236
+ out[3] = out[4] = out[5] = -Infinity;
1237
+
1238
+ for (let i = 0; i < 8; i++)
1239
+ {
1240
+ point[0] = i & 1 ? bx : ax;
1241
+ point[1] = i & 2 ? by : ay;
1242
+ point[2] = i & 4 ? bz : az;
1243
+ vec3.transformMat4(point, point, m);
1244
+ box3.addPoint(out, out, point);
1245
+ }
1246
+
1247
+ vec3.unalloc(point);
1248
+
1249
+ return out;
1250
+ };
1251
+
1252
+ /**
1253
+ * Sets a box3 from the union of two box3s
1254
+ *
1255
+ * @param {box3} out - receiving box3
1256
+ * @param {box3} a - first box3
1257
+ * @param {box3} b - second box3
1258
+ * @returns {box3} out - receiving box3
1259
+ */
1260
+ box3.union = function (out, a, b)
1261
+ {
1262
+ if (box3.isEmpty(a)) return box3.copy(out, b);
1263
+ if (box3.isEmpty(b)) return box3.copy(out, a);
1264
+
1265
+ out[0] = Math.min(a[0], b[0]);
1266
+ out[1] = Math.min(a[1], b[1]);
1267
+ out[2] = Math.min(a[2], b[2]);
1268
+ out[3] = Math.max(a[3], b[3]);
1269
+ out[4] = Math.max(a[4], b[4]);
1270
+ out[5] = Math.max(a[5], b[5]);
1271
+ return out;
1272
+ };
1273
+
1274
+ /**
1275
+ * Sets a box3 from the union of a box3 and min and max bounds
1276
+ *
1277
+ * @param {box3} out - receiving box3
1278
+ * @param {box3} a - box3
1279
+ * @param {vec3} min - min bounds
1280
+ * @param {vec3} max - max bounds
1281
+ * @returns {box3} out - receiving box3
1282
+ */
1283
+ box3.unionBounds = function (out, a, min, max)
1284
+ {
1285
+ if (box3.isEmpty(a)) return box3.fromBounds(out, min, max);
1286
+ if (min[0] > max[0] || min[1] > max[1] || min[2] > max[2]) return box3.copy(out, a);
1287
+
1288
+ out[0] = Math.min(a[0], min[0]);
1289
+ out[1] = Math.min(a[1], min[1]);
1290
+ out[2] = Math.min(a[2], min[2]);
1291
+ out[3] = Math.max(a[3], max[0]);
1292
+ out[4] = Math.max(a[4], max[1]);
1293
+ out[5] = Math.max(a[5], max[2]);
1294
+ return out;
1295
+ };
1296
+
1297
+ /**
1298
+ * Sets bounds from the union of two sets of bounds
1299
+ *
1300
+ * @param {vec3} outMin
1301
+ * @param {vec3} outMax
1302
+ * @param {vec3} aMin
1303
+ * @param {vec3} aMax
1304
+ * @param {vec3} bMin
1305
+ * @param {vec3} bMax
1306
+ */
1307
+ box3.bounds.union = function (outMin, outMax, aMin, aMax, bMin, bMax)
1308
+ {
1309
+ outMin[0] = Math.min(aMin[0], bMin[0]);
1310
+ outMin[1] = Math.min(aMin[1], bMin[1]);
1311
+ outMin[2] = Math.min(aMin[2], bMin[2]);
1312
+ outMax[0] = Math.max(aMax[0], bMax[0]);
1313
+ outMax[1] = Math.max(aMax[1], bMax[1]);
1314
+ outMax[2] = Math.max(aMax[2], bMax[2]);
1315
+ };
1316
+
1317
+ export const {
1318
+ bounds,
1319
+ alloc,
1320
+ unalloc,
1321
+ $min,
1322
+ $max,
1323
+ addPoint,
1324
+ addPoints,
1325
+ clone,
1326
+ contains,
1327
+ containsBounds,
1328
+ containsPoint,
1329
+ containsValue,
1330
+ copy,
1331
+ copyMin,
1332
+ copyMax,
1333
+ create,
1334
+ distanceToPoint,
1335
+ distanceToValues,
1336
+ empty,
1337
+ equals,
1338
+ equalsBounds,
1339
+ exactEquals,
1340
+ exactEqualsBounds,
1341
+ expandScalar,
1342
+ expandValues,
1343
+ expandVec3,
1344
+ fromArray,
1345
+ fromBounds,
1346
+ from,
1347
+ fromPositionSize,
1348
+ fromPoints,
1349
+ setPoints,
1350
+ fromPositionRadius,
1351
+ fromSph3,
1352
+ fromTransform,
1353
+ fromValues,
1354
+ getCenter,
1355
+ getClampedPoint,
1356
+ getMax,
1357
+ getMin,
1358
+ getSize,
1359
+ getPosition,
1360
+ radius,
1361
+ intersect,
1362
+ intersectBounds,
1363
+ intersects,
1364
+ intersectsBounds,
1365
+ intersectsNormalConstant,
1366
+ intersectsPln,
1367
+ intersectsPoint,
1368
+ intersectsValues,
1369
+ intersectsPositionRadius,
1370
+ intersectsSph3,
1371
+ isEmpty,
1372
+ set,
1373
+ setMinBounds,
1374
+ setMaxBounds,
1375
+ squaredDistanceToPoint,
1376
+ surfaceArea,
1377
+ toArray,
1378
+ toBounds,
1379
+ toPoints,
1380
+ toPositionRadius,
1381
+ translate,
1382
+ transformMat4,
1383
+ union,
1384
+ unionBounds
1385
+ } = box3;