Path: blob/master/scene/2d/navigation/navigation_region_2d.cpp
9904 views
/**************************************************************************/1/* navigation_region_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "navigation_region_2d.h"3132#include "core/math/random_pcg.h"33#include "scene/resources/world_2d.h"34#include "servers/navigation_server_2d.h"3536RID NavigationRegion2D::get_rid() const {37return region;38}3940void NavigationRegion2D::set_enabled(bool p_enabled) {41if (enabled == p_enabled) {42return;43}4445enabled = p_enabled;4647NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);4849#ifdef DEBUG_ENABLED50if (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_navigation_enabled()) {51queue_redraw();52}53#endif // DEBUG_ENABLED54}5556bool NavigationRegion2D::is_enabled() const {57return enabled;58}5960void NavigationRegion2D::set_use_edge_connections(bool p_enabled) {61if (use_edge_connections == p_enabled) {62return;63}6465use_edge_connections = p_enabled;6667NavigationServer2D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);68}6970bool NavigationRegion2D::get_use_edge_connections() const {71return use_edge_connections;72}7374void NavigationRegion2D::set_navigation_layers(uint32_t p_navigation_layers) {75if (navigation_layers == p_navigation_layers) {76return;77}7879navigation_layers = p_navigation_layers;8081NavigationServer2D::get_singleton()->region_set_navigation_layers(region, navigation_layers);82}8384uint32_t NavigationRegion2D::get_navigation_layers() const {85return navigation_layers;86}8788void NavigationRegion2D::set_navigation_layer_value(int p_layer_number, bool p_value) {89ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");90ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");9192uint32_t _navigation_layers = get_navigation_layers();9394if (p_value) {95_navigation_layers |= 1 << (p_layer_number - 1);96} else {97_navigation_layers &= ~(1 << (p_layer_number - 1));98}99100set_navigation_layers(_navigation_layers);101}102103bool NavigationRegion2D::get_navigation_layer_value(int p_layer_number) const {104ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");105ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");106107return get_navigation_layers() & (1 << (p_layer_number - 1));108}109110void NavigationRegion2D::set_enter_cost(real_t p_enter_cost) {111ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");112if (Math::is_equal_approx(enter_cost, p_enter_cost)) {113return;114}115116enter_cost = p_enter_cost;117118NavigationServer2D::get_singleton()->region_set_enter_cost(region, enter_cost);119}120121real_t NavigationRegion2D::get_enter_cost() const {122return enter_cost;123}124125void NavigationRegion2D::set_travel_cost(real_t p_travel_cost) {126ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");127if (Math::is_equal_approx(travel_cost, p_travel_cost)) {128return;129}130131travel_cost = p_travel_cost;132133NavigationServer2D::get_singleton()->region_set_travel_cost(region, travel_cost);134}135136real_t NavigationRegion2D::get_travel_cost() const {137return travel_cost;138}139140RID NavigationRegion2D::get_region_rid() const {141return get_rid();142}143144#ifdef DEBUG_ENABLED145Rect2 NavigationRegion2D::_edit_get_rect() const {146return navigation_polygon.is_valid() ? navigation_polygon->_edit_get_rect() : Rect2();147}148149bool NavigationRegion2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {150return navigation_polygon.is_valid() ? navigation_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;151}152#endif // DEBUG_ENABLED153154void NavigationRegion2D::_notification(int p_what) {155switch (p_what) {156case NOTIFICATION_ENTER_TREE: {157_region_enter_navigation_map();158} break;159160case NOTIFICATION_TRANSFORM_CHANGED: {161set_physics_process_internal(true);162} break;163164case NOTIFICATION_VISIBILITY_CHANGED: {165#ifdef DEBUG_ENABLED166_set_debug_visible(is_visible_in_tree());167#endif // DEBUG_ENABLED168} break;169170case NOTIFICATION_EXIT_TREE: {171_region_exit_navigation_map();172#ifdef DEBUG_ENABLED173_set_debug_visible(false);174#endif // DEBUG_ENABLED175} break;176177case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {178set_physics_process_internal(false);179_region_update_transform();180} break;181182case NOTIFICATION_DRAW: {183#ifdef DEBUG_ENABLED184if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || (NavigationServer2D::get_singleton()->get_debug_enabled() && NavigationServer2D::get_singleton()->get_debug_navigation_enabled())) && navigation_polygon.is_valid()) {185_update_debug_mesh();186_update_debug_edge_connections_mesh();187_update_debug_baking_rect();188}189#endif // DEBUG_ENABLED190} break;191}192}193194void NavigationRegion2D::set_navigation_polygon(const Ref<NavigationPolygon> &p_navigation_polygon) {195if (navigation_polygon.is_valid()) {196navigation_polygon->disconnect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));197}198199navigation_polygon = p_navigation_polygon;200201if (navigation_polygon.is_valid()) {202navigation_polygon->connect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));203}204205_navigation_polygon_changed();206}207208Ref<NavigationPolygon> NavigationRegion2D::get_navigation_polygon() const {209return navigation_polygon;210}211212void NavigationRegion2D::set_navigation_map(RID p_navigation_map) {213if (map_override == p_navigation_map) {214return;215}216217map_override = p_navigation_map;218219NavigationServer2D::get_singleton()->region_set_map(region, map_override);220}221222RID NavigationRegion2D::get_navigation_map() const {223if (map_override.is_valid()) {224return map_override;225} else if (is_inside_tree()) {226return get_world_2d()->get_navigation_map();227}228return RID();229}230231void NavigationRegion2D::bake_navigation_polygon(bool p_on_thread) {232ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "The SceneTree can only be parsed on the main thread. Call this function from the main thread or use call_deferred().");233ERR_FAIL_COND_MSG(navigation_polygon.is_null(), "Baking the navigation polygon requires a valid `NavigationPolygon` resource.");234235Ref<NavigationMeshSourceGeometryData2D> source_geometry_data;236source_geometry_data.instantiate();237238NavigationServer2D::get_singleton()->parse_source_geometry_data(navigation_polygon, source_geometry_data, this);239240if (p_on_thread) {241NavigationServer2D::get_singleton()->bake_from_source_geometry_data_async(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished));242} else {243NavigationServer2D::get_singleton()->bake_from_source_geometry_data(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished));244}245}246247void NavigationRegion2D::_bake_finished() {248if (!Thread::is_main_thread()) {249callable_mp(this, &NavigationRegion2D::_bake_finished).call_deferred();250return;251}252253emit_signal(SNAME("bake_finished"));254}255256bool NavigationRegion2D::is_baking() const {257return NavigationServer2D::get_singleton()->is_baking_navigation_polygon(navigation_polygon);258}259260void NavigationRegion2D::_navigation_polygon_changed() {261_update_bounds();262263NavigationServer2D::get_singleton()->region_set_navigation_polygon(region, navigation_polygon);264265#ifdef DEBUG_ENABLED266debug_mesh_dirty = true;267268if (navigation_polygon.is_null()) {269_set_debug_visible(false);270}271272if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {273queue_redraw();274}275#endif // DEBUG_ENABLED276277emit_signal(SNAME("navigation_polygon_changed"));278279update_configuration_warnings();280}281282#ifdef DEBUG_ENABLED283void NavigationRegion2D::_navigation_map_changed(RID p_map) {284if (is_inside_tree() && get_world_2d()->get_navigation_map() == p_map) {285queue_redraw();286}287}288#endif // DEBUG_ENABLED289290#ifdef DEBUG_ENABLED291void NavigationRegion2D::_navigation_debug_changed() {292if (is_inside_tree()) {293queue_redraw();294}295}296#endif // DEBUG_ENABLED297298PackedStringArray NavigationRegion2D::get_configuration_warnings() const {299PackedStringArray warnings = Node2D::get_configuration_warnings();300301if (is_visible_in_tree() && is_inside_tree()) {302if (navigation_polygon.is_null()) {303warnings.push_back(RTR("A NavigationPolygon resource must be set or created for this node to work. Please set a property or draw a polygon."));304}305}306307return warnings;308}309310void NavigationRegion2D::_bind_methods() {311ClassDB::bind_method(D_METHOD("get_rid"), &NavigationRegion2D::get_rid);312313ClassDB::bind_method(D_METHOD("set_navigation_polygon", "navigation_polygon"), &NavigationRegion2D::set_navigation_polygon);314ClassDB::bind_method(D_METHOD("get_navigation_polygon"), &NavigationRegion2D::get_navigation_polygon);315316ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion2D::set_enabled);317ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion2D::is_enabled);318319ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion2D::set_navigation_map);320ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion2D::get_navigation_map);321322ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion2D::set_use_edge_connections);323ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion2D::get_use_edge_connections);324325ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion2D::set_navigation_layers);326ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion2D::get_navigation_layers);327328ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion2D::set_navigation_layer_value);329ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion2D::get_navigation_layer_value);330331ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion2D::get_region_rid);332333ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion2D::set_enter_cost);334ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion2D::get_enter_cost);335336ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion2D::set_travel_cost);337ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion2D::get_travel_cost);338339ClassDB::bind_method(D_METHOD("bake_navigation_polygon", "on_thread"), &NavigationRegion2D::bake_navigation_polygon, DEFVAL(true));340ClassDB::bind_method(D_METHOD("is_baking"), &NavigationRegion2D::is_baking);341342ClassDB::bind_method(D_METHOD("_navigation_polygon_changed"), &NavigationRegion2D::_navigation_polygon_changed);343344ClassDB::bind_method(D_METHOD("get_bounds"), &NavigationRegion2D::get_bounds);345346ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_polygon", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), "set_navigation_polygon", "get_navigation_polygon");347ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");348ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections");349ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");350ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");351ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");352353ADD_SIGNAL(MethodInfo("navigation_polygon_changed"));354ADD_SIGNAL(MethodInfo("bake_finished"));355}356357#ifndef DISABLE_DEPRECATED358// Compatibility with earlier 4.0 betas.359bool NavigationRegion2D::_set(const StringName &p_name, const Variant &p_value) {360if (p_name == "navpoly") {361set_navigation_polygon(p_value);362return true;363}364return false;365}366367bool NavigationRegion2D::_get(const StringName &p_name, Variant &r_ret) const {368if (p_name == "navpoly") {369r_ret = get_navigation_polygon();370return true;371}372return false;373}374#endif // DISABLE_DEPRECATED375376NavigationRegion2D::NavigationRegion2D() {377set_notify_transform(true);378set_hide_clip_children(true);379380region = NavigationServer2D::get_singleton()->region_create();381NavigationServer2D::get_singleton()->region_set_owner_id(region, get_instance_id());382NavigationServer2D::get_singleton()->region_set_enter_cost(region, get_enter_cost());383NavigationServer2D::get_singleton()->region_set_travel_cost(region, get_travel_cost());384NavigationServer2D::get_singleton()->region_set_navigation_layers(region, navigation_layers);385NavigationServer2D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);386NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);387388#ifdef DEBUG_ENABLED389NavigationServer2D::get_singleton()->connect(SNAME("map_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));390NavigationServer2D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion2D::_navigation_debug_changed));391#endif // DEBUG_ENABLED392}393394NavigationRegion2D::~NavigationRegion2D() {395ERR_FAIL_NULL(NavigationServer2D::get_singleton());396NavigationServer2D::get_singleton()->free(region);397398#ifdef DEBUG_ENABLED399NavigationServer2D::get_singleton()->disconnect(SNAME("map_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));400NavigationServer2D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion2D::_navigation_debug_changed));401if (debug_instance_rid.is_valid()) {402RS::get_singleton()->free(debug_instance_rid);403}404if (debug_mesh_rid.is_valid()) {405RS::get_singleton()->free(debug_mesh_rid);406}407#endif // DEBUG_ENABLED408}409410void NavigationRegion2D::_region_enter_navigation_map() {411if (!is_inside_tree()) {412return;413}414415if (map_override.is_valid()) {416NavigationServer2D::get_singleton()->region_set_map(region, map_override);417} else {418NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());419}420421current_global_transform = get_global_transform();422NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);423424NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);425426queue_redraw();427}428429void NavigationRegion2D::_region_exit_navigation_map() {430NavigationServer2D::get_singleton()->region_set_map(region, RID());431}432433void NavigationRegion2D::_region_update_transform() {434if (!is_inside_tree()) {435return;436}437438Transform2D new_global_transform = get_global_transform();439if (current_global_transform != new_global_transform) {440current_global_transform = new_global_transform;441NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);442}443444queue_redraw();445}446447#ifdef DEBUG_ENABLED448void NavigationRegion2D::_update_debug_mesh() {449if (!is_inside_tree()) {450_set_debug_visible(false);451return;452}453454const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();455RenderingServer *rs = RenderingServer::get_singleton();456457if (!debug_instance_rid.is_valid()) {458debug_instance_rid = rs->canvas_item_create();459}460if (!debug_mesh_rid.is_valid()) {461debug_mesh_rid = rs->mesh_create();462}463464const Transform2D region_gt = get_global_transform();465466rs->canvas_item_set_parent(debug_instance_rid, get_world_2d()->get_canvas());467rs->canvas_item_set_z_index(debug_instance_rid, RS::CANVAS_ITEM_Z_MAX - 2);468rs->canvas_item_set_transform(debug_instance_rid, region_gt);469470if (!debug_mesh_dirty) {471return;472}473474rs->canvas_item_clear(debug_instance_rid);475rs->mesh_clear(debug_mesh_rid);476debug_mesh_dirty = false;477478const Vector<Vector2> &vertices = navigation_polygon->get_vertices();479if (vertices.size() < 3) {480return;481}482483int polygon_count = navigation_polygon->get_polygon_count();484if (polygon_count == 0) {485return;486}487488bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();489bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();490491Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();492Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();493494if (!enabled) {495debug_face_color = ns2d->get_debug_navigation_geometry_face_disabled_color();496debug_edge_color = ns2d->get_debug_navigation_geometry_edge_disabled_color();497}498499int vertex_count = 0;500int line_count = 0;501502for (int i = 0; i < polygon_count; i++) {503const Vector<int> &polygon = navigation_polygon->get_polygon(i);504int polygon_size = polygon.size();505if (polygon_size < 3) {506continue;507}508line_count += polygon_size * 2;509vertex_count += (polygon_size - 2) * 3;510}511512Vector<Vector2> face_vertex_array;513face_vertex_array.resize(vertex_count);514515Vector<Color> face_color_array;516if (enabled_geometry_face_random_color) {517face_color_array.resize(vertex_count);518}519520Vector<Vector2> line_vertex_array;521if (enabled_edge_lines) {522line_vertex_array.resize(line_count);523}524525RandomPCG rand;526Color polygon_color = debug_face_color;527528int face_vertex_index = 0;529int line_vertex_index = 0;530531Vector2 *face_vertex_array_ptrw = face_vertex_array.ptrw();532Color *face_color_array_ptrw = face_color_array.ptrw();533Vector2 *line_vertex_array_ptrw = line_vertex_array.ptrw();534535for (int polygon_index = 0; polygon_index < polygon_count; polygon_index++) {536const Vector<int> &polygon_indices = navigation_polygon->get_polygon(polygon_index);537int polygon_indices_size = polygon_indices.size();538if (polygon_indices_size < 3) {539continue;540}541542if (enabled_geometry_face_random_color) {543// Generate the polygon color, slightly randomly modified from the settings one.544polygon_color.set_hsv(debug_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, debug_face_color.get_s(), debug_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);545polygon_color.a = debug_face_color.a;546}547548for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size - 2; polygon_indices_index++) {549face_vertex_array_ptrw[face_vertex_index] = vertices[polygon_indices[0]];550face_vertex_array_ptrw[face_vertex_index + 1] = vertices[polygon_indices[polygon_indices_index + 1]];551face_vertex_array_ptrw[face_vertex_index + 2] = vertices[polygon_indices[polygon_indices_index + 2]];552if (enabled_geometry_face_random_color) {553face_color_array_ptrw[face_vertex_index] = polygon_color;554face_color_array_ptrw[face_vertex_index + 1] = polygon_color;555face_color_array_ptrw[face_vertex_index + 2] = polygon_color;556}557face_vertex_index += 3;558}559560if (enabled_edge_lines) {561for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size; polygon_indices_index++) {562line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index]];563line_vertex_index += 1;564if (polygon_indices_index + 1 == polygon_indices_size) {565line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[0]];566line_vertex_index += 1;567} else {568line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index + 1]];569line_vertex_index += 1;570}571}572}573}574575if (!enabled_geometry_face_random_color) {576face_color_array.resize(face_vertex_array.size());577face_color_array.fill(debug_face_color);578}579580Array face_mesh_array;581face_mesh_array.resize(Mesh::ARRAY_MAX);582face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;583face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;584585rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_TRIANGLES, face_mesh_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);586587if (enabled_edge_lines) {588Vector<Color> line_color_array;589line_color_array.resize(line_vertex_array.size());590line_color_array.fill(debug_edge_color);591592Array line_mesh_array;593line_mesh_array.resize(Mesh::ARRAY_MAX);594line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array;595line_mesh_array[Mesh::ARRAY_COLOR] = line_color_array;596597rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, line_mesh_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);598}599600rs->canvas_item_add_mesh(debug_instance_rid, debug_mesh_rid, Transform2D());601rs->canvas_item_set_visible(debug_instance_rid, is_visible_in_tree());602}603#endif // DEBUG_ENABLED604605#ifdef DEBUG_ENABLED606void NavigationRegion2D::_update_debug_edge_connections_mesh() {607const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();608bool enable_edge_connections = use_edge_connections && ns2d->get_debug_navigation_enable_edge_connections() && ns2d->map_get_use_edge_connections(get_world_2d()->get_navigation_map());609610if (enable_edge_connections) {611Color debug_edge_connection_color = ns2d->get_debug_navigation_edge_connection_color();612// Draw the region edge connections.613Transform2D xform = get_global_transform();614real_t radius = ns2d->map_get_edge_connection_margin(get_world_2d()->get_navigation_map()) / 2.0;615for (int i = 0; i < ns2d->region_get_connections_count(region); i++) {616// Two main points617Vector2 a = ns2d->region_get_connection_pathway_start(region, i);618a = xform.affine_inverse().xform(a);619Vector2 b = ns2d->region_get_connection_pathway_end(region, i);620b = xform.affine_inverse().xform(b);621draw_line(a, b, debug_edge_connection_color);622623// Draw a circle to illustrate the margins.624real_t angle = a.angle_to_point(b);625draw_arc(a, radius, angle + Math::PI / 2.0, angle - Math::PI / 2.0 + Math::TAU, 10, debug_edge_connection_color);626draw_arc(b, radius, angle - Math::PI / 2.0, angle + Math::PI / 2.0, 10, debug_edge_connection_color);627}628}629}630#endif // DEBUG_ENABLED631632#ifdef DEBUG_ENABLED633void NavigationRegion2D::_update_debug_baking_rect() {634Rect2 baking_rect = get_navigation_polygon()->get_baking_rect();635if (baking_rect.has_area()) {636Vector2 baking_rect_offset = get_navigation_polygon()->get_baking_rect_offset();637Rect2 debug_baking_rect = Rect2(baking_rect.position.x + baking_rect_offset.x, baking_rect.position.y + baking_rect_offset.y, baking_rect.size.x, baking_rect.size.y);638Color debug_baking_rect_color = Color(0.8, 0.5, 0.7, 0.1);639draw_rect(debug_baking_rect, debug_baking_rect_color);640}641}642#endif // DEBUG_ENABLED643644#ifdef DEBUG_ENABLED645void NavigationRegion2D::_set_debug_visible(bool p_visible) {646RenderingServer *rs = RenderingServer::get_singleton();647ERR_FAIL_NULL(rs);648if (debug_instance_rid.is_valid()) {649RS::get_singleton()->canvas_item_set_visible(debug_instance_rid, p_visible);650}651}652#endif // DEBUG_ENABLED653654void NavigationRegion2D::_update_bounds() {655if (navigation_polygon.is_null()) {656bounds = Rect2();657return;658}659660const Vector<Vector2> &vertices = navigation_polygon->get_vertices();661if (vertices.is_empty()) {662bounds = Rect2();663return;664}665666const Transform2D gt = is_inside_tree() ? get_global_transform() : get_transform();667668Rect2 new_bounds;669new_bounds.position = gt.xform(vertices[0]);670671for (const Vector2 &vertex : vertices) {672new_bounds.expand_to(gt.xform(vertex));673}674bounds = new_bounds;675}676677678