grid_map.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-2015 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 BakedLightInstance;
  37. class GridMap : public Spatial {
  38. OBJ_TYPE( GridMap, Spatial );
  39. enum {
  40. MAP_DIRTY_TRANSFORMS=1,
  41. MAP_DIRTY_INSTANCES=2,
  42. };
  43. union IndexKey {
  44. struct {
  45. int16_t x;
  46. int16_t y;
  47. int16_t z;
  48. };
  49. uint64_t key;
  50. _FORCE_INLINE_ bool operator<(const IndexKey& p_key) const {
  51. return key < p_key.key;
  52. }
  53. IndexKey() { key=0; }
  54. };
  55. union Cell {
  56. struct {
  57. unsigned int item : 16;
  58. unsigned int rot:5;
  59. unsigned int layer:8;
  60. };
  61. uint32_t cell;
  62. Cell() { item=0; rot=0; layer=0; }
  63. };
  64. struct Octant {
  65. struct ItemInstances {
  66. Set<IndexKey> cells;
  67. Ref<Mesh> mesh;
  68. Ref<Shape> shape;
  69. Ref<MultiMesh> multimesh;
  70. RID multimesh_instance;
  71. };
  72. Ref<Mesh> baked;
  73. RID bake_instance;
  74. bool dirty;
  75. RID static_body;
  76. Map<int,ItemInstances> items;
  77. };
  78. union OctantKey {
  79. struct {
  80. int16_t x;
  81. int16_t y;
  82. int16_t z;
  83. int16_t area;
  84. };
  85. uint64_t key;
  86. _FORCE_INLINE_ bool operator<(const OctantKey& p_key) const {
  87. return key < p_key.key;
  88. }
  89. //OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; }
  90. OctantKey() { key=0; }
  91. };
  92. Transform last_transform;
  93. bool _in_tree;
  94. float cell_size;
  95. int octant_size;
  96. bool center_x,center_y,center_z;
  97. bool bake;
  98. float cell_scale;
  99. bool clip;
  100. bool clip_above;
  101. int clip_floor;
  102. bool baked_lock;
  103. Vector3::Axis clip_axis;
  104. struct Area {
  105. String name;
  106. RID base_portal;
  107. RID instance;
  108. IndexKey from;
  109. IndexKey to;
  110. struct Portal {
  111. Transform xform;
  112. RID instance;
  113. ~Portal();
  114. };
  115. Vector<Portal> portals;
  116. float portal_disable_distance;
  117. Color portal_disable_color;
  118. bool exterior_portal;
  119. Area();
  120. ~Area();
  121. };
  122. Ref<MeshLibrary> theme;
  123. Map<OctantKey,Octant*> octant_map;
  124. Map<IndexKey,Cell> cell_map;
  125. Map<int,Area*> area_map;
  126. void _recreate_octant_data();
  127. struct BakeLight {
  128. VS::LightType type;
  129. Vector3 pos;
  130. Vector3 dir;
  131. float param[VS::LIGHT_PARAM_MAX];
  132. };
  133. _FORCE_INLINE_ int _find_area(const IndexKey& p_pos) const;
  134. _FORCE_INLINE_ Vector3 _octant_get_offset(const OctantKey &p_key) const {
  135. return Vector3(p_key.x,p_key.y,p_key.z)*cell_size*octant_size;
  136. }
  137. void _octant_enter_world(const OctantKey &p_key);
  138. void _octant_exit_world(const OctantKey &p_key);
  139. void _octant_update(const OctantKey &p_key);
  140. void _octant_transform(const OctantKey &p_key);
  141. void _octant_clear_baked(const OctantKey &p_key);
  142. 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);
  143. bool awaiting_update;
  144. void _queue_dirty_map();
  145. void _update_dirty_map_callback();
  146. void resource_changed(const RES& p_res);
  147. void _update_areas();
  148. void _update_area_instances();
  149. void _clear_internal(bool p_keep_areas=false);
  150. BakedLightInstance *baked_light_instance;
  151. bool use_baked_light;
  152. void _find_baked_light();
  153. void _baked_light_changed();
  154. Array _get_baked_light_meshes();
  155. protected:
  156. bool _set(const StringName& p_name, const Variant& p_value);
  157. bool _get(const StringName& p_name,Variant &r_ret) const;
  158. void _get_property_list( List<PropertyInfo> *p_list) const;
  159. void _notification(int p_what);
  160. static void _bind_methods();
  161. public:
  162. enum {
  163. INVALID_CELL_ITEM=-1
  164. };
  165. void set_theme(const Ref<MeshLibrary>& p_theme);
  166. Ref<MeshLibrary> get_theme() const;
  167. void set_cell_size(float p_size);
  168. float get_cell_size() const;
  169. void set_octant_size(int p_size);
  170. int get_octant_size() const;
  171. void set_center_x(bool p_enable);
  172. bool get_center_x() const;
  173. void set_center_y(bool p_enable);
  174. bool get_center_y() const;
  175. void set_center_z(bool p_enable);
  176. bool get_center_z() const;
  177. void set_cell_item(int p_x,int p_y,int p_z, int p_item,int p_orientation=0);
  178. int get_cell_item(int p_x,int p_y,int p_z) const;
  179. int get_cell_item_orientation(int p_x,int p_y,int p_z) const;
  180. void set_clip(bool p_enabled, bool p_clip_above=true, int p_floor=0, Vector3::Axis p_axis=Vector3::AXIS_X);
  181. Error create_area(int p_id,const AABB& p_area);
  182. AABB area_get_bounds(int p_area) const;
  183. void area_set_exterior_portal(int p_area,bool p_enable);
  184. void area_set_name(int p_area,const String& p_name);
  185. String area_get_name(int p_area) const;
  186. bool area_is_exterior_portal(int p_area) const;
  187. void area_set_portal_disable_distance(int p_area, float p_distance);
  188. float area_get_portal_disable_distance(int p_area) const;
  189. void area_set_portal_disable_color(int p_area, Color p_color);
  190. Color area_get_portal_disable_color(int p_area) const;
  191. void get_area_list(List<int> *p_areas) const;
  192. void erase_area(int p_area);
  193. int get_unused_area_id() const;
  194. void set_cell_scale(float p_scale);
  195. float get_cell_scale() const;
  196. void set_bake(bool p_bake);
  197. bool is_baking_enabled() const;
  198. void bake_geometry();
  199. void set_use_baked_light(bool p_use);
  200. bool is_using_baked_light() const;
  201. void clear();
  202. GridMap();
  203. ~GridMap();
  204. };
  205. #endif // CUBE_GRID_MAP_H