// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com) // This file is part of the "Nazara Engine - Graphics module" // For conditions of distribution and use, see copyright notice in Config.hpp #include #include #include #include #include #include #include #include #include namespace Nz { namespace Loaders { MaterialLoader::Entry GetMaterialLoader_Texture() { MaterialLoader::Entry loaderEntry; loaderEntry.extensionSupport = [](std::string_view extension) { return Utility::Instance()->GetImageLoader().IsExtensionSupported(extension); }; loaderEntry.streamLoader = [](Stream& stream, const MaterialParams& parameters) -> Result, ResourceLoadingError> { TextureParams texParams; texParams.renderDevice = Graphics::Instance()->GetRenderDevice(); std::shared_ptr texture = Texture::LoadFromStream(stream, texParams); if (!texture) return Err(ResourceLoadingError::Unrecognized); std::shared_ptr material = std::make_shared(); bool hasAlphaTest = parameters.custom.GetBooleanParameter("EnableAlphaTest").GetValueOr(false); // ForwardPass { std::shared_ptr matPass; if (parameters.lightingType == MaterialLightingType::Phong) matPass = std::make_shared(PhongLightingMaterial::GetSettings()); else if (parameters.lightingType == MaterialLightingType::PhysicallyBased) matPass = std::make_shared(PhysicallyBasedMaterial::GetSettings()); else matPass = std::make_shared(BasicMaterial::GetSettings()); matPass->EnableDepthBuffer(true); BasicMaterial forwardPass(*matPass); forwardPass.SetBaseColorMap(texture); if (hasAlphaTest && PixelFormatInfo::HasAlpha(texture->GetFormat())) forwardPass.EnableAlphaTest(true); material->AddPass("ForwardPass", std::move(matPass)); } // DepthPass { std::shared_ptr matPass = std::make_shared(DepthMaterial::GetSettings()); matPass->EnableDepthBuffer(true); if (hasAlphaTest && PixelFormatInfo::HasAlpha(texture->GetFormat())) { BasicMaterial depthPass(*matPass); depthPass.SetBaseColorMap(texture); depthPass.EnableAlphaTest(true); } material->AddPass("DepthPass", std::move(matPass)); } return material; }; loaderEntry.parameterFilter = [](const MaterialParams& parameters) { if (auto result = parameters.custom.GetBooleanParameter("SkipNativeTextureLoader"); result.GetValueOr(false)) return false; return true; }; return loaderEntry; } } }