@babylonjs/core 7.27.2 → 7.27.3

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 (61) hide show
  1. package/Animations/animatable.core.d.ts +198 -0
  2. package/Animations/animatable.core.js +893 -0
  3. package/Animations/animatable.core.js.map +1 -0
  4. package/Animations/animatable.d.ts +4 -213
  5. package/Animations/animatable.js +5 -886
  6. package/Animations/animatable.js.map +1 -1
  7. package/Animations/animationGroup.d.ts +1 -1
  8. package/Animations/animationGroup.js.map +1 -1
  9. package/Animations/runtimeAnimation.d.ts +1 -0
  10. package/Animations/runtimeAnimation.js +25 -1
  11. package/Animations/runtimeAnimation.js.map +1 -1
  12. package/Audio/audioSceneComponent.d.ts +0 -4
  13. package/Audio/audioSceneComponent.js.map +1 -1
  14. package/Behaviors/Cameras/bouncingBehavior.js.map +1 -1
  15. package/Behaviors/Cameras/framingBehavior.js.map +1 -1
  16. package/Bones/skeleton.d.ts +1 -1
  17. package/Bones/skeleton.js.map +1 -1
  18. package/Culling/ray.core.d.ts +328 -0
  19. package/Culling/ray.core.js +934 -0
  20. package/Culling/ray.core.js.map +1 -0
  21. package/Culling/ray.d.ts +1 -220
  22. package/Culling/ray.js +12 -791
  23. package/Culling/ray.js.map +1 -1
  24. package/Engines/abstractEngine.js +2 -2
  25. package/Engines/abstractEngine.js.map +1 -1
  26. package/FlowGraph/Blocks/Execution/Animation/flowGraphPauseAnimationBlock.d.ts +1 -1
  27. package/FlowGraph/Blocks/Execution/Animation/flowGraphPauseAnimationBlock.js.map +1 -1
  28. package/FlowGraph/Blocks/Execution/Animation/flowGraphPlayAnimationBlock.d.ts +1 -1
  29. package/FlowGraph/Blocks/Execution/Animation/flowGraphPlayAnimationBlock.js.map +1 -1
  30. package/FlowGraph/Blocks/Execution/Animation/flowGraphStopAnimationBlock.d.ts +1 -1
  31. package/FlowGraph/Blocks/Execution/Animation/flowGraphStopAnimationBlock.js.map +1 -1
  32. package/Inputs/scene.inputManager.js +1 -1
  33. package/Inputs/scene.inputManager.js.map +1 -1
  34. package/Layers/effectLayerSceneComponent.d.ts +0 -6
  35. package/Layers/effectLayerSceneComponent.js +0 -1
  36. package/Layers/effectLayerSceneComponent.js.map +1 -1
  37. package/Layers/layerSceneComponent.d.ts +0 -9
  38. package/Layers/layerSceneComponent.js +0 -1
  39. package/Layers/layerSceneComponent.js.map +1 -1
  40. package/LensFlares/lensFlareSystemSceneComponent.d.ts +0 -5
  41. package/LensFlares/lensFlareSystemSceneComponent.js +0 -1
  42. package/LensFlares/lensFlareSystemSceneComponent.js.map +1 -1
  43. package/Lights/light.js.map +1 -1
  44. package/Materials/Textures/Procedurals/proceduralTextureSceneComponent.d.ts +0 -10
  45. package/Materials/Textures/Procedurals/proceduralTextureSceneComponent.js +0 -1
  46. package/Materials/Textures/Procedurals/proceduralTextureSceneComponent.js.map +1 -1
  47. package/Misc/assetsManager.d.ts +1 -1
  48. package/Misc/assetsManager.js.map +1 -1
  49. package/Misc/tools.js +1 -13
  50. package/Misc/tools.js.map +1 -1
  51. package/Sprites/spriteSceneComponent.d.ts +1 -1
  52. package/Sprites/spriteSceneComponent.js +4 -4
  53. package/Sprites/spriteSceneComponent.js.map +1 -1
  54. package/assetContainer.d.ts +1 -1
  55. package/assetContainer.js.map +1 -1
  56. package/node.d.ts +1 -1
  57. package/node.js.map +1 -1
  58. package/package.json +1 -1
  59. package/scene.d.ts +41 -4
  60. package/scene.js +25 -6
  61. package/scene.js.map +1 -1
