xrfragment-haxe/src/3rd/aframe/index.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-05-09 17:42:29 +02:00
window.AFRAME.registerComponent('xrf', {
schema: {
rig: {type: 'selector'}
},
init: function () {
if( !AFRAME.XRF ) this.initXRFragments()
if( typeof this.data == "string" ){
AFRAME.XRF.navigate.to(this.data)
.then( (model) => {
let gets = [ ...document.querySelectorAll('[xrf-get]') ]
gets.map( (g) => g.emit('update',model) )
})
}
2023-05-09 17:42:29 +02:00
},
2023-05-09 17:42:29 +02:00
initXRFragments: function(){
let aScene = document.querySelector('a-scene')
// enable XR fragments
let XRF = AFRAME.XRF = xrfragment.init({
THREE,
camera: aScene.camera,
2023-05-09 17:42:29 +02:00
scene: aScene.object3D,
renderer: aScene.renderer,
debug: true,
loaders: { gltf: THREE.GLTFLoader } // which 3D assets (exts) to check for XR fragments?
2023-05-09 17:42:29 +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
// override the camera-related XR Fragments so the camera-rig is affected
let camOverride = (xrf,v,opts) => {
opts.camera = $('[camera]').object3D //parentElement.object3D
xrf(v,opts)
2023-05-09 17:42:29 +02:00
}
XRF.pos = camOverride
XRF.rot = camOverride
XRF.href = camOverride
2023-05-09 17:42:29 +02:00
},
})
2023-05-09 17:42:29 +02:00
window.AFRAME.registerComponent('xrf-get', {
2023-05-10 19:12:15 +02:00
schema: {
name: {type: 'string'},
duplicate: {type: 'boolean'}
2023-05-10 19:12:15 +02:00
},
init: function () {
2023-05-10 19:12:15 +02:00
var el = this.el;
var meshname = this.data.name || this.data;
this.el.addEventListener('update', (evt) => {
2023-05-10 19:12:15 +02:00
let scene = evt.detail.scene
let mesh = scene.getObjectByName(meshname);
if (!mesh){
console.error("mesh with name '"+meshname+"' not found in model")
2023-05-10 19:12:15 +02:00
return;
}
if( !this.data.duplicate ) mesh.parent.remove(mesh)
if( this.mesh ) this.mesh.parent.remove(this.mesh) // cleanup old clone
let clone = this.mesh = mesh.clone()
////mesh.updateMatrixWorld();
this.el.object3D.position.setFromMatrixPosition(scene.matrixWorld);
this.el.object3D.quaternion.setFromRotationMatrix(scene.matrixWorld);
this.el.setObject3D('mesh', clone );
if( !this.el.id ) this.el.setAttribute("id",`xrf-${clone.name}`)
})
2023-05-10 19:12:15 +02:00
}
2023-05-10 19:12:15 +02:00
});