Line.js 5.1 KB

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