@@ -29,7 +29,7 @@ const _box = new Box3();
29
29
const _sphere = new Sphere ( ) ;
30
30
const _clipToWorldVector = new Vector4 ( ) ;
31
31
32
- let _ray , _instanceStart , _instanceEnd , _lineWidth ;
32
+ let _ray , _lineWidth ;
33
33
34
34
// Returns the margin required to expand by in world space given the distance from the camera,
35
35
// line width, resolution, and camera projection
@@ -52,11 +52,15 @@ function getWorldSpaceHalfWidth( camera, distance, resolution ) {
52
52
function raycastWorldUnits ( lineSegments , intersects ) {
53
53
54
54
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 ) ;
55
59
56
- for ( let i = 0 , l = _instanceStart . count ; i < l ; i ++ ) {
60
+ for ( let i = 0 , l = segmentCount ; i < l ; i ++ ) {
57
61
58
- _line . start . fromBufferAttribute ( _instanceStart , i ) ;
59
- _line . end . fromBufferAttribute ( _instanceEnd , i ) ;
62
+ _line . start . fromBufferAttribute ( instanceStart , i ) ;
63
+ _line . end . fromBufferAttribute ( instanceEnd , i ) ;
60
64
61
65
_line . applyMatrix4 ( matrixWorld ) ;
62
66
@@ -95,6 +99,7 @@ function raycastScreenSpace( lineSegments, camera, intersects ) {
95
99
const geometry = lineSegments . geometry ;
96
100
const instanceStart = geometry . attributes . instanceStart ;
97
101
const instanceEnd = geometry . attributes . instanceEnd ;
102
+ const segmentCount = Math . min ( geometry . instanceCount , instanceStart . count ) ;
98
103
99
104
const near = - camera . near ;
100
105
@@ -120,7 +125,7 @@ function raycastScreenSpace( lineSegments, camera, intersects ) {
120
125
121
126
_mvMatrix . multiplyMatrices ( camera . matrixWorldInverse , matrixWorld ) ;
122
127
123
- for ( let i = 0 , l = instanceStart . count ; i < l ; i ++ ) {
128
+ for ( let i = 0 , l = segmentCount ; i < l ; i ++ ) {
124
129
125
130
_start4 . fromBufferAttribute ( instanceStart , i ) ;
126
131
_end4 . fromBufferAttribute ( instanceEnd , i ) ;
@@ -279,9 +284,6 @@ class LineSegments2 extends Mesh {
279
284
280
285
_lineWidth = material . linewidth + threshold ;
281
286
282
- _instanceStart = geometry . attributes . instanceStart ;
283
- _instanceEnd = geometry . attributes . instanceEnd ;
284
-
285
287
// check if we intersect the sphere bounds
286
288
if ( geometry . boundingSphere === null ) {
287
289
0 commit comments