Ray3.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. recastSelf: function ( t ) {
  29. this.origin = this.at( t );
  30. return this;
  31. },
  32. recast: function ( t ) {
  33. return this.clone().recastSelf( t );
  34. },
  35. closestPointToPoint: function ( point ) {
  36. var result = point.clone().subSelf( this.origin );
  37. var directionDistance = result.dot( this.direction );
  38. return result.copy( this.direction ).multiplyScalar( directionDistance ).addSelf( this.origin );
  39. },
  40. distanceToPoint: function ( point ) {
  41. // NOTE: this creates a THREE.VEctor3 internally via closestPointToPoint
  42. // that is never returned, can be further GC optimized
  43. return this.closestPointToPoint( point ).distanceTo( point );
  44. },
  45. closestPointToRay: function ( ray ) {
  46. // Assumes the lines are normalized
  47. // based on algorithm in ILM's Imath Plane class.
  48. __v1.copy( this.origin ).subSelf( ray.origin );
  49. var c = this.direction.dot( __v1 );
  50. var a = ray.direction.dot( this.direction );
  51. var f = ray.direction.dot( __v1 );
  52. var num = c - a * f;
  53. var denom = a*a - 1;
  54. var absDenom = ( ( denom >= 0 ) ? denom: -denom );
  55. if ( absDenom < 1 )
  56. {
  57. var absNum = ( ( num >= 0 ) ? num: -num );
  58. if (absNum >= absDenom * Number.MAX_VALUE ) {
  59. // Unsure if this is the correct method to handle this case.
  60. return this.origin.clone();
  61. }
  62. }
  63. return this.direction.clone().multipleScalar( num / denom ).addSelf( this.origin );
  64. },
  65. distanceToRay: function ( ray ) {
  66. __v1.copy( this.direction ).crossSelf( ray.direction );
  67. __v2.copy( ray.origin ).subSelf( this.origin );
  68. var d = __v1.dot( __v2 );
  69. if( d >= 0 ) {
  70. return d;
  71. } else {
  72. // Unsure if this is the correct method to handle this case.
  73. return -1;
  74. }
  75. },
  76. isIntersectionPlane: function ( plane ) {
  77. return ( plane.normal.dot( this.direction ) != 0 );
  78. },
  79. intersectPlane: function ( plane ) {
  80. var a = plane.normal.dot( this.direction );
  81. if ( a == 0.0 ) {
  82. // Unsure if this is the correct method to handle this case.
  83. return undefined;
  84. }
  85. var t = - ( ( this.origin ^ plane.normal ) - plane.constant ) / a;
  86. return this.at( t );
  87. },
  88. equals: function ( ray ) {
  89. return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
  90. },
  91. clone: function () {
  92. return new THREE.Ray3().copy( this );
  93. }
  94. };
  95. THREE.Ray3.__v1 = new THREE.Vector3();
  96. THREE.Ray3.__v2 = new THREE.Vector3();
粤ICP备19079148号