Sfoglia il codice sorgente

LineSegments2: Fix raytracing when the geometry has `instanceCount` set. (#25032)

* Fix raytracing of LineSegments2 when the geometry has instanceCount set.

* Lint fix

Co-authored-by: mloeppky <[email protected]>
Mitchell Loeppky 2 anni fa
parent
commit
5ff3fe63d8
1 ha cambiato i file con 10 aggiunte e 8 eliminazioni
  1. 10 8
      examples/jsm/lines/LineSegments2.js

+ 10 - 8
examples/jsm/lines/LineSegments2.js

@@ -29,7 +29,7 @@ const _box = new Box3();
 const _sphere = new Sphere();
 const _clipToWorldVector = new Vector4();
 
-let _ray, _instanceStart, _instanceEnd, _lineWidth;
+let _ray, _lineWidth;
 
 // Returns the margin required to expand by in world space given the distance from the camera,
 // line width, resolution, and camera projection
@@ -52,11 +52,15 @@ function getWorldSpaceHalfWidth( camera, distance, resolution ) {
 function raycastWorldUnits( lineSegments, intersects ) {
 
 	const matrixWorld = lineSegments.matrixWorld;
+	const geometry = lineSegments.geometry;
+	const instanceStart = geometry.attributes.instanceStart;
+	const instanceEnd = geometry.attributes.instanceEnd;
+	const segmentCount = Math.min( geometry.instanceCount, instanceStart.count );
 
-	for ( let i = 0, l = _instanceStart.count; i < l; i ++ ) {
+	for ( let i = 0, l = segmentCount; i < l; i ++ ) {
 
-		_line.start.fromBufferAttribute( _instanceStart, i );
-		_line.end.fromBufferAttribute( _instanceEnd, i );
+		_line.start.fromBufferAttribute( instanceStart, i );
+		_line.end.fromBufferAttribute( instanceEnd, i );
 
 		_line.applyMatrix4( matrixWorld );
 
@@ -95,6 +99,7 @@ function raycastScreenSpace( lineSegments, camera, intersects ) {
 	const geometry = lineSegments.geometry;
 	const instanceStart = geometry.attributes.instanceStart;
 	const instanceEnd = geometry.attributes.instanceEnd;
+	const segmentCount = Math.min( geometry.instanceCount, instanceStart.count );
 
 	const near = - camera.near;
 
@@ -120,7 +125,7 @@ function raycastScreenSpace( lineSegments, camera, intersects ) {
 
 	_mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
 
-	for ( let i = 0, l = instanceStart.count; i < l; i ++ ) {
+	for ( let i = 0, l = segmentCount; i < l; i ++ ) {
 
 		_start4.fromBufferAttribute( instanceStart, i );
 		_end4.fromBufferAttribute( instanceEnd, i );
@@ -279,9 +284,6 @@ class LineSegments2 extends Mesh {
 
 		_lineWidth = material.linewidth + threshold;
 
-		_instanceStart = geometry.attributes.instanceStart;
-		_instanceEnd = geometry.attributes.instanceEnd;
-
 		// check if we intersect the sphere bounds
 		if ( geometry.boundingSphere === null ) {