Ray.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.Ray = function ( origin, direction ) {
  5. this.origin = origin || new THREE.Vector3();
  6. this.direction = direction || new THREE.Vector3();
  7. var precision = 0.0001;
  8. this.setPrecision = function ( value ) {
  9. precision = value;
  10. };
  11. var a = new THREE.Vector3();
  12. var b = new THREE.Vector3();
  13. var c = new THREE.Vector3();
  14. var d = new THREE.Vector3();
  15. var originCopy = new THREE.Vector3();
  16. var directionCopy = new THREE.Vector3();
  17. var vector = new THREE.Vector3();
  18. var normal = new THREE.Vector3();
  19. var intersectPoint = new THREE.Vector3()
  20. this.intersectObject = function ( object ) {
  21. var intersect, intersects = [];
  22. if ( object instanceof THREE.Particle ) {
  23. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  24. if ( distance > object.scale.x ) {
  25. return [];
  26. }
  27. intersect = {
  28. distance: distance,
  29. point: object.position,
  30. face: null,
  31. object: object
  32. };
  33. intersects.push( intersect );
  34. } else if ( object instanceof THREE.Mesh ) {
  35. // Checking boundingSphere
  36. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  37. var scale = THREE.Frustum.__v1.set( object.matrixWorld.getColumnX().length(), object.matrixWorld.getColumnY().length(), object.matrixWorld.getColumnZ().length() );
  38. if ( distance > object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) ) ) {
  39. return intersects;
  40. }
  41. // Checking faces
  42. var f, fl, face, dot, scalar,
  43. geometry = object.geometry,
  44. vertices = geometry.vertices,
  45. objMatrix;
  46. object.matrixRotationWorld.extractRotation( object.matrixWorld );
  47. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  48. face = geometry.faces[ f ];
  49. originCopy.copy( this.origin );
  50. directionCopy.copy( this.direction );
  51. objMatrix = object.matrixWorld;
  52. // determine if ray intersects the plane of the face
  53. // note: this works regardless of the direction of the face normal
  54. vector = objMatrix.multiplyVector3( vector.copy( face.centroid ) ).subSelf( originCopy );
  55. normal = object.matrixRotationWorld.multiplyVector3( normal.copy( face.normal ) );
  56. dot = directionCopy.dot( normal );
  57. // bail if ray and plane are parallel
  58. if ( Math.abs( dot ) < precision ) continue;
  59. // calc distance to plane
  60. scalar = normal.dot( vector ) / dot;
  61. // if negative distance, then plane is behind ray
  62. if ( scalar < 0 ) continue;
  63. if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) {
  64. intersectPoint.add( originCopy, directionCopy.multiplyScalar( scalar ) );
  65. if ( face instanceof THREE.Face3 ) {
  66. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ] ) );
  67. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ] ) );
  68. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ] ) );
  69. if ( pointInFace3( intersectPoint, a, b, c ) ) {
  70. intersect = {
  71. distance: originCopy.distanceTo( intersectPoint ),
  72. point: intersectPoint.clone(),
  73. face: face,
  74. object: object
  75. };
  76. intersects.push( intersect );
  77. }
  78. } else if ( face instanceof THREE.Face4 ) {
  79. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ] ) );
  80. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ] ) );
  81. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ] ) );
  82. d = objMatrix.multiplyVector3( d.copy( vertices[ face.d ] ) );
  83. if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
  84. intersect = {
  85. distance: originCopy.distanceTo( intersectPoint ),
  86. point: intersectPoint.clone(),
  87. face: face,
  88. object: object
  89. };
  90. intersects.push( intersect );
  91. }
  92. }
  93. }
  94. }
  95. }
  96. return intersects;
  97. }
  98. this.intersectObjects = function ( objects ) {
  99. var intersects = [];
  100. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  101. Array.prototype.push.apply( intersects, this.intersectObject( objects[ i ] ) );
  102. }
  103. intersects.sort( function ( a, b ) { return a.distance - b.distance; } );
  104. return intersects;
  105. };
  106. var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
  107. var dot, intersect, distance;
  108. function distanceFromIntersection( origin, direction, position ) {
  109. v0.sub( position, origin );
  110. dot = v0.dot( direction );
  111. intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
  112. distance = position.distanceTo( intersect );
  113. return distance;
  114. }
  115. // http://www.blackpawn.com/texts/pointinpoly/default.html
  116. var dot00, dot01, dot02, dot11, dot12, invDenom, u, v;
  117. function pointInFace3( p, a, b, c ) {
  118. v0.sub( c, a );
  119. v1.sub( b, a );
  120. v2.sub( p, a );
  121. dot00 = v0.dot( v0 );
  122. dot01 = v0.dot( v1 );
  123. dot02 = v0.dot( v2 );
  124. dot11 = v1.dot( v1 );
  125. dot12 = v1.dot( v2 );
  126. invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 );
  127. u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  128. v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  129. return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 );
  130. }
  131. };