grid_map.cpp 47 KB

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