@@ -0,0 +1,934 @@
1
+ import { Epsilon } from "../Maths/math.constants.js";
2
+ import { Matrix, TmpVectors, Vector3 } from "../Maths/math.vector.js";
3
+ import { BuildArray } from "../Misc/arrayTools.js";
4
+ import { IntersectionInfo } from "../Collisions/intersectionInfo.js";
5
+ import { PickingInfo } from "../Collisions/pickingInfo.js";
6
+ import { EngineStore } from "../Engines/engineStore.js";
7
+ /**
8
+ * Class representing a ray with position and direction
9
+ */
10
+ export class Ray {
11
+ /**
12
+ * Creates a new ray
13
+ * @param origin origin point
14
+ * @param direction direction
15
+ * @param length length of the ray
16
+ * @param epsilon The epsilon value to use when calculating the ray/triangle intersection (default: 0)
17
+ */
18
+ constructor(
19
+ /** origin point */
20
+ origin,
21
+ /** direction */
22
+ direction,
23
+ /** [Number.MAX_VALUE] length of the ray */
24
+ length = Number.MAX_VALUE,
25
+ /** [Epsilon] The epsilon value to use when calculating the ray/triangle intersection (default: Epsilon from math constants) */
26
+ epsilon = Epsilon) {
27
+ this.origin = origin;
28
+ this.direction = direction;
29
+ this.length = length;
30
+ this.epsilon = epsilon;
31
+ }
32
+ // Methods
33
+ /**
34
+ * Clone the current ray
35
+ * @returns a new ray
36
+ */
37
+ clone() {
38
+ return new Ray(this.origin.clone(), this.direction.clone(), this.length);
39
+ }
40
+ /**
41
+ * Checks if the ray intersects a box
42
+ * This does not account for the ray length by design to improve perfs.
43
+ * @param minimum bound of the box
44
+ * @param maximum bound of the box
45
+ * @param intersectionTreshold extra extend to be added to the box in all direction
46
+ * @returns if the box was hit
47
+ */
48
+ intersectsBoxMinMax(minimum, maximum, intersectionTreshold = 0) {
49
+ const newMinimum = Ray._TmpVector3[0].copyFromFloats(minimum.x - intersectionTreshold, minimum.y - intersectionTreshold, minimum.z - intersectionTreshold);
50
+ const newMaximum = Ray._TmpVector3[1].copyFromFloats(maximum.x + intersectionTreshold, maximum.y + intersectionTreshold, maximum.z + intersectionTreshold);
51
+ let d = 0.0;
52
+ let maxValue = Number.MAX_VALUE;
53
+ let inv;
54
+ let min;
55
+ let max;
56
+ let temp;
57
+ if (Math.abs(this.direction.x) < 0.0000001) {
58
+ if (this.origin.x < newMinimum.x || this.origin.x > newMaximum.x) {
59
+ return false;
60
+ }
61
+ }
62
+ else {
63
+ inv = 1.0 / this.direction.x;
64
+ min = (newMinimum.x - this.origin.x) * inv;
65
+ max = (newMaximum.x - this.origin.x) * inv;
66
+ if (max === -Infinity) {
67
+ max = Infinity;
68
+ }
69
+ if (min > max) {
70
+ temp = min;
71
+ min = max;
72
+ max = temp;
73
+ }
74
+ d = Math.max(min, d);
75
+ maxValue = Math.min(max, maxValue);
76
+ if (d > maxValue) {
77
+ return false;
78
+ }
79
+ }
80
+ if (Math.abs(this.direction.y) < 0.0000001) {
81
+ if (this.origin.y < newMinimum.y || this.origin.y > newMaximum.y) {
82
+ return false;
83
+ }
84
+ }
85
+ else {
86
+ inv = 1.0 / this.direction.y;
87
+ min = (newMinimum.y - this.origin.y) * inv;
88
+ max = (newMaximum.y - this.origin.y) * inv;
89
+ if (max === -Infinity) {
90
+ max = Infinity;
91
+ }
92
+ if (min > max) {
93
+ temp = min;
94
+ min = max;
95
+ max = temp;
96
+ }
97
+ d = Math.max(min, d);
98
+ maxValue = Math.min(max, maxValue);
99
+ if (d > maxValue) {
100
+ return false;
101
+ }
102
+ }
103
+ if (Math.abs(this.direction.z) < 0.0000001) {
104
+ if (this.origin.z < newMinimum.z || this.origin.z > newMaximum.z) {
105
+ return false;
106
+ }
107
+ }
108
+ else {
109
+ inv = 1.0 / this.direction.z;
110
+ min = (newMinimum.z - this.origin.z) * inv;
111
+ max = (newMaximum.z - this.origin.z) * inv;
112
+ if (max === -Infinity) {
113
+ max = Infinity;
114
+ }
115
+ if (min > max) {
116
+ temp = min;
117
+ min = max;
118
+ max = temp;
119
+ }
120
+ d = Math.max(min, d);
121
+ maxValue = Math.min(max, maxValue);
122
+ if (d > maxValue) {
123
+ return false;
124
+ }
125
+ }
126
+ return true;
127
+ }
128
+ /**
129
+ * Checks if the ray intersects a box
130
+ * This does not account for the ray lenght by design to improve perfs.
131
+ * @param box the bounding box to check
132
+ * @param intersectionTreshold extra extend to be added to the BoundingBox in all direction
133
+ * @returns if the box was hit
134
+ */
135
+ intersectsBox(box, intersectionTreshold = 0) {
136
+ return this.intersectsBoxMinMax(box.minimum, box.maximum, intersectionTreshold);
137
+ }
138
+ /**
139
+ * If the ray hits a sphere
140
+ * @param sphere the bounding sphere to check
141
+ * @param intersectionTreshold extra extend to be added to the BoundingSphere in all direction
142
+ * @returns true if it hits the sphere
143
+ */
144
+ intersectsSphere(sphere, intersectionTreshold = 0) {
145
+ const x = sphere.center.x - this.origin.x;
146
+ const y = sphere.center.y - this.origin.y;
147
+ const z = sphere.center.z - this.origin.z;
148
+ const pyth = x * x + y * y + z * z;
149
+ const radius = sphere.radius + intersectionTreshold;
150
+ const rr = radius * radius;
151
+ if (pyth <= rr) {
152
+ return true;
153
+ }
154
+ const dot = x * this.direction.x + y * this.direction.y + z * this.direction.z;
155
+ if (dot < 0.0) {
156
+ return false;
157
+ }
158
+ const temp = pyth - dot * dot;
159
+ return temp <= rr;
160
+ }
161
+ /**
162
+ * If the ray hits a triange
163
+ * @param vertex0 triangle vertex
164
+ * @param vertex1 triangle vertex
165
+ * @param vertex2 triangle vertex
166
+ * @returns intersection information if hit
167
+ */
168
+ intersectsTriangle(vertex0, vertex1, vertex2) {
169
+ const edge1 = Ray._TmpVector3[0];
170
+ const edge2 = Ray._TmpVector3[1];
171
+ const pvec = Ray._TmpVector3[2];
172
+ const tvec = Ray._TmpVector3[3];
173
+ const qvec = Ray._TmpVector3[4];
174
+ vertex1.subtractToRef(vertex0, edge1);
175
+ vertex2.subtractToRef(vertex0, edge2);
176
+ Vector3.CrossToRef(this.direction, edge2, pvec);
177
+ const det = Vector3.Dot(edge1, pvec);
178
+ if (det === 0) {
179
+ return null;
180
+ }
181
+ const invdet = 1 / det;
182
+ this.origin.subtractToRef(vertex0, tvec);
183
+ const bv = Vector3.Dot(tvec, pvec) * invdet;
184
+ if (bv < -this.epsilon || bv > 1.0 + this.epsilon) {
185
+ return null;
186
+ }
187
+ Vector3.CrossToRef(tvec, edge1, qvec);
188
+ const bw = Vector3.Dot(this.direction, qvec) * invdet;
189
+ if (bw < -this.epsilon || bv + bw > 1.0 + this.epsilon) {
190
+ return null;
191
+ }
192
+ //check if the distance is longer than the predefined length.
193
+ const distance = Vector3.Dot(edge2, qvec) * invdet;
194
+ if (distance > this.length) {
195
+ return null;
196
+ }
197
+ return new IntersectionInfo(1 - bv - bw, bv, distance);
198
+ }
199
+ /**
200
+ * Checks if ray intersects a plane
201
+ * @param plane the plane to check
202
+ * @returns the distance away it was hit
203
+ */
204
+ intersectsPlane(plane) {
205
+ let distance;
206
+ const result1 = Vector3.Dot(plane.normal, this.direction);
207
+ if (Math.abs(result1) < 9.99999997475243e-7) {
208
+ return null;
209
+ }
210
+ else {
211
+ const result2 = Vector3.Dot(plane.normal, this.origin);
212
+ distance = (-plane.d - result2) / result1;
213
+ if (distance < 0.0) {
214
+ if (distance < -9.99999997475243e-7) {
215
+ return null;
216
+ }
217
+ else {
218
+ return 0;
219
+ }
220
+ }
221
+ return distance;
222
+ }
223
+ }
224
+ /**
225
+ * Calculate the intercept of a ray on a given axis
226
+ * @param axis to check 'x' | 'y' | 'z'
227
+ * @param offset from axis interception (i.e. an offset of 1y is intercepted above ground)
228
+ * @returns a vector containing the coordinates where 'axis' is equal to zero (else offset), or null if there is no intercept.
229
+ */
230
+ intersectsAxis(axis, offset = 0) {
231
+ switch (axis) {
232
+ case "y": {
233
+ const t = (this.origin.y - offset) / this.direction.y;
234
+ if (t > 0) {
235
+ return null;
236
+ }
237
+ return new Vector3(this.origin.x + this.direction.x * -t, offset, this.origin.z + this.direction.z * -t);
238
+ }
239
+ case "x": {
240
+ const t = (this.origin.x - offset) / this.direction.x;
241
+ if (t > 0) {
242
+ return null;
243
+ }
244
+ return new Vector3(offset, this.origin.y + this.direction.y * -t, this.origin.z + this.direction.z * -t);
245
+ }
246
+ case "z": {
247
+ const t = (this.origin.z - offset) / this.direction.z;
248
+ if (t > 0) {
249
+ return null;
250
+ }
251
+ return new Vector3(this.origin.x + this.direction.x * -t, this.origin.y + this.direction.y * -t, offset);
252
+ }
253
+ default:
254
+ return null;
255
+ }
256
+ }
257
+ /**
258
+ * Checks if ray intersects a mesh. The ray is defined in WORLD space. A mesh triangle can be picked both from its front and back sides,
259
+ * irrespective of orientation.
260
+ * @param mesh the mesh to check
261
+ * @param fastCheck defines if the first intersection will be used (and not the closest)
262
+ * @param trianglePredicate defines an optional predicate used to select faces when a mesh intersection is detected
263
+ * @param onlyBoundingInfo defines a boolean indicating if picking should only happen using bounding info (false by default)
264
+ * @param worldToUse defines the world matrix to use to get the world coordinate of the intersection point
265
+ * @param skipBoundingInfo a boolean indicating if we should skip the bounding info check
266
+ * @returns picking info of the intersection
267
+ */
268
+ intersectsMesh(mesh, fastCheck, trianglePredicate, onlyBoundingInfo = false, worldToUse, skipBoundingInfo = false) {
269
+ const tm = TmpVectors.Matrix[0];
270
+ mesh.getWorldMatrix().invertToRef(tm);
271
+ if (this._tmpRay) {
272
+ Ray.TransformToRef(this, tm, this._tmpRay);
273
+ }
274
+ else {
275
+ this._tmpRay = Ray.Transform(this, tm);
276
+ }
277
+ return mesh.intersects(this._tmpRay, fastCheck, trianglePredicate, onlyBoundingInfo, worldToUse, skipBoundingInfo);
278
+ }
279
+ /**
280
+ * Checks if ray intersects a mesh
281
+ * @param meshes the meshes to check
282
+ * @param fastCheck defines if the first intersection will be used (and not the closest)
283
+ * @param results array to store result in
284
+ * @returns Array of picking infos
285
+ */
286
+ intersectsMeshes(meshes, fastCheck, results) {
287
+ if (results) {
288
+ results.length = 0;
289
+ }
290
+ else {
291
+ results = [];
292
+ }
293
+ for (let i = 0; i < meshes.length; i++) {
294
+ const pickInfo = this.intersectsMesh(meshes[i], fastCheck);
295
+ if (pickInfo.hit) {
296
+ results.push(pickInfo);
297
+ }
298
+ }
299
+ results.sort(this._comparePickingInfo);
300
+ return results;
301
+ }
302
+ _comparePickingInfo(pickingInfoA, pickingInfoB) {
303
+ if (pickingInfoA.distance < pickingInfoB.distance) {
304
+ return -1;
305
+ }
306
+ else if (pickingInfoA.distance > pickingInfoB.distance) {
307
+ return 1;
308
+ }
309
+ else {
310
+ return 0;
311
+ }
312
+ }
313
+ /**
314
+ * Intersection test between the ray and a given segment within a given tolerance (threshold)
315
+ * @param sega the first point of the segment to test the intersection against
316
+ * @param segb the second point of the segment to test the intersection against
317
+ * @param threshold the tolerance margin, if the ray doesn't intersect the segment but is close to the given threshold, the intersection is successful
318
+ * @returns the distance from the ray origin to the intersection point if there's intersection, or -1 if there's no intersection
319
+ */
320
+ intersectionSegment(sega, segb, threshold) {
321
+ const o = this.origin;
322
+ const u = TmpVectors.Vector3[0];
323
+ const rsegb = TmpVectors.Vector3[1];
324
+ const v = TmpVectors.Vector3[2];
325
+ const w = TmpVectors.Vector3[3];
326
+ segb.subtractToRef(sega, u);
327
+ this.direction.scaleToRef(Ray._Rayl, v);
328
+ o.addToRef(v, rsegb);
329
+ sega.subtractToRef(o, w);
330
+ const a = Vector3.Dot(u, u); // always >= 0
331
+ const b = Vector3.Dot(u, v);
332
+ const c = Vector3.Dot(v, v); // always >= 0
333
+ const d = Vector3.Dot(u, w);
334
+ const e = Vector3.Dot(v, w);
335
+ const D = a * c - b * b; // always >= 0
336
+ let sN, sD = D; // sc = sN / sD, default sD = D >= 0
337
+ let tN, tD = D; // tc = tN / tD, default tD = D >= 0
338
+ // compute the line parameters of the two closest points
339
+ if (D < Ray._Smallnum) {
340
+ // the lines are almost parallel
341
+ sN = 0.0; // force using point P0 on segment S1
342
+ sD = 1.0; // to prevent possible division by 0.0 later
343
+ tN = e;
344
+ tD = c;
345
+ }
346
+ else {
347
+ // get the closest points on the infinite lines
348
+ sN = b * e - c * d;
349
+ tN = a * e - b * d;
350
+ if (sN < 0.0) {
351
+ // sc < 0 => the s=0 edge is visible
352
+ sN = 0.0;
353
+ tN = e;
354
+ tD = c;
355
+ }
356
+ else if (sN > sD) {
357
+ // sc > 1 => the s=1 edge is visible
358
+ sN = sD;
359
+ tN = e + b;
360
+ tD = c;
361
+ }
362
+ }
363
+ if (tN < 0.0) {
364
+ // tc < 0 => the t=0 edge is visible
365
+ tN = 0.0;
366
+ // recompute sc for this edge
367
+ if (-d < 0.0) {
368
+ sN = 0.0;
369
+ }
370
+ else if (-d > a) {
371
+ sN = sD;
372
+ }
373
+ else {
374
+ sN = -d;
375
+ sD = a;
376
+ }
377
+ }
378
+ else if (tN > tD) {
379
+ // tc > 1 => the t=1 edge is visible
380
+ tN = tD;
381
+ // recompute sc for this edge
382
+ if (-d + b < 0.0) {
383
+ sN = 0;
384
+ }
385
+ else if (-d + b > a) {
386
+ sN = sD;
387
+ }
388
+ else {
389
+ sN = -d + b;
390
+ sD = a;
391
+ }
392
+ }
393
+ // finally do the division to get sc and tc
394
+ const sc = Math.abs(sN) < Ray._Smallnum ? 0.0 : sN / sD;
395
+ const tc = Math.abs(tN) < Ray._Smallnum ? 0.0 : tN / tD;
396
+ // get the difference of the two closest points
397
+ const qtc = TmpVectors.Vector3[4];
398
+ v.scaleToRef(tc, qtc);
399
+ const qsc = TmpVectors.Vector3[5];
400
+ u.scaleToRef(sc, qsc);
401
+ qsc.addInPlace(w);
402
+ const dP = TmpVectors.Vector3[6];
403
+ qsc.subtractToRef(qtc, dP); // = S1(sc) - S2(tc)
404
+ const isIntersected = tc > 0 && tc <= this.length && dP.lengthSquared() < threshold * threshold; // return intersection result
405
+ if (isIntersected) {
406
+ return qsc.length();
407
+ }
408
+ return -1;
409
+ }
410
+ /**
411
+ * Update the ray from viewport position
412
+ * @param x position
413
+ * @param y y position
414
+ * @param viewportWidth viewport width
415
+ * @param viewportHeight viewport height
416
+ * @param world world matrix
417
+ * @param view view matrix
418
+ * @param projection projection matrix
419
+ * @param enableDistantPicking defines if picking should handle large values for mesh position/scaling (false by default)
420
+ * @returns this ray updated
421
+ */
422
+ update(x, y, viewportWidth, viewportHeight, world, view, projection, enableDistantPicking = false) {
423
+ if (enableDistantPicking) {
424
+ // With world matrices having great values (like 8000000000 on 1 or more scaling or position axis),
425
+ // multiplying view/projection/world and doing invert will result in loss of float precision in the matrix.
426
+ // One way to fix it is to compute the ray with world at identity then transform the ray in object space.
427
+ // This is slower (2 matrix inverts instead of 1) but precision is preserved.
428
+ // This is hidden behind `EnableDistantPicking` flag (default is false)
429
+ if (!Ray._RayDistant) {
430
+ Ray._RayDistant = Ray.Zero();
431
+ }
432
+ Ray._RayDistant.unprojectRayToRef(x, y, viewportWidth, viewportHeight, Matrix.IdentityReadOnly, view, projection);
433
+ const tm = TmpVectors.Matrix[0];
434
+ world.invertToRef(tm);
435
+ Ray.TransformToRef(Ray._RayDistant, tm, this);
436
+ }
437
+ else {
438
+ this.unprojectRayToRef(x, y, viewportWidth, viewportHeight, world, view, projection);
439
+ }
440
+ return this;
441
+ }
442
+ // Statics
443
+ /**
444
+ * Creates a ray with origin and direction of 0,0,0
445
+ * @returns the new ray
446
+ */
447
+ static Zero() {
448
+ return new Ray(Vector3.Zero(), Vector3.Zero());
449
+ }
450
+ /**
451
+ * Creates a new ray from screen space and viewport
452
+ * @param x position
453
+ * @param y y position
454
+ * @param viewportWidth viewport width
455
+ * @param viewportHeight viewport height
456
+ * @param world world matrix
457
+ * @param view view matrix
458
+ * @param projection projection matrix
459
+ * @returns new ray
460
+ */
461
+ static CreateNew(x, y, viewportWidth, viewportHeight, world, view, projection) {
462
+ const result = Ray.Zero();
463
+ return result.update(x, y, viewportWidth, viewportHeight, world, view, projection);
464
+ }
465
+ /**
466
+ * Function will create a new transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be
467
+ * transformed to the given world matrix.
468
+ * @param origin The origin point
469
+ * @param end The end point
470
+ * @param world a matrix to transform the ray to. Default is the identity matrix.
471
+ * @returns the new ray
472
+ */
473
+ static CreateNewFromTo(origin, end, world = Matrix.IdentityReadOnly) {
474
+ const result = new Ray(new Vector3(0, 0, 0), new Vector3(0, 0, 0));
475
+ return Ray.CreateFromToToRef(origin, end, result, world);
476
+ }
477
+ /**
478
+ * Function will update a transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be
479
+ * transformed to the given world matrix.
480
+ * @param origin The origin point
481
+ * @param end The end point
482
+ * @param result the object to store the result
483
+ * @param world a matrix to transform the ray to. Default is the identity matrix.
484
+ * @returns the ref ray
485
+ */
486
+ static CreateFromToToRef(origin, end, result, world = Matrix.IdentityReadOnly) {
487
+ result.origin.copyFrom(origin);
488
+ const direction = end.subtractToRef(origin, result.direction);
489
+ const length = Math.sqrt(direction.x * direction.x + direction.y * direction.y + direction.z * direction.z);
490
+ result.length = length;
491
+ result.direction.normalize();
492
+ return Ray.TransformToRef(result, world, result);
493
+ }
494
+ /**
495
+ * Transforms a ray by a matrix
496
+ * @param ray ray to transform
497
+ * @param matrix matrix to apply
498
+ * @returns the resulting new ray
499
+ */
500
+ static Transform(ray, matrix) {
501
+ const result = new Ray(new Vector3(0, 0, 0), new Vector3(0, 0, 0));
502
+ Ray.TransformToRef(ray, matrix, result);
503
+ return result;
504
+ }
505
+ /**
506
+ * Transforms a ray by a matrix
507
+ * @param ray ray to transform
508
+ * @param matrix matrix to apply
509
+ * @param result ray to store result in
510
+ * @returns the updated result ray
511
+ */
512
+ static TransformToRef(ray, matrix, result) {
513
+ Vector3.TransformCoordinatesToRef(ray.origin, matrix, result.origin);
514
+ Vector3.TransformNormalToRef(ray.direction, matrix, result.direction);
515
+ result.length = ray.length;
516
+ result.epsilon = ray.epsilon;
517
+ const dir = result.direction;
518
+ const len = dir.length();
519
+ if (!(len === 0 || len === 1)) {
520
+ const num = 1.0 / len;
521
+ dir.x *= num;
522
+ dir.y *= num;
523
+ dir.z *= num;
524
+ result.length *= len;
525
+ }
526
+ return result;
527
+ }
528
+ /**
529
+ * Unproject a ray from screen space to object space
530
+ * @param sourceX defines the screen space x coordinate to use
531
+ * @param sourceY defines the screen space y coordinate to use
532
+ * @param viewportWidth defines the current width of the viewport
533
+ * @param viewportHeight defines the current height of the viewport
534
+ * @param world defines the world matrix to use (can be set to Identity to go to world space)
535
+ * @param view defines the view matrix to use
536
+ * @param projection defines the projection matrix to use
537
+ */
538
+ unprojectRayToRef(sourceX, sourceY, viewportWidth, viewportHeight, world, view, projection) {
539
+ const matrix = TmpVectors.Matrix[0];
540
+ world.multiplyToRef(view, matrix);
541
+ matrix.multiplyToRef(projection, matrix);
542
+ matrix.invert();
543
+ const engine = EngineStore.LastCreatedEngine;
544
+ const nearScreenSource = TmpVectors.Vector3[0];
545
+ nearScreenSource.x = (sourceX / viewportWidth) * 2 - 1;
546
+ nearScreenSource.y = -((sourceY / viewportHeight) * 2 - 1);
547
+ nearScreenSource.z = engine?.useReverseDepthBuffer ? 1 : engine?.isNDCHalfZRange ? 0 : -1;
548
+ // far Z need to be close but < to 1 or camera projection matrix with maxZ = 0 will NaN
549
+ const farScreenSource = TmpVectors.Vector3[1].copyFromFloats(nearScreenSource.x, nearScreenSource.y, 1.0 - 1e-8);
550
+ const nearVec3 = TmpVectors.Vector3[2];
551
+ const farVec3 = TmpVectors.Vector3[3];
552
+ Vector3._UnprojectFromInvertedMatrixToRef(nearScreenSource, matrix, nearVec3);
553
+ Vector3._UnprojectFromInvertedMatrixToRef(farScreenSource, matrix, farVec3);
554
+ this.origin.copyFrom(nearVec3);
555
+ farVec3.subtractToRef(nearVec3, this.direction);
556
+ this.direction.normalize();
557
+ }
558
+ }
559
+ Ray._TmpVector3 = BuildArray(6, Vector3.Zero);
560
+ Ray._RayDistant = Ray.Zero();
561
+ Ray._Smallnum = 0.00000001;
562
+ Ray._Rayl = 10e8;
563
+ /**
564
+ * Creates a ray that can be used to pick in the scene
565
+ * @param scene defines the scene to use for the picking
566
+ * @param x defines the x coordinate of the origin (on-screen)
567
+ * @param y defines the y coordinate of the origin (on-screen)
568
+ * @param world defines the world matrix to use if you want to pick in object space (instead of world space)
569
+ * @param camera defines the camera to use for the picking
570
+ * @param cameraViewSpace defines if picking will be done in view space (false by default)
571
+ * @returns a Ray
572
+ */
573
+ export function CreatePickingRay(scene, x, y, world, camera, cameraViewSpace = false) {
574
+ const result = Ray.Zero();
575
+ CreatePickingRayToRef(scene, x, y, world, result, camera, cameraViewSpace);
576
+ return result;
577
+ }
578
+ /**
579
+ * Creates a ray that can be used to pick in the scene
580
+ * @param scene defines the scene to use for the picking
581
+ * @param x defines the x coordinate of the origin (on-screen)
582
+ * @param y defines the y coordinate of the origin (on-screen)
583
+ * @param world defines the world matrix to use if you want to pick in object space (instead of world space)
584
+ * @param result defines the ray where to store the picking ray
585
+ * @param camera defines the camera to use for the picking
586
+ * @param cameraViewSpace defines if picking will be done in view space (false by default)
587
+ * @param enableDistantPicking defines if picking should handle large values for mesh position/scaling (false by default)
588
+ * @returns the current scene
589
+ */
590
+ export function CreatePickingRayToRef(scene, x, y, world, result, camera, cameraViewSpace = false, enableDistantPicking = false) {
591
+ const engine = scene.getEngine();
592
+ if (!camera && !(camera = scene.activeCamera)) {
593
+ return scene;
594
+ }
595
+ const cameraViewport = camera.viewport;
596
+ const renderHeight = engine.getRenderHeight();
597
+ const { x: vx, y: vy, width, height } = cameraViewport.toGlobal(engine.getRenderWidth(), renderHeight);
598
+ // Moving coordinates to local viewport world
599
+ const levelInv = 1 / engine.getHardwareScalingLevel();
600
+ x = x * levelInv - vx;
601
+ y = y * levelInv - (renderHeight - vy - height);
602
+ result.update(x, y, width, height, world ? world : Matrix.IdentityReadOnly, cameraViewSpace ? Matrix.IdentityReadOnly : camera.getViewMatrix(), camera.getProjectionMatrix(), enableDistantPicking);
603
+ return scene;
604
+ }
605
+ /**
606
+ * Creates a ray that can be used to pick in the scene
607
+ * @param scene defines the scene to use for the picking
608
+ * @param x defines the x coordinate of the origin (on-screen)
609
+ * @param y defines the y coordinate of the origin (on-screen)
610
+ * @param camera defines the camera to use for the picking
611
+ * @returns a Ray
612
+ */
613
+ export function CreatePickingRayInCameraSpace(scene, x, y, camera) {
614
+ const result = Ray.Zero();
615
+ CreatePickingRayInCameraSpaceToRef(scene, x, y, result, camera);
616
+ return result;
617
+ }
618
+ /**
619
+ * Creates a ray that can be used to pick in the scene
620
+ * @param scene defines the scene to use for the picking
621
+ * @param x defines the x coordinate of the origin (on-screen)
622
+ * @param y defines the y coordinate of the origin (on-screen)
623
+ * @param result defines the ray where to store the picking ray
624
+ * @param camera defines the camera to use for the picking
625
+ * @returns the current scene
626
+ */
627
+ export function CreatePickingRayInCameraSpaceToRef(scene, x, y, result, camera) {
628
+ if (!PickingInfo) {
629
+ return scene;
630
+ }
631
+ const engine = scene.getEngine();
632
+ if (!camera && !(camera = scene.activeCamera)) {
633
+ throw new Error("Active camera not set");
634
+ }
635
+ const cameraViewport = camera.viewport;
636
+ const renderHeight = engine.getRenderHeight();
637
+ const { x: vx, y: vy, width, height } = cameraViewport.toGlobal(engine.getRenderWidth(), renderHeight);
638
+ const identity = Matrix.Identity();
639
+ // Moving coordinates to local viewport world
640
+ const levelInv = 1 / engine.getHardwareScalingLevel();
641
+ x = x * levelInv - vx;
642
+ y = y * levelInv - (renderHeight - vy - height);
643
+ result.update(x, y, width, height, identity, identity, camera.getProjectionMatrix());
644
+ return scene;
645
+ }
646
+ function InternalPickForMesh(pickingInfo, rayFunction, mesh, world, fastCheck, onlyBoundingInfo, trianglePredicate, skipBoundingInfo) {
647
+ const ray = rayFunction(world, mesh.enableDistantPicking);
648
+ const result = mesh.intersects(ray, fastCheck, trianglePredicate, onlyBoundingInfo, world, skipBoundingInfo);
649
+ if (!result || !result.hit) {
650
+ return null;
651
+ }
652
+ if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance) {
653
+ return null;
654
+ }
655
+ return result;
656
+ }
657
+ function InternalPick(scene, rayFunction, predicate, fastCheck, onlyBoundingInfo, trianglePredicate) {
658
+ let pickingInfo = null;
659
+ const computeWorldMatrixForCamera = !!(scene.activeCameras && scene.activeCameras.length > 1 && scene.cameraToUseForPointers !== scene.activeCamera);
660
+ const currentCamera = scene.cameraToUseForPointers || scene.activeCamera;
661
+ for (let meshIndex = 0; meshIndex < scene.meshes.length; meshIndex++) {
662
+ const mesh = scene.meshes[meshIndex];
663
+ if (predicate) {
664
+ if (!predicate(mesh, -1)) {
665
+ continue;
666
+ }
667
+ }
668
+ else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
669
+ continue;
670
+ }
671
+ const forceCompute = computeWorldMatrixForCamera && mesh.isWorldMatrixCameraDependent();
672
+ const world = mesh.computeWorldMatrix(forceCompute, currentCamera);
673
+ if (mesh.hasThinInstances && mesh.thinInstanceEnablePicking) {
674
+ // first check if the ray intersects the whole bounding box/sphere of the mesh
675
+ const result = InternalPickForMesh(pickingInfo, rayFunction, mesh, world, true, true, trianglePredicate);
676
+ if (result) {
677
+ if (onlyBoundingInfo) {
678
+ // the user only asked for a bounding info check so we can return
679
+ return result;
680
+ }
681
+ const tmpMatrix = TmpVectors.Matrix[1];
682
+ const thinMatrices = mesh.thinInstanceGetWorldMatrices();
683
+ for (let index = 0; index < thinMatrices.length; index++) {
684
+ if (predicate && !predicate(mesh, index)) {
685
+ continue;
686
+ }
687
+ const thinMatrix = thinMatrices[index];
688
+ thinMatrix.multiplyToRef(world, tmpMatrix);
689
+ const result = InternalPickForMesh(pickingInfo, rayFunction, mesh, tmpMatrix, fastCheck, onlyBoundingInfo, trianglePredicate, true);
690
+ if (result) {
691
+ pickingInfo = result;
692
+ pickingInfo.thinInstanceIndex = index;
693
+ if (fastCheck) {
694
+ return pickingInfo;
695
+ }
696
+ }
697
+ }
698
+ }
699
+ }
700
+ else {
701
+ const result = InternalPickForMesh(pickingInfo, rayFunction, mesh, world, fastCheck, onlyBoundingInfo, trianglePredicate);
702
+ if (result) {
703
+ pickingInfo = result;
704
+ if (fastCheck) {
705
+ return pickingInfo;
706
+ }
707
+ }
708
+ }
709
+ }
710
+ return pickingInfo || new PickingInfo();
711
+ }
712
+ function InternalMultiPick(scene, rayFunction, predicate, trianglePredicate) {
713
+ if (!PickingInfo) {
714
+ return null;
715
+ }
716
+ const pickingInfos = [];
717
+ const computeWorldMatrixForCamera = !!(scene.activeCameras && scene.activeCameras.length > 1 && scene.cameraToUseForPointers !== scene.activeCamera);
718
+ const currentCamera = scene.cameraToUseForPointers || scene.activeCamera;
719
+ for (let meshIndex = 0; meshIndex < scene.meshes.length; meshIndex++) {
720
+ const mesh = scene.meshes[meshIndex];
721
+ if (predicate) {
722
+ if (!predicate(mesh, -1)) {
723
+ continue;
724
+ }
725
+ }
726
+ else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
727
+ continue;
728
+ }
729
+ const forceCompute = computeWorldMatrixForCamera && mesh.isWorldMatrixCameraDependent();
730
+ const world = mesh.computeWorldMatrix(forceCompute, currentCamera);
731
+ if (mesh.hasThinInstances && mesh.thinInstanceEnablePicking) {
732
+ const result = InternalPickForMesh(null, rayFunction, mesh, world, true, true, trianglePredicate);
733
+ if (result) {
734
+ const tmpMatrix = TmpVectors.Matrix[1];
735
+ const thinMatrices = mesh.thinInstanceGetWorldMatrices();
736
+ for (let index = 0; index < thinMatrices.length; index++) {
737
+ if (predicate && !predicate(mesh, index)) {
738
+ continue;
739
+ }
740
+ const thinMatrix = thinMatrices[index];
741
+ thinMatrix.multiplyToRef(world, tmpMatrix);
742
+ const result = InternalPickForMesh(null, rayFunction, mesh, tmpMatrix, false, false, trianglePredicate, true);
743
+ if (result) {
744
+ result.thinInstanceIndex = index;
745
+ pickingInfos.push(result);
746
+ }
747
+ }
748
+ }
749
+ }
750
+ else {
751
+ const result = InternalPickForMesh(null, rayFunction, mesh, world, false, false, trianglePredicate);
752
+ if (result) {
753
+ pickingInfos.push(result);
754
+ }
755
+ }
756
+ }
757
+ return pickingInfos;
758
+ }
759
+ /** Launch a ray to try to pick a mesh in the scene using only bounding information of the main mesh (not using submeshes)
760
+ * @param scene defines the scene to use for the picking
761
+ * @param x position on screen
762
+ * @param y position on screen
763
+ * @param predicate Predicate function used to determine eligible meshes. Can be set to null. In this case, a mesh must be enabled, visible and with isPickable set to true. thinInstanceIndex is -1 when the mesh is non-instanced
764
+ * @param fastCheck defines if the first intersection will be used (and not the closest)
765
+ * @param camera to use for computing the picking ray. Can be set to null. In this case, the scene.activeCamera will be used
766
+ * @returns a PickingInfo (Please note that some info will not be set like distance, bv, bu and everything that cannot be capture by only using bounding infos)
767
+ */
768
+ export function PickWithBoundingInfo(scene, x, y, predicate, fastCheck, camera) {
769
+ if (!PickingInfo) {
770
+ return null;
771
+ }
772
+ const result = InternalPick(scene, (world) => {
773
+ if (!scene._tempPickingRay) {
774
+ scene._tempPickingRay = Ray.Zero();
775
+ }
776
+ CreatePickingRayToRef(scene, x, y, world, scene._tempPickingRay, camera || null);
777
+ return scene._tempPickingRay;
778
+ }, predicate, fastCheck, true);
779
+ if (result) {
780
+ result.ray = CreatePickingRay(scene, x, y, Matrix.Identity(), camera || null);
781
+ }
782
+ return result;
783
+ }
784
+ // Object.defineProperty(Scene.prototype, "_pickingAvailable", {
785
+ // get: () => true,
786
+ // enumerable: false,
787
+ // configurable: false,
788
+ // });
789
+ /** Launch a ray to try to pick a mesh in the scene
790
+ * @param scene defines the scene to use for the picking
791
+ * @param x position on screen
792
+ * @param y position on screen
793
+ * @param predicate Predicate function used to determine eligible meshes. Can be set to null. In this case, a mesh must be enabled, visible and with isPickable set to true. thinInstanceIndex is -1 when the mesh is non-instanced
794
+ * @param fastCheck defines if the first intersection will be used (and not the closest)
795
+ * @param camera to use for computing the picking ray. Can be set to null. In this case, the scene.activeCamera will be used
796
+ * @param trianglePredicate defines an optional predicate used to select faces when a mesh intersection is detected
797
+ * @param _enableDistantPicking defines if picking should handle large values for mesh position/scaling (false by default)
798
+ * @returns a PickingInfo
799
+ */
800
+ export function Pick(scene, x, y, predicate, fastCheck, camera, trianglePredicate, _enableDistantPicking = false) {
801
+ const result = InternalPick(scene, (world, enableDistantPicking) => {
802
+ if (!scene._tempPickingRay) {
803
+ scene._tempPickingRay = Ray.Zero();
804
+ }
805
+ CreatePickingRayToRef(scene, x, y, world, scene._tempPickingRay, camera || null, false, enableDistantPicking);
806
+ return scene._tempPickingRay;
807
+ }, predicate, fastCheck, false, trianglePredicate);
808
+ if (result) {
809
+ result.ray = CreatePickingRay(scene, x, y, Matrix.Identity(), camera || null);
810
+ }
811
+ return result;
812
+ }
813
+ /**
814
+ * Use the given ray to pick a mesh in the scene. A mesh triangle can be picked both from its front and back sides,
815
+ * irrespective of orientation.
816
+ * @param scene defines the scene to use for the picking
817
+ * @param ray The ray to use to pick meshes
818
+ * @param predicate Predicate function used to determine eligible meshes. Can be set to null. In this case, a mesh must have isPickable set to true. thinInstanceIndex is -1 when the mesh is non-instanced
819
+ * @param fastCheck defines if the first intersection will be used (and not the closest)
820
+ * @param trianglePredicate defines an optional predicate used to select faces when a mesh intersection is detected
821
+ * @returns a PickingInfo
822
+ */
823
+ export function PickWithRay(scene, ray, predicate, fastCheck, trianglePredicate) {
824
+ const result = InternalPick(scene, (world) => {
825
+ if (!scene._pickWithRayInverseMatrix) {
826
+ scene._pickWithRayInverseMatrix = Matrix.Identity();
827
+ }
828
+ world.invertToRef(scene._pickWithRayInverseMatrix);
829
+ if (!scene._cachedRayForTransform) {
830
+ scene._cachedRayForTransform = Ray.Zero();
831
+ }
832
+ Ray.TransformToRef(ray, scene._pickWithRayInverseMatrix, scene._cachedRayForTransform);
833
+ return scene._cachedRayForTransform;
834
+ }, predicate, fastCheck, false, trianglePredicate);
835
+ if (result) {
836
+ result.ray = ray;
837
+ }
838
+ return result;
839
+ }
840
+ /**
841
+ * Launch a ray to try to pick a mesh in the scene. A mesh triangle can be picked both from its front and back sides,
842
+ * irrespective of orientation.
843
+ * @param scene defines the scene to use for the picking
844
+ * @param x X position on screen
845
+ * @param y Y position on screen
846
+ * @param predicate Predicate function used to determine eligible meshes and instances. Can be set to null. In this case, a mesh must be enabled, visible and with isPickable set to true. thinInstanceIndex is -1 when the mesh is non-instanced
847
+ * @param camera camera to use for computing the picking ray. Can be set to null. In this case, the scene.activeCamera will be used
848
+ * @param trianglePredicate defines an optional predicate used to select faces when a mesh intersection is detected
849
+ * @returns an array of PickingInfo
850
+ */
851
+ export function MultiPick(scene, x, y, predicate, camera, trianglePredicate) {
852
+ return InternalMultiPick(scene, (world) => CreatePickingRay(scene, x, y, world, camera || null), predicate, trianglePredicate);
853
+ }
854
+ /**
855
+ * Launch a ray to try to pick a mesh in the scene
856
+ * @param scene defines the scene to use for the picking
857
+ * @param ray Ray to use
858
+ * @param predicate Predicate function used to determine eligible meshes and instances. Can be set to null. In this case, a mesh must be enabled, visible and with isPickable set to true. thinInstanceIndex is -1 when the mesh is non-instanced
859
+ * @param trianglePredicate defines an optional predicate used to select faces when a mesh intersection is detected
860
+ * @returns an array of PickingInfo
861
+ */
862
+ export function MultiPickWithRay(scene, ray, predicate, trianglePredicate) {
863
+ return InternalMultiPick(scene, (world) => {
864
+ if (!scene._pickWithRayInverseMatrix) {
865
+ scene._pickWithRayInverseMatrix = Matrix.Identity();
866
+ }
867
+ world.invertToRef(scene._pickWithRayInverseMatrix);
868
+ if (!scene._cachedRayForTransform) {
869
+ scene._cachedRayForTransform = Ray.Zero();
870
+ }
871
+ Ray.TransformToRef(ray, scene._pickWithRayInverseMatrix, scene._cachedRayForTransform);
872
+ return scene._cachedRayForTransform;
873
+ }, predicate, trianglePredicate);
874
+ }
875
+ /**
876
+ * Gets a ray in the forward direction from the camera.
877
+ * @param camera Defines the camera to use to get the ray from
878
+ * @param length Defines the length of the ray to create
879
+ * @param transform Defines the transform to apply to the ray, by default the world matrix is used to create a workd space ray
880
+ * @param origin Defines the start point of the ray which defaults to the camera position
881
+ * @returns the forward ray
882
+ */
883
+ export function GetForwardRay(camera, length = 100, transform, origin) {
884
+ return GetForwardRayToRef(camera, new Ray(Vector3.Zero(), Vector3.Zero(), length), length, transform, origin);
885
+ }
886
+ /**
887
+ * Gets a ray in the forward direction from the camera.
888
+ * @param camera Defines the camera to use to get the ray from
889
+ * @param refRay the ray to (re)use when setting the values
890
+ * @param length Defines the length of the ray to create
891
+ * @param transform Defines the transform to apply to the ray, by default the world matrx is used to create a workd space ray
892
+ * @param origin Defines the start point of the ray which defaults to the camera position
893
+ * @returns the forward ray
894
+ */
895
+ export function GetForwardRayToRef(camera, refRay, length = 100, transform, origin) {
896
+ if (!transform) {
897
+ transform = camera.getWorldMatrix();
898
+ }
899
+ refRay.length = length;
900
+ if (origin) {
901
+ refRay.origin.copyFrom(origin);
902
+ }
903
+ else {
904
+ refRay.origin.copyFrom(camera.position);
905
+ }
906
+ const forward = TmpVectors.Vector3[2];
907
+ forward.set(0, 0, camera._scene.useRightHandedSystem ? -1 : 1);
908
+ const worldForward = TmpVectors.Vector3[3];
909
+ Vector3.TransformNormalToRef(forward, transform, worldForward);
910
+ Vector3.NormalizeToRef(worldForward, refRay.direction);
911
+ return refRay;
912
+ }
913
+ /**
914
+ * Initialize the minimal interdependecies between the Ray and Scene and Camera
915
+ * @param sceneClass defines the scene prototype to use
916
+ * @param cameraClass defines the camera prototype to use
917
+ */
918
+ export function AddRayExtensions(sceneClass, cameraClass) {
919
+ if (cameraClass) {
920
+ cameraClass.prototype.getForwardRay = function (length = 100, transform, origin) {
921
+ return GetForwardRayToRef(this, new Ray(Vector3.Zero(), Vector3.Zero(), length), length, transform, origin);
922
+ };
923
+ cameraClass.prototype.getForwardRayToRef = function (refRay, length = 100, transform, origin) {
924
+ return GetForwardRayToRef(this, refRay, length, transform, origin);
925
+ };
926
+ }
927
+ if (!sceneClass) {
928
+ return;
929
+ }
930
+ sceneClass.prototype.createPickingRay = function (x, y, world, camera, cameraViewSpace = false) {
931
+ return CreatePickingRay(this, x, y, world, camera, cameraViewSpace);
932
+ };
933
+ }
934
+ //# sourceMappingURL=ray.core.js.map