add fps counter to debug_draw
This commit is contained in:
@@ -7,27 +7,34 @@ const DEFAULT_COLOR: Color = Color.RED
|
||||
|
||||
var enabled: bool = false
|
||||
|
||||
var camera: Camera3D
|
||||
var control: Control = Control.new()
|
||||
var label: RichTextLabel = RichTextLabel.new()
|
||||
|
||||
var vectors_to_draw: Array[Dictionary] = []
|
||||
var markers_to_draw: Array[Dictionary] = []
|
||||
var circles_to_draw: Array[Dictionary] = []
|
||||
var text_to_draw: PackedStringArray = []
|
||||
|
||||
@onready var camera: Camera3D = get_viewport().get_camera_3d()
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
camera = get_viewport().get_camera_3d()
|
||||
layer = 999
|
||||
enabled = false
|
||||
control.draw.connect(_on_control_draw)
|
||||
add_child(control)
|
||||
|
||||
label.set_anchors_preset(Control.PRESET_FULL_RECT)
|
||||
add_child(label)
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if not enabled:
|
||||
return
|
||||
control.queue_redraw()
|
||||
|
||||
text('fps: ' + str(Engine.get_frames_per_second()))
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("toggle_debug"):
|
||||
@@ -36,6 +43,13 @@ func _input(event: InputEvent) -> void:
|
||||
vectors_to_draw.clear()
|
||||
|
||||
|
||||
func text(body: String) -> void:
|
||||
if not enabled:
|
||||
return
|
||||
|
||||
text_to_draw.append(body)
|
||||
|
||||
|
||||
func vector(from: Vector3, to: Vector3, color: Color = DEFAULT_COLOR) -> void:
|
||||
if not enabled:
|
||||
return
|
||||
@@ -46,7 +60,7 @@ func vector(from: Vector3, to: Vector3, color: Color = DEFAULT_COLOR) -> void:
|
||||
func marker(
|
||||
pos: Vector3,
|
||||
radius: float = MARKER_RADIUS,
|
||||
color: Color = DEFAULT_COLOR
|
||||
color: Color = DEFAULT_COLOR,
|
||||
) -> void:
|
||||
if not enabled:
|
||||
return
|
||||
@@ -65,6 +79,10 @@ func _unproject(pos: Vector3) -> Vector2:
|
||||
return camera.unproject_position(pos)
|
||||
|
||||
|
||||
func _draw_text() -> void:
|
||||
label.text = '\n'.join(text_to_draw)
|
||||
|
||||
|
||||
func _draw_vector(from: Vector3, to: Vector3, color: Color) -> void:
|
||||
var start := _unproject(from)
|
||||
var end := _unproject(to)
|
||||
@@ -76,7 +94,7 @@ func _draw_triangle(
|
||||
pos: Vector2,
|
||||
dir: Vector2,
|
||||
size: float,
|
||||
color: Color
|
||||
color: Color,
|
||||
) -> void:
|
||||
var a := pos + dir * size
|
||||
var b := pos + dir.rotated(2 * PI / 3) * size
|
||||
@@ -112,7 +130,7 @@ func _on_control_draw() -> void:
|
||||
_draw_vector(
|
||||
v["from"] as Vector3,
|
||||
v["to"] as Vector3,
|
||||
v["color"] as Color
|
||||
v["color"] as Color,
|
||||
)
|
||||
vectors_to_draw.clear()
|
||||
|
||||
@@ -120,10 +138,13 @@ func _on_control_draw() -> void:
|
||||
_draw_marker(
|
||||
v["pos"] as Vector3,
|
||||
v["radius"] as float,
|
||||
v["color"] as Color
|
||||
v["color"] as Color,
|
||||
)
|
||||
markers_to_draw.clear()
|
||||
|
||||
for v in circles_to_draw:
|
||||
_draw_circle(v["pos"] as Vector3, v["color"] as Color)
|
||||
circles_to_draw.clear()
|
||||
|
||||
_draw_text()
|
||||
text_to_draw.clear()
|
||||
|
||||
Reference in New Issue
Block a user