From 31408657807f12b1fed4aa6062d1500f48885e65 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 6 Aug 2024 17:06:23 -0500 Subject: [PATCH] Add physics and lights debug drawing in prefabs when enabled. --- .../Editor/Viewport/PrefabWindowViewport.cs | 30 +++++++++++++++++-- Source/Engine/Debug/DebugDraw.cpp | 19 ++++++++++++ Source/Engine/Debug/DebugDraw.h | 19 +++++++++++- Source/Engine/Physics/Colliders/Collider.h | 8 ++--- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/Source/Editor/Viewport/PrefabWindowViewport.cs b/Source/Editor/Viewport/PrefabWindowViewport.cs index f51abb1fc..b9a654cf9 100644 --- a/Source/Editor/Viewport/PrefabWindowViewport.cs +++ b/Source/Editor/Viewport/PrefabWindowViewport.cs @@ -12,6 +12,7 @@ using FlaxEditor.Viewport.Previews; using FlaxEditor.Windows.Assets; using FlaxEngine; using FlaxEngine.GUI; +using Utils = FlaxEditor.Utilities.Utils; namespace FlaxEditor.Viewport { @@ -624,12 +625,37 @@ namespace FlaxEditor.Viewport } } - // Debug draw all actors in prefab + // Debug draw all actors in prefab and collect actors + List pActors = new List(); foreach (var child in SceneGraphRoot.ChildNodes) { if (child is not ActorNode actorNode) continue; - DebugDraw.DrawActorsTree(actorNode.Actor); + var actor = actorNode.Actor; + Utils.GetActorsTree(pActors, actor); + DebugDraw.DrawActorsTree(actor); + } + + // Draw physics debug + if ((Task.ViewFlags & ViewFlags.PhysicsDebug) != 0) + { + foreach (var actor in pActors) + { + if (actor is Collider c && c.IsActiveInHierarchy) + { + DebugDraw.DrawColliderDebugPhysics(c, renderContext.View); + } + } + } + + // Draw lights debug + if ((Task.ViewFlags & ViewFlags.LightsDebug) != 0) + { + foreach (var actor in pActors) + { + if (actor is Light l && l.IsActiveInHierarchy) + DebugDraw.DrawLightDebug(l, renderContext.View); + } } } } diff --git a/Source/Engine/Debug/DebugDraw.cpp b/Source/Engine/Debug/DebugDraw.cpp index 0bb97c0b2..496b4ab1f 100644 --- a/Source/Engine/Debug/DebugDraw.cpp +++ b/Source/Engine/Debug/DebugDraw.cpp @@ -27,6 +27,8 @@ #include "Engine/Render2D/FontAsset.h" #if USE_EDITOR #include "Editor/Editor.h" +#include "Engine/Level/Actors/Light.h" +#include "Engine/Physics/Colliders/Collider.h" #endif // Debug draw service configuration @@ -950,6 +952,23 @@ void DebugDraw::DrawActorsTree(Actor* actor) actor->TreeExecute(function); } +#if USE_EDITOR +void DebugDraw::DrawColliderDebugPhysics(Collider* collider, RenderView& view) +{ + if (!collider) + return; + + collider->DrawPhysicsDebug(view); +} + +void DebugDraw::DrawLightDebug(Light* light, RenderView& view) +{ + if (!light) + return; + + light->DrawLightsDebug(view); +} +#endif void DebugDraw::DrawAxisFromDirection(const Vector3& origin, const Vector3& direction, float size, float duration, bool depthTest) { ASSERT(direction.IsNormalized()); diff --git a/Source/Engine/Debug/DebugDraw.h b/Source/Engine/Debug/DebugDraw.h index ad5bd6fa1..13b556b1b 100644 --- a/Source/Engine/Debug/DebugDraw.h +++ b/Source/Engine/Debug/DebugDraw.h @@ -8,6 +8,9 @@ #include "Engine/Core/Math/Color.h" #include "Engine/Core/Types/Span.h" +struct RenderView; +class Collider; +class Light; struct RenderContext; class GPUTextureView; class GPUContext; @@ -70,9 +73,23 @@ API_CLASS(Static) class FLAXENGINE_API DebugDraw /// /// Draws the debug shapes for the given actor and the actor's children /// - /// /// The actor to start drawing at. + /// The actor to start drawing at. API_FUNCTION() static void DrawActorsTree(Actor* actor); +#if USE_EDITOR + /// + /// Draws the physics debug shapes for the given collider. Editor Only + /// + /// The collider to draw. + /// The render view to draw in. + API_FUNCTION() static void DrawColliderDebugPhysics(Collider* collider, RenderView& view); + /// + /// Draws the light debug shapes for the given light. Editor Only + /// + /// The light debug to draw. + /// The render view to draw in. + API_FUNCTION() static void DrawLightDebug(Light* light, RenderView& view); +#endif /// /// Draws the lines axis from direction. /// diff --git a/Source/Engine/Physics/Colliders/Collider.h b/Source/Engine/Physics/Colliders/Collider.h index a1660d531..e48bfa37a 100644 --- a/Source/Engine/Physics/Colliders/Collider.h +++ b/Source/Engine/Physics/Colliders/Collider.h @@ -154,10 +154,6 @@ protected: /// void RemoveStaticActor(); -#if USE_EDITOR - virtual void DrawPhysicsDebug(RenderView& view); -#endif - private: void OnMaterialChanged(); @@ -169,6 +165,10 @@ public: void ClosestPoint(const Vector3& point, Vector3& result) const final; bool ContainsPoint(const Vector3& point) const final; +#if USE_EDITOR + virtual void DrawPhysicsDebug(RenderView& view); +#endif + protected: // [PhysicsColliderActor] void OnEnable() override;