Debug Draw 3D (and 2D) 1.5.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;
26
27#ifndef DISABLE_DEBUG_RENDERING
28class DebugGeometryContainer;
29class NodesContainer;
30struct DelayedRendererLine;
31#endif
32
34enum class MeshMaterialType : char {
35 Wireframe,
36 Billboard,
37 Plane,
38 Extendable,
39 MAX,
40};
41
43enum class MeshMaterialVariant : char {
44 Normal,
45 NoDepth,
46 MAX,
47};
48
49#ifndef DISABLE_DEBUG_RENDERING
51class _DD3D_WorldWatcher : public Node3D {
52 GDCLASS(_DD3D_WorldWatcher, Node3D)
53protected:
54 DebugDraw3D *m_owner = nullptr;
55 uint64_t m_world_id;
56 static void _bind_methods() {};
57
58public:
59 virtual void _process(double p_delta) override;
60 virtual void _notification(int p_what);
61
62 _DD3D_WorldWatcher() :
63 m_world_id() {}
64 _DD3D_WorldWatcher(DebugDraw3D *p_root, uint64_t p_world_id);
65};
66
67#endif
68
116class DebugDraw3D : public Object, public IScopeStorage<DebugDraw3DScopeConfig, DebugDraw3DScopeConfig::Data> {
117 GDCLASS(DebugDraw3D, Object)
118
119 friend DebugDrawManager;
120
121#ifndef DISABLE_DEBUG_RENDERING
122 friend DebugGeometryContainer;
123 friend NodesContainer;
124 friend _DD3D_WorldWatcher;
125#endif
126
127public:
131 enum PointType : int {
132 POINT_TYPE_SQUARE,
133 POINT_TYPE_SPHERE,
134 };
135
136private:
137 static DebugDraw3D *singleton;
138
139 String root_settings_section;
140 const static char *s_use_icosphere;
141 const static char *s_use_icosphere_hd;
142 const static char *s_add_bevel_to_volumetric;
143 const static char *s_default_frustum_scale;
144
145 const static char *s_default_thickness;
146 const static char *s_default_center_brightness;
147 const static char *s_default_hd_spheres;
148 const static char *s_default_plane_size;
149
150 const static char *s_render_priority;
151 const static char *s_render_mode;
152 const static char *s_render_fog_disabled;
153
154 std::vector<SubViewport *> custom_editor_viewports;
155 DebugDrawManager *root_node = nullptr;
156
157 Ref<DebugDraw3DScopeConfig> default_scoped_config;
158
159#ifndef DISABLE_DEBUG_RENDERING
160 ProfiledMutex(std::recursive_mutex, datalock, "3D Geometry lock");
161
162 struct ScopedPairIdConfig {
163 uint64_t id;
165 ScopedPairIdConfig(uint64_t id, DebugDraw3DScopeConfig *scfg) :
166 id(id), scfg(scfg) {}
167 };
168 // stores thread id and array of id's with ptrs
169 std::unordered_map<uint64_t, std::vector<ScopedPairIdConfig> > scoped_configs;
170 // stores thread id and most recent config
171 std::unordered_map<uint64_t, std::shared_ptr<DebugDraw3DScopeConfig::Data> > cached_scoped_configs;
172 uint64_t created_scoped_configs = 0;
173 struct {
174 uint64_t created;
175 uint64_t orphans;
176 } scoped_stats_3d = {};
177
178 // Inherited via IScopeStorage
179 const DebugDraw3DScopeConfig::Data *scoped_config_for_current_thread() override;
180
181 // Meshes
183 std::vector<std::array<Ref<ArrayMesh>, (int)MeshMaterialVariant::MAX> > shared_generated_meshes;
184
186 struct ViewportToDebugContainerItem {
187 uint64_t world_id;
188 _DD3D_WorldWatcher *world_watcher;
189 std::unique_ptr<DebugGeometryContainer> dgcs[(int)MeshMaterialVariant::MAX];
190 std::unique_ptr<NodesContainer> ncs[(int)MeshMaterialVariant::MAX];
191
192 ViewportToDebugContainerItem();
193 ViewportToDebugContainerItem(ViewportToDebugContainerItem &&other) noexcept;
194 ~ViewportToDebugContainerItem();
195 };
196
197 std::unordered_map<uint64_t /* World3D */, ViewportToDebugContainerItem> debug_containers;
198 // invalidate on add/remove operations or use ptrs
199 std::unordered_map<const Viewport *, ViewportToDebugContainerItem *> viewport_to_world_cache;
200 std::unordered_map<uint64_t /*Viewport * */, Ref<World3D> > world3ds_found_for_threads_сache;
201
202 // Default materials and shaders
203 Ref<ShaderMaterial> mesh_shaders[(int)MeshMaterialType::MAX][(int)MeshMaterialVariant::MAX];
204
205 // Inherited via IScopeStorage
206 void _register_scoped_config(uint64_t p_thread_id, uint64_t p_guard_id, DebugDraw3DScopeConfig *p_cfg) override;
207 void _unregister_scoped_config(uint64_t p_thread_id, uint64_t p_guard_id) override;
208 void _clear_scoped_configs() override;
209
210 std::array<Ref<ArrayMesh>, (int)MeshMaterialVariant::MAX> *get_shared_meshes();
211 DebugDraw3D::ViewportToDebugContainerItem *get_debug_container(const DebugDraw3DScopeConfig::DebugContainerDependent &p_dgcd, const bool p_generate_new_container);
212 void _deferred_find_world_in_viewport(uint64_t p_viewport_id);
213 void _register_viewport_world_deferred(uint64_t /*Viewport * */ p_viewport_id, const uint64_t p_world_id, _DD3D_WorldWatcher *watcher);
214 Node *_get_root_world_node(Node *p_scene_root, Viewport *p_vp);
215 void _remove_debug_container(const uint64_t &p_world_id);
216
217 _FORCE_INLINE_ Vector3 get_up_vector(const Vector3 &p_dir);
218 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);
219 Node *get_root_node();
220
221 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);
222
223#ifdef DEV_ENABLED
224 void _save_generated_meshes();
225#endif
226
227#endif
228
229 void init(DebugDrawManager *p_root);
230
231 void set_custom_editor_viewport(std::vector<SubViewport *> p_viewports);
232 std::vector<SubViewport *> get_custom_editor_viewports();
233
234 Ref<ShaderMaterial> get_material_variant(MeshMaterialType p_type, MeshMaterialVariant p_var);
235
236 void _load_materials();
237 inline bool _is_enabled_override() const;
238
239 void process_start(double delta);
240 void process_end(double delta);
241 void physics_process_start(double p_delta);
242 void physics_process_end(double p_delta);
243
244#pragma region Exposed Parameter Values
245
247 bool debug_enabled = true;
248
249 Ref<DebugDraw3DConfig> config;
250
251#pragma endregion // Exposed Parameter Values
252
253protected:
255 static void _bind_methods();
256
257public:
258 DebugDraw3D();
259 ~DebugDraw3D();
260
264 static DebugDraw3D *get_singleton() {
265 return singleton;
266 };
267
268#pragma region Configs
276 Ref<DebugDraw3DScopeConfig> new_scoped_config();
282 Ref<DebugDraw3DScopeConfig> scoped_config() override;
283
287 void set_config(Ref<DebugDraw3DConfig> cfg);
291 Ref<DebugDraw3DConfig> get_config() const;
292
293#pragma endregion // Configs
294
295#pragma region Exposed Parameters
297 void set_empty_color(const Color &col) {};
301 Color get_empty_color() const;
302
306 void set_debug_enabled(const bool &state);
307 bool is_debug_enabled() const;
308
309#pragma endregion // Exposed Parametes
310
311#pragma region Exposed Draw Methods
312
318 Ref<DebugDraw3DStats> get_render_stats();
319
325 Ref<DebugDraw3DStats> get_render_stats_for_world(Viewport *viewport);
326
327#ifndef DISABLE_DEBUG_RENDERING
328#define FAKE_FUNC_IMPL
329#else
330#define FAKE_FUNC_IMPL \
331 { \
332 }
333#endif
334
341
345 void clear_all();
346
347#pragma region Spheres
348
350 void draw_sphere_base(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
361 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;
371 void draw_sphere_xf(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
372
373#pragma endregion // Spheres
374
375#pragma region Cylinders
376
386 void draw_cylinder(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
387
399 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;
400
401#pragma endregion // Cylinders
402
403#pragma region Boxes
404
419 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;
420
435 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;
436
445 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;
446
454 void draw_aabb(const AABB &aabb, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
455
464 void draw_aabb_ab(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
465
466#pragma endregion // Boxes
467
468#pragma region Lines
469
486 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;
487
504 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;
505
506#pragma region Normal
507
509 void draw_lines_c(const std::vector<Vector3> &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
520 void draw_line(const Vector3 &a, const Vector3 &b, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
521
533 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;
534
544 void draw_lines(const PackedVector3Array &lines, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
545
557 void draw_line_path(const PackedVector3Array &path, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
558
559#pragma endregion // Normal
560
561#pragma region Arrows
562
570 void draw_arrowhead(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
571
584 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;
585
597 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;
598
610 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;
611
612#pragma endregion // Arrows
613#pragma region Points
614
629 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;
630
631#pragma endregion // Points
632#pragma endregion // Lines
633
634#pragma region Misc
635
647 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;
648
657 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;
658
671 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;
672
682 void draw_position(const Transform3D &transform, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
683
696 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;
697
711 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;
712
724 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;
725
726#pragma region Camera Frustum
727
729 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;
739 void draw_camera_frustum(const class godot::Camera3D *camera, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
740
748 void draw_camera_frustum_planes(const Array &camera_frustum, const Color &color = Colors::empty_color, const real_t &duration = 0) FAKE_FUNC_IMPL;
749
750#pragma endregion // Camera Frustum
751
752#pragma region Text
768 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;
769
770#pragma endregion // Text
771
772#pragma endregion // Misc
773#pragma endregion // Exposed Draw Methods
774
775#undef FAKE_FUNC_IMPL
776};
777
778VARIANT_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:116
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:131
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:264
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