Add DebugDraw::DrawText for drawing 2D debug text on a screen

This commit is contained in:
Wojtek Figat
2021-02-18 12:39:53 +01:00
parent 161917977c
commit 74823a8bcf
3 changed files with 80 additions and 5 deletions

View File

@@ -97,6 +97,8 @@ bool DeployDataStep::Perform(CookingData& data)
data.AddRootEngineAsset(PRE_INTEGRATED_GF_ASSET_NAME);
data.AddRootEngineAsset(SMAA_AREA_TEX);
data.AddRootEngineAsset(SMAA_SEARCH_TEX);
if (data.Configuration != BuildConfiguration::Release)
data.AddRootEngineAsset(TEXT("Editor/Fonts/Roboto-Regular"));
// Register game assets
data.StepProgress(TEXT("Deploying game data"), 50);

View File

@@ -20,6 +20,8 @@
#include "Engine/Animations/AnimationUtils.h"
#include "Engine/Profiler/Profiler.h"
#include "Engine/Debug/DebugLog.h"
#include "Engine/Render2D/Render2D.h"
#include "Engine/Render2D/FontAsset.h"
// Debug draw service configuration
#define DEBUG_DRAW_INITIAL_VB_CAPACITY (4 * 1024)
@@ -54,6 +56,15 @@ struct DebugTriangle
float TimeLeft;
};
struct DebugText2D
{
Array<Char, InlinedAllocation<64>> Text;
Vector2 Position;
int32 Size;
Color Color;
float TimeLeft;
};
PACK_STRUCT(struct Vertex {
Vector3 Position;
Color32 Color;
@@ -133,10 +144,12 @@ struct DebugDrawData
Array<DebugTriangle> OneFrameTriangles;
Array<DebugTriangle> DefaultWireTriangles;
Array<DebugTriangle> OneFrameWireTriangles;
Array<DebugText2D> DefaultText2D;
Array<DebugText2D> OneFrameText2D;
inline int32 Count() const
{
return LinesCount() + TrianglesCount();
return LinesCount() + TrianglesCount() + Text2DCount();
}
inline int32 LinesCount() const
@@ -149,6 +162,11 @@ struct DebugDrawData
return DefaultTriangles.Count() + OneFrameTriangles.Count() + DefaultWireTriangles.Count() + OneFrameWireTriangles.Count();
}
inline int32 Text2DCount() const
{
return DefaultText2D.Count() + OneFrameText2D.Count();
}
inline void Add(const DebugLine& l)
{
if (l.TimeLeft > 0)
@@ -178,10 +196,12 @@ struct DebugDrawData
UpdateList(deltaTime, DefaultLines);
UpdateList(deltaTime, DefaultTriangles);
UpdateList(deltaTime, DefaultWireTriangles);
UpdateList(deltaTime, DefaultText2D);
OneFrameLines.Clear();
OneFrameTriangles.Clear();
OneFrameWireTriangles.Clear();
OneFrameText2D.Clear();
}
inline void Clear()
@@ -192,6 +212,8 @@ struct DebugDrawData
OneFrameTriangles.Clear();
DefaultWireTriangles.Clear();
OneFrameWireTriangles.Clear();
DefaultText2D.Clear();
OneFrameText2D.Clear();
}
inline void Release()
@@ -202,6 +224,8 @@ struct DebugDrawData
OneFrameTriangles.Resize(0);
DefaultWireTriangles.Resize(0);
OneFrameWireTriangles.Resize(0);
DefaultText2D.Resize(0);
OneFrameText2D.Resize(0);
}
};
@@ -216,6 +240,7 @@ namespace
DebugDrawContext GlobalContext;
DebugDrawContext* Context;
AssetReference<Shader> DebugDrawShader;
AssetReference<FontAsset> DebugDrawFont;
PsData DebugDrawPsLinesDefault;
PsData DebugDrawPsLinesDepthTest;
PsData DebugDrawPsWireTrianglesDefault;
@@ -601,8 +626,6 @@ void DebugDraw::Draw(RenderContext& renderContext, GPUTextureView* target, GPUTe
context->BindCB(0, cb);
auto vb = DebugDrawVB->GetBuffer();
#define DRAW(drawCall) if (drawCall.VertexCount)
// Draw with depth test
if (depthTestLines.VertexCount + depthTestTriangles.VertexCount + depthTestWireTriangles.VertexCount > 0)
{
@@ -673,7 +696,30 @@ void DebugDraw::Draw(RenderContext& renderContext, GPUTextureView* target, GPUTe
}
}
#undef DRAW
// Text 2D
if (Context->DebugDrawDefault.TrianglesCount())
{
PROFILE_GPU_CPU_NAMED("2D");
if (!DebugDrawFont)
{
DebugDrawFont = Content::LoadAsyncInternal<FontAsset>(TEXT("Editor/Fonts/Roboto-Regular"));
}
if (DebugDrawFont && DebugDrawFont->IsLoaded())
{
Viewport viewport = renderContext.Task->GetViewport();
Render2D::Begin(context, target, nullptr, viewport);
for (auto& t : Context->DebugDrawDefault.DefaultText2D)
{
}
for (auto& t : Context->DebugDrawDefault.OneFrameText2D)
{
const StringView text(t.Text.Get(), t.Text.Count() - 1);
Render2D::DrawText(DebugDrawFont->CreateFont(t.Size), text, t.Color, t.Position);
}
Render2D::End();
}
}
}
void DebugDraw::DrawActors(Actor** selectedActors, int32 selectedActorsCount, bool drawScenes)
@@ -1276,4 +1322,19 @@ void DebugDraw::DrawBox(const OrientedBoundingBox& box, const Color& color, floa
}
}
void DebugDraw::DrawText(const StringView& text, const Vector2& position, const Color& color, int32 size, float duration)
{
if (text.Length() == 0 || size < 4)
return;
Array<DebugText2D>* list = duration > 0 ? &Context->DebugDrawDefault.DefaultText2D : &Context->DebugDrawDefault.OneFrameText2D;
auto& t = list->AddOne();
t.Text.Resize(text.Length() + 1);
Platform::MemoryCopy(t.Text.Get(), text.Get(), text.Length() * sizeof(Char));
t.Text[text.Length()] = 0;
t.Position = position;
t.Size = size;
t.Color = color;
t.TimeLeft = duration;
}
#endif

