grid_map.cpp 56 KB

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