This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
forked from ryanbetts/aframe-daydream-controller-component
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
291 lines (261 loc) · 10.4 KB
/
index.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
/* global AFRAME */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
var bind = AFRAME.utils.bind;
var trackedControlsUtils = AFRAME.utils.trackedControls;
var THREE = AFRAME.THREE;
var DAYDREAM_CONTROLLER_MODEL_OBJ_URL = 'https://raw.githubusercontent.com/TechnoBuddhist/VR-Controller-Daydream/master/vr_controller_daydream.obj';
var DAYDREAM_CONTROLLER_MODEL_OBJ_MTL = 'https://raw.githubusercontent.com/TechnoBuddhist/VR-Controller-Daydream/master/vr_controller_daydream.mtl';
var GAMEPAD_ID_PREFIX = 'Daydream Controller';
/**
* Daydream Controller component for A-Frame.
*/
AFRAME.registerComponent('daydream-controller', {
/**
* Set if component needs multiple instancing.
*/
multiple: false,
schema: {
buttonColor: {default: '#FAFAFA'}, // Off-white.
buttonTouchedColor: {default: 'yellow'}, // Light blue.
buttonPressedColor: {default: 'orange'}, // Light blue.
model: {default: true},
rotationOffset: {default: 0}, // use -999 as sentinel value to auto-determine based on hand
eyesToElbow: {default: {x: 0.175, y: -0.3, z: -0.03}}, // vector from eyes to elbow (divided by user height)
forearm: {default: {x: 0, y: 0, z: -0.175}}, // vector from eyes to elbow (divided by user height)
defaultUserHeight: {type: 'number', default: 1.6} // default user height (for cameras with zero)
},
// buttonId
// 0 - trackpad
mapping: {
axis0: 'trackpad',
axis1: 'trackpad',
button0: 'trackpad',
button1: 'menu',
button2: 'system'
},
/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () {
this.controllerPresent = false;
this.isControllerPresent = trackedControlsUtils.isControllerPresent; // to allow mock
this.buttonStates = {};
this.previousAxis = [];
this.onModelLoaded = bind(this.onModelLoaded, this);
this.checkIfControllerPresent = bind(this.checkIfControllerPresent, this);
this.onGamepadConnected = bind(this.onGamepadConnected, this);
this.onGamepadDisconnected = bind(this.onGamepadDisconnected, this);
},
tick: function (time, delta) {
if (!this.controller) return;
var mesh = this.el.getObject3D('mesh');
// Update mesh animations.
if (mesh && mesh.update) { mesh.update(delta / 1000); }
this.updatePose();
this.updateButtons();
},
/**
* Called when entity resumes.
* Use to continue or add any dynamic or background behavior such as events.
*/
play: function () {
this.checkIfControllerPresent();
window.addEventListener('gamepadconnected', this.onGamepadConnected, false);
window.addEventListener('gamepaddisconnected', this.onGamepadDisconnected, false);
},
/**
* Called when entity pauses.
* Use to stop or remove any dynamic or background behavior such as events.
*/
pause: function () {
window.removeEventListener('gamepadconnected', this.onGamepadConnected, false);
window.removeEventListener('gamepaddisconnected', this.onGamepadDisconnected, false);
},
/**
* Called when a component is removed (e.g., via removeAttribute).
* Generally undoes all modifications to the entity.
*/
// TODO ... remove: function () { },
checkIfControllerPresent: function () {
var isPresent = this.isControllerPresent(this.el.sceneEl, GAMEPAD_ID_PREFIX, {});
if (isPresent === this.controllerPresent) { return; }
this.controllerPresent = isPresent;
if (isPresent) {
this.el.addEventListener('model-loaded', this.onModelLoaded);
this.controller = trackedControlsUtils.getGamepadsByPrefix(GAMEPAD_ID_PREFIX)[0];
if (!this.data.model) { return; }
this.el.setAttribute('obj-model', {
obj: DAYDREAM_CONTROLLER_MODEL_OBJ_URL,
mtl: DAYDREAM_CONTROLLER_MODEL_OBJ_MTL
});
} else {
this.controller = null;
this.el.removeAttribute('obj-model');
this.el.removeEventListener('model-loaded', this.onModelLoaded);
}
},
onGamepadConnected: function (evt) {
this.checkIfControllerPresent();
},
onGamepadDisconnected: function (evt) {
this.checkIfControllerPresent();
},
onModelLoaded: function (evt) {
var controllerObject3D = evt.detail.model;
var buttonMeshes;
if (!this.data.model) { return; }
buttonMeshes = this.buttonMeshes = {};
buttonMeshes.menu = controllerObject3D.getObjectByName('AppButton_AppButton_Cylinder.004');
buttonMeshes.system = controllerObject3D.getObjectByName('HomeButton_HomeButton_Cylinder.005');
buttonMeshes.trackpad = controllerObject3D.getObjectByName('TouchPad_TouchPad_Cylinder.003');
// Offset pivot point
controllerObject3D.position.set(0, 0, -0.04);
},
updateButtonModel: function (buttonName, state) {
var color = this.data.buttonColor;
if (state === 'touchstart' || state === 'up') {
color = this.data.buttonTouchedColor;
} else if (state === 'down') {
color = this.data.buttonPressedColor;
}
var buttonMeshes = this.buttonMeshes;
if (!buttonMeshes) { return; }
buttonMeshes[buttonName].material.color.set(color);
},
updatePose: (function () {
var offset = new THREE.Vector3();
var position = new THREE.Vector3();
var controllerQuaternion = new THREE.Quaternion();
var controllerEuler = new THREE.Euler(0, 0, 0, 'YXZ');
return function () {
var controller = this.controller;
var pose = controller.pose;
var el = this.el;
var camera = this.el.sceneEl.camera;
var cameraComponent = camera.el.components.camera;
var eyesToElbow = this.data.eyesToElbow;
var forearm = this.data.forearm;
// get camera position
position.copy(camera.el.object3D.position);
// set offset for degenerate "arm model" to elbow
offset.set(
this.data.hand === 'left' ? -eyesToElbow.x : eyesToElbow.x, // hand is to your left, or right
eyesToElbow.y, // lower than your eyes
eyesToElbow.z); // slightly out in front
// scale offset by user height
offset.multiplyScalar(cameraComponent.data.userHeight || this.data.defaultUserHeight);
// apply camera Y rotation (not X or Z, so you can look down at your hand)
offset.applyAxisAngle(camera.el.object3D.up, camera.el.object3D.rotation.y);
// apply rotated offset to camera position
position.add(offset);
// set offset for degenerate "arm model" forearm
offset.set(forearm.x, forearm.y, forearm.z); // forearm sticking out from elbow
// scale offset by user height
offset.multiplyScalar(cameraComponent.data.userHeight || this.data.defaultUserHeight);
// apply controller X and Y rotation (tilting up/down/left/right is usually moving the arm)
controllerQuaternion.fromArray(pose.orientation || [0, 0, 0, 1]);
controllerEuler.setFromQuaternion(controllerQuaternion);
controllerEuler.set(controllerEuler.x, controllerEuler.y, 0);
offset.applyEuler(controllerEuler);
// apply rotated offset to camera position
position.add(offset);
// set as controller position
el.setAttribute('position', { x: position.x, y: position.y, z: position.z });
// set controller rotation directly from pose, if any (NO EULER!)
el.object3D.quaternion.copy(controllerQuaternion);
};
})(),
updateButtons: function () {
if (!this.controller) { return; }
this.handleTrackpadButton();
this.handleTrackpadGestures();
},
handleTrackpadGestures: function () {
var controllerAxes = this.controller.axes;
var previousAxis = this.previousAxis;
var changed = false;
var i;
for (i = 0; i < controllerAxes.length; ++i) {
if (previousAxis[i] !== controllerAxes[i]) {
changed = true;
break;
}
}
if (!changed) { return; }
this.previousAxis = controllerAxes.slice();
this.el.emit('axismove', {axis: this.previousAxis});
},
handleTrackpadButton: function () {
// handle all button states
var id = 0;
var buttonState = this.controller.buttons[id];
var changed = false;
changed = changed || this.handlePress(id, buttonState);
changed = changed || this.handleTrackpadTouch(id, buttonState);
if (!changed) { return; }
this.el.emit('buttonchanged', {id: id, state: buttonState});
},
handleMenuButton: function () {
// TODO: implement when Gamepad API starts returning menu button state
},
handleSystemButton: function () {
// TODO: implement when Gamepad API starts returning system button state
},
/**
* Determine whether a button press has occured and emit events as appropriate.
*
* @param {string} id - id of the button to check.
* @param {object} buttonState - state of the button to check.
* @returns {boolean} true if button press state changed, false otherwise.
*/
handlePress: function (id, buttonState) {
var buttonStates = this.buttonStates;
var evtName;
var buttonName;
var previousButtonState = buttonStates[id] = buttonStates[id] || {};
if (buttonState.pressed === previousButtonState.pressed) { return false; }
if (buttonState.pressed) {
evtName = 'down';
} else {
evtName = 'up';
}
this.el.emit('button' + evtName, {id: id});
buttonName = this.mapping['button' + id];
this.updateButtonModel(buttonName, evtName);
previousButtonState.pressed = buttonState.pressed;
return true;
},
/**
* Determine whether a button touch has occured and emit events as appropriate.
*
* @param {string} id - id of the button to check.
* @param {object} buttonState - state of the button to check.
* @returns {boolean} true if button touch state changed, false otherwise.
*/
handleTrackpadTouch: function (id, buttonState) {
var buttonStates = this.buttonStates;
var evtName;
var buttonName;
var previousButtonState = buttonStates[id] = buttonStates[id] || {};
if (buttonState.touched === previousButtonState.touched) { return false; }
if (buttonState.touched) {
evtName = 'start';
} else {
evtName = 'end';
}
previousButtonState.touched = buttonState.touched;
this.el.dispatchEvent(new CustomEvent('touch' + evtName, {
'touches': [], // avoid exception in TouchPanner due to namespace collision
'detail': {
id: id,
state: previousButtonState,
axis: this.controller.axes
}
}));
buttonName = this.mapping['button' + id];
this.updateButtonModel(buttonName, 'touch' + evtName);
return true;
}
});