|
@@ -11,6 +11,9 @@ import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
|
|
* @author mrdoob / http://mrdoob.com/
|
|
|
*/
|
|
|
|
|
|
+var _start, _end;
|
|
|
+var _inverseMatrix, _ray, _sphere;
|
|
|
+
|
|
|
function Line( geometry, material, mode ) {
|
|
|
|
|
|
if ( mode === 1 ) {
|
|
@@ -34,186 +37,154 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), {
|
|
|
|
|
|
isLine: true,
|
|
|
|
|
|
- computeLineDistances: ( function () {
|
|
|
-
|
|
|
- var start = new Vector3();
|
|
|
- var end = new Vector3();
|
|
|
-
|
|
|
- return function computeLineDistances() {
|
|
|
-
|
|
|
- var geometry = this.geometry;
|
|
|
-
|
|
|
- if ( geometry.isBufferGeometry ) {
|
|
|
-
|
|
|
- // we assume non-indexed geometry
|
|
|
+ computeLineDistances: function () {
|
|
|
|
|
|
- if ( geometry.index === null ) {
|
|
|
+ if ( _end === undefined ) {
|
|
|
|
|
|
- var positionAttribute = geometry.attributes.position;
|
|
|
- var lineDistances = [ 0 ];
|
|
|
+ _start = new Vector3();
|
|
|
+ _end = new Vector3();
|
|
|
|
|
|
- for ( var i = 1, l = positionAttribute.count; i < l; i ++ ) {
|
|
|
+ }
|
|
|
|
|
|
- start.fromBufferAttribute( positionAttribute, i - 1 );
|
|
|
- end.fromBufferAttribute( positionAttribute, i );
|
|
|
+ var geometry = this.geometry;
|
|
|
|
|
|
- lineDistances[ i ] = lineDistances[ i - 1 ];
|
|
|
- lineDistances[ i ] += start.distanceTo( end );
|
|
|
+ if ( geometry.isBufferGeometry ) {
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- geometry.addAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
|
|
|
-
|
|
|
- }
|
|
|
+ // we assume non-indexed geometry
|
|
|
|
|
|
- } else if ( geometry.isGeometry ) {
|
|
|
+ if ( geometry.index === null ) {
|
|
|
|
|
|
- var vertices = geometry.vertices;
|
|
|
- var lineDistances = geometry.lineDistances;
|
|
|
+ var positionAttribute = geometry.attributes.position;
|
|
|
+ var lineDistances = [ 0 ];
|
|
|
|
|
|
- lineDistances[ 0 ] = 0;
|
|
|
+ for ( var i = 1, l = positionAttribute.count; i < l; i ++ ) {
|
|
|
|
|
|
- for ( var i = 1, l = vertices.length; i < l; i ++ ) {
|
|
|
+ _start.fromBufferAttribute( positionAttribute, i - 1 );
|
|
|
+ _end.fromBufferAttribute( positionAttribute, i );
|
|
|
|
|
|
lineDistances[ i ] = lineDistances[ i - 1 ];
|
|
|
- lineDistances[ i ] += vertices[ i - 1 ].distanceTo( vertices[ i ] );
|
|
|
+ lineDistances[ i ] += _start.distanceTo( _end );
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- return this;
|
|
|
-
|
|
|
- };
|
|
|
+ geometry.addAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
|
|
|
|
|
|
- }() ),
|
|
|
+ } else {
|
|
|
|
|
|
- raycast: ( function () {
|
|
|
+ console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
|
|
|
|
|
|
- var inverseMatrix = new Matrix4();
|
|
|
- var ray = new Ray();
|
|
|
- var sphere = new Sphere();
|
|
|
-
|
|
|
- return function raycast( raycaster, intersects ) {
|
|
|
+ }
|
|
|
|
|
|
- var precision = raycaster.linePrecision;
|
|
|
+ } else if ( geometry.isGeometry ) {
|
|
|
|
|
|
- var geometry = this.geometry;
|
|
|
- var matrixWorld = this.matrixWorld;
|
|
|
+ var vertices = geometry.vertices;
|
|
|
+ var lineDistances = geometry.lineDistances;
|
|
|
|
|
|
- // Checking boundingSphere distance to ray
|
|
|
+ lineDistances[ 0 ] = 0;
|
|
|
|
|
|
- if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
|
|
|
+ for ( var i = 1, l = vertices.length; i < l; i ++ ) {
|
|
|
|
|
|
- sphere.copy( geometry.boundingSphere );
|
|
|
- sphere.applyMatrix4( matrixWorld );
|
|
|
- sphere.radius += precision;
|
|
|
+ lineDistances[ i ] = lineDistances[ i - 1 ];
|
|
|
+ lineDistances[ i ] += vertices[ i - 1 ].distanceTo( vertices[ i ] );
|
|
|
|
|
|
- if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;
|
|
|
+ }
|
|
|
|
|
|
- //
|
|
|
+ }
|
|
|
|
|
|
- inverseMatrix.getInverse( matrixWorld );
|
|
|
- ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
|
|
|
+ return this;
|
|
|
|
|
|
- var localPrecision = precision / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
|
|
|
- var localPrecisionSq = localPrecision * localPrecision;
|
|
|
+ },
|
|
|
|
|
|
- var vStart = new Vector3();
|
|
|
- var vEnd = new Vector3();
|
|
|
- var interSegment = new Vector3();
|
|
|
- var interRay = new Vector3();
|
|
|
- var step = ( this && this.isLineSegments ) ? 2 : 1;
|
|
|
+ raycast: function ( raycaster, intersects ) {
|
|
|
|
|
|
- if ( geometry.isBufferGeometry ) {
|
|
|
+ if ( _sphere === undefined ) {
|
|
|
|
|
|
- var index = geometry.index;
|
|
|
- var attributes = geometry.attributes;
|
|
|
- var positions = attributes.position.array;
|
|
|
+ _inverseMatrix = new Matrix4();
|
|
|
+ _ray = new Ray();
|
|
|
+ _sphere = new Sphere();
|
|
|
|
|
|
- if ( index !== null ) {
|
|
|
+ }
|
|
|
|
|
|
- var indices = index.array;
|
|
|
+ var precision = raycaster.linePrecision;
|
|
|
|
|
|
- for ( var i = 0, l = indices.length - 1; i < l; i += step ) {
|
|
|
+ var geometry = this.geometry;
|
|
|
+ var matrixWorld = this.matrixWorld;
|
|
|
|
|
|
- var a = indices[ i ];
|
|
|
- var b = indices[ i + 1 ];
|
|
|
+ // Checking boundingSphere distance to ray
|
|
|
|
|
|
- vStart.fromArray( positions, a * 3 );
|
|
|
- vEnd.fromArray( positions, b * 3 );
|
|
|
+ if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
|
|
|
|
|
|
- var distSq = ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
|
|
|
+ _sphere.copy( geometry.boundingSphere );
|
|
|
+ _sphere.applyMatrix4( matrixWorld );
|
|
|
+ _sphere.radius += precision;
|
|
|
|
|
|
- if ( distSq > localPrecisionSq ) continue;
|
|
|
+ if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
|
|
|
|
|
|
- interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
|
|
|
+ //
|
|
|
|
|
|
- var distance = raycaster.ray.origin.distanceTo( interRay );
|
|
|
+ _inverseMatrix.getInverse( matrixWorld );
|
|
|
+ _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
|
|
|
|
|
|
- if ( distance < raycaster.near || distance > raycaster.far ) continue;
|
|
|
+ var localPrecision = precision / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
|
|
|
+ var localPrecisionSq = localPrecision * localPrecision;
|
|
|
|
|
|
- intersects.push( {
|
|
|
+ var vStart = new Vector3();
|
|
|
+ var vEnd = new Vector3();
|
|
|
+ var interSegment = new Vector3();
|
|
|
+ var interRay = new Vector3();
|
|
|
+ var step = ( this && this.isLineSegments ) ? 2 : 1;
|
|
|
|
|
|
- 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 ( geometry.isBufferGeometry ) {
|
|
|
|
|
|
- } );
|
|
|
+ var index = geometry.index;
|
|
|
+ var attributes = geometry.attributes;
|
|
|
+ var positions = attributes.position.array;
|
|
|
|
|
|
- }
|
|
|
+ if ( index !== null ) {
|
|
|
|
|
|
- } else {
|
|
|
+ var indices = index.array;
|
|
|
|
|
|
- for ( var i = 0, l = positions.length / 3 - 1; i < l; i += step ) {
|
|
|
+ for ( var i = 0, l = indices.length - 1; i < l; i += step ) {
|
|
|
|
|
|
- vStart.fromArray( positions, 3 * i );
|
|
|
- vEnd.fromArray( positions, 3 * i + 3 );
|
|
|
+ var a = indices[ i ];
|
|
|
+ var b = indices[ i + 1 ];
|
|
|
|
|
|
- var distSq = ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
|
|
|
+ vStart.fromArray( positions, a * 3 );
|
|
|
+ vEnd.fromArray( positions, b * 3 );
|
|
|
|
|
|
- if ( distSq > localPrecisionSq ) continue;
|
|
|
+ var distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
|
|
|
|
|
|
- interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
|
|
|
+ if ( distSq > localPrecisionSq ) continue;
|
|
|
|
|
|
- var distance = raycaster.ray.origin.distanceTo( interRay );
|
|
|
+ interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
|
|
|
|
|
|
- if ( distance < raycaster.near || distance > raycaster.far ) continue;
|
|
|
+ var distance = raycaster.ray.origin.distanceTo( interRay );
|
|
|
|
|
|
- intersects.push( {
|
|
|
+ if ( distance < raycaster.near || distance > raycaster.far ) continue;
|
|
|
|
|
|
- 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
|
|
|
+ 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 ),
|
|
|
+ index: i,
|
|
|
+ face: null,
|
|
|
+ faceIndex: null,
|
|
|
+ object: this
|
|
|
|
|
|
- }
|
|
|
+ } );
|
|
|
|
|
|
}
|
|
|
|
|
|
- } else if ( geometry.isGeometry ) {
|
|
|
+ } else {
|
|
|
|
|
|
- var vertices = geometry.vertices;
|
|
|
- var nbVertices = vertices.length;
|
|
|
+ for ( var i = 0, l = positions.length / 3 - 1; i < l; i += step ) {
|
|
|
|
|
|
- for ( var i = 0; i < nbVertices - 1; i += step ) {
|
|
|
+ vStart.fromArray( positions, 3 * i );
|
|
|
+ vEnd.fromArray( positions, 3 * i + 3 );
|
|
|
|
|
|
- var distSq = ray.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
|
|
|
+ var distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
|
|
|
|
|
|
if ( distSq > localPrecisionSq ) continue;
|
|
|
|
|
@@ -240,9 +211,41 @@ Line.prototype = Object.assign( Object.create( Object3D.prototype ), {
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
+ } else if ( geometry.isGeometry ) {
|
|
|
+
|
|
|
+ var vertices = geometry.vertices;
|
|
|
+ var nbVertices = vertices.length;
|
|
|
+
|
|
|
+ for ( var i = 0; i < nbVertices - 1; i += step ) {
|
|
|
+
|
|
|
+ var distSq = _ray.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], 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 ),
|
|
|
+ index: i,
|
|
|
+ face: null,
|
|
|
+ faceIndex: null,
|
|
|
+ object: this
|
|
|
+
|
|
|
+ } );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- }() ),
|
|
|
+ },
|
|
|
|
|
|
clone: function () {
|
|
|
|