Ray.js 4.9 KB

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