rendering_server_scene.cpp 98 KB

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