Skip to content

Commit

Permalink
Merge pull request #79643 from smix8/navgenerator_4.x
Browse files Browse the repository at this point in the history
Move navigation mesh baking to NavigationServer
  • Loading branch information
akien-mga committed Aug 7, 2023
2 parents 8018b47 + 744fa87 commit 7eb047a
Show file tree
Hide file tree
Showing 13 changed files with 896 additions and 755 deletions.
2 changes: 1 addition & 1 deletion doc/classes/NavigationMeshGenerator.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="NavigationMeshGenerator" inherits="Object" is_experimental="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<class name="NavigationMeshGenerator" inherits="Object" is_deprecated="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Helper class for creating and clearing navigation meshes.
</brief_description>
Expand Down
9 changes: 9 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ void finalize_display() {

void initialize_navigation_server() {
ERR_FAIL_COND(navigation_server_3d != nullptr);
ERR_FAIL_COND(navigation_server_2d != nullptr);

// Init 3D Navigation Server
navigation_server_3d = NavigationServer3DManager::new_default_server();
Expand All @@ -330,16 +331,20 @@ void initialize_navigation_server() {

// Should be impossible, but make sure it's not null.
ERR_FAIL_NULL_MSG(navigation_server_3d, "Failed to initialize NavigationServer3D.");
navigation_server_3d->init();

// Init 2D Navigation Server
navigation_server_2d = memnew(NavigationServer2D);
ERR_FAIL_NULL_MSG(navigation_server_2d, "Failed to initialize NavigationServer2D.");
}

void finalize_navigation_server() {
ERR_FAIL_NULL(navigation_server_3d);
navigation_server_3d->finish();
memdelete(navigation_server_3d);
navigation_server_3d = nullptr;

ERR_FAIL_NULL(navigation_server_2d);
memdelete(navigation_server_2d);
navigation_server_2d = nullptr;
}
Expand Down Expand Up @@ -581,6 +586,8 @@ Error Main::test_setup() {
theme_db->initialize_theme();
register_scene_singletons();

initialize_navigation_server();

ERR_FAIL_COND_V(TextServerManager::get_singleton()->get_interface_count() == 0, ERR_CANT_CREATE);

/* Use one with the most features available. */
Expand Down Expand Up @@ -639,6 +646,8 @@ void Main::test_cleanup() {

finalize_theme_db();

finalize_navigation_server();

GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
unregister_server_types();
Expand Down
20 changes: 9 additions & 11 deletions modules/navigation/editor/navigation_mesh_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@

#ifdef TOOLS_ENABLED

#include "../navigation_mesh_generator.h"

#include "core/io/marshalls.h"
#include "core/io/resource_saver.h"
#include "editor/editor_node.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/navigation_region_3d.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/dialogs.h"
Expand Down Expand Up @@ -99,18 +98,16 @@ void NavigationMeshEditor::_bake_pressed() {
}
}

NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh());
Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
source_geometry_data.instantiate();
NavigationMeshGenerator::get_singleton()->parse_source_geometry_data(node->get_navigation_mesh(), source_geometry_data, node);
NavigationMeshGenerator::get_singleton()->bake_from_source_geometry_data(node->get_navigation_mesh(), source_geometry_data);
node->bake_navigation_mesh(false);

node->update_gizmos();
}

void NavigationMeshEditor::_clear_pressed() {
if (node) {
NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh());
if (node->get_navigation_mesh().is_valid()) {
node->get_navigation_mesh()->clear();
}
}

button_bake->set_pressed(false);
Expand Down Expand Up @@ -139,14 +136,15 @@ NavigationMeshEditor::NavigationMeshEditor() {
button_bake->set_flat(true);
bake_hbox->add_child(button_bake);
button_bake->set_toggle_mode(true);
button_bake->set_text(TTR("Bake NavMesh"));
button_bake->set_text(TTR("Bake NavigationMesh"));
button_bake->set_tooltip_text(TTR("Bakes the NavigationMesh by first parsing the scene for source geometry and then creating the navigation mesh vertices and polygons."));
button_bake->connect("pressed", callable_mp(this, &NavigationMeshEditor::_bake_pressed));

button_reset = memnew(Button);
button_reset->set_flat(true);
bake_hbox->add_child(button_reset);
// No button text, we only use a revert icon which is set when entering the tree.
button_reset->set_tooltip_text(TTR("Clear the navigation mesh."));
button_reset->set_text(TTR("Clear NavigationMesh"));
button_reset->set_tooltip_text(TTR("Clears the internal NavigationMesh vertices and polygons."));
button_reset->connect("pressed", callable_mp(this, &NavigationMeshEditor::_clear_pressed));

bake_info = memnew(Label);
Expand Down
55 changes: 46 additions & 9 deletions modules/navigation/godot_navigation_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#include "godot_navigation_server.h"

#ifndef _3D_DISABLED
#include "navigation_mesh_generator.h"
#endif
#include "nav_mesh_generator_3d.h"
#endif // _3D_DISABLED

#include "core/os/mutex.h"

Expand Down Expand Up @@ -468,11 +468,11 @@ void GodotNavigationServer::region_bake_navigation_mesh(Ref<NavigationMesh> p_na
WARN_PRINT_ONCE("NavigationServer3D::region_bake_navigation_mesh() is deprecated due to core threading changes. To upgrade existing code, first create a NavigationMeshSourceGeometryData3D resource. Use this resource with method parse_source_geometry_data() to parse the SceneTree for nodes that should contribute to the navigation mesh baking. The SceneTree parsing needs to happen on the main thread. After the parsing is finished use the resource with method bake_from_source_geometry_data() to bake a navigation mesh..");

#ifndef _3D_DISABLED
NavigationMeshGenerator::get_singleton()->clear(p_navigation_mesh);
p_navigation_mesh->clear();
Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
source_geometry_data.instantiate();
NavigationMeshGenerator::get_singleton()->parse_source_geometry_data(p_navigation_mesh, source_geometry_data, p_root_node);
NavigationMeshGenerator::get_singleton()->bake_from_source_geometry_data(p_navigation_mesh, source_geometry_data);
parse_source_geometry_data(p_navigation_mesh, source_geometry_data, p_root_node);
bake_from_source_geometry_data(p_navigation_mesh, source_geometry_data);
#endif
}
#endif // DISABLE_DEPRECATED
Expand Down Expand Up @@ -932,14 +932,34 @@ COMMAND_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers) {

void GodotNavigationServer::parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback) {
#ifndef _3D_DISABLED
NavigationMeshGenerator::get_singleton()->parse_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_root_node, p_callback);
#endif
ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "The SceneTree can only be parsed on the main thread. Call this function from the main thread or use call_deferred().");
ERR_FAIL_COND_MSG(!p_navigation_mesh.is_valid(), "Invalid navigation mesh.");
ERR_FAIL_COND_MSG(p_root_node == nullptr, "No parsing root node specified.");
ERR_FAIL_COND_MSG(!p_root_node->is_inside_tree(), "The root node needs to be inside the SceneTree.");

