if (nearPlacements[i3].model) {
console.log('asset found');
modelAssetLoad = nearPlacements[i3].model.asset;
var asset3 = this.app.assets.get(modelAssetLoad);
console.log(asset3);
this.app.assets.load(asset3);
I can console.log the asset after unload - prior
Once trying to re-load [playcanvas.dbg.js?version=1.53.4:38935]: Cannot read properties of undefined (reading 'loading') or [[playcanvas.dbg.js?version=1.53.4:35745]](https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.53.4): Cannot read properties of null (reading 'load')
The issue here is that the actual asset is the GLB container and all the materials, model, render assets etc are part of the container. You cannot reload individual assets in the container but the container asset itself.
It’s also worth noting that the model asset is created on demand (not documented as the model component is deprecated) when asset.resource.model is called and therefore when the container is reloaded, it needs to be assigned again to the model component and collision. I think this might be true for all the assets in the container thinking about it
Hey, this indeed works, the problem is that this is used on hundreds of entities, I’ve discovered that asset is not entity asset model and asset IDs are different
I set each entities container asset ID as a tag, and load or unload it if needed, unloading somewhat works, again loading is a problem
var Test = pc.createScript('test');
// initialize code called once per entity
Test.prototype.initialize = function() {
entity = new pc.Entity('tempPlacement');
entity.tags.add('Placement');
entity.setPosition(0,0,0);
pc.app.root.addChild(entity);
this.app.assets.loadFromUrlAndFilename('https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Lantern/glTF-Binary/Lantern.glb', null, "container", function (err, asset) {
entity.addComponent('model', {
asset: asset.resource.model
});
entity.addComponent('collision', {
type: 'mesh',
asset: asset.resource.model
});
entity.addComponent('rigidbody', {
type: pc.BODYTYPE_STATIC,
friction: 1,
restitution: 0
});
entity.tags.add(String(asset.id));
console.log(String(asset.id));
});
};
// update code called every frame
Test.prototype.update = function(dt) {
if(this.app.keyboard.wasReleased(pc.KEY_V) ){
var placement = this.app.root.findByName('tempPlacement');
containerAsset = placement.tags._list[1];
// modelAsset = placement.model.asset;
var asset2 = this.app.assets.get(containerAsset);
console.log(asset2);
asset2.unload();
// placement.removeComponent('model');
// placement.removeComponent('collision');
//placement.removeComponent('rigidbody');
}
if(this.app.keyboard.wasReleased(pc.KEY_C) ){
var placement2 = this.app.root.findByName('tempPlacement');
if (placement2.model) {
containerAssetLoad = placement2.tags._list[1];
// console.log(containerAssetLoad);
asset3 = this.app.assets.get(containerAssetLoad);
this.app.assets.load(asset3);
console.log(asset3);
// console.log(placement2.model);
}
}
};
We are near, loading does nothing, no errors
Console logs the right glb container asset id, I think the problem occurs with existing model and collision components in this case
Thanks for your help! All works as it should, elements get reloade from inspector cache, vram gets released upon unloading, just one more question, how to handle entities that uses same model container but are in different locations? Since unloading for one, unloads for all, or should I just add a random text value to the end of url like model.glb?random hoping the engine would create different container asset
I’m not sure I fully understand the question. Are you asking what to do if two entities share the same model asset from a container?
If that’s the case, it’s down to the developer to manage that. You could have a manager that handles all the external assets being loaded and it reference counts how many entities are using certain resources.
Once no entities are using assets from a container then the manager can unload it.
You could also do something more simpler such as adding a tag of the asset id of the container it is using to the entity and using findByTags to see how many entities are left in the scene. That will tell you how many references to the container are being used and you can use that to make the decision whether to unload the container or not.