2
0

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