Raycaster.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { Ray } from '../math/Ray.js';
  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.camera = null;
  13. this.params = {
  14. Mesh: {},
  15. Line: {},
  16. LOD: {},
  17. Points: { threshold: 1 },
  18. Sprite: {}
  19. };
  20. Object.defineProperties( this.params, {
  21. PointCloud: {
  22. get: function () {
  23. console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' );
  24. return this.Points;
  25. }
  26. }
  27. } );
  28. }
  29. function ascSort( a, b ) {
  30. return a.distance - b.distance;
  31. }
  32. function intersectObject( object, raycaster, intersects, recursive ) {
  33. if ( object.visible === false ) return;
  34. object.raycast( raycaster, intersects );
  35. if ( recursive === true ) {
  36. var children = object.children;
  37. for ( var i = 0, l = children.length; i < l; i ++ ) {
  38. intersectObject( children[ i ], raycaster, intersects, true );
  39. }
  40. }
  41. }
  42. Object.assign( Raycaster.prototype, {
  43. linePrecision: 1,
  44. set: function ( origin, direction ) {
  45. // direction is assumed to be normalized (for accurate distance calculations)
  46. this.ray.set( origin, direction );
  47. },
  48. setFromCamera: function ( coords, camera ) {
  49. if ( ( camera && camera.isPerspectiveCamera ) ) {
  50. this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
  51. this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
  52. this.camera = camera;
  53. } else if ( ( camera && camera.isOrthographicCamera ) ) {
  54. this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
  55. this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
  56. this.camera = camera;
  57. } else {
  58. console.error( 'THREE.Raycaster: Unsupported camera type.' );
  59. }
  60. },
  61. intersectObject: function ( object, recursive, optionalTarget ) {
  62. var intersects = optionalTarget || [];
  63. intersectObject( object, this, intersects, recursive );
  64. intersects.sort( ascSort );
  65. return intersects;
  66. },
  67. intersectObjects: function ( objects, recursive, optionalTarget ) {
  68. var intersects = optionalTarget || [];
  69. if ( Array.isArray( objects ) === false ) {
  70. console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
  71. return intersects;
  72. }
  73. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  74. intersectObject( objects[ i ], this, intersects, recursive );
  75. }
  76. intersects.sort( ascSort );
  77. return intersects;
  78. }
  79. } );
  80. export { Raycaster };