Skip to content

Commit 325c594

Browse files
committed
optimize tracked controls tick and various utils
1 parent 06492d7 commit 325c594

File tree

6 files changed

+165
-136
lines changed

6 files changed

+165
-136
lines changed

‎src/components/daydream-controls.js‎

Lines changed: 31 additions & 19 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);
@@ -50,17 +46,25 @@ module.exports.Component = registerComponent('daydream-controls', {
5046
var self = this;
5147
this.animationActive = 'pointing';
5248
this.onButtonChanged = bind(this.onButtonChanged, this);
53-
this.onButtonDown = function (evt) { self.onButtonEvent(evt.detail.id, 'down'); };
54-
this.onButtonUp = function (evt) { self.onButtonEvent(evt.detail.id, 'up'); };
55-
this.onButtonTouchStart = function (evt) { self.onButtonEvent(evt.detail.id, 'touchstart'); };
56-
this.onButtonTouchEnd = function (evt) { self.onButtonEvent(evt.detail.id, 'touchend'); };
49+
this.onButtonDown = function (evt) {
50+
self.onButtonEvent(evt.detail.id, 'down');
51+
};
52+
this.onButtonUp = function (evt) {
53+
self.onButtonEvent(evt.detail.id, 'up');
54+
};
55+
this.onButtonTouchStart = function (evt) {
56+
self.onButtonEvent(evt.detail.id, 'touchstart');
57+
};
58+
this.onButtonTouchEnd = function (evt) {
59+
self.onButtonEvent(evt.detail.id, 'touchend');
60+
};
5761
this.onAxisMoved = bind(this.onAxisMoved, this);
5862
this.controllerPresent = false;
5963
this.everGotGamepadEvent = false;
6064
this.lastControllerCheck = 0;
6165
this.bindMethods();
62-
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // to allow mock
63-
this.emitIfAxesChanged = emitIfAxesChanged; // to allow mock
66+
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // To allow mock.
67+
this.emitIfAxesChanged = emitIfAxesChanged; // To allow mock.
6468
},
6569

6670
addEventListeners: function () {
@@ -117,7 +121,12 @@ module.exports.Component = registerComponent('daydream-controls', {
117121
injectTrackedControls: function () {
118122
var el = this.el;
119123
var data = this.data;
120-
el.setAttribute('tracked-controls', {idPrefix: GAMEPAD_ID_PREFIX, hand: data.hand, rotationOffset: data.rotationOffset, armModel: data.armModel});
124+
el.setAttribute('tracked-controls', {
125+
armModel: data.armModel,
126+
hand: data.hand,
127+
idPrefix: GAMEPAD_ID_PREFIX,
128+
rotationOffset: data.rotationOffset
129+
});
121130
if (!this.data.model) { return; }
122131
this.el.setAttribute('obj-model', {
123132
obj: DAYDREAM_CONTROLLER_MODEL_OBJ_URL,
@@ -137,21 +146,24 @@ module.exports.Component = registerComponent('daydream-controls', {
137146
if (!this.everGotGamepadEvent) { this.checkIfControllerPresent(); }
138147
},
139148

140-
// No need for onButtonChanged, since Daydream controller has no analog buttons.
141-
142149
onModelLoaded: function (evt) {
143150
var controllerObject3D = evt.detail.model;
144151
var buttonMeshes;
145152
if (!this.data.model) { return; }
146153
buttonMeshes = this.buttonMeshes = {};
147-
buttonMeshes.menu = controllerObject3D.getObjectByName('AppButton_AppButton_Cylinder.004');
148-
buttonMeshes.system = controllerObject3D.getObjectByName('HomeButton_HomeButton_Cylinder.005');
149-
buttonMeshes.trackpad = controllerObject3D.getObjectByName('TouchPad_TouchPad_Cylinder.003');
150-
// Offset pivot point
154+
buttonMeshes.menu = controllerObject3D.getObjectByName(
155+
'AppButton_AppButton_Cylinder.004');
156+
buttonMeshes.system = controllerObject3D.getObjectByName(
157+
'HomeButton_HomeButton_Cylinder.005');
158+
buttonMeshes.trackpad = controllerObject3D.getObjectByName(
159+
'TouchPad_TouchPad_Cylinder.003');
160+
// Offset pivot point.
151161
controllerObject3D.position.set(0, 0, -0.04);
152162
},
153163

154-
onAxisMoved: function (evt) { this.emitIfAxesChanged(this, this.mapping.axes, evt); },
164+
onAxisMoved: function (evt) {
165+
this.emitIfAxesChanged(this, this.mapping.axes, evt);
166+
},
155167

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

‎src/components/gearvr-controls.js‎

Lines changed: 26 additions & 15 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);
@@ -51,17 +47,25 @@ module.exports.Component = registerComponent('gearvr-controls', {
5147
var self = this;
5248
this.animationActive = 'pointing';
5349
this.onButtonChanged = bind(this.onButtonChanged, this);
54-
this.onButtonDown = function (evt) { self.onButtonEvent(evt.detail.id, 'down'); };
55-
this.onButtonUp = function (evt) { self.onButtonEvent(evt.detail.id, 'up'); };
56-
this.onButtonTouchStart = function (evt) { self.onButtonEvent(evt.detail.id, 'touchstart'); };
57-
this.onButtonTouchEnd = function (evt) { self.onButtonEvent(evt.detail.id, 'touchend'); };
50+
this.onButtonDown = function (evt) {
51+
self.onButtonEvent(evt.detail.id, 'down');
52+
};
53+
this.onButtonUp = function (evt) {
54+
self.onButtonEvent(evt.detail.id, 'up');
55+
};
56+
this.onButtonTouchStart = function (evt) {
57+
self.onButtonEvent(evt.detail.id, 'touchstart');
58+
};
59+
this.onButtonTouchEnd = function (evt) {
60+
self.onButtonEvent(evt.detail.id, 'touchend');
61+
};
5862
this.onAxisMoved = bind(this.onAxisMoved, this);
5963
this.controllerPresent = false;
6064
this.everGotGamepadEvent = false;
6165
this.lastControllerCheck = 0;
6266
this.bindMethods();
63-
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // to allow mock
64-
this.emitIfAxesChanged = emitIfAxesChanged; // to allow mock
67+
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // To allow mock.
68+
this.emitIfAxesChanged = emitIfAxesChanged; // To allow mock.
6569
},
6670

6771
addEventListeners: function () {
@@ -91,7 +95,8 @@ module.exports.Component = registerComponent('gearvr-controls', {
9195
},
9296

9397
checkIfControllerPresent: function () {
94-
this.checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX, this.data.hand ? {hand: this.data.hand} : {});
98+
this.checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX,
99+
this.data.hand ? {hand: this.data.hand} : {});
95100
},
96101

97102
onGamepadConnectionEvent: function (evt) {
@@ -115,7 +120,11 @@ module.exports.Component = registerComponent('gearvr-controls', {
115120
injectTrackedControls: function () {
116121
var el = this.el;
117122
var data = this.data;
118-
el.setAttribute('tracked-controls', {idPrefix: GAMEPAD_ID_PREFIX, rotationOffset: data.rotationOffset, armModel: data.armModel});
123+
el.setAttribute('tracked-controls', {
124+
armModel: data.armModel,
125+
idPrefix: GAMEPAD_ID_PREFIX,
126+
rotationOffset: data.rotationOffset
127+
});
119128
if (!this.data.model) { return; }
120129
this.el.setAttribute('obj-model', {
121130
obj: GEARVR_CONTROLLER_MODEL_OBJ_URL,
@@ -167,7 +176,9 @@ module.exports.Component = registerComponent('gearvr-controls', {
167176
this.updateModel(buttonName, evtName);
168177
},
169178

170-
onAxisMoved: function (evt) { this.emitIfAxesChanged(this, this.mapping.axes, evt); },
179+
onAxisMoved: function (evt) {
180+
this.emitIfAxesChanged(this, this.mapping.axes, evt);
181+
},
171182

172183
updateModel: function (buttonName, evtName) {
173184
var i;

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

Lines changed: 23 additions & 18 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);
@@ -60,17 +56,26 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
6056
init: function () {
6157
var self = this;
6258
this.onButtonChanged = bind(this.onButtonChanged, this);
63-
this.onButtonDown = function (evt) { self.onButtonEvent(evt.detail.id, 'down'); };
64-
this.onButtonUp = function (evt) { self.onButtonEvent(evt.detail.id, 'up'); };
65-
this.onButtonTouchStart = function (evt) { self.onButtonEvent(evt.detail.id, 'touchstart'); };
66-
this.onButtonTouchEnd = function (evt) { self.onButtonEvent(evt.detail.id, 'touchend'); };
59+
this.onButtonDown = function (evt) {
60+
self.onButtonEvent(evt.detail.id, 'down');
61+
};
62+
this.onButtonUp = function (evt) {
63+
self.onButtonEvent(evt.detail.id, 'up');
64+
};
65+
this.onButtonTouchStart = function (evt) {
66+
self.onButtonEvent(evt.detail.id, 'touchstart');
67+
};
68+
this.onButtonTouchEnd = function (evt) {
69+
self.onButtonEvent(evt.detail.id, 'touchend');
70+
};
6771
this.controllerPresent = false;
6872
this.lastControllerCheck = 0;
6973
this.previousButtonValues = {};
7074
this.bindMethods();
7175

72-
this.emitIfAxesChanged = controllerUtils.emitIfAxesChanged; // Allow mock.
73-
this.checkControllerPresentAndSetup = controllerUtils.checkControllerPresentAndSetup; // Allow mock.
76+
// Allow mock.
77+
this.emitIfAxesChanged = controllerUtils.emitIfAxesChanged;
78+
this.checkControllerPresentAndSetup = controllerUtils.checkControllerPresentAndSetup;
7479
},
7580

7681
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)