grid_map.h 9.9 KB

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