ERR_FAIL_NULL(NavMeshGenerator3D::get_singleton());
NavMeshGenerator3D::get_singleton()->parse_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_root_node, p_callback);
#endif // _3D_DISABLED
}

void GodotNavigationServer::bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback) {
#ifndef _3D_DISABLED
NavigationMeshGenerator::get_singleton()->bake_from_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_callback);
#endif
ERR_FAIL_COND_MSG(!p_navigation_mesh.is_valid(), "Invalid navigation mesh.");
ERR_FAIL_COND_MSG(!p_source_geometry_data.is_valid(), "Invalid NavigationMeshSourceGeometryData3D.");

if (!p_source_geometry_data->has_data()) {
p_navigation_mesh->clear();
if (p_callback.is_valid()) {
Callable::CallError ce;
Variant result;
p_callback.callp(nullptr, 0, result, ce);
}
return;
}

ERR_FAIL_NULL(NavMeshGenerator3D::get_singleton());
NavMeshGenerator3D::get_singleton()->bake_from_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_callback);
#endif // _3D_DISABLED
}

COMMAND_1(free, RID, p_object) {
Expand Down Expand Up @@ -1117,6 +1137,23 @@ void GodotNavigationServer::process(real_t p_delta_time) {
pm_edge_free_count = _new_pm_edge_free_count;
}

void GodotNavigationServer::init() {
#ifndef _3D_DISABLED
navmesh_generator_3d = memnew(NavMeshGenerator3D);
#endif // _3D_DISABLED
}

void GodotNavigationServer::finish() {
flush_queries();
#ifndef _3D_DISABLED
if (navmesh_generator_3d) {
navmesh_generator_3d->finish();
memdelete(navmesh_generator_3d);
navmesh_generator_3d = nullptr;
}
#endif // _3D_DISABLED
}

PathQueryResult GodotNavigationServer::_query_path(const PathQueryParameters &p_parameters) const {
PathQueryResult r_query_result;

Expand Down
5 changes: 5 additions & 0 deletions modules/navigation/godot_navigation_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)

class GodotNavigationServer;
class NavMeshGenerator3D;

struct SetCommand {
virtual ~SetCommand() {}
Expand All @@ -79,6 +80,8 @@ class GodotNavigationServer : public NavigationServer3D {
LocalVector<NavMap *> active_maps;
LocalVector<uint32_t> active_maps_update_id;

NavMeshGenerator3D *navmesh_generator_3d = nullptr;

// Performance Monitor
int pm_region_count = 0;
int pm_agent_count = 0;
Expand Down Expand Up @@ -234,6 +237,8 @@ class GodotNavigationServer : public NavigationServer3D {

void flush_queries();
virtual void process(real_t p_delta_time) override;
virtual void init() override;
virtual void finish() override;

virtual NavigationUtilities::PathQueryResult _query_path(const NavigationUtilities::PathQueryParameters &p_parameters) const override;

Expand Down
Loading

0 comments on commit 7eb047a

Please sign in to comment.