grid_map.cpp 50 KB

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