View File

@@ -307,6 +307,16 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(DebugDraw);
/// <param name="duration">The duration (in seconds). Use 0 to draw it only once.</param>
/// <param name="depthTest">If set to <c>true</c> depth test will be performed, otherwise depth will be ignored.</param>
API_FUNCTION() static void DrawBox(const OrientedBoundingBox& box, const Color& color, float duration = 0.0f, bool depthTest = true);
/// <summary>
/// Draws the text on a screen (2D).
/// </summary>
/// <param name="text">The text.</param>
/// <param name="position">The position of the text on the screen (in screen-space coordinates).</param>
/// <param name="color">The color.</param>
/// <param name="size">The font size.</param>
/// <param name="duration">The duration (in seconds). Use 0 to draw it only once.</param>
API_FUNCTION() static void DrawText(const StringView& text, const Vector2& position, const Color& color, int32 size = 20, float duration = 0.0f);
};
#define DEBUG_DRAW_LINE(start, end, color, duration, depthTest) DebugDraw::DrawLine(start, end, color, duration, depthTest)
@@ -326,7 +336,8 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(DebugDraw);
#define DEBUG_DRAW_WIRE_SPHERE(sphere, color, duration, depthTest) DebugDraw::DrawWireSphere(sphere, color, duration, depthTest)
#define DEBUG_DRAW_WIRE_TUBE(position, orientation, radius, length, color, duration, depthTest) DebugDraw::DrawWireTube(position, orientation, radius, length, color, duration, depthTest)
#define DEBUG_DRAW_WIRE_CYLINDER(position, orientation, radius, height, color, duration, depthTest) DebugDraw::DrawWireTube(position, orientation, radius, height, color, duration, depthTest)
#define DEBUG_DRAW_WIRE_ARROW(position, orientation, scale, color, duration, depthTest) DebugDraw::DrawWireTube(position, orientation, scale, color, duration, depthTest)
#define DEBUG_DRAW_WIRE_ARROW(position, orientation, scale, color, duration, depthTest) DebugDraw::DrawWireArrow(position, orientation, scale, color, duration, depthTest)
#define DEBUG_DRAW_TEXT(text, position, color, size, duration) DebugDraw::DrawText(text, position, color, size, duration)
#else
@@ -348,5 +359,6 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(DebugDraw);
#define DEBUG_DRAW_WIRE_TUBE(position, orientation, radius, length, color, duration, depthTest)
#define DEBUG_DRAW_WIRE_CYLINDER(position, orientation, radius, height, color, duration, depthTest)
#define DEBUG_DRAW_WIRE_ARROW(position, orientation, scale, color, duration, depthTest)
#define DEBUG_DRAW_TEXT(text, position, color, size, duration)
#endif