Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/navigation_3d/3d/nav_mesh_generator_3d.h
11354 views
1
/**************************************************************************/
2
/* nav_mesh_generator_3d.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/object/class_db.h"
34
#include "core/object/worker_thread_pool.h"
35
#include "core/templates/rid_owner.h"
36
#include "servers/navigation_3d/navigation_server_3d.h"
37
38
class Node;
39
class NavigationMesh;
40
class NavigationMeshSourceGeometryData3D;
41
42
class NavMeshGenerator3D : public Object {
43
GDSOFTCLASS(NavMeshGenerator3D, Object);
44
45
static NavMeshGenerator3D *singleton;
46
47
static Mutex baking_navmesh_mutex;
48
static Mutex generator_task_mutex;
49
50
static RWLock generator_parsers_rwlock;
51
static LocalVector<NavMeshGeometryParser3D *> generator_parsers;
52
53
static bool use_threads;
54
static bool baking_use_multiple_threads;
55
static bool baking_use_high_priority_threads;
56
57
public:
58
enum NavMeshBakeState {
59
BAKE_STATE_NONE,
60
BAKE_STATE_CONFIGURATION,
61
BAKE_STATE_CALC_GRID_SIZE,
62
BAKE_STATE_CREATE_HEIGHTFIELD,
63
BAKE_STATE_MARK_WALKABLE_TRIANGLES,
64
BAKE_STATE_CONSTRUCT_COMPACT_HEIGHTFIELD,
65
BAKE_STATE_ERODE_WALKABLE_AREA,
66
BAKE_STATE_SAMPLE_PARTITIONING,
67
BAKE_STATE_CREATING_CONTOURS,
68
BAKE_STATE_CREATING_POLYMESH,
69
BAKE_STATE_CONVERTING_NATIVE_NAVMESH,
70
BAKE_STATE_BAKE_CLEANUP,
71
BAKE_STATE_BAKE_FINISHED,
72
BAKE_STATE_MAX,
73
};
74
75
private:
76
struct NavMeshGeneratorTask3D {
77
enum TaskStatus {
78
BAKING_STARTED,
79
BAKING_FINISHED,
80
BAKING_FAILED,
81
CALLBACK_DISPATCHED,
82
CALLBACK_FAILED,
83
};
84
85
Ref<NavigationMesh> navigation_mesh;
86
Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
87
Callable callback;
88
WorkerThreadPool::TaskID thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
89
NavMeshGeneratorTask3D::TaskStatus status = NavMeshGeneratorTask3D::TaskStatus::BAKING_STARTED;
90
91
NavMeshBakeState bake_state = NavMeshBakeState::BAKE_STATE_NONE;
92
};
93
94
static HashMap<WorkerThreadPool::TaskID, NavMeshGeneratorTask3D *> generator_tasks;
95
96
static void generator_thread_bake(void *p_arg);
97
98
static HashMap<Ref<NavigationMesh>, NavMeshGeneratorTask3D *> baking_navmeshes;
99
100
static void generator_parse_geometry_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node, bool p_recurse_children);
101
static void generator_parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node);
102
static void generator_bake_from_source_geometry_data(NavMeshGeneratorTask3D *p_generator_task);
103
104
static bool generator_emit_callback(const Callable &p_callback);
105
106
public:
107
static NavMeshGenerator3D *get_singleton();
108
109
static void sync();
110
static void cleanup();
111
static void finish();
112
113
static void set_generator_parsers(LocalVector<NavMeshGeometryParser3D *> p_parsers);
114
115
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());
116
static void bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
117
static void bake_from_source_geometry_data_async(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
118
static bool is_baking(Ref<NavigationMesh> p_navigation_mesh);
119
static String get_baking_state_msg(Ref<NavigationMesh> p_navigation_mesh);
120
121
NavMeshGenerator3D();
122
~NavMeshGenerator3D();
123
};
124
125