2025-07-22 12:35:20 +00:00
|
|
|
extends Control
|
2025-07-29 12:08:08 +00:00
|
|
|
## A utility node for drawing 3D debug visuals (lines, rays, cubes) in screen space.
|
2025-07-22 12:35:20 +00:00
|
|
|
@export var MouseOverlay: Node
|
2025-07-28 13:55:03 +00:00
|
|
|
@onready var Cam = get_viewport().get_camera_3d()
|
2025-07-22 12:35:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready() -> void:
|
|
|
|
if !OS.is_debug_build():
|
|
|
|
self.queue_free()
|
2025-07-29 12:08:08 +00:00
|
|
|
return
|
2025-07-28 13:55:03 +00:00
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
# Ensure the node processes and draws even when the game is paused.
|
|
|
|
process_mode = Node.PROCESS_MODE_ALWAYS
|
|
|
|
# Ensure the node is visible and set to draw in screen space.
|
|
|
|
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
2025-07-28 13:55:03 +00:00
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
|
|
|
|
## A class representing a single debug line with start/end points, color, and duration.
|
2025-07-28 13:55:03 +00:00
|
|
|
class Line:
|
|
|
|
var Start:Vector3
|
|
|
|
var End:Vector3
|
|
|
|
var LineColor:Color
|
2025-07-29 12:08:08 +00:00
|
|
|
var time:float
|
2025-07-28 13:55:03 +00:00
|
|
|
|
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
## Constructor for DebugLine. [br]
|
|
|
|
## [param _start] Starting point in 3D space.[br]
|
|
|
|
## [param _end] Ending point in 3D space.[br]
|
|
|
|
## [param _color] Color of the line.[br]
|
|
|
|
## [param _time] Duration the line persists (seconds).
|
|
|
|
func _init(_Start:Vector3, _End:Vector3, _LineColor:Color, _time:float):
|
|
|
|
self.Start = _Start
|
|
|
|
self.End = _End
|
|
|
|
self.LineColor = _LineColor
|
|
|
|
self.time = _time
|
2025-07-28 13:55:03 +00:00
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
## Array storing all active debug lines.
|
2025-07-28 13:55:03 +00:00
|
|
|
var Lines = []
|
2025-07-29 12:08:08 +00:00
|
|
|
## Flag to track if a line was removed, triggering a redraw.
|
2025-07-28 13:55:03 +00:00
|
|
|
var RemovedLine = false
|
|
|
|
|
|
|
|
|
|
|
|
func _process(delta):
|
|
|
|
for i in range(len(Lines)):
|
|
|
|
Lines[i].time -= delta
|
2025-07-29 12:08:08 +00:00
|
|
|
# Trigger a redraw if lines exist or a line was removed.
|
2025-07-28 13:55:03 +00:00
|
|
|
if (len(Lines) > 0 || RemovedLine):
|
|
|
|
queue_redraw() #Calls _draw
|
|
|
|
RemovedLine = false
|
|
|
|
|
|
|
|
|
|
|
|
func _draw():
|
2025-07-29 12:08:08 +00:00
|
|
|
# Exit early if no lines to draw or camera is not available.
|
|
|
|
if Lines.is_empty() or not Cam:
|
|
|
|
return
|
2025-07-28 13:55:03 +00:00
|
|
|
for i in range(len(Lines)):
|
|
|
|
if Lines[i].Start.length() == 0:return
|
|
|
|
var ScreenPointStart = Cam.unproject_position(Lines[i].Start)
|
|
|
|
var ScreenPointEnd = Cam.unproject_position(Lines[i].End)
|
|
|
|
|
|
|
|
#Dont draw line if either start or end is considered behind the camera
|
|
|
|
#this causes the line to not be drawn sometimes but avoids a bug where the
|
|
|
|
#line is drawn incorrectly
|
|
|
|
if (Cam.is_position_behind(Lines[i].Start) ||
|
|
|
|
Cam.is_position_behind(Lines[i].End)):
|
|
|
|
continue
|
|
|
|
|
|
|
|
draw_line(ScreenPointStart, ScreenPointEnd, Lines[i].LineColor)
|
|
|
|
|
|
|
|
#Remove lines that have timed out
|
|
|
|
var i = Lines.size() - 1
|
|
|
|
while (i >= 0):
|
|
|
|
if (Lines[i].time < 0.0):
|
|
|
|
Lines.remove_at(i)
|
|
|
|
RemovedLine = true
|
|
|
|
|
|
|
|
i -= 1
|
|
|
|
|
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
## Draws a line between two 3D points. [br]
|
|
|
|
## [param start] Starting point in 3D space.[br]
|
|
|
|
## [param end] Ending point in 3D space.[br]
|
|
|
|
## [param time] Duration the line persists (seconds, default: 0.0 for one frame).[br]
|
|
|
|
## [param color] Color of the line (default: red, fully opaque).
|
|
|
|
func DrawLine(Start, End, time := 0.0, LineColor := Color(1.0, 0.0, 0.0, 1.0)):
|
2025-07-28 13:55:03 +00:00
|
|
|
Lines.append(Line.new(Start, End, LineColor, time))
|
|
|
|
|
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
## Draws a ray from a starting point in a given direction.[br]
|
|
|
|
## [param start] Starting point in 3D space.[br]
|
|
|
|
## [param ray] Direction and magnitude of the ray (Vector3).[br]
|
|
|
|
## [param time] Duration the ray persists (seconds, default: 0.0 for one frame).[br]
|
|
|
|
## [param color] Color of the ray (default: red, fully opaque).
|
|
|
|
func DrawRay(Start, Ray, time := 0.0, LineColor := Color(1.0, 0.0, 0.0, 1.0)):
|
2025-07-28 13:55:03 +00:00
|
|
|
Lines.append(Line.new(Start, Start + Ray, LineColor, time))
|
|
|
|
|
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
## Draws a wireframe cube in 3D space.[br]
|
|
|
|
## [param center] Center point of the cube in 3D space.[br]
|
|
|
|
## [param half_extents] Half the width of the cube (default: 0.1).[br]
|
|
|
|
## [param time] Duration the cube persists (seconds, default: 0.0 for one frame).[br]
|
|
|
|
## [param color] Color of the cube's edges (default: red, fully opaque).
|
|
|
|
func DrawCube(Center, HalfExtents := 0.1, time := 0.0, LineColor := Color(1.0, 0.0, 0.0, 1.0)):
|
2025-07-28 13:55:03 +00:00
|
|
|
#Start at the 'top left'
|
|
|
|
var LinePointStart = Center
|
|
|
|
LinePointStart.x -= HalfExtents
|
|
|
|
LinePointStart.y += HalfExtents
|
|
|
|
LinePointStart.z -= HalfExtents
|
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
# Draw top square (four edges).
|
2025-07-28 13:55:03 +00:00
|
|
|
var LinePointEnd = LinePointStart + Vector3(0, 0, HalfExtents * 2.0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawLine(LinePointStart, LinePointEnd, time, LineColor);
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart = LinePointEnd
|
|
|
|
LinePointEnd = LinePointStart + Vector3(HalfExtents * 2.0, 0, 0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawLine(LinePointStart, LinePointEnd, time, LineColor);
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart = LinePointEnd
|
|
|
|
LinePointEnd = LinePointStart + Vector3(0, 0, -HalfExtents * 2.0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawLine(LinePointStart, LinePointEnd, time, LineColor);
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart = LinePointEnd
|
|
|
|
LinePointEnd = LinePointStart + Vector3(-HalfExtents * 2.0, 0, 0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawLine(LinePointStart, LinePointEnd, time, LineColor);
|
2025-07-28 13:55:03 +00:00
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
# Draw bottom square (four edges).
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart = LinePointEnd + Vector3(0, -HalfExtents * 2.0, 0)
|
|
|
|
LinePointEnd = LinePointStart + Vector3(0, 0, HalfExtents * 2.0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawLine(LinePointStart, LinePointEnd, time, LineColor);
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart = LinePointEnd
|
|
|
|
LinePointEnd = LinePointStart + Vector3(HalfExtents * 2.0, 0, 0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawLine(LinePointStart, LinePointEnd, time, LineColor);
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart = LinePointEnd
|
|
|
|
LinePointEnd = LinePointStart + Vector3(0, 0, -HalfExtents * 2.0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawLine(LinePointStart, LinePointEnd, time, LineColor);
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart = LinePointEnd
|
|
|
|
LinePointEnd = LinePointStart + Vector3(-HalfExtents * 2.0, 0, 0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawLine(LinePointStart, LinePointEnd, time, LineColor);
|
2025-07-28 13:55:03 +00:00
|
|
|
|
2025-07-29 12:08:08 +00:00
|
|
|
# Draw vertical lines (four edges connecting top and bottom squares).
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart = LinePointEnd
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0),time, LineColor)
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart += Vector3(0, 0, HalfExtents * 2.0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), time, LineColor)
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart += Vector3(HalfExtents * 2.0, 0, 0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), time, LineColor)
|
2025-07-28 13:55:03 +00:00
|
|
|
LinePointStart += Vector3(0, 0, -HalfExtents * 2.0)
|
2025-07-29 12:08:08 +00:00
|
|
|
DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), time, LineColor)
|