From 28804c5a634946b9fba6d463c433eca3e15feed1 Mon Sep 17 00:00:00 2001 From: manthrax Date: Wed, 16 Sep 2020 18:57:34 -0700 Subject: [PATCH 1/3] Update Line.js Correctly compute stride and offset for reading vertices from InterleavedBufferAttribute --- src/objects/Line.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/objects/Line.js b/src/objects/Line.js index 40d52d76c30b2c..97844b7f1f1e62 100644 --- a/src/objects/Line.js +++ b/src/objects/Line.js @@ -136,6 +136,9 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), { const attributes = geometry.attributes; const positions = attributes.position.array; + const stride = attributes.position.isInterleavedBufferAttribute ? attributes.position.data.stride : 3; + const offset = attributes.position.isInterleavedBufferAttribute ? attributes.position.offset : 0; + if ( index !== null ) { const indices = index.array; @@ -145,8 +148,8 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), { const a = indices[ i ]; const b = indices[ i + 1 ]; - vStart.fromArray( positions, a * 3 ); - vEnd.fromArray( positions, b * 3 ); + vStart.fromArray( positions, a * stride + offset ); + vEnd.fromArray( positions, b * stride + offset); const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment ); @@ -177,8 +180,8 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), { for ( let i = 0, l = positions.length / 3 - 1; i < l; i += step ) { - vStart.fromArray( positions, 3 * i ); - vEnd.fromArray( positions, 3 * i + 3 ); + vStart.fromArray( positions, i * stride + offset); + vEnd.fromArray( positions, ((i+1) * stride) + offset ); const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment ); From 75b3ec18b4f15723cc1d539d01ca02c401224df8 Mon Sep 17 00:00:00 2001 From: manthrax Date: Wed, 16 Sep 2020 19:06:12 -0700 Subject: [PATCH 2/3] Update Line.js Fix lint errors. --- src/objects/Line.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/objects/Line.js b/src/objects/Line.js index 97844b7f1f1e62..19a1925940b86f 100644 --- a/src/objects/Line.js +++ b/src/objects/Line.js @@ -149,7 +149,7 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), { const b = indices[ i + 1 ]; vStart.fromArray( positions, a * stride + offset ); - vEnd.fromArray( positions, b * stride + offset); + vEnd.fromArray( positions, b * stride + offset ); const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment ); @@ -180,8 +180,8 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), { for ( let i = 0, l = positions.length / 3 - 1; i < l; i += step ) { - vStart.fromArray( positions, i * stride + offset); - vEnd.fromArray( positions, ((i+1) * stride) + offset ); + vStart.fromArray( positions, i * stride + offset ); + vEnd.fromArray( positions, ( ( i + 1 ) * stride ) + offset ); const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment ); From 9fb4bb744c7fba071df69cacdb25acbfcad3a971 Mon Sep 17 00:00:00 2001 From: manthrax Date: Thu, 17 Sep 2020 14:54:56 -0700 Subject: [PATCH 3/3] Switch vertex access to fromBufferAttribute.. remove redundant null check. --- src/objects/Line.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/objects/Line.js b/src/objects/Line.js index 19a1925940b86f..ba4b7bea86b7eb 100644 --- a/src/objects/Line.js +++ b/src/objects/Line.js @@ -128,16 +128,13 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), { const vEnd = new Vector3(); const interSegment = new Vector3(); const interRay = new Vector3(); - const step = ( this && this.isLineSegments ) ? 2 : 1; + const step = this.isLineSegments ? 2 : 1; if ( geometry.isBufferGeometry ) { const index = geometry.index; const attributes = geometry.attributes; - const positions = attributes.position.array; - - const stride = attributes.position.isInterleavedBufferAttribute ? attributes.position.data.stride : 3; - const offset = attributes.position.isInterleavedBufferAttribute ? attributes.position.offset : 0; + const positionAttr = attributes.position; if ( index !== null ) { @@ -148,8 +145,8 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), { const a = indices[ i ]; const b = indices[ i + 1 ]; - vStart.fromArray( positions, a * stride + offset ); - vEnd.fromArray( positions, b * stride + offset ); + vStart.fromBufferAttribute( positionAttr, a ); + vEnd.fromBufferAttribute( positionAttr, b ); const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment ); @@ -178,10 +175,10 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), { } else { - for ( let i = 0, l = positions.length / 3 - 1; i < l; i += step ) { + for ( let i = 0, l = positionAttr.count - 1; i < l; i += step ) { - vStart.fromArray( positions, i * stride + offset ); - vEnd.fromArray( positions, ( ( i + 1 ) * stride ) + offset ); + vStart.fromBufferAttribute( positionAttr, i ); + vEnd.fromBufferAttribute( positionAttr, i + 1 ); const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );