Line.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.Line = function ( geometry, material, type ) {
  5. THREE.Object3D.call( this );
  6. this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
  7. this.material = material !== undefined ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } );
  8. this.type = ( type !== undefined ) ? type : THREE.LineStrip;
  9. };
  10. THREE.LineStrip = 0;
  11. THREE.LinePieces = 1;
  12. THREE.Line.prototype = Object.create( THREE.Object3D.prototype );
  13. THREE.Line.prototype.raycast = ( function () {
  14. var inverseMatrix = new THREE.Matrix4();
  15. var ray = new THREE.Ray();
  16. var sphere = new THREE.Sphere();
  17. return function ( raycaster, intersects ) {
  18. var precision = raycaster.linePrecision;
  19. var precisionSq = precision * precision;
  20. var geometry = this.geometry;
  21. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  22. // Checking boundingSphere distance to ray
  23. sphere.copy( geometry.boundingSphere );
  24. sphere.applyMatrix4( this.matrixWorld );
  25. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  26. return;
  27. }
  28. inverseMatrix.getInverse( this.matrixWorld );
  29. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  30. /* if ( geometry instanceof THREE.BufferGeometry ) {
  31. } else */ if ( geometry instanceof THREE.Geometry ) {
  32. var vertices = geometry.vertices;
  33. var nbVertices = vertices.length;
  34. var interSegment = new THREE.Vector3();
  35. var interRay = new THREE.Vector3();
  36. var step = this.type === THREE.LineStrip ? 1 : 2;
  37. for ( var i = 0; i < nbVertices - 1; i = i + step ) {
  38. var distSq = ray.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
  39. if ( distSq > precisionSq ) continue;
  40. var distance = ray.origin.distanceTo( interRay );
  41. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  42. intersects.push( {
  43. distance: distance,
  44. // What do we want? intersection point on the ray or on the segment??
  45. // point: raycaster.ray.at( distance ),
  46. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  47. face: null,
  48. faceIndex: null,
  49. object: this
  50. } );
  51. }
  52. }
  53. };
  54. }() );
  55. THREE.Line.prototype.clone = function ( object ) {
  56. if ( object === undefined ) object = new THREE.Line( this.geometry, this.material, this.type );
  57. THREE.Object3D.prototype.clone.call( this, object );
  58. return object;
  59. };