Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line: Honor last segment of LineLoop in raycast(). #28114

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
97 changes: 59 additions & 38 deletions src/objects/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const _end = /*@__PURE__*/ new Vector3();
const _inverseMatrix = /*@__PURE__*/ new Matrix4();
const _ray = /*@__PURE__*/ new Ray();
const _sphere = /*@__PURE__*/ new Sphere();
const _interRay = /*@__PURE__*/ new Vector3();
const _interSegment = /*@__PURE__*/ new Vector3();

class Line extends Object3D {

Expand Down Expand Up @@ -74,6 +76,38 @@ class Line extends Object3D {

}

_raycastSegment( iStart, iEnd, raycaster, ray, localThresholdSq ) {
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

const positionAttribute = this.geometry.attributes.position;

_start.fromBufferAttribute( positionAttribute, iStart );
_end.fromBufferAttribute( positionAttribute, iEnd );

const distSq = ray.distanceSqToSegment( _start, _end, _interRay, _interSegment );

if ( distSq > localThresholdSq ) return;

_interRay.applyMatrix4( this.matrixWorld ); // Move back to world space for distance calculation

const distance = raycaster.ray.origin.distanceTo( _interRay );

if ( distance < raycaster.near || distance > raycaster.far ) return;

return {

distance: distance,
// What do we want? intersection point on the ray or on the segment??
// point: raycaster.ray.at( distance ),
point: _interSegment.clone().applyMatrix4( this.matrixWorld ),
index: iStart,
face: null,
faceIndex: null,
object: this

};

}

raycast( raycaster, intersects ) {

const geometry = this.geometry;
Expand All @@ -99,10 +133,6 @@ class Line extends Object3D {
const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
const localThresholdSq = localThreshold * localThreshold;

const vStart = new Vector3();
const vEnd = new Vector3();
const interSegment = new Vector3();
const interRay = new Vector3();
const step = this.isLineSegments ? 2 : 1;

const index = geometry.index;
Expand All @@ -119,31 +149,28 @@ class Line extends Object3D {
const a = index.getX( i );
const b = index.getX( i + 1 );

vStart.fromBufferAttribute( positionAttribute, a );
vEnd.fromBufferAttribute( positionAttribute, b );
const intersect = this._raycastSegment( a, b, raycaster, _ray, localThresholdSq );

if ( intersect ) {

const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
intersects.push( intersect );

if ( distSq > localThresholdSq ) continue;
}

interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
}

const distance = raycaster.ray.origin.distanceTo( interRay );
if ( this.isLineLoop ) {

if ( distance < raycaster.near || distance > raycaster.far ) continue;
const a = index.getX( end - 1 );
const b = index.getX( start );

intersects.push( {
const intersect = this._raycastSegment( a, b, raycaster, _ray, localThresholdSq );

distance: distance,
// What do we want? intersection point on the ray or on the segment??
// point: raycaster.ray.at( distance ),
point: interSegment.clone().applyMatrix4( this.matrixWorld ),
index: i,
face: null,
faceIndex: null,
object: this
if ( intersect ) {

} );
intersects.push( intersect );

}

}

Expand All @@ -154,31 +181,25 @@ class Line extends Object3D {

for ( let i = start, l = end - 1; i < l; i += step ) {

vStart.fromBufferAttribute( positionAttribute, i );
vEnd.fromBufferAttribute( positionAttribute, i + 1 );
const intersect = this._raycastSegment( i, i + 1, raycaster, _ray, localThresholdSq );

const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
if ( intersect ) {

if ( distSq > localThresholdSq ) continue;
intersects.push( intersect );

interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
}

const distance = raycaster.ray.origin.distanceTo( interRay );
}

if ( distance < raycaster.near || distance > raycaster.far ) continue;
if ( this.isLineLoop ) {

intersects.push( {
const intersect = this._raycastSegment( end - 1, start, raycaster, _ray, localThresholdSq );

distance: distance,
// What do we want? intersection point on the ray or on the segment??
// point: raycaster.ray.at( distance ),
point: interSegment.clone().applyMatrix4( this.matrixWorld ),
index: i,
face: null,
faceIndex: null,
object: this
if ( intersect ) {

} );
intersects.push( intersect );

}

}

Expand Down