Line.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { Sphere } from '../math/Sphere.js';
  2. import { Ray } from '../math/Ray.js';
  3. import { Matrix4 } from '../math/Matrix4.js';
  4. import { Object3D } from '../core/Object3D.js';
  5. import { Vector3 } from '../math/Vector3.js';
  6. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  7. import { BufferGeometry } from '../core/BufferGeometry.js';
  8. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  9. /**
  10. * @author mrdoob / http://mrdoob.com/
  11. */
  12. var _start = new Vector3();
  13. var _end = new Vector3();
  14. var _inverseMatrix = new Matrix4();
  15. var _ray = new Ray();
  16. var _sphere = new Sphere();
  17. function Line( geometry, material, mode ) {
  18. if ( mode === 1 ) {
  19. console.error( 'THREE.Line: parameter THREE.LinePieces no longer supported. Use THREE.LineSegments instead.' );
  20. }
  21. Object3D.call( this );
  22. this.type = 'Line';
  23. this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
  24. this.material = material !== undefined ? material : new LineBasicMaterial();
  25. }
  26. Line.prototype = Object.assign( Object.create( Object3D.prototype ), {
  27. constructor: Line,
  28. isLine: true,
  29. computeLineDistances: function () {
  30. var geometry = this.geometry;
  31. if ( geometry.isBufferGeometry ) {
  32. // we assume non-indexed geometry
  33. if ( geometry.index === null ) {
  34. var positionAttribute = geometry.attributes.position;
  35. var lineDistances = [ 0 ];
  36. for ( var i = 1, l = positionAttribute.count; i < l; i ++ ) {
  37. _start.fromBufferAttribute( positionAttribute, i - 1 );
  38. _end.fromBufferAttribute( positionAttribute, i );
  39. lineDistances[ i ] = lineDistances[ i - 1 ];
  40. lineDistances[ i ] += _start.distanceTo( _end );
  41. }
  42. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  43. } else {
  44. console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  45. }
  46. } else if ( geometry.isGeometry ) {
  47. var vertices = geometry.vertices;
  48. var lineDistances = geometry.lineDistances;
  49. lineDistances[ 0 ] = 0;
  50. for ( var i = 1, l = vertices.length; i < l; i ++ ) {
  51. lineDistances[ i ] = lineDistances[ i - 1 ];
  52. lineDistances[ i ] += vertices[ i - 1 ].distanceTo( vertices[ i ] );
  53. }
  54. }
  55. return this;
  56. },
  57. raycast: function ( raycaster, intersects ) {
  58. var precision = raycaster.linePrecision;
  59. var geometry = this.geometry;
  60. var matrixWorld = this.matrixWorld;
  61. // Checking boundingSphere distance to ray
  62. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  63. _sphere.copy( geometry.boundingSphere );
  64. _sphere.applyMatrix4( matrixWorld );
  65. _sphere.radius += precision;
  66. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  67. //
  68. _inverseMatrix.getInverse( matrixWorld );
  69. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  70. var localPrecision = precision / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  71. var localPrecisionSq = localPrecision * localPrecision;
  72. var vStart = new Vector3();
  73. var vEnd = new Vector3();
  74. var interSegment = new Vector3();
  75. var interRay = new Vector3();
  76. var step = ( this && this.isLineSegments ) ? 2 : 1;
  77. if ( geometry.isBufferGeometry ) {
  78. var index = geometry.index;
  79. var attributes = geometry.attributes;
  80. var positions = attributes.position.array;
  81. if ( index !== null ) {
  82. var indices = index.array;
  83. for ( var i = 0, l = indices.length - 1; i < l; i += step ) {
  84. var a = indices[ i ];
  85. var b = indices[ i + 1 ];
  86. vStart.fromArray( positions, a * 3 );
  87. vEnd.fromArray( positions, b * 3 );
  88. var distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  89. if ( distSq > localPrecisionSq ) continue;
  90. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  91. var distance = raycaster.ray.origin.distanceTo( interRay );
  92. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  93. intersects.push( {
  94. distance: distance,
  95. // What do we want? intersection point on the ray or on the segment??
  96. // point: raycaster.ray.at( distance ),
  97. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  98. index: i,
  99. face: null,
  100. faceIndex: null,
  101. object: this
  102. } );
  103. }
  104. } else {
  105. for ( var i = 0, l = positions.length / 3 - 1; i < l; i += step ) {
  106. vStart.fromArray( positions, 3 * i );
  107. vEnd.fromArray( positions, 3 * i + 3 );
  108. var distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  109. if ( distSq > localPrecisionSq ) continue;
  110. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  111. var distance = raycaster.ray.origin.distanceTo( interRay );
  112. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  113. intersects.push( {
  114. distance: distance,
  115. // What do we want? intersection point on the ray or on the segment??
  116. // point: raycaster.ray.at( distance ),
  117. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  118. index: i,
  119. face: null,
  120. faceIndex: null,
  121. object: this
  122. } );
  123. }
  124. }
  125. } else if ( geometry.isGeometry ) {
  126. var vertices = geometry.vertices;
  127. var nbVertices = vertices.length;
  128. for ( var i = 0; i < nbVertices - 1; i += step ) {
  129. var distSq = _ray.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
  130. if ( distSq > localPrecisionSq ) continue;
  131. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  132. var distance = raycaster.ray.origin.distanceTo( interRay );
  133. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  134. intersects.push( {
  135. distance: distance,
  136. // What do we want? intersection point on the ray or on the segment??
  137. // point: raycaster.ray.at( distance ),
  138. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  139. index: i,
  140. face: null,
  141. faceIndex: null,
  142. object: this
  143. } );
  144. }
  145. }
  146. },
  147. clone: function () {
  148. return new this.constructor( this.geometry, this.material ).copy( this );
  149. }
  150. } );
  151. export { Line };