Ray.js 5.2 KB

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