visual_server_scene.cpp 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494
  1. /*************************************************************************/
  2. /* visual_server_scene.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_server_scene.h"
  31. #include "os/os.h"
  32. #include "visual_server_global.h"
  33. #include "visual_server_raster.h"
  34. /* CAMERA API */
  35. RID VisualServerScene::camera_create() {
  36. Camera *camera = memnew(Camera);
  37. return camera_owner.make_rid(camera);
  38. }
  39. void VisualServerScene::camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far) {
  40. Camera *camera = camera_owner.get(p_camera);
  41. ERR_FAIL_COND(!camera);
  42. camera->type = Camera::PERSPECTIVE;
  43. camera->fov = p_fovy_degrees;
  44. camera->znear = p_z_near;
  45. camera->zfar = p_z_far;
  46. }
  47. void VisualServerScene::camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far) {
  48. Camera *camera = camera_owner.get(p_camera);
  49. ERR_FAIL_COND(!camera);
  50. camera->type = Camera::ORTHOGONAL;
  51. camera->size = p_size;
  52. camera->znear = p_z_near;
  53. camera->zfar = p_z_far;
  54. }
  55. void VisualServerScene::camera_set_transform(RID p_camera, const Transform &p_transform) {
  56. Camera *camera = camera_owner.get(p_camera);
  57. ERR_FAIL_COND(!camera);
  58. camera->transform = p_transform.orthonormalized();
  59. }
  60. void VisualServerScene::camera_set_cull_mask(RID p_camera, uint32_t p_layers) {
  61. Camera *camera = camera_owner.get(p_camera);
  62. ERR_FAIL_COND(!camera);
  63. camera->visible_layers = p_layers;
  64. }
  65. void VisualServerScene::camera_set_environment(RID p_camera, RID p_env) {
  66. Camera *camera = camera_owner.get(p_camera);
  67. ERR_FAIL_COND(!camera);
  68. camera->env = p_env;
  69. }
  70. void VisualServerScene::camera_set_use_vertical_aspect(RID p_camera, bool p_enable) {
  71. Camera *camera = camera_owner.get(p_camera);
  72. ERR_FAIL_COND(!camera);
  73. camera->vaspect = p_enable;
  74. }
  75. /* SCENARIO API */
  76. void *VisualServerScene::_instance_pair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int) {
  77. //VisualServerScene *self = (VisualServerScene*)p_self;
  78. Instance *A = p_A;
  79. Instance *B = p_B;
  80. //instance indices are designed so greater always contains lesser
  81. if (A->base_type > B->base_type) {
  82. SWAP(A, B); //lesser always first
  83. }
  84. if (B->base_type == VS::INSTANCE_LIGHT && (1 << A->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  85. InstanceLightData *light = static_cast<InstanceLightData *>(B->base_data);
  86. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  87. InstanceLightData::PairInfo pinfo;
  88. pinfo.geometry = A;
  89. pinfo.L = geom->lighting.push_back(B);
  90. List<InstanceLightData::PairInfo>::Element *E = light->geometries.push_back(pinfo);
  91. if (geom->can_cast_shadows) {
  92. light->shadow_dirty = true;
  93. }
  94. geom->lighting_dirty = true;
  95. return E; //this element should make freeing faster
  96. } else if (B->base_type == VS::INSTANCE_REFLECTION_PROBE && (1 << A->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  97. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(B->base_data);
  98. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  99. InstanceReflectionProbeData::PairInfo pinfo;
  100. pinfo.geometry = A;
  101. pinfo.L = geom->reflection_probes.push_back(B);
  102. List<InstanceReflectionProbeData::PairInfo>::Element *E = reflection_probe->geometries.push_back(pinfo);
  103. geom->reflection_dirty = true;
  104. return E; //this element should make freeing faster
  105. } else if (B->base_type == VS::INSTANCE_GI_PROBE && (1 << A->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  106. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  107. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  108. InstanceGIProbeData::PairInfo pinfo;
  109. pinfo.geometry = A;
  110. pinfo.L = geom->gi_probes.push_back(B);
  111. List<InstanceGIProbeData::PairInfo>::Element *E = gi_probe->geometries.push_back(pinfo);
  112. geom->gi_probes_dirty = true;
  113. return E; //this element should make freeing faster
  114. } else if (B->base_type == VS::INSTANCE_GI_PROBE && A->base_type == VS::INSTANCE_LIGHT) {
  115. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  116. InstanceLightData *light = static_cast<InstanceLightData *>(A->base_data);
  117. return gi_probe->lights.insert(A);
  118. }
  119. #if 0
  120. if (A->base_type==INSTANCE_PORTAL) {
  121. ERR_FAIL_COND_V( B->base_type!=INSTANCE_PORTAL,NULL );
  122. A->portal_info->candidate_set.insert(B);
  123. B->portal_info->candidate_set.insert(A);
  124. self->_portal_attempt_connect(A);
  125. //attempt to conncet portal A (will go through B anyway)
  126. //this is a little hackish, but works fine in practice
  127. } else if (A->base_type==INSTANCE_GI_PROBE || B->base_type==INSTANCE_GI_PROBE) {
  128. if (B->base_type==INSTANCE_GI_PROBE) {
  129. SWAP(A,B);
  130. }
  131. ERR_FAIL_COND_V(B->base_type!=INSTANCE_GI_PROBE_SAMPLER,NULL);
  132. B->gi_probe_sampler_info->gi_probes.insert(A);
  133. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  134. if (B->base_type==INSTANCE_ROOM)
  135. SWAP(A,B);
  136. ERR_FAIL_COND_V(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ),NULL);
  137. B->auto_rooms.insert(A);
  138. A->room_info->owned_autoroom_geometry.insert(B);
  139. self->_instance_validate_autorooms(B);
  140. } else {
  141. if (B->base_type==INSTANCE_LIGHT) {
  142. SWAP(A,B);
  143. } else if (A->base_type!=INSTANCE_LIGHT) {
  144. return NULL;
  145. }
  146. A->light_info->affected.insert(B);
  147. B->lights.insert(A);
  148. B->light_cache_dirty=true;
  149. }
  150. #endif
  151. return NULL;
  152. }
  153. void VisualServerScene::_instance_unpair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int, void *udata) {
  154. //VisualServerScene *self = (VisualServerScene*)p_self;
  155. Instance *A = p_A;
  156. Instance *B = p_B;
  157. //instance indices are designed so greater always contains lesser
  158. if (A->base_type > B->base_type) {
  159. SWAP(A, B); //lesser always first
  160. }
  161. if (B->base_type == VS::INSTANCE_LIGHT && (1 << A->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  162. InstanceLightData *light = static_cast<InstanceLightData *>(B->base_data);
  163. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  164. List<InstanceLightData::PairInfo>::Element *E = reinterpret_cast<List<InstanceLightData::PairInfo>::Element *>(udata);
  165. geom->lighting.erase(E->get().L);
  166. light->geometries.erase(E);
  167. if (geom->can_cast_shadows) {
  168. light->shadow_dirty = true;
  169. }
  170. geom->lighting_dirty = true;
  171. } else if (B->base_type == VS::INSTANCE_REFLECTION_PROBE && (1 << A->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  172. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(B->base_data);
  173. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  174. List<InstanceReflectionProbeData::PairInfo>::Element *E = reinterpret_cast<List<InstanceReflectionProbeData::PairInfo>::Element *>(udata);
  175. geom->reflection_probes.erase(E->get().L);
  176. reflection_probe->geometries.erase(E);
  177. geom->reflection_dirty = true;
  178. } else if (B->base_type == VS::INSTANCE_GI_PROBE && (1 << A->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  179. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  180. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  181. List<InstanceGIProbeData::PairInfo>::Element *E = reinterpret_cast<List<InstanceGIProbeData::PairInfo>::Element *>(udata);
  182. geom->gi_probes.erase(E->get().L);
  183. gi_probe->geometries.erase(E);
  184. geom->gi_probes_dirty = true;
  185. } else if (B->base_type == VS::INSTANCE_GI_PROBE && A->base_type == VS::INSTANCE_LIGHT) {
  186. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  187. InstanceLightData *light = static_cast<InstanceLightData *>(A->base_data);
  188. Set<Instance *>::Element *E = reinterpret_cast<Set<Instance *>::Element *>(udata);
  189. gi_probe->lights.erase(E);
  190. }
  191. #if 0
  192. if (A->base_type==INSTANCE_PORTAL) {
  193. ERR_FAIL_COND( B->base_type!=INSTANCE_PORTAL );
  194. A->portal_info->candidate_set.erase(B);
  195. B->portal_info->candidate_set.erase(A);
  196. //after disconnecting them, see if they can connect again
  197. self->_portal_attempt_connect(A);
  198. self->_portal_attempt_connect(B);
  199. } else if (A->base_type==INSTANCE_GI_PROBE || B->base_type==INSTANCE_GI_PROBE) {
  200. if (B->base_type==INSTANCE_GI_PROBE) {
  201. SWAP(A,B);
  202. }
  203. ERR_FAIL_COND(B->base_type!=INSTANCE_GI_PROBE_SAMPLER);
  204. B->gi_probe_sampler_info->gi_probes.erase(A);
  205. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  206. if (B->base_type==INSTANCE_ROOM)
  207. SWAP(A,B);
  208. ERR_FAIL_COND(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ));
  209. B->auto_rooms.erase(A);
  210. B->valid_auto_rooms.erase(A);
  211. A->room_info->owned_autoroom_geometry.erase(B);
  212. }else {
  213. if (B->base_type==INSTANCE_LIGHT) {
  214. SWAP(A,B);
  215. } else if (A->base_type!=INSTANCE_LIGHT) {
  216. return;
  217. }
  218. A->light_info->affected.erase(B);
  219. B->lights.erase(A);
  220. B->light_cache_dirty=true;
  221. }
  222. #endif
  223. }
  224. RID VisualServerScene::scenario_create() {
  225. Scenario *scenario = memnew(Scenario);
  226. ERR_FAIL_COND_V(!scenario, RID());
  227. RID scenario_rid = scenario_owner.make_rid(scenario);
  228. scenario->self = scenario_rid;
  229. scenario->octree.set_pair_callback(_instance_pair, this);
  230. scenario->octree.set_unpair_callback(_instance_unpair, this);
  231. scenario->reflection_probe_shadow_atlas = VSG::scene_render->shadow_atlas_create();
  232. VSG::scene_render->shadow_atlas_set_size(scenario->reflection_probe_shadow_atlas, 1024); //make enough shadows for close distance, don't bother with rest
  233. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 0, 4);
  234. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 1, 4);
  235. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 2, 4);
  236. VSG::scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 3, 8);
  237. scenario->reflection_atlas = VSG::scene_render->reflection_atlas_create();
  238. return scenario_rid;
  239. }
  240. void VisualServerScene::scenario_set_debug(RID p_scenario, VS::ScenarioDebugMode p_debug_mode) {
  241. Scenario *scenario = scenario_owner.get(p_scenario);
  242. ERR_FAIL_COND(!scenario);
  243. scenario->debug = p_debug_mode;
  244. }
  245. void VisualServerScene::scenario_set_environment(RID p_scenario, RID p_environment) {
  246. Scenario *scenario = scenario_owner.get(p_scenario);
  247. ERR_FAIL_COND(!scenario);
  248. scenario->environment = p_environment;
  249. }
  250. void VisualServerScene::scenario_set_fallback_environment(RID p_scenario, RID p_environment) {
  251. Scenario *scenario = scenario_owner.get(p_scenario);
  252. ERR_FAIL_COND(!scenario);
  253. scenario->fallback_environment = p_environment;
  254. }
  255. void VisualServerScene::scenario_set_reflection_atlas_size(RID p_scenario, int p_size, int p_subdiv) {
  256. Scenario *scenario = scenario_owner.get(p_scenario);
  257. ERR_FAIL_COND(!scenario);
  258. VSG::scene_render->reflection_atlas_set_size(scenario->reflection_atlas, p_size);
  259. VSG::scene_render->reflection_atlas_set_subdivision(scenario->reflection_atlas, p_subdiv);
  260. }
  261. /* INSTANCING API */
  262. void VisualServerScene::_instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_materials) {
  263. if (p_update_aabb)
  264. p_instance->update_aabb = true;
  265. if (p_update_materials)
  266. p_instance->update_materials = true;
  267. if (p_instance->update_item.in_list())
  268. return;
  269. _instance_update_list.add(&p_instance->update_item);
  270. }
  271. // from can be mesh, light, area and portal so far.
  272. RID VisualServerScene::instance_create() {
  273. Instance *instance = memnew(Instance);
  274. ERR_FAIL_COND_V(!instance, RID());
  275. RID instance_rid = instance_owner.make_rid(instance);
  276. instance->self = instance_rid;
  277. return instance_rid;
  278. }
  279. void VisualServerScene::instance_set_base(RID p_instance, RID p_base) {
  280. Instance *instance = instance_owner.get(p_instance);
  281. ERR_FAIL_COND(!instance);
  282. Scenario *scenario = instance->scenario;
  283. if (instance->base_type != VS::INSTANCE_NONE) {
  284. //free anything related to that base
  285. VSG::storage->instance_remove_dependency(instance->base, instance);
  286. if (scenario && instance->octree_id) {
  287. scenario->octree.erase(instance->octree_id); //make dependencies generated by the octree go away
  288. instance->octree_id = 0;
  289. }
  290. switch (instance->base_type) {
  291. case VS::INSTANCE_LIGHT: {
  292. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  293. if (instance->scenario && light->D) {
  294. instance->scenario->directional_lights.erase(light->D);
  295. light->D = NULL;
  296. }
  297. VSG::scene_render->free(light->instance);
  298. } break;
  299. case VS::INSTANCE_REFLECTION_PROBE: {
  300. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(instance->base_data);
  301. VSG::scene_render->free(reflection_probe->instance);
  302. if (reflection_probe->update_list.in_list()) {
  303. reflection_probe_render_list.remove(&reflection_probe->update_list);
  304. }
  305. } break;
  306. case VS::INSTANCE_GI_PROBE: {
  307. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(instance->base_data);
  308. while (gi_probe->dynamic.updating_stage == GI_UPDATE_STAGE_LIGHTING) {
  309. //wait until bake is done if it's baking
  310. OS::get_singleton()->delay_usec(1);
  311. }
  312. if (gi_probe->update_element.in_list()) {
  313. gi_probe_update_list.remove(&gi_probe->update_element);
  314. }
  315. if (gi_probe->dynamic.probe_data.is_valid()) {
  316. VSG::storage->free(gi_probe->dynamic.probe_data);
  317. }
  318. VSG::scene_render->free(gi_probe->probe_instance);
  319. } break;
  320. }
  321. if (instance->base_data) {
  322. memdelete(instance->base_data);
  323. instance->base_data = NULL;
  324. }
  325. instance->blend_values.clear();
  326. for (int i = 0; i < instance->materials.size(); i++) {
  327. if (instance->materials[i].is_valid()) {
  328. VSG::storage->material_remove_instance_owner(instance->materials[i], instance);
  329. }
  330. }
  331. instance->materials.clear();
  332. #if 0
  333. if (instance->light_info) {
  334. if (instance->scenario && instance->light_info->D)
  335. instance->scenario->directional_lights.erase( instance->light_info->D );
  336. rasterizer->free(instance->light_info->instance);
  337. memdelete(instance->light_info);
  338. instance->light_info=NULL;
  339. }
  340. if ( instance->room ) {
  341. instance_set_room(p_instance,RID());
  342. /*
  343. if((1<<instance->base_type)&INSTANCE_GEOMETRY_MASK)
  344. instance->room->room_info->owned_geometry_instances.erase(instance->RE);
  345. else if (instance->base_type==INSTANCE_PORTAL) {
  346. print_line("freeing portal, is it there? "+itos(instance->room->room_info->owned_portal_instances.(instance->RE)));
  347. instance->room->room_info->owned_portal_instances.erase(instance->RE);
  348. } else if (instance->base_type==INSTANCE_ROOM)
  349. instance->room->room_info->owned_room_instances.erase(instance->RE);
  350. else if (instance->base_type==INSTANCE_LIGHT)
  351. instance->room->room_info->owned_light_instances.erase(instance->RE);
  352. instance->RE=NULL;*/
  353. }
  354. if (instance->portal_info) {
  355. _portal_disconnect(instance,true);
  356. memdelete(instance->portal_info);
  357. instance->portal_info=NULL;
  358. }
  359. if (instance->gi_probe_info) {
  360. while(instance->gi_probe_info->owned_instances.size()) {
  361. Instance *owned=instance->gi_probe_info->owned_instances.front()->get();
  362. owned->gi_probe=NULL;
  363. owned->data.gi_probe=NULL;
  364. owned->data.gi_probe_octree_xform=NULL;
  365. owned->BLE=NULL;
  366. instance->gi_probe_info->owned_instances.pop_front();
  367. }
  368. memdelete(instance->gi_probe_info);
  369. instance->gi_probe_info=NULL;
  370. }
  371. if (instance->scenario && instance->octree_id) {
  372. instance->scenario->octree.erase( instance->octree_id );
  373. instance->octree_id=0;
  374. }
  375. if (instance->room_info) {
  376. for(List<Instance*>::Element *E=instance->room_info->owned_geometry_instances.front();E;E=E->next()) {
  377. Instance *owned = E->get();
  378. owned->room=NULL;
  379. owned->RE=NULL;
  380. }
  381. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) {
  382. _portal_disconnect(E->get(),true);
  383. Instance *owned = E->get();
  384. owned->room=NULL;
  385. owned->RE=NULL;
  386. }
  387. for(List<Instance*>::Element *E=instance->room_info->owned_room_instances.front();E;E=E->next()) {
  388. Instance *owned = E->get();
  389. owned->room=NULL;
  390. owned->RE=NULL;
  391. }
  392. if (instance->room_info->disconnected_child_portals.size()) {
  393. ERR_PRINT("BUG: Disconnected portals remain!");
  394. }
  395. memdelete(instance->room_info);
  396. instance->room_info=NULL;
  397. }
  398. if (instance->particles_info) {
  399. rasterizer->free( instance->particles_info->instance );
  400. memdelete(instance->particles_info);
  401. instance->particles_info=NULL;
  402. }
  403. if (instance->gi_probe_sampler_info) {
  404. while (instance->gi_probe_sampler_info->owned_instances.size()) {
  405. instance_geometry_set_gi_probe_sampler(instance->gi_probe_sampler_info->owned_instances.front()->get()->self,RID());
  406. }
  407. if (instance->gi_probe_sampler_info->sampled_light.is_valid()) {
  408. rasterizer->free(instance->gi_probe_sampler_info->sampled_light);
  409. }
  410. memdelete( instance->gi_probe_sampler_info );
  411. instance->gi_probe_sampler_info=NULL;
  412. }
  413. #endif
  414. }
  415. instance->base_type = VS::INSTANCE_NONE;
  416. instance->base = RID();
  417. if (p_base.is_valid()) {
  418. instance->base_type = VSG::storage->get_base_type(p_base);
  419. ERR_FAIL_COND(instance->base_type == VS::INSTANCE_NONE);
  420. switch (instance->base_type) {
  421. case VS::INSTANCE_LIGHT: {
  422. InstanceLightData *light = memnew(InstanceLightData);
  423. if (scenario && VSG::storage->light_get_type(p_base) == VS::LIGHT_DIRECTIONAL) {
  424. light->D = scenario->directional_lights.push_back(instance);
  425. }
  426. light->instance = VSG::scene_render->light_instance_create(p_base);
  427. instance->base_data = light;
  428. } break;
  429. case VS::INSTANCE_MESH:
  430. case VS::INSTANCE_MULTIMESH:
  431. case VS::INSTANCE_IMMEDIATE:
  432. case VS::INSTANCE_PARTICLES: {
  433. InstanceGeometryData *geom = memnew(InstanceGeometryData);
  434. instance->base_data = geom;
  435. } break;
  436. case VS::INSTANCE_REFLECTION_PROBE: {
  437. InstanceReflectionProbeData *reflection_probe = memnew(InstanceReflectionProbeData);
  438. reflection_probe->owner = instance;
  439. instance->base_data = reflection_probe;
  440. reflection_probe->instance = VSG::scene_render->reflection_probe_instance_create(p_base);
  441. } break;
  442. case VS::INSTANCE_GI_PROBE: {
  443. InstanceGIProbeData *gi_probe = memnew(InstanceGIProbeData);
  444. instance->base_data = gi_probe;
  445. gi_probe->owner = instance;
  446. if (scenario && !gi_probe->update_element.in_list()) {
  447. gi_probe_update_list.add(&gi_probe->update_element);
  448. }
  449. gi_probe->probe_instance = VSG::scene_render->gi_probe_instance_create();
  450. } break;
  451. }
  452. VSG::storage->instance_add_dependency(p_base, instance);
  453. instance->base = p_base;
  454. if (scenario)
  455. _instance_queue_update(instance, true, true);
  456. #if 0
  457. if (rasterizer->is_mesh(p_base)) {
  458. instance->base_type=INSTANCE_MESH;
  459. instance->data.morph_values.resize( rasterizer->mesh_get_morph_target_count(p_base));
  460. instance->data.materials.resize( rasterizer->mesh_get_surface_count(p_base));
  461. } else if (rasterizer->is_multimesh(p_base)) {
  462. instance->base_type=INSTANCE_MULTIMESH;
  463. } else if (rasterizer->is_immediate(p_base)) {
  464. instance->base_type=INSTANCE_IMMEDIATE;
  465. } else if (rasterizer->is_particles(p_base)) {
  466. instance->base_type=INSTANCE_PARTICLES;
  467. instance->particles_info=memnew( Instance::ParticlesInfo );
  468. instance->particles_info->instance = rasterizer->particles_instance_create( p_base );
  469. } else if (rasterizer->is_light(p_base)) {
  470. instance->base_type=INSTANCE_LIGHT;
  471. instance->light_info = memnew( Instance::LightInfo );
  472. instance->light_info->instance = rasterizer->light_instance_create(p_base);
  473. if (instance->scenario && rasterizer->light_get_type(p_base)==LIGHT_DIRECTIONAL) {
  474. instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self);
  475. }
  476. } else if (room_owner.owns(p_base)) {
  477. instance->base_type=INSTANCE_ROOM;
  478. instance->room_info = memnew( Instance::RoomInfo );
  479. instance->room_info->room=room_owner.get(p_base);
  480. } else if (portal_owner.owns(p_base)) {
  481. instance->base_type=INSTANCE_PORTAL;
  482. instance->portal_info = memnew(Instance::PortalInfo);
  483. instance->portal_info->portal=portal_owner.get(p_base);
  484. } else if (gi_probe_owner.owns(p_base)) {
  485. instance->base_type=INSTANCE_GI_PROBE;
  486. instance->gi_probe_info=memnew(Instance::BakedLightInfo);
  487. instance->gi_probe_info->gi_probe=gi_probe_owner.get(p_base);
  488. //instance->portal_info = memnew(Instance::PortalInfo);
  489. //instance->portal_info->portal=portal_owner.get(p_base);
  490. } else if (gi_probe_sampler_owner.owns(p_base)) {
  491. instance->base_type=INSTANCE_GI_PROBE_SAMPLER;
  492. instance->gi_probe_sampler_info=memnew( Instance::BakedLightSamplerInfo);
  493. instance->gi_probe_sampler_info->sampler=gi_probe_sampler_owner.get(p_base);
  494. //instance->portal_info = memnew(Instance::PortalInfo);
  495. //instance->portal_info->portal=portal_owner.get(p_base);
  496. } else {
  497. ERR_EXPLAIN("Invalid base RID for instance!")
  498. ERR_FAIL();
  499. }
  500. instance_dependency_map[ p_base ].insert( instance->self );
  501. #endif
  502. }
  503. }
  504. void VisualServerScene::instance_set_scenario(RID p_instance, RID p_scenario) {
  505. Instance *instance = instance_owner.get(p_instance);
  506. ERR_FAIL_COND(!instance);
  507. if (instance->scenario) {
  508. instance->scenario->instances.remove(&instance->scenario_item);
  509. if (instance->octree_id) {
  510. instance->scenario->octree.erase(instance->octree_id); //make dependencies generated by the octree go away
  511. instance->octree_id = 0;
  512. }
  513. switch (instance->base_type) {
  514. case VS::INSTANCE_LIGHT: {
  515. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  516. if (light->D) {
  517. instance->scenario->directional_lights.erase(light->D);
  518. light->D = NULL;
  519. }
  520. } break;
  521. case VS::INSTANCE_REFLECTION_PROBE: {
  522. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(instance->base_data);
  523. VSG::scene_render->reflection_probe_release_atlas_index(reflection_probe->instance);
  524. } break;
  525. case VS::INSTANCE_GI_PROBE: {
  526. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(instance->base_data);
  527. if (gi_probe->update_element.in_list()) {
  528. gi_probe_update_list.remove(&gi_probe->update_element);
  529. }
  530. } break;
  531. }
  532. instance->scenario = NULL;
  533. }
  534. if (p_scenario.is_valid()) {
  535. Scenario *scenario = scenario_owner.get(p_scenario);
  536. ERR_FAIL_COND(!scenario);
  537. instance->scenario = scenario;
  538. scenario->instances.add(&instance->scenario_item);
  539. switch (instance->base_type) {
  540. case VS::INSTANCE_LIGHT: {
  541. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  542. if (VSG::storage->light_get_type(instance->base) == VS::LIGHT_DIRECTIONAL) {
  543. light->D = scenario->directional_lights.push_back(instance);
  544. }
  545. } break;
  546. case VS::INSTANCE_GI_PROBE: {
  547. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(instance->base_data);
  548. if (!gi_probe->update_element.in_list()) {
  549. gi_probe_update_list.add(&gi_probe->update_element);
  550. }
  551. } break;
  552. }
  553. _instance_queue_update(instance, true, true);
  554. }
  555. }
  556. void VisualServerScene::instance_set_layer_mask(RID p_instance, uint32_t p_mask) {
  557. Instance *instance = instance_owner.get(p_instance);
  558. ERR_FAIL_COND(!instance);
  559. instance->layer_mask = p_mask;
  560. }
  561. void VisualServerScene::instance_set_transform(RID p_instance, const Transform &p_transform) {
  562. Instance *instance = instance_owner.get(p_instance);
  563. ERR_FAIL_COND(!instance);
  564. if (instance->transform == p_transform)
  565. return; //must be checked to avoid worst evil
  566. instance->transform = p_transform;
  567. _instance_queue_update(instance, true);
  568. }
  569. void VisualServerScene::instance_attach_object_instance_ID(RID p_instance, ObjectID p_ID) {
  570. Instance *instance = instance_owner.get(p_instance);
  571. ERR_FAIL_COND(!instance);
  572. instance->object_ID = p_ID;
  573. }
  574. void VisualServerScene::instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight) {
  575. Instance *instance = instance_owner.get(p_instance);
  576. ERR_FAIL_COND(!instance);
  577. if (instance->update_item.in_list()) {
  578. _update_dirty_instance(instance);
  579. }
  580. ERR_FAIL_INDEX(p_shape, instance->blend_values.size());
  581. instance->blend_values[p_shape] = p_weight;
  582. }
  583. void VisualServerScene::instance_set_surface_material(RID p_instance, int p_surface, RID p_material) {
  584. Instance *instance = instance_owner.get(p_instance);
  585. ERR_FAIL_COND(!instance);
  586. if (instance->update_item.in_list()) {
  587. _update_dirty_instance(instance);
  588. }
  589. ERR_FAIL_INDEX(p_surface, instance->materials.size());
  590. if (instance->materials[p_surface].is_valid()) {
  591. VSG::storage->material_remove_instance_owner(instance->materials[p_surface], instance);
  592. }
  593. instance->materials[p_surface] = p_material;
  594. instance->base_material_changed();
  595. if (instance->materials[p_surface].is_valid()) {
  596. VSG::storage->material_add_instance_owner(instance->materials[p_surface], instance);
  597. }
  598. }
  599. void VisualServerScene::instance_set_visible(RID p_instance, bool p_visible) {
  600. Instance *instance = instance_owner.get(p_instance);
  601. ERR_FAIL_COND(!instance);
  602. if (instance->visible == p_visible)
  603. return;
  604. instance->visible = p_visible;
  605. switch (instance->base_type) {
  606. case VS::INSTANCE_LIGHT: {
  607. if (VSG::storage->light_get_type(instance->base) != VS::LIGHT_DIRECTIONAL && instance->octree_id && instance->scenario) {
  608. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << VS::INSTANCE_LIGHT, p_visible ? VS::INSTANCE_GEOMETRY_MASK : 0);
  609. }
  610. } break;
  611. case VS::INSTANCE_REFLECTION_PROBE: {
  612. if (instance->octree_id && instance->scenario) {
  613. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << VS::INSTANCE_REFLECTION_PROBE, p_visible ? VS::INSTANCE_GEOMETRY_MASK : 0);
  614. }
  615. } break;
  616. case VS::INSTANCE_GI_PROBE: {
  617. if (instance->octree_id && instance->scenario) {
  618. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << VS::INSTANCE_GI_PROBE, p_visible ? (VS::INSTANCE_GEOMETRY_MASK | (1 << VS::INSTANCE_LIGHT)) : 0);
  619. }
  620. } break;
  621. }
  622. }
  623. void VisualServerScene::instance_attach_skeleton(RID p_instance, RID p_skeleton) {
  624. Instance *instance = instance_owner.get(p_instance);
  625. ERR_FAIL_COND(!instance);
  626. if (instance->skeleton == p_skeleton)
  627. return;
  628. if (instance->skeleton.is_valid()) {
  629. VSG::storage->instance_remove_skeleton(p_skeleton, instance);
  630. }
  631. instance->skeleton = p_skeleton;
  632. if (instance->skeleton.is_valid()) {
  633. VSG::storage->instance_add_skeleton(p_skeleton, instance);
  634. }
  635. _instance_queue_update(instance, true);
  636. }
  637. void VisualServerScene::instance_set_exterior(RID p_instance, bool p_enabled) {
  638. }
  639. void VisualServerScene::instance_set_room(RID p_instance, RID p_room) {
  640. }
  641. void VisualServerScene::instance_set_extra_visibility_margin(RID p_instance, real_t p_margin) {
  642. }
  643. Vector<ObjectID> VisualServerScene::instances_cull_aabb(const Rect3 &p_aabb, RID p_scenario) const {
  644. Vector<ObjectID> instances;
  645. Scenario *scenario = scenario_owner.get(p_scenario);
  646. ERR_FAIL_COND_V(!scenario, instances);
  647. const_cast<VisualServerScene *>(this)->update_dirty_instances(); // check dirty instances before culling
  648. int culled = 0;
  649. Instance *cull[1024];
  650. culled = scenario->octree.cull_AABB(p_aabb, cull, 1024);
  651. for (int i = 0; i < culled; i++) {
  652. Instance *instance = cull[i];
  653. ERR_CONTINUE(!instance);
  654. if (instance->object_ID == 0)
  655. continue;
  656. instances.push_back(instance->object_ID);
  657. }
  658. return instances;
  659. }
  660. Vector<ObjectID> VisualServerScene::instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario) const {
  661. Vector<ObjectID> instances;
  662. Scenario *scenario = scenario_owner.get(p_scenario);
  663. ERR_FAIL_COND_V(!scenario, instances);
  664. const_cast<VisualServerScene *>(this)->update_dirty_instances(); // check dirty instances before culling
  665. int culled = 0;
  666. Instance *cull[1024];
  667. culled = scenario->octree.cull_segment(p_from, p_to * 10000, cull, 1024);
  668. for (int i = 0; i < culled; i++) {
  669. Instance *instance = cull[i];
  670. ERR_CONTINUE(!instance);
  671. if (instance->object_ID == 0)
  672. continue;
  673. instances.push_back(instance->object_ID);
  674. }
  675. return instances;
  676. }
  677. Vector<ObjectID> VisualServerScene::instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario) const {
  678. Vector<ObjectID> instances;
  679. Scenario *scenario = scenario_owner.get(p_scenario);
  680. ERR_FAIL_COND_V(!scenario, instances);
  681. const_cast<VisualServerScene *>(this)->update_dirty_instances(); // check dirty instances before culling
  682. int culled = 0;
  683. Instance *cull[1024];
  684. culled = scenario->octree.cull_convex(p_convex, cull, 1024);
  685. for (int i = 0; i < culled; i++) {
  686. Instance *instance = cull[i];
  687. ERR_CONTINUE(!instance);
  688. if (instance->object_ID == 0)
  689. continue;
  690. instances.push_back(instance->object_ID);
  691. }
  692. return instances;
  693. }
  694. void VisualServerScene::instance_geometry_set_flag(RID p_instance, VS::InstanceFlags p_flags, bool p_enabled) {
  695. Instance *instance = instance_owner.get(p_instance);
  696. ERR_FAIL_COND(!instance);
  697. switch (p_flags) {
  698. case VS::INSTANCE_FLAG_CAST_SHADOW: {
  699. if (p_enabled == true) {
  700. instance->cast_shadows = VS::SHADOW_CASTING_SETTING_ON;
  701. } else {
  702. instance->cast_shadows = VS::SHADOW_CASTING_SETTING_OFF;
  703. }
  704. instance->base_material_changed(); // to actually compute if shadows are visible or not
  705. } break;
  706. case VS::INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: {
  707. instance->visible_in_all_rooms = p_enabled;
  708. } break;
  709. case VS::INSTANCE_FLAG_USE_BAKED_LIGHT: {
  710. instance->baked_light = p_enabled;
  711. } break;
  712. }
  713. }
  714. void VisualServerScene::instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting) {
  715. }
  716. void VisualServerScene::instance_geometry_set_material_override(RID p_instance, RID p_material) {
  717. Instance *instance = instance_owner.get(p_instance);
  718. ERR_FAIL_COND(!instance);
  719. if (instance->material_override.is_valid()) {
  720. VSG::storage->material_remove_instance_owner(instance->material_override, instance);
  721. }
  722. instance->material_override = p_material;
  723. instance->base_material_changed();
  724. if (instance->material_override.is_valid()) {
  725. VSG::storage->material_add_instance_owner(instance->material_override, instance);
  726. }
  727. }
  728. void VisualServerScene::instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin) {
  729. }
  730. void VisualServerScene::instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance) {
  731. }
  732. void VisualServerScene::_update_instance(Instance *p_instance) {
  733. p_instance->version++;
  734. if (p_instance->base_type == VS::INSTANCE_LIGHT) {
  735. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  736. VSG::scene_render->light_instance_set_transform(light->instance, p_instance->transform);
  737. light->shadow_dirty = true;
  738. }
  739. if (p_instance->base_type == VS::INSTANCE_REFLECTION_PROBE) {
  740. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  741. VSG::scene_render->reflection_probe_instance_set_transform(reflection_probe->instance, p_instance->transform);
  742. reflection_probe->reflection_dirty = true;
  743. }
  744. if (p_instance->base_type == VS::INSTANCE_PARTICLES) {
  745. VSG::storage->particles_set_emission_transform(p_instance->base, p_instance->transform);
  746. }
  747. if (p_instance->aabb.has_no_surface())
  748. return;
  749. #if 0
  750. if (p_instance->base_type == VS::INSTANCE_PARTICLES) {
  751. rasterizer->particles_instance_set_transform( p_instance->particles_info->instance, p_instance->data.transform );
  752. }
  753. #endif
  754. if ((1 << p_instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  755. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  756. //make sure lights are updated if it casts shadow
  757. if (geom->can_cast_shadows) {
  758. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  759. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  760. light->shadow_dirty = true;
  761. }
  762. }
  763. }
  764. #if 0
  765. else if (p_instance->base_type == INSTANCE_ROOM) {
  766. p_instance->room_info->affine_inverse=p_instance->data.transform.affine_inverse();
  767. } else if (p_instance->base_type == INSTANCE_GI_PROBE) {
  768. Transform scale;
  769. scale.basis.scale(p_instance->gi_probe_info->gi_probe->octree_aabb.size);
  770. scale.origin=p_instance->gi_probe_info->gi_probe->octree_aabb.pos;
  771. //print_line("scale: "+scale);
  772. p_instance->gi_probe_info->affine_inverse=(p_instance->data.transform*scale).affine_inverse();
  773. }
  774. #endif
  775. p_instance->mirror = p_instance->transform.basis.determinant() < 0.0;
  776. Rect3 new_aabb;
  777. #if 0
  778. if (p_instance->base_type==INSTANCE_PORTAL) {
  779. //portals need to be transformed in a special way, so they don't become too wide if they have scale..
  780. Transform portal_xform = p_instance->data.transform;
  781. portal_xform.basis.set_axis(2,portal_xform.basis.get_axis(2).normalized());
  782. p_instance->portal_info->plane_cache=Plane( p_instance->data.transform.origin, portal_xform.basis.get_axis(2));
  783. int point_count=p_instance->portal_info->portal->shape.size();
  784. p_instance->portal_info->transformed_point_cache.resize(point_count);
  785. AABB portal_aabb;
  786. for(int i=0;i<point_count;i++) {
  787. Point2 src = p_instance->portal_info->portal->shape[i];
  788. Vector3 point = portal_xform.xform(Vector3(src.x,src.y,0));
  789. p_instance->portal_info->transformed_point_cache[i]=point;
  790. if (i==0)
  791. portal_aabb.pos=point;
  792. else
  793. portal_aabb.expand_to(point);
  794. }
  795. portal_aabb.grow_by(p_instance->portal_info->portal->connect_range);
  796. new_aabb = portal_aabb;
  797. } else {
  798. #endif
  799. new_aabb = p_instance->transform.xform(p_instance->aabb);
  800. #if 0
  801. }
  802. #endif
  803. p_instance->transformed_aabb = new_aabb;
  804. if (!p_instance->scenario) {
  805. return;
  806. }
  807. if (p_instance->octree_id == 0) {
  808. uint32_t base_type = 1 << p_instance->base_type;
  809. uint32_t pairable_mask = 0;
  810. bool pairable = false;
  811. if (p_instance->base_type == VS::INSTANCE_LIGHT || p_instance->base_type == VS::INSTANCE_REFLECTION_PROBE) {
  812. pairable_mask = p_instance->visible ? VS::INSTANCE_GEOMETRY_MASK : 0;
  813. pairable = true;
  814. }
  815. if (p_instance->base_type == VS::INSTANCE_GI_PROBE) {
  816. //lights and geometries
  817. pairable_mask = p_instance->visible ? VS::INSTANCE_GEOMETRY_MASK | (1 << VS::INSTANCE_LIGHT) : 0;
  818. pairable = true;
  819. }
  820. #if 0
  821. if (p_instance->base_type == VS::INSTANCE_PORTAL) {
  822. pairable_mask=(1<<INSTANCE_PORTAL);
  823. pairable=true;
  824. }
  825. if (p_instance->base_type == VS::INSTANCE_GI_PROBE_SAMPLER) {
  826. pairable_mask=(1<<INSTANCE_GI_PROBE);
  827. pairable=true;
  828. }
  829. if (!p_instance->room && (1<<p_instance->base_type)&VS::INSTANCE_GEOMETRY_MASK) {
  830. base_type|=VS::INSTANCE_ROOMLESS_MASK;
  831. }
  832. if (p_instance->base_type == VS::INSTANCE_ROOM) {
  833. pairable_mask=INSTANCE_ROOMLESS_MASK;
  834. pairable=true;
  835. }
  836. #endif
  837. // not inside octree
  838. p_instance->octree_id = p_instance->scenario->octree.create(p_instance, new_aabb, 0, pairable, base_type, pairable_mask);
  839. } else {
  840. /*
  841. if (new_aabb==p_instance->data.transformed_aabb)
  842. return;
  843. */
  844. p_instance->scenario->octree.move(p_instance->octree_id, new_aabb);
  845. }
  846. #if 0
  847. if (p_instance->base_type==INSTANCE_PORTAL) {
  848. _portal_attempt_connect(p_instance);
  849. }
  850. if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  851. _instance_validate_autorooms(p_instance);
  852. }
  853. if (p_instance->base_type == INSTANCE_ROOM) {
  854. for(Set<Instance*>::Element *E=p_instance->room_info->owned_autoroom_geometry.front();E;E=E->next())
  855. _instance_validate_autorooms(E->get());
  856. }
  857. #endif
  858. }
  859. void VisualServerScene::_update_instance_aabb(Instance *p_instance) {
  860. Rect3 new_aabb;
  861. ERR_FAIL_COND(p_instance->base_type != VS::INSTANCE_NONE && !p_instance->base.is_valid());
  862. switch (p_instance->base_type) {
  863. case VisualServer::INSTANCE_NONE: {
  864. // do nothing
  865. } break;
  866. case VisualServer::INSTANCE_MESH: {
  867. new_aabb = VSG::storage->mesh_get_aabb(p_instance->base, p_instance->skeleton);
  868. } break;
  869. case VisualServer::INSTANCE_MULTIMESH: {
  870. new_aabb = VSG::storage->multimesh_get_aabb(p_instance->base);
  871. } break;
  872. case VisualServer::INSTANCE_IMMEDIATE: {
  873. new_aabb = VSG::storage->immediate_get_aabb(p_instance->base);
  874. } break;
  875. case VisualServer::INSTANCE_PARTICLES: {
  876. new_aabb = VSG::storage->particles_get_aabb(p_instance->base);
  877. } break;
  878. #if 0
  879. case VisualServer::INSTANCE_PARTICLES: {
  880. new_aabb = rasterizer->particles_get_aabb(p_instance->base);
  881. } break;
  882. #endif
  883. case VisualServer::INSTANCE_LIGHT: {
  884. new_aabb = VSG::storage->light_get_aabb(p_instance->base);
  885. } break;
  886. case VisualServer::INSTANCE_REFLECTION_PROBE: {
  887. new_aabb = VSG::storage->reflection_probe_get_aabb(p_instance->base);
  888. } break;
  889. case VisualServer::INSTANCE_GI_PROBE: {
  890. new_aabb = VSG::storage->gi_probe_get_bounds(p_instance->base);
  891. } break;
  892. #if 0
  893. case VisualServer::INSTANCE_ROOM: {
  894. Room *room = room_owner.get( p_instance->base );
  895. ERR_FAIL_COND(!room);
  896. new_aabb=room->bounds.get_aabb();
  897. } break;
  898. case VisualServer::INSTANCE_PORTAL: {
  899. Portal *portal = portal_owner.get( p_instance->base );
  900. ERR_FAIL_COND(!portal);
  901. for (int i=0;i<portal->shape.size();i++) {
  902. Vector3 point( portal->shape[i].x, portal->shape[i].y, 0 );
  903. if (i==0) {
  904. new_aabb.pos=point;
  905. new_aabb.size.z=0.01; // make it not flat for octree
  906. } else {
  907. new_aabb.expand_to(point);
  908. }
  909. }
  910. } break;
  911. case VisualServer::INSTANCE_GI_PROBE: {
  912. BakedLight *gi_probe = gi_probe_owner.get( p_instance->base );
  913. ERR_FAIL_COND(!gi_probe);
  914. new_aabb=gi_probe->octree_aabb;
  915. } break;
  916. case VisualServer::INSTANCE_GI_PROBE_SAMPLER: {
  917. BakedLightSampler *gi_probe_sampler = gi_probe_sampler_owner.get( p_instance->base );
  918. ERR_FAIL_COND(!gi_probe_sampler);
  919. float radius = gi_probe_sampler->params[VS::BAKED_LIGHT_SAMPLER_RADIUS];
  920. new_aabb=AABB(Vector3(-radius,-radius,-radius),Vector3(radius*2,radius*2,radius*2));
  921. } break;
  922. #endif
  923. default: {}
  924. }
  925. if (p_instance->extra_margin)
  926. new_aabb.grow_by(p_instance->extra_margin);
  927. p_instance->aabb = new_aabb;
  928. }
  929. 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) {
  930. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  931. switch (VSG::storage->light_get_type(p_instance->base)) {
  932. case VS::LIGHT_DIRECTIONAL: {
  933. float max_distance = p_cam_projection.get_z_far();
  934. float shadow_max = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE);
  935. if (shadow_max > 0) {
  936. max_distance = MIN(shadow_max, max_distance);
  937. }
  938. max_distance = MAX(max_distance, p_cam_projection.get_z_near() + 0.001);
  939. float range = max_distance - p_cam_projection.get_z_near();
  940. int splits = 0;
  941. switch (VSG::storage->light_directional_get_shadow_mode(p_instance->base)) {
  942. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: splits = 1; break;
  943. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: splits = 2; break;
  944. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: splits = 4; break;
  945. }
  946. float distances[5];
  947. distances[0] = p_cam_projection.get_z_near();
  948. for (int i = 0; i < splits; i++) {
  949. 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;
  950. };
  951. distances[splits] = max_distance;
  952. float texture_size = VSG::scene_render->get_directional_light_shadow_size(light->instance);
  953. bool overlap = VSG::storage->light_directional_get_blend_splits(p_instance->base);
  954. float first_radius = 0.0;
  955. for (int i = 0; i < splits; i++) {
  956. // setup a camera matrix for that range!
  957. CameraMatrix camera_matrix;
  958. float aspect = p_cam_projection.get_aspect();
  959. if (p_cam_orthogonal) {
  960. float w, h;
  961. p_cam_projection.get_viewport_size(w, h);
  962. camera_matrix.set_orthogonal(w, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  963. } else {
  964. float fov = p_cam_projection.get_fov();
  965. camera_matrix.set_perspective(fov, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  966. }
  967. //obtain the frustum endpoints
  968. Vector3 endpoints[8]; // frustum plane endpoints
  969. bool res = camera_matrix.get_endpoints(p_cam_transform, endpoints);
  970. ERR_CONTINUE(!res);
  971. // obtain the light frustm ranges (given endpoints)
  972. Transform transform = p_instance->transform.orthonormalized(); //discard scale and stabilize light
  973. Vector3 x_vec = transform.basis.get_axis(Vector3::AXIS_X).normalized();
  974. Vector3 y_vec = transform.basis.get_axis(Vector3::AXIS_Y).normalized();
  975. Vector3 z_vec = transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  976. //z_vec points agsint the camera, like in default opengl
  977. float x_min, x_max;
  978. float y_min, y_max;
  979. float z_min, z_max;
  980. float x_min_cam, x_max_cam;
  981. float y_min_cam, y_max_cam;
  982. float z_min_cam, z_max_cam;
  983. float bias_scale = 1.0;
  984. //used for culling
  985. for (int j = 0; j < 8; j++) {
  986. float d_x = x_vec.dot(endpoints[j]);
  987. float d_y = y_vec.dot(endpoints[j]);
  988. float d_z = z_vec.dot(endpoints[j]);
  989. if (j == 0 || d_x < x_min)
  990. x_min = d_x;
  991. if (j == 0 || d_x > x_max)
  992. x_max = d_x;
  993. if (j == 0 || d_y < y_min)
  994. y_min = d_y;
  995. if (j == 0 || d_y > y_max)
  996. y_max = d_y;
  997. if (j == 0 || d_z < z_min)
  998. z_min = d_z;
  999. if (j == 0 || d_z > z_max)
  1000. z_max = d_z;
  1001. }
  1002. {
  1003. //camera viewport stuff
  1004. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  1005. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  1006. Vector3 center;
  1007. for (int j = 0; j < 8; j++) {
  1008. center += endpoints[j];
  1009. }
  1010. center /= 8.0;
  1011. //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;
  1012. float radius = 0;
  1013. for (int j = 0; j < 8; j++) {
  1014. float d = center.distance_to(endpoints[j]);
  1015. if (d > radius)
  1016. radius = d;
  1017. }
  1018. radius *= texture_size / (texture_size - 2.0); //add a texel by each side, so stepified texture will always fit
  1019. if (i == 0) {
  1020. first_radius = radius;
  1021. } else {
  1022. bias_scale = radius / first_radius;
  1023. }
  1024. x_max_cam = x_vec.dot(center) + radius;
  1025. x_min_cam = x_vec.dot(center) - radius;
  1026. y_max_cam = y_vec.dot(center) + radius;
  1027. y_min_cam = y_vec.dot(center) - radius;
  1028. z_max_cam = z_vec.dot(center) + radius;
  1029. z_min_cam = z_vec.dot(center) - radius;
  1030. float unit = radius * 2.0 / texture_size;
  1031. x_max_cam = Math::stepify(x_max_cam, unit);
  1032. x_min_cam = Math::stepify(x_min_cam, unit);
  1033. y_max_cam = Math::stepify(y_max_cam, unit);
  1034. y_min_cam = Math::stepify(y_min_cam, unit);
  1035. }
  1036. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  1037. Vector<Plane> light_frustum_planes;
  1038. light_frustum_planes.resize(6);
  1039. //right/left
  1040. light_frustum_planes[0] = Plane(x_vec, x_max);
  1041. light_frustum_planes[1] = Plane(-x_vec, -x_min);
  1042. //top/bottom
  1043. light_frustum_planes[2] = Plane(y_vec, y_max);
  1044. light_frustum_planes[3] = Plane(-y_vec, -y_min);
  1045. //near/far
  1046. light_frustum_planes[4] = Plane(z_vec, z_max + 1e6);
  1047. light_frustum_planes[5] = Plane(-z_vec, -z_min); // z_min is ok, since casters further than far-light plane are not needed
  1048. int cull_count = p_scenario->octree.cull_convex(light_frustum_planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, VS::INSTANCE_GEOMETRY_MASK);
  1049. // a pre pass will need to be needed to determine the actual z-near to be used
  1050. Plane near_plane(p_instance->transform.origin, -p_instance->transform.basis.get_axis(2));
  1051. for (int j = 0; j < cull_count; j++) {
  1052. float min, max;
  1053. Instance *instance = instance_shadow_cull_result[j];
  1054. if (!instance->visible || !((1 << instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1055. cull_count--;
  1056. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1057. j--;
  1058. continue;
  1059. }
  1060. instance->transformed_aabb.project_range_in_plane(Plane(z_vec, 0), min, max);
  1061. instance->depth = near_plane.distance_to(instance->transform.origin);
  1062. instance->depth_layer = 0;
  1063. if (max > z_max)
  1064. z_max = max;
  1065. }
  1066. {
  1067. CameraMatrix ortho_camera;
  1068. real_t half_x = (x_max_cam - x_min_cam) * 0.5;
  1069. real_t half_y = (y_max_cam - y_min_cam) * 0.5;
  1070. ortho_camera.set_orthogonal(-half_x, half_x, -half_y, half_y, 0, (z_max - z_min_cam));
  1071. Transform ortho_transform;
  1072. ortho_transform.basis = transform.basis;
  1073. ortho_transform.origin = x_vec * (x_min_cam + half_x) + y_vec * (y_min_cam + half_y) + z_vec * z_max;
  1074. VSG::scene_render->light_instance_set_shadow_transform(light->instance, ortho_camera, ortho_transform, 0, distances[i + 1], i, bias_scale);
  1075. }
  1076. VSG::scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1077. }
  1078. } break;
  1079. case VS::LIGHT_OMNI: {
  1080. VS::LightOmniShadowMode shadow_mode = VSG::storage->light_omni_get_shadow_mode(p_instance->base);
  1081. switch (shadow_mode) {
  1082. case VS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID: {
  1083. for (int i = 0; i < 2; i++) {
  1084. //using this one ensures that raster deferred will have it
  1085. float radius = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_RANGE);
  1086. float z = i == 0 ? -1 : 1;
  1087. Vector<Plane> planes;
  1088. planes.resize(5);
  1089. planes[0] = p_instance->transform.xform(Plane(Vector3(0, 0, z), radius));
  1090. planes[1] = p_instance->transform.xform(Plane(Vector3(1, 0, z).normalized(), radius));
  1091. planes[2] = p_instance->transform.xform(Plane(Vector3(-1, 0, z).normalized(), radius));
  1092. planes[3] = p_instance->transform.xform(Plane(Vector3(0, 1, z).normalized(), radius));
  1093. planes[4] = p_instance->transform.xform(Plane(Vector3(0, -1, z).normalized(), radius));
  1094. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, VS::INSTANCE_GEOMETRY_MASK);
  1095. Plane near_plane(p_instance->transform.origin, p_instance->transform.basis.get_axis(2) * z);
  1096. for (int j = 0; j < cull_count; j++) {
  1097. Instance *instance = instance_shadow_cull_result[j];
  1098. if (!instance->visible || !((1 << instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1099. cull_count--;
  1100. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1101. j--;
  1102. } else {
  1103. instance->depth = near_plane.distance_to(instance->transform.origin);
  1104. instance->depth_layer = 0;
  1105. }
  1106. }
  1107. VSG::scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), p_instance->transform, radius, 0, i);
  1108. VSG::scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1109. }
  1110. } break;
  1111. case VS::LIGHT_OMNI_SHADOW_CUBE: {
  1112. float radius = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_RANGE);
  1113. CameraMatrix cm;
  1114. cm.set_perspective(90, 1, 0.01, radius);
  1115. for (int i = 0; i < 6; i++) {
  1116. //using this one ensures that raster deferred will have it
  1117. static const Vector3 view_normals[6] = {
  1118. Vector3(-1, 0, 0),
  1119. Vector3(+1, 0, 0),
  1120. Vector3(0, -1, 0),
  1121. Vector3(0, +1, 0),
  1122. Vector3(0, 0, -1),
  1123. Vector3(0, 0, +1)
  1124. };
  1125. static const Vector3 view_up[6] = {
  1126. Vector3(0, -1, 0),
  1127. Vector3(0, -1, 0),
  1128. Vector3(0, 0, -1),
  1129. Vector3(0, 0, +1),
  1130. Vector3(0, -1, 0),
  1131. Vector3(0, -1, 0)
  1132. };
  1133. Transform xform = p_instance->transform * Transform().looking_at(view_normals[i], view_up[i]);
  1134. Vector<Plane> planes = cm.get_projection_planes(xform);
  1135. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, VS::INSTANCE_GEOMETRY_MASK);
  1136. Plane near_plane(xform.origin, -xform.basis.get_axis(2));
  1137. for (int j = 0; j < cull_count; j++) {
  1138. Instance *instance = instance_shadow_cull_result[j];
  1139. if (!instance->visible || !((1 << instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1140. cull_count--;
  1141. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1142. j--;
  1143. } else {
  1144. instance->depth = near_plane.distance_to(instance->transform.origin);
  1145. instance->depth_layer = 0;
  1146. }
  1147. }
  1148. VSG::scene_render->light_instance_set_shadow_transform(light->instance, cm, xform, radius, 0, i);
  1149. VSG::scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1150. }
  1151. //restore the regular DP matrix
  1152. VSG::scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), p_instance->transform, radius, 0, 0);
  1153. } break;
  1154. }
  1155. } break;
  1156. case VS::LIGHT_SPOT: {
  1157. float radius = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_RANGE);
  1158. float angle = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_SPOT_ANGLE);
  1159. CameraMatrix cm;
  1160. cm.set_perspective(angle * 2.0, 1.0, 0.01, radius);
  1161. Vector<Plane> planes = cm.get_projection_planes(p_instance->transform);
  1162. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, VS::INSTANCE_GEOMETRY_MASK);
  1163. Plane near_plane(p_instance->transform.origin, -p_instance->transform.basis.get_axis(2));
  1164. for (int j = 0; j < cull_count; j++) {
  1165. Instance *instance = instance_shadow_cull_result[j];
  1166. if (!instance->visible || !((1 << instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1167. cull_count--;
  1168. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1169. j--;
  1170. } else {
  1171. instance->depth = near_plane.distance_to(instance->transform.origin);
  1172. instance->depth_layer = 0;
  1173. }
  1174. }
  1175. VSG::scene_render->light_instance_set_shadow_transform(light->instance, cm, p_instance->transform, radius, 0, 0);
  1176. VSG::scene_render->render_shadow(light->instance, p_shadow_atlas, 0, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1177. } break;
  1178. }
  1179. }
  1180. void VisualServerScene::render_camera(RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas) {
  1181. Camera *camera = camera_owner.getornull(p_camera);
  1182. ERR_FAIL_COND(!camera);
  1183. /* STEP 1 - SETUP CAMERA */
  1184. CameraMatrix camera_matrix;
  1185. bool ortho = false;
  1186. switch (camera->type) {
  1187. case Camera::ORTHOGONAL: {
  1188. camera_matrix.set_orthogonal(
  1189. camera->size,
  1190. p_viewport_size.width / (float)p_viewport_size.height,
  1191. camera->znear,
  1192. camera->zfar,
  1193. camera->vaspect
  1194. );
  1195. ortho = true;
  1196. } break;
  1197. case Camera::PERSPECTIVE: {
  1198. camera_matrix.set_perspective(
  1199. camera->fov,
  1200. p_viewport_size.width / (float)p_viewport_size.height,
  1201. camera->znear,
  1202. camera->zfar,
  1203. camera->vaspect
  1204. );
  1205. ortho = false;
  1206. } break;
  1207. }
  1208. _render_scene(camera->transform, camera_matrix, ortho, camera->env, camera->visible_layers, p_scenario, p_shadow_atlas, RID(), -1);
  1209. }
  1210. 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) {
  1211. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1212. render_pass++;
  1213. uint32_t camera_layer_mask = p_visible_layers;
  1214. VSG::scene_render->set_scene_pass(render_pass);
  1215. //rasterizer->set_camera(camera->transform, camera_matrix,ortho);
  1216. Vector<Plane> planes = p_cam_projection.get_projection_planes(p_cam_transform);
  1217. Plane near_plane(p_cam_transform.origin, -p_cam_transform.basis.get_axis(2).normalized());
  1218. float z_far = p_cam_projection.get_z_far();
  1219. /* STEP 2 - CULL */
  1220. int cull_count = scenario->octree.cull_convex(planes, instance_cull_result, MAX_INSTANCE_CULL);
  1221. light_cull_count = 0;
  1222. reflection_probe_cull_count = 0;
  1223. //light_samplers_culled=0;
  1224. /* print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  1225. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  1226. //print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  1227. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  1228. */
  1229. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  1230. // compute portals
  1231. #if 0
  1232. exterior_visited=false;
  1233. exterior_portal_cull_count=0;
  1234. if (room_cull_enabled) {
  1235. for(int i=0;i<cull_count;i++) {
  1236. Instance *ins = instance_cull_result[i];
  1237. ins->last_render_pass=render_pass;
  1238. if (ins->base_type!=INSTANCE_PORTAL)
  1239. continue;
  1240. if (ins->room)
  1241. continue;
  1242. ERR_CONTINUE(exterior_portal_cull_count>=MAX_EXTERIOR_PORTALS);
  1243. exterior_portal_cull_result[exterior_portal_cull_count++]=ins;
  1244. }
  1245. room_cull_count = p_scenario->octree.cull_point(camera->transform.origin,room_cull_result,MAX_ROOM_CULL,NULL,(1<<INSTANCE_ROOM)|(1<<INSTANCE_PORTAL));
  1246. Set<Instance*> current_rooms;
  1247. Set<Instance*> portal_rooms;
  1248. //add to set
  1249. for(int i=0;i<room_cull_count;i++) {
  1250. if (room_cull_result[i]->base_type==INSTANCE_ROOM) {
  1251. current_rooms.insert(room_cull_result[i]);
  1252. }
  1253. if (room_cull_result[i]->base_type==INSTANCE_PORTAL) {
  1254. //assume inside that room if also inside the portal..
  1255. if (room_cull_result[i]->room) {
  1256. portal_rooms.insert(room_cull_result[i]->room);
  1257. }
  1258. SWAP(room_cull_result[i],room_cull_result[room_cull_count-1]);
  1259. room_cull_count--;
  1260. i--;
  1261. }
  1262. }
  1263. //remove from set if it has a parent room or BSP doesn't contain
  1264. for(int i=0;i<room_cull_count;i++) {
  1265. Instance *r = room_cull_result[i];
  1266. //check inside BSP
  1267. Vector3 room_local_point = r->room_info->affine_inverse.xform( camera->transform.origin );
  1268. if (!portal_rooms.has(r) && !r->room_info->room->bounds.point_is_inside(room_local_point)) {
  1269. current_rooms.erase(r);
  1270. continue;
  1271. }
  1272. //check parent
  1273. while (r->room) {// has parent room
  1274. current_rooms.erase(r);
  1275. r=r->room;
  1276. }
  1277. }
  1278. if (current_rooms.size()) {
  1279. //camera is inside a room
  1280. // go through rooms
  1281. for(Set<Instance*>::Element *E=current_rooms.front();E;E=E->next()) {
  1282. _cull_room(camera,E->get());
  1283. }
  1284. } else {
  1285. //start from exterior
  1286. _cull_room(camera,NULL);
  1287. }
  1288. }
  1289. #endif
  1290. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  1291. for (int i = 0; i < cull_count; i++) {
  1292. Instance *ins = instance_cull_result[i];
  1293. bool keep = false;
  1294. if ((camera_layer_mask & ins->layer_mask) == 0) {
  1295. //failure
  1296. } else if (ins->base_type == VS::INSTANCE_LIGHT && ins->visible) {
  1297. if (ins->visible && light_cull_count < MAX_LIGHTS_CULLED) {
  1298. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1299. if (!light->geometries.empty()) {
  1300. //do not add this light if no geometry is affected by it..
  1301. light_cull_result[light_cull_count] = ins;
  1302. light_instance_cull_result[light_cull_count] = light->instance;
  1303. if (p_shadow_atlas.is_valid() && VSG::storage->light_has_shadow(ins->base)) {
  1304. VSG::scene_render->light_instance_mark_visible(light->instance); //mark it visible for shadow allocation later
  1305. }
  1306. light_cull_count++;
  1307. }
  1308. }
  1309. } else if (ins->base_type == VS::INSTANCE_REFLECTION_PROBE && ins->visible) {
  1310. if (ins->visible && reflection_probe_cull_count < MAX_REFLECTION_PROBES_CULLED) {
  1311. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(ins->base_data);
  1312. if (p_reflection_probe != reflection_probe->instance) {
  1313. //avoid entering The Matrix
  1314. if (!reflection_probe->geometries.empty()) {
  1315. //do not add this light if no geometry is affected by it..
  1316. if (reflection_probe->reflection_dirty || VSG::scene_render->reflection_probe_instance_needs_redraw(reflection_probe->instance)) {
  1317. if (!reflection_probe->update_list.in_list()) {
  1318. reflection_probe->render_step = 0;
  1319. reflection_probe_render_list.add_last(&reflection_probe->update_list);
  1320. }
  1321. reflection_probe->reflection_dirty = false;
  1322. }
  1323. if (VSG::scene_render->reflection_probe_instance_has_reflection(reflection_probe->instance)) {
  1324. reflection_probe_instance_cull_result[reflection_probe_cull_count] = reflection_probe->instance;
  1325. reflection_probe_cull_count++;
  1326. }
  1327. }
  1328. }
  1329. }
  1330. } else if (ins->base_type == VS::INSTANCE_GI_PROBE && ins->visible) {
  1331. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(ins->base_data);
  1332. if (!gi_probe->update_element.in_list()) {
  1333. gi_probe_update_list.add(&gi_probe->update_element);
  1334. }
  1335. } else if ((1 << ins->base_type) & VS::INSTANCE_GEOMETRY_MASK && ins->visible && ins->cast_shadows != VS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) {
  1336. keep = true;
  1337. #if 0
  1338. bool discarded=false;
  1339. if (ins->draw_range_end>0) {
  1340. float d = cull_range.nearp.distance_to(ins->data.transform.origin);
  1341. if (d<0)
  1342. d=0;
  1343. discarded=(d<ins->draw_range_begin || d>=ins->draw_range_end);
  1344. }
  1345. if (!discarded) {
  1346. // test if this geometry should be visible
  1347. if (room_cull_enabled) {
  1348. if (ins->visible_in_all_rooms) {
  1349. keep=true;
  1350. } else if (ins->room) {
  1351. if (ins->room->room_info->last_visited_pass==render_pass)
  1352. keep=true;
  1353. } else if (ins->auto_rooms.size()) {
  1354. for(Set<Instance*>::Element *E=ins->auto_rooms.front();E;E=E->next()) {
  1355. if (E->get()->room_info->last_visited_pass==render_pass) {
  1356. keep=true;
  1357. break;
  1358. }
  1359. }
  1360. } else if(exterior_visited)
  1361. keep=true;
  1362. } else {
  1363. keep=true;
  1364. }
  1365. }
  1366. if (keep) {
  1367. // update cull range
  1368. float min,max;
  1369. ins->transformed_aabb.project_range_in_plane(cull_range.nearp,min,max);
  1370. if (min<cull_range.min)
  1371. cull_range.min=min;
  1372. if (max>cull_range.max)
  1373. cull_range.max=max;
  1374. if (ins->sampled_light && ins->sampled_light->gi_probe_sampler_info->last_pass!=render_pass) {
  1375. if (light_samplers_culled<MAX_LIGHT_SAMPLERS) {
  1376. light_sampler_cull_result[light_samplers_culled++]=ins->sampled_light;
  1377. ins->sampled_light->gi_probe_sampler_info->last_pass=render_pass;
  1378. }
  1379. }
  1380. }
  1381. #endif
  1382. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(ins->base_data);
  1383. if (ins->base_type == VS::INSTANCE_PARTICLES) {
  1384. //particles visible? process them
  1385. VSG::storage->particles_request_process(ins->base);
  1386. //particles visible? request redraw
  1387. VisualServerRaster::redraw_request();
  1388. }
  1389. if (geom->lighting_dirty) {
  1390. int l = 0;
  1391. //only called when lights AABB enter/exit this geometry
  1392. ins->light_instances.resize(geom->lighting.size());
  1393. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  1394. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1395. ins->light_instances[l++] = light->instance;
  1396. }
  1397. geom->lighting_dirty = false;
  1398. }
  1399. if (geom->reflection_dirty) {
  1400. int l = 0;
  1401. //only called when reflection probe AABB enter/exit this geometry
  1402. ins->reflection_probe_instances.resize(geom->reflection_probes.size());
  1403. for (List<Instance *>::Element *E = geom->reflection_probes.front(); E; E = E->next()) {
  1404. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(E->get()->base_data);
  1405. ins->reflection_probe_instances[l++] = reflection_probe->instance;
  1406. }
  1407. geom->reflection_dirty = false;
  1408. }
  1409. if (geom->gi_probes_dirty) {
  1410. int l = 0;
  1411. //only called when reflection probe AABB enter/exit this geometry
  1412. ins->gi_probe_instances.resize(geom->gi_probes.size());
  1413. for (List<Instance *>::Element *E = geom->gi_probes.front(); E; E = E->next()) {
  1414. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(E->get()->base_data);
  1415. ins->gi_probe_instances[l++] = gi_probe->probe_instance;
  1416. }
  1417. geom->gi_probes_dirty = false;
  1418. }
  1419. ins->depth = near_plane.distance_to(ins->transform.origin);
  1420. ins->depth_layer = CLAMP(int(ins->depth * 8 / z_far), 0, 7);
  1421. }
  1422. if (!keep) {
  1423. // remove, no reason to keep
  1424. cull_count--;
  1425. SWAP(instance_cull_result[i], instance_cull_result[cull_count]);
  1426. i--;
  1427. ins->last_render_pass = 0; // make invalid
  1428. } else {
  1429. ins->last_render_pass = render_pass;
  1430. }
  1431. }
  1432. /* STEP 5 - PROCESS LIGHTS */
  1433. RID *directional_light_ptr = &light_instance_cull_result[light_cull_count];
  1434. int directional_light_count = 0;
  1435. // directional lights
  1436. {
  1437. Instance **lights_with_shadow = (Instance **)alloca(sizeof(Instance *) * scenario->directional_lights.size());
  1438. int directional_shadow_count = 0;
  1439. for (List<Instance *>::Element *E = scenario->directional_lights.front(); E; E = E->next()) {
  1440. if (light_cull_count + directional_light_count >= MAX_LIGHTS_CULLED) {
  1441. break;
  1442. }
  1443. if (!E->get()->visible)
  1444. continue;
  1445. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1446. //check shadow..
  1447. if (light && p_shadow_atlas.is_valid() && VSG::storage->light_has_shadow(E->get()->base)) {
  1448. lights_with_shadow[directional_shadow_count++] = E->get();
  1449. }
  1450. //add to list
  1451. directional_light_ptr[directional_light_count++] = light->instance;
  1452. }
  1453. VSG::scene_render->set_directional_shadow_count(directional_shadow_count);
  1454. for (int i = 0; i < directional_shadow_count; i++) {
  1455. _light_instance_update_shadow(lights_with_shadow[i], p_cam_transform, p_cam_projection, p_cam_orthogonal, p_shadow_atlas, scenario);
  1456. }
  1457. }
  1458. { //setup shadow maps
  1459. //SortArray<Instance*,_InstanceLightsort> sorter;
  1460. //sorter.sort(light_cull_result,light_cull_count);
  1461. for (int i = 0; i < light_cull_count; i++) {
  1462. Instance *ins = light_cull_result[i];
  1463. if (!p_shadow_atlas.is_valid() || !VSG::storage->light_has_shadow(ins->base))
  1464. continue;
  1465. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1466. float coverage;
  1467. { //compute coverage
  1468. Transform cam_xf = p_cam_transform;
  1469. float zn = p_cam_projection.get_z_near();
  1470. Plane p(cam_xf.origin + cam_xf.basis.get_axis(2) * -zn, -cam_xf.basis.get_axis(2)); //camera near plane
  1471. float vp_w, vp_h; //near plane size in screen coordinates
  1472. p_cam_projection.get_viewport_size(vp_w, vp_h);
  1473. switch (VSG::storage->light_get_type(ins->base)) {
  1474. case VS::LIGHT_OMNI: {
  1475. float radius = VSG::storage->light_get_param(ins->base, VS::LIGHT_PARAM_RANGE);
  1476. //get two points parallel to near plane
  1477. Vector3 points[2] = {
  1478. ins->transform.origin,
  1479. ins->transform.origin + cam_xf.basis.get_axis(0) * radius
  1480. };
  1481. if (!p_cam_orthogonal) {
  1482. //if using perspetive, map them to near plane
  1483. for (int j = 0; j < 2; j++) {
  1484. if (p.distance_to(points[j]) < 0) {
  1485. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1486. }
  1487. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1488. }
  1489. }
  1490. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1491. coverage = screen_diameter / (vp_w + vp_h);
  1492. } break;
  1493. case VS::LIGHT_SPOT: {
  1494. float radius = VSG::storage->light_get_param(ins->base, VS::LIGHT_PARAM_RANGE);
  1495. float angle = VSG::storage->light_get_param(ins->base, VS::LIGHT_PARAM_SPOT_ANGLE);
  1496. float w = radius * Math::sin(Math::deg2rad(angle));
  1497. float d = radius * Math::cos(Math::deg2rad(angle));
  1498. Vector3 base = ins->transform.origin - ins->transform.basis.get_axis(2).normalized() * d;
  1499. Vector3 points[2] = {
  1500. base,
  1501. base + cam_xf.basis.get_axis(0) * w
  1502. };
  1503. if (!p_cam_orthogonal) {
  1504. //if using perspetive, map them to near plane
  1505. for (int j = 0; j < 2; j++) {
  1506. if (p.distance_to(points[j]) < 0) {
  1507. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1508. }
  1509. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1510. }
  1511. }
  1512. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1513. coverage = screen_diameter / (vp_w + vp_h);
  1514. } break;
  1515. default: {
  1516. ERR_PRINT("Invalid Light Type");
  1517. }
  1518. }
  1519. }
  1520. if (light->shadow_dirty) {
  1521. light->last_version++;
  1522. light->shadow_dirty = false;
  1523. }
  1524. bool redraw = VSG::scene_render->shadow_atlas_update_light(p_shadow_atlas, light->instance, coverage, light->last_version);
  1525. if (redraw) {
  1526. //must redraw!
  1527. _light_instance_update_shadow(ins, p_cam_transform, p_cam_projection, p_cam_orthogonal, p_shadow_atlas, scenario);
  1528. }
  1529. }
  1530. }
  1531. /* ENVIRONMENT */
  1532. RID environment;
  1533. if (p_force_environment.is_valid()) //camera has more environment priority
  1534. environment = p_force_environment;
  1535. else if (scenario->environment.is_valid())
  1536. environment = scenario->environment;
  1537. else
  1538. environment = scenario->fallback_environment;
  1539. #if 0
  1540. /* STEP 6 - SAMPLE BAKED LIGHT */
  1541. bool islinear =false;
  1542. if (environment.is_valid()) {
  1543. islinear = rasterizer->environment_is_fx_enabled(environment,VS::ENV_FX_SRGB);
  1544. }
  1545. for(int i=0;i<light_samplers_culled;i++) {
  1546. _process_sampled_light(camera->transform,light_sampler_cull_result[i],islinear);
  1547. }
  1548. #endif
  1549. /* STEP 7 - PROCESS GEOMETRY AND DRAW SCENE*/
  1550. 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);
  1551. }
  1552. void VisualServerScene::render_empty_scene(RID p_scenario, RID p_shadow_atlas) {
  1553. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1554. RID environment;
  1555. if (scenario->environment.is_valid())
  1556. environment = scenario->environment;
  1557. else
  1558. environment = scenario->fallback_environment;
  1559. VSG::scene_render->render_scene(Transform(), CameraMatrix(), true, NULL, 0, NULL, 0, NULL, 0, environment, p_shadow_atlas, scenario->reflection_atlas, RID(), 0);
  1560. }
  1561. bool VisualServerScene::_render_reflection_probe_step(Instance *p_instance, int p_step) {
  1562. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  1563. Scenario *scenario = p_instance->scenario;
  1564. ERR_FAIL_COND_V(!scenario, true);
  1565. if (p_step == 0) {
  1566. if (!VSG::scene_render->reflection_probe_instance_begin_render(reflection_probe->instance, scenario->reflection_atlas)) {
  1567. return true; //sorry, all full :(
  1568. }
  1569. }
  1570. if (p_step >= 0 && p_step < 6) {
  1571. static const Vector3 view_normals[6] = {
  1572. Vector3(-1, 0, 0),
  1573. Vector3(+1, 0, 0),
  1574. Vector3(0, -1, 0),
  1575. Vector3(0, +1, 0),
  1576. Vector3(0, 0, -1),
  1577. Vector3(0, 0, +1)
  1578. };
  1579. Vector3 extents = VSG::storage->reflection_probe_get_extents(p_instance->base);
  1580. Vector3 origin_offset = VSG::storage->reflection_probe_get_origin_offset(p_instance->base);
  1581. float max_distance = VSG::storage->reflection_probe_get_origin_max_distance(p_instance->base);
  1582. Vector3 edge = view_normals[p_step] * extents;
  1583. 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
  1584. max_distance = MAX(max_distance, distance);
  1585. //render cubemap side
  1586. CameraMatrix cm;
  1587. cm.set_perspective(90, 1, 0.01, max_distance);
  1588. static const Vector3 view_up[6] = {
  1589. Vector3(0, -1, 0),
  1590. Vector3(0, -1, 0),
  1591. Vector3(0, 0, -1),
  1592. Vector3(0, 0, +1),
  1593. Vector3(0, -1, 0),
  1594. Vector3(0, -1, 0)
  1595. };
  1596. Transform local_view;
  1597. local_view.set_look_at(origin_offset, origin_offset + view_normals[p_step], view_up[p_step]);
  1598. Transform xform = p_instance->transform * local_view;
  1599. RID shadow_atlas;
  1600. if (VSG::storage->reflection_probe_renders_shadows(p_instance->base)) {
  1601. shadow_atlas = scenario->reflection_probe_shadow_atlas;
  1602. }
  1603. _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);
  1604. } else {
  1605. //do roughness postprocess step until it belives it's done
  1606. return VSG::scene_render->reflection_probe_instance_postprocess_step(reflection_probe->instance);
  1607. }
  1608. return false;
  1609. }
  1610. void VisualServerScene::_gi_probe_fill_local_data(int p_idx, int p_level, int p_x, int p_y, int p_z, const GIProbeDataCell *p_cell, const GIProbeDataHeader *p_header, InstanceGIProbeData::LocalData *p_local_data, Vector<uint32_t> *prev_cell) {
  1611. if (p_level == p_header->cell_subdiv - 1) {
  1612. Vector3 emission;
  1613. emission.x = (p_cell[p_idx].emission >> 24) / 255.0;
  1614. emission.y = ((p_cell[p_idx].emission >> 16) & 0xFF) / 255.0;
  1615. emission.z = ((p_cell[p_idx].emission >> 8) & 0xFF) / 255.0;
  1616. float l = (p_cell[p_idx].emission & 0xFF) / 255.0;
  1617. l *= 8.0;
  1618. emission *= l;
  1619. p_local_data[p_idx].energy[0] = uint16_t(emission.x * 1024); //go from 0 to 1024 for light
  1620. p_local_data[p_idx].energy[1] = uint16_t(emission.y * 1024); //go from 0 to 1024 for light
  1621. p_local_data[p_idx].energy[2] = uint16_t(emission.z * 1024); //go from 0 to 1024 for light
  1622. } else {
  1623. p_local_data[p_idx].energy[0] = 0;
  1624. p_local_data[p_idx].energy[1] = 0;
  1625. p_local_data[p_idx].energy[2] = 0;
  1626. int half = (1 << (p_header->cell_subdiv - 1)) >> (p_level + 1);
  1627. for (int i = 0; i < 8; i++) {
  1628. uint32_t child = p_cell[p_idx].children[i];
  1629. if (child == 0xFFFFFFFF)
  1630. continue;
  1631. int x = p_x;
  1632. int y = p_y;
  1633. int z = p_z;
  1634. if (i & 1)
  1635. x += half;
  1636. if (i & 2)
  1637. y += half;
  1638. if (i & 4)
  1639. z += half;
  1640. _gi_probe_fill_local_data(child, p_level + 1, x, y, z, p_cell, p_header, p_local_data, prev_cell);
  1641. }
  1642. }
  1643. //position for each part of the mipmaped texture
  1644. p_local_data[p_idx].pos[0] = p_x >> (p_header->cell_subdiv - p_level - 1);
  1645. p_local_data[p_idx].pos[1] = p_y >> (p_header->cell_subdiv - p_level - 1);
  1646. p_local_data[p_idx].pos[2] = p_z >> (p_header->cell_subdiv - p_level - 1);
  1647. prev_cell[p_level].push_back(p_idx);
  1648. }
  1649. void VisualServerScene::_gi_probe_bake_threads(void *self) {
  1650. VisualServerScene *vss = (VisualServerScene *)self;
  1651. vss->_gi_probe_bake_thread();
  1652. }
  1653. void VisualServerScene::_setup_gi_probe(Instance *p_instance) {
  1654. InstanceGIProbeData *probe = static_cast<InstanceGIProbeData *>(p_instance->base_data);
  1655. if (probe->dynamic.probe_data.is_valid()) {
  1656. VSG::storage->free(probe->dynamic.probe_data);
  1657. probe->dynamic.probe_data = RID();
  1658. }
  1659. probe->dynamic.light_data = VSG::storage->gi_probe_get_dynamic_data(p_instance->base);
  1660. if (probe->dynamic.light_data.size() == 0)
  1661. return;
  1662. //using dynamic data
  1663. PoolVector<int>::Read r = probe->dynamic.light_data.read();
  1664. const GIProbeDataHeader *header = (GIProbeDataHeader *)r.ptr();
  1665. probe->dynamic.local_data.resize(header->cell_count);
  1666. int cell_count = probe->dynamic.local_data.size();
  1667. PoolVector<InstanceGIProbeData::LocalData>::Write ldw = probe->dynamic.local_data.write();
  1668. const GIProbeDataCell *cells = (GIProbeDataCell *)&r[16];
  1669. probe->dynamic.level_cell_lists.resize(header->cell_subdiv);
  1670. _gi_probe_fill_local_data(0, 0, 0, 0, 0, cells, header, ldw.ptr(), probe->dynamic.level_cell_lists.ptr());
  1671. bool compress = VSG::storage->gi_probe_is_compressed(p_instance->base);
  1672. probe->dynamic.compression = compress ? VSG::storage->gi_probe_get_dynamic_data_get_preferred_compression() : RasterizerStorage::GI_PROBE_UNCOMPRESSED;
  1673. probe->dynamic.probe_data = VSG::storage->gi_probe_dynamic_data_create(header->width, header->height, header->depth, probe->dynamic.compression);
  1674. probe->dynamic.bake_dynamic_range = VSG::storage->gi_probe_get_dynamic_range(p_instance->base);
  1675. probe->dynamic.mipmaps_3d.clear();
  1676. probe->dynamic.propagate = VSG::storage->gi_probe_get_propagation(p_instance->base);
  1677. probe->dynamic.grid_size[0] = header->width;
  1678. probe->dynamic.grid_size[1] = header->height;
  1679. probe->dynamic.grid_size[2] = header->depth;
  1680. int size_limit = 1;
  1681. int size_divisor = 1;
  1682. if (probe->dynamic.compression == RasterizerStorage::GI_PROBE_S3TC) {
  1683. print_line("S3TC");
  1684. size_limit = 4;
  1685. size_divisor = 4;
  1686. }
  1687. for (int i = 0; i < (int)header->cell_subdiv; i++) {
  1688. uint32_t x = header->width >> i;
  1689. uint32_t y = header->height >> i;
  1690. uint32_t z = header->depth >> i;
  1691. //create and clear mipmap
  1692. PoolVector<uint8_t> mipmap;
  1693. int size = x * y * z * 4;
  1694. size /= size_divisor;
  1695. mipmap.resize(size);
  1696. PoolVector<uint8_t>::Write w = mipmap.write();
  1697. zeromem(w.ptr(), size);
  1698. w = PoolVector<uint8_t>::Write();
  1699. probe->dynamic.mipmaps_3d.push_back(mipmap);
  1700. if (x <= size_limit || y <= size_limit || z <= size_limit)
  1701. break;
  1702. }
  1703. probe->dynamic.updating_stage = GI_UPDATE_STAGE_CHECK;
  1704. probe->invalid = false;
  1705. probe->dynamic.enabled = true;
  1706. Transform cell_to_xform = VSG::storage->gi_probe_get_to_cell_xform(p_instance->base);
  1707. Rect3 bounds = VSG::storage->gi_probe_get_bounds(p_instance->base);
  1708. float cell_size = VSG::storage->gi_probe_get_cell_size(p_instance->base);
  1709. probe->dynamic.light_to_cell_xform = cell_to_xform * p_instance->transform.affine_inverse();
  1710. VSG::scene_render->gi_probe_instance_set_light_data(probe->probe_instance, p_instance->base, probe->dynamic.probe_data);
  1711. VSG::scene_render->gi_probe_instance_set_transform_to_data(probe->probe_instance, probe->dynamic.light_to_cell_xform);
  1712. VSG::scene_render->gi_probe_instance_set_bounds(probe->probe_instance, bounds.size / cell_size);
  1713. probe->base_version = VSG::storage->gi_probe_get_version(p_instance->base);
  1714. //if compression is S3TC, fill it up
  1715. if (probe->dynamic.compression == RasterizerStorage::GI_PROBE_S3TC) {
  1716. //create all blocks
  1717. Vector<Map<uint32_t, InstanceGIProbeData::CompBlockS3TC> > comp_blocks;
  1718. int mipmap_count = probe->dynamic.mipmaps_3d.size();
  1719. comp_blocks.resize(mipmap_count);
  1720. for (int i = 0; i < cell_count; i++) {
  1721. const GIProbeDataCell &c = cells[i];
  1722. const InstanceGIProbeData::LocalData &ld = ldw[i];
  1723. int level = c.level_alpha >> 16;
  1724. int mipmap = header->cell_subdiv - level - 1;
  1725. if (mipmap >= mipmap_count)
  1726. continue; //uninteresting
  1727. int blockx = (ld.pos[0] >> 2);
  1728. int blocky = (ld.pos[1] >> 2);
  1729. int blockz = (ld.pos[2]); //compression is x/y only
  1730. int blockw = (header->width >> mipmap) >> 2;
  1731. int blockh = (header->height >> mipmap) >> 2;
  1732. //print_line("cell "+itos(i)+" level "+itos(level)+"mipmap: "+itos(mipmap)+" pos: "+Vector3(blockx,blocky,blockz)+" size "+Vector2(blockw,blockh));
  1733. uint32_t key = blockz * blockw * blockh + blocky * blockw + blockx;
  1734. Map<uint32_t, InstanceGIProbeData::CompBlockS3TC> &cmap = comp_blocks[mipmap];
  1735. if (!cmap.has(key)) {
  1736. InstanceGIProbeData::CompBlockS3TC k;
  1737. k.offset = key; //use offset as counter first
  1738. k.source_count = 0;
  1739. cmap[key] = k;
  1740. }
  1741. InstanceGIProbeData::CompBlockS3TC &k = cmap[key];
  1742. ERR_CONTINUE(k.source_count == 16);
  1743. k.sources[k.source_count++] = i;
  1744. }
  1745. //fix the blocks, precomputing what is needed
  1746. probe->dynamic.mipmaps_s3tc.resize(mipmap_count);
  1747. for (int i = 0; i < mipmap_count; i++) {
  1748. print_line("S3TC level: " + itos(i) + " blocks: " + itos(comp_blocks[i].size()));
  1749. probe->dynamic.mipmaps_s3tc[i].resize(comp_blocks[i].size());
  1750. PoolVector<InstanceGIProbeData::CompBlockS3TC>::Write w = probe->dynamic.mipmaps_s3tc[i].write();
  1751. int block_idx = 0;
  1752. for (Map<uint32_t, InstanceGIProbeData::CompBlockS3TC>::Element *E = comp_blocks[i].front(); E; E = E->next()) {
  1753. InstanceGIProbeData::CompBlockS3TC k = E->get();
  1754. //PRECOMPUTE ALPHA
  1755. int max_alpha = -100000;
  1756. int min_alpha = k.source_count == 16 ? 100000 : 0; //if the block is not completely full, minimum is always 0, (and those blocks will map to 1, which will be zero)
  1757. uint8_t alpha_block[4][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
  1758. for (int j = 0; j < k.source_count; j++) {
  1759. int alpha = (cells[k.sources[j]].level_alpha >> 8) & 0xFF;
  1760. if (alpha < min_alpha)
  1761. min_alpha = alpha;
  1762. if (alpha > max_alpha)
  1763. max_alpha = alpha;
  1764. //fill up alpha block
  1765. alpha_block[ldw[k.sources[j]].pos[0] % 4][ldw[k.sources[j]].pos[1] % 4] = alpha;
  1766. }
  1767. //use the first mode (8 adjustable levels)
  1768. k.alpha[0] = max_alpha;
  1769. k.alpha[1] = min_alpha;
  1770. uint64_t alpha_bits = 0;
  1771. if (max_alpha != min_alpha) {
  1772. int idx = 0;
  1773. for (int y = 0; y < 4; y++) {
  1774. for (int x = 0; x < 4; x++) {
  1775. //subtract minimum
  1776. uint32_t a = uint32_t(alpha_block[x][y]) - min_alpha;
  1777. //convert range to 3 bits
  1778. a = int((a * 7.0 / (max_alpha - min_alpha)) + 0.5);
  1779. a = CLAMP(a, 0, 7); //just to be sure
  1780. a = 7 - a; //because range is inverted in this mode
  1781. if (a == 0) {
  1782. //do none, remain
  1783. } else if (a == 7) {
  1784. a = 1;
  1785. } else {
  1786. a = a + 1;
  1787. }
  1788. alpha_bits |= uint64_t(a) << (idx * 3);
  1789. idx++;
  1790. }
  1791. }
  1792. }
  1793. k.alpha[2] = (alpha_bits >> 0) & 0xFF;
  1794. k.alpha[3] = (alpha_bits >> 8) & 0xFF;
  1795. k.alpha[4] = (alpha_bits >> 16) & 0xFF;
  1796. k.alpha[5] = (alpha_bits >> 24) & 0xFF;
  1797. k.alpha[6] = (alpha_bits >> 32) & 0xFF;
  1798. k.alpha[7] = (alpha_bits >> 40) & 0xFF;
  1799. w[block_idx++] = k;
  1800. }
  1801. }
  1802. }
  1803. }
  1804. void VisualServerScene::_gi_probe_bake_thread() {
  1805. while (true) {
  1806. probe_bake_sem->wait();
  1807. if (probe_bake_thread_exit) {
  1808. break;
  1809. }
  1810. Instance *to_bake = NULL;
  1811. probe_bake_mutex->lock();
  1812. if (!probe_bake_list.empty()) {
  1813. to_bake = probe_bake_list.front()->get();
  1814. probe_bake_list.pop_front();
  1815. }
  1816. probe_bake_mutex->unlock();
  1817. if (!to_bake)
  1818. continue;
  1819. _bake_gi_probe(to_bake);
  1820. }
  1821. }
  1822. uint32_t VisualServerScene::_gi_bake_find_cell(const GIProbeDataCell *cells, int x, int y, int z, int p_cell_subdiv) {
  1823. uint32_t cell = 0;
  1824. int ofs_x = 0;
  1825. int ofs_y = 0;
  1826. int ofs_z = 0;
  1827. int size = 1 << (p_cell_subdiv - 1);
  1828. int half = size / 2;
  1829. if (x < 0 || x >= size)
  1830. return -1;
  1831. if (y < 0 || y >= size)
  1832. return -1;
  1833. if (z < 0 || z >= size)
  1834. return -1;
  1835. for (int i = 0; i < p_cell_subdiv - 1; i++) {
  1836. const GIProbeDataCell *bc = &cells[cell];
  1837. int child = 0;
  1838. if (x >= ofs_x + half) {
  1839. child |= 1;
  1840. ofs_x += half;
  1841. }
  1842. if (y >= ofs_y + half) {
  1843. child |= 2;
  1844. ofs_y += half;
  1845. }
  1846. if (z >= ofs_z + half) {
  1847. child |= 4;
  1848. ofs_z += half;
  1849. }
  1850. cell = bc->children[child];
  1851. if (cell == 0xFFFFFFFF)
  1852. return 0xFFFFFFFF;
  1853. half >>= 1;
  1854. }
  1855. return cell;
  1856. }
  1857. static float _get_normal_advance(const Vector3 &p_normal) {
  1858. Vector3 normal = p_normal;
  1859. Vector3 unorm = normal.abs();
  1860. if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
  1861. // x code
  1862. unorm = normal.x > 0.0 ? Vector3(1.0, 0.0, 0.0) : Vector3(-1.0, 0.0, 0.0);
  1863. } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
  1864. // y code
  1865. unorm = normal.y > 0.0 ? Vector3(0.0, 1.0, 0.0) : Vector3(0.0, -1.0, 0.0);
  1866. } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
  1867. // z code
  1868. unorm = normal.z > 0.0 ? Vector3(0.0, 0.0, 1.0) : Vector3(0.0, 0.0, -1.0);
  1869. } else {
  1870. // oh-no we messed up code
  1871. // has to be
  1872. unorm = Vector3(1.0, 0.0, 0.0);
  1873. }
  1874. return 1.0 / normal.dot(unorm);
  1875. }
  1876. void VisualServerScene::_bake_gi_probe_light(const GIProbeDataHeader *header, const GIProbeDataCell *cells, InstanceGIProbeData::LocalData *local_data, const uint32_t *leaves, int leaf_count, const InstanceGIProbeData::LightCache &light_cache, int sign) {
  1877. int light_r = int(light_cache.color.r * light_cache.energy * 1024.0) * sign;
  1878. int light_g = int(light_cache.color.g * light_cache.energy * 1024.0) * sign;
  1879. int light_b = int(light_cache.color.b * light_cache.energy * 1024.0) * sign;
  1880. float limits[3] = { float(header->width), float(header->height), float(header->depth) };
  1881. Plane clip[3];
  1882. int clip_planes = 0;
  1883. switch (light_cache.type) {
  1884. case VS::LIGHT_DIRECTIONAL: {
  1885. float max_len = Vector3(limits[0], limits[1], limits[2]).length() * 1.1;
  1886. Vector3 light_axis = -light_cache.transform.basis.get_axis(2).normalized();
  1887. for (int i = 0; i < 3; i++) {
  1888. if (ABS(light_axis[i]) < CMP_EPSILON)
  1889. continue;
  1890. clip[clip_planes].normal[i] = 1.0;
  1891. if (light_axis[i] < 0) {
  1892. clip[clip_planes].d = limits[i] + 1;
  1893. } else {
  1894. clip[clip_planes].d -= 1.0;
  1895. }
  1896. clip_planes++;
  1897. }
  1898. float distance_adv = _get_normal_advance(light_axis);
  1899. int success_count = 0;
  1900. uint64_t us = OS::get_singleton()->get_ticks_usec();
  1901. for (int i = 0; i < leaf_count; i++) {
  1902. uint32_t idx = leaves[i];
  1903. const GIProbeDataCell *cell = &cells[idx];
  1904. InstanceGIProbeData::LocalData *light = &local_data[idx];
  1905. Vector3 to(light->pos[0] + 0.5, light->pos[1] + 0.5, light->pos[2] + 0.5);
  1906. Vector3 norm(
  1907. (((cells[idx].normal >> 16) & 0xFF) / 255.0) * 2.0 - 1.0,
  1908. (((cells[idx].normal >> 8) & 0xFF) / 255.0) * 2.0 - 1.0,
  1909. (((cells[idx].normal >> 0) & 0xFF) / 255.0) * 2.0 - 1.0);
  1910. float att = norm.dot(-light_axis);
  1911. if (att < 0.001) {
  1912. //not lighting towards this
  1913. continue;
  1914. }
  1915. Vector3 from = to - max_len * light_axis;
  1916. for (int j = 0; j < clip_planes; j++) {
  1917. clip[j].intersects_segment(from, to, &from);
  1918. }
  1919. float distance = (to - from).length();
  1920. distance += distance_adv - Math::fmod(distance, distance_adv); //make it reach the center of the box always
  1921. from = to - light_axis * distance;
  1922. uint32_t result = 0xFFFFFFFF;
  1923. while (distance > -distance_adv) { //use this to avoid precision errors
  1924. result = _gi_bake_find_cell(cells, int(floor(from.x)), int(floor(from.y)), int(floor(from.z)), header->cell_subdiv);
  1925. if (result != 0xFFFFFFFF) {
  1926. break;
  1927. }
  1928. from += light_axis * distance_adv;
  1929. distance -= distance_adv;
  1930. }
  1931. if (result == idx) {
  1932. //cell hit itself! hooray!
  1933. light->energy[0] += int32_t(light_r * att * ((cell->albedo >> 16) & 0xFF) / 255.0);
  1934. light->energy[1] += int32_t(light_g * att * ((cell->albedo >> 8) & 0xFF) / 255.0);
  1935. light->energy[2] += int32_t(light_b * att * ((cell->albedo) & 0xFF) / 255.0);
  1936. success_count++;
  1937. }
  1938. }
  1939. print_line("BAKE TIME: " + rtos((OS::get_singleton()->get_ticks_usec() - us) / 1000000.0));
  1940. print_line("valid cells: " + itos(success_count));
  1941. } break;
  1942. case VS::LIGHT_OMNI:
  1943. case VS::LIGHT_SPOT: {
  1944. uint64_t us = OS::get_singleton()->get_ticks_usec();
  1945. Vector3 light_pos = light_cache.transform.origin;
  1946. Vector3 spot_axis = -light_cache.transform.basis.get_axis(2).normalized();
  1947. float local_radius = light_cache.radius * light_cache.transform.basis.get_axis(2).length();
  1948. for (int i = 0; i < leaf_count; i++) {
  1949. uint32_t idx = leaves[i];
  1950. const GIProbeDataCell *cell = &cells[idx];
  1951. InstanceGIProbeData::LocalData *light = &local_data[idx];
  1952. Vector3 to(light->pos[0] + 0.5, light->pos[1] + 0.5, light->pos[2] + 0.5);
  1953. Vector3 norm(
  1954. (((cells[idx].normal >> 16) & 0xFF) / 255.0) * 2.0 - 1.0,
  1955. (((cells[idx].normal >> 8) & 0xFF) / 255.0) * 2.0 - 1.0,
  1956. (((cells[idx].normal >> 0) & 0xFF) / 255.0) * 2.0 - 1.0);
  1957. Vector3 light_axis = (to - light_pos).normalized();
  1958. float distance_adv = _get_normal_advance(light_axis);
  1959. float att = norm.dot(-light_axis);
  1960. if (att < 0.001) {
  1961. //not lighting towards this
  1962. continue;
  1963. }
  1964. {
  1965. float d = light_pos.distance_to(to);
  1966. if (d + distance_adv > local_radius)
  1967. continue; // too far away
  1968. float dt = CLAMP((d + distance_adv) / local_radius, 0, 1);
  1969. att *= powf(1.0 - dt, light_cache.attenuation);
  1970. }
  1971. if (light_cache.type == VS::LIGHT_SPOT) {
  1972. float angle = Math::rad2deg(acos(light_axis.dot(spot_axis)));
  1973. if (angle > light_cache.spot_angle)
  1974. continue;
  1975. float d = CLAMP(angle / light_cache.spot_angle, 1, 0);
  1976. att *= powf(1.0 - d, light_cache.spot_attenuation);
  1977. }
  1978. clip_planes = 0;
  1979. for (int c = 0; c < 3; c++) {
  1980. if (ABS(light_axis[c]) < CMP_EPSILON)
  1981. continue;
  1982. clip[clip_planes].normal[c] = 1.0;
  1983. if (light_axis[c] < 0) {
  1984. clip[clip_planes].d = limits[c] + 1;
  1985. } else {
  1986. clip[clip_planes].d -= 1.0;
  1987. }
  1988. clip_planes++;
  1989. }
  1990. Vector3 from = light_pos;
  1991. for (int j = 0; j < clip_planes; j++) {
  1992. clip[j].intersects_segment(from, to, &from);
  1993. }
  1994. float distance = (to - from).length();
  1995. distance -= Math::fmod(distance, distance_adv); //make it reach the center of the box always, but this tame make it closer
  1996. from = to - light_axis * distance;
  1997. uint32_t result = 0xFFFFFFFF;
  1998. while (distance > -distance_adv) { //use this to avoid precision errors
  1999. result = _gi_bake_find_cell(cells, int(floor(from.x)), int(floor(from.y)), int(floor(from.z)), header->cell_subdiv);
  2000. if (result != 0xFFFFFFFF) {
  2001. break;
  2002. }
  2003. from += light_axis * distance_adv;
  2004. distance -= distance_adv;
  2005. }
  2006. if (result == idx) {
  2007. //cell hit itself! hooray!
  2008. light->energy[0] += int32_t(light_r * att * ((cell->albedo >> 16) & 0xFF) / 255.0);
  2009. light->energy[1] += int32_t(light_g * att * ((cell->albedo >> 8) & 0xFF) / 255.0);
  2010. light->energy[2] += int32_t(light_b * att * ((cell->albedo) & 0xFF) / 255.0);
  2011. }
  2012. }
  2013. print_line("BAKE TIME: " + rtos((OS::get_singleton()->get_ticks_usec() - us) / 1000000.0));
  2014. } break;
  2015. }
  2016. }
  2017. void VisualServerScene::_bake_gi_downscale_light(int p_idx, int p_level, const GIProbeDataCell *p_cells, const GIProbeDataHeader *p_header, InstanceGIProbeData::LocalData *p_local_data, float p_propagate) {
  2018. //average light to upper level
  2019. float divisor = 0;
  2020. float sum[3] = { 0.0, 0.0, 0.0 };
  2021. for (int i = 0; i < 8; i++) {
  2022. uint32_t child = p_cells[p_idx].children[i];
  2023. if (child == 0xFFFFFFFF)
  2024. continue;
  2025. if (p_level + 1 < (int)p_header->cell_subdiv - 1) {
  2026. _bake_gi_downscale_light(child, p_level + 1, p_cells, p_header, p_local_data, p_propagate);
  2027. }
  2028. sum[0] += p_local_data[child].energy[0];
  2029. sum[1] += p_local_data[child].energy[1];
  2030. sum[2] += p_local_data[child].energy[2];
  2031. divisor += 1.0;
  2032. }
  2033. divisor = Math::lerp((float)8.0, divisor, p_propagate);
  2034. sum[0] /= divisor;
  2035. sum[1] /= divisor;
  2036. sum[2] /= divisor;
  2037. //divide by eight for average
  2038. p_local_data[p_idx].energy[0] = Math::fast_ftoi(sum[0]);
  2039. p_local_data[p_idx].energy[1] = Math::fast_ftoi(sum[1]);
  2040. p_local_data[p_idx].energy[2] = Math::fast_ftoi(sum[2]);
  2041. }
  2042. void VisualServerScene::_bake_gi_probe(Instance *p_gi_probe) {
  2043. InstanceGIProbeData *probe_data = static_cast<InstanceGIProbeData *>(p_gi_probe->base_data);
  2044. PoolVector<int>::Read r = probe_data->dynamic.light_data.read();
  2045. const GIProbeDataHeader *header = (const GIProbeDataHeader *)r.ptr();
  2046. const GIProbeDataCell *cells = (const GIProbeDataCell *)&r[16];
  2047. int leaf_count = probe_data->dynamic.level_cell_lists[header->cell_subdiv - 1].size();
  2048. const uint32_t *leaves = probe_data->dynamic.level_cell_lists[header->cell_subdiv - 1].ptr();
  2049. PoolVector<InstanceGIProbeData::LocalData>::Write ldw = probe_data->dynamic.local_data.write();
  2050. InstanceGIProbeData::LocalData *local_data = ldw.ptr();
  2051. //remove what must be removed
  2052. for (Map<RID, InstanceGIProbeData::LightCache>::Element *E = probe_data->dynamic.light_cache.front(); E; E = E->next()) {
  2053. RID rid = E->key();
  2054. const InstanceGIProbeData::LightCache &lc = E->get();
  2055. if (!probe_data->dynamic.light_cache_changes.has(rid) || !(probe_data->dynamic.light_cache_changes[rid] == lc)) {
  2056. //erase light data
  2057. _bake_gi_probe_light(header, cells, local_data, leaves, leaf_count, lc, -1);
  2058. }
  2059. }
  2060. //add what must be added
  2061. for (Map<RID, InstanceGIProbeData::LightCache>::Element *E = probe_data->dynamic.light_cache_changes.front(); E; E = E->next()) {
  2062. RID rid = E->key();
  2063. const InstanceGIProbeData::LightCache &lc = E->get();
  2064. if (!probe_data->dynamic.light_cache.has(rid) || !(probe_data->dynamic.light_cache[rid] == lc)) {
  2065. //add light data
  2066. _bake_gi_probe_light(header, cells, local_data, leaves, leaf_count, lc, 1);
  2067. }
  2068. }
  2069. SWAP(probe_data->dynamic.light_cache_changes, probe_data->dynamic.light_cache);
  2070. //downscale to lower res levels
  2071. _bake_gi_downscale_light(0, 0, cells, header, local_data, probe_data->dynamic.propagate);
  2072. //plot result to 3D texture!
  2073. if (probe_data->dynamic.compression == RasterizerStorage::GI_PROBE_UNCOMPRESSED) {
  2074. for (int i = 0; i < (int)header->cell_subdiv; i++) {
  2075. int stage = header->cell_subdiv - i - 1;
  2076. if (stage >= probe_data->dynamic.mipmaps_3d.size())
  2077. continue; //no mipmap for this one
  2078. print_line("generating mipmap stage: " + itos(stage));
  2079. int level_cell_count = probe_data->dynamic.level_cell_lists[i].size();
  2080. const uint32_t *level_cells = probe_data->dynamic.level_cell_lists[i].ptr();
  2081. PoolVector<uint8_t>::Write lw = probe_data->dynamic.mipmaps_3d[stage].write();
  2082. uint8_t *mipmapw = lw.ptr();
  2083. uint32_t sizes[3] = { header->width >> stage, header->height >> stage, header->depth >> stage };
  2084. for (int j = 0; j < level_cell_count; j++) {
  2085. uint32_t idx = level_cells[j];
  2086. uint32_t r = (uint32_t(local_data[idx].energy[0]) / probe_data->dynamic.bake_dynamic_range) >> 2;
  2087. uint32_t g = (uint32_t(local_data[idx].energy[1]) / probe_data->dynamic.bake_dynamic_range) >> 2;
  2088. uint32_t b = (uint32_t(local_data[idx].energy[2]) / probe_data->dynamic.bake_dynamic_range) >> 2;
  2089. uint32_t a = (cells[idx].level_alpha >> 8) & 0xFF;
  2090. uint32_t mm_ofs = sizes[0] * sizes[1] * (local_data[idx].pos[2]) + sizes[0] * (local_data[idx].pos[1]) + (local_data[idx].pos[0]);
  2091. mm_ofs *= 4; //for RGBA (4 bytes)
  2092. mipmapw[mm_ofs + 0] = uint8_t(CLAMP(r, 0, 255));
  2093. mipmapw[mm_ofs + 1] = uint8_t(CLAMP(g, 0, 255));
  2094. mipmapw[mm_ofs + 2] = uint8_t(CLAMP(b, 0, 255));
  2095. mipmapw[mm_ofs + 3] = uint8_t(CLAMP(a, 0, 255));
  2096. }
  2097. }
  2098. } else if (probe_data->dynamic.compression == RasterizerStorage::GI_PROBE_S3TC) {
  2099. int mipmap_count = probe_data->dynamic.mipmaps_3d.size();
  2100. for (int mmi = 0; mmi < mipmap_count; mmi++) {
  2101. PoolVector<uint8_t>::Write mmw = probe_data->dynamic.mipmaps_3d[mmi].write();
  2102. int block_count = probe_data->dynamic.mipmaps_s3tc[mmi].size();
  2103. PoolVector<InstanceGIProbeData::CompBlockS3TC>::Read mmr = probe_data->dynamic.mipmaps_s3tc[mmi].read();
  2104. for (int i = 0; i < block_count; i++) {
  2105. const InstanceGIProbeData::CompBlockS3TC &b = mmr[i];
  2106. uint8_t *blockptr = &mmw[b.offset * 16];
  2107. copymem(blockptr, b.alpha, 8); //copy alpha part, which is precomputed
  2108. Vector3 colors[16];
  2109. for (int j = 0; j < b.source_count; j++) {
  2110. colors[j].x = (local_data[b.sources[j]].energy[0] / float(probe_data->dynamic.bake_dynamic_range)) / 1024.0;
  2111. colors[j].y = (local_data[b.sources[j]].energy[1] / float(probe_data->dynamic.bake_dynamic_range)) / 1024.0;
  2112. colors[j].z = (local_data[b.sources[j]].energy[2] / float(probe_data->dynamic.bake_dynamic_range)) / 1024.0;
  2113. }
  2114. //super quick and dirty compression
  2115. //find 2 most further apart
  2116. float distance = 0;
  2117. Vector3 from, to;
  2118. if (b.source_count == 16) {
  2119. //all cells are used so, find minmax between them
  2120. int further_apart[2] = { 0, 0 };
  2121. for (int j = 0; j < b.source_count; j++) {
  2122. for (int k = j + 1; k < b.source_count; k++) {
  2123. float d = colors[j].distance_squared_to(colors[k]);
  2124. if (d > distance) {
  2125. distance = d;
  2126. further_apart[0] = j;
  2127. further_apart[1] = k;
  2128. }
  2129. }
  2130. }
  2131. from = colors[further_apart[0]];
  2132. to = colors[further_apart[1]];
  2133. } else {
  2134. //if a block is missing, the priority is that this block remains black,
  2135. //otherwise the geometry will appear deformed
  2136. //correct shape wins over correct color in this case
  2137. //average all colors first
  2138. Vector3 average;
  2139. for (int j = 0; j < b.source_count; j++) {
  2140. average += colors[j];
  2141. }
  2142. average.normalize();
  2143. //find max distance in normal from average
  2144. for (int j = 0; j < b.source_count; j++) {
  2145. float d = average.dot(colors[j]);
  2146. distance = MAX(d, distance);
  2147. }
  2148. from = Vector3(); //from black
  2149. to = average * distance;
  2150. //find max distance
  2151. }
  2152. int indices[16];
  2153. uint16_t color_0 = 0;
  2154. color_0 = CLAMP(int(from.x * 31), 0, 31) << 11;
  2155. color_0 |= CLAMP(int(from.y * 63), 0, 63) << 5;
  2156. color_0 |= CLAMP(int(from.z * 31), 0, 31);
  2157. uint16_t color_1 = 0;
  2158. color_1 = CLAMP(int(to.x * 31), 0, 31) << 11;
  2159. color_1 |= CLAMP(int(to.y * 63), 0, 63) << 5;
  2160. color_1 |= CLAMP(int(to.z * 31), 0, 31);
  2161. if (color_1 > color_0) {
  2162. SWAP(color_1, color_0);
  2163. SWAP(from, to);
  2164. }
  2165. if (distance > 0) {
  2166. Vector3 dir = (to - from).normalized();
  2167. for (int j = 0; j < b.source_count; j++) {
  2168. float d = (colors[j] - from).dot(dir) / distance;
  2169. indices[j] = int(d * 3 + 0.5);
  2170. static const int index_swap[4] = { 0, 3, 1, 2 };
  2171. indices[j] = index_swap[CLAMP(indices[j], 0, 3)];
  2172. }
  2173. } else {
  2174. for (int j = 0; j < b.source_count; j++) {
  2175. indices[j] = 0;
  2176. }
  2177. }
  2178. //by default, 1 is black, otherwise it will be overridden by source
  2179. uint32_t index_block[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  2180. for (int j = 0; j < b.source_count; j++) {
  2181. int x = local_data[b.sources[j]].pos[0] % 4;
  2182. int y = local_data[b.sources[j]].pos[1] % 4;
  2183. index_block[y * 4 + x] = indices[j];
  2184. }
  2185. uint32_t encode = 0;
  2186. for (int j = 0; j < 16; j++) {
  2187. encode |= index_block[j] << (j * 2);
  2188. }
  2189. blockptr[8] = color_0 & 0xFF;
  2190. blockptr[9] = (color_0 >> 8) & 0xFF;
  2191. blockptr[10] = color_1 & 0xFF;
  2192. blockptr[11] = (color_1 >> 8) & 0xFF;
  2193. blockptr[12] = encode & 0xFF;
  2194. blockptr[13] = (encode >> 8) & 0xFF;
  2195. blockptr[14] = (encode >> 16) & 0xFF;
  2196. blockptr[15] = (encode >> 24) & 0xFF;
  2197. }
  2198. }
  2199. }
  2200. //send back to main thread to update un little chunks
  2201. probe_data->dynamic.updating_stage = GI_UPDATE_STAGE_UPLOADING;
  2202. }
  2203. bool VisualServerScene::_check_gi_probe(Instance *p_gi_probe) {
  2204. InstanceGIProbeData *probe_data = static_cast<InstanceGIProbeData *>(p_gi_probe->base_data);
  2205. probe_data->dynamic.light_cache_changes.clear();
  2206. bool all_equal = true;
  2207. for (List<Instance *>::Element *E = p_gi_probe->scenario->directional_lights.front(); E; E = E->next()) {
  2208. InstanceGIProbeData::LightCache lc;
  2209. lc.type = VSG::storage->light_get_type(E->get()->base);
  2210. lc.color = VSG::storage->light_get_color(E->get()->base);
  2211. lc.energy = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_ENERGY);
  2212. lc.radius = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_RANGE);
  2213. lc.attenuation = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_ATTENUATION);
  2214. lc.spot_angle = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_SPOT_ANGLE);
  2215. lc.spot_attenuation = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_SPOT_ATTENUATION);
  2216. lc.transform = probe_data->dynamic.light_to_cell_xform * E->get()->transform;
  2217. if (!probe_data->dynamic.light_cache.has(E->get()->self) || !(probe_data->dynamic.light_cache[E->get()->self] == lc)) {
  2218. all_equal = false;
  2219. }
  2220. probe_data->dynamic.light_cache_changes[E->get()->self] = lc;
  2221. }
  2222. for (Set<Instance *>::Element *E = probe_data->lights.front(); E; E = E->next()) {
  2223. InstanceGIProbeData::LightCache lc;
  2224. lc.type = VSG::storage->light_get_type(E->get()->base);
  2225. lc.color = VSG::storage->light_get_color(E->get()->base);
  2226. lc.energy = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_ENERGY);
  2227. lc.radius = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_RANGE);
  2228. lc.attenuation = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_ATTENUATION);
  2229. lc.spot_angle = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_SPOT_ANGLE);
  2230. lc.spot_attenuation = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_SPOT_ATTENUATION);
  2231. lc.transform = probe_data->dynamic.light_to_cell_xform * E->get()->transform;
  2232. if (!probe_data->dynamic.light_cache.has(E->get()->self) || !(probe_data->dynamic.light_cache[E->get()->self] == lc)) {
  2233. all_equal = false;
  2234. }
  2235. probe_data->dynamic.light_cache_changes[E->get()->self] = lc;
  2236. }
  2237. //lighting changed from after to before, must do some updating
  2238. return !all_equal || probe_data->dynamic.light_cache_changes.size() != probe_data->dynamic.light_cache.size();
  2239. }
  2240. void VisualServerScene::render_probes() {
  2241. /* REFLECTION PROBES */
  2242. SelfList<InstanceReflectionProbeData> *ref_probe = reflection_probe_render_list.first();
  2243. bool busy = false;
  2244. while (ref_probe) {
  2245. SelfList<InstanceReflectionProbeData> *next = ref_probe->next();
  2246. RID base = ref_probe->self()->owner->base;
  2247. switch (VSG::storage->reflection_probe_get_update_mode(base)) {
  2248. case VS::REFLECTION_PROBE_UPDATE_ONCE: {
  2249. if (busy) //already rendering something
  2250. break;
  2251. bool done = _render_reflection_probe_step(ref_probe->self()->owner, ref_probe->self()->render_step);
  2252. if (done) {
  2253. reflection_probe_render_list.remove(ref_probe);
  2254. } else {
  2255. ref_probe->self()->render_step++;
  2256. }
  2257. busy = true; //do not render another one of this kind
  2258. } break;
  2259. case VS::REFLECTION_PROBE_UPDATE_ALWAYS: {
  2260. int step = 0;
  2261. bool done = false;
  2262. while (!done) {
  2263. done = _render_reflection_probe_step(ref_probe->self()->owner, step);
  2264. step++;
  2265. }
  2266. reflection_probe_render_list.remove(ref_probe);
  2267. } break;
  2268. }
  2269. ref_probe = next;
  2270. }
  2271. /* GI PROBES */
  2272. SelfList<InstanceGIProbeData> *gi_probe = gi_probe_update_list.first();
  2273. while (gi_probe) {
  2274. SelfList<InstanceGIProbeData> *next = gi_probe->next();
  2275. InstanceGIProbeData *probe = gi_probe->self();
  2276. Instance *instance_probe = probe->owner;
  2277. //check if probe must be setup, but don't do if on the lighting thread
  2278. bool force_lighting = false;
  2279. if (probe->invalid || (probe->dynamic.updating_stage == GI_UPDATE_STAGE_CHECK && probe->base_version != VSG::storage->gi_probe_get_version(instance_probe->base))) {
  2280. _setup_gi_probe(instance_probe);
  2281. force_lighting = true;
  2282. }
  2283. float propagate = VSG::storage->gi_probe_get_propagation(instance_probe->base);
  2284. if (probe->dynamic.propagate != propagate) {
  2285. probe->dynamic.propagate = propagate;
  2286. force_lighting = true;
  2287. }
  2288. if (probe->invalid == false && probe->dynamic.enabled) {
  2289. switch (probe->dynamic.updating_stage) {
  2290. case GI_UPDATE_STAGE_CHECK: {
  2291. if (_check_gi_probe(instance_probe) || force_lighting) {
  2292. //send to lighting thread
  2293. probe->dynamic.updating_stage = GI_UPDATE_STAGE_LIGHTING;
  2294. #ifndef NO_THREADS
  2295. probe_bake_mutex->lock();
  2296. probe_bake_list.push_back(instance_probe);
  2297. probe_bake_mutex->unlock();
  2298. probe_bake_sem->post();
  2299. #else
  2300. _bake_gi_probe(instance_probe);
  2301. #endif
  2302. }
  2303. } break;
  2304. case GI_UPDATE_STAGE_LIGHTING: {
  2305. //do none, wait til done!
  2306. } break;
  2307. case GI_UPDATE_STAGE_UPLOADING: {
  2308. uint64_t us = OS::get_singleton()->get_ticks_usec();
  2309. for (int i = 0; i < (int)probe->dynamic.mipmaps_3d.size(); i++) {
  2310. int mmsize = probe->dynamic.mipmaps_3d[i].size();
  2311. PoolVector<uint8_t>::Read r = probe->dynamic.mipmaps_3d[i].read();
  2312. VSG::storage->gi_probe_dynamic_data_update(probe->dynamic.probe_data, 0, probe->dynamic.grid_size[2] >> i, i, r.ptr());
  2313. }
  2314. probe->dynamic.updating_stage = GI_UPDATE_STAGE_CHECK;
  2315. //print_line("UPLOAD TIME: "+rtos((OS::get_singleton()->get_ticks_usec()-us)/1000000.0));
  2316. } break;
  2317. }
  2318. }
  2319. //_update_gi_probe(gi_probe->self()->owner);
  2320. gi_probe = next;
  2321. }
  2322. }
  2323. void VisualServerScene::_update_dirty_instance(Instance *p_instance) {
  2324. if (p_instance->update_aabb)
  2325. _update_instance_aabb(p_instance);
  2326. if (p_instance->update_materials) {
  2327. if (p_instance->base_type == VS::INSTANCE_MESH) {
  2328. //remove materials no longer used and un-own them
  2329. int new_mat_count = VSG::storage->mesh_get_surface_count(p_instance->base);
  2330. for (int i = p_instance->materials.size() - 1; i >= new_mat_count; i--) {
  2331. if (p_instance->materials[i].is_valid()) {
  2332. VSG::storage->material_remove_instance_owner(p_instance->materials[i], p_instance);
  2333. }
  2334. }
  2335. p_instance->materials.resize(new_mat_count);
  2336. int new_blend_shape_count = VSG::storage->mesh_get_blend_shape_count(p_instance->base);
  2337. if (new_blend_shape_count != p_instance->blend_values.size()) {
  2338. p_instance->blend_values.resize(new_blend_shape_count);
  2339. for (int i = 0; i < new_blend_shape_count; i++) {
  2340. p_instance->blend_values[i] = 0;
  2341. }
  2342. }
  2343. }
  2344. if ((1 << p_instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  2345. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  2346. bool can_cast_shadows = true;
  2347. if (p_instance->cast_shadows == VS::SHADOW_CASTING_SETTING_OFF) {
  2348. can_cast_shadows = false;
  2349. } else if (p_instance->material_override.is_valid()) {
  2350. can_cast_shadows = VSG::storage->material_casts_shadows(p_instance->material_override);
  2351. } else {
  2352. if (p_instance->base_type == VS::INSTANCE_MESH) {
  2353. RID mesh = p_instance->base;
  2354. if (mesh.is_valid()) {
  2355. bool cast_shadows = false;
  2356. for (int i = 0; i < p_instance->materials.size(); i++) {
  2357. RID mat = p_instance->materials[i].is_valid() ? p_instance->materials[i] : VSG::storage->mesh_surface_get_material(mesh, i);
  2358. if (!mat.is_valid()) {
  2359. cast_shadows = true;
  2360. break;
  2361. }
  2362. if (VSG::storage->material_casts_shadows(mat)) {
  2363. cast_shadows = true;
  2364. break;
  2365. }
  2366. }
  2367. if (!cast_shadows) {
  2368. can_cast_shadows = false;
  2369. }
  2370. }
  2371. } else if (p_instance->base_type == VS::INSTANCE_MULTIMESH) {
  2372. RID mesh = VSG::storage->multimesh_get_mesh(p_instance->base);
  2373. if (mesh.is_valid()) {
  2374. bool cast_shadows = false;
  2375. int sc = VSG::storage->mesh_get_surface_count(mesh);
  2376. for (int i = 0; i < sc; i++) {
  2377. RID mat = VSG::storage->mesh_surface_get_material(mesh, i);
  2378. if (!mat.is_valid()) {
  2379. cast_shadows = true;
  2380. break;
  2381. }
  2382. if (VSG::storage->material_casts_shadows(mat)) {
  2383. cast_shadows = true;
  2384. break;
  2385. }
  2386. }
  2387. if (!cast_shadows) {
  2388. can_cast_shadows = false;
  2389. }
  2390. }
  2391. } else if (p_instance->base_type == VS::INSTANCE_IMMEDIATE) {
  2392. RID mat = VSG::storage->immediate_get_material(p_instance->base);
  2393. if (!mat.is_valid() || VSG::storage->material_casts_shadows(mat)) {
  2394. can_cast_shadows = true;
  2395. } else {
  2396. can_cast_shadows = false;
  2397. }
  2398. } else if (p_instance->base_type == VS::INSTANCE_PARTICLES) {
  2399. bool cast_shadows = false;
  2400. int dp = VSG::storage->particles_get_draw_passes(p_instance->base);
  2401. for (int i = 0; i < dp; i++) {
  2402. RID mesh = VSG::storage->particles_get_draw_pass_mesh(p_instance->base, i);
  2403. if (!mesh.is_valid())
  2404. continue;
  2405. int sc = VSG::storage->mesh_get_surface_count(mesh);
  2406. for (int j = 0; j < sc; j++) {
  2407. RID mat = VSG::storage->mesh_surface_get_material(mesh, j);
  2408. if (!mat.is_valid()) {
  2409. cast_shadows = true;
  2410. break;
  2411. }
  2412. if (VSG::storage->material_casts_shadows(mat)) {
  2413. cast_shadows = true;
  2414. break;
  2415. }
  2416. }
  2417. }
  2418. if (!cast_shadows) {
  2419. can_cast_shadows = false;
  2420. }
  2421. }
  2422. }
  2423. if (can_cast_shadows != geom->can_cast_shadows) {
  2424. //ability to cast shadows change, let lights now
  2425. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  2426. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  2427. light->shadow_dirty = true;
  2428. }
  2429. geom->can_cast_shadows = can_cast_shadows;
  2430. }
  2431. }
  2432. }
  2433. _update_instance(p_instance);
  2434. p_instance->update_aabb = false;
  2435. p_instance->update_materials = false;
  2436. _instance_update_list.remove(&p_instance->update_item);
  2437. }
  2438. void VisualServerScene::update_dirty_instances() {
  2439. VSG::storage->update_dirty_resources();
  2440. while (_instance_update_list.first()) {
  2441. _update_dirty_instance(_instance_update_list.first()->self());
  2442. }
  2443. }
  2444. bool VisualServerScene::free(RID p_rid) {
  2445. if (camera_owner.owns(p_rid)) {
  2446. Camera *camera = camera_owner.get(p_rid);
  2447. camera_owner.free(p_rid);
  2448. memdelete(camera);
  2449. } else if (scenario_owner.owns(p_rid)) {
  2450. Scenario *scenario = scenario_owner.get(p_rid);
  2451. while (scenario->instances.first()) {
  2452. instance_set_scenario(scenario->instances.first()->self()->self, RID());
  2453. }
  2454. VSG::scene_render->free(scenario->reflection_probe_shadow_atlas);
  2455. VSG::scene_render->free(scenario->reflection_atlas);
  2456. scenario_owner.free(p_rid);
  2457. memdelete(scenario);
  2458. } else if (instance_owner.owns(p_rid)) {
  2459. // delete the instance
  2460. update_dirty_instances();
  2461. Instance *instance = instance_owner.get(p_rid);
  2462. instance_set_room(p_rid, RID());
  2463. instance_set_scenario(p_rid, RID());
  2464. instance_set_base(p_rid, RID());
  2465. instance_geometry_set_material_override(p_rid, RID());
  2466. instance_attach_skeleton(p_rid, RID());
  2467. update_dirty_instances(); //in case something changed this
  2468. instance_owner.free(p_rid);
  2469. memdelete(instance);
  2470. } else {
  2471. return false;
  2472. }
  2473. return true;
  2474. }
  2475. VisualServerScene *VisualServerScene::singleton = NULL;
  2476. VisualServerScene::VisualServerScene() {
  2477. #ifndef NO_THREADS
  2478. probe_bake_sem = Semaphore::create();
  2479. probe_bake_mutex = Mutex::create();
  2480. probe_bake_thread = Thread::create(_gi_probe_bake_threads, this);
  2481. probe_bake_thread_exit = false;
  2482. #endif
  2483. render_pass = 1;
  2484. singleton = this;
  2485. }
  2486. VisualServerScene::~VisualServerScene() {
  2487. #ifndef NO_THREADS
  2488. probe_bake_thread_exit = true;
  2489. Thread::wait_to_finish(probe_bake_thread);
  2490. memdelete(probe_bake_thread);
  2491. memdelete(probe_bake_sem);
  2492. memdelete(probe_bake_mutex);
  2493. #endif
  2494. }