2023-05-09 17:42:29 +02:00
|
|
|
window.AFRAME.registerComponent('xrf', {
|
|
|
|
|
schema: {
|
|
|
|
|
},
|
|
|
|
|
init: function () {
|
2023-09-15 19:42:37 +02:00
|
|
|
if( !AFRAME.XRF ){
|
|
|
|
|
document.querySelector('a-scene').addEventListener('loaded', () => {
|
2023-05-12 22:06:21 +02:00
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
//window.addEventListener('popstate', clear )
|
|
|
|
|
//window.addEventListener('pushstate', clear )
|
2023-05-17 21:31:28 +02:00
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
// enable XR fragments
|
|
|
|
|
let aScene = document.querySelector('a-scene')
|
|
|
|
|
let XRF = AFRAME.XRF = xrf.init({
|
|
|
|
|
THREE,
|
2023-10-12 17:04:46 +02:00
|
|
|
camera: aScene.camera,
|
|
|
|
|
scene: aScene.object3D,
|
|
|
|
|
renderer: aScene.renderer,
|
2023-09-15 19:42:37 +02:00
|
|
|
debug: true,
|
|
|
|
|
loaders: {
|
|
|
|
|
gltf: THREE.GLTFLoader, // which 3D assets (exts) to check for XR fragments?
|
|
|
|
|
glb: THREE.GLTFLoader
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if( !XRF.camera ) throw 'xrfragment: no camera detected, please declare <a-entity camera..> ABOVE entities with xrf-attributes'
|
2023-05-17 21:31:28 +02:00
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
// override the camera-related XR Fragments so the camera-rig is affected
|
|
|
|
|
let camOverride = (xrf,v,opts) => {
|
|
|
|
|
opts.camera = document.querySelector('[camera]').object3D.parent
|
|
|
|
|
xrf(v,opts)
|
|
|
|
|
}
|
2023-06-27 09:42:49 +02:00
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
xrf.pos = camOverride
|
2023-10-11 13:46:38 +02:00
|
|
|
xrf.href = camOverride
|
2023-06-27 09:42:49 +02:00
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
// in order to set the rotation programmatically
|
|
|
|
|
// we need to disable look-controls
|
|
|
|
|
xrf.rot = (xrf,v,opts) => {
|
|
|
|
|
let {frag,renderer} = opts;
|
|
|
|
|
if( frag.q ) return // camera was not targeted for rotation
|
|
|
|
|
let look = document.querySelector('[look-controls]')
|
|
|
|
|
if( look ) look.removeAttribute("look-controls")
|
|
|
|
|
// camOverride(xrf,v,opts)
|
|
|
|
|
// *TODO* make look-controls compatible, because simply
|
|
|
|
|
// adding the look-controls will revert to the old rotation (cached somehow?)
|
|
|
|
|
//setTimeout( () => look.setAttribute("look-controls",""), 100 )
|
|
|
|
|
}
|
2023-05-12 22:40:09 +02:00
|
|
|
|
2023-10-11 13:46:38 +02:00
|
|
|
// convert href's to a-entity's so AFRAME
|
2023-09-15 19:42:37 +02:00
|
|
|
// raycaster can find & execute it
|
2023-10-11 13:46:38 +02:00
|
|
|
AFRAME.XRF.clickableMeshToEntity = (opts) => {
|
|
|
|
|
let {mesh,clickHandler} = opts;
|
2023-09-15 19:42:37 +02:00
|
|
|
let el = document.createElement("a-entity")
|
2023-10-11 13:46:38 +02:00
|
|
|
el.setAttribute("xrf-get",mesh.name ) // turn into AFRAME entity
|
|
|
|
|
el.setAttribute("class","ray") // expose to raycaster
|
|
|
|
|
el.setAttribute("pressable", '') // detect hand-controller click
|
|
|
|
|
// add click
|
|
|
|
|
el.addEventListener("click", clickHandler )
|
|
|
|
|
el.addEventListener("pressedstarted", clickHandler )
|
|
|
|
|
// this.el.addEventListener("buttondown", console.dir )
|
|
|
|
|
// this.el.addEventListener("touchstart", console.dir )
|
|
|
|
|
// this.el.addEventListener("triggerdown", console.dir )
|
|
|
|
|
// this.el.addEventListener("gripdown", console.dir )
|
|
|
|
|
// this.el.addEventListener("abuttondown", console.dir )
|
|
|
|
|
// this.el.addEventListener("pinchended", console.dir )
|
|
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
$('a-scene').appendChild(el)
|
|
|
|
|
}
|
2023-10-11 13:46:38 +02:00
|
|
|
xrf.addEventListener('interactionReady', AFRAME.XRF.clickableMeshToEntity )
|
2023-05-23 14:41:24 +02:00
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
// cleanup xrf-get objects when resetting scene
|
|
|
|
|
xrf.reset = ((reset) => () => {
|
|
|
|
|
reset()
|
|
|
|
|
console.log("aframe reset")
|
|
|
|
|
let els = [...document.querySelectorAll('[xrf-get]')]
|
|
|
|
|
els.map( (el) => document.querySelector('a-scene').removeChild(el) )
|
|
|
|
|
})(XRF.reset)
|
2023-05-18 12:32:57 +02:00
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
// undo lookup-control shenanigans (which blocks updating camerarig position in VR)
|
|
|
|
|
aScene.addEventListener('enter-vr', () => document.querySelector('[camera]').object3D.parent.matrixAutoUpdate = true )
|
2023-06-27 09:42:49 +02:00
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
AFRAME.XRF.navigator.to(this.data)
|
|
|
|
|
.then( (model) => {
|
|
|
|
|
let gets = [ ...document.querySelectorAll('[xrf-get]') ]
|
|
|
|
|
gets.map( (g) => g.emit('update') )
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
aScene.emit('XRF',{})
|
2023-10-14 20:10:06 +02:00
|
|
|
|
|
|
|
|
// enable gaze-click on Mobile VR
|
|
|
|
|
aScene.setAttribute('xrf-gaze','')
|
|
|
|
|
|
2023-09-15 19:42:37 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( typeof this.data == "string" ){
|
|
|
|
|
if( document.location.search || document.location.hash.length > 1 ){ // override url
|
|
|
|
|
this.data = `${document.location.search.substr(1)}${document.location.hash}`
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-12 22:06:21 +02:00
|
|
|
},
|
2023-09-15 19:42:37 +02:00
|
|
|
|
2023-05-12 22:06:21 +02:00
|
|
|
})
|