Skip to content

Commit 874fced

Browse files
committed
Children of hand-tracking-controls entity are attached to the wrist joint. It allows parenting entities to the hand. e.g watch
1 parent a0722c5 commit 874fced

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/components/hand-tracking-controls.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
var registerComponent = require('../core/component').registerComponent;
33
var bind = require('../utils/bind');
44

5+
var AEntity = require('../core/a-entity').AEntity;
6+
57
var trackedControlsUtils = require('../utils/tracked-controls');
68
var checkControllerPresentAndSetup = trackedControlsUtils.checkControllerPresentAndSetup;
79

@@ -84,6 +86,7 @@ module.exports.Component = registerComponent('hand-tracking-controls', {
8486
}
8587

8688
this.onModelLoaded = this.onModelLoaded.bind(this);
89+
this.onChildAttached = this.onChildAttached.bind(this);
8790
this.jointEls = [];
8891
this.controllerPresent = false;
8992
this.isPinched = false;
@@ -102,6 +105,11 @@ module.exports.Component = registerComponent('hand-tracking-controls', {
102105
this.updateReferenceSpace = this.updateReferenceSpace.bind(this);
103106
this.el.sceneEl.addEventListener('enter-vr', this.updateReferenceSpace);
104107
this.el.sceneEl.addEventListener('exit-vr', this.updateReferenceSpace);
108+
this.el.addEventListener('child-attached', this.onChildAttached);
109+
},
110+
111+
onChildAttached: function (evt) {
112+
this.addChildEntity(evt.detail.el);
105113
},
106114

107115
update: function () {
@@ -164,9 +172,21 @@ module.exports.Component = registerComponent('hand-tracking-controls', {
164172

165173
this.updateHandModel();
166174
this.detectGesture();
175+
this.updateWristObject();
167176
}
168177
},
169178

179+
updateWristObject: (function () {
180+
var jointPose = new THREE.Matrix4();
181+
return function () {
182+
var wristObject3D = this.wristObject3D;
183+
if (!wristObject3D) { return; }
184+
jointPose.fromArray(this.jointPoses, WRIST_INDEX * 16);
185+
wristObject3D.position.setFromMatrixPosition(jointPose);
186+
wristObject3D.quaternion.setFromRotationMatrix(jointPose);
187+
};
188+
})(),
189+
170190
updateHandModel: function () {
171191
if (this.data.modelStyle === 'dots') {
172192
this.updateHandDotsModel();
@@ -348,6 +368,22 @@ module.exports.Component = registerComponent('hand-tracking-controls', {
348368
mesh.rotation.set(0, 0, 0);
349369
skinnedMesh.frustumCulled = false;
350370
skinnedMesh.material = new THREE.MeshStandardMaterial({color: this.data.modelColor});
371+
this.setupChildrenEntities();
351372
this.el.setObject3D('mesh', mesh);
373+
},
374+
375+
setupChildrenEntities: function () {
376+
var childrenEls = this.el.children;
377+
this.wristObject3D = new THREE.Object3D();
378+
for (var i = 0; i < childrenEls.length; ++i) {
379+
if (!(childrenEls[i] instanceof AEntity)) { continue; }
380+
this.addChildEntity(childrenEls[i]);
381+
}
382+
this.el.sceneEl.object3D.add(this.wristObject3D);
383+
},
384+
385+
addChildEntity: function (childEl) {
386+
if (!(childEl instanceof AEntity)) { return; }
387+
this.wristObject3D.add(childEl.object3D);
352388
}
353389
});

tests/components/hand-tracking-controls.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ suite('tracked-controls-webxr', function () {
6565
});
6666
});
6767

68+
suite('children entities', function () {
69+
test('attached to the wrist joint', function (done) {
70+
var boxEl = document.createElement('a-box');
71+
el.setAttribute('hand-tracking-controls', {hand: 'left'});
72+
el.components['hand-tracking-controls'].checkIfControllerPresent();
73+
el.addEventListener('model-loaded', function () {
74+
assert.ok(el.components['hand-tracking-controls'].wristObject3D);
75+
el.appendChild(boxEl);
76+
});
77+
el.addEventListener('child-attached', function () {
78+
assert.equal(boxEl.object3D.parent, el.components['hand-tracking-controls'].wristObject3D);
79+
done();
80+
});
81+
});
82+
});
83+
6884
suite('emit events', function () {
6985
test('pinchstarted', function () {
7086
const emitSpy = sinon.spy(el, 'emit');

0 commit comments

Comments
 (0)