Graphics/Sprite: Add [Get|Set]TextureCoords & SetTextureRect

This commit is contained in:
Jérôme Leclercq 2021-11-24 22:00:29 +01:00
parent b8b0552a4e
commit c9aba016a8
5 changed files with 64 additions and 11 deletions

View File

@ -22,6 +22,8 @@ namespace Nz
inline void AddPass(std::size_t passIndex, std::shared_ptr<MaterialPass> pass);
inline void AddPass(std::string passName, std::shared_ptr<MaterialPass> pass);
inline const std::shared_ptr<MaterialPass>& FindPass(const std::string& passName) const;
inline const std::shared_ptr<MaterialPass>& GetPass(std::size_t passIndex) const;
inline bool HasPass(std::size_t passIndex) const;

View File

@ -21,6 +21,12 @@ namespace Nz
return AddPass(registry.GetPassIndex(passName), std::move(pass));
}
inline const std::shared_ptr<MaterialPass>& Material::FindPass(const std::string& passName) const
{
auto& registry = Graphics::Instance()->GetMaterialPassRegistry();
return GetPass(registry.GetPassIndex(passName));
}
inline const std::shared_ptr<MaterialPass>& Material::GetPass(std::size_t passIndex) const
{
if (passIndex >= m_passes.size())

View File

@ -29,13 +29,17 @@ namespace Nz
inline const Color& GetColor() const;
inline const Color& GetCornerColor(RectCorner corner) const;
const std::shared_ptr<Material>& GetMaterial(std::size_t i) const;
const std::shared_ptr<Material>& GetMaterial(std::size_t i = 0) const;
std::size_t GetMaterialCount() const;
inline const Rectf& GetTextureCoords() const;
Vector3ui GetTextureSize() const;
inline void SetColor(const Color& color);
inline void SetCornerColor(RectCorner corner, const Color& color);
inline void SetMaterial(std::shared_ptr<Material> material);
inline void SetSize(const Vector2f& size);
inline void SetTextureCoords(const Rectf& textureCoords);
inline void SetTextureRect(const Rectf& textureRect);
Sprite& operator=(const Sprite&) = delete;
Sprite& operator=(Sprite&&) noexcept = default;

View File

@ -8,6 +8,21 @@
namespace Nz
{
inline const Color& Sprite::GetColor() const
{
return m_color;
}
inline const Color& Sprite::GetCornerColor(RectCorner corner) const
{
return m_cornerColor[UnderlyingCast(corner)];
}
inline const Rectf& Sprite::GetTextureCoords() const
{
return m_textureCoords;
}
inline void Sprite::SetColor(const Color& color)
{
m_color = color;
@ -24,6 +39,8 @@ namespace Nz
inline void Sprite::SetMaterial(std::shared_ptr<Material> material)
{
assert(material);
m_material = std::move(material);
}
@ -34,6 +51,19 @@ namespace Nz
UpdateVertices();
}
inline void Sprite::SetTextureCoords(const Rectf& textureCoords)
{
m_textureCoords = textureCoords;
UpdateVertices();
}
inline void Sprite::SetTextureRect(const Rectf& textureRect)
{
Vector2ui textureSize(GetTextureSize());
return SetTextureCoords(textureRect / Vector2f(textureSize));
}
inline void Sprite::UpdateVertices()
{
VertexStruct_XYZ_Color_UV* vertices = m_vertices.data();

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Graphics/BasicMaterial.hpp>
#include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/RenderSpriteChain.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
@ -44,16 +45,6 @@ namespace Nz
elements.emplace_back(std::make_unique<RenderSpriteChain>(0, materialPass, renderPipeline, worldInstance, vertexDeclaration, whiteTexture, 1, m_vertices.data()));
}
inline const Color& Sprite::GetColor() const
{
return m_color;
}
inline const Color& Sprite::GetCornerColor(RectCorner corner) const
{
return m_cornerColor[UnderlyingCast(corner)];
}
const std::shared_ptr<Material>& Sprite::GetMaterial(std::size_t i) const
{
assert(i == 0);
@ -66,4 +57,24 @@ namespace Nz
{
return 1;
}
Vector3ui Sprite::GetTextureSize() const
{
assert(m_material);
//TODO: Cache index in registry?
if (const auto& material = m_material->FindPass("ForwardPass"))
{
BasicMaterial mat(*material);
if (mat.HasDiffuseMap())
{
// Material should always have textures but we're better safe than sorry
if (const auto& texture = mat.GetDiffuseMap())
return mat.GetDiffuseMap()->GetSize();
}
}
// Couldn't get material pass or texture
return Vector3ui::Zero();
}
}