grid_map.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /*************************************************************************/
  2. /* grid_map.cpp */
  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. #include "grid_map.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/message_queue.h"
  33. #include "scene/3d/light.h"
  34. #include "scene/resources/mesh_library.h"
  35. #include "scene/resources/surface_tool.h"
  36. #include "scene/scene_string_names.h"
  37. #include "servers/visual_server.h"
  38. bool GridMap::_set(const StringName &p_name, const Variant &p_value) {
  39. String name = p_name;
  40. if (name == "data") {
  41. Dictionary d = p_value;
  42. if (d.has("cells")) {
  43. PoolVector<int> cells = d["cells"];
  44. int amount = cells.size();
  45. PoolVector<int>::Read r = cells.read();
  46. ERR_FAIL_COND_V(amount % 3, false); // not even
  47. cell_map.clear();
  48. for (int i = 0; i < amount / 3; i++) {
  49. IndexKey ik;
  50. ik.key = decode_uint64((const uint8_t *)&r[i * 3]);
  51. Cell cell;
  52. cell.cell = decode_uint32((const uint8_t *)&r[i * 3 + 2]);
  53. cell_map[ik] = cell;
  54. }
  55. }
  56. _recreate_octant_data();
  57. } else if (name == "baked_meshes") {
  58. clear_baked_meshes();
  59. Array meshes = p_value;
  60. for (int i = 0; i < meshes.size(); i++) {
  61. BakedMesh bm;
  62. bm.mesh = meshes[i];
  63. ERR_CONTINUE(!bm.mesh.is_valid());
  64. bm.instance = VS::get_singleton()->instance_create();
  65. VS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  66. VS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  67. if (is_inside_tree()) {
  68. VS::get_singleton()->instance_set_scenario(bm.instance, get_world()->get_scenario());
  69. VS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  70. }
  71. baked_meshes.push_back(bm);
  72. }
  73. _recreate_octant_data();
  74. } else {
  75. return false;
  76. }
  77. return true;
  78. }
  79. bool GridMap::_get(const StringName &p_name, Variant &r_ret) const {
  80. String name = p_name;
  81. if (name == "data") {
  82. Dictionary d;
  83. PoolVector<int> cells;
  84. cells.resize(cell_map.size() * 3);
  85. {
  86. PoolVector<int>::Write w = cells.write();
  87. int i = 0;
  88. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next(), i++) {
  89. encode_uint64(E->key().key, (uint8_t *)&w[i * 3]);
  90. encode_uint32(E->get().cell, (uint8_t *)&w[i * 3 + 2]);
  91. }
  92. }
  93. d["cells"] = cells;
  94. r_ret = d;
  95. } else if (name == "baked_meshes") {
  96. Array ret;
  97. ret.resize(baked_meshes.size());
  98. for (int i = 0; i < baked_meshes.size(); i++) {
  99. ret.push_back(baked_meshes[i].mesh);
  100. }
  101. r_ret = ret;
  102. } else
  103. return false;
  104. return true;
  105. }
  106. void GridMap::_get_property_list(List<PropertyInfo> *p_list) const {
  107. if (baked_meshes.size()) {
  108. p_list->push_back(PropertyInfo(Variant::ARRAY, "baked_meshes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  109. }
  110. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  111. }
  112. void GridMap::set_collision_layer(uint32_t p_layer) {
  113. collision_layer = p_layer;
  114. _reset_physic_bodies_collision_filters();
  115. }
  116. uint32_t GridMap::get_collision_layer() const {
  117. return collision_layer;
  118. }
  119. void GridMap::set_collision_mask(uint32_t p_mask) {
  120. collision_mask = p_mask;
  121. _reset_physic_bodies_collision_filters();
  122. }
  123. uint32_t GridMap::get_collision_mask() const {
  124. return collision_mask;
  125. }
  126. void GridMap::set_collision_mask_bit(int p_bit, bool p_value) {
  127. uint32_t mask = get_collision_mask();
  128. if (p_value)
  129. mask |= 1 << p_bit;
  130. else
  131. mask &= ~(1 << p_bit);
  132. set_collision_mask(mask);
  133. }
  134. bool GridMap::get_collision_mask_bit(int p_bit) const {
  135. return get_collision_mask() & (1 << p_bit);
  136. }
  137. void GridMap::set_collision_layer_bit(int p_bit, bool p_value) {
  138. uint32_t mask = get_collision_layer();
  139. if (p_value)
  140. mask |= 1 << p_bit;
  141. else
  142. mask &= ~(1 << p_bit);
  143. set_collision_layer(mask);
  144. }
  145. bool GridMap::get_collision_layer_bit(int p_bit) const {
  146. return get_collision_layer() & (1 << p_bit);
  147. }
  148. #ifndef DISABLE_DEPRECATED
  149. void GridMap::set_theme(const Ref<MeshLibrary> &p_theme) {
  150. ERR_EXPLAIN("GridMap.theme/set_theme() is deprecated and will be removed in a future version. Use GridMap.mesh_library/set_mesh_library() instead.");
  151. WARN_DEPRECATED;
  152. set_mesh_library(p_theme);
  153. }
  154. Ref<MeshLibrary> GridMap::get_theme() const {
  155. ERR_EXPLAIN("GridMap.theme/get_theme() is deprecated and will be removed in a future version. Use GridMap.mesh_library/get_mesh_library() instead.");
  156. WARN_DEPRECATED;
  157. return get_mesh_library();
  158. }
  159. #endif // DISABLE_DEPRECATED
  160. void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) {
  161. if (!mesh_library.is_null())
  162. mesh_library->unregister_owner(this);
  163. mesh_library = p_mesh_library;
  164. if (!mesh_library.is_null())
  165. mesh_library->register_owner(this);
  166. _recreate_octant_data();
  167. _change_notify("mesh_library");
  168. }
  169. Ref<MeshLibrary> GridMap::get_mesh_library() const {
  170. return mesh_library;
  171. }
  172. void GridMap::set_cell_size(const Vector3 &p_size) {
  173. ERR_FAIL_COND(p_size.x < 0.001 || p_size.y < 0.001 || p_size.z < 0.001);
  174. cell_size = p_size;
  175. _recreate_octant_data();
  176. }
  177. Vector3 GridMap::get_cell_size() const {
  178. return cell_size;
  179. }
  180. void GridMap::set_octant_size(int p_size) {
  181. ERR_FAIL_COND(p_size == 0);
  182. octant_size = p_size;
  183. _recreate_octant_data();
  184. }
  185. int GridMap::get_octant_size() const {
  186. return octant_size;
  187. }
  188. void GridMap::set_center_x(bool p_enable) {
  189. center_x = p_enable;
  190. _recreate_octant_data();
  191. }
  192. bool GridMap::get_center_x() const {
  193. return center_x;
  194. }
  195. void GridMap::set_center_y(bool p_enable) {
  196. center_y = p_enable;
  197. _recreate_octant_data();
  198. }
  199. bool GridMap::get_center_y() const {
  200. return center_y;
  201. }
  202. void GridMap::set_center_z(bool p_enable) {
  203. center_z = p_enable;
  204. _recreate_octant_data();
  205. }
  206. bool GridMap::get_center_z() const {
  207. return center_z;
  208. }
  209. void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
  210. if (baked_meshes.size() && !recreating_octants) {
  211. //if you set a cell item, baked meshes go good bye
  212. clear_baked_meshes();
  213. _recreate_octant_data();
  214. }
  215. ERR_FAIL_INDEX(ABS(p_x), 1 << 20);
  216. ERR_FAIL_INDEX(ABS(p_y), 1 << 20);
  217. ERR_FAIL_INDEX(ABS(p_z), 1 << 20);
  218. IndexKey key;
  219. key.x = p_x;
  220. key.y = p_y;
  221. key.z = p_z;
  222. OctantKey ok;
  223. ok.x = p_x / octant_size;
  224. ok.y = p_y / octant_size;
  225. ok.z = p_z / octant_size;
  226. if (p_item < 0) {
  227. //erase
  228. if (cell_map.has(key)) {
  229. OctantKey octantkey = ok;
  230. ERR_FAIL_COND(!octant_map.has(octantkey));
  231. Octant &g = *octant_map[octantkey];
  232. g.cells.erase(key);
  233. g.dirty = true;
  234. cell_map.erase(key);
  235. _queue_octants_dirty();
  236. }
  237. return;
  238. }
  239. OctantKey octantkey = ok;
  240. if (!octant_map.has(octantkey)) {
  241. //create octant because it does not exist
  242. Octant *g = memnew(Octant);
  243. g->dirty = true;
  244. g->static_body = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
  245. PhysicsServer::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
  246. PhysicsServer::get_singleton()->body_set_collision_layer(g->static_body, collision_layer);
  247. PhysicsServer::get_singleton()->body_set_collision_mask(g->static_body, collision_mask);
  248. SceneTree *st = SceneTree::get_singleton();
  249. if (st && st->is_debugging_collisions_hint()) {
  250. g->collision_debug = VisualServer::get_singleton()->mesh_create();
  251. g->collision_debug_instance = VisualServer::get_singleton()->instance_create();
  252. VisualServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  253. }
  254. octant_map[octantkey] = g;
  255. if (is_inside_world()) {
  256. _octant_enter_world(octantkey);
  257. _octant_transform(octantkey);
  258. }
  259. }
  260. Octant &g = *octant_map[octantkey];
  261. g.cells.insert(key);
  262. g.dirty = true;
  263. _queue_octants_dirty();
  264. Cell c;
  265. c.item = p_item;
  266. c.rot = p_rot;
  267. cell_map[key] = c;
  268. }
  269. int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
  270. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, INVALID_CELL_ITEM);
  271. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, INVALID_CELL_ITEM);
  272. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, INVALID_CELL_ITEM);
  273. IndexKey key;
  274. key.x = p_x;
  275. key.y = p_y;
  276. key.z = p_z;
  277. if (!cell_map.has(key))
  278. return INVALID_CELL_ITEM;
  279. return cell_map[key].item;
  280. }
  281. int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const {
  282. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, -1);
  283. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, -1);
  284. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, -1);
  285. IndexKey key;
  286. key.x = p_x;
  287. key.y = p_y;
  288. key.z = p_z;
  289. if (!cell_map.has(key))
  290. return -1;
  291. return cell_map[key].rot;
  292. }
  293. Vector3 GridMap::world_to_map(const Vector3 &p_world_pos) const {
  294. Vector3 map_pos = p_world_pos / cell_size;
  295. map_pos.x = floor(map_pos.x);
  296. map_pos.y = floor(map_pos.y);
  297. map_pos.z = floor(map_pos.z);
  298. return map_pos;
  299. }
  300. Vector3 GridMap::map_to_world(int p_x, int p_y, int p_z) const {
  301. Vector3 offset = _get_offset();
  302. Vector3 world_pos(
  303. p_x * cell_size.x + offset.x,
  304. p_y * cell_size.y + offset.y,
  305. p_z * cell_size.z + offset.z);
  306. return world_pos;
  307. }
  308. void GridMap::_octant_transform(const OctantKey &p_key) {
  309. ERR_FAIL_COND(!octant_map.has(p_key));
  310. Octant &g = *octant_map[p_key];
  311. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  312. if (g.collision_debug_instance.is_valid()) {
  313. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  314. }
  315. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  316. VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  317. }
  318. }
  319. bool GridMap::_octant_update(const OctantKey &p_key) {
  320. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  321. Octant &g = *octant_map[p_key];
  322. if (!g.dirty)
  323. return false;
  324. //erase body shapes
  325. PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
  326. //erase body shapes debug
  327. if (g.collision_debug.is_valid()) {
  328. VS::get_singleton()->mesh_clear(g.collision_debug);
  329. }
  330. //erase navigation
  331. if (navigation) {
  332. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  333. navigation->navmesh_remove(E->get().id);
  334. }
  335. g.navmesh_ids.clear();
  336. }
  337. //erase multimeshes
  338. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  339. VS::get_singleton()->free(g.multimesh_instances[i].instance);
  340. VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  341. }
  342. g.multimesh_instances.clear();
  343. if (g.cells.size() == 0) {
  344. //octant no longer needed
  345. _octant_clean_up(p_key);
  346. return true;
  347. }
  348. PoolVector<Vector3> col_debug;
  349. /*
  350. * foreach item in this octant,
  351. * set item's multimesh's instance count to number of cells which have this item
  352. * and set said multimesh bounding box to one containing all cells which have this item
  353. */
  354. Map<int, List<Pair<Transform, IndexKey> > > multimesh_items;
  355. for (Set<IndexKey>::Element *E = g.cells.front(); E; E = E->next()) {
  356. ERR_CONTINUE(!cell_map.has(E->get()));
  357. const Cell &c = cell_map[E->get()];
  358. if (!mesh_library.is_valid() || !mesh_library->has_item(c.item))
  359. continue;
  360. Vector3 cellpos = Vector3(E->get().x, E->get().y, E->get().z);
  361. Vector3 ofs = _get_offset();
  362. Transform xform;
  363. xform.basis.set_orthogonal_index(c.rot);
  364. xform.set_origin(cellpos * cell_size + ofs);
  365. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  366. if (baked_meshes.size() == 0) {
  367. if (mesh_library->get_item_mesh(c.item).is_valid()) {
  368. if (!multimesh_items.has(c.item)) {
  369. multimesh_items[c.item] = List<Pair<Transform, IndexKey> >();
  370. }
  371. Pair<Transform, IndexKey> p;
  372. p.first = xform;
  373. p.second = E->get();
  374. multimesh_items[c.item].push_back(p);
  375. }
  376. }
  377. Vector<MeshLibrary::ShapeData> shapes = mesh_library->get_item_shapes(c.item);
  378. // add the item's shape at given xform to octant's static_body
  379. for (int i = 0; i < shapes.size(); i++) {
  380. // add the item's shape
  381. if (!shapes[i].shape.is_valid())
  382. continue;
  383. PhysicsServer::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  384. if (g.collision_debug.is_valid()) {
  385. shapes.write[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  386. }
  387. }
  388. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  389. Ref<NavigationMesh> navmesh = mesh_library->get_item_navmesh(c.item);
  390. if (navmesh.is_valid()) {
  391. Octant::NavMesh nm;
  392. nm.xform = xform * mesh_library->get_item_navmesh_transform(c.item);
  393. if (navigation) {
  394. nm.id = navigation->navmesh_add(navmesh, xform, this);
  395. } else {
  396. nm.id = -1;
  397. }
  398. g.navmesh_ids[E->get()] = nm;
  399. }
  400. }
  401. //update multimeshes, only if not baked
  402. if (baked_meshes.size() == 0) {
  403. for (Map<int, List<Pair<Transform, IndexKey> > >::Element *E = multimesh_items.front(); E; E = E->next()) {
  404. Octant::MultimeshInstance mmi;
  405. RID mm = VS::get_singleton()->multimesh_create();
  406. VS::get_singleton()->multimesh_allocate(mm, E->get().size(), VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_NONE);
  407. VS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E->key())->get_rid());
  408. int idx = 0;
  409. for (List<Pair<Transform, IndexKey> >::Element *F = E->get().front(); F; F = F->next()) {
  410. VS::get_singleton()->multimesh_instance_set_transform(mm, idx, F->get().first);
  411. #ifdef TOOLS_ENABLED
  412. Octant::MultimeshInstance::Item it;
  413. it.index = idx;
  414. it.transform = F->get().first;
  415. it.key = F->get().second;
  416. mmi.items.push_back(it);
  417. #endif
  418. idx++;
  419. }
  420. RID instance = VS::get_singleton()->instance_create();
  421. VS::get_singleton()->instance_set_base(instance, mm);
  422. if (is_inside_tree()) {
  423. VS::get_singleton()->instance_set_scenario(instance, get_world()->get_scenario());
  424. VS::get_singleton()->instance_set_transform(instance, get_global_transform());
  425. }
  426. mmi.multimesh = mm;
  427. mmi.instance = instance;
  428. g.multimesh_instances.push_back(mmi);
  429. }
  430. }
  431. if (col_debug.size()) {
  432. Array arr;
  433. arr.resize(VS::ARRAY_MAX);
  434. arr[VS::ARRAY_VERTEX] = col_debug;
  435. VS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, VS::PRIMITIVE_LINES, arr);
  436. SceneTree *st = SceneTree::get_singleton();
  437. if (st) {
  438. VS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  439. }
  440. }
  441. g.dirty = false;
  442. return false;
  443. }
  444. void GridMap::_reset_physic_bodies_collision_filters() {
  445. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  446. PhysicsServer::get_singleton()->body_set_collision_layer(E->get()->static_body, collision_layer);
  447. PhysicsServer::get_singleton()->body_set_collision_mask(E->get()->static_body, collision_mask);
  448. }
  449. }
  450. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  451. ERR_FAIL_COND(!octant_map.has(p_key));
  452. Octant &g = *octant_map[p_key];
  453. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  454. PhysicsServer::get_singleton()->body_set_space(g.static_body, get_world()->get_space());
  455. if (g.collision_debug_instance.is_valid()) {
  456. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world()->get_scenario());
  457. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  458. }
  459. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  460. VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world()->get_scenario());
  461. VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  462. }
  463. if (navigation && mesh_library.is_valid()) {
  464. for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
  465. if (cell_map.has(F->key()) && F->get().id < 0) {
  466. Ref<NavigationMesh> nm = mesh_library->get_item_navmesh(cell_map[F->key()].item);
  467. if (nm.is_valid()) {
  468. F->get().id = navigation->navmesh_add(nm, F->get().xform, this);
  469. }
  470. }
  471. }
  472. }
  473. }
  474. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  475. ERR_FAIL_COND(!octant_map.has(p_key));
  476. Octant &g = *octant_map[p_key];
  477. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  478. PhysicsServer::get_singleton()->body_set_space(g.static_body, RID());
  479. if (g.collision_debug_instance.is_valid()) {
  480. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  481. }
  482. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  483. VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  484. }
  485. if (navigation) {
  486. for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
  487. if (F->get().id >= 0) {
  488. navigation->navmesh_remove(F->get().id);
  489. F->get().id = -1;
  490. }
  491. }
  492. }
  493. }
  494. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  495. ERR_FAIL_COND(!octant_map.has(p_key));
  496. Octant &g = *octant_map[p_key];
  497. if (g.collision_debug.is_valid())
  498. VS::get_singleton()->free(g.collision_debug);
  499. if (g.collision_debug_instance.is_valid())
  500. VS::get_singleton()->free(g.collision_debug_instance);
  501. PhysicsServer::get_singleton()->free(g.static_body);
  502. //erase navigation
  503. if (navigation) {
  504. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  505. navigation->navmesh_remove(E->get().id);
  506. }
  507. g.navmesh_ids.clear();
  508. }
  509. //erase multimeshes
  510. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  511. VS::get_singleton()->free(g.multimesh_instances[i].instance);
  512. VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  513. }
  514. g.multimesh_instances.clear();
  515. }
  516. void GridMap::_notification(int p_what) {
  517. switch (p_what) {
  518. case NOTIFICATION_ENTER_WORLD: {
  519. Spatial *c = this;
  520. while (c) {
  521. navigation = Object::cast_to<Navigation>(c);
  522. if (navigation) {
  523. break;
  524. }
  525. c = Object::cast_to<Spatial>(c->get_parent());
  526. }
  527. last_transform = get_global_transform();
  528. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  529. _octant_enter_world(E->key());
  530. }
  531. for (int i = 0; i < baked_meshes.size(); i++) {
  532. VS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world()->get_scenario());
  533. VS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  534. }
  535. } break;
  536. case NOTIFICATION_TRANSFORM_CHANGED: {
  537. Transform new_xform = get_global_transform();
  538. if (new_xform == last_transform)
  539. break;
  540. //update run
  541. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  542. _octant_transform(E->key());
  543. }
  544. last_transform = new_xform;
  545. for (int i = 0; i < baked_meshes.size(); i++) {
  546. VS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  547. }
  548. } break;
  549. case NOTIFICATION_EXIT_WORLD: {
  550. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  551. _octant_exit_world(E->key());
  552. }
  553. navigation = NULL;
  554. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  555. //_update_octants_callback();
  556. //_update_area_instances();
  557. for (int i = 0; i < baked_meshes.size(); i++) {
  558. VS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID());
  559. }
  560. } break;
  561. case NOTIFICATION_VISIBILITY_CHANGED: {
  562. _update_visibility();
  563. } break;
  564. }
  565. }
  566. void GridMap::_update_visibility() {
  567. if (!is_inside_tree())
  568. return;
  569. _change_notify("visible");
  570. for (Map<OctantKey, Octant *>::Element *e = octant_map.front(); e; e = e->next()) {
  571. Octant *octant = e->value();
  572. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  573. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  574. VS::get_singleton()->instance_set_visible(mi.instance, is_visible());
  575. }
  576. }
  577. }
  578. void GridMap::_queue_octants_dirty() {
  579. if (awaiting_update)
  580. return;
  581. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  582. awaiting_update = true;
  583. }
  584. void GridMap::_recreate_octant_data() {
  585. recreating_octants = true;
  586. Map<IndexKey, Cell> cell_copy = cell_map;
  587. _clear_internal();
  588. for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
  589. set_cell_item(E->key().x, E->key().y, E->key().z, E->get().item, E->get().rot);
  590. }
  591. recreating_octants = false;
  592. }
  593. void GridMap::_clear_internal() {
  594. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  595. if (is_inside_world())
  596. _octant_exit_world(E->key());
  597. _octant_clean_up(E->key());
  598. memdelete(E->get());
  599. }
  600. octant_map.clear();
  601. cell_map.clear();
  602. }
  603. void GridMap::clear() {
  604. _clear_internal();
  605. clear_baked_meshes();
  606. }
  607. void GridMap::resource_changed(const RES &p_res) {
  608. _recreate_octant_data();
  609. }
  610. void GridMap::_update_octants_callback() {
  611. if (!awaiting_update)
  612. return;
  613. List<OctantKey> to_delete;
  614. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  615. if (_octant_update(E->key())) {
  616. to_delete.push_back(E->key());
  617. }
  618. }
  619. while (to_delete.front()) {
  620. octant_map.erase(to_delete.front()->get());
  621. to_delete.pop_back();
  622. }
  623. _update_visibility();
  624. awaiting_update = false;
  625. }
  626. void GridMap::_bind_methods() {
  627. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  628. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  629. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  630. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  631. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &GridMap::set_collision_mask_bit);
  632. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &GridMap::get_collision_mask_bit);
  633. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &GridMap::set_collision_layer_bit);
  634. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &GridMap::get_collision_layer_bit);
  635. #ifndef DISABLE_DEPRECATED
  636. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &GridMap::set_theme);
  637. ClassDB::bind_method(D_METHOD("get_theme"), &GridMap::get_theme);
  638. #endif // DISABLE_DEPRECATED
  639. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  640. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  641. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  642. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  643. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  644. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  645. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  646. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  647. ClassDB::bind_method(D_METHOD("set_cell_item", "x", "y", "z", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  648. ClassDB::bind_method(D_METHOD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item);
  649. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
  650. ClassDB::bind_method(D_METHOD("world_to_map", "pos"), &GridMap::world_to_map);
  651. ClassDB::bind_method(D_METHOD("map_to_world", "x", "y", "z"), &GridMap::map_to_world);
  652. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  653. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  654. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  655. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  656. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  657. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  658. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  659. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  660. ClassDB::bind_method(D_METHOD("set_clip", "enabled", "clipabove", "floor", "axis"), &GridMap::set_clip, DEFVAL(true), DEFVAL(0), DEFVAL(Vector3::AXIS_X));
  661. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  662. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  663. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  664. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  665. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  666. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  667. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  668. #ifndef DISABLE_DEPRECATED
  669. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary", 0), "set_theme", "get_theme");
  670. #endif // DISABLE_DEPRECATED
  671. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  672. ADD_GROUP("Cell", "cell_");
  673. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size"), "set_cell_size", "get_cell_size");
  674. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  675. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  676. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  677. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  678. ADD_PROPERTY(PropertyInfo(Variant::REAL, "cell_scale"), "set_cell_scale", "get_cell_scale");
  679. ADD_GROUP("Collision", "collision_");
  680. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  681. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  682. BIND_CONSTANT(INVALID_CELL_ITEM);
  683. }
  684. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  685. if (!p_enabled && !clip)
  686. return;
  687. if (clip && p_enabled && clip_floor == p_floor && p_clip_above == clip_above && p_axis == clip_axis)
  688. return;
  689. clip = p_enabled;
  690. clip_floor = p_floor;
  691. clip_axis = p_axis;
  692. clip_above = p_clip_above;
  693. //make it all update
  694. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  695. Octant *g = E->get();
  696. g->dirty = true;
  697. }
  698. awaiting_update = true;
  699. _update_octants_callback();
  700. }
  701. void GridMap::set_cell_scale(float p_scale) {
  702. cell_scale = p_scale;
  703. _recreate_octant_data();
  704. }
  705. float GridMap::get_cell_scale() const {
  706. return cell_scale;
  707. }
  708. Array GridMap::get_used_cells() const {
  709. Array a;
  710. a.resize(cell_map.size());
  711. int i = 0;
  712. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  713. Vector3 p(E->key().x, E->key().y, E->key().z);
  714. a[i++] = p;
  715. }
  716. return a;
  717. }
  718. Array GridMap::get_meshes() {
  719. if (mesh_library.is_null())
  720. return Array();
  721. Vector3 ofs = _get_offset();
  722. Array meshes;
  723. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  724. int id = E->get().item;
  725. if (!mesh_library->has_item(id))
  726. continue;
  727. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  728. if (mesh.is_null())
  729. continue;
  730. IndexKey ik = E->key();
  731. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  732. Transform xform;
  733. xform.basis.set_orthogonal_index(E->get().rot);
  734. xform.set_origin(cellpos * cell_size + ofs);
  735. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  736. meshes.push_back(xform);
  737. meshes.push_back(mesh);
  738. }
  739. return meshes;
  740. }
  741. Vector3 GridMap::_get_offset() const {
  742. return Vector3(
  743. cell_size.x * 0.5 * int(center_x),
  744. cell_size.y * 0.5 * int(center_y),
  745. cell_size.z * 0.5 * int(center_z));
  746. }
  747. void GridMap::clear_baked_meshes() {
  748. for (int i = 0; i < baked_meshes.size(); i++) {
  749. VS::get_singleton()->free(baked_meshes[i].instance);
  750. }
  751. baked_meshes.clear();
  752. _recreate_octant_data();
  753. }
  754. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  755. if (!mesh_library.is_valid())
  756. return;
  757. //generate
  758. Map<OctantKey, Map<Ref<Material>, Ref<SurfaceTool> > > surface_map;
  759. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  760. IndexKey key = E->key();
  761. int item = E->get().item;
  762. if (!mesh_library->has_item(item))
  763. continue;
  764. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  765. if (!mesh.is_valid())
  766. continue;
  767. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  768. Vector3 ofs = _get_offset();
  769. Transform xform;
  770. xform.basis.set_orthogonal_index(E->get().rot);
  771. xform.set_origin(cellpos * cell_size + ofs);
  772. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  773. OctantKey ok;
  774. ok.x = key.x / octant_size;
  775. ok.y = key.y / octant_size;
  776. ok.z = key.z / octant_size;
  777. if (!surface_map.has(ok)) {
  778. surface_map[ok] = Map<Ref<Material>, Ref<SurfaceTool> >();
  779. }
  780. Map<Ref<Material>, Ref<SurfaceTool> > &mat_map = surface_map[ok];
  781. for (int i = 0; i < mesh->get_surface_count(); i++) {
  782. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  783. continue;
  784. Ref<Material> surf_mat = mesh->surface_get_material(i);
  785. if (!mat_map.has(surf_mat)) {
  786. Ref<SurfaceTool> st;
  787. st.instance();
  788. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  789. st->set_material(surf_mat);
  790. mat_map[surf_mat] = st;
  791. }
  792. mat_map[surf_mat]->append_from(mesh, i, xform);
  793. }
  794. }
  795. for (Map<OctantKey, Map<Ref<Material>, Ref<SurfaceTool> > >::Element *E = surface_map.front(); E; E = E->next()) {
  796. Ref<ArrayMesh> mesh;
  797. mesh.instance();
  798. for (Map<Ref<Material>, Ref<SurfaceTool> >::Element *F = E->get().front(); F; F = F->next()) {
  799. F->get()->commit(mesh);
  800. }
  801. BakedMesh bm;
  802. bm.mesh = mesh;
  803. bm.instance = VS::get_singleton()->instance_create();
  804. VS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  805. VS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  806. if (is_inside_tree()) {
  807. VS::get_singleton()->instance_set_scenario(bm.instance, get_world()->get_scenario());
  808. VS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  809. }
  810. if (p_gen_lightmap_uv) {
  811. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  812. }
  813. baked_meshes.push_back(bm);
  814. }
  815. _recreate_octant_data();
  816. }
  817. Array GridMap::get_bake_meshes() {
  818. if (!baked_meshes.size()) {
  819. make_baked_meshes(true);
  820. }
  821. Array arr;
  822. for (int i = 0; i < baked_meshes.size(); i++) {
  823. arr.push_back(baked_meshes[i].mesh);
  824. arr.push_back(Transform());
  825. }
  826. return arr;
  827. }
  828. RID GridMap::get_bake_mesh_instance(int p_idx) {
  829. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  830. return baked_meshes[p_idx].instance;
  831. }
  832. GridMap::GridMap() {
  833. collision_layer = 1;
  834. collision_mask = 1;
  835. cell_size = Vector3(2, 2, 2);
  836. octant_size = 8;
  837. awaiting_update = false;
  838. _in_tree = false;
  839. center_x = true;
  840. center_y = true;
  841. center_z = true;
  842. clip = false;
  843. clip_floor = 0;
  844. clip_axis = Vector3::AXIS_Z;
  845. clip_above = true;
  846. cell_scale = 1.0;
  847. navigation = NULL;
  848. set_notify_transform(true);
  849. recreating_octants = false;
  850. }
  851. GridMap::~GridMap() {
  852. if (!mesh_library.is_null())
  853. mesh_library->unregister_owner(this);
  854. clear();
  855. }