This is quite funny, but I can’t understand it. I am messing with glbs to replace all the old fbx/JSON stuff. I finally sort of got textures to apply to an untextured (but properly unwrapped) glb model. I made sure this model and unwrapping is valid by exporting it with UVW and textures included.
So now I re-exported it and removed the export with texture option so I can dynamically change the texture in game. After some trial and error it worked, but it also applied the texture to a totally unrelated plane! I verified that my other models don’t seem to be affected. I verified this by first loading my other models and then the new glb model, and also by loading the glb model and then my other models (just to see if it was messing with all models).
So how can this be? When I set my glbs models material, it updates a totally unrelated pc.Entity('plane')
!
Here is relevant code. Note my app ref is called this.engine
Create the plane
this.background = new pc.Entity('plane');
this.background.addComponent('model', {type: 'plane'});
this.background.setLocalScale(25, 25, 25);
this.engine.root.addChild(this.background);
Load the glb and apply the texture/material
// Load assets (this has been working for ~ 1 year with no side effects)
loadAsset(url, type){
return new Promise((resolve, reject) => {
this.engine.assets.loadFromUrl(url, type, (err, asset) => {
if(err){
reject(err);
}
else {
resolve(asset)
}
});
});
}
// Load glbs
loadGlbContainerFromUrl(url, options, assetName, callback) {
var filename = assetName + '.glb';
var file = {url, filename};
var asset = new pc.Asset(filename, 'container', file, null, options);
asset.once('load', function (containerAsset) {
if (callback) {
callback(null, containerAsset);
}
});
this.engine.assets.add(asset);
this.engine.assets.load(asset);
return asset;
}
// Test
let url = "/test.glb"
this.loadGlbContainerFromUrl(url, null, 'test', async (err, asset) => {
let entity = new pc.Entity();
entity.addComponent('model');
entity.model.asset = asset.resource.model;
entity.setPosition(2,2,2);
this.pc_entity.addChild(entity);
const texture = await this.loadAsset("/test.jpg", 'texture');
entity.model.material.diffuseMap = texture.resource;
entity.model.material.update();
});