This repository was archived by the owner on May 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathaframe-controller-cursor-component.js
More file actions
272 lines (224 loc) · 8.13 KB
/
aframe-controller-cursor-component.js
File metadata and controls
272 lines (224 loc) · 8.13 KB
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
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
/* global AFRAME, THREE */
var bind = AFRAME.utils.bind;
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
var EVENTS = {
CLICK: 'click',
MOUSEENTER: 'mouseenter',
MOUSEDOWN: 'mousedown',
MOUSELEAVE: 'mouseleave',
MOUSEUP: 'mouseup'
};
var STATES = {
HOVERING: 'cursor-hovering',
HOVERED: 'cursor-hovered'
};
/**
* Controller cursor component.
*
* @member {Element} triggerDownEl - Entity that was last clicked.
* @member {object} intersection - Attributes of the current intersection event, including
* 3D- and 2D-space coordinates. See: http://threejs.org/docs/api/core/Raycaster.html
* @member {Element} intersectedEl - Currently-intersected entity. Used to keep track to
* emit events when unintersecting.
*/
AFRAME.registerComponent('controller-cursor', {
dependencies: ['raycaster'],
schema: {
color: {default: '#74BEC1'},
downEvents: {default: ['triggerdown']},
radius: {default: 0.001},
upEvents: {default: ['triggerup']}
},
init: function () {
var cursorEl = this.el;
var data = this.data;
this.triggerDownEl = null;
this.intersection = null;
this.intersectedEl = null;
// Wait for canvas to load.
var canvas = cursorEl.sceneEl.canvas;
if (!canvas) {
cursorEl.sceneEl.addEventListener('render-target-loaded', bind(this.init, this));
return;
}
// Prevent laser from interfering with raycaster by setting near property
cursorEl.setAttribute('raycaster', {near: 0.03});
// Create laser beam.
var raycasterLength = cursorEl.getAttribute('raycaster').far;
var laserLength = raycasterLength === Infinity ? 1000 : raycasterLength;
var cursorGeometry = new THREE.CylinderBufferGeometry(
data.radius, data.radius, laserLength, 32);
var cursorMaterial = new THREE.MeshBasicMaterial({color: data.color});
var cursorMesh = new THREE.Mesh(cursorGeometry, cursorMaterial);
// Move mesh so beam starts at tip of controller model.
cursorMesh.position.z = -1 * laserLength / 2;
// Rotate mesh to point directly away from controller model.
cursorMesh.rotation.x = 90 * (Math.PI / 180);
cursorEl.setObject3D('cursormesh', cursorMesh);
// Bind methods.
this.onIntersectionBind = bind(this.onIntersection, this);
this.onIntersectionClearedBind = bind(this.onIntersectionCleared, this);
this.onTriggerDownBind = bind(this.onTriggerDown, this);
this.onTriggerUpBind = bind(this.onTriggerUp, this);
},
/**
* Add event listeners.
*/
play: function () {
var cursorEl = this.el;
var data = this.data;
var self = this;
cursorEl.addEventListener('raycaster-intersection', this.onIntersectionBind);
cursorEl.addEventListener('raycaster-intersection-cleared',
this.onIntersectionClearedBind);
data.downEvents.forEach(function (downEvent) {
cursorEl.addEventListener(downEvent, self.onTriggerDownBind);
});
data.upEvents.forEach(function (upEvent) {
cursorEl.addEventListener(upEvent, self.onTriggerUpBind);
});
},
/**
* Remove event listeners.
*/
pause: function () {
var cursorEl = this.el;
var data = this.data;
var self = this;
cursorEl.removeEventListener('raycaster-intersection', this.onIntersectionBind);
cursorEl.removeEventListener('raycaster-intersection-cleared',
this.onIntersectionClearedBind);
data.downEvents.forEach(function (downEvent) {
cursorEl.removeEventListener(downEvent, self.onTriggerDownBind);
});
data.upEvents.forEach(function (upEvent) {
cursorEl.removeEventListener(upEvent, self.onTriggerUpBind);
});
},
/**
* Remove mesh.
*/
remove: function () {
var cursorEl = this.el;
cursorEl.removeObject3D('cursormesh');
},
/**
* Trigger mousedown and keep track of the mousedowned entity.
*/
onTriggerDown: function (evt) {
this.twoWayEmit(EVENTS.MOUSEDOWN);
this.triggerDownEl = this.intersectedEl;
},
/**
* Trigger mouseup if:
* - Currently intersecting an entity.
* - Currently-intersected entity is the same as the one when mousedown was triggered,
* in case user mousedowned one entity, dragged to another, and mouseupped.
*/
onTriggerUp: function (evt) {
this.twoWayEmit(EVENTS.MOUSEUP);
if (!this.intersectedEl || this.triggerDownEl !== this.intersectedEl) { return; }
this.twoWayEmit(EVENTS.CLICK);
},
/**
* Handle intersection.
*/
onIntersection: function (evt) {
var self = this;
var cursorEl = this.el;
var index;
var intersectedEl;
var intersection;
// Select closest object, excluding the cursor.
index = evt.detail.els[0] === cursorEl ? 1 : 0;
intersection = evt.detail.intersections[index];
intersectedEl = evt.detail.els[index];
// If cursor is the only intersected object, ignore the event.
if (!intersectedEl) { return; }
// Already intersecting this entity.
if (this.intersectedEl === intersectedEl) {
this.intersection = intersection;
return;
}
// Unset current intersection.
if (this.intersectedEl) { this.clearCurrentIntersection(); }
// Set new intersection.
this.intersection = intersection;
this.intersectedEl = intersectedEl;
// Hovering.
cursorEl.addState(STATES.HOVERING);
intersectedEl.addState(STATES.HOVERED);
self.twoWayEmit(EVENTS.MOUSEENTER);
},
/**
* Handle intersection cleared.
*/
onIntersectionCleared: function (evt) {
var cursorEl = this.el;
var intersectedEl = evt.detail.el;
// Ignore the cursor.
if (cursorEl === intersectedEl) { return; }
// Ignore if the event didn't occur on the current intersection.
if (intersectedEl !== this.intersectedEl) { return; }
this.clearCurrentIntersection();
},
clearCurrentIntersection: function () {
var cursorEl = this.el;
// No longer hovering.
this.intersectedEl.removeState(STATES.HOVERED);
cursorEl.removeState(STATES.HOVERING);
this.twoWayEmit(EVENTS.MOUSELEAVE);
// Unset intersected entity (after emitting the event).
this.intersection = null;
this.intersectedEl = null;
},
/**
* Helper to emit on both the cursor and the intersected entity (if exists).
*/
twoWayEmit: function (evtName) {
var el = this.el;
var intersectedEl = this.intersectedEl;
var intersection = this.intersection;
el.emit(evtName, {intersectedEl: intersectedEl, intersection: intersection});
if (!intersectedEl) { return; }
intersectedEl.emit(evtName, {cursorEl: el, intersection: intersection});
}
});
/***/ })
/******/ ]);