Line.js 6.2 KB

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