Raycaster.js 2.8 KB

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