Ray.js 4.1 KB

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