Debug Draw 3D (and 2D) 1.4.0
Draw 3D debug graphics and 2D overlays with this add-on.
Loading...
Searching...
No Matches
debug_draw_3d.h
1#pragma once
2
3#include "common/colors.h"
4#include "common/i_scope_storage.h"
5#include "config_scope_3d.h"
6#include "render_instances_enums.h"
7#include "utils/profiler.h"
8
9#include <map>
10#include <memory>
11#include <mutex>
12
13GODOT_WARNING_DISABLE()
14#include <godot_cpp/classes/array_mesh.hpp>
15#include <godot_cpp/classes/node3d.hpp>
16#include <godot_cpp/classes/shader.hpp>
17#include <godot_cpp/classes/shader_material.hpp>
18#include <godot_cpp/classes/sub_viewport.hpp>
19GODOT_WARNING_RESTORE()
20using namespace godot;
21
22class DebugDraw3D;
23class DataGraphManager;
27
28#ifndef DISABLE_DEBUG_RENDERING
29class DebugGeometryContainer;
30struct DelayedRendererLine;
31#endif
32
33#ifndef DISABLE_DEBUG_RENDERING
35class _DD3D_WorldWatcher : public Node3D {
36 GDCLASS(_DD3D_WorldWatcher, Node3D)
37protected:
38 DebugDraw3D *m_owner = nullptr;
39 uint64_t m_world_id;
40 static void _bind_methods(){};
41
42public:
43 virtual void _process(double p_delta) override;
44 virtual void _notification(int p_what);
45
46 _DD3D_WorldWatcher() :
47 m_world_id() {}
48 _DD3D_WorldWatcher(DebugDraw3D *p_root, uint64_t p_world_id);
49};
50
51#endif
52
100class DebugDraw3D : public Object, public IScopeStorage<DebugDraw3DScopeConfig, DebugDraw3DScopeConfig::Data> {
101 GDCLASS(DebugDraw3D, Object)
102
103 friend DebugDrawManager;
104
105#ifndef DISABLE_DEBUG_RENDERING
106 friend DebugGeometryContainer;
107 friend _DD3D_WorldWatcher;
108#endif
109
110public:
114 enum PointType : int {
115 POINT_TYPE_SQUARE,
116 POINT_TYPE_SPHERE,
117 };
118
119private:
120 static DebugDraw3D *singleton;
121
122 String root_settings_section;
123 const static char *s_use_icosphere;
124 const static char *s_use_icosphere_hd;
125 const static char *s_add_bevel_to_volumetric;
126 const static char *s_default_frustum_scale;
127
128 const static char *s_default_thickness;
129 const static char *s_default_center_brightness;
130 const static char *s_default_hd_spheres;
131 const static char *s_default_plane_size;
132
133 std::vector<SubViewport *> custom_editor_viewports;
134 DebugDrawManager *root_node = nullptr;
135
136 Ref<DebugDraw3DScopeConfig> default_scoped_config;
137
138#ifndef DISABLE_DEBUG_RENDERING
139 ProfiledMutex(std::recursive_mutex, datalock, "3D Geometry lock");
140
141 typedef std::pair<uint64_t, DebugDraw3DScopeConfig *> ScopedPairIdConfig;
142 // stores thread id and array of id's with ptrs
143 std::unordered_map<uint64_t, std::vector<ScopedPairIdConfig> > scoped_configs;
144 // stores thread id and most recent config
145 std::unordered_map<uint64_t, std::shared_ptr<DebugDraw3DScopeConfig::Data> > cached_scoped_configs;
146 uint64_t created_scoped_configs = 0;
147 struct {
148 uint64_t created;
149 uint64_t orphans;
150 } scoped_stats_3d = {};
151
152 // Inherited via IScopeStorage
153 const std::shared_ptr<DebugDraw3DScopeConfig::Data> scoped_config_for_current_thread() override;
154
155 // Meshes
157 std::vector<Ref<ArrayMesh> > shared_generated_meshes;
159 std::unordered_map<uint64_t, std::shared_ptr<DebugGeometryContainer> > debug_containers;
160 struct viewportToWorldCache {
161 uint64_t world_id = 0;
162 std::shared_ptr<DebugGeometryContainer> dgc;
163 };
164 std::unordered_map<const Viewport *, viewportToWorldCache> viewport_to_world_cache;
165
166 // Default materials and shaders
167 Ref<ShaderMaterial> shader_wireframe_mat;
168 Ref<Shader> shader_wireframe_code;
169
170 Ref<ShaderMaterial> shader_billboard_mat;
171 Ref<Shader> shader_billboard_code;
172
173 Ref<ShaderMaterial> shader_plane_mat;
174 Ref<Shader> shader_plane_code;
175
176 Ref<ShaderMaterial> shader_extendable_mat;
177 Ref<Shader> shader_extendable_code;
178
179 // Inherited via IScopeStorage
180 void _register_scoped_config(uint64_t p_thread_id, uint64_t p_guard_id, DebugDraw3DScopeConfig *p_cfg) override;
181 void _unregister_scoped_config(uint64_t p_thread_id, uint64_t p_guard_id) override;
182 void _clear_scoped_configs() override;
183
184 Ref<ArrayMesh> *get_shared_meshes();
185 std::shared_ptr<DebugGeometryContainer> create_debug_container();
186 std::shared_ptr<DebugGeometryContainer> get_debug_container(Viewport *p_vp);
187 void _register_viewport_world_deferred(Viewport *p_vp, const uint64_t p_world_id);
188 Viewport *_get_root_world_viewport(Viewport *p_vp);
189 void _remove_debug_container(const uint64_t &p_world_id);
190
191 _FORCE_INLINE_ Vector3 get_up_vector(const Vector3 &p_dir);
192 void add_or_update_line_with_thickness(real_t p_exp_time, std::unique_ptr<Vector3[]> p_lines, const size_t p_line_count, const Color &p_col, const std::function<void(DelayedRendererLine *)> p_custom_upd = nullptr);
193 Node *get_root_node();
194
195 void create_arrow(const Vector3 &p_a, const Vector3 &p_b, const Color &p_color, const real_t &p_arrow_size, const bool &p_is_absolute_size, const real_t &p_duration = 0);
196
197#endif
198
199 void init(DebugDrawManager *p_root);
200
201 void set_custom_editor_viewport(std::vector<SubViewport *> p_viewports);
202 std::vector<SubViewport *> get_custom_editor_viewports();
203
204 Ref<ShaderMaterial> get_wireframe_material();
205 Ref<ShaderMaterial> get_billboard_material();
206 Ref<ShaderMaterial> get_plane_material();
207 Ref<ShaderMaterial> get_extendable_material();
208
209 void _load_materials();
210 inline bool _is_enabled_override() const;
211
212 void process(double p_delta);
213 void physics_process_start(double p_delta);
214 void physics_process_end(double p_delta);
215
216#pragma region Exposed Parameter Values
217
219 bool debug_enabled = true;
220
221 Ref<DebugDraw3DConfig> config;
222
223#pragma endregion // Exposed Parameter Values
224
225protected:
227 static void _bind_methods();
228
229public:
230 DebugDraw3D();
231 ~DebugDraw3D();
232
237 return singleton;
238 };
239
240#pragma region Configs
248 Ref<DebugDraw3DScopeConfig> new_scoped_config();
254 Ref<DebugDraw3DScopeConfig> scoped_config() override;
255
259 void set_config(Ref<DebugDraw3DConfig> cfg);
263 Ref<DebugDraw3DConfig> get_config() const;
264
265#pragma endregion // Configs
266
267#pragma region Exposed Parameters
269 void set_empty_color(const Color &col){};
273 Color get_empty_color() const;
274
278 void set_debug_enabled(const bool &state);
279 bool is_debug_enabled() const;
280
281#pragma endregion // Exposed Parametes
282
283#pragma region Exposed Draw Methods
284
290 Ref<DebugDraw3DStats> get_render_stats();
291
297 Ref<DebugDraw3DStats> get_render_stats_for_world(Viewport *viewport);
298
299#ifndef DISABLE_DEBUG_RENDERING
300#define FAKE_FUNC_IMPL
301#else
302#define FAKE_FUNC_IMPL \
303 {}
304#endif
305
312
316 void clear_all();
317
318#pragma region Spheres
319
321 void draw_sphere_base(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
332 void draw_sphere(const Vector3 &position, const real_t &radius = 0.5f, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
342 void draw_sphere_xf(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
343
344#pragma endregion // Spheres
345
346#pragma region Cylinders
347
357 void draw_cylinder(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
358
370 void draw_cylinder_ab(const Vector3 &a, const Vector3 &b, const real_t &radius = 0.5f, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
371
372#pragma endregion // Cylinders
373
374#pragma region Boxes
375
390 void draw_box(const Vector3 &position, const Quaternion &rotation, const Vector3 &size, const Color &color = Colors::empty_color, const bool &is_box_centered = false, const real_t &duration = 0) FAKE_FUNC_IMPL;
391
406 void draw_box_ab(const Vector3 &a, const Vector3 &b, const Vector3 &up, const Color &color = Colors::empty_color, const bool &is_ab_diagonal = true, const real_t &duration = 0) FAKE_FUNC_IMPL;
407
416 void draw_box_xf(const Transform3D &transform, const Color &color = Colors::empty_color, const bool &is_box_centered = true, const real_t &duration = 0) FAKE_FUNC_IMPL;
417
425 void draw_aabb(const AABB &aabb, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
426
435 void draw_aabb_ab(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
436
437#pragma endregion // Boxes
438
439#pragma region Lines
440
457 void draw_line_hit(const Vector3 &start, const Vector3 &end, const Vector3 &hit, const bool &is_hit, const real_t &hit_size = 0.25f, const Color &hit_color = Colors::empty_color, const Color &after_hit_color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
458
475 void draw_line_hit_offset(const Vector3 &start, const Vector3 &end, const bool &is_hit, const real_t &unit_offset_of_hit = 0.5f, const real_t &hit_size = 0.25f, const Color &hit_color = Colors::empty_color, const Color &after_hit_color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
476
477#pragma region Normal
478
480 void draw_lines_c(const std::vector<Vector3> &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
491 void draw_line(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
492
504 void draw_ray(const Vector3 &origin, const Vector3 &direction, const real_t &length, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
505
515 void draw_lines(const PackedVector3Array &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
516
528 void draw_line_path(const PackedVector3Array &path, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
529
530#pragma endregion // Normal
531
532#pragma region Arrows
533
541 void draw_arrowhead(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
542
555 void draw_arrow(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &arrow_size = 0.5f, const bool &is_absolute_size = false, const real_t &duration = 0) FAKE_FUNC_IMPL;
556
568 void draw_arrow_ray(const Vector3 &origin, const Vector3 &direction, const real_t &length, const Color &color = Colors::empty_color, const real_t &arrow_size = 0.5f, const bool &is_absolute_size = false, const real_t &duration = 0) FAKE_FUNC_IMPL;
569
581 void draw_arrow_path(const PackedVector3Array &path, const Color &color = Colors::empty_color, const real_t &arrow_size = 0.75f, const bool &is_absolute_size = true, const real_t &duration = 0) FAKE_FUNC_IMPL;
582
583#pragma endregion // Arrows
584#pragma region Points
585
600 void draw_point_path(const PackedVector3Array &path, const PointType type = PointType::POINT_TYPE_SQUARE, const real_t &size = 0.25f, const Color &points_color = Colors::empty_color, const Color &lines_color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
601
602#pragma endregion // Points
603#pragma endregion // Lines
604
605#pragma region Misc
606
618 void draw_points(const PackedVector3Array &points, const PointType type = PointType::POINT_TYPE_SQUARE, const real_t &size = 0.25f, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
619
628 void draw_square(const Vector3 &position, const real_t &size = 0.2f, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
629
642 void draw_plane(const Plane &plane, const Color &color = Colors::empty_color, const Vector3 &anchor_point = Vector3(INFINITY, INFINITY, INFINITY), const real_t &duration = 0) FAKE_FUNC_IMPL;
643
653 void draw_position(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
654
667 void draw_gizmo(const Transform3D &transform, const Color &color = Colors::empty_color, const bool &is_centered = false, const real_t &duration = 0) FAKE_FUNC_IMPL;
668
682 void draw_grid(const Vector3 &origin, const Vector3 &x_size, const Vector3 &y_size, const Vector2i &subdivision, const Color &color = Colors::empty_color, const bool &is_centered = true, const real_t &duration = 0) FAKE_FUNC_IMPL;
683
695 void draw_grid_xf(const Transform3D &transform, const Vector2i &p_subdivision, const Color &color = Colors::empty_color, const bool &is_centered = true, const real_t &duration = 0) FAKE_FUNC_IMPL;
696
697#pragma region Camera Frustum
698
700 void draw_camera_frustum_planes_c(const std::array<Plane, 6> &planes, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
710 void draw_camera_frustum(const class godot::Camera3D *camera, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
711
719 void draw_camera_frustum_planes(const Array &camera_frustum, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
720
721#pragma endregion // Camera Frustum
722
723#pragma endregion // Misc
724#pragma endregion // Exposed Draw Methods
725
726#undef FAKE_FUNC_IMPL
727};
728
729VARIANT_ENUM_CAST(DebugDraw3D::PointType);
This is a class for storing part of the DebugDraw3D configuration.
Definition config_3d.h:17
This class is used to override scope parameters for DebugDraw3D.
Definition config_scope_3d.h:57
You can get statistics about 3D rendering from this class.
Definition stats_3d.h:24
Singleton class for calling debugging 3D methods.
Definition debug_draw_3d.h:100
void draw_line(const Vector3 &a, const Vector3 &b, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_points(const PackedVector3Array &points, const PointType type=PointType::POINT_TYPE_SQUARE, const real_t &size=0.25f, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_camera_frustum_planes(const Array &camera_frustum, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_square(const Vector3 &position, const real_t &size=0.2f, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_cylinder_ab(const Vector3 &a, const Vector3 &b, const real_t &radius=0.5f, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_grid_xf(const Transform3D &transform, const Vector2i &p_subdivision, const Color &color=Colors::empty_color, const bool &is_centered=true, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_arrow_ray(const Vector3 &origin, const Vector3 &direction, const real_t &length, const Color &color=Colors::empty_color, const real_t &arrow_size=0.5f, const bool &is_absolute_size=false, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_box_ab(const Vector3 &a, const Vector3 &b, const Vector3 &up, const Color &color=Colors::empty_color, const bool &is_ab_diagonal=true, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_camera_frustum(const class godot::Camera3D *camera, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void set_debug_enabled(const bool &state)
void draw_aabb(const AABB &aabb, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void clear_all()
void draw_cylinder(const Transform3D &transform, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_ray(const Vector3 &origin, const Vector3 &direction, const real_t &length, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void regenerate_geometry_meshes()
Ref< DebugDraw3DConfig > get_config() const
void draw_line_hit(const Vector3 &start, const Vector3 &end, const Vector3 &hit, const bool &is_hit, const real_t &hit_size=0.25f, const Color &hit_color=Colors::empty_color, const Color &after_hit_color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_line_path(const PackedVector3Array &path, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_sphere(const Vector3 &position, const real_t &radius=0.5f, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_aabb_ab(const Vector3 &a, const Vector3 &b, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
PointType
Definition debug_draw_3d.h:114
void draw_arrow_path(const PackedVector3Array &path, const Color &color=Colors::empty_color, const real_t &arrow_size=0.75f, const bool &is_absolute_size=true, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_box(const Vector3 &position, const Quaternion &rotation, const Vector3 &size, const Color &color=Colors::empty_color, const bool &is_box_centered=false, const real_t &duration=0) FAKE_FUNC_IMPL
Ref< DebugDraw3DScopeConfig > new_scoped_config()
Color get_empty_color() const
Ref< DebugDraw3DStats > get_render_stats()
Ref< DebugDraw3DScopeConfig > scoped_config() override
void draw_grid(const Vector3 &origin, const Vector3 &x_size, const Vector3 &y_size, const Vector2i &subdivision, const Color &color=Colors::empty_color, const bool &is_centered=true, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_gizmo(const Transform3D &transform, const Color &color=Colors::empty_color, const bool &is_centered=false, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_line_hit_offset(const Vector3 &start, const Vector3 &end, const bool &is_hit, const real_t &unit_offset_of_hit=0.5f, const real_t &hit_size=0.25f, const Color &hit_color=Colors::empty_color, const Color &after_hit_color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_plane(const Plane &plane, const Color &color=Colors::empty_color, const Vector3 &anchor_point=Vector3(INFINITY, INFINITY, INFINITY), const real_t &duration=0) FAKE_FUNC_IMPL
void draw_lines(const PackedVector3Array &lines, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_position(const Transform3D &transform, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_sphere_xf(const Transform3D &transform, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_arrow(const Vector3 &a, const Vector3 &b, const Color &color=Colors::empty_color, const real_t &arrow_size=0.5f, const bool &is_absolute_size=false, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_box_xf(const Transform3D &transform, const Color &color=Colors::empty_color, const bool &is_box_centered=true, const real_t &duration=0) FAKE_FUNC_IMPL
static DebugDraw3D * get_singleton()
Definition debug_draw_3d.h:236
void draw_arrowhead(const Transform3D &transform, const Color &color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
void draw_point_path(const PackedVector3Array &path, const PointType type=PointType::POINT_TYPE_SQUARE, const real_t &size=0.25f, const Color &points_color=Colors::empty_color, const Color &lines_color=Colors::empty_color, const real_t &duration=0) FAKE_FUNC_IMPL
Ref< DebugDraw3DStats > get_render_stats_for_world(Viewport *viewport)
void set_config(Ref< DebugDraw3DConfig > cfg)
The main singleton class that handles DebugDraw2D and DebugDraw3D.
Definition debug_draw_manager.h:59