Graphics: Add origin to all sprite-based renderables (and made it a factor)

This commit is contained in:
SirLynix
2022-12-17 14:48:50 +01:00
parent 0537be3201
commit d4422c4102
12 changed files with 98 additions and 38 deletions

View File

@@ -18,7 +18,8 @@ namespace Nz
m_color(Color::White),
m_orientation(orientation),
m_textureCoords(0.f, 0.f, 1.f, 1.f),
m_size(64.f, 64.f)
m_origin(0.f, 0.f),
m_size(64.f)
{
UpdateVertices();
}
@@ -74,16 +75,20 @@ namespace Nz
void LinearSlicedSprite::UpdateVertices()
{
VertexStruct_XYZ_Color_UV* vertices = m_vertices.data();
float totalSectionSize = 0.f;
for (const Section& section : m_sections)
totalSectionSize += std::max(section.size, 0.f);
Vector3f origin = Vector3f::Zero();
Vector2f originShift = m_origin * ((m_orientation == Orientation::Horizontal) ? Vector2f(totalSectionSize, m_size) : Vector2f(m_size, totalSectionSize));
Vector3f topLeftCorner = -originShift;
Vector2f topLeftUV = m_textureCoords.GetCorner(RectCorner::LeftTop);
m_spriteCount = 0;
for (std::size_t i = 0; i < m_sectionCount; ++i)
VertexStruct_XYZ_Color_UV* vertices = m_vertices.data();
for (const Section& section : m_sections)
{
const auto& section = m_sections[i];
if (section.size <= 0.f)
continue;
@@ -93,37 +98,37 @@ namespace Nz
if (m_orientation == Orientation::Horizontal)
{
dir = Vector2(1.f, 0.f);
size = Vector2f(section.size, m_size.y);
size = Vector2f(section.size, m_size);
texCoords = Vector2f(section.textureCoord, m_textureCoords.height);
}
else
{
dir = Vector2(0.f, 1.f);
size = Vector2f(m_size.x, section.size);
size = Vector2f(m_size, section.size);
texCoords = Vector2f(m_textureCoords.width, section.textureCoord);
}
vertices->color = m_color;
vertices->position = origin;
vertices->position = topLeftCorner;
vertices->uv = topLeftUV;
vertices++;
vertices->color = m_color;
vertices->position = origin + size.x * Vector3f::Right();
vertices->position = topLeftCorner + size.x * Vector3f::Right();
vertices->uv = topLeftUV + Vector2f(texCoords.x, 0.f);
vertices++;
vertices->color = m_color;
vertices->position = origin + size.y * Vector3f::Up();
vertices->position = topLeftCorner + size.y * Vector3f::Up();
vertices->uv = topLeftUV + Vector2f(0.f, texCoords.y);
vertices++;
vertices->color = m_color;
vertices->position = origin + size.x * Vector3f::Right() + size.y * Vector3f::Up();
vertices->position = topLeftCorner + size.x * Vector3f::Right() + size.y * Vector3f::Up();
vertices->uv = topLeftUV + Vector2f(texCoords.x, texCoords.y);
vertices++;
origin += dir * section.size;
topLeftCorner += dir * section.size;
topLeftUV += dir * section.textureCoord;
m_spriteCount++;
}

View File

@@ -15,6 +15,7 @@ namespace Nz
m_material(std::move(material)),
m_color(Color::White),
m_textureCoords(0.f, 0.f, 1.f, 1.f),
m_origin(0.f, 0.f),
m_size(64.f, 64.f)
{
UpdateVertices();
@@ -98,7 +99,8 @@ namespace Nz
m_bottomRightCorner.textureCoords.y * m_textureCoords.height
};
Vector3f origin = Vector3f::Zero();
Vector3f originShift = m_origin * m_size;
Vector3f topLeftCorner = -originShift;
Vector2f topLeftUV = m_textureCoords.GetCorner(RectCorner::LeftTop);
m_spriteCount = 0;
@@ -113,36 +115,36 @@ namespace Nz
if (width > 0.f)
{
vertices->color = m_color;
vertices->position = origin;
vertices->position = topLeftCorner;
vertices->uv = topLeftUV;
vertices++;
vertices->color = m_color;
vertices->position = origin + width * Vector3f::Right();
vertices->position = topLeftCorner + width * Vector3f::Right();
vertices->uv = topLeftUV + Vector2f(texCoordsX[x], 0.f);
vertices++;
vertices->color = m_color;
vertices->position = origin + height * Vector3f::Up();
vertices->position = topLeftCorner + height * Vector3f::Up();
vertices->uv = topLeftUV + Vector2f(0.f, texCoordsY[y]);
vertices++;
vertices->color = m_color;
vertices->position = origin + width * Vector3f::Right() + height * Vector3f::Up();
vertices->position = topLeftCorner + width * Vector3f::Right() + height * Vector3f::Up();
vertices->uv = topLeftUV + Vector2f(texCoordsX[x], texCoordsY[y]);
vertices++;
origin.x += width;
topLeftCorner.x += width;
m_spriteCount++;
}
topLeftUV.x += texCoordsX[x];
}
origin.y += height;
topLeftCorner.y += height;
}
origin.x = 0;
topLeftCorner.x = -originShift.x;
topLeftUV.x = m_textureCoords.x;
topLeftUV.y += texCoordsY[y];

View File

@@ -16,8 +16,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_origin(0.f, 0.f, 0.f)
m_origin(0.f, 0.f),
m_size(64.f, 64.f)
{
m_cornerColor.fill(Color::White);

View File

@@ -27,6 +27,7 @@ namespace Nz
m_layers(materialCount),
m_mapSize(mapSize),
m_tileSize(tileSize),
m_origin(0.f, 0.f),
m_isometricModeEnabled(false),
m_shouldRebuildVertices(false)
{
@@ -120,9 +121,8 @@ namespace Nz
m_vertices.resize(spriteCount * 4);
VertexStruct_XYZ_Color_UV* vertexPtr = reinterpret_cast<VertexStruct_XYZ_Color_UV*>(m_vertices.data());
Vector3f originShift = m_origin * GetSize();
float topCorner = m_tileSize.y * (m_mapSize.y - 1);
Vector3f originShift = m_origin * GetSize();
for (const Layer& layer : m_layers)
{