-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy paththree_mmi.js
292 lines (243 loc) · 9.61 KB
/
three_mmi.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Github: https://github.com/danielblagy/three_mmi
MIT License
Copyright (c) 2022 danielblagy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You can find an example project in the github repository (https://github.com/danielblagy/three_mmi) under folder
named 'example'.
UPDATES:
1/19/2021: added support for the following even types: mouseenter, mouseleave, mousedown, mouseup
1/20/2021: fixed bug: mouseleave event wouldn't trigger if there was mouseenter for
another mesh with the same name property
(mistake in if condition, due to the utility using the mesh names)
added xylophone simulation example project
9/25/2021: changed local variable 'event' to 'e' in function handleEvent(e), as is logically proper,
and 'event' being deprecated
4/9/2022: updated license
4/20/2022: fixed the grouping bug (when if meshes were in a group, mouse interactions wouldn't be registered by three_mmi)
USAGE:
// pass threejs scene and camera
const mmi = new MouseMeshInteraction(scene, camera);
// create an interactable mesh
const mesh = new THREE.Mesh(geometry, material);
// specify a name for the mesh (needed for mmi to work, you can give the same name to multiple meshes)
mesh.name = 'my_interactable_mesh';
scene.add(mesh);
// there are 7 types of interactions available:
// * 'click' (left mouse button click)
// * 'dblclick' (left mouse button double click)
// * 'contextmenu' (right mouse button click, triggered before opening the context menu)
// * 'mouseenter' (mouse cursor is moved onto the element that has the listener attached)
// * 'mouseleave' (mouse cursor is moved off the element that has the listener attached)
// * 'mousedown' (mouse button is pressed on an element)
// * 'mouseup' (mouse button is released over an element)
// create a handler for when user clicks on the mesh with name 'my_interactable_mesh'
mmi.addHandler('my_interactable_mesh', 'click', function(mesh) {
console.log('interactable mesh has been clicked!');
console.log(mesh);
});
// put mmi.update() inside the graphics update function
function animate() {
requestAnimationFrame( animate );
mmi.update();
renderer.render( scene, camera );
}
animate();
QUICKSTART TEMPLATE:
For a project with the following structure:
index.html
js/three.js
js/three_mmi.js
*index.html*
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>three_mmi Quickstart Template</title>
</head>
<body>
<script src="js/three.js"></script>
<script src="js/three_mmi.js"></script>
<script>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 50);
const renderer = new THREE.WebGLRenderer({antialias : true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xe0b2a4, 1.0);
document.body.appendChild(renderer.domElement);
// create an interactable light buld mesh
const gray_color = new THREE.Color(0x57554f);
const yellow_color = new THREE.Color(0xe0c53a);
const bulb_geometry = new THREE.BoxGeometry(5, 5, 5);
const bulb_material = new THREE.MeshBasicMaterial( { color: yellow_color } );
var bulb_mesh = new THREE.Mesh(bulb_geometry, bulb_material);
// add a name to the mesh (needed for mmi to work, you can give the same name to multiple meshes)
bulb_mesh.name = 'bulb';
bulb_mesh.position.set(0, 0, 0);
scene.add(bulb_mesh);
// initialize instance of class MouseMeshInteraction, passing threejs scene and camera
const mmi = new MouseMeshInteraction(scene, camera);
// add a handler on mouse click for mesh (or meshes) with the name 'bulb'
mmi.addHandler('bulb', 'click', function(mesh) {
console.log('bulb mesh is being clicked!');
// switch between colors
if (mesh.material.color === gray_color) {
mesh.material.color = yellow_color;
}
else {
mesh.material.color = gray_color;
}
});
function render() {
requestAnimationFrame(render);
// update the mmi
mmi.update();
renderer.render(scene, camera);
}
render();
</script>
</body>
</html>
*/
class MouseMeshInteractionHandler {
constructor(mesh_name, handler_function) {
this.mesh_name = mesh_name;
this.handler_function = handler_function;
}
}
class MouseMeshInteraction {
constructor(scene, camera) {
this.scene = scene;
this.camera = camera;
this.raycaster = new THREE.Raycaster();
this.mouse = new THREE.Vector2();
this.updated = false;
this.event = '';
// last mesh that the mouse cursor was over
this.last_mouseenter_mesh = undefined;
// last mesh that the mouse was pressing down
this.last_pressed_mesh = undefined;
this.handlers = new Map();
this.handlers.set('click', []);
this.handlers.set('dblclick', []);
this.handlers.set('contextmenu', []);
this.handlers.set('mousedown', []);
this.handlers.set('mouseup', []);
this.handlers.set('mouseenter', []);
this.handlers.set('mouseleave', []);
window.addEventListener('mousemove', this);
window.addEventListener('click', this);
window.addEventListener('dblclick', this);
window.addEventListener('contextmenu', this);
window.addEventListener('mousedown', this);
}
handleEvent(e) {
switch(e.type) {
case "mousemove": {
this.mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
this.mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
this.updated = true;
this.event = 'motion';
}
break;
default: {
this.updated = true;
this.event = e.type;
}
}
}
addHandler(mesh_name, event_type, handler_function) {
if (this.handlers.has(event_type)) {
this.handlers.get(event_type).push(new MouseMeshInteractionHandler(mesh_name, handler_function));
}
}
update() {
if (this.updated) {
// update the picking ray with the camera and mouse position
this.raycaster.setFromCamera(this.mouse, this.camera);
// calculate objects intersecting the picking ray
const intersects = this.raycaster.intersectObjects(this.scene.children, true);
if (intersects.length > 0) {
// special test for events: 'mouseenter', 'mouseleave'
if (this.event === 'motion') {
let mouseenter_handlers = this.handlers.get('mouseenter');
let mouseleave_handlers = this.handlers.get('mouseleave');
if (mouseleave_handlers.length > 0) {
for (const handler of mouseleave_handlers) {
// if mesh was entered by mouse previously, but not anymore, that means it has been mouseleave'd
if (
this.last_mouseenter_mesh !== undefined
&& intersects[0].object !== this.last_mouseenter_mesh
&& handler.mesh_name === this.last_mouseenter_mesh.name
) {
handler.handler_function(this.last_mouseenter_mesh);
break;
}
}
}
if (mouseenter_handlers.length > 0) {
for (const handler of mouseenter_handlers) {
if (handler.mesh_name === intersects[0].object.name && intersects[0].object !== this.last_mouseenter_mesh) {
this.last_mouseenter_mesh = intersects[0].object;
handler.handler_function(intersects[0].object);
break;
}
}
}
}
else {
// if mouseup event has occurred
if (this.event === 'click' && this.last_pressed_mesh === intersects[0].object) {
for (const handler of this.handlers.get('mouseup')) {
if (handler.mesh_name === intersects[0].object.name) {
handler.handler_function(intersects[0].object);
break;
}
}
this.last_pressed_mesh = undefined;
}
// for mouseup event handler to work
if (this.event === 'mousedown') {
this.last_pressed_mesh = intersects[0].object;
}
let handlers_of_event = this.handlers.get(this.event);
for (const handler of handlers_of_event) {
if (handler.mesh_name === intersects[0].object.name) {
handler.handler_function(intersects[0].object);
break;
}
}
}
}
// if mouse doesn't intersect any meshes
else if (this.event === 'motion') {
// special test for 'mouseleave' event
// (since it may be triggered when cursor doesn't intersect with any meshes)
for (const handler of this.handlers.get('mouseleave')) {
// if mesh was entered by mouse previously, but not anymore, that means it has been mouseleave'd
if (this.last_mouseenter_mesh !== undefined && handler.mesh_name === this.last_mouseenter_mesh.name) {
handler.handler_function(this.last_mouseenter_mesh);
this.last_mouseenter_mesh = undefined;
break;
}
}
}
this.updated = false;
}
}
}