From b1f867f1072d68978e0de3a006987084cf7822c0 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Perrier Date: Mon, 15 Feb 2021 19:33:25 +0100 Subject: [PATCH] Add DrawVertices & FillTriangle funcs. --- Source/Engine/Render2D/Render2D.cpp | 38 +++++++++++++++++++++++++++++ Source/Engine/Render2D/Render2D.h | 26 ++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/Source/Engine/Render2D/Render2D.cpp b/Source/Engine/Render2D/Render2D.cpp index e834c5939..818265551 100644 --- a/Source/Engine/Render2D/Render2D.cpp +++ b/Source/Engine/Render2D/Render2D.cpp @@ -1744,3 +1744,41 @@ void Render2D::DrawBlur(const Rectangle& rect, float blurStrength) drawCall.AsBlur.BottomRightY = p.Y; WriteRect(rect, Color::White); } + +void Render2D::DrawVertices(GPUTexture* t, const Array vertices, const Array uvs) +{ + RENDER2D_CHECK_RENDERING_STATE; + + Render2DDrawCall& drawCall = DrawCalls.AddOne(); + drawCall.Type = DrawCallType::FillTexture; + drawCall.StartIB = IBIndex; + drawCall.CountIB = vertices.Count(); + drawCall.AsTexture.Ptr = t; + + for (int32 i = 0; i < vertices.Count(); i += 3) + WriteTri(vertices[i], vertices[i + 1], vertices[i + 2], uvs[i], uvs[i + 1], uvs[i + 2]); +} + +void Render2D::DrawVertices(const Array vertices, const Array colors, bool useAlpha) +{ + RENDER2D_CHECK_RENDERING_STATE; + + Render2DDrawCall& drawCall = DrawCalls.AddOne(); + drawCall.Type = useAlpha ? DrawCallType::FillRect : DrawCallType::FillRectNoAlpha; + drawCall.StartIB = IBIndex; + drawCall.CountIB = vertices.Count(); + + for (int32 i = 0; i < vertices.Count(); i += 3) + WriteTri(vertices[i], vertices[i + 1], vertices[i + 2], colors[i], colors[i + 1], colors[i + 2]); +} + +void Render2D::FillTriangle(const Vector2& p0, const Vector2& p1, const Vector2& p2, const Color& color) +{ + RENDER2D_CHECK_RENDERING_STATE; + + Render2DDrawCall& drawCall = DrawCalls.AddOne(); + drawCall.Type = color.A < 1.0f ? DrawCallType::FillRect : DrawCallType::FillRectNoAlpha; + drawCall.StartIB = IBIndex; + drawCall.CountIB = 3; + WriteTri(p0, p1, p2, color, color, color); +} diff --git a/Source/Engine/Render2D/Render2D.h b/Source/Engine/Render2D/Render2D.h index a421c56a6..1cf32a886 100644 --- a/Source/Engine/Render2D/Render2D.h +++ b/Source/Engine/Render2D/Render2D.h @@ -4,6 +4,7 @@ #include "Engine/Core/Math/Color.h" #include "Engine/Scripting/ScriptingType.h" +#include "Engine/Core/Collections/Array.h" struct SpriteHandle; struct TextLayoutOptions; @@ -339,4 +340,29 @@ public: /// The target rectangle to draw (blurs its background). /// The blur strength defines how blurry the background is. Larger numbers increase blur, resulting in a larger runtime cost on the GPU. API_FUNCTION() static void DrawBlur(const Rectangle& rect, float blurStrength); + + /// + /// Draws vertices array. + /// + /// The texture. + /// The vertices array. + /// The uvs array. + API_FUNCTION() static void DrawVertices(GPUTexture* t, Array vertices, Array uvs); + + /// + /// Draws vertices array. + /// + /// The vertices array. + /// The colors array. + /// If true alpha blending will be enabled. + API_FUNCTION() static void DrawVertices(Array vertices, Array colors, bool useAlpha); + + /// + /// Fills a triangular area. + /// + /// The first point. + /// The second point. + /// The third point. + /// The color. + API_FUNCTION() static void FillTriangle(const Vector2& p0, const Vector2& p1, const Vector2& p2, const Color& color); };