feat: fix scene loading that it loads from disk if not already loaded
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
#include <destrum/Components/MeshRendererComponent.h>
|
||||
#include <destrum/Assets/AssetReference.h>
|
||||
#include <destrum/Graphics/GfxDevice.h>
|
||||
#include <destrum/Graphics/Renderer.h>
|
||||
#include <destrum/ObjectModel/Transform.h>
|
||||
|
||||
#include "destrum/Components/Animator.h"
|
||||
@@ -79,19 +82,53 @@ void MeshRendererComponent::ResolveReferences(const ObjectMap&) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto loadAssetForKey = [&](const std::string& key) {
|
||||
const auto asset = AssetReference::fromCacheKey(key);
|
||||
if (!asset) {
|
||||
return;
|
||||
}
|
||||
if (!GameState::GetInstance().HasGfxDevice()) {
|
||||
throw std::runtime_error(
|
||||
"Cannot load mesh asset without a graphics device: " + asset->path);
|
||||
}
|
||||
|
||||
resources->loadModelAsset(GameState::GetInstance().Gfx(), asset->path);
|
||||
};
|
||||
|
||||
if (!meshKey.empty()) {
|
||||
const auto resolved = resources->meshes().findMeshByKey(meshKey);
|
||||
auto resolved = resources->meshes().findMeshByKey(meshKey);
|
||||
if (!resolved) {
|
||||
loadAssetForKey(meshKey);
|
||||
resolved = resources->meshes().findMeshByKey(meshKey);
|
||||
}
|
||||
if (!resolved) {
|
||||
throw std::runtime_error("Mesh resource not found: " + meshKey);
|
||||
}
|
||||
meshID = *resolved;
|
||||
}
|
||||
if (!materialKey.empty()) {
|
||||
const auto resolved = resources->materials().findMaterialByKey(materialKey);
|
||||
auto resolved = resources->materials().findMaterialByKey(materialKey);
|
||||
if (!resolved) {
|
||||
loadAssetForKey(materialKey);
|
||||
resolved = resources->materials().findMaterialByKey(materialKey);
|
||||
}
|
||||
|
||||
if (!resolved) {
|
||||
const auto meshAsset = AssetReference::fromCacheKey(meshKey);
|
||||
resolved = resources->materials().findMaterialByName(
|
||||
materialKey,
|
||||
meshAsset ? meshAsset->path : std::string_view{});
|
||||
}
|
||||
|
||||
if (!resolved) {
|
||||
resolved = resources->materials().findMaterialByName(materialKey);
|
||||
}
|
||||
|
||||
if (!resolved) {
|
||||
throw std::runtime_error("Material resource not found: " + materialKey);
|
||||
}
|
||||
materialID = *resolved;
|
||||
materialKey = resources->materials().getMaterialKey(*resolved);
|
||||
}
|
||||
|
||||
if (meshID != NULL_MESH_ID && meshKey.empty()) {
|
||||
|
||||
Reference in New Issue
Block a user