grid_map.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /*************************************************************************/
  2. /* grid_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/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. _change_notify("mesh_library");
  162. }
  163. Ref<MeshLibrary> GridMap::get_mesh_library() const {
  164. return mesh_library;
  165. }
  166. void GridMap::set_cell_size(const Vector3 &p_size) {
  167. ERR_FAIL_COND(p_size.x < 0.001 || p_size.y < 0.001 || p_size.z < 0.001);
  168. cell_size = p_size;
  169. _recreate_octant_data();
  170. emit_signal("cell_size_changed", cell_size);
  171. }
  172. Vector3 GridMap::get_cell_size() const {
  173. return cell_size;
  174. }
  175. void GridMap::set_octant_size(int p_size) {
  176. ERR_FAIL_COND(p_size == 0);
  177. octant_size = p_size;
  178. _recreate_octant_data();
  179. }
  180. int GridMap::get_octant_size() const {
  181. return octant_size;
  182. }
  183. void GridMap::set_center_x(bool p_enable) {
  184. center_x = p_enable;
  185. _recreate_octant_data();
  186. }
  187. bool GridMap::get_center_x() const {
  188. return center_x;
  189. }
  190. void GridMap::set_center_y(bool p_enable) {
  191. center_y = p_enable;
  192. _recreate_octant_data();
  193. }
  194. bool GridMap::get_center_y() const {
  195. return center_y;
  196. }
  197. void GridMap::set_center_z(bool p_enable) {
  198. center_z = p_enable;
  199. _recreate_octant_data();
  200. }
  201. bool GridMap::get_center_z() const {
  202. return center_z;
  203. }
  204. void GridMap::set_cell_item(const Vector3i &p_position, int p_item, int p_rot) {
  205. if (baked_meshes.size() && !recreating_octants) {
  206. //if you set a cell item, baked meshes go good bye
  207. clear_baked_meshes();
  208. _recreate_octant_data();
  209. }
  210. ERR_FAIL_INDEX(ABS(p_position.x), 1 << 20);
  211. ERR_FAIL_INDEX(ABS(p_position.y), 1 << 20);
  212. ERR_FAIL_INDEX(ABS(p_position.z), 1 << 20);
  213. IndexKey key;
  214. key.x = p_position.x;
  215. key.y = p_position.y;
  216. key.z = p_position.z;
  217. OctantKey ok;
  218. ok.x = p_position.x / octant_size;
  219. ok.y = p_position.y / octant_size;
  220. ok.z = p_position.z / octant_size;
  221. if (p_item < 0) {
  222. //erase
  223. if (cell_map.has(key)) {
  224. OctantKey octantkey = ok;
  225. ERR_FAIL_COND(!octant_map.has(octantkey));
  226. Octant &g = *octant_map[octantkey];
  227. g.cells.erase(key);
  228. g.dirty = true;
  229. cell_map.erase(key);
  230. _queue_octants_dirty();
  231. }
  232. return;
  233. }
  234. OctantKey octantkey = ok;
  235. if (!octant_map.has(octantkey)) {
  236. //create octant because it does not exist
  237. Octant *g = memnew(Octant);
  238. g->dirty = true;
  239. g->static_body = PhysicsServer3D::get_singleton()->body_create(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(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. _change_notify("visible");
  576. for (Map<OctantKey, Octant *>::Element *e = octant_map.front(); e; e = e->next()) {
  577. Octant *octant = e->value();
  578. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  579. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  580. RS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree());
  581. }
  582. }
  583. }
  584. void GridMap::_queue_octants_dirty() {
  585. if (awaiting_update) {
  586. return;
  587. }
  588. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  589. awaiting_update = true;
  590. }
  591. void GridMap::_recreate_octant_data() {
  592. recreating_octants = true;
  593. Map<IndexKey, Cell> cell_copy = cell_map;
  594. _clear_internal();
  595. for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
  596. set_cell_item(Vector3i(E->key()), E->get().item, E->get().rot);
  597. }
  598. recreating_octants = false;
  599. }
  600. void GridMap::_clear_internal() {
  601. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  602. if (is_inside_world()) {
  603. _octant_exit_world(E->key());
  604. }
  605. _octant_clean_up(E->key());
  606. memdelete(E->get());
  607. }
  608. octant_map.clear();
  609. cell_map.clear();
  610. }
  611. void GridMap::clear() {
  612. _clear_internal();
  613. clear_baked_meshes();
  614. }
  615. void GridMap::resource_changed(const RES &p_res) {
  616. _recreate_octant_data();
  617. }
  618. void GridMap::_update_octants_callback() {
  619. if (!awaiting_update) {
  620. return;
  621. }
  622. List<OctantKey> to_delete;
  623. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  624. if (_octant_update(E->key())) {
  625. to_delete.push_back(E->key());
  626. }
  627. }
  628. while (to_delete.front()) {
  629. octant_map.erase(to_delete.front()->get());
  630. to_delete.pop_back();
  631. }
  632. _update_visibility();
  633. awaiting_update = false;
  634. }
  635. void GridMap::_bind_methods() {
  636. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  637. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  638. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  639. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  640. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &GridMap::set_collision_mask_bit);
  641. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &GridMap::get_collision_mask_bit);
  642. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &GridMap::set_collision_layer_bit);
  643. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &GridMap::get_collision_layer_bit);
  644. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  645. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  646. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  647. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  648. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  649. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  650. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  651. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  652. ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  653. ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item);
  654. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);
  655. ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &GridMap::world_to_map);
  656. ClassDB::bind_method(D_METHOD("map_to_world", "map_position"), &GridMap::map_to_world);
  657. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  658. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  659. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  660. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  661. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  662. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  663. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  664. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  665. ClassDB::bind_method(D_METHOD("set_clip", "enabled", "clipabove", "floor", "axis"), &GridMap::set_clip, DEFVAL(true), DEFVAL(0), DEFVAL(Vector3::AXIS_X));
  666. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  667. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  668. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  669. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  670. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  671. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  672. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  673. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  674. ADD_GROUP("Cell", "cell_");
  675. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size"), "set_cell_size", "get_cell_size");
  676. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  677. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  678. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  679. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  680. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_scale"), "set_cell_scale", "get_cell_scale");
  681. ADD_GROUP("Collision", "collision_");
  682. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  683. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  684. BIND_CONSTANT(INVALID_CELL_ITEM);
  685. ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size")));
  686. }
  687. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  688. if (!p_enabled && !clip) {
  689. return;
  690. }
  691. if (clip && p_enabled && clip_floor == p_floor && p_clip_above == clip_above && p_axis == clip_axis) {
  692. return;
  693. }
  694. clip = p_enabled;
  695. clip_floor = p_floor;
  696. clip_axis = p_axis;
  697. clip_above = p_clip_above;
  698. //make it all update
  699. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  700. Octant *g = E->get();
  701. g->dirty = true;
  702. }
  703. awaiting_update = true;
  704. _update_octants_callback();
  705. }
  706. void GridMap::set_cell_scale(float p_scale) {
  707. cell_scale = p_scale;
  708. _recreate_octant_data();
  709. }
  710. float GridMap::get_cell_scale() const {
  711. return cell_scale;
  712. }
  713. Array GridMap::get_used_cells() const {
  714. Array a;
  715. a.resize(cell_map.size());
  716. int i = 0;
  717. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  718. Vector3 p(E->key().x, E->key().y, E->key().z);
  719. a[i++] = p;
  720. }
  721. return a;
  722. }
  723. Array GridMap::get_meshes() {
  724. if (mesh_library.is_null()) {
  725. return Array();
  726. }
  727. Vector3 ofs = _get_offset();
  728. Array meshes;
  729. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  730. int id = E->get().item;
  731. if (!mesh_library->has_item(id)) {
  732. continue;
  733. }
  734. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  735. if (mesh.is_null()) {
  736. continue;
  737. }
  738. IndexKey ik = E->key();
  739. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  740. Transform xform;
  741. xform.basis.set_orthogonal_index(E->get().rot);
  742. xform.set_origin(cellpos * cell_size + ofs);
  743. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  744. meshes.push_back(xform);
  745. meshes.push_back(mesh);
  746. }
  747. return meshes;
  748. }
  749. Vector3 GridMap::_get_offset() const {
  750. return Vector3(
  751. cell_size.x * 0.5 * int(center_x),
  752. cell_size.y * 0.5 * int(center_y),
  753. cell_size.z * 0.5 * int(center_z));
  754. }
  755. void GridMap::clear_baked_meshes() {
  756. for (int i = 0; i < baked_meshes.size(); i++) {
  757. RS::get_singleton()->free(baked_meshes[i].instance);
  758. }
  759. baked_meshes.clear();
  760. _recreate_octant_data();
  761. }
  762. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  763. if (!mesh_library.is_valid()) {
  764. return;
  765. }
  766. //generate
  767. Map<OctantKey, Map<Ref<Material>, Ref<SurfaceTool>>> surface_map;
  768. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  769. IndexKey key = E->key();
  770. int item = E->get().item;
  771. if (!mesh_library->has_item(item)) {
  772. continue;
  773. }
  774. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  775. if (!mesh.is_valid()) {
  776. continue;
  777. }
  778. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  779. Vector3 ofs = _get_offset();
  780. Transform xform;
  781. xform.basis.set_orthogonal_index(E->get().rot);
  782. xform.set_origin(cellpos * cell_size + ofs);
  783. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  784. OctantKey ok;
  785. ok.x = key.x / octant_size;
  786. ok.y = key.y / octant_size;
  787. ok.z = key.z / octant_size;
  788. if (!surface_map.has(ok)) {
  789. surface_map[ok] = Map<Ref<Material>, Ref<SurfaceTool>>();
  790. }
  791. Map<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok];
  792. for (int i = 0; i < mesh->get_surface_count(); i++) {
  793. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  794. continue;
  795. }
  796. Ref<Material> surf_mat = mesh->surface_get_material(i);
  797. if (!mat_map.has(surf_mat)) {
  798. Ref<SurfaceTool> st;
  799. st.instance();
  800. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  801. st->set_material(surf_mat);
  802. mat_map[surf_mat] = st;
  803. }
  804. mat_map[surf_mat]->append_from(mesh, i, xform);
  805. }
  806. }
  807. for (Map<OctantKey, Map<Ref<Material>, Ref<SurfaceTool>>>::Element *E = surface_map.front(); E; E = E->next()) {
  808. Ref<ArrayMesh> mesh;
  809. mesh.instance();
  810. for (Map<Ref<Material>, Ref<SurfaceTool>>::Element *F = E->get().front(); F; F = F->next()) {
  811. F->get()->commit(mesh);
  812. }
  813. BakedMesh bm;
  814. bm.mesh = mesh;
  815. bm.instance = RS::get_singleton()->instance_create();
  816. RS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  817. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  818. if (is_inside_tree()) {
  819. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  820. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  821. }
  822. if (p_gen_lightmap_uv) {
  823. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  824. }
  825. baked_meshes.push_back(bm);
  826. }
  827. _recreate_octant_data();
  828. }
  829. Array GridMap::get_bake_meshes() {
  830. if (!baked_meshes.size()) {
  831. make_baked_meshes(true);
  832. }
  833. Array arr;
  834. for (int i = 0; i < baked_meshes.size(); i++) {
  835. arr.push_back(baked_meshes[i].mesh);
  836. arr.push_back(Transform());
  837. }
  838. return arr;
  839. }
  840. RID GridMap::get_bake_mesh_instance(int p_idx) {
  841. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  842. return baked_meshes[p_idx].instance;
  843. }
  844. GridMap::GridMap() {
  845. collision_layer = 1;
  846. collision_mask = 1;
  847. cell_size = Vector3(2, 2, 2);
  848. octant_size = 8;
  849. awaiting_update = false;
  850. _in_tree = false;
  851. center_x = true;
  852. center_y = true;
  853. center_z = true;
  854. clip = false;
  855. clip_floor = 0;
  856. clip_axis = Vector3::AXIS_Z;
  857. clip_above = true;
  858. cell_scale = 1.0;
  859. navigation = nullptr;
  860. set_notify_transform(true);
  861. recreating_octants = false;
  862. }
  863. GridMap::~GridMap() {
  864. if (!mesh_library.is_null()) {
  865. mesh_library->unregister_owner(this);
  866. }
  867. clear();
  868. }