grid_map.cpp 58 KB

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