forked from github/game-off-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
picker.js
45 lines (37 loc) · 1.33 KB
/
picker.js
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
"use strict";
define(["three.min"],function() {
function Picker( camera, element ) {
element.addEventListener( 'mousedown', onDocumentMouseDown, false );
var projector = new THREE.Projector();
var mouse = { x:0, y:0 };
function onDocumentMouseDown( event )
{
var x = event.clientX - element.offsetLeft - element.parentElement.offsetLeft;
var y = event.clientY - element.offsetTop - element.parentElement.offsetTop;
mouse.x = x / element.width * 2 - 1;
mouse.y = -y / element.height * 2 + 1;
detect();
}
function detect() {
var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
var intersects = ray.intersectObjects( registered );
for( var index = 0; index < intersects.length; index++ ) {
if( intersects[index].object.onPicked() ) {
return;
}
}
}
var registered = [];
function setList( list ) {
registered = list;
}
return {
setList:setList,
};
}
return {
Picker:Picker,
};
});