grid_map.cpp 59 KB

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