From 6f8e18f5b300c79a104d551ea05e29281a6d7ef8 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 14 Jul 2021 14:51:50 +0200 Subject: [PATCH] Add faked lighting to improve Debug Draw solid shapes rendering readability --- Content/Shaders/DebugDraw.flax | 4 ++-- Source/Engine/Debug/DebugDraw.cpp | 29 +++++++++++++++++++++++++++++ Source/Engine/Debug/DebugDraw.h | 20 ++++++++++++++++++++ Source/Shaders/DebugDraw.shader | 13 +++++++++++-- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/Content/Shaders/DebugDraw.flax b/Content/Shaders/DebugDraw.flax index 6e2db8bf9..ed6621345 100644 --- a/Content/Shaders/DebugDraw.flax +++ b/Content/Shaders/DebugDraw.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:17006324e818ddff6880b7c6b23a49fec90111c185a6355153ae79ab4b244807 -size 1746 +oid sha256:84dbec978e3a2cfecc5e50df4d85b168a9f1ec7f215a03e9b4773507c3099d59 +size 2115 diff --git a/Source/Engine/Debug/DebugDraw.cpp b/Source/Engine/Debug/DebugDraw.cpp index f4e2097a1..401ad0959 100644 --- a/Source/Engine/Debug/DebugDraw.cpp +++ b/Source/Engine/Debug/DebugDraw.cpp @@ -1184,11 +1184,40 @@ void DebugDraw::DrawTriangles(const Span& vertices, const Color& color, } } +void DebugDraw::DrawTriangles(const Span& vertices, const Matrix& transform, const Color& color, float duration, bool depthTest) +{ + CHECK(vertices.Length() % 3 == 0); + + DebugTriangle t; + t.Color = Color32(color); + t.TimeLeft = duration; + + Array* list; + if (depthTest) + list = duration > 0 ? &Context->DebugDrawDepthTest.DefaultTriangles : &Context->DebugDrawDepthTest.OneFrameTriangles; + else + list = duration > 0 ? &Context->DebugDrawDefault.DefaultTriangles : &Context->DebugDrawDefault.OneFrameTriangles; + list->EnsureCapacity(list->Count() + vertices.Length() / 3); + + for (int32 i = 0; i < vertices.Length();) + { + Vector3::Transform(vertices[i++], transform, t.V0); + Vector3::Transform(vertices[i++], transform, t.V1); + Vector3::Transform(vertices[i++], transform, t.V2); + list->Add(t); + } +} + void DebugDraw::DrawTriangles(const Array& vertices, const Color& color, float duration, bool depthTest) { DrawTriangles(Span(vertices.Get(), vertices.Count()), color, duration, depthTest); } +void DebugDraw::DrawTriangles(const Array& vertices, const Matrix& transform, const Color& color, float duration, bool depthTest) +{ + DrawTriangles(Span(vertices.Get(), vertices.Count()), transform, color, duration, depthTest); +} + void DebugDraw::DrawTriangles(const Span& vertices, const Span& indices, const Color& color, float duration, bool depthTest) { CHECK(indices.Length() % 3 == 0); diff --git a/Source/Engine/Debug/DebugDraw.h b/Source/Engine/Debug/DebugDraw.h index 520b688ec..b8a073ec6 100644 --- a/Source/Engine/Debug/DebugDraw.h +++ b/Source/Engine/Debug/DebugDraw.h @@ -154,6 +154,16 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(DebugDraw); /// If set to true depth test will be performed, otherwise depth will be ignored. API_FUNCTION() static void DrawTriangles(const Span& vertices, const Color& color, float duration = 0.0f, bool depthTest = true); + /// + /// Draws the triangles. + /// + /// The triangle vertices list (must have multiple of 3 elements). + /// The custom matrix used to transform all line vertices. + /// The color. + /// The duration (in seconds). Use 0 to draw it only once. + /// If set to true depth test will be performed, otherwise depth will be ignored. + API_FUNCTION() static void DrawTriangles(const Span& vertices, const Matrix& transform, const Color& color, float duration = 0.0f, bool depthTest = true); + /// /// Draws the triangles. /// @@ -163,6 +173,16 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(DebugDraw); /// If set to true depth test will be performed, otherwise depth will be ignored. static void DrawTriangles(const Array& vertices, const Color& color, float duration = 0.0f, bool depthTest = true); + /// + /// Draws the triangles. + /// + /// The triangle vertices list (must have multiple of 3 elements). + /// The custom matrix used to transform all line vertices. + /// The color. + /// The duration (in seconds). Use 0 to draw it only once. + /// If set to true depth test will be performed, otherwise depth will be ignored. + static void DrawTriangles(const Array& vertices, const Matrix& transform, const Color& color, float duration = 0.0f, bool depthTest = true); + /// /// Draws the triangles using the given index buffer. /// diff --git a/Source/Shaders/DebugDraw.shader b/Source/Shaders/DebugDraw.shader index 0b6c55450..5e9992ce7 100644 --- a/Source/Shaders/DebugDraw.shader +++ b/Source/Shaders/DebugDraw.shader @@ -39,15 +39,24 @@ void PerformDepthTest(float4 svPosition) } } +float4 PerformFakeLighting(float4 svPosition, float4 c) +{ + // Reconstruct view normal and calculate faked lighting + float depth = svPosition.z * 10000; + float3 n = normalize(float3(-ddx(depth), -ddy(depth), 1.0f)); + c.rgb *= saturate(abs(dot(n, float3(0, 1, 0))) + 0.5f); + return c; +} + META_PS(true, FEATURE_LEVEL_ES2) float4 PS(VS2PS input) : SV_Target { - return input.Color; + return PerformFakeLighting(input.Position, input.Color); } META_PS(true, FEATURE_LEVEL_ES2) float4 PS_DepthTest(VS2PS input) : SV_Target { PerformDepthTest(input.Position); - return input.Color; + return PerformFakeLighting(input.Position, input.Color); }