grid_map.cpp 33 KB

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