grid_map.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. // update transform for NavigationServer regions and navigation debugmesh instances
  362. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  363. if (bake_navigation) {
  364. if (E.value.region.is_valid()) {
  365. NavigationServer3D::get_singleton()->region_set_transform(E.value.region, get_global_transform() * E.value.xform);
  366. }
  367. if (E.value.navmesh_debug_instance.is_valid()) {
  368. RS::get_singleton()->instance_set_transform(E.value.navmesh_debug_instance, get_global_transform() * E.value.xform);
  369. }
  370. }
  371. }
  372. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  373. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  374. }
  375. }
  376. bool GridMap::_octant_update(const OctantKey &p_key) {
  377. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  378. Octant &g = *octant_map[p_key];
  379. if (!g.dirty) {
  380. return false;
  381. }
  382. //erase body shapes
  383. PhysicsServer3D::get_singleton()->body_clear_shapes(g.static_body);
  384. //erase body shapes debug
  385. if (g.collision_debug.is_valid()) {
  386. RS::get_singleton()->mesh_clear(g.collision_debug);
  387. }
  388. //erase navigation
  389. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  390. NavigationServer3D::get_singleton()->free(E.value.region);
  391. if (E.value.navmesh_debug_instance.is_valid()) {
  392. RS::get_singleton()->free(E.value.navmesh_debug_instance);
  393. }
  394. }
  395. g.navmesh_ids.clear();
  396. //erase multimeshes
  397. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  398. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  399. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  400. }
  401. g.multimesh_instances.clear();
  402. if (g.cells.size() == 0) {
  403. //octant no longer needed
  404. _octant_clean_up(p_key);
  405. return true;
  406. }
  407. Vector<Vector3> col_debug;
  408. /*
  409. * foreach item in this octant,
  410. * set item's multimesh's instance count to number of cells which have this item
  411. * and set said multimesh bounding box to one containing all cells which have this item
  412. */
  413. HashMap<int, List<Pair<Transform3D, IndexKey>>> multimesh_items;
  414. for (const IndexKey &E : g.cells) {
  415. ERR_CONTINUE(!cell_map.has(E));
  416. const Cell &c = cell_map[E];
  417. if (!mesh_library.is_valid() || !mesh_library->has_item(c.item)) {
  418. continue;
  419. }
  420. Vector3 cellpos = Vector3(E.x, E.y, E.z);
  421. Vector3 ofs = _get_offset();
  422. Transform3D xform;
  423. xform.basis.set_orthogonal_index(c.rot);
  424. xform.set_origin(cellpos * cell_size + ofs);
  425. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  426. if (baked_meshes.size() == 0) {
  427. if (mesh_library->get_item_mesh(c.item).is_valid()) {
  428. if (!multimesh_items.has(c.item)) {
  429. multimesh_items[c.item] = List<Pair<Transform3D, IndexKey>>();
  430. }
  431. Pair<Transform3D, IndexKey> p;
  432. p.first = xform * mesh_library->get_item_mesh_transform(c.item);
  433. p.second = E;
  434. multimesh_items[c.item].push_back(p);
  435. }
  436. }
  437. Vector<MeshLibrary::ShapeData> shapes = mesh_library->get_item_shapes(c.item);
  438. // add the item's shape at given xform to octant's static_body
  439. for (int i = 0; i < shapes.size(); i++) {
  440. // add the item's shape
  441. if (!shapes[i].shape.is_valid()) {
  442. continue;
  443. }
  444. PhysicsServer3D::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  445. if (g.collision_debug.is_valid()) {
  446. shapes.write[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  447. }
  448. }
  449. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  450. Ref<NavigationMesh> navmesh = mesh_library->get_item_navmesh(c.item);
  451. if (navmesh.is_valid()) {
  452. Octant::NavMesh nm;
  453. nm.xform = xform * mesh_library->get_item_navmesh_transform(c.item);
  454. if (bake_navigation) {
  455. RID region = NavigationServer3D::get_singleton()->region_create();
  456. NavigationServer3D::get_singleton()->region_set_layers(region, navigation_layers);
  457. NavigationServer3D::get_singleton()->region_set_navmesh(region, navmesh);
  458. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * nm.xform);
  459. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  460. nm.region = region;
  461. // add navigation debugmesh visual instances if debug is enabled
  462. SceneTree *st = SceneTree::get_singleton();
  463. if (st && st->is_debugging_navigation_hint()) {
  464. if (!nm.navmesh_debug_instance.is_valid()) {
  465. RID navmesh_debug_rid = navmesh->get_debug_mesh()->get_rid();
  466. nm.navmesh_debug_instance = RS::get_singleton()->instance_create();
  467. RS::get_singleton()->instance_set_base(nm.navmesh_debug_instance, navmesh_debug_rid);
  468. RS::get_singleton()->mesh_surface_set_material(navmesh_debug_rid, 0, st->get_debug_navigation_material()->get_rid());
  469. }
  470. if (is_inside_tree()) {
  471. RS::get_singleton()->instance_set_scenario(nm.navmesh_debug_instance, get_world_3d()->get_scenario());
  472. RS::get_singleton()->instance_set_transform(nm.navmesh_debug_instance, get_global_transform() * nm.xform);
  473. }
  474. }
  475. }
  476. g.navmesh_ids[E] = nm;
  477. }
  478. }
  479. //update multimeshes, only if not baked
  480. if (baked_meshes.size() == 0) {
  481. for (const KeyValue<int, List<Pair<Transform3D, IndexKey>>> &E : multimesh_items) {
  482. Octant::MultimeshInstance mmi;
  483. RID mm = RS::get_singleton()->multimesh_create();
  484. RS::get_singleton()->multimesh_allocate_data(mm, E.value.size(), RS::MULTIMESH_TRANSFORM_3D);
  485. RS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E.key)->get_rid());
  486. int idx = 0;
  487. for (const Pair<Transform3D, IndexKey> &F : E.value) {
  488. RS::get_singleton()->multimesh_instance_set_transform(mm, idx, F.first);
  489. #ifdef TOOLS_ENABLED
  490. Octant::MultimeshInstance::Item it;
  491. it.index = idx;
  492. it.transform = F.first;
  493. it.key = F.second;
  494. mmi.items.push_back(it);
  495. #endif
  496. idx++;
  497. }
  498. RID instance = RS::get_singleton()->instance_create();
  499. RS::get_singleton()->instance_set_base(instance, mm);
  500. if (is_inside_tree()) {
  501. RS::get_singleton()->instance_set_scenario(instance, get_world_3d()->get_scenario());
  502. RS::get_singleton()->instance_set_transform(instance, get_global_transform());
  503. }
  504. mmi.multimesh = mm;
  505. mmi.instance = instance;
  506. g.multimesh_instances.push_back(mmi);
  507. }
  508. }
  509. if (col_debug.size()) {
  510. Array arr;
  511. arr.resize(RS::ARRAY_MAX);
  512. arr[RS::ARRAY_VERTEX] = col_debug;
  513. RS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, RS::PRIMITIVE_LINES, arr);
  514. SceneTree *st = SceneTree::get_singleton();
  515. if (st) {
  516. RS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  517. }
  518. }
  519. g.dirty = false;
  520. return false;
  521. }
  522. void GridMap::_reset_physic_bodies_collision_filters() {
  523. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  524. PhysicsServer3D::get_singleton()->body_set_collision_layer(E.value->static_body, collision_layer);
  525. PhysicsServer3D::get_singleton()->body_set_collision_mask(E.value->static_body, collision_mask);
  526. }
  527. }
  528. void GridMap::_octant_enter_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, get_world_3d()->get_space());
  533. if (g.collision_debug_instance.is_valid()) {
  534. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world_3d()->get_scenario());
  535. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  536. }
  537. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  538. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world_3d()->get_scenario());
  539. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  540. }
  541. if (bake_navigation && mesh_library.is_valid()) {
  542. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  543. if (cell_map.has(F.key) && F.value.region.is_valid() == false) {
  544. Ref<NavigationMesh> nm = mesh_library->get_item_navmesh(cell_map[F.key].item);
  545. if (nm.is_valid()) {
  546. RID region = NavigationServer3D::get_singleton()->region_create();
  547. NavigationServer3D::get_singleton()->region_set_layers(region, navigation_layers);
  548. NavigationServer3D::get_singleton()->region_set_navmesh(region, nm);
  549. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform);
  550. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  551. F.value.region = region;
  552. }
  553. }
  554. }
  555. }
  556. }
  557. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  558. ERR_FAIL_COND(!octant_map.has(p_key));
  559. Octant &g = *octant_map[p_key];
  560. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  561. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID());
  562. if (g.collision_debug_instance.is_valid()) {
  563. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  564. }
  565. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  566. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  567. }
  568. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  569. if (F.value.region.is_valid()) {
  570. NavigationServer3D::get_singleton()->free(F.value.region);
  571. F.value.region = RID();
  572. }
  573. if (F.value.navmesh_debug_instance.is_valid()) {
  574. RS::get_singleton()->free(F.value.navmesh_debug_instance);
  575. F.value.navmesh_debug_instance = RID();
  576. }
  577. }
  578. }
  579. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  580. ERR_FAIL_COND(!octant_map.has(p_key));
  581. Octant &g = *octant_map[p_key];
  582. if (g.collision_debug.is_valid()) {
  583. RS::get_singleton()->free(g.collision_debug);
  584. }
  585. if (g.collision_debug_instance.is_valid()) {
  586. RS::get_singleton()->free(g.collision_debug_instance);
  587. }
  588. PhysicsServer3D::get_singleton()->free(g.static_body);
  589. // Erase navigation
  590. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  591. if (E.value.region.is_valid()) {
  592. NavigationServer3D::get_singleton()->free(E.value.region);
  593. }
  594. if (E.value.navmesh_debug_instance.is_valid()) {
  595. RS::get_singleton()->free(E.value.navmesh_debug_instance);
  596. }
  597. }
  598. g.navmesh_ids.clear();
  599. //erase multimeshes
  600. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  601. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  602. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  603. }
  604. g.multimesh_instances.clear();
  605. }
  606. void GridMap::_notification(int p_what) {
  607. switch (p_what) {
  608. case NOTIFICATION_ENTER_WORLD: {
  609. last_transform = get_global_transform();
  610. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  611. _octant_enter_world(E.key);
  612. }
  613. for (int i = 0; i < baked_meshes.size(); i++) {
  614. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world_3d()->get_scenario());
  615. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  616. }
  617. } break;
  618. case NOTIFICATION_TRANSFORM_CHANGED: {
  619. Transform3D new_xform = get_global_transform();
  620. if (new_xform == last_transform) {
  621. break;
  622. }
  623. //update run
  624. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  625. _octant_transform(E.key);
  626. }
  627. last_transform = new_xform;
  628. for (int i = 0; i < baked_meshes.size(); i++) {
  629. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  630. }
  631. } break;
  632. case NOTIFICATION_EXIT_WORLD: {
  633. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  634. _octant_exit_world(E.key);
  635. }
  636. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  637. //_update_octants_callback();
  638. //_update_area_instances();
  639. for (int i = 0; i < baked_meshes.size(); i++) {
  640. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID());
  641. }
  642. } break;
  643. case NOTIFICATION_VISIBILITY_CHANGED: {
  644. _update_visibility();
  645. } break;
  646. }
  647. }
  648. void GridMap::_update_visibility() {
  649. if (!is_inside_tree()) {
  650. return;
  651. }
  652. for (KeyValue<OctantKey, Octant *> &e : octant_map) {
  653. Octant *octant = e.value;
  654. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  655. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  656. RS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree());
  657. }
  658. }
  659. for (int i = 0; i < baked_meshes.size(); i++) {
  660. RS::get_singleton()->instance_set_visible(baked_meshes[i].instance, is_visible_in_tree());
  661. }
  662. }
  663. void GridMap::_queue_octants_dirty() {
  664. if (awaiting_update) {
  665. return;
  666. }
  667. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  668. awaiting_update = true;
  669. }
  670. void GridMap::_recreate_octant_data() {
  671. recreating_octants = true;
  672. HashMap<IndexKey, Cell, IndexKey> cell_copy = cell_map;
  673. _clear_internal();
  674. for (const KeyValue<IndexKey, Cell> &E : cell_copy) {
  675. set_cell_item(Vector3i(E.key), E.value.item, E.value.rot);
  676. }
  677. recreating_octants = false;
  678. }
  679. void GridMap::_clear_internal() {
  680. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  681. if (is_inside_world()) {
  682. _octant_exit_world(E.key);
  683. }
  684. _octant_clean_up(E.key);
  685. memdelete(E.value);
  686. }
  687. octant_map.clear();
  688. cell_map.clear();
  689. }
  690. void GridMap::clear() {
  691. _clear_internal();
  692. clear_baked_meshes();
  693. }
  694. void GridMap::resource_changed(const Ref<Resource> &p_res) {
  695. _recreate_octant_data();
  696. }
  697. void GridMap::_update_octants_callback() {
  698. if (!awaiting_update) {
  699. return;
  700. }
  701. List<OctantKey> to_delete;
  702. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  703. if (_octant_update(E.key)) {
  704. to_delete.push_back(E.key);
  705. }
  706. }
  707. while (to_delete.front()) {
  708. memdelete(octant_map[to_delete.front()->get()]);
  709. octant_map.erase(to_delete.front()->get());
  710. to_delete.pop_front();
  711. }
  712. _update_visibility();
  713. awaiting_update = false;
  714. }
  715. void GridMap::_bind_methods() {
  716. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  717. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  718. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  719. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  720. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &GridMap::set_collision_mask_value);
  721. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &GridMap::get_collision_mask_value);
  722. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &GridMap::set_collision_layer_value);
  723. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &GridMap::get_collision_layer_value);
  724. ClassDB::bind_method(D_METHOD("set_physics_material", "material"), &GridMap::set_physics_material);
  725. ClassDB::bind_method(D_METHOD("get_physics_material"), &GridMap::get_physics_material);
  726. ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &GridMap::set_bake_navigation);
  727. ClassDB::bind_method(D_METHOD("is_baking_navigation"), &GridMap::is_baking_navigation);
  728. ClassDB::bind_method(D_METHOD("set_navigation_layers", "layers"), &GridMap::set_navigation_layers);
  729. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &GridMap::get_navigation_layers);
  730. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  731. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  732. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  733. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  734. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  735. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  736. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  737. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  738. ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  739. ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item);
  740. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);
  741. ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &GridMap::world_to_map);
  742. ClassDB::bind_method(D_METHOD("map_to_world", "map_position"), &GridMap::map_to_world);
  743. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  744. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  745. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  746. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  747. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  748. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  749. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  750. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  751. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  752. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  753. ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item);
  754. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  755. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  756. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  757. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  758. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  759. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  760. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material");
  761. ADD_GROUP("Cell", "cell_");
  762. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size", PROPERTY_HINT_NONE, "suffix:m"), "set_cell_size", "get_cell_size");
  763. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  764. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  765. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  766. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  767. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_scale"), "set_cell_scale", "get_cell_scale");
  768. ADD_GROUP("Collision", "collision_");
  769. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  770. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  771. ADD_GROUP("Navigation", "");
  772. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_navigation"), "set_bake_navigation", "is_baking_navigation");
  773. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  774. BIND_CONSTANT(INVALID_CELL_ITEM);
  775. ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size")));
  776. }
  777. void GridMap::set_cell_scale(float p_scale) {
  778. cell_scale = p_scale;
  779. _recreate_octant_data();
  780. }
  781. float GridMap::get_cell_scale() const {
  782. return cell_scale;
  783. }
  784. Array GridMap::get_used_cells() const {
  785. Array a;
  786. a.resize(cell_map.size());
  787. int i = 0;
  788. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  789. Vector3 p(E.key.x, E.key.y, E.key.z);
  790. a[i++] = p;
  791. }
  792. return a;
  793. }
  794. Array GridMap::get_used_cells_by_item(int p_item) const {
  795. Array a;
  796. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  797. if (E.value.item == p_item) {
  798. Vector3 p(E.key.x, E.key.y, E.key.z);
  799. a.push_back(p);
  800. }
  801. }
  802. return a;
  803. }
  804. Array GridMap::get_meshes() const {
  805. if (mesh_library.is_null()) {
  806. return Array();
  807. }
  808. Vector3 ofs = _get_offset();
  809. Array meshes;
  810. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  811. int id = E.value.item;
  812. if (!mesh_library->has_item(id)) {
  813. continue;
  814. }
  815. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  816. if (mesh.is_null()) {
  817. continue;
  818. }
  819. IndexKey ik = E.key;
  820. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  821. Transform3D xform;
  822. xform.basis.set_orthogonal_index(E.value.rot);
  823. xform.set_origin(cellpos * cell_size + ofs);
  824. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  825. meshes.push_back(xform * mesh_library->get_item_mesh_transform(id));
  826. meshes.push_back(mesh);
  827. }
  828. return meshes;
  829. }
  830. Vector3 GridMap::_get_offset() const {
  831. return Vector3(
  832. cell_size.x * 0.5 * int(center_x),
  833. cell_size.y * 0.5 * int(center_y),
  834. cell_size.z * 0.5 * int(center_z));
  835. }
  836. void GridMap::clear_baked_meshes() {
  837. for (int i = 0; i < baked_meshes.size(); i++) {
  838. RS::get_singleton()->free(baked_meshes[i].instance);
  839. }
  840. baked_meshes.clear();
  841. _recreate_octant_data();
  842. }
  843. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  844. if (!mesh_library.is_valid()) {
  845. return;
  846. }
  847. //generate
  848. HashMap<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>, OctantKey> surface_map;
  849. for (KeyValue<IndexKey, Cell> &E : cell_map) {
  850. IndexKey key = E.key;
  851. int item = E.value.item;
  852. if (!mesh_library->has_item(item)) {
  853. continue;
  854. }
  855. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  856. if (!mesh.is_valid()) {
  857. continue;
  858. }
  859. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  860. Vector3 ofs = _get_offset();
  861. Transform3D xform;
  862. xform.basis.set_orthogonal_index(E.value.rot);
  863. xform.set_origin(cellpos * cell_size + ofs);
  864. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  865. OctantKey ok;
  866. ok.x = key.x / octant_size;
  867. ok.y = key.y / octant_size;
  868. ok.z = key.z / octant_size;
  869. if (!surface_map.has(ok)) {
  870. surface_map[ok] = HashMap<Ref<Material>, Ref<SurfaceTool>>();
  871. }
  872. HashMap<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok];
  873. for (int i = 0; i < mesh->get_surface_count(); i++) {
  874. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  875. continue;
  876. }
  877. Ref<Material> surf_mat = mesh->surface_get_material(i);
  878. if (!mat_map.has(surf_mat)) {
  879. Ref<SurfaceTool> st;
  880. st.instantiate();
  881. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  882. st->set_material(surf_mat);
  883. mat_map[surf_mat] = st;
  884. }
  885. mat_map[surf_mat]->append_from(mesh, i, xform);
  886. }
  887. }
  888. for (KeyValue<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>> &E : surface_map) {
  889. Ref<ArrayMesh> mesh;
  890. mesh.instantiate();
  891. for (KeyValue<Ref<Material>, Ref<SurfaceTool>> &F : E.value) {
  892. F.value->commit(mesh);
  893. }
  894. BakedMesh bm;
  895. bm.mesh = mesh;
  896. bm.instance = RS::get_singleton()->instance_create();
  897. RS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  898. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  899. if (is_inside_tree()) {
  900. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  901. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  902. }
  903. if (p_gen_lightmap_uv) {
  904. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  905. }
  906. baked_meshes.push_back(bm);
  907. }
  908. _recreate_octant_data();
  909. }
  910. Array GridMap::get_bake_meshes() {
  911. if (!baked_meshes.size()) {
  912. make_baked_meshes(true);
  913. }
  914. Array arr;
  915. for (int i = 0; i < baked_meshes.size(); i++) {
  916. arr.push_back(baked_meshes[i].mesh);
  917. arr.push_back(Transform3D());
  918. }
  919. return arr;
  920. }
  921. RID GridMap::get_bake_mesh_instance(int p_idx) {
  922. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  923. return baked_meshes[p_idx].instance;
  924. }
  925. GridMap::GridMap() {
  926. set_notify_transform(true);
  927. }
  928. GridMap::~GridMap() {
  929. if (!mesh_library.is_null()) {
  930. mesh_library->unregister_owner(this);
  931. }
  932. clear();
  933. }