Merge pull request #87715 from Scony/add-is-baking

Expose `is_baking` method in navigation servers and region nodes.
This commit is contained in:
Rémi Verschelde 2024-02-07 10:58:27 +01:00
commit 251d5b3669
No known key found for this signature in database
GPG Key ID: C3336907360768E1
24 changed files with 137 additions and 12 deletions

View File

@ -56,6 +56,12 @@
Returns the [RID] of this region on the [NavigationServer2D]. Combined with [method NavigationServer2D.map_get_closest_point_owner] can be used to identify the [NavigationRegion2D] closest to a point on the merged navigation map.
</description>
</method>
<method name="is_baking" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] when the [NavigationPolygon] is being baked on a background thread.
</description>
</method>
<method name="set_avoidance_layer_value">
<return type="void" />
<param index="0" name="layer_number" type="int" />

View File

@ -49,6 +49,12 @@
Returns the [RID] of this region on the [NavigationServer3D]. Combined with [method NavigationServer3D.map_get_closest_point_owner] can be used to identify the [NavigationRegion3D] closest to a point on the merged navigation map.
</description>
</method>
<method name="is_baking" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] when the [NavigationMesh] is being baked on a background thread.
</description>
</method>
<method name="set_navigation_layer_value">
<return type="void" />
<param index="0" name="layer_number" type="int" />

View File

@ -303,6 +303,13 @@
Returns all created navigation map [RID]s on the NavigationServer. This returns both 2D and 3D created navigation maps as there is technically no distinction between them.
</description>
</method>
<method name="is_baking_navigation_polygon" qualifiers="const">
<return type="bool" />
<param index="0" name="navigation_polygon" type="NavigationPolygon" />
<description>
Returns [code]true[/code] when the provided navigation polygon is being baked on a background thread.
</description>
</method>
<method name="link_create">
<return type="RID" />
<description>

View File

@ -342,6 +342,13 @@
Returns information about the current state of the NavigationServer. See [enum ProcessInfo] for a list of available states.
</description>
</method>
<method name="is_baking_navigation_mesh" qualifiers="const">
<return type="bool" />
<param index="0" name="navigation_mesh" type="NavigationMesh" />
<description>
Returns [code]true[/code] when the provided navigation mesh is being baked on a background thread.
</description>
</method>
<method name="link_create">
<return type="RID" />
<description>

View File

