Add faked lighting to improve Debug Draw solid shapes rendering readability

This commit is contained in:
Wojtek Figat
2021-07-14 14:51:50 +02:00
parent 42f05a5072
commit 6f8e18f5b3
4 changed files with 62 additions and 4 deletions

BIN
Content/Shaders/DebugDraw.flax (Stored with Git LFS)

Binary file not shown.

View File

@@ -1184,11 +1184,40 @@ void DebugDraw::DrawTriangles(const Span<Vector3>& vertices, const Color& color,
}
}
void DebugDraw::DrawTriangles(const Span<Vector3>& 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<DebugTriangle>* 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<Vector3>& vertices, const Color& color, float duration, bool depthTest)
{
DrawTriangles(Span<Vector3>(vertices.Get(), vertices.Count()), color, duration, depthTest);
}
void DebugDraw::DrawTriangles(const Array<Vector3>& vertices, const Matrix& transform, const Color& color, float duration, bool depthTest)
{
DrawTriangles(Span<Vector3>(vertices.Get(), vertices.Count()), transform, color, duration, depthTest);
}
void DebugDraw::DrawTriangles(const Span<Vector3>& vertices, const Span<int32>& indices, const Color& color, float duration, bool depthTest)
{
CHECK(indices.Length() % 3 == 0);

View File

@@ -154,6 +154,16 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(DebugDraw);
/// <param name="depthTest">If set to <c>true</c> depth test will be performed, otherwise depth will be ignored.</param>
API_FUNCTION() static void DrawTriangles(const Span<Vector3>& vertices, const Color& color, float duration = 0.0f, bool depthTest = true);
/// <summary>
/// Draws the triangles.
/// </summary>
/// <param name="vertices">The triangle vertices list (must have multiple of 3 elements).</param>
/// <param name="transform">The custom matrix used to transform all line vertices.</param>
/// <param name="color">The color.</param>
/// <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 DrawTriangles(const Span<Vector3>& vertices, const Matrix& transform, const Color& color, float duration = 0.0f, bool depthTest = true);
/// <summary>
/// Draws the triangles.
/// </summary>
@@ -163,6 +173,16 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(DebugDraw);
/// <param name="depthTest">If set to <c>true</c> depth test will be performed, otherwise depth will be ignored.</param>
static void DrawTriangles(const Array<Vector3, HeapAllocation>& vertices, const Color& color, float duration = 0.0f, bool depthTest = true);
/// <summary>
/// Draws the triangles.
/// </summary>
/// <param name="vertices">The triangle vertices list (must have multiple of 3 elements).</param>
/// <param name="transform">The custom matrix used to transform all line vertices.</param>
/// <param name="color">The color.</param>
/// <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>
static void DrawTriangles(const Array<Vector3, HeapAllocation>& vertices, const Matrix& transform, const Color& color, float duration = 0.0f, bool depthTest = true);
/// <summary>
/// Draws the triangles using the given index buffer.
/// </summary>

View File

@@ -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);
}