From 24f3dfe2649e671fc44680a909d95ed7f767c883 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 31 Jan 2025 21:01:26 +0100 Subject: [PATCH] Fix selected spline points to be drawn relative to the view for greater readability #2595 --- Source/Engine/Debug/DebugDraw.cpp | 5 +++++ Source/Engine/Debug/DebugDraw.h | 7 +++++-- Source/Engine/Level/Actors/Spline.cpp | 15 +++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Source/Engine/Debug/DebugDraw.cpp b/Source/Engine/Debug/DebugDraw.cpp index 447e6cabc..d19737dbb 100644 --- a/Source/Engine/Debug/DebugDraw.cpp +++ b/Source/Engine/Debug/DebugDraw.cpp @@ -741,6 +741,11 @@ void DebugDraw::SetContext(void* context) #endif +Vector3 DebugDraw::GetViewPos() +{ + return Context->LastViewPos; +} + void DebugDraw::Draw(RenderContext& renderContext, GPUTextureView* target, GPUTextureView* depthBuffer, bool enableDepthTest) { PROFILE_GPU_CPU("Debug Draw"); diff --git a/Source/Engine/Debug/DebugDraw.h b/Source/Engine/Debug/DebugDraw.h index 6cfceca6d..e9d596aa1 100644 --- a/Source/Engine/Debug/DebugDraw.h +++ b/Source/Engine/Debug/DebugDraw.h @@ -39,7 +39,7 @@ API_CLASS(Static) class FLAXENGINE_API DebugDraw #if USE_EDITOR /// - /// Allocates the context for Debug Drawing. Can be use to redirect debug shapes collecting to a separate container (instead of global state). + /// Allocates the context for Debug Drawing. Can be used to redirect debug shapes collecting to a separate container (instead of global state). /// /// The context object. Release it wil FreeContext. Returns null if failed. API_FUNCTION() static void* AllocateContext(); @@ -64,6 +64,9 @@ API_CLASS(Static) class FLAXENGINE_API DebugDraw API_FUNCTION() static void SetContext(void* context); #endif + // Gets the last view position when rendering the current context. Can be sued for custom culling or LODing when drawing more complex shapes. + static Vector3 GetViewPos(); + /// /// Draws the collected debug shapes to the output. /// @@ -717,7 +720,7 @@ API_CLASS(Static) class FLAXENGINE_API DebugDraw API_FUNCTION() static void Clear(void* context = nullptr); }; -#define DEBUG_DRAW_AXIS_FROM_DIRECTION(origin, direction, size, duration, depthTest) DebugDraw::DrawAxisFromDirection(origin, direction, size, duration, depthTest); +#define DEBUG_DRAW_AXIS_FROM_DIRECTION(origin, direction, size, duration, depthTest) DebugDraw::DrawAxisFromDirection(origin, direction, size, duration, depthTest); #define DEBUG_DRAW_DIRECTION(origin, direction, color, duration, depthTest) DebugDraw::DrawDirection(origin, direction, color, duration, depthTest); #define DEBUG_DRAW_RAY(origin, direction, color, length, duration, depthTest) DebugDraw::DrawRay(origin, direction, color, length, duration, depthTest); #define DEBUG_DRAW_RAY(ray, color, length, duration, depthTest) DebugDraw::DrawRay(ray, color, length, duration, depthTest); diff --git a/Source/Engine/Level/Actors/Spline.cpp b/Source/Engine/Level/Actors/Spline.cpp index 37842a89d..22d13b6e5 100644 --- a/Source/Engine/Level/Actors/Spline.cpp +++ b/Source/Engine/Level/Actors/Spline.cpp @@ -490,19 +490,26 @@ void Spline::SetKeyframes(MArray* data, int32 keySize) namespace { - void DrawSpline(Spline* spline, const Color& color, const Transform& transform, bool depthTest) + FORCE_INLINE float NodeSizeByDistance(const Vector3& nodePosition, bool scaleByDistance) + { + if (scaleByDistance) + return (float)(Vector3::Distance(DebugDraw::GetViewPos(), nodePosition) / 100); + return 5.0f; + } + + void DrawSpline(Spline* spline, const Color& color, const Transform& transform, bool depthTest, bool scaleByDistance = false) { const int32 count = spline->Curve.GetKeyframes().Count(); if (count == 0) return; Spline::Keyframe* prev = spline->Curve.GetKeyframes().Get(); Vector3 prevPos = transform.LocalToWorld(prev->Value.Translation); - DEBUG_DRAW_WIRE_SPHERE(BoundingSphere(prevPos, 5.0f), color, 0.0f, depthTest); + DEBUG_DRAW_WIRE_SPHERE(BoundingSphere(prevPos, NodeSizeByDistance(prevPos, scaleByDistance)), color, 0.0f, depthTest); for (int32 i = 1; i < count; i++) { Spline::Keyframe* next = prev + 1; Vector3 nextPos = transform.LocalToWorld(next->Value.Translation); - DEBUG_DRAW_WIRE_SPHERE(BoundingSphere(nextPos, 5.0f), color, 0.0f, depthTest); + DEBUG_DRAW_WIRE_SPHERE(BoundingSphere(nextPos, NodeSizeByDistance(nextPos, scaleByDistance)), color, 0.0f, depthTest); const float d = (next->Time - prev->Time) / 3.0f; DEBUG_DRAW_BEZIER(prevPos, prevPos + prev->TangentOut.Translation * d, nextPos + next->TangentIn.Translation * d, nextPos, color, 0.0f, depthTest); prev = next; @@ -521,7 +528,7 @@ void Spline::OnDebugDraw() void Spline::OnDebugDrawSelected() { - DrawSpline(this, Color::White, _transform, false); + DrawSpline(this, Color::White, _transform, false, true); // Base Actor::OnDebugDrawSelected();