Graphics/Sprite: Allow to change origin
This commit is contained in:
parent
e2882f6595
commit
ed3ee34565
|
|
@ -31,12 +31,15 @@ namespace Nz
|
||||||
inline const Color& GetCornerColor(RectCorner corner) const;
|
inline const Color& GetCornerColor(RectCorner corner) const;
|
||||||
const std::shared_ptr<Material>& GetMaterial(std::size_t i = 0) const override;
|
const std::shared_ptr<Material>& GetMaterial(std::size_t i = 0) const override;
|
||||||
std::size_t GetMaterialCount() const override;
|
std::size_t GetMaterialCount() const override;
|
||||||
|
inline const Vector3f& GetOrigin() const;
|
||||||
|
inline const Vector2f& GetSize() const;
|
||||||
inline const Rectf& GetTextureCoords() const;
|
inline const Rectf& GetTextureCoords() const;
|
||||||
Vector3ui GetTextureSize() const;
|
Vector3ui GetTextureSize() const;
|
||||||
|
|
||||||
inline void SetColor(const Color& color);
|
inline void SetColor(const Color& color);
|
||||||
inline void SetCornerColor(RectCorner corner, const Color& color);
|
inline void SetCornerColor(RectCorner corner, const Color& color);
|
||||||
inline void SetMaterial(std::shared_ptr<Material> material);
|
inline void SetMaterial(std::shared_ptr<Material> material);
|
||||||
|
inline void SetOrigin(const Vector3f& origin);
|
||||||
inline void SetSize(const Vector2f& size);
|
inline void SetSize(const Vector2f& size);
|
||||||
inline void SetTextureCoords(const Rectf& textureCoords);
|
inline void SetTextureCoords(const Rectf& textureCoords);
|
||||||
inline void SetTextureRect(const Rectf& textureRect);
|
inline void SetTextureRect(const Rectf& textureRect);
|
||||||
|
|
@ -53,6 +56,7 @@ namespace Nz
|
||||||
Color m_color;
|
Color m_color;
|
||||||
Rectf m_textureCoords;
|
Rectf m_textureCoords;
|
||||||
Vector2f m_size;
|
Vector2f m_size;
|
||||||
|
Vector3f m_origin;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,16 @@ namespace Nz
|
||||||
return m_cornerColor[UnderlyingCast(corner)];
|
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
|
inline const Rectf& Sprite::GetTextureCoords() const
|
||||||
{
|
{
|
||||||
return m_textureCoords;
|
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)
|
inline void Sprite::SetSize(const Vector2f& size)
|
||||||
{
|
{
|
||||||
m_size = size;
|
m_size = size;
|
||||||
|
|
@ -72,37 +89,26 @@ namespace Nz
|
||||||
|
|
||||||
inline void Sprite::UpdateVertices()
|
inline void Sprite::UpdateVertices()
|
||||||
{
|
{
|
||||||
VertexStruct_XYZ_Color_UV* vertices = m_vertices.data();
|
|
||||||
|
|
||||||
Vector3f origin = Vector3f::Zero(); //< TODO
|
|
||||||
Boxf aabb;
|
Boxf aabb;
|
||||||
|
|
||||||
vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::LeftBottom)];
|
VertexStruct_XYZ_Color_UV* vertices = m_vertices.data();
|
||||||
vertices->position = Vector3f(-origin);
|
|
||||||
vertices->uv = m_textureCoords.GetCorner(RectCorner::LeftBottom);
|
|
||||||
|
|
||||||
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++;
|
for (RectCorner corner : { RectCorner::LeftBottom, RectCorner::RightBottom, RectCorner::LeftTop, RectCorner::RightTop })
|
||||||
vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::RightBottom)];
|
{
|
||||||
vertices->position = m_size.x * Vector3f::Right() - origin;
|
vertices->color = m_color * m_cornerColor[UnderlyingCast(corner)];
|
||||||
vertices->uv = m_textureCoords.GetCorner(RectCorner::RightBottom);
|
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++;
|
||||||
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);
|
|
||||||
|
|
||||||
UpdateAABB(aabb);
|
UpdateAABB(aabb);
|
||||||
OnElementInvalidated(this);
|
OnElementInvalidated(this);
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,8 @@ namespace Nz
|
||||||
m_material(std::move(material)),
|
m_material(std::move(material)),
|
||||||
m_color(Color::White),
|
m_color(Color::White),
|
||||||
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
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);
|
m_cornerColor.fill(Color::White);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue