@plastic-software/three 0.178.0 → 0.179.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 (141) hide show
  1. package/README.md +1 -1
  2. package/build/three.cjs +856 -196
  3. package/build/three.core.js +647 -123
  4. package/build/three.core.min.js +1 -1
  5. package/build/three.module.js +211 -76
  6. package/build/three.module.min.js +1 -1
  7. package/build/three.tsl.js +70 -21
  8. package/build/three.tsl.min.js +1 -1
  9. package/build/three.webgpu.js +1796 -557
  10. package/build/three.webgpu.min.js +1 -1
  11. package/build/three.webgpu.nodes.js +1754 -557
  12. package/build/three.webgpu.nodes.min.js +1 -1
  13. package/examples/jsm/Addons.js +1 -2
  14. package/examples/jsm/capabilities/WebGPU.js +1 -1
  15. package/examples/jsm/csm/CSMShadowNode.js +4 -4
  16. package/examples/jsm/environments/RoomEnvironment.js +8 -3
  17. package/examples/jsm/exporters/USDZExporter.js +676 -299
  18. package/examples/jsm/geometries/RoundedBoxGeometry.js +47 -8
  19. package/examples/jsm/interactive/HTMLMesh.js +5 -3
  20. package/examples/jsm/libs/meshopt_decoder.module.js +75 -58
  21. package/examples/jsm/lights/LightProbeGenerator.js +14 -3
  22. package/examples/jsm/loaders/EXRLoader.js +210 -22
  23. package/examples/jsm/loaders/FBXLoader.js +1 -1
  24. package/examples/jsm/loaders/MaterialXLoader.js +212 -30
  25. package/examples/jsm/loaders/TTFLoader.js +13 -1
  26. package/examples/jsm/loaders/USDLoader.js +219 -0
  27. package/examples/jsm/loaders/USDZLoader.js +4 -892
  28. package/examples/jsm/loaders/usd/USDAParser.js +741 -0
  29. package/examples/jsm/loaders/usd/USDCParser.js +17 -0
  30. package/examples/jsm/objects/LensflareMesh.js +3 -3
  31. package/examples/jsm/objects/SkyMesh.js +2 -2
  32. package/examples/jsm/physics/RapierPhysics.js +14 -5
  33. package/examples/jsm/postprocessing/GTAOPass.js +10 -9
  34. package/examples/jsm/postprocessing/OutlinePass.js +17 -17
  35. package/examples/jsm/postprocessing/SSAOPass.js +10 -9
  36. package/examples/jsm/shaders/UnpackDepthRGBAShader.js +11 -2
  37. package/examples/jsm/transpiler/GLSLDecoder.js +2 -2
  38. package/examples/jsm/tsl/display/BloomNode.js +8 -7
  39. package/examples/jsm/tsl/display/GaussianBlurNode.js +6 -8
  40. package/examples/jsm/tsl/display/{TRAAPassNode.js → TRAANode.js} +181 -172
  41. package/examples/jsm/tsl/lighting/TiledLightsNode.js +1 -1
  42. package/package.json +1 -1
  43. package/src/Three.Core.js +1 -0
  44. package/src/Three.TSL.js +69 -20
  45. package/src/animation/KeyframeTrack.js +1 -1
  46. package/src/animation/tracks/BooleanKeyframeTrack.js +1 -1
  47. package/src/animation/tracks/StringKeyframeTrack.js +1 -1
  48. package/src/cameras/Camera.js +14 -0
  49. package/src/cameras/OrthographicCamera.js +1 -1
  50. package/src/cameras/PerspectiveCamera.js +1 -1
  51. package/src/constants.js +1 -1
  52. package/{examples/jsm/misc → src/core}/Timer.js +4 -42
  53. package/src/extras/PMREMGenerator.js +11 -0
  54. package/src/helpers/CameraHelper.js +41 -11
  55. package/src/helpers/SkeletonHelper.js +35 -6
  56. package/src/lights/LightShadow.js +21 -8
  57. package/src/lights/PointLightShadow.js +1 -1
  58. package/src/loaders/FileLoader.js +25 -2
  59. package/src/loaders/ImageBitmapLoader.js +23 -0
  60. package/src/loaders/Loader.js +14 -0
  61. package/src/loaders/LoadingManager.js +23 -0
  62. package/src/materials/MeshBasicMaterial.js +1 -1
  63. package/src/materials/nodes/Line2NodeMaterial.js +0 -8
  64. package/src/materials/nodes/NodeMaterial.js +1 -1
  65. package/src/materials/nodes/PointsNodeMaterial.js +5 -0
  66. package/src/materials/nodes/manager/NodeMaterialObserver.js +87 -2
  67. package/src/math/Frustum.js +19 -8
  68. package/src/math/FrustumArray.js +10 -5
  69. package/src/math/Line3.js +129 -2
  70. package/src/math/Matrix4.js +48 -27
  71. package/src/math/Spherical.js +2 -2
  72. package/src/nodes/Nodes.js +1 -0
  73. package/src/nodes/TSL.js +1 -0
  74. package/src/nodes/accessors/Camera.js +12 -12
  75. package/src/nodes/accessors/Normal.js +11 -11
  76. package/src/nodes/accessors/ReferenceNode.js +18 -3
  77. package/src/nodes/accessors/SceneNode.js +1 -1
  78. package/src/nodes/accessors/StorageTextureNode.js +1 -1
  79. package/src/nodes/accessors/TextureNode.js +12 -0
  80. package/src/nodes/core/ArrayNode.js +12 -0
  81. package/src/nodes/core/AssignNode.js +3 -0
  82. package/src/nodes/core/ContextNode.js +20 -1
  83. package/src/nodes/core/Node.js +14 -2
  84. package/src/nodes/core/NodeBuilder.js +25 -20
  85. package/src/nodes/core/NodeUtils.js +4 -1
  86. package/src/nodes/core/StackNode.js +42 -0
  87. package/src/nodes/core/UniformNode.js +63 -5
  88. package/src/nodes/core/VarNode.js +91 -2
  89. package/src/nodes/display/PassNode.js +148 -2
  90. package/src/nodes/display/ViewportTextureNode.js +67 -7
  91. package/src/nodes/functions/PhysicalLightingModel.js +2 -2
  92. package/src/nodes/gpgpu/AtomicFunctionNode.js +1 -1
  93. package/src/nodes/gpgpu/ComputeNode.js +67 -23
  94. package/src/nodes/gpgpu/WorkgroupInfoNode.js +28 -3
  95. package/src/nodes/lighting/ProjectorLightNode.js +19 -6
  96. package/src/nodes/lighting/ShadowFilterNode.js +1 -1
  97. package/src/nodes/materialx/MaterialXNodes.js +131 -2
  98. package/src/nodes/materialx/lib/mx_noise.js +165 -1
  99. package/src/nodes/math/ConditionalNode.js +1 -1
  100. package/src/nodes/math/MathNode.js +78 -54
  101. package/src/nodes/math/OperatorNode.js +22 -22
  102. package/src/nodes/tsl/TSLCore.js +64 -9
  103. package/src/nodes/utils/DebugNode.js +1 -1
  104. package/src/nodes/utils/EventNode.js +83 -0
  105. package/src/nodes/utils/RTTNode.js +9 -0
  106. package/src/objects/BatchedMesh.js +4 -2
  107. package/src/renderers/WebGLRenderer.js +21 -22
  108. package/src/renderers/common/Bindings.js +19 -18
  109. package/src/renderers/common/Color4.js +2 -2
  110. package/src/renderers/common/PostProcessing.js +60 -5
  111. package/src/renderers/common/Renderer.js +18 -15
  112. package/src/renderers/common/SampledTexture.js +3 -71
  113. package/src/renderers/common/Sampler.js +79 -0
  114. package/src/renderers/common/Storage3DTexture.js +21 -0
  115. package/src/renderers/common/StorageArrayTexture.js +21 -0
  116. package/src/renderers/common/StorageTexture.js +19 -0
  117. package/src/renderers/common/Textures.js +19 -3
  118. package/src/renderers/common/XRManager.js +26 -8
  119. package/src/renderers/common/nodes/NodeSampledTexture.js +0 -12
  120. package/src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl.js +20 -2
  121. package/src/renderers/shaders/ShaderLib/depth.glsl.js +11 -2
  122. package/src/renderers/webgl/WebGLCapabilities.js +2 -2
  123. package/src/renderers/webgl/WebGLMaterials.js +6 -6
  124. package/src/renderers/webgl/WebGLProgram.js +22 -16
  125. package/src/renderers/webgl/WebGLPrograms.js +4 -4
  126. package/src/renderers/webgl/WebGLShadowMap.js +11 -1
  127. package/src/renderers/webgl/WebGLTextures.js +19 -7
  128. package/src/renderers/webgl-fallback/WebGLBackend.js +22 -12
  129. package/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js +2 -2
  130. package/src/renderers/webgpu/WebGPUBackend.js +54 -15
  131. package/src/renderers/webgpu/nodes/WGSLNodeBuilder.js +53 -73
  132. package/src/renderers/webgpu/utils/WebGPUBindingUtils.js +35 -31
  133. package/src/renderers/webgpu/utils/WebGPUPipelineUtils.js +1 -1
  134. package/src/renderers/webgpu/utils/WebGPUTextureUtils.js +11 -64
  135. package/src/renderers/webgpu/utils/WebGPUUtils.js +2 -17
  136. package/src/renderers/webxr/WebXRDepthSensing.js +6 -10
  137. package/src/renderers/webxr/WebXRManager.js +68 -8
  138. package/src/textures/ExternalTexture.js +45 -0
  139. package/src/textures/FramebufferTexture.js +2 -2
  140. package/src/textures/Source.js +11 -1
  141. package/src/textures/VideoTexture.js +30 -2