@ -1116,6 +1116,10 @@ void GodotNavigationServer::bake_from_source_geometry_data_async(const Ref<Navig
#endif // _3D_DISABLED
}
bool GodotNavigationServer::is_baking_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) const {
return NavMeshGenerator3D::get_singleton()->is_baking(p_navigation_mesh);
}
COMMAND_1(free, RID, p_object) {
if (map_owner.owns(p_object)) {
NavMap *map = map_owner.get_or_null(p_object);

View File

@ -258,6 +258,7 @@ public:
virtual void parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
virtual void bake_from_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
virtual void bake_from_source_geometry_data_async(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
virtual bool is_baking_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) const override;
COMMAND_1(free, RID, p_object);

View File

@ -221,6 +221,10 @@ void GodotNavigationServer2D::bake_from_source_geometry_data_async(const Ref<Nav
#endif // CLIPPER2_ENABLED
}
bool GodotNavigationServer2D::is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const {
return NavMeshGenerator2D::get_singleton()->is_baking(p_navigation_polygon);
}
GodotNavigationServer2D::GodotNavigationServer2D() {}
GodotNavigationServer2D::~GodotNavigationServer2D() {}

View File

@ -250,6 +250,7 @@ public:
virtual void parse_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
virtual void bake_from_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
virtual void bake_from_source_geometry_data_async(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
virtual bool is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const override;
};
#endif // GODOT_NAVIGATION_SERVER_2D_H

View File

@ -157,11 +157,10 @@ void NavMeshGenerator2D::bake_from_source_geometry_data(Ref<NavigationPolygon> p
return;
}
baking_navmesh_mutex.lock();
if (baking_navmeshes.has(p_navigation_mesh)) {
baking_navmesh_mutex.unlock();
if (is_baking(p_navigation_mesh)) {
ERR_FAIL_MSG("NavigationPolygon is already baking. Wait for current bake to finish.");
}
baking_navmesh_mutex.lock();
baking_navmeshes.insert(p_navigation_mesh);
baking_navmesh_mutex.unlock();
@ -193,11 +192,10 @@ void NavMeshGenerator2D::bake_from_source_geometry_data_async(Ref<NavigationPoly
return;
}
baking_navmesh_mutex.lock();
if (baking_navmeshes.has(p_navigation_mesh)) {
baking_navmesh_mutex.unlock();
if (is_baking(p_navigation_mesh)) {
ERR_FAIL_MSG("NavigationPolygon is already baking. Wait for current bake to finish.");
}
baking_navmesh_mutex.lock();
baking_navmeshes.insert(p_navigation_mesh);
baking_navmesh_mutex.unlock();
@ -212,6 +210,13 @@ void NavMeshGenerator2D::bake_from_source_geometry_data_async(Ref<NavigationPoly
generator_task_mutex.unlock();
}
bool NavMeshGenerator2D::is_baking(Ref<NavigationPolygon> p_navigation_polygon) {
baking_navmesh_mutex.lock();
bool baking = baking_navmeshes.has(p_navigation_polygon);
baking_navmesh_mutex.unlock();
return baking;
}
void NavMeshGenerator2D::generator_thread_bake(void *p_arg) {
NavMeshGeneratorTask2D *generator_task = static_cast<NavMeshGeneratorTask2D *>(p_arg);

View File

@ -92,6 +92,7 @@ public:
static void parse_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable());
static void bake_from_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback = Callable());
static void bake_from_source_geometry_data_async(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback = Callable());
static bool is_baking(Ref<NavigationPolygon> p_navigation_polygon);
NavMeshGenerator2D();
~NavMeshGenerator2D();

View File

@ -172,11 +172,10 @@ void NavMeshGenerator3D::bake_from_source_geometry_data(Ref<NavigationMesh> p_na
return;
}
baking_navmesh_mutex.lock();
if (baking_navmeshes.has(p_navigation_mesh)) {
baking_navmesh_mutex.unlock();
if (is_baking(p_navigation_mesh)) {
ERR_FAIL_MSG("NavigationMesh is already baking. Wait for current bake to finish.");
}
baking_navmesh_mutex.lock();
baking_navmeshes.insert(p_navigation_mesh);
baking_navmesh_mutex.unlock();
@ -208,12 +207,11 @@ void NavMeshGenerator3D::bake_from_source_geometry_data_async(Ref<NavigationMesh
return;
}
baking_navmesh_mutex.lock();
if (baking_navmeshes.has(p_navigation_mesh)) {
baking_navmesh_mutex.unlock();
if (is_baking(p_navigation_mesh)) {
ERR_FAIL_MSG("NavigationMesh is already baking. Wait for current bake to finish.");
return;
}
baking_navmesh_mutex.lock();
baking_navmeshes.insert(p_navigation_mesh);
baking_navmesh_mutex.unlock();
@ -228,6 +226,13 @@ void NavMeshGenerator3D::bake_from_source_geometry_data_async(Ref<NavigationMesh
generator_task_mutex.unlock();
}
bool NavMeshGenerator3D::is_baking(Ref<NavigationMesh> p_navigation_mesh) {
baking_navmesh_mutex.lock();
bool baking = baking_navmeshes.has(p_navigation_mesh);
baking_navmesh_mutex.unlock();
return baking;
}
void NavMeshGenerator3D::generator_thread_bake(void *p_arg) {
NavMeshGeneratorTask3D *generator_task = static_cast<NavMeshGeneratorTask3D *>(p_arg);

View File

@ -99,6 +99,7 @@ public:
static void parse_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable());
static void bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
static void bake_from_source_geometry_data_async(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
static bool is_baking(Ref<NavigationMesh> p_navigation_mesh);
NavMeshGenerator3D();
~NavMeshGenerator3D();

View File

@ -253,6 +253,10 @@ void NavigationRegion2D::_bake_finished(Ref<NavigationPolygon> p_navigation_poly
emit_signal(SNAME("bake_finished"));
}
bool NavigationRegion2D::is_baking() const {
return NavigationServer2D::get_singleton()->is_baking_navigation_polygon(navigation_polygon);
}
void NavigationRegion2D::_navigation_polygon_changed() {
if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
queue_redraw();
@ -320,6 +324,7 @@ void NavigationRegion2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion2D::get_travel_cost);
ClassDB::bind_method(D_METHOD("bake_navigation_polygon", "on_thread"), &NavigationRegion2D::bake_navigation_polygon, DEFVAL(true));
ClassDB::bind_method(D_METHOD("is_baking"), &NavigationRegion2D::is_baking);
ClassDB::bind_method(D_METHOD("_navigation_polygon_changed"), &NavigationRegion2D::_navigation_polygon_changed);

View File

@ -117,6 +117,7 @@ public:
void bake_navigation_polygon(bool p_on_thread);
void _bake_finished(Ref<NavigationPolygon> p_navigation_polygon);
bool is_baking() const;
NavigationRegion2D();
~NavigationRegion2D();

View File

@ -266,6 +266,10 @@ void NavigationRegion3D::_bake_finished(Ref<NavigationMesh> p_navigation_mesh) {
emit_signal(SNAME("bake_finished"));
}
bool NavigationRegion3D::is_baking() const {
return NavigationServer3D::get_singleton()->is_baking_navigation_mesh(navigation_mesh);
}
PackedStringArray NavigationRegion3D::get_configuration_warnings() const {
PackedStringArray warnings = Node::get_configuration_warnings();
@ -308,6 +312,7 @@ void NavigationRegion3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion3D::get_travel_cost);
ClassDB::bind_method(D_METHOD("bake_navigation_mesh", "on_thread"), &NavigationRegion3D::bake_navigation_mesh, DEFVAL(true));
ClassDB::bind_method(D_METHOD("is_baking"), &NavigationRegion3D::is_baking);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_mesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");

View File

@ -105,6 +105,7 @@ public:
/// sets the new navigation mesh and emits a signal
void bake_navigation_mesh(bool p_on_thread);
void _bake_finished(Ref<NavigationMesh> p_navigation_mesh);
bool is_baking() const;
PackedStringArray get_configuration_warnings() const override;

View File

@ -162,6 +162,7 @@ void NavigationServer2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("parse_source_geometry_data", "navigation_polygon", "source_geometry_data", "root_node", "callback"), &NavigationServer2D::parse_source_geometry_data, DEFVAL(Callable()));
ClassDB::bind_method(D_METHOD("bake_from_source_geometry_data", "navigation_polygon", "source_geometry_data", "callback"), &NavigationServer2D::bake_from_source_geometry_data, DEFVAL(Callable()));
ClassDB::bind_method(D_METHOD("bake_from_source_geometry_data_async", "navigation_polygon", "source_geometry_data", "callback"), &NavigationServer2D::bake_from_source_geometry_data_async, DEFVAL(Callable()));
ClassDB::bind_method(D_METHOD("is_baking_navigation_polygon", "navigation_polygon"), &NavigationServer2D::is_baking_navigation_polygon);
ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer2D::free);

View File

@ -303,6 +303,7 @@ public:
virtual void parse_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) = 0;
virtual void bake_from_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) = 0;
virtual void bake_from_source_geometry_data_async(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) = 0;
virtual bool is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const = 0;
NavigationServer2D();
~NavigationServer2D() override;

View File

@ -167,6 +167,7 @@ public:
void parse_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override {}
void bake_from_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override {}
void bake_from_source_geometry_data_async(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override {}
bool is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const override { return false; }
void set_debug_enabled(bool p_enabled) {}
bool get_debug_enabled() const { return false; }

View File

@ -179,6 +179,7 @@ void NavigationServer3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("parse_source_geometry_data", "navigation_mesh", "source_geometry_data", "root_node", "callback"), &NavigationServer3D::parse_source_geometry_data, DEFVAL(Callable()));
ClassDB::bind_method(D_METHOD("bake_from_source_geometry_data", "navigation_mesh", "source_geometry_data", "callback"), &NavigationServer3D::bake_from_source_geometry_data, DEFVAL(Callable()));
ClassDB::bind_method(D_METHOD("bake_from_source_geometry_data_async", "navigation_mesh", "source_geometry_data", "callback"), &NavigationServer3D::bake_from_source_geometry_data_async, DEFVAL(Callable()));
ClassDB::bind_method(D_METHOD("is_baking_navigation_mesh", "navigation_mesh"), &NavigationServer3D::is_baking_navigation_mesh);
ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer3D::free);

View File

@ -343,6 +343,7 @@ public:
virtual void parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) = 0;
virtual void bake_from_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) = 0;
virtual void bake_from_source_geometry_data_async(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) = 0;
virtual bool is_baking_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) const = 0;
NavigationServer3D();
~NavigationServer3D() override;

