Graphics/MaterialInstance: Add Build method

This commit is contained in:
SirLynix 2024-01-04 17:47:54 +01:00
parent 7ae76b32b0
commit e8a362b695
4 changed files with 101 additions and 6 deletions

View File

@ -65,7 +65,6 @@ namespace Nz
std::shared_ptr<MaterialInstance> Instantiate() const;
static std::shared_ptr<Material> Build(const ParameterList& materialData);
static std::shared_ptr<Material> Get(MaterialType lightingType);
static std::shared_ptr<Material> LoadFromFile(const std::filesystem::path& filePath, const MaterialParams& params = MaterialParams());
static std::shared_ptr<Material> LoadFromMemory(const void* data, std::size_t size, const MaterialParams& params = MaterialParams());

View File

@ -115,6 +115,7 @@ namespace Nz
static constexpr std::size_t InvalidPropertyIndex = MaterialSettings::InvalidPropertyIndex;
static std::shared_ptr<MaterialInstance> Build(const ParameterList& materialData);
static std::shared_ptr<MaterialInstance> GetDefault(MaterialType materialType, MaterialInstancePreset preset = MaterialInstancePreset::Default);
static std::shared_ptr<MaterialInstance> LoadFromFile(const std::filesystem::path& filePath, const MaterialInstanceParams& params = MaterialInstanceParams());
static std::shared_ptr<MaterialInstance> LoadFromMemory(const void* data, std::size_t size, const MaterialInstanceParams& params = MaterialInstanceParams());

View File

@ -199,11 +199,6 @@ namespace Nz
return std::make_shared<MaterialInstance>(shared_from_this());
}
std::shared_ptr<Material> Material::Build(const ParameterList& materialData)
{
return std::shared_ptr<Material>();
}
std::shared_ptr<Material> Material::Get(MaterialType lightingType)
{
Graphics* graphics = Graphics::Instance();

View File

@ -10,6 +10,7 @@
#include <Nazara/Renderer/CommandBufferBuilder.hpp>
#include <Nazara/Renderer/RenderResources.hpp>
#include <Nazara/Renderer/UploadPool.hpp>
#include <Nazara/Utility/MaterialData.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
@ -345,6 +346,105 @@ namespace Nz
OnTransferRequired(this);
}
std::shared_ptr<MaterialInstance> 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<MaterialInstance> 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<float>(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<long long>(SamplerFilter::Max))
samplerInfo.magFilter = samplerInfo.minFilter = static_cast<SamplerFilter>(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<long long>(SamplerWrap::Max))
samplerInfo.wrapModeU = samplerInfo.wrapModeV = samplerInfo.wrapModeW = static_cast<SamplerWrap>(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> MaterialInstance::GetDefault(MaterialType materialType, MaterialInstancePreset preset)
{
Graphics* graphics = Graphics::Instance();