Raycaster.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author bhouston / http://clara.io/
  4. * @author stephomi / http://stephaneginier.com/
  5. */
  6. ( function ( THREE ) {
  7. THREE.Raycaster = function ( origin, direction, near, far ) {
  8. this.ray = new THREE.Ray( origin, direction );
  9. // direction is assumed to be normalized (for accurate distance calculations)
  10. this.near = near || 0;
  11. this.far = far || Infinity;
  12. this.params = {
  13. Mesh: {},
  14. Line: {},
  15. LOD: {},
  16. Points: { threshold: 1 },
  17. Sprite: {}
  18. };
  19. Object.defineProperties( this.params, {
  20. PointCloud: {
  21. get: function () {
  22. console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' );
  23. return this.Points;
  24. }
  25. }
  26. } );
  27. };
  28. function ascSort( a, b ) {
  29. return a.distance - b.distance;
  30. }
  31. function intersectObject( object, raycaster, intersects, recursive ) {
  32. if ( object.visible === false ) return;
  33. object.raycast( raycaster, intersects );
  34. if ( recursive === true ) {
  35. var children = object.children;
  36. for ( var i = 0, l = children.length; i < l; i ++ ) {
  37. intersectObject( children[ i ], raycaster, intersects, true );
  38. }
  39. }
  40. }
  41. //
  42. THREE.Raycaster.prototype = {
  43. constructor: THREE.Raycaster,
  44. linePrecision: 1,
  45. set: function ( origin, direction ) {
  46. // direction is assumed to be normalized (for accurate distance calculations)
  47. this.ray.set( origin, direction );
  48. },
  49. setFromCamera: function ( coords, camera ) {
  50. if ( camera instanceof THREE.PerspectiveCamera ) {
  51. this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
  52. this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
  53. } else if ( camera instanceof THREE.OrthographicCamera ) {
  54. this.ray.origin.set( coords.x, coords.y, - 1 ).unproject( camera );
  55. this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
  56. } else {
  57. console.error( 'THREE.Raycaster: Unsupported camera type.' );
  58. }
  59. },
  60. intersectObject: function ( object, recursive ) {
  61. var intersects = [];
  62. intersectObject( object, this, intersects, recursive );
  63. intersects.sort( ascSort );
  64. return intersects;
  65. },
  66. intersectObjects: function ( objects, recursive ) {
  67. var intersects = [];
  68. if ( Array.isArray( objects ) === false ) {
  69. console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
  70. return intersects;
  71. }
  72. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  73. intersectObject( objects[ i ], this, intersects, recursive );
  74. }
  75. intersects.sort( ascSort );
  76. return intersects;
  77. }
  78. };
  79. }( THREE ) );