grid_map.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifndef GRID_MAP_H
  31. #define GRID_MAP_H
  32. #include "scene/3d/navigation.h"
  33. #include "scene/3d/spatial.h"
  34. #include "scene/resources/mesh_library.h"
  35. #include "scene/resources/multimesh.h"
  36. //heh heh, godotsphir!! this shares no code and the design is completely different with previous projects i've done..
  37. //should scale better with hardware that supports instancing
  38. class PhysicsMaterial;
  39. class GridMap : public Spatial {
  40. GDCLASS(GridMap, Spatial);
  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;
  52. _FORCE_INLINE_ bool operator<(const IndexKey &p_key) const {
  53. return key < p_key.key;
  54. }
  55. IndexKey() { key = 0; }
  56. };
  57. /**
  58. * @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.
  59. */
  60. union Cell {
  61. struct {
  62. unsigned int item : 16;
  63. unsigned int rot : 5;
  64. unsigned int layer : 8;
  65. };
  66. uint32_t cell;
  67. Cell() {
  68. item = 0;
  69. rot = 0;
  70. layer = 0;
  71. }
  72. };
  73. /**
  74. * @brief An Octant is a prism containing Cells, and possibly belonging to an Area.
  75. * A GridMap can have multiple Octants.
  76. */
  77. struct Octant {
  78. struct NavMesh {
  79. RID region;
  80. Transform xform;
  81. RID navmesh_debug_instance;
  82. };
  83. struct MultimeshInstance {
  84. RID instance;
  85. RID multimesh;
  86. struct Item {
  87. int index;
  88. Transform transform;
  89. IndexKey key;
  90. };
  91. Vector<Item> items; //tools only, for changing visibility
  92. };
  93. Vector<MultimeshInstance> multimesh_instances;
  94. Set<IndexKey> cells;
  95. RID collision_debug;
  96. RID collision_debug_instance;
  97. bool dirty;
  98. RID static_body;
  99. Map<IndexKey, NavMesh> navmesh_ids;
  100. };
  101. union OctantKey {
  102. struct {
  103. int16_t x;
  104. int16_t y;
  105. int16_t z;
  106. int16_t empty;
  107. };
  108. uint64_t key;
  109. _FORCE_INLINE_ bool operator<(const OctantKey &p_key) const {
  110. return key < p_key.key;
  111. }
  112. //OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; }
  113. OctantKey() { key = 0; }
  114. };
  115. uint32_t collision_layer;
  116. uint32_t collision_mask;
  117. Ref<PhysicsMaterial> physics_material;
  118. bool bake_navigation = false;
  119. uint32_t navigation_layers = 1;
  120. Transform last_transform;
  121. bool _in_tree;
  122. Vector3 cell_size;
  123. int octant_size;
  124. bool center_x, center_y, center_z;
  125. float cell_scale;
  126. Navigation *navigation;
  127. bool clip;
  128. bool clip_above;
  129. int clip_floor;
  130. bool recreating_octants;
  131. Vector3::Axis clip_axis;
  132. Ref<MeshLibrary> mesh_library;
  133. bool use_in_baked_light;
  134. Map<OctantKey, Octant *> octant_map;
  135. Map<IndexKey, Cell> cell_map;
  136. void _recreate_octant_data();
  137. struct BakeLight {
  138. VS::LightType type;
  139. Vector3 pos;
  140. Vector3 dir;
  141. float param[VS::LIGHT_PARAM_MAX];
  142. };
  143. _FORCE_INLINE_ Vector3 _octant_get_offset(const OctantKey &p_key) const {
  144. return Vector3(p_key.x, p_key.y, p_key.z) * cell_size * octant_size;
  145. }
  146. void _reset_physic_bodies_collision_filters();
  147. void _octant_enter_world(const OctantKey &p_key);
  148. void _octant_exit_world(const OctantKey &p_key);
  149. bool _octant_update(const OctantKey &p_key);
  150. void _octant_clean_up(const OctantKey &p_key);
  151. void _octant_transform(const OctantKey &p_key);
  152. bool awaiting_update;
  153. void _queue_octants_dirty();
  154. void _update_octants_callback();
  155. void resource_changed(const RES &p_res);
  156. void _clear_internal();
  157. Vector3 _get_offset() const;
  158. struct BakedMesh {
  159. Ref<Mesh> mesh;
  160. RID instance;
  161. };
  162. Vector<BakedMesh> baked_meshes;
  163. protected:
  164. bool _set(const StringName &p_name, const Variant &p_value);
  165. bool _get(const StringName &p_name, Variant &r_ret) const;
  166. void _get_property_list(List<PropertyInfo> *p_list) const;
  167. void _notification(int p_what);
  168. void _update_visibility();
  169. static void _bind_methods();
  170. public:
  171. enum {
  172. INVALID_CELL_ITEM = -1
  173. };
  174. void set_collision_layer(uint32_t p_layer);
  175. uint32_t get_collision_layer() const;
  176. void set_collision_mask(uint32_t p_mask);
  177. uint32_t get_collision_mask() const;
  178. void set_collision_layer_bit(int p_bit, bool p_value);
  179. bool get_collision_layer_bit(int p_bit) const;
  180. void set_collision_mask_bit(int p_bit, bool p_value);
  181. bool get_collision_mask_bit(int p_bit) const;
  182. void set_physics_material(Ref<PhysicsMaterial> p_material);
  183. Ref<PhysicsMaterial> get_physics_material() const;
  184. Array get_collision_shapes() const;
  185. void set_bake_navigation(bool p_bake_navigation);
  186. bool is_baking_navigation();
  187. void set_navigation_layers(uint32_t p_navigation_layers);
  188. uint32_t get_navigation_layers();
  189. void set_mesh_library(const Ref<MeshLibrary> &p_mesh_library);
  190. Ref<MeshLibrary> get_mesh_library() const;
  191. void set_use_in_baked_light(bool p_use_baked_light);
  192. bool get_use_in_baked_light() const;
  193. void set_cell_size(const Vector3 &p_size);
  194. Vector3 get_cell_size() const;
  195. void set_octant_size(int p_size);
  196. int get_octant_size() const;
  197. void set_center_x(bool p_enable);
  198. bool get_center_x() const;
  199. void set_center_y(bool p_enable);
  200. bool get_center_y() const;
  201. void set_center_z(bool p_enable);
  202. bool get_center_z() const;
  203. void set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot = 0);
  204. int get_cell_item(int p_x, int p_y, int p_z) const;
  205. int get_cell_item_orientation(int p_x, int p_y, int p_z) const;
  206. Vector3 world_to_map(const Vector3 &p_world_pos) const;
  207. Vector3 map_to_world(int p_x, int p_y, int p_z) const;
  208. void set_clip(bool p_enabled, bool p_clip_above = true, int p_floor = 0, Vector3::Axis p_axis = Vector3::AXIS_X);
  209. void set_cell_scale(float p_scale);
  210. float get_cell_scale() const;
  211. Array get_used_cells() const;
  212. Array get_used_cells_by_item(int p_item) const;
  213. Array get_meshes() const;
  214. void clear_baked_meshes();
  215. void make_baked_meshes(bool p_gen_lightmap_uv = false, float p_lightmap_uv_texel_size = 0.1);
  216. void clear();
  217. Array get_bake_meshes();
  218. RID get_bake_mesh_instance(int p_idx);
  219. GridMap();
  220. ~GridMap();
  221. };
  222. #endif // GRID_MAP_H