From afc4158dd3c60f1ca4f83fe5720174b3edb19b4d Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 24 Feb 2025 14:19:51 +0100 Subject: [PATCH] Fix sprite and texture 9-slice rendering brush to properly calculate border size --- Source/Engine/Render2D/Render2D.cpp | 18 +++++++++--------- Source/Engine/UI/GUI/Brushes/SpriteBrush.cs | 4 +++- Source/Engine/UI/GUI/Brushes/TextureBrush.cs | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Source/Engine/Render2D/Render2D.cpp b/Source/Engine/Render2D/Render2D.cpp index a51d16ecc..f709ed420 100644 --- a/Source/Engine/Render2D/Render2D.cpp +++ b/Source/Engine/Render2D/Render2D.cpp @@ -363,17 +363,17 @@ void Write9SlicingRect(const Rectangle& rect, const Color& color, const Float4& const Float2 bottomLeftUV(borderUVs.X, 1.0f - borderUVs.W); const Float2 bottomRightUV(1.0f - borderUVs.Y, 1.0f - borderUVs.W); - WriteRect(upperLeft, color, Float2::Zero, upperLeftUV); - WriteRect(upperRight, color, Float2(upperRightUV.X, 0), Float2(1, upperLeftUV.Y)); - WriteRect(bottomLeft, color, Float2(0, bottomLeftUV.Y), Float2(bottomLeftUV.X, 1)); - WriteRect(bottomRight, color, bottomRightUV, Float2::One); + WriteRect(upperLeft, color, Float2::Zero, upperLeftUV); // Upper left corner + WriteRect(upperRight, color, Float2(upperRightUV.X, 0), Float2(1, upperLeftUV.Y)); // Upper right corner + WriteRect(bottomLeft, color, Float2(0, bottomLeftUV.Y), Float2(bottomLeftUV.X, 1)); // Bottom left corner + WriteRect(bottomRight, color, bottomRightUV, Float2::One); // Bottom right corner - WriteRect(Rectangle(upperLeft.GetUpperRight(), upperRight.GetBottomLeft() - upperLeft.GetUpperRight()), color, Float2(upperLeftUV.X, 0), upperRightUV); - WriteRect(Rectangle(upperLeft.GetBottomLeft(), bottomLeft.GetUpperRight() - upperLeft.GetBottomLeft()), color, Float2(0, upperLeftUV.Y), bottomLeftUV); - WriteRect(Rectangle(bottomLeft.GetUpperRight(), bottomRight.GetBottomLeft() - bottomLeft.GetUpperRight()), color, bottomLeftUV, Float2(bottomRightUV.X, 1)); - WriteRect(Rectangle(upperRight.GetBottomLeft(), bottomRight.GetUpperRight() - upperRight.GetBottomLeft()), color, upperRightUV, Float2(1, bottomRightUV.Y)); + WriteRect(Rectangle(upperLeft.GetUpperRight(), upperRight.GetBottomLeft() - upperLeft.GetUpperRight()), color, Float2(upperLeftUV.X, 0), upperRightUV); // Top side + WriteRect(Rectangle(upperLeft.GetBottomLeft(), bottomLeft.GetUpperRight() - upperLeft.GetBottomLeft()), color, Float2(0, upperLeftUV.Y), bottomLeftUV); // Left side + WriteRect(Rectangle(bottomLeft.GetUpperRight(), bottomRight.GetBottomLeft() - bottomLeft.GetUpperRight()), color, bottomLeftUV, Float2(bottomRightUV.X, 1)); // Bottom side + WriteRect(Rectangle(upperRight.GetBottomLeft(), bottomRight.GetUpperRight() - upperRight.GetBottomLeft()), color, upperRightUV, Float2(1, bottomRightUV.Y)); // Right Side - WriteRect(Rectangle(upperLeft.GetBottomRight(), bottomRight.GetUpperLeft() - upperLeft.GetBottomRight()), color, upperRightUV, bottomRightUV); + WriteRect(Rectangle(upperLeft.GetBottomRight(), bottomRight.GetUpperLeft() - upperLeft.GetBottomRight()), color, upperLeftUV, bottomRightUV); // Center } void Write9SlicingRect(const Rectangle& rect, const Color& color, const Float4& border, const Float4& borderUVs, const Float2& uvLocation, const Float2& uvSize) diff --git a/Source/Engine/UI/GUI/Brushes/SpriteBrush.cs b/Source/Engine/UI/GUI/Brushes/SpriteBrush.cs index 5be953f29..e1f8676bc 100644 --- a/Source/Engine/UI/GUI/Brushes/SpriteBrush.cs +++ b/Source/Engine/UI/GUI/Brushes/SpriteBrush.cs @@ -100,9 +100,11 @@ namespace FlaxEngine.GUI /// public unsafe void Draw(Rectangle rect, Color color) { + if (!Sprite.IsValid) + return; var border = Border; var borderUV = *(Float4*)&border; - var borderSize = borderUV * new Float4(BorderSize, BorderSize, BorderSize, BorderSize); + var borderSize = new Float4(BorderSize, BorderSize, BorderSize, BorderSize); if (Filter == BrushFilter.Point) Render2D.Draw9SlicingSpritePoint(Sprite, rect, borderSize, borderUV, color); else diff --git a/Source/Engine/UI/GUI/Brushes/TextureBrush.cs b/Source/Engine/UI/GUI/Brushes/TextureBrush.cs index 98e7e5324..8c7618135 100644 --- a/Source/Engine/UI/GUI/Brushes/TextureBrush.cs +++ b/Source/Engine/UI/GUI/Brushes/TextureBrush.cs @@ -113,7 +113,7 @@ namespace FlaxEngine.GUI return; var border = Border; var borderUV = *(Float4*)&border; - var borderSize = borderUV * new Float4(BorderSize, BorderSize, BorderSize, BorderSize); + var borderSize = new Float4(BorderSize, BorderSize, BorderSize, BorderSize); if (Filter == BrushFilter.Point) Render2D.Draw9SlicingTexturePoint(Texture, rect, borderSize, borderUV, color); else