From 94d34c1aa4590f6d91324ba35dfb7f1a9f3bdd19 Mon Sep 17 00:00:00 2001 From: mloeppky Date: Mon, 28 Nov 2022 12:42:50 -0800 Subject: [PATCH 1/2] Fix raytracing of LineSegments2 when the geometry has instanceCount set. --- examples/jsm/lines/LineSegments2.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/jsm/lines/LineSegments2.js b/examples/jsm/lines/LineSegments2.js index e5d50258053510..7bc0cda4981544 100644 --- a/examples/jsm/lines/LineSegments2.js +++ b/examples/jsm/lines/LineSegments2.js @@ -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 ); From 728b0f3f147650b975d2d0aedc37582c87fe207c Mon Sep 17 00:00:00 2001 From: mloeppky Date: Mon, 28 Nov 2022 13:31:09 -0800 Subject: [PATCH 2/2] Lint fix --- examples/jsm/lines/LineSegments2.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/jsm/lines/LineSegments2.js b/examples/jsm/lines/LineSegments2.js index 7bc0cda4981544..089ad26503a38f 100644 --- a/examples/jsm/lines/LineSegments2.js +++ b/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 @@ -284,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 ) {