From b256ddd06b190fb003670754dda2ec4b8a61d346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Wed, 1 Dec 2021 10:38:53 +0100 Subject: [PATCH] Graphics/SlicedSprite: Handle corner removal + lower size --- include/Nazara/Graphics/SlicedSprite.hpp | 5 +- src/Nazara/Graphics/SlicedSprite.cpp | 73 +++++++++++++++--------- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/include/Nazara/Graphics/SlicedSprite.hpp b/include/Nazara/Graphics/SlicedSprite.hpp index 125a5a22f..2a60b37ae 100644 --- a/include/Nazara/Graphics/SlicedSprite.hpp +++ b/include/Nazara/Graphics/SlicedSprite.hpp @@ -51,8 +51,8 @@ namespace Nz struct Corner { - Vector2f textureCoords = Vector2f(0.125f, 0.125f); - Vector2f size = Vector2f(16.f, 16.f); + Vector2f textureCoords = Vector2f(0.15625f, 0.15625f); + Vector2f size = Vector2f(10.f, 10.f); }; private: @@ -60,6 +60,7 @@ namespace Nz std::array m_vertices; std::shared_ptr m_material; + std::size_t m_spriteCount; Color m_color; Corner m_topLeftCorner; Corner m_bottomRightCorner; diff --git a/src/Nazara/Graphics/SlicedSprite.cpp b/src/Nazara/Graphics/SlicedSprite.cpp index 700137126..b96d19838 100644 --- a/src/Nazara/Graphics/SlicedSprite.cpp +++ b/src/Nazara/Graphics/SlicedSprite.cpp @@ -39,7 +39,7 @@ namespace Nz const auto& whiteTexture = Graphics::Instance()->GetDefaultTextures().whiteTextures[UnderlyingCast(ImageType::E2D)]; - elements.emplace_back(std::make_unique(0, materialPass, renderPipeline, worldInstance, vertexDeclaration, whiteTexture, 9, m_vertices.data())); + elements.emplace_back(std::make_unique(0, materialPass, renderPipeline, worldInstance, vertexDeclaration, whiteTexture, m_spriteCount, m_vertices.data())); } const std::shared_ptr& SlicedSprite::GetMaterial(std::size_t i) const @@ -111,49 +111,66 @@ namespace Nz Vector3f origin = Vector3f::Zero(); Vector2f topLeftUV = m_textureCoords.GetCorner(RectCorner::LeftTop); + m_spriteCount = 0; for (std::size_t y = 0; y < 3; ++y) { - for (std::size_t x = 0; x < 3; ++x) + float height = heights[y]; + if (height > 0.f) { - vertices->color = m_color; - vertices->position = origin; - vertices->uv = topLeftUV; - vertices++; + for (std::size_t x = 0; x < 3; ++x) + { + float width = widths[x]; + if (width > 0.f) + { + vertices->color = m_color; + vertices->position = origin; + vertices->uv = topLeftUV; + vertices++; - vertices->color = m_color; - vertices->position = origin + widths[x] * Vector3f::Right(); - vertices->uv = topLeftUV + Vector2f(texCoordsX[x], 0.f); - vertices++; + vertices->color = m_color; + vertices->position = origin + width * Vector3f::Right(); + vertices->uv = topLeftUV + Vector2f(texCoordsX[x], 0.f); + vertices++; - vertices->color = m_color; - vertices->position = origin + heights[y] * Vector3f::Up(); - vertices->uv = topLeftUV + Vector2f(0.f, texCoordsY[y]); - vertices++; + vertices->color = m_color; + vertices->position = origin + height * Vector3f::Up(); + vertices->uv = topLeftUV + Vector2f(0.f, texCoordsY[y]); + vertices++; - vertices->color = m_color; - vertices->position = origin + widths[x] * Vector3f::Right() + heights[y] * Vector3f::Up(); - vertices->uv = topLeftUV + Vector2f(texCoordsX[x], texCoordsY[y]); - vertices++; + vertices->color = m_color; + vertices->position = origin + width * Vector3f::Right() + height * Vector3f::Up(); + vertices->uv = topLeftUV + Vector2f(texCoordsX[x], texCoordsY[y]); + vertices++; - origin.x += widths[x]; - topLeftUV.x += texCoordsX[x]; + origin.x += width; + m_spriteCount++; + } + + topLeftUV.x += texCoordsX[x]; + } + + origin.y += height; } origin.x = 0; - origin.y += heights[y]; topLeftUV.x = m_textureCoords.x; topLeftUV.y += texCoordsY[y]; } - // Reverse texcoords Y - for (auto& vertex : m_vertices) - vertex.uv.y = m_textureCoords.height - vertex.uv.y; + Boxf aabb = Boxf::Zero(); - Boxf aabb; - aabb.Set(m_vertices[0].position); - for (std::size_t i = 1; i < m_vertices.size(); ++i) - aabb.ExtendTo(m_vertices[i].position); + std::size_t vertexCount = 4 * m_spriteCount; + if (vertexCount > 0) + { + // Reverse texcoords Y + for (std::size_t i = 0; i < vertexCount; ++i) + m_vertices[i].uv.y = m_textureCoords.height - m_vertices[i].uv.y; + + aabb.Set(m_vertices[0].position); + for (std::size_t i = 1; i < vertexCount; ++i) + aabb.ExtendTo(m_vertices[i].position); + } UpdateAABB(aabb); OnElementInvalidated(this);