Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gridmap/grid_map.h
10277 views
1
/**************************************************************************/
2
/* grid_map.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 "scene/3d/node_3d.h"
34
#include "scene/resources/3d/mesh_library.h"
35
#include "scene/resources/multimesh.h"
36
37
class NavigationMesh;
38
class NavigationMeshSourceGeometryData3D;
39
#ifndef PHYSICS_3D_DISABLED
40
class PhysicsMaterial;
41
#endif // PHYSICS_3D_DISABLED
42
43
class GridMap : public Node3D {
44
GDCLASS(GridMap, Node3D);
45
46
enum {
47
MAP_DIRTY_TRANSFORMS = 1,
48
MAP_DIRTY_INSTANCES = 2,
49
};
50
51
union IndexKey {
52
struct {
53
int16_t x;
54
int16_t y;
55
int16_t z;
56
};
57
uint64_t key = 0;
58
59
static uint32_t hash(const IndexKey &p_key) {
60
return hash_one_uint64(p_key.key);
61
}
62
_FORCE_INLINE_ bool operator<(const IndexKey &p_key) const {
63
return key < p_key.key;
64
}
65
_FORCE_INLINE_ bool operator==(const IndexKey &p_key) const {
66
return key == p_key.key;
67
}
68
69
_FORCE_INLINE_ operator Vector3i() const {
70
return Vector3i(x, y, z);
71
}
72
73
IndexKey(Vector3i p_vector) {
74
x = (int16_t)p_vector.x;
75
y = (int16_t)p_vector.y;
76
z = (int16_t)p_vector.z;
77
}
78
IndexKey() {}
79
};
80
81
/**
82
* @brief A Cell is a single cell in the cube map space; it is defined by its coordinates and the populating Item, identified by int id.
83
*/
84
union Cell {
85
struct {
86
unsigned int item : 16;
87
unsigned int rot : 5;
88
unsigned int layer : 8;
89
};
90
uint32_t cell = 0;
91
};
92
93
/**
94
* @brief An Octant is a prism containing Cells, and possibly belonging to an Area.
95
* A GridMap can have multiple Octants.
96
*/
97
struct Octant {
98
struct NavigationCell {
99
RID region;
100
Transform3D xform;
101
RID navigation_mesh_debug_instance;
102
uint32_t navigation_layers = 1;
103
};
104
105
struct MultimeshInstance {
106
RID instance;
107
RID multimesh;
108
struct Item {
109
int index = 0;
110
Transform3D transform;
111
IndexKey key;
112
};
113
114
Vector<Item> items; //tools only, for changing visibility
115
};
116
117
Vector<MultimeshInstance> multimesh_instances;
118
HashSet<IndexKey> cells;
119
RID collision_debug;
120
RID collision_debug_instance;
121
#ifdef DEBUG_ENABLED
122
RID navigation_debug_edge_connections_instance;
123
Ref<ArrayMesh> navigation_debug_edge_connections_mesh;
124
#endif // DEBUG_ENABLED
125
126
bool dirty = false;
127
RID static_body;
128
HashMap<IndexKey, NavigationCell> navigation_cell_ids;
129
};
130
131
union OctantKey {
132
struct {
133
int16_t x;
134
int16_t y;
135
int16_t z;
136
int16_t empty;
137
};
138
139
uint64_t key = 0;
140
141
static uint32_t hash(const OctantKey &p_key) {
142
return hash_one_uint64(p_key.key);
143
}
144
_FORCE_INLINE_ bool operator==(const OctantKey &p_key) const {
145
return key == p_key.key;
146
}
147
148
//OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; }
149
OctantKey() {}
150
};
151
152
OctantKey get_octant_key_from_index_key(const IndexKey &p_index_key) const;
153
OctantKey get_octant_key_from_cell_coords(const Vector3i &p_cell_coords) const;
154
155
#ifndef PHYSICS_3D_DISABLED
156
uint32_t collision_layer = 1;
157
uint32_t collision_mask = 1;
158
real_t collision_priority = 1.0;
159
Ref<PhysicsMaterial> physics_material;
160
#endif // PHYSICS_3D_DISABLED
161
bool bake_navigation = false;
162
RID map_override;
163
164
Transform3D last_transform;
165
166
bool _in_tree = false;
167
Vector3 cell_size = Vector3(2, 2, 2);
168
int octant_size = 8;
169
bool center_x = true;
170
bool center_y = true;
171
bool center_z = true;
172
float cell_scale = 1.0;
173
174
bool recreating_octants = false;
175
176
Ref<MeshLibrary> mesh_library;
177
178
HashMap<OctantKey, Octant *, OctantKey> octant_map;
179
HashMap<IndexKey, Cell, IndexKey> cell_map;
180
181
void _recreate_octant_data();
182
183
struct BakeLight {
184
RS::LightType type = RS::LightType::LIGHT_DIRECTIONAL;
185
Vector3 pos;
186
Vector3 dir;
187
float param[RS::LIGHT_PARAM_MAX] = {};
188
};
189
190
_FORCE_INLINE_ Vector3 _octant_get_offset(const OctantKey &p_key) const {
191
return Vector3(p_key.x, p_key.y, p_key.z) * cell_size * octant_size;
192
}
193
194
#ifndef PHYSICS_3D_DISABLED
195
void _update_physics_bodies_collision_properties();
196
void _update_physics_bodies_characteristics();
197
#endif // PHYSICS_3D_DISABLED
198
void _octant_enter_world(const OctantKey &p_key);
199
void _octant_exit_world(const OctantKey &p_key);
200
bool _octant_update(const OctantKey &p_key);
201
void _octant_clean_up(const OctantKey &p_key);
202
void _octant_transform(const OctantKey &p_key);
203
#if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
204
void _update_octant_navigation_debug_edge_connections_mesh(const OctantKey &p_key);
205
void _navigation_map_changed(RID p_map);
206
void _update_navigation_debug_edge_connections();
207
#endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
208
bool awaiting_update = false;
209
210
void _queue_octants_dirty();
211
void _update_octants_callback();
212
213
#ifndef DISABLE_DEPRECATED
214
void resource_changed(const Ref<Resource> &p_res);
215
#endif
216
217
void _clear_internal();
218
219
Vector3 _get_offset() const;
220
221
struct BakedMesh {
222
Ref<Mesh> mesh;
223
RID instance;
224
};
225
226
Vector<BakedMesh> baked_meshes;
227
228
protected:
229
bool _set(const StringName &p_name, const Variant &p_value);
230
bool _get(const StringName &p_name, Variant &r_ret) const;
231
void _get_property_list(List<PropertyInfo> *p_list) const;
232
233
void _notification(int p_what);
234
void _update_visibility();
235
static void _bind_methods();
236
237
public:
238
enum {
239
INVALID_CELL_ITEM = -1
240
};
241
242
#ifndef PHYSICS_3D_DISABLED
243
void set_collision_layer(uint32_t p_layer);
244
uint32_t get_collision_layer() const;
245
246
void set_collision_mask(uint32_t p_mask);
247
uint32_t get_collision_mask() const;
248
249
void set_collision_layer_value(int p_layer_number, bool p_value);
250
bool get_collision_layer_value(int p_layer_number) const;
251
252
void set_collision_mask_value(int p_layer_number, bool p_value);
253
bool get_collision_mask_value(int p_layer_number) const;
254
255
void set_collision_priority(real_t p_priority);
256
real_t get_collision_priority() const;
257
258
void set_physics_material(Ref<PhysicsMaterial> p_material);
259
Ref<PhysicsMaterial> get_physics_material() const;
260
261
Array get_collision_shapes() const;
262
#endif // PHYSICS_3D_DISABLED
263
264
void set_bake_navigation(bool p_bake_navigation);
265
bool is_baking_navigation();
266
267
#ifndef NAVIGATION_3D_DISABLED
268
void set_navigation_map(RID p_navigation_map);
269
RID get_navigation_map() const;
270
#endif // NAVIGATION_3D_DISABLED
271
272
void set_mesh_library(const Ref<MeshLibrary> &p_mesh_library);
273
Ref<MeshLibrary> get_mesh_library() const;
274
275
void set_cell_size(const Vector3 &p_size);
276
Vector3 get_cell_size() const;
277
278
void set_octant_size(int p_size);
279
int get_octant_size() const;
280
281
void set_center_x(bool p_enable);
282
bool get_center_x() const;
283
void set_center_y(bool p_enable);
284
bool get_center_y() const;
285
void set_center_z(bool p_enable);
286
bool get_center_z() const;
287
288
void set_cell_item(const Vector3i &p_position, int p_item, int p_rot = 0);
289
int get_cell_item(const Vector3i &p_position) const;
290
int get_cell_item_orientation(const Vector3i &p_position) const;
291
Basis get_cell_item_basis(const Vector3i &p_position) const;
292
Basis get_basis_with_orthogonal_index(int p_index) const;
293
int get_orthogonal_index_from_basis(const Basis &p_basis) const;
294
295
Vector3i local_to_map(const Vector3 &p_local_position) const;
296
Vector3 map_to_local(const Vector3i &p_map_position) const;
297
298
void set_cell_scale(float p_scale);
299
float get_cell_scale() const;
300
301
TypedArray<Vector3i> get_used_cells() const;
302
TypedArray<Vector3i> get_used_cells_by_item(int p_item) const;
303
304
Array get_meshes() const;
305
306
void clear_baked_meshes();
307
void make_baked_meshes(bool p_gen_lightmap_uv = false, float p_lightmap_uv_texel_size = 0.1);
308
309
void clear();
310
311
Array get_bake_meshes();
312
RID get_bake_mesh_instance(int p_idx);
313
314
#ifndef NAVIGATION_3D_DISABLED
315
private:
316
static Callable _navmesh_source_geometry_parsing_callback;
317
static RID _navmesh_source_geometry_parser;
318
#endif // NAVIGATION_3D_DISABLED
319
320
public:
321
#ifndef NAVIGATION_3D_DISABLED
322
static void navmesh_parse_init();
323
static void navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
324
#endif // NAVIGATION_3D_DISABLED
325
326
GridMap();
327
~GridMap();
328
};
329
330