diff --git a/Editor/ModelImporter_GLTF.cpp b/Editor/ModelImporter_GLTF.cpp index eaf272de3..b7d62bc2f 100644 --- a/Editor/ModelImporter_GLTF.cpp +++ b/Editor/ModelImporter_GLTF.cpp @@ -91,7 +91,7 @@ namespace tinygltf } } -void RegisterTexture(tinygltf::Image *image, const string& type_name) +std::shared_ptr RegisterTexture(tinygltf::Image *image, const string& type_name) { // We will load the texture2d by hand here and register to the resource manager (if it was not already registered) if (!wiResourceManager::Contains(wiHashString(image->uri))) @@ -154,7 +154,7 @@ void RegisterTexture(tinygltf::Image *image, const string& type_name) } // We loaded the texture2d, so register to the resource manager to be retrieved later: - wiResourceManager::Register(image->uri, tex, wiResource::IMAGE); + return wiResourceManager::Register(image->uri, tex, wiResource::IMAGE); } } else @@ -164,6 +164,8 @@ void RegisterTexture(tinygltf::Image *image, const string& type_name) } } + + return nullptr; } @@ -348,11 +350,12 @@ void ImportModel_GLTF(const std::string& fileName, Scene& scene) auto& alphaCutoff = x.additionalValues.find("alphaCutoff"); auto& alphaMode = x.additionalValues.find("alphaMode"); + if (baseColorTexture != x.values.end()) { auto& tex = state.gltfModel.textures[baseColorTexture->second.TextureIndex()]; auto& img = state.gltfModel.images[tex.source]; - RegisterTexture(&img, "basecolor"); + material.baseColorMap = RegisterTexture(&img, "basecolor"); material.baseColorMapName = img.uri; material.uvset_baseColorMap = baseColorTexture->second.TextureTexCoord(); } @@ -360,7 +363,7 @@ void ImportModel_GLTF(const std::string& fileName, Scene& scene) { auto& tex = state.gltfModel.textures[normalTexture->second.TextureIndex()]; auto& img = state.gltfModel.images[tex.source]; - RegisterTexture(&img, "normal"); + material.normalMap = RegisterTexture(&img, "normal"); material.normalMapName = img.uri; material.SetFlipNormalMap(true); // gltf import will always flip normal map by default material.uvset_normalMap = normalTexture->second.TextureTexCoord(); @@ -369,7 +372,7 @@ void ImportModel_GLTF(const std::string& fileName, Scene& scene) { auto& tex = state.gltfModel.textures[metallicRoughnessTexture->second.TextureIndex()]; auto& img = state.gltfModel.images[tex.source]; - RegisterTexture(&img, "roughness_metallic"); + material.surfaceMap = RegisterTexture(&img, "roughness_metallic"); material.surfaceMapName = img.uri; material.uvset_surfaceMap = metallicRoughnessTexture->second.TextureTexCoord(); } @@ -377,7 +380,7 @@ void ImportModel_GLTF(const std::string& fileName, Scene& scene) { auto& tex = state.gltfModel.textures[emissiveTexture->second.TextureIndex()]; auto& img = state.gltfModel.images[tex.source]; - RegisterTexture(&img, "emissive"); + material.emissiveMap = RegisterTexture(&img, "emissive"); material.emissiveMapName = img.uri; material.uvset_emissiveMap = emissiveTexture->second.TextureTexCoord(); } @@ -385,7 +388,7 @@ void ImportModel_GLTF(const std::string& fileName, Scene& scene) { auto& tex = state.gltfModel.textures[occlusionTexture->second.TextureIndex()]; auto& img = state.gltfModel.images[tex.source]; - RegisterTexture(&img, "occlusion"); + material.occlusionMap = RegisterTexture(&img, "occlusion"); material.occlusionMapName = img.uri; material.uvset_occlusionMap = occlusionTexture->second.TextureTexCoord(); material.SetOcclusionEnabled_Secondary(true); @@ -436,7 +439,7 @@ void ImportModel_GLTF(const std::string& fileName, Scene& scene) int index = specularGlossinessWorkflow->second.Get("diffuseTexture").Get("index").Get(); auto& tex = state.gltfModel.textures[index]; auto& img = state.gltfModel.images[tex.source]; - RegisterTexture(&img, "diffuse"); + material.baseColorMap = RegisterTexture(&img, "diffuse"); material.baseColorMapName = img.uri; material.uvset_baseColorMap = (uint32_t)specularGlossinessWorkflow->second.Get("diffuseTexture").Get("texCoord").Get(); } @@ -445,7 +448,7 @@ void ImportModel_GLTF(const std::string& fileName, Scene& scene) int index = specularGlossinessWorkflow->second.Get("specularGlossinessTexture").Get("index").Get(); auto& tex = state.gltfModel.textures[index]; auto& img = state.gltfModel.images[tex.source]; - RegisterTexture(&img, "specular_glossiness"); + material.surfaceMap = RegisterTexture(&img, "specular_glossiness"); material.surfaceMapName = img.uri; material.uvset_surfaceMap = (uint32_t)specularGlossinessWorkflow->second.Get("specularGlossinessTexture").Get("texCoord").Get(); } @@ -476,18 +479,6 @@ void ImportModel_GLTF(const std::string& fileName, Scene& scene) // Avoid zero roughness factors: material.roughness = max(0.001f, material.roughness); - // Retrieve textures by name: - if (!material.baseColorMapName.empty()) - material.baseColorMap = wiResourceManager::Load(material.baseColorMapName); - if (!material.normalMapName.empty()) - material.normalMap = wiResourceManager::Load(material.normalMapName); - if (!material.surfaceMapName.empty()) - material.surfaceMap = wiResourceManager::Load(material.surfaceMapName); - if (!material.emissiveMapName.empty()) - material.emissiveMap = wiResourceManager::Load(material.emissiveMapName); - if (!material.occlusionMapName.empty()) - material.occlusionMap = wiResourceManager::Load(material.occlusionMapName); - } if (scene.materials.GetCount() == 0) diff --git a/WickedEngine/wiResourceManager.cpp b/WickedEngine/wiResourceManager.cpp index 378d90684..884729ec1 100644 --- a/WickedEngine/wiResourceManager.cpp +++ b/WickedEngine/wiResourceManager.cpp @@ -312,17 +312,26 @@ namespace wiResourceManager return result; } - void Register(const wiHashString& name, void* data, wiResource::DATA_TYPE data_type) + std::shared_ptr Register(const wiHashString& name, void* data, wiResource::DATA_TYPE data_type) { + std::shared_ptr resource; + locker.lock(); - if (resources.find(name) == resources.end()) + auto it = resources.find(name); + if (it == resources.end() || it->second.lock() == nullptr) { - std::shared_ptr res = std::make_shared(); - res->data = data; - res->type = data_type; - resources.insert(make_pair(name, res)); + resource = std::make_shared(); + resource->data = data; + resource->type = data_type; + resources.insert(make_pair(name, resource)); + } + else + { + resource = it->second.lock(); } locker.unlock(); + + return resource; } void Clear() diff --git a/WickedEngine/wiResourceManager.h b/WickedEngine/wiResourceManager.h index d79ea3a65..6ec50324e 100644 --- a/WickedEngine/wiResourceManager.h +++ b/WickedEngine/wiResourceManager.h @@ -34,7 +34,7 @@ namespace wiResourceManager // Check if a resource is currently loaded bool Contains(const wiHashString& name); // Register a pre-created resource - void Register(const wiHashString& name, void* data, wiResource::DATA_TYPE data_type); + std::shared_ptr Register(const wiHashString& name, void* data, wiResource::DATA_TYPE data_type); // Invalidate all resources void Clear(); }; diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index f8ff1d6e8..90b26122e 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 36; // minor bug fixes, alterations, refactors, updates - const int revision = 16; + const int revision = 17; long GetVersion()