@@ -187,6 +187,7 @@ NavigationObstacle2D::NavigationObstacle2D() {
187
187
188
188
#ifdef DEBUG_ENABLED
189
189
debug_canvas_item = RenderingServer::get_singleton ()->canvas_item_create ();
190
+ debug_mesh_rid = RenderingServer::get_singleton ()->mesh_create ();
190
191
#endif // DEBUG_ENABLED
191
192
}
192
193
@@ -197,6 +198,10 @@ NavigationObstacle2D::~NavigationObstacle2D() {
197
198
obstacle = RID ();
198
199
199
200
#ifdef DEBUG_ENABLED
201
+ if (debug_mesh_rid.is_valid ()) {
202
+ RenderingServer::get_singleton ()->free (debug_mesh_rid);
203
+ debug_mesh_rid = RID ();
204
+ }
200
205
if (debug_canvas_item.is_valid ()) {
201
206
RenderingServer::get_singleton ()->free (debug_canvas_item);
202
207
debug_canvas_item = RID ();
@@ -206,6 +211,10 @@ NavigationObstacle2D::~NavigationObstacle2D() {
206
211
207
212
void NavigationObstacle2D::set_vertices (const Vector<Vector2> &p_vertices) {
208
213
vertices = p_vertices;
214
+
215
+ vertices_are_clockwise = !Geometry2D::is_polygon_clockwise (vertices); // Geometry2D is inverted.
216
+ vertices_are_valid = !Geometry2D::triangulate_polygon (vertices).is_empty ();
217
+
209
218
const Transform2D node_transform = is_inside_tree () ? get_global_transform () : Transform2D ();
210
219
NavigationServer2D::get_singleton ()->obstacle_set_vertices (obstacle, node_transform.xform (vertices));
211
220
#ifdef DEBUG_ENABLED
@@ -370,43 +379,61 @@ void NavigationObstacle2D::_update_fake_agent_radius_debug() {
370
379
371
380
#ifdef DEBUG_ENABLED
372
381
void NavigationObstacle2D::_update_static_obstacle_debug () {
373
- if (get_vertices ().size () > 2 && NavigationServer2D::get_singleton ()->get_debug_navigation_avoidance_enable_obstacles_static ()) {
374
- bool obstacle_pushes_inward = Geometry2D::is_polygon_clockwise (get_vertices ());
382
+ if (get_vertices ().size () < 3 ) {
383
+ return ;
384
+ }
375
385
376
- Color debug_static_obstacle_face_color;
386
+ if (!NavigationServer2D::get_singleton ()->get_debug_navigation_avoidance_enable_obstacles_static ()) {
387
+ return ;
388
+ }
377
389
378
- if (obstacle_pushes_inward) {
379
- debug_static_obstacle_face_color = NavigationServer2D::get_singleton ()->get_debug_navigation_avoidance_static_obstacle_pushin_face_color ();
380
- } else {
381
- debug_static_obstacle_face_color = NavigationServer2D::get_singleton ()->get_debug_navigation_avoidance_static_obstacle_pushout_face_color ();
382
- }
390
+ RenderingServer *rs = RenderingServer::get_singleton ();
383
391
384
- Vector<Vector2> debug_obstacle_polygon_vertices = get_vertices ( );
392
+ rs-> mesh_clear (debug_mesh_rid );
385
393
386
- Vector<Color> debug_obstacle_polygon_colors;
387
- debug_obstacle_polygon_colors.resize (debug_obstacle_polygon_vertices.size ());
388
- debug_obstacle_polygon_colors.fill (debug_static_obstacle_face_color);
394
+ const int vertex_count = vertices.size ();
389
395
390
- RS::get_singleton ()->canvas_item_add_polygon (debug_canvas_item, get_global_transform ().xform (debug_obstacle_polygon_vertices), debug_obstacle_polygon_colors);
396
+ Vector<Vector2> edge_vertex_array;
397
+ edge_vertex_array.resize (vertex_count * 4 );
391
398
392
- Color debug_static_obstacle_edge_color ;
399
+ Vector2 *edge_vertex_array_ptrw = edge_vertex_array. ptrw () ;
393
400
394
- if (obstacle_pushes_inward) {
395
- debug_static_obstacle_edge_color = NavigationServer2D::get_singleton ()->get_debug_navigation_avoidance_static_obstacle_pushin_edge_color ();
396
- } else {
397
- debug_static_obstacle_edge_color = NavigationServer2D::get_singleton ()->get_debug_navigation_avoidance_static_obstacle_pushout_edge_color ();
398
- }
401
+ int vertex_index = 0 ;
399
402
400
- Vector<Vector2> debug_obstacle_line_vertices = get_vertices ();
401
- debug_obstacle_line_vertices. push_back (debug_obstacle_line_vertices[ 0 ]) ;
402
- debug_obstacle_line_vertices. resize (debug_obstacle_line_vertices. size ()) ;
403
+ for ( int i = 0 ; i < vertex_count; i++) {
404
+ Vector2 point = vertices[i] ;
405
+ Vector2 next_point = vertices[(i + 1 ) % vertex_count] ;
403
406
404
- Vector<Color> debug_obstacle_line_colors ;
405
- debug_obstacle_line_colors. resize (debug_obstacle_line_vertices. size () );
406
- debug_obstacle_line_colors. fill (debug_static_obstacle_edge_color );
407
+ Vector2 direction = next_point. direction_to (point) ;
408
+ Vector2 arrow_dir = -direction. orthogonal ( );
409
+ Vector2 edge_middle = point + ((next_point - point) * 0.5 );
407
410
408
- // Transforming the vertices directly instead of the canvas item in order to not affect the circle shape by non-uniform scales.
409
- RS::get_singleton ()->canvas_item_add_polyline (debug_canvas_item, get_global_transform ().xform (debug_obstacle_line_vertices), debug_obstacle_line_colors, 4.0 );
411
+ edge_vertex_array_ptrw[vertex_index++] = edge_middle;
412
+ edge_vertex_array_ptrw[vertex_index++] = edge_middle + (arrow_dir * 10.0 );
413
+
414
+ edge_vertex_array_ptrw[vertex_index++] = point;
415
+ edge_vertex_array_ptrw[vertex_index++] = next_point;
416
+ }
417
+
418
+ Color debug_static_obstacle_edge_color;
419
+
420
+ if (are_vertices_valid ()) {
421
+ debug_static_obstacle_edge_color = NavigationServer2D::get_singleton ()->get_debug_navigation_avoidance_static_obstacle_pushout_edge_color ();
422
+ } else {
423
+ debug_static_obstacle_edge_color = NavigationServer2D::get_singleton ()->get_debug_navigation_avoidance_static_obstacle_pushin_edge_color ();
410
424
}
425
+
426
+ Vector<Color> line_color_array;
427
+ line_color_array.resize (edge_vertex_array.size ());
428
+ line_color_array.fill (debug_static_obstacle_edge_color);
429
+
430
+ Array edge_mesh_array;
431
+ edge_mesh_array.resize (Mesh::ARRAY_MAX);
432
+ edge_mesh_array[Mesh::ARRAY_VERTEX] = edge_vertex_array;
433
+ edge_mesh_array[Mesh::ARRAY_COLOR] = line_color_array;
434
+
435
+ rs->mesh_add_surface_from_arrays (debug_mesh_rid, RS::PRIMITIVE_LINES, edge_mesh_array, Array (), Dictionary (), RS::ARRAY_FLAG_USE_2D_VERTICES);
436
+
437
+ rs->canvas_item_add_mesh (debug_canvas_item, debug_mesh_rid, get_global_transform ());
411
438
}
412
439
#endif // DEBUG_ENABLED
0 commit comments