Ported the improvements to the NavigationMesh from the NavigationMeshGenerator rework pr.

This commit is contained in:
Relintai 2023-06-05 19:20:28 +02:00
parent c72a079c49
commit a3aebe8bc5
3 changed files with 80 additions and 6 deletions

View File

@ -70,6 +70,8 @@ void NavigationMesh::create_from_mesh(const Ref<Mesh> &p_mesh) {
add_polygon(vi); add_polygon(vi);
} }
} }
navigation_mesh_dirty = true;
} }
void NavigationMesh::set_sample_partition_type(SamplePartitionType p_value) { void NavigationMesh::set_sample_partition_type(SamplePartitionType p_value) {
@ -321,7 +323,7 @@ Array NavigationMesh::_get_polygons() const {
void NavigationMesh::add_polygon(const Vector<int> &p_polygon) { void NavigationMesh::add_polygon(const Vector<int> &p_polygon) {
polygons.push_back(p_polygon); polygons.push_back(p_polygon);
_change_notify(); navigation_mesh_dirty = true;
} }
int NavigationMesh::get_polygon_count() const { int NavigationMesh::get_polygon_count() const {
return polygons.size(); return polygons.size();
@ -332,6 +334,52 @@ Vector<int> NavigationMesh::get_polygon(int p_idx) {
} }
void NavigationMesh::clear_polygons() { void NavigationMesh::clear_polygons() {
polygons.clear(); polygons.clear();
navigation_mesh_dirty = true;
}
void NavigationMesh::clear() {
polygons.clear();
vertices.clear();
navigation_mesh_dirty = true;
}
void NavigationMesh::commit_changes() {
// Function to synchronize the navigation mesh with the NavigationServer
// Placeholder function that gets fleshed out with PR 'Add NavigationServer Navmesh and NavMesh Instances'
// Function was already added so users and tutorials and documentation only have to adapt once to the procedural workflow changes
if (navigation_mesh_dirty) {
navigation_mesh_dirty = false;
/*
Vector<Vector3> new_navmesh_vertices;
Vector<Vector<int32_t>> new_navmesh_polygons;
new_navmesh_vertices.resize(vertices.size());
new_navmesh_polygons.resize(polygons.size());
Vector3 *new_navmesh_vertices_ptrw = new_navmesh_vertices.ptrw();
Vector<int32_t> *new_navmesh_polygons_ptrw = new_navmesh_polygons.ptrw();
for (int i = 0; i < vertices.size(); i++) {
new_navmesh_vertices_ptrw[i] = vertices[i];
}
for (int i = 0; i < polygons.size(); i++) {
new_navmesh_polygons_ptrw[i] = polygons[i];
}
NavigationServer3D::get_singleton()->navmesh_set_data(navmesh_rid, new_navmesh_vertices, new_navmesh_polygons);
*/
emit_changed();
}
}
void NavigationMesh::set_polygons(const Vector<Vector<int>> &p_polygons) {
polygons = p_polygons;
navigation_mesh_dirty = true;
}
const Vector<Vector<int>> &NavigationMesh::get_polygons() const {
return polygons;
}
RID NavigationMesh::get_rid() const {
if (navmesh_rid.is_valid()) {
return navmesh_rid;
}
return RID();
} }
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
@ -510,6 +558,9 @@ void NavigationMesh::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_from_mesh", "mesh"), &NavigationMesh::create_from_mesh); ClassDB::bind_method(D_METHOD("create_from_mesh", "mesh"), &NavigationMesh::create_from_mesh);
ClassDB::bind_method(D_METHOD("clear"), &NavigationMesh::clear);
ClassDB::bind_method(D_METHOD("commit_changes"), &NavigationMesh::commit_changes);
ClassDB::bind_method(D_METHOD("_set_polygons", "polygons"), &NavigationMesh::_set_polygons); ClassDB::bind_method(D_METHOD("_set_polygons", "polygons"), &NavigationMesh::_set_polygons);
ClassDB::bind_method(D_METHOD("_get_polygons"), &NavigationMesh::_get_polygons); ClassDB::bind_method(D_METHOD("_get_polygons"), &NavigationMesh::_get_polygons);
@ -612,4 +663,14 @@ NavigationMesh::NavigationMesh() {
filter_low_hanging_obstacles = false; filter_low_hanging_obstacles = false;
filter_ledge_spans = false; filter_ledge_spans = false;
filter_walkable_low_height_spans = false; filter_walkable_low_height_spans = false;
navigation_mesh_dirty = true;
//navmesh_rid = NavigationServer3D::get_singleton()->navmesh_create();
call_deferred("commit_changes");
} }
NavigationMesh::~NavigationMesh() {
//ERR_FAIL_NULL(NavigationServer::get_singleton());
//NavigationServer::get_singleton()->free(navmesh_rid);
}

View File

@ -40,10 +40,16 @@ class NavigationMesh : public Resource {
friend class NavigationMeshGenerator; friend class NavigationMeshGenerator;
RID navmesh_rid;
PoolVector<Vector3> vertices; PoolVector<Vector3> vertices;
Vector<Vector<int>> polygons; Vector<Vector<int>> polygons;
Ref<ArrayMesh> debug_mesh; Ref<ArrayMesh> debug_mesh;
bool navigation_mesh_dirty;
protected: protected:
static void _bind_methods(); static void _bind_methods();
virtual void _validate_property(PropertyInfo &property) const; virtual void _validate_property(PropertyInfo &property) const;
@ -185,11 +191,20 @@ public:
Vector<int> get_polygon(int p_idx); Vector<int> get_polygon(int p_idx);
void clear_polygons(); void clear_polygons();
void clear();
void commit_changes();
void set_polygons(const Vector<Vector<int>> &p_polygons);
const Vector<Vector<int>> &get_polygons() const;
RID get_rid() const;
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
Ref<ArrayMesh> get_debug_mesh(); Ref<ArrayMesh> get_debug_mesh();
#endif #endif
NavigationMesh(); NavigationMesh();
~NavigationMesh();
}; };
VARIANT_ENUM_CAST(NavigationMesh::SamplePartitionType); VARIANT_ENUM_CAST(NavigationMesh::SamplePartitionType);

View File

@ -37,6 +37,7 @@
#include "scene/resources/navigation_mesh.h" #include "scene/resources/navigation_mesh.h"
#include "scene/resources/world_2d.h" #include "scene/resources/world_2d.h"
#include "servers/navigation_2d_server.h" #include "servers/navigation_2d_server.h"
#include "core/core_string_names.h"
#include "thirdparty/misc/triangulator.h" #include "thirdparty/misc/triangulator.h"
@ -232,14 +233,11 @@ void NavigationPolygon::commit_changes() {
for (int i = 0; i < get_polygon_count(); i++) { for (int i = 0; i < get_polygon_count(); i++) {
Vector<int> new_navigation_mesh_polygon = get_polygon(i); Vector<int> new_navigation_mesh_polygon = get_polygon(i);
new_navigation_mesh_polygons.push_back(new_navigation_mesh_polygon); new_navigation_mesh_polygons.push_back(new_navigation_mesh_polygon);
//OLD, remove:
navigation_mesh->add_polygon(get_polygon(i));
} }
navigation_mesh->set_vertices(new_navigation_mesh_vertices); navigation_mesh->set_vertices(new_navigation_mesh_vertices);
// New, enable: navigation_mesh->set_polygons(new_navigation_mesh_polygons);
//navigation_mesh->set_polygons(new_navigation_mesh_polygons); navigation_mesh->commit_changes();
//navigation_mesh->commit_changes();
emit_changed(); emit_changed();
} }