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

Nlines #17160

Closed
wants to merge 13 commits into from
Closed

Nlines #17160

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion examples/js/lines/LineSegments2.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,86 @@ THREE.LineSegments2.prototype = Object.assign( Object.create( THREE.Mesh.prototy

return this;

}
},

raycast: ( function () {

var inverseMatrix = new THREE.Matrix4();
var ray = new THREE.Ray();
var sphere = new THREE.Sphere();

return function raycast( raycaster, intersects ) {

var precision = raycaster.linePrecision;

// If using a persepective-line material, there is a known precision:
// This doesn't work correctly though; the collision distance turns out too large.
if(this.material && this.material.worldlinewidth) precision = this.material.worldlinewidth;

var geometry = this.geometry;
var matrixWorld = this.matrixWorld;

// Checking boundingSphere distance to ray

if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
sphere.copy( geometry.boundingSphere );
sphere.applyMatrix4( matrixWorld );
sphere.radius += precision;

if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;

inverseMatrix.getInverse( matrixWorld );
ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );

// This was in the orignal Line.js raycast code, but it's correctness is debatable. Line objects
// have infintesimal width, so exapnding the size of the object should not exapand the width of the line.
// var localPrecision = precision / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
//var localPrecisionSq = localPrecision * localPrecision;

var localPrecisionSq = precision * precision;


var vStart = new Vector3();
var vEnd = new Vector3();
var interSegment = new Vector3();
var interRay = new Vector3();

// Currently, the geometry is always a LineSegments2 geometry, which uses the instanceStart/instanceEnd to store segment locations
var starts = geometry.attributes.instanceStart;
var ends = geometry.attributes.instanceEnd;

for ( var i = 0; i < starts.count; i ++ ) {

vStart.fromArray( starts.data.array, i * starts.data.stride + starts.offset );
vEnd.fromArray( ends.data.array, i * ends.data.stride + ends.offset );
var distSq = ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );

if ( distSq > localPrecisionSq ) continue;

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

var distance = raycaster.ray.origin.distanceTo( interRay );

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

intersects.push( {

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 ),
transverseDistance: Math.sqrt(distSq), // Special value indicating the distance of interesection from the ray
index: i,
face: null,
faceIndex: null,
object: this
} );


}

};

}() )

} );
Loading