visual_server_scene.cpp 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489
  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. }
  710. }
  711. void VisualServerScene::instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting) {
  712. }
  713. void VisualServerScene::instance_geometry_set_material_override(RID p_instance, RID p_material) {
  714. Instance *instance = instance_owner.get(p_instance);
  715. ERR_FAIL_COND(!instance);
  716. if (instance->material_override.is_valid()) {
  717. VSG::storage->material_remove_instance_owner(instance->material_override, instance);
  718. }
  719. instance->material_override = p_material;
  720. instance->base_material_changed();
  721. if (instance->material_override.is_valid()) {
  722. VSG::storage->material_add_instance_owner(instance->material_override, instance);
  723. }
  724. }
  725. void VisualServerScene::instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin) {
  726. }
  727. void VisualServerScene::instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance) {
  728. }
  729. void VisualServerScene::_update_instance(Instance *p_instance) {
  730. p_instance->version++;
  731. if (p_instance->base_type == VS::INSTANCE_LIGHT) {
  732. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  733. VSG::scene_render->light_instance_set_transform(light->instance, p_instance->transform);
  734. light->shadow_dirty = true;
  735. }
  736. if (p_instance->base_type == VS::INSTANCE_REFLECTION_PROBE) {
  737. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  738. VSG::scene_render->reflection_probe_instance_set_transform(reflection_probe->instance, p_instance->transform);
  739. reflection_probe->reflection_dirty = true;
  740. }
  741. if (p_instance->base_type == VS::INSTANCE_PARTICLES) {
  742. VSG::storage->particles_set_emission_transform(p_instance->base, p_instance->transform);
  743. }
  744. if (p_instance->aabb.has_no_surface())
  745. return;
  746. #if 0
  747. if (p_instance->base_type == VS::INSTANCE_PARTICLES) {
  748. rasterizer->particles_instance_set_transform( p_instance->particles_info->instance, p_instance->data.transform );
  749. }
  750. #endif
  751. if ((1 << p_instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  752. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  753. //make sure lights are updated if it casts shadow
  754. if (geom->can_cast_shadows) {
  755. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  756. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  757. light->shadow_dirty = true;
  758. }
  759. }
  760. }
  761. #if 0
  762. else if (p_instance->base_type == INSTANCE_ROOM) {
  763. p_instance->room_info->affine_inverse=p_instance->data.transform.affine_inverse();
  764. } else if (p_instance->base_type == INSTANCE_GI_PROBE) {
  765. Transform scale;
  766. scale.basis.scale(p_instance->gi_probe_info->gi_probe->octree_aabb.size);
  767. scale.origin=p_instance->gi_probe_info->gi_probe->octree_aabb.pos;
  768. //print_line("scale: "+scale);
  769. p_instance->gi_probe_info->affine_inverse=(p_instance->data.transform*scale).affine_inverse();
  770. }
  771. #endif
  772. p_instance->mirror = p_instance->transform.basis.determinant() < 0.0;
  773. Rect3 new_aabb;
  774. #if 0
  775. if (p_instance->base_type==INSTANCE_PORTAL) {
  776. //portals need to be transformed in a special way, so they don't become too wide if they have scale..
  777. Transform portal_xform = p_instance->data.transform;
  778. portal_xform.basis.set_axis(2,portal_xform.basis.get_axis(2).normalized());
  779. p_instance->portal_info->plane_cache=Plane( p_instance->data.transform.origin, portal_xform.basis.get_axis(2));
  780. int point_count=p_instance->portal_info->portal->shape.size();
  781. p_instance->portal_info->transformed_point_cache.resize(point_count);
  782. AABB portal_aabb;
  783. for(int i=0;i<point_count;i++) {
  784. Point2 src = p_instance->portal_info->portal->shape[i];
  785. Vector3 point = portal_xform.xform(Vector3(src.x,src.y,0));
  786. p_instance->portal_info->transformed_point_cache[i]=point;
  787. if (i==0)
  788. portal_aabb.pos=point;
  789. else
  790. portal_aabb.expand_to(point);
  791. }
  792. portal_aabb.grow_by(p_instance->portal_info->portal->connect_range);
  793. new_aabb = portal_aabb;
  794. } else {
  795. #endif
  796. new_aabb = p_instance->transform.xform(p_instance->aabb);
  797. #if 0
  798. }
  799. #endif
  800. p_instance->transformed_aabb = new_aabb;
  801. if (!p_instance->scenario) {
  802. return;
  803. }
  804. if (p_instance->octree_id == 0) {
  805. uint32_t base_type = 1 << p_instance->base_type;
  806. uint32_t pairable_mask = 0;
  807. bool pairable = false;
  808. if (p_instance->base_type == VS::INSTANCE_LIGHT || p_instance->base_type == VS::INSTANCE_REFLECTION_PROBE) {
  809. pairable_mask = p_instance->visible ? VS::INSTANCE_GEOMETRY_MASK : 0;
  810. pairable = true;
  811. }
  812. if (p_instance->base_type == VS::INSTANCE_GI_PROBE) {
  813. //lights and geometries
  814. pairable_mask = p_instance->visible ? VS::INSTANCE_GEOMETRY_MASK | (1 << VS::INSTANCE_LIGHT) : 0;
  815. pairable = true;
  816. }
  817. #if 0
  818. if (p_instance->base_type == VS::INSTANCE_PORTAL) {
  819. pairable_mask=(1<<INSTANCE_PORTAL);
  820. pairable=true;
  821. }
  822. if (p_instance->base_type == VS::INSTANCE_GI_PROBE_SAMPLER) {
  823. pairable_mask=(1<<INSTANCE_GI_PROBE);
  824. pairable=true;
  825. }
  826. if (!p_instance->room && (1<<p_instance->base_type)&VS::INSTANCE_GEOMETRY_MASK) {
  827. base_type|=VS::INSTANCE_ROOMLESS_MASK;
  828. }
  829. if (p_instance->base_type == VS::INSTANCE_ROOM) {
  830. pairable_mask=INSTANCE_ROOMLESS_MASK;
  831. pairable=true;
  832. }
  833. #endif
  834. // not inside octree
  835. p_instance->octree_id = p_instance->scenario->octree.create(p_instance, new_aabb, 0, pairable, base_type, pairable_mask);
  836. } else {
  837. /*
  838. if (new_aabb==p_instance->data.transformed_aabb)
  839. return;
  840. */
  841. p_instance->scenario->octree.move(p_instance->octree_id, new_aabb);
  842. }
  843. #if 0
  844. if (p_instance->base_type==INSTANCE_PORTAL) {
  845. _portal_attempt_connect(p_instance);
  846. }
  847. if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  848. _instance_validate_autorooms(p_instance);
  849. }
  850. if (p_instance->base_type == INSTANCE_ROOM) {
  851. for(Set<Instance*>::Element *E=p_instance->room_info->owned_autoroom_geometry.front();E;E=E->next())
  852. _instance_validate_autorooms(E->get());
  853. }
  854. #endif
  855. }
  856. void VisualServerScene::_update_instance_aabb(Instance *p_instance) {
  857. Rect3 new_aabb;
  858. ERR_FAIL_COND(p_instance->base_type != VS::INSTANCE_NONE && !p_instance->base.is_valid());
  859. switch (p_instance->base_type) {
  860. case VisualServer::INSTANCE_NONE: {
  861. // do nothing
  862. } break;
  863. case VisualServer::INSTANCE_MESH: {
  864. new_aabb = VSG::storage->mesh_get_aabb(p_instance->base, p_instance->skeleton);
  865. } break;
  866. case VisualServer::INSTANCE_MULTIMESH: {
  867. new_aabb = VSG::storage->multimesh_get_aabb(p_instance->base);
  868. } break;
  869. case VisualServer::INSTANCE_IMMEDIATE: {
  870. new_aabb = VSG::storage->immediate_get_aabb(p_instance->base);
  871. } break;
  872. case VisualServer::INSTANCE_PARTICLES: {
  873. new_aabb = VSG::storage->particles_get_aabb(p_instance->base);
  874. } break;
  875. #if 0
  876. case VisualServer::INSTANCE_PARTICLES: {
  877. new_aabb = rasterizer->particles_get_aabb(p_instance->base);
  878. } break;
  879. #endif
  880. case VisualServer::INSTANCE_LIGHT: {
  881. new_aabb = VSG::storage->light_get_aabb(p_instance->base);
  882. } break;
  883. case VisualServer::INSTANCE_REFLECTION_PROBE: {
  884. new_aabb = VSG::storage->reflection_probe_get_aabb(p_instance->base);
  885. } break;
  886. case VisualServer::INSTANCE_GI_PROBE: {
  887. new_aabb = VSG::storage->gi_probe_get_bounds(p_instance->base);
  888. } break;
  889. #if 0
  890. case VisualServer::INSTANCE_ROOM: {
  891. Room *room = room_owner.get( p_instance->base );
  892. ERR_FAIL_COND(!room);
  893. new_aabb=room->bounds.get_aabb();
  894. } break;
  895. case VisualServer::INSTANCE_PORTAL: {
  896. Portal *portal = portal_owner.get( p_instance->base );
  897. ERR_FAIL_COND(!portal);
  898. for (int i=0;i<portal->shape.size();i++) {
  899. Vector3 point( portal->shape[i].x, portal->shape[i].y, 0 );
  900. if (i==0) {
  901. new_aabb.pos=point;
  902. new_aabb.size.z=0.01; // make it not flat for octree
  903. } else {
  904. new_aabb.expand_to(point);
  905. }
  906. }
  907. } break;
  908. case VisualServer::INSTANCE_GI_PROBE: {
  909. BakedLight *gi_probe = gi_probe_owner.get( p_instance->base );
  910. ERR_FAIL_COND(!gi_probe);
  911. new_aabb=gi_probe->octree_aabb;
  912. } break;
  913. case VisualServer::INSTANCE_GI_PROBE_SAMPLER: {
  914. BakedLightSampler *gi_probe_sampler = gi_probe_sampler_owner.get( p_instance->base );
  915. ERR_FAIL_COND(!gi_probe_sampler);
  916. float radius = gi_probe_sampler->params[VS::BAKED_LIGHT_SAMPLER_RADIUS];
  917. new_aabb=AABB(Vector3(-radius,-radius,-radius),Vector3(radius*2,radius*2,radius*2));
  918. } break;
  919. #endif
  920. default: {}
  921. }
  922. if (p_instance->extra_margin)
  923. new_aabb.grow_by(p_instance->extra_margin);
  924. p_instance->aabb = new_aabb;
  925. }
  926. 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) {
  927. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  928. switch (VSG::storage->light_get_type(p_instance->base)) {
  929. case VS::LIGHT_DIRECTIONAL: {
  930. float max_distance = p_cam_projection.get_z_far();
  931. float shadow_max = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE);
  932. if (shadow_max > 0) {
  933. max_distance = MIN(shadow_max, max_distance);
  934. }
  935. max_distance = MAX(max_distance, p_cam_projection.get_z_near() + 0.001);
  936. float range = max_distance - p_cam_projection.get_z_near();
  937. int splits = 0;
  938. switch (VSG::storage->light_directional_get_shadow_mode(p_instance->base)) {
  939. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: splits = 1; break;
  940. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: splits = 2; break;
  941. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: splits = 4; break;
  942. }
  943. float distances[5];
  944. distances[0] = p_cam_projection.get_z_near();
  945. for (int i = 0; i < splits; i++) {
  946. 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;
  947. };
  948. distances[splits] = max_distance;
  949. float texture_size = VSG::scene_render->get_directional_light_shadow_size(light->instance);
  950. bool overlap = VSG::storage->light_directional_get_blend_splits(p_instance->base);
  951. float first_radius = 0.0;
  952. for (int i = 0; i < splits; i++) {
  953. // setup a camera matrix for that range!
  954. CameraMatrix camera_matrix;
  955. float aspect = p_cam_projection.get_aspect();
  956. if (p_cam_orthogonal) {
  957. float w, h;
  958. p_cam_projection.get_viewport_size(w, h);
  959. camera_matrix.set_orthogonal(w, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  960. } else {
  961. float fov = p_cam_projection.get_fov();
  962. camera_matrix.set_perspective(fov, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  963. }
  964. //obtain the frustum endpoints
  965. Vector3 endpoints[8]; // frustum plane endpoints
  966. bool res = camera_matrix.get_endpoints(p_cam_transform, endpoints);
  967. ERR_CONTINUE(!res);
  968. // obtain the light frustm ranges (given endpoints)
  969. Transform transform = p_instance->transform.orthonormalized(); //discard scale and stabilize light
  970. Vector3 x_vec = transform.basis.get_axis(Vector3::AXIS_X).normalized();
  971. Vector3 y_vec = transform.basis.get_axis(Vector3::AXIS_Y).normalized();
  972. Vector3 z_vec = transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  973. //z_vec points agsint the camera, like in default opengl
  974. float x_min, x_max;
  975. float y_min, y_max;
  976. float z_min, z_max;
  977. float x_min_cam, x_max_cam;
  978. float y_min_cam, y_max_cam;
  979. float z_min_cam, z_max_cam;
  980. float bias_scale = 1.0;
  981. //used for culling
  982. for (int j = 0; j < 8; j++) {
  983. float d_x = x_vec.dot(endpoints[j]);
  984. float d_y = y_vec.dot(endpoints[j]);
  985. float d_z = z_vec.dot(endpoints[j]);
  986. if (j == 0 || d_x < x_min)
  987. x_min = d_x;
  988. if (j == 0 || d_x > x_max)
  989. x_max = d_x;
  990. if (j == 0 || d_y < y_min)
  991. y_min = d_y;
  992. if (j == 0 || d_y > y_max)
  993. y_max = d_y;
  994. if (j == 0 || d_z < z_min)
  995. z_min = d_z;
  996. if (j == 0 || d_z > z_max)
  997. z_max = d_z;
  998. }
  999. {
  1000. //camera viewport stuff
  1001. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  1002. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  1003. Vector3 center;
  1004. for (int j = 0; j < 8; j++) {
  1005. center += endpoints[j];
  1006. }
  1007. center /= 8.0;
  1008. //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;
  1009. float radius = 0;
  1010. for (int j = 0; j < 8; j++) {
  1011. float d = center.distance_to(endpoints[j]);
  1012. if (d > radius)
  1013. radius = d;
  1014. }
  1015. radius *= texture_size / (texture_size - 2.0); //add a texel by each side, so stepified texture will always fit
  1016. if (i == 0) {
  1017. first_radius = radius;
  1018. } else {
  1019. bias_scale = radius / first_radius;
  1020. }
  1021. x_max_cam = x_vec.dot(center) + radius;
  1022. x_min_cam = x_vec.dot(center) - radius;
  1023. y_max_cam = y_vec.dot(center) + radius;
  1024. y_min_cam = y_vec.dot(center) - radius;
  1025. z_max_cam = z_vec.dot(center) + radius;
  1026. z_min_cam = z_vec.dot(center) - radius;
  1027. float unit = radius * 2.0 / texture_size;
  1028. x_max_cam = Math::stepify(x_max_cam, unit);
  1029. x_min_cam = Math::stepify(x_min_cam, unit);
  1030. y_max_cam = Math::stepify(y_max_cam, unit);
  1031. y_min_cam = Math::stepify(y_min_cam, unit);
  1032. }
  1033. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  1034. Vector<Plane> light_frustum_planes;
  1035. light_frustum_planes.resize(6);
  1036. //right/left
  1037. light_frustum_planes[0] = Plane(x_vec, x_max);
  1038. light_frustum_planes[1] = Plane(-x_vec, -x_min);
  1039. //top/bottom
  1040. light_frustum_planes[2] = Plane(y_vec, y_max);
  1041. light_frustum_planes[3] = Plane(-y_vec, -y_min);
  1042. //near/far
  1043. light_frustum_planes[4] = Plane(z_vec, z_max + 1e6);
  1044. light_frustum_planes[5] = Plane(-z_vec, -z_min); // z_min is ok, since casters further than far-light plane are not needed
  1045. int cull_count = p_scenario->octree.cull_convex(light_frustum_planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, VS::INSTANCE_GEOMETRY_MASK);
  1046. // a pre pass will need to be needed to determine the actual z-near to be used
  1047. Plane near_plane(p_instance->transform.origin, -p_instance->transform.basis.get_axis(2));
  1048. for (int j = 0; j < cull_count; j++) {
  1049. float min, max;
  1050. Instance *instance = instance_shadow_cull_result[j];
  1051. if (!instance->visible || !((1 << instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1052. cull_count--;
  1053. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1054. j--;
  1055. continue;
  1056. }
  1057. instance->transformed_aabb.project_range_in_plane(Plane(z_vec, 0), min, max);
  1058. instance->depth = near_plane.distance_to(instance->transform.origin);
  1059. instance->depth_layer = 0;
  1060. if (max > z_max)
  1061. z_max = max;
  1062. }
  1063. {
  1064. CameraMatrix ortho_camera;
  1065. real_t half_x = (x_max_cam - x_min_cam) * 0.5;
  1066. real_t half_y = (y_max_cam - y_min_cam) * 0.5;
  1067. ortho_camera.set_orthogonal(-half_x, half_x, -half_y, half_y, 0, (z_max - z_min_cam));
  1068. Transform ortho_transform;
  1069. ortho_transform.basis = transform.basis;
  1070. ortho_transform.origin = x_vec * (x_min_cam + half_x) + y_vec * (y_min_cam + half_y) + z_vec * z_max;
  1071. VSG::scene_render->light_instance_set_shadow_transform(light->instance, ortho_camera, ortho_transform, 0, distances[i + 1], i, bias_scale);
  1072. }
  1073. VSG::scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1074. }
  1075. } break;
  1076. case VS::LIGHT_OMNI: {
  1077. VS::LightOmniShadowMode shadow_mode = VSG::storage->light_omni_get_shadow_mode(p_instance->base);
  1078. switch (shadow_mode) {
  1079. case VS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID: {
  1080. for (int i = 0; i < 2; i++) {
  1081. //using this one ensures that raster deferred will have it
  1082. float radius = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_RANGE);
  1083. float z = i == 0 ? -1 : 1;
  1084. Vector<Plane> planes;
  1085. planes.resize(5);
  1086. planes[0] = p_instance->transform.xform(Plane(Vector3(0, 0, z), radius));
  1087. planes[1] = p_instance->transform.xform(Plane(Vector3(1, 0, z).normalized(), radius));
  1088. planes[2] = p_instance->transform.xform(Plane(Vector3(-1, 0, z).normalized(), radius));
  1089. planes[3] = p_instance->transform.xform(Plane(Vector3(0, 1, z).normalized(), radius));
  1090. planes[4] = p_instance->transform.xform(Plane(Vector3(0, -1, z).normalized(), radius));
  1091. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, VS::INSTANCE_GEOMETRY_MASK);
  1092. Plane near_plane(p_instance->transform.origin, p_instance->transform.basis.get_axis(2) * z);
  1093. for (int j = 0; j < cull_count; j++) {
  1094. Instance *instance = instance_shadow_cull_result[j];
  1095. if (!instance->visible || !((1 << instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1096. cull_count--;
  1097. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1098. j--;
  1099. } else {
  1100. instance->depth = near_plane.distance_to(instance->transform.origin);
  1101. instance->depth_layer = 0;
  1102. }
  1103. }
  1104. VSG::scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), p_instance->transform, radius, 0, i);
  1105. VSG::scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1106. }
  1107. } break;
  1108. case VS::LIGHT_OMNI_SHADOW_CUBE: {
  1109. float radius = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_RANGE);
  1110. CameraMatrix cm;
  1111. cm.set_perspective(90, 1, 0.01, radius);
  1112. for (int i = 0; i < 6; i++) {
  1113. //using this one ensures that raster deferred will have it
  1114. static const Vector3 view_normals[6] = {
  1115. Vector3(-1, 0, 0),
  1116. Vector3(+1, 0, 0),
  1117. Vector3(0, -1, 0),
  1118. Vector3(0, +1, 0),
  1119. Vector3(0, 0, -1),
  1120. Vector3(0, 0, +1)
  1121. };
  1122. static const Vector3 view_up[6] = {
  1123. Vector3(0, -1, 0),
  1124. Vector3(0, -1, 0),
  1125. Vector3(0, 0, -1),
  1126. Vector3(0, 0, +1),
  1127. Vector3(0, -1, 0),
  1128. Vector3(0, -1, 0)
  1129. };
  1130. Transform xform = p_instance->transform * Transform().looking_at(view_normals[i], view_up[i]);
  1131. Vector<Plane> planes = cm.get_projection_planes(xform);
  1132. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, VS::INSTANCE_GEOMETRY_MASK);
  1133. Plane near_plane(xform.origin, -xform.basis.get_axis(2));
  1134. for (int j = 0; j < cull_count; j++) {
  1135. Instance *instance = instance_shadow_cull_result[j];
  1136. if (!instance->visible || !((1 << instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1137. cull_count--;
  1138. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1139. j--;
  1140. } else {
  1141. instance->depth = near_plane.distance_to(instance->transform.origin);
  1142. instance->depth_layer = 0;
  1143. }
  1144. }
  1145. VSG::scene_render->light_instance_set_shadow_transform(light->instance, cm, xform, radius, 0, i);
  1146. VSG::scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1147. }
  1148. //restore the regular DP matrix
  1149. VSG::scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), p_instance->transform, radius, 0, 0);
  1150. } break;
  1151. }
  1152. } break;
  1153. case VS::LIGHT_SPOT: {
  1154. float radius = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_RANGE);
  1155. float angle = VSG::storage->light_get_param(p_instance->base, VS::LIGHT_PARAM_SPOT_ANGLE);
  1156. CameraMatrix cm;
  1157. cm.set_perspective(angle * 2.0, 1.0, 0.01, radius);
  1158. Vector<Plane> planes = cm.get_projection_planes(p_instance->transform);
  1159. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, VS::INSTANCE_GEOMETRY_MASK);
  1160. Plane near_plane(p_instance->transform.origin, -p_instance->transform.basis.get_axis(2));
  1161. for (int j = 0; j < cull_count; j++) {
  1162. Instance *instance = instance_shadow_cull_result[j];
  1163. if (!instance->visible || !((1 << instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1164. cull_count--;
  1165. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1166. j--;
  1167. } else {
  1168. instance->depth = near_plane.distance_to(instance->transform.origin);
  1169. instance->depth_layer = 0;
  1170. }
  1171. }
  1172. VSG::scene_render->light_instance_set_shadow_transform(light->instance, cm, p_instance->transform, radius, 0, 0);
  1173. VSG::scene_render->render_shadow(light->instance, p_shadow_atlas, 0, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1174. } break;
  1175. }
  1176. }
  1177. void VisualServerScene::render_camera(RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas) {
  1178. Camera *camera = camera_owner.getornull(p_camera);
  1179. ERR_FAIL_COND(!camera);
  1180. /* STEP 1 - SETUP CAMERA */
  1181. CameraMatrix camera_matrix;
  1182. bool ortho = false;
  1183. switch (camera->type) {
  1184. case Camera::ORTHOGONAL: {
  1185. camera_matrix.set_orthogonal(
  1186. camera->size,
  1187. p_viewport_size.width / (float)p_viewport_size.height,
  1188. camera->znear,
  1189. camera->zfar,
  1190. camera->vaspect
  1191. );
  1192. ortho = true;
  1193. } break;
  1194. case Camera::PERSPECTIVE: {
  1195. camera_matrix.set_perspective(
  1196. camera->fov,
  1197. p_viewport_size.width / (float)p_viewport_size.height,
  1198. camera->znear,
  1199. camera->zfar,
  1200. camera->vaspect
  1201. );
  1202. ortho = false;
  1203. } break;
  1204. }
  1205. _render_scene(camera->transform, camera_matrix, ortho, camera->env, camera->visible_layers, p_scenario, p_shadow_atlas, RID(), -1);
  1206. }
  1207. 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) {
  1208. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1209. render_pass++;
  1210. uint32_t camera_layer_mask = p_visible_layers;
  1211. VSG::scene_render->set_scene_pass(render_pass);
  1212. //rasterizer->set_camera(camera->transform, camera_matrix,ortho);
  1213. Vector<Plane> planes = p_cam_projection.get_projection_planes(p_cam_transform);
  1214. Plane near_plane(p_cam_transform.origin, -p_cam_transform.basis.get_axis(2).normalized());
  1215. float z_far = p_cam_projection.get_z_far();
  1216. /* STEP 2 - CULL */
  1217. int cull_count = scenario->octree.cull_convex(planes, instance_cull_result, MAX_INSTANCE_CULL);
  1218. light_cull_count = 0;
  1219. reflection_probe_cull_count = 0;
  1220. //light_samplers_culled=0;
  1221. /* print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  1222. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  1223. //print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  1224. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  1225. */
  1226. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  1227. // compute portals
  1228. #if 0
  1229. exterior_visited=false;
  1230. exterior_portal_cull_count=0;
  1231. if (room_cull_enabled) {
  1232. for(int i=0;i<cull_count;i++) {
  1233. Instance *ins = instance_cull_result[i];
  1234. ins->last_render_pass=render_pass;
  1235. if (ins->base_type!=INSTANCE_PORTAL)
  1236. continue;
  1237. if (ins->room)
  1238. continue;
  1239. ERR_CONTINUE(exterior_portal_cull_count>=MAX_EXTERIOR_PORTALS);
  1240. exterior_portal_cull_result[exterior_portal_cull_count++]=ins;
  1241. }
  1242. room_cull_count = p_scenario->octree.cull_point(camera->transform.origin,room_cull_result,MAX_ROOM_CULL,NULL,(1<<INSTANCE_ROOM)|(1<<INSTANCE_PORTAL));
  1243. Set<Instance*> current_rooms;
  1244. Set<Instance*> portal_rooms;
  1245. //add to set
  1246. for(int i=0;i<room_cull_count;i++) {
  1247. if (room_cull_result[i]->base_type==INSTANCE_ROOM) {
  1248. current_rooms.insert(room_cull_result[i]);
  1249. }
  1250. if (room_cull_result[i]->base_type==INSTANCE_PORTAL) {
  1251. //assume inside that room if also inside the portal..
  1252. if (room_cull_result[i]->room) {
  1253. portal_rooms.insert(room_cull_result[i]->room);
  1254. }
  1255. SWAP(room_cull_result[i],room_cull_result[room_cull_count-1]);
  1256. room_cull_count--;
  1257. i--;
  1258. }
  1259. }
  1260. //remove from set if it has a parent room or BSP doesn't contain
  1261. for(int i=0;i<room_cull_count;i++) {
  1262. Instance *r = room_cull_result[i];
  1263. //check inside BSP
  1264. Vector3 room_local_point = r->room_info->affine_inverse.xform( camera->transform.origin );
  1265. if (!portal_rooms.has(r) && !r->room_info->room->bounds.point_is_inside(room_local_point)) {
  1266. current_rooms.erase(r);
  1267. continue;
  1268. }
  1269. //check parent
  1270. while (r->room) {// has parent room
  1271. current_rooms.erase(r);
  1272. r=r->room;
  1273. }
  1274. }
  1275. if (current_rooms.size()) {
  1276. //camera is inside a room
  1277. // go through rooms
  1278. for(Set<Instance*>::Element *E=current_rooms.front();E;E=E->next()) {
  1279. _cull_room(camera,E->get());
  1280. }
  1281. } else {
  1282. //start from exterior
  1283. _cull_room(camera,NULL);
  1284. }
  1285. }
  1286. #endif
  1287. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  1288. for (int i = 0; i < cull_count; i++) {
  1289. Instance *ins = instance_cull_result[i];
  1290. bool keep = false;
  1291. if ((camera_layer_mask & ins->layer_mask) == 0) {
  1292. //failure
  1293. } else if (ins->base_type == VS::INSTANCE_LIGHT && ins->visible) {
  1294. if (ins->visible && light_cull_count < MAX_LIGHTS_CULLED) {
  1295. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1296. if (!light->geometries.empty()) {
  1297. //do not add this light if no geometry is affected by it..
  1298. light_cull_result[light_cull_count] = ins;
  1299. light_instance_cull_result[light_cull_count] = light->instance;
  1300. if (p_shadow_atlas.is_valid() && VSG::storage->light_has_shadow(ins->base)) {
  1301. VSG::scene_render->light_instance_mark_visible(light->instance); //mark it visible for shadow allocation later
  1302. }
  1303. light_cull_count++;
  1304. }
  1305. }
  1306. } else if (ins->base_type == VS::INSTANCE_REFLECTION_PROBE && ins->visible) {
  1307. if (ins->visible && reflection_probe_cull_count < MAX_REFLECTION_PROBES_CULLED) {
  1308. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(ins->base_data);
  1309. if (p_reflection_probe != reflection_probe->instance) {
  1310. //avoid entering The Matrix
  1311. if (!reflection_probe->geometries.empty()) {
  1312. //do not add this light if no geometry is affected by it..
  1313. if (reflection_probe->reflection_dirty || VSG::scene_render->reflection_probe_instance_needs_redraw(reflection_probe->instance)) {
  1314. if (!reflection_probe->update_list.in_list()) {
  1315. reflection_probe->render_step = 0;
  1316. reflection_probe_render_list.add(&reflection_probe->update_list);
  1317. }
  1318. reflection_probe->reflection_dirty = false;
  1319. }
  1320. if (VSG::scene_render->reflection_probe_instance_has_reflection(reflection_probe->instance)) {
  1321. reflection_probe_instance_cull_result[reflection_probe_cull_count] = reflection_probe->instance;
  1322. reflection_probe_cull_count++;
  1323. }
  1324. }
  1325. }
  1326. }
  1327. } else if (ins->base_type == VS::INSTANCE_GI_PROBE && ins->visible) {
  1328. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(ins->base_data);
  1329. if (!gi_probe->update_element.in_list()) {
  1330. gi_probe_update_list.add(&gi_probe->update_element);
  1331. }
  1332. } else if ((1 << ins->base_type) & VS::INSTANCE_GEOMETRY_MASK && ins->visible && ins->cast_shadows != VS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) {
  1333. keep = true;
  1334. #if 0
  1335. bool discarded=false;
  1336. if (ins->draw_range_end>0) {
  1337. float d = cull_range.nearp.distance_to(ins->data.transform.origin);
  1338. if (d<0)
  1339. d=0;
  1340. discarded=(d<ins->draw_range_begin || d>=ins->draw_range_end);
  1341. }
  1342. if (!discarded) {
  1343. // test if this geometry should be visible
  1344. if (room_cull_enabled) {
  1345. if (ins->visible_in_all_rooms) {
  1346. keep=true;
  1347. } else if (ins->room) {
  1348. if (ins->room->room_info->last_visited_pass==render_pass)
  1349. keep=true;
  1350. } else if (ins->auto_rooms.size()) {
  1351. for(Set<Instance*>::Element *E=ins->auto_rooms.front();E;E=E->next()) {
  1352. if (E->get()->room_info->last_visited_pass==render_pass) {
  1353. keep=true;
  1354. break;
  1355. }
  1356. }
  1357. } else if(exterior_visited)
  1358. keep=true;
  1359. } else {
  1360. keep=true;
  1361. }
  1362. }
  1363. if (keep) {
  1364. // update cull range
  1365. float min,max;
  1366. ins->transformed_aabb.project_range_in_plane(cull_range.nearp,min,max);
  1367. if (min<cull_range.min)
  1368. cull_range.min=min;
  1369. if (max>cull_range.max)
  1370. cull_range.max=max;
  1371. if (ins->sampled_light && ins->sampled_light->gi_probe_sampler_info->last_pass!=render_pass) {
  1372. if (light_samplers_culled<MAX_LIGHT_SAMPLERS) {
  1373. light_sampler_cull_result[light_samplers_culled++]=ins->sampled_light;
  1374. ins->sampled_light->gi_probe_sampler_info->last_pass=render_pass;
  1375. }
  1376. }
  1377. }
  1378. #endif
  1379. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(ins->base_data);
  1380. if (ins->base_type == VS::INSTANCE_PARTICLES) {
  1381. //particles visible? process them
  1382. VSG::storage->particles_request_process(ins->base);
  1383. //particles visible? request redraw
  1384. VisualServerRaster::redraw_request();
  1385. }
  1386. if (geom->lighting_dirty) {
  1387. int l = 0;
  1388. //only called when lights AABB enter/exit this geometry
  1389. ins->light_instances.resize(geom->lighting.size());
  1390. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  1391. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1392. ins->light_instances[l++] = light->instance;
  1393. }
  1394. geom->lighting_dirty = false;
  1395. }
  1396. if (geom->reflection_dirty) {
  1397. int l = 0;
  1398. //only called when reflection probe AABB enter/exit this geometry
  1399. ins->reflection_probe_instances.resize(geom->reflection_probes.size());
  1400. for (List<Instance *>::Element *E = geom->reflection_probes.front(); E; E = E->next()) {
  1401. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(E->get()->base_data);
  1402. ins->reflection_probe_instances[l++] = reflection_probe->instance;
  1403. }
  1404. geom->reflection_dirty = false;
  1405. }
  1406. if (geom->gi_probes_dirty) {
  1407. int l = 0;
  1408. //only called when reflection probe AABB enter/exit this geometry
  1409. ins->gi_probe_instances.resize(geom->gi_probes.size());
  1410. for (List<Instance *>::Element *E = geom->gi_probes.front(); E; E = E->next()) {
  1411. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(E->get()->base_data);
  1412. ins->gi_probe_instances[l++] = gi_probe->probe_instance;
  1413. }
  1414. geom->gi_probes_dirty = false;
  1415. }
  1416. ins->depth = near_plane.distance_to(ins->transform.origin);
  1417. ins->depth_layer = CLAMP(int(ins->depth * 8 / z_far), 0, 7);
  1418. }
  1419. if (!keep) {
  1420. // remove, no reason to keep
  1421. cull_count--;
  1422. SWAP(instance_cull_result[i], instance_cull_result[cull_count]);
  1423. i--;
  1424. ins->last_render_pass = 0; // make invalid
  1425. } else {
  1426. ins->last_render_pass = render_pass;
  1427. }
  1428. }
  1429. /* STEP 5 - PROCESS LIGHTS */
  1430. RID *directional_light_ptr = &light_instance_cull_result[light_cull_count];
  1431. int directional_light_count = 0;
  1432. // directional lights
  1433. {
  1434. Instance **lights_with_shadow = (Instance **)alloca(sizeof(Instance *) * scenario->directional_lights.size());
  1435. int directional_shadow_count = 0;
  1436. for (List<Instance *>::Element *E = scenario->directional_lights.front(); E; E = E->next()) {
  1437. if (light_cull_count + directional_light_count >= MAX_LIGHTS_CULLED) {
  1438. break;
  1439. }
  1440. if (!E->get()->visible)
  1441. continue;
  1442. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1443. //check shadow..
  1444. if (light && p_shadow_atlas.is_valid() && VSG::storage->light_has_shadow(E->get()->base)) {
  1445. lights_with_shadow[directional_shadow_count++] = E->get();
  1446. }
  1447. //add to list
  1448. directional_light_ptr[directional_light_count++] = light->instance;
  1449. }
  1450. VSG::scene_render->set_directional_shadow_count(directional_shadow_count);
  1451. for (int i = 0; i < directional_shadow_count; i++) {
  1452. _light_instance_update_shadow(lights_with_shadow[i], p_cam_transform, p_cam_projection, p_cam_orthogonal, p_shadow_atlas, scenario);
  1453. }
  1454. }
  1455. { //setup shadow maps
  1456. //SortArray<Instance*,_InstanceLightsort> sorter;
  1457. //sorter.sort(light_cull_result,light_cull_count);
  1458. for (int i = 0; i < light_cull_count; i++) {
  1459. Instance *ins = light_cull_result[i];
  1460. if (!p_shadow_atlas.is_valid() || !VSG::storage->light_has_shadow(ins->base))
  1461. continue;
  1462. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1463. float coverage;
  1464. { //compute coverage
  1465. Transform cam_xf = p_cam_transform;
  1466. float zn = p_cam_projection.get_z_near();
  1467. Plane p(cam_xf.origin + cam_xf.basis.get_axis(2) * -zn, -cam_xf.basis.get_axis(2)); //camera near plane
  1468. float vp_w, vp_h; //near plane size in screen coordinates
  1469. p_cam_projection.get_viewport_size(vp_w, vp_h);
  1470. switch (VSG::storage->light_get_type(ins->base)) {
  1471. case VS::LIGHT_OMNI: {
  1472. float radius = VSG::storage->light_get_param(ins->base, VS::LIGHT_PARAM_RANGE);
  1473. //get two points parallel to near plane
  1474. Vector3 points[2] = {
  1475. ins->transform.origin,
  1476. ins->transform.origin + cam_xf.basis.get_axis(0) * radius
  1477. };
  1478. if (!p_cam_orthogonal) {
  1479. //if using perspetive, map them to near plane
  1480. for (int j = 0; j < 2; j++) {
  1481. if (p.distance_to(points[j]) < 0) {
  1482. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1483. }
  1484. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1485. }
  1486. }
  1487. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1488. coverage = screen_diameter / (vp_w + vp_h);
  1489. } break;
  1490. case VS::LIGHT_SPOT: {
  1491. float radius = VSG::storage->light_get_param(ins->base, VS::LIGHT_PARAM_RANGE);
  1492. float angle = VSG::storage->light_get_param(ins->base, VS::LIGHT_PARAM_SPOT_ANGLE);
  1493. float w = radius * Math::sin(Math::deg2rad(angle));
  1494. float d = radius * Math::cos(Math::deg2rad(angle));
  1495. Vector3 base = ins->transform.origin - ins->transform.basis.get_axis(2).normalized() * d;
  1496. Vector3 points[2] = {
  1497. base,
  1498. base + cam_xf.basis.get_axis(0) * w
  1499. };
  1500. if (!p_cam_orthogonal) {
  1501. //if using perspetive, map them to near plane
  1502. for (int j = 0; j < 2; j++) {
  1503. if (p.distance_to(points[j]) < 0) {
  1504. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1505. }
  1506. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1507. }
  1508. }
  1509. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1510. coverage = screen_diameter / (vp_w + vp_h);
  1511. } break;
  1512. default: {
  1513. ERR_PRINT("Invalid Light Type");
  1514. }
  1515. }
  1516. }
  1517. if (light->shadow_dirty) {
  1518. light->last_version++;
  1519. light->shadow_dirty = false;
  1520. }
  1521. bool redraw = VSG::scene_render->shadow_atlas_update_light(p_shadow_atlas, light->instance, coverage, light->last_version);
  1522. if (redraw) {
  1523. //must redraw!
  1524. _light_instance_update_shadow(ins, p_cam_transform, p_cam_projection, p_cam_orthogonal, p_shadow_atlas, scenario);
  1525. }
  1526. }
  1527. }
  1528. /* ENVIRONMENT */
  1529. RID environment;
  1530. if (p_force_environment.is_valid()) //camera has more environment priority
  1531. environment = p_force_environment;
  1532. else if (scenario->environment.is_valid())
  1533. environment = scenario->environment;
  1534. else
  1535. environment = scenario->fallback_environment;
  1536. #if 0
  1537. /* STEP 6 - SAMPLE BAKED LIGHT */
  1538. bool islinear =false;
  1539. if (environment.is_valid()) {
  1540. islinear = rasterizer->environment_is_fx_enabled(environment,VS::ENV_FX_SRGB);
  1541. }
  1542. for(int i=0;i<light_samplers_culled;i++) {
  1543. _process_sampled_light(camera->transform,light_sampler_cull_result[i],islinear);
  1544. }
  1545. #endif
  1546. /* STEP 7 - PROCESS GEOMETRY AND DRAW SCENE*/
  1547. 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);
  1548. }
  1549. void VisualServerScene::render_empty_scene(RID p_scenario, RID p_shadow_atlas) {
  1550. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1551. RID environment;
  1552. if (scenario->environment.is_valid())
  1553. environment = scenario->environment;
  1554. else
  1555. environment = scenario->fallback_environment;
  1556. VSG::scene_render->render_scene(Transform(), CameraMatrix(), true, NULL, 0, NULL, 0, NULL, 0, environment, p_shadow_atlas, scenario->reflection_atlas, RID(), 0);
  1557. }
  1558. bool VisualServerScene::_render_reflection_probe_step(Instance *p_instance, int p_step) {
  1559. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  1560. Scenario *scenario = p_instance->scenario;
  1561. ERR_FAIL_COND_V(!scenario, true);
  1562. if (p_step == 0) {
  1563. if (!VSG::scene_render->reflection_probe_instance_begin_render(reflection_probe->instance, scenario->reflection_atlas)) {
  1564. return true; //sorry, all full :(
  1565. }
  1566. }
  1567. if (p_step >= 0 && p_step < 6) {
  1568. static const Vector3 view_normals[6] = {
  1569. Vector3(-1, 0, 0),
  1570. Vector3(+1, 0, 0),
  1571. Vector3(0, -1, 0),
  1572. Vector3(0, +1, 0),
  1573. Vector3(0, 0, -1),
  1574. Vector3(0, 0, +1)
  1575. };
  1576. Vector3 extents = VSG::storage->reflection_probe_get_extents(p_instance->base);
  1577. Vector3 origin_offset = VSG::storage->reflection_probe_get_origin_offset(p_instance->base);
  1578. float max_distance = VSG::storage->reflection_probe_get_origin_max_distance(p_instance->base);
  1579. Vector3 edge = view_normals[p_step] * extents;
  1580. 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
  1581. max_distance = MAX(max_distance, distance);
  1582. //render cubemap side
  1583. CameraMatrix cm;
  1584. cm.set_perspective(90, 1, 0.01, max_distance);
  1585. static const Vector3 view_up[6] = {
  1586. Vector3(0, -1, 0),
  1587. Vector3(0, -1, 0),
  1588. Vector3(0, 0, -1),
  1589. Vector3(0, 0, +1),
  1590. Vector3(0, -1, 0),
  1591. Vector3(0, -1, 0)
  1592. };
  1593. Transform local_view;
  1594. local_view.set_look_at(origin_offset, origin_offset + view_normals[p_step], view_up[p_step]);
  1595. Transform xform = p_instance->transform * local_view;
  1596. RID shadow_atlas;
  1597. if (VSG::storage->reflection_probe_renders_shadows(p_instance->base)) {
  1598. shadow_atlas = scenario->reflection_probe_shadow_atlas;
  1599. }
  1600. _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);
  1601. } else {
  1602. //do roughness postprocess step until it belives it's done
  1603. return VSG::scene_render->reflection_probe_instance_postprocess_step(reflection_probe->instance);
  1604. }
  1605. return false;
  1606. }
  1607. 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) {
  1608. if (p_level == p_header->cell_subdiv - 1) {
  1609. Vector3 emission;
  1610. emission.x = (p_cell[p_idx].emission >> 24) / 255.0;
  1611. emission.y = ((p_cell[p_idx].emission >> 16) & 0xFF) / 255.0;
  1612. emission.z = ((p_cell[p_idx].emission >> 8) & 0xFF) / 255.0;
  1613. float l = (p_cell[p_idx].emission & 0xFF) / 255.0;
  1614. l *= 8.0;
  1615. emission *= l;
  1616. p_local_data[p_idx].energy[0] = uint16_t(emission.x * 1024); //go from 0 to 1024 for light
  1617. p_local_data[p_idx].energy[1] = uint16_t(emission.y * 1024); //go from 0 to 1024 for light
  1618. p_local_data[p_idx].energy[2] = uint16_t(emission.z * 1024); //go from 0 to 1024 for light
  1619. } else {
  1620. p_local_data[p_idx].energy[0] = 0;
  1621. p_local_data[p_idx].energy[1] = 0;
  1622. p_local_data[p_idx].energy[2] = 0;
  1623. int half = (1 << (p_header->cell_subdiv - 1)) >> (p_level + 1);
  1624. for (int i = 0; i < 8; i++) {
  1625. uint32_t child = p_cell[p_idx].children[i];
  1626. if (child == 0xFFFFFFFF)
  1627. continue;
  1628. int x = p_x;
  1629. int y = p_y;
  1630. int z = p_z;
  1631. if (i & 1)
  1632. x += half;
  1633. if (i & 2)
  1634. y += half;
  1635. if (i & 4)
  1636. z += half;
  1637. _gi_probe_fill_local_data(child, p_level + 1, x, y, z, p_cell, p_header, p_local_data, prev_cell);
  1638. }
  1639. }
  1640. //position for each part of the mipmaped texture
  1641. p_local_data[p_idx].pos[0] = p_x >> (p_header->cell_subdiv - p_level - 1);
  1642. p_local_data[p_idx].pos[1] = p_y >> (p_header->cell_subdiv - p_level - 1);
  1643. p_local_data[p_idx].pos[2] = p_z >> (p_header->cell_subdiv - p_level - 1);
  1644. prev_cell[p_level].push_back(p_idx);
  1645. }
  1646. void VisualServerScene::_gi_probe_bake_threads(void *self) {
  1647. VisualServerScene *vss = (VisualServerScene *)self;
  1648. vss->_gi_probe_bake_thread();
  1649. }
  1650. void VisualServerScene::_setup_gi_probe(Instance *p_instance) {
  1651. InstanceGIProbeData *probe = static_cast<InstanceGIProbeData *>(p_instance->base_data);
  1652. if (probe->dynamic.probe_data.is_valid()) {
  1653. VSG::storage->free(probe->dynamic.probe_data);
  1654. probe->dynamic.probe_data = RID();
  1655. }
  1656. probe->dynamic.light_data = VSG::storage->gi_probe_get_dynamic_data(p_instance->base);
  1657. if (probe->dynamic.light_data.size() == 0)
  1658. return;
  1659. //using dynamic data
  1660. PoolVector<int>::Read r = probe->dynamic.light_data.read();
  1661. const GIProbeDataHeader *header = (GIProbeDataHeader *)r.ptr();
  1662. probe->dynamic.local_data.resize(header->cell_count);
  1663. int cell_count = probe->dynamic.local_data.size();
  1664. PoolVector<InstanceGIProbeData::LocalData>::Write ldw = probe->dynamic.local_data.write();
  1665. const GIProbeDataCell *cells = (GIProbeDataCell *)&r[16];
  1666. probe->dynamic.level_cell_lists.resize(header->cell_subdiv);
  1667. _gi_probe_fill_local_data(0, 0, 0, 0, 0, cells, header, ldw.ptr(), probe->dynamic.level_cell_lists.ptr());
  1668. bool compress = VSG::storage->gi_probe_is_compressed(p_instance->base);
  1669. probe->dynamic.compression = compress ? VSG::storage->gi_probe_get_dynamic_data_get_preferred_compression() : RasterizerStorage::GI_PROBE_UNCOMPRESSED;
  1670. probe->dynamic.probe_data = VSG::storage->gi_probe_dynamic_data_create(header->width, header->height, header->depth, probe->dynamic.compression);
  1671. probe->dynamic.bake_dynamic_range = VSG::storage->gi_probe_get_dynamic_range(p_instance->base);
  1672. probe->dynamic.mipmaps_3d.clear();
  1673. probe->dynamic.propagate = VSG::storage->gi_probe_get_propagation(p_instance->base);
  1674. probe->dynamic.grid_size[0] = header->width;
  1675. probe->dynamic.grid_size[1] = header->height;
  1676. probe->dynamic.grid_size[2] = header->depth;
  1677. int size_limit = 1;
  1678. int size_divisor = 1;
  1679. if (probe->dynamic.compression == RasterizerStorage::GI_PROBE_S3TC) {
  1680. print_line("S3TC");
  1681. size_limit = 4;
  1682. size_divisor = 4;
  1683. }
  1684. for (int i = 0; i < (int)header->cell_subdiv; i++) {
  1685. uint32_t x = header->width >> i;
  1686. uint32_t y = header->height >> i;
  1687. uint32_t z = header->depth >> i;
  1688. //create and clear mipmap
  1689. PoolVector<uint8_t> mipmap;
  1690. int size = x * y * z * 4;
  1691. size /= size_divisor;
  1692. mipmap.resize(size);
  1693. PoolVector<uint8_t>::Write w = mipmap.write();
  1694. zeromem(w.ptr(), size);
  1695. w = PoolVector<uint8_t>::Write();
  1696. probe->dynamic.mipmaps_3d.push_back(mipmap);
  1697. if (x <= size_limit || y <= size_limit || z <= size_limit)
  1698. break;
  1699. }
  1700. probe->dynamic.updating_stage = GI_UPDATE_STAGE_CHECK;
  1701. probe->invalid = false;
  1702. probe->dynamic.enabled = true;
  1703. Transform cell_to_xform = VSG::storage->gi_probe_get_to_cell_xform(p_instance->base);
  1704. Rect3 bounds = VSG::storage->gi_probe_get_bounds(p_instance->base);
  1705. float cell_size = VSG::storage->gi_probe_get_cell_size(p_instance->base);
  1706. probe->dynamic.light_to_cell_xform = cell_to_xform * p_instance->transform.affine_inverse();
  1707. VSG::scene_render->gi_probe_instance_set_light_data(probe->probe_instance, p_instance->base, probe->dynamic.probe_data);
  1708. VSG::scene_render->gi_probe_instance_set_transform_to_data(probe->probe_instance, probe->dynamic.light_to_cell_xform);
  1709. VSG::scene_render->gi_probe_instance_set_bounds(probe->probe_instance, bounds.size / cell_size);
  1710. probe->base_version = VSG::storage->gi_probe_get_version(p_instance->base);
  1711. //if compression is S3TC, fill it up
  1712. if (probe->dynamic.compression == RasterizerStorage::GI_PROBE_S3TC) {
  1713. //create all blocks
  1714. Vector<Map<uint32_t, InstanceGIProbeData::CompBlockS3TC> > comp_blocks;
  1715. int mipmap_count = probe->dynamic.mipmaps_3d.size();
  1716. comp_blocks.resize(mipmap_count);
  1717. for (int i = 0; i < cell_count; i++) {
  1718. const GIProbeDataCell &c = cells[i];
  1719. const InstanceGIProbeData::LocalData &ld = ldw[i];
  1720. int level = c.level_alpha >> 16;
  1721. int mipmap = header->cell_subdiv - level - 1;
  1722. if (mipmap >= mipmap_count)
  1723. continue; //uninteresting
  1724. int blockx = (ld.pos[0] >> 2);
  1725. int blocky = (ld.pos[1] >> 2);
  1726. int blockz = (ld.pos[2]); //compression is x/y only
  1727. int blockw = (header->width >> mipmap) >> 2;
  1728. int blockh = (header->height >> mipmap) >> 2;
  1729. //print_line("cell "+itos(i)+" level "+itos(level)+"mipmap: "+itos(mipmap)+" pos: "+Vector3(blockx,blocky,blockz)+" size "+Vector2(blockw,blockh));
  1730. uint32_t key = blockz * blockw * blockh + blocky * blockw + blockx;
  1731. Map<uint32_t, InstanceGIProbeData::CompBlockS3TC> &cmap = comp_blocks[mipmap];
  1732. if (!cmap.has(key)) {
  1733. InstanceGIProbeData::CompBlockS3TC k;
  1734. k.offset = key; //use offset as counter first
  1735. k.source_count = 0;
  1736. cmap[key] = k;
  1737. }
  1738. InstanceGIProbeData::CompBlockS3TC &k = cmap[key];
  1739. ERR_CONTINUE(k.source_count == 16);
  1740. k.sources[k.source_count++] = i;
  1741. }
  1742. //fix the blocks, precomputing what is needed
  1743. probe->dynamic.mipmaps_s3tc.resize(mipmap_count);
  1744. for (int i = 0; i < mipmap_count; i++) {
  1745. print_line("S3TC level: " + itos(i) + " blocks: " + itos(comp_blocks[i].size()));
  1746. probe->dynamic.mipmaps_s3tc[i].resize(comp_blocks[i].size());
  1747. PoolVector<InstanceGIProbeData::CompBlockS3TC>::Write w = probe->dynamic.mipmaps_s3tc[i].write();
  1748. int block_idx = 0;
  1749. for (Map<uint32_t, InstanceGIProbeData::CompBlockS3TC>::Element *E = comp_blocks[i].front(); E; E = E->next()) {
  1750. InstanceGIProbeData::CompBlockS3TC k = E->get();
  1751. //PRECOMPUTE ALPHA
  1752. int max_alpha = -100000;
  1753. 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)
  1754. uint8_t alpha_block[4][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
  1755. for (int j = 0; j < k.source_count; j++) {
  1756. int alpha = (cells[k.sources[j]].level_alpha >> 8) & 0xFF;
  1757. if (alpha < min_alpha)
  1758. min_alpha = alpha;
  1759. if (alpha > max_alpha)
  1760. max_alpha = alpha;
  1761. //fill up alpha block
  1762. alpha_block[ldw[k.sources[j]].pos[0] % 4][ldw[k.sources[j]].pos[1] % 4] = alpha;
  1763. }
  1764. //use the first mode (8 adjustable levels)
  1765. k.alpha[0] = max_alpha;
  1766. k.alpha[1] = min_alpha;
  1767. uint64_t alpha_bits = 0;
  1768. if (max_alpha != min_alpha) {
  1769. int idx = 0;
  1770. for (int y = 0; y < 4; y++) {
  1771. for (int x = 0; x < 4; x++) {
  1772. //subtract minimum
  1773. uint32_t a = uint32_t(alpha_block[x][y]) - min_alpha;
  1774. //convert range to 3 bits
  1775. a = int((a * 7.0 / (max_alpha - min_alpha)) + 0.5);
  1776. a = CLAMP(a, 0, 7); //just to be sure
  1777. a = 7 - a; //because range is inverted in this mode
  1778. if (a == 0) {
  1779. //do none, remain
  1780. } else if (a == 7) {
  1781. a = 1;
  1782. } else {
  1783. a = a + 1;
  1784. }
  1785. alpha_bits |= uint64_t(a) << (idx * 3);
  1786. idx++;
  1787. }
  1788. }
  1789. }
  1790. k.alpha[2] = (alpha_bits >> 0) & 0xFF;
  1791. k.alpha[3] = (alpha_bits >> 8) & 0xFF;
  1792. k.alpha[4] = (alpha_bits >> 16) & 0xFF;
  1793. k.alpha[5] = (alpha_bits >> 24) & 0xFF;
  1794. k.alpha[6] = (alpha_bits >> 32) & 0xFF;
  1795. k.alpha[7] = (alpha_bits >> 40) & 0xFF;
  1796. w[block_idx++] = k;
  1797. }
  1798. }
  1799. }
  1800. }
  1801. void VisualServerScene::_gi_probe_bake_thread() {
  1802. while (true) {
  1803. probe_bake_sem->wait();
  1804. if (probe_bake_thread_exit) {
  1805. break;
  1806. }
  1807. Instance *to_bake = NULL;
  1808. probe_bake_mutex->lock();
  1809. if (!probe_bake_list.empty()) {
  1810. to_bake = probe_bake_list.front()->get();
  1811. probe_bake_list.pop_front();
  1812. }
  1813. probe_bake_mutex->unlock();
  1814. if (!to_bake)
  1815. continue;
  1816. _bake_gi_probe(to_bake);
  1817. }
  1818. }
  1819. uint32_t VisualServerScene::_gi_bake_find_cell(const GIProbeDataCell *cells, int x, int y, int z, int p_cell_subdiv) {
  1820. uint32_t cell = 0;
  1821. int ofs_x = 0;
  1822. int ofs_y = 0;
  1823. int ofs_z = 0;
  1824. int size = 1 << (p_cell_subdiv - 1);
  1825. int half = size / 2;
  1826. if (x < 0 || x >= size)
  1827. return -1;
  1828. if (y < 0 || y >= size)
  1829. return -1;
  1830. if (z < 0 || z >= size)
  1831. return -1;
  1832. for (int i = 0; i < p_cell_subdiv - 1; i++) {
  1833. const GIProbeDataCell *bc = &cells[cell];
  1834. int child = 0;
  1835. if (x >= ofs_x + half) {
  1836. child |= 1;
  1837. ofs_x += half;
  1838. }
  1839. if (y >= ofs_y + half) {
  1840. child |= 2;
  1841. ofs_y += half;
  1842. }
  1843. if (z >= ofs_z + half) {
  1844. child |= 4;
  1845. ofs_z += half;
  1846. }
  1847. cell = bc->children[child];
  1848. if (cell == 0xFFFFFFFF)
  1849. return 0xFFFFFFFF;
  1850. half >>= 1;
  1851. }
  1852. return cell;
  1853. }
  1854. static float _get_normal_advance(const Vector3 &p_normal) {
  1855. Vector3 normal = p_normal;
  1856. Vector3 unorm = normal.abs();
  1857. if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
  1858. // x code
  1859. unorm = normal.x > 0.0 ? Vector3(1.0, 0.0, 0.0) : Vector3(-1.0, 0.0, 0.0);
  1860. } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
  1861. // y code
  1862. unorm = normal.y > 0.0 ? Vector3(0.0, 1.0, 0.0) : Vector3(0.0, -1.0, 0.0);
  1863. } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
  1864. // z code
  1865. unorm = normal.z > 0.0 ? Vector3(0.0, 0.0, 1.0) : Vector3(0.0, 0.0, -1.0);
  1866. } else {
  1867. // oh-no we messed up code
  1868. // has to be
  1869. unorm = Vector3(1.0, 0.0, 0.0);
  1870. }
  1871. return 1.0 / normal.dot(unorm);
  1872. }
  1873. 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) {
  1874. int light_r = int(light_cache.color.r * light_cache.energy * 1024.0) * sign;
  1875. int light_g = int(light_cache.color.g * light_cache.energy * 1024.0) * sign;
  1876. int light_b = int(light_cache.color.b * light_cache.energy * 1024.0) * sign;
  1877. float limits[3] = { float(header->width), float(header->height), float(header->depth) };
  1878. Plane clip[3];
  1879. int clip_planes = 0;
  1880. switch (light_cache.type) {
  1881. case VS::LIGHT_DIRECTIONAL: {
  1882. float max_len = Vector3(limits[0], limits[1], limits[2]).length() * 1.1;
  1883. Vector3 light_axis = -light_cache.transform.basis.get_axis(2).normalized();
  1884. for (int i = 0; i < 3; i++) {
  1885. if (ABS(light_axis[i]) < CMP_EPSILON)
  1886. continue;
  1887. clip[clip_planes].normal[i] = 1.0;
  1888. if (light_axis[i] < 0) {
  1889. clip[clip_planes].d = limits[i] + 1;
  1890. } else {
  1891. clip[clip_planes].d -= 1.0;
  1892. }
  1893. clip_planes++;
  1894. }
  1895. float distance_adv = _get_normal_advance(light_axis);
  1896. int success_count = 0;
  1897. uint64_t us = OS::get_singleton()->get_ticks_usec();
  1898. for (int i = 0; i < leaf_count; i++) {
  1899. uint32_t idx = leaves[i];
  1900. const GIProbeDataCell *cell = &cells[idx];
  1901. InstanceGIProbeData::LocalData *light = &local_data[idx];
  1902. Vector3 to(light->pos[0] + 0.5, light->pos[1] + 0.5, light->pos[2] + 0.5);
  1903. Vector3 norm(
  1904. (((cells[idx].normal >> 16) & 0xFF) / 255.0) * 2.0 - 1.0,
  1905. (((cells[idx].normal >> 8) & 0xFF) / 255.0) * 2.0 - 1.0,
  1906. (((cells[idx].normal >> 0) & 0xFF) / 255.0) * 2.0 - 1.0);
  1907. float att = norm.dot(-light_axis);
  1908. if (att < 0.001) {
  1909. //not lighting towards this
  1910. continue;
  1911. }
  1912. Vector3 from = to - max_len * light_axis;
  1913. for (int j = 0; j < clip_planes; j++) {
  1914. clip[j].intersects_segment(from, to, &from);
  1915. }
  1916. float distance = (to - from).length();
  1917. distance += distance_adv - Math::fmod(distance, distance_adv); //make it reach the center of the box always
  1918. from = to - light_axis * distance;
  1919. uint32_t result = 0xFFFFFFFF;
  1920. while (distance > -distance_adv) { //use this to avoid precision errors
  1921. result = _gi_bake_find_cell(cells, int(floor(from.x)), int(floor(from.y)), int(floor(from.z)), header->cell_subdiv);
  1922. if (result != 0xFFFFFFFF) {
  1923. break;
  1924. }
  1925. from += light_axis * distance_adv;
  1926. distance -= distance_adv;
  1927. }
  1928. if (result == idx) {
  1929. //cell hit itself! hooray!
  1930. light->energy[0] += int32_t(light_r * att * ((cell->albedo >> 16) & 0xFF) / 255.0);
  1931. light->energy[1] += int32_t(light_g * att * ((cell->albedo >> 8) & 0xFF) / 255.0);
  1932. light->energy[2] += int32_t(light_b * att * ((cell->albedo) & 0xFF) / 255.0);
  1933. success_count++;
  1934. }
  1935. }
  1936. print_line("BAKE TIME: " + rtos((OS::get_singleton()->get_ticks_usec() - us) / 1000000.0));
  1937. print_line("valid cells: " + itos(success_count));
  1938. } break;
  1939. case VS::LIGHT_OMNI:
  1940. case VS::LIGHT_SPOT: {
  1941. uint64_t us = OS::get_singleton()->get_ticks_usec();
  1942. Vector3 light_pos = light_cache.transform.origin;
  1943. Vector3 spot_axis = -light_cache.transform.basis.get_axis(2).normalized();
  1944. float local_radius = light_cache.radius * light_cache.transform.basis.get_axis(2).length();
  1945. for (int i = 0; i < leaf_count; i++) {
  1946. uint32_t idx = leaves[i];
  1947. const GIProbeDataCell *cell = &cells[idx];
  1948. InstanceGIProbeData::LocalData *light = &local_data[idx];
  1949. Vector3 to(light->pos[0] + 0.5, light->pos[1] + 0.5, light->pos[2] + 0.5);
  1950. Vector3 norm(
  1951. (((cells[idx].normal >> 16) & 0xFF) / 255.0) * 2.0 - 1.0,
  1952. (((cells[idx].normal >> 8) & 0xFF) / 255.0) * 2.0 - 1.0,
  1953. (((cells[idx].normal >> 0) & 0xFF) / 255.0) * 2.0 - 1.0);
  1954. Vector3 light_axis = (to - light_pos).normalized();
  1955. float distance_adv = _get_normal_advance(light_axis);
  1956. float att = norm.dot(-light_axis);
  1957. if (att < 0.001) {
  1958. //not lighting towards this
  1959. continue;
  1960. }
  1961. {
  1962. float d = light_pos.distance_to(to);
  1963. if (d + distance_adv > local_radius)
  1964. continue; // too far away
  1965. float dt = CLAMP((d + distance_adv) / local_radius, 0, 1);
  1966. att *= powf(1.0 - dt, light_cache.attenuation);
  1967. }
  1968. if (light_cache.type == VS::LIGHT_SPOT) {
  1969. float angle = Math::rad2deg(acos(light_axis.dot(spot_axis)));
  1970. if (angle > light_cache.spot_angle)
  1971. continue;
  1972. float d = CLAMP(angle / light_cache.spot_angle, 1, 0);
  1973. att *= powf(1.0 - d, light_cache.spot_attenuation);
  1974. }
  1975. clip_planes = 0;
  1976. for (int c = 0; c < 3; c++) {
  1977. if (ABS(light_axis[c]) < CMP_EPSILON)
  1978. continue;
  1979. clip[clip_planes].normal[c] = 1.0;
  1980. if (light_axis[c] < 0) {
  1981. clip[clip_planes].d = limits[c] + 1;
  1982. } else {
  1983. clip[clip_planes].d -= 1.0;
  1984. }
  1985. clip_planes++;
  1986. }
  1987. Vector3 from = light_pos;
  1988. for (int j = 0; j < clip_planes; j++) {
  1989. clip[j].intersects_segment(from, to, &from);
  1990. }
  1991. float distance = (to - from).length();
  1992. distance -= Math::fmod(distance, distance_adv); //make it reach the center of the box always, but this tame make it closer
  1993. from = to - light_axis * distance;
  1994. uint32_t result = 0xFFFFFFFF;
  1995. while (distance > -distance_adv) { //use this to avoid precision errors
  1996. result = _gi_bake_find_cell(cells, int(floor(from.x)), int(floor(from.y)), int(floor(from.z)), header->cell_subdiv);
  1997. if (result != 0xFFFFFFFF) {
  1998. break;
  1999. }
  2000. from += light_axis * distance_adv;
  2001. distance -= distance_adv;
  2002. }
  2003. if (result == idx) {
  2004. //cell hit itself! hooray!
  2005. light->energy[0] += int32_t(light_r * att * ((cell->albedo >> 16) & 0xFF) / 255.0);
  2006. light->energy[1] += int32_t(light_g * att * ((cell->albedo >> 8) & 0xFF) / 255.0);
  2007. light->energy[2] += int32_t(light_b * att * ((cell->albedo) & 0xFF) / 255.0);
  2008. }
  2009. }
  2010. print_line("BAKE TIME: " + rtos((OS::get_singleton()->get_ticks_usec() - us) / 1000000.0));
  2011. } break;
  2012. }
  2013. }
  2014. 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) {
  2015. //average light to upper level
  2016. float divisor = 0;
  2017. float sum[3] = { 0.0, 0.0, 0.0 };
  2018. for (int i = 0; i < 8; i++) {
  2019. uint32_t child = p_cells[p_idx].children[i];
  2020. if (child == 0xFFFFFFFF)
  2021. continue;
  2022. if (p_level + 1 < (int)p_header->cell_subdiv - 1) {
  2023. _bake_gi_downscale_light(child, p_level + 1, p_cells, p_header, p_local_data, p_propagate);
  2024. }
  2025. sum[0] += p_local_data[child].energy[0];
  2026. sum[1] += p_local_data[child].energy[1];
  2027. sum[2] += p_local_data[child].energy[2];
  2028. divisor += 1.0;
  2029. }
  2030. divisor = Math::lerp((float)8.0, divisor, p_propagate);
  2031. sum[0] /= divisor;
  2032. sum[1] /= divisor;
  2033. sum[2] /= divisor;
  2034. //divide by eight for average
  2035. p_local_data[p_idx].energy[0] = Math::fast_ftoi(sum[0]);
  2036. p_local_data[p_idx].energy[1] = Math::fast_ftoi(sum[1]);
  2037. p_local_data[p_idx].energy[2] = Math::fast_ftoi(sum[2]);
  2038. }
  2039. void VisualServerScene::_bake_gi_probe(Instance *p_gi_probe) {
  2040. InstanceGIProbeData *probe_data = static_cast<InstanceGIProbeData *>(p_gi_probe->base_data);
  2041. PoolVector<int>::Read r = probe_data->dynamic.light_data.read();
  2042. const GIProbeDataHeader *header = (const GIProbeDataHeader *)r.ptr();
  2043. const GIProbeDataCell *cells = (const GIProbeDataCell *)&r[16];
  2044. int leaf_count = probe_data->dynamic.level_cell_lists[header->cell_subdiv - 1].size();
  2045. const uint32_t *leaves = probe_data->dynamic.level_cell_lists[header->cell_subdiv - 1].ptr();
  2046. PoolVector<InstanceGIProbeData::LocalData>::Write ldw = probe_data->dynamic.local_data.write();
  2047. InstanceGIProbeData::LocalData *local_data = ldw.ptr();
  2048. //remove what must be removed
  2049. for (Map<RID, InstanceGIProbeData::LightCache>::Element *E = probe_data->dynamic.light_cache.front(); E; E = E->next()) {
  2050. RID rid = E->key();
  2051. const InstanceGIProbeData::LightCache &lc = E->get();
  2052. if (!probe_data->dynamic.light_cache_changes.has(rid) || !(probe_data->dynamic.light_cache_changes[rid] == lc)) {
  2053. //erase light data
  2054. _bake_gi_probe_light(header, cells, local_data, leaves, leaf_count, lc, -1);
  2055. }
  2056. }
  2057. //add what must be added
  2058. for (Map<RID, InstanceGIProbeData::LightCache>::Element *E = probe_data->dynamic.light_cache_changes.front(); E; E = E->next()) {
  2059. RID rid = E->key();
  2060. const InstanceGIProbeData::LightCache &lc = E->get();
  2061. if (!probe_data->dynamic.light_cache.has(rid) || !(probe_data->dynamic.light_cache[rid] == lc)) {
  2062. //add light data
  2063. _bake_gi_probe_light(header, cells, local_data, leaves, leaf_count, lc, 1);
  2064. }
  2065. }
  2066. SWAP(probe_data->dynamic.light_cache_changes, probe_data->dynamic.light_cache);
  2067. //downscale to lower res levels
  2068. _bake_gi_downscale_light(0, 0, cells, header, local_data, probe_data->dynamic.propagate);
  2069. //plot result to 3D texture!
  2070. if (probe_data->dynamic.compression == RasterizerStorage::GI_PROBE_UNCOMPRESSED) {
  2071. for (int i = 0; i < (int)header->cell_subdiv; i++) {
  2072. int stage = header->cell_subdiv - i - 1;
  2073. if (stage >= probe_data->dynamic.mipmaps_3d.size())
  2074. continue; //no mipmap for this one
  2075. print_line("generating mipmap stage: " + itos(stage));
  2076. int level_cell_count = probe_data->dynamic.level_cell_lists[i].size();
  2077. const uint32_t *level_cells = probe_data->dynamic.level_cell_lists[i].ptr();
  2078. PoolVector<uint8_t>::Write lw = probe_data->dynamic.mipmaps_3d[stage].write();
  2079. uint8_t *mipmapw = lw.ptr();
  2080. uint32_t sizes[3] = { header->width >> stage, header->height >> stage, header->depth >> stage };
  2081. for (int j = 0; j < level_cell_count; j++) {
  2082. uint32_t idx = level_cells[j];
  2083. uint32_t r = (uint32_t(local_data[idx].energy[0]) / probe_data->dynamic.bake_dynamic_range) >> 2;
  2084. uint32_t g = (uint32_t(local_data[idx].energy[1]) / probe_data->dynamic.bake_dynamic_range) >> 2;
  2085. uint32_t b = (uint32_t(local_data[idx].energy[2]) / probe_data->dynamic.bake_dynamic_range) >> 2;
  2086. uint32_t a = (cells[idx].level_alpha >> 8) & 0xFF;
  2087. 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]);
  2088. mm_ofs *= 4; //for RGBA (4 bytes)
  2089. mipmapw[mm_ofs + 0] = uint8_t(CLAMP(r, 0, 255));
  2090. mipmapw[mm_ofs + 1] = uint8_t(CLAMP(g, 0, 255));
  2091. mipmapw[mm_ofs + 2] = uint8_t(CLAMP(b, 0, 255));
  2092. mipmapw[mm_ofs + 3] = uint8_t(CLAMP(a, 0, 255));
  2093. }
  2094. }
  2095. } else if (probe_data->dynamic.compression == RasterizerStorage::GI_PROBE_S3TC) {
  2096. int mipmap_count = probe_data->dynamic.mipmaps_3d.size();
  2097. for (int mmi = 0; mmi < mipmap_count; mmi++) {
  2098. PoolVector<uint8_t>::Write mmw = probe_data->dynamic.mipmaps_3d[mmi].write();
  2099. int block_count = probe_data->dynamic.mipmaps_s3tc[mmi].size();
  2100. PoolVector<InstanceGIProbeData::CompBlockS3TC>::Read mmr = probe_data->dynamic.mipmaps_s3tc[mmi].read();
  2101. for (int i = 0; i < block_count; i++) {
  2102. const InstanceGIProbeData::CompBlockS3TC &b = mmr[i];
  2103. uint8_t *blockptr = &mmw[b.offset * 16];
  2104. copymem(blockptr, b.alpha, 8); //copy alpha part, which is precomputed
  2105. Vector3 colors[16];
  2106. for (int j = 0; j < b.source_count; j++) {
  2107. colors[j].x = (local_data[b.sources[j]].energy[0] / float(probe_data->dynamic.bake_dynamic_range)) / 1024.0;
  2108. colors[j].y = (local_data[b.sources[j]].energy[1] / float(probe_data->dynamic.bake_dynamic_range)) / 1024.0;
  2109. colors[j].z = (local_data[b.sources[j]].energy[2] / float(probe_data->dynamic.bake_dynamic_range)) / 1024.0;
  2110. }
  2111. //super quick and dirty compression
  2112. //find 2 most further apart
  2113. float distance = 0;
  2114. Vector3 from, to;
  2115. if (b.source_count == 16) {
  2116. //all cells are used so, find minmax between them
  2117. int further_apart[2] = { 0, 0 };
  2118. for (int j = 0; j < b.source_count; j++) {
  2119. for (int k = j + 1; k < b.source_count; k++) {
  2120. float d = colors[j].distance_squared_to(colors[k]);
  2121. if (d > distance) {
  2122. distance = d;
  2123. further_apart[0] = j;
  2124. further_apart[1] = k;
  2125. }
  2126. }
  2127. }
  2128. from = colors[further_apart[0]];
  2129. to = colors[further_apart[1]];
  2130. } else {
  2131. //if a block is missing, the priority is that this block remains black,
  2132. //otherwise the geometry will appear deformed
  2133. //correct shape wins over correct color in this case
  2134. //average all colors first
  2135. Vector3 average;
  2136. for (int j = 0; j < b.source_count; j++) {
  2137. average += colors[j];
  2138. }
  2139. average.normalize();
  2140. //find max distance in normal from average
  2141. for (int j = 0; j < b.source_count; j++) {
  2142. float d = average.dot(colors[j]);
  2143. distance = MAX(d, distance);
  2144. }
  2145. from = Vector3(); //from black
  2146. to = average * distance;
  2147. //find max distance
  2148. }
  2149. int indices[16];
  2150. uint16_t color_0 = 0;
  2151. color_0 = CLAMP(int(from.x * 31), 0, 31) << 11;
  2152. color_0 |= CLAMP(int(from.y * 63), 0, 63) << 5;
  2153. color_0 |= CLAMP(int(from.z * 31), 0, 31);
  2154. uint16_t color_1 = 0;
  2155. color_1 = CLAMP(int(to.x * 31), 0, 31) << 11;
  2156. color_1 |= CLAMP(int(to.y * 63), 0, 63) << 5;
  2157. color_1 |= CLAMP(int(to.z * 31), 0, 31);
  2158. if (color_1 > color_0) {
  2159. SWAP(color_1, color_0);
  2160. SWAP(from, to);
  2161. }
  2162. if (distance > 0) {
  2163. Vector3 dir = (to - from).normalized();
  2164. for (int j = 0; j < b.source_count; j++) {
  2165. float d = (colors[j] - from).dot(dir) / distance;
  2166. indices[j] = int(d * 3 + 0.5);
  2167. static const int index_swap[4] = { 0, 3, 1, 2 };
  2168. indices[j] = index_swap[CLAMP(indices[j], 0, 3)];
  2169. }
  2170. } else {
  2171. for (int j = 0; j < b.source_count; j++) {
  2172. indices[j] = 0;
  2173. }
  2174. }
  2175. //by default, 1 is black, otherwise it will be overridden by source
  2176. uint32_t index_block[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  2177. for (int j = 0; j < b.source_count; j++) {
  2178. int x = local_data[b.sources[j]].pos[0] % 4;
  2179. int y = local_data[b.sources[j]].pos[1] % 4;
  2180. index_block[y * 4 + x] = indices[j];
  2181. }
  2182. uint32_t encode = 0;
  2183. for (int j = 0; j < 16; j++) {
  2184. encode |= index_block[j] << (j * 2);
  2185. }
  2186. blockptr[8] = color_0 & 0xFF;
  2187. blockptr[9] = (color_0 >> 8) & 0xFF;
  2188. blockptr[10] = color_1 & 0xFF;
  2189. blockptr[11] = (color_1 >> 8) & 0xFF;
  2190. blockptr[12] = encode & 0xFF;
  2191. blockptr[13] = (encode >> 8) & 0xFF;
  2192. blockptr[14] = (encode >> 16) & 0xFF;
  2193. blockptr[15] = (encode >> 24) & 0xFF;
  2194. }
  2195. }
  2196. }
  2197. //send back to main thread to update un little chunks
  2198. probe_data->dynamic.updating_stage = GI_UPDATE_STAGE_UPLOADING;
  2199. }
  2200. bool VisualServerScene::_check_gi_probe(Instance *p_gi_probe) {
  2201. InstanceGIProbeData *probe_data = static_cast<InstanceGIProbeData *>(p_gi_probe->base_data);
  2202. probe_data->dynamic.light_cache_changes.clear();
  2203. bool all_equal = true;
  2204. for (List<Instance *>::Element *E = p_gi_probe->scenario->directional_lights.front(); E; E = E->next()) {
  2205. InstanceGIProbeData::LightCache lc;
  2206. lc.type = VSG::storage->light_get_type(E->get()->base);
  2207. lc.color = VSG::storage->light_get_color(E->get()->base);
  2208. lc.energy = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_ENERGY);
  2209. lc.radius = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_RANGE);
  2210. lc.attenuation = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_ATTENUATION);
  2211. lc.spot_angle = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_SPOT_ANGLE);
  2212. lc.spot_attenuation = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_SPOT_ATTENUATION);
  2213. lc.transform = probe_data->dynamic.light_to_cell_xform * E->get()->transform;
  2214. if (!probe_data->dynamic.light_cache.has(E->get()->self) || !(probe_data->dynamic.light_cache[E->get()->self] == lc)) {
  2215. all_equal = false;
  2216. }
  2217. probe_data->dynamic.light_cache_changes[E->get()->self] = lc;
  2218. }
  2219. for (Set<Instance *>::Element *E = probe_data->lights.front(); E; E = E->next()) {
  2220. InstanceGIProbeData::LightCache lc;
  2221. lc.type = VSG::storage->light_get_type(E->get()->base);
  2222. lc.color = VSG::storage->light_get_color(E->get()->base);
  2223. lc.energy = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_ENERGY);
  2224. lc.radius = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_RANGE);
  2225. lc.attenuation = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_ATTENUATION);
  2226. lc.spot_angle = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_SPOT_ANGLE);
  2227. lc.spot_attenuation = VSG::storage->light_get_param(E->get()->base, VS::LIGHT_PARAM_SPOT_ATTENUATION);
  2228. lc.transform = probe_data->dynamic.light_to_cell_xform * E->get()->transform;
  2229. if (!probe_data->dynamic.light_cache.has(E->get()->self) || !(probe_data->dynamic.light_cache[E->get()->self] == lc)) {
  2230. all_equal = false;
  2231. }
  2232. probe_data->dynamic.light_cache_changes[E->get()->self] = lc;
  2233. }
  2234. //lighting changed from after to before, must do some updating
  2235. return !all_equal || probe_data->dynamic.light_cache_changes.size() != probe_data->dynamic.light_cache.size();
  2236. }
  2237. void VisualServerScene::render_probes() {
  2238. /* REFLECTION PROBES */
  2239. SelfList<InstanceReflectionProbeData> *ref_probe = reflection_probe_render_list.first();
  2240. bool busy = false;
  2241. while (ref_probe) {
  2242. SelfList<InstanceReflectionProbeData> *next = ref_probe->next();
  2243. RID base = ref_probe->self()->owner->base;
  2244. switch (VSG::storage->reflection_probe_get_update_mode(base)) {
  2245. case VS::REFLECTION_PROBE_UPDATE_ONCE: {
  2246. if (busy) //already rendering something
  2247. break;
  2248. bool done = _render_reflection_probe_step(ref_probe->self()->owner, ref_probe->self()->render_step);
  2249. if (done) {
  2250. reflection_probe_render_list.remove(ref_probe);
  2251. } else {
  2252. ref_probe->self()->render_step++;
  2253. }
  2254. busy = true; //do not render another one of this kind
  2255. } break;
  2256. case VS::REFLECTION_PROBE_UPDATE_ALWAYS: {
  2257. int step = 0;
  2258. bool done = false;
  2259. while (!done) {
  2260. done = _render_reflection_probe_step(ref_probe->self()->owner, step);
  2261. step++;
  2262. }
  2263. reflection_probe_render_list.remove(ref_probe);
  2264. } break;
  2265. }
  2266. ref_probe = next;
  2267. }
  2268. /* GI PROBES */
  2269. SelfList<InstanceGIProbeData> *gi_probe = gi_probe_update_list.first();
  2270. while (gi_probe) {
  2271. SelfList<InstanceGIProbeData> *next = gi_probe->next();
  2272. InstanceGIProbeData *probe = gi_probe->self();
  2273. Instance *instance_probe = probe->owner;
  2274. //check if probe must be setup, but don't do if on the lighting thread
  2275. bool force_lighting = false;
  2276. if (probe->invalid || (probe->dynamic.updating_stage == GI_UPDATE_STAGE_CHECK && probe->base_version != VSG::storage->gi_probe_get_version(instance_probe->base))) {
  2277. _setup_gi_probe(instance_probe);
  2278. force_lighting = true;
  2279. }
  2280. float propagate = VSG::storage->gi_probe_get_propagation(instance_probe->base);
  2281. if (probe->dynamic.propagate != propagate) {
  2282. probe->dynamic.propagate = propagate;
  2283. force_lighting = true;
  2284. }
  2285. if (probe->invalid == false && probe->dynamic.enabled) {
  2286. switch (probe->dynamic.updating_stage) {
  2287. case GI_UPDATE_STAGE_CHECK: {
  2288. if (_check_gi_probe(instance_probe) || force_lighting) {
  2289. //send to lighting thread
  2290. probe->dynamic.updating_stage = GI_UPDATE_STAGE_LIGHTING;
  2291. #ifndef NO_THREADS
  2292. probe_bake_mutex->lock();
  2293. probe_bake_list.push_back(instance_probe);
  2294. probe_bake_mutex->unlock();
  2295. probe_bake_sem->post();
  2296. #else
  2297. _bake_gi_probe(instance_probe);
  2298. #endif
  2299. }
  2300. } break;
  2301. case GI_UPDATE_STAGE_LIGHTING: {
  2302. //do none, wait til done!
  2303. } break;
  2304. case GI_UPDATE_STAGE_UPLOADING: {
  2305. uint64_t us = OS::get_singleton()->get_ticks_usec();
  2306. for (int i = 0; i < (int)probe->dynamic.mipmaps_3d.size(); i++) {
  2307. int mmsize = probe->dynamic.mipmaps_3d[i].size();
  2308. PoolVector<uint8_t>::Read r = probe->dynamic.mipmaps_3d[i].read();
  2309. VSG::storage->gi_probe_dynamic_data_update(probe->dynamic.probe_data, 0, probe->dynamic.grid_size[2] >> i, i, r.ptr());
  2310. }
  2311. probe->dynamic.updating_stage = GI_UPDATE_STAGE_CHECK;
  2312. //print_line("UPLOAD TIME: "+rtos((OS::get_singleton()->get_ticks_usec()-us)/1000000.0));
  2313. } break;
  2314. }
  2315. }
  2316. //_update_gi_probe(gi_probe->self()->owner);
  2317. gi_probe = next;
  2318. }
  2319. }
  2320. void VisualServerScene::_update_dirty_instance(Instance *p_instance) {
  2321. if (p_instance->update_aabb)
  2322. _update_instance_aabb(p_instance);
  2323. if (p_instance->update_materials) {
  2324. if (p_instance->base_type == VS::INSTANCE_MESH) {
  2325. //remove materials no longer used and un-own them
  2326. int new_mat_count = VSG::storage->mesh_get_surface_count(p_instance->base);
  2327. for (int i = p_instance->materials.size() - 1; i >= new_mat_count; i--) {
  2328. if (p_instance->materials[i].is_valid()) {
  2329. VSG::storage->material_remove_instance_owner(p_instance->materials[i], p_instance);
  2330. }
  2331. }
  2332. p_instance->materials.resize(new_mat_count);
  2333. int new_blend_shape_count = VSG::storage->mesh_get_blend_shape_count(p_instance->base);
  2334. if (new_blend_shape_count != p_instance->blend_values.size()) {
  2335. p_instance->blend_values.resize(new_blend_shape_count);
  2336. for (int i = 0; i < new_blend_shape_count; i++) {
  2337. p_instance->blend_values[i] = 0;
  2338. }
  2339. }
  2340. }
  2341. if ((1 << p_instance->base_type) & VS::INSTANCE_GEOMETRY_MASK) {
  2342. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  2343. bool can_cast_shadows = true;
  2344. if (p_instance->cast_shadows == VS::SHADOW_CASTING_SETTING_OFF) {
  2345. can_cast_shadows = false;
  2346. } else if (p_instance->material_override.is_valid()) {
  2347. can_cast_shadows = VSG::storage->material_casts_shadows(p_instance->material_override);
  2348. } else {
  2349. if (p_instance->base_type == VS::INSTANCE_MESH) {
  2350. RID mesh = p_instance->base;
  2351. if (mesh.is_valid()) {
  2352. bool cast_shadows = false;
  2353. for (int i = 0; i < p_instance->materials.size(); i++) {
  2354. RID mat = p_instance->materials[i].is_valid() ? p_instance->materials[i] : VSG::storage->mesh_surface_get_material(mesh, i);
  2355. if (!mat.is_valid()) {
  2356. cast_shadows = true;
  2357. break;
  2358. }
  2359. if (VSG::storage->material_casts_shadows(mat)) {
  2360. cast_shadows = true;
  2361. break;
  2362. }
  2363. }
  2364. if (!cast_shadows) {
  2365. can_cast_shadows = false;
  2366. }
  2367. }
  2368. } else if (p_instance->base_type == VS::INSTANCE_MULTIMESH) {
  2369. RID mesh = VSG::storage->multimesh_get_mesh(p_instance->base);
  2370. if (mesh.is_valid()) {
  2371. bool cast_shadows = false;
  2372. int sc = VSG::storage->mesh_get_surface_count(mesh);
  2373. for (int i = 0; i < sc; i++) {
  2374. RID mat = VSG::storage->mesh_surface_get_material(mesh, i);
  2375. if (!mat.is_valid()) {
  2376. cast_shadows = true;
  2377. break;
  2378. }
  2379. if (VSG::storage->material_casts_shadows(mat)) {
  2380. cast_shadows = true;
  2381. break;
  2382. }
  2383. }
  2384. if (!cast_shadows) {
  2385. can_cast_shadows = false;
  2386. }
  2387. }
  2388. } else if (p_instance->base_type == VS::INSTANCE_IMMEDIATE) {
  2389. RID mat = VSG::storage->immediate_get_material(p_instance->base);
  2390. if (!mat.is_valid() || VSG::storage->material_casts_shadows(mat)) {
  2391. can_cast_shadows = true;
  2392. } else {
  2393. can_cast_shadows = false;
  2394. }
  2395. } else if (p_instance->base_type == VS::INSTANCE_PARTICLES) {
  2396. bool cast_shadows = false;
  2397. int dp = VSG::storage->particles_get_draw_passes(p_instance->base);
  2398. for (int i = 0; i < dp; i++) {
  2399. RID mesh = VSG::storage->particles_get_draw_pass_mesh(p_instance->base, i);
  2400. if (!mesh.is_valid())
  2401. continue;
  2402. int sc = VSG::storage->mesh_get_surface_count(mesh);
  2403. for (int j = 0; j < sc; j++) {
  2404. RID mat = VSG::storage->mesh_surface_get_material(mesh, j);
  2405. if (!mat.is_valid()) {
  2406. cast_shadows = true;
  2407. break;
  2408. }
  2409. if (VSG::storage->material_casts_shadows(mat)) {
  2410. cast_shadows = true;
  2411. break;
  2412. }
  2413. }
  2414. }
  2415. if (!cast_shadows) {
  2416. can_cast_shadows = false;
  2417. }
  2418. }
  2419. }
  2420. if (can_cast_shadows != geom->can_cast_shadows) {
  2421. //ability to cast shadows change, let lights now
  2422. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  2423. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  2424. light->shadow_dirty = true;
  2425. }
  2426. geom->can_cast_shadows = can_cast_shadows;
  2427. }
  2428. }
  2429. }
  2430. _update_instance(p_instance);
  2431. p_instance->update_aabb = false;
  2432. p_instance->update_materials = false;
  2433. _instance_update_list.remove(&p_instance->update_item);
  2434. }
  2435. void VisualServerScene::update_dirty_instances() {
  2436. VSG::storage->update_dirty_resources();
  2437. while (_instance_update_list.first()) {
  2438. _update_dirty_instance(_instance_update_list.first()->self());
  2439. }
  2440. }
  2441. bool VisualServerScene::free(RID p_rid) {
  2442. if (camera_owner.owns(p_rid)) {
  2443. Camera *camera = camera_owner.get(p_rid);
  2444. camera_owner.free(p_rid);
  2445. memdelete(camera);
  2446. } else if (scenario_owner.owns(p_rid)) {
  2447. Scenario *scenario = scenario_owner.get(p_rid);
  2448. while (scenario->instances.first()) {
  2449. instance_set_scenario(scenario->instances.first()->self()->self, RID());
  2450. }
  2451. VSG::scene_render->free(scenario->reflection_probe_shadow_atlas);
  2452. VSG::scene_render->free(scenario->reflection_atlas);
  2453. scenario_owner.free(p_rid);
  2454. memdelete(scenario);
  2455. } else if (instance_owner.owns(p_rid)) {
  2456. // delete the instance
  2457. update_dirty_instances();
  2458. Instance *instance = instance_owner.get(p_rid);
  2459. instance_set_room(p_rid, RID());
  2460. instance_set_scenario(p_rid, RID());
  2461. instance_set_base(p_rid, RID());
  2462. instance_geometry_set_material_override(p_rid, RID());
  2463. instance_attach_skeleton(p_rid, RID());
  2464. update_dirty_instances(); //in case something changed this
  2465. instance_owner.free(p_rid);
  2466. memdelete(instance);
  2467. } else {
  2468. return false;
  2469. }
  2470. return true;
  2471. }
  2472. VisualServerScene *VisualServerScene::singleton = NULL;
  2473. VisualServerScene::VisualServerScene() {
  2474. #ifndef NO_THREADS
  2475. probe_bake_sem = Semaphore::create();
  2476. probe_bake_mutex = Mutex::create();
  2477. probe_bake_thread = Thread::create(_gi_probe_bake_threads, this);
  2478. probe_bake_thread_exit = false;
  2479. #endif
  2480. render_pass = 1;
  2481. singleton = this;
  2482. }
  2483. VisualServerScene::~VisualServerScene() {
  2484. #ifndef NO_THREADS
  2485. probe_bake_thread_exit = true;
  2486. Thread::wait_to_finish(probe_bake_thread);
  2487. memdelete(probe_bake_thread);
  2488. memdelete(probe_bake_sem);
  2489. memdelete(probe_bake_mutex);
  2490. #endif
  2491. }