@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/pln.js ADDED
@@ -0,0 +1,762 @@
1
+ import { num } from "./num.js";
2
+ import { vec3 } from "./vec3.js";
3
+ import { vec4 } from "./vec4.js";
4
+ import { mat3 } from "./mat3.js";
5
+ import { box3 } from "./box3.js";
6
+ import { pool } from "./pool.js";
7
+
8
+ /**
9
+ * Plane
10
+ *
11
+ * @typedef {Float32Array} pln
12
+ */
13
+
14
+ export const pln = {};
15
+
16
+ /**
17
+ * Allocates a pooled pln
18
+ * @returns {Float32Array|pln}
19
+ */
20
+ pln.alloc = function()
21
+ {
22
+ return pool.allocF32(4);
23
+ };
24
+
25
+ /**
26
+ * Unallocates a pooled pln
27
+ * @param {pln|Float32Array} a
28
+ */
29
+ pln.unalloc = function(a)
30
+ {
31
+ pool.freeType(a);
32
+ };
33
+
34
+ /**
35
+ * Returns a subarray of a plane's normal
36
+ *
37
+ * @param {pln} a - source plane
38
+ * @returns {*} - plane normal reference
39
+ */
40
+ pln.$normal = function(a)
41
+ {
42
+ return a.subarray(0, 3);
43
+ };
44
+
45
+
46
+ /**
47
+ * Clones a pln
48
+ *
49
+ * @param {pln} a - Source plane
50
+ * @returns {pln} - Cloned plane
51
+ */
52
+ pln.clone = vec4.clone;
53
+
54
+ /**
55
+ * Returns the constant component of the pln
56
+ *
57
+ * @param {pln} a - Source plane
58
+ * @returns {number} - plane's constant
59
+ */
60
+ pln.constant = function(a)
61
+ {
62
+ return a[3];
63
+ };
64
+
65
+ /**
66
+ * Copies a pln
67
+ *
68
+ * @param {pln} a - Target plane
69
+ * @param {pln} b - Source plane
70
+ * @returns {pln} - Target plane
71
+ */
72
+ pln.copy = vec4.copy;
73
+
74
+ /**
75
+ * Creates a plane
76
+ *
77
+ * @returns {pln}
78
+ */
79
+ pln.create = vec4.create;
80
+
81
+ /**
82
+ * Gets the distance from a plane to a point
83
+ *
84
+ * @param {pln} a - plane to compare
85
+ * @param {vec3} p - Point to compare
86
+ * @returns {number} - The distance between them
87
+ */
88
+ pln.distanceToPoint = function(a, p)
89
+ {
90
+ const length = Math.hypot(a[0], a[1], a[2]);
91
+ const distance = (a[0] * p[0] + a[1] * p[1] + a[2] * p[2]) + a[3];
92
+ return length === 0 ? distance : distance / length;
93
+ };
94
+
95
+ /**
96
+ * Gets the distance from a plane to the components of a sphere
97
+ *
98
+ * @param {pln} a - plane to compare
99
+ * @param {vec3} position - sphere position to compare
100
+ * @param {number} radius - sphere radius to compare
101
+ * @returns {number} - The distance between them
102
+ */
103
+ pln.distanceToPositionRadius = function(a, position, radius)
104
+ {
105
+ return pln.distanceToPoint(a, position) - radius;
106
+ };
107
+
108
+ /**
109
+ * Gets the distance from a plane to a Float32Array(4) sphere
110
+ *
111
+ * @param {pln} a - plane to compare
112
+ * @param {sph3} sphere - sphere to compare
113
+ * @returns {number} - The distance between them
114
+ */
115
+ pln.distanceToSph3 = function(a, sphere)
116
+ {
117
+ return pln.distanceToPoint(a, sphere) - sphere[3];
118
+ };
119
+
120
+ /**
121
+ * Gets the distance from a plane to a point's values
122
+ *
123
+ * @param {pln} a - plane to compare
124
+ * @param {Number} px - Point x to compare
125
+ * @param {Number} py - Point y to compare
126
+ * @param {Number} pz - Point z to compare
127
+ * @returns {number} - The distance between them
128
+ */
129
+ pln.distanceToValues = function(a, px, py, pz)
130
+ {
131
+ const length = Math.hypot(a[0], a[1], a[2]);
132
+ const distance = (a[0] * px + a[1] * py + a[2] * pz) + a[3];
133
+ return length === 0 ? distance : distance / length;
134
+ };
135
+
136
+ /**
137
+ * Compares two plns for equality
138
+ *
139
+ * @param {pln} a - plane to compare
140
+ * @param {pln} b - plane to compare
141
+ * @returns {boolean} - true if equal
142
+ */
143
+ pln.equals = vec4.equals;
144
+
145
+ /**
146
+ * Compares a pln to plane components
147
+ *
148
+ * @param {pln} a - plane to compare
149
+ * @param {vec3} normal - plane normal to compare
150
+ * @param {number} constant - plane constant to compare
151
+ * @returns {boolean} - true if equal
152
+ */
153
+ pln.equalsNormalConstant = function(a, normal, constant)
154
+ {
155
+ let a0 = a[0],
156
+ a1 = a[1],
157
+ a2 = a[2],
158
+ a3 = a[3],
159
+ b0 = normal[0],
160
+ b1 = normal[1],
161
+ b2 = normal[2],
162
+ b3 = constant;
163
+
164
+ return (
165
+ Math.abs(a0 - b0) <= num.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
166
+ Math.abs(a1 - b1) <= num.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
167
+ Math.abs(a2 - b2) <= num.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
168
+ Math.abs(a3 - b3) <= num.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))
169
+ );
170
+ };
171
+
172
+ /**
173
+ * Compares two plns for exact equality
174
+ *
175
+ * @param {pln} a - plane to compare
176
+ * @param {pln} b - plane to compare
177
+ * @returns {boolean}
178
+ */
179
+ pln.exactEquals = vec4.exactEquals;
180
+
181
+ /**
182
+ * Compares a pln to plane components for exact equality
183
+ *
184
+ * @param {pln} a - plane to compare
185
+ * @param {vec3} normal - plane normal to compare
186
+ * @param {number} constant - plane constant to compare
187
+ * @returns {boolean}
188
+ */
189
+ pln.exactEqualsNormalConstant = function(a, normal, constant)
190
+ {
191
+ return a[0] === normal[0] && a[1] === normal[1] && a[2] === normal[2] && a[3] === constant;
192
+ };
193
+
194
+ /**
195
+ * Extracts a pln's components
196
+ *
197
+ * @param {pln} a - Source plane
198
+ * @param {vec3} outNormal - Receiving vec3
199
+ * @returns {number} - plane constant
200
+ */
201
+ pln.extract = function(a, outNormal)
202
+ {
203
+ outNormal[0] = a[0];
204
+ outNormal[1] = a[1];
205
+ outNormal[2] = a[2];
206
+ return a[3];
207
+ };
208
+
209
+ /**
210
+ * Sets a pln from plane components
211
+ *
212
+ * @param {pln} out - Receiving plane
213
+ * @param {vec3} n - plane normal to set
214
+ * @param {number} c - plane constant to set
215
+ * @returns {pln} out - receiving plane
216
+ */
217
+ pln.fromNormalConstant = function(out, n, c)
218
+ {
219
+ out[0] = n[0];
220
+ out[1] = n[1];
221
+ out[2] = n[2];
222
+ out[3] = c;
223
+ return out;
224
+ };
225
+
226
+ /**
227
+ * Sets from coplanar points
228
+ *
229
+ * @author three.js (conversion)
230
+ * @param {pln} out - Receiving plane
231
+ * @param {vec3} a - Coplanar point a
232
+ * @param {vec3} b - Coplanar point b
233
+ * @param {vec3} c - Coplanar point c
234
+ * @returns {pln} out - Receiving pln
235
+ */
236
+ pln.fromCoplanarPoints = function(out, a, b, c)
237
+ {
238
+ let ax = c[0] - b[0],
239
+ ay = c[1] - b[1],
240
+ az = c[2] - b[2],
241
+ bx = a[0] - b[0],
242
+ by = a[1] - b[1],
243
+ bz = a[2] - b[2];
244
+
245
+ // get cross product
246
+ let x = ay * bz - az * by,
247
+ y = az * bx - ax * bz,
248
+ z = ax * by - ay * bx;
249
+
250
+ // normalize
251
+ let len = x * x + y * y + z * z;
252
+ if (len > 0)
253
+ {
254
+ len = 1 / Math.sqrt(len);
255
+ out[0] = x * len;
256
+ out[1] = y * len;
257
+ out[2] = z * len;
258
+ }
259
+ else
260
+ {
261
+ out[0] = 0;
262
+ out[1] = 0;
263
+ out[2] = 0;
264
+ throw new Error("Normalization error");
265
+ }
266
+
267
+ // set coplanar point
268
+ out[3] = -(a[0] * out[0] + a[1] * out[1] + a[2] * out[2]);
269
+ return out;
270
+ };
271
+
272
+ /**
273
+ * Sets a pln from normal and a coplanar point
274
+ *
275
+ * @param {pln} out - receiving plane
276
+ * @param {vec3} normal - normal
277
+ * @param {vec3} point - coplanar point
278
+ * @returns {pln} out - receiving plane
279
+ */
280
+ pln.fromNormalAndCoplanarPoint = function(out, normal, point)
281
+ {
282
+ out[0] = normal[0];
283
+ out[1] = normal[1];
284
+ out[2] = normal[2];
285
+ out[3] = -(point[0] * normal[0] + point[1] * normal[1] + point[2] * normal[2]);
286
+ return out;
287
+ };
288
+
289
+ /**
290
+ * Gets a pln's coplanar point
291
+ *
292
+ * @param {vec3} out - receiving vec3
293
+ * @param {pln} a - the source plane
294
+ * @returns {vec3} out - receiving vec3
295
+ */
296
+ pln.getCoplanarPoint = function(out, a)
297
+ {
298
+ const lengthSquared = a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
299
+ if (lengthSquared === 0)
300
+ {
301
+ out[0] = out[1] = out[2] = 0;
302
+ return out;
303
+ }
304
+ const inverseLengthSquared = 1 / lengthSquared;
305
+ out[0] = a[0] * -a[3] * inverseLengthSquared;
306
+ out[1] = a[1] * -a[3] * inverseLengthSquared;
307
+ out[2] = a[2] * -a[3] * inverseLengthSquared;
308
+ return out;
309
+ };
310
+
311
+ /**
312
+ * Sets a vec3 with the intersection point of a plane and a Float32Array(6) line
313
+ * - Returns null if there was no intersection, or the receiving vec3 if there was
314
+ *
315
+ * @author Three.js (conversion)
316
+ * @param {vec3} out - receiving vec3
317
+ * @param {pln} a - plane
318
+ * @param {(lne3|Float32Array)} l - line
319
+ * @returns {(null|vec3)} null|out - null or receiving vec3
320
+ */
321
+ pln.getIntersectLne3 = function(out, a, l)
322
+ {
323
+ let lsx = l[0],
324
+ lsy = l[1],
325
+ lsz = l[2],
326
+ lex = l[3],
327
+ ley = l[4],
328
+ lez = l[5];
329
+
330
+ // Get line delta
331
+ let dirX = lex - lsx,
332
+ dirY = ley - lsy,
333
+ dirZ = lez - lsz;
334
+
335
+ // Get dot of the plane normal and line delta
336
+ let den = a[0] * dirX + a[1] * dirY + a[2] * dirZ;
337
+
338
+ if (den === 0)
339
+ {
340
+ // Check if distance to the line start is 0
341
+ if ((a[0] * lsx + a[1] * lsy + a[2] * lsz) + a[3] === 0)
342
+ {
343
+ out[0] = lsx;
344
+ out[1] = lsy;
345
+ out[2] = lsz;
346
+ return out;
347
+ }
348
+
349
+ return null;
350
+ }
351
+
352
+ let t = -((lsx * a[0] + lsy * a[1] + lsz * a[2]) + a[3]) / den;
353
+
354
+ if (t < 0 || t > 1)
355
+ {
356
+ return null;
357
+ }
358
+
359
+ out[0] = (dirX * t) + lsx;
360
+ out[1] = (dirY * t) + lsy;
361
+ out[2] = (dirZ * t) + lsz;
362
+ return out;
363
+ };
364
+
365
+
366
+ /**
367
+ * Sets a vec3 with the intersection point of a plane and a 3d line's components
368
+ * Returns null if there was no intersection, or the receiving vec3 if there was
369
+ *
370
+ * @param {vec3} out
371
+ * @param {pln} a
372
+ * @param {vec3} lineStart
373
+ * @param {vec3} lineEnd
374
+ * @returns {vec3}
375
+ */
376
+ pln.getIntersectStartEnd = function(out, a, lineStart, lineEnd)
377
+ {
378
+ const vec6_0 = box3.alloc();
379
+ box3.from(vec6_0, lineStart, lineEnd);
380
+ const result = pln.getIntersectLne3(out, a, vec6_0);
381
+ box3.unalloc(vec6_0);
382
+ return result;
383
+ };
384
+
385
+ /**
386
+ * Sets a vec3 with the normal component of the pln
387
+ *
388
+ * @param {vec3} out - receiving vec3
389
+ * @param {pln} a - source plane
390
+ * @returns {vec3} out - receiving vec3
391
+ */
392
+ pln.getNormal = function(out, a)
393
+ {
394
+ out[0] = a[0];
395
+ out[1] = a[1];
396
+ out[2] = a[2];
397
+ return out;
398
+ };
399
+
400
+ /**
401
+ * Gets an orthographic point
402
+ *
403
+ * @param {vec3} out - receiving vec3
404
+ * @param {pln} a - the plane to project from
405
+ * @param {vec3} p - the point to project
406
+ * @returns {vec3} out - receiving vec3
407
+ */
408
+ pln.getOrthoPoint = function(out, a, p)
409
+ {
410
+ const
411
+ lengthSquared = a[0] * a[0] + a[1] * a[1] + a[2] * a[2],
412
+ scale = lengthSquared === 0 ? 0 :
413
+ ((a[0] * p[0] + a[1] * p[1] + a[2] * p[2]) + a[3]) / lengthSquared;
414
+
415
+ out[0] = a[0] * scale;
416
+ out[1] = a[1] * scale;
417
+ out[2] = a[2] * scale;
418
+ return out;
419
+ };
420
+
421
+ /**
422
+ * Gets a projected point
423
+ *
424
+ * @param {vec3} out - receiving vec3
425
+ * @param {pln} a - the plane to project from
426
+ * @param {vec3} p - the point to project
427
+ * @returns {vec3} out - receiving vec3
428
+ */
429
+ pln.getProjectedPoint = function(out, a, p)
430
+ {
431
+ pln.getOrthoPoint(out, a, p);
432
+ out[0] = -(out[0] - p[0]);
433
+ out[1] = -(out[1] - p[1]);
434
+ out[2] = -(out[2] - p[2]);
435
+ return out;
436
+ };
437
+
438
+ /**
439
+ * Checks if a plane intersects min and max bounds
440
+ *
441
+ * @param {pln} a - plane to compare
442
+ * @param {vec3} min - box min bounds to compare
443
+ * @param {vec3} max - box max bounds to compare
444
+ * @returns {boolean} - true if intersection occurs
445
+ */
446
+ pln.intersectsBounds = function(a, min, max)
447
+ {
448
+ const box3_0 = box3.from(box3.alloc(), min, max);
449
+ let result = box3.intersectsPln(box3_0, a);
450
+ box3.unalloc(box3_0);
451
+ return result;
452
+ };
453
+
454
+ /**
455
+ * Checks if a plane intersects a Float32Array(6) bounding box
456
+ *
457
+ * @param {pln} a - plane to compare
458
+ * @param {(box3|Float32Array)} b - box to compare
459
+ * @returns {boolean} - true if intersection occurs
460
+ */
461
+ pln.intersectsBox3 = function(a, b)
462
+ {
463
+ return box3.intersectsPln(b, a);
464
+ };
465
+
466
+ /**
467
+ * Checks if a plane intersects a Float32Array(6) lne3
468
+ *
469
+ * @author three.js (conversion)
470
+ * @param {pln} a - plane to compare
471
+ * @param {lne3|Float32Array} l - line to compare
472
+ * @returns {boolean} - true if intersection occurs
473
+ */
474
+ pln.intersectsLne3 = function(a, l)
475
+ {
476
+ let startSign = (a[0] * l[0] + a[1] * l[1] + a[2] * l[2]) + a[3];
477
+ let endSign = (a[0] * l[3] + a[1] * l[4] + a[2] * l[5]) + a[3];
478
+ return startSign === 0 || endSign === 0 ||
479
+ (startSign < 0 && endSign > 0) || (endSign < 0 && startSign > 0);
480
+ };
481
+
482
+ /**
483
+ * Checks if a plane intersects spherical bounds
484
+ *
485
+ * @param {pln} a - plane to compare
486
+ * @param {vec3} position - sphere position to compare
487
+ * @param {number} radius - sphere radius compare
488
+ * @returns {boolean} - true if intersection occurs
489
+ */
490
+ pln.intersectsPositionRadius = function(a, position, radius)
491
+ {
492
+ const
493
+ distance = position[0] * a[0] + position[1] * a[1] + position[2] * a[2] + a[3],
494
+ normalLength = Math.hypot(a[0], a[1], a[2]);
495
+ return radius >= 0 && Math.abs(distance) <= radius * (normalLength || 1);
496
+ };
497
+
498
+ /**
499
+ * Checks if a plane intersects a sph3
500
+ *
501
+ * @param {pln} a - plane to compare
502
+ * @param {sph3} s - sphere to compare
503
+ * @returns {boolean} - true if intersection occurs
504
+ */
505
+ pln.intersectsSph3 = function(a, s)
506
+ {
507
+ const
508
+ distance = s[0] * a[0] + s[1] * a[1] + s[2] * a[2] + a[3],
509
+ normalLength = Math.hypot(a[0], a[1], a[2]);
510
+ return s[3] >= 0 && Math.abs(distance) <= s[3] * (normalLength || 1);
511
+ };
512
+
513
+ /**
514
+ * Checks if a plane intersects a lne3's components
515
+ *
516
+ * @param {pln} a - plane to compare
517
+ * @param {vec3} start - line start to compare
518
+ * @param {vec3} end - line end to compare
519
+ * @returns {boolean} - true if intersection occurs
520
+ */
521
+ pln.intersectsStartEnd = function(a, start, end)
522
+ {
523
+ let startSign = (a[0] * start[0] + a[1] * start[1] + a[2] * start[2]) + a[3];
524
+ let endSign = (a[0] * end[0] + a[1] * end[1] + a[2] * end[2]) + a[3];
525
+ return startSign === 0 || endSign === 0 ||
526
+ (startSign < 0 && endSign > 0) || (endSign < 0 && startSign > 0);
527
+ };
528
+
529
+ /**
530
+ * Negates a plane
531
+ *
532
+ * @param {pln} out - receiving plane
533
+ * @param {pln} a - the plane to negate
534
+ * @returns {pln} out - receiving plane
535
+ */
536
+ pln.negate = function(out, a)
537
+ {
538
+ out[0] = -a[0];
539
+ out[1] = -a[1];
540
+ out[2] = -a[2];
541
+ out[3] = a[3] * -1;
542
+ return out;
543
+ };
544
+
545
+ /**
546
+ * Normalizes a plane
547
+ *
548
+ * @param {pln} out - receiving plane
549
+ * @param {pln} a - the plane to normalize
550
+ * @returns {pln} out - receiving plane
551
+ */
552
+ pln.normalize = function(out, a)
553
+ {
554
+ let x = a[0],
555
+ y = a[1],
556
+ z = a[2];
557
+
558
+ let len = x * x + y * y + z * z;
559
+
560
+ if (len > 0)
561
+ {
562
+ len = 1 / Math.sqrt(len);
563
+ out[0] = a[0] * len;
564
+ out[1] = a[1] * len;
565
+ out[2] = a[2] * len;
566
+ out[3] = a[3] * len;
567
+ }
568
+ else
569
+ {
570
+ out[0] = 0;
571
+ out[1] = 0;
572
+ out[2] = 0;
573
+ throw new Error("Normalization error");
574
+ }
575
+
576
+ return out;
577
+ };
578
+
579
+
580
+ /**
581
+ * Sets a plane from values
582
+ *
583
+ * @param {pln} out
584
+ * @param {number} nX
585
+ * @param {number} nY
586
+ * @param {number} nZ
587
+ * @param {number} c
588
+ * @returns {pln} out
589
+ */
590
+ pln.set = vec4.set;
591
+
592
+
593
+ /**
594
+ * Sets a plane from values and then normalizes the results
595
+ * @param {pln} out
596
+ * @param {Number} nx
597
+ * @param {Number} ny
598
+ * @param {Number} nz
599
+ * @param {Number} c
600
+ * @returns {pln} out
601
+ */
602
+ pln.setAndNormalize = function(out, nx, ny, nz, c)
603
+ {
604
+ out[0] = nx;
605
+ out[1] = ny;
606
+ out[2] = nz;
607
+ out[3] = c;
608
+ return pln.normalize(out, out);
609
+ };
610
+
611
+
612
+ /**
613
+ * Sets a pln from an array at an optional offset
614
+ *
615
+ * @param {pln} out
616
+ * @param {Array} arr
617
+ * @param {number} [index=0]
618
+ * @returns {pln} out
619
+ */
620
+ pln.setArray = vec4.setArray;
621
+
622
+ /**
623
+ * Sets an array at an optional offset, with the values of a pln
624
+ *
625
+ * @param {pln} a
626
+ * @param {Array} arr
627
+ * @param {number} [offset = 0]
628
+ * @returns {pln} a
629
+ */
630
+ pln.toArray = function(a, arr, offset = 0)
631
+ {
632
+ arr[offset] = a[0];
633
+ arr[offset + 1] = a[1];
634
+ arr[offset + 2] = a[2];
635
+ arr[offset + 3] = a[3];
636
+ return a;
637
+ };
638
+
639
+ /**
640
+ * Transforms a plane by a mat4
641
+ *
642
+ * @author three.js (conversion)
643
+ * @param {pln} out - the receiving plane
644
+ * @param {pln} a - the plane to transform
645
+ * @param {mat4} m - the affine matrix to transform with
646
+ * @param {mat3} [nMatrix] - optional normal matrix
647
+ * @returns {pln} out - the receiving plane
648
+ */
649
+ pln.transformMat4 = function(out, a, m, nMatrix)
650
+ {
651
+ const sourceLengthSquared = a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
652
+ if (sourceLengthSquared === 0) return pln.copy(out, a);
653
+
654
+ let mat3_0;
655
+ if (!nMatrix)
656
+ {
657
+ mat3_0 = mat3.alloc();
658
+ nMatrix = mat3.normalFromMat4(mat3_0, m);
659
+ }
660
+
661
+ // Coplanar Point
662
+ const inverseLengthSquared = 1 / sourceLengthSquared;
663
+ let cpX = a[0] * -a[3] * inverseLengthSquared,
664
+ cpY = a[1] * -a[3] * inverseLengthSquared,
665
+ cpZ = a[2] * -a[3] * inverseLengthSquared;
666
+
667
+ // Create reference point from Coplanar Point transformed by the affine mat4
668
+ let rX = m[0] * cpX + m[4] * cpY + m[8] * cpZ + m[12],
669
+ rY = m[1] * cpX + m[5] * cpY + m[9] * cpZ + m[13],
670
+ rZ = m[2] * cpX + m[6] * cpY + m[10] * cpZ + m[14];
671
+
672
+ // Transform plane normal by normal matrix
673
+ let nX = a[0],
674
+ nY = a[1],
675
+ nZ = a[2],
676
+ pX = nX * nMatrix[0] + nY * nMatrix[3] + nZ * nMatrix[6],
677
+ pY = nX * nMatrix[1] + nY * nMatrix[4] + nZ * nMatrix[7],
678
+ pZ = nX * nMatrix[2] + nY * nMatrix[5] + nZ * nMatrix[8];
679
+
680
+ if (mat3_0) mat3.unalloc(mat3_0);
681
+
682
+ // Normalize plane normal
683
+ let len = pX * pX + pY * pY + pZ * pZ;
684
+ if (len > 0)
685
+ {
686
+ len = 1 / Math.sqrt(len);
687
+ out[0] = pX * len;
688
+ out[1] = pY * len;
689
+ out[2] = pZ * len;
690
+ }
691
+ else
692
+ {
693
+ out[0] = 0;
694
+ out[1] = 0;
695
+ out[2] = 0;
696
+ throw new Error("Normalization error");
697
+ }
698
+
699
+ // recalculate constant from negative dot of reference point and the resulting plane normal
700
+ out[3] = -(rX * out[0] + rY * out[1] + rZ * out[2]);
701
+
702
+ return out;
703
+ };
704
+
705
+ /**
706
+ * Translates a plane with the given vector
707
+ *
708
+ * @param {pln} out - the receiving plane
709
+ * @param {pln} a - the plane to translate
710
+ * @param {vec3} v - the vector to translate with
711
+ * @returns {pln} out - the receiving plane
712
+ */
713
+ pln.translate = function(out, a, v)
714
+ {
715
+ out[0] = a[0];
716
+ out[1] = a[1];
717
+ out[2] = a[2];
718
+ out[3] = a[3] - (v[0] * a[0] + v[1] * a[1] + v[2] * a[2]);
719
+ return out;
720
+ };
721
+
722
+ export const {
723
+ alloc,
724
+ unalloc,
725
+ $normal,
726
+ clone,
727
+ constant,
728
+ copy,
729
+ create,
730
+ distanceToPoint,
731
+ distanceToPositionRadius,
732
+ distanceToSph3,
733
+ distanceToValues,
734
+ equals,
735
+ equalsNormalConstant,
736
+ exactEquals,
737
+ exactEqualsNormalConstant,
738
+ extract,
739
+ fromNormalConstant,
740
+ fromCoplanarPoints,
741
+ fromNormalAndCoplanarPoint,
742
+ getCoplanarPoint,
743
+ getIntersectLne3,
744
+ getIntersectStartEnd,
745
+ getNormal,
746
+ getOrthoPoint,
747
+ getProjectedPoint,
748
+ intersectsBounds,
749
+ intersectsBox3,
750
+ intersectsLne3,
751
+ intersectsPositionRadius,
752
+ intersectsSph3,
753
+ intersectsStartEnd,
754
+ negate,
755
+ normalize,
756
+ set,
757
+ setAndNormalize,
758
+ setArray,
759
+ toArray,
760
+ transformMat4,
761
+ translate
762
+ } = pln;