grid_map.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*************************************************************************/
  2. /* grid_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 BakedLightInstance;
  39. class GridMap : public Spatial {
  40. OBJ_TYPE(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. int id;
  80. Transform xform;
  81. };
  82. struct ItemInstances {
  83. Set<IndexKey> cells;
  84. Ref<Mesh> mesh;
  85. Ref<Shape> shape;
  86. Ref<MultiMesh> multimesh;
  87. RID multimesh_instance;
  88. Ref<NavigationMesh> navmesh;
  89. };
  90. Ref<Mesh> baked;
  91. RID bake_instance;
  92. RID collision_debug;
  93. RID collision_debug_instance;
  94. bool dirty;
  95. RID static_body;
  96. Map<int, ItemInstances> items;
  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 area;
  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. Transform last_transform;
  114. bool _in_tree;
  115. float cell_size;
  116. int octant_size;
  117. bool center_x, center_y, center_z;
  118. bool bake;
  119. float cell_scale;
  120. Navigation *navigation;
  121. bool clip;
  122. bool clip_above;
  123. int clip_floor;
  124. bool baked_lock;
  125. Vector3::Axis clip_axis;
  126. /**
  127. * @brief An Area is something like a room: it has doors, and Octants can choose to belong to it.
  128. */
  129. struct Area {
  130. String name;
  131. RID base_portal;
  132. RID instance;
  133. IndexKey from;
  134. IndexKey to;
  135. struct Portal {
  136. Transform xform;
  137. RID instance;
  138. ~Portal();
  139. };
  140. Vector<Portal> portals;
  141. float portal_disable_distance;
  142. Color portal_disable_color;
  143. bool exterior_portal;
  144. Area();
  145. ~Area();
  146. };
  147. Ref<MeshLibrary> theme;
  148. Map<OctantKey, Octant *> octant_map;
  149. Map<IndexKey, Cell> cell_map;
  150. Map<int, Area *> area_map;
  151. void _recreate_octant_data();
  152. struct BakeLight {
  153. VS::LightType type;
  154. Vector3 pos;
  155. Vector3 dir;
  156. float param[VS::LIGHT_PARAM_MAX];
  157. };
  158. _FORCE_INLINE_ int _find_area(const IndexKey &p_pos) const;
  159. _FORCE_INLINE_ Vector3 _octant_get_offset(const OctantKey &p_key) const {
  160. return Vector3(p_key.x, p_key.y, p_key.z) * cell_size * octant_size;
  161. }
  162. void _octant_enter_world(const OctantKey &p_key);
  163. void _octant_enter_tree(const OctantKey &p_key);
  164. void _octant_exit_world(const OctantKey &p_key);
  165. void _octant_update(const OctantKey &p_key);
  166. void _octant_transform(const OctantKey &p_key);
  167. void _octant_clear_baked(const OctantKey &p_key);
  168. void _octant_clear_navmesh(const GridMap::OctantKey &);
  169. void _octant_bake(const OctantKey &p_key, const Ref<TriangleMesh> &p_tmesh = RES(), const Vector<BakeLight> &p_lights = Vector<BakeLight>(), List<Vector3> *r_prebake = NULL);
  170. bool awaiting_update;
  171. void _queue_dirty_map();
  172. void _update_dirty_map_callback();
  173. void resource_changed(const RES &p_res);
  174. void _update_areas();
  175. void _update_area_instances();
  176. void _clear_internal(bool p_keep_areas = false);
  177. BakedLightInstance *baked_light_instance;
  178. bool use_baked_light;
  179. void _find_baked_light();
  180. void _baked_light_changed();
  181. Array _get_baked_light_meshes();
  182. protected:
  183. bool _set(const StringName &p_name, const Variant &p_value);
  184. bool _get(const StringName &p_name, Variant &r_ret) const;
  185. void _get_property_list(List<PropertyInfo> *p_list) const;
  186. void _notification(int p_what);
  187. static void _bind_methods();
  188. public:
  189. enum {
  190. INVALID_CELL_ITEM = -1
  191. };
  192. void set_theme(const Ref<MeshLibrary> &p_theme);
  193. Ref<MeshLibrary> get_theme() const;
  194. void set_cell_size(float p_size);
  195. float get_cell_size() const;
  196. void set_octant_size(int p_size);
  197. int get_octant_size() const;
  198. void set_center_x(bool p_enable);
  199. bool get_center_x() const;
  200. void set_center_y(bool p_enable);
  201. bool get_center_y() const;
  202. void set_center_z(bool p_enable);
  203. bool get_center_z() const;
  204. void set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_orientation = 0);
  205. int get_cell_item(int p_x, int p_y, int p_z) const;
  206. int get_cell_item_orientation(int p_x, int p_y, int p_z) const;
  207. void set_clip(bool p_enabled, bool p_clip_above = true, int p_floor = 0, Vector3::Axis p_axis = Vector3::AXIS_X);
  208. Error create_area(int p_id, const AABB &p_area);
  209. AABB area_get_bounds(int p_area) const;
  210. void area_set_exterior_portal(int p_area, bool p_enable);
  211. void area_set_name(int p_area, const String &p_name);
  212. String area_get_name(int p_area) const;
  213. bool area_is_exterior_portal(int p_area) const;
  214. void area_set_portal_disable_distance(int p_area, float p_distance);
  215. float area_get_portal_disable_distance(int p_area) const;
  216. void area_set_portal_disable_color(int p_area, Color p_color);
  217. Color area_get_portal_disable_color(int p_area) const;
  218. void get_area_list(List<int> *p_areas) const;
  219. void erase_area(int p_area);
  220. int get_unused_area_id() const;
  221. void set_cell_scale(float p_scale);
  222. float get_cell_scale() const;
  223. void set_bake(bool p_bake);
  224. bool is_baking_enabled() const;
  225. void bake_geometry();
  226. void set_use_baked_light(bool p_use);
  227. bool is_using_baked_light() const;
  228. void clear();
  229. GridMap();
  230. ~GridMap();
  231. };
  232. #endif // CUBE_GRID_MAP_H