Ray.js 2.8 KB

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