grid_map.cpp 33 KB

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