Ray.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Ray = function ( origin, direction ) {
  5. this.origin = ( origin !== undefined ) ? origin : new THREE.Vector3();
  6. this.direction = ( direction !== undefined ) ? direction : new THREE.Vector3();
  7. };
  8. THREE.Ray.prototype = {
  9. constructor: THREE.Ray,
  10. set: function ( origin, direction ) {
  11. this.origin.copy( origin );
  12. this.direction.copy( direction );
  13. return this;
  14. },
  15. copy: function ( ray ) {
  16. this.origin.copy( ray.origin );
  17. this.direction.copy( ray.direction );
  18. return this;
  19. },
  20. at: function( t, optionalTarget ) {
  21. var result = optionalTarget || new THREE.Vector3();
  22. return result.copy( this.direction ).multiplyScalar( t ).add( this.origin );
  23. },
  24. recast: function() {
  25. var v1 = new THREE.Vector3();
  26. return function ( t ) {
  27. this.origin.copy( this.at( t, v1 ) );
  28. return this;
  29. };
  30. }(),
  31. closestPointToPoint: function ( point, optionalTarget ) {
  32. var result = optionalTarget || new THREE.Vector3();
  33. result.subVectors( point, this.origin );
  34. var directionDistance = result.dot( this.direction );
  35. return result.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  36. },
  37. distanceToPoint: function() {
  38. var v1 = new THREE.Vector3();
  39. return function ( point ) {
  40. var directionDistance = v1.subVectors( point, this.origin ).dot( this.direction );
  41. v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  42. return v1.distanceTo( point );
  43. };
  44. }(),
  45. isIntersectionSphere: function( sphere ) {
  46. return ( this.distanceToPoint( sphere.center ) <= sphere.radius );
  47. },
  48. isIntersectionPlane: function ( plane ) {
  49. // check if the line and plane are non-perpendicular, if they
  50. // eventually they will intersect.
  51. var denominator = plane.normal.dot( this.direction );
  52. if ( denominator != 0 ) {
  53. return true;
  54. }
  55. // line is coplanar, return origin
  56. if( plane.distanceToPoint( this.origin ) == 0 ) {
  57. return true;
  58. }
  59. return false;
  60. },
  61. distanceToPlane: function ( plane ) {
  62. var denominator = plane.normal.dot( this.direction );
  63. if ( denominator == 0 ) {
  64. // line is coplanar, return origin
  65. if( plane.distanceToPoint( this.origin ) == 0 ) {
  66. return 0;
  67. }
  68. // Unsure if this is the correct method to handle this case.
  69. return undefined;
  70. }
  71. var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
  72. return t;
  73. },
  74. intersectPlane: function ( plane, optionalTarget ) {
  75. var t = this.distanceToPlane( plane );
  76. if ( t === undefined ) {
  77. return undefined;
  78. }
  79. return this.at( t, optionalTarget );
  80. },
  81. applyMatrix4: function ( matrix4 ) {
  82. this.direction.add( this.origin ).applyMatrix4( matrix4 );
  83. this.origin.applyMatrix4( matrix4 );
  84. this.direction.sub( this.origin );
  85. return this;
  86. },
  87. equals: function ( ray ) {
  88. return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
  89. },
  90. clone: function () {
  91. return new THREE.Ray().copy( this );
  92. }
  93. };