@@ -13,6 +13,8 @@ import {
13
13
import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry'
14
14
import { LineMaterial } from '../lines/LineMaterial'
15
15
16
+ const _viewport = new Vector4 ( ) ;
17
+
16
18
const _start = new Vector3 ( )
17
19
const _end = new Vector3 ( )
18
20
@@ -29,7 +31,7 @@ const _box = new Box3()
29
31
const _sphere = new Sphere ( )
30
32
const _clipToWorldVector = new Vector4 ( )
31
33
32
- let _ray , _instanceStart , _instanceEnd , _lineWidth
34
+ let _ray , _lineWidth
33
35
34
36
// Returns the margin required to expand by in world space given the distance from the camera,
35
37
// line width, resolution, and camera projection
@@ -48,9 +50,18 @@ function getWorldSpaceHalfWidth(camera, distance, resolution) {
48
50
}
49
51
50
52
function 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 ) ;
54
65
55
66
const pointOnLine = new Vector3 ( )
56
67
const point = new Vector3 ( )
@@ -82,6 +93,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) {
82
93
const geometry = lineSegments . geometry
83
94
const instanceStart = geometry . attributes . instanceStart
84
95
const instanceEnd = geometry . attributes . instanceEnd
96
+ const segmentCount = Math . min ( geometry . instanceCount , instanceStart . count ) ;
85
97
86
98
const near = - camera . near
87
99
@@ -107,7 +119,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) {
107
119
108
120
_mvMatrix . multiplyMatrices ( camera . matrixWorldInverse , matrixWorld )
109
121
110
- for ( let i = 0 , l = instanceStart . count ; i < l ; i ++ ) {
122
+ for ( let i = 0 , l = segmentCount ; i < l ; i ++ ) {
111
123
_start4 . fromBufferAttribute ( instanceStart , i )
112
124
_end4 . fromBufferAttribute ( instanceEnd , i )
113
125
@@ -247,9 +259,6 @@ class LineSegments2 extends Mesh {
247
259
248
260
_lineWidth = material . linewidth + threshold
249
261
250
- _instanceStart = geometry . attributes . instanceStart
251
- _instanceEnd = geometry . attributes . instanceEnd
252
-
253
262
// check if we intersect the sphere bounds
254
263
if ( geometry . boundingSphere === null ) {
255
264
geometry . computeBoundingSphere ( )
@@ -300,6 +309,19 @@ class LineSegments2 extends Mesh {
300
309
raycastScreenSpace ( this , camera , intersects )
301
310
}
302
311
}
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
+ }
303
325
}
304
326
305
327
export { LineSegments2 }
0 commit comments