Graphics/TextureLoader: Add support for alpha-test and alpha-blending
This commit is contained in:
parent
fe0d70d9e1
commit
04a8b03cf3
|
|
@ -5,6 +5,7 @@
|
||||||
#include <Nazara/Graphics/Formats/TextureLoader.hpp>
|
#include <Nazara/Graphics/Formats/TextureLoader.hpp>
|
||||||
#include <Nazara/Graphics/Graphics.hpp>
|
#include <Nazara/Graphics/Graphics.hpp>
|
||||||
#include <Nazara/Renderer/Texture.hpp>
|
#include <Nazara/Renderer/Texture.hpp>
|
||||||
|
#include <Nazara/Utility/Image.hpp>
|
||||||
#include <Nazara/Utility/Utility.hpp>
|
#include <Nazara/Utility/Utility.hpp>
|
||||||
#include <Nazara/Graphics/Debug.hpp>
|
#include <Nazara/Graphics/Debug.hpp>
|
||||||
|
|
||||||
|
|
@ -24,12 +25,27 @@ namespace Nz
|
||||||
{
|
{
|
||||||
TextureParams texParams;
|
TextureParams texParams;
|
||||||
texParams.renderDevice = Graphics::Instance()->GetRenderDevice();
|
texParams.renderDevice = Graphics::Instance()->GetRenderDevice();
|
||||||
|
// TODO: Set format as sRGB
|
||||||
|
|
||||||
std::shared_ptr<Texture> texture = Texture::LoadFromStream(stream, texParams);
|
std::shared_ptr<Image> image = Image::LoadFromStream(stream, texParams);
|
||||||
if (!texture)
|
if (!image)
|
||||||
return Err(ResourceLoadingError::Unrecognized);
|
return Err(ResourceLoadingError::Unrecognized);
|
||||||
|
|
||||||
bool hasAlphaTest = parameters.custom.GetBooleanParameter("EnableAlphaTest").GetValueOr(false);
|
std::shared_ptr<Texture> texture = Texture::CreateFromImage(*image, texParams);
|
||||||
|
if (!texture)
|
||||||
|
return Err(ResourceLoadingError::Internal);
|
||||||
|
|
||||||
|
bool enableAlphaTest = parameters.custom.GetBooleanParameter("EnableAlphaTest").GetValueOr(false);
|
||||||
|
bool enableAlphaBlending = parameters.custom.GetBooleanParameter("EnableAlphaBlending").GetValueOr(false);
|
||||||
|
|
||||||
|
bool hasAlpha;
|
||||||
|
if (enableAlphaTest || enableAlphaBlending)
|
||||||
|
hasAlpha = image->HasAlpha();
|
||||||
|
else
|
||||||
|
hasAlpha = false;
|
||||||
|
|
||||||
|
// Delete image to free memory
|
||||||
|
image.reset();
|
||||||
|
|
||||||
std::shared_ptr<MaterialInstance> materialInstance;
|
std::shared_ptr<MaterialInstance> materialInstance;
|
||||||
switch (parameters.lightingType)
|
switch (parameters.lightingType)
|
||||||
|
|
@ -49,9 +65,28 @@ namespace Nz
|
||||||
if (!materialInstance)
|
if (!materialInstance)
|
||||||
materialInstance = Graphics::Instance()->GetDefaultMaterials().basicMaterial->Instantiate();
|
materialInstance = Graphics::Instance()->GetDefaultMaterials().basicMaterial->Instantiate();
|
||||||
|
|
||||||
if (hasAlphaTest && PixelFormatInfo::HasAlpha(texture->GetFormat()))
|
if (enableAlphaTest && hasAlpha)
|
||||||
materialInstance->SetValueProperty("AlphaTest", true);
|
materialInstance->SetValueProperty("AlphaTest", true);
|
||||||
|
|
||||||
|
if (enableAlphaBlending && hasAlpha)
|
||||||
|
{
|
||||||
|
materialInstance->DisablePass("DepthPass");
|
||||||
|
materialInstance->UpdatePassStates("ForwardPass", [](RenderStates& renderStates)
|
||||||
|
{
|
||||||
|
renderStates.depthBuffer = true;
|
||||||
|
renderStates.depthWrite = false;
|
||||||
|
renderStates.blending = true;
|
||||||
|
renderStates.blend.modeColor = BlendEquation::Add;
|
||||||
|
renderStates.blend.modeAlpha = BlendEquation::Add;
|
||||||
|
renderStates.blend.srcColor = BlendFunc::SrcAlpha;
|
||||||
|
renderStates.blend.dstColor = BlendFunc::InvSrcAlpha;
|
||||||
|
renderStates.blend.srcAlpha = BlendFunc::One;
|
||||||
|
renderStates.blend.dstAlpha = BlendFunc::One;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
materialInstance->SetTextureProperty("BaseColorMap", std::move(texture));
|
materialInstance->SetTextureProperty("BaseColorMap", std::move(texture));
|
||||||
|
|
||||||
return materialInstance;
|
return materialInstance;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue