feat: fix scene loading that it loads from disk if not already loaded

This commit is contained in:
2026-08-11 01:52:14 +02:00
parent f298c7ada5
commit 662940793e
20 changed files with 964 additions and 434 deletions
+52 -11
View File
@@ -7,6 +7,7 @@
#include <algorithm>
#include <string>
#include <stdexcept>
#include <utility>
void MaterialCache::init(
GfxDevice& gfxDevice,
@@ -26,9 +27,9 @@ void MaterialCache::init(
materialDataBuffer.buffer,
"material data");
Material placeholderMaterial{};
placeholderMaterial.name = "PLACEHOLDER_MATERIAL";
placeholderMaterial.diffuseTexture = defaultTextures.white;
Material placeholderMaterial = Material::SimpleColor(
glm::vec3{1.0f},
"PLACEHOLDER_MATERIAL");
placeholderMaterialId = addMaterial(placeholderMaterial);
gfxDevice.getMemoryManager().flushAllocation(materialDataBuffer);
@@ -104,9 +105,14 @@ MaterialID MaterialCache::addMaterial(Material material)
// exact range after batching updates.
const auto materialId = static_cast<MaterialID>(materials.size());
std::string key = material.name.empty()
? "material:" + std::to_string(materialId)
: material.name;
std::string key;
if (!material.assetReference.empty()) {
key = material.assetReference.cacheKey();
} else {
key = material.name.empty()
? "material:" + std::to_string(materialId)
: material.name;
}
const std::string baseKey = key;
std::size_t suffix = 1;
while (std::find(materialKeys.begin(), materialKeys.end(), key) != materialKeys.end()) {
@@ -121,13 +127,17 @@ MaterialID MaterialCache::addMaterial(Material material)
MaterialID MaterialCache::addSimpleTextureMaterial(ImageID textureID)
{
Material material{};
material.name = "simple texture material";
Material material = Material::SimpleColor(
glm::vec3{1.0f},
"simple texture material");
material.diffuseTexture = textureID;
material.metallicFactor = 0.0f;
material.roughnessFactor = 1.0f;
return addMaterial(material);
return addMaterial(std::move(material));
}
MaterialID MaterialCache::addSimpleColorMaterial(glm::vec3 color, std::string name)
{
return addMaterial(Material::SimpleColor(std::move(color), std::move(name)));
}
const Material& MaterialCache::getMaterial(MaterialID id) const
@@ -149,6 +159,37 @@ std::optional<MaterialID> MaterialCache::findMaterialByKey(std::string_view key)
return static_cast<MaterialID>(std::distance(materialKeys.begin(), it));
}
std::optional<MaterialID> MaterialCache::findMaterialByName(
std::string_view name,
std::string_view sourceReference) const
{
std::optional<MaterialID> fallback;
for (MaterialID id = 0; id < materials.size(); ++id) {
const auto& material = materials[id];
if (material.name != name) {
continue;
}
if (sourceReference.empty()) {
return id;
}
if (!material.assetReference.empty() &&
material.assetReference.path == sourceReference) {
return id;
}
// A manually-created material may have the same name but no source
// reference. Keep it as a fallback for scenes saved before canonical
// file-backed material keys were introduced.
if (!fallback.has_value() && material.assetReference.empty()) {
fallback = id;
}
}
return fallback;
}
MaterialID MaterialCache::getFreeMaterialId() const
{
return static_cast<MaterialID>(materials.size());