Ray.js 4.6 KB

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