rendering_server_scene_raster.cpp 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072
  1. /*************************************************************************/
  2. /* rendering_server_scene_raster.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "rendering_server_scene_raster.h"
  31. #include "core/os/os.h"
  32. #include "rendering_server_default.h"
  33. #include "rendering_server_globals.h"
  34. #include <new>
  35. /* CAMERA API */
  36. RID RenderingServerSceneRaster::camera_create() {
  37. Camera *camera = memnew(Camera);
  38. return camera_owner.make_rid(camera);
  39. }
  40. void RenderingServerSceneRaster::camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far) {
  41. Camera *camera = camera_owner.getornull(p_camera);
  42. ERR_FAIL_COND(!camera);
  43. camera->type = Camera::PERSPECTIVE;
  44. camera->fov = p_fovy_degrees;
  45. camera->znear = p_z_near;
  46. camera->zfar = p_z_far;
  47. }
  48. void RenderingServerSceneRaster::camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far) {
  49. Camera *camera = camera_owner.getornull(p_camera);
  50. ERR_FAIL_COND(!camera);
  51. camera->type = Camera::ORTHOGONAL;
  52. camera->size = p_size;
  53. camera->znear = p_z_near;
  54. camera->zfar = p_z_far;
  55. }
  56. void RenderingServerSceneRaster::camera_set_frustum(RID p_camera, float p_size, Vector2 p_offset, float p_z_near, float p_z_far) {
  57. Camera *camera = camera_owner.getornull(p_camera);
  58. ERR_FAIL_COND(!camera);
  59. camera->type = Camera::FRUSTUM;
  60. camera->size = p_size;
  61. camera->offset = p_offset;
  62. camera->znear = p_z_near;
  63. camera->zfar = p_z_far;
  64. }
  65. void RenderingServerSceneRaster::camera_set_transform(RID p_camera, const Transform &p_transform) {
  66. Camera *camera = camera_owner.getornull(p_camera);
  67. ERR_FAIL_COND(!camera);
  68. camera->transform = p_transform.orthonormalized();
  69. }
  70. void RenderingServerSceneRaster::camera_set_cull_mask(RID p_camera, uint32_t p_layers) {
  71. Camera *camera = camera_owner.getornull(p_camera);
  72. ERR_FAIL_COND(!camera);
  73. camera->visible_layers = p_layers;
  74. }
  75. void RenderingServerSceneRaster::camera_set_environment(RID p_camera, RID p_env) {
  76. Camera *camera = camera_owner.getornull(p_camera);
  77. ERR_FAIL_COND(!camera);
  78. camera->env = p_env;
  79. }
  80. void RenderingServerSceneRaster::camera_set_camera_effects(RID p_camera, RID p_fx) {
  81. Camera *camera = camera_owner.getornull(p_camera);
  82. ERR_FAIL_COND(!camera);
  83. camera->effects = p_fx;
  84. }
  85. void RenderingServerSceneRaster::camera_set_use_vertical_aspect(RID p_camera, bool p_enable) {
  86. Camera *camera = camera_owner.getornull(p_camera);
  87. ERR_FAIL_COND(!camera);
  88. camera->vaspect = p_enable;
  89. }
  90. bool RenderingServerSceneRaster::is_camera(RID p_camera) const {
  91. return camera_owner.owns(p_camera);
  92. }
  93. /* SCENARIO API */
  94. void *RenderingServerSceneRaster::_instance_pair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int) {
  95. //RenderingServerSceneRaster *self = (RenderingServerSceneRaster*)p_self;
  96. Instance *A = p_A;
  97. Instance *B = p_B;
  98. //instance indices are designed so greater always contains lesser
  99. if (A->base_type > B->base_type) {
  100. SWAP(A, B); //lesser always first
  101. }
  102. if (B->base_type == RS::INSTANCE_LIGHT && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  103. InstanceLightData *light = static_cast<InstanceLightData *>(B->base_data);
  104. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  105. InstanceLightData::PairInfo pinfo;
  106. pinfo.geometry = A;
  107. pinfo.L = geom->lighting.push_back(B);
  108. List<InstanceLightData::PairInfo>::Element *E = light->geometries.push_back(pinfo);
  109. if (geom->can_cast_shadows) {
  110. light->shadow_dirty = true;
  111. }
  112. geom->lighting_dirty = true;
  113. return E; //this element should make freeing faster
  114. } else if (B->base_type == RS::INSTANCE_REFLECTION_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  115. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(B->base_data);
  116. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  117. InstanceReflectionProbeData::PairInfo pinfo;
  118. pinfo.geometry = A;
  119. pinfo.L = geom->reflection_probes.push_back(B);
  120. List<InstanceReflectionProbeData::PairInfo>::Element *E = reflection_probe->geometries.push_back(pinfo);
  121. geom->reflection_dirty = true;
  122. return E; //this element should make freeing faster
  123. } else if (B->base_type == RS::INSTANCE_DECAL && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  124. InstanceDecalData *decal = static_cast<InstanceDecalData *>(B->base_data);
  125. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  126. InstanceDecalData::PairInfo pinfo;
  127. pinfo.geometry = A;
  128. pinfo.L = geom->decals.push_back(B);
  129. List<InstanceDecalData::PairInfo>::Element *E = decal->geometries.push_back(pinfo);
  130. geom->decal_dirty = true;
  131. return E; //this element should make freeing faster
  132. } else if (B->base_type == RS::INSTANCE_LIGHTMAP && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  133. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(B->base_data);
  134. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  135. if (A->dynamic_gi) {
  136. InstanceLightmapData::PairInfo pinfo;
  137. pinfo.geometry = A;
  138. pinfo.L = geom->lightmap_captures.push_back(B);
  139. List<InstanceLightmapData::PairInfo>::Element *E = lightmap_data->geometries.push_back(pinfo);
  140. ((RenderingServerSceneRaster *)p_self)->_instance_queue_update(A, false, false); //need to update capture
  141. return E; //this element should make freeing faster
  142. } else {
  143. return nullptr;
  144. }
  145. } else if (B->base_type == RS::INSTANCE_GI_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  146. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  147. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  148. InstanceGIProbeData::PairInfo pinfo;
  149. pinfo.geometry = A;
  150. pinfo.L = geom->gi_probes.push_back(B);
  151. List<InstanceGIProbeData::PairInfo>::Element *E;
  152. if (A->dynamic_gi) {
  153. E = gi_probe->dynamic_geometries.push_back(pinfo);
  154. } else {
  155. E = gi_probe->geometries.push_back(pinfo);
  156. }
  157. geom->gi_probes_dirty = true;
  158. return E; //this element should make freeing faster
  159. } else if (B->base_type == RS::INSTANCE_GI_PROBE && A->base_type == RS::INSTANCE_LIGHT) {
  160. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  161. return gi_probe->lights.insert(A);
  162. } else if (B->base_type == RS::INSTANCE_PARTICLES_COLLISION && A->base_type == RS::INSTANCE_PARTICLES) {
  163. RSG::storage->particles_add_collision(A->base, B);
  164. }
  165. return nullptr;
  166. }
  167. void RenderingServerSceneRaster::_instance_unpair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int, void *udata) {
  168. //RenderingServerSceneRaster *self = (RenderingServerSceneRaster*)p_self;
  169. Instance *A = p_A;
  170. Instance *B = p_B;
  171. //instance indices are designed so greater always contains lesser
  172. if (A->base_type > B->base_type) {
  173. SWAP(A, B); //lesser always first
  174. }
  175. if (B->base_type == RS::INSTANCE_LIGHT && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  176. InstanceLightData *light = static_cast<InstanceLightData *>(B->base_data);
  177. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  178. List<InstanceLightData::PairInfo>::Element *E = reinterpret_cast<List<InstanceLightData::PairInfo>::Element *>(udata);
  179. geom->lighting.erase(E->get().L);
  180. light->geometries.erase(E);
  181. if (geom->can_cast_shadows) {
  182. light->shadow_dirty = true;
  183. }
  184. geom->lighting_dirty = true;
  185. } else if (B->base_type == RS::INSTANCE_REFLECTION_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  186. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(B->base_data);
  187. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  188. List<InstanceReflectionProbeData::PairInfo>::Element *E = reinterpret_cast<List<InstanceReflectionProbeData::PairInfo>::Element *>(udata);
  189. geom->reflection_probes.erase(E->get().L);
  190. reflection_probe->geometries.erase(E);
  191. geom->reflection_dirty = true;
  192. } else if (B->base_type == RS::INSTANCE_DECAL && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  193. InstanceDecalData *decal = static_cast<InstanceDecalData *>(B->base_data);
  194. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  195. List<InstanceDecalData::PairInfo>::Element *E = reinterpret_cast<List<InstanceDecalData::PairInfo>::Element *>(udata);
  196. geom->decals.erase(E->get().L);
  197. decal->geometries.erase(E);
  198. geom->decal_dirty = true;
  199. } else if (B->base_type == RS::INSTANCE_LIGHTMAP && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  200. if (udata) { //only for dynamic geometries
  201. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(B->base_data);
  202. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  203. List<InstanceLightmapData::PairInfo>::Element *E = reinterpret_cast<List<InstanceLightmapData::PairInfo>::Element *>(udata);
  204. geom->lightmap_captures.erase(E->get().L);
  205. lightmap_data->geometries.erase(E);
  206. ((RenderingServerSceneRaster *)p_self)->_instance_queue_update(A, false, false); //need to update capture
  207. }
  208. } else if (B->base_type == RS::INSTANCE_GI_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  209. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  210. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  211. List<InstanceGIProbeData::PairInfo>::Element *E = reinterpret_cast<List<InstanceGIProbeData::PairInfo>::Element *>(udata);
  212. geom->gi_probes.erase(E->get().L);
  213. if (A->dynamic_gi) {
  214. gi_probe->dynamic_geometries.erase(E);
  215. } else {
  216. gi_probe->geometries.erase(E);
  217. }
  218. geom->gi_probes_dirty = true;
  219. } else if (B->base_type == RS::INSTANCE_GI_PROBE && A->base_type == RS::INSTANCE_LIGHT) {
  220. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  221. Set<Instance *>::Element *E = reinterpret_cast<Set<Instance *>::Element *>(udata);
  222. gi_probe->lights.erase(E);
  223. } else if (B->base_type == RS::INSTANCE_PARTICLES_COLLISION && A->base_type == RS::INSTANCE_PARTICLES) {
  224. RSG::storage->particles_remove_collision(A->base, B);
  225. }
  226. }
  227. RID RenderingServerSceneRaster::scenario_create() {
  228. Scenario *scenario = memnew(Scenario);
  229. ERR_FAIL_COND_V(!scenario, RID());
  230. RID scenario_rid = scenario_owner.make_rid(scenario);
  231. scenario->self = scenario_rid;
  232. scenario->octree.set_pair_callback(_instance_pair, this);
  233. scenario->octree.set_unpair_callback(_instance_unpair, this);
  234. scenario->reflection_probe_shadow_atlas = scene_render->shadow_atlas_create();
  235. scene_render->shadow_atlas_set_size(scenario->reflection_probe_shadow_atlas, 1024); //make enough shadows for close distance, don't bother with rest
  236. scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 0, 4);
  237. scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 1, 4);
  238. scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 2, 4);
  239. scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 3, 8);
  240. scenario->reflection_atlas = scene_render->reflection_atlas_create();
  241. return scenario_rid;
  242. }
  243. void RenderingServerSceneRaster::scenario_set_debug(RID p_scenario, RS::ScenarioDebugMode p_debug_mode) {
  244. Scenario *scenario = scenario_owner.getornull(p_scenario);
  245. ERR_FAIL_COND(!scenario);
  246. scenario->debug = p_debug_mode;
  247. }
  248. void RenderingServerSceneRaster::scenario_set_environment(RID p_scenario, RID p_environment) {
  249. Scenario *scenario = scenario_owner.getornull(p_scenario);
  250. ERR_FAIL_COND(!scenario);
  251. scenario->environment = p_environment;
  252. }
  253. void RenderingServerSceneRaster::scenario_set_camera_effects(RID p_scenario, RID p_camera_effects) {
  254. Scenario *scenario = scenario_owner.getornull(p_scenario);
  255. ERR_FAIL_COND(!scenario);
  256. scenario->camera_effects = p_camera_effects;
  257. }
  258. void RenderingServerSceneRaster::scenario_set_fallback_environment(RID p_scenario, RID p_environment) {
  259. Scenario *scenario = scenario_owner.getornull(p_scenario);
  260. ERR_FAIL_COND(!scenario);
  261. scenario->fallback_environment = p_environment;
  262. }
  263. void RenderingServerSceneRaster::scenario_set_reflection_atlas_size(RID p_scenario, int p_reflection_size, int p_reflection_count) {
  264. Scenario *scenario = scenario_owner.getornull(p_scenario);
  265. ERR_FAIL_COND(!scenario);
  266. scene_render->reflection_atlas_set_size(scenario->reflection_atlas, p_reflection_size, p_reflection_count);
  267. }
  268. bool RenderingServerSceneRaster::is_scenario(RID p_scenario) const {
  269. return scenario_owner.owns(p_scenario);
  270. }
  271. RID RenderingServerSceneRaster::scenario_get_environment(RID p_scenario) {
  272. Scenario *scenario = scenario_owner.getornull(p_scenario);
  273. ERR_FAIL_COND_V(!scenario, RID());
  274. return scenario->environment;
  275. }
  276. /* INSTANCING API */
  277. void RenderingServerSceneRaster::_instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_dependencies) {
  278. if (p_update_aabb) {
  279. p_instance->update_aabb = true;
  280. }
  281. if (p_update_dependencies) {
  282. p_instance->update_dependencies = true;
  283. }
  284. if (p_instance->update_item.in_list()) {
  285. return;
  286. }
  287. _instance_update_list.add(&p_instance->update_item);
  288. }
  289. RID RenderingServerSceneRaster::instance_create() {
  290. Instance *instance = memnew(Instance);
  291. ERR_FAIL_COND_V(!instance, RID());
  292. RID instance_rid = instance_owner.make_rid(instance);
  293. instance->self = instance_rid;
  294. return instance_rid;
  295. }
  296. void RenderingServerSceneRaster::instance_set_base(RID p_instance, RID p_base) {
  297. Instance *instance = instance_owner.getornull(p_instance);
  298. ERR_FAIL_COND(!instance);
  299. Scenario *scenario = instance->scenario;
  300. if (instance->base_type != RS::INSTANCE_NONE) {
  301. //free anything related to that base
  302. if (scenario && instance->octree_id) {
  303. scenario->octree.erase(instance->octree_id); //make dependencies generated by the octree go away
  304. instance->octree_id = 0;
  305. }
  306. switch (instance->base_type) {
  307. case RS::INSTANCE_LIGHT: {
  308. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  309. if (scenario && RSG::storage->light_get_type(instance->base) != RS::LIGHT_DIRECTIONAL && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  310. scenario->dynamic_lights.erase(light->instance);
  311. }
  312. #ifdef DEBUG_ENABLED
  313. if (light->geometries.size()) {
  314. ERR_PRINT("BUG, indexing did not unpair geometries from light.");
  315. }
  316. #endif
  317. if (scenario && light->D) {
  318. scenario->directional_lights.erase(light->D);
  319. light->D = nullptr;
  320. }
  321. scene_render->free(light->instance);
  322. } break;
  323. case RS::INSTANCE_REFLECTION_PROBE: {
  324. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(instance->base_data);
  325. scene_render->free(reflection_probe->instance);
  326. if (reflection_probe->update_list.in_list()) {
  327. reflection_probe_render_list.remove(&reflection_probe->update_list);
  328. }
  329. } break;
  330. case RS::INSTANCE_DECAL: {
  331. InstanceDecalData *decal = static_cast<InstanceDecalData *>(instance->base_data);
  332. scene_render->free(decal->instance);
  333. } break;
  334. case RS::INSTANCE_LIGHTMAP: {
  335. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(instance->base_data);
  336. //erase dependencies, since no longer a lightmap
  337. while (lightmap_data->users.front()) {
  338. instance_geometry_set_lightmap(lightmap_data->users.front()->get()->self, RID(), Rect2(), 0);
  339. }
  340. } break;
  341. case RS::INSTANCE_GI_PROBE: {
  342. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(instance->base_data);
  343. #ifdef DEBUG_ENABLED
  344. if (gi_probe->geometries.size()) {
  345. ERR_PRINT("BUG, indexing did not unpair geometries from GIProbe.");
  346. }
  347. #endif
  348. #ifdef DEBUG_ENABLED
  349. if (gi_probe->lights.size()) {
  350. ERR_PRINT("BUG, indexing did not unpair lights from GIProbe.");
  351. }
  352. #endif
  353. if (gi_probe->update_element.in_list()) {
  354. gi_probe_update_list.remove(&gi_probe->update_element);
  355. }
  356. scene_render->free(gi_probe->probe_instance);
  357. } break;
  358. default: {
  359. }
  360. }
  361. if (instance->base_data) {
  362. memdelete(instance->base_data);
  363. instance->base_data = nullptr;
  364. }
  365. instance->blend_values.clear();
  366. instance->materials.clear();
  367. }
  368. instance->base_type = RS::INSTANCE_NONE;
  369. instance->base = RID();
  370. if (p_base.is_valid()) {
  371. instance->base_type = RSG::storage->get_base_type(p_base);
  372. ERR_FAIL_COND(instance->base_type == RS::INSTANCE_NONE);
  373. switch (instance->base_type) {
  374. case RS::INSTANCE_LIGHT: {
  375. InstanceLightData *light = memnew(InstanceLightData);
  376. if (scenario && RSG::storage->light_get_type(p_base) == RS::LIGHT_DIRECTIONAL) {
  377. light->D = scenario->directional_lights.push_back(instance);
  378. }
  379. light->instance = scene_render->light_instance_create(p_base);
  380. instance->base_data = light;
  381. } break;
  382. case RS::INSTANCE_MESH:
  383. case RS::INSTANCE_MULTIMESH:
  384. case RS::INSTANCE_IMMEDIATE:
  385. case RS::INSTANCE_PARTICLES: {
  386. InstanceGeometryData *geom = memnew(InstanceGeometryData);
  387. instance->base_data = geom;
  388. if (instance->base_type == RS::INSTANCE_MESH) {
  389. instance->blend_values.resize(RSG::storage->mesh_get_blend_shape_count(p_base));
  390. }
  391. } break;
  392. case RS::INSTANCE_REFLECTION_PROBE: {
  393. InstanceReflectionProbeData *reflection_probe = memnew(InstanceReflectionProbeData);
  394. reflection_probe->owner = instance;
  395. instance->base_data = reflection_probe;
  396. reflection_probe->instance = scene_render->reflection_probe_instance_create(p_base);
  397. } break;
  398. case RS::INSTANCE_DECAL: {
  399. InstanceDecalData *decal = memnew(InstanceDecalData);
  400. decal->owner = instance;
  401. instance->base_data = decal;
  402. decal->instance = scene_render->decal_instance_create(p_base);
  403. } break;
  404. case RS::INSTANCE_LIGHTMAP: {
  405. InstanceLightmapData *lightmap_data = memnew(InstanceLightmapData);
  406. instance->base_data = lightmap_data;
  407. //lightmap_data->instance = scene_render->lightmap_data_instance_create(p_base);
  408. } break;
  409. case RS::INSTANCE_GI_PROBE: {
  410. InstanceGIProbeData *gi_probe = memnew(InstanceGIProbeData);
  411. instance->base_data = gi_probe;
  412. gi_probe->owner = instance;
  413. if (scenario && !gi_probe->update_element.in_list()) {
  414. gi_probe_update_list.add(&gi_probe->update_element);
  415. }
  416. gi_probe->probe_instance = scene_render->gi_probe_instance_create(p_base);
  417. } break;
  418. default: {
  419. }
  420. }
  421. instance->base = p_base;
  422. //forcefully update the dependency now, so if for some reason it gets removed, we can immediately clear it
  423. RSG::storage->base_update_dependency(p_base, instance);
  424. }
  425. _instance_queue_update(instance, true, true);
  426. }
  427. void RenderingServerSceneRaster::instance_set_scenario(RID p_instance, RID p_scenario) {
  428. Instance *instance = instance_owner.getornull(p_instance);
  429. ERR_FAIL_COND(!instance);
  430. if (instance->scenario) {
  431. instance->scenario->instances.remove(&instance->scenario_item);
  432. if (instance->octree_id) {
  433. instance->scenario->octree.erase(instance->octree_id); //make dependencies generated by the octree go away
  434. instance->octree_id = 0;
  435. }
  436. switch (instance->base_type) {
  437. case RS::INSTANCE_LIGHT: {
  438. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  439. #ifdef DEBUG_ENABLED
  440. if (light->geometries.size()) {
  441. ERR_PRINT("BUG, indexing did not unpair geometries from light.");
  442. }
  443. #endif
  444. if (light->D) {
  445. instance->scenario->directional_lights.erase(light->D);
  446. light->D = nullptr;
  447. }
  448. } break;
  449. case RS::INSTANCE_REFLECTION_PROBE: {
  450. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(instance->base_data);
  451. scene_render->reflection_probe_release_atlas_index(reflection_probe->instance);
  452. } break;
  453. case RS::INSTANCE_PARTICLES_COLLISION: {
  454. heightfield_particle_colliders_update_list.erase(instance);
  455. } break;
  456. case RS::INSTANCE_GI_PROBE: {
  457. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(instance->base_data);
  458. #ifdef DEBUG_ENABLED
  459. if (gi_probe->geometries.size()) {
  460. ERR_PRINT("BUG, indexing did not unpair geometries from GIProbe.");
  461. }
  462. #endif
  463. #ifdef DEBUG_ENABLED
  464. if (gi_probe->lights.size()) {
  465. ERR_PRINT("BUG, indexing did not unpair lights from GIProbe.");
  466. }
  467. #endif
  468. if (gi_probe->update_element.in_list()) {
  469. gi_probe_update_list.remove(&gi_probe->update_element);
  470. }
  471. } break;
  472. default: {
  473. }
  474. }
  475. instance->scenario = nullptr;
  476. }
  477. if (p_scenario.is_valid()) {
  478. Scenario *scenario = scenario_owner.getornull(p_scenario);
  479. ERR_FAIL_COND(!scenario);
  480. instance->scenario = scenario;
  481. scenario->instances.add(&instance->scenario_item);
  482. switch (instance->base_type) {
  483. case RS::INSTANCE_LIGHT: {
  484. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  485. if (RSG::storage->light_get_type(instance->base) == RS::LIGHT_DIRECTIONAL) {
  486. light->D = scenario->directional_lights.push_back(instance);
  487. }
  488. } break;
  489. case RS::INSTANCE_GI_PROBE: {
  490. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(instance->base_data);
  491. if (!gi_probe->update_element.in_list()) {
  492. gi_probe_update_list.add(&gi_probe->update_element);
  493. }
  494. } break;
  495. default: {
  496. }
  497. }
  498. _instance_queue_update(instance, true, true);
  499. }
  500. }
  501. void RenderingServerSceneRaster::instance_set_layer_mask(RID p_instance, uint32_t p_mask) {
  502. Instance *instance = instance_owner.getornull(p_instance);
  503. ERR_FAIL_COND(!instance);
  504. instance->layer_mask = p_mask;
  505. }
  506. void RenderingServerSceneRaster::instance_set_transform(RID p_instance, const Transform &p_transform) {
  507. Instance *instance = instance_owner.getornull(p_instance);
  508. ERR_FAIL_COND(!instance);
  509. if (instance->transform == p_transform) {
  510. return; //must be checked to avoid worst evil
  511. }
  512. #ifdef DEBUG_ENABLED
  513. for (int i = 0; i < 4; i++) {
  514. const Vector3 &v = i < 3 ? p_transform.basis.elements[i] : p_transform.origin;
  515. ERR_FAIL_COND(Math::is_inf(v.x));
  516. ERR_FAIL_COND(Math::is_nan(v.x));
  517. ERR_FAIL_COND(Math::is_inf(v.y));
  518. ERR_FAIL_COND(Math::is_nan(v.y));
  519. ERR_FAIL_COND(Math::is_inf(v.z));
  520. ERR_FAIL_COND(Math::is_nan(v.z));
  521. }
  522. #endif
  523. instance->transform = p_transform;
  524. _instance_queue_update(instance, true);
  525. }
  526. void RenderingServerSceneRaster::instance_attach_object_instance_id(RID p_instance, ObjectID p_id) {
  527. Instance *instance = instance_owner.getornull(p_instance);
  528. ERR_FAIL_COND(!instance);
  529. instance->object_id = p_id;
  530. }
  531. void RenderingServerSceneRaster::instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight) {
  532. Instance *instance = instance_owner.getornull(p_instance);
  533. ERR_FAIL_COND(!instance);
  534. if (instance->update_item.in_list()) {
  535. _update_dirty_instance(instance);
  536. }
  537. ERR_FAIL_INDEX(p_shape, instance->blend_values.size());
  538. instance->blend_values.write[p_shape] = p_weight;
  539. }
  540. void RenderingServerSceneRaster::instance_set_surface_material(RID p_instance, int p_surface, RID p_material) {
  541. Instance *instance = instance_owner.getornull(p_instance);
  542. ERR_FAIL_COND(!instance);
  543. if (instance->base_type == RS::INSTANCE_MESH) {
  544. //may not have been updated yet, may also have not been set yet. When updated will be correcte, worst case
  545. instance->materials.resize(MAX(p_surface + 1, RSG::storage->mesh_get_surface_count(instance->base)));
  546. }
  547. ERR_FAIL_INDEX(p_surface, instance->materials.size());
  548. instance->materials.write[p_surface] = p_material;
  549. _instance_queue_update(instance, false, true);
  550. }
  551. void RenderingServerSceneRaster::instance_set_visible(RID p_instance, bool p_visible) {
  552. Instance *instance = instance_owner.getornull(p_instance);
  553. ERR_FAIL_COND(!instance);
  554. if (instance->visible == p_visible) {
  555. return;
  556. }
  557. instance->visible = p_visible;
  558. switch (instance->base_type) {
  559. case RS::INSTANCE_LIGHT: {
  560. if (RSG::storage->light_get_type(instance->base) != RS::LIGHT_DIRECTIONAL && instance->octree_id && instance->scenario) {
  561. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_LIGHT, p_visible ? RS::INSTANCE_GEOMETRY_MASK : 0);
  562. }
  563. } break;
  564. case RS::INSTANCE_REFLECTION_PROBE: {
  565. if (instance->octree_id && instance->scenario) {
  566. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_REFLECTION_PROBE, p_visible ? RS::INSTANCE_GEOMETRY_MASK : 0);
  567. }
  568. } break;
  569. case RS::INSTANCE_DECAL: {
  570. if (instance->octree_id && instance->scenario) {
  571. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_DECAL, p_visible ? RS::INSTANCE_GEOMETRY_MASK : 0);
  572. }
  573. } break;
  574. case RS::INSTANCE_LIGHTMAP: {
  575. if (instance->octree_id && instance->scenario) {
  576. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_LIGHTMAP, p_visible ? RS::INSTANCE_GEOMETRY_MASK : 0);
  577. }
  578. } break;
  579. case RS::INSTANCE_GI_PROBE: {
  580. if (instance->octree_id && instance->scenario) {
  581. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_GI_PROBE, p_visible ? (RS::INSTANCE_GEOMETRY_MASK | (1 << RS::INSTANCE_LIGHT)) : 0);
  582. }
  583. } break;
  584. case RS::INSTANCE_PARTICLES_COLLISION: {
  585. if (instance->octree_id && instance->scenario) {
  586. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_PARTICLES_COLLISION, p_visible ? (1 << RS::INSTANCE_PARTICLES) : 0);
  587. }
  588. } break;
  589. default: {
  590. }
  591. }
  592. }
  593. inline bool is_geometry_instance(RenderingServer::InstanceType p_type) {
  594. return p_type == RS::INSTANCE_MESH || p_type == RS::INSTANCE_MULTIMESH || p_type == RS::INSTANCE_PARTICLES || p_type == RS::INSTANCE_IMMEDIATE;
  595. }
  596. void RenderingServerSceneRaster::instance_set_custom_aabb(RID p_instance, AABB p_aabb) {
  597. Instance *instance = instance_owner.getornull(p_instance);
  598. ERR_FAIL_COND(!instance);
  599. ERR_FAIL_COND(!is_geometry_instance(instance->base_type));
  600. if (p_aabb != AABB()) {
  601. // Set custom AABB
  602. if (instance->custom_aabb == nullptr) {
  603. instance->custom_aabb = memnew(AABB);
  604. }
  605. *instance->custom_aabb = p_aabb;
  606. } else {
  607. // Clear custom AABB
  608. if (instance->custom_aabb != nullptr) {
  609. memdelete(instance->custom_aabb);
  610. instance->custom_aabb = nullptr;
  611. }
  612. }
  613. if (instance->scenario) {
  614. _instance_queue_update(instance, true, false);
  615. }
  616. }
  617. void RenderingServerSceneRaster::instance_attach_skeleton(RID p_instance, RID p_skeleton) {
  618. Instance *instance = instance_owner.getornull(p_instance);
  619. ERR_FAIL_COND(!instance);
  620. if (instance->skeleton == p_skeleton) {
  621. return;
  622. }
  623. instance->skeleton = p_skeleton;
  624. if (p_skeleton.is_valid()) {
  625. //update the dependency now, so if cleared, we remove it
  626. RSG::storage->skeleton_update_dependency(p_skeleton, instance);
  627. }
  628. _instance_queue_update(instance, true, true);
  629. }
  630. void RenderingServerSceneRaster::instance_set_exterior(RID p_instance, bool p_enabled) {
  631. }
  632. void RenderingServerSceneRaster::instance_set_extra_visibility_margin(RID p_instance, real_t p_margin) {
  633. Instance *instance = instance_owner.getornull(p_instance);
  634. ERR_FAIL_COND(!instance);
  635. instance->extra_margin = p_margin;
  636. _instance_queue_update(instance, true, false);
  637. }
  638. Vector<ObjectID> RenderingServerSceneRaster::instances_cull_aabb(const AABB &p_aabb, RID p_scenario) const {
  639. Vector<ObjectID> instances;
  640. Scenario *scenario = scenario_owner.getornull(p_scenario);
  641. ERR_FAIL_COND_V(!scenario, instances);
  642. const_cast<RenderingServerSceneRaster *>(this)->update_dirty_instances(); // check dirty instances before culling
  643. int culled = 0;
  644. Instance *cull[1024];
  645. culled = scenario->octree.cull_aabb(p_aabb, cull, 1024);
  646. for (int i = 0; i < culled; i++) {
  647. Instance *instance = cull[i];
  648. ERR_CONTINUE(!instance);
  649. if (instance->object_id.is_null()) {
  650. continue;
  651. }
  652. instances.push_back(instance->object_id);
  653. }
  654. return instances;
  655. }
  656. Vector<ObjectID> RenderingServerSceneRaster::instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario) const {
  657. Vector<ObjectID> instances;
  658. Scenario *scenario = scenario_owner.getornull(p_scenario);
  659. ERR_FAIL_COND_V(!scenario, instances);
  660. const_cast<RenderingServerSceneRaster *>(this)->update_dirty_instances(); // check dirty instances before culling
  661. int culled = 0;
  662. Instance *cull[1024];
  663. culled = scenario->octree.cull_segment(p_from, p_from + p_to * 10000, cull, 1024);
  664. for (int i = 0; i < culled; i++) {
  665. Instance *instance = cull[i];
  666. ERR_CONTINUE(!instance);
  667. if (instance->object_id.is_null()) {
  668. continue;
  669. }
  670. instances.push_back(instance->object_id);
  671. }
  672. return instances;
  673. }
  674. Vector<ObjectID> RenderingServerSceneRaster::instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario) const {
  675. Vector<ObjectID> instances;
  676. Scenario *scenario = scenario_owner.getornull(p_scenario);
  677. ERR_FAIL_COND_V(!scenario, instances);
  678. const_cast<RenderingServerSceneRaster *>(this)->update_dirty_instances(); // check dirty instances before culling
  679. int culled = 0;
  680. Instance *cull[1024];
  681. culled = scenario->octree.cull_convex(p_convex, cull, 1024);
  682. for (int i = 0; i < culled; i++) {
  683. Instance *instance = cull[i];
  684. ERR_CONTINUE(!instance);
  685. if (instance->object_id.is_null()) {
  686. continue;
  687. }
  688. instances.push_back(instance->object_id);
  689. }
  690. return instances;
  691. }
  692. void RenderingServerSceneRaster::instance_geometry_set_flag(RID p_instance, RS::InstanceFlags p_flags, bool p_enabled) {
  693. Instance *instance = instance_owner.getornull(p_instance);
  694. ERR_FAIL_COND(!instance);
  695. //ERR_FAIL_COND(((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK));
  696. switch (p_flags) {
  697. case RS::INSTANCE_FLAG_USE_BAKED_LIGHT: {
  698. instance->baked_light = p_enabled;
  699. } break;
  700. case RS::INSTANCE_FLAG_USE_DYNAMIC_GI: {
  701. if (p_enabled == instance->dynamic_gi) {
  702. //bye, redundant
  703. return;
  704. }
  705. if (instance->octree_id != 0) {
  706. //remove from octree, it needs to be re-paired
  707. instance->scenario->octree.erase(instance->octree_id);
  708. instance->octree_id = 0;
  709. _instance_queue_update(instance, true, true);
  710. }
  711. //once out of octree, can be changed
  712. instance->dynamic_gi = p_enabled;
  713. } break;
  714. case RS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE: {
  715. instance->redraw_if_visible = p_enabled;
  716. } break;
  717. default: {
  718. }
  719. }
  720. }
  721. void RenderingServerSceneRaster::instance_geometry_set_cast_shadows_setting(RID p_instance, RS::ShadowCastingSetting p_shadow_casting_setting) {
  722. Instance *instance = instance_owner.getornull(p_instance);
  723. ERR_FAIL_COND(!instance);
  724. instance->cast_shadows = p_shadow_casting_setting;
  725. _instance_queue_update(instance, false, true);
  726. }
  727. void RenderingServerSceneRaster::instance_geometry_set_material_override(RID p_instance, RID p_material) {
  728. Instance *instance = instance_owner.getornull(p_instance);
  729. ERR_FAIL_COND(!instance);
  730. instance->material_override = p_material;
  731. _instance_queue_update(instance, false, true);
  732. }
  733. void RenderingServerSceneRaster::instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin) {
  734. }
  735. void RenderingServerSceneRaster::instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance) {
  736. }
  737. void RenderingServerSceneRaster::instance_geometry_set_lightmap(RID p_instance, RID p_lightmap, const Rect2 &p_lightmap_uv_scale, int p_slice_index) {
  738. Instance *instance = instance_owner.getornull(p_instance);
  739. ERR_FAIL_COND(!instance);
  740. if (instance->lightmap) {
  741. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(((Instance *)instance->lightmap)->base_data);
  742. lightmap_data->users.erase(instance);
  743. instance->lightmap = nullptr;
  744. }
  745. Instance *lightmap_instance = instance_owner.getornull(p_lightmap);
  746. instance->lightmap = lightmap_instance;
  747. instance->lightmap_uv_scale = p_lightmap_uv_scale;
  748. instance->lightmap_slice_index = p_slice_index;
  749. if (lightmap_instance) {
  750. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(lightmap_instance->base_data);
  751. lightmap_data->users.insert(instance);
  752. }
  753. }
  754. void RenderingServerSceneRaster::instance_geometry_set_shader_parameter(RID p_instance, const StringName &p_parameter, const Variant &p_value) {
  755. Instance *instance = instance_owner.getornull(p_instance);
  756. ERR_FAIL_COND(!instance);
  757. Map<StringName, RasterizerScene::InstanceBase::InstanceShaderParameter>::Element *E = instance->instance_shader_parameters.find(p_parameter);
  758. if (!E) {
  759. RasterizerScene::InstanceBase::InstanceShaderParameter isp;
  760. isp.index = -1;
  761. isp.info = PropertyInfo();
  762. isp.value = p_value;
  763. instance->instance_shader_parameters[p_parameter] = isp;
  764. } else {
  765. E->get().value = p_value;
  766. if (E->get().index >= 0 && instance->instance_allocated_shader_parameters) {
  767. //update directly
  768. RSG::storage->global_variables_instance_update(p_instance, E->get().index, p_value);
  769. }
  770. }
  771. }
  772. Variant RenderingServerSceneRaster::instance_geometry_get_shader_parameter(RID p_instance, const StringName &p_parameter) const {
  773. const Instance *instance = const_cast<RenderingServerSceneRaster *>(this)->instance_owner.getornull(p_instance);
  774. ERR_FAIL_COND_V(!instance, Variant());
  775. if (instance->instance_shader_parameters.has(p_parameter)) {
  776. return instance->instance_shader_parameters[p_parameter].value;
  777. }
  778. return Variant();
  779. }
  780. Variant RenderingServerSceneRaster::instance_geometry_get_shader_parameter_default_value(RID p_instance, const StringName &p_parameter) const {
  781. const Instance *instance = const_cast<RenderingServerSceneRaster *>(this)->instance_owner.getornull(p_instance);
  782. ERR_FAIL_COND_V(!instance, Variant());
  783. if (instance->instance_shader_parameters.has(p_parameter)) {
  784. return instance->instance_shader_parameters[p_parameter].default_value;
  785. }
  786. return Variant();
  787. }
  788. void RenderingServerSceneRaster::instance_geometry_get_shader_parameter_list(RID p_instance, List<PropertyInfo> *p_parameters) const {
  789. const Instance *instance = const_cast<RenderingServerSceneRaster *>(this)->instance_owner.getornull(p_instance);
  790. ERR_FAIL_COND(!instance);
  791. const_cast<RenderingServerSceneRaster *>(this)->update_dirty_instances();
  792. Vector<StringName> names;
  793. for (Map<StringName, RasterizerScene::InstanceBase::InstanceShaderParameter>::Element *E = instance->instance_shader_parameters.front(); E; E = E->next()) {
  794. names.push_back(E->key());
  795. }
  796. names.sort_custom<StringName::AlphCompare>();
  797. for (int i = 0; i < names.size(); i++) {
  798. PropertyInfo pinfo = instance->instance_shader_parameters[names[i]].info;
  799. p_parameters->push_back(pinfo);
  800. }
  801. }
  802. void RenderingServerSceneRaster::_update_instance(Instance *p_instance) {
  803. p_instance->version++;
  804. if (p_instance->base_type == RS::INSTANCE_LIGHT) {
  805. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  806. scene_render->light_instance_set_transform(light->instance, p_instance->transform);
  807. scene_render->light_instance_set_aabb(light->instance, p_instance->transform.xform(p_instance->aabb));
  808. light->shadow_dirty = true;
  809. RS::LightBakeMode bake_mode = RSG::storage->light_get_bake_mode(p_instance->base);
  810. if (RSG::storage->light_get_type(p_instance->base) != RS::LIGHT_DIRECTIONAL && bake_mode != light->bake_mode) {
  811. if (p_instance->scenario && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  812. p_instance->scenario->dynamic_lights.erase(light->instance);
  813. }
  814. light->bake_mode = bake_mode;
  815. if (p_instance->scenario && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  816. p_instance->scenario->dynamic_lights.push_back(light->instance);
  817. }
  818. }
  819. uint32_t max_sdfgi_cascade = RSG::storage->light_get_max_sdfgi_cascade(p_instance->base);
  820. if (light->max_sdfgi_cascade != max_sdfgi_cascade) {
  821. light->max_sdfgi_cascade = max_sdfgi_cascade; //should most likely make sdfgi dirty in scenario
  822. }
  823. }
  824. if (p_instance->base_type == RS::INSTANCE_REFLECTION_PROBE) {
  825. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  826. scene_render->reflection_probe_instance_set_transform(reflection_probe->instance, p_instance->transform);
  827. reflection_probe->reflection_dirty = true;
  828. }
  829. if (p_instance->base_type == RS::INSTANCE_DECAL) {
  830. InstanceDecalData *decal = static_cast<InstanceDecalData *>(p_instance->base_data);
  831. scene_render->decal_instance_set_transform(decal->instance, p_instance->transform);
  832. }
  833. if (p_instance->base_type == RS::INSTANCE_GI_PROBE) {
  834. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(p_instance->base_data);
  835. scene_render->gi_probe_instance_set_transform_to_data(gi_probe->probe_instance, p_instance->transform);
  836. }
  837. if (p_instance->base_type == RS::INSTANCE_PARTICLES) {
  838. RSG::storage->particles_set_emission_transform(p_instance->base, p_instance->transform);
  839. }
  840. if (p_instance->base_type == RS::INSTANCE_PARTICLES_COLLISION) {
  841. //remove materials no longer used and un-own them
  842. if (RSG::storage->particles_collision_is_heightfield(p_instance->base)) {
  843. heightfield_particle_colliders_update_list.insert(p_instance);
  844. }
  845. }
  846. if (p_instance->aabb.has_no_surface()) {
  847. return;
  848. }
  849. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  850. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  851. //make sure lights are updated if it casts shadow
  852. if (geom->can_cast_shadows) {
  853. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  854. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  855. light->shadow_dirty = true;
  856. }
  857. }
  858. if (!p_instance->lightmap && geom->lightmap_captures.size()) {
  859. //affected by lightmap captures, must update capture info!
  860. _update_instance_lightmap_captures(p_instance);
  861. } else {
  862. if (!p_instance->lightmap_sh.empty()) {
  863. p_instance->lightmap_sh.clear(); //don't need SH
  864. p_instance->lightmap_target_sh.clear(); //don't need SH
  865. }
  866. }
  867. }
  868. if (p_instance->base_type == RS::INSTANCE_LIGHTMAP) {
  869. //if this moved, update the captured objects
  870. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(p_instance->base_data);
  871. //erase dependencies, since no longer a lightmap
  872. for (List<InstanceLightmapData::PairInfo>::Element *E = lightmap_data->geometries.front(); E; E = E->next()) {
  873. Instance *geom = E->get().geometry;
  874. _instance_queue_update(geom, true, false);
  875. }
  876. }
  877. p_instance->mirror = p_instance->transform.basis.determinant() < 0.0;
  878. AABB new_aabb;
  879. new_aabb = p_instance->transform.xform(p_instance->aabb);
  880. p_instance->transformed_aabb = new_aabb;
  881. if (!p_instance->scenario) {
  882. return;
  883. }
  884. if (p_instance->octree_id == 0) {
  885. uint32_t base_type = 1 << p_instance->base_type;
  886. uint32_t pairable_mask = 0;
  887. bool pairable = false;
  888. if (p_instance->base_type == RS::INSTANCE_LIGHT || p_instance->base_type == RS::INSTANCE_REFLECTION_PROBE || p_instance->base_type == RS::INSTANCE_DECAL || p_instance->base_type == RS::INSTANCE_LIGHTMAP) {
  889. pairable_mask = p_instance->visible ? RS::INSTANCE_GEOMETRY_MASK : 0;
  890. pairable = true;
  891. }
  892. if (p_instance->base_type == RS::INSTANCE_PARTICLES_COLLISION) {
  893. pairable_mask = p_instance->visible ? (1 << RS::INSTANCE_PARTICLES) : 0;
  894. pairable = true;
  895. }
  896. if (p_instance->base_type == RS::INSTANCE_GI_PROBE) {
  897. //lights and geometries
  898. pairable_mask = p_instance->visible ? RS::INSTANCE_GEOMETRY_MASK | (1 << RS::INSTANCE_LIGHT) : 0;
  899. pairable = true;
  900. }
  901. // not inside octree
  902. p_instance->octree_id = p_instance->scenario->octree.create(p_instance, new_aabb, 0, pairable, base_type, pairable_mask);
  903. } else {
  904. /*
  905. if (new_aabb==p_instance->data.transformed_aabb)
  906. return;
  907. */
  908. p_instance->scenario->octree.move(p_instance->octree_id, new_aabb);
  909. }
  910. }
  911. void RenderingServerSceneRaster::_update_instance_aabb(Instance *p_instance) {
  912. AABB new_aabb;
  913. ERR_FAIL_COND(p_instance->base_type != RS::INSTANCE_NONE && !p_instance->base.is_valid());
  914. switch (p_instance->base_type) {
  915. case RenderingServer::INSTANCE_NONE: {
  916. // do nothing
  917. } break;
  918. case RenderingServer::INSTANCE_MESH: {
  919. if (p_instance->custom_aabb) {
  920. new_aabb = *p_instance->custom_aabb;
  921. } else {
  922. new_aabb = RSG::storage->mesh_get_aabb(p_instance->base, p_instance->skeleton);
  923. }
  924. } break;
  925. case RenderingServer::INSTANCE_MULTIMESH: {
  926. if (p_instance->custom_aabb) {
  927. new_aabb = *p_instance->custom_aabb;
  928. } else {
  929. new_aabb = RSG::storage->multimesh_get_aabb(p_instance->base);
  930. }
  931. } break;
  932. case RenderingServer::INSTANCE_IMMEDIATE: {
  933. if (p_instance->custom_aabb) {
  934. new_aabb = *p_instance->custom_aabb;
  935. } else {
  936. new_aabb = RSG::storage->immediate_get_aabb(p_instance->base);
  937. }
  938. } break;
  939. case RenderingServer::INSTANCE_PARTICLES: {
  940. if (p_instance->custom_aabb) {
  941. new_aabb = *p_instance->custom_aabb;
  942. } else {
  943. new_aabb = RSG::storage->particles_get_aabb(p_instance->base);
  944. }
  945. } break;
  946. case RenderingServer::INSTANCE_PARTICLES_COLLISION: {
  947. new_aabb = RSG::storage->particles_collision_get_aabb(p_instance->base);
  948. } break;
  949. case RenderingServer::INSTANCE_LIGHT: {
  950. new_aabb = RSG::storage->light_get_aabb(p_instance->base);
  951. } break;
  952. case RenderingServer::INSTANCE_REFLECTION_PROBE: {
  953. new_aabb = RSG::storage->reflection_probe_get_aabb(p_instance->base);
  954. } break;
  955. case RenderingServer::INSTANCE_DECAL: {
  956. new_aabb = RSG::storage->decal_get_aabb(p_instance->base);
  957. } break;
  958. case RenderingServer::INSTANCE_GI_PROBE: {
  959. new_aabb = RSG::storage->gi_probe_get_bounds(p_instance->base);
  960. } break;
  961. case RenderingServer::INSTANCE_LIGHTMAP: {
  962. new_aabb = RSG::storage->lightmap_get_aabb(p_instance->base);
  963. } break;
  964. default: {
  965. }
  966. }
  967. // <Zylann> This is why I didn't re-use Instance::aabb to implement custom AABBs
  968. if (p_instance->extra_margin) {
  969. new_aabb.grow_by(p_instance->extra_margin);
  970. }
  971. p_instance->aabb = new_aabb;
  972. }
  973. void RenderingServerSceneRaster::_update_instance_lightmap_captures(Instance *p_instance) {
  974. bool first_set = p_instance->lightmap_sh.size() == 0;
  975. p_instance->lightmap_sh.resize(9); //using SH
  976. p_instance->lightmap_target_sh.resize(9); //using SH
  977. Color *instance_sh = p_instance->lightmap_target_sh.ptrw();
  978. bool inside = false;
  979. Color accum_sh[9];
  980. float accum_blend = 0.0;
  981. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  982. for (List<Instance *>::Element *E = geom->lightmap_captures.front(); E; E = E->next()) {
  983. Instance *lightmap = E->get();
  984. bool interior = RSG::storage->lightmap_is_interior(lightmap->base);
  985. if (inside && !interior) {
  986. continue; //we are inside, ignore exteriors
  987. }
  988. Transform to_bounds = lightmap->transform.affine_inverse();
  989. Vector3 center = p_instance->transform.xform(p_instance->aabb.position + p_instance->aabb.size * 0.5); //use aabb center
  990. Vector3 lm_pos = to_bounds.xform(center);
  991. AABB bounds = RSG::storage->lightmap_get_aabb(lightmap->base);
  992. if (!bounds.has_point(lm_pos)) {
  993. continue; //not in this lightmap
  994. }
  995. Color sh[9];
  996. RSG::storage->lightmap_tap_sh_light(lightmap->base, lm_pos, sh);
  997. //rotate it
  998. Basis rot = lightmap->transform.basis.orthonormalized();
  999. for (int i = 0; i < 3; i++) {
  1000. float csh[9];
  1001. for (int j = 0; j < 9; j++) {
  1002. csh[j] = sh[j][i];
  1003. }
  1004. rot.rotate_sh(csh);
  1005. for (int j = 0; j < 9; j++) {
  1006. sh[j][i] = csh[j];
  1007. }
  1008. }
  1009. Vector3 inner_pos = ((lm_pos - bounds.position) / bounds.size) * 2.0 - Vector3(1.0, 1.0, 1.0);
  1010. float blend = MAX(inner_pos.x, MAX(inner_pos.y, inner_pos.z));
  1011. //make blend more rounded
  1012. blend = Math::lerp(inner_pos.length(), blend, blend);
  1013. blend *= blend;
  1014. blend = MAX(0.0, 1.0 - blend);
  1015. if (interior && !inside) {
  1016. //do not blend, just replace
  1017. for (int j = 0; j < 9; j++) {
  1018. accum_sh[j] = sh[j] * blend;
  1019. }
  1020. accum_blend = blend;
  1021. inside = true;
  1022. } else {
  1023. for (int j = 0; j < 9; j++) {
  1024. accum_sh[j] += sh[j] * blend;
  1025. }
  1026. accum_blend += blend;
  1027. }
  1028. }
  1029. if (accum_blend > 0.0) {
  1030. for (int j = 0; j < 9; j++) {
  1031. instance_sh[j] = accum_sh[j] / accum_blend;
  1032. if (first_set) {
  1033. p_instance->lightmap_sh.write[j] = instance_sh[j];
  1034. }
  1035. }
  1036. }
  1037. }
  1038. bool RenderingServerSceneRaster::_light_instance_update_shadow(Instance *p_instance, const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_shadow_atlas, Scenario *p_scenario) {
  1039. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  1040. Transform light_transform = p_instance->transform;
  1041. light_transform.orthonormalize(); //scale does not count on lights
  1042. bool animated_material_found = false;
  1043. switch (RSG::storage->light_get_type(p_instance->base)) {
  1044. case RS::LIGHT_DIRECTIONAL: {
  1045. real_t max_distance = p_cam_projection.get_z_far();
  1046. real_t shadow_max = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE);
  1047. if (shadow_max > 0 && !p_cam_orthogonal) { //its impractical (and leads to unwanted behaviors) to set max distance in orthogonal camera
  1048. max_distance = MIN(shadow_max, max_distance);
  1049. }
  1050. max_distance = MAX(max_distance, p_cam_projection.get_z_near() + 0.001);
  1051. real_t min_distance = MIN(p_cam_projection.get_z_near(), max_distance);
  1052. RS::LightDirectionalShadowDepthRangeMode depth_range_mode = RSG::storage->light_directional_get_shadow_depth_range_mode(p_instance->base);
  1053. real_t pancake_size = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE);
  1054. if (depth_range_mode == RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_OPTIMIZED) {
  1055. //optimize min/max
  1056. Vector<Plane> planes = p_cam_projection.get_projection_planes(p_cam_transform);
  1057. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1058. Plane base(p_cam_transform.origin, -p_cam_transform.basis.get_axis(2));
  1059. //check distance max and min
  1060. bool found_items = false;
  1061. real_t z_max = -1e20;
  1062. real_t z_min = 1e20;
  1063. for (int i = 0; i < cull_count; i++) {
  1064. Instance *instance = instance_shadow_cull_result[i];
  1065. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1066. continue;
  1067. }
  1068. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1069. animated_material_found = true;
  1070. }
  1071. real_t max, min;
  1072. instance->transformed_aabb.project_range_in_plane(base, min, max);
  1073. if (max > z_max) {
  1074. z_max = max;
  1075. }
  1076. if (min < z_min) {
  1077. z_min = min;
  1078. }
  1079. found_items = true;
  1080. }
  1081. if (found_items) {
  1082. min_distance = MAX(min_distance, z_min);
  1083. max_distance = MIN(max_distance, z_max);
  1084. }
  1085. }
  1086. real_t range = max_distance - min_distance;
  1087. int splits = 0;
  1088. switch (RSG::storage->light_directional_get_shadow_mode(p_instance->base)) {
  1089. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  1090. splits = 1;
  1091. break;
  1092. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  1093. splits = 2;
  1094. break;
  1095. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  1096. splits = 4;
  1097. break;
  1098. }
  1099. real_t distances[5];
  1100. distances[0] = min_distance;
  1101. for (int i = 0; i < splits; i++) {
  1102. distances[i + 1] = min_distance + RSG::storage->light_get_param(p_instance->base, RS::LightParam(RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET + i)) * range;
  1103. };
  1104. distances[splits] = max_distance;
  1105. real_t texture_size = scene_render->get_directional_light_shadow_size(light->instance);
  1106. bool overlap = RSG::storage->light_directional_get_blend_splits(p_instance->base);
  1107. real_t first_radius = 0.0;
  1108. real_t min_distance_bias_scale = pancake_size > 0 ? distances[1] / 10.0 : 0;
  1109. for (int i = 0; i < splits; i++) {
  1110. RENDER_TIMESTAMP("Culling Directional Light split" + itos(i));
  1111. // setup a camera matrix for that range!
  1112. CameraMatrix camera_matrix;
  1113. real_t aspect = p_cam_projection.get_aspect();
  1114. if (p_cam_orthogonal) {
  1115. Vector2 vp_he = p_cam_projection.get_viewport_half_extents();
  1116. camera_matrix.set_orthogonal(vp_he.y * 2.0, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  1117. } else {
  1118. real_t fov = p_cam_projection.get_fov(); //this is actually yfov, because set aspect tries to keep it
  1119. camera_matrix.set_perspective(fov, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], true);
  1120. }
  1121. //obtain the frustum endpoints
  1122. Vector3 endpoints[8]; // frustum plane endpoints
  1123. bool res = camera_matrix.get_endpoints(p_cam_transform, endpoints);
  1124. ERR_CONTINUE(!res);
  1125. // obtain the light frustm ranges (given endpoints)
  1126. Transform transform = light_transform; //discard scale and stabilize light
  1127. Vector3 x_vec = transform.basis.get_axis(Vector3::AXIS_X).normalized();
  1128. Vector3 y_vec = transform.basis.get_axis(Vector3::AXIS_Y).normalized();
  1129. Vector3 z_vec = transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  1130. //z_vec points agsint the camera, like in default opengl
  1131. real_t x_min = 0.f, x_max = 0.f;
  1132. real_t y_min = 0.f, y_max = 0.f;
  1133. real_t z_min = 0.f, z_max = 0.f;
  1134. // FIXME: z_max_cam is defined, computed, but not used below when setting up
  1135. // ortho_camera. Commented out for now to fix warnings but should be investigated.
  1136. real_t x_min_cam = 0.f, x_max_cam = 0.f;
  1137. real_t y_min_cam = 0.f, y_max_cam = 0.f;
  1138. real_t z_min_cam = 0.f;
  1139. //real_t z_max_cam = 0.f;
  1140. real_t bias_scale = 1.0;
  1141. real_t aspect_bias_scale = 1.0;
  1142. //used for culling
  1143. for (int j = 0; j < 8; j++) {
  1144. real_t d_x = x_vec.dot(endpoints[j]);
  1145. real_t d_y = y_vec.dot(endpoints[j]);
  1146. real_t d_z = z_vec.dot(endpoints[j]);
  1147. if (j == 0 || d_x < x_min) {
  1148. x_min = d_x;
  1149. }
  1150. if (j == 0 || d_x > x_max) {
  1151. x_max = d_x;
  1152. }
  1153. if (j == 0 || d_y < y_min) {
  1154. y_min = d_y;
  1155. }
  1156. if (j == 0 || d_y > y_max) {
  1157. y_max = d_y;
  1158. }
  1159. if (j == 0 || d_z < z_min) {
  1160. z_min = d_z;
  1161. }
  1162. if (j == 0 || d_z > z_max) {
  1163. z_max = d_z;
  1164. }
  1165. }
  1166. real_t radius = 0;
  1167. real_t soft_shadow_expand = 0;
  1168. Vector3 center;
  1169. {
  1170. //camera viewport stuff
  1171. for (int j = 0; j < 8; j++) {
  1172. center += endpoints[j];
  1173. }
  1174. center /= 8.0;
  1175. //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;
  1176. for (int j = 0; j < 8; j++) {
  1177. real_t d = center.distance_to(endpoints[j]);
  1178. if (d > radius) {
  1179. radius = d;
  1180. }
  1181. }
  1182. radius *= texture_size / (texture_size - 2.0); //add a texel by each side
  1183. if (i == 0) {
  1184. first_radius = radius;
  1185. } else {
  1186. bias_scale = radius / first_radius;
  1187. }
  1188. z_min_cam = z_vec.dot(center) - radius;
  1189. {
  1190. float soft_shadow_angle = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SIZE);
  1191. if (soft_shadow_angle > 0.0 && pancake_size > 0.0) {
  1192. float z_range = (z_vec.dot(center) + radius + pancake_size) - z_min_cam;
  1193. soft_shadow_expand = Math::tan(Math::deg2rad(soft_shadow_angle)) * z_range;
  1194. x_max += soft_shadow_expand;
  1195. y_max += soft_shadow_expand;
  1196. x_min -= soft_shadow_expand;
  1197. y_min -= soft_shadow_expand;
  1198. }
  1199. }
  1200. x_max_cam = x_vec.dot(center) + radius + soft_shadow_expand;
  1201. x_min_cam = x_vec.dot(center) - radius - soft_shadow_expand;
  1202. y_max_cam = y_vec.dot(center) + radius + soft_shadow_expand;
  1203. y_min_cam = y_vec.dot(center) - radius - soft_shadow_expand;
  1204. if (depth_range_mode == RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE) {
  1205. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  1206. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  1207. real_t unit = radius * 2.0 / texture_size;
  1208. x_max_cam = Math::stepify(x_max_cam, unit);
  1209. x_min_cam = Math::stepify(x_min_cam, unit);
  1210. y_max_cam = Math::stepify(y_max_cam, unit);
  1211. y_min_cam = Math::stepify(y_min_cam, unit);
  1212. }
  1213. }
  1214. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  1215. Vector<Plane> light_frustum_planes;
  1216. light_frustum_planes.resize(6);
  1217. //right/left
  1218. light_frustum_planes.write[0] = Plane(x_vec, x_max);
  1219. light_frustum_planes.write[1] = Plane(-x_vec, -x_min);
  1220. //top/bottom
  1221. light_frustum_planes.write[2] = Plane(y_vec, y_max);
  1222. light_frustum_planes.write[3] = Plane(-y_vec, -y_min);
  1223. //near/far
  1224. light_frustum_planes.write[4] = Plane(z_vec, z_max + 1e6);
  1225. light_frustum_planes.write[5] = Plane(-z_vec, -z_min); // z_min is ok, since casters further than far-light plane are not needed
  1226. int cull_count = p_scenario->octree.cull_convex(light_frustum_planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1227. // a pre pass will need to be needed to determine the actual z-near to be used
  1228. Plane near_plane(light_transform.origin, -light_transform.basis.get_axis(2));
  1229. real_t cull_max = 0;
  1230. for (int j = 0; j < cull_count; j++) {
  1231. real_t min, max;
  1232. Instance *instance = instance_shadow_cull_result[j];
  1233. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1234. cull_count--;
  1235. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1236. j--;
  1237. continue;
  1238. }
  1239. instance->transformed_aabb.project_range_in_plane(Plane(z_vec, 0), min, max);
  1240. instance->depth = near_plane.distance_to(instance->transform.origin);
  1241. instance->depth_layer = 0;
  1242. if (j == 0 || max > cull_max) {
  1243. cull_max = max;
  1244. }
  1245. }
  1246. if (cull_max > z_max) {
  1247. z_max = cull_max;
  1248. }
  1249. if (pancake_size > 0) {
  1250. z_max = z_vec.dot(center) + radius + pancake_size;
  1251. }
  1252. if (aspect != 1.0) {
  1253. // if the aspect is different, then the radius will become larger.
  1254. // if this happens, then bias needs to be adjusted too, as depth will increase
  1255. // to do this, compare the depth of one that would have resulted from a square frustum
  1256. CameraMatrix camera_matrix_square;
  1257. if (p_cam_orthogonal) {
  1258. Vector2 vp_he = camera_matrix.get_viewport_half_extents();
  1259. if (p_cam_vaspect) {
  1260. camera_matrix_square.set_orthogonal(vp_he.x * 2.0, 1.0, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], true);
  1261. } else {
  1262. camera_matrix_square.set_orthogonal(vp_he.y * 2.0, 1.0, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  1263. }
  1264. } else {
  1265. Vector2 vp_he = camera_matrix.get_viewport_half_extents();
  1266. if (p_cam_vaspect) {
  1267. camera_matrix_square.set_frustum(vp_he.x * 2.0, 1.0, Vector2(), distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], true);
  1268. } else {
  1269. camera_matrix_square.set_frustum(vp_he.y * 2.0, 1.0, Vector2(), distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  1270. }
  1271. }
  1272. Vector3 endpoints_square[8]; // frustum plane endpoints
  1273. res = camera_matrix_square.get_endpoints(p_cam_transform, endpoints_square);
  1274. ERR_CONTINUE(!res);
  1275. Vector3 center_square;
  1276. real_t z_max_square = 0;
  1277. for (int j = 0; j < 8; j++) {
  1278. center_square += endpoints_square[j];
  1279. real_t d_z = z_vec.dot(endpoints_square[j]);
  1280. if (j == 0 || d_z > z_max_square) {
  1281. z_max_square = d_z;
  1282. }
  1283. }
  1284. if (cull_max > z_max_square) {
  1285. z_max_square = cull_max;
  1286. }
  1287. center_square /= 8.0;
  1288. real_t radius_square = 0;
  1289. for (int j = 0; j < 8; j++) {
  1290. real_t d = center_square.distance_to(endpoints_square[j]);
  1291. if (d > radius_square) {
  1292. radius_square = d;
  1293. }
  1294. }
  1295. radius_square *= texture_size / (texture_size - 2.0); //add a texel by each side
  1296. if (pancake_size > 0) {
  1297. z_max_square = z_vec.dot(center_square) + radius_square + pancake_size;
  1298. }
  1299. real_t z_min_cam_square = z_vec.dot(center_square) - radius_square;
  1300. aspect_bias_scale = (z_max - z_min_cam) / (z_max_square - z_min_cam_square);
  1301. // this is not entirely perfect, because the cull-adjusted z-max may be different
  1302. // but at least it's warranted that it results in a greater bias, so no acne should be present either way.
  1303. // pancaking also helps with this.
  1304. }
  1305. {
  1306. CameraMatrix ortho_camera;
  1307. real_t half_x = (x_max_cam - x_min_cam) * 0.5;
  1308. real_t half_y = (y_max_cam - y_min_cam) * 0.5;
  1309. ortho_camera.set_orthogonal(-half_x, half_x, -half_y, half_y, 0, (z_max - z_min_cam));
  1310. Vector2 uv_scale(1.0 / (x_max_cam - x_min_cam), 1.0 / (y_max_cam - y_min_cam));
  1311. Transform ortho_transform;
  1312. ortho_transform.basis = transform.basis;
  1313. ortho_transform.origin = x_vec * (x_min_cam + half_x) + y_vec * (y_min_cam + half_y) + z_vec * z_max;
  1314. {
  1315. Vector3 max_in_view = p_cam_transform.affine_inverse().xform(z_vec * cull_max);
  1316. Vector3 dir_in_view = p_cam_transform.xform_inv(z_vec).normalized();
  1317. cull_max = dir_in_view.dot(max_in_view);
  1318. }
  1319. scene_render->light_instance_set_shadow_transform(light->instance, ortho_camera, ortho_transform, z_max - z_min_cam, distances[i + 1], i, radius * 2.0 / texture_size, bias_scale * aspect_bias_scale * min_distance_bias_scale, z_max, uv_scale);
  1320. }
  1321. scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1322. }
  1323. } break;
  1324. case RS::LIGHT_OMNI: {
  1325. RS::LightOmniShadowMode shadow_mode = RSG::storage->light_omni_get_shadow_mode(p_instance->base);
  1326. if (shadow_mode == RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID || !scene_render->light_instances_can_render_shadow_cube()) {
  1327. for (int i = 0; i < 2; i++) {
  1328. //using this one ensures that raster deferred will have it
  1329. RENDER_TIMESTAMP("Culling Shadow Paraboloid" + itos(i));
  1330. real_t radius = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1331. real_t z = i == 0 ? -1 : 1;
  1332. Vector<Plane> planes;
  1333. planes.resize(6);
  1334. planes.write[0] = light_transform.xform(Plane(Vector3(0, 0, z), radius));
  1335. planes.write[1] = light_transform.xform(Plane(Vector3(1, 0, z).normalized(), radius));
  1336. planes.write[2] = light_transform.xform(Plane(Vector3(-1, 0, z).normalized(), radius));
  1337. planes.write[3] = light_transform.xform(Plane(Vector3(0, 1, z).normalized(), radius));
  1338. planes.write[4] = light_transform.xform(Plane(Vector3(0, -1, z).normalized(), radius));
  1339. planes.write[5] = light_transform.xform(Plane(Vector3(0, 0, -z), 0));
  1340. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1341. Plane near_plane(light_transform.origin, light_transform.basis.get_axis(2) * z);
  1342. for (int j = 0; j < cull_count; j++) {
  1343. Instance *instance = instance_shadow_cull_result[j];
  1344. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1345. cull_count--;
  1346. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1347. j--;
  1348. } else {
  1349. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1350. animated_material_found = true;
  1351. }
  1352. instance->depth = near_plane.distance_to(instance->transform.origin);
  1353. instance->depth_layer = 0;
  1354. }
  1355. }
  1356. scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), light_transform, radius, 0, i, 0);
  1357. scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1358. }
  1359. } else { //shadow cube
  1360. real_t radius = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1361. CameraMatrix cm;
  1362. cm.set_perspective(90, 1, 0.01, radius);
  1363. for (int i = 0; i < 6; i++) {
  1364. RENDER_TIMESTAMP("Culling Shadow Cube side" + itos(i));
  1365. //using this one ensures that raster deferred will have it
  1366. static const Vector3 view_normals[6] = {
  1367. Vector3(+1, 0, 0),
  1368. Vector3(-1, 0, 0),
  1369. Vector3(0, -1, 0),
  1370. Vector3(0, +1, 0),
  1371. Vector3(0, 0, +1),
  1372. Vector3(0, 0, -1)
  1373. };
  1374. static const Vector3 view_up[6] = {
  1375. Vector3(0, -1, 0),
  1376. Vector3(0, -1, 0),
  1377. Vector3(0, 0, -1),
  1378. Vector3(0, 0, +1),
  1379. Vector3(0, -1, 0),
  1380. Vector3(0, -1, 0)
  1381. };
  1382. Transform xform = light_transform * Transform().looking_at(view_normals[i], view_up[i]);
  1383. Vector<Plane> planes = cm.get_projection_planes(xform);
  1384. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1385. Plane near_plane(xform.origin, -xform.basis.get_axis(2));
  1386. for (int j = 0; j < cull_count; j++) {
  1387. Instance *instance = instance_shadow_cull_result[j];
  1388. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1389. cull_count--;
  1390. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1391. j--;
  1392. } else {
  1393. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1394. animated_material_found = true;
  1395. }
  1396. instance->depth = near_plane.distance_to(instance->transform.origin);
  1397. instance->depth_layer = 0;
  1398. }
  1399. }
  1400. scene_render->light_instance_set_shadow_transform(light->instance, cm, xform, radius, 0, i, 0);
  1401. scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1402. }
  1403. //restore the regular DP matrix
  1404. scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), light_transform, radius, 0, 0, 0);
  1405. }
  1406. } break;
  1407. case RS::LIGHT_SPOT: {
  1408. RENDER_TIMESTAMP("Culling Spot Light");
  1409. real_t radius = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1410. real_t angle = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1411. CameraMatrix cm;
  1412. cm.set_perspective(angle * 2.0, 1.0, 0.01, radius);
  1413. Vector<Plane> planes = cm.get_projection_planes(light_transform);
  1414. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1415. Plane near_plane(light_transform.origin, -light_transform.basis.get_axis(2));
  1416. for (int j = 0; j < cull_count; j++) {
  1417. Instance *instance = instance_shadow_cull_result[j];
  1418. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1419. cull_count--;
  1420. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1421. j--;
  1422. } else {
  1423. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1424. animated_material_found = true;
  1425. }
  1426. instance->depth = near_plane.distance_to(instance->transform.origin);
  1427. instance->depth_layer = 0;
  1428. }
  1429. }
  1430. scene_render->light_instance_set_shadow_transform(light->instance, cm, light_transform, radius, 0, 0, 0);
  1431. scene_render->render_shadow(light->instance, p_shadow_atlas, 0, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1432. } break;
  1433. }
  1434. return animated_material_found;
  1435. }
  1436. void RenderingServerSceneRaster::render_camera(RID p_render_buffers, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas) {
  1437. // render to mono camera
  1438. #ifndef _3D_DISABLED
  1439. Camera *camera = camera_owner.getornull(p_camera);
  1440. ERR_FAIL_COND(!camera);
  1441. /* STEP 1 - SETUP CAMERA */
  1442. CameraMatrix camera_matrix;
  1443. bool ortho = false;
  1444. switch (camera->type) {
  1445. case Camera::ORTHOGONAL: {
  1446. camera_matrix.set_orthogonal(
  1447. camera->size,
  1448. p_viewport_size.width / (float)p_viewport_size.height,
  1449. camera->znear,
  1450. camera->zfar,
  1451. camera->vaspect);
  1452. ortho = true;
  1453. } break;
  1454. case Camera::PERSPECTIVE: {
  1455. camera_matrix.set_perspective(
  1456. camera->fov,
  1457. p_viewport_size.width / (float)p_viewport_size.height,
  1458. camera->znear,
  1459. camera->zfar,
  1460. camera->vaspect);
  1461. ortho = false;
  1462. } break;
  1463. case Camera::FRUSTUM: {
  1464. camera_matrix.set_frustum(
  1465. camera->size,
  1466. p_viewport_size.width / (float)p_viewport_size.height,
  1467. camera->offset,
  1468. camera->znear,
  1469. camera->zfar,
  1470. camera->vaspect);
  1471. ortho = false;
  1472. } break;
  1473. }
  1474. RID environment = _render_get_environment(p_camera, p_scenario);
  1475. _prepare_scene(camera->transform, camera_matrix, ortho, camera->vaspect, p_render_buffers, environment, camera->visible_layers, p_scenario, p_shadow_atlas, RID());
  1476. _render_scene(p_render_buffers, camera->transform, camera_matrix, ortho, environment, camera->effects, p_scenario, p_shadow_atlas, RID(), -1);
  1477. #endif
  1478. }
  1479. void RenderingServerSceneRaster::render_camera(RID p_render_buffers, Ref<XRInterface> &p_interface, XRInterface::Eyes p_eye, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas) {
  1480. // render for AR/VR interface
  1481. Camera *camera = camera_owner.getornull(p_camera);
  1482. ERR_FAIL_COND(!camera);
  1483. /* SETUP CAMERA, we are ignoring type and FOV here */
  1484. float aspect = p_viewport_size.width / (float)p_viewport_size.height;
  1485. CameraMatrix camera_matrix = p_interface->get_projection_for_eye(p_eye, aspect, camera->znear, camera->zfar);
  1486. // We also ignore our camera position, it will have been positioned with a slightly old tracking position.
  1487. // Instead we take our origin point and have our ar/vr interface add fresh tracking data! Whoohoo!
  1488. Transform world_origin = XRServer::get_singleton()->get_world_origin();
  1489. Transform cam_transform = p_interface->get_transform_for_eye(p_eye, world_origin);
  1490. RID environment = _render_get_environment(p_camera, p_scenario);
  1491. // For stereo render we only prepare for our left eye and then reuse the outcome for our right eye
  1492. if (p_eye == XRInterface::EYE_LEFT) {
  1493. // Center our transform, we assume basis is equal.
  1494. Transform mono_transform = cam_transform;
  1495. Transform right_transform = p_interface->get_transform_for_eye(XRInterface::EYE_RIGHT, world_origin);
  1496. mono_transform.origin += right_transform.origin;
  1497. mono_transform.origin *= 0.5;
  1498. // We need to combine our projection frustums for culling.
  1499. // Ideally we should use our clipping planes for this and combine them,
  1500. // however our shadow map logic uses our projection matrix.
  1501. // Note: as our left and right frustums should be mirrored, we don't need our right projection matrix.
  1502. // - get some base values we need
  1503. float eye_dist = (mono_transform.origin - cam_transform.origin).length();
  1504. float z_near = camera_matrix.get_z_near(); // get our near plane
  1505. float z_far = camera_matrix.get_z_far(); // get our far plane
  1506. float width = (2.0 * z_near) / camera_matrix.matrix[0][0];
  1507. float x_shift = width * camera_matrix.matrix[2][0];
  1508. float height = (2.0 * z_near) / camera_matrix.matrix[1][1];
  1509. float y_shift = height * camera_matrix.matrix[2][1];
  1510. // printf("Eye_dist = %f, Near = %f, Far = %f, Width = %f, Shift = %f\n", eye_dist, z_near, z_far, width, x_shift);
  1511. // - calculate our near plane size (horizontal only, right_near is mirrored)
  1512. float left_near = -eye_dist - ((width - x_shift) * 0.5);
  1513. // - calculate our far plane size (horizontal only, right_far is mirrored)
  1514. float left_far = -eye_dist - (z_far * (width - x_shift) * 0.5 / z_near);
  1515. float left_far_right_eye = eye_dist - (z_far * (width + x_shift) * 0.5 / z_near);
  1516. if (left_far > left_far_right_eye) {
  1517. // on displays smaller then double our iod, the right eye far frustrum can overtake the left eyes.
  1518. left_far = left_far_right_eye;
  1519. }
  1520. // - figure out required z-shift
  1521. float slope = (left_far - left_near) / (z_far - z_near);
  1522. float z_shift = (left_near / slope) - z_near;
  1523. // - figure out new vertical near plane size (this will be slightly oversized thanks to our z-shift)
  1524. float top_near = (height - y_shift) * 0.5;
  1525. top_near += (top_near / z_near) * z_shift;
  1526. float bottom_near = -(height + y_shift) * 0.5;
  1527. bottom_near += (bottom_near / z_near) * z_shift;
  1528. // printf("Left_near = %f, Left_far = %f, Top_near = %f, Bottom_near = %f, Z_shift = %f\n", left_near, left_far, top_near, bottom_near, z_shift);
  1529. // - generate our frustum
  1530. CameraMatrix combined_matrix;
  1531. combined_matrix.set_frustum(left_near, -left_near, bottom_near, top_near, z_near + z_shift, z_far + z_shift);
  1532. // and finally move our camera back
  1533. Transform apply_z_shift;
  1534. apply_z_shift.origin = Vector3(0.0, 0.0, z_shift); // z negative is forward so this moves it backwards
  1535. mono_transform *= apply_z_shift;
  1536. // now prepare our scene with our adjusted transform projection matrix
  1537. _prepare_scene(mono_transform, combined_matrix, false, false, p_render_buffers, environment, camera->visible_layers, p_scenario, p_shadow_atlas, RID());
  1538. } else if (p_eye == XRInterface::EYE_MONO) {
  1539. // For mono render, prepare as per usual
  1540. _prepare_scene(cam_transform, camera_matrix, false, false, p_render_buffers, environment, camera->visible_layers, p_scenario, p_shadow_atlas, RID());
  1541. }
  1542. // And render our scene...
  1543. _render_scene(p_render_buffers, cam_transform, camera_matrix, false, environment, camera->effects, p_scenario, p_shadow_atlas, RID(), -1);
  1544. };
  1545. void RenderingServerSceneRaster::_prepare_scene(const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_render_buffers, RID p_environment, uint32_t p_visible_layers, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, bool p_using_shadows) {
  1546. // Note, in stereo rendering:
  1547. // - p_cam_transform will be a transform in the middle of our two eyes
  1548. // - p_cam_projection is a wider frustrum that encompasses both eyes
  1549. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1550. render_pass++;
  1551. uint32_t camera_layer_mask = p_visible_layers;
  1552. scene_render->set_scene_pass(render_pass);
  1553. if (p_render_buffers.is_valid()) {
  1554. scene_render->sdfgi_update(p_render_buffers, p_environment, p_cam_transform.origin); //update conditions for SDFGI (whether its used or not)
  1555. }
  1556. RENDER_TIMESTAMP("Frustum Culling");
  1557. //rasterizer->set_camera(camera->transform, camera_matrix,ortho);
  1558. Vector<Plane> planes = p_cam_projection.get_projection_planes(p_cam_transform);
  1559. Plane near_plane(p_cam_transform.origin, -p_cam_transform.basis.get_axis(2).normalized());
  1560. float z_far = p_cam_projection.get_z_far();
  1561. /* STEP 2 - CULL */
  1562. instance_cull_count = scenario->octree.cull_convex(planes, instance_cull_result, MAX_INSTANCE_CULL);
  1563. light_cull_count = 0;
  1564. reflection_probe_cull_count = 0;
  1565. decal_cull_count = 0;
  1566. gi_probe_cull_count = 0;
  1567. lightmap_cull_count = 0;
  1568. //light_samplers_culled=0;
  1569. /*
  1570. print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  1571. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  1572. print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  1573. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  1574. */
  1575. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  1576. //removed, will replace with culling
  1577. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  1578. uint64_t frame_number = RSG::rasterizer->get_frame_number();
  1579. float lightmap_probe_update_speed = RSG::storage->lightmap_get_probe_capture_update_speed() * RSG::rasterizer->get_frame_delta_time();
  1580. for (int i = 0; i < instance_cull_count; i++) {
  1581. Instance *ins = instance_cull_result[i];
  1582. bool keep = false;
  1583. if ((camera_layer_mask & ins->layer_mask) == 0) {
  1584. //failure
  1585. } else if (ins->base_type == RS::INSTANCE_LIGHT && ins->visible) {
  1586. if (light_cull_count < MAX_LIGHTS_CULLED) {
  1587. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1588. if (!light->geometries.empty()) {
  1589. //do not add this light if no geometry is affected by it..
  1590. light_cull_result[light_cull_count] = ins;
  1591. light_instance_cull_result[light_cull_count] = light->instance;
  1592. if (p_shadow_atlas.is_valid() && RSG::storage->light_has_shadow(ins->base)) {
  1593. scene_render->light_instance_mark_visible(light->instance); //mark it visible for shadow allocation later
  1594. }
  1595. light_cull_count++;
  1596. }
  1597. }
  1598. } else if (ins->base_type == RS::INSTANCE_REFLECTION_PROBE && ins->visible) {
  1599. if (reflection_probe_cull_count < MAX_REFLECTION_PROBES_CULLED) {
  1600. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(ins->base_data);
  1601. if (p_reflection_probe != reflection_probe->instance) {
  1602. //avoid entering The Matrix
  1603. if (!reflection_probe->geometries.empty()) {
  1604. //do not add this light if no geometry is affected by it..
  1605. if (reflection_probe->reflection_dirty || scene_render->reflection_probe_instance_needs_redraw(reflection_probe->instance)) {
  1606. if (!reflection_probe->update_list.in_list()) {
  1607. reflection_probe->render_step = 0;
  1608. reflection_probe_render_list.add_last(&reflection_probe->update_list);
  1609. }
  1610. reflection_probe->reflection_dirty = false;
  1611. }
  1612. if (scene_render->reflection_probe_instance_has_reflection(reflection_probe->instance)) {
  1613. reflection_probe_instance_cull_result[reflection_probe_cull_count] = reflection_probe->instance;
  1614. reflection_probe_cull_count++;
  1615. }
  1616. }
  1617. }
  1618. }
  1619. } else if (ins->base_type == RS::INSTANCE_DECAL && ins->visible) {
  1620. if (decal_cull_count < MAX_DECALS_CULLED) {
  1621. InstanceDecalData *decal = static_cast<InstanceDecalData *>(ins->base_data);
  1622. if (!decal->geometries.empty()) {
  1623. //do not add this decal if no geometry is affected by it..
  1624. decal_instance_cull_result[decal_cull_count] = decal->instance;
  1625. decal_cull_count++;
  1626. }
  1627. }
  1628. } else if (ins->base_type == RS::INSTANCE_GI_PROBE && ins->visible) {
  1629. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(ins->base_data);
  1630. if (!gi_probe->update_element.in_list()) {
  1631. gi_probe_update_list.add(&gi_probe->update_element);
  1632. }
  1633. if (gi_probe_cull_count < MAX_GI_PROBES_CULLED) {
  1634. gi_probe_instance_cull_result[gi_probe_cull_count] = gi_probe->probe_instance;
  1635. gi_probe_cull_count++;
  1636. }
  1637. } else if (ins->base_type == RS::INSTANCE_LIGHTMAP && ins->visible) {
  1638. if (lightmap_cull_count < MAX_LIGHTMAPS_CULLED) {
  1639. lightmap_cull_result[lightmap_cull_count] = ins;
  1640. lightmap_cull_count++;
  1641. }
  1642. } else if (((1 << ins->base_type) & RS::INSTANCE_GEOMETRY_MASK) && ins->visible && ins->cast_shadows != RS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) {
  1643. keep = true;
  1644. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(ins->base_data);
  1645. if (ins->redraw_if_visible) {
  1646. RenderingServerDefault::redraw_request();
  1647. }
  1648. if (ins->base_type == RS::INSTANCE_PARTICLES) {
  1649. //particles visible? process them
  1650. if (RSG::storage->particles_is_inactive(ins->base)) {
  1651. //but if nothing is going on, don't do it.
  1652. keep = false;
  1653. } else {
  1654. RSG::storage->particles_request_process(ins->base);
  1655. RSG::storage->particles_set_view_axis(ins->base, -p_cam_transform.basis.get_axis(2).normalized());
  1656. //particles visible? request redraw
  1657. RenderingServerDefault::redraw_request();
  1658. }
  1659. }
  1660. if (geom->lighting_dirty) {
  1661. int l = 0;
  1662. //only called when lights AABB enter/exit this geometry
  1663. ins->light_instances.resize(geom->lighting.size());
  1664. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  1665. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1666. ins->light_instances.write[l++] = light->instance;
  1667. }
  1668. geom->lighting_dirty = false;
  1669. }
  1670. if (geom->reflection_dirty) {
  1671. int l = 0;
  1672. //only called when reflection probe AABB enter/exit this geometry
  1673. ins->reflection_probe_instances.resize(geom->reflection_probes.size());
  1674. for (List<Instance *>::Element *E = geom->reflection_probes.front(); E; E = E->next()) {
  1675. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(E->get()->base_data);
  1676. ins->reflection_probe_instances.write[l++] = reflection_probe->instance;
  1677. }
  1678. geom->reflection_dirty = false;
  1679. }
  1680. if (geom->gi_probes_dirty) {
  1681. int l = 0;
  1682. //only called when reflection probe AABB enter/exit this geometry
  1683. ins->gi_probe_instances.resize(geom->gi_probes.size());
  1684. for (List<Instance *>::Element *E = geom->gi_probes.front(); E; E = E->next()) {
  1685. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(E->get()->base_data);
  1686. ins->gi_probe_instances.write[l++] = gi_probe->probe_instance;
  1687. }
  1688. geom->gi_probes_dirty = false;
  1689. }
  1690. if (ins->last_frame_pass != frame_number && !ins->lightmap_target_sh.empty() && !ins->lightmap_sh.empty()) {
  1691. Color *sh = ins->lightmap_sh.ptrw();
  1692. const Color *target_sh = ins->lightmap_target_sh.ptr();
  1693. for (uint32_t j = 0; j < 9; j++) {
  1694. sh[j] = sh[j].lerp(target_sh[j], MIN(1.0, lightmap_probe_update_speed));
  1695. }
  1696. }
  1697. ins->depth = near_plane.distance_to(ins->transform.origin);
  1698. ins->depth_layer = CLAMP(int(ins->depth * 16 / z_far), 0, 15);
  1699. }
  1700. if (!keep) {
  1701. // remove, no reason to keep
  1702. instance_cull_count--;
  1703. SWAP(instance_cull_result[i], instance_cull_result[instance_cull_count]);
  1704. i--;
  1705. ins->last_render_pass = 0; // make invalid
  1706. } else {
  1707. ins->last_render_pass = render_pass;
  1708. }
  1709. ins->last_frame_pass = frame_number;
  1710. }
  1711. /* STEP 5 - PROCESS LIGHTS */
  1712. RID *directional_light_ptr = &light_instance_cull_result[light_cull_count];
  1713. directional_light_count = 0;
  1714. // directional lights
  1715. {
  1716. Instance **lights_with_shadow = (Instance **)alloca(sizeof(Instance *) * scenario->directional_lights.size());
  1717. int directional_shadow_count = 0;
  1718. for (List<Instance *>::Element *E = scenario->directional_lights.front(); E; E = E->next()) {
  1719. if (light_cull_count + directional_light_count >= MAX_LIGHTS_CULLED) {
  1720. break;
  1721. }
  1722. if (!E->get()->visible) {
  1723. continue;
  1724. }
  1725. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1726. //check shadow..
  1727. if (light) {
  1728. if (p_using_shadows && p_shadow_atlas.is_valid() && RSG::storage->light_has_shadow(E->get()->base) && !(RSG::storage->light_get_type(E->get()->base) == RS::LIGHT_DIRECTIONAL && RSG::storage->light_directional_is_sky_only(E->get()->base))) {
  1729. lights_with_shadow[directional_shadow_count++] = E->get();
  1730. }
  1731. //add to list
  1732. directional_light_ptr[directional_light_count++] = light->instance;
  1733. }
  1734. }
  1735. scene_render->set_directional_shadow_count(directional_shadow_count);
  1736. for (int i = 0; i < directional_shadow_count; i++) {
  1737. RENDER_TIMESTAMP(">Rendering Directional Light " + itos(i));
  1738. _light_instance_update_shadow(lights_with_shadow[i], p_cam_transform, p_cam_projection, p_cam_orthogonal, p_cam_vaspect, p_shadow_atlas, scenario);
  1739. RENDER_TIMESTAMP("<Rendering Directional Light " + itos(i));
  1740. }
  1741. }
  1742. if (p_using_shadows) { //setup shadow maps
  1743. //SortArray<Instance*,_InstanceLightsort> sorter;
  1744. //sorter.sort(light_cull_result,light_cull_count);
  1745. for (int i = 0; i < light_cull_count; i++) {
  1746. Instance *ins = light_cull_result[i];
  1747. if (!p_shadow_atlas.is_valid() || !RSG::storage->light_has_shadow(ins->base)) {
  1748. continue;
  1749. }
  1750. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1751. float coverage = 0.f;
  1752. { //compute coverage
  1753. Transform cam_xf = p_cam_transform;
  1754. float zn = p_cam_projection.get_z_near();
  1755. Plane p(cam_xf.origin + cam_xf.basis.get_axis(2) * -zn, -cam_xf.basis.get_axis(2)); //camera near plane
  1756. // near plane half width and height
  1757. Vector2 vp_half_extents = p_cam_projection.get_viewport_half_extents();
  1758. switch (RSG::storage->light_get_type(ins->base)) {
  1759. case RS::LIGHT_OMNI: {
  1760. float radius = RSG::storage->light_get_param(ins->base, RS::LIGHT_PARAM_RANGE);
  1761. //get two points parallel to near plane
  1762. Vector3 points[2] = {
  1763. ins->transform.origin,
  1764. ins->transform.origin + cam_xf.basis.get_axis(0) * radius
  1765. };
  1766. if (!p_cam_orthogonal) {
  1767. //if using perspetive, map them to near plane
  1768. for (int j = 0; j < 2; j++) {
  1769. if (p.distance_to(points[j]) < 0) {
  1770. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1771. }
  1772. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1773. }
  1774. }
  1775. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1776. coverage = screen_diameter / (vp_half_extents.x + vp_half_extents.y);
  1777. } break;
  1778. case RS::LIGHT_SPOT: {
  1779. float radius = RSG::storage->light_get_param(ins->base, RS::LIGHT_PARAM_RANGE);
  1780. float angle = RSG::storage->light_get_param(ins->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1781. float w = radius * Math::sin(Math::deg2rad(angle));
  1782. float d = radius * Math::cos(Math::deg2rad(angle));
  1783. Vector3 base = ins->transform.origin - ins->transform.basis.get_axis(2).normalized() * d;
  1784. Vector3 points[2] = {
  1785. base,
  1786. base + cam_xf.basis.get_axis(0) * w
  1787. };
  1788. if (!p_cam_orthogonal) {
  1789. //if using perspetive, map them to near plane
  1790. for (int j = 0; j < 2; j++) {
  1791. if (p.distance_to(points[j]) < 0) {
  1792. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1793. }
  1794. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1795. }
  1796. }
  1797. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1798. coverage = screen_diameter / (vp_half_extents.x + vp_half_extents.y);
  1799. } break;
  1800. default: {
  1801. ERR_PRINT("Invalid Light Type");
  1802. }
  1803. }
  1804. }
  1805. if (light->shadow_dirty) {
  1806. light->last_version++;
  1807. light->shadow_dirty = false;
  1808. }
  1809. bool redraw = scene_render->shadow_atlas_update_light(p_shadow_atlas, light->instance, coverage, light->last_version);
  1810. if (redraw) {
  1811. //must redraw!
  1812. RENDER_TIMESTAMP(">Rendering Light " + itos(i));
  1813. light->shadow_dirty = _light_instance_update_shadow(ins, p_cam_transform, p_cam_projection, p_cam_orthogonal, p_cam_vaspect, p_shadow_atlas, scenario);
  1814. RENDER_TIMESTAMP("<Rendering Light " + itos(i));
  1815. }
  1816. }
  1817. }
  1818. /* UPDATE SDFGI */
  1819. if (p_render_buffers.is_valid()) {
  1820. uint32_t cascade_index[8];
  1821. uint32_t cascade_sizes[8];
  1822. const RID *cascade_ptrs[8];
  1823. uint32_t cascade_count = 0;
  1824. uint32_t sdfgi_light_cull_count = 0;
  1825. uint32_t prev_cascade = 0xFFFFFFFF;
  1826. for (int i = 0; i < scene_render->sdfgi_get_pending_region_count(p_render_buffers); i++) {
  1827. AABB region = scene_render->sdfgi_get_pending_region_bounds(p_render_buffers, i);
  1828. uint32_t region_cascade = scene_render->sdfgi_get_pending_region_cascade(p_render_buffers, i);
  1829. if (region_cascade != prev_cascade) {
  1830. cascade_sizes[cascade_count] = 0;
  1831. cascade_index[cascade_count] = region_cascade;
  1832. cascade_ptrs[cascade_count] = &sdfgi_light_cull_result[sdfgi_light_cull_count];
  1833. cascade_count++;
  1834. sdfgi_light_cull_pass++;
  1835. prev_cascade = region_cascade;
  1836. }
  1837. uint32_t sdfgi_cull_count = scenario->octree.cull_aabb(region, instance_shadow_cull_result, MAX_INSTANCE_CULL);
  1838. for (uint32_t j = 0; j < sdfgi_cull_count; j++) {
  1839. Instance *ins = instance_shadow_cull_result[j];
  1840. bool keep = false;
  1841. if (ins->base_type == RS::INSTANCE_LIGHT && ins->visible) {
  1842. InstanceLightData *instance_light = (InstanceLightData *)ins->base_data;
  1843. if (instance_light->bake_mode != RS::LIGHT_BAKE_STATIC || region_cascade > instance_light->max_sdfgi_cascade) {
  1844. continue;
  1845. }
  1846. if (sdfgi_light_cull_pass != instance_light->sdfgi_cascade_light_pass && sdfgi_light_cull_count < MAX_LIGHTS_CULLED) {
  1847. instance_light->sdfgi_cascade_light_pass = sdfgi_light_cull_pass;
  1848. sdfgi_light_cull_result[sdfgi_light_cull_count++] = instance_light->instance;
  1849. cascade_sizes[cascade_count - 1]++;
  1850. }
  1851. } else if ((1 << ins->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1852. if (ins->baked_light) {
  1853. keep = true;
  1854. }
  1855. }
  1856. if (!keep) {
  1857. // remove, no reason to keep
  1858. sdfgi_cull_count--;
  1859. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[sdfgi_cull_count]);
  1860. j--;
  1861. }
  1862. }
  1863. scene_render->render_sdfgi(p_render_buffers, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, sdfgi_cull_count);
  1864. //have to save updated cascades, then update static lights.
  1865. }
  1866. if (sdfgi_light_cull_count) {
  1867. scene_render->render_sdfgi_static_lights(p_render_buffers, cascade_count, cascade_index, cascade_ptrs, cascade_sizes);
  1868. }
  1869. scene_render->sdfgi_update_probes(p_render_buffers, p_environment, directional_light_ptr, directional_light_count, scenario->dynamic_lights.ptr(), scenario->dynamic_lights.size());
  1870. }
  1871. }
  1872. RID RenderingServerSceneRaster::_render_get_environment(RID p_camera, RID p_scenario) {
  1873. Camera *camera = camera_owner.getornull(p_camera);
  1874. if (camera && scene_render->is_environment(camera->env)) {
  1875. return camera->env;
  1876. }
  1877. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1878. if (!scenario) {
  1879. return RID();
  1880. }
  1881. if (scene_render->is_environment(scenario->environment)) {
  1882. return scenario->environment;
  1883. }
  1884. if (scene_render->is_environment(scenario->fallback_environment)) {
  1885. return scenario->fallback_environment;
  1886. }
  1887. return RID();
  1888. }
  1889. void RenderingServerSceneRaster::_render_scene(RID p_render_buffers, const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, RID p_environment, RID p_force_camera_effects, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, int p_reflection_probe_pass) {
  1890. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1891. RID camera_effects;
  1892. if (p_force_camera_effects.is_valid()) {
  1893. camera_effects = p_force_camera_effects;
  1894. } else {
  1895. camera_effects = scenario->camera_effects;
  1896. }
  1897. /* PROCESS GEOMETRY AND DRAW SCENE */
  1898. RENDER_TIMESTAMP("Render Scene ");
  1899. scene_render->render_scene(p_render_buffers, p_cam_transform, p_cam_projection, p_cam_orthogonal, (RasterizerScene::InstanceBase **)instance_cull_result, instance_cull_count, light_instance_cull_result, light_cull_count + directional_light_count, reflection_probe_instance_cull_result, reflection_probe_cull_count, gi_probe_instance_cull_result, gi_probe_cull_count, decal_instance_cull_result, decal_cull_count, (RasterizerScene::InstanceBase **)lightmap_cull_result, lightmap_cull_count, p_environment, camera_effects, p_shadow_atlas, p_reflection_probe.is_valid() ? RID() : scenario->reflection_atlas, p_reflection_probe, p_reflection_probe_pass);
  1900. }
  1901. void RenderingServerSceneRaster::render_empty_scene(RID p_render_buffers, RID p_scenario, RID p_shadow_atlas) {
  1902. #ifndef _3D_DISABLED
  1903. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1904. RID environment;
  1905. if (scenario->environment.is_valid()) {
  1906. environment = scenario->environment;
  1907. } else {
  1908. environment = scenario->fallback_environment;
  1909. }
  1910. RENDER_TIMESTAMP("Render Empty Scene ");
  1911. scene_render->render_scene(p_render_buffers, Transform(), CameraMatrix(), true, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, environment, RID(), p_shadow_atlas, scenario->reflection_atlas, RID(), 0);
  1912. #endif
  1913. }
  1914. bool RenderingServerSceneRaster::_render_reflection_probe_step(Instance *p_instance, int p_step) {
  1915. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  1916. Scenario *scenario = p_instance->scenario;
  1917. ERR_FAIL_COND_V(!scenario, true);
  1918. RenderingServerDefault::redraw_request(); //update, so it updates in editor
  1919. if (p_step == 0) {
  1920. if (!scene_render->reflection_probe_instance_begin_render(reflection_probe->instance, scenario->reflection_atlas)) {
  1921. return true; //all full
  1922. }
  1923. }
  1924. if (p_step >= 0 && p_step < 6) {
  1925. static const Vector3 view_normals[6] = {
  1926. Vector3(+1, 0, 0),
  1927. Vector3(-1, 0, 0),
  1928. Vector3(0, +1, 0),
  1929. Vector3(0, -1, 0),
  1930. Vector3(0, 0, +1),
  1931. Vector3(0, 0, -1)
  1932. };
  1933. static const Vector3 view_up[6] = {
  1934. Vector3(0, -1, 0),
  1935. Vector3(0, -1, 0),
  1936. Vector3(0, 0, +1),
  1937. Vector3(0, 0, -1),
  1938. Vector3(0, -1, 0),
  1939. Vector3(0, -1, 0)
  1940. };
  1941. Vector3 extents = RSG::storage->reflection_probe_get_extents(p_instance->base);
  1942. Vector3 origin_offset = RSG::storage->reflection_probe_get_origin_offset(p_instance->base);
  1943. float max_distance = RSG::storage->reflection_probe_get_origin_max_distance(p_instance->base);
  1944. Vector3 edge = view_normals[p_step] * extents;
  1945. 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
  1946. max_distance = MAX(max_distance, distance);
  1947. //render cubemap side
  1948. CameraMatrix cm;
  1949. cm.set_perspective(90, 1, 0.01, max_distance);
  1950. Transform local_view;
  1951. local_view.set_look_at(origin_offset, origin_offset + view_normals[p_step], view_up[p_step]);
  1952. Transform xform = p_instance->transform * local_view;
  1953. RID shadow_atlas;
  1954. bool use_shadows = RSG::storage->reflection_probe_renders_shadows(p_instance->base);
  1955. if (use_shadows) {
  1956. shadow_atlas = scenario->reflection_probe_shadow_atlas;
  1957. }
  1958. RENDER_TIMESTAMP("Render Reflection Probe, Step " + itos(p_step));
  1959. _prepare_scene(xform, cm, false, false, RID(), RID(), RSG::storage->reflection_probe_get_cull_mask(p_instance->base), p_instance->scenario->self, shadow_atlas, reflection_probe->instance, use_shadows);
  1960. _render_scene(RID(), xform, cm, false, RID(), RID(), p_instance->scenario->self, shadow_atlas, reflection_probe->instance, p_step);
  1961. } else {
  1962. //do roughness postprocess step until it believes it's done
  1963. RENDER_TIMESTAMP("Post-Process Reflection Probe, Step " + itos(p_step));
  1964. return scene_render->reflection_probe_instance_postprocess_step(reflection_probe->instance);
  1965. }
  1966. return false;
  1967. }
  1968. void RenderingServerSceneRaster::render_probes() {
  1969. /* REFLECTION PROBES */
  1970. SelfList<InstanceReflectionProbeData> *ref_probe = reflection_probe_render_list.first();
  1971. bool busy = false;
  1972. while (ref_probe) {
  1973. SelfList<InstanceReflectionProbeData> *next = ref_probe->next();
  1974. RID base = ref_probe->self()->owner->base;
  1975. switch (RSG::storage->reflection_probe_get_update_mode(base)) {
  1976. case RS::REFLECTION_PROBE_UPDATE_ONCE: {
  1977. if (busy) { //already rendering something
  1978. break;
  1979. }
  1980. bool done = _render_reflection_probe_step(ref_probe->self()->owner, ref_probe->self()->render_step);
  1981. if (done) {
  1982. reflection_probe_render_list.remove(ref_probe);
  1983. } else {
  1984. ref_probe->self()->render_step++;
  1985. }
  1986. busy = true; //do not render another one of this kind
  1987. } break;
  1988. case RS::REFLECTION_PROBE_UPDATE_ALWAYS: {
  1989. int step = 0;
  1990. bool done = false;
  1991. while (!done) {
  1992. done = _render_reflection_probe_step(ref_probe->self()->owner, step);
  1993. step++;
  1994. }
  1995. reflection_probe_render_list.remove(ref_probe);
  1996. } break;
  1997. }
  1998. ref_probe = next;
  1999. }
  2000. /* GI PROBES */
  2001. SelfList<InstanceGIProbeData> *gi_probe = gi_probe_update_list.first();
  2002. if (gi_probe) {
  2003. RENDER_TIMESTAMP("Render GI Probes");
  2004. }
  2005. while (gi_probe) {
  2006. SelfList<InstanceGIProbeData> *next = gi_probe->next();
  2007. InstanceGIProbeData *probe = gi_probe->self();
  2008. //Instance *instance_probe = probe->owner;
  2009. //check if probe must be setup, but don't do if on the lighting thread
  2010. bool cache_dirty = false;
  2011. int cache_count = 0;
  2012. {
  2013. int light_cache_size = probe->light_cache.size();
  2014. const InstanceGIProbeData::LightCache *caches = probe->light_cache.ptr();
  2015. const RID *instance_caches = probe->light_instances.ptr();
  2016. int idx = 0; //must count visible lights
  2017. for (Set<Instance *>::Element *E = probe->lights.front(); E; E = E->next()) {
  2018. Instance *instance = E->get();
  2019. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2020. if (!instance->visible) {
  2021. continue;
  2022. }
  2023. if (cache_dirty) {
  2024. //do nothing, since idx must count all visible lights anyway
  2025. } else if (idx >= light_cache_size) {
  2026. cache_dirty = true;
  2027. } else {
  2028. const InstanceGIProbeData::LightCache *cache = &caches[idx];
  2029. if (
  2030. instance_caches[idx] != instance_light->instance ||
  2031. cache->has_shadow != RSG::storage->light_has_shadow(instance->base) ||
  2032. cache->type != RSG::storage->light_get_type(instance->base) ||
  2033. cache->transform != instance->transform ||
  2034. cache->color != RSG::storage->light_get_color(instance->base) ||
  2035. cache->energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY) ||
  2036. cache->bake_energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY) ||
  2037. cache->radius != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE) ||
  2038. cache->attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION) ||
  2039. cache->spot_angle != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE) ||
  2040. cache->spot_attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION)) {
  2041. cache_dirty = true;
  2042. }
  2043. }
  2044. idx++;
  2045. }
  2046. for (List<Instance *>::Element *E = probe->owner->scenario->directional_lights.front(); E; E = E->next()) {
  2047. Instance *instance = E->get();
  2048. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2049. if (!instance->visible) {
  2050. continue;
  2051. }
  2052. if (cache_dirty) {
  2053. //do nothing, since idx must count all visible lights anyway
  2054. } else if (idx >= light_cache_size) {
  2055. cache_dirty = true;
  2056. } else {
  2057. const InstanceGIProbeData::LightCache *cache = &caches[idx];
  2058. if (
  2059. instance_caches[idx] != instance_light->instance ||
  2060. cache->has_shadow != RSG::storage->light_has_shadow(instance->base) ||
  2061. cache->type != RSG::storage->light_get_type(instance->base) ||
  2062. cache->transform != instance->transform ||
  2063. cache->color != RSG::storage->light_get_color(instance->base) ||
  2064. cache->energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY) ||
  2065. cache->bake_energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY) ||
  2066. cache->radius != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE) ||
  2067. cache->attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION) ||
  2068. cache->spot_angle != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE) ||
  2069. cache->spot_attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION) ||
  2070. cache->sky_only != RSG::storage->light_directional_is_sky_only(instance->base)) {
  2071. cache_dirty = true;
  2072. }
  2073. }
  2074. idx++;
  2075. }
  2076. if (idx != light_cache_size) {
  2077. cache_dirty = true;
  2078. }
  2079. cache_count = idx;
  2080. }
  2081. bool update_lights = scene_render->gi_probe_needs_update(probe->probe_instance);
  2082. if (cache_dirty) {
  2083. probe->light_cache.resize(cache_count);
  2084. probe->light_instances.resize(cache_count);
  2085. if (cache_count) {
  2086. InstanceGIProbeData::LightCache *caches = probe->light_cache.ptrw();
  2087. RID *instance_caches = probe->light_instances.ptrw();
  2088. int idx = 0; //must count visible lights
  2089. for (Set<Instance *>::Element *E = probe->lights.front(); E; E = E->next()) {
  2090. Instance *instance = E->get();
  2091. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2092. if (!instance->visible) {
  2093. continue;
  2094. }
  2095. InstanceGIProbeData::LightCache *cache = &caches[idx];
  2096. instance_caches[idx] = instance_light->instance;
  2097. cache->has_shadow = RSG::storage->light_has_shadow(instance->base);
  2098. cache->type = RSG::storage->light_get_type(instance->base);
  2099. cache->transform = instance->transform;
  2100. cache->color = RSG::storage->light_get_color(instance->base);
  2101. cache->energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY);
  2102. cache->bake_energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  2103. cache->radius = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE);
  2104. cache->attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION);
  2105. cache->spot_angle = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  2106. cache->spot_attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  2107. idx++;
  2108. }
  2109. for (List<Instance *>::Element *E = probe->owner->scenario->directional_lights.front(); E; E = E->next()) {
  2110. Instance *instance = E->get();
  2111. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2112. if (!instance->visible) {
  2113. continue;
  2114. }
  2115. InstanceGIProbeData::LightCache *cache = &caches[idx];
  2116. instance_caches[idx] = instance_light->instance;
  2117. cache->has_shadow = RSG::storage->light_has_shadow(instance->base);
  2118. cache->type = RSG::storage->light_get_type(instance->base);
  2119. cache->transform = instance->transform;
  2120. cache->color = RSG::storage->light_get_color(instance->base);
  2121. cache->energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY);
  2122. cache->bake_energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  2123. cache->radius = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE);
  2124. cache->attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION);
  2125. cache->spot_angle = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  2126. cache->spot_attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  2127. cache->sky_only = RSG::storage->light_directional_is_sky_only(instance->base);
  2128. idx++;
  2129. }
  2130. }
  2131. update_lights = true;
  2132. }
  2133. instance_cull_count = 0;
  2134. for (List<InstanceGIProbeData::PairInfo>::Element *E = probe->dynamic_geometries.front(); E; E = E->next()) {
  2135. if (instance_cull_count < MAX_INSTANCE_CULL) {
  2136. Instance *ins = E->get().geometry;
  2137. if (!ins->visible) {
  2138. continue;
  2139. }
  2140. InstanceGeometryData *geom = (InstanceGeometryData *)ins->base_data;
  2141. if (geom->gi_probes_dirty) {
  2142. //giprobes may be dirty, so update
  2143. int l = 0;
  2144. //only called when reflection probe AABB enter/exit this geometry
  2145. ins->gi_probe_instances.resize(geom->gi_probes.size());
  2146. for (List<Instance *>::Element *F = geom->gi_probes.front(); F; F = F->next()) {
  2147. InstanceGIProbeData *gi_probe2 = static_cast<InstanceGIProbeData *>(F->get()->base_data);
  2148. ins->gi_probe_instances.write[l++] = gi_probe2->probe_instance;
  2149. }
  2150. geom->gi_probes_dirty = false;
  2151. }
  2152. instance_cull_result[instance_cull_count++] = E->get().geometry;
  2153. }
  2154. }
  2155. scene_render->gi_probe_update(probe->probe_instance, update_lights, probe->light_instances, instance_cull_count, (RasterizerScene::InstanceBase **)instance_cull_result);
  2156. gi_probe_update_list.remove(gi_probe);
  2157. gi_probe = next;
  2158. }
  2159. }
  2160. void RenderingServerSceneRaster::render_particle_colliders() {
  2161. while (heightfield_particle_colliders_update_list.front()) {
  2162. Instance *hfpc = heightfield_particle_colliders_update_list.front()->get();
  2163. if (hfpc->scenario && hfpc->base_type == RS::INSTANCE_PARTICLES_COLLISION && RSG::storage->particles_collision_is_heightfield(hfpc->base)) {
  2164. //update heightfield
  2165. int cull_count = hfpc->scenario->octree.cull_aabb(hfpc->transformed_aabb, instance_cull_result, MAX_INSTANCE_CULL); //@TODO: cull mask missing
  2166. for (int i = 0; i < cull_count; i++) {
  2167. Instance *instance = instance_cull_result[i];
  2168. if (!instance->visible || !((1 << instance->base_type) & (RS::INSTANCE_GEOMETRY_MASK & (~(1 << RS::INSTANCE_PARTICLES))))) { //all but particles to avoid self collision
  2169. cull_count--;
  2170. SWAP(instance_cull_result[i], instance_cull_result[cull_count]);
  2171. }
  2172. }
  2173. scene_render->render_particle_collider_heightfield(hfpc->base, hfpc->transform, (RasterizerScene::InstanceBase **)instance_cull_result, cull_count);
  2174. }
  2175. heightfield_particle_colliders_update_list.erase(heightfield_particle_colliders_update_list.front());
  2176. }
  2177. }
  2178. void RenderingServerSceneRaster::_update_instance_shader_parameters_from_material(Map<StringName, RasterizerScene::InstanceBase::InstanceShaderParameter> &isparams, const Map<StringName, RasterizerScene::InstanceBase::InstanceShaderParameter> &existing_isparams, RID p_material) {
  2179. List<RasterizerStorage::InstanceShaderParam> plist;
  2180. RSG::storage->material_get_instance_shader_parameters(p_material, &plist);
  2181. for (List<RasterizerStorage::InstanceShaderParam>::Element *E = plist.front(); E; E = E->next()) {
  2182. StringName name = E->get().info.name;
  2183. if (isparams.has(name)) {
  2184. if (isparams[name].info.type != E->get().info.type) {
  2185. WARN_PRINT("More than one material in instance export the same instance shader uniform '" + E->get().info.name + "', but they do it with different data types. Only the first one (in order) will display correctly.");
  2186. }
  2187. if (isparams[name].index != E->get().index) {
  2188. WARN_PRINT("More than one material in instance export the same instance shader uniform '" + E->get().info.name + "', but they do it with different indices. Only the first one (in order) will display correctly.");
  2189. }
  2190. continue; //first one found always has priority
  2191. }
  2192. RasterizerScene::InstanceBase::InstanceShaderParameter isp;
  2193. isp.index = E->get().index;
  2194. isp.info = E->get().info;
  2195. isp.default_value = E->get().default_value;
  2196. if (existing_isparams.has(name)) {
  2197. isp.value = existing_isparams[name].value;
  2198. } else {
  2199. isp.value = E->get().default_value;
  2200. }
  2201. isparams[name] = isp;
  2202. }
  2203. }
  2204. void RenderingServerSceneRaster::_update_dirty_instance(Instance *p_instance) {
  2205. if (p_instance->update_aabb) {
  2206. _update_instance_aabb(p_instance);
  2207. }
  2208. if (p_instance->update_dependencies) {
  2209. p_instance->instance_increase_version();
  2210. if (p_instance->base.is_valid()) {
  2211. RSG::storage->base_update_dependency(p_instance->base, p_instance);
  2212. }
  2213. if (p_instance->material_override.is_valid()) {
  2214. RSG::storage->material_update_dependency(p_instance->material_override, p_instance);
  2215. }
  2216. if (p_instance->base_type == RS::INSTANCE_MESH) {
  2217. //remove materials no longer used and un-own them
  2218. int new_mat_count = RSG::storage->mesh_get_surface_count(p_instance->base);
  2219. p_instance->materials.resize(new_mat_count);
  2220. int new_blend_shape_count = RSG::storage->mesh_get_blend_shape_count(p_instance->base);
  2221. if (new_blend_shape_count != p_instance->blend_values.size()) {
  2222. p_instance->blend_values.resize(new_blend_shape_count);
  2223. for (int i = 0; i < new_blend_shape_count; i++) {
  2224. p_instance->blend_values.write[i] = 0;
  2225. }
  2226. }
  2227. }
  2228. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  2229. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  2230. bool can_cast_shadows = true;
  2231. bool is_animated = false;
  2232. Map<StringName, RasterizerScene::InstanceBase::InstanceShaderParameter> isparams;
  2233. if (p_instance->cast_shadows == RS::SHADOW_CASTING_SETTING_OFF) {
  2234. can_cast_shadows = false;
  2235. }
  2236. if (p_instance->material_override.is_valid()) {
  2237. if (!RSG::storage->material_casts_shadows(p_instance->material_override)) {
  2238. can_cast_shadows = false;
  2239. }
  2240. is_animated = RSG::storage->material_is_animated(p_instance->material_override);
  2241. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, p_instance->material_override);
  2242. } else {
  2243. if (p_instance->base_type == RS::INSTANCE_MESH) {
  2244. RID mesh = p_instance->base;
  2245. if (mesh.is_valid()) {
  2246. bool cast_shadows = false;
  2247. for (int i = 0; i < p_instance->materials.size(); i++) {
  2248. RID mat = p_instance->materials[i].is_valid() ? p_instance->materials[i] : RSG::storage->mesh_surface_get_material(mesh, i);
  2249. if (!mat.is_valid()) {
  2250. cast_shadows = true;
  2251. } else {
  2252. if (RSG::storage->material_casts_shadows(mat)) {
  2253. cast_shadows = true;
  2254. }
  2255. if (RSG::storage->material_is_animated(mat)) {
  2256. is_animated = true;
  2257. }
  2258. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, mat);
  2259. RSG::storage->material_update_dependency(mat, p_instance);
  2260. }
  2261. }
  2262. if (!cast_shadows) {
  2263. can_cast_shadows = false;
  2264. }
  2265. }
  2266. } else if (p_instance->base_type == RS::INSTANCE_MULTIMESH) {
  2267. RID mesh = RSG::storage->multimesh_get_mesh(p_instance->base);
  2268. if (mesh.is_valid()) {
  2269. bool cast_shadows = false;
  2270. int sc = RSG::storage->mesh_get_surface_count(mesh);
  2271. for (int i = 0; i < sc; i++) {
  2272. RID mat = RSG::storage->mesh_surface_get_material(mesh, i);
  2273. if (!mat.is_valid()) {
  2274. cast_shadows = true;
  2275. } else {
  2276. if (RSG::storage->material_casts_shadows(mat)) {
  2277. cast_shadows = true;
  2278. }
  2279. if (RSG::storage->material_is_animated(mat)) {
  2280. is_animated = true;
  2281. }
  2282. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, mat);
  2283. RSG::storage->material_update_dependency(mat, p_instance);
  2284. }
  2285. }
  2286. if (!cast_shadows) {
  2287. can_cast_shadows = false;
  2288. }
  2289. RSG::storage->base_update_dependency(mesh, p_instance);
  2290. }
  2291. } else if (p_instance->base_type == RS::INSTANCE_IMMEDIATE) {
  2292. RID mat = RSG::storage->immediate_get_material(p_instance->base);
  2293. if (!(!mat.is_valid() || RSG::storage->material_casts_shadows(mat))) {
  2294. can_cast_shadows = false;
  2295. }
  2296. if (mat.is_valid() && RSG::storage->material_is_animated(mat)) {
  2297. is_animated = true;
  2298. }
  2299. if (mat.is_valid()) {
  2300. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, mat);
  2301. }
  2302. if (mat.is_valid()) {
  2303. RSG::storage->material_update_dependency(mat, p_instance);
  2304. }
  2305. } else if (p_instance->base_type == RS::INSTANCE_PARTICLES) {
  2306. bool cast_shadows = false;
  2307. int dp = RSG::storage->particles_get_draw_passes(p_instance->base);
  2308. for (int i = 0; i < dp; i++) {
  2309. RID mesh = RSG::storage->particles_get_draw_pass_mesh(p_instance->base, i);
  2310. if (!mesh.is_valid()) {
  2311. continue;
  2312. }
  2313. int sc = RSG::storage->mesh_get_surface_count(mesh);
  2314. for (int j = 0; j < sc; j++) {
  2315. RID mat = RSG::storage->mesh_surface_get_material(mesh, j);
  2316. if (!mat.is_valid()) {
  2317. cast_shadows = true;
  2318. } else {
  2319. if (RSG::storage->material_casts_shadows(mat)) {
  2320. cast_shadows = true;
  2321. }
  2322. if (RSG::storage->material_is_animated(mat)) {
  2323. is_animated = true;
  2324. }
  2325. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, mat);
  2326. RSG::storage->material_update_dependency(mat, p_instance);
  2327. }
  2328. }
  2329. }
  2330. if (!cast_shadows) {
  2331. can_cast_shadows = false;
  2332. }
  2333. }
  2334. }
  2335. if (can_cast_shadows != geom->can_cast_shadows) {
  2336. //ability to cast shadows change, let lights now
  2337. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  2338. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  2339. light->shadow_dirty = true;
  2340. }
  2341. geom->can_cast_shadows = can_cast_shadows;
  2342. }
  2343. geom->material_is_animated = is_animated;
  2344. p_instance->instance_shader_parameters = isparams;
  2345. if (p_instance->instance_allocated_shader_parameters != (p_instance->instance_shader_parameters.size() > 0)) {
  2346. p_instance->instance_allocated_shader_parameters = (p_instance->instance_shader_parameters.size() > 0);
  2347. if (p_instance->instance_allocated_shader_parameters) {
  2348. p_instance->instance_allocated_shader_parameters_offset = RSG::storage->global_variables_instance_allocate(p_instance->self);
  2349. for (Map<StringName, RasterizerScene::InstanceBase::InstanceShaderParameter>::Element *E = p_instance->instance_shader_parameters.front(); E; E = E->next()) {
  2350. if (E->get().value.get_type() != Variant::NIL) {
  2351. RSG::storage->global_variables_instance_update(p_instance->self, E->get().index, E->get().value);
  2352. }
  2353. }
  2354. } else {
  2355. RSG::storage->global_variables_instance_free(p_instance->self);
  2356. p_instance->instance_allocated_shader_parameters_offset = -1;
  2357. }
  2358. }
  2359. }
  2360. if (p_instance->skeleton.is_valid()) {
  2361. RSG::storage->skeleton_update_dependency(p_instance->skeleton, p_instance);
  2362. }
  2363. p_instance->clean_up_dependencies();
  2364. }
  2365. _instance_update_list.remove(&p_instance->update_item);
  2366. _update_instance(p_instance);
  2367. p_instance->update_aabb = false;
  2368. p_instance->update_dependencies = false;
  2369. }
  2370. void RenderingServerSceneRaster::update_dirty_instances() {
  2371. RSG::storage->update_dirty_resources();
  2372. while (_instance_update_list.first()) {
  2373. _update_dirty_instance(_instance_update_list.first()->self());
  2374. }
  2375. }
  2376. void RenderingServerSceneRaster::update() {
  2377. scene_render->update();
  2378. update_dirty_instances();
  2379. render_particle_colliders();
  2380. }
  2381. bool RenderingServerSceneRaster::free(RID p_rid) {
  2382. if (scene_render->free(p_rid)) {
  2383. return true;
  2384. }
  2385. if (camera_owner.owns(p_rid)) {
  2386. Camera *camera = camera_owner.getornull(p_rid);
  2387. camera_owner.free(p_rid);
  2388. memdelete(camera);
  2389. } else if (scenario_owner.owns(p_rid)) {
  2390. Scenario *scenario = scenario_owner.getornull(p_rid);
  2391. while (scenario->instances.first()) {
  2392. instance_set_scenario(scenario->instances.first()->self()->self, RID());
  2393. }
  2394. scene_render->free(scenario->reflection_probe_shadow_atlas);
  2395. scene_render->free(scenario->reflection_atlas);
  2396. scenario_owner.free(p_rid);
  2397. memdelete(scenario);
  2398. } else if (instance_owner.owns(p_rid)) {
  2399. // delete the instance
  2400. update_dirty_instances();
  2401. Instance *instance = instance_owner.getornull(p_rid);
  2402. instance_geometry_set_lightmap(p_rid, RID(), Rect2(), 0);
  2403. instance_set_scenario(p_rid, RID());
  2404. instance_set_base(p_rid, RID());
  2405. instance_geometry_set_material_override(p_rid, RID());
  2406. instance_attach_skeleton(p_rid, RID());
  2407. if (instance->instance_allocated_shader_parameters) {
  2408. //free the used shader parameters
  2409. RSG::storage->global_variables_instance_free(instance->self);
  2410. }
  2411. update_dirty_instances(); //in case something changed this
  2412. instance_owner.free(p_rid);
  2413. memdelete(instance);
  2414. } else {
  2415. return false;
  2416. }
  2417. return true;
  2418. }
  2419. TypedArray<Image> RenderingServerSceneRaster::bake_render_uv2(RID p_base, const Vector<RID> &p_material_overrides, const Size2i &p_image_size) {
  2420. return scene_render->bake_render_uv2(p_base, p_material_overrides, p_image_size);
  2421. }
  2422. /*******************************/
  2423. /* Passthrough to Scene Render */
  2424. /*******************************/
  2425. /* ENVIRONMENT API */
  2426. RenderingServerSceneRaster *RenderingServerSceneRaster::singleton = nullptr;
  2427. RenderingServerSceneRaster::RenderingServerSceneRaster() {
  2428. render_pass = 1;
  2429. singleton = this;
  2430. }
  2431. RenderingServerSceneRaster::~RenderingServerSceneRaster() {
  2432. }