grid_map.cpp 33 KB

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