Skip to content

Commit

Permalink
Make NavigationMeshGenerator independent from navigation module
Browse files Browse the repository at this point in the history
Moves the NavigationMeshGenerator singleton out from Godot "navigation" module.
  • Loading branch information
smix8 committed Dec 21, 2022
1 parent f84c308 commit 5d7df32
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 112 deletions.
1 change: 1 addition & 0 deletions doc/classes/NavigationRegion3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<param index="0" name="on_thread" type="bool" default="true" />
<description>
Bakes the [NavigationMesh]. If [param on_thread] is set to [code]true[/code] (default), the baking is done on a separate thread. Baking on separate thread is useful because navigation baking is not a cheap operation. When it is completed, it automatically sets the new [NavigationMesh]. Please note that baking on separate thread may be very slow if geometry is parsed from meshes as async access to each mesh involves heavy synchronization. Also, please note that baking on a separate thread is automatically disabled on operating systems that cannot use threads (such as Web with threads disabled).
[b]Note:[/b] Only functional in Godot builds with NavigationMeshGenerator module enabled.
</description>
</method>
<method name="get_navigation_layer_value" qualifiers="const">
Expand Down
8 changes: 0 additions & 8 deletions doc/classes/NavigationServer3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,6 @@
Queries a path in a given navigation map. Start and target position and other parameters are defined through [NavigationPathQueryParameters3D]. Updates the provided [NavigationPathQueryResult3D] result object with the path among other results requested by the query.
</description>
</method>
<method name="region_bake_navigation_mesh" qualifiers="const">
<return type="void" />
<param index="0" name="navigation_mesh" type="NavigationMesh" />
<param index="1" name="root_node" type="Node" />
<description>
Bakes the [param navigation_mesh] with bake source geometry collected starting from the [param root_node].
</description>
</method>
<method name="region_create" qualifiers="const">
<return type="RID" />
<description>
Expand Down
26 changes: 0 additions & 26 deletions modules/navigation/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,6 @@ env_navigation = env_modules.Clone()

thirdparty_obj = []

# Recast Thirdparty source files
if env["builtin_recast"]:
thirdparty_dir = "#thirdparty/recastnavigation/Recast/"
thirdparty_sources = [
"Source/Recast.cpp",
"Source/RecastAlloc.cpp",
"Source/RecastArea.cpp",
"Source/RecastAssert.cpp",
"Source/RecastContour.cpp",
"Source/RecastFilter.cpp",
"Source/RecastLayers.cpp",
"Source/RecastMesh.cpp",
"Source/RecastMeshDetail.cpp",
"Source/RecastRasterization.cpp",
"Source/RecastRegion.cpp",
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]

env_navigation.Prepend(CPPPATH=[thirdparty_dir + "Include"])

env_thirdparty = env_navigation.Clone()
env_thirdparty.disable_warnings()
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)

# RVO Thirdparty source files
if env["builtin_rvo2"]:
thirdparty_dir = "#thirdparty/rvo2/"
Expand All @@ -57,8 +33,6 @@ env.modules_sources += thirdparty_obj
module_obj = []

env_navigation.add_source_files(module_obj, "*.cpp")
if env.editor_build:
env_navigation.add_source_files(module_obj, "editor/*.cpp")
env.modules_sources += module_obj

# Needed to force rebuilding the module files when the thirdparty library is updated.
Expand Down
14 changes: 0 additions & 14 deletions modules/navigation/godot_navigation_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@

#include "core/os/mutex.h"

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

using namespace NavigationUtilities;

/// Creates a struct for each function and a function that once called creates
Expand Down Expand Up @@ -430,16 +426,6 @@ COMMAND_2(region_set_navigation_mesh, RID, p_region, Ref<NavigationMesh>, p_navi
region->set_mesh(p_navigation_mesh);
}

void GodotNavigationServer::region_bake_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh, Node *p_root_node) const {
ERR_FAIL_COND(p_navigation_mesh.is_null());
ERR_FAIL_COND(p_root_node == nullptr);

#ifndef _3D_DISABLED
NavigationMeshGenerator::get_singleton()->clear(p_navigation_mesh);
NavigationMeshGenerator::get_singleton()->bake(p_navigation_mesh, p_root_node);
#endif
}

