Ray.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. object = objects[ i ];
  17. if ( object instanceof THREE.Mesh ) {
  18. intersects = intersects.concat( this.intersectObject( object ) );
  19. }
  20. }
  21. intersects.sort( function ( a, b ) { return a.distance - b.distance; } );
  22. return intersects;
  23. },
  24. intersectObject : function ( object ) {
  25. var f, fl, face, a, b, c, d, normal,
  26. dot, scalar,
  27. origin, direction,
  28. geometry = object.geometry,
  29. vertices = geometry.vertices,
  30. objMatrix,
  31. intersect, intersects = [],
  32. intersectPoint;
  33. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  34. face = geometry.faces[ f ];
  35. origin = this.origin.clone();
  36. direction = this.direction.clone();
  37. objMatrix = object.matrixWorld;
  38. a = objMatrix.multiplyVector3( vertices[ face.a ].position.clone() );
  39. b = objMatrix.multiplyVector3( vertices[ face.b ].position.clone() );
  40. c = objMatrix.multiplyVector3( vertices[ face.c ].position.clone() );
  41. d = face instanceof THREE.Face4 ? objMatrix.multiplyVector3( vertices[ face.d ].position.clone() ) : null;
  42. normal = object.matrixRotationWorld.multiplyVector3( face.normal.clone() );
  43. dot = direction.dot( normal );
  44. if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) { // Math.abs( dot ) > 0.0001
  45. scalar = normal.dot( new THREE.Vector3().sub( a, origin ) ) / dot;
  46. intersectPoint = origin.addSelf( direction.multiplyScalar( scalar ) );
  47. if ( face instanceof THREE.Face3 ) {
  48. if ( pointInFace3( intersectPoint, a, b, c ) ) {
  49. intersect = {
  50. distance: this.origin.distanceTo( intersectPoint ),
  51. point: intersectPoint,
  52. face: face,
  53. object: object
  54. };
  55. intersects.push( intersect );
  56. }
  57. } else if ( face instanceof THREE.Face4 ) {
  58. if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
  59. intersect = {
  60. distance: this.origin.distanceTo( intersectPoint ),
  61. point: intersectPoint,
  62. face: face,
  63. object: object
  64. };
  65. intersects.push( intersect );
  66. }
  67. }
  68. }
  69. }
  70. return intersects;
  71. // http://www.blackpawn.com/texts/pointinpoly/default.html
  72. function pointInFace3( p, a, b, c ) {
  73. var v0 = c.clone().subSelf( a ), v1 = b.clone().subSelf( a ), v2 = p.clone().subSelf( a ),
  74. dot00 = v0.dot( v0 ), dot01 = v0.dot( v1 ), dot02 = v0.dot( v2 ), dot11 = v1.dot( v1 ), dot12 = v1.dot( v2 ),
  75. invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 ),
  76. u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom,
  77. v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  78. return ( u > 0 ) && ( v > 0 ) && ( u + v < 1 );
  79. }
  80. }
  81. };