visual_server_scene.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432
  1. #include "visual_server_scene.h"
  2. #include "visual_server_global.h"
  3. /* CAMERA API */
  4. RID VisualServerScene::camera_create() {
  5. Camera * camera = memnew( Camera );
  6. return camera_owner.make_rid( camera );
  7. }
  8. void VisualServerScene::camera_set_perspective(RID p_camera,float p_fovy_degrees, float p_z_near, float p_z_far) {
  9. Camera *camera = camera_owner.get( p_camera );
  10. ERR_FAIL_COND(!camera);
  11. camera->type=Camera::PERSPECTIVE;
  12. camera->fov=p_fovy_degrees;
  13. camera->znear=p_z_near;
  14. camera->zfar=p_z_far;
  15. }
  16. void VisualServerScene::camera_set_orthogonal(RID p_camera,float p_size, float p_z_near, float p_z_far) {
  17. Camera *camera = camera_owner.get( p_camera );
  18. ERR_FAIL_COND(!camera);
  19. camera->type=Camera::ORTHOGONAL;
  20. camera->size=p_size;
  21. camera->znear=p_z_near;
  22. camera->zfar=p_z_far;
  23. }
  24. void VisualServerScene::camera_set_transform(RID p_camera,const Transform& p_transform) {
  25. Camera *camera = camera_owner.get( p_camera );
  26. ERR_FAIL_COND(!camera);
  27. camera->transform=p_transform.orthonormalized();
  28. }
  29. void VisualServerScene::camera_set_cull_mask(RID p_camera,uint32_t p_layers) {
  30. Camera *camera = camera_owner.get( p_camera );
  31. ERR_FAIL_COND(!camera);
  32. camera->visible_layers=p_layers;
  33. }
  34. void VisualServerScene::camera_set_environment(RID p_camera,RID p_env) {
  35. Camera *camera = camera_owner.get( p_camera );
  36. ERR_FAIL_COND(!camera);
  37. camera->env=p_env;
  38. }
  39. void VisualServerScene::camera_set_use_vertical_aspect(RID p_camera,bool p_enable) {
  40. Camera *camera = camera_owner.get( p_camera );
  41. ERR_FAIL_COND(!camera);
  42. camera->vaspect=p_enable;
  43. }
  44. /* SCENARIO API */
  45. void* VisualServerScene::_instance_pair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int) {
  46. // VisualServerScene *self = (VisualServerScene*)p_self;
  47. Instance *A = p_A;
  48. Instance *B = p_B;
  49. //instance indices are designed so greater always contains lesser
  50. if (A->base_type > B->base_type) {
  51. SWAP(A,B); //lesser always first
  52. }
  53. if (B->base_type==VS::INSTANCE_LIGHT && (1<<A->base_type)&VS::INSTANCE_GEOMETRY_MASK) {
  54. InstanceLightData * light = static_cast<InstanceLightData*>(B->base_data);
  55. InstanceGeometryData * geom = static_cast<InstanceGeometryData*>(A->base_data);
  56. InstanceLightData::PairInfo pinfo;
  57. pinfo.geometry=A;
  58. pinfo.L = geom->lighting.push_back(B);
  59. List<InstanceLightData::PairInfo>::Element *E = light->geometries.push_back(pinfo);
  60. if (geom->can_cast_shadows) {
  61. light->shadow_dirty=true;
  62. }
  63. geom->lighting_dirty=true;
  64. return E; //this element should make freeing faster
  65. } else if (B->base_type==VS::INSTANCE_REFLECTION_PROBE && (1<<A->base_type)&VS::INSTANCE_GEOMETRY_MASK) {
  66. InstanceReflectionProbeData * reflection_probe = static_cast<InstanceReflectionProbeData*>(B->base_data);
  67. InstanceGeometryData * geom = static_cast<InstanceGeometryData*>(A->base_data);
  68. InstanceReflectionProbeData::PairInfo pinfo;
  69. pinfo.geometry=A;
  70. pinfo.L = geom->reflection_probes.push_back(B);
  71. List<InstanceReflectionProbeData::PairInfo>::Element *E = reflection_probe->geometries.push_back(pinfo);
  72. geom->reflection_dirty=true;
  73. return E; //this element should make freeing faster
  74. }
  75. #if 0
  76. if (A->base_type==INSTANCE_PORTAL) {
  77. ERR_FAIL_COND_V( B->base_type!=INSTANCE_PORTAL,NULL );
  78. A->portal_info->candidate_set.insert(B);
  79. B->portal_info->candidate_set.insert(A);
  80. self->_portal_attempt_connect(A);
  81. //attempt to conncet portal A (will go through B anyway)
  82. //this is a little hackish, but works fine in practice
  83. } else if (A->base_type==INSTANCE_BAKED_LIGHT || B->base_type==INSTANCE_BAKED_LIGHT) {
  84. if (B->base_type==INSTANCE_BAKED_LIGHT) {
  85. SWAP(A,B);
  86. }
  87. ERR_FAIL_COND_V(B->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER,NULL);
  88. B->baked_light_sampler_info->baked_lights.insert(A);
  89. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  90. if (B->base_type==INSTANCE_ROOM)
  91. SWAP(A,B);
  92. ERR_FAIL_COND_V(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ),NULL);
  93. B->auto_rooms.insert(A);
  94. A->room_info->owned_autoroom_geometry.insert(B);
  95. self->_instance_validate_autorooms(B);
  96. } else {
  97. if (B->base_type==INSTANCE_LIGHT) {
  98. SWAP(A,B);
  99. } else if (A->base_type!=INSTANCE_LIGHT) {
  100. return NULL;
  101. }
  102. A->light_info->affected.insert(B);
  103. B->lights.insert(A);
  104. B->light_cache_dirty=true;
  105. }
  106. #endif
  107. return NULL;
  108. }
  109. void VisualServerScene::_instance_unpair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int,void* udata) {
  110. // VisualServerScene *self = (VisualServerScene*)p_self;
  111. Instance *A = p_A;
  112. Instance *B = p_B;
  113. //instance indices are designed so greater always contains lesser
  114. if (A->base_type > B->base_type) {
  115. SWAP(A,B); //lesser always first
  116. }
  117. if (B->base_type==VS::INSTANCE_LIGHT && (1<<A->base_type)&VS::INSTANCE_GEOMETRY_MASK) {
  118. InstanceLightData * light = static_cast<InstanceLightData*>(B->base_data);
  119. InstanceGeometryData * geom = static_cast<InstanceGeometryData*>(A->base_data);
  120. List<InstanceLightData::PairInfo>::Element *E = reinterpret_cast<List<InstanceLightData::PairInfo>::Element*>(udata);
  121. geom->lighting.erase(E->get().L);
  122. light->geometries.erase(E);
  123. if (geom->can_cast_shadows) {
  124. light->shadow_dirty=true;
  125. }
  126. geom->lighting_dirty=true;
  127. } else if (B->base_type==VS::INSTANCE_REFLECTION_PROBE && (1<<A->base_type)&VS::INSTANCE_GEOMETRY_MASK) {
  128. InstanceReflectionProbeData * reflection_probe = static_cast<InstanceReflectionProbeData*>(B->base_data);
  129. InstanceGeometryData * geom = static_cast<InstanceGeometryData*>(A->base_data);
  130. List<InstanceReflectionProbeData::PairInfo>::Element *E = reinterpret_cast<List<InstanceReflectionProbeData::PairInfo>::Element*>(udata);
  131. geom->reflection_probes.erase(E->get().L);
  132. reflection_probe->geometries.erase(E);
  133. geom->reflection_dirty=true;
  134. }
  135. #if 0
  136. if (A->base_type==INSTANCE_PORTAL) {
  137. ERR_FAIL_COND( B->base_type!=INSTANCE_PORTAL );
  138. A->portal_info->candidate_set.erase(B);
  139. B->portal_info->candidate_set.erase(A);
  140. //after disconnecting them, see if they can connect again
  141. self->_portal_attempt_connect(A);
  142. self->_portal_attempt_connect(B);
  143. } else if (A->base_type==INSTANCE_BAKED_LIGHT || B->base_type==INSTANCE_BAKED_LIGHT) {
  144. if (B->base_type==INSTANCE_BAKED_LIGHT) {
  145. SWAP(A,B);
  146. }
  147. ERR_FAIL_COND(B->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER);
  148. B->baked_light_sampler_info->baked_lights.erase(A);
  149. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  150. if (B->base_type==INSTANCE_ROOM)
  151. SWAP(A,B);
  152. ERR_FAIL_COND(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ));
  153. B->auto_rooms.erase(A);
  154. B->valid_auto_rooms.erase(A);
  155. A->room_info->owned_autoroom_geometry.erase(B);
  156. }else {
  157. if (B->base_type==INSTANCE_LIGHT) {
  158. SWAP(A,B);
  159. } else if (A->base_type!=INSTANCE_LIGHT) {
  160. return;
  161. }
  162. A->light_info->affected.erase(B);
  163. B->lights.erase(A);
  164. B->light_cache_dirty=true;
  165. }
  166. #endif
  167. }
  168. RID VisualServerScene::scenario_create() {
  169. Scenario *scenario = memnew( Scenario );
  170. ERR_FAIL_COND_V(!scenario,RID());
  171. RID scenario_rid = scenario_owner.make_rid( scenario );
  172. scenario->self=scenario_rid;
  173. scenario->octree.set_pair_callback(_instance_pair,this);
  174. scenario->octree.set_unpair_callback(_instance_unpair,this);
  175. scenario->reflection_probe_shadow_atlas=VSG::scene_render->shadow_atlas_create();
  176. VSG::scene_render->shadow_atlas_set_size(scenario->reflection_probe_shadow_atlas,1024); //make enough shadows for close distance, don't bother with rest
  177. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas,0,4);
  178. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas,1,4);
  179. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas,2,4);
  180. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas,3,8);
  181. scenario->reflection_atlas=VSG::scene_render->reflection_atlas_create();
  182. return scenario_rid;
  183. }
  184. void VisualServerScene::scenario_set_debug(RID p_scenario,VS::ScenarioDebugMode p_debug_mode) {
  185. Scenario *scenario = scenario_owner.get(p_scenario);
  186. ERR_FAIL_COND(!scenario);
  187. scenario->debug=p_debug_mode;
  188. }
  189. void VisualServerScene::scenario_set_environment(RID p_scenario, RID p_environment) {
  190. Scenario *scenario = scenario_owner.get(p_scenario);
  191. ERR_FAIL_COND(!scenario);
  192. scenario->environment=p_environment;
  193. }
  194. void VisualServerScene::scenario_set_fallback_environment(RID p_scenario, RID p_environment) {
  195. Scenario *scenario = scenario_owner.get(p_scenario);
  196. ERR_FAIL_COND(!scenario);
  197. scenario->fallback_environment=p_environment;
  198. }
  199. void VisualServerScene::scenario_set_reflection_atlas_size(RID p_scenario, int p_size,int p_subdiv) {
  200. Scenario *scenario = scenario_owner.get(p_scenario);
  201. ERR_FAIL_COND(!scenario);
  202. VSG::scene_render->reflection_atlas_set_size(scenario->reflection_atlas,p_size);
  203. VSG::scene_render->reflection_atlas_set_subdivision(scenario->reflection_atlas,p_subdiv);
  204. }
  205. /* INSTANCING API */
  206. void VisualServerScene::_instance_queue_update(Instance *p_instance,bool p_update_aabb,bool p_update_materials) {
  207. if (p_update_aabb)
  208. p_instance->update_aabb=true;
  209. if (p_update_materials)
  210. p_instance->update_materials=true;
  211. if (p_instance->update_item.in_list())
  212. return;
  213. _instance_update_list.add(&p_instance->update_item);
  214. }
  215. // from can be mesh, light, area and portal so far.
  216. RID VisualServerScene::instance_create(){
  217. Instance *instance = memnew( Instance );
  218. ERR_FAIL_COND_V(!instance,RID());
  219. RID instance_rid = instance_owner.make_rid(instance);
  220. instance->self=instance_rid;
  221. return instance_rid;
  222. }
  223. void VisualServerScene::instance_set_base(RID p_instance, RID p_base){
  224. Instance *instance = instance_owner.get( p_instance );
  225. ERR_FAIL_COND( !instance );
  226. Scenario *scenario = instance->scenario;
  227. if (instance->base_type!=VS::INSTANCE_NONE) {
  228. //free anything related to that base
  229. VSG::storage->instance_remove_dependency(instance->base,instance);
  230. if (scenario && instance->octree_id) {
  231. scenario->octree.erase(instance->octree_id); //make dependencies generated by the octree go away
  232. instance->octree_id=0;
  233. }
  234. switch(instance->base_type) {
  235. case VS::INSTANCE_LIGHT: {
  236. InstanceLightData *light = static_cast<InstanceLightData*>(instance->base_data);
  237. if (instance->scenario && light->D) {
  238. instance->scenario->directional_lights.erase( light->D );
  239. light->D=NULL;
  240. }
  241. VSG::scene_render->free(light->instance);
  242. } break;
  243. case VS::INSTANCE_REFLECTION_PROBE: {
  244. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData*>(instance->base_data);
  245. VSG::scene_render->free(reflection_probe->instance);
  246. if (reflection_probe->update_list.in_list()) {
  247. reflection_probe_render_list.remove(&reflection_probe->update_list);
  248. }
  249. } break;
  250. }
  251. if (instance->base_data) {
  252. memdelete( instance->base_data );
  253. instance->base_data=NULL;
  254. }
  255. instance->morph_values.clear();
  256. for(int i=0;i<instance->materials.size();i++) {
  257. if (instance->materials[i].is_valid()) {
  258. VSG::storage->material_remove_instance_owner(instance->materials[i],instance);
  259. }
  260. }
  261. instance->materials.clear();
  262. #if 0
  263. if (instance->light_info) {
  264. if (instance->scenario && instance->light_info->D)
  265. instance->scenario->directional_lights.erase( instance->light_info->D );
  266. rasterizer->free(instance->light_info->instance);
  267. memdelete(instance->light_info);
  268. instance->light_info=NULL;
  269. }
  270. if ( instance->room ) {
  271. instance_set_room(p_instance,RID());
  272. /*
  273. if((1<<instance->base_type)&INSTANCE_GEOMETRY_MASK)
  274. instance->room->room_info->owned_geometry_instances.erase(instance->RE);
  275. else if (instance->base_type==INSTANCE_PORTAL) {
  276. print_line("freeing portal, is it there? "+itos(instance->room->room_info->owned_portal_instances.(instance->RE)));
  277. instance->room->room_info->owned_portal_instances.erase(instance->RE);
  278. } else if (instance->base_type==INSTANCE_ROOM)
  279. instance->room->room_info->owned_room_instances.erase(instance->RE);
  280. else if (instance->base_type==INSTANCE_LIGHT)
  281. instance->room->room_info->owned_light_instances.erase(instance->RE);
  282. instance->RE=NULL;*/
  283. }
  284. if (instance->portal_info) {
  285. _portal_disconnect(instance,true);
  286. memdelete(instance->portal_info);
  287. instance->portal_info=NULL;
  288. }
  289. if (instance->baked_light_info) {
  290. while(instance->baked_light_info->owned_instances.size()) {
  291. Instance *owned=instance->baked_light_info->owned_instances.front()->get();
  292. owned->baked_light=NULL;
  293. owned->data.baked_light=NULL;
  294. owned->data.baked_light_octree_xform=NULL;
  295. owned->BLE=NULL;
  296. instance->baked_light_info->owned_instances.pop_front();
  297. }
  298. memdelete(instance->baked_light_info);
  299. instance->baked_light_info=NULL;
  300. }
  301. if (instance->scenario && instance->octree_id) {
  302. instance->scenario->octree.erase( instance->octree_id );
  303. instance->octree_id=0;
  304. }
  305. if (instance->room_info) {
  306. for(List<Instance*>::Element *E=instance->room_info->owned_geometry_instances.front();E;E=E->next()) {
  307. Instance *owned = E->get();
  308. owned->room=NULL;
  309. owned->RE=NULL;
  310. }
  311. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) {
  312. _portal_disconnect(E->get(),true);
  313. Instance *owned = E->get();
  314. owned->room=NULL;
  315. owned->RE=NULL;
  316. }
  317. for(List<Instance*>::Element *E=instance->room_info->owned_room_instances.front();E;E=E->next()) {
  318. Instance *owned = E->get();
  319. owned->room=NULL;
  320. owned->RE=NULL;
  321. }
  322. if (instance->room_info->disconnected_child_portals.size()) {
  323. ERR_PRINT("BUG: Disconnected portals remain!");
  324. }
  325. memdelete(instance->room_info);
  326. instance->room_info=NULL;
  327. }
  328. if (instance->particles_info) {
  329. rasterizer->free( instance->particles_info->instance );
  330. memdelete(instance->particles_info);
  331. instance->particles_info=NULL;
  332. }
  333. if (instance->baked_light_sampler_info) {
  334. while (instance->baked_light_sampler_info->owned_instances.size()) {
  335. instance_geometry_set_baked_light_sampler(instance->baked_light_sampler_info->owned_instances.front()->get()->self,RID());
  336. }
  337. if (instance->baked_light_sampler_info->sampled_light.is_valid()) {
  338. rasterizer->free(instance->baked_light_sampler_info->sampled_light);
  339. }
  340. memdelete( instance->baked_light_sampler_info );
  341. instance->baked_light_sampler_info=NULL;
  342. }
  343. #endif
  344. }
  345. instance->base_type=VS::INSTANCE_NONE;
  346. instance->base=RID();
  347. if (p_base.is_valid()) {
  348. instance->base_type=VSG::storage->get_base_type(p_base);
  349. ERR_FAIL_COND(instance->base_type==VS::INSTANCE_NONE);
  350. switch(instance->base_type) {
  351. case VS::INSTANCE_LIGHT: {
  352. InstanceLightData *light = memnew( InstanceLightData );
  353. if (scenario && VSG::storage->light_get_type(p_base)==VS::LIGHT_DIRECTIONAL) {
  354. light->D = scenario->directional_lights.push_back(instance);
  355. }
  356. light->instance = VSG::scene_render->light_instance_create(p_base);
  357. instance->base_data=light;
  358. } break;
  359. case VS::INSTANCE_MESH:
  360. case VS::INSTANCE_MULTIMESH:
  361. case VS::INSTANCE_IMMEDIATE: {
  362. InstanceGeometryData *geom = memnew( InstanceGeometryData );
  363. instance->base_data=geom;
  364. } break;
  365. case VS::INSTANCE_REFLECTION_PROBE: {
  366. InstanceReflectionProbeData *reflection_probe = memnew( InstanceReflectionProbeData );
  367. reflection_probe->owner=instance;
  368. instance->base_data=reflection_probe;
  369. reflection_probe->instance=VSG::scene_render->reflection_probe_instance_create(p_base);
  370. } break;
  371. }
  372. VSG::storage->instance_add_dependency(p_base,instance);
  373. instance->base=p_base;
  374. if (scenario)
  375. _instance_queue_update(instance,true,true);
  376. #if 0
  377. if (rasterizer->is_mesh(p_base)) {
  378. instance->base_type=INSTANCE_MESH;
  379. instance->data.morph_values.resize( rasterizer->mesh_get_morph_target_count(p_base));
  380. instance->data.materials.resize( rasterizer->mesh_get_surface_count(p_base));
  381. } else if (rasterizer->is_multimesh(p_base)) {
  382. instance->base_type=INSTANCE_MULTIMESH;
  383. } else if (rasterizer->is_immediate(p_base)) {
  384. instance->base_type=INSTANCE_IMMEDIATE;
  385. } else if (rasterizer->is_particles(p_base)) {
  386. instance->base_type=INSTANCE_PARTICLES;
  387. instance->particles_info=memnew( Instance::ParticlesInfo );
  388. instance->particles_info->instance = rasterizer->particles_instance_create( p_base );
  389. } else if (rasterizer->is_light(p_base)) {
  390. instance->base_type=INSTANCE_LIGHT;
  391. instance->light_info = memnew( Instance::LightInfo );
  392. instance->light_info->instance = rasterizer->light_instance_create(p_base);
  393. if (instance->scenario && rasterizer->light_get_type(p_base)==LIGHT_DIRECTIONAL) {
  394. instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self);
  395. }
  396. } else if (room_owner.owns(p_base)) {
  397. instance->base_type=INSTANCE_ROOM;
  398. instance->room_info = memnew( Instance::RoomInfo );
  399. instance->room_info->room=room_owner.get(p_base);
  400. } else if (portal_owner.owns(p_base)) {
  401. instance->base_type=INSTANCE_PORTAL;
  402. instance->portal_info = memnew(Instance::PortalInfo);
  403. instance->portal_info->portal=portal_owner.get(p_base);
  404. } else if (baked_light_owner.owns(p_base)) {
  405. instance->base_type=INSTANCE_BAKED_LIGHT;
  406. instance->baked_light_info=memnew(Instance::BakedLightInfo);
  407. instance->baked_light_info->baked_light=baked_light_owner.get(p_base);
  408. //instance->portal_info = memnew(Instance::PortalInfo);
  409. //instance->portal_info->portal=portal_owner.get(p_base);
  410. } else if (baked_light_sampler_owner.owns(p_base)) {
  411. instance->base_type=INSTANCE_BAKED_LIGHT_SAMPLER;
  412. instance->baked_light_sampler_info=memnew( Instance::BakedLightSamplerInfo);
  413. instance->baked_light_sampler_info->sampler=baked_light_sampler_owner.get(p_base);
  414. //instance->portal_info = memnew(Instance::PortalInfo);
  415. //instance->portal_info->portal=portal_owner.get(p_base);
  416. } else {
  417. ERR_EXPLAIN("Invalid base RID for instance!")
  418. ERR_FAIL();
  419. }
  420. instance_dependency_map[ p_base ].insert( instance->self );
  421. #endif
  422. }
  423. }
  424. void VisualServerScene::instance_set_scenario(RID p_instance, RID p_scenario){
  425. Instance *instance = instance_owner.get( p_instance );
  426. ERR_FAIL_COND( !instance );
  427. if (instance->scenario) {
  428. instance->scenario->instances.remove( &instance->scenario_item );
  429. if (instance->octree_id) {
  430. instance->scenario->octree.erase(instance->octree_id); //make dependencies generated by the octree go away
  431. instance->octree_id=0;
  432. }
  433. switch(instance->base_type) {
  434. case VS::INSTANCE_LIGHT: {
  435. InstanceLightData *light = static_cast<InstanceLightData*>(instance->base_data);
  436. if (light->D) {
  437. instance->scenario->directional_lights.erase( light->D );
  438. light->D=NULL;
  439. }
  440. } break;
  441. case VS::INSTANCE_REFLECTION_PROBE: {
  442. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData*>(instance->base_data);
  443. VSG::scene_render->reflection_probe_release_atlas_index(reflection_probe->instance);
  444. } break;
  445. }
  446. instance->scenario=NULL;
  447. }
  448. if (p_scenario.is_valid()) {
  449. Scenario *scenario = scenario_owner.get( p_scenario );
  450. ERR_FAIL_COND(!scenario);
  451. instance->scenario=scenario;
  452. scenario->instances.add( &instance->scenario_item );
  453. switch(instance->base_type) {
  454. case VS::INSTANCE_LIGHT: {
  455. InstanceLightData *light = static_cast<InstanceLightData*>(instance->base_data);
  456. if (VSG::storage->light_get_type(instance->base)==VS::LIGHT_DIRECTIONAL) {
  457. light->D = scenario->directional_lights.push_back(instance);
  458. }
  459. } break;
  460. }
  461. _instance_queue_update(instance,true,true);
  462. }
  463. }
  464. void VisualServerScene::instance_set_layer_mask(RID p_instance, uint32_t p_mask){
  465. Instance *instance = instance_owner.get( p_instance );
  466. ERR_FAIL_COND( !instance );
  467. instance->layer_mask=p_mask;
  468. }
  469. void VisualServerScene::instance_set_transform(RID p_instance, const Transform& p_transform){
  470. Instance *instance = instance_owner.get( p_instance );
  471. ERR_FAIL_COND( !instance );
  472. if (instance->transform==p_transform)
  473. return; //must be checked to avoid worst evil
  474. instance->transform=p_transform;
  475. _instance_queue_update(instance,true);
  476. }
  477. void VisualServerScene::instance_attach_object_instance_ID(RID p_instance,ObjectID p_ID){
  478. Instance *instance = instance_owner.get( p_instance );
  479. ERR_FAIL_COND( !instance );
  480. instance->object_ID=p_ID;
  481. }
  482. void VisualServerScene::instance_set_morph_target_weight(RID p_instance,int p_shape, float p_weight){
  483. Instance *instance = instance_owner.get( p_instance );
  484. ERR_FAIL_COND( !instance );
  485. if (instance->update_item.in_list()) {
  486. _update_dirty_instance(instance);
  487. }
  488. ERR_FAIL_INDEX(p_shape,instance->morph_values.size());
  489. instance->morph_values[p_shape]=p_weight;
  490. }
  491. void VisualServerScene::instance_set_surface_material(RID p_instance,int p_surface, RID p_material){
  492. Instance *instance = instance_owner.get( p_instance );
  493. ERR_FAIL_COND( !instance );
  494. if (instance->update_item.in_list()) {
  495. _update_dirty_instance(instance);
  496. }
  497. ERR_FAIL_INDEX(p_surface,instance->materials.size());
  498. if (instance->materials[p_surface].is_valid()) {
  499. VSG::storage->material_remove_instance_owner(instance->materials[p_surface],instance);
  500. }
  501. instance->materials[p_surface]=p_material;
  502. instance->base_material_changed();
  503. if (instance->materials[p_surface].is_valid()) {
  504. VSG::storage->material_add_instance_owner(instance->materials[p_surface],instance);
  505. }
  506. }
  507. void VisualServerScene::instance_attach_skeleton(RID p_instance,RID p_skeleton){
  508. Instance *instance = instance_owner.get( p_instance );
  509. ERR_FAIL_COND( !instance );
  510. if (instance->skeleton==p_skeleton)
  511. return;
  512. if (instance->skeleton.is_valid()) {
  513. VSG::storage->instance_remove_skeleton(p_skeleton,instance);
  514. }
  515. instance->skeleton=p_skeleton;
  516. if (instance->skeleton.is_valid()) {
  517. VSG::storage->instance_add_skeleton(p_skeleton,instance);
  518. }
  519. _instance_queue_update(instance,true);
  520. }
  521. void VisualServerScene::instance_set_exterior( RID p_instance, bool p_enabled ){
  522. }
  523. void VisualServerScene::instance_set_room( RID p_instance, RID p_room ){
  524. }
  525. void VisualServerScene::instance_set_extra_visibility_margin( RID p_instance, real_t p_margin ){
  526. }
  527. Vector<ObjectID> VisualServerScene::instances_cull_aabb(const AABB& p_aabb, RID p_scenario) const {
  528. Vector<ObjectID> instances;
  529. Scenario *scenario=scenario_owner.get(p_scenario);
  530. ERR_FAIL_COND_V(!scenario,instances);
  531. const_cast<VisualServerScene*>(this)->update_dirty_instances(); // check dirty instances before culling
  532. int culled=0;
  533. Instance *cull[1024];
  534. culled=scenario->octree.cull_AABB(p_aabb,cull,1024);
  535. for (int i=0;i<culled;i++) {
  536. Instance *instance=cull[i];
  537. ERR_CONTINUE(!instance);
  538. if (instance->object_ID==0)
  539. continue;
  540. instances.push_back(instance->object_ID);
  541. }
  542. return instances;
  543. }
  544. Vector<ObjectID> VisualServerScene::instances_cull_ray(const Vector3& p_from, const Vector3& p_to, RID p_scenario) const{
  545. Vector<ObjectID> instances;
  546. Scenario *scenario=scenario_owner.get(p_scenario);
  547. ERR_FAIL_COND_V(!scenario,instances);
  548. const_cast<VisualServerScene*>(this)->update_dirty_instances(); // check dirty instances before culling
  549. int culled=0;
  550. Instance *cull[1024];
  551. culled=scenario->octree.cull_segment(p_from,p_to*10000,cull,1024);
  552. for (int i=0;i<culled;i++) {
  553. Instance *instance=cull[i];
  554. ERR_CONTINUE(!instance);
  555. if (instance->object_ID==0)
  556. continue;
  557. instances.push_back(instance->object_ID);
  558. }
  559. return instances;
  560. }
  561. Vector<ObjectID> VisualServerScene::instances_cull_convex(const Vector<Plane>& p_convex, RID p_scenario) const{
  562. Vector<ObjectID> instances;
  563. Scenario *scenario=scenario_owner.get(p_scenario);
  564. ERR_FAIL_COND_V(!scenario,instances);
  565. const_cast<VisualServerScene*>(this)->update_dirty_instances(); // check dirty instances before culling
  566. int culled=0;
  567. Instance *cull[1024];
  568. culled=scenario->octree.cull_convex(p_convex,cull,1024);
  569. for (int i=0;i<culled;i++) {
  570. Instance *instance=cull[i];
  571. ERR_CONTINUE(!instance);
  572. if (instance->object_ID==0)
  573. continue;
  574. instances.push_back(instance->object_ID);
  575. }
  576. return instances;
  577. }
  578. void VisualServerScene::instance_geometry_set_flag(RID p_instance,VS::InstanceFlags p_flags,bool p_enabled){
  579. Instance *instance = instance_owner.get( p_instance );
  580. ERR_FAIL_COND( !instance );
  581. switch(p_flags) {
  582. case VS::INSTANCE_FLAG_VISIBLE: {
  583. instance->visible=p_enabled;
  584. } break;
  585. case VS::INSTANCE_FLAG_BILLBOARD: {
  586. instance->billboard=p_enabled;
  587. } break;
  588. case VS::INSTANCE_FLAG_BILLBOARD_FIX_Y: {
  589. instance->billboard_y=p_enabled;
  590. } break;
  591. case VS::INSTANCE_FLAG_CAST_SHADOW: {
  592. if (p_enabled == true) {
  593. instance->cast_shadows = VS::SHADOW_CASTING_SETTING_ON;
  594. }
  595. else {
  596. instance->cast_shadows = VS::SHADOW_CASTING_SETTING_OFF;
  597. }
  598. instance->base_material_changed(); // to actually compute if shadows are visible or not
  599. } break;
  600. case VS::INSTANCE_FLAG_DEPH_SCALE: {
  601. instance->depth_scale=p_enabled;
  602. } break;
  603. case VS::INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: {
  604. instance->visible_in_all_rooms=p_enabled;
  605. } break;
  606. }
  607. }
  608. void VisualServerScene::instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting) {
  609. }
  610. void VisualServerScene::instance_geometry_set_material_override(RID p_instance, RID p_material){
  611. Instance *instance = instance_owner.get( p_instance );
  612. ERR_FAIL_COND( !instance );
  613. if (instance->material_override.is_valid()) {
  614. VSG::storage->material_remove_instance_owner(instance->material_override,instance);
  615. }
  616. instance->material_override=p_material;
  617. instance->base_material_changed();
  618. if (instance->material_override.is_valid()) {
  619. VSG::storage->material_add_instance_owner(instance->material_override,instance);
  620. }
  621. }
  622. void VisualServerScene::instance_geometry_set_draw_range(RID p_instance,float p_min,float p_max,float p_min_margin,float p_max_margin){
  623. }
  624. void VisualServerScene::instance_geometry_set_as_instance_lod(RID p_instance,RID p_as_lod_of_instance){
  625. }
  626. void VisualServerScene::_update_instance(Instance *p_instance) {
  627. p_instance->version++;
  628. if (p_instance->base_type == VS::INSTANCE_LIGHT) {
  629. InstanceLightData *light = static_cast<InstanceLightData*>(p_instance->base_data);
  630. VSG::scene_render->light_instance_set_transform( light->instance, p_instance->transform );
  631. light->shadow_dirty=true;
  632. }
  633. if (p_instance->base_type == VS::INSTANCE_REFLECTION_PROBE) {
  634. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData*>(p_instance->base_data);
  635. VSG::scene_render->reflection_probe_instance_set_transform( reflection_probe->instance, p_instance->transform );
  636. reflection_probe->reflection_dirty=true;
  637. }
  638. if (p_instance->aabb.has_no_surface())
  639. return;
  640. #if 0
  641. if (p_instance->base_type == VS::INSTANCE_PARTICLES) {
  642. rasterizer->particles_instance_set_transform( p_instance->particles_info->instance, p_instance->data.transform );
  643. }
  644. #endif
  645. if ((1<<p_instance->base_type)&VS::INSTANCE_GEOMETRY_MASK) {
  646. InstanceGeometryData *geom = static_cast<InstanceGeometryData*>(p_instance->base_data);
  647. //make sure lights are updated if it casts shadow
  648. if (geom->can_cast_shadows) {
  649. for (List<Instance*>::Element *E=geom->lighting.front();E;E=E->next()) {
  650. InstanceLightData *light = static_cast<InstanceLightData*>(E->get()->base_data);
  651. light->shadow_dirty=true;
  652. }
  653. }
  654. }
  655. #if 0
  656. else if (p_instance->base_type == INSTANCE_ROOM) {
  657. p_instance->room_info->affine_inverse=p_instance->data.transform.affine_inverse();
  658. } else if (p_instance->base_type == INSTANCE_BAKED_LIGHT) {
  659. Transform scale;
  660. scale.basis.scale(p_instance->baked_light_info->baked_light->octree_aabb.size);
  661. scale.origin=p_instance->baked_light_info->baked_light->octree_aabb.pos;
  662. //print_line("scale: "+scale);
  663. p_instance->baked_light_info->affine_inverse=(p_instance->data.transform*scale).affine_inverse();
  664. }
  665. #endif
  666. p_instance->mirror = p_instance->transform.basis.determinant() < 0.0;
  667. AABB new_aabb;
  668. #if 0
  669. if (p_instance->base_type==INSTANCE_PORTAL) {
  670. //portals need to be transformed in a special way, so they don't become too wide if they have scale..
  671. Transform portal_xform = p_instance->data.transform;
  672. portal_xform.basis.set_axis(2,portal_xform.basis.get_axis(2).normalized());
  673. p_instance->portal_info->plane_cache=Plane( p_instance->data.transform.origin, portal_xform.basis.get_axis(2));
  674. int point_count=p_instance->portal_info->portal->shape.size();
  675. p_instance->portal_info->transformed_point_cache.resize(point_count);
  676. AABB portal_aabb;
  677. for(int i=0;i<point_count;i++) {
  678. Point2 src = p_instance->portal_info->portal->shape[i];
  679. Vector3 point = portal_xform.xform(Vector3(src.x,src.y,0));
  680. p_instance->portal_info->transformed_point_cache[i]=point;
  681. if (i==0)
  682. portal_aabb.pos=point;
  683. else
  684. portal_aabb.expand_to(point);
  685. }
  686. portal_aabb.grow_by(p_instance->portal_info->portal->connect_range);
  687. new_aabb = portal_aabb;
  688. } else {
  689. #endif
  690. new_aabb = p_instance->transform.xform(p_instance->aabb);
  691. #if 0
  692. }
  693. #endif
  694. p_instance->transformed_aabb=new_aabb;
  695. if (!p_instance->scenario) {
  696. return;
  697. }
  698. if (p_instance->octree_id==0) {
  699. uint32_t base_type = 1<<p_instance->base_type;
  700. uint32_t pairable_mask=0;
  701. bool pairable=false;
  702. if (p_instance->base_type == VS::INSTANCE_LIGHT || p_instance->base_type==VS::INSTANCE_REFLECTION_PROBE) {
  703. pairable_mask=p_instance->visible?VS::INSTANCE_GEOMETRY_MASK:0;
  704. pairable=true;
  705. }
  706. #if 0
  707. if (p_instance->base_type == VS::INSTANCE_PORTAL) {
  708. pairable_mask=(1<<INSTANCE_PORTAL);
  709. pairable=true;
  710. }
  711. if (p_instance->base_type == VS::INSTANCE_BAKED_LIGHT_SAMPLER) {
  712. pairable_mask=(1<<INSTANCE_BAKED_LIGHT);
  713. pairable=true;
  714. }
  715. if (!p_instance->room && (1<<p_instance->base_type)&VS::INSTANCE_GEOMETRY_MASK) {
  716. base_type|=VS::INSTANCE_ROOMLESS_MASK;
  717. }
  718. if (p_instance->base_type == VS::INSTANCE_ROOM) {
  719. pairable_mask=INSTANCE_ROOMLESS_MASK;
  720. pairable=true;
  721. }
  722. #endif
  723. // not inside octree
  724. p_instance->octree_id = p_instance->scenario->octree.create(p_instance,new_aabb,0,pairable,base_type,pairable_mask);
  725. } else {
  726. // if (new_aabb==p_instance->data.transformed_aabb)
  727. // return;
  728. p_instance->scenario->octree.move(p_instance->octree_id,new_aabb);
  729. }
  730. #if 0
  731. if (p_instance->base_type==INSTANCE_PORTAL) {
  732. _portal_attempt_connect(p_instance);
  733. }
  734. if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  735. _instance_validate_autorooms(p_instance);
  736. }
  737. if (p_instance->base_type == INSTANCE_ROOM) {
  738. for(Set<Instance*>::Element *E=p_instance->room_info->owned_autoroom_geometry.front();E;E=E->next())
  739. _instance_validate_autorooms(E->get());
  740. }
  741. #endif
  742. }
  743. void VisualServerScene::_update_instance_aabb(Instance *p_instance) {
  744. AABB new_aabb;
  745. ERR_FAIL_COND(p_instance->base_type!=VS::INSTANCE_NONE && !p_instance->base.is_valid());
  746. switch(p_instance->base_type) {
  747. case VisualServer::INSTANCE_NONE: {
  748. // do nothing
  749. } break;
  750. case VisualServer::INSTANCE_MESH: {
  751. new_aabb = VSG::storage->mesh_get_aabb(p_instance->base,p_instance->skeleton);
  752. } break;
  753. case VisualServer::INSTANCE_MULTIMESH: {
  754. new_aabb = VSG::storage->multimesh_get_aabb(p_instance->base);
  755. } break;
  756. case VisualServer::INSTANCE_IMMEDIATE: {
  757. new_aabb = VSG::storage->immediate_get_aabb(p_instance->base);
  758. } break;
  759. #if 0
  760. case VisualServer::INSTANCE_PARTICLES: {
  761. new_aabb = rasterizer->particles_get_aabb(p_instance->base);
  762. } break;
  763. #endif
  764. case VisualServer::INSTANCE_LIGHT: {
  765. new_aabb = VSG::storage->light_get_aabb(p_instance->base);
  766. } break;
  767. case VisualServer::INSTANCE_REFLECTION_PROBE: {
  768. new_aabb = VSG::storage->reflection_probe_get_aabb(p_instance->base);
  769. } break;
  770. #if 0
  771. case VisualServer::INSTANCE_ROOM: {
  772. Room *room = room_owner.get( p_instance->base );
  773. ERR_FAIL_COND(!room);
  774. new_aabb=room->bounds.get_aabb();
  775. } break;
  776. case VisualServer::INSTANCE_PORTAL: {
  777. Portal *portal = portal_owner.get( p_instance->base );
  778. ERR_FAIL_COND(!portal);
  779. for (int i=0;i<portal->shape.size();i++) {
  780. Vector3 point( portal->shape[i].x, portal->shape[i].y, 0 );
  781. if (i==0) {
  782. new_aabb.pos=point;
  783. new_aabb.size.z=0.01; // make it not flat for octree
  784. } else {
  785. new_aabb.expand_to(point);
  786. }
  787. }
  788. } break;
  789. case VisualServer::INSTANCE_BAKED_LIGHT: {
  790. BakedLight *baked_light = baked_light_owner.get( p_instance->base );
  791. ERR_FAIL_COND(!baked_light);
  792. new_aabb=baked_light->octree_aabb;
  793. } break;
  794. case VisualServer::INSTANCE_BAKED_LIGHT_SAMPLER: {
  795. BakedLightSampler *baked_light_sampler = baked_light_sampler_owner.get( p_instance->base );
  796. ERR_FAIL_COND(!baked_light_sampler);
  797. float radius = baked_light_sampler->params[VS::BAKED_LIGHT_SAMPLER_RADIUS];
  798. new_aabb=AABB(Vector3(-radius,-radius,-radius),Vector3(radius*2,radius*2,radius*2));
  799. } break;
  800. #endif
  801. default: {}
  802. }
  803. if (p_instance->extra_margin)
  804. new_aabb.grow_by(p_instance->extra_margin);
  805. p_instance->aabb=new_aabb;
  806. }
  807. void VisualServerScene::_light_instance_update_shadow(Instance *p_instance,const Transform p_cam_transform,const CameraMatrix& p_cam_projection,bool p_cam_orthogonal,RID p_shadow_atlas,Scenario* p_scenario) {
  808. InstanceLightData * light = static_cast<InstanceLightData*>(p_instance->base_data);
  809. switch(VSG::storage->light_get_type(p_instance->base)) {
  810. case VS::LIGHT_DIRECTIONAL: {
  811. float max_distance =p_cam_projection.get_z_far();
  812. float shadow_max = VSG::storage->light_get_param(p_instance->base,VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE);
  813. if (shadow_max>0) {
  814. max_distance=MIN(shadow_max,max_distance);
  815. }
  816. max_distance=MAX(max_distance,p_cam_projection.get_z_near()+0.001);
  817. float range = max_distance-p_cam_projection.get_z_near();
  818. int splits=0;
  819. switch(VSG::storage->light_directional_get_shadow_mode(p_instance->base)) {
  820. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: splits=1; break;
  821. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: splits=2; break;
  822. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: splits=4; break;
  823. }
  824. float distances[5];
  825. distances[0]=p_cam_projection.get_z_near();
  826. for(int i=0;i<splits;i++) {
  827. distances[i+1]=p_cam_projection.get_z_near()+VSG::storage->light_get_param(p_instance->base,VS::LightParam(VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET+i))*range;
  828. };
  829. distances[splits]=max_distance;
  830. float texture_size=VSG::scene_render->get_directional_light_shadow_size(light->instance);
  831. bool overlap = VSG::storage->light_directional_get_blend_splits(p_instance->base);
  832. for (int i=0;i<splits;i++) {
  833. // setup a camera matrix for that range!
  834. CameraMatrix camera_matrix;
  835. float aspect = p_cam_projection.get_aspect();
  836. if (p_cam_orthogonal) {
  837. float w,h;
  838. p_cam_projection.get_viewport_size(w,h);
  839. camera_matrix.set_orthogonal(w,aspect,distances[(i==0 || !overlap )?i:i-1],distances[i+1],false);
  840. } else {
  841. float fov = p_cam_projection.get_fov();
  842. camera_matrix.set_perspective(fov,aspect,distances[(i==0 || !overlap )?i:i-1],distances[i+1],false);
  843. }
  844. //obtain the frustum endpoints
  845. Vector3 endpoints[8]; // frustum plane endpoints
  846. bool res = camera_matrix.get_endpoints(p_cam_transform,endpoints);
  847. ERR_CONTINUE(!res);
  848. // obtain the light frustm ranges (given endpoints)
  849. Vector3 x_vec=p_instance->transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  850. Vector3 y_vec=p_instance->transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  851. Vector3 z_vec=p_instance->transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  852. //z_vec points agsint the camera, like in default opengl
  853. float x_min,x_max;
  854. float y_min,y_max;
  855. float z_min,z_max;
  856. float x_min_cam,x_max_cam;
  857. float y_min_cam,y_max_cam;
  858. float z_min_cam,z_max_cam;
  859. //used for culling
  860. for(int j=0;j<8;j++) {
  861. float d_x=x_vec.dot(endpoints[j]);
  862. float d_y=y_vec.dot(endpoints[j]);
  863. float d_z=z_vec.dot(endpoints[j]);
  864. if (j==0 || d_x<x_min)
  865. x_min=d_x;
  866. if (j==0 || d_x>x_max)
  867. x_max=d_x;
  868. if (j==0 || d_y<y_min)
  869. y_min=d_y;
  870. if (j==0 || d_y>y_max)
  871. y_max=d_y;
  872. if (j==0 || d_z<z_min)
  873. z_min=d_z;
  874. if (j==0 || d_z>z_max)
  875. z_max=d_z;
  876. }
  877. {
  878. //camera viewport stuff
  879. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  880. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  881. Vector3 center;
  882. for(int j=0;j<8;j++) {
  883. center+=endpoints[j];
  884. }
  885. center/=8.0;
  886. //center=x_vec*(x_max-x_min)*0.5 + y_vec*(y_max-y_min)*0.5 + z_vec*(z_max-z_min)*0.5;
  887. float radius=0;
  888. for(int j=0;j<8;j++) {
  889. float d = center.distance_to(endpoints[j]);
  890. if (d>radius)
  891. radius=d;
  892. }
  893. radius *= texture_size/(texture_size-2.0); //add a texel by each side, so stepified texture will always fit
  894. x_max_cam=x_vec.dot(center)+radius;
  895. x_min_cam=x_vec.dot(center)-radius;
  896. y_max_cam=y_vec.dot(center)+radius;
  897. y_min_cam=y_vec.dot(center)-radius;
  898. z_max_cam=z_vec.dot(center)+radius;
  899. z_min_cam=z_vec.dot(center)-radius;
  900. float unit = radius*2.0/texture_size;
  901. x_max_cam=Math::stepify(x_max_cam,unit);
  902. x_min_cam=Math::stepify(x_min_cam,unit);
  903. y_max_cam=Math::stepify(y_max_cam,unit);
  904. y_min_cam=Math::stepify(y_min_cam,unit);
  905. }
  906. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  907. Vector<Plane> light_frustum_planes;
  908. light_frustum_planes.resize(6);
  909. //right/left
  910. light_frustum_planes[0]=Plane( x_vec, x_max );
  911. light_frustum_planes[1]=Plane( -x_vec, -x_min );
  912. //top/bottom
  913. light_frustum_planes[2]=Plane( y_vec, y_max );
  914. light_frustum_planes[3]=Plane( -y_vec, -y_min );
  915. //near/far
  916. light_frustum_planes[4]=Plane( z_vec, z_max+1e6 );
  917. light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed
  918. int cull_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,VS::INSTANCE_GEOMETRY_MASK);
  919. // a pre pass will need to be needed to determine the actual z-near to be used
  920. for (int j=0;j<cull_count;j++) {
  921. float min,max;
  922. Instance *instance = instance_shadow_cull_result[j];
  923. if (!instance->visible || !((1<<instance->base_type)&VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData*>(instance->base_data)->can_cast_shadows) {
  924. cull_count--;
  925. SWAP(instance_shadow_cull_result[j],instance_shadow_cull_result[cull_count]);
  926. j--;
  927. }
  928. instance->transformed_aabb.project_range_in_plane(Plane(z_vec,0),min,max);
  929. if (max>z_max)
  930. z_max=max;
  931. }
  932. {
  933. CameraMatrix ortho_camera;
  934. real_t half_x = (x_max_cam-x_min_cam) * 0.5;
  935. real_t half_y = (y_max_cam-y_min_cam) * 0.5;
  936. ortho_camera.set_orthogonal( -half_x, half_x,-half_y,half_y, 0, (z_max-z_min_cam) );
  937. Transform ortho_transform;
  938. ortho_transform.basis=p_instance->transform.basis;
  939. ortho_transform.origin=x_vec*(x_min_cam+half_x)+y_vec*(y_min_cam+half_y)+z_vec*z_max;
  940. VSG::scene_render->light_instance_set_shadow_transform(light->instance,ortho_camera,ortho_transform,0,distances[i+1],i);
  941. }
  942. VSG::scene_render->render_shadow(light->instance,p_shadow_atlas,i,(RasterizerScene::InstanceBase**)instance_shadow_cull_result,cull_count);
  943. }
  944. } break;
  945. case VS::LIGHT_OMNI: {
  946. VS::LightOmniShadowMode shadow_mode = VSG::storage->light_omni_get_shadow_mode(p_instance->base);
  947. switch(shadow_mode) {
  948. case VS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID: {
  949. for(int i=0;i<2;i++) {
  950. //using this one ensures that raster deferred will have it
  951. float radius = VSG::storage->light_get_param( p_instance->base, VS::LIGHT_PARAM_RANGE);
  952. float z =i==0?-1:1;
  953. Vector<Plane> planes;
  954. planes.resize(5);
  955. planes[0]=p_instance->transform.xform(Plane(Vector3(0,0,z),radius));
  956. planes[1]=p_instance->transform.xform(Plane(Vector3(1,0,z).normalized(),radius));
  957. planes[2]=p_instance->transform.xform(Plane(Vector3(-1,0,z).normalized(),radius));
  958. planes[3]=p_instance->transform.xform(Plane(Vector3(0,1,z).normalized(),radius));
  959. planes[4]=p_instance->transform.xform(Plane(Vector3(0,-1,z).normalized(),radius));
  960. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,VS::INSTANCE_GEOMETRY_MASK);
  961. for (int j=0;j<cull_count;j++) {
  962. Instance *instance = instance_shadow_cull_result[j];
  963. if (!instance->visible || !((1<<instance->base_type)&VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData*>(instance->base_data)->can_cast_shadows) {
  964. cull_count--;
  965. SWAP(instance_shadow_cull_result[j],instance_shadow_cull_result[cull_count]);
  966. j--;
  967. }
  968. }
  969. VSG::scene_render->light_instance_set_shadow_transform(light->instance,CameraMatrix(),p_instance->transform,radius,0,i);
  970. VSG::scene_render->render_shadow(light->instance,p_shadow_atlas,i,(RasterizerScene::InstanceBase**)instance_shadow_cull_result,cull_count);
  971. }
  972. } break;
  973. case VS::LIGHT_OMNI_SHADOW_CUBE: {
  974. float radius = VSG::storage->light_get_param( p_instance->base, VS::LIGHT_PARAM_RANGE);
  975. CameraMatrix cm;
  976. cm.set_perspective(90,1,0.01,radius);
  977. for(int i=0;i<6;i++) {
  978. //using this one ensures that raster deferred will have it
  979. static const Vector3 view_normals[6]={
  980. Vector3(-1, 0, 0),
  981. Vector3(+1, 0, 0),
  982. Vector3( 0,-1, 0),
  983. Vector3( 0,+1, 0),
  984. Vector3( 0, 0,-1),
  985. Vector3( 0, 0,+1)
  986. };
  987. static const Vector3 view_up[6]={
  988. Vector3( 0,-1, 0),
  989. Vector3( 0,-1, 0),
  990. Vector3( 0, 0,-1),
  991. Vector3( 0, 0,+1),
  992. Vector3( 0,-1, 0),
  993. Vector3( 0,-1, 0)
  994. };
  995. Transform xform = p_instance->transform * Transform().looking_at(view_normals[i],view_up[i]);
  996. Vector<Plane> planes = cm.get_projection_planes(xform);
  997. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,VS::INSTANCE_GEOMETRY_MASK);
  998. for (int j=0;j<cull_count;j++) {
  999. Instance *instance = instance_shadow_cull_result[j];
  1000. if (!instance->visible || !((1<<instance->base_type)&VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData*>(instance->base_data)->can_cast_shadows) {
  1001. cull_count--;
  1002. SWAP(instance_shadow_cull_result[j],instance_shadow_cull_result[cull_count]);
  1003. j--;
  1004. }
  1005. }
  1006. VSG::scene_render->light_instance_set_shadow_transform(light->instance,cm,xform,radius,0,i);
  1007. VSG::scene_render->render_shadow(light->instance,p_shadow_atlas,i,(RasterizerScene::InstanceBase**)instance_shadow_cull_result,cull_count);
  1008. }
  1009. //restore the regular DP matrix
  1010. VSG::scene_render->light_instance_set_shadow_transform(light->instance,CameraMatrix(),p_instance->transform,radius,0,0);
  1011. } break;
  1012. }
  1013. } break;
  1014. case VS::LIGHT_SPOT: {
  1015. float radius = VSG::storage->light_get_param( p_instance->base, VS::LIGHT_PARAM_RANGE);
  1016. float angle = VSG::storage->light_get_param( p_instance->base, VS::LIGHT_PARAM_SPOT_ANGLE);
  1017. CameraMatrix cm;
  1018. cm.set_perspective( angle, 1.0, 0.01, radius );
  1019. Vector<Plane> planes = cm.get_projection_planes(p_instance->transform);
  1020. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,VS::INSTANCE_GEOMETRY_MASK);
  1021. for (int j=0;j<cull_count;j++) {
  1022. Instance *instance = instance_shadow_cull_result[j];
  1023. if (!instance->visible || !((1<<instance->base_type)&VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData*>(instance->base_data)->can_cast_shadows) {
  1024. cull_count--;
  1025. SWAP(instance_shadow_cull_result[j],instance_shadow_cull_result[cull_count]);
  1026. j--;
  1027. }
  1028. }
  1029. VSG::scene_render->light_instance_set_shadow_transform(light->instance,cm,p_instance->transform,radius,0,0);
  1030. VSG::scene_render->render_shadow(light->instance,p_shadow_atlas,0,(RasterizerScene::InstanceBase**)instance_shadow_cull_result,cull_count);
  1031. } break;
  1032. }
  1033. }
  1034. void VisualServerScene::render_camera(RID p_camera, RID p_scenario,Size2 p_viewport_size,RID p_shadow_atlas) {
  1035. Camera *camera = camera_owner.getornull(p_camera);
  1036. ERR_FAIL_COND(!camera);
  1037. /* STEP 1 - SETUP CAMERA */
  1038. CameraMatrix camera_matrix;
  1039. bool ortho=false;
  1040. switch(camera->type) {
  1041. case Camera::ORTHOGONAL: {
  1042. camera_matrix.set_orthogonal(
  1043. camera->size,
  1044. p_viewport_size.width / (float)p_viewport_size.height,
  1045. camera->znear,
  1046. camera->zfar,
  1047. camera->vaspect
  1048. );
  1049. ortho=true;
  1050. } break;
  1051. case Camera::PERSPECTIVE: {
  1052. camera_matrix.set_perspective(
  1053. camera->fov,
  1054. p_viewport_size.width / (float)p_viewport_size.height,
  1055. camera->znear,
  1056. camera->zfar,
  1057. camera->vaspect
  1058. );
  1059. ortho=false;
  1060. } break;
  1061. }
  1062. _render_scene(camera->transform,camera_matrix,ortho,camera->env,camera->visible_layers,p_scenario,p_shadow_atlas,RID(),-1);
  1063. }
  1064. void VisualServerScene::_render_scene(const Transform p_cam_transform,const CameraMatrix& p_cam_projection,bool p_cam_orthogonal,RID p_force_environment,uint32_t p_visible_layers, RID p_scenario,RID p_shadow_atlas,RID p_reflection_probe,int p_reflection_probe_pass) {
  1065. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1066. render_pass++;
  1067. uint32_t camera_layer_mask=p_visible_layers;
  1068. VSG::scene_render->set_scene_pass(render_pass);
  1069. // rasterizer->set_camera(camera->transform, camera_matrix,ortho);
  1070. Vector<Plane> planes = p_cam_projection.get_projection_planes(p_cam_transform);
  1071. Plane near_plane(p_cam_transform.origin,-p_cam_transform.basis.get_axis(2).normalized());
  1072. float z_far = p_cam_projection.get_z_far();
  1073. /* STEP 2 - CULL */
  1074. int cull_count = scenario->octree.cull_convex(planes,instance_cull_result,MAX_INSTANCE_CULL);
  1075. light_cull_count=0;
  1076. reflection_probe_cull_count=0;
  1077. // light_samplers_culled=0;
  1078. /* print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  1079. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  1080. // print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  1081. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  1082. */
  1083. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  1084. // compute portals
  1085. #if 0
  1086. exterior_visited=false;
  1087. exterior_portal_cull_count=0;
  1088. if (room_cull_enabled) {
  1089. for(int i=0;i<cull_count;i++) {
  1090. Instance *ins = instance_cull_result[i];
  1091. ins->last_render_pass=render_pass;
  1092. if (ins->base_type!=INSTANCE_PORTAL)
  1093. continue;
  1094. if (ins->room)
  1095. continue;
  1096. ERR_CONTINUE(exterior_portal_cull_count>=MAX_EXTERIOR_PORTALS);
  1097. exterior_portal_cull_result[exterior_portal_cull_count++]=ins;
  1098. }
  1099. room_cull_count = p_scenario->octree.cull_point(camera->transform.origin,room_cull_result,MAX_ROOM_CULL,NULL,(1<<INSTANCE_ROOM)|(1<<INSTANCE_PORTAL));
  1100. Set<Instance*> current_rooms;
  1101. Set<Instance*> portal_rooms;
  1102. //add to set
  1103. for(int i=0;i<room_cull_count;i++) {
  1104. if (room_cull_result[i]->base_type==INSTANCE_ROOM) {
  1105. current_rooms.insert(room_cull_result[i]);
  1106. }
  1107. if (room_cull_result[i]->base_type==INSTANCE_PORTAL) {
  1108. //assume inside that room if also inside the portal..
  1109. if (room_cull_result[i]->room) {
  1110. portal_rooms.insert(room_cull_result[i]->room);
  1111. }
  1112. SWAP(room_cull_result[i],room_cull_result[room_cull_count-1]);
  1113. room_cull_count--;
  1114. i--;
  1115. }
  1116. }
  1117. //remove from set if it has a parent room or BSP doesn't contain
  1118. for(int i=0;i<room_cull_count;i++) {
  1119. Instance *r = room_cull_result[i];
  1120. //check inside BSP
  1121. Vector3 room_local_point = r->room_info->affine_inverse.xform( camera->transform.origin );
  1122. if (!portal_rooms.has(r) && !r->room_info->room->bounds.point_is_inside(room_local_point)) {
  1123. current_rooms.erase(r);
  1124. continue;
  1125. }
  1126. //check parent
  1127. while (r->room) {// has parent room
  1128. current_rooms.erase(r);
  1129. r=r->room;
  1130. }
  1131. }
  1132. if (current_rooms.size()) {
  1133. //camera is inside a room
  1134. // go through rooms
  1135. for(Set<Instance*>::Element *E=current_rooms.front();E;E=E->next()) {
  1136. _cull_room(camera,E->get());
  1137. }
  1138. } else {
  1139. //start from exterior
  1140. _cull_room(camera,NULL);
  1141. }
  1142. }
  1143. #endif
  1144. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  1145. for(int i=0;i<cull_count;i++) {
  1146. Instance *ins = instance_cull_result[i];
  1147. bool keep=false;
  1148. if ((camera_layer_mask&ins->layer_mask)==0) {
  1149. //failure
  1150. } else if (ins->base_type==VS::INSTANCE_LIGHT && ins->visible) {
  1151. if (ins->visible && light_cull_count<MAX_LIGHTS_CULLED) {
  1152. InstanceLightData * light = static_cast<InstanceLightData*>(ins->base_data);
  1153. if (!light->geometries.empty()) {
  1154. //do not add this light if no geometry is affected by it..
  1155. light_cull_result[light_cull_count]=ins;
  1156. light_instance_cull_result[light_cull_count]=light->instance;
  1157. if (p_shadow_atlas.is_valid() && VSG::storage->light_has_shadow(ins->base)) {
  1158. VSG::scene_render->light_instance_mark_visible(light->instance); //mark it visible for shadow allocation later
  1159. }
  1160. light_cull_count++;
  1161. }
  1162. }
  1163. } else if (ins->base_type==VS::INSTANCE_REFLECTION_PROBE && ins->visible) {
  1164. if (ins->visible && reflection_probe_cull_count<MAX_REFLECTION_PROBES_CULLED) {
  1165. InstanceReflectionProbeData * reflection_probe = static_cast<InstanceReflectionProbeData*>(ins->base_data);
  1166. if (p_reflection_probe!=reflection_probe->instance) {
  1167. //avoid entering The Matrix
  1168. if (!reflection_probe->geometries.empty()) {
  1169. //do not add this light if no geometry is affected by it..
  1170. if (reflection_probe->reflection_dirty || VSG::scene_render->reflection_probe_instance_needs_redraw(reflection_probe->instance)) {
  1171. if (!reflection_probe->update_list.in_list()) {
  1172. reflection_probe->render_step=0;
  1173. reflection_probe_render_list.add(&reflection_probe->update_list);
  1174. }
  1175. reflection_probe->reflection_dirty=false;
  1176. }
  1177. if (VSG::scene_render->reflection_probe_instance_has_reflection(reflection_probe->instance)) {
  1178. reflection_probe_instance_cull_result[reflection_probe_cull_count]=reflection_probe->instance;
  1179. reflection_probe_cull_count++;
  1180. }
  1181. }
  1182. }
  1183. }
  1184. } else if ((1<<ins->base_type)&VS::INSTANCE_GEOMETRY_MASK && ins->visible && ins->cast_shadows!=VS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) {
  1185. keep=true;
  1186. #if 0
  1187. bool discarded=false;
  1188. if (ins->draw_range_end>0) {
  1189. float d = cull_range.nearp.distance_to(ins->data.transform.origin);
  1190. if (d<0)
  1191. d=0;
  1192. discarded=(d<ins->draw_range_begin || d>=ins->draw_range_end);
  1193. }
  1194. if (!discarded) {
  1195. // test if this geometry should be visible
  1196. if (room_cull_enabled) {
  1197. if (ins->visible_in_all_rooms) {
  1198. keep=true;
  1199. } else if (ins->room) {
  1200. if (ins->room->room_info->last_visited_pass==render_pass)
  1201. keep=true;
  1202. } else if (ins->auto_rooms.size()) {
  1203. for(Set<Instance*>::Element *E=ins->auto_rooms.front();E;E=E->next()) {
  1204. if (E->get()->room_info->last_visited_pass==render_pass) {
  1205. keep=true;
  1206. break;
  1207. }
  1208. }
  1209. } else if(exterior_visited)
  1210. keep=true;
  1211. } else {
  1212. keep=true;
  1213. }
  1214. }
  1215. if (keep) {
  1216. // update cull range
  1217. float min,max;
  1218. ins->transformed_aabb.project_range_in_plane(cull_range.nearp,min,max);
  1219. if (min<cull_range.min)
  1220. cull_range.min=min;
  1221. if (max>cull_range.max)
  1222. cull_range.max=max;
  1223. if (ins->sampled_light && ins->sampled_light->baked_light_sampler_info->last_pass!=render_pass) {
  1224. if (light_samplers_culled<MAX_LIGHT_SAMPLERS) {
  1225. light_sampler_cull_result[light_samplers_culled++]=ins->sampled_light;
  1226. ins->sampled_light->baked_light_sampler_info->last_pass=render_pass;
  1227. }
  1228. }
  1229. }
  1230. #endif
  1231. InstanceGeometryData * geom = static_cast<InstanceGeometryData*>(ins->base_data);
  1232. if (geom->lighting_dirty) {
  1233. int l=0;
  1234. //only called when lights AABB enter/exit this geometry
  1235. ins->light_instances.resize(geom->lighting.size());
  1236. for (List<Instance*>::Element *E=geom->lighting.front();E;E=E->next()) {
  1237. InstanceLightData * light = static_cast<InstanceLightData*>(E->get()->base_data);
  1238. ins->light_instances[l++]=light->instance;
  1239. }
  1240. geom->lighting_dirty=false;
  1241. }
  1242. if (geom->reflection_dirty) {
  1243. int l=0;
  1244. //only called when reflection probe AABB enter/exit this geometry
  1245. ins->reflection_probe_instances.resize(geom->reflection_probes.size());
  1246. for (List<Instance*>::Element *E=geom->reflection_probes.front();E;E=E->next()) {
  1247. InstanceReflectionProbeData * reflection_probe = static_cast<InstanceReflectionProbeData*>(E->get()->base_data);
  1248. ins->reflection_probe_instances[l++]=reflection_probe->instance;
  1249. }
  1250. geom->reflection_dirty=false;
  1251. }
  1252. ins->depth = near_plane.distance_to(ins->transform.origin);
  1253. ins->depth_layer=CLAMP(int(ins->depth*8/z_far),0,7);
  1254. }
  1255. if (!keep) {
  1256. // remove, no reason to keep
  1257. cull_count--;
  1258. SWAP( instance_cull_result[i], instance_cull_result[ cull_count ] );
  1259. i--;
  1260. ins->last_render_pass=0; // make invalid
  1261. } else {
  1262. ins->last_render_pass=render_pass;
  1263. }
  1264. }
  1265. /* STEP 5 - PROCESS LIGHTS */
  1266. RID *directional_light_ptr=&light_instance_cull_result[light_cull_count];
  1267. int directional_light_count=0;
  1268. // directional lights
  1269. {
  1270. Instance** lights_with_shadow = (Instance**)alloca(sizeof(Instance*)*light_cull_count);
  1271. int directional_shadow_count=0;
  1272. for (List<Instance*>::Element *E=scenario->directional_lights.front();E;E=E->next()) {
  1273. if (light_cull_count+directional_light_count>=MAX_LIGHTS_CULLED) {
  1274. break;
  1275. }
  1276. if (!E->get()->visible)
  1277. continue;
  1278. InstanceLightData * light = static_cast<InstanceLightData*>(E->get()->base_data);
  1279. //check shadow..
  1280. if (light && p_shadow_atlas.is_valid() && VSG::storage->light_has_shadow(E->get()->base)) {
  1281. lights_with_shadow[directional_shadow_count++]=E->get();
  1282. }
  1283. //add to list
  1284. directional_light_ptr[directional_light_count++]=light->instance;
  1285. }
  1286. VSG::scene_render->set_directional_shadow_count(directional_shadow_count);
  1287. for(int i=0;i<directional_shadow_count;i++) {
  1288. _light_instance_update_shadow(lights_with_shadow[i],p_cam_transform,p_cam_projection,p_cam_orthogonal,p_shadow_atlas,scenario);
  1289. }
  1290. }
  1291. { //setup shadow maps
  1292. //SortArray<Instance*,_InstanceLightsort> sorter;
  1293. //sorter.sort(light_cull_result,light_cull_count);
  1294. for (int i=0;i<light_cull_count;i++) {
  1295. Instance *ins = light_cull_result[i];
  1296. if (!p_shadow_atlas.is_valid() || !VSG::storage->light_has_shadow(ins->base))
  1297. continue;
  1298. InstanceLightData * light = static_cast<InstanceLightData*>(ins->base_data);
  1299. float coverage;
  1300. { //compute coverage
  1301. Transform cam_xf = p_cam_transform;
  1302. float zn = p_cam_projection.get_z_near();
  1303. Plane p (cam_xf.origin + cam_xf.basis.get_axis(2) * -zn, -cam_xf.basis.get_axis(2) ); //camera near plane
  1304. float vp_w,vp_h; //near plane size in screen coordinates
  1305. p_cam_projection.get_viewport_size(vp_w,vp_h);
  1306. switch(VSG::storage->light_get_type(ins->base)) {
  1307. case VS::LIGHT_OMNI: {
  1308. float radius = VSG::storage->light_get_param(ins->base,VS::LIGHT_PARAM_RANGE);
  1309. //get two points parallel to near plane
  1310. Vector3 points[2]={
  1311. ins->transform.origin,
  1312. ins->transform.origin+cam_xf.basis.get_axis(0)*radius
  1313. };
  1314. if (!p_cam_orthogonal) {
  1315. //if using perspetive, map them to near plane
  1316. for(int j=0;j<2;j++) {
  1317. if (p.distance_to(points[j]) < 0 ) {
  1318. points[j].z=-zn; //small hack to keep size constant when hitting the screen
  1319. }
  1320. p.intersects_segment(cam_xf.origin,points[j],&points[j]); //map to plane
  1321. }
  1322. }
  1323. float screen_diameter = points[0].distance_to(points[1])*2;
  1324. coverage = screen_diameter / (vp_w+vp_h);
  1325. } break;
  1326. case VS::LIGHT_SPOT: {
  1327. float radius = VSG::storage->light_get_param(ins->base,VS::LIGHT_PARAM_RANGE);
  1328. float angle = VSG::storage->light_get_param(ins->base,VS::LIGHT_PARAM_SPOT_ANGLE);
  1329. float w = radius*Math::sin(Math::deg2rad(angle));
  1330. float d = radius*Math::cos(Math::deg2rad(angle));
  1331. Vector3 base = ins->transform.origin-ins->transform.basis.get_axis(2).normalized()*d;
  1332. Vector3 points[2]={
  1333. base,
  1334. base+cam_xf.basis.get_axis(0)*w
  1335. };
  1336. if (!p_cam_orthogonal) {
  1337. //if using perspetive, map them to near plane
  1338. for(int j=0;j<2;j++) {
  1339. if (p.distance_to(points[j]) < 0 ) {
  1340. points[j].z=-zn; //small hack to keep size constant when hitting the screen
  1341. }
  1342. p.intersects_segment(cam_xf.origin,points[j],&points[j]); //map to plane
  1343. }
  1344. }
  1345. float screen_diameter = points[0].distance_to(points[1])*2;
  1346. coverage = screen_diameter / (vp_w+vp_h);
  1347. } break;
  1348. default: {
  1349. ERR_PRINT("Invalid Light Type");
  1350. }
  1351. }
  1352. }
  1353. if (light->shadow_dirty) {
  1354. light->last_version++;
  1355. light->shadow_dirty=false;
  1356. }
  1357. bool redraw = VSG::scene_render->shadow_atlas_update_light(p_shadow_atlas,light->instance,coverage,light->last_version);
  1358. if (redraw) {
  1359. //must redraw!
  1360. _light_instance_update_shadow(ins,p_cam_transform,p_cam_projection,p_cam_orthogonal,p_shadow_atlas,scenario);
  1361. }
  1362. }
  1363. }
  1364. /* ENVIRONMENT */
  1365. RID environment;
  1366. if (p_force_environment.is_valid()) //camera has more environment priority
  1367. environment=p_force_environment;
  1368. else if (scenario->environment.is_valid())
  1369. environment=scenario->environment;
  1370. else
  1371. environment=scenario->fallback_environment;
  1372. #if 0
  1373. /* STEP 6 - SAMPLE BAKED LIGHT */
  1374. bool islinear =false;
  1375. if (environment.is_valid()) {
  1376. islinear = rasterizer->environment_is_fx_enabled(environment,VS::ENV_FX_SRGB);
  1377. }
  1378. for(int i=0;i<light_samplers_culled;i++) {
  1379. _process_sampled_light(camera->transform,light_sampler_cull_result[i],islinear);
  1380. }
  1381. #endif
  1382. /* STEP 7 - PROCESS GEOMETRY AND DRAW SCENE*/
  1383. VSG::scene_render->render_scene(p_cam_transform, p_cam_projection,p_cam_orthogonal,(RasterizerScene::InstanceBase**)instance_cull_result,cull_count,light_instance_cull_result,light_cull_count+directional_light_count,reflection_probe_instance_cull_result,reflection_probe_cull_count,environment,p_shadow_atlas,scenario->reflection_atlas,p_reflection_probe,p_reflection_probe_pass);
  1384. }
  1385. bool VisualServerScene::_render_probe_step(Instance* p_instance,int p_step) {
  1386. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData*>(p_instance->base_data);
  1387. Scenario *scenario = p_instance->scenario;
  1388. ERR_FAIL_COND_V(!scenario,true);
  1389. if (p_step==0) {
  1390. if (!VSG::scene_render->reflection_probe_instance_begin_render(reflection_probe->instance,scenario->reflection_atlas)) {
  1391. return true; //sorry, all full :(
  1392. }
  1393. }
  1394. if (p_step>=0 && p_step<6) {
  1395. static const Vector3 view_normals[6]={
  1396. Vector3(-1, 0, 0),
  1397. Vector3(+1, 0, 0),
  1398. Vector3( 0,-1, 0),
  1399. Vector3( 0,+1, 0),
  1400. Vector3( 0, 0,-1),
  1401. Vector3( 0, 0,+1)
  1402. };
  1403. Vector3 extents = VSG::storage->reflection_probe_get_extents(p_instance->base);
  1404. Vector3 origin_offset = VSG::storage->reflection_probe_get_origin_offset(p_instance->base);
  1405. float max_distance = VSG::storage->reflection_probe_get_origin_max_distance(p_instance->base);
  1406. Vector3 edge = view_normals[p_step]*extents;
  1407. float distance = ABS(view_normals[p_step].dot(edge)-view_normals[p_step].dot(origin_offset)); //distance from origin offset to actual view distance limit
  1408. max_distance = MAX(max_distance,distance);
  1409. //render cubemap side
  1410. CameraMatrix cm;
  1411. cm.set_perspective(90,1,0.01,max_distance);
  1412. static const Vector3 view_up[6]={
  1413. Vector3( 0,-1, 0),
  1414. Vector3( 0,-1, 0),
  1415. Vector3( 0, 0,-1),
  1416. Vector3( 0, 0,+1),
  1417. Vector3( 0,-1, 0),
  1418. Vector3( 0,-1, 0)
  1419. };
  1420. Transform local_view;
  1421. local_view.set_look_at(origin_offset,origin_offset+view_normals[p_step],view_up[p_step]);
  1422. Transform xform = p_instance->transform * local_view;
  1423. RID shadow_atlas;
  1424. if (VSG::storage->reflection_probe_renders_shadows(p_instance->base)) {
  1425. shadow_atlas=scenario->reflection_probe_shadow_atlas;
  1426. }
  1427. _render_scene(xform,cm,false,RID(),VSG::storage->reflection_probe_get_cull_mask(p_instance->base),p_instance->scenario->self,shadow_atlas,reflection_probe->instance,p_step);
  1428. } else {
  1429. //do roughness postprocess step until it belives it's done
  1430. return VSG::scene_render->reflection_probe_instance_postprocess_step(reflection_probe->instance);
  1431. }
  1432. return false;
  1433. }
  1434. void VisualServerScene::render_probes() {
  1435. SelfList<InstanceReflectionProbeData> *probe = reflection_probe_render_list.first();
  1436. bool busy=false;
  1437. while(probe) {
  1438. SelfList<InstanceReflectionProbeData> *next=probe->next();
  1439. RID base = probe->self()->owner->base;
  1440. switch(VSG::storage->reflection_probe_get_update_mode(base)) {
  1441. case VS::REFLECTION_PROBE_UPDATE_ONCE: {
  1442. if (busy) //already rendering something
  1443. break;
  1444. bool done = _render_probe_step(probe->self()->owner,probe->self()->render_step);
  1445. if (done) {
  1446. reflection_probe_render_list.remove(probe);
  1447. } else {
  1448. probe->self()->render_step++;
  1449. }
  1450. busy=true; //do not render another one of this kind
  1451. } break;
  1452. case VS::REFLECTION_PROBE_UPDATE_ALWAYS: {
  1453. int step=0;
  1454. bool done=false;
  1455. while(!done) {
  1456. done = _render_probe_step(probe->self()->owner,step);
  1457. step++;
  1458. }
  1459. reflection_probe_render_list.remove(probe);
  1460. } break;
  1461. }
  1462. probe=next;
  1463. }
  1464. }
  1465. void VisualServerScene::_update_dirty_instance(Instance *p_instance) {
  1466. if (p_instance->update_aabb)
  1467. _update_instance_aabb(p_instance);
  1468. if (p_instance->update_materials) {
  1469. if (p_instance->base_type==VS::INSTANCE_MESH) {
  1470. //remove materials no longer used and un-own them
  1471. int new_mat_count = VSG::storage->mesh_get_surface_count(p_instance->base);
  1472. for(int i=p_instance->materials.size()-1;i>=new_mat_count;i--) {
  1473. if (p_instance->materials[i].is_valid()) {
  1474. VSG::storage->material_remove_instance_owner(p_instance->materials[i],p_instance);
  1475. }
  1476. }
  1477. p_instance->materials.resize(new_mat_count);
  1478. int new_morph_count = VSG::storage->mesh_get_morph_target_count(p_instance->base);
  1479. if (new_morph_count!=p_instance->morph_values.size()) {
  1480. p_instance->morph_values.resize(new_morph_count);
  1481. for(int i=0;i<new_morph_count;i++) {
  1482. p_instance->morph_values[i]=0;
  1483. }
  1484. }
  1485. }
  1486. if ((1<<p_instance->base_type)&VS::INSTANCE_GEOMETRY_MASK) {
  1487. InstanceGeometryData *geom = static_cast<InstanceGeometryData*>(p_instance->base_data);
  1488. bool can_cast_shadows=true;
  1489. if (p_instance->cast_shadows==VS::SHADOW_CASTING_SETTING_OFF) {
  1490. can_cast_shadows=false;
  1491. } else if (p_instance->material_override.is_valid()) {
  1492. can_cast_shadows=VSG::storage->material_casts_shadows(p_instance->material_override);
  1493. } else {
  1494. if (p_instance->base_type==VS::INSTANCE_MESH) {
  1495. RID mesh=p_instance->base;
  1496. if (mesh.is_valid()) {
  1497. bool cast_shadows=false;
  1498. for(int i=0;i<p_instance->materials.size();i++) {
  1499. RID mat = p_instance->materials[i].is_valid()?p_instance->materials[i]:VSG::storage->mesh_surface_get_material(mesh,i);
  1500. if (!mat.is_valid()) {
  1501. cast_shadows=true;
  1502. break;
  1503. }
  1504. if (VSG::storage->material_casts_shadows(mat)) {
  1505. cast_shadows=true;
  1506. break;
  1507. }
  1508. }
  1509. if (!cast_shadows) {
  1510. can_cast_shadows=false;
  1511. }
  1512. }
  1513. } else if (p_instance->base_type==VS::INSTANCE_MULTIMESH) {
  1514. RID mesh = VSG::storage->multimesh_get_mesh(p_instance->base);
  1515. if (mesh.is_valid()) {
  1516. bool cast_shadows=false;
  1517. int sc = VSG::storage->mesh_get_surface_count(mesh);
  1518. for(int i=0;i<sc;i++) {
  1519. RID mat =VSG::storage->mesh_surface_get_material(mesh,i);
  1520. if (!mat.is_valid()) {
  1521. cast_shadows=true;
  1522. break;
  1523. }
  1524. if (VSG::storage->material_casts_shadows(mat)) {
  1525. cast_shadows=true;
  1526. break;
  1527. }
  1528. }
  1529. if (!cast_shadows) {
  1530. can_cast_shadows=false;
  1531. }
  1532. }
  1533. } else if (p_instance->base_type==VS::INSTANCE_IMMEDIATE) {
  1534. RID mat = VSG::storage->immediate_get_material(p_instance->base);
  1535. if (!mat.is_valid() || VSG::storage->material_casts_shadows(mat)) {
  1536. can_cast_shadows=true;
  1537. } else {
  1538. can_cast_shadows=false;
  1539. }
  1540. }
  1541. }
  1542. if (can_cast_shadows!=geom->can_cast_shadows) {
  1543. //ability to cast shadows change, let lights now
  1544. for (List<Instance*>::Element *E=geom->lighting.front();E;E=E->next()) {
  1545. InstanceLightData *light = static_cast<InstanceLightData*>(E->get()->base_data);
  1546. light->shadow_dirty=true;
  1547. }
  1548. geom->can_cast_shadows=can_cast_shadows;
  1549. }
  1550. }
  1551. }
  1552. _update_instance(p_instance);
  1553. p_instance->update_aabb=false;
  1554. p_instance->update_materials=false;
  1555. _instance_update_list.remove( &p_instance->update_item );
  1556. }
  1557. void VisualServerScene::update_dirty_instances() {
  1558. while(_instance_update_list.first()) {
  1559. _update_dirty_instance( _instance_update_list.first()->self() );
  1560. }
  1561. }
  1562. bool VisualServerScene::free(RID p_rid) {
  1563. if (camera_owner.owns(p_rid)) {
  1564. Camera *camera = camera_owner.get( p_rid );
  1565. camera_owner.free(p_rid);
  1566. memdelete(camera);
  1567. } else if (scenario_owner.owns(p_rid)) {
  1568. Scenario *scenario = scenario_owner.get( p_rid );
  1569. while(scenario->instances.first()) {
  1570. instance_set_scenario(scenario->instances.first()->self()->self,RID());
  1571. }
  1572. VSG::scene_render->free(scenario->reflection_probe_shadow_atlas);
  1573. VSG::scene_render->free(scenario->reflection_atlas);
  1574. scenario_owner.free(p_rid);
  1575. memdelete(scenario);
  1576. } else if (instance_owner.owns(p_rid)) {
  1577. // delete the instance
  1578. update_dirty_instances();
  1579. Instance *instance = instance_owner.get(p_rid);
  1580. instance_set_room(p_rid,RID());
  1581. instance_set_scenario(p_rid,RID());
  1582. instance_set_base(p_rid,RID());
  1583. instance_geometry_set_material_override(p_rid,RID());
  1584. instance_attach_skeleton(p_rid,RID());
  1585. update_dirty_instances(); //in case something changed this
  1586. instance_owner.free(p_rid);
  1587. memdelete(instance);
  1588. } else {
  1589. return false;
  1590. }
  1591. return true;
  1592. }
  1593. VisualServerScene *VisualServerScene::singleton=NULL;
  1594. VisualServerScene::VisualServerScene() {
  1595. render_pass=1;
  1596. singleton=this;
  1597. }