View File

@ -170,6 +170,8 @@ public:
void parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override {}
void bake_from_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override {}
void bake_from_source_geometry_data_async(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override {}
bool is_baking_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) const override { return false; }
void free(RID p_object) override {}
void set_active(bool p_active) override {}
void process(real_t delta_time) override {}

View File

@ -31,8 +31,10 @@
#ifndef TEST_NAVIGATION_REGION_3D_H
#define TEST_NAVIGATION_REGION_3D_H
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/navigation_region_3d.h"
#include "scene/main/window.h"
#include "scene/resources/primitive_meshes.h"
#include "tests/test_macros.h"
@ -44,6 +46,44 @@ TEST_SUITE("[Navigation]") {
CHECK(region_node->get_region_rid().is_valid());
memdelete(region_node);
}
TEST_CASE("[SceneTree][NavigationRegion3D] Region should bake successfully from valid geometry") {
Node3D *node_3d = memnew(Node3D);
SceneTree::get_singleton()->get_root()->add_child(node_3d);
Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
NavigationRegion3D *navigation_region = memnew(NavigationRegion3D);
navigation_region->set_navigation_mesh(navigation_mesh);
node_3d->add_child(navigation_region);
Ref<PlaneMesh> plane_mesh = memnew(PlaneMesh);
plane_mesh->set_size(Size2(10.0, 10.0));
MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
mesh_instance->set_mesh(plane_mesh);
navigation_region->add_child(mesh_instance);
CHECK_FALSE(navigation_region->is_baking());
CHECK_EQ(navigation_mesh->get_polygon_count(), 0);
CHECK_EQ(navigation_mesh->get_vertices().size(), 0);
SUBCASE("Synchronous bake should have immediate effects") {
navigation_region->bake_navigation_mesh(false);
CHECK_FALSE(navigation_region->is_baking());
CHECK_NE(navigation_mesh->get_polygon_count(), 0);
CHECK_NE(navigation_mesh->get_vertices().size(), 0);
}
// Race condition is present in the below subcase, but baking should take many
// orders of magnitude longer than basic checks on the main thread, so it's fine.
SUBCASE("Asynchronous bake should not be immediate") {
navigation_region->bake_navigation_mesh(true);
CHECK(navigation_region->is_baking());
CHECK_EQ(navigation_mesh->get_polygon_count(), 0);
CHECK_EQ(navigation_mesh->get_vertices().size(), 0);
}
memdelete(mesh_instance);
memdelete(navigation_region);
memdelete(node_3d);
}
}
} //namespace TestNavigationRegion3D

View File

@ -720,6 +720,24 @@ TEST_SUITE("[Navigation]") {
navigation_server->free(map);
navigation_server->process(0.0); // Give server some cycles to commit.
}
TEST_CASE("[NavigationServer3D] Server should be able to bake asynchronously") {
NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
Ref<NavigationMeshSourceGeometryData3D> source_geometry = memnew(NavigationMeshSourceGeometryData3D);
Array arr;
arr.resize(RS::ARRAY_MAX);
BoxMesh::create_mesh_array(arr, Vector3(10.0, 0.001, 10.0));
source_geometry->add_mesh_array(arr, Transform3D());
// Race condition is present below, but baking should take many orders of magnitude
// longer than basic checks on the main thread, so it's fine.
navigation_server->bake_from_source_geometry_data_async(navigation_mesh, source_geometry, Callable());
CHECK(navigation_server->is_baking_navigation_mesh(navigation_mesh));
CHECK_EQ(navigation_mesh->get_polygon_count(), 0);
CHECK_EQ(navigation_mesh->get_vertices().size(), 0);
}
}
} //namespace TestNavigationServer3D