grid_map.cpp 34 KB

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