Skip to content

Commit fa0cc74

Browse files
committed
Improve entity and controller model visibility handling when there are controller switches. e.g Logitech MX Ink pen to Touch controllers
1 parent 1037860 commit fa0cc74

File tree

7 files changed

+39
-32
lines changed

7 files changed

+39
-32
lines changed

‎examples/showcase/painter/components/brush.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ AFRAME.registerComponent('brush', {
3636
onControllerConnected: function (evt) {
3737
this.hand = evt.target.getAttribute(evt.detail.name).hand;
3838
this.controllerName = evt.detail.name;
39+
this.controllerJustConnected = true;
3940
},
4041

4142
onTouchStarted: function (evt) {
@@ -74,6 +75,13 @@ AFRAME.registerComponent('brush', {
7475

7576
return function tick (time, delta) {
7677
if (!this.painting || !this.stroke) { return; }
78+
// Skips first frame when a controller have just connected.
79+
// It prevents using the last position of the previous controller as part of the stroke.
80+
// This can happen when there's a controller switch. e.g From pen to touch controller or viceversa
81+
if (this.controllerJustConnected) {
82+
this.controllerJustConnected = false;
83+
return;
84+
}
7785
this.el.object3D.matrixWorld.decompose(position, rotation, scale);
7886
var pointerPosition = this.getPointerPosition(position, rotation);
7987
this.stroke.addPoint(position, rotation, pointerPosition);

‎examples/showcase/painter/index.html‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
<a-entity environment hide-on-enter-ar></a-entity>
2222
<a-entity
2323
id="right-hand"
24-
hand-tracking-controls="hand: right"
2524
brush="hand: right">
2625
</a-entity>
2726
<a-entity
2827
id="left-hand"
29-
hand-tracking-controls="hand: left"
3028
brush="hand: left">
3129
</a-entity>
3230
</a-scene>

‎src/components/hand-tracking-controls.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ module.exports.Component = registerComponent('hand-tracking-controls', {
202202
})(),
203203

204204
updateHandModel: function () {
205+
this.wristObject3D.visible = true;
206+
this.el.object3D.visible = true;
207+
205208
if (this.data.modelStyle === 'dots') {
206209
this.updateHandDotsModel();
207210
}

‎src/components/logitech-mx-ink-controls.js‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ module.exports.Component = registerComponent('logitech-mx-ink-controls', {
102102

103103
checkIfControllerPresent: function () {
104104
var data = this.data;
105+
var controllerObject3D = this.controllerObject3D;
106+
if (controllerObject3D) { controllerObject3D.visible = false; }
105107
checkControllerPresentAndSetup(this, GAMEPAD_ID, {
106108
hand: this.data.hand,
107109
iterateControllerProfiles: true
@@ -111,19 +113,25 @@ module.exports.Component = registerComponent('logitech-mx-ink-controls', {
111113
injectTrackedControls: function () {
112114
var el = this.el;
113115
var data = this.data;
116+
var id = GAMEPAD_ID;
114117
el.setAttribute('tracked-controls', {
118+
id: id,
115119
hand: data.hand,
116-
idPrefix: GAMEPAD_ID,
120+
handTrackingEnabled: false,
121+
iterateControllerProfiles: true,
117122
orientationOffset: data.orientationOffset,
118123
space: 'gripSpace'
119124
});
120125
this.loadModel();
121126
},
122127

123128
loadModel: function () {
129+
var controllerObject3D = this.controllerObject3D;
124130
if (!this.data.model) { return; }
125-
if (this.controllerObject3D) {
126-
this.controllerObject3D.visible = this.el.sceneEl.is('vr-mode');
131+
if (controllerObject3D) {
132+
controllerObject3D.visible = this.el.sceneEl.is('vr-mode');
133+
this.el.setObject3D('mesh', controllerObject3D);
134+
return;
127135
}
128136
this.el.setAttribute('gltf-model', LOGITECH_MX_INK_MODEL_GLB_BASE_URL + 'logitech-mx-ink.glb');
129137
},
@@ -137,7 +145,6 @@ module.exports.Component = registerComponent('logitech-mx-ink-controls', {
137145
},
138146

139147
onControllersUpdate: function () {
140-
// Note that due to gamepadconnected event propagation issues, we don't rely on events.
141148
this.checkIfControllerPresent();
142149
},
143150

@@ -165,10 +172,7 @@ module.exports.Component = registerComponent('logitech-mx-ink-controls', {
165172
});
166173

167174
this.controllerObject3D = this.el.getObject3D('mesh');
168-
169-
if (this.el.sceneEl.is('ar-mode')) {
170-
this.controllerObject3D.visible = false;
171-
}
175+
this.controllerObject3D.visible = this.el.sceneEl.is('vr-mode');
172176
},
173177

174178
onAxisMoved: function (evt) {

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
144144
this.onThumbstickMoved = this.onThumbstickMoved.bind(this);
145145
this.onModelLoaded = this.onModelLoaded.bind(this);
146146
this.onControllersUpdate = this.onControllersUpdate.bind(this);
147-
this.onControllerDisconnected = this.onControllerDisconnected.bind(this);
148147
this.checkIfControllerPresent = this.checkIfControllerPresent.bind(this);
149148
this.onAxisMoved = this.onAxisMoved.bind(this);
150149
},
@@ -188,6 +187,8 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
188187
},
189188

190189
checkIfControllerPresent: function () {
190+
var controllerObject3D = this.controllerObject3D;
191+
if (controllerObject3D) { controllerObject3D.visible = false; }
191192
checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX, {
192193
hand: this.data.hand,
193194
iterateControllerProfiles: true
@@ -208,10 +209,11 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
208209
var data = this.data;
209210
var controllerId;
210211
if (!data.model) { return; }
212+
211213
// If model has been already loaded
212214
if (this.controllerObject3D) {
213-
this.el.setObject3D('mesh', this.controllerObject3D);
214215
this.controllerObject3D.visible = true;
216+
this.el.setObject3D('mesh', this.controllerObject3D);
215217
return;
216218
}
217219

@@ -252,21 +254,13 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
252254

253255
addControllersUpdateListener: function () {
254256
this.el.sceneEl.addEventListener('controllersupdated', this.onControllersUpdate, false);
255-
this.el.addEventListener('controllerdisconnected', this.onControllerDisconnected);
256257
},
257258

258259
removeControllersUpdateListener: function () {
259260
this.el.sceneEl.removeEventListener('controllersupdated', this.onControllersUpdate, false);
260-
this.el.removeEventListener('controllerdisconnected', this.onControllerDisconnected);
261-
},
262-
263-
onControllerDisconnected: function () {
264-
if (!this.controllerObject3D) { return; }
265-
this.controllerObject3D.visible = false;
266261
},
267262

268263
onControllersUpdate: function () {
269-
// Note that due to gamepadconnected event propagation issues, we don't rely on events.
270264
this.checkIfControllerPresent();
271265
},
272266

‎src/components/tracked-controls-webxr.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var EVENTS = {
1313
module.exports.Component = registerComponent('tracked-controls-webxr', {
1414
schema: {
1515
id: {type: 'string', default: ''},
16+
autoHide: {default: true},
1617
hand: {type: 'string', default: ''},
1718
handTrackingEnabled: {default: false},
1819
index: {type: 'int', default: -1},
@@ -21,12 +22,13 @@ module.exports.Component = registerComponent('tracked-controls-webxr', {
2122
},
2223

2324
init: function () {
24-
this.updateController = this.updateController.bind(this);
2525
this.buttonEventDetails = {};
2626
this.buttonStates = this.el.components['tracked-controls'].buttonStates = {};
2727
this.axis = this.el.components['tracked-controls'].axis = [0, 0, 0];
2828
this.changedAxes = [];
2929
this.axisMoveEventDetail = {axis: this.axis, changed: this.changedAxes};
30+
31+
this.updateController = this.updateController.bind(this);
3032
},
3133

3234
update: function () {
@@ -67,13 +69,13 @@ module.exports.Component = registerComponent('tracked-controls-webxr', {
6769
);
6870
// Legacy handle to the controller for old components.
6971
this.el.components['tracked-controls'].controller = this.controller;
70-
if (this.data.autoHide) { this.el.object3D.visible = !!this.controller; }
7172
},
7273

7374
tick: function () {
7475
var sceneEl = this.el.sceneEl;
7576
var controller = this.controller;
7677
var frame = sceneEl.frame;
78+
if (this.data.autoHide) { this.el.object3D.visible = !!controller; }
7779
if (!controller || !sceneEl.frame || !this.system.referenceSpace) { return; }
7880
if (!controller.hand) {
7981
this.pose = frame.getPose(controller[this.data.space], this.system.referenceSpace);

‎src/components/tracked-controls.js‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ module.exports.Component = registerComponent('tracked-controls', {
2727
update: function () {
2828
var data = this.data;
2929
var el = this.el;
30-
if (el.sceneEl.hasWebXR) {
31-
el.setAttribute('tracked-controls-webxr', {
32-
id: data.id,
33-
hand: data.hand,
34-
index: data.controller,
35-
iterateControllerProfiles: data.iterateControllerProfiles,
36-
handTrackingEnabled: data.handTrackingEnabled,
37-
space: data.space
38-
});
39-
}
30+
el.setAttribute('tracked-controls-webxr', {
31+
id: data.id,
32+
hand: data.hand,
33+
index: data.controller,
34+
iterateControllerProfiles: data.iterateControllerProfiles,
35+
handTrackingEnabled: data.handTrackingEnabled,
36+
space: data.space
37+
});
4038
}
4139
});

0 commit comments

Comments
 (0)