grid_map.cpp 41 KB

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