I have an element with a gltf-model component attached. Inside, I want to get reference to some of the materials using variables that have component level scope, ie schema properties. I notice (in the aframe docs) that the type for schema objects is 'string' or 'number', and I want to store a reference to THREE.js material (not sure if this works). I tried that, and it works, sort of, but the scope is limited to the listener function, so apparently, it's not working. How do I store references to THREE.objects that can be accessed anywhere in my component? Ultimately I want to store many references to various materials, and modify their properties in other listener functions ('loaded') outside of the ('model-loaded') listener function.
I tried to use a schema property to store the THREE.material like this
schema:{
bonsaiMat:{type:'string'}...
update:function(){
let data = this.data;
el.addEventListener('model-loaded', function(ev){
data.bonsaiMat = node.material;
but logging data.bonsaiMat outside of the listener function returns nothing (works fine inside listener function).
Also tried this.bonsaiMat = node.material; // errors
AFRAME.registerComponent("bonsai-gltf", {
schema:{
bonsaiMat:{type:'string'}
},
update:function(){
let el = this.el;
let scene = el.sceneEl.object3D;
let data = this.data;
// Initialize materials setup on the scultpure.
el.addEventListener('model-loaded', function(ev){
// console.log('bonsai loaded');
let mesh = el.getObject3D('mesh');
// Get references to Materials, to be adjusted later.
mesh.traverse(function(node){
if(node.isMesh){
if(node.material.name === 'pbr_Metal'){
data.bonsaiMat = node.material;
console.log(bonsaiMat); // <--this works
}
}
});
console.log(data.bonsaiMat); // <--this fails
I expected variables stored in the schema, ie this.data.myMaterial to be usable anywhere in the component, but the scope is limited to the listener function, so something is wrong. data type for schema property? Is there some other way to get component wide scope? My project is rather sprawling, so putting it inside of a glitch would be tedious, but I can do it if required.