Skip to content

Commit 88fb20b

Browse files
committed
optimize tracked controls tick and various utils
1 parent 06492d7 commit 88fb20b

File tree

6 files changed

+123
-121
lines changed

6 files changed

+123
-121
lines changed

‎src/components/daydream-controls.js‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ module.exports.Component = registerComponent('daydream-controls', {
2929
// 1 - menu (never dispatched on this layer)
3030
// 2 - system (never dispatched on this layer)
3131
mapping: {
32-
axes: {'trackpad': [0, 1]},
32+
axes: {trackpad: [0, 1]},
3333
buttons: ['trackpad', 'menu', 'system']
3434
},
3535

36-
// Use these labels for detail on axis events such as thumbstickmoved.
37-
// e.g. for thumbstickmoved detail, the first axis returned is labeled x, and the second is labeled y.
38-
axisLabels: ['x', 'y', 'z', 'w'],
39-
4036
bindMethods: function () {
4137
this.onModelLoaded = bind(this.onModelLoaded, this);
4238
this.onControllersUpdate = bind(this.onControllersUpdate, this);
@@ -59,8 +55,8 @@ module.exports.Component = registerComponent('daydream-controls', {
5955
this.everGotGamepadEvent = false;
6056
this.lastControllerCheck = 0;
6157
this.bindMethods();
62-
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // to allow mock
63-
this.emitIfAxesChanged = emitIfAxesChanged; // to allow mock
58+
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // To allow mock.
59+
this.emitIfAxesChanged = emitIfAxesChanged; // To allow mock.
6460
},
6561

6662
addEventListeners: function () {
@@ -117,7 +113,12 @@ module.exports.Component = registerComponent('daydream-controls', {
117113
injectTrackedControls: function () {
118114
var el = this.el;
119115
var data = this.data;
120-
el.setAttribute('tracked-controls', {idPrefix: GAMEPAD_ID_PREFIX, hand: data.hand, rotationOffset: data.rotationOffset, armModel: data.armModel});
116+
el.setAttribute('tracked-controls', {
117+
armModel: data.armModel,
118+
hand: data.hand,
119+
idPrefix: GAMEPAD_ID_PREFIX,
120+
rotationOffset: data.rotationOffset
121+
});
121122
if (!this.data.model) { return; }
122123
this.el.setAttribute('obj-model', {
123124
obj: DAYDREAM_CONTROLLER_MODEL_OBJ_URL,
@@ -137,8 +138,6 @@ module.exports.Component = registerComponent('daydream-controls', {
137138
if (!this.everGotGamepadEvent) { this.checkIfControllerPresent(); }
138139
},
139140

140-
// No need for onButtonChanged, since Daydream controller has no analog buttons.
141-
142141
onModelLoaded: function (evt) {
143142
var controllerObject3D = evt.detail.model;
144143
var buttonMeshes;
@@ -147,11 +146,13 @@ module.exports.Component = registerComponent('daydream-controls', {
147146
buttonMeshes.menu = controllerObject3D.getObjectByName('AppButton_AppButton_Cylinder.004');
148147
buttonMeshes.system = controllerObject3D.getObjectByName('HomeButton_HomeButton_Cylinder.005');
149148
buttonMeshes.trackpad = controllerObject3D.getObjectByName('TouchPad_TouchPad_Cylinder.003');
150-
// Offset pivot point
149+
// Offset pivot point.
151150
controllerObject3D.position.set(0, 0, -0.04);
152151
},
153152

154-
onAxisMoved: function (evt) { this.emitIfAxesChanged(this, this.mapping.axes, evt); },
153+
onAxisMoved: function (evt) {
154+
this.emitIfAxesChanged(this, this.mapping.axes, evt);
155+
},
155156

156157
onButtonChanged: function (evt) {
157158
var button = this.mapping.buttons[evt.detail.id];

‎src/components/gearvr-controls.js‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,18 @@ module.exports.Component = registerComponent('gearvr-controls', {
2222
buttonTouchedColor: {type: 'color', default: '#777777'},
2323
buttonHighlightColor: {type: 'color', default: '#FFFFFF'},
2424
model: {default: true},
25-
rotationOffset: {default: 0}, // use -999 as sentinel value to auto-determine based on hand
25+
rotationOffset: {default: 0},
2626
armModel: {default: true}
2727
},
2828

2929
// buttonId
3030
// 0 - trackpad
3131
// 1 - triggeri
3232
mapping: {
33-
axes: {'trackpad': [0, 1]},
33+
axes: {trackpad: [0, 1]},
3434
buttons: ['trackpad', 'trigger']
3535
},
3636

37-
// Use these labels for detail on axis events such as thumbstickmoved.
38-
// e.g. for thumbstickmoved detail, the first axis returned is labeled x, and the second is labeled y.
39-
axisLabels: ['x', 'y', 'z', 'w'],
40-
4137
bindMethods: function () {
4238
this.onModelLoaded = bind(this.onModelLoaded, this);
4339
this.onControllersUpdate = bind(this.onControllersUpdate, this);
@@ -60,8 +56,8 @@ module.exports.Component = registerComponent('gearvr-controls', {
6056
this.everGotGamepadEvent = false;
6157
this.lastControllerCheck = 0;
6258
this.bindMethods();
63-
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // to allow mock
64-
this.emitIfAxesChanged = emitIfAxesChanged; // to allow mock
59+
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // To allow mock.
60+
this.emitIfAxesChanged = emitIfAxesChanged; // To allow mock.
6561
},
6662

6763
addEventListeners: function () {
@@ -91,7 +87,8 @@ module.exports.Component = registerComponent('gearvr-controls', {
9187
},
9288

9389
checkIfControllerPresent: function () {
94-
this.checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX, this.data.hand ? {hand: this.data.hand} : {});
90+
this.checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX,
91+
this.data.hand ? {hand: this.data.hand} : {});
9592
},
9693

9794
onGamepadConnectionEvent: function (evt) {
@@ -115,7 +112,11 @@ module.exports.Component = registerComponent('gearvr-controls', {
115112
injectTrackedControls: function () {
116113
var el = this.el;
117114
var data = this.data;
118-
el.setAttribute('tracked-controls', {idPrefix: GAMEPAD_ID_PREFIX, rotationOffset: data.rotationOffset, armModel: data.armModel});
115+
el.setAttribute('tracked-controls', {
116+
armModel: data.armModel,
117+
idPrefix: GAMEPAD_ID_PREFIX,
118+
rotationOffset: data.rotationOffset
119+
});
119120
if (!this.data.model) { return; }
120121
this.el.setAttribute('obj-model', {
121122
obj: GEARVR_CONTROLLER_MODEL_OBJ_URL,
@@ -167,7 +168,9 @@ module.exports.Component = registerComponent('gearvr-controls', {
167168
this.updateModel(buttonName, evtName);
168169
},
169170

170-
onAxisMoved: function (evt) { this.emitIfAxesChanged(this, this.mapping.axes, evt); },
171+
onAxisMoved: function (evt) {
172+
this.emitIfAxesChanged(this, this.mapping.axes, evt);
173+
},
171174

172175
updateModel: function (buttonName, evtName) {
173176
var i;

‎src/components/oculus-touch-controls.js‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ var GAMEPAD_ID_PREFIX = 'Oculus Touch';
1313
var PIVOT_OFFSET = {x: 0, y: -0.015, z: 0.04};
1414

1515
/**
16-
* Oculus Touch Controls Component
17-
* Interfaces with Oculus Touch controllers and maps Gamepad events to
16+
* Oculus Touch controls component.
17+
* Interface with Oculus Touch controllers and maps Gamepad events to
1818
* common controller buttons: trackpad, trigger, grip, menu and system
19-
* It loads a controller model and highlights the pressed buttons
19+
* Load a controller model and highlights the pressed buttons
2020
*/
2121
module.exports.Component = registerComponent('oculus-touch-controls', {
2222
schema: {
2323
hand: {default: 'left'},
24-
buttonColor: {type: 'color', default: '#999'}, // Off-white.
24+
buttonColor: {type: 'color', default: '#999'}, // Off-white.
2525
buttonTouchColor: {type: 'color', default: '#8AB'},
26-
buttonHighlightColor: {type: 'color', default: '#2DF'}, // Light blue.
26+
buttonHighlightColor: {type: 'color', default: '#2DF'}, // Light blue.
2727
model: {default: true},
28-
rotationOffset: {default: 0} // no default offset; -999 is sentinel value to auto-determine based on hand
28+
rotationOffset: {default: 0}
2929
},
3030

3131
// buttonId
@@ -37,19 +37,15 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
3737
// 5 - surface (touch only)
3838
mapping: {
3939
left: {
40-
axes: {'thumbstick': [0, 1]},
40+
axes: {thumbstick: [0, 1]},
4141
buttons: ['thumbstick', 'trigger', 'grip', 'xbutton', 'ybutton', 'surface']
4242
},
4343
right: {
44-
axes: {'thumbstick': [0, 1]},
44+
axes: {thumbstick: [0, 1]},
4545
buttons: ['thumbstick', 'trigger', 'grip', 'abutton', 'bbutton', 'surface']
4646
}
4747
},
4848

49-
// Use these labels for detail on axis events such as thumbstickmoved.
50-
// e.g. for thumbstickmoved detail, the first axis returned is labeled x, and the second is labeled y.
51-
axisLabels: ['x', 'y', 'z', 'w'],
52-
5349
bindMethods: function () {
5450
this.onModelLoaded = bind(this.onModelLoaded, this);
5551
this.onControllersUpdate = bind(this.onControllersUpdate, this);
@@ -69,8 +65,9 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
6965
this.previousButtonValues = {};
7066
this.bindMethods();
7167

72-
this.emitIfAxesChanged = controllerUtils.emitIfAxesChanged; // Allow mock.
73-
this.checkControllerPresentAndSetup = controllerUtils.checkControllerPresentAndSetup; // Allow mock.
68+
// Allow mock.
69+
this.emitIfAxesChanged = controllerUtils.emitIfAxesChanged;
70+
this.checkControllerPresentAndSetup = controllerUtils.checkControllerPresentAndSetup;
7471
},
7572

7673
addEventListeners: function () {

‎src/components/vive-controls.js‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,10 @@ module.exports.Component = registerComponent('vive-controls', {
3434
* 4 - system (never dispatched on this layer)
3535
*/
3636
mapping: {
37-
axes: {'trackpad': [0, 1]},
37+
axes: {trackpad: [0, 1]},
3838
buttons: ['trackpad', 'trigger', 'grip', 'menu', 'system']
3939
},
4040

41-
/**
42-
* Labels for detail on axis events such as `thumbstickmoved`.
43-
* For example, on `thumbstickmoved` detail, the first axis returned is labeled x, and the
44-
* second is labeled y.
45-
*/
46-
axisLabels: ['x', 'y', 'z', 'w'],
47-
4841
init: function () {
4942
var self = this;
5043
this.animationActive = 'pointing';

‎src/systems/tracked-controls.js‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,58 @@
11
var registerSystem = require('../core/system').registerSystem;
2-
var trackedControlsUtils = require('../utils/tracked-controls');
32
var utils = require('../utils');
43

54
/**
65
* Tracked controls system.
7-
* It maintains a list with the available tracked controllers
6+
* Maintain list with available tracked controllers.
87
*/
98
module.exports.System = registerSystem('tracked-controls', {
109
init: function () {
1110
var self = this;
11+
1212
this.controllers = [];
13-
this.lastControllersUpdate = 0;
14-
// Throttle the (renamed) tick handler to minimum 10ms interval.
15-
this.tick = utils.throttle(this.throttledTick, 10, this);
13+
14+
// Throttle to every 500ms.
15+
this.tick = utils.throttle(this.tick, 500, this);
16+
17+
this.updateControllerList();
18+
1619
if (!navigator.getVRDisplays) { return; }
20+
1721
this.sceneEl.addEventListener('enter-vr', function () {
1822
navigator.getVRDisplays().then(function (displays) {
1923
if (displays.length) { self.vrDisplay = displays[0]; }
2024
});
2125
});
2226
},
2327

24-
updateControllerList: function () {
25-
var controllers = this.controllers = [];
26-
var gamepads = trackedControlsUtils.getGamepadsByPrefix('');
27-
for (var i = 0; i < gamepads.length; i++) {
28-
var gamepad = gamepads[i];
29-
if (gamepad && gamepad.pose) { controllers.push(gamepad); }
30-
}
28+
tick: function () {
29+
this.updateControllerList();
3130
},
3231

3332
/**
34-
* Update controller list every 10 miliseconds.
33+
* Update controller list.
3534
*/
36-
throttledTick: function () {
37-
this.updateControllerList();
38-
this.sceneEl.emit('controllersupdated', { controllers: this.controllers });
35+
updateControllerList: function () {
36+
var controllers = this.controllers;
37+
var gamepad;
38+
var gamepads;
39+
var i;
40+
var prevCount;
41+
42+
gamepads = navigator.getGamepads && navigator.getGamepads();
43+
if (!gamepads) { return; }
44+
45+
prevCount = controllers.length;
46+
controllers.length = 0;
47+
for (i = 0; i < gamepads.length; ++i) {
48+
gamepad = gamepads[i];
49+
if (gamepad && gamepad.pose) {
50+
controllers.push(gamepad);
51+
}
52+
}
53+
54+
if (controllers.length !== prevCount) {
55+
this.el.emit('controllersupdated', undefined, false);
56+
}
3957
}
4058
});

0 commit comments

Comments
 (0)