forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepad-controls.js
345 lines (281 loc) · 10.2 KB
/
gamepad-controls.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* Gamepad controls for A-Frame.
*
* Stripped-down version of: https://github.com/donmccurdy/aframe-gamepad-controls
*
* For more information about the Gamepad API, see:
* https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API
*/
const GamepadButton = require('../../lib/GamepadButton'),
GamepadButtonEvent = require('../../lib/GamepadButtonEvent');
const JOYSTICK_EPS = 0.2;
const Hand = {
LEFT: 'left',
RIGHT: 'right'
};
const Joystick = {
MOVEMENT: 1,
ROTATION: 2
};
module.exports = AFRAME.registerComponent('gamepad-controls', {
/*******************************************************************
* Statics
*/
GamepadButton: GamepadButton,
/*******************************************************************
* Schema
*/
schema: {
// Enable/disable gamepad-controls
enabled: { default: true },
// Heading element for rotation
camera: { default: '[camera]', type: 'selector' },
// Rotation sensitivity
rotationSensitivity: { default: 2.0 },
},
/*******************************************************************
* Core
*/
/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () {
const sceneEl = this.el.sceneEl;
this.system = sceneEl.systems['tracked-controls-webxr'] || {controllers: []};
this.prevTime = window.performance.now();
// Button state
this.buttons = {};
// Rotation
const rotation = this.el.object3D.rotation;
this.pitch = new THREE.Object3D();
this.pitch.rotation.x = THREE.Math.degToRad(rotation.x);
this.yaw = new THREE.Object3D();
this.yaw.position.y = 10;
this.yaw.rotation.y = THREE.Math.degToRad(rotation.y);
this.yaw.add(this.pitch);
this._lookVector = new THREE.Vector2();
this._moveVector = new THREE.Vector2();
this._dpadVector = new THREE.Vector2();
sceneEl.addBehavior(this);
},
/**
* Called when component is attached and when component data changes.
* Generally modifies the entity based on the data.
*/
update: function () { this.tick(); },
/**
* Called on each iteration of main render loop.
*/
tick: function (t, dt) {
this.updateButtonState();
this.updateRotation(dt);
},
/**
* Called when a component is removed (e.g., via removeAttribute).
* Generally undoes all modifications to the entity.
*/
remove: function () { },
/*******************************************************************
* Movement
*/
isVelocityActive: function () {
if (!this.data.enabled || !this.isConnected()) return false;
const dpad = this._dpadVector;
const joystick = this._moveVector;
this.getDpad(dpad);
this.getJoystick(Joystick.MOVEMENT, joystick);
const inputX = dpad.x || joystick.x;
const inputY = dpad.y || joystick.y;
return Math.abs(inputX) > JOYSTICK_EPS || Math.abs(inputY) > JOYSTICK_EPS;
},
getVelocityDelta: function () {
const dpad = this._dpadVector;
const joystick = this._moveVector;
this.getDpad(dpad);
this.getJoystick(Joystick.MOVEMENT, joystick);
const inputX = dpad.x || joystick.x;
const inputY = dpad.y || joystick.y;
const dVelocity = new THREE.Vector3();
if (Math.abs(inputX) > JOYSTICK_EPS) {
dVelocity.x += inputX;
}
if (Math.abs(inputY) > JOYSTICK_EPS) {
dVelocity.z += inputY;
}
return dVelocity;
},
/*******************************************************************
* Rotation
*/
isRotationActive: function () {
if (!this.data.enabled || !this.isConnected()) return false;
const joystick = this._lookVector;
this.getJoystick(Joystick.ROTATION, joystick);
return Math.abs(joystick.x) > JOYSTICK_EPS || Math.abs(joystick.y) > JOYSTICK_EPS;
},
updateRotation: function (dt) {
if (!this.isRotationActive()) return;
const data = this.data;
const yaw = this.yaw;
const pitch = this.pitch;
const lookControls = data.camera.components['look-controls'];
const hasLookControls = lookControls && lookControls.pitchObject && lookControls.yawObject;
// Sync with look-controls pitch/yaw if available.
if (hasLookControls) {
pitch.rotation.copy(lookControls.pitchObject.rotation);
yaw.rotation.copy(lookControls.yawObject.rotation);
}
const lookVector = this._lookVector;
this.getJoystick(Joystick.ROTATION, lookVector);
if (Math.abs(lookVector.x) <= JOYSTICK_EPS) lookVector.x = 0;
if (Math.abs(lookVector.y) <= JOYSTICK_EPS) lookVector.y = 0;
lookVector.multiplyScalar(data.rotationSensitivity * dt / 1000);
yaw.rotation.y -= lookVector.x;
pitch.rotation.x -= lookVector.y;
pitch.rotation.x = Math.max(- Math.PI / 2, Math.min(Math.PI / 2, pitch.rotation.x));
data.camera.object3D.rotation.set(pitch.rotation.x, yaw.rotation.y, 0);
// Sync with look-controls pitch/yaw if available.
if (hasLookControls) {
lookControls.pitchObject.rotation.copy(pitch.rotation);
lookControls.yawObject.rotation.copy(yaw.rotation);
}
},
/*******************************************************************
* Button events
*/
updateButtonState: function () {
const gamepad = this.getGamepad(Hand.RIGHT);
if (this.data.enabled && gamepad) {
// Fire DOM events for button state changes.
for (var i = 0; i < gamepad.buttons.length; i++) {
if (gamepad.buttons[i].pressed && !this.buttons[i]) {
this.emit(new GamepadButtonEvent('gamepadbuttondown', i, gamepad.buttons[i]));
} else if (!gamepad.buttons[i].pressed && this.buttons[i]) {
this.emit(new GamepadButtonEvent('gamepadbuttonup', i, gamepad.buttons[i]));
}
this.buttons[i] = gamepad.buttons[i].pressed;
}
} else if (Object.keys(this.buttons)) {
// Reset state if controls are disabled or controller is lost.
this.buttons = {};
}
},
emit: function (event) {
// Emit original event.
this.el.emit(event.type, event);
// Emit convenience event, identifying button index.
this.el.emit(
event.type + ':' + event.index,
new GamepadButtonEvent(event.type, event.index, event)
);
},
/*******************************************************************
* Gamepad state
*/
/**
* Returns the Gamepad instance attached to the component. If connected,
* a proxy-controls component may provide access to Gamepad input from a
* remote device.
*
* @param {string} handPreference
* @return {Gamepad}
*/
getGamepad: (function () {
const _xrGamepads = [];
const _empty = [];
return function (handPreference) {
// https://github.com/donmccurdy/aframe-proxy-controls
const proxyControls = this.el.sceneEl.components['proxy-controls'];
const proxyGamepad = proxyControls && proxyControls.isConnected()
&& proxyControls.getGamepad(0);
if (proxyGamepad) return proxyGamepad;
// https://www.w3.org/TR/webxr/#dom-xrinputsource-handedness
_xrGamepads.length = 0;
for (let i = 0; i < this.system.controllers.length; i++) {
const xrController = this.system.controllers[i];
const xrGamepad = xrController ? xrController.gamepad : null;
_xrGamepads.push(xrGamepad);
if (xrGamepad && xrGamepad.handedness === handPreference) return xrGamepad;
}
// https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/hand
const navGamepads = navigator.getGamepads ? navigator.getGamepads() : _empty;
for (let i = 0; i < navGamepads.length; i++) {
const navGamepad = navGamepads[i];
if (navGamepad && navGamepad.hand === handPreference) return navGamepad;
}
return _xrGamepads[0] || navGamepads[0];
};
})(),
/**
* Returns the state of the given button.
* @param {number} index The button (0-N) for which to find state.
* @return {GamepadButton}
*/
getButton: function (index) {
return this.getGamepad(Hand.RIGHT).buttons[index];
},
/**
* Returns state of the given axis. Axes are labelled 0-N, where 0-1 will
* represent X/Y on the first joystick, and 2-3 X/Y on the second.
* @param {number} index The axis (0-N) for which to find state.
* @return {number} On the interval [-1,1].
*/
getAxis: function (index) {
return this.getGamepad(index > 1 ? Hand.RIGHT : Hand.LEFT).axes[index];
},
/**
* Returns the state of the specified joystick as a THREE.Vector2.
* @param {Joystick} role
* @param {THREE.Vector2} target
* @return {THREE.Vector2}
*/
getJoystick: function (index, target) {
const gamepad = this.getGamepad(index === Joystick.MOVEMENT ? Hand.LEFT : Hand.RIGHT);
if (gamepad.mapping === 'xr-standard') {
// See: https://github.com/donmccurdy/aframe-extras/issues/307
switch (index) {
case Joystick.MOVEMENT: return target.set(gamepad.axes[2], gamepad.axes[3]);
case Joystick.ROTATION: return target.set(gamepad.axes[0], gamepad.axes[1]);
}
} else {
switch (index) {
case Joystick.MOVEMENT: return target.set(gamepad.axes[0], gamepad.axes[1]);
case Joystick.ROTATION: return target.set(gamepad.axes[2], gamepad.axes[3]);
}
}
throw new Error('Unexpected joystick index "%d".', index);
},
/**
* Returns the state of the dpad as a THREE.Vector2.
* @param {THREE.Vector2} target
* @return {THREE.Vector2}
*/
getDpad: function (target) {
const gamepad = this.getGamepad(Hand.LEFT);
if (!gamepad.buttons[GamepadButton.DPAD_RIGHT]) {
return target.set(0, 0);
}
return target.set(
(gamepad.buttons[GamepadButton.DPAD_RIGHT].pressed ? 1 : 0)
+ (gamepad.buttons[GamepadButton.DPAD_LEFT].pressed ? -1 : 0),
(gamepad.buttons[GamepadButton.DPAD_UP].pressed ? -1 : 0)
+ (gamepad.buttons[GamepadButton.DPAD_DOWN].pressed ? 1 : 0)
);
},
/**
* Returns true if the gamepad is currently connected to the system.
* @return {boolean}
*/
isConnected: function () {
const gamepad = this.getGamepad(Hand.LEFT);
return !!(gamepad && gamepad.connected);
},
/**
* Returns a string containing some information about the controller. Result
* may vary across browsers, for a given controller.
* @return {string}
*/
getID: function () {
return this.getGamepad(Hand.LEFT).id;
}
});