@@ -204,14 +204,27 @@ class LightShadow {
204
204
  shadowCamera.updateMatrixWorld();
205
205
 
206
206
  _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
207
- this._frustum.setFromProjectionMatrix( _projScreenMatrix );
208
-
209
- shadowMatrix.set(
210
- 0.5, 0.0, 0.0, 0.5,
211
- 0.0, 0.5, 0.0, 0.5,
212
- 0.0, 0.0, 0.5, 0.5,
213
- 0.0, 0.0, 0.0, 1.0
214
- );
207
+ this._frustum.setFromProjectionMatrix( _projScreenMatrix, shadowCamera.coordinateSystem, shadowCamera.reversedDepth );
208
+
209
+ if ( shadowCamera.reversedDepth ) {
210
+
211
+ shadowMatrix.set(
212
+ 0.5, 0.0, 0.0, 0.5,
213
+ 0.0, 0.5, 0.0, 0.5,
214
+ 0.0, 0.0, 1.0, 0.0,
215
+ 0.0, 0.0, 0.0, 1.0
216
+ );
217
+
218
+ } else {
219
+
220
+ shadowMatrix.set(
221
+ 0.5, 0.0, 0.0, 0.5,
222
+ 0.0, 0.5, 0.0, 0.5,
223
+ 0.0, 0.0, 0.5, 0.5,
224
+ 0.0, 0.0, 0.0, 1.0
225
+ );
226
+
227
+ }
215
228
 
216
229
  shadowMatrix.multiply( _projScreenMatrix );
217
230
 
@@ -108,7 +108,7 @@ class PointLightShadow extends LightShadow {
108
108
  shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z );
109
109
 
110
110
  _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
111
- this._frustum.setFromProjectionMatrix( _projScreenMatrix );
111
+ this._frustum.setFromProjectionMatrix( _projScreenMatrix, camera.coordinateSystem, camera.reversedDepth );
112
112
 
113
113
  }
