1 | import { Ray } from '../math/Ray.js';
|
2 | import { Layers } from './Layers.js';
|
3 |
|
4 | function Raycaster( origin, direction, near, far ) {
|
5 |
|
6 | this.ray = new Ray( origin, direction );
|
7 |
|
8 |
|
9 | this.near = near || 0;
|
10 | this.far = far || Infinity;
|
11 | this.camera = null;
|
12 | this.layers = new Layers();
|
13 |
|
14 | this.params = {
|
15 | Mesh: {},
|
16 | Line: { threshold: 1 },
|
17 | LOD: {},
|
18 | Points: { threshold: 1 },
|
19 | Sprite: {}
|
20 | };
|
21 |
|
22 | Object.defineProperties( this.params, {
|
23 | PointCloud: {
|
24 | get: function () {
|
25 |
|
26 | console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' );
|
27 | return this.Points;
|
28 |
|
29 | }
|
30 | }
|
31 | } );
|
32 |
|
33 | }
|
34 |
|
35 | function ascSort( a, b ) {
|
36 |
|
37 | return a.distance - b.distance;
|
38 |
|
39 | }
|
40 |
|
41 | function intersectObject( object, raycaster, intersects, recursive ) {
|
42 |
|
43 | if ( object.layers.test( raycaster.layers ) ) {
|
44 |
|
45 | object.raycast( raycaster, intersects );
|
46 |
|
47 | }
|
48 |
|
49 | if ( recursive === true ) {
|
50 |
|
51 | const children = object.children;
|
52 |
|
53 | for ( let i = 0, l = children.length; i < l; i ++ ) {
|
54 |
|
55 | intersectObject( children[ i ], raycaster, intersects, true );
|
56 |
|
57 | }
|
58 |
|
59 | }
|
60 |
|
61 | }
|
62 |
|
63 | Object.assign( Raycaster.prototype, {
|
64 |
|
65 | set: function ( origin, direction ) {
|
66 |
|
67 |
|
68 |
|
69 | this.ray.set( origin, direction );
|
70 |
|
71 | },
|
72 |
|
73 | setFromCamera: function ( coords, camera ) {
|
74 |
|
75 | if ( ( camera && camera.isPerspectiveCamera ) ) {
|
76 |
|
77 | this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
|
78 | this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
|
79 | this.camera = camera;
|
80 |
|
81 | } else if ( ( camera && camera.isOrthographicCamera ) ) {
|
82 |
|
83 | this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera );
|
84 | this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
|
85 | this.camera = camera;
|
86 |
|
87 | } else {
|
88 |
|
89 | console.error( 'THREE.Raycaster: Unsupported camera type.' );
|
90 |
|
91 | }
|
92 |
|
93 | },
|
94 |
|
95 | intersectObject: function ( object, recursive, optionalTarget ) {
|
96 |
|
97 | const intersects = optionalTarget || [];
|
98 |
|
99 | intersectObject( object, this, intersects, recursive );
|
100 |
|
101 | intersects.sort( ascSort );
|
102 |
|
103 | return intersects;
|
104 |
|
105 | },
|
106 |
|
107 | intersectObjects: function ( objects, recursive, optionalTarget ) {
|
108 |
|
109 | const intersects = optionalTarget || [];
|
110 |
|
111 | if ( Array.isArray( objects ) === false ) {
|
112 |
|
113 | console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
|
114 | return intersects;
|
115 |
|
116 | }
|
117 |
|
118 | for ( let i = 0, l = objects.length; i < l; i ++ ) {
|
119 |
|
120 | intersectObject( objects[ i ], this, intersects, recursive );
|
121 |
|
122 | }
|
123 |
|
124 | intersects.sort( ascSort );
|
125 |
|
126 | return intersects;
|
127 |
|
128 | }
|
129 |
|
130 | } );
|
131 |
|
132 |
|
133 | export { Raycaster };
|