Ray.js 3.0 KB

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