AssetListLoader should load dependencies for all asset types
Currently AssetListLoader is only loading dependencies of asset type model.
https://github.com/playcanvas/engine/blob/c5ee86946dc18acda9deaadf07ac50288f6e5a1c/src/framework/asset/asset-list-loader.js#L144-L153
https://github.com/playcanvas/engine/blob/c5ee86946dc18acda9deaadf07ac50288f6e5a1c/src/framework/asset/asset-list-loader.js#L112-L114
My use case: When switching between scenes I want to use the AssetListLoader to preload all assets and show a progress bar.
I parse the scene and create a list of all assets which need to be loaded.
Let's say the scene contains a render component. When loading the render component the glb won't be loaded with it. This results in pop in, when changing the scene.
There are multiple asset types, where this problem occurs:
| Asset Type | Missing dependencies |
|---|---|
| cubemap | textures |
| material | textures |
| render | glb |
| ... | ... |
There are 2 solutions to this problem:
- Handle loading of dependencies in AssetListLoader (similar to model asset type)
- Handle loading in AssetRegistry.load. (When a asset is loaded, always load it's dependencies)
Perhaps a third option a more simple way of getting actual dependencies, something like:
const related = asset.getRelated();
That way you can provide a full list to AssetListLoader. Changing AssetRegistry.load - would not be within its semantic, as it suppose to load a single asset.
The reason why model was loading straight away but render isn't, is because model asset contained actual geometry data, while render asset is only a "reference" to a container asset.
That would definetly make loading of dependencies easier!
However I do advocate for some API where I can pass in an asset and it loads all dependencies as well. I don't really want to do that manually as this can be generalized.
Could be a new method like AssetRegistry.loadWithDependencies?
AssetRegistry.loadWithDependencies - might be way too simplified, as there are cases when we would want to load only a diff, and unload what is not required anymore. This is common when you load game level after different one. To avoid unloading resources that used in both levels, you get a list of assets required, and remove them from list of previously used assets, and just unload the remaining. And then load the new list, which of course will ignore loading for already loaded resources.
So asset.getRelated - is very useful in this case, and solves all cases, just requires a bit more code from developer.
Currently to do that, we tag assets manually, and use tags to find lists and manipulate them what described above.
AssetRegistry.loadWithDependencies - could be simplified useful method indeed. But if it is based on asset.getRelated - even better.
I didn't think about unloading! But that's a valid use case as well. I mostly never unload as my scenes are pretty small.
I've seen the examples where tagging is used to define which assets are loaded. However I'm not a big fan of this, as this involves manual setup, which introduces human errors.
Currently I'm parsing the scene data, to get all relevant assets which are needed by a scene. scene.getRelatedAssets would be helpful here as well.
I do like your idea of AssetRegistry.loadWithDependencies using asset.getRelated!