@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/ray3.js ADDED
@@ -0,0 +1,1085 @@
1
+ import { vec3 } from "./vec3.js";
2
+ import { pln } from "./pln.js";
3
+ import { sph3 } from "./sph3.js";
4
+ import { box3 } from "./box3.js";
5
+ import { tri3 } from "./tri3.js";
6
+ import { mat3 } from "./mat3.js";
7
+
8
+ /**
9
+ * 3D Ray
10
+ * @typedef {Float32Array} ray3
11
+ */
12
+
13
+ export const ray3 = {};
14
+
15
+ function getAtParameter(out, a, t)
16
+ {
17
+ out[0] = a[0] + a[3] * t;
18
+ out[1] = a[1] + a[4] * t;
19
+ out[2] = a[2] + a[5] * t;
20
+ return out;
21
+ }
22
+
23
+ /**
24
+ * Allocates a pooled ray3
25
+ * @returns {Float32Array|ray3}
26
+ */
27
+ ray3.alloc = box3.alloc;
28
+
29
+ /**
30
+ * Unallocates a pooled ray3
31
+ * @param {ray3|Float32Array} a
32
+ */
33
+ ray3.unalloc = box3.unalloc;
34
+
35
+ /**
36
+ * Gets a subarray of a ray3's origin vector
37
+ * @property {box3} a
38
+ * @returns {TypedArray}
39
+ */
40
+ ray3.$origin = box3.$min;
41
+
42
+ /**
43
+ * Gets a subarray of a ray3's direction vector
44
+ * @property {box3} a
45
+ * @returns {TypedArray}
46
+ */
47
+ ray3.$direction = box3.$max;
48
+
49
+ /**
50
+ * Clones a ray3
51
+ *
52
+ * @param {ray3} a
53
+ * @returns {ray3}
54
+ */
55
+ ray3.clone = box3.clone;
56
+
57
+ /**
58
+ * Copies the values from one ray3 into another
59
+ *
60
+ * @param {ray3} out
61
+ * @param {ray3} a
62
+ * @returns {ray3} out
63
+ */
64
+ ray3.copy = box3.copy;
65
+
66
+ /**
67
+ * Copies the origin vector of one ray3 to another
68
+ *
69
+ * @param {ray3} out
70
+ * @param {ray3} a
71
+ * @returns {ray3}
72
+ */
73
+ ray3.copyOrigin = box3.copyMin;
74
+
75
+ /**
76
+ * Copies the direction vector of one ray3 to another
77
+ *
78
+ * @param {ray3} out
79
+ * @param {ray3} a
80
+ * @returns {ray3}
81
+ */
82
+ ray3.copyDirection = box3.copyMax;
83
+
84
+ /**
85
+ * Creates a ray3
86
+ *
87
+ * @returns {ray3}
88
+ */
89
+ ray3.create = function ()
90
+ {
91
+ return new Float32Array(6);
92
+ };
93
+
94
+ /**
95
+ * Gets the distance from a ray3 to a point
96
+ *
97
+ * @param {ray3} a - source ray3
98
+ * @param {vec3} p - point to measure distance to
99
+ * @returns {number} - distance
100
+ */
101
+ ray3.distance = function (a, p)
102
+ {
103
+ return Math.sqrt(ray3.squaredDistance(a, p));
104
+ };
105
+
106
+ /**
107
+ * Gets the distance from a ray3"s origin to a plane"s components
108
+ *
109
+ * @author three.js authors (converted)
110
+ * @param {ray3} a - source ray3
111
+ * @param {vec3} n - plane normal
112
+ * @param {number} c - plane constant
113
+ * @returns {null|number} - distance
114
+ */
115
+ ray3.distanceNormalConstant = function (a, n, c)
116
+ {
117
+ let den = n[0] * a[3] + n[1] * a[4] + n[2] * a[5];
118
+ let dist = (a[0] * n[0] + a[1] * n[1] + a[2] * n[2]) + c;
119
+ if (den === 0)
120
+ {
121
+ if (dist === 0) return 0;
122
+ return null;
123
+ }
124
+
125
+ let t = -dist / den;
126
+ return t >= 0 ? t * Math.hypot(a[3], a[4], a[5]) : null;
127
+ };
128
+
129
+ /**
130
+ * Gets the distance from a ray3"s origin to a Float32Array(4) plane
131
+ *
132
+ * @author three.js authors (converted)
133
+ * @param {ray3} a - source ray3
134
+ * @param {pln} p - plane to measure distance to
135
+ * @returns {null|Number} - distance
136
+ */
137
+ ray3.distancePln = function (a, p)
138
+ {
139
+ return ray3.distanceNormalConstant(a, p, p[3]);
140
+ };
141
+
142
+ /**
143
+ * Checks two ray3"s for equality
144
+ *
145
+ * @param {ray3} a
146
+ * @param {ray3} b
147
+ * @returns {boolean}
148
+ */
149
+ ray3.equals = box3.equals;
150
+
151
+ /**
152
+ * Checks a ray3 against it"s components for equality
153
+ *
154
+ * @param {ray3} a
155
+ * @param {vec3} min
156
+ * @param {vec3} max
157
+ * @returns {boolean}
158
+ */
159
+ ray3.equalsOriginDestination = box3.equalsBounds;
160
+
161
+ /**
162
+ * Checks for box3 exact equality
163
+ *
164
+ * @param {ray3} a
165
+ * @param {ray3} b
166
+ * @returns {boolean}
167
+ */
168
+ ray3.exactEquals = box3.exactEquals;
169
+
170
+ /**
171
+ * Checks for exact equality between a ray3 and ray3 components
172
+ *
173
+ * @param {ray3} a
174
+ * @param {vec3} min
175
+ * @param {vec3} max
176
+ * @returns {boolean}
177
+ */
178
+ ray3.exactEqualsOriginDestination = box3.exactEqualsBounds;
179
+
180
+ /**
181
+ * Sets a ray3 from origin and direction
182
+ *
183
+ * @param {ray3} out
184
+ * @param {vec3} o
185
+ * @param {vec3} d
186
+ * @returns {ray3} out
187
+ */
188
+ ray3.from = function (out, origin, direction)
189
+ {
190
+ out[0] = origin[0];
191
+ out[1] = origin[1];
192
+ out[2] = origin[2];
193
+ out[3] = direction[0];
194
+ out[4] = direction[1];
195
+ out[5] = direction[2];
196
+ return direction[0] || direction[1] || direction[2] ? ray3.normalize(out, out) : out;
197
+ };
198
+
199
+ /**
200
+ * Sets a ray3 from an array at an optional offset
201
+ *
202
+ * @param {ray3} out
203
+ * @param {Array} arr
204
+ * @param {number} [index=0]
205
+ * @returns {ray3}
206
+ */
207
+ ray3.fromArray = function (out, array, offset = 0)
208
+ {
209
+ box3.fromArray(out, array, offset);
210
+ return out[3] || out[4] || out[5] ? ray3.normalize(out, out) : out;
211
+ };
212
+
213
+ /**
214
+ * Sets a ray 3 from start and end vectors
215
+ *
216
+ * @param {ray3} out
217
+ * @param {vec3} start
218
+ * @param {vec3} end
219
+ * @returns {ray3}
220
+ */
221
+ ray3.fromStartEnd = function (out, start, end)
222
+ {
223
+ out[0] = start[0];
224
+ out[1] = start[1];
225
+ out[2] = start[2];
226
+ out[3] = end[0] - start[0];
227
+ out[4] = end[1] - start[1];
228
+ out[5] = end[2] - start[2];
229
+ return ray3.normalize(out, out);
230
+ };
231
+
232
+
233
+ let mat3_0;
234
+
235
+ /**
236
+ * Sets a ray3 from a mat4
237
+ * @param {ray3} out
238
+ * @param {mat4} m
239
+ * @return {ray3}
240
+ */
241
+ ray3.fromMat4 = function (out, m)
242
+ {
243
+ if (!mat3_0) mat3_0 = mat3.create();
244
+
245
+ out[0] = 0;
246
+ out[1] = 0;
247
+ out[2] = 0;
248
+ out[3] = 0;
249
+ out[4] = 0;
250
+ out[5] = -1;
251
+
252
+ vec3.transformMat4(ray3.$origin(out), ray3.$origin(out), m);
253
+ mat3.fromMat4(mat3_0, m);
254
+ vec3.transformMat3(ray3.$direction(out), ray3.$direction(out), mat3_0);
255
+ return ray3.normalize(out, out);
256
+ };
257
+
258
+ /**
259
+ * Sets a ray3 from a screen coordinates and an inverse view projection matrix
260
+ *
261
+ * @param {vec3} out - receiving ray3
262
+ * @param {vec2} coords - canvas coordinates (not client)
263
+ * @param {mat4} m - - inverse view projection matrix
264
+ * @param {mat4} viewport - viewport settings (x, y, width, height)
265
+ */
266
+ ray3.fromPerspective = function (out, coords, m, viewport)
267
+ {
268
+ // Convert view port co-ordinates
269
+ let x = (coords[0] - viewport[0]) * 2.0 / viewport[2] - 1.0,
270
+ y = (coords[1] - viewport[1]) * 2.0 / viewport[3] - 1.0;
271
+ // Calculate w
272
+ let oW = (m[3] * x + m[7] * y + m[11] * -1 + m[15]),
273
+ dW = (m[3] * x + m[7] * y + m[11] * 1 + m[15]);
274
+ // Check for perspective divide error
275
+ if (oW === 0.0 || dW === 0.0)
276
+ {
277
+ throw new Error("Perspective Divide Error");
278
+ }
279
+ // Transform origin
280
+ out[0] = (m[0] * x + m[4] * y + m[8] * -1 + m[12]) / oW;
281
+ out[1] = (m[1] * x + m[5] * y + m[9] * -1 + m[13]) / oW;
282
+ out[2] = (m[2] * x + m[6] * y + m[10] * -1 + m[14]) / oW;
283
+ // Transform direction
284
+ out[3] = ((m[0] * x + m[4] * y + m[8] * 1 + m[12]) / dW) - out[0];
285
+ out[4] = ((m[1] * x + m[5] * y + m[9] * 1 + m[13]) / dW) - out[1];
286
+ out[5] = ((m[2] * x + m[6] * y + m[10] * 1 + m[14]) / dW) - out[2];
287
+
288
+ // Normalize direction
289
+ return ray3.normalize(out, out);
290
+ };
291
+
292
+
293
+
294
+ /**
295
+ * Alternative to ray3.fromPerspective
296
+ * @param {ray3} out - receiving ray3
297
+ * @param {vec2} coords - event.ClientX, canvasHeight - event.clientY
298
+ * @param {mat4} invProjView - inverse projection view matrix
299
+ * @param {vec4} viewport - x, y, width, height
300
+ * @returns {ray3}
301
+ */
302
+ ray3.unproject = function (out, coords, invProjView, viewport)
303
+ {
304
+ const
305
+ start = [ coords[0], coords[1], 0 ],
306
+ end = [ coords[0], coords[1], 1 ];
307
+
308
+ vec3.unproject(start, start, invProjView, viewport);
309
+ vec3.unproject(end, end, invProjView, viewport);
310
+
311
+ return ray3.fromStartEnd(out, start, end);
312
+ };
313
+
314
+ /**
315
+ * Sets a vec3 with the position on a ray3 at a given distance from it"s origin
316
+ *
317
+ * @param {vec3} out - receiving vec3
318
+ * @param {ray3} a - source ray3
319
+ * @param {number} t - distance along the ray3
320
+ * @returns {vec3} out - receiving vec3
321
+ */
322
+ ray3.get = function (out, a, t)
323
+ {
324
+ const length = Math.hypot(a[3], a[4], a[5]);
325
+ return length === 0 ? vec3.set(out, a[0], a[1], a[2]) :
326
+ getAtParameter(out, a, t / length);
327
+ };
328
+
329
+ /**
330
+ * Sets a vec3 with the ray3's direction
331
+ *
332
+ * @param {vec3} out - receiving vec3
333
+ * @param {ray3} a - source ray
334
+ * @returns {vec3} [out] - receiving vec3
335
+ */
336
+ ray3.getDirection = box3.getMax;
337
+
338
+ /**
339
+ * Sets a vec3 with the closest point on a ray3 to a given point
340
+ *
341
+ * @author three.js authors (converted)
342
+ * @param {vec3} out - receiving vec3
343
+ * @param {ray3} a - source ray3
344
+ * @param {vec3} p - point to compare
345
+ * @returns {vec3} out - receiving vec3
346
+ */
347
+ ray3.getClosestPointToPoint = function (out, a, p)
348
+ {
349
+ let x = p[0] - a[0],
350
+ y = p[1] - a[1],
351
+ z = p[2] - a[2];
352
+
353
+ const directionLengthSquared = a[3] * a[3] + a[4] * a[4] + a[5] * a[5];
354
+ let dirDist = directionLengthSquared === 0 ? 0 :
355
+ (x * a[3] + y * a[4] + z * a[5]) / directionLengthSquared;
356
+
357
+ if (dirDist < 0)
358
+ {
359
+ out[0] = a[0];
360
+ out[1] = a[1];
361
+ out[2] = a[2];
362
+ }
363
+ else
364
+ {
365
+ getAtParameter(out, a, dirDist);
366
+ }
367
+
368
+ return out;
369
+ };
370
+
371
+ /**
372
+ * Sets a vec3 with the intersection point of a ray3 and boxes' components
373
+ * - Returns null if there was no intersection, or the receiving vec3 if there was
374
+ *
375
+ * @author three.js authors (converted)
376
+ * @param {vec3} out - receiving vec3
377
+ * @param {ray3} a - source ray3
378
+ * @param {vec3} min - box min bounds
379
+ * @param {vec3} max - box max bounds
380
+ * @returns {(null|vec3)} [out] - null or receiving vec3
381
+ */
382
+ ray3.getIntersectBounds = function (out, a, min, max)
383
+ {
384
+ let tMin, tMax, tyMin, tyMax, tzMin, tzMax;
385
+
386
+ let invDirX = 1 / a[3],
387
+ invDirY = 1 / a[4],
388
+ invDirZ = 1 / a[5];
389
+
390
+ if (invDirX >= 0)
391
+ {
392
+ tMin = (min[0] - a[0]) * invDirX;
393
+ tMax = (max[0] - a[0]) * invDirX;
394
+ }
395
+ else
396
+ {
397
+ tMin = (max[0] - a[0]) * invDirX;
398
+ tMax = (min[0] - a[0]) * invDirX;
399
+ }
400
+
401
+ if (invDirY >= 0)
402
+ {
403
+ tyMin = (min[1] - a[1]) * invDirY;
404
+ tyMax = (max[1] - a[1]) * invDirY;
405
+ }
406
+ else
407
+ {
408
+ tyMin = (max[1] - a[1]) * invDirY;
409
+ tyMax = (min[1] - a[1]) * invDirY;
410
+ }
411
+
412
+ if ((tMin > tyMax) || (tyMin > tMax)) return null;
413
+
414
+ if (tyMin > tMin || tMin !== tMin) tMin = tyMin;
415
+ if (tyMax < tMax || tMax !== tMax) tMax = tyMax;
416
+
417
+ if (invDirZ >= 0)
418
+ {
419
+ tzMin = (min[2] - a[2]) * invDirZ;
420
+ tzMax = (max[2] - a[2]) * invDirZ;
421
+ }
422
+ else
423
+ {
424
+ tzMin = (max[2] - a[2]) * invDirZ;
425
+ tzMax = (min[2] - a[2]) * invDirZ;
426
+ }
427
+
428
+ if ((tMin > tzMax) || (tzMin > tMax)) return null;
429
+
430
+ if (tzMin > tMin || tMin !== tMin) tMin = tzMin;
431
+ if (tzMax < tMax || tMax !== tMax) tMax = tzMax;
432
+ if (tMax < 0) return null;
433
+
434
+ return getAtParameter(out, a, tMin >= 0 ? tMin : tMax);
435
+ };
436
+
437
+ /**
438
+ * Sets a vec3 with the intersection point of a ray3 and boxes" components
439
+ * - Returns null if there was no intersection, or the receiving vec3 if there was
440
+ * TODO: Flip internals of getIntersectBounds with this as it will be called more often
441
+ *
442
+ * @param {vec3} out
443
+ * @param {ray3} ray
444
+ * @param {box3|Float32Array} box
445
+ * @returns {null|vec3}
446
+ */
447
+ ray3.getIntersectBox3 = (function ()
448
+ {
449
+ let vec3_0, vec3_1;
450
+
451
+ return function (out, a, b)
452
+ {
453
+ if (!vec3_0)
454
+ {
455
+ vec3_0 = vec3.create();
456
+ vec3_1 = vec3.create();
457
+ }
458
+
459
+ box3.getMin(vec3_0, b);
460
+ box3.getMax(vec3_1, b);
461
+
462
+ return ray3.getIntersectBounds(out, a, vec3_0, vec3_1);
463
+ };
464
+ })();
465
+
466
+ /**
467
+ * Sets a vec3 with the intersection point of a ray3 and a triangle"s components
468
+ * - Returns null if there was no intersection, or the receiving vec3 if there was
469
+ * @TODO: Flip internals with getIntersectVertices as this will be called more often
470
+ *
471
+ * @param {vec3} out - receiving vec3
472
+ * @param {ray3} a - source ray3
473
+ * @param {tri3} f - The tri3 to intersect
474
+ * @param {boolean} bfc - enables/ disables back face culling
475
+ * @returns {(null|vec3)} [out] - null or receiving vec3
476
+ */
477
+ ray3.getIntersectTri3 = (function ()
478
+ {
479
+ let vec3_0, vec3_1, vec3_2;
480
+
481
+ return function (out, a, f, bfc)
482
+ {
483
+ if (!vec3_0)
484
+ {
485
+ vec3_0 = vec3.create();
486
+ vec3_1 = vec3.create();
487
+ vec3_2 = vec3.create();
488
+ }
489
+
490
+ tri3.getV1(vec3_0, f);
491
+ tri3.getV2(vec3_1, f);
492
+ tri3.getV3(vec3_2, f);
493
+
494
+ return ray3.getIntersectVertices(out, a, vec3_0, vec3_1, vec3_2, bfc);
495
+ };
496
+ })();
497
+
498
+
499
+ /**
500
+ * Sets a vec3 with the intersection point of a ray3 and a triangle"s components
501
+ * - Returns null if there was no intersection, or the receiving vec3 if there was
502
+ *
503
+ * @author three.js authors (converted)
504
+ * @param {vec3} out - receiving vec3
505
+ * @param {ray3} a - source ray3
506
+ * @param {vec3} vertA - first triangle vertex position
507
+ * @param {vec3} vertB - second triangle vertex position
508
+ * @param {vec3} vertC - third triangle vertex position
509
+ * @param {boolean} bfc - enables/ disables back face culling
510
+ * @returns {(null|vec3)} [out] - null or receiving vec3
511
+ */
512
+ ray3.getIntersectVertices = (function ()
513
+ {
514
+ let vec3_0, vec3_1, vec3_2, vec3_3, vec3_4, vec3_5;
515
+
516
+ return function (out, a, vertA, vertB, vertC, bfc)
517
+ {
518
+ if (!vec3_0)
519
+ {
520
+ vec3_0 = vec3.create();
521
+ vec3_1 = vec3.create();
522
+ vec3_2 = vec3.create();
523
+ vec3_3 = vec3.create();
524
+ vec3_4 = vec3.create();
525
+ vec3_5 = vec3.create();
526
+ }
527
+
528
+ let o = ray3.getOrigin(vec3_4, a);
529
+ let d = ray3.getDirection(vec3_5, a);
530
+
531
+ let diff = vec3.subtract(vec3_0, o, vertA);
532
+ let e1 = vec3.subtract(vec3_1, vertB, vertA);
533
+ let e2 = vec3.subtract(vec3_2, vertC, vertA);
534
+ let n = vec3.cross(vec3_3, e1, e2);
535
+ let DdN = vec3.dot(d, n);
536
+ let sign;
537
+
538
+ if (DdN > 0)
539
+ {
540
+ if (bfc) return null;
541
+ sign = 1;
542
+ }
543
+ else if (DdN < 0)
544
+ {
545
+ sign = -1;
546
+ DdN = -DdN;
547
+ }
548
+ else return null;
549
+
550
+ vec3.cross(e2, diff, e2);
551
+ let b1 = sign * vec3.dot(d, e2);
552
+ if (b1 < 0) return null;
553
+
554
+ vec3.cross(e1, e1, diff);
555
+ let b2 = sign * vec3.dot(d, e1);
556
+ if (b2 < 0) return null;
557
+ if (b1 + b2 > DdN) return null;
558
+
559
+ let QdN = -sign * vec3.dot(diff, n);
560
+ if (QdN < 0) return null;
561
+
562
+ return getAtParameter(out, a, QdN / DdN);
563
+ };
564
+ })();
565
+
566
+ /**
567
+ * Sets a vec3 with the intersection point of a ray3 and a plane"s components
568
+ * - Returns null if there was no intersection, or the receiving vec3 if there was
569
+ *
570
+ * @param {vec3} out - receiving vec3
571
+ * @param {ray3} a - source ray3
572
+ * @param {vec3} n - plane normal
573
+ * @param {number} c - plane constant
574
+ * @returns {(null|vec3)} vecOut - null or receiving vec3
575
+ */
576
+ ray3.getIntersectNormalConstant = function (out, a, n, c)
577
+ {
578
+ let t = ray3.distanceNormalConstant(a, n, c);
579
+ return t !== null ? ray3.get(out, a, t) : null;
580
+ };
581
+
582
+ /**
583
+ * Sets a vec3 with the intersection point of a ray3 and a Float32Array(4) Plane
584
+ * - Returns null if there was no intersection, or the receiving vec3 if there was
585
+ *
586
+ * @param {vec3} out - receiving vec3
587
+ * @param {ray3} a - source ray3
588
+ * @param {(pln|Float32Array)} p - plane to intersect
589
+ * @returns {(null|vec3)} [out] - null or receiving vec3
590
+ */
591
+ ray3.getIntersectPln = function (out, a, p)
592
+ {
593
+ let t = ray3.distancePln(a, p);
594
+ return t !== null ? ray3.get(out, a, t) : null;
595
+ };
596
+
597
+ /**
598
+ * Sets a vec3 with the intersection point of a ray3 and a sphere"s components
599
+ * - Returns null if there was no intersection, or the receiving vec3 if there was
600
+ *
601
+ * @author three.js authors (converted)
602
+ * @param {vec3} out - receiving vec3
603
+ * @param {ray3} a - source ray3
604
+ * @param {vec3} p - sphere position
605
+ * @param {number} r - sphere radius
606
+ * @returns {(null|vec3)} [out] - null or receiving vec3
607
+ */
608
+ ray3.getIntersectPositionRadius = (function ()
609
+ {
610
+ let sph3_0;
611
+
612
+ return function (out, a, p, r)
613
+ {
614
+ if (!sph3_0) sph3_0 = sph3.create();
615
+ sph3.from(sph3_0, p, r);
616
+ return ray3.getIntersectSph3(out, a, sph3_0);
617
+ };
618
+ })();
619
+
620
+ /**
621
+ * Sets a vec3 with the intersection point of a ray3 and a Float32Array(4) sphere
622
+ * - Returns null if there was no intersection, or the receiving vec3 if there was
623
+ *
624
+ * @author three.js authors (converted)
625
+ * @param {vec3} out - receiving vec3
626
+ * @param {ray3} a - source ray3
627
+ * @param {sph3} s - sphere
628
+ * @returns {(null|vec3)} [out] - null or receiving vec3
629
+ */
630
+ ray3.getIntersectSph3 = function (out, a, s)
631
+ {
632
+ if (s[3] < 0) return null;
633
+
634
+ const
635
+ x = a[0] - s[0],
636
+ y = a[1] - s[1],
637
+ z = a[2] - s[2],
638
+ directionLengthSquared = a[3] * a[3] + a[4] * a[4] + a[5] * a[5];
639
+
640
+ if (directionLengthSquared === 0) return null;
641
+
642
+ const
643
+ directionDotOffset = a[3] * x + a[4] * y + a[5] * z,
644
+ constant = x * x + y * y + z * z - s[3] * s[3],
645
+ discriminant = directionDotOffset * directionDotOffset - directionLengthSquared * constant;
646
+
647
+ if (discriminant < 0) return null;
648
+
649
+ const
650
+ root = Math.sqrt(discriminant),
651
+ t0 = (-directionDotOffset - root) / directionLengthSquared,
652
+ t1 = (-directionDotOffset + root) / directionLengthSquared;
653
+
654
+ if (t0 < 0 && t1 < 0) return null;
655
+ if (t0 < 0) return getAtParameter(out, a, t1);
656
+ return getAtParameter(out, a, t0);
657
+ };
658
+
659
+
660
+ /**
661
+ * Gets the origin component of a ray3
662
+ *
663
+ * @param {vec3} out
664
+ * @param {ray3} a
665
+ * @returns {vec3} out
666
+ */
667
+ ray3.getOrigin = box3.getMin;
668
+
669
+ /**
670
+ * Checks for ray3 intersection with a Float32Array(6) box
671
+ * TODO: Replace internals with intersectsBounds as this will get called more often
672
+ * @param {ray3} a - ray3
673
+ * @param {(box3|Float32Array)} b - box
674
+ * @returns {boolean} - true if intersection occurs
675
+ */
676
+ ray3.intersectsBox3 = (function ()
677
+ {
678
+ let vec3_0;
679
+ return function (a, b)
680
+ {
681
+ if (!vec3_0) vec3_0 = vec3.create();
682
+ return ray3.getIntersectBox3(vec3_0, a, b) !== null;
683
+ };
684
+ })();
685
+
686
+ /**
687
+ * Checks for ray3 intersection with bounds
688
+ *
689
+ * @param {ray3} a - ray3
690
+ * @param {vec3} min - box min bounds
691
+ * @param {vec3} max - box max bounds
692
+ * @returns {boolean} - true if intersection occurs
693
+ */
694
+ ray3.intersectsBounds = (function ()
695
+ {
696
+ let vec3_0;
697
+ return function intersectsBounds(a, min, max)
698
+ {
699
+ if (!vec3_0) vec3_0 = vec3.create();
700
+ return ray3.getIntersectBounds(vec3_0, a, min, max) !== null;
701
+ };
702
+ })();
703
+
704
+ /**
705
+ * Checks for ray3 intersection with a plane"s components
706
+ *
707
+ * @param {ray3} a - source ray3
708
+ * @param {vec3} n - plane normal
709
+ * @param {number} c - plane constant
710
+ * @returns {boolean} - true if intersection occurs
711
+ */
712
+ ray3.intersectsNormalConstant = function (a, n, c)
713
+ {
714
+ let dist = (a[0] * n[0] + a[1] * n[1] + a[2] * n[2]) + c;
715
+ return dist === 0 ? true : ((n[0] * a[3] + n[1] * a[4] + n[2] * a[5]) * dist < 0);
716
+ };
717
+
718
+ /**
719
+ * Checks for ray3 intersection with a Float32Array(4) plane
720
+ *
721
+ * @param {ray3} a - source ray3
722
+ * @param {pln} p - plane
723
+ * @returns {boolean} - true if intersection occurs
724
+ */
725
+ ray3.intersectsPln = function (a, p)
726
+ {
727
+ let dist = (a[0] * p[0] + a[1] * p[1] + a[2] * p[2]) + p[3];
728
+ return (dist === 0) ? true : ((p[0] * a[3] + p[1] * a[4] + p[2] * a[5]) * dist < 0);
729
+ };
730
+
731
+ /**
732
+ * Checks for ray3 intersection with a sphere"s components
733
+ *
734
+ * @param {ray3} a - source ray3
735
+ * @param {vec3} p - sphere position
736
+ * @param {number} r - sphere radius
737
+ * @returns {boolean} - true if intersection occurs
738
+ */
739
+ ray3.intersectsPositionRadius = function (a, p, r)
740
+ {
741
+ return ray3.distance(a, p) <= r;
742
+ };
743
+
744
+ /**
745
+ * Checks for ray3 intersection with a Float32Array(4) Sphere
746
+ *
747
+ * @param {ray3} a - source ray3
748
+ * @param {sph3|Float32Array} s - sphere
749
+ * @returns {boolean} - true if intersection occurs
750
+ */
751
+ ray3.intersectsSph3 = function (a, s)
752
+ {
753
+ return ray3.distance(a, s) <= s[3];
754
+ };
755
+
756
+ /**
757
+ * Sets the direction of a ray3 to be looking at a specific point
758
+ *
759
+ * @param {ray3} out - receiving ray3
760
+ * @param {vec3} a - source ray3
761
+ * @param {vec3} p - point to look at
762
+ * @returns {ray3} out - receiving ray3
763
+ */
764
+ ray3.lookAt = function (out, a, p)
765
+ {
766
+ out[0] = a[0];
767
+ out[1] = a[1];
768
+ out[2] = a[2];
769
+ out[3] = p[0] - a[0];
770
+ out[4] = p[1] - a[1];
771
+ out[5] = p[2] - a[2];
772
+ return ray3.normalize(out, out);
773
+ };
774
+
775
+ /**
776
+ * Sets a ray3 from the results of normalizing another
777
+ *
778
+ * @param {ray3} out - receiving ray3
779
+ * @param {ray3} a - source ray3
780
+ * @returns {ray3} out - receiving ray3
781
+ */
782
+ ray3.normalize = function (out, a)
783
+ {
784
+ if (out !== a)
785
+ {
786
+ out[0] = a[0];
787
+ out[1] = a[1];
788
+ out[2] = a[2];
789
+ }
790
+
791
+ let x = a[3],
792
+ y = a[4],
793
+ z = a[5];
794
+
795
+ // Normalize the direction
796
+ let len = x * x + y * y + z * z;
797
+ if (len > 0)
798
+ {
799
+ len = 1 / Math.sqrt(len);
800
+ out[3] = x * len;
801
+ out[4] = y * len;
802
+ out[5] = z * len;
803
+ }
804
+ else
805
+ {
806
+ out[3] = 0;
807
+ out[4] = 0;
808
+ out[5] = 0;
809
+ throw new Error("Normalization error");
810
+ }
811
+
812
+ return out;
813
+ };
814
+
815
+ /**
816
+ * Shifts the origin of a ray3 to be further down it"s direction
817
+ *
818
+ * @param {ray3} out - receiving ray3
819
+ * @param {ray3} a - source ray3
820
+ * @param {number} t - distance along the ray3
821
+ * @returns {ray3} - receiving ray3
822
+ */
823
+ ray3.recast = function (out, a, t)
824
+ {
825
+ const length = Math.hypot(a[3], a[4], a[5]);
826
+ const scale = length === 0 ? 0 : t / length;
827
+ out[0] = a[0] + (a[3] * scale);
828
+ out[1] = a[1] + (a[4] * scale);
829
+ out[2] = a[2] + (a[5] * scale);
830
+ out[3] = a[3];
831
+ out[4] = a[4];
832
+ out[5] = a[5];
833
+ return out;
834
+ };
835
+
836
+ /**
837
+ * Sets a ray3 from values
838
+ *
839
+ * @param {ray3} out
840
+ * @param {vec3} startProp
841
+ * @param {vec3} endProp
842
+ * @returns {ray3} out
843
+ */
844
+ ray3.set = function (out, originX, originY, originZ, directionX, directionY, directionZ)
845
+ {
846
+ box3.set(out, originX, originY, originZ, directionX, directionY, directionZ);
847
+ return directionX || directionY || directionZ ? ray3.normalize(out, out) : out;
848
+ };
849
+
850
+ /**
851
+ * Ray3 generic sorting
852
+ *
853
+ * @param {{}} a
854
+ * @param {number} a.distance
855
+ * @param {{}} b
856
+ * @param {number} b.distance
857
+ * @returns {number}
858
+ */
859
+ ray3.SORT = function (a, b)
860
+ {
861
+ return a.distance - b.distance;
862
+ };
863
+
864
+ /**
865
+ * Gets the squared distance from a ray3 to a point
866
+ *
867
+ * @author three.js authors (converted)
868
+ * @param {ray3} a - source ray3
869
+ * @param {vec3} p - point to measure distance to
870
+ * @returns {number} - squared distance
871
+ */
872
+ ray3.squaredDistance = (function ()
873
+ {
874
+ let vec3_0, vec3_1;
875
+ return function distanceSquared(a, p)
876
+ {
877
+ if (!vec3_0)
878
+ {
879
+ vec3_0 = vec3.create();
880
+ vec3_1 = vec3.create();
881
+ }
882
+
883
+ vec3_0[0] = p[0] - a[0];
884
+ vec3_0[1] = p[1] - a[1];
885
+ vec3_0[2] = p[2] - a[2];
886
+
887
+ const directionLengthSquared = a[3] * a[3] + a[4] * a[4] + a[5] * a[5];
888
+ let dirDist = directionLengthSquared === 0 ? 0 :
889
+ (vec3_0[0] * a[3] + vec3_0[1] * a[4] + vec3_0[2] * a[5]) / directionLengthSquared;
890
+ if (dirDist < 0)
891
+ {
892
+ ray3.getOrigin(vec3_1, a);
893
+ return vec3.squaredDistance(vec3_1, p); // could just pass in the ray3
894
+ }
895
+
896
+ getAtParameter(vec3_0, a, dirDist);
897
+ return vec3.squaredDistance(vec3_0, p);
898
+ };
899
+ })();
900
+
901
+ /**
902
+ * Sets an array at at optional offset from a ray3
903
+ *
904
+ * @param {ray3} a
905
+ * @param {Array} arr
906
+ * @param {number} [index]
907
+ * @returns {ray3} a
908
+ */
909
+ ray3.toArray = box3.toArray;
910
+
911
+ /**
912
+ * Sets origin and direction vectors from a ray3
913
+ *
914
+ * @param {ray3} a
915
+ * @param {vec3} origin
916
+ * @param {vec3} direction
917
+ * @returns {ray3} a
918
+ */
919
+ ray3.toOriginDirection = function (a, origin, direction)
920
+ {
921
+ origin[0] = a[0];
922
+ origin[1] = a[1];
923
+ origin[2] = a[2];
924
+ direction[0] = a[3];
925
+ direction[1] = a[4];
926
+ direction[2] = a[5];
927
+ return a;
928
+ };
929
+
930
+ /**
931
+ * Transforms a ray3 by a mat4
932
+ *
933
+ * @param {ray3} out - receiving ray3
934
+ * @param {vec3} a - ray3 to transform
935
+ * @param {mat4} m - matrix to transform by
936
+ */
937
+ ray3.transformMat4 = function (out, a, m)
938
+ {
939
+ let oX = a[0],
940
+ oY = a[1],
941
+ oZ = a[2],
942
+ dX = a[3] + a[0],
943
+ dY = a[4] + a[1],
944
+ dZ = a[5] + a[2];
945
+
946
+ // Calculate w
947
+ let oW = (m[3] * oX + m[7] * oY + m[11] * oZ + m[15]) || 1.0,
948
+ dW = (m[3] * dX + m[7] * dY + m[11] * dZ + m[15]) || 1.0;
949
+ // Transform origin
950
+ out[0] = (m[0] * oX + m[4] * oY + m[8] * oZ + m[12]) / oW;
951
+ out[1] = (m[1] * oX + m[5] * oY + m[9] * oZ + m[13]) / oW;
952
+ out[2] = (m[2] * oX + m[6] * oY + m[10] * oZ + m[14]) / oW;
953
+ // Transform direction
954
+ out[3] = (m[0] * dX + m[4] * dY + m[8] * dZ + m[12]) / dW - out[0];
955
+ out[4] = (m[1] * dX + m[5] * dY + m[9] * dZ + m[13]) / dW - out[1];
956
+ out[5] = (m[2] * dX + m[6] * dY + m[10] * dZ + m[14]) / dW - out[2];
957
+ // Normalize direction
958
+ return ray3.normalize(out, out);
959
+ };
960
+
961
+ /**
962
+ * Translates a ray3
963
+ *
964
+ * @param {ray3} out
965
+ * @param {ray3} a
966
+ * @param {vec3} v
967
+ * @returns {ray3} out
968
+ */
969
+ ray3.translate = function (out, a, v)
970
+ {
971
+ out[0] = a[0] + v[0];
972
+ out[1] = a[1] + v[1];
973
+ out[2] = a[2] + v[2];
974
+ out[3] = a[3];
975
+ out[4] = a[4];
976
+ out[5] = a[5];
977
+ return out;
978
+ };
979
+
980
+ let viewPort;
981
+
982
+ /**
983
+ * Gets a ray3 from an element event
984
+ * @param {ray3} ray
985
+ * @param {mat4} viewProjectionInverse
986
+ * @param {event} event
987
+ * @param {vec2} [mouse]
988
+ * @param {vec2} [pixel]
989
+ * @param {vec2} [css]
990
+ * @returns {ray3}
991
+ */
992
+ ray3.fromEvent = function (ray, viewProjectionInverse, event, mouse, pixel, css)
993
+ {
994
+ if (!viewPort) viewPort = pln.create();
995
+
996
+ const
997
+ el = event.target || event.srcElement,
998
+ rect = el.getBoundingClientRect(),
999
+ mX = event.clientX,
1000
+ mY = event.clientY,
1001
+ cX = mX - rect.left,
1002
+ cY = mY - rect.top,
1003
+ pX = cX * el.width / el.clientWidth,
1004
+ pY = el.height - cY * el.height / el.clientHeight - 1;
1005
+
1006
+ // Should this be a param?
1007
+ viewPort[0] = 0;
1008
+ viewPort[1] = 0;
1009
+ viewPort[2] = el.width;
1010
+ viewPort[3] = el.height;
1011
+
1012
+ if (mouse)
1013
+ {
1014
+ mouse[0] = mX;
1015
+ mouse[1] = mY;
1016
+ }
1017
+
1018
+ if (pixel)
1019
+ {
1020
+ pixel[0] = pX;
1021
+ pixel[1] = pY;
1022
+ }
1023
+
1024
+ if (css)
1025
+ {
1026
+ css[0] = cX;
1027
+ css[1] = cY;
1028
+ }
1029
+
1030
+ return ray3.unproject(ray, [ pX, pY ], viewProjectionInverse, viewPort);
1031
+ };
1032
+
1033
+ export const {
1034
+ alloc,
1035
+ unalloc,
1036
+ $origin,
1037
+ $direction,
1038
+ clone,
1039
+ copy,
1040
+ copyOrigin,
1041
+ copyDirection,
1042
+ create,
1043
+ distance,
1044
+ distanceNormalConstant,
1045
+ distancePln,
1046
+ equals,
1047
+ equalsOriginDestination,
1048
+ exactEquals,
1049
+ exactEqualsOriginDestination,
1050
+ from,
1051
+ fromArray,
1052
+ fromStartEnd,
1053
+ fromMat4,
1054
+ fromPerspective,
1055
+ unproject,
1056
+ get,
1057
+ getDirection,
1058
+ getClosestPointToPoint,
1059
+ getIntersectBounds,
1060
+ getIntersectBox3,
1061
+ getIntersectTri3,
1062
+ getIntersectVertices,
1063
+ getIntersectNormalConstant,
1064
+ getIntersectPln,
1065
+ getIntersectPositionRadius,
1066
+ getIntersectSph3,
1067
+ getOrigin,
1068
+ intersectsBox3,
1069
+ intersectsBounds,
1070
+ intersectsNormalConstant,
1071
+ intersectsPln,
1072
+ intersectsPositionRadius,
1073
+ intersectsSph3,
1074
+ lookAt,
1075
+ normalize,
1076
+ recast,
1077
+ set,
1078
+ SORT,
1079
+ squaredDistance,
1080
+ toArray,
1081
+ toOriginDirection,
1082
+ transformMat4,
1083
+ translate,
1084
+ fromEvent
1085
+ } = ray3;