Debug Draw 3D (and 2D) 1.3.1
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, DebugDraw3DScopeConfig::Data> {
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 typedef std::shared_ptr<DebugDraw3DScopeConfig::Data> DebugDraw3DScopeConfig_Data;
121 // stores thread id and array of id's with ptrs
122 std::unordered_map<uint64_t, std::vector<ScopedPairIdConfig> > scoped_configs;
123 // stores thread id and most recent config
124 std::unordered_map<uint64_t, DebugDraw3DScopeConfig_Data> cached_scoped_configs;
125 uint64_t create_scoped_configs = 0;
126
127 // Inherited via IScopeStorage
128 const DebugDraw3DScopeConfig_Data scoped_config_for_current_thread() override;
129
130 // Meshes
131 std::unique_ptr<DebugGeometryContainer> dgc;
132
133 Vector3 previous_camera_position;
134 double previous_camera_far_plane = 0;
135
136 // Default materials and shaders
137 Ref<ShaderMaterial> shader_wireframe_mat;
138 Ref<Shader> shader_wireframe_code;
139
140 Ref<ShaderMaterial> shader_billboard_mat;
141 Ref<Shader> shader_billboard_code;
142
143 Ref<ShaderMaterial> shader_plane_mat;
144 Ref<Shader> shader_plane_code;
145
146 Ref<ShaderMaterial> shader_extendable_mat;
147 Ref<Shader> shader_extendable_code;
148
149 // Inherited via IScopeStorage
150 void _register_scoped_config(uint64_t thread_id, uint64_t guard_id, DebugDraw3DScopeConfig *cfg) override;
151 void _unregister_scoped_config(uint64_t thread_id, uint64_t guard_id) override;
152 void _clear_scoped_configs() override;
153
154 // Internal use of raw pointer to avoid ref/unref
155 Color _scoped_config_to_custom(const DebugDraw3DScopeConfig_Data &cfg);
156 InstanceType _scoped_config_type_convert(ConvertableInstanceType type, const DebugDraw3DScopeConfig_Data &cfg);
157 GeometryType _scoped_config_get_geometry_type(const DebugDraw3DScopeConfig_Data &cfg);
158
159 _FORCE_INLINE_ Vector3 get_up_vector(const Vector3 &dir);
160 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);
161 Node *get_root_node();
162
163 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);
164
165#endif
166
167 void init(DebugDrawManager *root);
168
169 void set_custom_editor_viewport(std::vector<SubViewport *> _viewports);
170 std::vector<SubViewport *> get_custom_editor_viewports();
171
172 Ref<ShaderMaterial> get_wireframe_material();
173 Ref<ShaderMaterial> get_billboard_material();
174 Ref<ShaderMaterial> get_plane_material();
175 Ref<ShaderMaterial> get_extendable_material();
176
177 void _load_materials();
178 void _set_base_world_node(Node *world_base);
179 inline bool _is_enabled_override() const;
180
181 void process(double delta);
182 void physics_process_start(double delta);
183 void physics_process_end(double delta);
184
185#pragma region Exposed Parameter Values
186
188 bool debug_enabled = true;
190 Viewport *custom_viewport = nullptr;
191
192 Ref<DebugDraw3DConfig> config;
193
194#pragma endregion // Exposed Parameter Values
195
196protected:
198 static void _bind_methods();
199
200public:
201 DebugDraw3D();
202 ~DebugDraw3D();
203
208 return singleton;
209 };
210
211#pragma region Configs
219 Ref<DebugDraw3DScopeConfig> new_scoped_config();
225 Ref<DebugDraw3DScopeConfig> scoped_config() override;
226
230 void set_config(Ref<DebugDraw3DConfig> _cfg);
234 Ref<DebugDraw3DConfig> get_config() const;
235
236#pragma endregion // Configs
237
238#pragma region Exposed Parameters
240 void set_empty_color(const Color &_col){};
244 Color get_empty_color() const;
245
249 void set_debug_enabled(const bool &_state);
250 bool is_debug_enabled() const;
251
255 void set_custom_viewport(Viewport *_viewport);
256 Viewport *get_custom_viewport() const;
257
258#pragma endregion // Exposed Parametes
259
260#pragma region Exposed Draw Methods
261
267 Ref<DebugDraw3DStats> get_render_stats();
268
269#ifndef DISABLE_DEBUG_RENDERING
270#define FAKE_FUNC_IMPL
271#else
272#define FAKE_FUNC_IMPL \
273 {}
274#endif
275
282
286 void clear_all();
287
288#pragma region Spheres
289
291 void draw_sphere_base(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
302 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;
312 void draw_sphere_xf(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
313
314#pragma endregion // Spheres
315
316#pragma region Cylinders
317
327 void draw_cylinder(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
328
340 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;
341
342#pragma endregion // Cylinders
343
344#pragma region Boxes
345
360 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;
361
376 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;
377
386 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;
387
395 void draw_aabb(const AABB &aabb, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
396
405 void draw_aabb_ab(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
406
407#pragma endregion // Boxes
408
409#pragma region Lines
410
427 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;
428
445 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;
446
447#pragma region Normal
448
450 void draw_lines_c(const std::vector<Vector3> &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
461 void draw_line(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
462
474 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;
475
485 void draw_lines(const PackedVector3Array &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
486
498 void draw_line_path(const PackedVector3Array &path, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
499
500#pragma endregion // Normal
501
502#pragma region Arrows
503
511 void draw_arrowhead(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
512
525 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;
526
538 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;
539
551 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;
552
553#pragma endregion // Arrows
554#pragma region Points
555
570 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;
571
572#pragma endregion // Points
573#pragma endregion // Lines
574
575#pragma region Misc
576
588 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;
589
598 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;
599
612 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;
613
623 void draw_position(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
624
637 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;
638
652 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;
653
665 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;
666
667#pragma region Camera Frustum
668
670 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;
680 void draw_camera_frustum(const class godot::Camera3D *camera, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
681
689 void draw_camera_frustum_planes(const Array &camera_frustum, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
690
691#pragma endregion // Camera Frustum
692
693#pragma endregion // Misc
694#pragma endregion // Exposed Draw Methods
695
696#undef FAKE_FUNC_IMPL
697};
698
699VARIANT_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:56
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:207
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