Line.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. raycast: ( function () {
  26. var inverseMatrix = new Matrix4();
  27. var ray = new Ray();
  28. var sphere = new Sphere();
  29. return function raycast( raycaster, intersects ) {
  30. var precision = raycaster.linePrecision;
  31. var precisionSq = precision * precision;
  32. var geometry = this.geometry;
  33. var matrixWorld = this.matrixWorld;
  34. // Checking boundingSphere distance to ray
  35. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  36. sphere.copy( geometry.boundingSphere );
  37. sphere.applyMatrix4( matrixWorld );
  38. if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;
  39. //
  40. inverseMatrix.getInverse( matrixWorld );
  41. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  42. var vStart = new Vector3();
  43. var vEnd = new Vector3();
  44. var interSegment = new Vector3();
  45. var interRay = new Vector3();
  46. var step = ( this && this.isLineSegments ) ? 2 : 1;
  47. if ( geometry.isBufferGeometry ) {
  48. var index = geometry.index;
  49. var attributes = geometry.attributes;
  50. var positions = attributes.position.array;
  51. if ( index !== null ) {
  52. var indices = index.array;
  53. for ( var i = 0, l = indices.length - 1; i < l; i += step ) {
  54. var a = indices[ i ];
  55. var b = indices[ i + 1 ];
  56. vStart.fromArray( positions, a * 3 );
  57. vEnd.fromArray( positions, b * 3 );
  58. var distSq = ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  59. if ( distSq > precisionSq ) continue;
  60. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  61. var distance = raycaster.ray.origin.distanceTo( interRay );
  62. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  63. intersects.push( {
  64. distance: distance,
  65. // What do we want? intersection point on the ray or on the segment??
  66. // point: raycaster.ray.at( distance ),
  67. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  68. index: i,
  69. face: null,
  70. faceIndex: null,
  71. object: this
  72. } );
  73. }
  74. } else {
  75. for ( var i = 0, l = positions.length / 3 - 1; i < l; i += step ) {
  76. vStart.fromArray( positions, 3 * i );
  77. vEnd.fromArray( positions, 3 * i + 3 );
  78. var distSq = ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  79. if ( distSq > precisionSq ) continue;
  80. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  81. var distance = raycaster.ray.origin.distanceTo( interRay );
  82. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  83. intersects.push( {
  84. distance: distance,
  85. // What do we want? intersection point on the ray or on the segment??
  86. // point: raycaster.ray.at( distance ),
  87. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  88. index: i,
  89. face: null,
  90. faceIndex: null,
  91. object: this
  92. } );
  93. }
  94. }
  95. } else if ( geometry.isGeometry ) {
  96. var vertices = geometry.vertices;
  97. var nbVertices = vertices.length;
  98. for ( var i = 0; i < nbVertices - 1; i += step ) {
  99. var distSq = ray.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
  100. if ( distSq > precisionSq ) continue;
  101. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  102. var distance = raycaster.ray.origin.distanceTo( interRay );
  103. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  104. intersects.push( {
  105. distance: distance,
  106. // What do we want? intersection point on the ray or on the segment??
  107. // point: raycaster.ray.at( distance ),
  108. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  109. index: i,
  110. face: null,
  111. faceIndex: null,
  112. object: this
  113. } );
  114. }
  115. }
  116. };
  117. }() ),
  118. clone: function () {
  119. return new this.constructor( this.geometry, this.material ).copy( this );
  120. }
  121. } );
  122. export { Line };