Line.js 5.0 KB

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