grid_map.cpp 34 KB

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