Ray.js 3.0 KB

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