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