Ray3.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Ray3 = function ( origin, direction ) {
  5. if ( origin === undefined && direction === undefined ) {
  6. this.origin = new THREE.Vector3();
  7. this.direction = new THREE.Vector3( 0, 0, 0 );
  8. } else {
  9. this.origin = origin.clone();
  10. this.direction = direction.clone();
  11. }
  12. };
  13. THREE.Ray3.prototype = {
  14. constructor: THREE.Ray3,
  15. set: function ( origin, direction) {
  16. this.origin.copy( origin );
  17. this.direction.copy( direction );
  18. return this;
  19. },
  20. copy: function ( ray ) {
  21. this.origin.copy( ray.origin );
  22. this.direction.copy( ray.direction );
  23. return this;
  24. },
  25. at: function( t ) {
  26. return this.direction.clone().multiplyScalar( t ).addSelf( this.origin );
  27. },
  28. recast: function ( t ) {
  29. return new THREE.Ray3( this.at( t ), this.direction );
  30. },
  31. closestPointToPoint: function ( point ) {
  32. var result = point.clone().subSelf( this.origin );
  33. var directionDistance = result.dot( this.direction );
  34. return result.copy( this.direction ).multiplyScalar( directionDistance ).addSelf( this.origin );
  35. },
  36. distanceToPoint: function ( point ) {
  37. // NOTE: this creates a THREE.VEctor3 internally via closestPointToPoint
  38. // that is never returned, can be further GC optimized
  39. return this.closestPointToPoint( point ).distanceTo( point );
  40. },
  41. closestPointToRay: function ( ray ) {
  42. // Assumes the lines are normalized
  43. // based on algorithm in ILM's Imath Plane class.
  44. __v1.copy( this.origin ).subSelf( ray.origin );
  45. var c = this.direction.dot( __v1 );
  46. var a = ray.direction.dot( this.direction );
  47. var f = ray.direction.dot( __v1 );
  48. var num = c - a * f;
  49. var denom = a*a - 1;
  50. var absDenom = ( ( denom >= 0 ) ? denom: -denom );
  51. if ( absDenom < 1 )
  52. {
  53. var absNum = ( ( num >= 0 ) ? num: -num );
  54. if (absNum >= absDenom * Number.MAX_VALUE ) {
  55. return this.origin.clone();
  56. }
  57. }
  58. return this.direction.clone().multipleScalar( num / denom ).addSelf( this.origin );
  59. },
  60. distanceToRay: function ( ray ) {
  61. __v1.copy( this.direction ).crossSelf( ray.direction );
  62. __v2.copy( ray.origin ).subSelf( this.origin );
  63. var d = __v1.dot( __v2 );
  64. if( d >= 0 ) {
  65. return d;
  66. }
  67. return -1;
  68. },
  69. isIntersectionPlane: function ( plane ) {
  70. return ( plane.normal.dot( this.direction ) != 0 );
  71. },
  72. intersectPlane: function ( plane ) {
  73. var a = plane.normal.dot( this.direction );
  74. if ( a == 0.0 ) {
  75. // Unsure if this is the correct method to handle this case.
  76. return undefined;
  77. }
  78. var t = - ( ( this.origin ^ plane.normal ) - plane.constant ) / a;
  79. return this.at( t );
  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.Ray3().copy( this );
  86. }
  87. };
  88. THREE.Ray3.__v1 = new THREE.Vector3();
  89. THREE.Ray3.__v2 = new THREE.Vector3();
粤ICP备19079148号