Debug Draw 3D (and 2D) 1.3.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 "utils/profiler.h"
7
8#include <map>
9#include <memory>
10#include <mutex>
11
12GODOT_WARNING_DISABLE()
13#include <godot_cpp/classes/shader.hpp>
14#include <godot_cpp/classes/shader_material.hpp>
15#include <godot_cpp/classes/sub_viewport.hpp>
16GODOT_WARNING_RESTORE()
17using namespace godot;
18
19class DataGraphManager;
23
24#ifndef DISABLE_DEBUG_RENDERING
25class DebugGeometryContainer;
26class DelayedRendererLine;
27
28enum class InstanceType : char;
29enum class ConvertableInstanceType : char;
30#endif
31
74class DebugDraw3D : public Object, public IScopeStorage<DebugDraw3DScopeConfig> {
75 GDCLASS(DebugDraw3D, Object)
76
77 friend DebugDrawManager;
78
79#ifndef DISABLE_DEBUG_RENDERING
80 friend DebugGeometryContainer;
81
82 enum GeometryType{
83 Wireframe,
84 Volumetric,
85 Solid,
86 };
87#endif
88
89public:
93 enum PointType : int {
94 POINT_TYPE_SQUARE,
95 POINT_TYPE_SPHERE,
96 };
97
98private:
99 static DebugDraw3D *singleton;
100
101 String root_settings_section;
102 const static char *s_use_icosphere;
103 const static char *s_use_icosphere_hd;
104 const static char *s_add_bevel_to_volumetric;
105 const static char *s_default_thickness;
106 const static char *s_default_center_brightness;
107 const static char *s_default_hd_spheres;
108 const static char *s_default_plane_size;
109
110 std::vector<SubViewport *> custom_editor_viewports;
111 DebugDrawManager *root_node = nullptr;
112
113 Ref<DebugDraw3DStats> stats_3d;
114 Ref<DebugDraw3DScopeConfig> default_scoped_config;
115
116#ifndef DISABLE_DEBUG_RENDERING
117 ProfiledMutex(std::recursive_mutex, datalock, "3D Geometry lock");
118
119 typedef std::pair<uint64_t, DebugDraw3DScopeConfig *> ScopedPairIdConfig;
120 // stores thread id and array of id's with ptrs
121 std::unordered_map<uint64_t, std::vector<ScopedPairIdConfig> > scoped_configs;
122 // stores thread id and most recent config
123 std::unordered_map<uint64_t, DebugDraw3DScopeConfig *> cached_scoped_configs;
124 uint64_t create_scoped_configs = 0;
125
126 // Inherited via IScopeStorage
127 DebugDraw3DScopeConfig *scoped_config_for_current_thread() override;
128
129 // Meshes
130 std::unique_ptr<DebugGeometryContainer> dgc;
131
132 Vector3 previous_camera_position;
133 double previous_camera_far_plane = 0;
134
135 // Default materials and shaders
136 Ref<ShaderMaterial> shader_wireframe_mat;
137 Ref<Shader> shader_wireframe_code;
138
139 Ref<ShaderMaterial> shader_billboard_mat;
140 Ref<Shader> shader_billboard_code;
141
142 Ref<ShaderMaterial> shader_plane_mat;
143 Ref<Shader> shader_plane_code;
144
145 Ref<ShaderMaterial> shader_extendable_mat;
146 Ref<Shader> shader_extendable_code;
147
148 // Inherited via IScopeStorage
149 void _register_scoped_config(uint64_t thread_id, uint64_t guard_id, DebugDraw3DScopeConfig *cfg) override;
150 void _unregister_scoped_config(uint64_t thread_id, uint64_t guard_id) override;
151 void _clear_scoped_configs() override;
152
153 // Internal use of raw pointer to avoid ref/unref
154 Color _scoped_config_to_custom(DebugDraw3DScopeConfig *cfg);
155 InstanceType _scoped_config_type_convert(ConvertableInstanceType type, DebugDraw3DScopeConfig *cfg);
156 GeometryType _scoped_config_get_geometry_type(DebugDraw3DScopeConfig *cfg);
157
158 _FORCE_INLINE_ Vector3 get_up_vector(const Vector3 &dir);
159 void add_or_update_line_with_thickness(real_t _exp_time, std::unique_ptr<Vector3[]> _lines, const size_t _line_count, const Color &_col, const std::function<void(DelayedRendererLine *)> _custom_upd = nullptr);
160 Node *get_root_node();
161
162 void create_arrow(const Vector3 &a, const Vector3 &b, const Color &color, const real_t &arrow_size, const bool &is_absolute_size, const real_t &duration = 0);
163
164#endif
165
166 void init(DebugDrawManager *root);
167
168 void set_custom_editor_viewport(std::vector<SubViewport *> _viewports);
169 std::vector<SubViewport *> get_custom_editor_viewports();
170
171 Ref<ShaderMaterial> get_wireframe_material();
172 Ref<ShaderMaterial> get_billboard_material();
173 Ref<ShaderMaterial> get_plane_material();
174 Ref<ShaderMaterial> get_extendable_material();
175
176 void _load_materials();
177 void _set_base_world_node(Node *world_base);
178 inline bool _is_enabled_override() const;
179
180 void process(double delta);
181 void physics_process_start(double delta);
182 void physics_process_end(double delta);
183
184#pragma region Exposed Parameter Values
185
187 bool debug_enabled = true;
189 Viewport *custom_viewport = nullptr;
190
191 Ref<DebugDraw3DConfig> config;
192
193#pragma endregion // Exposed Parameter Values
194
195protected:
197 static void _bind_methods();
198
199public:
200 DebugDraw3D();
201 ~DebugDraw3D();
202
207 return singleton;
208 };
209
210#pragma region Configs
218 Ref<DebugDraw3DScopeConfig> new_scoped_config();
224 Ref<DebugDraw3DScopeConfig> scoped_config() override;
225
229 void set_config(Ref<DebugDraw3DConfig> _cfg);
233 Ref<DebugDraw3DConfig> get_config() const;
234
235#pragma endregion // Configs
236
237#pragma region Exposed Parameters
239 void set_empty_color(const Color &_col){};
243 Color get_empty_color() const;
244
248 void set_debug_enabled(const bool &_state);
249 bool is_debug_enabled() const;
250
254 void set_custom_viewport(Viewport *_viewport);
255 Viewport *get_custom_viewport() const;
256
257#pragma endregion // Exposed Parametes
258
259#pragma region Exposed Draw Methods
260
266 Ref<DebugDraw3DStats> get_render_stats();
267
268#ifndef DISABLE_DEBUG_RENDERING
269#define FAKE_FUNC_IMPL
270#else
271#define FAKE_FUNC_IMPL \
272 {}
273#endif
274
281
285 void clear_all();
286
287#pragma region Spheres
288
290 void draw_sphere_base(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
301 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;
311 void draw_sphere_xf(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
312
313#pragma endregion // Spheres
314
315#pragma region Cylinders
316
326 void draw_cylinder(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
327
339 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;
340
341#pragma endregion // Cylinders
342
343#pragma region Boxes
344
359 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;
360
375 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;
376
385 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;
386
394 void draw_aabb(const AABB &aabb, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
395
404 void draw_aabb_ab(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
405
406#pragma endregion // Boxes
407
408#pragma region Lines
409
426 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;
427
444 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;
445
446#pragma region Normal
447
449 void draw_lines_c(const std::vector<Vector3> &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
460 void draw_line(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
461
473 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;
474
484 void draw_lines(const PackedVector3Array &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
485
497 void draw_line_path(const PackedVector3Array &path, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
498
499#pragma endregion // Normal
500
501#pragma region Arrows
502
510 void draw_arrowhead(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
511
524 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;
525
537 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;
538
550 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;
551
552#pragma endregion // Arrows
553#pragma region Points
554
569 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;
570
571#pragma endregion // Points
572#pragma endregion // Lines
573
574#pragma region Misc
575
587 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;
588
597 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;
598
611 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;
612
622 void draw_position(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
623
636 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;
637
651 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;
652
664 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;
665
666#pragma region Camera Frustum
667
669 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;
679 void draw_camera_frustum(const class godot::Camera3D *camera, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
680
688 void draw_camera_frustum_planes(const Array &camera_frustum, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
689
690#pragma endregion // Camera Frustum
691
692#pragma endregion // Misc
693#pragma endregion // Exposed Draw Methods
694
695#undef FAKE_FUNC_IMPL
696};
697
698VARIANT_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:32
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:74
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 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 set_custom_viewport(Viewport *_viewport)
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 set_debug_enabled(const bool &_state)
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:93
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:206
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
void set_config(Ref< DebugDraw3DConfig > _cfg)
The main singleton class that handles DebugDraw2D and DebugDraw3D.
Definition debug_draw_manager.h:59