Skip to content

Commit 15d67ce

Browse files
committed
Added object selection.
1 parent f59b6dd commit 15d67ce

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

utils/ObjectSelection.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
3+
THREE.ObjectSelection = function(parameters) {
4+
var parameters = parameters || {};
5+
6+
var mouse = { x: 0, y: 0 };
7+
this.projector = new THREE.Projector;
8+
this.INTERSECTED;
9+
10+
var callbackSelected = parameters.selected;
11+
12+
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
13+
function onDocumentMouseMove( event ) {
14+
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
15+
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
16+
}
17+
18+
this.render = function(scene, camera) {
19+
// find intersections
20+
camera.update();
21+
22+
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
23+
this.projector.unprojectVector( vector, camera );
24+
25+
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
26+
27+
var intersects = ray.intersectScene( scene );
28+
if( intersects.length > 0 ) {
29+
if ( this.INTERSECTED != intersects[ 0 ].object ) {
30+
if ( this.INTERSECTED ) {
31+
this.INTERSECTED.materials[ 0 ].color.setHex( this.INTERSECTED.currentHex );
32+
}
33+
34+
this.INTERSECTED = intersects[ 0 ].object;
35+
this.INTERSECTED.currentHex = this.INTERSECTED.materials[ 0 ].color.getHex();
36+
this.INTERSECTED.materials[ 0 ].color.setHex( 0xff0000 );
37+
callbackSelected(this.INTERSECTED);
38+
}
39+
} else {
40+
if ( this.INTERSECTED ) {
41+
this.INTERSECTED.materials[ 0 ].color.setHex( this.INTERSECTED.currentHex );
42+
}
43+
this.INTERSECTED = null;
44+
}
45+
}
46+
}
47+
48+
49+
50+
51+
52+

0 commit comments

Comments
 (0)