Merge pull request #90935 from smix8/navdata_append

Add NavigationMeshSourceGeometryData append functions
This commit is contained in:
Rémi Verschelde 2024-04-22 12:53:32 +02:00
commit bfca90acb7
No known key found for this signature in database
GPG key ID: C3336907360768E1
6 changed files with 66 additions and 8 deletions

View file

@ -31,6 +31,20 @@
Adds the outline points of a shape as traversable area.
</description>
</method>
<method name="append_obstruction_outlines">
<return type="void" />
<param index="0" name="obstruction_outlines" type="PackedVector2Array[]" />
<description>
Appends another array of [param obstruction_outlines] at the end of the existing obstruction outlines array.
</description>
</method>
<method name="append_traversable_outlines">
<return type="void" />
<param index="0" name="traversable_outlines" type="PackedVector2Array[]" />
<description>
Appends another array of [param traversable_outlines] at the end of the existing traversable outlines array.
</description>
</method>
<method name="clear">
<return type="void" />
<description>

View file

@ -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.
</description>
</method>
<method name="append_arrays">
<return type="void" />
<param index="0" name="vertices" type="PackedFloat32Array" />
<param index="1" name="indices" type="PackedInt32Array" />
<description>
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.
</description>
</method>
<method name="clear">
<return type="void" />
<description>

View file

@ -97,6 +97,24 @@ TypedArray<Vector<Vector2>> NavigationMeshSourceGeometryData2D::get_obstruction_
return typed_array_obstruction_outlines;
}
void NavigationMeshSourceGeometryData2D::append_traversable_outlines(const TypedArray<Vector<Vector2>> &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<Vector<Vector2>> &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<Vector2> 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);

View file

@ -82,6 +82,9 @@ public:
void set_obstruction_outlines(const TypedArray<Vector<Vector2>> &p_obstruction_outlines);
TypedArray<Vector<Vector2>> get_obstruction_outlines() const;
void append_traversable_outlines(const TypedArray<Vector<Vector2>> &p_traversable_outlines);
void append_obstruction_outlines(const TypedArray<Vector<Vector2>> &p_obstruction_outlines);
void add_traversable_outline(const PackedVector2Array &p_shape_outline);
void add_obstruction_outline(const PackedVector2Array &p_shape_outline);

View file

@ -35,9 +35,24 @@ void NavigationMeshSourceGeometryData3D::set_vertices(const Vector<float> &p_ver
}
void NavigationMeshSourceGeometryData3D::set_indices(const Vector<int> &p_indices) {
ERR_FAIL_COND(vertices.size() < p_indices.size());
indices = p_indices;
}
void NavigationMeshSourceGeometryData3D::append_arrays(const Vector<float> &p_vertices, const Vector<int> &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<NavigationMeshSourceGeometryData3D> &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);

View file

@ -80,6 +80,8 @@ public:
void set_indices(const Vector<int> &p_indices);
const Vector<int> &get_indices() const { return indices; }
void append_arrays(const Vector<float> &p_vertices, const Vector<int> &p_indices);
bool has_data() { return vertices.size() && indices.size(); };
void clear();
void clear_projected_obstructions();