Ray.js 5.7 KB

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