diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-06-29 20:36:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-29 20:36:43 +0200 |
commit | a7e589df382ef6a2c2557b1b3cbdcfdce389d7b6 (patch) | |
tree | 34330e7f0ca1f920f50e2bed48500cd61a6d0211 /scene | |
parent | 44516d1a5c858ce0425f2c278a5a0a941dca44be (diff) | |
parent | e12e239ab4a9bb764105e3a0cde284f788cd59b3 (diff) |
Merge pull request #62084 from smix8/path_debug_options_4.x
Add Path2D/3D debug options
Diffstat (limited to 'scene')
-rw-r--r-- | scene/2d/path_2d.cpp | 14 | ||||
-rw-r--r-- | scene/3d/path_3d.cpp | 90 | ||||
-rw-r--r-- | scene/3d/path_3d.h | 11 | ||||
-rw-r--r-- | scene/main/scene_tree.cpp | 46 | ||||
-rw-r--r-- | scene/main/scene_tree.h | 17 |
5 files changed, 169 insertions, 9 deletions
diff --git a/scene/2d/path_2d.cpp b/scene/2d/path_2d.cpp index ba90a275e6..8eb48ffb30 100644 --- a/scene/2d/path_2d.cpp +++ b/scene/2d/path_2d.cpp @@ -87,13 +87,13 @@ bool Path2D::_edit_is_selected_on_click(const Point2 &p_point, double p_toleranc void Path2D::_notification(int p_what) { switch (p_what) { - // Draw the curve if navigation debugging is enabled. + // Draw the curve if path debugging is enabled. case NOTIFICATION_DRAW: { if (!curve.is_valid()) { break; } - if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_navigation_hint()) { + if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_paths_hint()) { return; } @@ -102,12 +102,10 @@ void Path2D::_notification(int p_what) { } #ifdef TOOLS_ENABLED - const real_t line_width = 2 * EDSCALE; + const real_t line_width = get_tree()->get_debug_paths_width() * EDSCALE; #else - const real_t line_width = 2; + const real_t line_width = get_tree()->get_debug_paths_width(); #endif - const Color color = Color(0.5, 0.6, 1.0, 0.7); - _cached_draw_pts.resize(curve->get_point_count() * 8); int count = 0; @@ -119,7 +117,7 @@ void Path2D::_notification(int p_what) { } } - draw_polyline(_cached_draw_pts, color, line_width, true); + draw_polyline(_cached_draw_pts, get_tree()->get_debug_paths_color(), line_width, true); } break; } } @@ -129,7 +127,7 @@ void Path2D::_curve_changed() { return; } - if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_navigation_hint()) { + if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_paths_hint()) { return; } diff --git a/scene/3d/path_3d.cpp b/scene/3d/path_3d.cpp index e13cf6f9c8..f53e783fbd 100644 --- a/scene/3d/path_3d.cpp +++ b/scene/3d/path_3d.cpp @@ -30,6 +30,92 @@ #include "path_3d.h" +Path3D::Path3D() { + SceneTree *st = SceneTree::get_singleton(); + if (st && st->is_debugging_paths_hint()) { + debug_instance = RS::get_singleton()->instance_create(); + set_notify_transform(true); + _update_debug_mesh(); + } +} + +Path3D::~Path3D() { + if (debug_instance.is_valid()) { + RS::get_singleton()->free(debug_instance); + } + if (debug_mesh.is_valid()) { + RS::get_singleton()->free(debug_mesh->get_rid()); + } +} + +void Path3D::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + SceneTree *st = SceneTree::get_singleton(); + if (st && st->is_debugging_paths_hint()) { + _update_debug_mesh(); + } + } break; + + case NOTIFICATION_EXIT_TREE: { + SceneTree *st = SceneTree::get_singleton(); + if (st && st->is_debugging_paths_hint()) { + RS::get_singleton()->instance_set_visible(debug_instance, false); + } + } break; + + case NOTIFICATION_TRANSFORM_CHANGED: { + if (is_inside_tree() && debug_instance.is_valid()) { + RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform()); + } + } break; + } +} + +void Path3D::_update_debug_mesh() { + SceneTree *st = SceneTree::get_singleton(); + if (!(st && st->is_debugging_paths_hint())) { + return; + } + + if (!debug_mesh.is_valid()) { + debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); + } + + if (!(curve.is_valid())) { + RS::get_singleton()->instance_set_visible(debug_instance, false); + return; + } + if (curve->get_point_count() < 2) { + RS::get_singleton()->instance_set_visible(debug_instance, false); + return; + } + + Vector<Vector3> vertex_array; + + for (int i = 1; i < curve->get_point_count(); i++) { + Vector3 line_end = curve->get_point_position(i); + Vector3 line_start = curve->get_point_position(i - 1); + vertex_array.push_back(line_start); + vertex_array.push_back(line_end); + } + + Array mesh_array; + mesh_array.resize(Mesh::ARRAY_MAX); + mesh_array[Mesh::ARRAY_VERTEX] = vertex_array; + + debug_mesh->clear_surfaces(); + debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, mesh_array); + + RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid()); + RS::get_singleton()->mesh_surface_set_material(debug_mesh->get_rid(), 0, st->get_debug_paths_material()->get_rid()); + if (is_inside_tree()) { + RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario()); + RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform()); + RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree()); + } +} + void Path3D::_curve_changed() { if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) { update_gizmos(); @@ -48,6 +134,10 @@ void Path3D::_curve_changed() { } } } + SceneTree *st = SceneTree::get_singleton(); + if (st && st->is_debugging_paths_hint()) { + _update_debug_mesh(); + } } void Path3D::set_curve(const Ref<Curve3D> &p_curve) { diff --git a/scene/3d/path_3d.h b/scene/3d/path_3d.h index cb67a044d1..7c7284534e 100644 --- a/scene/3d/path_3d.h +++ b/scene/3d/path_3d.h @@ -41,14 +41,23 @@ class Path3D : public Node3D { void _curve_changed(); + RID debug_instance; + Ref<ArrayMesh> debug_mesh; + +private: + void _update_debug_mesh(); + protected: + void _notification(int p_what); + static void _bind_methods(); public: void set_curve(const Ref<Curve3D> &p_curve); Ref<Curve3D> get_curve() const; - Path3D() {} + Path3D(); + ~Path3D(); }; class PathFollow3D : public Node3D { diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp index dd77877c7d..18f69ecc82 100644 --- a/scene/main/scene_tree.cpp +++ b/scene/main/scene_tree.cpp @@ -659,6 +659,14 @@ bool SceneTree::is_debugging_collisions_hint() const { return debug_collisions_hint; } +void SceneTree::set_debug_paths_hint(bool p_enabled) { + debug_paths_hint = p_enabled; +} + +bool SceneTree::is_debugging_paths_hint() const { + return debug_paths_hint; +} + void SceneTree::set_debug_navigation_hint(bool p_enabled) { debug_navigation_hint = p_enabled; } @@ -684,6 +692,22 @@ Color SceneTree::get_debug_collision_contact_color() const { return debug_collision_contact_color; } +void SceneTree::set_debug_paths_color(const Color &p_color) { + debug_paths_color = p_color; +} + +Color SceneTree::get_debug_paths_color() const { + return debug_paths_color; +} + +void SceneTree::set_debug_paths_width(float p_width) { + debug_paths_width = p_width; +} + +float SceneTree::get_debug_paths_width() const { + return debug_paths_width; +} + void SceneTree::set_debug_navigation_color(const Color &p_color) { debug_navigation_color = p_color; } @@ -700,6 +724,23 @@ Color SceneTree::get_debug_navigation_disabled_color() const { return debug_navigation_disabled_color; } +Ref<Material> SceneTree::get_debug_paths_material() { + if (debug_paths_material.is_valid()) { + return debug_paths_material; + } + + Ref<StandardMaterial3D> _debug_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D)); + _debug_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); + _debug_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); + _debug_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); + _debug_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); + _debug_material->set_albedo(get_debug_paths_color()); + + debug_paths_material = _debug_material; + + return debug_paths_material; +} + Ref<Material> SceneTree::get_debug_navigation_material() { if (navigation_material.is_valid()) { return navigation_material; @@ -1207,6 +1248,8 @@ void SceneTree::_bind_methods() { ClassDB::bind_method(D_METHOD("set_debug_collisions_hint", "enable"), &SceneTree::set_debug_collisions_hint); ClassDB::bind_method(D_METHOD("is_debugging_collisions_hint"), &SceneTree::is_debugging_collisions_hint); + ClassDB::bind_method(D_METHOD("set_debug_paths_hint", "enable"), &SceneTree::set_debug_paths_hint); + ClassDB::bind_method(D_METHOD("is_debugging_paths_hint"), &SceneTree::is_debugging_paths_hint); ClassDB::bind_method(D_METHOD("set_debug_navigation_hint", "enable"), &SceneTree::set_debug_navigation_hint); ClassDB::bind_method(D_METHOD("is_debugging_navigation_hint"), &SceneTree::is_debugging_navigation_hint); @@ -1268,6 +1311,7 @@ void SceneTree::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_accept_quit"), "set_auto_accept_quit", "is_auto_accept_quit"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "quit_on_go_back"), "set_quit_on_go_back", "is_quit_on_go_back"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_collisions_hint"), "set_debug_collisions_hint", "is_debugging_collisions_hint"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_paths_hint"), "set_debug_paths_hint", "is_debugging_paths_hint"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_navigation_hint"), "set_debug_navigation_hint", "is_debugging_navigation_hint"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_pause", "is_paused"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_edited_scene_root", "get_edited_scene_root"); @@ -1344,6 +1388,8 @@ SceneTree::SceneTree() { } debug_collisions_color = GLOBAL_DEF("debug/shapes/collision/shape_color", Color(0.0, 0.6, 0.7, 0.42)); debug_collision_contact_color = GLOBAL_DEF("debug/shapes/collision/contact_color", Color(1.0, 0.2, 0.1, 0.8)); + debug_paths_color = GLOBAL_DEF("debug/shapes/paths/geometry_color", Color(0.1, 1.0, 0.7, 0.4)); + debug_paths_width = GLOBAL_DEF("debug/shapes/paths/geometry_width", 2.0); debug_navigation_color = GLOBAL_DEF("debug/shapes/navigation/geometry_color", Color(0.1, 1.0, 0.7, 0.4)); debug_navigation_disabled_color = GLOBAL_DEF("debug/shapes/navigation/disabled_geometry_color", Color(1.0, 0.7, 0.1, 0.4)); collision_debug_contacts = GLOBAL_DEF("debug/shapes/collision/max_contacts_displayed", 10000); diff --git a/scene/main/scene_tree.h b/scene/main/scene_tree.h index 67a17a69f2..a34aa8e2cd 100644 --- a/scene/main/scene_tree.h +++ b/scene/main/scene_tree.h @@ -97,6 +97,7 @@ private: #ifdef DEBUG_ENABLED bool debug_collisions_hint = false; + bool debug_paths_hint = false; bool debug_navigation_hint = false; #endif bool paused = false; @@ -146,9 +147,12 @@ private: Color debug_collisions_color; Color debug_collision_contact_color; + Color debug_paths_color; + float debug_paths_width = 1.0f; Color debug_navigation_color; Color debug_navigation_disabled_color; Ref<ArrayMesh> debug_contact_mesh; + Ref<Material> debug_paths_material; Ref<Material> navigation_material; Ref<Material> navigation_disabled_material; Ref<Material> collision_material; @@ -297,12 +301,18 @@ public: void set_debug_collisions_hint(bool p_enabled); bool is_debugging_collisions_hint() const; + void set_debug_paths_hint(bool p_enabled); + bool is_debugging_paths_hint() const; + void set_debug_navigation_hint(bool p_enabled); bool is_debugging_navigation_hint() const; #else void set_debug_collisions_hint(bool p_enabled) {} bool is_debugging_collisions_hint() const { return false; } + void set_debug_paths_hint(bool p_enabled) {} + bool is_debugging_paths_hint() const { return false; } + void set_debug_navigation_hint(bool p_enabled) {} bool is_debugging_navigation_hint() const { return false; } #endif @@ -313,12 +323,19 @@ public: void set_debug_collision_contact_color(const Color &p_color); Color get_debug_collision_contact_color() const; + void set_debug_paths_color(const Color &p_color); + Color get_debug_paths_color() const; + + void set_debug_paths_width(float p_width); + float get_debug_paths_width() const; + void set_debug_navigation_color(const Color &p_color); Color get_debug_navigation_color() const; void set_debug_navigation_disabled_color(const Color &p_color); Color get_debug_navigation_disabled_color() const; + Ref<Material> get_debug_paths_material(); Ref<Material> get_debug_navigation_material(); Ref<Material> get_debug_navigation_disabled_material(); Ref<Material> get_debug_collision_material(); |