Line3.js 2.2 KB

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