Graphics: Add TextureLoader

This commit is contained in:
SirLynix
2022-09-07 13:31:21 +02:00
parent 47cb878f9d
commit fbdc1faf8c
8 changed files with 202 additions and 23 deletions

View File

@@ -0,0 +1,88 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/Formats/TextureLoader.hpp>
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Graphics/BasicMaterial.hpp>
#include <Nazara/Graphics/PhongLightingMaterial.hpp>
#include <Nazara/Graphics/PhysicallyBasedMaterial.hpp>
#include <Nazara/Graphics/DepthMaterial.hpp>
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Utility/Utility.hpp>
#include <Nazara/Graphics/Debug.hpp>
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<std::shared_ptr<Material>, ResourceLoadingError>
{
TextureParams texParams;
texParams.renderDevice = Graphics::Instance()->GetRenderDevice();
std::shared_ptr<Texture> texture = Texture::LoadFromStream(stream, {});
if (!texture)
return Err(ResourceLoadingError::Unrecognized);
std::shared_ptr<Material> material = std::make_shared<Material>();
// ForwardPass
{
std::shared_ptr<MaterialPass> matPass;
if (parameters.lightingType == MaterialLightingType::Phong)
matPass = std::make_shared<MaterialPass>(PhongLightingMaterial::GetSettings());
else if (parameters.lightingType == MaterialLightingType::PhysicallyBased)
matPass = std::make_shared<MaterialPass>(PhysicallyBasedMaterial::GetSettings());
else
matPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
matPass->EnableDepthBuffer(true);
BasicMaterial forwardPass(*matPass);
forwardPass.SetBaseColorMap(texture);
if (PixelFormatInfo::HasAlpha(texture->GetFormat()))
forwardPass.EnableAlphaTest(true);
material->AddPass("ForwardPass", std::move(matPass));
}
// DepthPass
{
std::shared_ptr<MaterialPass> matPass = std::make_shared<MaterialPass>(DepthMaterial::GetSettings());
if (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)
{
bool skip;
if (parameters.custom.GetBooleanParameter("SkipNativeTextureLoader", &skip) && skip)
return false;
return true;
};
return loaderEntry;
}
}
}

View File

@@ -0,0 +1,18 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_LOADERS_TEXTURE_HPP
#define NAZARA_LOADERS_TEXTURE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/Material.hpp>
namespace Nz::Loaders
{
MaterialLoader::Entry GetMaterialLoader_Texture();
}
#endif // NAZARA_LOADERS_TEXTURE_HPP