grid_map.cpp 49 KB

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