2024-06-07 08:52:59 +00:00
|
|
|
//// this makes WebXR hand controls able to click things (by touching it)
|
|
|
|
|
|
2024-06-11 17:30:32 +00:00
|
|
|
AFRAME.registerSystem('xrf-hands',{
|
2024-06-07 08:52:59 +00:00
|
|
|
|
|
|
|
|
init: function(){
|
2024-06-11 17:30:32 +00:00
|
|
|
this.sceneEl.addEventListener('loaded', () => this.getFingers() )
|
2024-06-07 08:52:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
tick: function(){
|
2025-05-22 19:31:13 +02:00
|
|
|
if( !this.indexFinger || !xrf.interactive ) return
|
2024-06-11 17:30:32 +00:00
|
|
|
if( !this.el.sceneEl.renderer.xr.isPresenting || !this.indexFinger.length ) return
|
|
|
|
|
for( let i = 0; i < this.indexFinger.length; i++ ){
|
|
|
|
|
let indexFinger = this.indexFinger[i]
|
|
|
|
|
let intersects = xrf.interactive.intersect( indexFinger, 0.01 )
|
|
|
|
|
if( intersects.length ){
|
|
|
|
|
let obj = intersects[0].object
|
|
|
|
|
if( obj.clicked ) return
|
|
|
|
|
obj.clicked = true
|
|
|
|
|
obj.dispatchEvent({type:"click", message: indexFinger })
|
|
|
|
|
setTimeout( () => obj.clicked = false, 250 ) // prevent double clicks
|
|
|
|
|
}
|
2024-06-07 09:31:17 +00:00
|
|
|
}
|
2024-06-07 08:52:59 +00:00
|
|
|
},
|
|
|
|
|
|
2024-06-11 17:30:32 +00:00
|
|
|
|
2024-06-07 08:52:59 +00:00
|
|
|
getFingers: function(){
|
|
|
|
|
let handEls = [...document.querySelectorAll('[hand-tracking-controls]')]
|
2024-06-11 17:30:32 +00:00
|
|
|
if( !handEls.length ) return
|
2024-06-07 08:52:59 +00:00
|
|
|
this.indexFinger = []
|
|
|
|
|
|
2024-06-07 09:31:17 +00:00
|
|
|
const me = this
|
|
|
|
|
|
2024-06-07 08:52:59 +00:00
|
|
|
for( let i in handEls ){
|
|
|
|
|
let handEl = handEls[i]
|
2024-06-07 09:31:17 +00:00
|
|
|
handEl.addEventListener('model-loaded', function(e){
|
|
|
|
|
const handEl = this
|
2024-06-07 08:52:59 +00:00
|
|
|
// wait for bones get initialized
|
|
|
|
|
setTimeout( () => {
|
|
|
|
|
let bones = handEl.components['hand-tracking-controls'].bones
|
2025-02-18 14:50:47 +01:00
|
|
|
if( !bones ) return // dont bother
|
2024-06-07 08:52:59 +00:00
|
|
|
let indexFinger
|
2024-06-07 09:31:17 +00:00
|
|
|
for( let j = 0; j < bones.length; j++){
|
2024-06-07 08:52:59 +00:00
|
|
|
if( bones[j].name == "index-finger-tip" ){
|
|
|
|
|
indexFinger = j
|
2024-06-07 09:31:17 +00:00
|
|
|
me.indexFinger.push(bones[j])
|
|
|
|
|
const els = [...document.querySelectorAll('[xrf-pressable]')]
|
2024-06-11 17:30:32 +00:00
|
|
|
els.map( (el) => el.emit('indexFingerReady', {index: j} ) )
|
2024-06-07 08:52:59 +00:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},500)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 09:31:17 +00:00
|
|
|
}
|
2024-06-07 08:52:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|