resource manager and gltf importer fix

This commit is contained in:
turanszkij
2019-12-22 22:09:08 +01:00
parent de596ce485
commit f6cca39c01
4 changed files with 29 additions and 29 deletions
+12 -21
View File
@@ -91,7 +91,7 @@ namespace tinygltf
}
}
void RegisterTexture(tinygltf::Image *image, const string& type_name)
std::shared_ptr<wiResource> 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<int>();
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<int>();
}
@@ -445,7 +448,7 @@ void ImportModel_GLTF(const std::string& fileName, Scene& scene)
int index = specularGlossinessWorkflow->second.Get("specularGlossinessTexture").Get("index").Get<int>();
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<int>();
}
@@ -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)
+15 -6
View File
@@ -312,17 +312,26 @@ namespace wiResourceManager
return result;
}
void Register(const wiHashString& name, void* data, wiResource::DATA_TYPE data_type)
std::shared_ptr<wiResource> Register(const wiHashString& name, void* data, wiResource::DATA_TYPE data_type)
{
std::shared_ptr<wiResource> 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<wiResource> res = std::make_shared<wiResource>();
res->data = data;
res->type = data_type;
resources.insert(make_pair(name, res));
resource = std::make_shared<wiResource>();
resource->data = data;
resource->type = data_type;
resources.insert(make_pair(name, resource));
}
else
{
resource = it->second.lock();
}
locker.unlock();
return resource;
}
void Clear()
+1 -1
View File
@@ -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<wiResource> Register(const wiHashString& name, void* data, wiResource::DATA_TYPE data_type);
// Invalidate all resources
void Clear();
};
+1 -1
View File
@@ -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()