int GodotNavigationServer::region_get_connections_count(RID p_region) const {
NavRegion *region = region_owner.get_or_null(p_region);
ERR_FAIL_COND_V(!region, 0);
Expand Down
1 change: 0 additions & 1 deletion modules/navigation/godot_navigation_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ class GodotNavigationServer : public NavigationServer3D {
virtual uint32_t region_get_navigation_layers(RID p_region) const override;
COMMAND_2(region_set_transform, RID, p_region, Transform3D, p_transform);
COMMAND_2(region_set_navigation_mesh, RID, p_region, Ref<NavigationMesh>, p_navigation_mesh);
virtual void region_bake_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh, Node *p_root_node) const override;
virtual int region_get_connections_count(RID p_region) const override;
virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
Expand Down
37 changes: 1 addition & 36 deletions modules/navigation/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,8 @@

#include "register_types.h"

#include "core/config/engine.h"
#include "servers/navigation_server_3d.h"

#include "godot_navigation_server.h"

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

#ifdef TOOLS_ENABLED
#include "editor/navigation_mesh_editor_plugin.h"
#endif

#ifndef _3D_DISABLED
NavigationMeshGenerator *_nav_mesh_generator = nullptr;
#endif
#include "servers/navigation_server_3d.h"

NavigationServer3D *new_server() {
return memnew(GodotNavigationServer);
Expand All @@ -54,29 +40,8 @@ NavigationServer3D *new_server() {
void initialize_navigation_module(ModuleInitializationLevel p_level) {
if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
NavigationServer3DManager::set_default_server(new_server);

#ifndef _3D_DISABLED
_nav_mesh_generator = memnew(NavigationMeshGenerator);
GDREGISTER_CLASS(NavigationMeshGenerator);
Engine::get_singleton()->add_singleton(Engine::Singleton("NavigationMeshGenerator", NavigationMeshGenerator::get_singleton()));
#endif
}

#ifdef TOOLS_ENABLED
if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
EditorPlugins::add_by_type<NavigationMeshEditorPlugin>();
}
#endif
}

void uninitialize_navigation_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SERVERS) {
return;
}

#ifndef _3D_DISABLED
if (_nav_mesh_generator) {
memdelete(_nav_mesh_generator);
}
#endif
}
48 changes: 48 additions & 0 deletions modules/navigation_mesh_generator/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python

Import("env")
Import("env_modules")

env_navigation_mesh_generator = env_modules.Clone()

# Thirdparty source files

thirdparty_obj = []

# Recast Thirdparty source files
if env["builtin_recast"]:
thirdparty_dir = "#thirdparty/recastnavigation/Recast/"
thirdparty_sources = [
"Source/Recast.cpp",
"Source/RecastAlloc.cpp",
"Source/RecastArea.cpp",
"Source/RecastAssert.cpp",
"Source/RecastContour.cpp",
"Source/RecastFilter.cpp",
"Source/RecastLayers.cpp",
"Source/RecastMesh.cpp",
"Source/RecastMeshDetail.cpp",
"Source/RecastRasterization.cpp",
"Source/RecastRegion.cpp",
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]

env_navigation_mesh_generator.Prepend(CPPPATH=[thirdparty_dir + "Include"])

env_thirdparty = env_navigation_mesh_generator.Clone()
env_thirdparty.disable_warnings()
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)

env.modules_sources += thirdparty_obj

# Godot source files

module_obj = []

env_navigation_mesh_generator.add_source_files(module_obj, "*.cpp")
if env.editor_build:
env_navigation_mesh_generator.add_source_files(module_obj, "editor/*.cpp")
env.modules_sources += module_obj

# Needed to force rebuilding the module files when the thirdparty library is updated.
env.Depends(module_obj, thirdparty_obj)
16 changes: 16 additions & 0 deletions modules/navigation_mesh_generator/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def can_build(env, platform):
return True


def configure(env):
pass


def get_doc_classes():
return [
"NavigationMeshGenerator",
]


def get_doc_path():
return "doc_classes"
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" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<class name="NavigationMeshGenerator" inherits="Object" is_experimental="true" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Helper class for creating and clearing navigation meshes.
</brief_description>
Expand Down
5 changes: 5 additions & 0 deletions modules/navigation_mesh_generator/editor/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python

Import("env")

env.add_source_files(env.editor_sources, "*.cpp")
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#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"

