grid_map.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. /*************************************************************************/
  2. /* grid_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "io/marshalls.h"
  32. #include "message_queue.h"
  33. #include "os/os.h"
  34. #include "scene/3d/baked_light_instance.h"
  35. #include "scene/3d/light.h"
  36. #include "scene/resources/mesh_library.h"
  37. #include "scene/resources/surface_tool.h"
  38. #include "scene/scene_string_names.h"
  39. #include "servers/visual_server.h"
  40. bool GridMap::_set(const StringName &p_name, const Variant &p_value) {
  41. String name = p_name;
  42. if (name == "theme/theme") {
  43. set_theme(p_value);
  44. } else if (name == "cell/size") {
  45. set_cell_size(p_value);
  46. } else if (name == "cell/octant_size") {
  47. set_octant_size(p_value);
  48. } else if (name == "cell/center_x") {
  49. set_center_x(p_value);
  50. } else if (name == "cell/center_y") {
  51. set_center_y(p_value);
  52. } else if (name == "cell/center_z") {
  53. set_center_z(p_value);
  54. } else if (name == "cell/scale") {
  55. set_cell_scale(p_value);
  56. } else if (name == "lighting/bake") {
  57. set_use_baked_light(p_value);
  58. } else if (name == "theme/bake") {
  59. set_bake(p_value);
  60. /* } else if (name=="cells") {
  61. DVector<int> cells = p_value;
  62. int amount=cells.size();
  63. DVector<int>::Read r = cells.read();
  64. ERR_FAIL_COND_V(amount&1,false); // not even
  65. cell_map.clear();
  66. for(int i=0;i<amount/3;i++) {
  67. IndexKey ik;
  68. ik.key=decode_uint64(&r[i*3]);
  69. Cell cell;
  70. cell.cell=uint32_t(r[i*+1]);
  71. cell_map[ik]=cell;
  72. }
  73. _recreate_octant_data();*/
  74. } else if (name == "data") {
  75. Dictionary d = p_value;
  76. Dictionary baked;
  77. if (d.has("baked"))
  78. baked = d["baked"];
  79. if (d.has("cells")) {
  80. DVector<int> cells = d["cells"];
  81. int amount = cells.size();
  82. DVector<int>::Read r = cells.read();
  83. ERR_FAIL_COND_V(amount % 3, false); // not even
  84. cell_map.clear();
  85. for (int i = 0; i < amount / 3; i++) {
  86. IndexKey ik;
  87. ik.key = decode_uint64((const uint8_t *)&r[i * 3]);
  88. Cell cell;
  89. cell.cell = decode_uint32((const uint8_t *)&r[i * 3 + 2]);
  90. cell_map[ik] = cell;
  91. }
  92. }
  93. baked_lock = baked.size() != 0;
  94. _recreate_octant_data();
  95. baked_lock = false;
  96. if (!baked.empty()) {
  97. List<Variant> kl;
  98. baked.get_key_list(&kl);
  99. for (List<Variant>::Element *E = kl.front(); E; E = E->next()) {
  100. Plane ikv = E->get();
  101. Ref<Mesh> b = baked[ikv];
  102. ERR_CONTINUE(!b.is_valid());
  103. OctantKey ok;
  104. ok.x = ikv.normal.x;
  105. ok.y = ikv.normal.y;
  106. ok.z = ikv.normal.z;
  107. ok.area = ikv.d;
  108. ERR_CONTINUE(!octant_map.has(ok));
  109. Octant &g = *octant_map[ok];
  110. g.baked = b;
  111. g.bake_instance = VS::get_singleton()->instance_create();
  112. VS::get_singleton()->instance_set_base(g.bake_instance, g.baked->get_rid());
  113. VS::get_singleton()->instance_geometry_set_baked_light(g.bake_instance, baked_light_instance ? baked_light_instance->get_baked_light_instance() : RID());
  114. }
  115. }
  116. } else if (name.begins_with("areas/")) {
  117. int which = name.get_slicec('/', 1).to_int();
  118. String what = name.get_slicec('/', 2);
  119. if (what == "bounds") {
  120. ERR_FAIL_COND_V(area_map.has(which), false);
  121. create_area(which, p_value);
  122. return true;
  123. }
  124. ERR_FAIL_COND_V(!area_map.has(which), false);
  125. if (what == "name")
  126. area_set_name(which, p_value);
  127. else if (what == "disable_distance")
  128. area_set_portal_disable_distance(which, p_value);
  129. else if (what == "exterior_portal")
  130. area_set_portal_disable_color(which, p_value);
  131. else
  132. return false;
  133. } else
  134. return false;
  135. return true;
  136. }
  137. bool GridMap::_get(const StringName &p_name, Variant &r_ret) const {
  138. String name = p_name;
  139. if (name == "theme/theme") {
  140. r_ret = get_theme();
  141. } else if (name == "cell/size") {
  142. r_ret = get_cell_size();
  143. } else if (name == "cell/octant_size") {
  144. r_ret = get_octant_size();
  145. } else if (name == "cell/center_x") {
  146. r_ret = get_center_x();
  147. } else if (name == "cell/center_y") {
  148. r_ret = get_center_y();
  149. } else if (name == "cell/center_z") {
  150. r_ret = get_center_z();
  151. } else if (name == "cell/scale") {
  152. r_ret = cell_scale;
  153. } else if (name == "lighting/bake") {
  154. r_ret = is_using_baked_light();
  155. } else if (name == "theme/bake") {
  156. r_ret = bake;
  157. } else if (name == "data") {
  158. Dictionary d;
  159. DVector<int> cells;
  160. cells.resize(cell_map.size() * 3);
  161. {
  162. DVector<int>::Write w = cells.write();
  163. int i = 0;
  164. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next(), i++) {
  165. encode_uint64(E->key().key, (uint8_t *)&w[i * 3]);
  166. encode_uint32(E->get().cell, (uint8_t *)&w[i * 3 + 2]);
  167. }
  168. }
  169. d["cells"] = cells;
  170. Dictionary baked;
  171. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  172. Octant &g = *E->get();
  173. if (g.baked.is_valid()) {
  174. baked[Plane(E->key().x, E->key().y, E->key().z, E->key().area)] = g.baked;
  175. }
  176. }
  177. if (baked.size()) {
  178. d["baked"] = baked;
  179. }
  180. r_ret = d;
  181. } else if (name.begins_with("areas/")) {
  182. int which = name.get_slicec('/', 1).to_int();
  183. String what = name.get_slicec('/', 2);
  184. if (what == "bounds")
  185. r_ret = area_get_bounds(which);
  186. else if (what == "name")
  187. r_ret = area_get_name(which);
  188. else if (what == "disable_distance")
  189. r_ret = area_get_portal_disable_distance(which);
  190. else if (what == "exterior_portal")
  191. r_ret = area_is_exterior_portal(which);
  192. else
  193. return false;
  194. } else
  195. return false;
  196. return true;
  197. }
  198. void GridMap::_get_property_list(List<PropertyInfo> *p_list) const {
  199. p_list->push_back(PropertyInfo(Variant::OBJECT, "theme/theme", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"));
  200. p_list->push_back(PropertyInfo(Variant::BOOL, "theme/bake"));
  201. p_list->push_back(PropertyInfo(Variant::BOOL, "lighting/bake"));
  202. p_list->push_back(PropertyInfo(Variant::REAL, "cell/size", PROPERTY_HINT_RANGE, "0.01,16384,0.01"));
  203. p_list->push_back(PropertyInfo(Variant::INT, "cell/octant_size", PROPERTY_HINT_RANGE, "1,1024,1"));
  204. p_list->push_back(PropertyInfo(Variant::BOOL, "cell/center_x"));
  205. p_list->push_back(PropertyInfo(Variant::BOOL, "cell/center_y"));
  206. p_list->push_back(PropertyInfo(Variant::BOOL, "cell/center_z"));
  207. p_list->push_back(PropertyInfo(Variant::REAL, "cell/scale"));
  208. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  209. for (const Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  210. String base = "areas/" + itos(E->key()) + "/";
  211. p_list->push_back(PropertyInfo(Variant::_AABB, base + "bounds", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  212. p_list->push_back(PropertyInfo(Variant::STRING, base + "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  213. p_list->push_back(PropertyInfo(Variant::REAL, base + "disable_distance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  214. p_list->push_back(PropertyInfo(Variant::COLOR, base + "disable_color", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  215. p_list->push_back(PropertyInfo(Variant::BOOL, base + "exterior_portal", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  216. }
  217. }
  218. void GridMap::set_theme(const Ref<MeshLibrary> &p_theme) {
  219. if (!theme.is_null())
  220. theme->unregister_owner(this);
  221. theme = p_theme;
  222. if (!theme.is_null())
  223. theme->register_owner(this);
  224. _recreate_octant_data();
  225. _change_notify("theme");
  226. }
  227. Ref<MeshLibrary> GridMap::get_theme() const {
  228. return theme;
  229. }
  230. void GridMap::set_cell_size(float p_size) {
  231. cell_size = p_size;
  232. _recreate_octant_data();
  233. }
  234. float GridMap::get_cell_size() const {
  235. return cell_size;
  236. }
  237. void GridMap::set_octant_size(int p_size) {
  238. octant_size = p_size;
  239. _recreate_octant_data();
  240. }
  241. int GridMap::get_octant_size() const {
  242. return octant_size;
  243. }
  244. void GridMap::set_center_x(bool p_enable) {
  245. center_x = p_enable;
  246. _recreate_octant_data();
  247. }
  248. bool GridMap::get_center_x() const {
  249. return center_x;
  250. }
  251. void GridMap::set_center_y(bool p_enable) {
  252. center_y = p_enable;
  253. _recreate_octant_data();
  254. }
  255. bool GridMap::get_center_y() const {
  256. return center_y;
  257. }
  258. void GridMap::set_center_z(bool p_enable) {
  259. center_z = p_enable;
  260. _recreate_octant_data();
  261. }
  262. bool GridMap::get_center_z() const {
  263. return center_z;
  264. }
  265. int GridMap::_find_area(const IndexKey &p_pos) const {
  266. for (const Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  267. //this should somehow be faster...
  268. const Area &a = *E->get();
  269. if (p_pos.x >= a.from.x && p_pos.x < a.to.x &&
  270. p_pos.y >= a.from.y && p_pos.y < a.to.y &&
  271. p_pos.z >= a.from.z && p_pos.z < a.to.z) {
  272. return E->key();
  273. }
  274. }
  275. return 0;
  276. }
  277. void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
  278. ERR_FAIL_INDEX(ABS(p_x), 1 << 20);
  279. ERR_FAIL_INDEX(ABS(p_y), 1 << 20);
  280. ERR_FAIL_INDEX(ABS(p_z), 1 << 20);
  281. IndexKey key;
  282. key.x = p_x;
  283. key.y = p_y;
  284. key.z = p_z;
  285. OctantKey ok;
  286. ok.x = p_x / octant_size;
  287. ok.y = p_y / octant_size;
  288. ok.z = p_z / octant_size;
  289. ok.area = _find_area(key);
  290. if (cell_map.has(key)) {
  291. int prev_item = cell_map[key].item;
  292. OctantKey octantkey = ok;
  293. ERR_FAIL_COND(!octant_map.has(octantkey));
  294. Octant &g = *octant_map[octantkey];
  295. ERR_FAIL_COND(!g.items.has(prev_item));
  296. ERR_FAIL_COND(!g.items[prev_item].cells.has(key));
  297. g.items[prev_item].cells.erase(key);
  298. if (g.items[prev_item].cells.size() == 0) {
  299. VS::get_singleton()->free(g.items[prev_item].multimesh_instance);
  300. g.items.erase(prev_item);
  301. }
  302. if (g.items.empty() || !baked_lock) {
  303. //unbake just in case
  304. if (g.baked.is_valid()) {
  305. VS::get_singleton()->free(g.bake_instance);
  306. g.bake_instance = RID();
  307. g.baked = Ref<Mesh>();
  308. }
  309. }
  310. if (g.items.empty()) {
  311. PhysicsServer::get_singleton()->free(g.static_body);
  312. if (g.collision_debug.is_valid()) {
  313. PhysicsServer::get_singleton()->free(g.collision_debug);
  314. PhysicsServer::get_singleton()->free(g.collision_debug_instance);
  315. }
  316. memdelete(&g);
  317. octant_map.erase(octantkey);
  318. } else {
  319. g.dirty = true;
  320. }
  321. cell_map.erase(key);
  322. _queue_dirty_map();
  323. }
  324. if (p_item < 0)
  325. return;
  326. OctantKey octantkey = ok;
  327. //add later
  328. if (!octant_map.has(octantkey)) {
  329. Octant *g = memnew(Octant);
  330. g->dirty = true;
  331. g->static_body = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
  332. PhysicsServer::get_singleton()->body_attach_object_instance_ID(g->static_body, get_instance_ID());
  333. if (is_inside_world())
  334. PhysicsServer::get_singleton()->body_set_space(g->static_body, get_world()->get_space());
  335. SceneTree *st = SceneTree::get_singleton();
  336. if (st && st->is_debugging_collisions_hint()) {
  337. g->collision_debug = VisualServer::get_singleton()->mesh_create();
  338. g->collision_debug_instance = VisualServer::get_singleton()->instance_create();
  339. VisualServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  340. if (is_inside_world()) {
  341. VisualServer::get_singleton()->instance_set_scenario(g->collision_debug_instance, get_world()->get_scenario());
  342. VisualServer::get_singleton()->instance_set_transform(g->collision_debug_instance, get_global_transform());
  343. }
  344. }
  345. octant_map[octantkey] = g;
  346. }
  347. Octant &g = *octant_map[octantkey];
  348. if (!g.items.has(p_item)) {
  349. Octant::ItemInstances ii;
  350. if (theme.is_valid() && theme->has_item(p_item)) {
  351. ii.mesh = theme->get_item_mesh(p_item);
  352. ii.shape = theme->get_item_shape(p_item);
  353. ii.navmesh = theme->get_item_navmesh(p_item);
  354. }
  355. ii.multimesh = Ref<MultiMesh>(memnew(MultiMesh));
  356. ii.multimesh->set_mesh(ii.mesh);
  357. ii.multimesh_instance = VS::get_singleton()->instance_create();
  358. VS::get_singleton()->instance_set_base(ii.multimesh_instance, ii.multimesh->get_rid());
  359. VS::get_singleton()->instance_geometry_set_baked_light(ii.multimesh_instance, baked_light_instance ? baked_light_instance->get_baked_light_instance() : RID());
  360. if (!baked_lock) {
  361. //unbake just in case
  362. if (g.bake_instance.is_valid())
  363. VS::get_singleton()->free(g.bake_instance);
  364. g.baked = Ref<Mesh>();
  365. if (is_inside_world()) {
  366. VS::get_singleton()->instance_set_scenario(ii.multimesh_instance, get_world()->get_scenario());
  367. if (ok.area) {
  368. VS::get_singleton()->instance_set_room(ii.multimesh_instance, area_map[ok.area]->instance);
  369. }
  370. }
  371. }
  372. g.items[p_item] = ii;
  373. }
  374. Octant::ItemInstances &ii = g.items[p_item];
  375. ii.cells.insert(key);
  376. g.dirty = true;
  377. _queue_dirty_map();
  378. cell_map[key] = Cell();
  379. Cell &c = cell_map[key];
  380. c.item = p_item;
  381. c.rot = p_rot;
  382. }
  383. int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
  384. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, INVALID_CELL_ITEM);
  385. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, INVALID_CELL_ITEM);
  386. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, INVALID_CELL_ITEM);
  387. IndexKey key;
  388. key.x = p_x;
  389. key.y = p_y;
  390. key.z = p_z;
  391. if (!cell_map.has(key))
  392. return INVALID_CELL_ITEM;
  393. return cell_map[key].item;
  394. }
  395. int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const {
  396. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, -1);
  397. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, -1);
  398. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, -1);
  399. IndexKey key;
  400. key.x = p_x;
  401. key.y = p_y;
  402. key.z = p_z;
  403. if (!cell_map.has(key))
  404. return -1;
  405. return cell_map[key].rot;
  406. }
  407. void GridMap::_octant_enter_tree(const OctantKey &p_key) {
  408. ERR_FAIL_COND(!octant_map.has(p_key));
  409. if (navigation) {
  410. Octant &g = *octant_map[p_key];
  411. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  412. _octant_clear_navmesh(p_key);
  413. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  414. Octant::ItemInstances &ii = E->get();
  415. for (Set<IndexKey>::Element *F = ii.cells.front(); F; F = F->next()) {
  416. IndexKey ik = F->get();
  417. Map<IndexKey, Cell>::Element *C = cell_map.find(ik);
  418. ERR_CONTINUE(!C);
  419. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  420. Transform xform;
  421. if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
  422. xform.basis.set_zero();
  423. } else {
  424. xform.basis.set_orthogonal_index(C->get().rot);
  425. }
  426. xform.set_origin(cellpos * cell_size + ofs);
  427. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  428. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  429. if (ii.navmesh.is_valid()) {
  430. int nm_id = navigation->navmesh_create(ii.navmesh, xform, this);
  431. Octant::NavMesh nm;
  432. nm.id = nm_id;
  433. nm.xform = xform;
  434. g.navmesh_ids[ik] = nm;
  435. }
  436. }
  437. }
  438. }
  439. }
  440. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  441. ERR_FAIL_COND(!octant_map.has(p_key));
  442. Octant &g = *octant_map[p_key];
  443. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  444. PhysicsServer::get_singleton()->body_set_space(g.static_body, get_world()->get_space());
  445. //print_line("BODYPOS: "+get_global_transform());
  446. if (g.collision_debug_instance.is_valid()) {
  447. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world()->get_scenario());
  448. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  449. if (area_map.has(p_key.area)) {
  450. VS::get_singleton()->instance_set_room(g.collision_debug_instance, area_map[p_key.area]->instance);
  451. }
  452. }
  453. if (g.baked.is_valid()) {
  454. Transform xf = get_global_transform();
  455. xf.translate(_octant_get_offset(p_key));
  456. VS::get_singleton()->instance_set_transform(g.bake_instance, xf);
  457. VS::get_singleton()->instance_set_scenario(g.bake_instance, get_world()->get_scenario());
  458. if (area_map.has(p_key.area)) {
  459. VS::get_singleton()->instance_set_room(g.bake_instance, area_map[p_key.area]->instance);
  460. }
  461. } else {
  462. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  463. VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance, get_world()->get_scenario());
  464. VS::get_singleton()->instance_set_transform(E->get().multimesh_instance, get_global_transform());
  465. //print_line("INSTANCEPOS: "+get_global_transform());
  466. if (area_map.has(p_key.area)) {
  467. VS::get_singleton()->instance_set_room(E->get().multimesh_instance, area_map[p_key.area]->instance);
  468. }
  469. }
  470. }
  471. }
  472. void GridMap::_octant_transform(const OctantKey &p_key) {
  473. ERR_FAIL_COND(!octant_map.has(p_key));
  474. Octant &g = *octant_map[p_key];
  475. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  476. if (g.collision_debug_instance.is_valid()) {
  477. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  478. }
  479. if (g.baked.is_valid()) {
  480. Transform xf = get_global_transform();
  481. xf.origin += _octant_get_offset(p_key);
  482. VS::get_singleton()->instance_set_transform(g.bake_instance, xf);
  483. } else {
  484. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  485. VS::get_singleton()->instance_set_transform(E->get().multimesh_instance, get_global_transform());
  486. //print_line("UPDATEPOS: "+get_global_transform());
  487. }
  488. }
  489. }
  490. void GridMap::_octant_clear_navmesh(const OctantKey &p_key) {
  491. Octant &g = *octant_map[p_key];
  492. if (navigation) {
  493. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  494. Octant::NavMesh *nvm = &E->get();
  495. if (nvm && nvm->id) {
  496. navigation->navmesh_remove(E->get().id);
  497. }
  498. }
  499. g.navmesh_ids.clear();
  500. }
  501. }
  502. void GridMap::_octant_update(const OctantKey &p_key) {
  503. ERR_FAIL_COND(!octant_map.has(p_key));
  504. Octant &g = *octant_map[p_key];
  505. if (!g.dirty)
  506. return;
  507. Ref<Mesh> mesh;
  508. _octant_clear_navmesh(p_key);
  509. PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
  510. if (g.collision_debug.is_valid()) {
  511. VS::get_singleton()->mesh_clear(g.collision_debug);
  512. }
  513. DVector<Vector3> col_debug;
  514. /*
  515. * foreach item in this octant,
  516. * set item's multimesh's instance count to number of cells which have this item
  517. * and set said multimesh bounding box to one containing all cells which have this item
  518. */
  519. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  520. Octant::ItemInstances &ii = E->get();
  521. ii.multimesh->set_instance_count(ii.cells.size());
  522. AABB aabb;
  523. AABB mesh_aabb = ii.mesh.is_null() ? AABB() : ii.mesh->get_aabb();
  524. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  525. //print_line("OCTANT, CELLS: "+itos(ii.cells.size()));
  526. int idx = 0;
  527. // foreach cell containing this item type
  528. for (Set<IndexKey>::Element *F = ii.cells.front(); F; F = F->next()) {
  529. IndexKey ik = F->get();
  530. Map<IndexKey, Cell>::Element *C = cell_map.find(ik);
  531. ERR_CONTINUE(!C);
  532. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  533. Transform xform;
  534. if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
  535. xform.basis.set_zero();
  536. } else {
  537. xform.basis.set_orthogonal_index(C->get().rot);
  538. }
  539. xform.set_origin(cellpos * cell_size + ofs);
  540. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  541. ii.multimesh->set_instance_transform(idx, xform);
  542. //ii.multimesh->set_instance_transform(idx,Transform() );
  543. ii.multimesh->set_instance_color(idx, Color(1, 1, 1, 1));
  544. //print_line("MMINST: "+xform);
  545. if (idx == 0) {
  546. aabb = xform.xform(mesh_aabb);
  547. } else {
  548. aabb.merge_with(xform.xform(mesh_aabb));
  549. }
  550. // add the item's shape at given xform to octant's static_body
  551. if (ii.shape.is_valid()) {
  552. // add the item's shape
  553. PhysicsServer::get_singleton()->body_add_shape(g.static_body, ii.shape->get_rid(), xform);
  554. if (g.collision_debug.is_valid()) {
  555. ii.shape->add_vertices_to_array(col_debug, xform);
  556. }
  557. // print_line("PHIS x: "+xform);
  558. }
  559. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  560. if (navigation) {
  561. if (ii.navmesh.is_valid()) {
  562. int nm_id = navigation->navmesh_create(ii.navmesh, xform, this);
  563. Octant::NavMesh nm;
  564. nm.id = nm_id;
  565. nm.xform = xform;
  566. g.navmesh_ids[ik] = nm;
  567. }
  568. }
  569. idx++;
  570. }
  571. ii.multimesh->set_aabb(aabb);
  572. }
  573. if (col_debug.size()) {
  574. Array arr;
  575. arr.resize(VS::ARRAY_MAX);
  576. arr[VS::ARRAY_VERTEX] = col_debug;
  577. VS::get_singleton()->mesh_add_surface(g.collision_debug, VS::PRIMITIVE_LINES, arr);
  578. SceneTree *st = SceneTree::get_singleton();
  579. if (st) {
  580. VS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  581. }
  582. }
  583. g.dirty = false;
  584. }
  585. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  586. ERR_FAIL_COND(!octant_map.has(p_key));
  587. Octant &g = *octant_map[p_key];
  588. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  589. PhysicsServer::get_singleton()->body_set_space(g.static_body, RID());
  590. if (g.baked.is_valid()) {
  591. VS::get_singleton()->instance_set_room(g.bake_instance, RID());
  592. VS::get_singleton()->instance_set_scenario(g.bake_instance, RID());
  593. }
  594. if (g.collision_debug_instance.is_valid()) {
  595. VS::get_singleton()->instance_set_room(g.collision_debug_instance, RID());
  596. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  597. }
  598. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  599. VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance, RID());
  600. // VS::get_singleton()->instance_set_transform(E->get().multimesh_instance,get_global_transform());
  601. VS::get_singleton()->instance_set_room(E->get().multimesh_instance, RID());
  602. }
  603. }
  604. void GridMap::_octant_clear_baked(const OctantKey &p_key) {
  605. ERR_FAIL_COND(!octant_map.has(p_key));
  606. Octant &g = *octant_map[p_key];
  607. if (!g.baked.is_valid())
  608. return;
  609. VS::get_singleton()->free(g.bake_instance);
  610. g.bake_instance = RID();
  611. g.baked = Ref<Mesh>();
  612. if (is_inside_tree())
  613. _octant_enter_world(p_key);
  614. g.dirty = true;
  615. _queue_dirty_map();
  616. }
  617. void GridMap::_octant_bake(const OctantKey &p_key, const Ref<TriangleMesh> &p_tmesh, const Vector<BakeLight> &p_lights, List<Vector3> *p_prebake) {
  618. ERR_FAIL_COND(!octant_map.has(p_key));
  619. Octant &g = *octant_map[p_key];
  620. Ref<TriangleMesh> tm = p_tmesh;
  621. if (!p_prebake && is_inside_world())
  622. _octant_exit_world(p_key);
  623. Map<Ref<Material>, Ref<SurfaceTool> > surfaces;
  624. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  625. Vector3 octant_ofs = _octant_get_offset(p_key);
  626. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  627. Octant::ItemInstances &ii = E->get();
  628. if (ii.mesh.is_null())
  629. continue;
  630. for (Set<IndexKey>::Element *F = ii.cells.front(); F; F = F->next()) {
  631. IndexKey ik = F->get();
  632. Map<IndexKey, Cell>::Element *C = cell_map.find(ik);
  633. ERR_CONTINUE(!C);
  634. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  635. Transform xform;
  636. xform.basis.set_orthogonal_index(C->get().rot);
  637. xform.set_origin(cellpos * cell_size + ofs);
  638. if (!p_prebake)
  639. xform.origin -= octant_ofs;
  640. for (int i = 0; i < ii.mesh->get_surface_count(); i++) {
  641. if (p_prebake) {
  642. if (ii.mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  643. continue;
  644. Array a = ii.mesh->surface_get_arrays(i);
  645. DVector<Vector3> av = a[VS::ARRAY_VERTEX];
  646. int avs = av.size();
  647. DVector<Vector3>::Read vr = av.read();
  648. DVector<int> ai = a[VS::ARRAY_INDEX];
  649. int ais = ai.size();
  650. if (ais) {
  651. DVector<int>::Read ir = ai.read();
  652. for (int j = 0; j < ais; j++) {
  653. p_prebake->push_back(xform.xform(vr[ir[j]]));
  654. //print_line("V SET: "+xform.xform(vr[ir[j]]));
  655. }
  656. } else {
  657. for (int j = 0; j < avs; j++) {
  658. p_prebake->push_back(xform.xform(vr[j]));
  659. }
  660. }
  661. } else {
  662. Ref<Material> m = ii.mesh->surface_get_material(i);
  663. Map<Ref<Material>, Ref<SurfaceTool> >::Element *S = surfaces.find(m);
  664. if (!S) {
  665. S = surfaces.insert(m, Ref<SurfaceTool>(memnew(SurfaceTool)));
  666. }
  667. Ref<SurfaceTool> st = S->get();
  668. List<SurfaceTool::Vertex>::Element *V = st->get_vertex_array().back();
  669. st->append_from(ii.mesh, i, xform);
  670. st->set_material(m);
  671. if (tm.is_valid()) {
  672. if (V)
  673. V = V->next();
  674. else
  675. V = st->get_vertex_array().front();
  676. int lc = p_lights.size();
  677. const BakeLight *bl = p_lights.ptr();
  678. float ofs = cell_size * 0.02;
  679. for (; V; V = V->next()) {
  680. SurfaceTool::Vertex &v = V->get();
  681. Vector3 vertex = v.vertex + octant_ofs;
  682. //print_line("V GET: "+vertex);
  683. Vector3 normal = tm->get_area_normal(AABB(Vector3(-ofs, -ofs, -ofs) + vertex, Vector3(ofs, ofs, ofs) * 2.0));
  684. if (normal == Vector3()) {
  685. print_line("couldn't find for vertex: " + vertex);
  686. }
  687. ERR_CONTINUE(normal == Vector3());
  688. float max_l = 1.0;
  689. float max_dist = 1.0;
  690. if (lc) {
  691. for (int j = 0; j < lc; j++) {
  692. const BakeLight &l = bl[j];
  693. switch (l.type) {
  694. case VS::LIGHT_DIRECTIONAL: {
  695. Vector3 ray_from = vertex + normal * ofs;
  696. Vector3 ray_to = l.dir * 5000;
  697. Vector3 n;
  698. Vector3 p;
  699. if (tm->intersect_segment(ray_from, ray_to, p, n)) {
  700. float dist = 1.0 - l.param[VS::LIGHT_PARAM_SHADOW_DARKENING];
  701. if (dist <= max_dist) {
  702. max_dist = dist;
  703. max_l = 1.0 - dist;
  704. }
  705. }
  706. } break;
  707. }
  708. }
  709. }
  710. v.color = Color(max_l, max_l, max_l, 1.0);
  711. }
  712. st->add_to_format(VS::ARRAY_FORMAT_COLOR);
  713. if (m.is_valid()) {
  714. Ref<FixedMaterial> fm = m;
  715. if (fm.is_valid())
  716. fm->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  717. }
  718. }
  719. }
  720. }
  721. }
  722. }
  723. if (p_prebake)
  724. return;
  725. g.baked = Ref<Mesh>(memnew(Mesh));
  726. for (Map<Ref<Material>, Ref<SurfaceTool> >::Element *E = surfaces.front(); E; E = E->next()) {
  727. Ref<SurfaceTool> st = E->get();
  728. st->commit(g.baked);
  729. }
  730. g.bake_instance = VS::get_singleton()->instance_create();
  731. VS::get_singleton()->instance_set_base(g.bake_instance, g.baked->get_rid());
  732. if (is_inside_world())
  733. _octant_enter_world(p_key);
  734. g.dirty = true;
  735. _queue_dirty_map();
  736. }
  737. void GridMap::_notification(int p_what) {
  738. switch (p_what) {
  739. case NOTIFICATION_ENTER_WORLD: {
  740. _update_area_instances();
  741. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  742. // IndexKey ik;
  743. // ik.key = E->key().indexkey;
  744. _octant_enter_world(E->key());
  745. _octant_update(E->key());
  746. }
  747. awaiting_update = false;
  748. last_transform = get_global_transform();
  749. if (use_baked_light) {
  750. _find_baked_light();
  751. }
  752. } break;
  753. case NOTIFICATION_TRANSFORM_CHANGED: {
  754. Transform new_xform = get_global_transform();
  755. if (new_xform == last_transform)
  756. break;
  757. //update run
  758. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  759. _octant_transform(E->key());
  760. }
  761. last_transform = new_xform;
  762. } break;
  763. case NOTIFICATION_EXIT_WORLD: {
  764. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  765. _octant_exit_world(E->key());
  766. }
  767. if (use_baked_light) {
  768. if (baked_light_instance) {
  769. baked_light_instance->disconnect(SceneStringNames::get_singleton()->baked_light_changed, this, SceneStringNames::get_singleton()->_baked_light_changed);
  770. baked_light_instance = NULL;
  771. }
  772. _baked_light_changed();
  773. }
  774. //_queue_dirty_map(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  775. //_update_dirty_map_callback();
  776. //_update_area_instances();
  777. } break;
  778. case NOTIFICATION_ENTER_TREE: {
  779. Spatial *c = this;
  780. while (c) {
  781. navigation = c->cast_to<Navigation>();
  782. if (navigation) {
  783. break;
  784. }
  785. c = c->get_parent()->cast_to<Spatial>();
  786. }
  787. if (navigation) {
  788. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  789. if (navigation) {
  790. _octant_enter_tree(E->key());
  791. }
  792. }
  793. }
  794. _queue_dirty_map();
  795. } break;
  796. case NOTIFICATION_EXIT_TREE: {
  797. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  798. if (navigation) {
  799. _octant_clear_navmesh(E->key());
  800. }
  801. }
  802. navigation = NULL;
  803. } break;
  804. }
  805. }
  806. void GridMap::_queue_dirty_map() {
  807. if (awaiting_update)
  808. return;
  809. if (is_inside_world()) {
  810. MessageQueue::get_singleton()->push_call(this, "_update_dirty_map_callback");
  811. awaiting_update = true;
  812. }
  813. }
  814. void GridMap::_recreate_octant_data() {
  815. Map<IndexKey, Cell> cell_copy = cell_map;
  816. _clear_internal(true);
  817. for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
  818. set_cell_item(E->key().x, E->key().y, E->key().z, E->get().item, E->get().rot);
  819. }
  820. }
  821. void GridMap::_clear_internal(bool p_keep_areas) {
  822. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  823. if (is_inside_world())
  824. _octant_exit_world(E->key());
  825. for (Map<int, Octant::ItemInstances>::Element *F = E->get()->items.front(); F; F = F->next()) {
  826. VS::get_singleton()->free(F->get().multimesh_instance);
  827. }
  828. //unbake just in case
  829. if (E->get()->bake_instance.is_valid())
  830. VS::get_singleton()->free(E->get()->bake_instance);
  831. if (E->get()->collision_debug.is_valid())
  832. VS::get_singleton()->free(E->get()->collision_debug);
  833. if (E->get()->collision_debug_instance.is_valid())
  834. VS::get_singleton()->free(E->get()->collision_debug_instance);
  835. PhysicsServer::get_singleton()->free(E->get()->static_body);
  836. memdelete(E->get());
  837. }
  838. octant_map.clear();
  839. cell_map.clear();
  840. if (p_keep_areas)
  841. return;
  842. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  843. VS::get_singleton()->free(E->get()->base_portal);
  844. VS::get_singleton()->free(E->get()->instance);
  845. for (int i = 0; i < E->get()->portals.size(); i++) {
  846. VS::get_singleton()->free(E->get()->portals[i].instance);
  847. }
  848. memdelete(E->get());
  849. }
  850. }
  851. void GridMap::clear() {
  852. _clear_internal();
  853. }
  854. void GridMap::resource_changed(const RES &p_res) {
  855. _recreate_octant_data();
  856. }
  857. void GridMap::_update_dirty_map_callback() {
  858. if (!awaiting_update)
  859. return;
  860. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  861. _octant_update(E->key());
  862. }
  863. awaiting_update = false;
  864. }
  865. void GridMap::_bind_methods() {
  866. ObjectTypeDB::bind_method(_MD("set_theme", "theme:MeshLibrary"), &GridMap::set_theme);
  867. ObjectTypeDB::bind_method(_MD("get_theme:MeshLibrary"), &GridMap::get_theme);
  868. ObjectTypeDB::bind_method(_MD("set_bake", "enable"), &GridMap::set_bake);
  869. ObjectTypeDB::bind_method(_MD("is_baking_enabled"), &GridMap::is_baking_enabled);
  870. ObjectTypeDB::bind_method(_MD("set_cell_size", "size"), &GridMap::set_cell_size);
  871. ObjectTypeDB::bind_method(_MD("get_cell_size"), &GridMap::get_cell_size);
  872. ObjectTypeDB::bind_method(_MD("set_octant_size", "size"), &GridMap::set_octant_size);
  873. ObjectTypeDB::bind_method(_MD("get_octant_size"), &GridMap::get_octant_size);
  874. ObjectTypeDB::bind_method(_MD("set_cell_item", "x", "y", "z", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  875. ObjectTypeDB::bind_method(_MD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item);
  876. ObjectTypeDB::bind_method(_MD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
  877. // ObjectTypeDB::bind_method(_MD("_recreate_octants"),&GridMap::_recreate_octants);
  878. ObjectTypeDB::bind_method(_MD("_update_dirty_map_callback"), &GridMap::_update_dirty_map_callback);
  879. ObjectTypeDB::bind_method(_MD("resource_changed", "resource"), &GridMap::resource_changed);
  880. ObjectTypeDB::bind_method(_MD("set_center_x", "enable"), &GridMap::set_center_x);
  881. ObjectTypeDB::bind_method(_MD("get_center_x"), &GridMap::get_center_x);
  882. ObjectTypeDB::bind_method(_MD("set_center_y", "enable"), &GridMap::set_center_y);
  883. ObjectTypeDB::bind_method(_MD("get_center_y"), &GridMap::get_center_y);
  884. ObjectTypeDB::bind_method(_MD("set_center_z", "enable"), &GridMap::set_center_z);
  885. ObjectTypeDB::bind_method(_MD("get_center_z"), &GridMap::get_center_z);
  886. ObjectTypeDB::bind_method(_MD("set_clip", "enabled", "clipabove", "floor", "axis"), &GridMap::set_clip, DEFVAL(true), DEFVAL(0), DEFVAL(Vector3::AXIS_X));
  887. ObjectTypeDB::bind_method(_MD("create_area", "id", "area"), &GridMap::create_area);
  888. ObjectTypeDB::bind_method(_MD("area_get_bounds", "area", "bounds"), &GridMap::area_get_bounds);
  889. ObjectTypeDB::bind_method(_MD("area_set_exterior_portal", "area", "enable"), &GridMap::area_set_exterior_portal);
  890. ObjectTypeDB::bind_method(_MD("area_set_name", "area", "name"), &GridMap::area_set_name);
  891. ObjectTypeDB::bind_method(_MD("area_get_name", "area"), &GridMap::area_get_name);
  892. ObjectTypeDB::bind_method(_MD("area_is_exterior_portal", "area"), &GridMap::area_is_exterior_portal);
  893. ObjectTypeDB::bind_method(_MD("area_set_portal_disable_distance", "area", "distance"), &GridMap::area_set_portal_disable_distance);
  894. ObjectTypeDB::bind_method(_MD("area_get_portal_disable_distance", "area"), &GridMap::area_get_portal_disable_distance);
  895. ObjectTypeDB::bind_method(_MD("area_set_portal_disable_color", "area", "color"), &GridMap::area_set_portal_disable_color);
  896. ObjectTypeDB::bind_method(_MD("area_get_portal_disable_color", "area"), &GridMap::area_get_portal_disable_color);
  897. ObjectTypeDB::bind_method(_MD("erase_area", "area"), &GridMap::erase_area);
  898. ObjectTypeDB::bind_method(_MD("get_unused_area_id", "area"), &GridMap::get_unused_area_id);
  899. ObjectTypeDB::bind_method(_MD("bake_geometry"), &GridMap::bake_geometry);
  900. ObjectTypeDB::bind_method(_MD("_baked_light_changed"), &GridMap::_baked_light_changed);
  901. ObjectTypeDB::bind_method(_MD("set_use_baked_light", "use"), &GridMap::set_use_baked_light);
  902. ObjectTypeDB::bind_method(_MD("is_using_baked_light", "use"), &GridMap::is_using_baked_light);
  903. ObjectTypeDB::bind_method(_MD("_get_baked_light_meshes"), &GridMap::_get_baked_light_meshes);
  904. ObjectTypeDB::set_method_flags("GridMap", "bake_geometry", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  905. ObjectTypeDB::bind_method(_MD("clear"), &GridMap::clear);
  906. BIND_CONSTANT(INVALID_CELL_ITEM);
  907. }
  908. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  909. if (!p_enabled && !clip)
  910. return;
  911. if (clip && p_enabled && clip_floor == p_floor && p_clip_above == clip_above && p_axis == clip_axis)
  912. return;
  913. clip = p_enabled;
  914. clip_floor = p_floor;
  915. clip_axis = p_axis;
  916. clip_above = p_clip_above;
  917. //make it all update
  918. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  919. Octant *g = E->get();
  920. g->dirty = true;
  921. }
  922. awaiting_update = true;
  923. _update_dirty_map_callback();
  924. }
  925. void GridMap::_update_areas() {
  926. //clear the portals
  927. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  928. //this should somehow be faster...
  929. Area &a = *E->get();
  930. a.portals.clear();
  931. if (a.instance.is_valid()) {
  932. VisualServer::get_singleton()->free(a.instance);
  933. a.instance = RID();
  934. }
  935. }
  936. //test all areas against all areas and create portals - this sucks (slow :( )
  937. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  938. Area &a = *E->get();
  939. if (a.exterior_portal) //that's pretty much all it does... yes it is
  940. continue;
  941. Vector3 from_a(a.from.x, a.from.y, a.from.z);
  942. Vector3 to_a(a.to.x, a.to.y, a.to.z);
  943. for (Map<int, Area *>::Element *F = area_map.front(); F; F = F->next()) {
  944. Area &b = *F->get();
  945. Vector3 from_b(b.from.x, b.from.y, b.from.z);
  946. Vector3 to_b(b.to.x, b.to.y, b.to.z);
  947. // initially test intersection and discards
  948. int axis = -1;
  949. float sign = 0;
  950. bool valid = true;
  951. Vector3 axmin, axmax;
  952. for (int i = 0; i < 3; i++) {
  953. if (from_a[i] == to_b[i]) {
  954. if (axis != -1) {
  955. valid = false;
  956. break;
  957. }
  958. axis = i;
  959. sign = -1;
  960. } else if (from_b[i] == to_a[i]) {
  961. if (axis != -1) {
  962. valid = false;
  963. break;
  964. }
  965. axis = i;
  966. sign = +1;
  967. }
  968. if (from_a[i] > to_b[i] || to_a[i] < from_b[i]) {
  969. valid = false;
  970. break;
  971. } else {
  972. axmin[i] = (from_a[i] > from_b[i]) ? from_a[i] : from_b[i];
  973. axmax[i] = (to_a[i] < to_b[i]) ? to_a[i] : to_b[i];
  974. }
  975. }
  976. if (axis == -1 || !valid)
  977. continue;
  978. Transform xf;
  979. for (int i = 0; i < 3; i++) {
  980. int ax = (axis + i) % 3;
  981. Vector3 axis_vec;
  982. float scale = (i == 0) ? sign : ((axmax[ax] - axmin[ax]) * cell_size);
  983. axis_vec[ax] = scale;
  984. xf.basis.set_axis((2 + i) % 3, axis_vec);
  985. xf.origin[i] = axmin[i] * cell_size;
  986. }
  987. Area::Portal portal;
  988. portal.xform = xf;
  989. a.portals.push_back(portal);
  990. }
  991. }
  992. _update_area_instances();
  993. }
  994. void GridMap::_update_area_instances() {
  995. Transform base_xform;
  996. if (_in_tree)
  997. base_xform = get_global_transform();
  998. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  999. //this should somehow be faster...
  1000. Area &a = *E->get();
  1001. if (a.instance.is_valid() != _in_tree) {
  1002. if (!_in_tree) {
  1003. for (int i = 0; i < a.portals.size(); i++) {
  1004. Area::Portal &p = a.portals[i];
  1005. ERR_CONTINUE(!p.instance.is_valid());
  1006. VisualServer::get_singleton()->free(p.instance);
  1007. p.instance = RID();
  1008. }
  1009. VisualServer::get_singleton()->free(a.instance);
  1010. a.instance = RID();
  1011. } else {
  1012. //a.instance = VisualServer::get_singleton()->instance_create2(base_room,get_world()->get_scenario());
  1013. for (int i = 0; i < a.portals.size(); i++) {
  1014. Area::Portal &p = a.portals[i];
  1015. ERR_CONTINUE(p.instance.is_valid());
  1016. p.instance = VisualServer::get_singleton()->instance_create2(a.base_portal, get_world()->get_scenario());
  1017. VisualServer::get_singleton()->instance_set_room(p.instance, a.instance);
  1018. }
  1019. }
  1020. }
  1021. if (a.instance.is_valid()) {
  1022. Transform xform;
  1023. Vector3 from_a(a.from.x, a.from.y, a.from.z);
  1024. Vector3 to_a(a.to.x, a.to.y, a.to.z);
  1025. for (int i = 0; i < 3; i++) {
  1026. xform.origin[i] = from_a[i] * cell_size;
  1027. Vector3 s;
  1028. s[i] = (to_a[i] - from_a[i]) * cell_size;
  1029. xform.basis.set_axis(i, s);
  1030. }
  1031. VisualServer::get_singleton()->instance_set_transform(a.instance, base_xform * xform);
  1032. for (int i = 0; i < a.portals.size(); i++) {
  1033. Area::Portal &p = a.portals[i];
  1034. ERR_CONTINUE(!p.instance.is_valid());
  1035. VisualServer::get_singleton()->instance_set_transform(p.instance, base_xform * xform);
  1036. }
  1037. }
  1038. }
  1039. }
  1040. Error GridMap::create_area(int p_id, const AABB &p_bounds) {
  1041. ERR_FAIL_COND_V(area_map.has(p_id), ERR_ALREADY_EXISTS);
  1042. ERR_EXPLAIN("ID 0 is taken as global area, start from 1");
  1043. ERR_FAIL_COND_V(p_id == 0, ERR_INVALID_PARAMETER);
  1044. ERR_FAIL_COND_V(p_bounds.has_no_area(), ERR_INVALID_PARAMETER);
  1045. // FIRST VALIDATE AREA
  1046. IndexKey from, to;
  1047. from.x = p_bounds.pos.x;
  1048. from.y = p_bounds.pos.y;
  1049. from.z = p_bounds.pos.z;
  1050. to.x = p_bounds.pos.x + p_bounds.size.x;
  1051. to.y = p_bounds.pos.y + p_bounds.size.y;
  1052. to.z = p_bounds.pos.z + p_bounds.size.z;
  1053. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  1054. //this should somehow be faster...
  1055. Area &a = *E->get();
  1056. //does it interset with anything else?
  1057. if (from.x >= a.to.x ||
  1058. to.x <= a.from.x ||
  1059. from.y >= a.to.y ||
  1060. to.y <= a.from.y ||
  1061. from.z >= a.to.z ||
  1062. to.z <= a.from.z) {
  1063. // all good
  1064. } else {
  1065. return ERR_INVALID_PARAMETER;
  1066. }
  1067. }
  1068. Area *area = memnew(Area);
  1069. area->from = from;
  1070. area->to = to;
  1071. area->portal_disable_distance = 0;
  1072. area->exterior_portal = false;
  1073. area->name = "Area " + itos(p_id);
  1074. area_map[p_id] = area;
  1075. _recreate_octant_data();
  1076. return OK;
  1077. }
  1078. AABB GridMap::area_get_bounds(int p_area) const {
  1079. ERR_FAIL_COND_V(!area_map.has(p_area), AABB());
  1080. const Area *a = area_map[p_area];
  1081. AABB aabb;
  1082. aabb.pos = Vector3(a->from.x, a->from.y, a->from.z);
  1083. aabb.size = Vector3(a->to.x, a->to.y, a->to.z) - aabb.pos;
  1084. return aabb;
  1085. }
  1086. void GridMap::area_set_name(int p_area, const String &p_name) {
  1087. ERR_FAIL_COND(!area_map.has(p_area));
  1088. Area *a = area_map[p_area];
  1089. a->name = p_name;
  1090. }
  1091. String GridMap::area_get_name(int p_area) const {
  1092. ERR_FAIL_COND_V(!area_map.has(p_area), "");
  1093. const Area *a = area_map[p_area];
  1094. return a->name;
  1095. }
  1096. void GridMap::area_set_exterior_portal(int p_area, bool p_enable) {
  1097. ERR_FAIL_COND(!area_map.has(p_area));
  1098. Area *a = area_map[p_area];
  1099. if (a->exterior_portal == p_enable)
  1100. return;
  1101. a->exterior_portal = p_enable;
  1102. _recreate_octant_data();
  1103. }
  1104. bool GridMap::area_is_exterior_portal(int p_area) const {
  1105. ERR_FAIL_COND_V(!area_map.has(p_area), false);
  1106. const Area *a = area_map[p_area];
  1107. return a->exterior_portal;
  1108. }
  1109. void GridMap::area_set_portal_disable_distance(int p_area, float p_distance) {
  1110. ERR_FAIL_COND(!area_map.has(p_area));
  1111. Area *a = area_map[p_area];
  1112. a->portal_disable_distance = p_distance;
  1113. }
  1114. float GridMap::area_get_portal_disable_distance(int p_area) const {
  1115. ERR_FAIL_COND_V(!area_map.has(p_area), 0);
  1116. const Area *a = area_map[p_area];
  1117. return a->portal_disable_distance;
  1118. }
  1119. void GridMap::area_set_portal_disable_color(int p_area, Color p_color) {
  1120. ERR_FAIL_COND(!area_map.has(p_area));
  1121. Area *a = area_map[p_area];
  1122. a->portal_disable_color = p_color;
  1123. }
  1124. Color GridMap::area_get_portal_disable_color(int p_area) const {
  1125. ERR_FAIL_COND_V(!area_map.has(p_area), Color());
  1126. const Area *a = area_map[p_area];
  1127. return a->portal_disable_color;
  1128. }
  1129. void GridMap::get_area_list(List<int> *p_areas) const {
  1130. for (const Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  1131. p_areas->push_back(E->key());
  1132. }
  1133. }
  1134. GridMap::Area::Portal::~Portal() {
  1135. if (instance.is_valid())
  1136. VisualServer::get_singleton()->free(instance);
  1137. }
  1138. GridMap::Area::Area() {
  1139. base_portal = VisualServer::get_singleton()->portal_create();
  1140. Vector<Point2> points;
  1141. points.push_back(Point2(0, 1));
  1142. points.push_back(Point2(1, 1));
  1143. points.push_back(Point2(1, 0));
  1144. points.push_back(Point2(0, 0));
  1145. VisualServer::get_singleton()->portal_set_shape(base_portal, points);
  1146. }
  1147. GridMap::Area::~Area() {
  1148. if (instance.is_valid())
  1149. VisualServer::get_singleton()->free(instance);
  1150. VisualServer::get_singleton()->free(base_portal);
  1151. }
  1152. void GridMap::erase_area(int p_area) {
  1153. ERR_FAIL_COND(!area_map.has(p_area));
  1154. Area *a = area_map[p_area];
  1155. memdelete(a);
  1156. area_map.erase(p_area);
  1157. _recreate_octant_data();
  1158. }
  1159. int GridMap::get_unused_area_id() const {
  1160. if (area_map.empty())
  1161. return 1;
  1162. else
  1163. return area_map.back()->key() + 1;
  1164. }
  1165. void GridMap::set_bake(bool p_bake) {
  1166. bake = p_bake;
  1167. if (bake == false) {
  1168. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  1169. _octant_clear_baked(E->key());
  1170. }
  1171. }
  1172. }
  1173. bool GridMap::is_baking_enabled() const {
  1174. return bake;
  1175. }
  1176. void GridMap::set_cell_scale(float p_scale) {
  1177. cell_scale = p_scale;
  1178. _queue_dirty_map();
  1179. }
  1180. float GridMap::get_cell_scale() const {
  1181. return cell_scale;
  1182. }
  1183. void GridMap::bake_geometry() {
  1184. //used to compute vertex occlusion
  1185. Ref<TriangleMesh> tmesh;
  1186. Vector<BakeLight> lights;
  1187. if (true) {
  1188. List<Vector3> vertices;
  1189. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  1190. _octant_bake(E->key(), tmesh, lights, &vertices);
  1191. }
  1192. DVector<Vector3> vv;
  1193. vv.fill_with(vertices);
  1194. //print_line("TOTAL VERTICES: "+itos(vv.size()));
  1195. tmesh = Ref<TriangleMesh>(memnew(TriangleMesh));
  1196. tmesh->create(vv);
  1197. for (int i = 0; i < get_child_count(); i++) {
  1198. if (get_child(i)->cast_to<Light>()) {
  1199. Light *l = get_child(i)->cast_to<Light>();
  1200. BakeLight bl;
  1201. for (int i = 0; i < Light::PARAM_MAX; i++) {
  1202. bl.param[i] = l->get_parameter(Light::Parameter(i));
  1203. }
  1204. Transform t = l->get_global_transform();
  1205. bl.pos = t.origin;
  1206. bl.dir = t.basis.get_axis(2);
  1207. bl.type = l->get_light_type();
  1208. lights.push_back(bl);
  1209. }
  1210. }
  1211. }
  1212. int idx = 0;
  1213. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  1214. if (E->get()->baked.is_valid())
  1215. _octant_clear_baked(E->key());
  1216. _octant_bake(E->key(), tmesh, lights);
  1217. print_line("baking " + itos(idx) + "/" + itos(octant_map.size()));
  1218. idx++;
  1219. }
  1220. }
  1221. void GridMap::_baked_light_changed() {
  1222. // if (!baked_light_instance)
  1223. // VS::get_singleton()->instance_geometry_set_baked_light(get_instance(),RID());
  1224. // else
  1225. // VS::get_singleton()->instance_geometry_set_baked_light(get_instance(),baked_light_instance->get_baked_light_instance());
  1226. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  1227. for (Map<int, Octant::ItemInstances>::Element *F = E->get()->items.front(); F; F = F->next()) {
  1228. VS::get_singleton()->instance_geometry_set_baked_light(F->get().multimesh_instance, baked_light_instance ? baked_light_instance->get_baked_light_instance() : RID());
  1229. }
  1230. }
  1231. }
  1232. void GridMap::_find_baked_light() {
  1233. Node *n = get_parent();
  1234. while (n) {
  1235. BakedLightInstance *bl = n->cast_to<BakedLightInstance>();
  1236. if (bl) {
  1237. baked_light_instance = bl;
  1238. baked_light_instance->connect(SceneStringNames::get_singleton()->baked_light_changed, this, SceneStringNames::get_singleton()->_baked_light_changed);
  1239. _baked_light_changed();
  1240. return;
  1241. }
  1242. n = n->get_parent();
  1243. }
  1244. _baked_light_changed();
  1245. }
  1246. Array GridMap::_get_baked_light_meshes() {
  1247. if (theme.is_null())
  1248. return Array();
  1249. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  1250. Array meshes;
  1251. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  1252. int id = E->get().item;
  1253. if (!theme->has_item(id))
  1254. continue;
  1255. Ref<Mesh> mesh = theme->get_item_mesh(id);
  1256. if (mesh.is_null())
  1257. continue;
  1258. IndexKey ik = E->key();
  1259. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  1260. Transform xform;
  1261. xform.basis.set_orthogonal_index(E->get().rot);
  1262. xform.set_origin(cellpos * cell_size + ofs);
  1263. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  1264. meshes.push_back(xform);
  1265. meshes.push_back(mesh);
  1266. }
  1267. return meshes;
  1268. }
  1269. void GridMap::set_use_baked_light(bool p_use) {
  1270. if (use_baked_light == p_use)
  1271. return;
  1272. use_baked_light = p_use;
  1273. if (is_inside_world()) {
  1274. if (!p_use) {
  1275. if (baked_light_instance) {
  1276. baked_light_instance->disconnect(SceneStringNames::get_singleton()->baked_light_changed, this, SceneStringNames::get_singleton()->_baked_light_changed);
  1277. baked_light_instance = NULL;
  1278. }
  1279. _baked_light_changed();
  1280. } else {
  1281. _find_baked_light();
  1282. }
  1283. }
  1284. }
  1285. bool GridMap::is_using_baked_light() const {
  1286. return use_baked_light;
  1287. }
  1288. GridMap::GridMap() {
  1289. cell_size = 2;
  1290. octant_size = 4;
  1291. awaiting_update = false;
  1292. _in_tree = false;
  1293. center_x = true;
  1294. center_y = true;
  1295. center_z = true;
  1296. clip = false;
  1297. clip_floor = 0;
  1298. clip_axis = Vector3::AXIS_Z;
  1299. clip_above = true;
  1300. baked_lock = false;
  1301. bake = false;
  1302. cell_scale = 1.0;
  1303. baked_light_instance = NULL;
  1304. use_baked_light = false;
  1305. navigation = NULL;
  1306. }
  1307. GridMap::~GridMap() {
  1308. if (!theme.is_null())
  1309. theme->unregister_owner(this);
  1310. clear();
  1311. }