grid_map.cpp 37 KB

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