-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayPicker.js
More file actions
89 lines (65 loc) · 2.26 KB
/
RayPicker.js
File metadata and controls
89 lines (65 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as THREE from "three";
class RayPicker extends THREE.Raycaster {
constructor( layer ) {
super();
this.layers.set( layer || 0 );
this.params.Points.threshold = 1;
this.params.Line.threshold = 3;
/* public mouseCoords are Vector3 to allow camera.unproject */
this.mouseCoords = new THREE.Vector3();
this.centerCoords = new THREE.Vector3();
this.intersects = [];
}
setFromMouseEventAndCamera( evt, camera ) {
this.mouseCoords.x = ( evt.clientX / evt.target.clientWidth ) * 2 - 1;
this.mouseCoords.y = -( evt.clientY / evt.target.clientHeight ) * 2 + 1;
super.setFromCamera( this.mouseCoords, camera );
}
setFromCamera( camera ) {
super.setFromCamera( this.centerCoords, camera );
}
/* return array of all raycaster intersects */
pickAll( object ) {
this.intersects.length = 0;
if ( Array.isArray( object ) ) {
this.intersectObjects( object, true, this.intersects );
} else {
this.intersectObject( object, true, this.intersects );
}
if ( this.intersects.length ) {
return this.intersects;
}
return null;
}
/* return closest of all raycaster intersects, allowing for
* optional test function. example of use with test function to ignore "grass":
let result = rayPicker.pick( scene, ( intersect ) => {
return ( intersect.object.name !== "grass" );
} );
* or
let result = rayPicker.pick( scene, ( intersect ) => {
if ( intersect.object.name === "grass" ) {
return false;
}
return true;
} );
*
*/
pick( object, testFunction ) {
let intersectsArray = this.pickAll( object );
if ( intersectsArray ) {
var index = 0;
if ( typeof testFunction === "function" ) {
while ( index < intersectsArray.length &&
!testFunction( intersectsArray[ index ] ) ) {
index ++;
}
}
if ( intersectsArray[ index ] ) {
return intersectsArray[ index ];
}
}
return null;
}
}
export { RayPicker };