grid_map.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* grid_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef GRID_MAP_H
  30. #define GRID_MAP_H
  31. #include "scene/resources/mesh_library.h"
  32. #include "scene/3d/spatial.h"
  33. #include "scene/resources/multimesh.h"
  34. //heh heh, godotsphir!! this shares no code and the design is completely different with previous projects i've done..
  35. //should scale better with hardware that supports instancing
  36. class GridMap : public Spatial {
  37. OBJ_TYPE( GridMap, Spatial );
  38. enum {
  39. MAP_DIRTY_TRANSFORMS=1,
  40. MAP_DIRTY_INSTANCES=2,
  41. };
  42. union IndexKey {
  43. struct {
  44. int16_t x;
  45. int16_t y;
  46. int16_t z;
  47. };
  48. uint64_t key;
  49. _FORCE_INLINE_ bool operator<(const IndexKey& p_key) const {
  50. return key < p_key.key;
  51. }
  52. IndexKey() { key=0; }
  53. };
  54. union Cell {
  55. struct {
  56. unsigned int item : 16;
  57. unsigned int rot:5;
  58. unsigned int layer:8;
  59. };
  60. uint32_t cell;
  61. Cell() { item=0; rot=0; layer=0; }
  62. };
  63. struct Octant {
  64. struct ItemInstances {
  65. Set<IndexKey> cells;
  66. Ref<Mesh> mesh;
  67. Ref<Shape> shape;
  68. Ref<MultiMesh> multimesh;
  69. RID multimesh_instance;
  70. };
  71. Ref<Mesh> baked;
  72. RID bake_instance;
  73. bool dirty;
  74. RID static_body;
  75. Map<int,ItemInstances> items;
  76. };
  77. union OctantKey {
  78. struct {
  79. int16_t x;
  80. int16_t y;
  81. int16_t z;
  82. int16_t area;
  83. };
  84. uint64_t key;
  85. _FORCE_INLINE_ bool operator<(const OctantKey& p_key) const {
  86. return key < p_key.key;
  87. }
  88. //OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; }
  89. OctantKey() { key=0; }
  90. };
  91. Transform last_transform;
  92. bool _in_tree;
  93. float cell_size;
  94. int octant_size;
  95. bool center_x,center_y,center_z;
  96. bool bake;
  97. float cell_scale;
  98. bool clip;
  99. bool clip_above;
  100. int clip_floor;
  101. bool baked_lock;
  102. Vector3::Axis clip_axis;
  103. struct Area {
  104. String name;
  105. RID base_portal;
  106. RID instance;
  107. IndexKey from;
  108. IndexKey to;
  109. struct Portal {
  110. Transform xform;
  111. RID instance;
  112. ~Portal();
  113. };
  114. Vector<Portal> portals;
  115. float portal_disable_distance;
  116. Color portal_disable_color;
  117. bool exterior_portal;
  118. Area();
  119. ~Area();
  120. };
  121. Ref<MeshLibrary> theme;
  122. Map<OctantKey,Octant*> octant_map;
  123. Map<IndexKey,Cell> cell_map;
  124. Map<int,Area*> area_map;
  125. void _recreate_octant_data();
  126. struct BakeLight {
  127. VS::LightType type;
  128. Vector3 pos;
  129. Vector3 dir;
  130. float param[VS::LIGHT_PARAM_MAX];
  131. };
  132. _FORCE_INLINE_ int _find_area(const IndexKey& p_pos) const;
  133. _FORCE_INLINE_ Vector3 _octant_get_offset(const OctantKey &p_key) const {
  134. return Vector3(p_key.x,p_key.y,p_key.z)*cell_size*octant_size;
  135. }
  136. void _octant_enter_world(const OctantKey &p_key);
  137. void _octant_exit_world(const OctantKey &p_key);
  138. void _octant_update(const OctantKey &p_key);
  139. void _octant_transform(const OctantKey &p_key);
  140. void _octant_clear_baked(const OctantKey &p_key);
  141. 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);
  142. bool awaiting_update;
  143. void _queue_dirty_map();
  144. void _update_dirty_map_callback();
  145. void resource_changed(const RES& p_res);
  146. void _update_areas();
  147. void _update_area_instances();
  148. void _clear_internal(bool p_keep_areas=false);
  149. protected:
  150. bool _set(const StringName& p_name, const Variant& p_value);
  151. bool _get(const StringName& p_name,Variant &r_ret) const;
  152. void _get_property_list( List<PropertyInfo> *p_list) const;
  153. void _notification(int p_what);
  154. static void _bind_methods();
  155. public:
  156. enum {
  157. INVALID_CELL_ITEM=-1
  158. };
  159. void set_theme(const Ref<MeshLibrary>& p_theme);
  160. Ref<MeshLibrary> get_theme() const;
  161. void set_cell_size(float p_size);
  162. float get_cell_size() const;
  163. void set_octant_size(int p_size);
  164. int get_octant_size() const;
  165. void set_center_x(bool p_enable);
  166. bool get_center_x() const;
  167. void set_center_y(bool p_enable);
  168. bool get_center_y() const;
  169. void set_center_z(bool p_enable);
  170. bool get_center_z() const;
  171. void set_cell_item(int p_x,int p_y,int p_z, int p_item,int p_orientation=0);
  172. int get_cell_item(int p_x,int p_y,int p_z) const;
  173. int get_cell_item_orientation(int p_x,int p_y,int p_z) const;
  174. void set_clip(bool p_enabled, bool p_clip_above=true, int p_floor=0, Vector3::Axis p_axis=Vector3::AXIS_X);
  175. Error create_area(int p_id,const AABB& p_area);
  176. AABB area_get_bounds(int p_area) const;
  177. void area_set_exterior_portal(int p_area,bool p_enable);
  178. void area_set_name(int p_area,const String& p_name);
  179. String area_get_name(int p_area) const;
  180. bool area_is_exterior_portal(int p_area) const;
  181. void area_set_portal_disable_distance(int p_area, float p_distance);
  182. float area_get_portal_disable_distance(int p_area) const;
  183. void area_set_portal_disable_color(int p_area, Color p_color);
  184. Color area_get_portal_disable_color(int p_area) const;
  185. void get_area_list(List<int> *p_areas) const;
  186. void erase_area(int p_area);
  187. int get_unused_area_id() const;
  188. void set_cell_scale(float p_scale);
  189. float get_cell_scale() const;
  190. void set_bake(bool p_bake);
  191. bool is_baking_enabled() const;
  192. void bake_geometry();
  193. void clear();
  194. GridMap();
  195. ~GridMap();
  196. };
  197. #endif // CUBE_GRID_MAP_H