Refactor material system (#382)
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/BaseWidget.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
|
||||
#include <Nazara/Utility/Components/NodeComponent.hpp>
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/CheckboxWidget.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/MaterialPass.hpp>
|
||||
#include <Nazara/Graphics/SlicedSprite.hpp>
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/DefaultWidgetTheme.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/MaterialInstance.hpp>
|
||||
#include <Nazara/Graphics/MaterialPass.hpp>
|
||||
#include <Nazara/Widgets/SimpleWidgetStyles.hpp>
|
||||
#include <Nazara/Widgets/Widgets.hpp>
|
||||
@@ -125,20 +124,30 @@ namespace Nz
|
||||
texParams.renderDevice = Graphics::Instance()->GetRenderDevice();
|
||||
texParams.loadFormat = PixelFormat::RGBA8; //< TODO: Re-enable gamma correction
|
||||
|
||||
auto CreateMaterialFromTexture = [](std::shared_ptr<Texture> texture)
|
||||
const auto& defaultBasicMaterial = Graphics::Instance()->GetDefaultMaterials();
|
||||
const MaterialPassRegistry& materialPassRegistry = Graphics::Instance()->GetMaterialPassRegistry();
|
||||
|
||||
std::size_t depthPassIndex = materialPassRegistry.GetPassIndex("DepthPass");
|
||||
std::size_t forwardPassIndex = materialPassRegistry.GetPassIndex("ForwardPass");
|
||||
|
||||
auto CreateMaterialFromTexture = [&](std::shared_ptr<Texture> texture)
|
||||
{
|
||||
std::shared_ptr<MaterialPass> buttonMaterialPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
|
||||
buttonMaterialPass->EnableDepthBuffer(true);
|
||||
buttonMaterialPass->EnableDepthWrite(false);
|
||||
buttonMaterialPass->EnableBlending(true);
|
||||
buttonMaterialPass->SetBlendEquation(BlendEquation::Add, BlendEquation::Add);
|
||||
buttonMaterialPass->SetBlendFunc(BlendFunc::SrcAlpha, BlendFunc::InvSrcAlpha, BlendFunc::One, BlendFunc::One);
|
||||
std::shared_ptr<MaterialInstance> material = defaultBasicMaterial.basicMaterial->CreateInstance();
|
||||
material->DisablePass(depthPassIndex);
|
||||
material->UpdatePassStates(forwardPassIndex, [](RenderStates& renderStates)
|
||||
{
|
||||
renderStates.depthWrite = false;
|
||||
renderStates.scissorTest = true;
|
||||
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;
|
||||
});
|
||||
|
||||
std::shared_ptr<Material> material = std::make_shared<Material>();
|
||||
material->AddPass("ForwardPass", buttonMaterialPass);
|
||||
|
||||
BasicMaterial buttonBasicMat(*buttonMaterialPass);
|
||||
buttonBasicMat.SetBaseColorMap(texture);
|
||||
material->SetTextureProperty("BaseColorMap", std::move(texture));
|
||||
|
||||
return material;
|
||||
};
|
||||
|
||||
@@ -3,13 +3,12 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/ImageButtonWidget.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/MaterialInstance.hpp>
|
||||
#include <Nazara/Widgets/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
ImageButtonWidget::ImageButtonWidget(BaseWidget* parent, std::shared_ptr<Material> material, std::shared_ptr<Material> hoveredMaterial, std::shared_ptr<Material> pressedMaterial, float cornerSize, float cornerTexCoords) :
|
||||
ImageButtonWidget::ImageButtonWidget(BaseWidget* parent, std::shared_ptr<MaterialInstance> material, std::shared_ptr<MaterialInstance> hoveredMaterial, std::shared_ptr<MaterialInstance> pressedMaterial, float cornerSize, float cornerTexCoords) :
|
||||
BaseWidget(parent),
|
||||
m_hoveredMaterial(std::move(hoveredMaterial)),
|
||||
m_material(std::move(material)),
|
||||
@@ -80,20 +79,16 @@ namespace Nz
|
||||
const Rectf& textureCoords = GetTextureCoords();
|
||||
|
||||
// TODO: Move this in a separate function
|
||||
if (const auto& material = m_material->FindPass("ForwardPass"))
|
||||
if (const std::shared_ptr<Texture>* textureOpt = m_material->GetTextureProperty("BaseColorMap"))
|
||||
{
|
||||
BasicMaterial mat(*material);
|
||||
if (mat.HasBaseColorMap())
|
||||
// Material should always have textures but we're better safe than sorry
|
||||
if (const std::shared_ptr<Texture>& texture = *textureOpt)
|
||||
{
|
||||
// Material should always have textures but we're better safe than sorry
|
||||
if (const auto& texture = mat.GetBaseColorMap())
|
||||
{
|
||||
Vector2f textureSize = Vector2f(Vector2ui(texture->GetSize()));
|
||||
textureSize.x *= textureCoords.width;
|
||||
textureSize.y *= textureCoords.height;
|
||||
Vector2f textureSize = Vector2f(Vector2ui(texture->GetSize()));
|
||||
textureSize.x *= textureCoords.width;
|
||||
textureSize.y *= textureCoords.height;
|
||||
|
||||
SetPreferredSize(textureSize);
|
||||
}
|
||||
SetPreferredSize(textureSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
ImageWidget::ImageWidget(BaseWidget* parent, std::shared_ptr<Material> material) :
|
||||
ImageWidget::ImageWidget(BaseWidget* parent, std::shared_ptr<MaterialInstance> material) :
|
||||
BaseWidget(parent)
|
||||
{
|
||||
m_sprite = std::make_shared<Sprite>(Widgets::Instance()->GetTransparentMaterial());
|
||||
m_sprite = std::make_shared<Sprite>(std::move(material));
|
||||
|
||||
auto& registry = GetRegistry();
|
||||
|
||||
@@ -25,8 +25,6 @@ namespace Nz
|
||||
|
||||
auto& nodeComponent = registry.emplace<NodeComponent>(m_entity);
|
||||
nodeComponent.SetParent(this);
|
||||
|
||||
SetMaterial(std::move(material));
|
||||
}
|
||||
|
||||
void ImageWidget::Layout()
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/LabelWidget.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
|
||||
#include <Nazara/Utility/AbstractTextDrawer.hpp>
|
||||
#include <Nazara/Utility/Components/NodeComponent.hpp>
|
||||
|
||||
@@ -331,7 +331,7 @@ namespace Nz
|
||||
}
|
||||
|
||||
|
||||
SimpleLabelWidgetStyle::SimpleLabelWidgetStyle(LabelWidget* labelWidget, std::shared_ptr<Material> material, std::shared_ptr<Material> hoveredMaterial) :
|
||||
SimpleLabelWidgetStyle::SimpleLabelWidgetStyle(LabelWidget* labelWidget, std::shared_ptr<MaterialInstance> material, std::shared_ptr<MaterialInstance> hoveredMaterial) :
|
||||
LabelWidgetStyle(labelWidget, 1),
|
||||
m_hoveredMaterial(std::move(hoveredMaterial)),
|
||||
m_material(std::move(material))
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/Widgets.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/MaterialInstance.hpp>
|
||||
#include <Nazara/Graphics/MaterialPass.hpp>
|
||||
#include <Nazara/Widgets/Debug.hpp>
|
||||
|
||||
@@ -23,24 +22,36 @@ namespace Nz
|
||||
|
||||
void Widgets::CreateDefaultMaterials()
|
||||
{
|
||||
m_opaqueMaterialPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
|
||||
m_opaqueMaterialPass->EnableDepthBuffer(true);
|
||||
m_opaqueMaterialPass->EnableDepthWrite(false);
|
||||
m_opaqueMaterialPass->EnableScissorTest(true);
|
||||
const auto& defaultMaterials = Graphics::Instance()->GetDefaultMaterials();
|
||||
|
||||
m_opaqueMaterial = std::make_shared<Material>();
|
||||
m_opaqueMaterial->AddPass("ForwardPass", m_opaqueMaterialPass);
|
||||
const MaterialPassRegistry& materialPassRegistry = Graphics::Instance()->GetMaterialPassRegistry();
|
||||
std::size_t depthPassIndex = materialPassRegistry.GetPassIndex("DepthPass");
|
||||
std::size_t forwardPassIndex = materialPassRegistry.GetPassIndex("ForwardPass");
|
||||
|
||||
m_transparentMaterialPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
|
||||
m_transparentMaterialPass->EnableDepthBuffer(true);
|
||||
m_transparentMaterialPass->EnableDepthWrite(false);
|
||||
m_transparentMaterialPass->EnableScissorTest(true);
|
||||
m_transparentMaterialPass->EnableBlending(true);
|
||||
m_transparentMaterialPass->SetBlendEquation(BlendEquation::Add, BlendEquation::Add);
|
||||
m_transparentMaterialPass->SetBlendFunc(BlendFunc::SrcAlpha, BlendFunc::InvSrcAlpha, BlendFunc::One, BlendFunc::One);
|
||||
m_opaqueMaterial = defaultMaterials.basicMaterial->CreateInstance();
|
||||
for (std::size_t passIndex : { depthPassIndex, forwardPassIndex })
|
||||
{
|
||||
m_opaqueMaterial->UpdatePassStates(passIndex, [](RenderStates& renderStates)
|
||||
{
|
||||
renderStates.scissorTest = true;
|
||||
});
|
||||
}
|
||||
|
||||
m_transparentMaterial = std::make_shared<Material>();
|
||||
m_transparentMaterial->AddPass("ForwardPass", m_transparentMaterialPass);
|
||||
m_transparentMaterial = defaultMaterials.basicMaterial->CreateInstance();
|
||||
m_transparentMaterial->DisablePass(depthPassIndex);
|
||||
|
||||
m_transparentMaterial->UpdatePassStates(forwardPassIndex, [](RenderStates& renderStates)
|
||||
{
|
||||
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;
|
||||
renderStates.depthWrite = false;
|
||||
renderStates.scissorTest = true;
|
||||
});
|
||||
}
|
||||
|
||||
Widgets* Widgets::s_instance = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user