From 2594c573613707ca6b1f7a6c96e8cb5331413c4f Mon Sep 17 00:00:00 2001 From: smix8 <52464204+smix8@users.noreply.github.com> Date: Sat, 20 Apr 2024 13:15:55 +0200 Subject: [PATCH] Add NavigationMeshSourceGeometryData append functions Adds append functions to NavigationMeshSourceGeometryData. --- .../NavigationMeshSourceGeometryData2D.xml | 14 ++++++++++ .../NavigationMeshSourceGeometryData3D.xml | 8 ++++++ ...avigation_mesh_source_geometry_data_2d.cpp | 21 +++++++++++++++ .../navigation_mesh_source_geometry_data_2d.h | 3 +++ ...avigation_mesh_source_geometry_data_3d.cpp | 26 +++++++++++++------ .../navigation_mesh_source_geometry_data_3d.h | 2 ++ 6 files changed, 66 insertions(+), 8 deletions(-) diff --git a/doc/classes/NavigationMeshSourceGeometryData2D.xml b/doc/classes/NavigationMeshSourceGeometryData2D.xml index 609877fadce..1d8689420bd 100644 --- a/doc/classes/NavigationMeshSourceGeometryData2D.xml +++ b/doc/classes/NavigationMeshSourceGeometryData2D.xml @@ -31,6 +31,20 @@ Adds the outline points of a shape as traversable area. + + + + + Appends another array of [param obstruction_outlines] at the end of the existing obstruction outlines array. + + + + + + + Appends another array of [param traversable_outlines] at the end of the existing traversable outlines array. + + diff --git a/doc/classes/NavigationMeshSourceGeometryData3D.xml b/doc/classes/NavigationMeshSourceGeometryData3D.xml index 322e2e7c66c..0b3126a63bf 100644 --- a/doc/classes/NavigationMeshSourceGeometryData3D.xml +++ b/doc/classes/NavigationMeshSourceGeometryData3D.xml @@ -43,6 +43,14 @@ Adds a projected obstruction shape to the source geometry. The [param vertices] are considered projected on a xz-axes plane, placed at the global y-axis [param elevation] and extruded by [param height]. If [param carve] is [code]true[/code] the carved shape will not be affected by additional offsets (e.g. agent radius) of the navigation mesh baking process. + + + + + + Appends arrays of [param vertices] and [param indices] at the end of the existing arrays. Adds the existing index as an offset to the appended indices. + + diff --git a/scene/resources/2d/navigation_mesh_source_geometry_data_2d.cpp b/scene/resources/2d/navigation_mesh_source_geometry_data_2d.cpp index d613b498a12..aee743ccf24 100644 --- a/scene/resources/2d/navigation_mesh_source_geometry_data_2d.cpp +++ b/scene/resources/2d/navigation_mesh_source_geometry_data_2d.cpp @@ -97,6 +97,24 @@ TypedArray> NavigationMeshSourceGeometryData2D::get_obstruction_ return typed_array_obstruction_outlines; } +void NavigationMeshSourceGeometryData2D::append_traversable_outlines(const TypedArray> &p_traversable_outlines) { + RWLockWrite write_lock(geometry_rwlock); + int traversable_outlines_size = traversable_outlines.size(); + traversable_outlines.resize(traversable_outlines_size + p_traversable_outlines.size()); + for (int i = traversable_outlines_size; i < p_traversable_outlines.size(); i++) { + traversable_outlines.write[i] = p_traversable_outlines[i]; + } +} + +void NavigationMeshSourceGeometryData2D::append_obstruction_outlines(const TypedArray> &p_obstruction_outlines) { + RWLockWrite write_lock(geometry_rwlock); + int obstruction_outlines_size = obstruction_outlines.size(); + obstruction_outlines.resize(obstruction_outlines_size + p_obstruction_outlines.size()); + for (int i = obstruction_outlines_size; i < p_obstruction_outlines.size(); i++) { + obstruction_outlines.write[i] = p_obstruction_outlines[i]; + } +} + void NavigationMeshSourceGeometryData2D::add_traversable_outline(const PackedVector2Array &p_shape_outline) { if (p_shape_outline.size() > 1) { Vector traversable_outline; @@ -240,6 +258,9 @@ void NavigationMeshSourceGeometryData2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_obstruction_outlines", "obstruction_outlines"), &NavigationMeshSourceGeometryData2D::set_obstruction_outlines); ClassDB::bind_method(D_METHOD("get_obstruction_outlines"), &NavigationMeshSourceGeometryData2D::get_obstruction_outlines); + ClassDB::bind_method(D_METHOD("append_traversable_outlines", "traversable_outlines"), &NavigationMeshSourceGeometryData2D::append_traversable_outlines); + ClassDB::bind_method(D_METHOD("append_obstruction_outlines", "obstruction_outlines"), &NavigationMeshSourceGeometryData2D::append_obstruction_outlines); + ClassDB::bind_method(D_METHOD("add_traversable_outline", "shape_outline"), &NavigationMeshSourceGeometryData2D::add_traversable_outline); ClassDB::bind_method(D_METHOD("add_obstruction_outline", "shape_outline"), &NavigationMeshSourceGeometryData2D::add_obstruction_outline); diff --git a/scene/resources/2d/navigation_mesh_source_geometry_data_2d.h b/scene/resources/2d/navigation_mesh_source_geometry_data_2d.h index 11fc5d38505..aaa02ab40ec 100644 --- a/scene/resources/2d/navigation_mesh_source_geometry_data_2d.h +++ b/scene/resources/2d/navigation_mesh_source_geometry_data_2d.h @@ -82,6 +82,9 @@ public: void set_obstruction_outlines(const TypedArray> &p_obstruction_outlines); TypedArray> get_obstruction_outlines() const; + void append_traversable_outlines(const TypedArray> &p_traversable_outlines); + void append_obstruction_outlines(const TypedArray> &p_obstruction_outlines); + void add_traversable_outline(const PackedVector2Array &p_shape_outline); void add_obstruction_outline(const PackedVector2Array &p_shape_outline); diff --git a/scene/resources/3d/navigation_mesh_source_geometry_data_3d.cpp b/scene/resources/3d/navigation_mesh_source_geometry_data_3d.cpp index 39a17946fa8..5d920af400a 100644 --- a/scene/resources/3d/navigation_mesh_source_geometry_data_3d.cpp +++ b/scene/resources/3d/navigation_mesh_source_geometry_data_3d.cpp @@ -35,9 +35,24 @@ void NavigationMeshSourceGeometryData3D::set_vertices(const Vector &p_ver } void NavigationMeshSourceGeometryData3D::set_indices(const Vector &p_indices) { + ERR_FAIL_COND(vertices.size() < p_indices.size()); indices = p_indices; } +void NavigationMeshSourceGeometryData3D::append_arrays(const Vector &p_vertices, const Vector &p_indices) { + RWLockWrite write_lock(geometry_rwlock); + + const int64_t number_of_vertices_before_merge = vertices.size(); + const int64_t number_of_indices_before_merge = indices.size(); + + vertices.append_array(p_vertices); + indices.append_array(p_indices); + + for (int64_t i = number_of_indices_before_merge; i < indices.size(); i++) { + indices.set(i, indices[i] + number_of_vertices_before_merge / 3); + } +} + void NavigationMeshSourceGeometryData3D::clear() { vertices.clear(); indices.clear(); @@ -174,14 +189,7 @@ void NavigationMeshSourceGeometryData3D::add_faces(const PackedVector3Array &p_f void NavigationMeshSourceGeometryData3D::merge(const Ref &p_other_geometry) { ERR_FAIL_NULL(p_other_geometry); - // No need to worry about `root_node_transform` here as the vertices are already xformed. - const int64_t number_of_vertices_before_merge = vertices.size(); - const int64_t number_of_indices_before_merge = indices.size(); - vertices.append_array(p_other_geometry->vertices); - indices.append_array(p_other_geometry->indices); - for (int64_t i = number_of_indices_before_merge; i < indices.size(); i++) { - indices.set(i, indices[i] + number_of_vertices_before_merge / 3); - } + append_arrays(p_other_geometry->vertices, p_other_geometry->indices); if (p_other_geometry->_projected_obstructions.size() > 0) { RWLockWrite write_lock(geometry_rwlock); @@ -306,6 +314,8 @@ void NavigationMeshSourceGeometryData3D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_indices", "indices"), &NavigationMeshSourceGeometryData3D::set_indices); ClassDB::bind_method(D_METHOD("get_indices"), &NavigationMeshSourceGeometryData3D::get_indices); + ClassDB::bind_method(D_METHOD("append_arrays", "vertices", "indices"), &NavigationMeshSourceGeometryData3D::append_arrays); + ClassDB::bind_method(D_METHOD("clear"), &NavigationMeshSourceGeometryData3D::clear); ClassDB::bind_method(D_METHOD("has_data"), &NavigationMeshSourceGeometryData3D::has_data); diff --git a/scene/resources/3d/navigation_mesh_source_geometry_data_3d.h b/scene/resources/3d/navigation_mesh_source_geometry_data_3d.h index 79e2f3740d6..6c1ca760ea7 100644 --- a/scene/resources/3d/navigation_mesh_source_geometry_data_3d.h +++ b/scene/resources/3d/navigation_mesh_source_geometry_data_3d.h @@ -80,6 +80,8 @@ public: void set_indices(const Vector &p_indices); const Vector &get_indices() const { return indices; } + void append_arrays(const Vector &p_vertices, const Vector &p_indices); + bool has_data() { return vertices.size() && indices.size(); }; void clear(); void clear_projected_obstructions();