-
Notifications
You must be signed in to change notification settings - Fork 4
/
three_mouse.js
100 lines (82 loc) · 2.45 KB
/
three_mouse.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
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
90
91
92
93
94
95
96
97
98
99
100
import {
Matrix4,
Raycaster,
Vector2
} from "../libs/three/three.module.js";
var camera;
var container;
var raycaster;
var mouse = {
"is_inside_object":false,
"object":""
};
var mesh_list = [];
function send_custom_event(event_name,data){
var event = new CustomEvent(event_name, {detail:data});
window.dispatchEvent(event);
}
function get_mesh_intersect(l_x,l_y){
var rect = container.getBoundingClientRect();
var vect2 = new Vector2();
vect2.x = ( ( l_x - rect.left ) / rect.width ) * 2 - 1;
vect2.y = - ( ( l_y - rect.top ) / rect.height ) * 2 + 1;
let result = "";
camera.projectionMatrixInverse = new THREE.Matrix4();
camera.projectionMatrixInverse.getInverse(camera.projectionMatrix);
raycaster.setFromCamera( vect2, camera );
var intersects = raycaster.intersectObjects( mesh_list, true );
if(intersects.length > 0){
result = intersects[ 0 ].object.name;
}
return result;
}
function process_mouse_event(event_name, event){
event.preventDefault();
var obj_name = get_mesh_intersect(event.clientX,event.clientY);
if ( obj_name != "") {
mouse.object = obj_name;
if(!mouse.is_inside_object){
send_custom_event("mesh_mouse_enter",{ type: "light", name: mouse.object});
}
mouse.is_inside_object = true;
send_custom_event(event_name,{ type: "light", name: mouse.object});
}
else{
if(mouse.is_inside_object){
mouse.is_inside_object = false;
send_custom_event("mesh_mouse_exit",{ type: "light", name: mouse.object});
}
}
}
function onTouch(event){
event.preventDefault();
console.log("onTouch",event);
if(event.type == "touchstart"){
var obj_name = get_mesh_intersect(event.targetTouches[0].clientX,event.targetTouches[0].clientY);
if ( obj_name != "") {
send_custom_event("mesh_touch_start",{ type: "light", name: obj_name});
}
}
}
function onMouseDown(event){
process_mouse_event("mesh_mouse_down",event)
}
function onMouseMove(event){
process_mouse_event("mesh_mouse_move",event)
}
function init(l_camera,l_container) {
camera = l_camera;
container = document.getElementById('viewer');
console.log("three_mouse> init()");
raycaster = new Raycaster();
container.addEventListener( 'mousemove', onMouseMove, false );
container.addEventListener( 'mousedown', onMouseDown, false );
container.addEventListener( 'touchstart', onTouch, false );
}
function SetMeshList(l_mesh_list){
mesh_list = l_mesh_list;
mesh_list.forEach(mesh =>{
console.log(`three_mouse> added mesh ${mesh.name}`);
})
}
export{init,SetMeshList};