grid_map.cpp 47 KB

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