grid_map.cpp 36 KB

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