Line.js 6.2 KB

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