grid_map.cpp 48 KB

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