grid_map.cpp 59 KB

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