Created
September 19, 2023 15:07
-
-
Save addy1997/08e05da5cb99e086897a7cad7fefd715 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AFRAME.registerComponent('pressable', { | |
schema: { | |
pressDistance: { | |
default: 0.06 | |
} | |
}, | |
init: function() { | |
this.worldPosition = new THREE.Vector3(); | |
this.handEls = document.querySelectorAll('[hand-tracking-controls]'); | |
this.pressed = false; | |
}, | |
tick: function() { | |
var handEls = this.handEls; | |
var handEl; | |
var distance; | |
for (var i = 0; i < handEls.length; i++) { | |
handEl = handEls[i]; | |
distance = this.calculateFingerDistance(handEl.components['hand-tracking-controls'].indexTipPosition); | |
if (distance < this.data.pressDistance) { | |
if (!this.pressed) { | |
this.el.emit('pressedstarted'); | |
} | |
this.pressed = true; | |
return; | |
} | |
} | |
if (this.pressed) { | |
this.el.emit('pressedended'); | |
} | |
this.pressed = false; | |
}, | |
calculateFingerDistance: function(fingerPosition) { | |
var el = this.el; | |
var worldPosition = this.worldPosition; | |
worldPosition.copy(el.object3D.position); | |
el.object3D.parent.updateMatrixWorld(); | |
el.object3D.parent.localToWorld(worldPosition); | |
return worldPosition.distanceTo(fingerPosition); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment