grid_map.cpp 44 KB

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