xrfragment/src/3rd/js/aframe/xrf-get.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-05-22 18:58:05 +02:00
window.AFRAME.registerComponent('xrf-get', {
schema: {
name: {type: 'string'},
2023-10-25 11:43:44 +02:00
clone: {type: 'boolean', default:false},
reparent: {type: 'boolean', default:false}
2023-05-22 18:58:05 +02:00
},
init: function () {
var el = this.el;
var meshname = this.data.name || this.data;
this.el.addEventListener('update', (evt) => {
setTimeout( () => {
2023-11-30 17:51:01 +01:00
if( !this.mesh ){
let scene = AFRAME.XRF.scene
let mesh = this.mesh = scene.getObjectByName(meshname);
2023-11-30 17:51:01 +01:00
if( !this.el.className.match(/ray/) ) this.el.className += " ray"
if (!mesh){
console.error("mesh with name '"+meshname+"' not found in model")
return;
}
2023-10-25 11:43:44 +02:00
// we don't want to re-parent gltf-meshes
mesh.isXRF = true // mark for deletion by xrf
2023-10-25 11:43:44 +02:00
if( this.data.reparent ){
const world = {
pos: new THREE.Vector3(),
scale: new THREE.Vector3(),
quat: new THREE.Quaternion()
}
mesh.getWorldPosition(world.pos)
mesh.getWorldScale(world.scale)
mesh.getWorldQuaternion(world.quat);
mesh.position.copy(world.pos)
mesh.scale.copy(world.scale)
mesh.setRotationFromQuaternion(world.quat);
}else{
// add() will reparent the mesh so lets create a dummy
this.el.object3D.add = (a) => a
}
this.el.setObject3D('mesh',mesh)
if( !this.el.id ) this.el.setAttribute("id",`xrf-${mesh.name}`)
2023-11-29 19:39:31 +01:00
}else console.warn("xrf-get ignore: "+JSON.stringify(this.data))
}, evt && evt.timeout ? evt.timeout: 500)
2023-05-22 18:58:05 +02:00
})
2023-11-29 19:39:31 +01:00
this.el.emit("update",{timeout:0})
2023-05-22 18:58:05 +02:00
}
});