void NavigationMeshEditor::_node_removed(Node *p_node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef _3D_DISABLED

#include "navigation_mesh_generator.h"

#include "core/math/convex_hull.h"
#include "core/os/thread.h"

#ifndef _3D_DISABLED
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/multimesh_instance_3d.h"
#include "scene/3d/physics_body_3d.h"
Expand All @@ -60,9 +60,21 @@
#ifdef MODULE_GRIDMAP_ENABLED
#include "modules/gridmap/grid_map.h"
#endif
#endif // _3D_DISABLED

NavigationMeshGenerator *NavigationMeshGenerator::singleton = nullptr;

NavigationMeshGenerator *NavigationMeshGenerator::get_singleton() {
return singleton;
}

NavigationMeshGenerator::NavigationMeshGenerator() {
singleton = this;
}

NavigationMeshGenerator::~NavigationMeshGenerator() {
}

void NavigationMeshGenerator::_add_vertex(const Vector3 &p_vec3, Vector<float> &p_vertices) {
p_vertices.push_back(p_vec3.x);
p_vertices.push_back(p_vec3.y);
Expand Down Expand Up @@ -162,6 +174,7 @@ void NavigationMeshGenerator::_add_faces(const PackedVector3Array &p_faces, cons
}

void NavigationMeshGenerator::_parse_geometry(const Transform3D &p_navmesh_transform, Node *p_node, Vector<float> &p_vertices, Vector<int> &p_indices, NavigationMesh::ParsedGeometryType p_generate_from, uint32_t p_collision_mask, bool p_recurse_children) {
#ifndef _3D_DISABLED
if (Object::cast_to<MeshInstance3D>(p_node) && p_generate_from != NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS) {
MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(p_node);
Ref<Mesh> mesh = mesh_instance->get_mesh();
Expand Down Expand Up @@ -468,8 +481,10 @@ void NavigationMeshGenerator::_parse_geometry(const Transform3D &p_navmesh_trans
_parse_geometry(p_navmesh_transform, p_node->get_child(i), p_vertices, p_indices, p_generate_from, p_collision_mask, p_recurse_children);
}
}
#endif // _3D_DISABLED
}

#ifndef _3D_DISABLED
void NavigationMeshGenerator::_convert_detail_mesh_to_native_navigation_mesh(const rcPolyMeshDetail *p_detail_mesh, Ref<NavigationMesh> p_navigation_mesh) {
Vector<Vector3> nav_vertices;

Expand All @@ -496,7 +511,9 @@ void NavigationMeshGenerator::_convert_detail_mesh_to_native_navigation_mesh(con
}
}
}
#endif // _3D_DISABLED

#ifndef _3D_DISABLED
void NavigationMeshGenerator::_build_recast_navigation_mesh(
Ref<NavigationMesh> p_navigation_mesh,
#ifdef TOOLS_ENABLED
Expand Down Expand Up @@ -717,20 +734,11 @@ void NavigationMeshGenerator::_build_recast_navigation_mesh(
rcFreePolyMeshDetail(detail_mesh);
detail_mesh = nullptr;
}

NavigationMeshGenerator *NavigationMeshGenerator::get_singleton() {
return singleton;
}

NavigationMeshGenerator::NavigationMeshGenerator() {
singleton = this;
}

NavigationMeshGenerator::~NavigationMeshGenerator() {
}
#endif // _3D_DISABLED

void NavigationMeshGenerator::bake(Ref<NavigationMesh> p_navigation_mesh, Node *p_root_node) {
ERR_FAIL_COND_MSG(!p_navigation_mesh.is_valid(), "Invalid navigation mesh.");
#ifndef _3D_DISABLED

#ifdef TOOLS_ENABLED
EditorProgress *ep(nullptr);
Expand Down Expand Up @@ -813,7 +821,8 @@ void NavigationMeshGenerator::bake(Ref<NavigationMesh> p_navigation_mesh, Node *
if (ep) {
memdelete(ep);
}
#endif
#endif // TOOLS_ENABLED
#endif // _3D_DISABLED
}

void NavigationMeshGenerator::clear(Ref<NavigationMesh> p_navigation_mesh) {
Expand All @@ -827,5 +836,3 @@ void NavigationMeshGenerator::_bind_methods() {
ClassDB::bind_method(D_METHOD("bake", "navigation_mesh", "root_node"), &NavigationMeshGenerator::bake);
ClassDB::bind_method(D_METHOD("clear", "navigation_mesh"), &NavigationMeshGenerator::clear);
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
#ifndef NAVIGATION_MESH_GENERATOR_H
#define NAVIGATION_MESH_GENERATOR_H

#include "scene/resources/navigation_mesh.h"
#ifndef _3D_DISABLED

#include "scene/3d/navigation_region_3d.h"

#include <Recast.h>
#else
#include "scene/main/node.h"
#endif // _3D_DISABLED

#ifdef TOOLS_ENABLED
struct EditorProgress;
Expand All @@ -55,6 +56,7 @@ class NavigationMeshGenerator : public Object {
static void _add_faces(const PackedVector3Array &p_faces, const Transform3D &p_xform, Vector<float> &p_vertices, Vector<int> &p_indices);
static void _parse_geometry(const Transform3D &p_navmesh_transform, Node *p_node, Vector<float> &p_vertices, Vector<int> &p_indices, NavigationMesh::ParsedGeometryType p_generate_from, uint32_t p_collision_mask, bool p_recurse_children);

#ifndef _3D_DISABLED
static void _convert_detail_mesh_to_native_navigation_mesh(const rcPolyMeshDetail *p_detail_mesh, Ref<NavigationMesh> p_navigation_mesh);
static void _build_recast_navigation_mesh(
Ref<NavigationMesh> p_navigation_mesh,
Expand All @@ -68,6 +70,7 @@ class NavigationMeshGenerator : public Object {
rcPolyMeshDetail *detail_mesh,
Vector<float> &vertices,
Vector<int> &indices);
#endif // _3D_DISABLED

public:
static NavigationMeshGenerator *get_singleton();
Expand All @@ -79,6 +82,4 @@ class NavigationMeshGenerator : public Object {
void clear(Ref<NavigationMesh> p_navigation_mesh);
};

#endif

#endif // NAVIGATION_MESH_GENERATOR_H
Loading

0 comments on commit 5d7df32

Please sign in to comment.