check for glTF load errors and remove assets correctly

This commit is contained in:
Nick Fisher
2023-10-03 17:19:43 +11:00
parent 0c9387a9d6
commit 3d074f4563

View File

@@ -84,8 +84,7 @@ EntityId AssetManager::loadGltf(const char *uri,
ResourceBuffer rbuf = _resourceLoaderWrapper->load(uri); ResourceBuffer rbuf = _resourceLoaderWrapper->load(uri);
// Parse the glTF file and create Filament entities. // Parse the glTF file and create Filament entities.
FilamentAsset *asset = FilamentAsset *asset = _assetLoader->createAsset((uint8_t *)rbuf.data, rbuf.size);
_assetLoader->createAsset((uint8_t *)rbuf.data, rbuf.size);
if (!asset) { if (!asset) {
Log("Unable to parse asset"); Log("Unable to parse asset");
@@ -94,44 +93,55 @@ EntityId AssetManager::loadGltf(const char *uri,
const char *const *const resourceUris = asset->getResourceUris(); const char *const *const resourceUris = asset->getResourceUris();
const size_t resourceUriCount = asset->getResourceUriCount(); const size_t resourceUriCount = asset->getResourceUriCount();
std::vector<ResourceBuffer> resourceBuffers;
for (size_t i = 0; i < resourceUriCount; i++) { for (size_t i = 0; i < resourceUriCount; i++) {
string uri = string uri = string(relativeResourcePath) + string("/") + string(resourceUris[i]);
string(relativeResourcePath) + string("/") + string(resourceUris[i]); Log("Loading resource URI from relative path %s", resourceUris[i], uri.c_str());
ResourceBuffer buf = _resourceLoaderWrapper->load(uri.c_str()); ResourceBuffer buf = _resourceLoaderWrapper->load(uri.c_str());
resourceBuffers.push_back(buf);
ResourceLoader::BufferDescriptor b(buf.data, buf.size); ResourceLoader::BufferDescriptor b(buf.data, buf.size);
_gltfResourceLoader->addResourceData(resourceUris[i], std::move(b)); _gltfResourceLoader->addResourceData(resourceUris[i], std::move(b));
_resourceLoaderWrapper->free(buf);
} }
_gltfResourceLoader->loadResources(asset); // load resources synchronously
if (!_gltfResourceLoader->loadResources(asset)) {
Log("Unknown error loading glTF asset");
_resourceLoaderWrapper->free(rbuf);
for(auto& rb : resourceBuffers) {
_resourceLoaderWrapper->free(rb);
}
return 0;
}
const utils::Entity *entities = asset->getEntities(); const utils::Entity *entities = asset->getEntities();
RenderableManager &rm = _engine->getRenderableManager();
for (int i = 0; i < asset->getEntityCount(); i++) { _scene->addEntities(asset->getEntities(), asset->getEntityCount());
auto inst = rm.getInstance(entities[i]);
rm.setCulling(inst, false);
}
FilamentInstance* inst = asset->getInstance(); FilamentInstance* inst = asset->getInstance();
inst->getAnimator()->updateBoneMatrices(); inst->getAnimator()->updateBoneMatrices();
inst->recomputeBoundingBoxes(); inst->recomputeBoundingBoxes();
_scene->addEntities(asset->getEntities(), asset->getEntityCount());
asset->releaseSourceData(); asset->releaseSourceData();
Log("Load complete for GLTF at URI %s", uri);
SceneAsset sceneAsset(asset); SceneAsset sceneAsset(asset);
utils::Entity e = EntityManager::get().create(); utils::Entity e = EntityManager::get().create();
EntityId eid = Entity::smuggle(e); EntityId eid = Entity::smuggle(e);
_entityIdLookup.emplace(eid, _assets.size()); _entityIdLookup.emplace(eid, _assets.size());
_assets.push_back(sceneAsset); _assets.push_back(sceneAsset);
for(auto& rb : resourceBuffers) {
_resourceLoaderWrapper->free(rb);
}
_resourceLoaderWrapper->free(rbuf);
Log("Finished loading glTF from %s", uri);
return eid; return eid;
} }
@@ -153,8 +163,12 @@ EntityId AssetManager::loadGlb(const char *uri, bool unlit) {
_scene->addEntities(asset->getEntities(), entityCount); _scene->addEntities(asset->getEntities(), entityCount);
_gltfResourceLoader->loadResources(asset); if (!_gltfResourceLoader->loadResources(asset)) {
Log("Unknown error loading glb asset");
_resourceLoaderWrapper->free(rbuf);
return 0;
}
const Entity *entities = asset->getEntities(); const Entity *entities = asset->getEntities();
auto lights = asset->getLightEntities(); auto lights = asset->getLightEntities();
@@ -220,12 +234,9 @@ bool AssetManager::reveal(EntityId entityId, const char* meshName) {
void AssetManager::destroyAll() { void AssetManager::destroyAll() {
for (auto& asset : _assets) { for (auto& asset : _assets) {
_scene->removeEntities(asset.mAsset->getEntities(), _scene->removeEntities(asset.mAsset->getEntities(),
asset.mAsset->getEntityCount()); asset.mAsset->getEntityCount());
_scene->removeEntities(asset.mAsset->getLightEntities(), _scene->removeEntities(asset.mAsset->getLightEntities(),
asset.mAsset->getLightEntityCount()); asset.mAsset->getLightEntityCount());
_gltfResourceLoader->evictResourceData();
_assetLoader->destroyAsset(asset.mAsset); _assetLoader->destroyAsset(asset.mAsset);
} }
_assets.clear(); _assets.clear();
@@ -370,6 +381,10 @@ void AssetManager::remove(EntityId entityId) {
return; return;
} }
SceneAsset& sceneAsset = _assets[pos->second]; SceneAsset& sceneAsset = _assets[pos->second];
_assets.erase(std::remove_if(_assets.begin(), _assets.end(),
[=](SceneAsset& asset) { return asset.mAsset == sceneAsset.mAsset; }),
_assets.end());
_scene->removeEntities(sceneAsset.mAsset->getEntities(), _scene->removeEntities(sceneAsset.mAsset->getEntities(),
sceneAsset.mAsset->getEntityCount()); sceneAsset.mAsset->getEntityCount());
@@ -384,7 +399,8 @@ void AssetManager::remove(EntityId entityId) {
} }
EntityManager& em = EntityManager::get(); EntityManager& em = EntityManager::get();
em.destroy(Entity::import(entityId)); em.destroy(Entity::import(entityId));
sceneAsset.mAsset = nullptr; // still need to remove sceneAsset somewhere...
} }
void AssetManager::setMorphTargetWeights(EntityId entityId, const char* const entityName, const float* const weights, const int count) { void AssetManager::setMorphTargetWeights(EntityId entityId, const char* const entityName, const float* const weights, const int count) {