114
114
 
@@ -41,7 +41,8 @@ class FileLoader extends Loader {
41
41
  super( manager );
42
42
 
43
43
  /**
44
- * The expected mime type.
44
+ * The expected mime type. Valid values can be found
45
+ * [here]{@link hhttps://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#mimetype}
45
46
  *
46
47
  * @type {string}
47
48
  */
@@ -55,6 +56,14 @@ class FileLoader extends Loader {
55
56
  */
56
57
  this.responseType = '';
57
58
 
59
+ /**
60
+ * Used for aborting requests.
61
+ *
62
+ * @private
63
+ * @type {AbortController}
64
+ */
65
+ this._abortController = new AbortController();
66
+
58
67
  }
59
68
 
60
69
  /**
@@ -121,7 +130,7 @@ class FileLoader extends Loader {
121
130
  const req = new Request( url, {
122
131
  headers: new Headers( this.requestHeader ),
123
132
  credentials: this.withCredentials ? 'include' : 'same-origin',
124
- // An abort controller could be added within a future PR
133
+ signal: ( typeof AbortSignal.any === 'function' ) ? AbortSignal.any( [ this._abortController.signal, this.manager.abortController.signal ] ) : this._abortController.signal
125
134
  } );
126
135
 
127
136
  // record states ( avoid data race )
@@ -338,6 +347,20 @@ class FileLoader extends Loader {
338
347
 
339
348
  }
340
349
 
350
+ /**
351
+ * Aborts ongoing fetch requests.
352
+ *
353
+ * @return {FileLoader} A reference to this instance.
354
+ */
355
+ abort() {
356
+
357
+ this._abortController.abort();
358
+ this._abortController = new AbortController();
359
+
360
+ return this;
361
+
362
+ }
363
+
341
364
  }
