grid_map.cpp 33 KB

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