grid_map.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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_layers) {
  194. navigation_layers = p_layers;
  195. _recreate_octant_data();
  196. }
  197. uint32_t GridMap::get_navigation_layers() {
  198. return navigation_layers;
  199. }
  200. void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) {
  201. if (!mesh_library.is_null()) {
  202. mesh_library->unregister_owner(this);
  203. }
  204. mesh_library = p_mesh_library;
  205. if (!mesh_library.is_null()) {
  206. mesh_library->register_owner(this);
  207. }
  208. _recreate_octant_data();
  209. }
  210. Ref<MeshLibrary> GridMap::get_mesh_library() const {
  211. return mesh_library;
  212. }
  213. void GridMap::set_cell_size(const Vector3 &p_size) {
  214. ERR_FAIL_COND(p_size.x < 0.001 || p_size.y < 0.001 || p_size.z < 0.001);
  215. cell_size = p_size;
  216. _recreate_octant_data();
  217. emit_signal(SNAME("cell_size_changed"), cell_size);
  218. }
  219. Vector3 GridMap::get_cell_size() const {
  220. return cell_size;
  221. }
  222. void GridMap::set_octant_size(int p_size) {
  223. ERR_FAIL_COND(p_size == 0);
  224. octant_size = p_size;
  225. _recreate_octant_data();
  226. }
  227. int GridMap::get_octant_size() const {
  228. return octant_size;
  229. }
  230. void GridMap::set_center_x(bool p_enable) {
  231. center_x = p_enable;
  232. _recreate_octant_data();
  233. }
  234. bool GridMap::get_center_x() const {
  235. return center_x;
  236. }
  237. void GridMap::set_center_y(bool p_enable) {
  238. center_y = p_enable;
  239. _recreate_octant_data();
  240. }
  241. bool GridMap::get_center_y() const {
  242. return center_y;
  243. }
  244. void GridMap::set_center_z(bool p_enable) {
  245. center_z = p_enable;
  246. _recreate_octant_data();
  247. }
  248. bool GridMap::get_center_z() const {
  249. return center_z;
  250. }
  251. void GridMap::set_cell_item(const Vector3i &p_position, int p_item, int p_rot) {
  252. if (baked_meshes.size() && !recreating_octants) {
  253. //if you set a cell item, baked meshes go good bye
  254. clear_baked_meshes();
  255. _recreate_octant_data();
  256. }
  257. ERR_FAIL_INDEX(ABS(p_position.x), 1 << 20);
  258. ERR_FAIL_INDEX(ABS(p_position.y), 1 << 20);
  259. ERR_FAIL_INDEX(ABS(p_position.z), 1 << 20);
  260. IndexKey key;
  261. key.x = p_position.x;
  262. key.y = p_position.y;
  263. key.z = p_position.z;
  264. OctantKey ok;
  265. ok.x = p_position.x / octant_size;
  266. ok.y = p_position.y / octant_size;
  267. ok.z = p_position.z / octant_size;
  268. if (p_item < 0) {
  269. //erase
  270. if (cell_map.has(key)) {
  271. OctantKey octantkey = ok;
  272. ERR_FAIL_COND(!octant_map.has(octantkey));
  273. Octant &g = *octant_map[octantkey];
  274. g.cells.erase(key);
  275. g.dirty = true;
  276. cell_map.erase(key);
  277. _queue_octants_dirty();
  278. }
  279. return;
  280. }
  281. OctantKey octantkey = ok;
  282. if (!octant_map.has(octantkey)) {
  283. //create octant because it does not exist
  284. Octant *g = memnew(Octant);
  285. g->dirty = true;
  286. g->static_body = PhysicsServer3D::get_singleton()->body_create();
  287. PhysicsServer3D::get_singleton()->body_set_mode(g->static_body, PhysicsServer3D::BODY_MODE_STATIC);
  288. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
  289. PhysicsServer3D::get_singleton()->body_set_collision_layer(g->static_body, collision_layer);
  290. PhysicsServer3D::get_singleton()->body_set_collision_mask(g->static_body, collision_mask);
  291. if (physics_material.is_valid()) {
  292. PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_FRICTION, physics_material->get_friction());
  293. PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material->get_bounce());
  294. }
  295. SceneTree *st = SceneTree::get_singleton();
  296. if (st && st->is_debugging_collisions_hint()) {
  297. g->collision_debug = RenderingServer::get_singleton()->mesh_create();
  298. g->collision_debug_instance = RenderingServer::get_singleton()->instance_create();
  299. RenderingServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  300. }
  301. octant_map[octantkey] = g;
  302. if (is_inside_world()) {
  303. _octant_enter_world(octantkey);
  304. _octant_transform(octantkey);
  305. }
  306. }
  307. Octant &g = *octant_map[octantkey];
  308. g.cells.insert(key);
  309. g.dirty = true;
  310. _queue_octants_dirty();
  311. Cell c;
  312. c.item = p_item;
  313. c.rot = p_rot;
  314. cell_map[key] = c;
  315. }
  316. int GridMap::get_cell_item(const Vector3i &p_position) const {
  317. ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, INVALID_CELL_ITEM);
  318. ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, INVALID_CELL_ITEM);
  319. ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, INVALID_CELL_ITEM);
  320. IndexKey key;
  321. key.x = p_position.x;
  322. key.y = p_position.y;
  323. key.z = p_position.z;
  324. if (!cell_map.has(key)) {
  325. return INVALID_CELL_ITEM;
  326. }
  327. return cell_map[key].item;
  328. }
  329. int GridMap::get_cell_item_orientation(const Vector3i &p_position) const {
  330. ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, -1);
  331. ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, -1);
  332. ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, -1);
  333. IndexKey key;
  334. key.x = p_position.x;
  335. key.y = p_position.y;
  336. key.z = p_position.z;
  337. if (!cell_map.has(key)) {
  338. return -1;
  339. }
  340. return cell_map[key].rot;
  341. }
  342. Vector3i GridMap::world_to_map(const Vector3 &p_world_position) const {
  343. Vector3 map_position = (p_world_position / cell_size).floor();
  344. return Vector3i(map_position);
  345. }
  346. Vector3 GridMap::map_to_world(const Vector3i &p_map_position) const {
  347. Vector3 offset = _get_offset();
  348. Vector3 world_pos(
  349. p_map_position.x * cell_size.x + offset.x,
  350. p_map_position.y * cell_size.y + offset.y,
  351. p_map_position.z * cell_size.z + offset.z);
  352. return world_pos;
  353. }
  354. void GridMap::_octant_transform(const OctantKey &p_key) {
  355. ERR_FAIL_COND(!octant_map.has(p_key));
  356. Octant &g = *octant_map[p_key];
  357. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  358. if (g.collision_debug_instance.is_valid()) {
  359. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  360. }
  361. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  362. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  363. }
  364. }
  365. bool GridMap::_octant_update(const OctantKey &p_key) {
  366. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  367. Octant &g = *octant_map[p_key];
  368. if (!g.dirty) {
  369. return false;
  370. }
  371. //erase body shapes
  372. PhysicsServer3D::get_singleton()->body_clear_shapes(g.static_body);
  373. //erase body shapes debug
  374. if (g.collision_debug.is_valid()) {
  375. RS::get_singleton()->mesh_clear(g.collision_debug);
  376. }
  377. //erase navigation
  378. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  379. NavigationServer3D::get_singleton()->free(E.value.region);
  380. }
  381. g.navmesh_ids.clear();
  382. //erase multimeshes
  383. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  384. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  385. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  386. }
  387. g.multimesh_instances.clear();
  388. if (g.cells.size() == 0) {
  389. //octant no longer needed
  390. _octant_clean_up(p_key);
  391. return true;
  392. }
  393. Vector<Vector3> col_debug;
  394. /*
  395. * foreach item in this octant,
  396. * set item's multimesh's instance count to number of cells which have this item
  397. * and set said multimesh bounding box to one containing all cells which have this item
  398. */
  399. HashMap<int, List<Pair<Transform3D, IndexKey>>> multimesh_items;
  400. for (const IndexKey &E : g.cells) {
  401. ERR_CONTINUE(!cell_map.has(E));
  402. const Cell &c = cell_map[E];
  403. if (!mesh_library.is_valid() || !mesh_library->has_item(c.item)) {
  404. continue;
  405. }
  406. Vector3 cellpos = Vector3(E.x, E.y, E.z);
  407. Vector3 ofs = _get_offset();
  408. Transform3D xform;
  409. xform.basis.set_orthogonal_index(c.rot);
  410. xform.set_origin(cellpos * cell_size + ofs);
  411. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  412. if (baked_meshes.size() == 0) {
  413. if (mesh_library->get_item_mesh(c.item).is_valid()) {
  414. if (!multimesh_items.has(c.item)) {
  415. multimesh_items[c.item] = List<Pair<Transform3D, IndexKey>>();
  416. }
  417. Pair<Transform3D, IndexKey> p;
  418. p.first = xform * mesh_library->get_item_mesh_transform(c.item);
  419. p.second = E;
  420. multimesh_items[c.item].push_back(p);
  421. }
  422. }
  423. Vector<MeshLibrary::ShapeData> shapes = mesh_library->get_item_shapes(c.item);
  424. // add the item's shape at given xform to octant's static_body
  425. for (int i = 0; i < shapes.size(); i++) {
  426. // add the item's shape
  427. if (!shapes[i].shape.is_valid()) {
  428. continue;
  429. }
  430. PhysicsServer3D::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  431. if (g.collision_debug.is_valid()) {
  432. shapes.write[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  433. }
  434. }
  435. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  436. Ref<NavigationMesh> navmesh = mesh_library->get_item_navmesh(c.item);
  437. if (navmesh.is_valid()) {
  438. Octant::NavMesh nm;
  439. nm.xform = xform * mesh_library->get_item_navmesh_transform(c.item);
  440. if (bake_navigation) {
  441. RID region = NavigationServer3D::get_singleton()->region_create();
  442. NavigationServer3D::get_singleton()->region_set_layers(region, navigation_layers);
  443. NavigationServer3D::get_singleton()->region_set_navmesh(region, navmesh);
  444. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * mesh_library->get_item_navmesh_transform(c.item));
  445. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  446. nm.region = region;
  447. }
  448. g.navmesh_ids[E] = nm;
  449. }
  450. }
  451. //update multimeshes, only if not baked
  452. if (baked_meshes.size() == 0) {
  453. for (const KeyValue<int, List<Pair<Transform3D, IndexKey>>> &E : multimesh_items) {
  454. Octant::MultimeshInstance mmi;
  455. RID mm = RS::get_singleton()->multimesh_create();
  456. RS::get_singleton()->multimesh_allocate_data(mm, E.value.size(), RS::MULTIMESH_TRANSFORM_3D);
  457. RS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E.key)->get_rid());
  458. int idx = 0;
  459. for (const Pair<Transform3D, IndexKey> &F : E.value) {
  460. RS::get_singleton()->multimesh_instance_set_transform(mm, idx, F.first);
  461. #ifdef TOOLS_ENABLED
  462. Octant::MultimeshInstance::Item it;
  463. it.index = idx;
  464. it.transform = F.first;
  465. it.key = F.second;
  466. mmi.items.push_back(it);
  467. #endif
  468. idx++;
  469. }
  470. RID instance = RS::get_singleton()->instance_create();
  471. RS::get_singleton()->instance_set_base(instance, mm);
  472. if (is_inside_tree()) {
  473. RS::get_singleton()->instance_set_scenario(instance, get_world_3d()->get_scenario());
  474. RS::get_singleton()->instance_set_transform(instance, get_global_transform());
  475. }
  476. mmi.multimesh = mm;
  477. mmi.instance = instance;
  478. g.multimesh_instances.push_back(mmi);
  479. }
  480. }
  481. if (col_debug.size()) {
  482. Array arr;
  483. arr.resize(RS::ARRAY_MAX);
  484. arr[RS::ARRAY_VERTEX] = col_debug;
  485. RS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, RS::PRIMITIVE_LINES, arr);
  486. SceneTree *st = SceneTree::get_singleton();
  487. if (st) {
  488. RS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  489. }
  490. }
  491. g.dirty = false;
  492. return false;
  493. }
  494. void GridMap::_reset_physic_bodies_collision_filters() {
  495. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  496. PhysicsServer3D::get_singleton()->body_set_collision_layer(E.value->static_body, collision_layer);
  497. PhysicsServer3D::get_singleton()->body_set_collision_mask(E.value->static_body, collision_mask);
  498. }
  499. }
  500. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  501. ERR_FAIL_COND(!octant_map.has(p_key));
  502. Octant &g = *octant_map[p_key];
  503. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  504. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, get_world_3d()->get_space());
  505. if (g.collision_debug_instance.is_valid()) {
  506. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world_3d()->get_scenario());
  507. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  508. }
  509. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  510. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world_3d()->get_scenario());
  511. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  512. }
  513. if (bake_navigation && mesh_library.is_valid()) {
  514. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  515. if (cell_map.has(F.key) && F.value.region.is_valid() == false) {
  516. Ref<NavigationMesh> nm = mesh_library->get_item_navmesh(cell_map[F.key].item);
  517. if (nm.is_valid()) {
  518. RID region = NavigationServer3D::get_singleton()->region_create();
  519. NavigationServer3D::get_singleton()->region_set_layers(region, navigation_layers);
  520. NavigationServer3D::get_singleton()->region_set_navmesh(region, nm);
  521. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform);
  522. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  523. F.value.region = region;
  524. }
  525. }
  526. }
  527. }
  528. }
  529. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  530. ERR_FAIL_COND(!octant_map.has(p_key));
  531. Octant &g = *octant_map[p_key];
  532. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  533. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID());
  534. if (g.collision_debug_instance.is_valid()) {
  535. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  536. }
  537. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  538. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  539. }
  540. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  541. if (F.value.region.is_valid()) {
  542. NavigationServer3D::get_singleton()->free(F.value.region);
  543. F.value.region = RID();
  544. }
  545. }
  546. }
  547. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  548. ERR_FAIL_COND(!octant_map.has(p_key));
  549. Octant &g = *octant_map[p_key];
  550. if (g.collision_debug.is_valid()) {
  551. RS::get_singleton()->free(g.collision_debug);
  552. }
  553. if (g.collision_debug_instance.is_valid()) {
  554. RS::get_singleton()->free(g.collision_debug_instance);
  555. }
  556. PhysicsServer3D::get_singleton()->free(g.static_body);
  557. // Erase navigation
  558. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  559. NavigationServer3D::get_singleton()->free(E.value.region);
  560. }
  561. g.navmesh_ids.clear();
  562. //erase multimeshes
  563. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  564. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  565. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  566. }
  567. g.multimesh_instances.clear();
  568. }
  569. void GridMap::_notification(int p_what) {
  570. switch (p_what) {
  571. case NOTIFICATION_ENTER_WORLD: {
  572. last_transform = get_global_transform();
  573. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  574. _octant_enter_world(E.key);
  575. }
  576. for (int i = 0; i < baked_meshes.size(); i++) {
  577. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world_3d()->get_scenario());
  578. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  579. }
  580. } break;
  581. case NOTIFICATION_TRANSFORM_CHANGED: {
  582. Transform3D new_xform = get_global_transform();
  583. if (new_xform == last_transform) {
  584. break;
  585. }
  586. //update run
  587. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  588. _octant_transform(E.key);
  589. }
  590. last_transform = new_xform;
  591. for (int i = 0; i < baked_meshes.size(); i++) {
  592. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  593. }
  594. } break;
  595. case NOTIFICATION_EXIT_WORLD: {
  596. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  597. _octant_exit_world(E.key);
  598. }
  599. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  600. //_update_octants_callback();
  601. //_update_area_instances();
  602. for (int i = 0; i < baked_meshes.size(); i++) {
  603. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID());
  604. }
  605. } break;
  606. case NOTIFICATION_VISIBILITY_CHANGED: {
  607. _update_visibility();
  608. } break;
  609. }
  610. }
  611. void GridMap::_update_visibility() {
  612. if (!is_inside_tree()) {
  613. return;
  614. }
  615. for (KeyValue<OctantKey, Octant *> &e : octant_map) {
  616. Octant *octant = e.value;
  617. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  618. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  619. RS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree());
  620. }
  621. }
  622. for (int i = 0; i < baked_meshes.size(); i++) {
  623. RS::get_singleton()->instance_set_visible(baked_meshes[i].instance, is_visible_in_tree());
  624. }
  625. }
  626. void GridMap::_queue_octants_dirty() {
  627. if (awaiting_update) {
  628. return;
  629. }
  630. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  631. awaiting_update = true;
  632. }
  633. void GridMap::_recreate_octant_data() {
  634. recreating_octants = true;
  635. HashMap<IndexKey, Cell, IndexKey> cell_copy = cell_map;
  636. _clear_internal();
  637. for (const KeyValue<IndexKey, Cell> &E : cell_copy) {
  638. set_cell_item(Vector3i(E.key), E.value.item, E.value.rot);
  639. }
  640. recreating_octants = false;
  641. }
  642. void GridMap::_clear_internal() {
  643. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  644. if (is_inside_world()) {
  645. _octant_exit_world(E.key);
  646. }
  647. _octant_clean_up(E.key);
  648. memdelete(E.value);
  649. }
  650. octant_map.clear();
  651. cell_map.clear();
  652. }
  653. void GridMap::clear() {
  654. _clear_internal();
  655. clear_baked_meshes();
  656. }
  657. void GridMap::resource_changed(const Ref<Resource> &p_res) {
  658. _recreate_octant_data();
  659. }
  660. void GridMap::_update_octants_callback() {
  661. if (!awaiting_update) {
  662. return;
  663. }
  664. List<OctantKey> to_delete;
  665. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  666. if (_octant_update(E.key)) {
  667. to_delete.push_back(E.key);
  668. }
  669. }
  670. while (to_delete.front()) {
  671. memdelete(octant_map[to_delete.front()->get()]);
  672. octant_map.erase(to_delete.front()->get());
  673. to_delete.pop_front();
  674. }
  675. _update_visibility();
  676. awaiting_update = false;
  677. }
  678. void GridMap::_bind_methods() {
  679. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  680. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  681. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  682. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  683. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &GridMap::set_collision_mask_value);
  684. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &GridMap::get_collision_mask_value);
  685. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &GridMap::set_collision_layer_value);
  686. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &GridMap::get_collision_layer_value);
  687. ClassDB::bind_method(D_METHOD("set_physics_material", "material"), &GridMap::set_physics_material);
  688. ClassDB::bind_method(D_METHOD("get_physics_material"), &GridMap::get_physics_material);
  689. ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &GridMap::set_bake_navigation);
  690. ClassDB::bind_method(D_METHOD("is_baking_navigation"), &GridMap::is_baking_navigation);
  691. ClassDB::bind_method(D_METHOD("set_navigation_layers", "layers"), &GridMap::set_navigation_layers);
  692. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &GridMap::get_navigation_layers);
  693. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  694. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  695. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  696. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  697. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  698. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  699. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  700. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  701. ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  702. ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item);
  703. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);
  704. ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &GridMap::world_to_map);
  705. ClassDB::bind_method(D_METHOD("map_to_world", "map_position"), &GridMap::map_to_world);
  706. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  707. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  708. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  709. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  710. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  711. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  712. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  713. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  714. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  715. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  716. ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item);
  717. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  718. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  719. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  720. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  721. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  722. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  723. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material");
  724. ADD_GROUP("Cell", "cell_");
  725. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size"), "set_cell_size", "get_cell_size");
  726. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  727. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  728. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  729. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  730. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_scale"), "set_cell_scale", "get_cell_scale");
  731. ADD_GROUP("Collision", "collision_");
  732. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  733. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  734. ADD_GROUP("Navigation", "");
  735. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_navigation"), "set_bake_navigation", "is_baking_navigation");
  736. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  737. BIND_CONSTANT(INVALID_CELL_ITEM);
  738. ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size")));
  739. }
  740. void GridMap::set_cell_scale(float p_scale) {
  741. cell_scale = p_scale;
  742. _recreate_octant_data();
  743. }
  744. float GridMap::get_cell_scale() const {
  745. return cell_scale;
  746. }
  747. Array GridMap::get_used_cells() const {
  748. Array a;
  749. a.resize(cell_map.size());
  750. int i = 0;
  751. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  752. Vector3 p(E.key.x, E.key.y, E.key.z);
  753. a[i++] = p;
  754. }
  755. return a;
  756. }
  757. Array GridMap::get_used_cells_by_item(int p_item) const {
  758. Array a;
  759. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  760. if (E.value.item == p_item) {
  761. Vector3 p(E.key.x, E.key.y, E.key.z);
  762. a.push_back(p);
  763. }
  764. }
  765. return a;
  766. }
  767. Array GridMap::get_meshes() const {
  768. if (mesh_library.is_null()) {
  769. return Array();
  770. }
  771. Vector3 ofs = _get_offset();
  772. Array meshes;
  773. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  774. int id = E.value.item;
  775. if (!mesh_library->has_item(id)) {
  776. continue;
  777. }
  778. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  779. if (mesh.is_null()) {
  780. continue;
  781. }
  782. IndexKey ik = E.key;
  783. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  784. Transform3D xform;
  785. xform.basis.set_orthogonal_index(E.value.rot);
  786. xform.set_origin(cellpos * cell_size + ofs);
  787. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  788. meshes.push_back(xform);
  789. meshes.push_back(mesh);
  790. }
  791. return meshes;
  792. }
  793. Vector3 GridMap::_get_offset() const {
  794. return Vector3(
  795. cell_size.x * 0.5 * int(center_x),
  796. cell_size.y * 0.5 * int(center_y),
  797. cell_size.z * 0.5 * int(center_z));
  798. }
  799. void GridMap::clear_baked_meshes() {
  800. for (int i = 0; i < baked_meshes.size(); i++) {
  801. RS::get_singleton()->free(baked_meshes[i].instance);
  802. }
  803. baked_meshes.clear();
  804. _recreate_octant_data();
  805. }
  806. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  807. if (!mesh_library.is_valid()) {
  808. return;
  809. }
  810. //generate
  811. HashMap<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>, OctantKey> surface_map;
  812. for (KeyValue<IndexKey, Cell> &E : cell_map) {
  813. IndexKey key = E.key;
  814. int item = E.value.item;
  815. if (!mesh_library->has_item(item)) {
  816. continue;
  817. }
  818. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  819. if (!mesh.is_valid()) {
  820. continue;
  821. }
  822. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  823. Vector3 ofs = _get_offset();
  824. Transform3D xform;
  825. xform.basis.set_orthogonal_index(E.value.rot);
  826. xform.set_origin(cellpos * cell_size + ofs);
  827. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  828. OctantKey ok;
  829. ok.x = key.x / octant_size;
  830. ok.y = key.y / octant_size;
  831. ok.z = key.z / octant_size;
  832. if (!surface_map.has(ok)) {
  833. surface_map[ok] = HashMap<Ref<Material>, Ref<SurfaceTool>>();
  834. }
  835. HashMap<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok];
  836. for (int i = 0; i < mesh->get_surface_count(); i++) {
  837. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  838. continue;
  839. }
  840. Ref<Material> surf_mat = mesh->surface_get_material(i);
  841. if (!mat_map.has(surf_mat)) {
  842. Ref<SurfaceTool> st;
  843. st.instantiate();
  844. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  845. st->set_material(surf_mat);
  846. mat_map[surf_mat] = st;
  847. }
  848. mat_map[surf_mat]->append_from(mesh, i, xform);
  849. }
  850. }
  851. for (KeyValue<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>> &E : surface_map) {
  852. Ref<ArrayMesh> mesh;
  853. mesh.instantiate();
  854. for (KeyValue<Ref<Material>, Ref<SurfaceTool>> &F : E.value) {
  855. F.value->commit(mesh);
  856. }
  857. BakedMesh bm;
  858. bm.mesh = mesh;
  859. bm.instance = RS::get_singleton()->instance_create();
  860. RS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  861. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  862. if (is_inside_tree()) {
  863. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  864. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  865. }
  866. if (p_gen_lightmap_uv) {
  867. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  868. }
  869. baked_meshes.push_back(bm);
  870. }
  871. _recreate_octant_data();
  872. }
  873. Array GridMap::get_bake_meshes() {
  874. if (!baked_meshes.size()) {
  875. make_baked_meshes(true);
  876. }
  877. Array arr;
  878. for (int i = 0; i < baked_meshes.size(); i++) {
  879. arr.push_back(baked_meshes[i].mesh);
  880. arr.push_back(Transform3D());
  881. }
  882. return arr;
  883. }
  884. RID GridMap::get_bake_mesh_instance(int p_idx) {
  885. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  886. return baked_meshes[p_idx].instance;
  887. }
  888. GridMap::GridMap() {
  889. set_notify_transform(true);
  890. }
  891. GridMap::~GridMap() {
  892. if (!mesh_library.is_null()) {
  893. mesh_library->unregister_owner(this);
  894. }
  895. clear();
  896. }