Ray.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.intersectObjects = function ( objects ) {
  8. var i, l, object,
  9. intersects = [];
  10. for ( i = 0, l = objects.length; i < l; i ++ ) {
  11. Array.prototype.push.apply( intersects, this.intersectObject( objects[ i ] ) );
  12. }
  13. intersects.sort( function ( a, b ) { return a.distance - b.distance; } );
  14. return intersects;
  15. };
  16. var a = new THREE.Vector3();
  17. var b = new THREE.Vector3();
  18. var c = new THREE.Vector3();
  19. var d = new THREE.Vector3();
  20. var originCopy = new THREE.Vector3();
  21. var directionCopy = new THREE.Vector3();
  22. var vector = new THREE.Vector3();
  23. var normal = new THREE.Vector3();
  24. var intersectPoint = new THREE.Vector3()
  25. this.intersectObject = function ( object ) {
  26. var intersect, intersects = [];
  27. if ( object instanceof THREE.Particle ) {
  28. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  29. if ( distance > object.scale.x ) {
  30. return [];
  31. }
  32. intersect = {
  33. distance: distance,
  34. point: object.position,
  35. face: null,
  36. object: object
  37. };
  38. intersects.push( intersect );
  39. } else if ( object instanceof THREE.Mesh ) {
  40. // Checking boundingSphere
  41. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  42. var scale = THREE.Frustum.__v1.set( object.matrixWorld.getColumnX().length(), object.matrixWorld.getColumnY().length(), object.matrixWorld.getColumnZ().length() );
  43. if ( distance > object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) ) ) {
  44. return intersects;
  45. }
  46. // Checking faces
  47. var f, fl, face, dot, scalar,
  48. geometry = object.geometry,
  49. vertices = geometry.vertices,
  50. objMatrix;
  51. object.matrixRotationWorld.extractRotation( object.matrixWorld );
  52. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  53. face = geometry.faces[ f ];
  54. originCopy.copy( this.origin );
  55. directionCopy.copy( this.direction );
  56. objMatrix = object.matrixWorld;
  57. // determine if ray intersects the plane of the face
  58. // note: this works regardless of the direction of the face normal
  59. vector = objMatrix.multiplyVector3( vector.copy( face.centroid ) ).subSelf( originCopy );
  60. normal = object.matrixRotationWorld.multiplyVector3( normal.copy( face.normal ) );
  61. dot = directionCopy.dot( normal );
  62. // bail if ray and plane are parallel
  63. if ( Math.abs( dot ) < 0.0001 ) continue;
  64. // calc distance to plane
  65. scalar = normal.dot( vector ) / dot;
  66. // if negative distance, then plane is behind ray
  67. if ( scalar < 0 ) continue;
  68. if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) {
  69. intersectPoint.add( originCopy, directionCopy.multiplyScalar( scalar ) );
  70. if ( face instanceof THREE.Face3 ) {
  71. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  72. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  73. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  74. if ( pointInFace3( intersectPoint, a, b, c ) ) {
  75. intersect = {
  76. distance: originCopy.distanceTo( intersectPoint ),
  77. point: intersectPoint.clone(),
  78. face: face,
  79. object: object
  80. };
  81. intersects.push( intersect );
  82. }
  83. } else if ( face instanceof THREE.Face4 ) {
  84. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  85. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  86. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  87. d = objMatrix.multiplyVector3( d.copy( vertices[ face.d ].position ) );
  88. if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
  89. intersect = {
  90. distance: originCopy.distanceTo( intersectPoint ),
  91. point: intersectPoint.clone(),
  92. face: face,
  93. object: object
  94. };
  95. intersects.push( intersect );
  96. }
  97. }
  98. }
  99. }
  100. }
  101. return intersects;
  102. }
  103. var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
  104. var dot, intersect, distance;
  105. function distanceFromIntersection( origin, direction, position ) {
  106. v0.sub( position, origin );
  107. dot = v0.dot( direction );
  108. intersect = v1.add( origin, 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.sub( c, a );
  116. v1.sub( b, a );
  117. v2.sub( p, 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. };
粤ICP备19079148号