From 3265aba19f0194c8d4491054e4574f3c327b3e2c Mon Sep 17 00:00:00 2001 From: rodri Date: Mon, 5 Aug 2024 13:45:08 +0100 Subject: [PATCH 1/3] feat: Remove need for manually assigning Line2 material resolution https://github.com/mrdoob/three.js/pull/28343 --- src/lines/LineMaterial.js | 1 - src/lines/LineSegments2.js | 15 +++++++++++++++ src/lines/Wireframe.js | 16 +++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/lines/LineMaterial.js b/src/lines/LineMaterial.js index 728beb17..3c4959de 100644 --- a/src/lines/LineMaterial.js +++ b/src/lines/LineMaterial.js @@ -7,7 +7,6 @@ * dashSize: , * dashOffset: , * gapSize: , - * resolution: , // to be set by renderer * } */ diff --git a/src/lines/LineSegments2.js b/src/lines/LineSegments2.js index f9feecf0..987eec6f 100644 --- a/src/lines/LineSegments2.js +++ b/src/lines/LineSegments2.js @@ -13,6 +13,8 @@ import { import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry' import { LineMaterial } from '../lines/LineMaterial' +const _viewport = new Vector4(); + const _start = new Vector3() const _end = new Vector3() @@ -300,6 +302,19 @@ class LineSegments2 extends Mesh { raycastScreenSpace(this, camera, intersects) } } + + onBeforeRender(renderer) { + + const uniforms = this.material.uniforms; + + if (uniforms && uniforms.resolution) { + + renderer.getViewport(_viewport); + this.material.uniforms.resolution.value.set(_viewport.z, _viewport.w); + + } + + } } export { LineSegments2 } diff --git a/src/lines/Wireframe.js b/src/lines/Wireframe.js index 2e193dd4..e11e0b7a 100644 --- a/src/lines/Wireframe.js +++ b/src/lines/Wireframe.js @@ -1,9 +1,10 @@ -import { InstancedInterleavedBuffer, InterleavedBufferAttribute, Mesh, Vector3 } from 'three' +import { InstancedInterleavedBuffer, InterleavedBufferAttribute, Mesh, Vector3, Vector4 } from 'three' import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry' import { LineMaterial } from '../lines/LineMaterial' const _start = new Vector3() const _end = new Vector3() +const _viewport = new Vector4(); class Wireframe extends Mesh { constructor(geometry = new LineSegmentsGeometry(), material = new LineMaterial({ color: Math.random() * 0xffffff })) { @@ -38,6 +39,19 @@ class Wireframe extends Mesh { return this } + + onBeforeRender( renderer ) { + + const uniforms = this.material.uniforms; + + if ( uniforms && uniforms.resolution ) { + + renderer.getViewport( _viewport ); + this.material.uniforms.resolution.value.set( _viewport.z, _viewport.w ); + + } + + } } export { Wireframe } From 0525dad7577ea9ba3162a4a50991e6ba39e29916 Mon Sep 17 00:00:00 2001 From: rodri Date: Mon, 5 Aug 2024 13:59:59 +0100 Subject: [PATCH 2/3] fix: Line2 raytracing when instanceCount set https://github.com/mrdoob/three.js/pull/25032 --- src/lines/LineSegments2.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/lines/LineSegments2.js b/src/lines/LineSegments2.js index 987eec6f..e3738a40 100644 --- a/src/lines/LineSegments2.js +++ b/src/lines/LineSegments2.js @@ -31,7 +31,7 @@ const _box = new Box3() const _sphere = new Sphere() const _clipToWorldVector = new Vector4() -let _ray, _instanceStart, _instanceEnd, _lineWidth +let _ray, _lineWidth // Returns the margin required to expand by in world space given the distance from the camera, // line width, resolution, and camera projection @@ -50,9 +50,18 @@ function getWorldSpaceHalfWidth(camera, distance, resolution) { } function raycastWorldUnits(lineSegments, intersects) { - for (let i = 0, l = _instanceStart.count; i < l; i++) { - _line.start.fromBufferAttribute(_instanceStart, i) - _line.end.fromBufferAttribute(_instanceEnd, i) + + const matrixWorld = lineSegments.matrixWorld; + const geometry = lineSegments.geometry; + const instanceStart = geometry.attributes.instanceStart; + const instanceEnd = geometry.attributes.instanceEnd; + const segmentCount = Math.min(geometry.instanceCount, instanceStart.count); + + for (let i = 0, l = segmentCount; i < l; i++) { + _line.start.fromBufferAttribute(instanceStart, i) + _line.end.fromBufferAttribute(instanceEnd, i) + + _line.applyMatrix4(matrixWorld); const pointOnLine = new Vector3() const point = new Vector3() @@ -84,6 +93,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) { const geometry = lineSegments.geometry const instanceStart = geometry.attributes.instanceStart const instanceEnd = geometry.attributes.instanceEnd + const segmentCount = Math.min( geometry.instanceCount, instanceStart.count ); const near = -camera.near @@ -109,7 +119,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) { _mvMatrix.multiplyMatrices(camera.matrixWorldInverse, matrixWorld) - for (let i = 0, l = instanceStart.count; i < l; i++) { + for (let i = 0, l = segmentCount; i < l; i++) { _start4.fromBufferAttribute(instanceStart, i) _end4.fromBufferAttribute(instanceEnd, i) @@ -249,9 +259,6 @@ class LineSegments2 extends Mesh { _lineWidth = material.linewidth + threshold - _instanceStart = geometry.attributes.instanceStart - _instanceEnd = geometry.attributes.instanceEnd - // check if we intersect the sphere bounds if (geometry.boundingSphere === null) { geometry.computeBoundingSphere() From 334dcde1c980275a4892484845ca254935d39d0b Mon Sep 17 00:00:00 2001 From: rodri Date: Mon, 5 Aug 2024 14:12:44 +0100 Subject: [PATCH 3/3] format --- src/lines/LineSegments2.js | 14 +++++++------- src/lines/Wireframe.js | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/lines/LineSegments2.js b/src/lines/LineSegments2.js index e3738a40..71ac7b21 100644 --- a/src/lines/LineSegments2.js +++ b/src/lines/LineSegments2.js @@ -51,17 +51,17 @@ function getWorldSpaceHalfWidth(camera, distance, resolution) { function raycastWorldUnits(lineSegments, intersects) { - const matrixWorld = lineSegments.matrixWorld; - const geometry = lineSegments.geometry; - const instanceStart = geometry.attributes.instanceStart; - const instanceEnd = geometry.attributes.instanceEnd; - const segmentCount = Math.min(geometry.instanceCount, instanceStart.count); + const matrixWorld = lineSegments.matrixWorld; + const geometry = lineSegments.geometry; + const instanceStart = geometry.attributes.instanceStart; + const instanceEnd = geometry.attributes.instanceEnd; + const segmentCount = Math.min(geometry.instanceCount, instanceStart.count); for (let i = 0, l = segmentCount; i < l; i++) { _line.start.fromBufferAttribute(instanceStart, i) _line.end.fromBufferAttribute(instanceEnd, i) - _line.applyMatrix4(matrixWorld); + _line.applyMatrix4(matrixWorld); const pointOnLine = new Vector3() const point = new Vector3() @@ -93,7 +93,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) { const geometry = lineSegments.geometry const instanceStart = geometry.attributes.instanceStart const instanceEnd = geometry.attributes.instanceEnd - const segmentCount = Math.min( geometry.instanceCount, instanceStart.count ); + const segmentCount = Math.min(geometry.instanceCount, instanceStart.count); const near = -camera.near diff --git a/src/lines/Wireframe.js b/src/lines/Wireframe.js index e11e0b7a..5c3bfe87 100644 --- a/src/lines/Wireframe.js +++ b/src/lines/Wireframe.js @@ -40,18 +40,18 @@ class Wireframe extends Mesh { return this } - onBeforeRender( renderer ) { + onBeforeRender(renderer) { - const uniforms = this.material.uniforms; + const uniforms = this.material.uniforms; - if ( uniforms && uniforms.resolution ) { + if (uniforms && uniforms.resolution) { - renderer.getViewport( _viewport ); - this.material.uniforms.resolution.value.set( _viewport.z, _viewport.w ); + renderer.getViewport(_viewport); + this.material.uniforms.resolution.value.set(_viewport.z, _viewport.w); - } + } - } + } } export { Wireframe }