Line3.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Line3 = function ( start, end ) {
  5. this.start = ( start !== undefined ) ? start : new THREE.Vector3();
  6. this.end = ( end !== undefined ) ? end : new THREE.Vector3();
  7. };
  8. THREE.extend( THREE.Line3.prototype, {
  9. set: function ( start, end ) {
  10. this.start.copy( start );
  11. this.end.copy( end );
  12. return this;
  13. },
  14. copy: function ( line ) {
  15. this.start.copy( line.start );
  16. this.end.copy( line.end );
  17. return this;
  18. },
  19. center: function ( optionalTarget ) {
  20. var result = optionalTarget || new THREE.Vector3();
  21. return result.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  22. },
  23. delta: function ( optionalTarget ) {
  24. var result = optionalTarget || new THREE.Vector3();
  25. return result.subVectors( this.end, this.start );
  26. },
  27. distanceSq: function () {
  28. return this.start.distanceToSquared( this.end );
  29. },
  30. distance: function () {
  31. return this.start.distanceTo( this.end );
  32. },
  33. at: function ( t, optionalTarget ) {
  34. var result = optionalTarget || new THREE.Vector3();
  35. return this.delta( result ).multiplyScalar( t ).add( this.start );
  36. },
  37. closestPointToPointParameter: function() {
  38. var startP = new THREE.Vector3();
  39. var startEnd = new THREE.Vector3();
  40. return function ( point, clampToLine ) {
  41. startP.subVectors( point, this.start );
  42. startEnd.subVectors( this.end, this.start );
  43. var startEnd2 = startEnd.dot( startEnd );
  44. var startEnd_startP = startEnd.dot( startP );
  45. var t = startEnd_startP / startEnd2;
  46. if( clampToLine ) {
  47. t = THREE.Math.clamp( t, 0, 1 );
  48. }
  49. return t;
  50. };
  51. }(),
  52. closestPointToPoint: function ( point, clampToLine, optionalTarget ) {
  53. var t = this.closestPointToPointParameter( point, clampToLine );
  54. var result = optionalTarget || new THREE.Vector3();
  55. return this.delta( result ).multiplyScalar( t ).add( this.start );
  56. },
  57. applyMatrix4: function ( matrix ) {
  58. this.start.applyMatrix4( matrix );
  59. this.end.applyMatrix4( matrix );
  60. return this;
  61. },
  62. equals: function ( line ) {
  63. return line.start.equals( this.start ) && line.end.equals( this.end );
  64. },
  65. clone: function () {
  66. return new THREE.Line3().copy( this );
  67. }
  68. } );
粤ICP备19079148号