grid_map.cpp 33 KB

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