grid_map.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /*************************************************************************/
  2. /* grid_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/physics_material.h"
  36. #include "scene/resources/primitive_meshes.h"
  37. #include "scene/resources/surface_tool.h"
  38. #include "scene/scene_string_names.h"
  39. #include "servers/navigation_server_3d.h"
  40. #include "servers/rendering_server.h"
  41. bool GridMap::_set(const StringName &p_name, const Variant &p_value) {
  42. String name = p_name;
  43. if (name == "data") {
  44. Dictionary d = p_value;
  45. if (d.has("cells")) {
  46. Vector<int> cells = d["cells"];
  47. int amount = cells.size();
  48. const int *r = cells.ptr();
  49. ERR_FAIL_COND_V(amount % 3, false); // not even
  50. cell_map.clear();
  51. for (int i = 0; i < amount / 3; i++) {
  52. IndexKey ik;
  53. ik.key = decode_uint64((const uint8_t *)&r[i * 3]);
  54. Cell cell;
  55. cell.cell = decode_uint32((const uint8_t *)&r[i * 3 + 2]);
  56. cell_map[ik] = cell;
  57. }
  58. }
  59. _recreate_octant_data();
  60. } else if (name == "baked_meshes") {
  61. clear_baked_meshes();
  62. Array meshes = p_value;
  63. for (int i = 0; i < meshes.size(); i++) {
  64. BakedMesh bm;
  65. bm.mesh = meshes[i];
  66. ERR_CONTINUE(!bm.mesh.is_valid());
  67. bm.instance = RS::get_singleton()->instance_create();
  68. RS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  69. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  70. if (is_inside_tree()) {
  71. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  72. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  73. }
  74. baked_meshes.push_back(bm);
  75. }
  76. _recreate_octant_data();
  77. } else {
  78. return false;
  79. }
  80. return true;
  81. }
  82. bool GridMap::_get(const StringName &p_name, Variant &r_ret) const {
  83. String name = p_name;
  84. if (name == "data") {
  85. Dictionary d;
  86. Vector<int> cells;
  87. cells.resize(cell_map.size() * 3);
  88. {
  89. int *w = cells.ptrw();
  90. int i = 0;
  91. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  92. encode_uint64(E.key.key, (uint8_t *)&w[i * 3]);
  93. encode_uint32(E.value.cell, (uint8_t *)&w[i * 3 + 2]);
  94. i++;
  95. }
  96. }
  97. d["cells"] = cells;
  98. r_ret = d;
  99. } else if (name == "baked_meshes") {
  100. Array ret;
  101. ret.resize(baked_meshes.size());
  102. for (int i = 0; i < baked_meshes.size(); i++) {
  103. ret[i] = baked_meshes[i].mesh;
  104. }
  105. r_ret = ret;
  106. } else {
  107. return false;
  108. }
  109. return true;
  110. }
  111. void GridMap::_get_property_list(List<PropertyInfo> *p_list) const {
  112. if (baked_meshes.size()) {
  113. p_list->push_back(PropertyInfo(Variant::ARRAY, "baked_meshes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  114. }
  115. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  116. }
  117. void GridMap::set_collision_layer(uint32_t p_layer) {
  118. collision_layer = p_layer;
  119. _reset_physic_bodies_collision_filters();
  120. }
  121. uint32_t GridMap::get_collision_layer() const {
  122. return collision_layer;
  123. }
  124. void GridMap::set_collision_mask(uint32_t p_mask) {
  125. collision_mask = p_mask;
  126. _reset_physic_bodies_collision_filters();
  127. }
  128. uint32_t GridMap::get_collision_mask() const {
  129. return collision_mask;
  130. }
  131. void GridMap::set_collision_layer_value(int p_layer_number, bool p_value) {
  132. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  133. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  134. uint32_t collision_layer_new = get_collision_layer();
  135. if (p_value) {
  136. collision_layer_new |= 1 << (p_layer_number - 1);
  137. } else {
  138. collision_layer_new &= ~(1 << (p_layer_number - 1));
  139. }
  140. set_collision_layer(collision_layer_new);
  141. }
  142. bool GridMap::get_collision_layer_value(int p_layer_number) const {
  143. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  144. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  145. return get_collision_layer() & (1 << (p_layer_number - 1));
  146. }
  147. void GridMap::set_collision_mask_value(int p_layer_number, bool p_value) {
  148. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  149. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  150. uint32_t mask = get_collision_mask();
  151. if (p_value) {
  152. mask |= 1 << (p_layer_number - 1);
  153. } else {
  154. mask &= ~(1 << (p_layer_number - 1));
  155. }
  156. set_collision_mask(mask);
  157. }
  158. void GridMap::set_physics_material(Ref<PhysicsMaterial> p_material) {
  159. physics_material = p_material;
  160. _recreate_octant_data();
  161. }
  162. Ref<PhysicsMaterial> GridMap::get_physics_material() const {
  163. return physics_material;
  164. }
  165. bool GridMap::get_collision_mask_value(int p_layer_number) const {
  166. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  167. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  168. return get_collision_mask() & (1 << (p_layer_number - 1));
  169. }
  170. Array GridMap::get_collision_shapes() const {
  171. Array shapes;
  172. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  173. Octant *g = E.value;
  174. RID body = g->static_body;
  175. Transform3D body_xform = PhysicsServer3D::get_singleton()->body_get_state(body, PhysicsServer3D::BODY_STATE_TRANSFORM);
  176. int nshapes = PhysicsServer3D::get_singleton()->body_get_shape_count(body);
  177. for (int i = 0; i < nshapes; i++) {
  178. RID shape = PhysicsServer3D::get_singleton()->body_get_shape(body, i);
  179. Transform3D xform = PhysicsServer3D::get_singleton()->body_get_shape_transform(body, i);
  180. shapes.push_back(body_xform * xform);
  181. shapes.push_back(shape);
  182. }
  183. }
  184. return shapes;
  185. }
  186. void GridMap::set_bake_navigation(bool p_bake_navigation) {
  187. bake_navigation = p_bake_navigation;
  188. _recreate_octant_data();
  189. }
  190. bool GridMap::is_baking_navigation() {
  191. return bake_navigation;
  192. }
  193. void GridMap::set_navigation_map(RID p_navigation_map) {
  194. map_override = p_navigation_map;
  195. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  196. Octant &g = *octant_map[E.key];
  197. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  198. if (F.value.region.is_valid()) {
  199. NavigationServer3D::get_singleton()->region_set_map(F.value.region, map_override);
  200. }
  201. }
  202. }
  203. }
  204. RID GridMap::get_navigation_map() const {
  205. if (map_override.is_valid()) {
  206. return map_override;
  207. } else if (is_inside_tree()) {
  208. return get_world_3d()->get_navigation_map();
  209. }
  210. return RID();
  211. }
  212. void GridMap::set_navigation_layers(uint32_t p_navigation_layers) {
  213. navigation_layers = p_navigation_layers;
  214. _recreate_octant_data();
  215. }
  216. uint32_t GridMap::get_navigation_layers() const {
  217. return navigation_layers;
  218. }
  219. void GridMap::set_navigation_layer_value(int p_layer_number, bool p_value) {
  220. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  221. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  222. uint32_t _navigation_layers = get_navigation_layers();
  223. if (p_value) {
  224. _navigation_layers |= 1 << (p_layer_number - 1);
  225. } else {
  226. _navigation_layers &= ~(1 << (p_layer_number - 1));
  227. }
  228. set_navigation_layers(_navigation_layers);
  229. }
  230. bool GridMap::get_navigation_layer_value(int p_layer_number) const {
  231. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  232. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  233. return get_navigation_layers() & (1 << (p_layer_number - 1));
  234. }
  235. void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) {
  236. if (!mesh_library.is_null()) {
  237. mesh_library->unregister_owner(this);
  238. }
  239. mesh_library = p_mesh_library;
  240. if (!mesh_library.is_null()) {
  241. mesh_library->register_owner(this);
  242. }
  243. _recreate_octant_data();
  244. }
  245. Ref<MeshLibrary> GridMap::get_mesh_library() const {
  246. return mesh_library;
  247. }
  248. void GridMap::set_cell_size(const Vector3 &p_size) {
  249. ERR_FAIL_COND(p_size.x < 0.001 || p_size.y < 0.001 || p_size.z < 0.001);
  250. cell_size = p_size;
  251. _recreate_octant_data();
  252. emit_signal(SNAME("cell_size_changed"), cell_size);
  253. }
  254. Vector3 GridMap::get_cell_size() const {
  255. return cell_size;
  256. }
  257. void GridMap::set_octant_size(int p_size) {
  258. ERR_FAIL_COND(p_size == 0);
  259. octant_size = p_size;
  260. _recreate_octant_data();
  261. }
  262. int GridMap::get_octant_size() const {
  263. return octant_size;
  264. }
  265. void GridMap::set_center_x(bool p_enable) {
  266. center_x = p_enable;
  267. _recreate_octant_data();
  268. }
  269. bool GridMap::get_center_x() const {
  270. return center_x;
  271. }
  272. void GridMap::set_center_y(bool p_enable) {
  273. center_y = p_enable;
  274. _recreate_octant_data();
  275. }
  276. bool GridMap::get_center_y() const {
  277. return center_y;
  278. }
  279. void GridMap::set_center_z(bool p_enable) {
  280. center_z = p_enable;
  281. _recreate_octant_data();
  282. }
  283. bool GridMap::get_center_z() const {
  284. return center_z;
  285. }
  286. void GridMap::set_cell_item(const Vector3i &p_position, int p_item, int p_rot) {
  287. if (baked_meshes.size() && !recreating_octants) {
  288. //if you set a cell item, baked meshes go good bye
  289. clear_baked_meshes();
  290. _recreate_octant_data();
  291. }
  292. ERR_FAIL_INDEX(ABS(p_position.x), 1 << 20);
  293. ERR_FAIL_INDEX(ABS(p_position.y), 1 << 20);
  294. ERR_FAIL_INDEX(ABS(p_position.z), 1 << 20);
  295. IndexKey key;
  296. key.x = p_position.x;
  297. key.y = p_position.y;
  298. key.z = p_position.z;
  299. OctantKey ok;
  300. ok.x = p_position.x / octant_size;
  301. ok.y = p_position.y / octant_size;
  302. ok.z = p_position.z / octant_size;
  303. if (p_item < 0) {
  304. //erase
  305. if (cell_map.has(key)) {
  306. OctantKey octantkey = ok;
  307. ERR_FAIL_COND(!octant_map.has(octantkey));
  308. Octant &g = *octant_map[octantkey];
  309. g.cells.erase(key);
  310. g.dirty = true;
  311. cell_map.erase(key);
  312. _queue_octants_dirty();
  313. }
  314. return;
  315. }
  316. OctantKey octantkey = ok;
  317. if (!octant_map.has(octantkey)) {
  318. //create octant because it does not exist
  319. Octant *g = memnew(Octant);
  320. g->dirty = true;
  321. g->static_body = PhysicsServer3D::get_singleton()->body_create();
  322. PhysicsServer3D::get_singleton()->body_set_mode(g->static_body, PhysicsServer3D::BODY_MODE_STATIC);
  323. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
  324. PhysicsServer3D::get_singleton()->body_set_collision_layer(g->static_body, collision_layer);
  325. PhysicsServer3D::get_singleton()->body_set_collision_mask(g->static_body, collision_mask);
  326. if (physics_material.is_valid()) {
  327. PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_FRICTION, physics_material->get_friction());
  328. PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material->get_bounce());
  329. }
  330. SceneTree *st = SceneTree::get_singleton();
  331. if (st && st->is_debugging_collisions_hint()) {
  332. g->collision_debug = RenderingServer::get_singleton()->mesh_create();
  333. g->collision_debug_instance = RenderingServer::get_singleton()->instance_create();
  334. RenderingServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  335. }
  336. octant_map[octantkey] = g;
  337. if (is_inside_world()) {
  338. _octant_enter_world(octantkey);
  339. _octant_transform(octantkey);
  340. }
  341. }
  342. Octant &g = *octant_map[octantkey];
  343. g.cells.insert(key);
  344. g.dirty = true;
  345. _queue_octants_dirty();
  346. Cell c;
  347. c.item = p_item;
  348. c.rot = p_rot;
  349. cell_map[key] = c;
  350. }
  351. int GridMap::get_cell_item(const Vector3i &p_position) const {
  352. ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, INVALID_CELL_ITEM);
  353. ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, INVALID_CELL_ITEM);
  354. ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, INVALID_CELL_ITEM);
  355. IndexKey key;
  356. key.x = p_position.x;
  357. key.y = p_position.y;
  358. key.z = p_position.z;
  359. if (!cell_map.has(key)) {
  360. return INVALID_CELL_ITEM;
  361. }
  362. return cell_map[key].item;
  363. }
  364. int GridMap::get_cell_item_orientation(const Vector3i &p_position) const {
  365. ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, -1);
  366. ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, -1);
  367. ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, -1);
  368. IndexKey key;
  369. key.x = p_position.x;
  370. key.y = p_position.y;
  371. key.z = p_position.z;
  372. if (!cell_map.has(key)) {
  373. return -1;
  374. }
  375. return cell_map[key].rot;
  376. }
  377. static const Basis _ortho_bases[24] = {
  378. Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
  379. Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
  380. Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  381. Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
  382. Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
  383. Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
  384. Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  385. Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
  386. Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
  387. Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
  388. Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  389. Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
  390. Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
  391. Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
  392. Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  393. Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
  394. Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
  395. Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
  396. Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
  397. Basis(0, 1, 0, 0, 0, -1, -1, 0, 0),
  398. Basis(0, 0, 1, 0, -1, 0, 1, 0, 0),
  399. Basis(0, 1, 0, 0, 0, 1, 1, 0, 0),
  400. Basis(0, 0, -1, 0, 1, 0, 1, 0, 0),
  401. Basis(0, -1, 0, 0, 0, -1, 1, 0, 0)
  402. };
  403. Basis GridMap::get_cell_item_basis(const Vector3i &p_position) const {
  404. int orientation = get_cell_item_orientation(p_position);
  405. if (orientation == -1) {
  406. return Basis();
  407. }
  408. return get_basis_with_orthogonal_index(orientation);
  409. }
  410. Basis GridMap::get_basis_with_orthogonal_index(int p_index) const {
  411. ERR_FAIL_INDEX_V(p_index, 24, Basis());
  412. return _ortho_bases[p_index];
  413. }
  414. int GridMap::get_orthogonal_index_from_basis(const Basis &p_basis) const {
  415. Basis orth = p_basis;
  416. for (int i = 0; i < 3; i++) {
  417. for (int j = 0; j < 3; j++) {
  418. real_t v = orth[i][j];
  419. if (v > 0.5) {
  420. v = 1.0;
  421. } else if (v < -0.5) {
  422. v = -1.0;
  423. } else {
  424. v = 0;
  425. }
  426. orth[i][j] = v;
  427. }
  428. }
  429. for (int i = 0; i < 24; i++) {
  430. if (_ortho_bases[i] == orth) {
  431. return i;
  432. }
  433. }
  434. return 0;
  435. }
  436. Vector3i GridMap::local_to_map(const Vector3 &p_world_position) const {
  437. Vector3 map_position = (p_world_position / cell_size).floor();
  438. return Vector3i(map_position);
  439. }
  440. Vector3 GridMap::map_to_local(const Vector3i &p_map_position) const {
  441. Vector3 offset = _get_offset();
  442. Vector3 local_position(
  443. p_map_position.x * cell_size.x + offset.x,
  444. p_map_position.y * cell_size.y + offset.y,
  445. p_map_position.z * cell_size.z + offset.z);
  446. return local_position;
  447. }
  448. void GridMap::_octant_transform(const OctantKey &p_key) {
  449. ERR_FAIL_COND(!octant_map.has(p_key));
  450. Octant &g = *octant_map[p_key];
  451. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  452. if (g.collision_debug_instance.is_valid()) {
  453. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  454. }
  455. // update transform for NavigationServer regions and navigation debugmesh instances
  456. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  457. if (bake_navigation) {
  458. if (E.value.region.is_valid()) {
  459. NavigationServer3D::get_singleton()->region_set_transform(E.value.region, get_global_transform() * E.value.xform);
  460. }
  461. if (E.value.navmesh_debug_instance.is_valid()) {
  462. RS::get_singleton()->instance_set_transform(E.value.navmesh_debug_instance, get_global_transform() * E.value.xform);
  463. }
  464. }
  465. }
  466. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  467. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  468. }
  469. }
  470. bool GridMap::_octant_update(const OctantKey &p_key) {
  471. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  472. Octant &g = *octant_map[p_key];
  473. if (!g.dirty) {
  474. return false;
  475. }
  476. //erase body shapes
  477. PhysicsServer3D::get_singleton()->body_clear_shapes(g.static_body);
  478. //erase body shapes debug
  479. if (g.collision_debug.is_valid()) {
  480. RS::get_singleton()->mesh_clear(g.collision_debug);
  481. }
  482. //erase navigation
  483. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  484. NavigationServer3D::get_singleton()->free(E.value.region);
  485. if (E.value.navmesh_debug_instance.is_valid()) {
  486. RS::get_singleton()->free(E.value.navmesh_debug_instance);
  487. }
  488. }
  489. g.navmesh_ids.clear();
  490. //erase multimeshes
  491. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  492. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  493. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  494. }
  495. g.multimesh_instances.clear();
  496. if (g.cells.size() == 0) {
  497. //octant no longer needed
  498. _octant_clean_up(p_key);
  499. return true;
  500. }
  501. Vector<Vector3> col_debug;
  502. /*
  503. * foreach item in this octant,
  504. * set item's multimesh's instance count to number of cells which have this item
  505. * and set said multimesh bounding box to one containing all cells which have this item
  506. */
  507. HashMap<int, List<Pair<Transform3D, IndexKey>>> multimesh_items;
  508. for (const IndexKey &E : g.cells) {
  509. ERR_CONTINUE(!cell_map.has(E));
  510. const Cell &c = cell_map[E];
  511. if (!mesh_library.is_valid() || !mesh_library->has_item(c.item)) {
  512. continue;
  513. }
  514. Vector3 cellpos = Vector3(E.x, E.y, E.z);
  515. Vector3 ofs = _get_offset();
  516. Transform3D xform;
  517. xform.basis = _ortho_bases[c.rot];
  518. xform.set_origin(cellpos * cell_size + ofs);
  519. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  520. if (baked_meshes.size() == 0) {
  521. if (mesh_library->get_item_mesh(c.item).is_valid()) {
  522. if (!multimesh_items.has(c.item)) {
  523. multimesh_items[c.item] = List<Pair<Transform3D, IndexKey>>();
  524. }
  525. Pair<Transform3D, IndexKey> p;
  526. p.first = xform * mesh_library->get_item_mesh_transform(c.item);
  527. p.second = E;
  528. multimesh_items[c.item].push_back(p);
  529. }
  530. }
  531. Vector<MeshLibrary::ShapeData> shapes = mesh_library->get_item_shapes(c.item);
  532. // add the item's shape at given xform to octant's static_body
  533. for (int i = 0; i < shapes.size(); i++) {
  534. // add the item's shape
  535. if (!shapes[i].shape.is_valid()) {
  536. continue;
  537. }
  538. PhysicsServer3D::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  539. if (g.collision_debug.is_valid()) {
  540. shapes.write[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  541. }
  542. }
  543. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  544. Ref<NavigationMesh> navmesh = mesh_library->get_item_navmesh(c.item);
  545. if (navmesh.is_valid()) {
  546. Octant::NavMesh nm;
  547. nm.xform = xform * mesh_library->get_item_navmesh_transform(c.item);
  548. if (bake_navigation) {
  549. RID region = NavigationServer3D::get_singleton()->region_create();
  550. NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
  551. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  552. NavigationServer3D::get_singleton()->region_set_navmesh(region, navmesh);
  553. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * nm.xform);
  554. if (is_inside_tree()) {
  555. if (map_override.is_valid()) {
  556. NavigationServer3D::get_singleton()->region_set_map(region, map_override);
  557. } else {
  558. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  559. }
  560. }
  561. nm.region = region;
  562. #ifdef DEBUG_ENABLED
  563. // add navigation debugmesh visual instances if debug is enabled
  564. SceneTree *st = SceneTree::get_singleton();
  565. if (st && st->is_debugging_navigation_hint()) {
  566. if (!nm.navmesh_debug_instance.is_valid()) {
  567. RID navmesh_debug_rid = navmesh->get_debug_mesh()->get_rid();
  568. nm.navmesh_debug_instance = RS::get_singleton()->instance_create();
  569. RS::get_singleton()->instance_set_base(nm.navmesh_debug_instance, navmesh_debug_rid);
  570. }
  571. if (is_inside_tree()) {
  572. RS::get_singleton()->instance_set_scenario(nm.navmesh_debug_instance, get_world_3d()->get_scenario());
  573. RS::get_singleton()->instance_set_transform(nm.navmesh_debug_instance, get_global_transform() * nm.xform);
  574. }
  575. }
  576. #endif // DEBUG_ENABLED
  577. }
  578. g.navmesh_ids[E] = nm;
  579. }
  580. }
  581. #ifdef DEBUG_ENABLED
  582. if (bake_navigation) {
  583. _update_octant_navigation_debug_edge_connections_mesh(p_key);
  584. }
  585. #endif // DEBUG_ENABLED
  586. //update multimeshes, only if not baked
  587. if (baked_meshes.size() == 0) {
  588. for (const KeyValue<int, List<Pair<Transform3D, IndexKey>>> &E : multimesh_items) {
  589. Octant::MultimeshInstance mmi;
  590. RID mm = RS::get_singleton()->multimesh_create();
  591. RS::get_singleton()->multimesh_allocate_data(mm, E.value.size(), RS::MULTIMESH_TRANSFORM_3D);
  592. RS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E.key)->get_rid());
  593. int idx = 0;
  594. for (const Pair<Transform3D, IndexKey> &F : E.value) {
  595. RS::get_singleton()->multimesh_instance_set_transform(mm, idx, F.first);
  596. #ifdef TOOLS_ENABLED
  597. Octant::MultimeshInstance::Item it;
  598. it.index = idx;
  599. it.transform = F.first;
  600. it.key = F.second;
  601. mmi.items.push_back(it);
  602. #endif
  603. idx++;
  604. }
  605. RID instance = RS::get_singleton()->instance_create();
  606. RS::get_singleton()->instance_set_base(instance, mm);
  607. if (is_inside_tree()) {
  608. RS::get_singleton()->instance_set_scenario(instance, get_world_3d()->get_scenario());
  609. RS::get_singleton()->instance_set_transform(instance, get_global_transform());
  610. }
  611. mmi.multimesh = mm;
  612. mmi.instance = instance;
  613. g.multimesh_instances.push_back(mmi);
  614. }
  615. }
  616. if (col_debug.size()) {
  617. Array arr;
  618. arr.resize(RS::ARRAY_MAX);
  619. arr[RS::ARRAY_VERTEX] = col_debug;
  620. RS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, RS::PRIMITIVE_LINES, arr);
  621. SceneTree *st = SceneTree::get_singleton();
  622. if (st) {
  623. RS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  624. }
  625. }
  626. g.dirty = false;
  627. return false;
  628. }
  629. void GridMap::_reset_physic_bodies_collision_filters() {
  630. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  631. PhysicsServer3D::get_singleton()->body_set_collision_layer(E.value->static_body, collision_layer);
  632. PhysicsServer3D::get_singleton()->body_set_collision_mask(E.value->static_body, collision_mask);
  633. }
  634. }
  635. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  636. ERR_FAIL_COND(!octant_map.has(p_key));
  637. Octant &g = *octant_map[p_key];
  638. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  639. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, get_world_3d()->get_space());
  640. if (g.collision_debug_instance.is_valid()) {
  641. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world_3d()->get_scenario());
  642. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  643. }
  644. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  645. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world_3d()->get_scenario());
  646. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  647. }
  648. if (bake_navigation && mesh_library.is_valid()) {
  649. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  650. if (cell_map.has(F.key) && F.value.region.is_valid() == false) {
  651. Ref<NavigationMesh> nm = mesh_library->get_item_navmesh(cell_map[F.key].item);
  652. if (nm.is_valid()) {
  653. RID region = NavigationServer3D::get_singleton()->region_create();
  654. NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
  655. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  656. NavigationServer3D::get_singleton()->region_set_navmesh(region, nm);
  657. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform);
  658. if (map_override.is_valid()) {
  659. NavigationServer3D::get_singleton()->region_set_map(region, map_override);
  660. } else {
  661. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  662. }
  663. F.value.region = region;
  664. }
  665. }
  666. }
  667. #ifdef DEBUG_ENABLED
  668. if (bake_navigation) {
  669. if (!g.navigation_debug_edge_connections_instance.is_valid()) {
  670. g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
  671. }
  672. if (!g.navigation_debug_edge_connections_mesh.is_valid()) {
  673. g.navigation_debug_edge_connections_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  674. }
  675. _update_octant_navigation_debug_edge_connections_mesh(p_key);
  676. }
  677. #endif // DEBUG_ENABLED
  678. }
  679. }
  680. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  681. ERR_FAIL_COND(!octant_map.has(p_key));
  682. Octant &g = *octant_map[p_key];
  683. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  684. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID());
  685. if (g.collision_debug_instance.is_valid()) {
  686. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  687. }
  688. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  689. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  690. }
  691. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  692. if (F.value.region.is_valid()) {
  693. NavigationServer3D::get_singleton()->free(F.value.region);
  694. F.value.region = RID();
  695. }
  696. if (F.value.navmesh_debug_instance.is_valid()) {
  697. RS::get_singleton()->free(F.value.navmesh_debug_instance);
  698. F.value.navmesh_debug_instance = RID();
  699. }
  700. }
  701. #ifdef DEBUG_ENABLED
  702. if (bake_navigation) {
  703. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  704. RenderingServer::get_singleton()->free(g.navigation_debug_edge_connections_instance);
  705. g.navigation_debug_edge_connections_instance = RID();
  706. }
  707. if (g.navigation_debug_edge_connections_mesh.is_valid()) {
  708. RenderingServer::get_singleton()->free(g.navigation_debug_edge_connections_mesh->get_rid());
  709. }
  710. }
  711. #endif // DEBUG_ENABLED
  712. }
  713. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  714. ERR_FAIL_COND(!octant_map.has(p_key));
  715. Octant &g = *octant_map[p_key];
  716. if (g.collision_debug.is_valid()) {
  717. RS::get_singleton()->free(g.collision_debug);
  718. }
  719. if (g.collision_debug_instance.is_valid()) {
  720. RS::get_singleton()->free(g.collision_debug_instance);
  721. }
  722. PhysicsServer3D::get_singleton()->free(g.static_body);
  723. // Erase navigation
  724. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  725. if (E.value.region.is_valid()) {
  726. NavigationServer3D::get_singleton()->free(E.value.region);
  727. }
  728. if (E.value.navmesh_debug_instance.is_valid()) {
  729. RS::get_singleton()->free(E.value.navmesh_debug_instance);
  730. }
  731. }
  732. g.navmesh_ids.clear();
  733. #ifdef DEBUG_ENABLED
  734. if (bake_navigation) {
  735. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  736. RenderingServer::get_singleton()->free(g.navigation_debug_edge_connections_instance);
  737. g.navigation_debug_edge_connections_instance = RID();
  738. }
  739. if (g.navigation_debug_edge_connections_mesh.is_valid()) {
  740. RenderingServer::get_singleton()->free(g.navigation_debug_edge_connections_mesh->get_rid());
  741. }
  742. }
  743. #endif // DEBUG_ENABLED
  744. //erase multimeshes
  745. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  746. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  747. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  748. }
  749. g.multimesh_instances.clear();
  750. }
  751. void GridMap::_notification(int p_what) {
  752. switch (p_what) {
  753. case NOTIFICATION_ENTER_WORLD: {
  754. last_transform = get_global_transform();
  755. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  756. _octant_enter_world(E.key);
  757. }
  758. for (int i = 0; i < baked_meshes.size(); i++) {
  759. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world_3d()->get_scenario());
  760. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  761. }
  762. } break;
  763. #ifdef DEBUG_ENABLED
  764. case NOTIFICATION_ENTER_TREE: {
  765. if (bake_navigation && NavigationServer3D::get_singleton()->get_debug_enabled()) {
  766. _update_navigation_debug_edge_connections();
  767. }
  768. } break;
  769. #endif // DEBUG_ENABLED
  770. case NOTIFICATION_TRANSFORM_CHANGED: {
  771. Transform3D new_xform = get_global_transform();
  772. if (new_xform == last_transform) {
  773. break;
  774. }
  775. //update run
  776. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  777. _octant_transform(E.key);
  778. }
  779. last_transform = new_xform;
  780. for (int i = 0; i < baked_meshes.size(); i++) {
  781. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  782. }
  783. } break;
  784. case NOTIFICATION_EXIT_WORLD: {
  785. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  786. _octant_exit_world(E.key);
  787. }
  788. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  789. //_update_octants_callback();
  790. //_update_area_instances();
  791. for (int i = 0; i < baked_meshes.size(); i++) {
  792. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID());
  793. }
  794. } break;
  795. case NOTIFICATION_VISIBILITY_CHANGED: {
  796. _update_visibility();
  797. } break;
  798. }
  799. }
  800. void GridMap::_update_visibility() {
  801. if (!is_inside_tree()) {
  802. return;
  803. }
  804. for (KeyValue<OctantKey, Octant *> &e : octant_map) {
  805. Octant *octant = e.value;
  806. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  807. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  808. RS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree());
  809. }
  810. }
  811. for (int i = 0; i < baked_meshes.size(); i++) {
  812. RS::get_singleton()->instance_set_visible(baked_meshes[i].instance, is_visible_in_tree());
  813. }
  814. }
  815. void GridMap::_queue_octants_dirty() {
  816. if (awaiting_update) {
  817. return;
  818. }
  819. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  820. awaiting_update = true;
  821. }
  822. void GridMap::_recreate_octant_data() {
  823. recreating_octants = true;
  824. HashMap<IndexKey, Cell, IndexKey> cell_copy = cell_map;
  825. _clear_internal();
  826. for (const KeyValue<IndexKey, Cell> &E : cell_copy) {
  827. set_cell_item(Vector3i(E.key), E.value.item, E.value.rot);
  828. }
  829. recreating_octants = false;
  830. }
  831. void GridMap::_clear_internal() {
  832. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  833. if (is_inside_world()) {
  834. _octant_exit_world(E.key);
  835. }
  836. _octant_clean_up(E.key);
  837. memdelete(E.value);
  838. }
  839. octant_map.clear();
  840. cell_map.clear();
  841. }
  842. void GridMap::clear() {
  843. _clear_internal();
  844. clear_baked_meshes();
  845. }
  846. void GridMap::resource_changed(const Ref<Resource> &p_res) {
  847. _recreate_octant_data();
  848. }
  849. void GridMap::_update_octants_callback() {
  850. if (!awaiting_update) {
  851. return;
  852. }
  853. List<OctantKey> to_delete;
  854. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  855. if (_octant_update(E.key)) {
  856. to_delete.push_back(E.key);
  857. }
  858. }
  859. while (to_delete.front()) {
  860. memdelete(octant_map[to_delete.front()->get()]);
  861. octant_map.erase(to_delete.front()->get());
  862. to_delete.pop_front();
  863. }
  864. _update_visibility();
  865. awaiting_update = false;
  866. }
  867. void GridMap::_bind_methods() {
  868. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  869. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  870. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  871. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  872. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &GridMap::set_collision_mask_value);
  873. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &GridMap::get_collision_mask_value);
  874. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &GridMap::set_collision_layer_value);
  875. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &GridMap::get_collision_layer_value);
  876. ClassDB::bind_method(D_METHOD("set_physics_material", "material"), &GridMap::set_physics_material);
  877. ClassDB::bind_method(D_METHOD("get_physics_material"), &GridMap::get_physics_material);
  878. ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &GridMap::set_bake_navigation);
  879. ClassDB::bind_method(D_METHOD("is_baking_navigation"), &GridMap::is_baking_navigation);
  880. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &GridMap::set_navigation_map);
  881. ClassDB::bind_method(D_METHOD("get_navigation_map"), &GridMap::get_navigation_map);
  882. ClassDB::bind_method(D_METHOD("set_navigation_layers", "layers"), &GridMap::set_navigation_layers);
  883. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &GridMap::get_navigation_layers);
  884. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &GridMap::set_navigation_layer_value);
  885. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &GridMap::get_navigation_layer_value);
  886. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  887. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  888. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  889. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  890. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  891. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  892. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  893. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  894. ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  895. ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item);
  896. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);
  897. ClassDB::bind_method(D_METHOD("get_cell_item_basis", "position"), &GridMap::get_cell_item_basis);
  898. ClassDB::bind_method(D_METHOD("get_basis_with_orthogonal_index", "index"), &GridMap::get_basis_with_orthogonal_index);
  899. ClassDB::bind_method(D_METHOD("get_orthogonal_index_from_basis", "basis"), &GridMap::get_orthogonal_index_from_basis);
  900. ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &GridMap::local_to_map);
  901. ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &GridMap::map_to_local);
  902. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  903. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  904. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  905. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  906. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  907. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  908. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  909. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  910. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  911. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  912. ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item);
  913. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  914. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  915. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  916. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  917. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  918. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  919. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material");
  920. ADD_GROUP("Cell", "cell_");
  921. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size", PROPERTY_HINT_NONE, "suffix:m"), "set_cell_size", "get_cell_size");
  922. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  923. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  924. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  925. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  926. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_scale"), "set_cell_scale", "get_cell_scale");
  927. ADD_GROUP("Collision", "collision_");
  928. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  929. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  930. ADD_GROUP("Navigation", "");
  931. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_navigation"), "set_bake_navigation", "is_baking_navigation");
  932. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  933. BIND_CONSTANT(INVALID_CELL_ITEM);
  934. ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size")));
  935. }
  936. void GridMap::set_cell_scale(float p_scale) {
  937. cell_scale = p_scale;
  938. _recreate_octant_data();
  939. }
  940. float GridMap::get_cell_scale() const {
  941. return cell_scale;
  942. }
  943. TypedArray<Vector3i> GridMap::get_used_cells() const {
  944. TypedArray<Vector3i> a;
  945. a.resize(cell_map.size());
  946. int i = 0;
  947. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  948. Vector3i p(E.key.x, E.key.y, E.key.z);
  949. a[i++] = p;
  950. }
  951. return a;
  952. }
  953. TypedArray<Vector3i> GridMap::get_used_cells_by_item(int p_item) const {
  954. TypedArray<Vector3i> a;
  955. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  956. if ((int)E.value.item == p_item) {
  957. Vector3i p(E.key.x, E.key.y, E.key.z);
  958. a.push_back(p);
  959. }
  960. }
  961. return a;
  962. }
  963. Array GridMap::get_meshes() const {
  964. if (mesh_library.is_null()) {
  965. return Array();
  966. }
  967. Vector3 ofs = _get_offset();
  968. Array meshes;
  969. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  970. int id = E.value.item;
  971. if (!mesh_library->has_item(id)) {
  972. continue;
  973. }
  974. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  975. if (mesh.is_null()) {
  976. continue;
  977. }
  978. IndexKey ik = E.key;
  979. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  980. Transform3D xform;
  981. xform.basis = _ortho_bases[E.value.rot];
  982. xform.set_origin(cellpos * cell_size + ofs);
  983. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  984. meshes.push_back(xform * mesh_library->get_item_mesh_transform(id));
  985. meshes.push_back(mesh);
  986. }
  987. return meshes;
  988. }
  989. Vector3 GridMap::_get_offset() const {
  990. return Vector3(
  991. cell_size.x * 0.5 * int(center_x),
  992. cell_size.y * 0.5 * int(center_y),
  993. cell_size.z * 0.5 * int(center_z));
  994. }
  995. void GridMap::clear_baked_meshes() {
  996. for (int i = 0; i < baked_meshes.size(); i++) {
  997. RS::get_singleton()->free(baked_meshes[i].instance);
  998. }
  999. baked_meshes.clear();
  1000. _recreate_octant_data();
  1001. }
  1002. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  1003. if (!mesh_library.is_valid()) {
  1004. return;
  1005. }
  1006. //generate
  1007. HashMap<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>, OctantKey> surface_map;
  1008. for (KeyValue<IndexKey, Cell> &E : cell_map) {
  1009. IndexKey key = E.key;
  1010. int item = E.value.item;
  1011. if (!mesh_library->has_item(item)) {
  1012. continue;
  1013. }
  1014. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  1015. if (!mesh.is_valid()) {
  1016. continue;
  1017. }
  1018. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  1019. Vector3 ofs = _get_offset();
  1020. Transform3D xform;
  1021. xform.basis = _ortho_bases[E.value.rot];
  1022. xform.set_origin(cellpos * cell_size + ofs);
  1023. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  1024. OctantKey ok;
  1025. ok.x = key.x / octant_size;
  1026. ok.y = key.y / octant_size;
  1027. ok.z = key.z / octant_size;
  1028. if (!surface_map.has(ok)) {
  1029. surface_map[ok] = HashMap<Ref<Material>, Ref<SurfaceTool>>();
  1030. }
  1031. HashMap<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok];
  1032. for (int i = 0; i < mesh->get_surface_count(); i++) {
  1033. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  1034. continue;
  1035. }
  1036. Ref<Material> surf_mat = mesh->surface_get_material(i);
  1037. if (!mat_map.has(surf_mat)) {
  1038. Ref<SurfaceTool> st;
  1039. st.instantiate();
  1040. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  1041. st->set_material(surf_mat);
  1042. mat_map[surf_mat] = st;
  1043. }
  1044. mat_map[surf_mat]->append_from(mesh, i, xform);
  1045. }
  1046. }
  1047. for (KeyValue<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>> &E : surface_map) {
  1048. Ref<ArrayMesh> mesh;
  1049. mesh.instantiate();
  1050. for (KeyValue<Ref<Material>, Ref<SurfaceTool>> &F : E.value) {
  1051. F.value->commit(mesh);
  1052. }
  1053. BakedMesh bm;
  1054. bm.mesh = mesh;
  1055. bm.instance = RS::get_singleton()->instance_create();
  1056. RS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  1057. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  1058. if (is_inside_tree()) {
  1059. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  1060. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  1061. }
  1062. if (p_gen_lightmap_uv) {
  1063. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  1064. }
  1065. baked_meshes.push_back(bm);
  1066. }
  1067. _recreate_octant_data();
  1068. }
  1069. Array GridMap::get_bake_meshes() {
  1070. if (!baked_meshes.size()) {
  1071. make_baked_meshes(true);
  1072. }
  1073. Array arr;
  1074. for (int i = 0; i < baked_meshes.size(); i++) {
  1075. arr.push_back(baked_meshes[i].mesh);
  1076. arr.push_back(Transform3D());
  1077. }
  1078. return arr;
  1079. }
  1080. RID GridMap::get_bake_mesh_instance(int p_idx) {
  1081. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  1082. return baked_meshes[p_idx].instance;
  1083. }
  1084. GridMap::GridMap() {
  1085. set_notify_transform(true);
  1086. #ifdef DEBUG_ENABLED
  1087. NavigationServer3D::get_singleton_mut()->connect("map_changed", callable_mp(this, &GridMap::_navigation_map_changed));
  1088. NavigationServer3D::get_singleton_mut()->connect("navigation_debug_changed", callable_mp(this, &GridMap::_update_navigation_debug_edge_connections));
  1089. #endif // DEBUG_ENABLED
  1090. }
  1091. #ifdef DEBUG_ENABLED
  1092. void GridMap::_update_navigation_debug_edge_connections() {
  1093. if (bake_navigation) {
  1094. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  1095. _update_octant_navigation_debug_edge_connections_mesh(E.key);
  1096. }
  1097. }
  1098. }
  1099. void GridMap::_navigation_map_changed(RID p_map) {
  1100. if (bake_navigation && is_inside_tree() && p_map == get_world_3d()->get_navigation_map()) {
  1101. _update_navigation_debug_edge_connections();
  1102. }
  1103. }
  1104. #endif // DEBUG_ENABLED
  1105. GridMap::~GridMap() {
  1106. if (!mesh_library.is_null()) {
  1107. mesh_library->unregister_owner(this);
  1108. }
  1109. clear();
  1110. #ifdef DEBUG_ENABLED
  1111. NavigationServer3D::get_singleton_mut()->disconnect("map_changed", callable_mp(this, &GridMap::_navigation_map_changed));
  1112. NavigationServer3D::get_singleton_mut()->disconnect("navigation_debug_changed", callable_mp(this, &GridMap::_update_navigation_debug_edge_connections));
  1113. #endif // DEBUG_ENABLED
  1114. }
  1115. #ifdef DEBUG_ENABLED
  1116. void GridMap::_update_octant_navigation_debug_edge_connections_mesh(const OctantKey &p_key) {
  1117. ERR_FAIL_COND(!octant_map.has(p_key));
  1118. Octant &g = *octant_map[p_key];
  1119. if (!NavigationServer3D::get_singleton()->get_debug_enabled()) {
  1120. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  1121. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false);
  1122. }
  1123. return;
  1124. }
  1125. if (!is_inside_tree()) {
  1126. return;
  1127. }
  1128. if (!bake_navigation) {
  1129. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  1130. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false);
  1131. }
  1132. return;
  1133. }
  1134. if (!g.navigation_debug_edge_connections_instance.is_valid()) {
  1135. g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
  1136. }
  1137. if (!g.navigation_debug_edge_connections_mesh.is_valid()) {
  1138. g.navigation_debug_edge_connections_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  1139. }
  1140. g.navigation_debug_edge_connections_mesh->clear_surfaces();
  1141. float edge_connection_margin = NavigationServer3D::get_singleton()->map_get_edge_connection_margin(get_world_3d()->get_navigation_map());
  1142. float half_edge_connection_margin = edge_connection_margin * 0.5;
  1143. Vector<Vector3> vertex_array;
  1144. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  1145. if (cell_map.has(F.key) && F.value.region.is_valid()) {
  1146. int connections_count = NavigationServer3D::get_singleton()->region_get_connections_count(F.value.region);
  1147. if (connections_count == 0) {
  1148. continue;
  1149. }
  1150. for (int i = 0; i < connections_count; i++) {
  1151. Vector3 connection_pathway_start = NavigationServer3D::get_singleton()->region_get_connection_pathway_start(F.value.region, i);
  1152. Vector3 connection_pathway_end = NavigationServer3D::get_singleton()->region_get_connection_pathway_end(F.value.region, i);
  1153. Vector3 direction_start_end = connection_pathway_start.direction_to(connection_pathway_end);
  1154. Vector3 direction_end_start = connection_pathway_end.direction_to(connection_pathway_start);
  1155. Vector3 start_right_dir = direction_start_end.cross(Vector3(0, 1, 0));
  1156. Vector3 start_left_dir = -start_right_dir;
  1157. Vector3 end_right_dir = direction_end_start.cross(Vector3(0, 1, 0));
  1158. Vector3 end_left_dir = -end_right_dir;
  1159. Vector3 left_start_pos = connection_pathway_start + (start_left_dir * half_edge_connection_margin);
  1160. Vector3 right_start_pos = connection_pathway_start + (start_right_dir * half_edge_connection_margin);
  1161. Vector3 left_end_pos = connection_pathway_end + (end_right_dir * half_edge_connection_margin);
  1162. Vector3 right_end_pos = connection_pathway_end + (end_left_dir * half_edge_connection_margin);
  1163. vertex_array.push_back(right_end_pos);
  1164. vertex_array.push_back(left_start_pos);
  1165. vertex_array.push_back(right_start_pos);
  1166. vertex_array.push_back(left_end_pos);
  1167. vertex_array.push_back(right_end_pos);
  1168. vertex_array.push_back(right_start_pos);
  1169. }
  1170. }
  1171. }
  1172. if (vertex_array.size() == 0) {
  1173. return;
  1174. }
  1175. Ref<StandardMaterial3D> edge_connections_material = NavigationServer3D::get_singleton_mut()->get_debug_navigation_edge_connections_material();
  1176. Array mesh_array;
  1177. mesh_array.resize(Mesh::ARRAY_MAX);
  1178. mesh_array[Mesh::ARRAY_VERTEX] = vertex_array;
  1179. g.navigation_debug_edge_connections_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mesh_array);
  1180. g.navigation_debug_edge_connections_mesh->surface_set_material(0, edge_connections_material);
  1181. RS::get_singleton()->instance_set_base(g.navigation_debug_edge_connections_instance, g.navigation_debug_edge_connections_mesh->get_rid());
  1182. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, is_visible_in_tree());
  1183. if (is_inside_tree()) {
  1184. RS::get_singleton()->instance_set_scenario(g.navigation_debug_edge_connections_instance, get_world_3d()->get_scenario());
  1185. }
  1186. bool enable_edge_connections = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_connections();
  1187. if (!enable_edge_connections) {
  1188. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false);
  1189. }
  1190. }
  1191. #endif // DEBUG_ENABLED