grid_map.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. _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();
  240. PhysicsServer3D::get_singleton()->body_set_mode(g->static_body, PhysicsServer3D::BODY_MODE_STATIC);
  241. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
  242. PhysicsServer3D::get_singleton()->body_set_collision_layer(g->static_body, collision_layer);
  243. PhysicsServer3D::get_singleton()->body_set_collision_mask(g->static_body, collision_mask);
  244. SceneTree *st = SceneTree::get_singleton();
  245. if (st && st->is_debugging_collisions_hint()) {
  246. g->collision_debug = RenderingServer::get_singleton()->mesh_create();
  247. g->collision_debug_instance = RenderingServer::get_singleton()->instance_create();
  248. RenderingServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  249. }
  250. octant_map[octantkey] = g;
  251. if (is_inside_world()) {
  252. _octant_enter_world(octantkey);
  253. _octant_transform(octantkey);
  254. }
  255. }
  256. Octant &g = *octant_map[octantkey];
  257. g.cells.insert(key);
  258. g.dirty = true;
  259. _queue_octants_dirty();
  260. Cell c;
  261. c.item = p_item;
  262. c.rot = p_rot;
  263. cell_map[key] = c;
  264. }
  265. int GridMap::get_cell_item(const Vector3i &p_position) const {
  266. ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, INVALID_CELL_ITEM);
  267. ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, INVALID_CELL_ITEM);
  268. ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, INVALID_CELL_ITEM);
  269. IndexKey key;
  270. key.x = p_position.x;
  271. key.y = p_position.y;
  272. key.z = p_position.z;
  273. if (!cell_map.has(key)) {
  274. return INVALID_CELL_ITEM;
  275. }
  276. return cell_map[key].item;
  277. }
  278. int GridMap::get_cell_item_orientation(const Vector3i &p_position) const {
  279. ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, -1);
  280. ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, -1);
  281. ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, -1);
  282. IndexKey key;
  283. key.x = p_position.x;
  284. key.y = p_position.y;
  285. key.z = p_position.z;
  286. if (!cell_map.has(key)) {
  287. return -1;
  288. }
  289. return cell_map[key].rot;
  290. }
  291. Vector3i GridMap::world_to_map(const Vector3 &p_world_position) const {
  292. Vector3 map_position = p_world_position / cell_size;
  293. map_position.x = floor(map_position.x);
  294. map_position.y = floor(map_position.y);
  295. map_position.z = floor(map_position.z);
  296. return Vector3i(map_position);
  297. }
  298. Vector3 GridMap::map_to_world(const Vector3i &p_map_position) const {
  299. Vector3 offset = _get_offset();
  300. Vector3 world_pos(
  301. p_map_position.x * cell_size.x + offset.x,
  302. p_map_position.y * cell_size.y + offset.y,
  303. p_map_position.z * cell_size.z + offset.z);
  304. return world_pos;
  305. }
  306. void GridMap::_octant_transform(const OctantKey &p_key) {
  307. ERR_FAIL_COND(!octant_map.has(p_key));
  308. Octant &g = *octant_map[p_key];
  309. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  310. if (g.collision_debug_instance.is_valid()) {
  311. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  312. }
  313. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  314. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  315. }
  316. }
  317. bool GridMap::_octant_update(const OctantKey &p_key) {
  318. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  319. Octant &g = *octant_map[p_key];
  320. if (!g.dirty) {
  321. return false;
  322. }
  323. //erase body shapes
  324. PhysicsServer3D::get_singleton()->body_clear_shapes(g.static_body);
  325. //erase body shapes debug
  326. if (g.collision_debug.is_valid()) {
  327. RS::get_singleton()->mesh_clear(g.collision_debug);
  328. }
  329. //erase navigation
  330. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  331. NavigationServer3D::get_singleton()->free(E->get().region);
  332. }
  333. g.navmesh_ids.clear();
  334. //erase multimeshes
  335. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  336. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  337. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  338. }
  339. g.multimesh_instances.clear();
  340. if (g.cells.size() == 0) {
  341. //octant no longer needed
  342. _octant_clean_up(p_key);
  343. return true;
  344. }
  345. Vector<Vector3> col_debug;
  346. /*
  347. * foreach item in this octant,
  348. * set item's multimesh's instance count to number of cells which have this item
  349. * and set said multimesh bounding box to one containing all cells which have this item
  350. */
  351. Map<int, List<Pair<Transform, IndexKey>>> multimesh_items;
  352. for (Set<IndexKey>::Element *E = g.cells.front(); E; E = E->next()) {
  353. ERR_CONTINUE(!cell_map.has(E->get()));
  354. const Cell &c = cell_map[E->get()];
  355. if (!mesh_library.is_valid() || !mesh_library->has_item(c.item)) {
  356. continue;
  357. }
  358. Vector3 cellpos = Vector3(E->get().x, E->get().y, E->get().z);
  359. Vector3 ofs = _get_offset();
  360. Transform xform;
  361. xform.basis.set_orthogonal_index(c.rot);
  362. xform.set_origin(cellpos * cell_size + ofs);
  363. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  364. if (baked_meshes.size() == 0) {
  365. if (mesh_library->get_item_mesh(c.item).is_valid()) {
  366. if (!multimesh_items.has(c.item)) {
  367. multimesh_items[c.item] = List<Pair<Transform, IndexKey>>();
  368. }
  369. Pair<Transform, IndexKey> p;
  370. p.first = xform;
  371. p.second = E->get();
  372. multimesh_items[c.item].push_back(p);
  373. }
  374. }
  375. Vector<MeshLibrary::ShapeData> shapes = mesh_library->get_item_shapes(c.item);
  376. // add the item's shape at given xform to octant's static_body
  377. for (int i = 0; i < shapes.size(); i++) {
  378. // add the item's shape
  379. if (!shapes[i].shape.is_valid()) {
  380. continue;
  381. }
  382. PhysicsServer3D::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  383. if (g.collision_debug.is_valid()) {
  384. shapes.write[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  385. }
  386. }
  387. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  388. Ref<NavigationMesh> navmesh = mesh_library->get_item_navmesh(c.item);
  389. if (navmesh.is_valid()) {
  390. Octant::NavMesh nm;
  391. nm.xform = xform * mesh_library->get_item_navmesh_transform(c.item);
  392. if (navigation) {
  393. RID region = NavigationServer3D::get_singleton()->region_create();
  394. NavigationServer3D::get_singleton()->region_set_navmesh(region, navmesh);
  395. NavigationServer3D::get_singleton()->region_set_transform(region, navigation->get_global_transform() * nm.xform);
  396. NavigationServer3D::get_singleton()->region_set_map(region, navigation->get_rid());
  397. nm.region = region;
  398. }
  399. g.navmesh_ids[E->get()] = nm;
  400. }
  401. }
  402. //update multimeshes, only if not baked
  403. if (baked_meshes.size() == 0) {
  404. for (Map<int, List<Pair<Transform, IndexKey>>>::Element *E = multimesh_items.front(); E; E = E->next()) {
  405. Octant::MultimeshInstance mmi;
  406. RID mm = RS::get_singleton()->multimesh_create();
  407. RS::get_singleton()->multimesh_allocate_data(mm, E->get().size(), RS::MULTIMESH_TRANSFORM_3D);
  408. RS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E->key())->get_rid());
  409. int idx = 0;
  410. for (List<Pair<Transform, IndexKey>>::Element *F = E->get().front(); F; F = F->next()) {
  411. RS::get_singleton()->multimesh_instance_set_transform(mm, idx, F->get().first);
  412. #ifdef TOOLS_ENABLED
  413. Octant::MultimeshInstance::Item it;
  414. it.index = idx;
  415. it.transform = F->get().first;
  416. it.key = F->get().second;
  417. mmi.items.push_back(it);
  418. #endif
  419. idx++;
  420. }
  421. RID instance = RS::get_singleton()->instance_create();
  422. RS::get_singleton()->instance_set_base(instance, mm);
  423. if (is_inside_tree()) {
  424. RS::get_singleton()->instance_set_scenario(instance, get_world_3d()->get_scenario());
  425. RS::get_singleton()->instance_set_transform(instance, get_global_transform());
  426. }
  427. mmi.multimesh = mm;
  428. mmi.instance = instance;
  429. g.multimesh_instances.push_back(mmi);
  430. }
  431. }
  432. if (col_debug.size()) {
  433. Array arr;
  434. arr.resize(RS::ARRAY_MAX);
  435. arr[RS::ARRAY_VERTEX] = col_debug;
  436. RS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, RS::PRIMITIVE_LINES, arr);
  437. SceneTree *st = SceneTree::get_singleton();
  438. if (st) {
  439. RS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  440. }
  441. }
  442. g.dirty = false;
  443. return false;
  444. }
  445. void GridMap::_reset_physic_bodies_collision_filters() {
  446. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  447. PhysicsServer3D::get_singleton()->body_set_collision_layer(E->get()->static_body, collision_layer);
  448. PhysicsServer3D::get_singleton()->body_set_collision_mask(E->get()->static_body, collision_mask);
  449. }
  450. }
  451. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  452. ERR_FAIL_COND(!octant_map.has(p_key));
  453. Octant &g = *octant_map[p_key];
  454. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  455. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, get_world_3d()->get_space());
  456. if (g.collision_debug_instance.is_valid()) {
  457. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world_3d()->get_scenario());
  458. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  459. }
  460. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  461. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world_3d()->get_scenario());
  462. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  463. }
  464. if (navigation && mesh_library.is_valid()) {
  465. for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
  466. if (cell_map.has(F->key()) && F->get().region.is_valid() == false) {
  467. Ref<NavigationMesh> nm = mesh_library->get_item_navmesh(cell_map[F->key()].item);
  468. if (nm.is_valid()) {
  469. RID region = NavigationServer3D::get_singleton()->region_create();
  470. NavigationServer3D::get_singleton()->region_set_navmesh(region, nm);
  471. NavigationServer3D::get_singleton()->region_set_transform(region, navigation->get_global_transform() * F->get().xform);
  472. NavigationServer3D::get_singleton()->region_set_map(region, navigation->get_rid());
  473. F->get().region = region;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  480. ERR_FAIL_COND(!octant_map.has(p_key));
  481. Octant &g = *octant_map[p_key];
  482. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  483. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID());
  484. if (g.collision_debug_instance.is_valid()) {
  485. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  486. }
  487. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  488. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  489. }
  490. if (navigation) {
  491. for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
  492. if (F->get().region.is_valid()) {
  493. NavigationServer3D::get_singleton()->free(F->get().region);
  494. F->get().region = RID();
  495. }
  496. }
  497. }
  498. }
  499. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  500. ERR_FAIL_COND(!octant_map.has(p_key));
  501. Octant &g = *octant_map[p_key];
  502. if (g.collision_debug.is_valid()) {
  503. RS::get_singleton()->free(g.collision_debug);
  504. }
  505. if (g.collision_debug_instance.is_valid()) {
  506. RS::get_singleton()->free(g.collision_debug_instance);
  507. }
  508. PhysicsServer3D::get_singleton()->free(g.static_body);
  509. // Erase navigation
  510. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  511. NavigationServer3D::get_singleton()->free(E->get().region);
  512. }
  513. g.navmesh_ids.clear();
  514. //erase multimeshes
  515. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  516. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  517. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  518. }
  519. g.multimesh_instances.clear();
  520. }
  521. void GridMap::_notification(int p_what) {
  522. switch (p_what) {
  523. case NOTIFICATION_ENTER_WORLD: {
  524. Node3D *c = this;
  525. while (c) {
  526. navigation = Object::cast_to<Navigation3D>(c);
  527. if (navigation) {
  528. break;
  529. }
  530. c = Object::cast_to<Node3D>(c->get_parent());
  531. }
  532. last_transform = get_global_transform();
  533. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  534. _octant_enter_world(E->key());
  535. }
  536. for (int i = 0; i < baked_meshes.size(); i++) {
  537. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world_3d()->get_scenario());
  538. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  539. }
  540. } break;
  541. case NOTIFICATION_TRANSFORM_CHANGED: {
  542. Transform new_xform = get_global_transform();
  543. if (new_xform == last_transform) {
  544. break;
  545. }
  546. //update run
  547. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  548. _octant_transform(E->key());
  549. }
  550. last_transform = new_xform;
  551. for (int i = 0; i < baked_meshes.size(); i++) {
  552. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  553. }
  554. } break;
  555. case NOTIFICATION_EXIT_WORLD: {
  556. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  557. _octant_exit_world(E->key());
  558. }
  559. navigation = nullptr;
  560. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  561. //_update_octants_callback();
  562. //_update_area_instances();
  563. for (int i = 0; i < baked_meshes.size(); i++) {
  564. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID());
  565. }
  566. } break;
  567. case NOTIFICATION_VISIBILITY_CHANGED: {
  568. _update_visibility();
  569. } break;
  570. }
  571. }
  572. void GridMap::_update_visibility() {
  573. if (!is_inside_tree()) {
  574. return;
  575. }
  576. _change_notify("visible");
  577. for (Map<OctantKey, Octant *>::Element *e = octant_map.front(); e; e = e->next()) {
  578. Octant *octant = e->value();
  579. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  580. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  581. RS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree());
  582. }
  583. }
  584. }
  585. void GridMap::_queue_octants_dirty() {
  586. if (awaiting_update) {
  587. return;
  588. }
  589. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  590. awaiting_update = true;
  591. }
  592. void GridMap::_recreate_octant_data() {
  593. recreating_octants = true;
  594. Map<IndexKey, Cell> cell_copy = cell_map;
  595. _clear_internal();
  596. for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
  597. set_cell_item(Vector3i(E->key()), E->get().item, E->get().rot);
  598. }
  599. recreating_octants = false;
  600. }
  601. void GridMap::_clear_internal() {
  602. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  603. if (is_inside_world()) {
  604. _octant_exit_world(E->key());
  605. }
  606. _octant_clean_up(E->key());
  607. memdelete(E->get());
  608. }
  609. octant_map.clear();
  610. cell_map.clear();
  611. }
  612. void GridMap::clear() {
  613. _clear_internal();
  614. clear_baked_meshes();
  615. }
  616. void GridMap::resource_changed(const RES &p_res) {
  617. _recreate_octant_data();
  618. }
  619. void GridMap::_update_octants_callback() {
  620. if (!awaiting_update) {
  621. return;
  622. }
  623. List<OctantKey> to_delete;
  624. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  625. if (_octant_update(E->key())) {
  626. to_delete.push_back(E->key());
  627. }
  628. }
  629. while (to_delete.front()) {
  630. octant_map.erase(to_delete.front()->get());
  631. to_delete.pop_back();
  632. }
  633. _update_visibility();
  634. awaiting_update = false;
  635. }
  636. void GridMap::_bind_methods() {
  637. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  638. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  639. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  640. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  641. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &GridMap::set_collision_mask_bit);
  642. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &GridMap::get_collision_mask_bit);
  643. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &GridMap::set_collision_layer_bit);
  644. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &GridMap::get_collision_layer_bit);
  645. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  646. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  647. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  648. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  649. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  650. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  651. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  652. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  653. ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  654. ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item);
  655. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);
  656. ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &GridMap::world_to_map);
  657. ClassDB::bind_method(D_METHOD("map_to_world", "map_position"), &GridMap::map_to_world);
  658. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  659. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  660. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  661. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  662. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  663. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  664. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  665. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  666. ClassDB::bind_method(D_METHOD("set_clip", "enabled", "clipabove", "floor", "axis"), &GridMap::set_clip, DEFVAL(true), DEFVAL(0), DEFVAL(Vector3::AXIS_X));
  667. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  668. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  669. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  670. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  671. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  672. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  673. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  674. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  675. ADD_GROUP("Cell", "cell_");
  676. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size"), "set_cell_size", "get_cell_size");
  677. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  678. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  679. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  680. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  681. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_scale"), "set_cell_scale", "get_cell_scale");
  682. ADD_GROUP("Collision", "collision_");
  683. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  684. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  685. BIND_CONSTANT(INVALID_CELL_ITEM);
  686. ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size")));
  687. }
  688. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  689. if (!p_enabled && !clip) {
  690. return;
  691. }
  692. if (clip && p_enabled && clip_floor == p_floor && p_clip_above == clip_above && p_axis == clip_axis) {
  693. return;
  694. }
  695. clip = p_enabled;
  696. clip_floor = p_floor;
  697. clip_axis = p_axis;
  698. clip_above = p_clip_above;
  699. //make it all update
  700. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  701. Octant *g = E->get();
  702. g->dirty = true;
  703. }
  704. awaiting_update = true;
  705. _update_octants_callback();
  706. }
  707. void GridMap::set_cell_scale(float p_scale) {
  708. cell_scale = p_scale;
  709. _recreate_octant_data();
  710. }
  711. float GridMap::get_cell_scale() const {
  712. return cell_scale;
  713. }
  714. Array GridMap::get_used_cells() const {
  715. Array a;
  716. a.resize(cell_map.size());
  717. int i = 0;
  718. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  719. Vector3 p(E->key().x, E->key().y, E->key().z);
  720. a[i++] = p;
  721. }
  722. return a;
  723. }
  724. Array GridMap::get_meshes() {
  725. if (mesh_library.is_null()) {
  726. return Array();
  727. }
  728. Vector3 ofs = _get_offset();
  729. Array meshes;
  730. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  731. int id = E->get().item;
  732. if (!mesh_library->has_item(id)) {
  733. continue;
  734. }
  735. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  736. if (mesh.is_null()) {
  737. continue;
  738. }
  739. IndexKey ik = E->key();
  740. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  741. Transform xform;
  742. xform.basis.set_orthogonal_index(E->get().rot);
  743. xform.set_origin(cellpos * cell_size + ofs);
  744. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  745. meshes.push_back(xform);
  746. meshes.push_back(mesh);
  747. }
  748. return meshes;
  749. }
  750. Vector3 GridMap::_get_offset() const {
  751. return Vector3(
  752. cell_size.x * 0.5 * int(center_x),
  753. cell_size.y * 0.5 * int(center_y),
  754. cell_size.z * 0.5 * int(center_z));
  755. }
  756. void GridMap::clear_baked_meshes() {
  757. for (int i = 0; i < baked_meshes.size(); i++) {
  758. RS::get_singleton()->free(baked_meshes[i].instance);
  759. }
  760. baked_meshes.clear();
  761. _recreate_octant_data();
  762. }
  763. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  764. if (!mesh_library.is_valid()) {
  765. return;
  766. }
  767. //generate
  768. Map<OctantKey, Map<Ref<Material>, Ref<SurfaceTool>>> surface_map;
  769. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  770. IndexKey key = E->key();
  771. int item = E->get().item;
  772. if (!mesh_library->has_item(item)) {
  773. continue;
  774. }
  775. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  776. if (!mesh.is_valid()) {
  777. continue;
  778. }
  779. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  780. Vector3 ofs = _get_offset();
  781. Transform xform;
  782. xform.basis.set_orthogonal_index(E->get().rot);
  783. xform.set_origin(cellpos * cell_size + ofs);
  784. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  785. OctantKey ok;
  786. ok.x = key.x / octant_size;
  787. ok.y = key.y / octant_size;
  788. ok.z = key.z / octant_size;
  789. if (!surface_map.has(ok)) {
  790. surface_map[ok] = Map<Ref<Material>, Ref<SurfaceTool>>();
  791. }
  792. Map<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok];
  793. for (int i = 0; i < mesh->get_surface_count(); i++) {
  794. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  795. continue;
  796. }
  797. Ref<Material> surf_mat = mesh->surface_get_material(i);
  798. if (!mat_map.has(surf_mat)) {
  799. Ref<SurfaceTool> st;
  800. st.instance();
  801. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  802. st->set_material(surf_mat);
  803. mat_map[surf_mat] = st;
  804. }
  805. mat_map[surf_mat]->append_from(mesh, i, xform);
  806. }
  807. }
  808. for (Map<OctantKey, Map<Ref<Material>, Ref<SurfaceTool>>>::Element *E = surface_map.front(); E; E = E->next()) {
  809. Ref<ArrayMesh> mesh;
  810. mesh.instance();
  811. for (Map<Ref<Material>, Ref<SurfaceTool>>::Element *F = E->get().front(); F; F = F->next()) {
  812. F->get()->commit(mesh);
  813. }
  814. BakedMesh bm;
  815. bm.mesh = mesh;
  816. bm.instance = RS::get_singleton()->instance_create();
  817. RS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  818. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  819. if (is_inside_tree()) {
  820. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  821. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  822. }
  823. if (p_gen_lightmap_uv) {
  824. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  825. }
  826. baked_meshes.push_back(bm);
  827. }
  828. _recreate_octant_data();
  829. }
  830. Array GridMap::get_bake_meshes() {
  831. if (!baked_meshes.size()) {
  832. make_baked_meshes(true);
  833. }
  834. Array arr;
  835. for (int i = 0; i < baked_meshes.size(); i++) {
  836. arr.push_back(baked_meshes[i].mesh);
  837. arr.push_back(Transform());
  838. }
  839. return arr;
  840. }
  841. RID GridMap::get_bake_mesh_instance(int p_idx) {
  842. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  843. return baked_meshes[p_idx].instance;
  844. }
  845. GridMap::GridMap() {
  846. set_notify_transform(true);
  847. }
  848. GridMap::~GridMap() {
  849. if (!mesh_library.is_null()) {
  850. mesh_library->unregister_owner(this);
  851. }
  852. clear();
  853. }