grid_map.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /**************************************************************************/
  2. /* grid_map.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/templates/a_hash_map.h"
  33. #include "scene/resources/3d/mesh_library.h"
  34. #include "scene/resources/3d/primitive_meshes.h"
  35. #include "scene/resources/surface_tool.h"
  36. #include "servers/rendering/rendering_server.h"
  37. #ifndef PHYSICS_3D_DISABLED
  38. #include "core/math/convex_hull.h"
  39. #include "scene/resources/3d/box_shape_3d.h"
  40. #include "scene/resources/3d/capsule_shape_3d.h"
  41. #include "scene/resources/3d/concave_polygon_shape_3d.h"
  42. #include "scene/resources/3d/convex_polygon_shape_3d.h"
  43. #include "scene/resources/3d/cylinder_shape_3d.h"
  44. #include "scene/resources/3d/height_map_shape_3d.h"
  45. #include "scene/resources/3d/shape_3d.h"
  46. #include "scene/resources/3d/sphere_shape_3d.h"
  47. #include "scene/resources/physics_material.h"
  48. #endif // PHYSICS_3D_DISABLED
  49. #ifndef NAVIGATION_3D_DISABLED
  50. #include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h"
  51. #include "servers/navigation_3d/navigation_server_3d.h"
  52. Callable GridMap::_navmesh_source_geometry_parsing_callback;
  53. RID GridMap::_navmesh_source_geometry_parser;
  54. #endif // NAVIGATION_3D_DISABLED
  55. bool GridMap::_set(const StringName &p_name, const Variant &p_value) {
  56. String name = p_name;
  57. if (name == "data") {
  58. Dictionary d = p_value;
  59. if (d.has("cells")) {
  60. Vector<int> cells = d["cells"];
  61. int amount = cells.size();
  62. const int *r = cells.ptr();
  63. ERR_FAIL_COND_V(amount % 3, false); // not even
  64. cell_map.clear();
  65. for (int i = 0; i < amount / 3; i++) {
  66. IndexKey ik;
  67. ik.key = decode_uint64((const uint8_t *)&r[i * 3]);
  68. Cell cell;
  69. cell.cell = decode_uint32((const uint8_t *)&r[i * 3 + 2]);
  70. cell_map[ik] = cell;
  71. }
  72. }
  73. _recreate_octant_data();
  74. } else if (name == "baked_meshes") {
  75. clear_baked_meshes();
  76. Array meshes = p_value;
  77. RID scenario;
  78. if (is_inside_tree()) {
  79. scenario = get_world_3d()->get_scenario();
  80. }
  81. for (int i = 0; i < meshes.size(); i++) {
  82. BakedMesh bm;
  83. bm.mesh = meshes[i];
  84. ERR_CONTINUE(bm.mesh.is_null());
  85. bm.instance = RS::get_singleton()->instance_create();
  86. RS::get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  87. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  88. if (is_inside_tree()) {
  89. RS::get_singleton()->instance_set_scenario(bm.instance, scenario);
  90. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  91. }
  92. baked_meshes.push_back(bm);
  93. }
  94. _recreate_octant_data();
  95. } else {
  96. return false;
  97. }
  98. return true;
  99. }
  100. bool GridMap::_get(const StringName &p_name, Variant &r_ret) const {
  101. String name = p_name;
  102. if (name == "data") {
  103. Dictionary d;
  104. Vector<int> cells;
  105. cells.resize(cell_map.size() * 3);
  106. {
  107. int *w = cells.ptrw();
  108. int i = 0;
  109. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  110. encode_uint64(E.key.key, (uint8_t *)&w[i * 3]);
  111. encode_uint32(E.value.cell, (uint8_t *)&w[i * 3 + 2]);
  112. i++;
  113. }
  114. }
  115. d["cells"] = cells;
  116. r_ret = d;
  117. } else if (name == "baked_meshes") {
  118. Array ret;
  119. ret.resize(baked_meshes.size());
  120. for (int i = 0; i < baked_meshes.size(); i++) {
  121. ret[i] = baked_meshes[i].mesh;
  122. }
  123. r_ret = ret;
  124. } else {
  125. return false;
  126. }
  127. return true;
  128. }
  129. void GridMap::_get_property_list(List<PropertyInfo> *p_list) const {
  130. if (baked_meshes.size()) {
  131. p_list->push_back(PropertyInfo(Variant::ARRAY, "baked_meshes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  132. }
  133. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  134. }
  135. #ifndef PHYSICS_3D_DISABLED
  136. void GridMap::set_collision_layer(uint32_t p_layer) {
  137. collision_layer = p_layer;
  138. _update_physics_bodies_collision_properties();
  139. }
  140. uint32_t GridMap::get_collision_layer() const {
  141. return collision_layer;
  142. }
  143. void GridMap::set_collision_mask(uint32_t p_mask) {
  144. collision_mask = p_mask;
  145. _update_physics_bodies_collision_properties();
  146. }
  147. uint32_t GridMap::get_collision_mask() const {
  148. return collision_mask;
  149. }
  150. void GridMap::set_collision_layer_value(int p_layer_number, bool p_value) {
  151. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  152. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  153. uint32_t collision_layer_new = get_collision_layer();
  154. if (p_value) {
  155. collision_layer_new |= 1 << (p_layer_number - 1);
  156. } else {
  157. collision_layer_new &= ~(1 << (p_layer_number - 1));
  158. }
  159. set_collision_layer(collision_layer_new);
  160. }
  161. bool GridMap::get_collision_layer_value(int p_layer_number) const {
  162. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  163. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  164. return get_collision_layer() & (1 << (p_layer_number - 1));
  165. }
  166. void GridMap::set_collision_mask_value(int p_layer_number, bool p_value) {
  167. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  168. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  169. uint32_t mask = get_collision_mask();
  170. if (p_value) {
  171. mask |= 1 << (p_layer_number - 1);
  172. } else {
  173. mask &= ~(1 << (p_layer_number - 1));
  174. }
  175. set_collision_mask(mask);
  176. }
  177. void GridMap::set_collision_priority(real_t p_priority) {
  178. collision_priority = p_priority;
  179. _update_physics_bodies_collision_properties();
  180. }
  181. real_t GridMap::get_collision_priority() const {
  182. return collision_priority;
  183. }
  184. void GridMap::set_physics_material(Ref<PhysicsMaterial> p_material) {
  185. physics_material = p_material;
  186. _update_physics_bodies_characteristics();
  187. }
  188. Ref<PhysicsMaterial> GridMap::get_physics_material() const {
  189. return physics_material;
  190. }
  191. bool GridMap::get_collision_mask_value(int p_layer_number) const {
  192. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  193. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  194. return get_collision_mask() & (1 << (p_layer_number - 1));
  195. }
  196. Array GridMap::get_collision_shapes() const {
  197. Array shapes;
  198. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  199. Octant *g = E.value;
  200. RID body = g->static_body;
  201. Transform3D body_xform = PhysicsServer3D::get_singleton()->body_get_state(body, PhysicsServer3D::BODY_STATE_TRANSFORM);
  202. int nshapes = PhysicsServer3D::get_singleton()->body_get_shape_count(body);
  203. for (int i = 0; i < nshapes; i++) {
  204. RID shape = PhysicsServer3D::get_singleton()->body_get_shape(body, i);
  205. Transform3D xform = PhysicsServer3D::get_singleton()->body_get_shape_transform(body, i);
  206. shapes.push_back(body_xform * xform);
  207. shapes.push_back(shape);
  208. }
  209. }
  210. return shapes;
  211. }
  212. #endif // PHYSICS_3D_DISABLED
  213. void GridMap::set_bake_navigation(bool p_bake_navigation) {
  214. bake_navigation = p_bake_navigation;
  215. _recreate_octant_data();
  216. }
  217. bool GridMap::is_baking_navigation() {
  218. return bake_navigation;
  219. }
  220. #ifndef NAVIGATION_3D_DISABLED
  221. void GridMap::set_navigation_map(RID p_navigation_map) {
  222. map_override = p_navigation_map;
  223. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  224. Octant &g = *octant_map[E.key];
  225. for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) {
  226. if (F.value.region.is_valid()) {
  227. NavigationServer3D::get_singleton()->region_set_map(F.value.region, map_override);
  228. }
  229. }
  230. }
  231. }
  232. RID GridMap::get_navigation_map() const {
  233. if (map_override.is_valid()) {
  234. return map_override;
  235. } else if (is_inside_tree()) {
  236. return get_world_3d()->get_navigation_map();
  237. }
  238. return RID();
  239. }
  240. #endif // NAVIGATION_3D_DISABLED
  241. void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) {
  242. if (mesh_library.is_valid()) {
  243. mesh_library->disconnect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
  244. }
  245. mesh_library = p_mesh_library;
  246. if (mesh_library.is_valid()) {
  247. mesh_library->connect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
  248. }
  249. _recreate_octant_data();
  250. emit_signal(CoreStringName(changed));
  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(Math::abs(p_position.x), 1 << 20);
  300. ERR_FAIL_INDEX(Math::abs(p_position.y), 1 << 20);
  301. ERR_FAIL_INDEX(Math::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. const OctantKey ok = get_octant_key_from_cell_coords(p_position);
  307. if (p_item < 0) {
  308. //erase
  309. if (cell_map.has(key)) {
  310. OctantKey octantkey = ok;
  311. ERR_FAIL_COND(!octant_map.has(octantkey));
  312. Octant &g = *octant_map[octantkey];
  313. g.cells.erase(key);
  314. g.dirty = true;
  315. cell_map.erase(key);
  316. _queue_octants_dirty();
  317. }
  318. return;
  319. }
  320. OctantKey octantkey = ok;
  321. if (!octant_map.has(octantkey)) {
  322. //create octant because it does not exist
  323. Octant *g = memnew(Octant);
  324. g->dirty = true;
  325. #ifndef PHYSICS_3D_DISABLED
  326. g->static_body = PhysicsServer3D::get_singleton()->body_create();
  327. PhysicsServer3D::get_singleton()->body_set_mode(g->static_body, PhysicsServer3D::BODY_MODE_STATIC);
  328. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
  329. PhysicsServer3D::get_singleton()->body_set_collision_layer(g->static_body, collision_layer);
  330. PhysicsServer3D::get_singleton()->body_set_collision_mask(g->static_body, collision_mask);
  331. PhysicsServer3D::get_singleton()->body_set_collision_priority(g->static_body, collision_priority);
  332. if (physics_material.is_valid()) {
  333. PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_FRICTION, physics_material->computed_friction());
  334. PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material->computed_bounce());
  335. }
  336. #endif // PHYSICS_3D_DISABLED
  337. SceneTree *st = SceneTree::get_singleton();
  338. if (st && st->is_debugging_collisions_hint()) {
  339. g->collision_debug = RenderingServer::get_singleton()->mesh_create();
  340. g->collision_debug_instance = RenderingServer::get_singleton()->instance_create();
  341. RenderingServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  342. }
  343. octant_map[octantkey] = g;
  344. if (is_inside_world()) {
  345. _octant_enter_world(octantkey);
  346. _octant_transform(octantkey);
  347. }
  348. }
  349. Octant &g = *octant_map[octantkey];
  350. g.cells.insert(key);
  351. g.dirty = true;
  352. _queue_octants_dirty();
  353. Cell c;
  354. c.item = p_item;
  355. c.rot = p_rot;
  356. cell_map[key] = c;
  357. }
  358. int GridMap::get_cell_item(const Vector3i &p_position) const {
  359. ERR_FAIL_INDEX_V(Math::abs(p_position.x), 1 << 20, INVALID_CELL_ITEM);
  360. ERR_FAIL_INDEX_V(Math::abs(p_position.y), 1 << 20, INVALID_CELL_ITEM);
  361. ERR_FAIL_INDEX_V(Math::abs(p_position.z), 1 << 20, INVALID_CELL_ITEM);
  362. IndexKey key;
  363. key.x = p_position.x;
  364. key.y = p_position.y;
  365. key.z = p_position.z;
  366. if (!cell_map.has(key)) {
  367. return INVALID_CELL_ITEM;
  368. }
  369. return cell_map[key].item;
  370. }
  371. int GridMap::get_cell_item_orientation(const Vector3i &p_position) const {
  372. ERR_FAIL_INDEX_V(Math::abs(p_position.x), 1 << 20, -1);
  373. ERR_FAIL_INDEX_V(Math::abs(p_position.y), 1 << 20, -1);
  374. ERR_FAIL_INDEX_V(Math::abs(p_position.z), 1 << 20, -1);
  375. IndexKey key;
  376. key.x = p_position.x;
  377. key.y = p_position.y;
  378. key.z = p_position.z;
  379. if (!cell_map.has(key)) {
  380. return -1;
  381. }
  382. return cell_map[key].rot;
  383. }
  384. static const Basis _ortho_bases[24] = {
  385. Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
  386. Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
  387. Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  388. Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
  389. Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
  390. Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
  391. Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  392. Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
  393. Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
  394. Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
  395. Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  396. Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
  397. Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
  398. Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
  399. Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  400. Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
  401. Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
  402. Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
  403. Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
  404. Basis(0, 1, 0, 0, 0, -1, -1, 0, 0),
  405. Basis(0, 0, 1, 0, -1, 0, 1, 0, 0),
  406. Basis(0, 1, 0, 0, 0, 1, 1, 0, 0),
  407. Basis(0, 0, -1, 0, 1, 0, 1, 0, 0),
  408. Basis(0, -1, 0, 0, 0, -1, 1, 0, 0)
  409. };
  410. Basis GridMap::get_cell_item_basis(const Vector3i &p_position) const {
  411. int orientation = get_cell_item_orientation(p_position);
  412. if (orientation == -1) {
  413. return Basis();
  414. }
  415. return get_basis_with_orthogonal_index(orientation);
  416. }
  417. Basis GridMap::get_basis_with_orthogonal_index(int p_index) const {
  418. ERR_FAIL_INDEX_V(p_index, 24, Basis());
  419. return _ortho_bases[p_index];
  420. }
  421. int GridMap::get_orthogonal_index_from_basis(const Basis &p_basis) const {
  422. Basis orth = p_basis;
  423. for (int i = 0; i < 3; i++) {
  424. for (int j = 0; j < 3; j++) {
  425. real_t v = orth[i][j];
  426. if (v > 0.5) {
  427. v = 1.0;
  428. } else if (v < -0.5) {
  429. v = -1.0;
  430. } else {
  431. v = 0;
  432. }
  433. orth[i][j] = v;
  434. }
  435. }
  436. for (int i = 0; i < 24; i++) {
  437. if (_ortho_bases[i] == orth) {
  438. return i;
  439. }
  440. }
  441. return 0;
  442. }
  443. GridMap::OctantKey GridMap::get_octant_key_from_index_key(const IndexKey &p_index_key) const {
  444. const int x = p_index_key.x > 0 ? p_index_key.x / octant_size : (p_index_key.x - (octant_size - 1)) / octant_size;
  445. const int y = p_index_key.y > 0 ? p_index_key.y / octant_size : (p_index_key.y - (octant_size - 1)) / octant_size;
  446. const int z = p_index_key.z > 0 ? p_index_key.z / octant_size : (p_index_key.z - (octant_size - 1)) / octant_size;
  447. OctantKey ok;
  448. ok.key = 0;
  449. ok.x = x;
  450. ok.y = y;
  451. ok.z = z;
  452. return ok;
  453. }
  454. GridMap::OctantKey GridMap::get_octant_key_from_cell_coords(const Vector3i &p_cell_coords) const {
  455. const int x = p_cell_coords.x > 0 ? p_cell_coords.x / octant_size : (p_cell_coords.x - (octant_size - 1)) / octant_size;
  456. const int y = p_cell_coords.y > 0 ? p_cell_coords.y / octant_size : (p_cell_coords.y - (octant_size - 1)) / octant_size;
  457. const int z = p_cell_coords.z > 0 ? p_cell_coords.z / octant_size : (p_cell_coords.z - (octant_size - 1)) / octant_size;
  458. OctantKey ok;
  459. ok.key = 0;
  460. ok.x = x;
  461. ok.y = y;
  462. ok.z = z;
  463. return ok;
  464. }
  465. Vector3i GridMap::local_to_map(const Vector3 &p_world_position) const {
  466. Vector3 map_position = (p_world_position / cell_size).floor();
  467. return Vector3i(map_position);
  468. }
  469. Vector3 GridMap::map_to_local(const Vector3i &p_map_position) const {
  470. Vector3 offset = _get_offset();
  471. Vector3 local_position(
  472. p_map_position.x * cell_size.x + offset.x,
  473. p_map_position.y * cell_size.y + offset.y,
  474. p_map_position.z * cell_size.z + offset.z);
  475. return local_position;
  476. }
  477. void GridMap::_octant_transform(const OctantKey &p_key) {
  478. ERR_FAIL_COND(!octant_map.has(p_key));
  479. Octant &g = *octant_map[p_key];
  480. #ifndef PHYSICS_3D_DISABLED
  481. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  482. if (g.collision_debug_instance.is_valid()) {
  483. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  484. }
  485. #endif // PHYSICS_3D_DISABLED
  486. #ifndef NAVIGATION_3D_DISABLED
  487. // update transform for NavigationServer regions and navigation debugmesh instances
  488. for (const KeyValue<IndexKey, Octant::NavigationCell> &E : g.navigation_cell_ids) {
  489. if (bake_navigation) {
  490. if (E.value.region.is_valid()) {
  491. NavigationServer3D::get_singleton()->region_set_transform(E.value.region, get_global_transform() * E.value.xform);
  492. }
  493. if (E.value.navigation_mesh_debug_instance.is_valid()) {
  494. RS::get_singleton()->instance_set_transform(E.value.navigation_mesh_debug_instance, get_global_transform() * E.value.xform);
  495. }
  496. }
  497. }
  498. #endif // NAVIGATION_3D_DISABLED
  499. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  500. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  501. }
  502. }
  503. bool GridMap::_octant_update(const OctantKey &p_key) {
  504. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  505. Octant &g = *octant_map[p_key];
  506. if (!g.dirty) {
  507. return false;
  508. }
  509. #ifndef PHYSICS_3D_DISABLED
  510. //erase body shapes
  511. PhysicsServer3D::get_singleton()->body_clear_shapes(g.static_body);
  512. //erase body shapes debug
  513. if (g.collision_debug.is_valid()) {
  514. RS::get_singleton()->mesh_clear(g.collision_debug);
  515. }
  516. #endif // PHYSICS_3D_DISABLED
  517. #ifndef NAVIGATION_3D_DISABLED
  518. //erase navigation
  519. for (KeyValue<IndexKey, Octant::NavigationCell> &E : g.navigation_cell_ids) {
  520. if (E.value.region.is_valid()) {
  521. NavigationServer3D::get_singleton()->free_rid(E.value.region);
  522. E.value.region = RID();
  523. }
  524. if (E.value.navigation_mesh_debug_instance.is_valid()) {
  525. RS::get_singleton()->free_rid(E.value.navigation_mesh_debug_instance);
  526. E.value.navigation_mesh_debug_instance = RID();
  527. }
  528. }
  529. g.navigation_cell_ids.clear();
  530. #endif // NAVIGATION_3D_DISABLED
  531. //erase multimeshes
  532. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  533. RS::get_singleton()->free_rid(g.multimesh_instances[i].instance);
  534. RS::get_singleton()->free_rid(g.multimesh_instances[i].multimesh);
  535. }
  536. g.multimesh_instances.clear();
  537. if (g.cells.is_empty()) {
  538. //octant no longer needed
  539. _octant_clean_up(p_key);
  540. return true;
  541. }
  542. Vector<Vector3> col_debug;
  543. /*
  544. * foreach item in this octant,
  545. * set item's multimesh's instance count to number of cells which have this item
  546. * and set said multimesh bounding box to one containing all cells which have this item
  547. */
  548. struct MultiMeshItemPlacement {
  549. Transform3D transform;
  550. IndexKey index_key;
  551. };
  552. AHashMap<int, LocalVector<MultiMeshItemPlacement>> item_id_to_multimesh_item_placements;
  553. RID scenario;
  554. #ifndef NAVIGATION_3D_DISABLED
  555. RID navigation_map;
  556. #endif
  557. if (is_inside_tree()) {
  558. scenario = get_world_3d()->get_scenario();
  559. #ifndef NAVIGATION_3D_DISABLED
  560. navigation_map = get_world_3d()->get_navigation_map();
  561. #endif
  562. }
  563. for (const IndexKey &E : g.cells) {
  564. ERR_CONTINUE(!cell_map.has(E));
  565. const Cell &c = cell_map[E];
  566. if (mesh_library.is_null() || !mesh_library->has_item(c.item)) {
  567. continue;
  568. }
  569. Vector3 cellpos = Vector3(E.x, E.y, E.z);
  570. Vector3 ofs = _get_offset();
  571. Transform3D xform;
  572. xform.basis = _ortho_bases[c.rot];
  573. xform.set_origin(cellpos * cell_size + ofs);
  574. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  575. if (baked_meshes.is_empty()) {
  576. if (mesh_library->get_item_mesh(c.item).is_valid()) {
  577. if (!item_id_to_multimesh_item_placements.has(c.item)) {
  578. item_id_to_multimesh_item_placements[c.item] = LocalVector<MultiMeshItemPlacement>();
  579. }
  580. MultiMeshItemPlacement p;
  581. p.transform = xform * mesh_library->get_item_mesh_transform(c.item);
  582. p.index_key = E;
  583. item_id_to_multimesh_item_placements[c.item].push_back(p);
  584. }
  585. }
  586. #ifndef PHYSICS_3D_DISABLED
  587. Vector<MeshLibrary::ShapeData> shapes = mesh_library->get_item_shapes(c.item);
  588. // add the item's shape at given xform to octant's static_body
  589. for (int i = 0; i < shapes.size(); i++) {
  590. // add the item's shape
  591. if (shapes[i].shape.is_null()) {
  592. continue;
  593. }
  594. PhysicsServer3D::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  595. if (g.collision_debug.is_valid()) {
  596. shapes.write[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  597. }
  598. }
  599. #endif // PHYSICS_3D_DISABLED
  600. #ifndef NAVIGATION_3D_DISABLED
  601. // add the item's navigation_mesh at given xform to GridMap's Navigation ancestor
  602. Ref<NavigationMesh> navigation_mesh = mesh_library->get_item_navigation_mesh(c.item);
  603. if (navigation_mesh.is_valid()) {
  604. Octant::NavigationCell nm;
  605. nm.xform = xform * mesh_library->get_item_navigation_mesh_transform(c.item);
  606. nm.navigation_layers = mesh_library->get_item_navigation_layers(c.item);
  607. if (bake_navigation) {
  608. RID region = NavigationServer3D::get_singleton()->region_create();
  609. NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
  610. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, nm.navigation_layers);
  611. NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, navigation_mesh);
  612. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * nm.xform);
  613. if (is_inside_tree()) {
  614. if (map_override.is_valid()) {
  615. NavigationServer3D::get_singleton()->region_set_map(region, map_override);
  616. } else {
  617. NavigationServer3D::get_singleton()->region_set_map(region, navigation_map);
  618. }
  619. }
  620. nm.region = region;
  621. #ifdef DEBUG_ENABLED
  622. // add navigation debugmesh visual instances if debug is enabled
  623. SceneTree *st = SceneTree::get_singleton();
  624. if (st && st->is_debugging_navigation_hint()) {
  625. if (!nm.navigation_mesh_debug_instance.is_valid()) {
  626. RID navigation_mesh_debug_rid = navigation_mesh->get_debug_mesh()->get_rid();
  627. nm.navigation_mesh_debug_instance = RS::get_singleton()->instance_create();
  628. RS::get_singleton()->instance_set_base(nm.navigation_mesh_debug_instance, navigation_mesh_debug_rid);
  629. }
  630. if (is_inside_tree()) {
  631. RS::get_singleton()->instance_set_scenario(nm.navigation_mesh_debug_instance, scenario);
  632. RS::get_singleton()->instance_set_transform(nm.navigation_mesh_debug_instance, get_global_transform() * nm.xform);
  633. }
  634. }
  635. #endif // DEBUG_ENABLED
  636. }
  637. g.navigation_cell_ids[E] = nm;
  638. }
  639. #endif // NAVIGATION_3D_DISABLED
  640. }
  641. #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  642. if (bake_navigation) {
  643. _update_octant_navigation_debug_edge_connections_mesh(p_key);
  644. }
  645. #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  646. //update multimeshes, only if not baked
  647. if (baked_meshes.is_empty()) {
  648. for (const KeyValue<int, LocalVector<MultiMeshItemPlacement>> &E : item_id_to_multimesh_item_placements) {
  649. Octant::MultimeshInstance mmi;
  650. RID mm = RS::get_singleton()->multimesh_create();
  651. RS::get_singleton()->multimesh_allocate_data(mm, E.value.size(), RS::MULTIMESH_TRANSFORM_3D);
  652. RS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E.key)->get_rid());
  653. int idx = 0;
  654. const LocalVector<MultiMeshItemPlacement> &mm_item_placements = E.value;
  655. for (const MultiMeshItemPlacement &mm_item_placement : mm_item_placements) {
  656. RS::get_singleton()->multimesh_instance_set_transform(mm, idx, mm_item_placement.transform);
  657. #ifdef TOOLS_ENABLED
  658. Octant::MultimeshInstance::Item it;
  659. it.index = idx;
  660. it.transform = mm_item_placement.transform;
  661. it.key = mm_item_placement.index_key;
  662. mmi.items.push_back(it);
  663. #endif
  664. idx++;
  665. }
  666. RID instance = RS::get_singleton()->instance_create();
  667. RS::get_singleton()->instance_set_base(instance, mm);
  668. if (is_inside_tree()) {
  669. RS::get_singleton()->instance_set_scenario(instance, scenario);
  670. RS::get_singleton()->instance_set_transform(instance, get_global_transform());
  671. }
  672. RS::ShadowCastingSetting cast_shadows = (RS::ShadowCastingSetting)mesh_library->get_item_mesh_cast_shadow(E.key);
  673. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(instance, cast_shadows);
  674. mmi.multimesh = mm;
  675. mmi.instance = instance;
  676. g.multimesh_instances.push_back(mmi);
  677. }
  678. }
  679. #ifndef PHYSICS_3D_DISABLED
  680. if (col_debug.size()) {
  681. SceneTree *st = SceneTree::get_singleton();
  682. Vector<Color> colors;
  683. colors.resize(col_debug.size());
  684. if (st) {
  685. colors.fill(st->get_debug_collisions_color());
  686. }
  687. Array arr;
  688. arr.resize(RS::ARRAY_MAX);
  689. arr[RS::ARRAY_VERTEX] = col_debug;
  690. arr[RS::ARRAY_COLOR] = colors;
  691. RS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, RS::PRIMITIVE_LINES, arr);
  692. if (st) {
  693. RS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  694. }
  695. }
  696. #endif // PHYSICS_3D_DISABLED
  697. g.dirty = false;
  698. return false;
  699. }
  700. #ifndef PHYSICS_3D_DISABLED
  701. void GridMap::_update_physics_bodies_collision_properties() {
  702. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  703. PhysicsServer3D::get_singleton()->body_set_collision_layer(E.value->static_body, collision_layer);
  704. PhysicsServer3D::get_singleton()->body_set_collision_mask(E.value->static_body, collision_mask);
  705. PhysicsServer3D::get_singleton()->body_set_collision_priority(E.value->static_body, collision_priority);
  706. }
  707. }
  708. void GridMap::_update_physics_bodies_characteristics() {
  709. real_t friction = 1.0;
  710. real_t bounce = 0.0;
  711. if (physics_material.is_valid()) {
  712. friction = physics_material->computed_friction();
  713. bounce = physics_material->computed_bounce();
  714. }
  715. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  716. PhysicsServer3D::get_singleton()->body_set_param(E.value->static_body, PhysicsServer3D::BODY_PARAM_FRICTION, friction);
  717. PhysicsServer3D::get_singleton()->body_set_param(E.value->static_body, PhysicsServer3D::BODY_PARAM_BOUNCE, bounce);
  718. }
  719. }
  720. #endif // PHYSICS_3D_DISABLED
  721. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  722. ERR_FAIL_COND(!octant_map.has(p_key));
  723. Octant &g = *octant_map[p_key];
  724. const RID scenario = get_world_3d()->get_scenario();
  725. #ifndef PHYSICS_3D_DISABLED
  726. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  727. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, get_world_3d()->get_space());
  728. if (g.collision_debug_instance.is_valid()) {
  729. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, scenario);
  730. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  731. }
  732. #endif // PHYSICS_3D_DISABLED
  733. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  734. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, scenario);
  735. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  736. }
  737. #ifndef NAVIGATION_3D_DISABLED
  738. const RID navigation_map = get_world_3d()->get_navigation_map();
  739. if (bake_navigation && mesh_library.is_valid()) {
  740. for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) {
  741. if (cell_map.has(F.key) && F.value.region.is_valid() == false) {
  742. Ref<NavigationMesh> navigation_mesh = mesh_library->get_item_navigation_mesh(cell_map[F.key].item);
  743. if (navigation_mesh.is_valid()) {
  744. RID region = NavigationServer3D::get_singleton()->region_create();
  745. NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
  746. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, F.value.navigation_layers);
  747. NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, navigation_mesh);
  748. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform);
  749. if (map_override.is_valid()) {
  750. NavigationServer3D::get_singleton()->region_set_map(region, map_override);
  751. } else {
  752. NavigationServer3D::get_singleton()->region_set_map(region, navigation_map);
  753. }
  754. F.value.region = region;
  755. }
  756. }
  757. }
  758. #ifdef DEBUG_ENABLED
  759. if (bake_navigation) {
  760. if (!g.navigation_debug_edge_connections_instance.is_valid()) {
  761. g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
  762. }
  763. if (g.navigation_debug_edge_connections_mesh.is_null()) {
  764. g.navigation_debug_edge_connections_mesh.instantiate();
  765. }
  766. _update_octant_navigation_debug_edge_connections_mesh(p_key);
  767. }
  768. #endif // DEBUG_ENABLED
  769. }
  770. #endif // NAVIGATION_3D_DISABLED
  771. }
  772. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  773. ERR_FAIL_NULL(RenderingServer::get_singleton());
  774. #ifndef PHYSICS_3D_DISABLED
  775. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  776. #endif // PHYSICS_3D_DISABLED
  777. #ifndef NAVIGATION_3D_DISABLED
  778. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  779. #endif // NAVIGATION_3D_DISABLED
  780. ERR_FAIL_COND(!octant_map.has(p_key));
  781. Octant &g = *octant_map[p_key];
  782. #ifndef PHYSICS_3D_DISABLED
  783. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  784. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID());
  785. if (g.collision_debug_instance.is_valid()) {
  786. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  787. }
  788. #endif // PHYSICS_3D_DISABLED
  789. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  790. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  791. }
  792. #ifndef NAVIGATION_3D_DISABLED
  793. for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) {
  794. if (F.value.region.is_valid()) {
  795. NavigationServer3D::get_singleton()->free_rid(F.value.region);
  796. F.value.region = RID();
  797. }
  798. if (F.value.navigation_mesh_debug_instance.is_valid()) {
  799. RS::get_singleton()->free_rid(F.value.navigation_mesh_debug_instance);
  800. F.value.navigation_mesh_debug_instance = RID();
  801. }
  802. }
  803. #endif // NAVIGATION_3D_DISABLED
  804. #ifdef DEBUG_ENABLED
  805. if (bake_navigation) {
  806. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  807. RenderingServer::get_singleton()->free_rid(g.navigation_debug_edge_connections_instance);
  808. g.navigation_debug_edge_connections_instance = RID();
  809. }
  810. if (g.navigation_debug_edge_connections_mesh.is_valid()) {
  811. g.navigation_debug_edge_connections_mesh.unref();
  812. }
  813. }
  814. #endif // DEBUG_ENABLED
  815. }
  816. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  817. ERR_FAIL_NULL(RenderingServer::get_singleton());
  818. #ifndef PHYSICS_3D_DISABLED
  819. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  820. #endif // PHYSICS_3D_DISABLED
  821. #ifndef NAVIGATION_3D_DISABLED
  822. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  823. #endif // NAVIGATION_3D_DISABLED
  824. ERR_FAIL_COND(!octant_map.has(p_key));
  825. Octant &g = *octant_map[p_key];
  826. #ifndef PHYSICS_3D_DISABLED
  827. if (g.collision_debug.is_valid()) {
  828. RS::get_singleton()->free_rid(g.collision_debug);
  829. }
  830. if (g.collision_debug_instance.is_valid()) {
  831. RS::get_singleton()->free_rid(g.collision_debug_instance);
  832. }
  833. PhysicsServer3D::get_singleton()->free_rid(g.static_body);
  834. #endif // PHYSICS_3D_DISABLED
  835. #ifndef NAVIGATION_3D_DISABLED
  836. // Erase navigation
  837. for (const KeyValue<IndexKey, Octant::NavigationCell> &E : g.navigation_cell_ids) {
  838. if (E.value.region.is_valid()) {
  839. NavigationServer3D::get_singleton()->free_rid(E.value.region);
  840. }
  841. if (E.value.navigation_mesh_debug_instance.is_valid()) {
  842. RS::get_singleton()->free_rid(E.value.navigation_mesh_debug_instance);
  843. }
  844. }
  845. g.navigation_cell_ids.clear();
  846. #endif // NAVIGATION_3D_DISABLED
  847. #ifdef DEBUG_ENABLED
  848. if (bake_navigation) {
  849. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  850. RenderingServer::get_singleton()->free_rid(g.navigation_debug_edge_connections_instance);
  851. g.navigation_debug_edge_connections_instance = RID();
  852. }
  853. if (g.navigation_debug_edge_connections_mesh.is_valid()) {
  854. g.navigation_debug_edge_connections_mesh.unref();
  855. }
  856. }
  857. #endif // DEBUG_ENABLED
  858. //erase multimeshes
  859. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  860. RS::get_singleton()->free_rid(g.multimesh_instances[i].instance);
  861. RS::get_singleton()->free_rid(g.multimesh_instances[i].multimesh);
  862. }
  863. g.multimesh_instances.clear();
  864. }
  865. void GridMap::_notification(int p_what) {
  866. switch (p_what) {
  867. case NOTIFICATION_ENTER_WORLD: {
  868. last_transform = get_global_transform();
  869. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  870. _octant_enter_world(E.key);
  871. }
  872. for (int i = 0; i < baked_meshes.size(); i++) {
  873. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world_3d()->get_scenario());
  874. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  875. }
  876. } break;
  877. case NOTIFICATION_ENTER_TREE: {
  878. #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  879. if (bake_navigation && NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  880. _update_navigation_debug_edge_connections();
  881. }
  882. #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  883. _update_visibility();
  884. } break;
  885. case NOTIFICATION_TRANSFORM_CHANGED: {
  886. Transform3D new_xform = get_global_transform();
  887. if (new_xform == last_transform) {
  888. break;
  889. }
  890. //update run
  891. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  892. _octant_transform(E.key);
  893. }
  894. last_transform = new_xform;
  895. for (int i = 0; i < baked_meshes.size(); i++) {
  896. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  897. }
  898. } break;
  899. case NOTIFICATION_EXIT_WORLD: {
  900. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  901. _octant_exit_world(E.key);
  902. }
  903. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  904. //_update_octants_callback();
  905. //_update_area_instances();
  906. for (int i = 0; i < baked_meshes.size(); i++) {
  907. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID());
  908. }
  909. } break;
  910. case NOTIFICATION_VISIBILITY_CHANGED: {
  911. _update_visibility();
  912. } break;
  913. }
  914. }
  915. void GridMap::_update_visibility() {
  916. if (!is_inside_tree()) {
  917. return;
  918. }
  919. for (KeyValue<OctantKey, Octant *> &e : octant_map) {
  920. Octant *octant = e.value;
  921. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  922. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  923. RS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree());
  924. }
  925. }
  926. for (int i = 0; i < baked_meshes.size(); i++) {
  927. RS::get_singleton()->instance_set_visible(baked_meshes[i].instance, is_visible_in_tree());
  928. }
  929. }
  930. void GridMap::_queue_octants_dirty() {
  931. if (awaiting_update) {
  932. return;
  933. }
  934. callable_mp(this, &GridMap::_update_octants_callback).call_deferred();
  935. awaiting_update = true;
  936. }
  937. void GridMap::_recreate_octant_data() {
  938. recreating_octants = true;
  939. HashMap<IndexKey, Cell, IndexKey> cell_copy = cell_map;
  940. _clear_internal();
  941. for (const KeyValue<IndexKey, Cell> &E : cell_copy) {
  942. set_cell_item(Vector3i(E.key), E.value.item, E.value.rot);
  943. }
  944. recreating_octants = false;
  945. }
  946. void GridMap::_clear_internal() {
  947. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  948. if (is_inside_world()) {
  949. _octant_exit_world(E.key);
  950. }
  951. _octant_clean_up(E.key);
  952. memdelete(E.value);
  953. }
  954. octant_map.clear();
  955. cell_map.clear();
  956. }
  957. void GridMap::clear() {
  958. _clear_internal();
  959. clear_baked_meshes();
  960. }
  961. #ifndef DISABLE_DEPRECATED
  962. void GridMap::resource_changed(const Ref<Resource> &p_res) {
  963. }
  964. #endif
  965. void GridMap::_update_octants_callback() {
  966. if (!awaiting_update) {
  967. return;
  968. }
  969. LocalVector<OctantKey> to_delete;
  970. to_delete.reserve(octant_map.size());
  971. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  972. if (_octant_update(E.key)) {
  973. to_delete.push_back(E.key);
  974. }
  975. }
  976. while (!to_delete.is_empty()) {
  977. const OctantKey &octantkey = to_delete[0];
  978. memdelete(octant_map[octantkey]);
  979. octant_map.erase(octantkey);
  980. to_delete.remove_at_unordered(0);
  981. }
  982. _update_visibility();
  983. awaiting_update = false;
  984. }
  985. void GridMap::_bind_methods() {
  986. #ifndef PHYSICS_3D_DISABLED
  987. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  988. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  989. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  990. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  991. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &GridMap::set_collision_mask_value);
  992. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &GridMap::get_collision_mask_value);
  993. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &GridMap::set_collision_layer_value);
  994. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &GridMap::get_collision_layer_value);
  995. ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &GridMap::set_collision_priority);
  996. ClassDB::bind_method(D_METHOD("get_collision_priority"), &GridMap::get_collision_priority);
  997. ClassDB::bind_method(D_METHOD("set_physics_material", "material"), &GridMap::set_physics_material);
  998. ClassDB::bind_method(D_METHOD("get_physics_material"), &GridMap::get_physics_material);
  999. #endif // PHYSICS_3D_DISABLED
  1000. ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &GridMap::set_bake_navigation);
  1001. ClassDB::bind_method(D_METHOD("is_baking_navigation"), &GridMap::is_baking_navigation);
  1002. #ifndef NAVIGATION_3D_DISABLED
  1003. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &GridMap::set_navigation_map);
  1004. ClassDB::bind_method(D_METHOD("get_navigation_map"), &GridMap::get_navigation_map);
  1005. #endif // NAVIGATION_3D_DISABLED
  1006. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  1007. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  1008. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  1009. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  1010. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  1011. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  1012. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  1013. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  1014. ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  1015. ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item);
  1016. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);
  1017. ClassDB::bind_method(D_METHOD("get_cell_item_basis", "position"), &GridMap::get_cell_item_basis);
  1018. ClassDB::bind_method(D_METHOD("get_basis_with_orthogonal_index", "index"), &GridMap::get_basis_with_orthogonal_index);
  1019. ClassDB::bind_method(D_METHOD("get_orthogonal_index_from_basis", "basis"), &GridMap::get_orthogonal_index_from_basis);
  1020. ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &GridMap::local_to_map);
  1021. ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &GridMap::map_to_local);
  1022. #ifndef DISABLE_DEPRECATED
  1023. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  1024. #endif
  1025. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  1026. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  1027. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  1028. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  1029. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  1030. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  1031. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  1032. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  1033. ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item);
  1034. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  1035. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  1036. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  1037. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  1038. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  1039. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  1040. #ifndef PHYSICS_3D_DISABLED
  1041. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material");
  1042. #endif // PHYSICS_3D_DISABLED
  1043. ADD_GROUP("Cell", "cell_");
  1044. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size", PROPERTY_HINT_NONE, "suffix:m"), "set_cell_size", "get_cell_size");
  1045. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  1046. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  1047. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  1048. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  1049. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_scale"), "set_cell_scale", "get_cell_scale");
  1050. #ifndef PHYSICS_3D_DISABLED
  1051. ADD_GROUP("Collision", "collision_");
  1052. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  1053. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  1054. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
  1055. #endif // PHYSICS_3D_DISABLED
  1056. ADD_GROUP("Navigation", "");
  1057. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_navigation"), "set_bake_navigation", "is_baking_navigation");
  1058. BIND_CONSTANT(INVALID_CELL_ITEM);
  1059. ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size")));
  1060. ADD_SIGNAL(MethodInfo(CoreStringName(changed)));
  1061. }
  1062. void GridMap::set_cell_scale(float p_scale) {
  1063. cell_scale = p_scale;
  1064. _recreate_octant_data();
  1065. }
  1066. float GridMap::get_cell_scale() const {
  1067. return cell_scale;
  1068. }
  1069. TypedArray<Vector3i> GridMap::get_used_cells() const {
  1070. TypedArray<Vector3i> a;
  1071. a.resize(cell_map.size());
  1072. int i = 0;
  1073. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  1074. Vector3i p(E.key.x, E.key.y, E.key.z);
  1075. a[i++] = p;
  1076. }
  1077. return a;
  1078. }
  1079. TypedArray<Vector3i> GridMap::get_used_cells_by_item(int p_item) const {
  1080. TypedArray<Vector3i> a;
  1081. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  1082. if ((int)E.value.item == p_item) {
  1083. Vector3i p(E.key.x, E.key.y, E.key.z);
  1084. a.push_back(p);
  1085. }
  1086. }
  1087. return a;
  1088. }
  1089. Array GridMap::get_meshes() const {
  1090. if (mesh_library.is_null()) {
  1091. return Array();
  1092. }
  1093. Vector3 ofs = _get_offset();
  1094. Array meshes;
  1095. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  1096. int id = E.value.item;
  1097. if (!mesh_library->has_item(id)) {
  1098. continue;
  1099. }
  1100. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  1101. if (mesh.is_null()) {
  1102. continue;
  1103. }
  1104. IndexKey ik = E.key;
  1105. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  1106. Transform3D xform;
  1107. xform.basis = _ortho_bases[E.value.rot];
  1108. xform.set_origin(cellpos * cell_size + ofs);
  1109. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  1110. meshes.push_back(xform * mesh_library->get_item_mesh_transform(id));
  1111. meshes.push_back(mesh);
  1112. }
  1113. return meshes;
  1114. }
  1115. Vector3 GridMap::_get_offset() const {
  1116. return Vector3(
  1117. cell_size.x * 0.5 * int(center_x),
  1118. cell_size.y * 0.5 * int(center_y),
  1119. cell_size.z * 0.5 * int(center_z));
  1120. }
  1121. void GridMap::clear_baked_meshes() {
  1122. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1123. for (int i = 0; i < baked_meshes.size(); i++) {
  1124. RS::get_singleton()->free_rid(baked_meshes[i].instance);
  1125. }
  1126. baked_meshes.clear();
  1127. _recreate_octant_data();
  1128. }
  1129. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  1130. if (mesh_library.is_null()) {
  1131. return;
  1132. }
  1133. //generate
  1134. HashMap<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>, OctantKey> surface_map;
  1135. for (KeyValue<IndexKey, Cell> &E : cell_map) {
  1136. IndexKey key = E.key;
  1137. int item = E.value.item;
  1138. if (!mesh_library->has_item(item)) {
  1139. continue;
  1140. }
  1141. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  1142. if (mesh.is_null()) {
  1143. continue;
  1144. }
  1145. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  1146. Vector3 ofs = _get_offset();
  1147. Transform3D xform;
  1148. xform.basis = _ortho_bases[E.value.rot];
  1149. xform.set_origin(cellpos * cell_size + ofs);
  1150. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  1151. const OctantKey ok = get_octant_key_from_index_key(key);
  1152. if (!surface_map.has(ok)) {
  1153. surface_map[ok] = HashMap<Ref<Material>, Ref<SurfaceTool>>();
  1154. }
  1155. HashMap<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok];
  1156. for (int i = 0; i < mesh->get_surface_count(); i++) {
  1157. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  1158. continue;
  1159. }
  1160. Ref<Material> surf_mat = mesh->surface_get_material(i);
  1161. if (!mat_map.has(surf_mat)) {
  1162. Ref<SurfaceTool> st;
  1163. st.instantiate();
  1164. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  1165. st->set_material(surf_mat);
  1166. mat_map[surf_mat] = st;
  1167. }
  1168. mat_map[surf_mat]->append_from(mesh, i, xform);
  1169. }
  1170. }
  1171. for (KeyValue<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>> &E : surface_map) {
  1172. Ref<ArrayMesh> mesh;
  1173. mesh.instantiate();
  1174. for (KeyValue<Ref<Material>, Ref<SurfaceTool>> &F : E.value) {
  1175. F.value->commit(mesh);
  1176. }
  1177. BakedMesh bm;
  1178. bm.mesh = mesh;
  1179. bm.instance = RS::get_singleton()->instance_create();
  1180. RS::get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  1181. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  1182. if (is_inside_tree()) {
  1183. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  1184. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  1185. }
  1186. if (p_gen_lightmap_uv) {
  1187. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  1188. }
  1189. baked_meshes.push_back(bm);
  1190. }
  1191. _recreate_octant_data();
  1192. }
  1193. Array GridMap::get_bake_meshes() {
  1194. if (!baked_meshes.size()) {
  1195. make_baked_meshes(true);
  1196. }
  1197. Array arr;
  1198. for (int i = 0; i < baked_meshes.size(); i++) {
  1199. arr.push_back(baked_meshes[i].mesh);
  1200. arr.push_back(Transform3D());
  1201. }
  1202. return arr;
  1203. }
  1204. RID GridMap::get_bake_mesh_instance(int p_idx) {
  1205. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  1206. return baked_meshes[p_idx].instance;
  1207. }
  1208. GridMap::GridMap() {
  1209. set_notify_transform(true);
  1210. #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  1211. NavigationServer3D::get_singleton()->connect("map_changed", callable_mp(this, &GridMap::_navigation_map_changed));
  1212. NavigationServer3D::get_singleton()->connect("navigation_debug_changed", callable_mp(this, &GridMap::_update_navigation_debug_edge_connections));
  1213. #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  1214. }
  1215. #ifndef NAVIGATION_3D_DISABLED
  1216. void GridMap::navmesh_parse_init() {
  1217. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  1218. if (!_navmesh_source_geometry_parser.is_valid()) {
  1219. _navmesh_source_geometry_parsing_callback = callable_mp_static(&GridMap::navmesh_parse_source_geometry);
  1220. _navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create();
  1221. NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  1222. }
  1223. }
  1224. void GridMap::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) {
  1225. GridMap *gridmap = Object::cast_to<GridMap>(p_node);
  1226. if (gridmap == nullptr) {
  1227. return;
  1228. }
  1229. NavigationMesh::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
  1230. #ifndef PHYSICS_3D_DISABLED
  1231. uint32_t parsed_collision_mask = p_navigation_mesh->get_collision_mask();
  1232. #endif // PHYSICS_3D_DISABLED
  1233. if (parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) {
  1234. Array meshes = gridmap->get_meshes();
  1235. Transform3D xform = gridmap->get_global_transform();
  1236. for (int i = 0; i < meshes.size(); i += 2) {
  1237. Ref<Mesh> mesh = meshes[i + 1];
  1238. if (mesh.is_valid()) {
  1239. p_source_geometry_data->add_mesh(mesh, xform * (Transform3D)meshes[i]);
  1240. }
  1241. }
  1242. }
  1243. #ifndef PHYSICS_3D_DISABLED
  1244. else if ((parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) && (gridmap->get_collision_layer() & parsed_collision_mask)) {
  1245. Array shapes = gridmap->get_collision_shapes();
  1246. for (int i = 0; i < shapes.size(); i += 2) {
  1247. RID shape = shapes[i + 1];
  1248. PhysicsServer3D::ShapeType type = PhysicsServer3D::get_singleton()->shape_get_type(shape);
  1249. Variant data = PhysicsServer3D::get_singleton()->shape_get_data(shape);
  1250. switch (type) {
  1251. case PhysicsServer3D::SHAPE_SPHERE: {
  1252. real_t radius = data;
  1253. Array arr;
  1254. arr.resize(RS::ARRAY_MAX);
  1255. SphereMesh::create_mesh_array(arr, radius, radius * 2.0);
  1256. p_source_geometry_data->add_mesh_array(arr, shapes[i]);
  1257. } break;
  1258. case PhysicsServer3D::SHAPE_BOX: {
  1259. Vector3 extents = data;
  1260. Array arr;
  1261. arr.resize(RS::ARRAY_MAX);
  1262. BoxMesh::create_mesh_array(arr, extents * 2.0);
  1263. p_source_geometry_data->add_mesh_array(arr, shapes[i]);
  1264. } break;
  1265. case PhysicsServer3D::SHAPE_CAPSULE: {
  1266. Dictionary dict = data;
  1267. real_t radius = dict["radius"];
  1268. real_t height = dict["height"];
  1269. Array arr;
  1270. arr.resize(RS::ARRAY_MAX);
  1271. CapsuleMesh::create_mesh_array(arr, radius, height);
  1272. p_source_geometry_data->add_mesh_array(arr, shapes[i]);
  1273. } break;
  1274. case PhysicsServer3D::SHAPE_CYLINDER: {
  1275. Dictionary dict = data;
  1276. real_t radius = dict["radius"];
  1277. real_t height = dict["height"];
  1278. Array arr;
  1279. arr.resize(RS::ARRAY_MAX);
  1280. CylinderMesh::create_mesh_array(arr, radius, radius, height);
  1281. p_source_geometry_data->add_mesh_array(arr, shapes[i]);
  1282. } break;
  1283. case PhysicsServer3D::SHAPE_CONVEX_POLYGON: {
  1284. PackedVector3Array vertices = data;
  1285. Geometry3D::MeshData md;
  1286. Error err = ConvexHullComputer::convex_hull(vertices, md);
  1287. if (err == OK) {
  1288. PackedVector3Array faces;
  1289. for (const Geometry3D::MeshData::Face &face : md.faces) {
  1290. for (uint32_t k = 2; k < face.indices.size(); ++k) {
  1291. faces.push_back(md.vertices[face.indices[0]]);
  1292. faces.push_back(md.vertices[face.indices[k - 1]]);
  1293. faces.push_back(md.vertices[face.indices[k]]);
  1294. }
  1295. }
  1296. p_source_geometry_data->add_faces(faces, shapes[i]);
  1297. }
  1298. } break;
  1299. case PhysicsServer3D::SHAPE_CONCAVE_POLYGON: {
  1300. Dictionary dict = data;
  1301. PackedVector3Array faces = Variant(dict["faces"]);
  1302. p_source_geometry_data->add_faces(faces, shapes[i]);
  1303. } break;
  1304. case PhysicsServer3D::SHAPE_HEIGHTMAP: {
  1305. Dictionary dict = data;
  1306. ///< dict( int:"width", int:"depth",float:"cell_size", float_array:"heights"
  1307. int heightmap_depth = dict["depth"];
  1308. int heightmap_width = dict["width"];
  1309. if (heightmap_depth >= 2 && heightmap_width >= 2) {
  1310. const Vector<real_t> &map_data = dict["heights"];
  1311. Vector2 heightmap_gridsize(heightmap_width - 1, heightmap_depth - 1);
  1312. Vector3 start = Vector3(heightmap_gridsize.x, 0, heightmap_gridsize.y) * -0.5;
  1313. Vector<Vector3> vertex_array;
  1314. vertex_array.resize((heightmap_depth - 1) * (heightmap_width - 1) * 6);
  1315. Vector3 *vertex_array_ptrw = vertex_array.ptrw();
  1316. const real_t *map_data_ptr = map_data.ptr();
  1317. int vertex_index = 0;
  1318. for (int d = 0; d < heightmap_depth - 1; d++) {
  1319. for (int w = 0; w < heightmap_width - 1; w++) {
  1320. vertex_array_ptrw[vertex_index] = start + Vector3(w, map_data_ptr[(heightmap_width * d) + w], d);
  1321. vertex_array_ptrw[vertex_index + 1] = start + Vector3(w + 1, map_data_ptr[(heightmap_width * d) + w + 1], d);
  1322. vertex_array_ptrw[vertex_index + 2] = start + Vector3(w, map_data_ptr[(heightmap_width * d) + heightmap_width + w], d + 1);
  1323. vertex_array_ptrw[vertex_index + 3] = start + Vector3(w + 1, map_data_ptr[(heightmap_width * d) + w + 1], d);
  1324. vertex_array_ptrw[vertex_index + 4] = start + Vector3(w + 1, map_data_ptr[(heightmap_width * d) + heightmap_width + w + 1], d + 1);
  1325. vertex_array_ptrw[vertex_index + 5] = start + Vector3(w, map_data_ptr[(heightmap_width * d) + heightmap_width + w], d + 1);
  1326. vertex_index += 6;
  1327. }
  1328. }
  1329. if (vertex_array.size() > 0) {
  1330. p_source_geometry_data->add_faces(vertex_array, shapes[i]);
  1331. }
  1332. }
  1333. } break;
  1334. default: {
  1335. WARN_PRINT("Unsupported collision shape type.");
  1336. } break;
  1337. }
  1338. }
  1339. }
  1340. #endif // PHYSICS_3D_DISABLED
  1341. }
  1342. #endif // NAVIGATION_3D_DISABLED
  1343. #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  1344. void GridMap::_update_navigation_debug_edge_connections() {
  1345. if (bake_navigation) {
  1346. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  1347. _update_octant_navigation_debug_edge_connections_mesh(E.key);
  1348. }
  1349. }
  1350. }
  1351. void GridMap::_navigation_map_changed(RID p_map) {
  1352. if (bake_navigation && is_inside_tree() && p_map == get_world_3d()->get_navigation_map()) {
  1353. _update_navigation_debug_edge_connections();
  1354. }
  1355. }
  1356. #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  1357. GridMap::~GridMap() {
  1358. clear();
  1359. #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  1360. NavigationServer3D::get_singleton()->disconnect("map_changed", callable_mp(this, &GridMap::_navigation_map_changed));
  1361. NavigationServer3D::get_singleton()->disconnect("navigation_debug_changed", callable_mp(this, &GridMap::_update_navigation_debug_edge_connections));
  1362. #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  1363. }
  1364. #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)
  1365. void GridMap::_update_octant_navigation_debug_edge_connections_mesh(const OctantKey &p_key) {
  1366. ERR_FAIL_COND(!octant_map.has(p_key));
  1367. Octant &g = *octant_map[p_key];
  1368. if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  1369. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  1370. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false);
  1371. }
  1372. return;
  1373. }
  1374. if (!is_inside_tree()) {
  1375. return;
  1376. }
  1377. if (!bake_navigation) {
  1378. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  1379. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false);
  1380. }
  1381. return;
  1382. }
  1383. if (!g.navigation_debug_edge_connections_instance.is_valid()) {
  1384. g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
  1385. }
  1386. if (g.navigation_debug_edge_connections_mesh.is_null()) {
  1387. g.navigation_debug_edge_connections_mesh.instantiate();
  1388. }
  1389. g.navigation_debug_edge_connections_mesh->clear_surfaces();
  1390. float edge_connection_margin = NavigationServer3D::get_singleton()->map_get_edge_connection_margin(get_world_3d()->get_navigation_map());
  1391. float half_edge_connection_margin = edge_connection_margin * 0.5;
  1392. Vector<Vector3> vertex_array;
  1393. for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) {
  1394. if (cell_map.has(F.key) && F.value.region.is_valid()) {
  1395. int connections_count = NavigationServer3D::get_singleton()->region_get_connections_count(F.value.region);
  1396. if (connections_count == 0) {
  1397. continue;
  1398. }
  1399. for (int i = 0; i < connections_count; i++) {
  1400. Vector3 connection_pathway_start = NavigationServer3D::get_singleton()->region_get_connection_pathway_start(F.value.region, i);
  1401. Vector3 connection_pathway_end = NavigationServer3D::get_singleton()->region_get_connection_pathway_end(F.value.region, i);
  1402. Vector3 direction_start_end = connection_pathway_start.direction_to(connection_pathway_end);
  1403. Vector3 direction_end_start = connection_pathway_end.direction_to(connection_pathway_start);
  1404. Vector3 start_right_dir = direction_start_end.cross(Vector3(0, 1, 0));
  1405. Vector3 start_left_dir = -start_right_dir;
  1406. Vector3 end_right_dir = direction_end_start.cross(Vector3(0, 1, 0));
  1407. Vector3 end_left_dir = -end_right_dir;
  1408. Vector3 left_start_pos = connection_pathway_start + (start_left_dir * half_edge_connection_margin);
  1409. Vector3 right_start_pos = connection_pathway_start + (start_right_dir * half_edge_connection_margin);
  1410. Vector3 left_end_pos = connection_pathway_end + (end_right_dir * half_edge_connection_margin);
  1411. Vector3 right_end_pos = connection_pathway_end + (end_left_dir * half_edge_connection_margin);
  1412. vertex_array.push_back(right_end_pos);
  1413. vertex_array.push_back(left_start_pos);
  1414. vertex_array.push_back(right_start_pos);
  1415. vertex_array.push_back(left_end_pos);
  1416. vertex_array.push_back(right_end_pos);
  1417. vertex_array.push_back(right_start_pos);
  1418. }
  1419. }
  1420. }
  1421. if (vertex_array.is_empty()) {
  1422. return;
  1423. }
  1424. Ref<StandardMaterial3D> edge_connections_material = NavigationServer3D::get_singleton()->get_debug_navigation_edge_connections_material();
  1425. Array mesh_array;
  1426. mesh_array.resize(Mesh::ARRAY_MAX);
  1427. mesh_array[Mesh::ARRAY_VERTEX] = vertex_array;
  1428. g.navigation_debug_edge_connections_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mesh_array);
  1429. g.navigation_debug_edge_connections_mesh->surface_set_material(0, edge_connections_material);
  1430. RS::get_singleton()->instance_set_base(g.navigation_debug_edge_connections_instance, g.navigation_debug_edge_connections_mesh->get_rid());
  1431. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, is_visible_in_tree());
  1432. if (is_inside_tree()) {
  1433. RS::get_singleton()->instance_set_scenario(g.navigation_debug_edge_connections_instance, get_world_3d()->get_scenario());
  1434. }
  1435. bool enable_edge_connections = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_connections();
  1436. if (!enable_edge_connections) {
  1437. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false);
  1438. }
  1439. }
  1440. #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED)