grid_map.cpp 33 KB

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