2023-05-09 17:42:29 +02:00
|
|
|
window.AFRAME.registerComponent('xrf', {
|
|
|
|
|
schema: {
|
|
|
|
|
},
|
|
|
|
|
init: function () {
|
|
|
|
|
if( !AFRAME.XRF ) this.initXRFragments()
|
2023-05-18 17:11:11 +02:00
|
|
|
if( this.data ){
|
2023-06-22 08:49:24 +02:00
|
|
|
if( document.location.search || document.location.hash.length > 1 ){ // override url
|
|
|
|
|
this.data = `${document.location.search.substr(1)}${document.location.hash}`
|
|
|
|
|
}
|
2023-05-17 21:31:28 +02:00
|
|
|
AFRAME.XRF.navigator.to(this.data)
|
2023-05-12 22:06:21 +02:00
|
|
|
.then( (model) => {
|
|
|
|
|
let gets = [ ...document.querySelectorAll('[xrf-get]') ]
|
2023-05-12 22:40:09 +02:00
|
|
|
gets.map( (g) => g.emit('update') )
|
2023-05-12 22:06:21 +02:00
|
|
|
})
|
|
|
|
|
}
|
2023-05-09 17:42:29 +02:00
|
|
|
},
|
2023-05-12 22:06:21 +02:00
|
|
|
|
2023-05-09 17:42:29 +02:00
|
|
|
initXRFragments: function(){
|
2023-05-17 21:31:28 +02:00
|
|
|
|
2023-05-18 12:32:57 +02:00
|
|
|
//window.addEventListener('popstate', clear )
|
|
|
|
|
//window.addEventListener('pushstate', clear )
|
2023-05-17 21:31:28 +02:00
|
|
|
|
2023-05-09 17:42:29 +02:00
|
|
|
// enable XR fragments
|
2023-05-17 21:31:28 +02:00
|
|
|
let aScene = document.querySelector('a-scene')
|
2023-06-07 17:42:21 +02:00
|
|
|
let XRF = AFRAME.XRF = xrf.init({
|
2023-05-09 17:42:29 +02:00
|
|
|
THREE,
|
2023-05-12 22:06:21 +02:00
|
|
|
camera: aScene.camera,
|
2023-05-09 17:42:29 +02:00
|
|
|
scene: aScene.object3D,
|
|
|
|
|
renderer: aScene.renderer,
|
|
|
|
|
debug: true,
|
2023-05-12 22:06:21 +02:00
|
|
|
loaders: { gltf: THREE.GLTFLoader } // which 3D assets (exts) to check for XR fragments?
|
2023-05-09 17:42:29 +02:00
|
|
|
})
|
2023-05-12 22:06:21 +02:00
|
|
|
if( !XRF.camera ) throw 'xrfragment: no camera detected, please declare <a-entity camera..> ABOVE entities with xrf-attributes'
|
2023-05-09 17:42:29 +02:00
|
|
|
|
2023-05-12 22:06:21 +02:00
|
|
|
// override the camera-related XR Fragments so the camera-rig is affected
|
|
|
|
|
let camOverride = (xrf,v,opts) => {
|
2023-05-22 18:58:05 +02:00
|
|
|
opts.camera = document.querySelector('[camera]').object3D.parent
|
2023-05-12 22:06:21 +02:00
|
|
|
xrf(v,opts)
|
2023-05-09 17:42:29 +02:00
|
|
|
}
|
2023-05-12 22:06:21 +02:00
|
|
|
|
2023-06-07 17:42:21 +02:00
|
|
|
xrf.pos = camOverride
|
2023-05-12 22:40:09 +02:00
|
|
|
|
2023-05-23 14:41:24 +02:00
|
|
|
// in order to set the rotation programmatically
|
|
|
|
|
// we need to disable look-controls
|
2023-06-07 17:42:21 +02:00
|
|
|
xrf.rot = (xrf,v,opts) => {
|
2023-05-23 14:41:24 +02:00
|
|
|
let {renderer} = opts;
|
|
|
|
|
let look = document.querySelector('[look-controls]')
|
|
|
|
|
if( look ) look.removeAttribute("look-controls")
|
|
|
|
|
camOverride(xrf,v,opts)
|
2023-06-07 17:42:21 +02:00
|
|
|
// *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-23 14:41:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert portal to a-entity so AFRAME
|
|
|
|
|
// raycaster can find & execute it
|
2023-06-07 17:42:21 +02:00
|
|
|
xrf.href = (xrf,v,opts) => {
|
2023-05-23 14:41:24 +02:00
|
|
|
camOverride(xrf,v,opts)
|
2023-05-12 22:40:09 +02:00
|
|
|
let {mesh,camera} = opts;
|
|
|
|
|
let el = document.createElement("a-entity")
|
|
|
|
|
el.setAttribute("xrf-get",mesh.name )
|
2023-05-22 18:58:05 +02:00
|
|
|
el.setAttribute("class","ray")
|
2023-05-17 21:31:28 +02:00
|
|
|
el.addEventListener("click", mesh.userData.XRF.href.exec )
|
2023-05-12 22:40:09 +02:00
|
|
|
$('a-scene').appendChild(el)
|
|
|
|
|
}
|
2023-05-18 12:32:57 +02:00
|
|
|
|
|
|
|
|
// cleanup xrf-get objects when resetting scene
|
2023-06-07 17:42:21 +02:00
|
|
|
xrf.reset = ((reset) => () => {
|
2023-05-22 17:18:15 +02:00
|
|
|
reset()
|
2023-05-18 12:32:57 +02:00
|
|
|
console.log("aframe reset")
|
|
|
|
|
let els = [...document.querySelectorAll('[xrf-get]')]
|
|
|
|
|
els.map( (el) => document.querySelector('a-scene').removeChild(el) )
|
|
|
|
|
})(XRF.reset)
|
2023-05-22 17:18:15 +02:00
|
|
|
|
2023-05-22 18:58:05 +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-05-12 22:06:21 +02:00
|
|
|
},
|
|
|
|
|
})
|