342
365
 
343
366
 
@@ -66,6 +66,14 @@ class ImageBitmapLoader extends Loader {
66
66
  */
67
67
  this.options = { premultiplyAlpha: 'none' };
68
68
 
69
+ /**
70
+ * Used for aborting requests.
71
+ *
72
+ * @private
73
+ * @type {AbortController}
74
+ */
75
+ this._abortController = new AbortController();
76
+
69
77
  }
70
78
 
71
79
  /**
@@ -154,6 +162,7 @@ class ImageBitmapLoader extends Loader {
154
162
  const fetchOptions = {};
155
163
  fetchOptions.credentials = ( this.crossOrigin === 'anonymous' ) ? 'same-origin' : 'include';
156
164
  fetchOptions.headers = this.requestHeader;
165
+ fetchOptions.signal = ( typeof AbortSignal.any === 'function' ) ? AbortSignal.any( [ this._abortController.signal, this.manager.abortController.signal ] ) : this._abortController.signal;
157
166
 
158
167
  const promise = fetch( url, fetchOptions ).then( function ( res ) {
159
168
 
@@ -191,6 +200,20 @@ class ImageBitmapLoader extends Loader {
191
200
 
192
201
  }
193
202
 
203
+ /**
204
+ * Aborts ongoing fetch requests.
205
+ *
206
+ * @return {ImageBitmapLoader} A reference to this instance.
207
+ */
208
+ abort() {
209
+
210
+ this._abortController.abort();
211
+ this._abortController = new AbortController();
212
+
213
+ return this;
214
+
215
+ }
216
+
194
217
  }
195
218
 
196
219
  export { ImageBitmapLoader };
@@ -67,6 +67,7 @@ class Loader {
67
67
  * This method needs to be implemented by all concrete loaders. It holds the
68
68
  * logic for loading assets from the backend.
69
69
  *
70
+ * @abstract
70
71
  * @param {string} url - The path/URL of the file to be loaded.
71
72
  * @param {Function} onLoad - Executed when the loading process has been finished.
72
73
  * @param {onProgressCallback} [onProgress] - Executed while the loading is in progress.
@@ -97,6 +98,7 @@ class Loader {
97
98
  * This method needs to be implemented by all concrete loaders. It holds the
98
99
  * logic for parsing the asset into three.js entities.
99
100
  *
101
+ * @abstract
100
102
  * @param {any} data - The data to parse.
101
103
  */
102
104
  parse( /* data */ ) {}
@@ -171,6 +173,18 @@ class Loader {
171
173
 
172
174
  }
173
175
 
176
+ /**
177
+ * This method can be implemented in loaders for aborting ongoing requests.
178
+ *
179
+ * @abstract
180
+ * @return {Loader} A reference to this instance.
181
+ */
182
+ abort() {
183
+
184
+ return this;
185
+
186
+ }
187
+
174
188
  }
175
189
 
176
190
  /**
@@ -69,6 +69,13 @@ class LoadingManager {
69
69
  */
70
70
  this.onError = onError;
71
71
 
72
+ /**
73
+ * Used for aborting ongoing requests in loaders using this manager.
74
+ *
75
+ * @type {AbortController}
76
+ */
77
+ this.abortController = new AbortController();
78
+
72
79
  /**
73
80
  * This should be called by any loader using the manager when the loader
74
81
  * starts loading an item.
@@ -269,6 +276,22 @@ class LoadingManager {
269
276
 
270
277
  };
271
278
 
279
+ /**
280
+ * Can be used to abort ongoing loading requests in loaders using this manager.
281
+ * The abort only works if the loaders implement {@link Loader#abort} and `AbortSignal.any()`
282
+ * is supported in the browser.
283
+ *
284
+ * @return {LoadingManager} A reference to this loading manager.
285
+ */
286
+ this.abort = function () {
287
+
288
+ this.abortController.abort();
289
+ this.abortController = new AbortController();
290
+
291
+ return this;
292
+
293
+ };
294
+
272
295
  }
