grid_map.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /**************************************************************************/
  2. /* grid_map.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "grid_map.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/message_queue.h"
  33. #include "scene/3d/light.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.h"
  40. #include "servers/visual_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. PoolVector<int> cells = d["cells"];
  47. int amount = cells.size();
  48. PoolVector<int>::Read r = cells.read();
  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 = RID_PRIME(VS::get_singleton()->instance_create());
  68. VS::get_singleton()->instance_set_portal_mode(bm.instance, VisualServer::InstancePortalMode::INSTANCE_PORTAL_MODE_GLOBAL);
  69. VS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  70. VS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  71. if (is_inside_tree()) {
  72. VS::get_singleton()->instance_set_scenario(bm.instance, get_world()->get_scenario());
  73. VS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  74. }
  75. baked_meshes.push_back(bm);
  76. }
  77. _recreate_octant_data();
  78. } else {
  79. return false;
  80. }
  81. return true;
  82. }
  83. bool GridMap::_get(const StringName &p_name, Variant &r_ret) const {
  84. String name = p_name;
  85. if (name == "data") {
  86. Dictionary d;
  87. PoolVector<int> cells;
  88. cells.resize(cell_map.size() * 3);
  89. {
  90. PoolVector<int>::Write w = cells.write();
  91. int i = 0;
  92. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next(), i++) {
  93. encode_uint64(E->key().key, (uint8_t *)&w[i * 3]);
  94. encode_uint32(E->get().cell, (uint8_t *)&w[i * 3 + 2]);
  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_mask_bit(int p_bit, bool p_value) {
  132. ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
  133. uint32_t mask = get_collision_mask();
  134. if (p_value) {
  135. mask |= 1 << p_bit;
  136. } else {
  137. mask &= ~(1 << p_bit);
  138. }
  139. set_collision_mask(mask);
  140. }
  141. bool GridMap::get_collision_mask_bit(int p_bit) const {
  142. ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
  143. return get_collision_mask() & (1 << p_bit);
  144. }
  145. void GridMap::set_collision_layer_bit(int p_bit, bool p_value) {
  146. ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
  147. uint32_t layer = get_collision_layer();
  148. if (p_value) {
  149. layer |= 1 << p_bit;
  150. } else {
  151. layer &= ~(1 << p_bit);
  152. }
  153. set_collision_layer(layer);
  154. }
  155. bool GridMap::get_collision_layer_bit(int p_bit) const {
  156. ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision layer bit must be between 0 and 31 inclusive.");
  157. return get_collision_layer() & (1 << p_bit);
  158. }
  159. void GridMap::set_physics_material(Ref<PhysicsMaterial> p_material) {
  160. physics_material = p_material;
  161. _recreate_octant_data();
  162. }
  163. Ref<PhysicsMaterial> GridMap::get_physics_material() const {
  164. return physics_material;
  165. }
  166. Array GridMap::get_collision_shapes() const {
  167. Array shapes;
  168. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  169. Octant *g = E->get();
  170. RID body = g->static_body;
  171. Transform body_xform = PhysicsServer::get_singleton()->body_get_state(body, PhysicsServer::BODY_STATE_TRANSFORM);
  172. int nshapes = PhysicsServer::get_singleton()->body_get_shape_count(body);
  173. for (int i = 0; i < nshapes; i++) {
  174. RID shape = PhysicsServer::get_singleton()->body_get_shape(body, i);
  175. Transform xform = PhysicsServer::get_singleton()->body_get_shape_transform(body, i);
  176. shapes.push_back(body_xform * xform);
  177. shapes.push_back(shape);
  178. }
  179. }
  180. return shapes;
  181. }
  182. void GridMap::set_bake_navigation(bool p_bake_navigation) {
  183. bake_navigation = p_bake_navigation;
  184. _recreate_octant_data();
  185. }
  186. bool GridMap::is_baking_navigation() {
  187. return bake_navigation;
  188. }
  189. void GridMap::set_navigation_layers(uint32_t p_navigation_layers) {
  190. navigation_layers = p_navigation_layers;
  191. _recreate_octant_data();
  192. }
  193. uint32_t GridMap::get_navigation_layers() {
  194. return navigation_layers;
  195. }
  196. void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) {
  197. if (!mesh_library.is_null()) {
  198. mesh_library->unregister_owner(this);
  199. }
  200. mesh_library = p_mesh_library;
  201. if (!mesh_library.is_null()) {
  202. mesh_library->register_owner(this);
  203. }
  204. _recreate_octant_data();
  205. _change_notify("mesh_library");
  206. }
  207. Ref<MeshLibrary> GridMap::get_mesh_library() const {
  208. return mesh_library;
  209. }
  210. void GridMap::set_use_in_baked_light(bool p_use_baked_light) {
  211. use_in_baked_light = p_use_baked_light;
  212. }
  213. bool GridMap::get_use_in_baked_light() const {
  214. return use_in_baked_light;
  215. }
  216. void GridMap::set_cell_size(const Vector3 &p_size) {
  217. ERR_FAIL_COND(p_size.x < 0.001 || p_size.y < 0.001 || p_size.z < 0.001);
  218. cell_size = p_size;
  219. _recreate_octant_data();
  220. emit_signal("cell_size_changed", cell_size);
  221. }
  222. Vector3 GridMap::get_cell_size() const {
  223. return cell_size;
  224. }
  225. void GridMap::set_octant_size(int p_size) {
  226. ERR_FAIL_COND(p_size == 0);
  227. octant_size = p_size;
  228. _recreate_octant_data();
  229. }
  230. int GridMap::get_octant_size() const {
  231. return octant_size;
  232. }
  233. void GridMap::set_center_x(bool p_enable) {
  234. center_x = p_enable;
  235. _recreate_octant_data();
  236. }
  237. bool GridMap::get_center_x() const {
  238. return center_x;
  239. }
  240. void GridMap::set_center_y(bool p_enable) {
  241. center_y = p_enable;
  242. _recreate_octant_data();
  243. }
  244. bool GridMap::get_center_y() const {
  245. return center_y;
  246. }
  247. void GridMap::set_center_z(bool p_enable) {
  248. center_z = p_enable;
  249. _recreate_octant_data();
  250. }
  251. bool GridMap::get_center_z() const {
  252. return center_z;
  253. }
  254. void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
  255. if (baked_meshes.size() && !recreating_octants) {
  256. //if you set a cell item, baked meshes go good bye
  257. clear_baked_meshes();
  258. _recreate_octant_data();
  259. }
  260. ERR_FAIL_INDEX(ABS(p_x), 1 << 20);
  261. ERR_FAIL_INDEX(ABS(p_y), 1 << 20);
  262. ERR_FAIL_INDEX(ABS(p_z), 1 << 20);
  263. IndexKey key;
  264. key.x = p_x;
  265. key.y = p_y;
  266. key.z = p_z;
  267. OctantKey ok;
  268. ok.x = p_x / octant_size;
  269. ok.y = p_y / octant_size;
  270. ok.z = p_z / octant_size;
  271. if (p_item < 0) {
  272. //erase
  273. if (cell_map.has(key)) {
  274. OctantKey octantkey = ok;
  275. ERR_FAIL_COND(!octant_map.has(octantkey));
  276. Octant &g = *octant_map[octantkey];
  277. g.cells.erase(key);
  278. g.dirty = true;
  279. cell_map.erase(key);
  280. _queue_octants_dirty();
  281. }
  282. return;
  283. }
  284. OctantKey octantkey = ok;
  285. if (!octant_map.has(octantkey)) {
  286. //create octant because it does not exist
  287. Octant *g = memnew(Octant);
  288. g->dirty = true;
  289. g->static_body = RID_PRIME(PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC));
  290. PhysicsServer::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
  291. PhysicsServer::get_singleton()->body_set_collision_layer(g->static_body, collision_layer);
  292. PhysicsServer::get_singleton()->body_set_collision_mask(g->static_body, collision_mask);
  293. if (physics_material.is_valid()) {
  294. PhysicsServer::get_singleton()->body_set_param(g->static_body, PhysicsServer::BODY_PARAM_FRICTION, physics_material->get_friction());
  295. PhysicsServer::get_singleton()->body_set_param(g->static_body, PhysicsServer::BODY_PARAM_BOUNCE, physics_material->get_bounce());
  296. }
  297. SceneTree *st = SceneTree::get_singleton();
  298. if (st && st->is_debugging_collisions_hint()) {
  299. g->collision_debug = RID_PRIME(VisualServer::get_singleton()->mesh_create());
  300. g->collision_debug_instance = RID_PRIME(VisualServer::get_singleton()->instance_create());
  301. VS::get_singleton()->instance_set_portal_mode(g->collision_debug_instance, VisualServer::InstancePortalMode::INSTANCE_PORTAL_MODE_GLOBAL);
  302. VisualServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  303. }
  304. octant_map[octantkey] = g;
  305. if (is_inside_world()) {
  306. _octant_enter_world(octantkey);
  307. _octant_transform(octantkey);
  308. }
  309. }
  310. Octant &g = *octant_map[octantkey];
  311. g.cells.insert(key);
  312. g.dirty = true;
  313. _queue_octants_dirty();
  314. Cell c;
  315. c.item = p_item;
  316. c.rot = p_rot;
  317. cell_map[key] = c;
  318. }
  319. int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
  320. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, INVALID_CELL_ITEM);
  321. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, INVALID_CELL_ITEM);
  322. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, INVALID_CELL_ITEM);
  323. IndexKey key;
  324. key.x = p_x;
  325. key.y = p_y;
  326. key.z = p_z;
  327. if (!cell_map.has(key)) {
  328. return INVALID_CELL_ITEM;
  329. }
  330. return cell_map[key].item;
  331. }
  332. int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const {
  333. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, -1);
  334. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, -1);
  335. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, -1);
  336. IndexKey key;
  337. key.x = p_x;
  338. key.y = p_y;
  339. key.z = p_z;
  340. if (!cell_map.has(key)) {
  341. return -1;
  342. }
  343. return cell_map[key].rot;
  344. }
  345. Vector3 GridMap::world_to_map(const Vector3 &p_world_pos) const {
  346. Vector3 map_pos = p_world_pos / cell_size;
  347. map_pos.x = floor(map_pos.x);
  348. map_pos.y = floor(map_pos.y);
  349. map_pos.z = floor(map_pos.z);
  350. return map_pos;
  351. }
  352. Vector3 GridMap::map_to_world(int p_x, int p_y, int p_z) const {
  353. Vector3 offset = _get_offset();
  354. Vector3 world_pos(
  355. p_x * cell_size.x + offset.x,
  356. p_y * cell_size.y + offset.y,
  357. p_z * cell_size.z + offset.z);
  358. return world_pos;
  359. }
  360. void GridMap::_octant_transform(const OctantKey &p_key) {
  361. ERR_FAIL_COND(!octant_map.has(p_key));
  362. Octant &g = *octant_map[p_key];
  363. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  364. if (g.collision_debug_instance.is_valid()) {
  365. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  366. }
  367. // update transform for NavigationServer regions and navigation debugmesh instances
  368. if (bake_navigation) {
  369. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  370. if (E->get().region.is_valid()) {
  371. NavigationServer::get_singleton()->region_set_transform(E->get().region, get_global_transform() * E->get().xform);
  372. }
  373. if (E->get().navmesh_debug_instance.is_valid()) {
  374. VS::get_singleton()->instance_set_transform(E->get().navmesh_debug_instance, get_global_transform() * E->get().xform);
  375. }
  376. }
  377. }
  378. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  379. VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  380. }
  381. }
  382. bool GridMap::_octant_update(const OctantKey &p_key) {
  383. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  384. Octant &g = *octant_map[p_key];
  385. if (!g.dirty) {
  386. return false;
  387. }
  388. //erase body shapes
  389. PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
  390. //erase body shapes debug
  391. if (g.collision_debug.is_valid()) {
  392. VS::get_singleton()->mesh_clear(g.collision_debug);
  393. }
  394. //erase navigation
  395. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  396. if (E->get().region.is_valid()) {
  397. NavigationServer::get_singleton()->free(E->get().region);
  398. E->get().region = RID();
  399. }
  400. if (E->get().navmesh_debug_instance.is_valid()) {
  401. VS::get_singleton()->free(E->get().navmesh_debug_instance);
  402. }
  403. }
  404. g.navmesh_ids.clear();
  405. //erase multimeshes
  406. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  407. if (g.multimesh_instances[i].instance.is_valid()) {
  408. VS::get_singleton()->free(g.multimesh_instances[i].instance);
  409. }
  410. if (g.multimesh_instances[i].multimesh.is_valid()) {
  411. VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  412. }
  413. }
  414. g.multimesh_instances.clear();
  415. if (g.cells.size() == 0) {
  416. //octant no longer needed
  417. _octant_clean_up(p_key);
  418. return true;
  419. }
  420. PoolVector<Vector3> col_debug;
  421. /*
  422. * foreach item in this octant,
  423. * set item's multimesh's instance count to number of cells which have this item
  424. * and set said multimesh bounding box to one containing all cells which have this item
  425. */
  426. Map<int, List<Pair<Transform, IndexKey>>> multimesh_items;
  427. for (Set<IndexKey>::Element *E = g.cells.front(); E; E = E->next()) {
  428. ERR_CONTINUE(!cell_map.has(E->get()));
  429. const Cell &c = cell_map[E->get()];
  430. if (!mesh_library.is_valid() || !mesh_library->has_item(c.item)) {
  431. continue;
  432. }
  433. Vector3 cellpos = Vector3(E->get().x, E->get().y, E->get().z);
  434. Vector3 ofs = _get_offset();
  435. Transform xform;
  436. xform.basis.set_orthogonal_index(c.rot);
  437. xform.set_origin(cellpos * cell_size + ofs);
  438. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  439. if (baked_meshes.size() == 0) {
  440. if (mesh_library->get_item_mesh(c.item).is_valid()) {
  441. if (!multimesh_items.has(c.item)) {
  442. multimesh_items[c.item] = List<Pair<Transform, IndexKey>>();
  443. }
  444. Pair<Transform, IndexKey> p;
  445. p.first = xform * mesh_library->get_item_mesh_transform(c.item);
  446. p.second = E->get();
  447. multimesh_items[c.item].push_back(p);
  448. }
  449. }
  450. Vector<MeshLibrary::ShapeData> shapes = mesh_library->get_item_shapes(c.item);
  451. // add the item's shape at given xform to octant's static_body
  452. for (int i = 0; i < shapes.size(); i++) {
  453. // add the item's shape
  454. if (!shapes[i].shape.is_valid()) {
  455. continue;
  456. }
  457. PhysicsServer::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  458. if (g.collision_debug.is_valid()) {
  459. shapes.write[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  460. }
  461. }
  462. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  463. Ref<NavigationMesh> navmesh = mesh_library->get_item_navmesh(c.item);
  464. if (navmesh.is_valid()) {
  465. Octant::NavMesh nm;
  466. nm.xform = xform * mesh_library->get_item_navmesh_transform(c.item);
  467. if (bake_navigation) {
  468. RID region = NavigationServer::get_singleton()->region_create();
  469. NavigationServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  470. NavigationServer::get_singleton()->region_set_navmesh(region, navmesh);
  471. NavigationServer::get_singleton()->region_set_transform(region, get_global_transform() * nm.xform);
  472. if (is_inside_tree()) {
  473. if (navigation) {
  474. NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
  475. } else {
  476. NavigationServer::get_singleton()->region_set_map(region, get_world()->get_navigation_map());
  477. }
  478. }
  479. nm.region = region;
  480. // add navigation debugmesh visual instances if debug is enabled
  481. SceneTree *st = SceneTree::get_singleton();
  482. if (st && st->is_debugging_navigation_hint()) {
  483. if (!nm.navmesh_debug_instance.is_valid()) {
  484. RID navmesh_debug_rid = navmesh->get_debug_mesh()->get_rid();
  485. nm.navmesh_debug_instance = VS::get_singleton()->instance_create();
  486. VS::get_singleton()->instance_set_base(nm.navmesh_debug_instance, navmesh_debug_rid);
  487. VS::get_singleton()->mesh_surface_set_material(navmesh_debug_rid, 0, st->get_debug_navigation_material()->get_rid());
  488. }
  489. if (is_inside_tree()) {
  490. VS::get_singleton()->instance_set_scenario(nm.navmesh_debug_instance, get_world()->get_scenario());
  491. VS::get_singleton()->instance_set_transform(nm.navmesh_debug_instance, get_global_transform() * nm.xform);
  492. }
  493. }
  494. }
  495. g.navmesh_ids[E->get()] = nm;
  496. }
  497. }
  498. //update multimeshes, only if not baked
  499. if (baked_meshes.size() == 0) {
  500. for (Map<int, List<Pair<Transform, IndexKey>>>::Element *E = multimesh_items.front(); E; E = E->next()) {
  501. Octant::MultimeshInstance mmi;
  502. RID mm = RID_PRIME(VS::get_singleton()->multimesh_create());
  503. VS::get_singleton()->multimesh_allocate(mm, E->get().size(), VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_NONE);
  504. VS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E->key())->get_rid());
  505. int idx = 0;
  506. for (List<Pair<Transform, IndexKey>>::Element *F = E->get().front(); F; F = F->next()) {
  507. VS::get_singleton()->multimesh_instance_set_transform(mm, idx, F->get().first);
  508. #ifdef TOOLS_ENABLED
  509. Octant::MultimeshInstance::Item it;
  510. it.index = idx;
  511. it.transform = F->get().first;
  512. it.key = F->get().second;
  513. mmi.items.push_back(it);
  514. #endif
  515. idx++;
  516. }
  517. RID instance = RID_PRIME(VS::get_singleton()->instance_create());
  518. VS::get_singleton()->instance_set_base(instance, mm);
  519. VS::get_singleton()->instance_set_portal_mode(instance, VisualServer::InstancePortalMode::INSTANCE_PORTAL_MODE_GLOBAL);
  520. if (is_inside_tree()) {
  521. VS::get_singleton()->instance_set_scenario(instance, get_world()->get_scenario());
  522. VS::get_singleton()->instance_set_transform(instance, get_global_transform());
  523. }
  524. mmi.multimesh = mm;
  525. mmi.instance = instance;
  526. g.multimesh_instances.push_back(mmi);
  527. }
  528. }
  529. if (col_debug.size()) {
  530. Array arr;
  531. arr.resize(VS::ARRAY_MAX);
  532. arr[VS::ARRAY_VERTEX] = col_debug;
  533. VS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, VS::PRIMITIVE_LINES, arr);
  534. SceneTree *st = SceneTree::get_singleton();
  535. if (st) {
  536. VS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  537. }
  538. }
  539. g.dirty = false;
  540. return false;
  541. }
  542. void GridMap::_reset_physic_bodies_collision_filters() {
  543. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  544. PhysicsServer::get_singleton()->body_set_collision_layer(E->get()->static_body, collision_layer);
  545. PhysicsServer::get_singleton()->body_set_collision_mask(E->get()->static_body, collision_mask);
  546. }
  547. }
  548. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  549. ERR_FAIL_COND(!octant_map.has(p_key));
  550. Octant &g = *octant_map[p_key];
  551. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  552. PhysicsServer::get_singleton()->body_set_space(g.static_body, get_world()->get_space());
  553. if (g.collision_debug_instance.is_valid()) {
  554. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world()->get_scenario());
  555. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  556. }
  557. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  558. VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world()->get_scenario());
  559. VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  560. }
  561. if (bake_navigation && mesh_library.is_valid()) {
  562. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  563. if (cell_map.has(E->key()) && E->get().region.is_valid() == false) {
  564. Ref<NavigationMesh> nm = mesh_library->get_item_navmesh(cell_map[E->key()].item);
  565. if (nm.is_valid()) {
  566. RID region = NavigationServer::get_singleton()->region_create();
  567. NavigationServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  568. NavigationServer::get_singleton()->region_set_navmesh(region, nm);
  569. NavigationServer::get_singleton()->region_set_transform(region, get_global_transform() * E->get().xform);
  570. if (navigation) {
  571. NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
  572. } else {
  573. NavigationServer::get_singleton()->region_set_map(region, get_world()->get_navigation_map());
  574. }
  575. E->get().region = region;
  576. }
  577. }
  578. }
  579. }
  580. }
  581. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  582. ERR_FAIL_COND(!octant_map.has(p_key));
  583. Octant &g = *octant_map[p_key];
  584. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  585. PhysicsServer::get_singleton()->body_set_space(g.static_body, RID());
  586. if (g.collision_debug_instance.is_valid()) {
  587. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  588. }
  589. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  590. VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  591. }
  592. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  593. if (E->get().region.is_valid()) {
  594. NavigationServer::get_singleton()->free(E->get().region);
  595. E->get().region = RID();
  596. }
  597. if (E->get().navmesh_debug_instance.is_valid()) {
  598. VS::get_singleton()->free(E->get().navmesh_debug_instance);
  599. E->get().navmesh_debug_instance = RID();
  600. }
  601. }
  602. }
  603. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  604. ERR_FAIL_COND(!octant_map.has(p_key));
  605. Octant &g = *octant_map[p_key];
  606. if (g.collision_debug.is_valid()) {
  607. VS::get_singleton()->free(g.collision_debug);
  608. g.collision_debug = RID();
  609. }
  610. if (g.collision_debug_instance.is_valid()) {
  611. VS::get_singleton()->free(g.collision_debug_instance);
  612. g.collision_debug_instance = RID();
  613. }
  614. if (g.static_body.is_valid()) {
  615. PhysicsServer::get_singleton()->free(g.static_body);
  616. g.static_body = RID();
  617. }
  618. // Erase navigation
  619. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  620. if (E->get().region.is_valid()) {
  621. NavigationServer::get_singleton()->free(E->get().region);
  622. }
  623. if (E->get().navmesh_debug_instance.is_valid()) {
  624. VS::get_singleton()->free(E->get().navmesh_debug_instance);
  625. }
  626. }
  627. g.navmesh_ids.clear();
  628. //erase multimeshes
  629. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  630. if (g.multimesh_instances[i].instance.is_valid()) {
  631. VS::get_singleton()->free(g.multimesh_instances[i].instance);
  632. }
  633. if (g.multimesh_instances[i].multimesh.is_valid()) {
  634. VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  635. }
  636. }
  637. g.multimesh_instances.clear();
  638. }
  639. void GridMap::_notification(int p_what) {
  640. switch (p_what) {
  641. case NOTIFICATION_ENTER_WORLD: {
  642. Spatial *c = this;
  643. while (c) {
  644. navigation = Object::cast_to<Navigation>(c);
  645. if (navigation) {
  646. break;
  647. }
  648. c = Object::cast_to<Spatial>(c->get_parent());
  649. }
  650. last_transform = get_global_transform();
  651. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  652. _octant_enter_world(E->key());
  653. }
  654. for (int i = 0; i < baked_meshes.size(); i++) {
  655. VS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world()->get_scenario());
  656. VS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  657. }
  658. } break;
  659. case NOTIFICATION_TRANSFORM_CHANGED: {
  660. Transform new_xform = get_global_transform();
  661. if (new_xform == last_transform) {
  662. break;
  663. }
  664. //update run
  665. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  666. _octant_transform(E->key());
  667. }
  668. last_transform = new_xform;
  669. for (int i = 0; i < baked_meshes.size(); i++) {
  670. VS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  671. }
  672. } break;
  673. case NOTIFICATION_EXIT_WORLD: {
  674. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  675. _octant_exit_world(E->key());
  676. }
  677. navigation = nullptr;
  678. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  679. //_update_octants_callback();
  680. //_update_area_instances();
  681. for (int i = 0; i < baked_meshes.size(); i++) {
  682. VS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID());
  683. }
  684. } break;
  685. case NOTIFICATION_VISIBILITY_CHANGED: {
  686. _update_visibility();
  687. } break;
  688. }
  689. }
  690. void GridMap::_update_visibility() {
  691. if (!is_inside_tree()) {
  692. return;
  693. }
  694. _change_notify("visible");
  695. for (Map<OctantKey, Octant *>::Element *e = octant_map.front(); e; e = e->next()) {
  696. Octant *octant = e->value();
  697. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  698. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  699. VS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree());
  700. }
  701. }
  702. for (int i = 0; i < baked_meshes.size(); i++) {
  703. VS::get_singleton()->instance_set_visible(baked_meshes[i].instance, is_visible_in_tree());
  704. }
  705. }
  706. void GridMap::_queue_octants_dirty() {
  707. if (awaiting_update) {
  708. return;
  709. }
  710. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  711. awaiting_update = true;
  712. }
  713. void GridMap::_recreate_octant_data() {
  714. recreating_octants = true;
  715. Map<IndexKey, Cell> cell_copy = cell_map;
  716. _clear_internal();
  717. for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
  718. set_cell_item(E->key().x, E->key().y, E->key().z, E->get().item, E->get().rot);
  719. }
  720. recreating_octants = false;
  721. }
  722. void GridMap::_clear_internal() {
  723. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  724. if (is_inside_world()) {
  725. _octant_exit_world(E->key());
  726. }
  727. _octant_clean_up(E->key());
  728. memdelete(E->get());
  729. }
  730. octant_map.clear();
  731. cell_map.clear();
  732. }
  733. void GridMap::clear() {
  734. _clear_internal();
  735. clear_baked_meshes();
  736. }
  737. void GridMap::resource_changed(const RES &p_res) {
  738. _recreate_octant_data();
  739. }
  740. void GridMap::_update_octants_callback() {
  741. if (!awaiting_update) {
  742. return;
  743. }
  744. List<OctantKey> to_delete;
  745. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  746. if (_octant_update(E->key())) {
  747. to_delete.push_back(E->key());
  748. }
  749. }
  750. while (to_delete.front()) {
  751. memdelete(octant_map[to_delete.front()->get()]);
  752. octant_map.erase(to_delete.front()->get());
  753. to_delete.pop_front();
  754. }
  755. _update_visibility();
  756. awaiting_update = false;
  757. }
  758. void GridMap::_bind_methods() {
  759. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  760. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  761. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  762. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  763. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &GridMap::set_collision_mask_bit);
  764. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &GridMap::get_collision_mask_bit);
  765. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &GridMap::set_collision_layer_bit);
  766. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &GridMap::get_collision_layer_bit);
  767. ClassDB::bind_method(D_METHOD("set_physics_material", "material"), &GridMap::set_physics_material);
  768. ClassDB::bind_method(D_METHOD("get_physics_material"), &GridMap::get_physics_material);
  769. ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &GridMap::set_bake_navigation);
  770. ClassDB::bind_method(D_METHOD("is_baking_navigation"), &GridMap::is_baking_navigation);
  771. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &GridMap::set_navigation_layers);
  772. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &GridMap::get_navigation_layers);
  773. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  774. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  775. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  776. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  777. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  778. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  779. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  780. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  781. ClassDB::bind_method(D_METHOD("set_cell_item", "x", "y", "z", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  782. ClassDB::bind_method(D_METHOD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item);
  783. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
  784. ClassDB::bind_method(D_METHOD("world_to_map", "pos"), &GridMap::world_to_map);
  785. ClassDB::bind_method(D_METHOD("map_to_world", "x", "y", "z"), &GridMap::map_to_world);
  786. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  787. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  788. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  789. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  790. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  791. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  792. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  793. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  794. ClassDB::bind_method(D_METHOD("set_clip", "enabled", "clipabove", "floor", "axis"), &GridMap::set_clip, DEFVAL(true), DEFVAL(0), DEFVAL(Vector3::AXIS_X));
  795. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  796. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  797. ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item);
  798. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  799. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  800. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  801. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  802. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  803. ClassDB::bind_method(D_METHOD("set_use_in_baked_light", "use_in_baked_light"), &GridMap::set_use_in_baked_light);
  804. ClassDB::bind_method(D_METHOD("get_use_in_baked_light"), &GridMap::get_use_in_baked_light);
  805. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  806. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material");
  807. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_in_baked_light"), "set_use_in_baked_light", "get_use_in_baked_light");
  808. ADD_GROUP("Cell", "cell_");
  809. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size"), "set_cell_size", "get_cell_size");
  810. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  811. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  812. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  813. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  814. ADD_PROPERTY(PropertyInfo(Variant::REAL, "cell_scale"), "set_cell_scale", "get_cell_scale");
  815. ADD_GROUP("Collision", "collision_");
  816. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  817. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  818. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_navigation"), "set_bake_navigation", "is_baking_navigation");
  819. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  820. BIND_CONSTANT(INVALID_CELL_ITEM);
  821. ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size")));
  822. }
  823. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  824. if (!p_enabled && !clip) {
  825. return;
  826. }
  827. if (clip && p_enabled && clip_floor == p_floor && p_clip_above == clip_above && p_axis == clip_axis) {
  828. return;
  829. }
  830. clip = p_enabled;
  831. clip_floor = p_floor;
  832. clip_axis = p_axis;
  833. clip_above = p_clip_above;
  834. //make it all update
  835. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  836. Octant *g = E->get();
  837. g->dirty = true;
  838. }
  839. awaiting_update = true;
  840. _update_octants_callback();
  841. }
  842. void GridMap::set_cell_scale(float p_scale) {
  843. cell_scale = p_scale;
  844. _recreate_octant_data();
  845. }
  846. float GridMap::get_cell_scale() const {
  847. return cell_scale;
  848. }
  849. Array GridMap::get_used_cells() const {
  850. Array a;
  851. a.resize(cell_map.size());
  852. int i = 0;
  853. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  854. Vector3 p(E->key().x, E->key().y, E->key().z);
  855. a[i++] = p;
  856. }
  857. return a;
  858. }
  859. Array GridMap::get_used_cells_by_item(int p_item) const {
  860. Array a;
  861. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  862. if (E->value().item == p_item) {
  863. Vector3 p(E->key().x, E->key().y, E->key().z);
  864. a.push_back(p);
  865. }
  866. }
  867. return a;
  868. }
  869. Array GridMap::get_meshes() const {
  870. if (mesh_library.is_null()) {
  871. return Array();
  872. }
  873. Vector3 ofs = _get_offset();
  874. Array meshes;
  875. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  876. int id = E->get().item;
  877. if (!mesh_library->has_item(id)) {
  878. continue;
  879. }
  880. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  881. if (mesh.is_null()) {
  882. continue;
  883. }
  884. IndexKey ik = E->key();
  885. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  886. Transform xform;
  887. xform.basis.set_orthogonal_index(E->get().rot);
  888. xform.set_origin(cellpos * cell_size + ofs);
  889. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  890. meshes.push_back(xform * mesh_library->get_item_mesh_transform(id));
  891. meshes.push_back(mesh);
  892. }
  893. return meshes;
  894. }
  895. Vector3 GridMap::_get_offset() const {
  896. return Vector3(
  897. cell_size.x * 0.5 * int(center_x),
  898. cell_size.y * 0.5 * int(center_y),
  899. cell_size.z * 0.5 * int(center_z));
  900. }
  901. void GridMap::clear_baked_meshes() {
  902. for (int i = 0; i < baked_meshes.size(); i++) {
  903. if (baked_meshes[i].instance.is_valid()) {
  904. VS::get_singleton()->free(baked_meshes[i].instance);
  905. }
  906. }
  907. baked_meshes.clear();
  908. _recreate_octant_data();
  909. }
  910. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  911. if (!mesh_library.is_valid()) {
  912. return;
  913. }
  914. //generate
  915. Map<OctantKey, Map<Ref<Material>, Ref<SurfaceTool>>> surface_map;
  916. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  917. IndexKey key = E->key();
  918. int item = E->get().item;
  919. if (!mesh_library->has_item(item)) {
  920. continue;
  921. }
  922. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  923. if (!mesh.is_valid()) {
  924. continue;
  925. }
  926. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  927. Vector3 ofs = _get_offset();
  928. Transform xform;
  929. xform.basis.set_orthogonal_index(E->get().rot);
  930. xform.set_origin(cellpos * cell_size + ofs);
  931. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  932. OctantKey ok;
  933. ok.x = key.x / octant_size;
  934. ok.y = key.y / octant_size;
  935. ok.z = key.z / octant_size;
  936. if (!surface_map.has(ok)) {
  937. surface_map[ok] = Map<Ref<Material>, Ref<SurfaceTool>>();
  938. }
  939. Map<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok];
  940. for (int i = 0; i < mesh->get_surface_count(); i++) {
  941. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  942. continue;
  943. }
  944. Ref<Material> surf_mat = mesh->surface_get_material(i);
  945. if (!mat_map.has(surf_mat)) {
  946. Ref<SurfaceTool> st;
  947. st.instance();
  948. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  949. st->set_material(surf_mat);
  950. mat_map[surf_mat] = st;
  951. }
  952. mat_map[surf_mat]->append_from(mesh, i, xform);
  953. }
  954. }
  955. for (Map<OctantKey, Map<Ref<Material>, Ref<SurfaceTool>>>::Element *E = surface_map.front(); E; E = E->next()) {
  956. Ref<ArrayMesh> mesh;
  957. mesh.instance();
  958. for (Map<Ref<Material>, Ref<SurfaceTool>>::Element *F = E->get().front(); F; F = F->next()) {
  959. F->get()->commit(mesh);
  960. }
  961. BakedMesh bm;
  962. bm.mesh = mesh;
  963. bm.instance = RID_PRIME(VS::get_singleton()->instance_create());
  964. VS::get_singleton()->instance_set_portal_mode(bm.instance, VisualServer::InstancePortalMode::INSTANCE_PORTAL_MODE_GLOBAL);
  965. VS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  966. VS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  967. if (is_inside_tree()) {
  968. VS::get_singleton()->instance_set_scenario(bm.instance, get_world()->get_scenario());
  969. VS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  970. }
  971. if (p_gen_lightmap_uv) {
  972. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  973. }
  974. baked_meshes.push_back(bm);
  975. }
  976. _recreate_octant_data();
  977. }
  978. Array GridMap::get_bake_meshes() {
  979. if (!use_in_baked_light) {
  980. return Array();
  981. }
  982. if (!baked_meshes.size()) {
  983. make_baked_meshes(true);
  984. }
  985. Array arr;
  986. for (int i = 0; i < baked_meshes.size(); i++) {
  987. arr.push_back(baked_meshes[i].mesh);
  988. arr.push_back(Transform());
  989. }
  990. return arr;
  991. }
  992. RID GridMap::get_bake_mesh_instance(int p_idx) {
  993. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  994. return baked_meshes[p_idx].instance;
  995. }
  996. GridMap::GridMap() {
  997. collision_layer = 1;
  998. collision_mask = 1;
  999. cell_size = Vector3(2, 2, 2);
  1000. octant_size = 8;
  1001. awaiting_update = false;
  1002. _in_tree = false;
  1003. center_x = true;
  1004. center_y = true;
  1005. center_z = true;
  1006. clip = false;
  1007. clip_floor = 0;
  1008. clip_axis = Vector3::AXIS_Z;
  1009. clip_above = true;
  1010. cell_scale = 1.0;
  1011. navigation = nullptr;
  1012. set_notify_transform(true);
  1013. recreating_octants = false;
  1014. use_in_baked_light = false;
  1015. }
  1016. GridMap::~GridMap() {
  1017. if (!mesh_library.is_null()) {
  1018. mesh_library->unregister_owner(this);
  1019. }
  1020. clear();
  1021. }