Debug Draw 3D 1.6.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/compiler.h"
8#include "utils/profiler.h"
9
10#include <map>
11#include <memory>
12#include <mutex>
13
14GODOT_WARNING_DISABLE()
15#include <godot_cpp/classes/array_mesh.hpp>
16#include <godot_cpp/classes/node3d.hpp>
17#include <godot_cpp/classes/shader.hpp>
18#include <godot_cpp/classes/shader_material.hpp>
19#include <godot_cpp/classes/sub_viewport.hpp>
20GODOT_WARNING_RESTORE()
21using namespace godot;
22
23class DebugDraw3D;
27
28#ifndef DISABLE_DEBUG_RENDERING
29class DebugGeometryContainer;
30class NodesContainer;
31struct DelayedRendererLine;
32#endif
33
35enum class MeshMaterialType : char {
36 Wireframe,
37 Billboard,
38 Plane,
39 Extendable,
40 MAX,
41};
42
44enum class MeshMaterialVariant : char {
45 Normal,
46 NoDepth,
47 MAX,
48};
49
50#ifndef DISABLE_DEBUG_RENDERING
52class _DD3D_WorldWatcher : public Node3D {
53 GDCLASS(_DD3D_WorldWatcher, Node3D)
54protected:
55 DebugDraw3D *m_owner = nullptr;
56 uint64_t m_world_id;
57 static void _bind_methods() {};
58
59public:
60 virtual void _process(double p_delta) override;
61 virtual void _notification(int p_what);
62
63 _DD3D_WorldWatcher() :
64 m_world_id() {}
65 _DD3D_WorldWatcher(DebugDraw3D *p_root, uint64_t p_world_id);
66};
67
68#endif
69
117class DebugDraw3D : public Object, public IScopeStorage<DebugDraw3DScopeConfig, DebugDraw3DScopeConfig::Data> {
118 GDCLASS(DebugDraw3D, Object)
119
120 friend DebugDrawManager;
121
122#ifndef DISABLE_DEBUG_RENDERING
123 friend DebugGeometryContainer;
124 friend NodesContainer;
125 friend _DD3D_WorldWatcher;
126#endif
127
128public:
132 enum PointType : int {
133 POINT_TYPE_SQUARE,
134 POINT_TYPE_SPHERE,
135 };
136
137private:
138 static DebugDraw3D *singleton;
139
140 String root_settings_section;
141 const static char *s_use_icosphere;
142 const static char *s_use_icosphere_hd;
143 const static char *s_add_bevel_to_volumetric;
144 const static char *s_default_frustum_scale;
145
146 const static char *s_default_thickness;
147 const static char *s_default_center_brightness;
148 const static char *s_default_hd_spheres;
149 const static char *s_default_plane_size;
150
151 const static char *s_render_priority;
152 const static char *s_render_mode;
153 const static char *s_render_fog_disabled;
154
155 std::vector<SubViewport *> custom_editor_viewports;
156 DebugDrawManager *root_node = nullptr;
157
158 Ref<DebugDraw3DScopeConfig> default_scoped_config;
159
160#ifndef DISABLE_DEBUG_RENDERING
161 ProfiledMutex(std::recursive_mutex, datalock, "3D Geometry lock");
162
163 struct ScopedPairIdConfig {
164 uint64_t id;
166 ScopedPairIdConfig(uint64_t id, DebugDraw3DScopeConfig *scfg) :
167 id(id), scfg(scfg) {}
168 };
169 // stores thread id and array of id's with ptrs
170 std::unordered_map<uint64_t, std::vector<ScopedPairIdConfig> > scoped_configs;
171 // stores thread id and most recent config
172 std::unordered_map<uint64_t, std::shared_ptr<DebugDraw3DScopeConfig::Data> > cached_scoped_configs;
173 uint64_t created_scoped_configs = 0;
174 struct {
175 uint64_t created;
176 uint64_t orphans;
177 } scoped_stats_3d = {};
178
179 // Inherited via IScopeStorage
180 const DebugDraw3DScopeConfig::Data *scoped_config_for_current_thread() override;
181
182 // Meshes
184 std::vector<std::array<Ref<ArrayMesh>, (int)MeshMaterialVariant::MAX> > shared_generated_meshes;
185
187 struct ViewportToDebugContainerItem {
188 uint64_t world_id;
189 _DD3D_WorldWatcher *world_watcher;
190 std::unique_ptr<DebugGeometryContainer> dgcs[(int)MeshMaterialVariant::MAX];
191 std::unique_ptr<NodesContainer> ncs[(int)MeshMaterialVariant::MAX];
192
193 ViewportToDebugContainerItem();
194 ViewportToDebugContainerItem(ViewportToDebugContainerItem &&other) noexcept;
195 ~ViewportToDebugContainerItem();
196 };
197
198 std::unordered_map<uint64_t /* World3D */, ViewportToDebugContainerItem> debug_containers;
199 // invalidate on add/remove operations or use ptrs
200 std::unordered_map<const Viewport *, ViewportToDebugContainerItem *> viewport_to_world_cache;
201 std::unordered_map<uint64_t /*Viewport * */, Ref<World3D> > world3ds_found_for_threads_cache;
202
203 // Default materials and shaders
204 Ref<ShaderMaterial> mesh_shaders[(int)MeshMaterialType::MAX][(int)MeshMaterialVariant::MAX];
205
206 // Inherited via IScopeStorage
207 void _register_scoped_config(uint64_t p_thread_id, uint64_t p_guard_id, DebugDraw3DScopeConfig *p_cfg) override;
208 void _unregister_scoped_config(uint64_t p_thread_id, uint64_t p_guard_id) override;
209 void _clear_scoped_configs() override;
210
211 std::array<Ref<ArrayMesh>, (int)MeshMaterialVariant::MAX> *get_shared_meshes();
212 DebugDraw3D::ViewportToDebugContainerItem *get_debug_container(const DebugDraw3DScopeConfig::DebugContainerDependent &p_dgcd, const bool p_generate_new_container);
213 void _deferred_find_world_in_viewport(uint64_t p_viewport_id);
214 void _register_viewport_world_deferred(uint64_t /*Viewport * */ p_viewport_id, const uint64_t p_world_id, _DD3D_WorldWatcher *watcher);
215 Node *_get_root_world_node(Node *p_scene_root, Viewport *p_vp);
216 void _remove_debug_container(const uint64_t &p_world_id);
217
218 _FORCE_INLINE_ Vector3 get_up_vector(const Vector3 &p_dir);
219 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);
220 Node *get_root_node();
221
222 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);
223
224#ifdef DEV_ENABLED
225 void _save_generated_meshes();
226#endif
227
228#endif
229
230 void init(DebugDrawManager *p_root);
231
232 void set_custom_editor_viewport(std::vector<SubViewport *> p_viewports);
233 std::vector<SubViewport *> get_custom_editor_viewports();
234
235 Ref<ShaderMaterial> get_material_variant(MeshMaterialType p_type, MeshMaterialVariant p_var);
236
237 void _load_materials();
238 inline bool _is_enabled_override() const;
239
240 void process_start(double delta);
241 void process_end(double delta);
242 void physics_process_start(double p_delta);
243 void physics_process_end(double p_delta);
244
245#pragma region Exposed Parameter Values
246
248 bool debug_enabled = true;
249
250 Ref<DebugDraw3DConfig> config;
251
252#pragma endregion // Exposed Parameter Values
253
254protected:
256 static void _bind_methods();
257
258public:
259 DebugDraw3D();
260 ~DebugDraw3D();
261
265 static DebugDraw3D *get_singleton() {
266 return singleton;
267 };
268
269#pragma region Configs
277 Ref<DebugDraw3DScopeConfig> new_scoped_config();
283 Ref<DebugDraw3DScopeConfig> scoped_config() override;
284
288 void set_config(Ref<DebugDraw3DConfig> cfg);
292 Ref<DebugDraw3DConfig> get_config() const;
293
294#pragma endregion // Configs
295
296#pragma region Exposed Parameters
298 void set_empty_color(const Color &col) {};
302 Color get_empty_color() const;
303
307 void set_debug_enabled(const bool &state);
308 bool is_debug_enabled() const;
309
310#pragma endregion // Exposed Parametes
311
312#pragma region Exposed Draw Methods
313
319 Ref<DebugDraw3DStats> get_render_stats();
320
326 Ref<DebugDraw3DStats> get_render_stats_for_world(Viewport *viewport);
327
328#ifndef DISABLE_DEBUG_RENDERING
329#define FAKE_FUNC_IMPL
330#else
331#define FAKE_FUNC_IMPL \
332 { \
333 }
334#endif
335
342
346 void clear_all();
347
348#pragma region Spheres
349
351 void draw_sphere_base(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
362 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;
372 void draw_sphere_xf(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
373
374#pragma endregion // Spheres
375
376#pragma region Cylinders
377
387 void draw_cylinder(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
388
400 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;
401
402#pragma endregion // Cylinders
403
404#pragma region Boxes
405
420 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;
421
436 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;
437
446 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;
447
455 void draw_aabb(const AABB &aabb, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
456
465 void draw_aabb_ab(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
466
467#pragma endregion // Boxes
468
469#pragma region Lines
470
487 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;
488
505 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;
506
507#pragma region Normal
508
510 void draw_lines_c(const std::vector<Vector3> &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
521 void draw_line(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
522
534 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;
535
545 void draw_lines(const PackedVector3Array &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
546
558 void draw_line_path(const PackedVector3Array &path, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
559
560#pragma endregion // Normal
561
562#pragma region Arrows
563
571 void draw_arrowhead(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
572
585 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;
586
598 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;
599
611 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;
612
613#pragma endregion // Arrows
614#pragma region Points
615
630 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;
631
632#pragma endregion // Points
633#pragma endregion // Lines
634
635#pragma region Misc
636
648 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;
649
658 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;
659
672 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;
673
683 void draw_position(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
684
697 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;
698
712 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;
713
725 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;
726
727#pragma region Camera Frustum
728
730 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;
740 void draw_camera_frustum(const class godot::Camera3D *camera, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
741
749 void draw_camera_frustum_planes(const Array &camera_frustum, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
750
751#pragma endregion // Camera Frustum
752
753#pragma region Text
770 void draw_text(const Vector3 &position, const String text, const int size = 32, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
771
772#pragma endregion // Text
773
774#pragma endregion // Misc
775#pragma endregion // Exposed Draw Methods
776
777#undef FAKE_FUNC_IMPL
778};
779
780VARIANT_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:59
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:117
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_text(const Vector3 &position, const String text, const int size=32, const Color &color=Colors::empty_color, 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:132
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:265
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:54