Skip to content

Commit d89e3c3

Browse files
authored
feat: remove need for manually assigning Line2 material resolution (#376)
1 parent 40a4d33 commit d89e3c3

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

src/lines/LineMaterial.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* dashSize: <float>,
88
* dashOffset: <float>,
99
* gapSize: <float>,
10-
* resolution: <Vector2>, // to be set by renderer
1110
* }
1211
*/
1312

src/lines/LineSegments2.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry'
1414
import { LineMaterial } from '../lines/LineMaterial'
1515

16+
const _viewport = new Vector4();
17+
1618
const _start = new Vector3()
1719
const _end = new Vector3()
1820

@@ -29,7 +31,7 @@ const _box = new Box3()
2931
const _sphere = new Sphere()
3032
const _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

5052
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);
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

305327
export { LineSegments2 }

src/lines/Wireframe.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { InstancedInterleavedBuffer, InterleavedBufferAttribute, Mesh, Vector3 } from 'three'
1+
import { InstancedInterleavedBuffer, InterleavedBufferAttribute, Mesh, Vector3, Vector4 } from 'three'
22
import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry'
33
import { LineMaterial } from '../lines/LineMaterial'
44

55
const _start = new Vector3()
66
const _end = new Vector3()
7+
const _viewport = new Vector4();
78

89
class Wireframe extends Mesh {
910
constructor(geometry = new LineSegmentsGeometry(), material = new LineMaterial({ color: Math.random() * 0xffffff })) {
@@ -38,6 +39,19 @@ class Wireframe extends Mesh {
3839

3940
return this
4041
}
42+
43+
onBeforeRender(renderer) {
44+
45+
const uniforms = this.material.uniforms;
46+
47+
if (uniforms && uniforms.resolution) {
48+
49+
renderer.getViewport(_viewport);
50+
this.material.uniforms.resolution.value.set(_viewport.z, _viewport.w);
51+
52+
}
53+
54+
}
4155
}
4256

4357
export { Wireframe }

0 commit comments

Comments
 (0)