Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/navigation/navigation_region_2d.cpp
9904 views
1
/**************************************************************************/
2
/* navigation_region_2d.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "navigation_region_2d.h"
32
33
#include "core/math/random_pcg.h"
34
#include "scene/resources/world_2d.h"
35
#include "servers/navigation_server_2d.h"
36
37
RID NavigationRegion2D::get_rid() const {
38
return region;
39
}
40
41
void NavigationRegion2D::set_enabled(bool p_enabled) {
42
if (enabled == p_enabled) {
43
return;
44
}
45
46
enabled = p_enabled;
47
48
NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
49
50
#ifdef DEBUG_ENABLED
51
if (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_navigation_enabled()) {
52
queue_redraw();
53
}
54
#endif // DEBUG_ENABLED
55
}
56
57
bool NavigationRegion2D::is_enabled() const {
58
return enabled;
59
}
60
61
void NavigationRegion2D::set_use_edge_connections(bool p_enabled) {
62
if (use_edge_connections == p_enabled) {
63
return;
64
}
65
66
use_edge_connections = p_enabled;
67
68
NavigationServer2D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
69
}
70
71
bool NavigationRegion2D::get_use_edge_connections() const {
72
return use_edge_connections;
73
}
74
75
void NavigationRegion2D::set_navigation_layers(uint32_t p_navigation_layers) {
76
if (navigation_layers == p_navigation_layers) {
77
return;
78
}
79
80
navigation_layers = p_navigation_layers;
81
82
NavigationServer2D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
83
}
84
85
uint32_t NavigationRegion2D::get_navigation_layers() const {
86
return navigation_layers;
87
}
88
89
void NavigationRegion2D::set_navigation_layer_value(int p_layer_number, bool p_value) {
90
ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
91
ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
92
93
uint32_t _navigation_layers = get_navigation_layers();
94
95
if (p_value) {
96
_navigation_layers |= 1 << (p_layer_number - 1);
97
} else {
98
_navigation_layers &= ~(1 << (p_layer_number - 1));
99
}
100
101
set_navigation_layers(_navigation_layers);
102
}
103
104
bool NavigationRegion2D::get_navigation_layer_value(int p_layer_number) const {
105
ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
106
ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
107
108
return get_navigation_layers() & (1 << (p_layer_number - 1));
109
}
110
111
void NavigationRegion2D::set_enter_cost(real_t p_enter_cost) {
112
ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
113
if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
114
return;
115
}
116
117
enter_cost = p_enter_cost;
118
119
NavigationServer2D::get_singleton()->region_set_enter_cost(region, enter_cost);
120
}
121
122
real_t NavigationRegion2D::get_enter_cost() const {
123
return enter_cost;
124
}
125
126
void NavigationRegion2D::set_travel_cost(real_t p_travel_cost) {
127
ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
128
if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
129
return;
130
}
131
132
travel_cost = p_travel_cost;
133
134
NavigationServer2D::get_singleton()->region_set_travel_cost(region, travel_cost);
135
}
136
137
real_t NavigationRegion2D::get_travel_cost() const {
138
return travel_cost;
139
}
140
141
RID NavigationRegion2D::get_region_rid() const {
142
return get_rid();
143
}
144
145
#ifdef DEBUG_ENABLED
146
Rect2 NavigationRegion2D::_edit_get_rect() const {
147
return navigation_polygon.is_valid() ? navigation_polygon->_edit_get_rect() : Rect2();
148
}
149
150
bool NavigationRegion2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
151
return navigation_polygon.is_valid() ? navigation_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;
152
}
153
#endif // DEBUG_ENABLED
154
155
void NavigationRegion2D::_notification(int p_what) {
156
switch (p_what) {
157
case NOTIFICATION_ENTER_TREE: {
158
_region_enter_navigation_map();
159
} break;
160
161
case NOTIFICATION_TRANSFORM_CHANGED: {
162
set_physics_process_internal(true);
163
} break;
164
165
case NOTIFICATION_VISIBILITY_CHANGED: {
166
#ifdef DEBUG_ENABLED
167
_set_debug_visible(is_visible_in_tree());
168
#endif // DEBUG_ENABLED
169
} break;
170
171
case NOTIFICATION_EXIT_TREE: {
172
_region_exit_navigation_map();
173
#ifdef DEBUG_ENABLED
174
_set_debug_visible(false);
175
#endif // DEBUG_ENABLED
176
} break;
177
178
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
179
set_physics_process_internal(false);
180
_region_update_transform();
181
} break;
182
183
case NOTIFICATION_DRAW: {
184
#ifdef DEBUG_ENABLED
185
if (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()) {
186
_update_debug_mesh();
187
_update_debug_edge_connections_mesh();
188
_update_debug_baking_rect();
189
}
190
#endif // DEBUG_ENABLED
191
} break;
192
}
193
}
194
195
void NavigationRegion2D::set_navigation_polygon(const Ref<NavigationPolygon> &p_navigation_polygon) {
196
if (navigation_polygon.is_valid()) {
197
navigation_polygon->disconnect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));
198
}
199
200
navigation_polygon = p_navigation_polygon;
201
202
if (navigation_polygon.is_valid()) {
203
navigation_polygon->connect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));
204
}
205
206
_navigation_polygon_changed();
207
}
208
209
Ref<NavigationPolygon> NavigationRegion2D::get_navigation_polygon() const {
210
return navigation_polygon;
211
}
212
213
void NavigationRegion2D::set_navigation_map(RID p_navigation_map) {
214
if (map_override == p_navigation_map) {
215
return;
216
}
217
218
map_override = p_navigation_map;
219
220
NavigationServer2D::get_singleton()->region_set_map(region, map_override);
221
}
222
223
RID NavigationRegion2D::get_navigation_map() const {
224
if (map_override.is_valid()) {
225
return map_override;
226
} else if (is_inside_tree()) {
227
return get_world_2d()->get_navigation_map();
228
}
229
return RID();
230
}
231
232
void NavigationRegion2D::bake_navigation_polygon(bool p_on_thread) {
233
ERR_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().");
234
ERR_FAIL_COND_MSG(navigation_polygon.is_null(), "Baking the navigation polygon requires a valid `NavigationPolygon` resource.");
235
236
Ref<NavigationMeshSourceGeometryData2D> source_geometry_data;
237
source_geometry_data.instantiate();
238
239
NavigationServer2D::get_singleton()->parse_source_geometry_data(navigation_polygon, source_geometry_data, this);
240
241
if (p_on_thread) {
242
NavigationServer2D::get_singleton()->bake_from_source_geometry_data_async(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished));
243
} else {
244
NavigationServer2D::get_singleton()->bake_from_source_geometry_data(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished));
245
}
246
}
247
248
void NavigationRegion2D::_bake_finished() {
249
if (!Thread::is_main_thread()) {
250
callable_mp(this, &NavigationRegion2D::_bake_finished).call_deferred();
251
return;
252
}
253
254
emit_signal(SNAME("bake_finished"));
255
}
256
257
bool NavigationRegion2D::is_baking() const {
258
return NavigationServer2D::get_singleton()->is_baking_navigation_polygon(navigation_polygon);
259
}
260
261
void NavigationRegion2D::_navigation_polygon_changed() {
262
_update_bounds();
263
264
NavigationServer2D::get_singleton()->region_set_navigation_polygon(region, navigation_polygon);
265
266
#ifdef DEBUG_ENABLED
267
debug_mesh_dirty = true;
268
269
if (navigation_polygon.is_null()) {
270
_set_debug_visible(false);
271
}
272
273
if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
274
queue_redraw();
275
}
276
#endif // DEBUG_ENABLED
277
278
emit_signal(SNAME("navigation_polygon_changed"));
279
280
update_configuration_warnings();
281
}
282
283
#ifdef DEBUG_ENABLED
284
void NavigationRegion2D::_navigation_map_changed(RID p_map) {
285
if (is_inside_tree() && get_world_2d()->get_navigation_map() == p_map) {
286
queue_redraw();
287
}
288
}
289
#endif // DEBUG_ENABLED
290
291
#ifdef DEBUG_ENABLED
292
void NavigationRegion2D::_navigation_debug_changed() {
293
if (is_inside_tree()) {
294
queue_redraw();
295
}
296
}
297
#endif // DEBUG_ENABLED
298
299
PackedStringArray NavigationRegion2D::get_configuration_warnings() const {
300
PackedStringArray warnings = Node2D::get_configuration_warnings();
301
302
if (is_visible_in_tree() && is_inside_tree()) {
303
if (navigation_polygon.is_null()) {
304
warnings.push_back(RTR("A NavigationPolygon resource must be set or created for this node to work. Please set a property or draw a polygon."));
305
}
306
}
307
308
return warnings;
309
}
310
311
void NavigationRegion2D::_bind_methods() {
312
ClassDB::bind_method(D_METHOD("get_rid"), &NavigationRegion2D::get_rid);
313
314
ClassDB::bind_method(D_METHOD("set_navigation_polygon", "navigation_polygon"), &NavigationRegion2D::set_navigation_polygon);
315
ClassDB::bind_method(D_METHOD("get_navigation_polygon"), &NavigationRegion2D::get_navigation_polygon);
316
317
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion2D::set_enabled);
318
ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion2D::is_enabled);
319
320
ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion2D::set_navigation_map);
321
ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion2D::get_navigation_map);
322
323
ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion2D::set_use_edge_connections);
324
ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion2D::get_use_edge_connections);
325
326
ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion2D::set_navigation_layers);
327
ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion2D::get_navigation_layers);
328
329
ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion2D::set_navigation_layer_value);
330
ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion2D::get_navigation_layer_value);
331
332
ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion2D::get_region_rid);
333
334
ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion2D::set_enter_cost);
335
ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion2D::get_enter_cost);
336
337
ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion2D::set_travel_cost);
338
ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion2D::get_travel_cost);
339
340
ClassDB::bind_method(D_METHOD("bake_navigation_polygon", "on_thread"), &NavigationRegion2D::bake_navigation_polygon, DEFVAL(true));
341
ClassDB::bind_method(D_METHOD("is_baking"), &NavigationRegion2D::is_baking);
342
343
ClassDB::bind_method(D_METHOD("_navigation_polygon_changed"), &NavigationRegion2D::_navigation_polygon_changed);
344
345
ClassDB::bind_method(D_METHOD("get_bounds"), &NavigationRegion2D::get_bounds);
346
347
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_polygon", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), "set_navigation_polygon", "get_navigation_polygon");
348
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
349
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections");
350
ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
351
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
352
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
353
354
ADD_SIGNAL(MethodInfo("navigation_polygon_changed"));
355
ADD_SIGNAL(MethodInfo("bake_finished"));
356
}
357
358
#ifndef DISABLE_DEPRECATED
359
// Compatibility with earlier 4.0 betas.
360
bool NavigationRegion2D::_set(const StringName &p_name, const Variant &p_value) {
361
if (p_name == "navpoly") {
362
set_navigation_polygon(p_value);
363
return true;
364
}
365
return false;
366
}
367
368
bool NavigationRegion2D::_get(const StringName &p_name, Variant &r_ret) const {
369
if (p_name == "navpoly") {
370
r_ret = get_navigation_polygon();
371
return true;
372
}
373
return false;
374
}
375
#endif // DISABLE_DEPRECATED
376
377
NavigationRegion2D::NavigationRegion2D() {
378
set_notify_transform(true);
379
set_hide_clip_children(true);
380
381
region = NavigationServer2D::get_singleton()->region_create();
382
NavigationServer2D::get_singleton()->region_set_owner_id(region, get_instance_id());
383
NavigationServer2D::get_singleton()->region_set_enter_cost(region, get_enter_cost());
384
NavigationServer2D::get_singleton()->region_set_travel_cost(region, get_travel_cost());
385
NavigationServer2D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
386
NavigationServer2D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
387
NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
388
389
#ifdef DEBUG_ENABLED
390
NavigationServer2D::get_singleton()->connect(SNAME("map_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));
391
NavigationServer2D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion2D::_navigation_debug_changed));
392
#endif // DEBUG_ENABLED
393
}
394
395
NavigationRegion2D::~NavigationRegion2D() {
396
ERR_FAIL_NULL(NavigationServer2D::get_singleton());
397
NavigationServer2D::get_singleton()->free(region);
398
399
#ifdef DEBUG_ENABLED
400
NavigationServer2D::get_singleton()->disconnect(SNAME("map_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));
401
NavigationServer2D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion2D::_navigation_debug_changed));
402
if (debug_instance_rid.is_valid()) {
403
RS::get_singleton()->free(debug_instance_rid);
404
}
405
if (debug_mesh_rid.is_valid()) {
406
RS::get_singleton()->free(debug_mesh_rid);
407
}
408
#endif // DEBUG_ENABLED
409
}
410
411
void NavigationRegion2D::_region_enter_navigation_map() {
412
if (!is_inside_tree()) {
413
return;
414
}
415
416
if (map_override.is_valid()) {
417
NavigationServer2D::get_singleton()->region_set_map(region, map_override);
418
} else {
419
NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
420
}
421
422
current_global_transform = get_global_transform();
423
NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);
424
425
NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
426
427
queue_redraw();
428
}
429
430
void NavigationRegion2D::_region_exit_navigation_map() {
431
NavigationServer2D::get_singleton()->region_set_map(region, RID());
432
}
433
434
void NavigationRegion2D::_region_update_transform() {
435
if (!is_inside_tree()) {
436
return;
437
}
438
439
Transform2D new_global_transform = get_global_transform();
440
if (current_global_transform != new_global_transform) {
441
current_global_transform = new_global_transform;
442
NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);
443
}
444
445
queue_redraw();
446
}
447
448
#ifdef DEBUG_ENABLED
449
void NavigationRegion2D::_update_debug_mesh() {
450
if (!is_inside_tree()) {
451
_set_debug_visible(false);
452
return;
453
}
454
455
const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
456
RenderingServer *rs = RenderingServer::get_singleton();
457
458
if (!debug_instance_rid.is_valid()) {
459
debug_instance_rid = rs->canvas_item_create();
460
}
461
if (!debug_mesh_rid.is_valid()) {
462
debug_mesh_rid = rs->mesh_create();
463
}
464
465
const Transform2D region_gt = get_global_transform();
466
467
rs->canvas_item_set_parent(debug_instance_rid, get_world_2d()->get_canvas());
468
rs->canvas_item_set_z_index(debug_instance_rid, RS::CANVAS_ITEM_Z_MAX - 2);
469
rs->canvas_item_set_transform(debug_instance_rid, region_gt);
470
471
if (!debug_mesh_dirty) {
472
return;
473
}
474
475
rs->canvas_item_clear(debug_instance_rid);
476
rs->mesh_clear(debug_mesh_rid);
477
debug_mesh_dirty = false;
478
479
const Vector<Vector2> &vertices = navigation_polygon->get_vertices();
480
if (vertices.size() < 3) {
481
return;
482
}
483
484
int polygon_count = navigation_polygon->get_polygon_count();
485
if (polygon_count == 0) {
486
return;
487
}
488
489
bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();
490
bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
491
492
Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();
493
Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();
494
495
if (!enabled) {
496
debug_face_color = ns2d->get_debug_navigation_geometry_face_disabled_color();
497
debug_edge_color = ns2d->get_debug_navigation_geometry_edge_disabled_color();
498
}
499
500
int vertex_count = 0;
501
int line_count = 0;
502
503
for (int i = 0; i < polygon_count; i++) {
504
const Vector<int> &polygon = navigation_polygon->get_polygon(i);
505
int polygon_size = polygon.size();
506
if (polygon_size < 3) {
507
continue;
508
}
509
line_count += polygon_size * 2;
510
vertex_count += (polygon_size - 2) * 3;
511
}
512
513
Vector<Vector2> face_vertex_array;
514
face_vertex_array.resize(vertex_count);
515
516
Vector<Color> face_color_array;
517
if (enabled_geometry_face_random_color) {
518
face_color_array.resize(vertex_count);
519
}
520
521
Vector<Vector2> line_vertex_array;
522
if (enabled_edge_lines) {
523
line_vertex_array.resize(line_count);
524
}
525
526
RandomPCG rand;
527
Color polygon_color = debug_face_color;
528
529
int face_vertex_index = 0;
530
int line_vertex_index = 0;
531
532
Vector2 *face_vertex_array_ptrw = face_vertex_array.ptrw();
533
Color *face_color_array_ptrw = face_color_array.ptrw();
534
Vector2 *line_vertex_array_ptrw = line_vertex_array.ptrw();
535
536
for (int polygon_index = 0; polygon_index < polygon_count; polygon_index++) {
537
const Vector<int> &polygon_indices = navigation_polygon->get_polygon(polygon_index);
538
int polygon_indices_size = polygon_indices.size();
539
if (polygon_indices_size < 3) {
540
continue;
541
}
542
543
if (enabled_geometry_face_random_color) {
544
// Generate the polygon color, slightly randomly modified from the settings one.
545
polygon_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);
546
polygon_color.a = debug_face_color.a;
547
}
548
549
for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size - 2; polygon_indices_index++) {
550
face_vertex_array_ptrw[face_vertex_index] = vertices[polygon_indices[0]];
551
face_vertex_array_ptrw[face_vertex_index + 1] = vertices[polygon_indices[polygon_indices_index + 1]];
552
face_vertex_array_ptrw[face_vertex_index + 2] = vertices[polygon_indices[polygon_indices_index + 2]];
553
if (enabled_geometry_face_random_color) {
554
face_color_array_ptrw[face_vertex_index] = polygon_color;
555
face_color_array_ptrw[face_vertex_index + 1] = polygon_color;
556
face_color_array_ptrw[face_vertex_index + 2] = polygon_color;
557
}
558
face_vertex_index += 3;
559
}
560
561
if (enabled_edge_lines) {
562
for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size; polygon_indices_index++) {
563
line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index]];
564
line_vertex_index += 1;
565
if (polygon_indices_index + 1 == polygon_indices_size) {
566
line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[0]];
567
line_vertex_index += 1;
568
} else {
569
line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index + 1]];
570
line_vertex_index += 1;
571
}
572
}
573
}
574
}
575
576
if (!enabled_geometry_face_random_color) {
577
face_color_array.resize(face_vertex_array.size());
578
face_color_array.fill(debug_face_color);
579
}
580
581
Array face_mesh_array;
582
face_mesh_array.resize(Mesh::ARRAY_MAX);
583
face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
584
face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
585
586
rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_TRIANGLES, face_mesh_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
587
588
if (enabled_edge_lines) {
589
Vector<Color> line_color_array;
590
line_color_array.resize(line_vertex_array.size());
591
line_color_array.fill(debug_edge_color);
592
593
Array line_mesh_array;
594
line_mesh_array.resize(Mesh::ARRAY_MAX);
595
line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array;
596
line_mesh_array[Mesh::ARRAY_COLOR] = line_color_array;
597
598
rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, line_mesh_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
599
}
600
601
rs->canvas_item_add_mesh(debug_instance_rid, debug_mesh_rid, Transform2D());
602
rs->canvas_item_set_visible(debug_instance_rid, is_visible_in_tree());
603
}
604
#endif // DEBUG_ENABLED
605
606
#ifdef DEBUG_ENABLED
607
void NavigationRegion2D::_update_debug_edge_connections_mesh() {
608
const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
609
bool 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());
610
611
if (enable_edge_connections) {
612
Color debug_edge_connection_color = ns2d->get_debug_navigation_edge_connection_color();
613
// Draw the region edge connections.
614
Transform2D xform = get_global_transform();
615
real_t radius = ns2d->map_get_edge_connection_margin(get_world_2d()->get_navigation_map()) / 2.0;
616
for (int i = 0; i < ns2d->region_get_connections_count(region); i++) {
617
// Two main points
618
Vector2 a = ns2d->region_get_connection_pathway_start(region, i);
619
a = xform.affine_inverse().xform(a);
620
Vector2 b = ns2d->region_get_connection_pathway_end(region, i);
621
b = xform.affine_inverse().xform(b);
622
draw_line(a, b, debug_edge_connection_color);
623
624
// Draw a circle to illustrate the margins.
625
real_t angle = a.angle_to_point(b);
626
draw_arc(a, radius, angle + Math::PI / 2.0, angle - Math::PI / 2.0 + Math::TAU, 10, debug_edge_connection_color);
627
draw_arc(b, radius, angle - Math::PI / 2.0, angle + Math::PI / 2.0, 10, debug_edge_connection_color);
628
}
629
}
630
}
631
#endif // DEBUG_ENABLED
632
633
#ifdef DEBUG_ENABLED
634
void NavigationRegion2D::_update_debug_baking_rect() {
635
Rect2 baking_rect = get_navigation_polygon()->get_baking_rect();
636
if (baking_rect.has_area()) {
637
Vector2 baking_rect_offset = get_navigation_polygon()->get_baking_rect_offset();
638
Rect2 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);
639
Color debug_baking_rect_color = Color(0.8, 0.5, 0.7, 0.1);
640
draw_rect(debug_baking_rect, debug_baking_rect_color);
641
}
642
}
643
#endif // DEBUG_ENABLED
644
645
#ifdef DEBUG_ENABLED
646
void NavigationRegion2D::_set_debug_visible(bool p_visible) {
647
RenderingServer *rs = RenderingServer::get_singleton();
648
ERR_FAIL_NULL(rs);
649
if (debug_instance_rid.is_valid()) {
650
RS::get_singleton()->canvas_item_set_visible(debug_instance_rid, p_visible);
651
}
652
}
653
#endif // DEBUG_ENABLED
654
655
void NavigationRegion2D::_update_bounds() {
656
if (navigation_polygon.is_null()) {
657
bounds = Rect2();
658
return;
659
}
660
661
const Vector<Vector2> &vertices = navigation_polygon->get_vertices();
662
if (vertices.is_empty()) {
663
bounds = Rect2();
664
return;
665
}
666
667
const Transform2D gt = is_inside_tree() ? get_global_transform() : get_transform();
668
669
Rect2 new_bounds;
670
new_bounds.position = gt.xform(vertices[0]);
671
672
for (const Vector2 &vertex : vertices) {
673
new_bounds.expand_to(gt.xform(vertex));
674
}
675
bounds = new_bounds;
676
}
677
678