2
0

rendering_server_scene.cpp 110 KB

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