Raycaster.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { Ray } from '../math/Ray';
  2. /**
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author bhouston / http://clara.io/
  5. * @author stephomi / http://stephaneginier.com/
  6. */
  7. function Raycaster( origin, direction, near, far ) {
  8. this.ray = new 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. Object.assign( Raycaster.prototype, {
  42. linePrecision: 1,
  43. set: function ( origin, direction ) {
  44. // direction is assumed to be normalized (for accurate distance calculations)
  45. this.ray.set( origin, direction );
  46. },
  47. setFromCamera: function ( coords, camera ) {
  48. if ( ( camera && camera.isPerspectiveCamera ) ) {
  49. this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
  50. this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
  51. } else if ( ( camera && camera.isOrthographicCamera ) ) {
  52. this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
  53. this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
  54. } else {
  55. console.error( 'THREE.Raycaster: Unsupported camera type.' );
  56. }
  57. },
  58. intersectObject: function ( object, recursive ) {
  59. var intersects = [];
  60. intersectObject( object, this, intersects, recursive );
  61. intersects.sort( ascSort );
  62. return intersects;
  63. },
  64. intersectObjects: function ( objects, recursive ) {
  65. var intersects = [];
  66. if ( Array.isArray( objects ) === false ) {
  67. console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
  68. return intersects;
  69. }
  70. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  71. intersectObject( objects[ i ], this, intersects, recursive );
  72. }
  73. intersects.sort( ascSort );
  74. return intersects;
  75. }
  76. } );
  77. export { Raycaster };