grid_map.cpp 49 KB

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