Skip to content

Commit

Permalink
Circle::drawArc(LineStyle::RoundCap) で両端がグラデーションしない問題を修正 (#1198)
Browse files Browse the repository at this point in the history
  • Loading branch information
Raclamusi authored Feb 2, 2024
1 parent 0c1488b commit e786b24
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
63 changes: 60 additions & 3 deletions Siv3D/src/Siv3D/Renderer2D/Vertex2DBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,63 @@ namespace s3d
return indexSize;
}

Vertex2D::IndexType BuildCircleArcRoundCap(const BufferCreatorFunc& bufferCreator, const Float2& center, float r, float startAngle, const Float4& startColor, const Float4& endColor, float scale)
{
const Vertex2D::IndexType quality = detail::CalculateCirclePieQuality(r * scale, Math::PiF);
const Vertex2D::IndexType vertexSize = quality, indexSize = ((quality - 2) * 3);
auto [pVertex, pIndex, indexOffset] = bufferCreator(vertexSize, indexSize);

if (not pVertex)
{
return 0;
}

const float centerX = center.x;
const float centerY = center.y;
const float radDelta = (Math::PiF / (quality - 1));

//
{
Vertex2D* pDst = pVertex;

for (Vertex2D::IndexType i = 0; i < quality; ++i)
{
const float rad = (startAngle + (radDelta * i));
const auto [s, c] = FastMath::SinCos(rad);
(pDst++)->pos.set(centerX + r * s, centerY - r * c);
}
}

{
const size_t midIndex = (vertexSize >> 1);

pVertex[0].color = startColor;
pVertex[vertexSize - 1].color = endColor;

for (size_t i = 1; i < midIndex; ++i)
{
const float rad = (radDelta * i);
const auto f = (std::cos(rad) * 0.5f + 0.5f);
pVertex[i].color = endColor.lerp(startColor, f);
pVertex[vertexSize - i - 1].color = startColor.lerp(endColor, f);
}

if (vertexSize & 1)
{
pVertex[midIndex].color = endColor.lerp(startColor, 0.5f);
}
}

for (Vertex2D::IndexType i = 0; i < (quality - 2); ++i)
{
*pIndex++ = indexOffset;
*pIndex++ = (indexOffset + i + 1);
*pIndex++ = (indexOffset + i + 2);
}

return indexSize;
}

Vertex2D::IndexType BuildCircleArc(const BufferCreatorFunc& bufferCreator, const LineStyle& style, const Float2& center, const float rInner, const float startAngle, const float _angle, const float thickness, const Float4& innerColor, const Float4& outerColor, const float scale)
{
if (style.hasRoundCap())
Expand All @@ -680,8 +737,8 @@ namespace s3d
const Float2 startPos = OffsetCircularF(center, (rInner + halfThickness), startAngle).fastToFloat2();
const Float2 endPos = OffsetCircularF(center, (rInner + halfThickness), startAngle + _angle).fastToFloat2();

indexCount += BuildCirclePie(bufferCreator, startPos, halfThickness, (startAngle + Math::PiF), Math::PiF, outerColor, outerColor, scale);
indexCount += BuildCirclePie(bufferCreator, endPos, halfThickness, (startAngle + _angle), Math::PiF, outerColor, outerColor, scale);
indexCount += BuildCircleArcRoundCap(bufferCreator, startPos, halfThickness, (startAngle + Math::PiF), innerColor, outerColor, scale);
indexCount += BuildCircleArcRoundCap(bufferCreator, endPos, halfThickness, (startAngle + _angle), outerColor, innerColor, scale);

return indexCount;
}
Expand Down Expand Up @@ -727,8 +784,8 @@ namespace s3d

for (size_t i = 0; i < vertexSize / 2; ++i)
{
(pVertex++)->color = outerColor;
(pVertex++)->color = innerColor;
(pVertex++)->color = outerColor;
}

for (Vertex2D::IndexType i = 0; i < (quality - 1); ++i)
Expand Down
3 changes: 3 additions & 0 deletions Siv3D/src/Siv3D/Renderer2D/Vertex2DBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ namespace s3d
[[nodiscard]]
Vertex2D::IndexType BuildCirclePie(const BufferCreatorFunc& bufferCreator, const Float2& center, float r, float startAngle, float angle, const Float4& innerColor, const Float4& outerColor, float scale);

[[nodiscard]]
Vertex2D::IndexType BuildCircleArcRoundCap(const BufferCreatorFunc& bufferCreator, const Float2& center, float r, float startAngle, const Float4& startColor, const Float4& endColor, float scale);

[[nodiscard]]
Vertex2D::IndexType BuildCircleArc(const BufferCreatorFunc& bufferCreator, const LineStyle& style, const Float2& center, float rInner, float startAngle, float angle, float thickness, const Float4& innerColor, const Float4& outerColor, float scale);

Expand Down

0 comments on commit e786b24

Please sign in to comment.