grid_map.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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 "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. if (is_inside_world())
  326. PhysicsServer::get_singleton()->body_set_space(g->static_body,get_world()->get_space());
  327. octant_map[octantkey]=g;
  328. }
  329. Octant& g = *octant_map[octantkey];
  330. if (!g.items.has(p_item)) {
  331. Octant::ItemInstances ii;
  332. if (theme.is_valid() && theme->has_item(p_item)) {
  333. ii.mesh=theme->get_item_mesh(p_item);
  334. ii.shape=theme->get_item_shape(p_item);
  335. }
  336. ii.multimesh = Ref<MultiMesh>( memnew( MultiMesh ) );
  337. ii.multimesh->set_mesh(ii.mesh);
  338. ii.multimesh_instance = VS::get_singleton()->instance_create();
  339. VS::get_singleton()->instance_set_base(ii.multimesh_instance,ii.multimesh->get_rid());
  340. VS::get_singleton()->instance_geometry_set_baked_light(ii.multimesh_instance,baked_light_instance?baked_light_instance->get_baked_light_instance():RID());
  341. if (!baked_lock) {
  342. //unbake just in case
  343. if (g.bake_instance.is_valid())
  344. VS::get_singleton()->free(g.bake_instance);
  345. g.baked=Ref<Mesh>();
  346. if (is_inside_world()) {
  347. VS::get_singleton()->instance_set_scenario(ii.multimesh_instance,get_world()->get_scenario());
  348. if (ok.area) {
  349. VS::get_singleton()->instance_set_room( ii.multimesh_instance,area_map[ok.area]->instance);
  350. }
  351. }
  352. }
  353. g.items[p_item]=ii;
  354. }
  355. Octant::ItemInstances &ii = g.items[p_item];
  356. ii.cells.insert(key);
  357. g.dirty=true;
  358. _queue_dirty_map();
  359. cell_map[key]=Cell();
  360. Cell &c=cell_map[key];
  361. c.item=p_item;
  362. c.rot=p_rot;
  363. }
  364. int GridMap::get_cell_item(int p_x,int p_y,int p_z) const{
  365. ERR_FAIL_INDEX_V(ABS(p_x),1<<20,INVALID_CELL_ITEM);
  366. ERR_FAIL_INDEX_V(ABS(p_y),1<<20,INVALID_CELL_ITEM);
  367. ERR_FAIL_INDEX_V(ABS(p_z),1<<20,INVALID_CELL_ITEM);
  368. IndexKey key;
  369. key.x=p_x;
  370. key.y=p_y;
  371. key.z=p_z;
  372. if (!cell_map.has(key))
  373. return INVALID_CELL_ITEM;
  374. return cell_map[key].item;
  375. }
  376. int GridMap::get_cell_item_orientation(int p_x,int p_y,int p_z) const{
  377. ERR_FAIL_INDEX_V(ABS(p_x),1<<20,-1);
  378. ERR_FAIL_INDEX_V(ABS(p_y),1<<20,-1);
  379. ERR_FAIL_INDEX_V(ABS(p_z),1<<20,-1);
  380. IndexKey key;
  381. key.x=p_x;
  382. key.y=p_y;
  383. key.z=p_z;
  384. if (!cell_map.has(key))
  385. return -1;
  386. return cell_map[key].rot;
  387. }
  388. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  389. ERR_FAIL_COND(!octant_map.has(p_key));
  390. Octant&g = *octant_map[p_key];
  391. PhysicsServer::get_singleton()->body_set_state(g.static_body,PhysicsServer::BODY_STATE_TRANSFORM,get_global_transform());
  392. PhysicsServer::get_singleton()->body_set_space(g.static_body,get_world()->get_space());
  393. //print_line("BODYPOS: "+get_global_transform());
  394. if (g.baked.is_valid()) {
  395. Transform xf = get_global_transform();
  396. xf.translate(_octant_get_offset(p_key));
  397. VS::get_singleton()->instance_set_transform(g.bake_instance,xf);
  398. VS::get_singleton()->instance_set_scenario(g.bake_instance,get_world()->get_scenario());
  399. if (area_map.has(p_key.area)) {
  400. VS::get_singleton()->instance_set_room(g.bake_instance,area_map[p_key.area]->instance);
  401. }
  402. } else {
  403. for(Map<int,Octant::ItemInstances>::Element *E=g.items.front();E;E=E->next()) {
  404. VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance,get_world()->get_scenario());
  405. VS::get_singleton()->instance_set_transform(E->get().multimesh_instance,get_global_transform());
  406. //print_line("INSTANCEPOS: "+get_global_transform());
  407. if (area_map.has(p_key.area)) {
  408. VS::get_singleton()->instance_set_room(E->get().multimesh_instance,area_map[p_key.area]->instance);
  409. }
  410. }
  411. }
  412. }
  413. void GridMap::_octant_transform(const OctantKey &p_key) {
  414. ERR_FAIL_COND(!octant_map.has(p_key));
  415. Octant&g = *octant_map[p_key];
  416. PhysicsServer::get_singleton()->body_set_state(g.static_body,PhysicsServer::BODY_STATE_TRANSFORM,get_global_transform());
  417. if (g.baked.is_valid()) {
  418. Transform xf = get_global_transform();
  419. xf.origin+=_octant_get_offset(p_key);
  420. VS::get_singleton()->instance_set_transform(g.bake_instance,xf);
  421. } else {
  422. for(Map<int,Octant::ItemInstances>::Element *E=g.items.front();E;E=E->next()) {
  423. VS::get_singleton()->instance_set_transform(E->get().multimesh_instance,get_global_transform());
  424. //print_line("UPDATEPOS: "+get_global_transform());
  425. }
  426. }
  427. }
  428. void GridMap::_octant_update(const OctantKey &p_key) {
  429. ERR_FAIL_COND(!octant_map.has(p_key));
  430. Octant&g = *octant_map[p_key];
  431. if (!g.dirty)
  432. return;
  433. Ref<Mesh> mesh;
  434. PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
  435. for(Map<int,Octant::ItemInstances>::Element *E=g.items.front();E;E=E->next()) {
  436. Octant::ItemInstances &ii=E->get();
  437. ii.multimesh->set_instance_count(ii.cells.size());
  438. AABB aabb;
  439. AABB mesh_aabb = ii.mesh.is_null()?AABB():ii.mesh->get_aabb();
  440. Vector3 ofs(cell_size*0.5*int(center_x),cell_size*0.5*int(center_y),cell_size*0.5*int(center_z));
  441. //print_line("OCTANT, CELLS: "+itos(ii.cells.size()));
  442. int idx=0;
  443. for(Set<IndexKey>::Element *F=ii.cells.front();F;F=F->next()) {
  444. IndexKey ik=F->get();
  445. Map<IndexKey,Cell>::Element *C=cell_map.find(ik);
  446. ERR_CONTINUE(!C);
  447. Vector3 cellpos = Vector3(ik.x,ik.y,ik.z );
  448. Transform xform;
  449. if (clip && ( (clip_above && cellpos[clip_axis]>clip_floor) || (!clip_above && cellpos[clip_axis]<clip_floor))) {
  450. xform.basis.set_zero();
  451. } else {
  452. xform.basis.set_orthogonal_index(C->get().rot);
  453. }
  454. xform.set_origin( cellpos*cell_size+ofs);
  455. xform.basis.scale(Vector3(cell_scale,cell_scale,cell_scale));
  456. ii.multimesh->set_instance_transform(idx,xform);
  457. ii.multimesh->set_instance_color(idx,Color(1,1,1,1));
  458. //print_line("MMINST: "+xform);
  459. if(idx==0) {
  460. aabb=xform.xform(mesh_aabb);
  461. } else {
  462. aabb.merge_with(xform.xform(mesh_aabb));
  463. }
  464. if (ii.shape.is_valid()) {
  465. PhysicsServer::get_singleton()->body_add_shape(g.static_body,ii.shape->get_rid(),xform);
  466. // print_line("PHIS x: "+xform);
  467. }
  468. idx++;
  469. }
  470. ii.multimesh->set_aabb(aabb);
  471. }
  472. g.dirty=false;
  473. }
  474. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  475. ERR_FAIL_COND(!octant_map.has(p_key));
  476. Octant&g = *octant_map[p_key];
  477. PhysicsServer::get_singleton()->body_set_state(g.static_body,PhysicsServer::BODY_STATE_TRANSFORM,get_global_transform());
  478. PhysicsServer::get_singleton()->body_set_space(g.static_body,RID());
  479. if (g.baked.is_valid()) {
  480. VS::get_singleton()->instance_set_room(g.bake_instance,RID());
  481. VS::get_singleton()->instance_set_scenario(g.bake_instance,RID());
  482. }
  483. for(Map<int,Octant::ItemInstances>::Element *E=g.items.front();E;E=E->next()) {
  484. VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance,RID());
  485. // VS::get_singleton()->instance_set_transform(E->get().multimesh_instance,get_global_transform());
  486. VS::get_singleton()->instance_set_room(E->get().multimesh_instance,RID());
  487. }
  488. }
  489. void GridMap::_octant_clear_baked(const OctantKey &p_key) {
  490. ERR_FAIL_COND(!octant_map.has(p_key));
  491. Octant&g = *octant_map[p_key];
  492. if (!g.baked.is_valid())
  493. return;
  494. VS::get_singleton()->free(g.bake_instance);
  495. g.bake_instance=RID();
  496. g.baked=Ref<Mesh>();
  497. if (is_inside_scene())
  498. _octant_enter_world(p_key);
  499. g.dirty=true;
  500. _queue_dirty_map();
  501. }
  502. void GridMap::_octant_bake(const OctantKey &p_key, const Ref<TriangleMesh>& p_tmesh,const Vector<BakeLight> &p_lights,List<Vector3> *p_prebake) {
  503. ERR_FAIL_COND(!octant_map.has(p_key));
  504. Octant&g = *octant_map[p_key];
  505. Ref<TriangleMesh> tm=p_tmesh;
  506. if (!p_prebake && is_inside_world())
  507. _octant_exit_world(p_key);
  508. Map< Ref<Material>, Ref<SurfaceTool> > surfaces;
  509. Vector3 ofs(cell_size*0.5*int(center_x),cell_size*0.5*int(center_y),cell_size*0.5*int(center_z));
  510. Vector3 octant_ofs=_octant_get_offset(p_key);
  511. for(Map<int,Octant::ItemInstances>::Element *E=g.items.front();E;E=E->next()) {
  512. Octant::ItemInstances &ii=E->get();
  513. if (ii.mesh.is_null())
  514. continue;
  515. for(Set<IndexKey>::Element *F=ii.cells.front();F;F=F->next()) {
  516. IndexKey ik=F->get();
  517. Map<IndexKey,Cell>::Element *C=cell_map.find(ik);
  518. ERR_CONTINUE(!C);
  519. Vector3 cellpos = Vector3(ik.x,ik.y,ik.z );
  520. Transform xform;
  521. xform.basis.set_orthogonal_index(C->get().rot);
  522. xform.set_origin( cellpos*cell_size+ofs);
  523. if (!p_prebake)
  524. xform.origin-=octant_ofs;
  525. for(int i=0;i<ii.mesh->get_surface_count();i++) {
  526. if (p_prebake) {
  527. if (ii.mesh->surface_get_primitive_type(i)!=Mesh::PRIMITIVE_TRIANGLES)
  528. continue;
  529. Array a = ii.mesh->surface_get_arrays(i);
  530. DVector<Vector3> av=a[VS::ARRAY_VERTEX];
  531. int avs = av.size();
  532. DVector<Vector3>::Read vr = av.read();
  533. DVector<int> ai=a[VS::ARRAY_INDEX];
  534. int ais=ai.size();
  535. if (ais) {
  536. DVector<int>::Read ir=ai.read();
  537. for(int j=0;j<ais;j++) {
  538. p_prebake->push_back(xform.xform(vr[ir[j]]));
  539. //print_line("V SET: "+xform.xform(vr[ir[j]]));
  540. }
  541. } else {
  542. for(int j=0;j<avs;j++) {
  543. p_prebake->push_back(xform.xform(vr[j]));
  544. }
  545. }
  546. } else {
  547. Ref<Material> m = ii.mesh->surface_get_material(i);
  548. Map< Ref<Material>, Ref<SurfaceTool> >::Element *S=surfaces.find(m);
  549. if (!S) {
  550. S=surfaces.insert(m,Ref<SurfaceTool>( memnew( SurfaceTool )));
  551. }
  552. Ref<SurfaceTool> st = S->get();
  553. List<SurfaceTool::Vertex>::Element *V=st->get_vertex_array().back();
  554. st->append_from(ii.mesh,i,xform);
  555. st->set_material(m);
  556. if (tm.is_valid()) {
  557. if (V)
  558. V=V->next();
  559. else
  560. V=st->get_vertex_array().front();;
  561. int lc = p_lights.size();
  562. const BakeLight* bl = p_lights.ptr();
  563. float ofs = cell_size*0.02;
  564. float att = 0.2;
  565. for(;V;V=V->next()) {
  566. SurfaceTool::Vertex &v=V->get();
  567. Vector3 vertex = v.vertex + octant_ofs;
  568. //print_line("V GET: "+vertex);
  569. Vector3 normal = tm->get_area_normal( AABB( Vector3(-ofs,-ofs,-ofs)+vertex,Vector3(ofs,ofs,ofs)*2.0));
  570. if (normal==Vector3()) {
  571. print_line("couldn't find for vertex: "+vertex);
  572. }
  573. ERR_CONTINUE( normal== Vector3());
  574. float max_l=1.0;
  575. float max_dist=1.0;
  576. if (lc) {
  577. for(int j=0;j<lc;j++) {
  578. const BakeLight &l=bl[j];
  579. switch(l.type) {
  580. case VS::LIGHT_DIRECTIONAL: {
  581. Vector3 ray_from=vertex + normal *ofs;
  582. Vector3 ray_to=l.dir*5000;
  583. Vector3 n;
  584. Vector3 p;
  585. if (tm->intersect_segment(ray_from,ray_to,p,n)) {
  586. float dist = 1.0-l.param[VS::LIGHT_PARAM_SHADOW_DARKENING];
  587. if (dist<=max_dist) {
  588. max_dist=dist;
  589. max_l=1.0-dist;
  590. }
  591. }
  592. } break;
  593. }
  594. }
  595. }
  596. v.color=Color(max_l,max_l,max_l,1.0);
  597. }
  598. st->add_to_format(VS::ARRAY_FORMAT_COLOR);
  599. if (m.is_valid()) {
  600. Ref<FixedMaterial> fm = m;
  601. if (fm.is_valid())
  602. fm->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY,true);
  603. }
  604. }
  605. }
  606. }
  607. }
  608. }
  609. if (p_prebake)
  610. return;
  611. g.baked = Ref<Mesh>( memnew( Mesh ));
  612. for(Map< Ref<Material>, Ref<SurfaceTool> >::Element *E=surfaces.front();E;E=E->next()) {
  613. Ref<SurfaceTool> st = E->get();
  614. st->commit(g.baked);
  615. }
  616. g.bake_instance = VS::get_singleton()->instance_create();
  617. VS::get_singleton()->instance_set_base(g.bake_instance,g.baked->get_rid());
  618. if (is_inside_world())
  619. _octant_enter_world(p_key);
  620. g.dirty=true;
  621. _queue_dirty_map();
  622. }
  623. void GridMap::_notification(int p_what) {
  624. switch(p_what) {
  625. case NOTIFICATION_ENTER_WORLD: {
  626. _update_area_instances();
  627. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  628. // IndexKey ik;
  629. // ik.key = E->key().indexkey;
  630. _octant_enter_world(E->key());
  631. _octant_update(E->key());
  632. }
  633. awaiting_update=false;
  634. last_transform=get_global_transform();
  635. if (use_baked_light) {
  636. _find_baked_light();
  637. }
  638. } break;
  639. case NOTIFICATION_TRANSFORM_CHANGED: {
  640. Transform new_xform = get_global_transform();
  641. if (new_xform==last_transform)
  642. break;
  643. //update run
  644. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  645. _octant_transform(E->key());
  646. }
  647. last_transform=new_xform;
  648. } break;
  649. case NOTIFICATION_EXIT_WORLD: {
  650. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  651. _octant_exit_world(E->key());
  652. }
  653. if (use_baked_light) {
  654. if (baked_light_instance) {
  655. baked_light_instance->disconnect(SceneStringNames::get_singleton()->baked_light_changed,this,SceneStringNames::get_singleton()->_baked_light_changed);
  656. baked_light_instance=NULL;
  657. }
  658. _baked_light_changed();
  659. }
  660. //_queue_dirty_map(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  661. //_update_dirty_map_callback();
  662. //_update_area_instances();
  663. } break;
  664. }
  665. }
  666. void GridMap::_queue_dirty_map() {
  667. if (awaiting_update)
  668. return;
  669. if (is_inside_world()) {
  670. MessageQueue::get_singleton()->push_call(this,"_update_dirty_map_callback");
  671. awaiting_update=true;
  672. }
  673. }
  674. void GridMap::_recreate_octant_data() {
  675. Map<IndexKey,Cell> cell_copy=cell_map;
  676. _clear_internal(true);
  677. for (Map<IndexKey,Cell>::Element *E=cell_copy.front();E;E=E->next()) {
  678. set_cell_item(E->key().x,E->key().y,E->key().z,E->get().item,E->get().rot);
  679. }
  680. }
  681. void GridMap::_clear_internal(bool p_keep_areas) {
  682. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  683. if (is_inside_world())
  684. _octant_exit_world(E->key());
  685. for (Map<int,Octant::ItemInstances>::Element *F=E->get()->items.front();F;F=F->next()) {
  686. VS::get_singleton()->free(F->get().multimesh_instance);
  687. }
  688. //unbake just in case
  689. if (E->get()->bake_instance.is_valid())
  690. VS::get_singleton()->free(E->get()->bake_instance);
  691. PhysicsServer::get_singleton()->free(E->get()->static_body);
  692. memdelete(E->get());
  693. }
  694. octant_map.clear();
  695. cell_map.clear();
  696. if (p_keep_areas)
  697. return;
  698. for (Map<int,Area*>::Element *E=area_map.front();E;E=E->next()) {
  699. VS::get_singleton()->free(E->get()->base_portal);
  700. VS::get_singleton()->free(E->get()->instance);
  701. for(int i=0;i<E->get()->portals.size();i++) {
  702. VS::get_singleton()->free(E->get()->portals[i].instance);
  703. }
  704. memdelete(E->get());
  705. }
  706. }
  707. void GridMap::clear() {
  708. _clear_internal();
  709. }
  710. void GridMap::resource_changed(const RES& p_res) {
  711. _recreate_octant_data();
  712. }
  713. void GridMap::_update_dirty_map_callback() {
  714. if (!awaiting_update)
  715. return;
  716. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  717. _octant_update(E->key());
  718. }
  719. awaiting_update=false;
  720. }
  721. void GridMap::_bind_methods() {
  722. ObjectTypeDB::bind_method(_MD("set_theme","theme:MeshLibrary"),&GridMap::set_theme);
  723. ObjectTypeDB::bind_method(_MD("get_theme:MeshLibrary"),&GridMap::get_theme);
  724. ObjectTypeDB::bind_method(_MD("set_bake","enable"),&GridMap::set_bake);
  725. ObjectTypeDB::bind_method(_MD("is_baking_enabled"),&GridMap::is_baking_enabled);
  726. ObjectTypeDB::bind_method(_MD("set_cell_size","size"),&GridMap::set_cell_size);
  727. ObjectTypeDB::bind_method(_MD("get_cell_size"),&GridMap::get_cell_size);
  728. ObjectTypeDB::bind_method(_MD("set_octant_size","size"),&GridMap::set_octant_size);
  729. ObjectTypeDB::bind_method(_MD("get_octant_size"),&GridMap::get_octant_size);
  730. ObjectTypeDB::bind_method(_MD("set_cell_item","x","y","z","item","orientation"),&GridMap::set_cell_item,DEFVAL(0));
  731. ObjectTypeDB::bind_method(_MD("get_cell_item","x","y","z"),&GridMap::get_cell_item);
  732. ObjectTypeDB::bind_method(_MD("get_cell_item_orientation","x","y","z"),&GridMap::get_cell_item_orientation);
  733. // ObjectTypeDB::bind_method(_MD("_recreate_octants"),&GridMap::_recreate_octants);
  734. ObjectTypeDB::bind_method(_MD("_update_dirty_map_callback"),&GridMap::_update_dirty_map_callback);
  735. ObjectTypeDB::bind_method(_MD("resource_changed"),&GridMap::resource_changed);
  736. ObjectTypeDB::bind_method(_MD("set_center_x","enable"),&GridMap::set_center_x);
  737. ObjectTypeDB::bind_method(_MD("get_center_x"),&GridMap::get_center_x);
  738. ObjectTypeDB::bind_method(_MD("set_center_y","enable"),&GridMap::set_center_y);
  739. ObjectTypeDB::bind_method(_MD("get_center_y"),&GridMap::get_center_y);
  740. ObjectTypeDB::bind_method(_MD("set_center_z","enable"),&GridMap::set_center_z);
  741. ObjectTypeDB::bind_method(_MD("get_center_z"),&GridMap::get_center_z);
  742. ObjectTypeDB::bind_method(_MD("set_clip","enabled","clipabove","floor","axis"),&GridMap::set_clip,DEFVAL(true),DEFVAL(0),DEFVAL(Vector3::AXIS_X));
  743. ObjectTypeDB::bind_method(_MD("crate_area","id","area"),&GridMap::create_area);
  744. ObjectTypeDB::bind_method(_MD("area_get_bounds","area","bounds"),&GridMap::area_get_bounds);
  745. ObjectTypeDB::bind_method(_MD("area_set_exterior_portal","area","enable"),&GridMap::area_set_exterior_portal);
  746. ObjectTypeDB::bind_method(_MD("area_set_name","area","name"),&GridMap::area_set_name);
  747. ObjectTypeDB::bind_method(_MD("area_get_name","area"),&GridMap::area_get_name);
  748. ObjectTypeDB::bind_method(_MD("area_is_exterior_portal","area"),&GridMap::area_is_exterior_portal);
  749. ObjectTypeDB::bind_method(_MD("area_set_portal_disable_distance","area","distance"),&GridMap::area_set_portal_disable_distance);
  750. ObjectTypeDB::bind_method(_MD("area_get_portal_disable_distance","area"),&GridMap::area_get_portal_disable_distance);
  751. ObjectTypeDB::bind_method(_MD("area_set_portal_disable_color","area","color"),&GridMap::area_set_portal_disable_color);
  752. ObjectTypeDB::bind_method(_MD("area_get_portal_disable_color","area"),&GridMap::area_get_portal_disable_color);
  753. ObjectTypeDB::bind_method(_MD("erase_area","area"),&GridMap::erase_area);
  754. ObjectTypeDB::bind_method(_MD("get_unused_area_id","area"),&GridMap::get_unused_area_id);
  755. ObjectTypeDB::bind_method(_MD("bake_geometry"),&GridMap::bake_geometry);
  756. ObjectTypeDB::bind_method(_MD("_baked_light_changed"),&GridMap::_baked_light_changed);
  757. ObjectTypeDB::bind_method(_MD("set_use_baked_light","use"),&GridMap::set_use_baked_light);
  758. ObjectTypeDB::bind_method(_MD("is_using_baked_light","use"),&GridMap::is_using_baked_light);
  759. ObjectTypeDB::bind_method(_MD("_get_baked_light_meshes"),&GridMap::_get_baked_light_meshes);
  760. ObjectTypeDB::set_method_flags("GridMap","bake_geometry",METHOD_FLAGS_DEFAULT|METHOD_FLAG_EDITOR);
  761. ObjectTypeDB::bind_method(_MD("clear"),&GridMap::clear);
  762. BIND_CONSTANT( INVALID_CELL_ITEM );
  763. }
  764. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  765. if (!p_enabled && !clip)
  766. return;
  767. if (clip && p_enabled && clip_floor==p_floor && p_clip_above==clip_above && p_axis==clip_axis)
  768. return;
  769. clip=p_enabled;
  770. clip_floor=p_floor;
  771. clip_axis=p_axis;
  772. clip_above=p_clip_above;
  773. //make it all update
  774. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  775. Octant *g=E->get();
  776. g->dirty=true;
  777. }
  778. awaiting_update=true;
  779. _update_dirty_map_callback();
  780. }
  781. void GridMap::_update_areas() {
  782. //clear the portals
  783. for(Map<int,Area*>::Element *E=area_map.front();E;E=E->next()) {
  784. //this should somehow be faster...
  785. Area& a=*E->get();
  786. a.portals.clear();
  787. if (a.instance.is_valid()) {
  788. VisualServer::get_singleton()->free(a.instance);
  789. a.instance=RID();
  790. }
  791. }
  792. //test all areas against all areas and create portals - this sucks (slow :( )
  793. for(Map<int,Area*>::Element *E=area_map.front();E;E=E->next()) {
  794. Area& a=*E->get();
  795. if (a.exterior_portal) //that's pretty much all it does... yes it is
  796. continue;
  797. Vector3 from_a(a.from.x,a.from.y,a.from.z);
  798. Vector3 to_a(a.to.x,a.to.y,a.to.z);
  799. for(Map<int,Area*>::Element *F=area_map.front();F;F=F->next()) {
  800. Area& b=*F->get();
  801. Vector3 from_b(b.from.x,b.from.y,b.from.z);
  802. Vector3 to_b(b.to.x,b.to.y,b.to.z);
  803. // initially test intersection and discards
  804. int axis=-1;
  805. float sign=0;
  806. bool valid=true;
  807. Vector3 axmin,axmax;
  808. for(int i=0;i<3;i++) {
  809. if (from_a[i]==to_b[i]) {
  810. if (axis!=-1) {
  811. valid=false;
  812. break;
  813. }
  814. axis=i;
  815. sign=-1;
  816. } else if (from_b[i]==to_a[i]) {
  817. if (axis!=-1) {
  818. valid=false;
  819. break;
  820. }
  821. axis=i;
  822. sign=+1;
  823. }
  824. if (from_a[i] > to_b[i] || to_a[i] < from_b[i] ) {
  825. valid=false;
  826. break;
  827. } else {
  828. axmin[i]= ( from_a[i] > from_b[i] ) ? from_a[i] :from_b[i];
  829. axmax[i]= ( to_a[i] < to_b[i] ) ? to_a[i] :to_b[i];
  830. }
  831. }
  832. if (axis==-1 || !valid)
  833. continue;
  834. Transform xf;
  835. for(int i=0;i<3;i++) {
  836. int ax=(axis+i)%3;
  837. Vector3 axis_vec;
  838. float scale = (i==0)?sign:((axmax[ax]-axmin[ax])*cell_size);
  839. axis_vec[ax]=scale;
  840. xf.basis.set_axis((2+i)%3,axis_vec);
  841. xf.origin[i]=axmin[i]*cell_size;
  842. }
  843. Area::Portal portal;
  844. portal.xform=xf;
  845. a.portals.push_back(portal);
  846. }
  847. }
  848. _update_area_instances();
  849. }
  850. void GridMap::_update_area_instances() {
  851. Transform base_xform;
  852. if (_in_tree)
  853. base_xform=get_global_transform();
  854. for(Map<int,Area*>::Element *E=area_map.front();E;E=E->next()) {
  855. //this should somehow be faster...
  856. Area& a=*E->get();
  857. if (a.instance.is_valid()!=_in_tree) {
  858. if (!_in_tree) {
  859. for(int i=0;i<a.portals.size();i++) {
  860. Area::Portal&p=a.portals[i];
  861. ERR_CONTINUE(!p.instance.is_valid());
  862. VisualServer::get_singleton()->free(p.instance);
  863. p.instance=RID();
  864. }
  865. VisualServer::get_singleton()->free(a.instance);
  866. a.instance=RID();
  867. } else {
  868. //a.instance = VisualServer::get_singleton()->instance_create2(base_room,get_world()->get_scenario());
  869. for(int i=0;i<a.portals.size();i++) {
  870. Area::Portal&p=a.portals[i];
  871. ERR_CONTINUE(p.instance.is_valid());
  872. p.instance=VisualServer::get_singleton()->instance_create2(a.base_portal,get_world()->get_scenario());
  873. VisualServer::get_singleton()->instance_set_room(p.instance,a.instance);
  874. }
  875. }
  876. }
  877. if (a.instance.is_valid()) {
  878. Transform xform;
  879. Vector3 from_a(a.from.x,a.from.y,a.from.z);
  880. Vector3 to_a(a.to.x,a.to.y,a.to.z);
  881. for(int i=0;i<3;i++) {
  882. xform.origin[i]=from_a[i]*cell_size;
  883. Vector3 s;
  884. s[i]=(to_a[i]-from_a[i])*cell_size;
  885. xform.basis.set_axis(i,s);
  886. }
  887. VisualServer::get_singleton()->instance_set_transform(a.instance,base_xform * xform);
  888. for(int i=0;i<a.portals.size();i++) {
  889. Area::Portal&p=a.portals[i];
  890. ERR_CONTINUE(!p.instance.is_valid());
  891. VisualServer::get_singleton()->instance_set_transform(p.instance,base_xform * xform);
  892. }
  893. }
  894. }
  895. }
  896. Error GridMap::create_area(int p_id,const AABB& p_bounds) {
  897. ERR_FAIL_COND_V(area_map.has(p_id),ERR_ALREADY_EXISTS);
  898. ERR_EXPLAIN("ID 0 is taken as global area, start from 1");
  899. ERR_FAIL_COND_V(p_id==0,ERR_INVALID_PARAMETER);
  900. ERR_FAIL_COND_V(p_bounds.has_no_area(),ERR_INVALID_PARAMETER);
  901. // FIRST VALIDATE AREA
  902. IndexKey from,to;
  903. from.x=p_bounds.pos.x;
  904. from.y=p_bounds.pos.y;
  905. from.z=p_bounds.pos.z;
  906. to.x=p_bounds.pos.x+p_bounds.size.x;
  907. to.y=p_bounds.pos.y+p_bounds.size.y;
  908. to.z=p_bounds.pos.z+p_bounds.size.z;
  909. for(Map<int,Area*>::Element *E=area_map.front();E;E=E->next()) {
  910. //this should somehow be faster...
  911. Area& a=*E->get();
  912. //does it interset with anything else?
  913. if ( from.x >= a.to.x ||
  914. to.x <= a.from.x ||
  915. from.y >= a.to.y ||
  916. to.y <= a.from.y ||
  917. from.z >= a.to.z ||
  918. to.z <= a.from.z ) {
  919. // all good
  920. } else {
  921. return ERR_INVALID_PARAMETER;
  922. }
  923. }
  924. Area *area = memnew( Area );
  925. area->from=from;
  926. area->to=to;
  927. area->portal_disable_distance=0;
  928. area->exterior_portal=false;
  929. area->name="Area "+itos(p_id);
  930. area_map[p_id]=area;
  931. _recreate_octant_data();
  932. return OK;
  933. }
  934. AABB GridMap::area_get_bounds(int p_area) const {
  935. ERR_FAIL_COND_V(!area_map.has(p_area),AABB());
  936. const Area *a = area_map[p_area];
  937. AABB aabb;
  938. aabb.pos=Vector3(a->from.x,a->from.y,a->from.z);
  939. aabb.size=Vector3(a->to.x,a->to.y,a->to.z)-aabb.pos;
  940. return aabb;
  941. }
  942. void GridMap::area_set_name(int p_area,const String& p_name) {
  943. ERR_FAIL_COND(!area_map.has(p_area));
  944. Area *a = area_map[p_area];
  945. a->name=p_name;
  946. }
  947. String GridMap::area_get_name(int p_area) const {
  948. ERR_FAIL_COND_V(!area_map.has(p_area),"");
  949. const Area *a = area_map[p_area];
  950. return a->name;
  951. }
  952. void GridMap::area_set_exterior_portal(int p_area,bool p_enable) {
  953. ERR_FAIL_COND(!area_map.has(p_area));
  954. Area *a = area_map[p_area];
  955. if (a->exterior_portal==p_enable)
  956. return;
  957. a->exterior_portal=p_enable;
  958. _recreate_octant_data();
  959. }
  960. bool GridMap::area_is_exterior_portal(int p_area) const {
  961. ERR_FAIL_COND_V(!area_map.has(p_area),false);
  962. const Area *a = area_map[p_area];
  963. return a->exterior_portal;
  964. }
  965. void GridMap::area_set_portal_disable_distance(int p_area, float p_distance) {
  966. ERR_FAIL_COND(!area_map.has(p_area));
  967. Area *a = area_map[p_area];
  968. a->portal_disable_distance=p_distance;
  969. }
  970. float GridMap::area_get_portal_disable_distance(int p_area) const {
  971. ERR_FAIL_COND_V(!area_map.has(p_area),0);
  972. const Area *a = area_map[p_area];
  973. return a->portal_disable_distance;
  974. }
  975. void GridMap::area_set_portal_disable_color(int p_area, Color p_color) {
  976. ERR_FAIL_COND(!area_map.has(p_area));
  977. Area *a = area_map[p_area];
  978. a->portal_disable_color=p_color;
  979. }
  980. Color GridMap::area_get_portal_disable_color(int p_area) const {
  981. ERR_FAIL_COND_V(!area_map.has(p_area),Color());
  982. const Area *a = area_map[p_area];
  983. return a->portal_disable_color;
  984. }
  985. void GridMap::get_area_list(List<int> *p_areas) const {
  986. for(const Map<int,Area*>::Element *E=area_map.front();E;E=E->next()) {
  987. p_areas->push_back(E->key());
  988. }
  989. }
  990. GridMap::Area::Portal::~Portal() {
  991. if (instance.is_valid())
  992. VisualServer::get_singleton()->free(instance);
  993. }
  994. GridMap::Area::Area() {
  995. base_portal=VisualServer::get_singleton()->portal_create();
  996. Vector< Point2 > points;
  997. points.push_back( Point2( 0, 1 ) );
  998. points.push_back( Point2( 1, 1 ) );
  999. points.push_back( Point2( 1, 0 ) );
  1000. points.push_back( Point2( 0, 0 ) );
  1001. VisualServer::get_singleton()->portal_set_shape(base_portal,points);
  1002. }
  1003. GridMap::Area::~Area() {
  1004. if (instance.is_valid())
  1005. VisualServer::get_singleton()->free(instance);
  1006. VisualServer::get_singleton()->free(base_portal);
  1007. }
  1008. void GridMap::erase_area(int p_area) {
  1009. ERR_FAIL_COND(!area_map.has(p_area));
  1010. Area* a=area_map[p_area];
  1011. memdelete(a);
  1012. area_map.erase(p_area);
  1013. _recreate_octant_data();
  1014. }
  1015. int GridMap::get_unused_area_id() const {
  1016. if (area_map.empty())
  1017. return 1;
  1018. else
  1019. return area_map.back()->key()+1;
  1020. }
  1021. void GridMap::set_bake(bool p_bake) {
  1022. bake=p_bake;
  1023. if (bake==false) {
  1024. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  1025. _octant_clear_baked(E->key());
  1026. }
  1027. }
  1028. }
  1029. bool GridMap::is_baking_enabled() const {
  1030. return bake;
  1031. }
  1032. void GridMap::set_cell_scale(float p_scale) {
  1033. cell_scale=p_scale;
  1034. _queue_dirty_map();
  1035. }
  1036. float GridMap::get_cell_scale() const{
  1037. return cell_scale;
  1038. }
  1039. void GridMap::bake_geometry() {
  1040. //used to compute vertex occlusion
  1041. Ref<TriangleMesh> tmesh;
  1042. Vector<BakeLight> lights;
  1043. if (true) {
  1044. List<Vector3> vertices;
  1045. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  1046. _octant_bake(E->key(),tmesh,lights,&vertices);
  1047. }
  1048. DVector<Vector3> vv;
  1049. vv.fill_with(vertices);
  1050. //print_line("TOTAL VERTICES: "+itos(vv.size()));
  1051. tmesh = Ref<TriangleMesh>( memnew( TriangleMesh ));
  1052. tmesh->create(vv);
  1053. for(int i=0;i<get_child_count();i++) {
  1054. if (get_child(i)->cast_to<Light>()) {
  1055. Light *l = get_child(i)->cast_to<Light>();
  1056. BakeLight bl;
  1057. for(int i=0;i<Light::PARAM_MAX;i++) {
  1058. bl.param[i]=l->get_parameter(Light::Parameter(i));
  1059. }
  1060. Transform t=l->get_global_transform();
  1061. bl.pos=t.origin;
  1062. bl.dir=t.basis.get_axis(2);
  1063. bl.type=l->get_light_type();
  1064. lights.push_back(bl);
  1065. }
  1066. }
  1067. }
  1068. int idx=0;
  1069. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  1070. if (E->get()->baked.is_valid())
  1071. _octant_clear_baked(E->key());
  1072. _octant_bake(E->key(),tmesh,lights);
  1073. print_line("baking "+itos(idx)+"/"+itos(octant_map.size()));
  1074. idx++;
  1075. }
  1076. }
  1077. void GridMap::_baked_light_changed() {
  1078. // if (!baked_light_instance)
  1079. // VS::get_singleton()->instance_geometry_set_baked_light(get_instance(),RID());
  1080. // else
  1081. // VS::get_singleton()->instance_geometry_set_baked_light(get_instance(),baked_light_instance->get_baked_light_instance());
  1082. for(Map<OctantKey,Octant*>::Element *E=octant_map.front();E;E=E->next()) {
  1083. for(Map<int,Octant::ItemInstances>::Element *F=E->get()->items.front();F;F=F->next()) {
  1084. VS::get_singleton()->instance_geometry_set_baked_light(F->get().multimesh_instance,baked_light_instance?baked_light_instance->get_baked_light_instance():RID());
  1085. }
  1086. }
  1087. }
  1088. void GridMap::_find_baked_light() {
  1089. Node *n=get_parent();
  1090. while(n) {
  1091. BakedLightInstance *bl=n->cast_to<BakedLightInstance>();
  1092. if (bl) {
  1093. baked_light_instance=bl;
  1094. baked_light_instance->connect(SceneStringNames::get_singleton()->baked_light_changed,this,SceneStringNames::get_singleton()->_baked_light_changed);
  1095. _baked_light_changed();
  1096. return;
  1097. }
  1098. n=n->get_parent();
  1099. }
  1100. _baked_light_changed();
  1101. }
  1102. Array GridMap::_get_baked_light_meshes() {
  1103. if (theme.is_null())
  1104. return Array();
  1105. Vector3 ofs(cell_size*0.5*int(center_x),cell_size*0.5*int(center_y),cell_size*0.5*int(center_z));
  1106. Array meshes;
  1107. for (Map<IndexKey,Cell>::Element *E=cell_map.front();E;E=E->next()) {
  1108. int id = E->get().item;
  1109. if (!theme->has_item(id))
  1110. continue;
  1111. Ref<Mesh> mesh=theme->get_item_mesh(id);
  1112. if (mesh.is_null())
  1113. continue;
  1114. IndexKey ik=E->key();
  1115. Vector3 cellpos = Vector3(ik.x,ik.y,ik.z );
  1116. Transform xform;
  1117. xform.basis.set_orthogonal_index(E->get().rot);
  1118. xform.set_origin( cellpos*cell_size+ofs);
  1119. xform.basis.scale(Vector3(cell_scale,cell_scale,cell_scale));
  1120. meshes.push_back(xform);
  1121. meshes.push_back(mesh);
  1122. }
  1123. return meshes;
  1124. }
  1125. void GridMap::set_use_baked_light(bool p_use) {
  1126. if (use_baked_light==p_use)
  1127. return;
  1128. use_baked_light=p_use;
  1129. if (is_inside_world()) {
  1130. if (!p_use) {
  1131. if (baked_light_instance) {
  1132. baked_light_instance->disconnect(SceneStringNames::get_singleton()->baked_light_changed,this,SceneStringNames::get_singleton()->_baked_light_changed);
  1133. baked_light_instance=NULL;
  1134. }
  1135. _baked_light_changed();
  1136. } else {
  1137. _find_baked_light();
  1138. }
  1139. }
  1140. }
  1141. bool GridMap::is_using_baked_light() const{
  1142. return use_baked_light;
  1143. }
  1144. GridMap::GridMap() {
  1145. cell_size=2;
  1146. octant_size=4;
  1147. awaiting_update=false;
  1148. _in_tree=false;
  1149. center_x=true;
  1150. center_y=true;
  1151. center_z=true;
  1152. clip=false;
  1153. clip_floor=0;
  1154. clip_axis=Vector3::AXIS_Z;
  1155. clip_above=true;
  1156. baked_lock=false;
  1157. bake=false;
  1158. cell_scale=1.0;
  1159. baked_light_instance=NULL;
  1160. use_baked_light=false;
  1161. }
  1162. GridMap::~GridMap() {
  1163. if (!theme.is_null())
  1164. theme->unregister_owner(this);
  1165. clear();
  1166. }