2
2
@author David Piegza
3
3
4
4
Implements a selection for objects in a scene.
5
-
5
+
6
6
It invokes a callback function when the mouse enters and when it leaves the object.
7
7
Based on a Three.js selection example.
8
-
8
+
9
9
Parameters:
10
10
domElement: HTMLDomElement
11
11
selected: callback function, passes the current selected object (on mouseover)
@@ -16,11 +16,11 @@ THREE.ObjectSelection = function(parameters) {
16
16
var parameters = parameters || { } ;
17
17
18
18
this . domElement = parameters . domElement || document ;
19
- this . projector = new THREE . Projector ;
19
+ this . projector = new THREE . Projector ( ) ;
20
20
this . INTERSECTED ;
21
-
21
+
22
22
var _this = this ;
23
-
23
+
24
24
var callbackSelected = parameters . selected ;
25
25
var callbackClicked = parameters . clicked ;
26
26
var mouse = { x : 0 , y : 0 } ;
@@ -41,42 +41,34 @@ THREE.ObjectSelection = function(parameters) {
41
41
}
42
42
43
43
this . render = function ( scene , camera ) {
44
- // find intersections
45
- camera . update ( ) ;
46
-
47
44
var vector = new THREE . Vector3 ( mouse . x , mouse . y , 0.5 ) ;
48
45
this . projector . unprojectVector ( vector , camera ) ;
49
46
50
- var ray = new THREE . Ray ( camera . position , vector . subSelf ( camera . position ) . normalize ( ) ) ;
47
+ var raycaster = new THREE . Raycaster ( camera . position , vector . sub ( camera . position ) . normalize ( ) ) ;
48
+
49
+ var intersects = raycaster . intersectObject ( scene , true ) ;
51
50
52
- var intersects = ray . intersectScene ( scene ) ;
53
51
if ( intersects . length > 0 ) {
54
52
if ( this . INTERSECTED != intersects [ 0 ] . object ) {
55
53
if ( this . INTERSECTED ) {
56
- this . INTERSECTED . materials [ 0 ] . color . setHex ( this . INTERSECTED . currentHex ) ;
54
+ this . INTERSECTED . material . color . setHex ( this . INTERSECTED . currentHex ) ;
57
55
}
58
56
59
57
this . INTERSECTED = intersects [ 0 ] . object ;
60
- this . INTERSECTED . currentHex = this . INTERSECTED . materials [ 0 ] . color . getHex ( ) ;
61
- this . INTERSECTED . materials [ 0 ] . color . setHex ( 0xff0000 ) ;
58
+ this . INTERSECTED . currentHex = this . INTERSECTED . material . color . getHex ( ) ;
59
+ this . INTERSECTED . material . color . setHex ( 0xff0000 ) ;
62
60
if ( typeof callbackSelected === 'function' ) {
63
61
callbackSelected ( this . INTERSECTED ) ;
64
62
}
65
63
}
66
64
} else {
67
65
if ( this . INTERSECTED ) {
68
- this . INTERSECTED . materials [ 0 ] . color . setHex ( this . INTERSECTED . currentHex ) ;
66
+ this . INTERSECTED . material . color . setHex ( this . INTERSECTED . currentHex ) ;
69
67
}
70
68
this . INTERSECTED = null ;
71
69
if ( typeof callbackSelected === 'function' ) {
72
70
callbackSelected ( this . INTERSECTED ) ;
73
71
}
74
72
}
75
- }
73
+ }
76
74
}
77
-
78
-
79
-
80
-
81
-
82
-
0 commit comments