Ray.js 4.2 KB

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