Skip to content

Commit 9899020

Browse files
committed
Update three.js calls in ObjectSelection
1 parent ee198cd commit 9899020

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

utils/ObjectSelection.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
@author David Piegza
33
44
Implements a selection for objects in a scene.
5-
5+
66
It invokes a callback function when the mouse enters and when it leaves the object.
77
Based on a Three.js selection example.
8-
8+
99
Parameters:
1010
domElement: HTMLDomElement
1111
selected: callback function, passes the current selected object (on mouseover)
@@ -16,11 +16,11 @@ THREE.ObjectSelection = function(parameters) {
1616
var parameters = parameters || {};
1717

1818
this.domElement = parameters.domElement || document;
19-
this.projector = new THREE.Projector;
19+
this.projector = new THREE.Projector();
2020
this.INTERSECTED;
21-
21+
2222
var _this = this;
23-
23+
2424
var callbackSelected = parameters.selected;
2525
var callbackClicked = parameters.clicked;
2626
var mouse = { x: 0, y: 0 };
@@ -41,42 +41,34 @@ THREE.ObjectSelection = function(parameters) {
4141
}
4242

4343
this.render = function(scene, camera) {
44-
// find intersections
45-
camera.update();
46-
4744
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
4845
this.projector.unprojectVector( vector, camera );
4946

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);
5150

52-
var intersects = ray.intersectScene( scene );
5351
if( intersects.length > 0 ) {
5452
if ( this.INTERSECTED != intersects[ 0 ].object ) {
5553
if ( this.INTERSECTED ) {
56-
this.INTERSECTED.materials[ 0 ].color.setHex( this.INTERSECTED.currentHex );
54+
this.INTERSECTED.material.color.setHex( this.INTERSECTED.currentHex );
5755
}
5856

5957
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 );
6260
if(typeof callbackSelected === 'function') {
6361
callbackSelected(this.INTERSECTED);
6462
}
6563
}
6664
} else {
6765
if ( this.INTERSECTED ) {
68-
this.INTERSECTED.materials[ 0 ].color.setHex( this.INTERSECTED.currentHex );
66+
this.INTERSECTED.material.color.setHex( this.INTERSECTED.currentHex );
6967
}
7068
this.INTERSECTED = null;
7169
if(typeof callbackSelected === 'function') {
7270
callbackSelected(this.INTERSECTED);
7371
}
7472
}
75-
}
73+
}
7674
}
77-
78-
79-
80-
81-
82-

0 commit comments

Comments
 (0)