grid_map.cpp 30 KB

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