273
296
 
274
297
  }
@@ -42,7 +42,7 @@ class MeshBasicMaterial extends Material {
42
42
  * @type {Color}
43
43
  * @default (1,1,1)
44
44
  */
45
- this.color = new Color( 0xffffff ); // emissive
45
+ this.color = new Color( 0xffffff ); // diffuse
46
46
 
47
47
  /**
48
48
  * The color map. May optionally include an alpha channel, typically combined
@@ -66,14 +66,6 @@ class Line2NodeMaterial extends NodeMaterial {
66
66
  */
67
67
  this.dashOffset = 0;
68
68
 
69
- /**
70
- * The line width.
71
- *
72
- * @type {number}
73
- * @default 0
74
- */
75
- this.lineWidth = 1;
76
-
77
69
  /**
78
70
  * Defines the lines color.
79
71
  *
@@ -1071,7 +1071,7 @@ class NodeMaterial extends Material {
1071
1071
 
1072
1072
  output.assign( outputNode );
1073
1073
 
1074
- outputNode = vec4( fogNode );
1074
+ outputNode = vec4( fogNode.toVar() );
1075
1075
 
1076
1076
  }
1077
1077
 
@@ -35,6 +35,11 @@ class PointsNodeMaterial extends SpriteNodeMaterial {
35
35
  /**
36
36
  * This node property provides an additional way to set the point size.
37
37
  *
38
+ * Note that WebGPU only supports point primitives with 1 pixel size. Consequently,
39
+ * this node has no effect when the material is used with {@link Points} and a WebGPU
40
+ * backend. If an application wants to render points with a size larger than 1 pixel,
41
+ * the material should be used with {@link Sprite} and instancing.
42
+ *
38
43
  * @type {?Node<vec2>}
39
44
  * @default null
40
45
  */
@@ -55,6 +55,16 @@ const refreshUniforms = [
55
55
  'transmissionMap'
56
56
  ];
57
57
 
58
+
59
+ /**
60
+ * A WeakMap to cache lights data for node materials.
61
+ * Cache lights data by render ID to avoid unnecessary recalculations.
62
+ *
63
+ * @private
64
+ * @type {WeakMap<LightsNode,Object>}
65
+ */
66
+ const _lightsCache = new WeakMap();
67
+
58
68
  /**
59
69
  * This class is used by {@link WebGPURenderer} as management component.
60
70
  * It's primary purpose is to determine whether render objects require a
@@ -196,6 +206,8 @@ class NodeMaterialObserver {
196
206
 
197
207
  }
198
208
 
209
+ data.lights = this.getLightsData( renderObject.lightsNode.getLights() );
210
+
199
211
  this.renderObjects.set( renderObject, data );
200
212
 
201
213
  }
@@ -299,9 +311,10 @@ class NodeMaterialObserver {
299
311
  * Returns `true` if the given render object has not changed its state.
300
312
  *
301
313
  * @param {RenderObject} renderObject - The render object.
314
+ * @param {Array<Light>} lightsData - The current material lights.
302
315
  * @return {boolean} Whether the given render object has changed its state or not.
303
316
  */
304
- equals( renderObject ) {
317
+ equals( renderObject, lightsData ) {
305
318
 
306
319
  const { object, material, geometry } = renderObject;
307
320
 
@@ -462,6 +475,22 @@ class NodeMaterialObserver {
462
475
 
463
476
  }
464
477
 
478
+ // lights
479
+
480
+ if ( renderObjectData.lights ) {
481
+
482
+ for ( let i = 0; i < lightsData.length; i ++ ) {
483
+
484
+ if ( renderObjectData.lights[ i ].map !== lightsData[ i ].map ) {
485
+
486
+ return false;
487
+
488
+ }
489
+
490
+ }
491
+
492
+ }
493
+
465
494
  // center
466
495
 
467
496
  if ( renderObjectData.center ) {
@@ -488,6 +517,61 @@ class NodeMaterialObserver {
488
517
 
489
518
  }
490
519
 
520
+ /**
521
+ * Returns the lights data for the given material lights.
522
+ *
523
+ * @param {Array<Light>} materialLights - The material lights.
524
+ * @return {Array<Object>} The lights data for the given material lights.
525
+ */
526
+ getLightsData( materialLights ) {
527
+
528
+ const lights = [];
529
+
530
+ for ( const light of materialLights ) {
531
+
532
+ if ( light.isSpotLight === true && light.map !== null ) {
533
+
534
+ // only add lights that have a map
535
+
536
+ lights.push( { map: light.map.version } );
537
+
538
+ }
539
+
540
+ }
541
+
542
+ return lights;
543
+
544
+ }
545
+
546
+ /**
547
+ * Returns the lights for the given lights node and render ID.
548
+ *
549
+ * @param {LightsNode} lightsNode - The lights node.
550
+ * @param {number} renderId - The render ID.
551
+ * @return {Array} The lights for the given lights node and render ID.
552
+ */
553
+ getLights( lightsNode, renderId ) {
554
+
555
+ if ( _lightsCache.has( lightsNode ) ) {
556
+
557
+ const cached = _lightsCache.get( lightsNode );
558
+
559
+ if ( cached.renderId === renderId ) {
560
+
561
+ return cached.lightsData;
562
+
563
+ }
564
+
565
+ }
566
+
567
+ const lightsData = this.getLightsData( lightsNode.getLights() );
568
+
569
+ _lightsCache.set( lightsNode, { renderId, lightsData } );
570
+
571
+ return lightsData;
572
+
573
+ }
574
+
491
575
  /**
492
576
  * Checks if the given render object requires a refresh.
493
577
  *
@@ -516,7 +600,8 @@ class NodeMaterialObserver {
516
600
  if ( isStatic || isBundle )
517
601
  return false;
518
602
 
519
- const notEqual = this.equals( renderObject ) !== true;
603
+ const lightsData = this.getLights( renderObject.lightsNode, renderId );
604
+ const notEqual = this.equals( renderObject, lightsData ) !== true;
520
605
 
521
606
  return notEqual;
522
607
 
@@ -89,9 +89,10 @@ class Frustum {
89
89
  *
90
90
  * @param {Matrix4} m - The projection matrix.
91
91
  * @param {(WebGLCoordinateSystem|WebGPUCoordinateSystem)} coordinateSystem - The coordinate system.
92
+ * @param {boolean} [reversedDepth=false] - Whether to use a reversed depth.
92
93
  * @return {Frustum} A reference to this frustum.
93
94
  */
94
- setFromProjectionMatrix( m, coordinateSystem = WebGLCoordinateSystem ) {
95
+ setFromProjectionMatrix( m, coordinateSystem = WebGLCoordinateSystem, reversedDepth = false ) {
95
96
 
96
97
  const planes = this.planes;
97
98
  const me = m.elements;
@@ -104,19 +105,29 @@ class Frustum {
104
105
  planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
105
106
  planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
106
107
  planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
107
- planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
108
108
 
109
- if ( coordinateSystem === WebGLCoordinateSystem ) {
109
+ if ( reversedDepth ) {
110
110
 
111
- planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize();
111
+ planes[ 4 ].setComponents( me2, me6, me10, me14 ).normalize(); // far
112
+ planes[ 5 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize(); // near
112
113
 
113
- } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
114
+ } else {
114
115
 
115
- planes[ 5 ].setComponents( me2, me6, me10, me14 ).normalize();
116
+ planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize(); // far
116
117
 
117
- } else {
118
+ if ( coordinateSystem === WebGLCoordinateSystem ) {
119
+
120
+ planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize(); // near
121
+
122
+ } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
118
123
 
119
- throw new Error( 'THREE.Frustum.setFromProjectionMatrix(): Invalid coordinate system: ' + coordinateSystem );
124
+ planes[ 5 ].setComponents( me2, me6, me10, me14 ).normalize(); // near
125
+
126
+ } else {
127
+
128
+ throw new Error( 'THREE.Frustum.setFromProjectionMatrix(): Invalid coordinate system: ' + coordinateSystem );
129
+
130
+ }
120
131
 
121
132
  }
122
133
 
@@ -54,7 +54,8 @@ class FrustumArray {
54
54
 
55
55
  _frustum.setFromProjectionMatrix(
56
56
  _projScreenMatrix,
57
- this.coordinateSystem
57
+ camera.coordinateSystem,
58
+ camera.reversedDepth
58
59
  );
59
60
 
60
61
  if ( _frustum.intersectsObject( object ) ) {
@@ -96,7 +97,8 @@ class FrustumArray {
96
97
 
97
98
  _frustum.setFromProjectionMatrix(
98
99
  _projScreenMatrix,
99
- this.coordinateSystem
100
+ camera.coordinateSystem,
101
+ camera.reversedDepth
100
102
  );
101
103
 
102
104
  if ( _frustum.intersectsSprite( sprite ) ) {
@@ -138,7 +140,8 @@ class FrustumArray {
138
140
 
139
141
  _frustum.setFromProjectionMatrix(
140
142
  _projScreenMatrix,
141
- this.coordinateSystem
143
+ camera.coordinateSystem,
144
+ camera.reversedDepth
142
145
  );
143
146
 
144
147
  if ( _frustum.intersectsSphere( sphere ) ) {
@@ -180,7 +183,8 @@ class FrustumArray {
180
183
 
181
184
  _frustum.setFromProjectionMatrix(
182
185
  _projScreenMatrix,
183
- this.coordinateSystem
186
+ camera.coordinateSystem,
187
+ camera.reversedDepth
184
188
  );
185
189
 
186
190
  if ( _frustum.intersectsBox( box ) ) {
@@ -222,7 +226,8 @@ class FrustumArray {
222
226
 
223
227
  _frustum.setFromProjectionMatrix(
224
228
  _projScreenMatrix,
225
- this.coordinateSystem
229
+ camera.coordinateSystem,
230
+ camera.reversedDepth
226
231
  );
227
232
 
228
233
  if ( _frustum.containsPoint( point ) ) {
package/src/math/Line3.js CHANGED
@@ -4,6 +4,12 @@ import { clamp } from './MathUtils.js';
4
4
  const _startP = /*@__PURE__*/ new Vector3();
5
5
  const _startEnd = /*@__PURE__*/ new Vector3();
6
6
 
7
+ const _d1 = /*@__PURE__*/ new Vector3();
8
+ const _d2 = /*@__PURE__*/ new Vector3();
9
+ const _r = /*@__PURE__*/ new Vector3();
10
+ const _c1 = /*@__PURE__*/ new Vector3();
11
+ const _c2 = /*@__PURE__*/ new Vector3();
12
+
7
13
  /**
8
14
  * An analytical line segment in 3D space represented by a start and end point.
9
15
  */
@@ -151,11 +157,11 @@ class Line3 {
151
157
  }
152
158
 
153
159
  /**
154
- * Returns the closets point on the line for a given point.
160
+ * Returns the closest point on the line for a given point.
155
161
  *
156
162
  * @param {Vector3} point - The point to compute the closest point on the line for.
157
163
  * @param {boolean} clampToLine - Whether to clamp the result to the range `[0,1]` or not.
158
- * @param {Vector3} target - The target vector that is used to store the method's result.
164
+ * @param {Vector3} target - The target vector that is used to store the method's result.
159
165
  * @return {Vector3} The closest point on the line.
160
166
  */
161
167
  closestPointToPoint( point, clampToLine, target ) {
@@ -166,6 +172,127 @@ class Line3 {
166
172
 
167
173
  }
168
174
 
175
+ /**
176
+ * Returns the closest squared distance between this line segment and the given one.
177
+ *
178
+ * @param {Line3} line - The line segment to compute the closest squared distance to.
179
+ * @param {Vector3} [c1] - The closest point on this line segment.
180
+ * @param {Vector3} [c2] - The closest point on the given line segment.
181
+ * @return {number} The squared distance between this line segment and the given one.
182
+ */
183
+ distanceSqToLine3( line, c1 = _c1, c2 = _c2 ) {
184
+
185
+ // from Real-Time Collision Detection by Christer Ericson, chapter 5.1.9
186
+
187
+ // Computes closest points C1 and C2 of S1(s)=P1+s*(Q1-P1) and
188
+ // S2(t)=P2+t*(Q2-P2), returning s and t. Function result is squared
189
+ // distance between between S1(s) and S2(t)
190
+
191
+ const EPSILON = 1e-8 * 1e-8; // must be squared since we compare squared length
192
+ let s, t;
193
+
194
+ const p1 = this.start;
195
+ const p2 = line.start;
196
+ const q1 = this.end;
197
+ const q2 = line.end;
198
+
199
+ _d1.subVectors( q1, p1 ); // Direction vector of segment S1
200
+ _d2.subVectors( q2, p2 ); // Direction vector of segment S2
201
+ _r.subVectors( p1, p2 );
202
+
203
+ const a = _d1.dot( _d1 ); // Squared length of segment S1, always nonnegative
204
+ const e = _d2.dot( _d2 ); // Squared length of segment S2, always nonnegative
205
+ const f = _d2.dot( _r );
206
+
207
+ // Check if either or both segments degenerate into points
208
+
209
+ if ( a <= EPSILON && e <= EPSILON ) {
210
+
211
+ // Both segments degenerate into points
212
+
213
+ c1.copy( p1 );
214
+ c2.copy( p2 );
215
+
216
+ c1.sub( c2 );
217
+
218
+ return c1.dot( c1 );
219
+
220
+ }
221
+
222
+ if ( a <= EPSILON ) {
223
+
224
+ // First segment degenerates into a point
225
+
226
+ s = 0;
227
+ t = f / e; // s = 0 => t = (b*s + f) / e = f / e
228
+ t = clamp( t, 0, 1 );
229
+
230
+
231
+ } else {
232
+
233
+ const c = _d1.dot( _r );
234
+
235
+ if ( e <= EPSILON ) {
236
+
237
+ // Second segment degenerates into a point
238
+
239
+ t = 0;
240
+ s = clamp( - c / a, 0, 1 ); // t = 0 => s = (b*t - c) / a = -c / a
241
+
242
+ } else {
243
+
244
+ // The general nondegenerate case starts here
245
+
246
+ const b = _d1.dot( _d2 );
247
+ const denom = a * e - b * b; // Always nonnegative
248
+
249
+ // If segments not parallel, compute closest point on L1 to L2 and
250
+ // clamp to segment S1. Else pick arbitrary s (here 0)
251
+
252
+ if ( denom !== 0 ) {
253
+
254
+ s = clamp( ( b * f - c * e ) / denom, 0, 1 );
255
+
256
+ } else {
257
+
258
+ s = 0;
259
+
260
+ }
261
+
262
+ // Compute point on L2 closest to S1(s) using
263
+ // t = Dot((P1 + D1*s) - P2,D2) / Dot(D2,D2) = (b*s + f) / e
264
+
265
+ t = ( b * s + f ) / e;
266
+
267
+ // If t in [0,1] done. Else clamp t, recompute s for the new value
268
+ // of t using s = Dot((P2 + D2*t) - P1,D1) / Dot(D1,D1)= (t*b - c) / a
269
+ // and clamp s to [0, 1]
270
+
271
+ if ( t < 0 ) {
272
+
273
+ t = 0.;
274
+ s = clamp( - c / a, 0, 1 );
275
+
276
+ } else if ( t > 1 ) {
277
+
278
+ t = 1;
279
+ s = clamp( ( b - c ) / a, 0, 1 );
280
+
281
+ }
282
+
283
+ }
284
+
285
+ }
286
+
287
+ c1.copy( p1 ).add( _d1.multiplyScalar( s ) );
288
+ c2.copy( p2 ).add( _d2.multiplyScalar( t ) );
289
+
290
+ c1.sub( c2 );
291
+
292
+ return c1.dot( c1 );
293
+
294
+ }
295
+
169
296
  /**
170
297
  * Applies a 4x4 transformation matrix to this line segment.
171
298
  *