feat: fix scene loading that it loads from disk if not already loaded
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
#include <destrum/Assets/AssetManager.h>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <destrum/FS/AssetFS.h>
|
||||
|
||||
namespace {
|
||||
std::string ToReference(std::string_view reference) {
|
||||
if (reference.empty()) {
|
||||
throw std::invalid_argument("Asset reference cannot be empty");
|
||||
}
|
||||
return std::string{reference};
|
||||
}
|
||||
}
|
||||
|
||||
std::filesystem::path AssetManager::Resolve(std::string_view reference) const {
|
||||
const std::string value = ToReference(reference);
|
||||
if (value.find("://") != std::string::npos) {
|
||||
return AssetFS::GetInstance().GetFullPath(value);
|
||||
}
|
||||
|
||||
const std::filesystem::path path{value};
|
||||
if (path.is_absolute()) {
|
||||
return path;
|
||||
}
|
||||
throw std::invalid_argument(
|
||||
"Non-virtual asset references must be absolute: " + value);
|
||||
}
|
||||
|
||||
ModelDoc::ModelAsset AssetManager::LoadModel(
|
||||
std::string_view reference,
|
||||
const ModelDoc::LoadOptions& options) const
|
||||
{
|
||||
const std::string sourceReference = ToReference(reference);
|
||||
ModelDoc::ModelAsset model = ModelDoc::LoadModel(
|
||||
Resolve(sourceReference).generic_string(),
|
||||
options);
|
||||
|
||||
model.sourcePath = sourceReference;
|
||||
model.skeleton.assetReference.path = sourceReference;
|
||||
for (std::size_t index = 0; index < model.primitives.size(); ++index) {
|
||||
auto& primitive = model.primitives[index];
|
||||
primitive.mesh.assetReference.path = sourceReference;
|
||||
primitive.mesh.assetReference.subresource = primitive.sourceMeshIndex != ModelDoc::InvalidIndex
|
||||
? "mesh:" + std::to_string(primitive.sourceMeshIndex)
|
||||
: "primitive:" + std::to_string(index);
|
||||
}
|
||||
for (std::size_t index = 0; index < model.materials.size(); ++index) {
|
||||
auto& material = model.materials[index];
|
||||
material.assetReference.path = sourceReference;
|
||||
material.assetReference.subresource = "material:" + std::to_string(index);
|
||||
}
|
||||
for (std::size_t index = 0; index < model.animations.size(); ++index) {
|
||||
auto& animation = model.animations[index];
|
||||
animation.assetReference.path = sourceReference;
|
||||
animation.assetReference.subresource = "animation:" + std::to_string(index);
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
std::vector<SkeletalAnimation> AssetManager::LoadAnimationClips(
|
||||
std::string_view reference,
|
||||
const Skeleton& targetSkeleton) const
|
||||
{
|
||||
const std::string sourceReference = ToReference(reference);
|
||||
auto clips = ModelDoc::LoadAnimationClips(
|
||||
Resolve(sourceReference).generic_string(),
|
||||
targetSkeleton);
|
||||
for (std::size_t index = 0; index < clips.size(); ++index) {
|
||||
auto& clip = clips[index];
|
||||
clip.assetReference.path = sourceReference;
|
||||
clip.assetReference.subresource = "animation:" + std::to_string(index);
|
||||
}
|
||||
return clips;
|
||||
}
|
||||
Reference in New Issue
Block a user