@@ -13,6 +13,8 @@ import {
1313import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry'
1414import { LineMaterial } from '../lines/LineMaterial'
1515
16+ const _viewport = new Vector4 ( ) ;
17+
1618const _start = new Vector3 ( )
1719const _end = new Vector3 ( )
1820
@@ -29,7 +31,7 @@ const _box = new Box3()
2931const _sphere = new Sphere ( )
3032const _clipToWorldVector = new Vector4 ( )
3133
32- let _ray , _instanceStart , _instanceEnd , _lineWidth
34+ let _ray , _lineWidth
3335
3436// Returns the margin required to expand by in world space given the distance from the camera,
3537// line width, resolution, and camera projection
@@ -48,9 +50,18 @@ function getWorldSpaceHalfWidth(camera, distance, resolution) {
4850}
4951
5052function raycastWorldUnits ( lineSegments , intersects ) {
51- for ( let i = 0 , l = _instanceStart . count ; i < l ; i ++ ) {
52- _line . start . fromBufferAttribute ( _instanceStart , i )
53- _line . end . fromBufferAttribute ( _instanceEnd , i )
53+
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 ) ;
59+
60+ for ( let i = 0 , l = segmentCount ; i < l ; i ++ ) {
61+ _line . start . fromBufferAttribute ( instanceStart , i )
62+ _line . end . fromBufferAttribute ( instanceEnd , i )
63+
64+ _line . applyMatrix4 ( matrixWorld ) ;
5465
5566 const pointOnLine = new Vector3 ( )
5667 const point = new Vector3 ( )
@@ -82,6 +93,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) {
8293 const geometry = lineSegments . geometry
8394 const instanceStart = geometry . attributes . instanceStart
8495 const instanceEnd = geometry . attributes . instanceEnd
96+ const segmentCount = Math . min ( geometry . instanceCount , instanceStart . count ) ;
8597
8698 const near = - camera . near
8799
@@ -107,7 +119,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) {
107119
108120 _mvMatrix . multiplyMatrices ( camera . matrixWorldInverse , matrixWorld )
109121
110- for ( let i = 0 , l = instanceStart . count ; i < l ; i ++ ) {
122+ for ( let i = 0 , l = segmentCount ; i < l ; i ++ ) {
111123 _start4 . fromBufferAttribute ( instanceStart , i )
112124 _end4 . fromBufferAttribute ( instanceEnd , i )
113125
@@ -247,9 +259,6 @@ class LineSegments2 extends Mesh {
247259
248260 _lineWidth = material . linewidth + threshold
249261
250- _instanceStart = geometry . attributes . instanceStart
251- _instanceEnd = geometry . attributes . instanceEnd
252-
253262 // check if we intersect the sphere bounds
254263 if ( geometry . boundingSphere === null ) {
255264 geometry . computeBoundingSphere ( )
@@ -300,6 +309,19 @@ class LineSegments2 extends Mesh {
300309 raycastScreenSpace ( this , camera , intersects )
301310 }
302311 }
312+
313+ onBeforeRender ( renderer ) {
314+
315+ const uniforms = this . material . uniforms ;
316+
317+ if ( uniforms && uniforms . resolution ) {
318+
319+ renderer . getViewport ( _viewport ) ;
320+ this . material . uniforms . resolution . value . set ( _viewport . z , _viewport . w ) ;
321+
322+ }
323+
324+ }
303325}
304326
305327export { LineSegments2 }
0 commit comments