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