Graphics/Sprite: Allow to change origin

This commit is contained in:
Jérôme Leclercq 2021-12-23 17:32:18 +01:00
parent e2882f6595
commit ed3ee34565
3 changed files with 37 additions and 26 deletions

View File

@ -31,12 +31,15 @@ namespace Nz
inline const Color& GetCornerColor(RectCorner corner) const;
const std::shared_ptr<Material>& GetMaterial(std::size_t i = 0) const override;
std::size_t GetMaterialCount() const override;
inline const Vector3f& GetOrigin() const;
inline const Vector2f& GetSize() 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 SetOrigin(const Vector3f& origin);
inline void SetSize(const Vector2f& size);
inline void SetTextureCoords(const Rectf& textureCoords);
inline void SetTextureRect(const Rectf& textureRect);
@ -53,6 +56,7 @@ namespace Nz
Color m_color;
Rectf m_textureCoords;
Vector2f m_size;
Vector3f m_origin;
};
}

View File

@ -18,6 +18,16 @@ namespace Nz
return m_cornerColor[UnderlyingCast(corner)];
}
inline const Vector3f& Sprite::GetOrigin() const
{
return m_origin;
}
inline const Vector2f& Sprite::GetSize() const
{
return m_size;
}
inline const Rectf& Sprite::GetTextureCoords() const
{
return m_textureCoords;
@ -50,6 +60,13 @@ namespace Nz
}
}
inline void Sprite::SetOrigin(const Vector3f& origin)
{
m_origin = origin;
UpdateVertices();
}
inline void Sprite::SetSize(const Vector2f& size)
{
m_size = size;
@ -72,37 +89,26 @@ namespace Nz
inline void Sprite::UpdateVertices()
{
VertexStruct_XYZ_Color_UV* vertices = m_vertices.data();
Vector3f origin = Vector3f::Zero(); //< TODO
Boxf aabb;
vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::LeftBottom)];
vertices->position = Vector3f(-origin);
vertices->uv = m_textureCoords.GetCorner(RectCorner::LeftBottom);
VertexStruct_XYZ_Color_UV* vertices = m_vertices.data();
aabb.Set(vertices->position);
std::array<Vector2f, RectCornerCount> cornerExtent;
cornerExtent[UnderlyingCast(RectCorner::LeftBottom)] = Vector2f(0.f, 0.f);
cornerExtent[UnderlyingCast(RectCorner::RightBottom)] = Vector2f(1.f, 0.f);
cornerExtent[UnderlyingCast(RectCorner::LeftTop)] = Vector2f(0.f, 1.f);
cornerExtent[UnderlyingCast(RectCorner::RightTop)] = Vector2f(1.f, 1.f);
vertices++;
vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::RightBottom)];
vertices->position = m_size.x * Vector3f::Right() - origin;
vertices->uv = m_textureCoords.GetCorner(RectCorner::RightBottom);
for (RectCorner corner : { RectCorner::LeftBottom, RectCorner::RightBottom, RectCorner::LeftTop, RectCorner::RightTop })
{
vertices->color = m_color * m_cornerColor[UnderlyingCast(corner)];
vertices->position = Vector3f(m_size * cornerExtent[UnderlyingCast(corner)], 0.f) - m_origin;
vertices->uv = m_textureCoords.GetCorner(corner);
aabb.ExtendTo(vertices->position);
aabb.Set(vertices->position);
vertices++;
vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::LeftTop)];
vertices->position = m_size.y * Vector3f::Up() - origin;
vertices->uv = m_textureCoords.GetCorner(RectCorner::LeftTop);
aabb.ExtendTo(vertices->position);
vertices++;
vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::RightTop)];
vertices->position = m_size.x * Vector3f::Right() + m_size.y * Vector3f::Up() - origin;
vertices->uv = m_textureCoords.GetCorner(RectCorner::RightTop);
aabb.ExtendTo(vertices->position);
vertices++;
}
UpdateAABB(aabb);
OnElementInvalidated(this);

View File

@ -15,7 +15,8 @@ namespace Nz
m_material(std::move(material)),
m_color(Color::White),
m_textureCoords(0.f, 0.f, 1.f, 1.f),
m_size(64.f, 64.f)
m_size(64.f, 64.f),
m_origin(0.f, 0.f, 0.f)
{
m_cornerColor.fill(Color::White);