grid_map.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*************************************************************************/
  2. /* grid_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 GridMap : public Spatial {
  39. GDCLASS(GridMap, Spatial);
  40. enum {
  41. MAP_DIRTY_TRANSFORMS = 1,
  42. MAP_DIRTY_INSTANCES = 2,
  43. };
  44. union IndexKey {
  45. struct {
  46. int16_t x;
  47. int16_t y;
  48. int16_t z;
  49. };
  50. uint64_t key;
  51. _FORCE_INLINE_ bool operator<(const IndexKey &p_key) const {
  52. return key < p_key.key;
  53. }
  54. IndexKey() { key = 0; }
  55. };
  56. /**
  57. * @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.
  58. */
  59. union Cell {
  60. struct {
  61. unsigned int item : 16;
  62. unsigned int rot : 5;
  63. unsigned int layer : 8;
  64. };
  65. uint32_t cell;
  66. Cell() {
  67. item = 0;
  68. rot = 0;
  69. layer = 0;
  70. }
  71. };
  72. /**
  73. * @brief An Octant is a prism containing Cells, and possibly belonging to an Area.
  74. * A GridMap can have multiple Octants.
  75. */
  76. struct Octant {
  77. struct NavMesh {
  78. int id;
  79. Transform xform;
  80. };
  81. struct MultimeshInstance {
  82. RID instance;
  83. RID multimesh;
  84. struct Item {
  85. int index;
  86. Transform transform;
  87. IndexKey key;
  88. };
  89. Vector<Item> items; //tools only, for changing visibility
  90. };
  91. Vector<MultimeshInstance> multimesh_instances;
  92. Set<IndexKey> cells;
  93. RID collision_debug;
  94. RID collision_debug_instance;
  95. bool dirty;
  96. RID static_body;
  97. Map<IndexKey, NavMesh> navmesh_ids;
  98. };
  99. union OctantKey {
  100. struct {
  101. int16_t x;
  102. int16_t y;
  103. int16_t z;
  104. int16_t empty;
  105. };
  106. uint64_t key;
  107. _FORCE_INLINE_ bool operator<(const OctantKey &p_key) const {
  108. return key < p_key.key;
  109. }
  110. //OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; }
  111. OctantKey() { key = 0; }
  112. };
  113. uint32_t collision_layer;
  114. uint32_t collision_mask;
  115. Transform last_transform;
  116. bool _in_tree;
  117. Vector3 cell_size;
  118. int octant_size;
  119. bool center_x, center_y, center_z;
  120. float cell_scale;
  121. Navigation *navigation;
  122. bool clip;
  123. bool clip_above;
  124. int clip_floor;
  125. bool recreating_octants;
  126. Vector3::Axis clip_axis;
  127. Ref<MeshLibrary> mesh_library;
  128. Map<OctantKey, Octant *> octant_map;
  129. Map<IndexKey, Cell> cell_map;
  130. void _recreate_octant_data();
  131. struct BakeLight {
  132. VS::LightType type;
  133. Vector3 pos;
  134. Vector3 dir;
  135. float param[VS::LIGHT_PARAM_MAX];
  136. };
  137. _FORCE_INLINE_ Vector3 _octant_get_offset(const OctantKey &p_key) const {
  138. return Vector3(p_key.x, p_key.y, p_key.z) * cell_size * octant_size;
  139. }
  140. void _reset_physic_bodies_collision_filters();
  141. void _octant_enter_world(const OctantKey &p_key);
  142. void _octant_exit_world(const OctantKey &p_key);
  143. bool _octant_update(const OctantKey &p_key);
  144. void _octant_clean_up(const OctantKey &p_key);
  145. void _octant_transform(const OctantKey &p_key);
  146. bool awaiting_update;
  147. void _queue_octants_dirty();
  148. void _update_octants_callback();
  149. void resource_changed(const RES &p_res);
  150. void _clear_internal();
  151. Vector3 _get_offset() const;
  152. struct BakedMesh {
  153. Ref<Mesh> mesh;
  154. RID instance;
  155. };
  156. Vector<BakedMesh> baked_meshes;
  157. protected:
  158. bool _set(const StringName &p_name, const Variant &p_value);
  159. bool _get(const StringName &p_name, Variant &r_ret) const;
  160. void _get_property_list(List<PropertyInfo> *p_list) const;
  161. void _notification(int p_what);
  162. void _update_visibility();
  163. static void _bind_methods();
  164. public:
  165. enum {
  166. INVALID_CELL_ITEM = -1
  167. };
  168. void set_collision_layer(uint32_t p_layer);
  169. uint32_t get_collision_layer() const;
  170. void set_collision_mask(uint32_t p_mask);
  171. uint32_t get_collision_mask() const;
  172. void set_collision_layer_bit(int p_bit, bool p_value);
  173. bool get_collision_layer_bit(int p_bit) const;
  174. void set_collision_mask_bit(int p_bit, bool p_value);
  175. bool get_collision_mask_bit(int p_bit) const;
  176. #ifndef DISABLE_DEPRECATED
  177. void set_theme(const Ref<MeshLibrary> &p_theme);
  178. Ref<MeshLibrary> get_theme() const;
  179. #endif // DISABLE_DEPRECATED
  180. void set_mesh_library(const Ref<MeshLibrary> &p_mesh_library);
  181. Ref<MeshLibrary> get_mesh_library() const;
  182. void set_cell_size(const Vector3 &p_size);
  183. Vector3 get_cell_size() const;
  184. void set_octant_size(int p_size);
  185. int get_octant_size() const;
  186. void set_center_x(bool p_enable);
  187. bool get_center_x() const;
  188. void set_center_y(bool p_enable);
  189. bool get_center_y() const;
  190. void set_center_z(bool p_enable);
  191. bool get_center_z() const;
  192. void set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot = 0);
  193. int get_cell_item(int p_x, int p_y, int p_z) const;
  194. int get_cell_item_orientation(int p_x, int p_y, int p_z) const;
  195. Vector3 world_to_map(const Vector3 &p_world_pos) const;
  196. Vector3 map_to_world(int p_x, int p_y, int p_z) const;
  197. void set_clip(bool p_enabled, bool p_clip_above = true, int p_floor = 0, Vector3::Axis p_axis = Vector3::AXIS_X);
  198. void set_cell_scale(float p_scale);
  199. float get_cell_scale() const;
  200. Array get_used_cells() const;
  201. Array get_meshes();
  202. void clear_baked_meshes();
  203. void make_baked_meshes(bool p_gen_lightmap_uv = false, float p_lightmap_uv_texel_size = 0.1);
  204. void clear();
  205. Array get_bake_meshes();
  206. RID get_bake_mesh_instance(int p_idx);
  207. GridMap();
  208. ~GridMap();
  209. };
  210. #endif // GRID_MAP_H