Add light debug view flag to draw light shapes

This commit is contained in:
Chandler Cox
2023-07-07 17:32:46 -05:00
parent ca5cbf05be
commit a0bb3f2995
10 changed files with 89 additions and 0 deletions

View File

@@ -1538,6 +1538,7 @@ namespace FlaxEditor.Viewport
new ViewFlagOptions(ViewFlags.MotionBlur, "Motion Blur"),
new ViewFlagOptions(ViewFlags.ContactShadows, "Contact Shadows"),
new ViewFlagOptions(ViewFlags.PhysicsDebug, "Physics Debug"),
new ViewFlagOptions(ViewFlags.LightsDebug, "Lights Debug"),
new ViewFlagOptions(ViewFlags.DebugDraw, "Debug Draw"),
};

View File

@@ -1031,6 +1031,11 @@ API_ENUM(Attributes="Flags") enum class ViewFlags : uint64
/// </summary>
Sky = 1 << 26,
/// <summary>
/// Shows/hides light debug shapes.
/// </summary>
LightsDebug = 1 << 27,
/// <summary>
/// Default flags for Game.
/// </summary>

View File

@@ -26,6 +26,7 @@ void Light::OnEnable()
GetSceneRendering()->AddActor(this, _sceneRenderingKey);
#if USE_EDITOR
GetSceneRendering()->AddViewportIcon(this);
GetSceneRendering()->AddLightsDebug<Light, &Light::DrawLightsDebug>(this);
#endif
// Base
@@ -36,6 +37,7 @@ void Light::OnDisable()
{
#if USE_EDITOR
GetSceneRendering()->RemoveViewportIcon(this);
GetSceneRendering()->RemoveLightsDebug<Light, &Light::DrawLightsDebug>(this);
#endif
GetSceneRendering()->RemoveActor(this, _sceneRenderingKey);
@@ -43,6 +45,12 @@ void Light::OnDisable()
Actor::OnDisable();
}
#if USE_EDITOR
void Light::DrawLightsDebug(RenderView& view)
{
}
#endif
void Light::Serialize(SerializeStream& stream, const void* otherObj)
{
// Base

View File

@@ -66,6 +66,8 @@ public:
const Vector3 size(50);
return BoundingBox(_transform.Translation - size, _transform.Translation + size);
}
virtual void DrawLightsDebug(RenderView& view);
#endif
void Serialize(SerializeStream& stream, const void* otherObj) override;
void Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) override;

View File

@@ -143,6 +143,16 @@ void PointLight::OnDebugDrawSelected()
LightWithShadow::OnDebugDrawSelected();
}
void PointLight::DrawLightsDebug(RenderView& view)
{
const BoundingSphere sphere(_sphere.Center - view.Origin, _sphere.Radius);
if (!view.CullingFrustum.Intersects(sphere))
return;
// Draw influence range
DEBUG_DRAW_WIRE_SPHERE(_sphere, Color::Yellow, 0, true);
}
#endif
void PointLight::OnLayerChanged()

View File

@@ -95,6 +95,7 @@ public:
#if USE_EDITOR
void OnDebugDraw() override;
void OnDebugDrawSelected() override;
void DrawLightsDebug(RenderView& view) override;
#endif
void OnLayerChanged() override;
void Serialize(SerializeStream& stream, const void* otherObj) override;

View File

@@ -203,6 +203,11 @@ void SpotLight::OnDebugDrawSelected()
DEBUG_DRAW_LINE(position, position + forward * radius + right * discRadius, color, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius - right * discRadius, color, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius + up * falloffDiscRadius, color * 0.6f, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius - up * falloffDiscRadius, color * 0.6f, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius + right * falloffDiscRadius, color * 0.6f, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius - right * falloffDiscRadius, color * 0.6f, 0, true);
DEBUG_DRAW_CIRCLE(position + forward * radius, forward, discRadius, color, 0, true);
DEBUG_DRAW_CIRCLE(position + forward * radius, forward, falloffDiscRadius, color * 0.6f, 0, true);
@@ -210,6 +215,34 @@ void SpotLight::OnDebugDrawSelected()
LightWithShadow::OnDebugDrawSelected();
}
void SpotLight::DrawLightsDebug(RenderView& view)
{
const BoundingSphere sphere(_sphere.Center - view.Origin, _sphere.Radius);
if (!view.CullingFrustum.Intersects(sphere))
return;
const auto color = Color::Yellow;
Vector3 right = _transform.GetRight();
Vector3 up = _transform.GetUp();
Vector3 forward = GetDirection();
float radius = GetScaledRadius();
float discRadius = radius * Math::Tan(_outerConeAngle * DegreesToRadians);
float falloffDiscRadius = radius * Math::Tan(_innerConeAngle * DegreesToRadians);
Vector3 position = GetPosition();
DEBUG_DRAW_LINE(position, position + forward * radius + up * discRadius, color, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius - up * discRadius, color, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius + right * discRadius, color, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius - right * discRadius, color, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius + up * falloffDiscRadius, color * 0.6f, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius - up * falloffDiscRadius, color * 0.6f, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius + right * falloffDiscRadius, color * 0.6f, 0, true);
DEBUG_DRAW_LINE(position, position + forward * radius - right * falloffDiscRadius, color * 0.6f, 0, true);
DEBUG_DRAW_CIRCLE(position + forward * radius, forward, discRadius, color, 0, true);
DEBUG_DRAW_CIRCLE(position + forward * radius, forward, falloffDiscRadius, color * 0.6f, 0, true);
}
#endif
void SpotLight::Serialize(SerializeStream& stream, const void* otherObj)

View File

@@ -127,6 +127,7 @@ public:
#if USE_EDITOR
void OnDebugDraw() override;
void OnDebugDrawSelected() override;
void DrawLightsDebug(RenderView& view) override;
#endif
void Serialize(SerializeStream& stream, const void* otherObj) override;
void Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) override;

View File

@@ -96,6 +96,16 @@ void SceneRendering::Draw(RenderContextBatch& renderContextBatch, DrawCategory c
physicsDebugData[i](view);
}
}
// Draw light shapes
if (EnumHasAnyFlags(view.Flags, ViewFlags::LightsDebug))
{
const LightsDebugCallback* lightsDebugData = LightsDebug.Get();
for (int32 i = 0; i < LightsDebug.Count(); i++)
{
lightsDebugData[i](view);
}
}
}
#endif
}

View File

@@ -65,6 +65,7 @@ class FLAXENGINE_API SceneRendering
{
#if USE_EDITOR
typedef Function<void(RenderView&)> PhysicsDebugCallback;
typedef Function<void(RenderView&)> LightsDebugCallback;
friend class ViewportIconsRendererService;
#endif
public:
@@ -95,6 +96,7 @@ public:
private:
#if USE_EDITOR
Array<PhysicsDebugCallback> PhysicsDebug;
Array<LightsDebugCallback> LightsDebug;
Array<Actor*> ViewportIcons;
#endif
@@ -153,6 +155,22 @@ public:
PhysicsDebug.Remove(f);
}
template<class T, void(T::*Method)(RenderView&)>
FORCE_INLINE void AddLightsDebug(T* obj)
{
LightsDebugCallback f;
f.Bind<T, Method>(obj);
LightsDebug.Add(f);
}
template<class T, void(T::*Method)(RenderView&)>
void RemoveLightsDebug(T* obj)
{
LightsDebugCallback f;
f.Bind<T, Method>(obj);
LightsDebug.Remove(f);
}
FORCE_INLINE void AddViewportIcon(Actor* obj)
{
ViewportIcons.Add(obj);