Skip to content

Commit 5ff3fe6

Browse files
MixMasterMitchmloeppky
andauthored
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]>
1 parent dd5d429 commit 5ff3fe6

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

examples/jsm/lines/LineSegments2.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const _box = new Box3();
2929
const _sphere = new Sphere();
3030
const _clipToWorldVector = new Vector4();
3131

32-
let _ray, _instanceStart, _instanceEnd, _lineWidth;
32+
let _ray, _lineWidth;
3333

3434
// Returns the margin required to expand by in world space given the distance from the camera,
3535
// line width, resolution, and camera projection
@@ -52,11 +52,15 @@ function getWorldSpaceHalfWidth( camera, distance, resolution ) {
5252
function raycastWorldUnits( lineSegments, intersects ) {
5353

5454
const matrixWorld = lineSegments.matrixWorld;
55+
const geometry = lineSegments.geometry;
56+
const instanceStart = geometry.attributes.instanceStart;
57+
const instanceEnd = geometry.attributes.instanceEnd;
58+
const segmentCount = Math.min( geometry.instanceCount, instanceStart.count );
5559

56-
for ( let i = 0, l = _instanceStart.count; i < l; i ++ ) {
60+
for ( let i = 0, l = segmentCount; i < l; i ++ ) {
5761

58-
_line.start.fromBufferAttribute( _instanceStart, i );
59-
_line.end.fromBufferAttribute( _instanceEnd, i );
62+
_line.start.fromBufferAttribute( instanceStart, i );
63+
_line.end.fromBufferAttribute( instanceEnd, i );
6064

6165
_line.applyMatrix4( matrixWorld );
6266

@@ -95,6 +99,7 @@ function raycastScreenSpace( lineSegments, camera, intersects ) {
9599
const geometry = lineSegments.geometry;
96100
const instanceStart = geometry.attributes.instanceStart;
97101
const instanceEnd = geometry.attributes.instanceEnd;
102+
const segmentCount = Math.min( geometry.instanceCount, instanceStart.count );
98103

99104
const near = - camera.near;
100105

@@ -120,7 +125,7 @@ function raycastScreenSpace( lineSegments, camera, intersects ) {
120125

121126
_mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
122127

123-
for ( let i = 0, l = instanceStart.count; i < l; i ++ ) {
128+
for ( let i = 0, l = segmentCount; i < l; i ++ ) {
124129

125130
_start4.fromBufferAttribute( instanceStart, i );
126131
_end4.fromBufferAttribute( instanceEnd, i );
@@ -279,9 +284,6 @@ class LineSegments2 extends Mesh {
279284

280285
_lineWidth = material.linewidth + threshold;
281286

282-
_instanceStart = geometry.attributes.instanceStart;
283-
_instanceEnd = geometry.attributes.instanceEnd;
284-
285287
// check if we intersect the sphere bounds
286288
if ( geometry.boundingSphere === null ) {
287289

0 commit comments

Comments
 (0)