Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/components/hand-tracking-controls.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Use `hand-tracking-controls` to integrate [hand tracked input][webxrhandinput] i
| modelColor | Color of hand material. | white |
| modelStyle | Mesh representing the hand or dots matching the joints | mesh


## Events

| Event Name | Description |
Expand Down
14 changes: 9 additions & 5 deletions src/components/hand-tracking-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var BONE_MAPPING = [
'pinky_null'
];

var PINCH_START_DISTANCE = 0.015;
var PINCH_END_DISTANCE = 0.03;
var PINCH_POSITION_INTERPOLATION = 0.5;

/**
* Controls for hand tracking
*/
Expand Down Expand Up @@ -217,22 +221,22 @@ module.exports.Component = registerComponent('hand-tracking-controls', {

var distance = indexTipPosition.distanceTo(thumbTipPosition);

if (distance < 0.01 && this.isPinched === false) {
if (distance < PINCH_START_DISTANCE && this.isPinched === false) {
this.isPinched = true;
this.pinchEventDetail.position.copy(indexTipPose.transform.position);
this.pinchEventDetail.position.copy(indexTipPosition).lerp(thumbTipPosition, PINCH_POSITION_INTERPOLATION);
this.pinchEventDetail.position.y += 1.5;
this.el.emit('pinchstarted', this.pinchEventDetail);
}

if (distance > 0.03 && this.isPinched === true) {
if (distance > PINCH_END_DISTANCE && this.isPinched === true) {
this.isPinched = false;
this.pinchEventDetail.position.copy(indexTipPose.transform.position);
this.pinchEventDetail.position.copy(indexTipPosition).lerp(thumbTipPosition, PINCH_POSITION_INTERPOLATION);
this.pinchEventDetail.position.y += 1.5;
this.el.emit('pinchended', this.pinchEventDetail);
}

if (this.isPinched) {
this.pinchEventDetail.position.copy(indexTipPose.transform.position);
this.pinchEventDetail.position.copy(indexTipPosition).lerp(thumbTipPosition, PINCH_POSITION_INTERPOLATION);
this.pinchEventDetail.position.y += 1.5;
this.el.emit('pinchmoved', this.pinchEventDetail);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/components/hand-tracking-controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ suite('tracked-controls-webxr', function () {
var standingMatrix = new THREE.Matrix4();
var index = {transform: {position: {x: 0, y: 0, z: 0}}};
var thumb = {transform: {position: {x: 0, y: 0, z: 0}}};
var indexPosition = new THREE.Vector3();
var thumbPosition = new THREE.Vector3();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking care of the tests 🏅


setup(function (done) {
standingMatrix.identity();
Expand Down Expand Up @@ -53,6 +55,13 @@ suite('tracked-controls-webxr', function () {
el.components['hand-tracking-controls'].checkIfControllerPresent();
el.components['hand-tracking-controls'].detectPinch();
assert.equal(emitSpy.getCalls()[0].args[0], 'pinchstarted');
indexPosition.copy(index.transform.position);
indexPosition.y += 1.5;
thumbPosition.copy(thumb.transform.position);
thumbPosition.y += 1.5;
const indexThumbDistance = indexPosition.distanceTo(thumbPosition);
assert.isAtMost(emitSpy.getCalls()[0].args[1].position.distanceTo(indexPosition), indexThumbDistance);
assert.isAtMost(emitSpy.getCalls()[0].args[1].position.distanceTo(thumbPosition), indexThumbDistance);
});

test('pinchended', function () {
Expand All @@ -61,8 +70,16 @@ suite('tracked-controls-webxr', function () {
el.components['hand-tracking-controls'].checkIfControllerPresent();
el.components['hand-tracking-controls'].isPinched = true;
thumb.transform.position.z = 10;
thumbPosition.copy(thumb.transform.position);
el.components['hand-tracking-controls'].detectPinch();
assert.equal(emitSpy.getCalls()[0].args[0], 'pinchended');
indexPosition.copy(index.transform.position);
indexPosition.y += 1.5;
thumbPosition.copy(thumb.transform.position);
thumbPosition.y += 1.5;
const indexThumbDistance = indexPosition.distanceTo(thumbPosition);
assert.isAtMost(emitSpy.getCalls()[0].args[1].position.distanceTo(indexPosition), indexThumbDistance);
assert.isAtMost(emitSpy.getCalls()[0].args[1].position.distanceTo(thumbPosition), indexThumbDistance);
});
});
});
Expand Down