From e8a362b695e419cde2650da031b22a73eca8fc81 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Thu, 4 Jan 2024 17:47:54 +0100 Subject: [PATCH] Graphics/MaterialInstance: Add Build method --- include/Nazara/Graphics/Material.hpp | 1 - include/Nazara/Graphics/MaterialInstance.hpp | 1 + src/Nazara/Graphics/Material.cpp | 5 - src/Nazara/Graphics/MaterialInstance.cpp | 100 +++++++++++++++++++ 4 files changed, 101 insertions(+), 6 deletions(-) diff --git a/include/Nazara/Graphics/Material.hpp b/include/Nazara/Graphics/Material.hpp index 17ba7dc23..f9368401a 100644 --- a/include/Nazara/Graphics/Material.hpp +++ b/include/Nazara/Graphics/Material.hpp @@ -65,7 +65,6 @@ namespace Nz std::shared_ptr Instantiate() const; - static std::shared_ptr Build(const ParameterList& materialData); static std::shared_ptr Get(MaterialType lightingType); static std::shared_ptr LoadFromFile(const std::filesystem::path& filePath, const MaterialParams& params = MaterialParams()); static std::shared_ptr LoadFromMemory(const void* data, std::size_t size, const MaterialParams& params = MaterialParams()); diff --git a/include/Nazara/Graphics/MaterialInstance.hpp b/include/Nazara/Graphics/MaterialInstance.hpp index 5387bf4a1..52f50d0af 100644 --- a/include/Nazara/Graphics/MaterialInstance.hpp +++ b/include/Nazara/Graphics/MaterialInstance.hpp @@ -115,6 +115,7 @@ namespace Nz static constexpr std::size_t InvalidPropertyIndex = MaterialSettings::InvalidPropertyIndex; + static std::shared_ptr Build(const ParameterList& materialData); static std::shared_ptr GetDefault(MaterialType materialType, MaterialInstancePreset preset = MaterialInstancePreset::Default); static std::shared_ptr LoadFromFile(const std::filesystem::path& filePath, const MaterialInstanceParams& params = MaterialInstanceParams()); static std::shared_ptr LoadFromMemory(const void* data, std::size_t size, const MaterialInstanceParams& params = MaterialInstanceParams()); diff --git a/src/Nazara/Graphics/Material.cpp b/src/Nazara/Graphics/Material.cpp index a7f2d252e..6f6d6c50f 100644 --- a/src/Nazara/Graphics/Material.cpp +++ b/src/Nazara/Graphics/Material.cpp @@ -199,11 +199,6 @@ namespace Nz return std::make_shared(shared_from_this()); } - std::shared_ptr Material::Build(const ParameterList& materialData) - { - return std::shared_ptr(); - } - std::shared_ptr Material::Get(MaterialType lightingType) { Graphics* graphics = Graphics::Instance(); diff --git a/src/Nazara/Graphics/MaterialInstance.cpp b/src/Nazara/Graphics/MaterialInstance.cpp index f5a21e384..502fbda65 100644 --- a/src/Nazara/Graphics/MaterialInstance.cpp +++ b/src/Nazara/Graphics/MaterialInstance.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace Nz @@ -345,6 +346,105 @@ namespace Nz OnTransferRequired(this); } + std::shared_ptr MaterialInstance::Build(const ParameterList& materialData) + { + MaterialType matType = MaterialType::Basic; + if (auto value = materialData.GetStringViewParameter(MaterialData::Type)) + { + std::string_view valueStr = value.GetValue(); + if (valueStr == "Basic") + matType = MaterialType::Basic; + else if (valueStr == "Phong") + matType = MaterialType::Phong; + else if (valueStr == "PhysicallyBased") + matType = MaterialType::PhysicallyBased; + else + NazaraWarningFmt("unknown material type: {}", valueStr); + } + + std::shared_ptr matInstance = Instantiate(matType, MaterialInstancePreset::Default); + + auto ConvertBool = [&](const char* paramKey, std::string_view matKey) + { + if (auto value = materialData.GetBooleanParameter(paramKey)) + matInstance->SetValueProperty(matKey, value.GetValue()); + }; + + auto ConvertColor = [&](const char* paramKey, std::string_view matKey) + { + if (auto value = materialData.GetColorParameter(paramKey)) + matInstance->SetValueProperty(matKey, value.GetValue()); + }; + + auto ConvertFloat = [&](const char* paramKey, std::string_view matKey) + { + if (auto value = materialData.GetDoubleParameter(paramKey)) + matInstance->SetValueProperty(matKey, SafeCast(value.GetValue())); + }; + + auto ConvertTexture = [&](const char* paramKey, std::string_view matKey, const char* paramFilterKey, const char* paramWrapKey) + { + if (auto value = materialData.GetStringViewParameter(paramKey)) + { + matInstance->SetTextureProperty(matKey, Texture::LoadFromFile(Utf8Path(value.GetValue()))); + } + + Nz::TextureSamplerInfo samplerInfo; + if (auto value = materialData.GetIntegerParameter(paramFilterKey)) + { + long long filterInt = value.GetValue(); + if (filterInt >= 0 || filterInt <= SafeCast(SamplerFilter::Max)) + samplerInfo.magFilter = samplerInfo.minFilter = static_cast(filterInt); + else + NazaraErrorFmt("invalid value for {0} sampler filter: {1}", paramFilterKey, filterInt); + } + + if (auto value = materialData.GetIntegerParameter(paramWrapKey)) + { + long long wrapInt = value.GetValue(); + if (wrapInt >= 0 || wrapInt <= SafeCast(SamplerWrap::Max)) + samplerInfo.wrapModeU = samplerInfo.wrapModeV = samplerInfo.wrapModeW = static_cast(wrapInt); + else + NazaraErrorFmt("invalid value for {0} sampler wrap: {1}", paramWrapKey, wrapInt); + } + + matInstance->SetTextureSamplerProperty(matKey, samplerInfo); + }; + + ConvertColor(MaterialData::BaseColor, "BaseColor"); + ConvertBool(MaterialData::AlphaTest, "AlphaTest"); + ConvertFloat(MaterialData::AlphaThreshold, "AlphaTestThreshold"); + ConvertTexture(MaterialData::BaseColorTexturePath, "BaseColorMap", MaterialData::BaseColorTextureFilter, MaterialData::BaseColorTextureWrap); + ConvertTexture(MaterialData::AlphaTexturePath, "AlphaMap", MaterialData::AlphaTextureFilter, MaterialData::AlphaTextureWrap); + + switch (matType) + { + case MaterialType::Basic: + break; + + case MaterialType::Phong: + ConvertColor(MaterialData::AmbientColor, "AmbientColor"); + ConvertColor(MaterialData::SpecularColor, "SpecularColor"); + ConvertFloat(MaterialData::Shininess, "Shininess"); + ConvertTexture(MaterialData::EmissiveTexturePath, "EmissiveMap", MaterialData::EmissiveTextureFilter, MaterialData::EmissiveTextureWrap); + ConvertTexture(MaterialData::HeightTexturePath, "HeightMap", MaterialData::HeightTextureFilter, MaterialData::HeightTextureWrap); + ConvertTexture(MaterialData::NormalTexturePath, "NormalMap", MaterialData::NormalTextureFilter, MaterialData::NormalTextureWrap); + ConvertTexture(MaterialData::SpecularTexturePath, "SpecularMap", MaterialData::SpecularTextureFilter, MaterialData::SpecularTextureWrap); + break; + + case MaterialType::PhysicallyBased: + ConvertTexture(MaterialData::EmissiveTexturePath, "EmissiveMap", MaterialData::EmissiveTextureFilter, MaterialData::EmissiveTextureWrap); + ConvertTexture(MaterialData::HeightTexturePath, "HeightMap", MaterialData::HeightTextureFilter, MaterialData::HeightTextureWrap); + ConvertTexture(MaterialData::MetallicTexturePath, "MetallicMap", MaterialData::MetallicTextureFilter, MaterialData::MetallicTextureWrap); + ConvertTexture(MaterialData::NormalTexturePath, "NormalMap", MaterialData::NormalTextureFilter, MaterialData::NormalTextureWrap); + ConvertTexture(MaterialData::RoughnessTexturePath, "RoughnessMap", MaterialData::RoughnessTextureFilter, MaterialData::RoughnessTextureWrap); + ConvertTexture(MaterialData::SpecularTexturePath, "SpecularMap", MaterialData::SpecularTextureFilter, MaterialData::SpecularTextureWrap); + break; + } + + return matInstance; + } + std::shared_ptr MaterialInstance::GetDefault(MaterialType materialType, MaterialInstancePreset preset) { Graphics* graphics = Graphics::Instance();