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
10278 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_server_3d.h"
37
38
class Node;
39
class NavigationMesh;
40
class NavigationMeshSourceGeometryData3D;
41
42
class NavMeshGenerator3D : public Object {
43
static NavMeshGenerator3D *singleton;
44
45
static Mutex baking_navmesh_mutex;
46
static Mutex generator_task_mutex;
47
48
static RWLock generator_parsers_rwlock;
49
static LocalVector<NavMeshGeometryParser3D *> generator_parsers;
50
51
static bool use_threads;
52
static bool baking_use_multiple_threads;
53
static bool baking_use_high_priority_threads;
54
55
public:
56
enum NavMeshBakeState {
57
BAKE_STATE_NONE,
58
BAKE_STATE_CONFIGURATION,
59
BAKE_STATE_CALC_GRID_SIZE,
60
BAKE_STATE_CREATE_HEIGHTFIELD,
61
BAKE_STATE_MARK_WALKABLE_TRIANGLES,
62
BAKE_STATE_CONSTRUCT_COMPACT_HEIGHTFIELD,
63
BAKE_STATE_ERODE_WALKABLE_AREA,
64
BAKE_STATE_SAMPLE_PARTITIONING,
65
BAKE_STATE_CREATING_CONTOURS,
66
BAKE_STATE_CREATING_POLYMESH,
67
BAKE_STATE_CONVERTING_NATIVE_NAVMESH,
68
BAKE_STATE_BAKE_CLEANUP,
69
BAKE_STATE_BAKE_FINISHED,
70
BAKE_STATE_MAX,
71
};
72
73
private:
74
struct NavMeshGeneratorTask3D {
75
enum TaskStatus {
76
BAKING_STARTED,
77
BAKING_FINISHED,
78
BAKING_FAILED,
79
CALLBACK_DISPATCHED,
80
CALLBACK_FAILED,
81
};
82
83
Ref<NavigationMesh> navigation_mesh;
84
Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
85
Callable callback;
86
WorkerThreadPool::TaskID thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
87
NavMeshGeneratorTask3D::TaskStatus status = NavMeshGeneratorTask3D::TaskStatus::BAKING_STARTED;
88
89
NavMeshBakeState bake_state = NavMeshBakeState::BAKE_STATE_NONE;
90
};
91
92
static HashMap<WorkerThreadPool::TaskID, NavMeshGeneratorTask3D *> generator_tasks;
93
94
static void generator_thread_bake(void *p_arg);
95
96
static HashMap<Ref<NavigationMesh>, NavMeshGeneratorTask3D *> baking_navmeshes;
97
98
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);
99
static void generator_parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node);
100
static void generator_bake_from_source_geometry_data(NavMeshGeneratorTask3D *p_generator_task);
101
102
static bool generator_emit_callback(const Callable &p_callback);
103
104
public:
105
static NavMeshGenerator3D *get_singleton();
106
107
static void sync();
108
static void cleanup();
109
static void finish();
110
111
static void set_generator_parsers(LocalVector<NavMeshGeometryParser3D *> p_parsers);
112
113
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());
114
static void bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
115
static void bake_from_source_geometry_data_async(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
116
static bool is_baking(Ref<NavigationMesh> p_navigation_mesh);
117
static String get_baking_state_msg(Ref<NavigationMesh> p_navigation_mesh);
118
119
NavMeshGenerator3D();
120
~NavMeshGenerator3D();
121
};
122
123