rendering_server_scene.cpp 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839
  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 NULL;
  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 = NULL;
  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 = NULL;
  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 = NULL;
  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 = NULL;
  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 = NULL;
  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 = NULL;
  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 == NULL)
  561. instance->custom_aabb = memnew(AABB);
  562. *instance->custom_aabb = p_aabb;
  563. } else {
  564. // Clear custom AABB
  565. if (instance->custom_aabb != NULL) {
  566. memdelete(instance->custom_aabb);
  567. instance->custom_aabb = NULL;
  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, 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. float max_distance = p_cam_projection.get_z_far();
  996. float 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. float 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. if (depth_range_mode == RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_OPTIMIZED) {
  1004. //optimize min/max
  1005. Vector<Plane> planes = p_cam_projection.get_projection_planes(p_cam_transform);
  1006. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1007. Plane base(p_cam_transform.origin, -p_cam_transform.basis.get_axis(2));
  1008. //check distance max and min
  1009. bool found_items = false;
  1010. float z_max = -1e20;
  1011. float z_min = 1e20;
  1012. for (int i = 0; i < cull_count; i++) {
  1013. Instance *instance = instance_shadow_cull_result[i];
  1014. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1015. continue;
  1016. }
  1017. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1018. animated_material_found = true;
  1019. }
  1020. float max, min;
  1021. instance->transformed_aabb.project_range_in_plane(base, min, max);
  1022. if (max > z_max) {
  1023. z_max = max;
  1024. }
  1025. if (min < z_min) {
  1026. z_min = min;
  1027. }
  1028. found_items = true;
  1029. }
  1030. if (found_items) {
  1031. min_distance = MAX(min_distance, z_min);
  1032. max_distance = MIN(max_distance, z_max);
  1033. }
  1034. }
  1035. float range = max_distance - min_distance;
  1036. int splits = 0;
  1037. switch (RSG::storage->light_directional_get_shadow_mode(p_instance->base)) {
  1038. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: splits = 1; break;
  1039. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: splits = 2; break;
  1040. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: splits = 4; break;
  1041. }
  1042. float distances[5];
  1043. distances[0] = min_distance;
  1044. for (int i = 0; i < splits; i++) {
  1045. distances[i + 1] = min_distance + RSG::storage->light_get_param(p_instance->base, RS::LightParam(RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET + i)) * range;
  1046. };
  1047. distances[splits] = max_distance;
  1048. float texture_size = RSG::scene_render->get_directional_light_shadow_size(light->instance);
  1049. bool overlap = RSG::storage->light_directional_get_blend_splits(p_instance->base);
  1050. float first_radius = 0.0;
  1051. for (int i = 0; i < splits; i++) {
  1052. RENDER_TIMESTAMP("Culling Directional Light split" + itos(i));
  1053. // setup a camera matrix for that range!
  1054. CameraMatrix camera_matrix;
  1055. float aspect = p_cam_projection.get_aspect();
  1056. if (p_cam_orthogonal) {
  1057. Vector2 vp_he = p_cam_projection.get_viewport_half_extents();
  1058. camera_matrix.set_orthogonal(vp_he.y * 2.0, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  1059. } else {
  1060. float fov = p_cam_projection.get_fov();
  1061. camera_matrix.set_perspective(fov, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  1062. }
  1063. //obtain the frustum endpoints
  1064. Vector3 endpoints[8]; // frustum plane endpoints
  1065. bool res = camera_matrix.get_endpoints(p_cam_transform, endpoints);
  1066. ERR_CONTINUE(!res);
  1067. // obtain the light frustm ranges (given endpoints)
  1068. Transform transform = light_transform; //discard scale and stabilize light
  1069. Vector3 x_vec = transform.basis.get_axis(Vector3::AXIS_X).normalized();
  1070. Vector3 y_vec = transform.basis.get_axis(Vector3::AXIS_Y).normalized();
  1071. Vector3 z_vec = transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  1072. //z_vec points agsint the camera, like in default opengl
  1073. float x_min = 0.f, x_max = 0.f;
  1074. float y_min = 0.f, y_max = 0.f;
  1075. float z_min = 0.f, z_max = 0.f;
  1076. // FIXME: z_max_cam is defined, computed, but not used below when setting up
  1077. // ortho_camera. Commented out for now to fix warnings but should be investigated.
  1078. float x_min_cam = 0.f, x_max_cam = 0.f;
  1079. float y_min_cam = 0.f, y_max_cam = 0.f;
  1080. float z_min_cam = 0.f;
  1081. //float z_max_cam = 0.f;
  1082. float bias_scale = 1.0;
  1083. //used for culling
  1084. for (int j = 0; j < 8; j++) {
  1085. float d_x = x_vec.dot(endpoints[j]);
  1086. float d_y = y_vec.dot(endpoints[j]);
  1087. float d_z = z_vec.dot(endpoints[j]);
  1088. if (j == 0 || d_x < x_min)
  1089. x_min = d_x;
  1090. if (j == 0 || d_x > x_max)
  1091. x_max = d_x;
  1092. if (j == 0 || d_y < y_min)
  1093. y_min = d_y;
  1094. if (j == 0 || d_y > y_max)
  1095. y_max = d_y;
  1096. if (j == 0 || d_z < z_min)
  1097. z_min = d_z;
  1098. if (j == 0 || d_z > z_max)
  1099. z_max = d_z;
  1100. }
  1101. {
  1102. //camera viewport stuff
  1103. Vector3 center;
  1104. for (int j = 0; j < 8; j++) {
  1105. center += endpoints[j];
  1106. }
  1107. center /= 8.0;
  1108. //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;
  1109. float radius = 0;
  1110. for (int j = 0; j < 8; j++) {
  1111. float d = center.distance_to(endpoints[j]);
  1112. if (d > radius)
  1113. radius = d;
  1114. }
  1115. radius *= texture_size / (texture_size - 2.0); //add a texel by each side
  1116. if (i == 0) {
  1117. first_radius = radius;
  1118. } else {
  1119. bias_scale = radius / first_radius;
  1120. }
  1121. x_max_cam = x_vec.dot(center) + radius;
  1122. x_min_cam = x_vec.dot(center) - radius;
  1123. y_max_cam = y_vec.dot(center) + radius;
  1124. y_min_cam = y_vec.dot(center) - radius;
  1125. //z_max_cam = z_vec.dot(center) + radius;
  1126. z_min_cam = z_vec.dot(center) - radius;
  1127. if (depth_range_mode == RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE) {
  1128. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  1129. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  1130. float unit = radius * 2.0 / texture_size;
  1131. x_max_cam = Math::stepify(x_max_cam, unit);
  1132. x_min_cam = Math::stepify(x_min_cam, unit);
  1133. y_max_cam = Math::stepify(y_max_cam, unit);
  1134. y_min_cam = Math::stepify(y_min_cam, unit);
  1135. }
  1136. }
  1137. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  1138. Vector<Plane> light_frustum_planes;
  1139. light_frustum_planes.resize(6);
  1140. //right/left
  1141. light_frustum_planes.write[0] = Plane(x_vec, x_max);
  1142. light_frustum_planes.write[1] = Plane(-x_vec, -x_min);
  1143. //top/bottom
  1144. light_frustum_planes.write[2] = Plane(y_vec, y_max);
  1145. light_frustum_planes.write[3] = Plane(-y_vec, -y_min);
  1146. //near/far
  1147. light_frustum_planes.write[4] = Plane(z_vec, z_max + 1e6);
  1148. light_frustum_planes.write[5] = Plane(-z_vec, -z_min); // z_min is ok, since casters further than far-light plane are not needed
  1149. int cull_count = p_scenario->octree.cull_convex(light_frustum_planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1150. // a pre pass will need to be needed to determine the actual z-near to be used
  1151. Plane near_plane(light_transform.origin, -light_transform.basis.get_axis(2));
  1152. for (int j = 0; j < cull_count; j++) {
  1153. float min, max;
  1154. Instance *instance = instance_shadow_cull_result[j];
  1155. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1156. cull_count--;
  1157. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1158. j--;
  1159. continue;
  1160. }
  1161. instance->transformed_aabb.project_range_in_plane(Plane(z_vec, 0), min, max);
  1162. instance->depth = near_plane.distance_to(instance->transform.origin);
  1163. instance->depth_layer = 0;
  1164. if (max > z_max)
  1165. z_max = max;
  1166. }
  1167. {
  1168. CameraMatrix ortho_camera;
  1169. real_t half_x = (x_max_cam - x_min_cam) * 0.5;
  1170. real_t half_y = (y_max_cam - y_min_cam) * 0.5;
  1171. ortho_camera.set_orthogonal(-half_x, half_x, -half_y, half_y, 0, (z_max - z_min_cam));
  1172. Transform ortho_transform;
  1173. ortho_transform.basis = transform.basis;
  1174. ortho_transform.origin = x_vec * (x_min_cam + half_x) + y_vec * (y_min_cam + half_y) + z_vec * z_max;
  1175. RSG::scene_render->light_instance_set_shadow_transform(light->instance, ortho_camera, ortho_transform, 0, distances[i + 1], i, bias_scale);
  1176. }
  1177. RSG::scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1178. }
  1179. } break;
  1180. case RS::LIGHT_OMNI: {
  1181. RS::LightOmniShadowMode shadow_mode = RSG::storage->light_omni_get_shadow_mode(p_instance->base);
  1182. if (shadow_mode == RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID || !RSG::scene_render->light_instances_can_render_shadow_cube()) {
  1183. for (int i = 0; i < 2; i++) {
  1184. //using this one ensures that raster deferred will have it
  1185. RENDER_TIMESTAMP("Culling Shadow Paraboloid" + itos(i));
  1186. float radius = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1187. float z = i == 0 ? -1 : 1;
  1188. Vector<Plane> planes;
  1189. planes.resize(5);
  1190. planes.write[0] = light_transform.xform(Plane(Vector3(0, 0, z), radius));
  1191. planes.write[1] = light_transform.xform(Plane(Vector3(1, 0, z).normalized(), radius));
  1192. planes.write[2] = light_transform.xform(Plane(Vector3(-1, 0, z).normalized(), radius));
  1193. planes.write[3] = light_transform.xform(Plane(Vector3(0, 1, z).normalized(), radius));
  1194. planes.write[4] = light_transform.xform(Plane(Vector3(0, -1, z).normalized(), radius));
  1195. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1196. Plane near_plane(light_transform.origin, light_transform.basis.get_axis(2) * z);
  1197. for (int j = 0; j < cull_count; j++) {
  1198. Instance *instance = instance_shadow_cull_result[j];
  1199. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1200. cull_count--;
  1201. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1202. j--;
  1203. } else {
  1204. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1205. animated_material_found = true;
  1206. }
  1207. instance->depth = near_plane.distance_to(instance->transform.origin);
  1208. instance->depth_layer = 0;
  1209. }
  1210. }
  1211. RSG::scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), light_transform, radius, 0, i);
  1212. RSG::scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1213. }
  1214. } else { //shadow cube
  1215. float radius = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1216. CameraMatrix cm;
  1217. cm.set_perspective(90, 1, 0.01, radius);
  1218. for (int i = 0; i < 6; i++) {
  1219. RENDER_TIMESTAMP("Culling Shadow Cube side" + itos(i));
  1220. //using this one ensures that raster deferred will have it
  1221. static const Vector3 view_normals[6] = {
  1222. Vector3(+1, 0, 0),
  1223. Vector3(-1, 0, 0),
  1224. Vector3(0, -1, 0),
  1225. Vector3(0, +1, 0),
  1226. Vector3(0, 0, +1),
  1227. Vector3(0, 0, -1)
  1228. };
  1229. static const Vector3 view_up[6] = {
  1230. Vector3(0, -1, 0),
  1231. Vector3(0, -1, 0),
  1232. Vector3(0, 0, -1),
  1233. Vector3(0, 0, +1),
  1234. Vector3(0, -1, 0),
  1235. Vector3(0, -1, 0)
  1236. };
  1237. Transform xform = light_transform * Transform().looking_at(view_normals[i], view_up[i]);
  1238. Vector<Plane> planes = cm.get_projection_planes(xform);
  1239. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1240. Plane near_plane(xform.origin, -xform.basis.get_axis(2));
  1241. for (int j = 0; j < cull_count; j++) {
  1242. Instance *instance = instance_shadow_cull_result[j];
  1243. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1244. cull_count--;
  1245. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1246. j--;
  1247. } else {
  1248. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1249. animated_material_found = true;
  1250. }
  1251. instance->depth = near_plane.distance_to(instance->transform.origin);
  1252. instance->depth_layer = 0;
  1253. }
  1254. }
  1255. RSG::scene_render->light_instance_set_shadow_transform(light->instance, cm, xform, radius, 0, i);
  1256. RSG::scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1257. }
  1258. //restore the regular DP matrix
  1259. RSG::scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), light_transform, radius, 0, 0);
  1260. }
  1261. } break;
  1262. case RS::LIGHT_SPOT: {
  1263. RENDER_TIMESTAMP("Culling Spot Light");
  1264. float radius = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1265. float angle = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1266. CameraMatrix cm;
  1267. cm.set_perspective(angle * 2.0, 1.0, 0.01, radius);
  1268. Vector<Plane> planes = cm.get_projection_planes(light_transform);
  1269. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1270. Plane near_plane(light_transform.origin, -light_transform.basis.get_axis(2));
  1271. for (int j = 0; j < cull_count; j++) {
  1272. Instance *instance = instance_shadow_cull_result[j];
  1273. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1274. cull_count--;
  1275. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1276. j--;
  1277. } else {
  1278. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1279. animated_material_found = true;
  1280. }
  1281. instance->depth = near_plane.distance_to(instance->transform.origin);
  1282. instance->depth_layer = 0;
  1283. }
  1284. }
  1285. RSG::scene_render->light_instance_set_shadow_transform(light->instance, cm, light_transform, radius, 0, 0);
  1286. RSG::scene_render->render_shadow(light->instance, p_shadow_atlas, 0, (RasterizerScene::InstanceBase **)instance_shadow_cull_result, cull_count);
  1287. } break;
  1288. }
  1289. return animated_material_found;
  1290. }
  1291. void RenderingServerScene::render_camera(RID p_render_buffers, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas) {
  1292. // render to mono camera
  1293. #ifndef _3D_DISABLED
  1294. Camera *camera = camera_owner.getornull(p_camera);
  1295. ERR_FAIL_COND(!camera);
  1296. /* STEP 1 - SETUP CAMERA */
  1297. CameraMatrix camera_matrix;
  1298. bool ortho = false;
  1299. switch (camera->type) {
  1300. case Camera::ORTHOGONAL: {
  1301. camera_matrix.set_orthogonal(
  1302. camera->size,
  1303. p_viewport_size.width / (float)p_viewport_size.height,
  1304. camera->znear,
  1305. camera->zfar,
  1306. camera->vaspect);
  1307. ortho = true;
  1308. } break;
  1309. case Camera::PERSPECTIVE: {
  1310. camera_matrix.set_perspective(
  1311. camera->fov,
  1312. p_viewport_size.width / (float)p_viewport_size.height,
  1313. camera->znear,
  1314. camera->zfar,
  1315. camera->vaspect);
  1316. ortho = false;
  1317. } break;
  1318. case Camera::FRUSTUM: {
  1319. camera_matrix.set_frustum(
  1320. camera->size,
  1321. p_viewport_size.width / (float)p_viewport_size.height,
  1322. camera->offset,
  1323. camera->znear,
  1324. camera->zfar,
  1325. camera->vaspect);
  1326. ortho = false;
  1327. } break;
  1328. }
  1329. _prepare_scene(camera->transform, camera_matrix, ortho, camera->env, camera->effects, camera->visible_layers, p_scenario, p_shadow_atlas, RID());
  1330. _render_scene(p_render_buffers, camera->transform, camera_matrix, ortho, camera->env, camera->effects, p_scenario, p_shadow_atlas, RID(), -1);
  1331. #endif
  1332. }
  1333. void RenderingServerScene::render_camera(RID p_render_buffers, Ref<ARVRInterface> &p_interface, ARVRInterface::Eyes p_eye, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas) {
  1334. // render for AR/VR interface
  1335. Camera *camera = camera_owner.getornull(p_camera);
  1336. ERR_FAIL_COND(!camera);
  1337. /* SETUP CAMERA, we are ignoring type and FOV here */
  1338. float aspect = p_viewport_size.width / (float)p_viewport_size.height;
  1339. CameraMatrix camera_matrix = p_interface->get_projection_for_eye(p_eye, aspect, camera->znear, camera->zfar);
  1340. // We also ignore our camera position, it will have been positioned with a slightly old tracking position.
  1341. // Instead we take our origin point and have our ar/vr interface add fresh tracking data! Whoohoo!
  1342. Transform world_origin = ARVRServer::get_singleton()->get_world_origin();
  1343. Transform cam_transform = p_interface->get_transform_for_eye(p_eye, world_origin);
  1344. // For stereo render we only prepare for our left eye and then reuse the outcome for our right eye
  1345. if (p_eye == ARVRInterface::EYE_LEFT) {
  1346. ///@TODO possibly move responsibility for this into our ARVRServer or ARVRInterface?
  1347. // Center our transform, we assume basis is equal.
  1348. Transform mono_transform = cam_transform;
  1349. Transform right_transform = p_interface->get_transform_for_eye(ARVRInterface::EYE_RIGHT, world_origin);
  1350. mono_transform.origin += right_transform.origin;
  1351. mono_transform.origin *= 0.5;
  1352. // We need to combine our projection frustums for culling.
  1353. // Ideally we should use our clipping planes for this and combine them,
  1354. // however our shadow map logic uses our projection matrix.
  1355. // Note: as our left and right frustums should be mirrored, we don't need our right projection matrix.
  1356. // - get some base values we need
  1357. float eye_dist = (mono_transform.origin - cam_transform.origin).length();
  1358. float z_near = camera_matrix.get_z_near(); // get our near plane
  1359. float z_far = camera_matrix.get_z_far(); // get our far plane
  1360. float width = (2.0 * z_near) / camera_matrix.matrix[0][0];
  1361. float x_shift = width * camera_matrix.matrix[2][0];
  1362. float height = (2.0 * z_near) / camera_matrix.matrix[1][1];
  1363. float y_shift = height * camera_matrix.matrix[2][1];
  1364. // printf("Eye_dist = %f, Near = %f, Far = %f, Width = %f, Shift = %f\n", eye_dist, z_near, z_far, width, x_shift);
  1365. // - calculate our near plane size (horizontal only, right_near is mirrored)
  1366. float left_near = -eye_dist - ((width - x_shift) * 0.5);
  1367. // - calculate our far plane size (horizontal only, right_far is mirrored)
  1368. float left_far = -eye_dist - (z_far * (width - x_shift) * 0.5 / z_near);
  1369. float left_far_right_eye = eye_dist - (z_far * (width + x_shift) * 0.5 / z_near);
  1370. if (left_far > left_far_right_eye) {
  1371. // on displays smaller then double our iod, the right eye far frustrum can overtake the left eyes.
  1372. left_far = left_far_right_eye;
  1373. }
  1374. // - figure out required z-shift
  1375. float slope = (left_far - left_near) / (z_far - z_near);
  1376. float z_shift = (left_near / slope) - z_near;
  1377. // - figure out new vertical near plane size (this will be slightly oversized thanks to our z-shift)
  1378. float top_near = (height - y_shift) * 0.5;
  1379. top_near += (top_near / z_near) * z_shift;
  1380. float bottom_near = -(height + y_shift) * 0.5;
  1381. bottom_near += (bottom_near / z_near) * z_shift;
  1382. // 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);
  1383. // - generate our frustum
  1384. CameraMatrix combined_matrix;
  1385. combined_matrix.set_frustum(left_near, -left_near, bottom_near, top_near, z_near + z_shift, z_far + z_shift);
  1386. // and finally move our camera back
  1387. Transform apply_z_shift;
  1388. apply_z_shift.origin = Vector3(0.0, 0.0, z_shift); // z negative is forward so this moves it backwards
  1389. mono_transform *= apply_z_shift;
  1390. // now prepare our scene with our adjusted transform projection matrix
  1391. _prepare_scene(mono_transform, combined_matrix, false, camera->env, camera->effects, camera->visible_layers, p_scenario, p_shadow_atlas, RID());
  1392. } else if (p_eye == ARVRInterface::EYE_MONO) {
  1393. // For mono render, prepare as per usual
  1394. _prepare_scene(cam_transform, camera_matrix, false, camera->env, camera->effects, camera->visible_layers, p_scenario, p_shadow_atlas, RID());
  1395. }
  1396. // And render our scene...
  1397. _render_scene(p_render_buffers, cam_transform, camera_matrix, false, camera->env, camera->effects, p_scenario, p_shadow_atlas, RID(), -1);
  1398. };
  1399. void RenderingServerScene::_prepare_scene(const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, 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) {
  1400. // Note, in stereo rendering:
  1401. // - p_cam_transform will be a transform in the middle of our two eyes
  1402. // - p_cam_projection is a wider frustrum that encompasses both eyes
  1403. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1404. render_pass++;
  1405. uint32_t camera_layer_mask = p_visible_layers;
  1406. RSG::scene_render->set_scene_pass(render_pass);
  1407. RENDER_TIMESTAMP("Frustum Culling");
  1408. //rasterizer->set_camera(camera->transform, camera_matrix,ortho);
  1409. Vector<Plane> planes = p_cam_projection.get_projection_planes(p_cam_transform);
  1410. Plane near_plane(p_cam_transform.origin, -p_cam_transform.basis.get_axis(2).normalized());
  1411. float z_far = p_cam_projection.get_z_far();
  1412. /* STEP 2 - CULL */
  1413. instance_cull_count = scenario->octree.cull_convex(planes, instance_cull_result, MAX_INSTANCE_CULL);
  1414. light_cull_count = 0;
  1415. reflection_probe_cull_count = 0;
  1416. gi_probe_cull_count = 0;
  1417. //light_samplers_culled=0;
  1418. /*
  1419. print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  1420. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  1421. print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  1422. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  1423. */
  1424. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  1425. //removed, will replace with culling
  1426. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  1427. for (int i = 0; i < instance_cull_count; i++) {
  1428. Instance *ins = instance_cull_result[i];
  1429. bool keep = false;
  1430. if ((camera_layer_mask & ins->layer_mask) == 0) {
  1431. //failure
  1432. } else if (ins->base_type == RS::INSTANCE_LIGHT && ins->visible) {
  1433. if (light_cull_count < MAX_LIGHTS_CULLED) {
  1434. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1435. if (!light->geometries.empty()) {
  1436. //do not add this light if no geometry is affected by it..
  1437. light_cull_result[light_cull_count] = ins;
  1438. light_instance_cull_result[light_cull_count] = light->instance;
  1439. if (p_shadow_atlas.is_valid() && RSG::storage->light_has_shadow(ins->base)) {
  1440. RSG::scene_render->light_instance_mark_visible(light->instance); //mark it visible for shadow allocation later
  1441. }
  1442. light_cull_count++;
  1443. }
  1444. }
  1445. } else if (ins->base_type == RS::INSTANCE_REFLECTION_PROBE && ins->visible) {
  1446. if (reflection_probe_cull_count < MAX_REFLECTION_PROBES_CULLED) {
  1447. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(ins->base_data);
  1448. if (p_reflection_probe != reflection_probe->instance) {
  1449. //avoid entering The Matrix
  1450. if (!reflection_probe->geometries.empty()) {
  1451. //do not add this light if no geometry is affected by it..
  1452. if (reflection_probe->reflection_dirty || RSG::scene_render->reflection_probe_instance_needs_redraw(reflection_probe->instance)) {
  1453. if (!reflection_probe->update_list.in_list()) {
  1454. reflection_probe->render_step = 0;
  1455. reflection_probe_render_list.add_last(&reflection_probe->update_list);
  1456. }
  1457. reflection_probe->reflection_dirty = false;
  1458. }
  1459. if (RSG::scene_render->reflection_probe_instance_has_reflection(reflection_probe->instance)) {
  1460. reflection_probe_instance_cull_result[reflection_probe_cull_count] = reflection_probe->instance;
  1461. reflection_probe_cull_count++;
  1462. }
  1463. }
  1464. }
  1465. }
  1466. } else if (ins->base_type == RS::INSTANCE_GI_PROBE && ins->visible) {
  1467. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(ins->base_data);
  1468. if (!gi_probe->update_element.in_list()) {
  1469. gi_probe_update_list.add(&gi_probe->update_element);
  1470. }
  1471. if (gi_probe_cull_count < MAX_GI_PROBES_CULLED) {
  1472. gi_probe_instance_cull_result[gi_probe_cull_count] = gi_probe->probe_instance;
  1473. gi_probe_cull_count++;
  1474. }
  1475. } else if (((1 << ins->base_type) & RS::INSTANCE_GEOMETRY_MASK) && ins->visible && ins->cast_shadows != RS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) {
  1476. keep = true;
  1477. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(ins->base_data);
  1478. if (ins->redraw_if_visible) {
  1479. RenderingServerRaster::redraw_request();
  1480. }
  1481. if (ins->base_type == RS::INSTANCE_PARTICLES) {
  1482. //particles visible? process them
  1483. if (RSG::storage->particles_is_inactive(ins->base)) {
  1484. //but if nothing is going on, don't do it.
  1485. keep = false;
  1486. } else {
  1487. RSG::storage->particles_request_process(ins->base);
  1488. //particles visible? request redraw
  1489. RenderingServerRaster::redraw_request();
  1490. }
  1491. }
  1492. if (geom->lighting_dirty) {
  1493. int l = 0;
  1494. //only called when lights AABB enter/exit this geometry
  1495. ins->light_instances.resize(geom->lighting.size());
  1496. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  1497. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1498. ins->light_instances.write[l++] = light->instance;
  1499. }
  1500. geom->lighting_dirty = false;
  1501. }
  1502. if (geom->reflection_dirty) {
  1503. int l = 0;
  1504. //only called when reflection probe AABB enter/exit this geometry
  1505. ins->reflection_probe_instances.resize(geom->reflection_probes.size());
  1506. for (List<Instance *>::Element *E = geom->reflection_probes.front(); E; E = E->next()) {
  1507. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(E->get()->base_data);
  1508. ins->reflection_probe_instances.write[l++] = reflection_probe->instance;
  1509. }
  1510. geom->reflection_dirty = false;
  1511. }
  1512. if (geom->gi_probes_dirty) {
  1513. int l = 0;
  1514. //only called when reflection probe AABB enter/exit this geometry
  1515. ins->gi_probe_instances.resize(geom->gi_probes.size());
  1516. for (List<Instance *>::Element *E = geom->gi_probes.front(); E; E = E->next()) {
  1517. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(E->get()->base_data);
  1518. ins->gi_probe_instances.write[l++] = gi_probe->probe_instance;
  1519. }
  1520. geom->gi_probes_dirty = false;
  1521. }
  1522. ins->depth = near_plane.distance_to(ins->transform.origin);
  1523. ins->depth_layer = CLAMP(int(ins->depth * 16 / z_far), 0, 15);
  1524. }
  1525. if (!keep) {
  1526. // remove, no reason to keep
  1527. instance_cull_count--;
  1528. SWAP(instance_cull_result[i], instance_cull_result[instance_cull_count]);
  1529. i--;
  1530. ins->last_render_pass = 0; // make invalid
  1531. } else {
  1532. ins->last_render_pass = render_pass;
  1533. }
  1534. }
  1535. /* STEP 5 - PROCESS LIGHTS */
  1536. RID *directional_light_ptr = &light_instance_cull_result[light_cull_count];
  1537. directional_light_count = 0;
  1538. // directional lights
  1539. {
  1540. Instance **lights_with_shadow = (Instance **)alloca(sizeof(Instance *) * scenario->directional_lights.size());
  1541. int directional_shadow_count = 0;
  1542. for (List<Instance *>::Element *E = scenario->directional_lights.front(); E; E = E->next()) {
  1543. if (light_cull_count + directional_light_count >= MAX_LIGHTS_CULLED) {
  1544. break;
  1545. }
  1546. if (!E->get()->visible)
  1547. continue;
  1548. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1549. //check shadow..
  1550. if (light) {
  1551. if (p_using_shadows && p_shadow_atlas.is_valid() && RSG::storage->light_has_shadow(E->get()->base)) {
  1552. lights_with_shadow[directional_shadow_count++] = E->get();
  1553. }
  1554. //add to list
  1555. directional_light_ptr[directional_light_count++] = light->instance;
  1556. }
  1557. }
  1558. RSG::scene_render->set_directional_shadow_count(directional_shadow_count);
  1559. for (int i = 0; i < directional_shadow_count; i++) {
  1560. RENDER_TIMESTAMP(">Rendering Directional Light " + itos(i));
  1561. _light_instance_update_shadow(lights_with_shadow[i], p_cam_transform, p_cam_projection, p_cam_orthogonal, p_shadow_atlas, scenario);
  1562. RENDER_TIMESTAMP("<Rendering Directional Light " + itos(i));
  1563. }
  1564. }
  1565. if (p_using_shadows) { //setup shadow maps
  1566. //SortArray<Instance*,_InstanceLightsort> sorter;
  1567. //sorter.sort(light_cull_result,light_cull_count);
  1568. for (int i = 0; i < light_cull_count; i++) {
  1569. Instance *ins = light_cull_result[i];
  1570. if (!p_shadow_atlas.is_valid() || !RSG::storage->light_has_shadow(ins->base))
  1571. continue;
  1572. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1573. float coverage = 0.f;
  1574. { //compute coverage
  1575. Transform cam_xf = p_cam_transform;
  1576. float zn = p_cam_projection.get_z_near();
  1577. Plane p(cam_xf.origin + cam_xf.basis.get_axis(2) * -zn, -cam_xf.basis.get_axis(2)); //camera near plane
  1578. // near plane half width and height
  1579. Vector2 vp_half_extents = p_cam_projection.get_viewport_half_extents();
  1580. switch (RSG::storage->light_get_type(ins->base)) {
  1581. case RS::LIGHT_OMNI: {
  1582. float radius = RSG::storage->light_get_param(ins->base, RS::LIGHT_PARAM_RANGE);
  1583. //get two points parallel to near plane
  1584. Vector3 points[2] = {
  1585. ins->transform.origin,
  1586. ins->transform.origin + cam_xf.basis.get_axis(0) * radius
  1587. };
  1588. if (!p_cam_orthogonal) {
  1589. //if using perspetive, map them to near plane
  1590. for (int j = 0; j < 2; j++) {
  1591. if (p.distance_to(points[j]) < 0) {
  1592. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1593. }
  1594. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1595. }
  1596. }
  1597. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1598. coverage = screen_diameter / (vp_half_extents.x + vp_half_extents.y);
  1599. } break;
  1600. case RS::LIGHT_SPOT: {
  1601. float radius = RSG::storage->light_get_param(ins->base, RS::LIGHT_PARAM_RANGE);
  1602. float angle = RSG::storage->light_get_param(ins->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1603. float w = radius * Math::sin(Math::deg2rad(angle));
  1604. float d = radius * Math::cos(Math::deg2rad(angle));
  1605. Vector3 base = ins->transform.origin - ins->transform.basis.get_axis(2).normalized() * d;
  1606. Vector3 points[2] = {
  1607. base,
  1608. base + cam_xf.basis.get_axis(0) * w
  1609. };
  1610. if (!p_cam_orthogonal) {
  1611. //if using perspetive, map them to near plane
  1612. for (int j = 0; j < 2; j++) {
  1613. if (p.distance_to(points[j]) < 0) {
  1614. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1615. }
  1616. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1617. }
  1618. }
  1619. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1620. coverage = screen_diameter / (vp_half_extents.x + vp_half_extents.y);
  1621. } break;
  1622. default: {
  1623. ERR_PRINT("Invalid Light Type");
  1624. }
  1625. }
  1626. }
  1627. if (light->shadow_dirty) {
  1628. light->last_version++;
  1629. light->shadow_dirty = false;
  1630. }
  1631. bool redraw = RSG::scene_render->shadow_atlas_update_light(p_shadow_atlas, light->instance, coverage, light->last_version);
  1632. if (redraw) {
  1633. //must redraw!
  1634. RENDER_TIMESTAMP(">Rendering Light " + itos(i));
  1635. light->shadow_dirty = _light_instance_update_shadow(ins, p_cam_transform, p_cam_projection, p_cam_orthogonal, p_shadow_atlas, scenario);
  1636. RENDER_TIMESTAMP("<Rendering Light " + itos(i));
  1637. }
  1638. }
  1639. }
  1640. }
  1641. 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) {
  1642. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1643. /* ENVIRONMENT */
  1644. RID environment;
  1645. if (p_force_environment.is_valid()) //camera has more environment priority
  1646. environment = p_force_environment;
  1647. else if (scenario->environment.is_valid())
  1648. environment = scenario->environment;
  1649. else
  1650. environment = scenario->fallback_environment;
  1651. RID camera_effects;
  1652. if (p_force_camera_effects.is_valid()) {
  1653. camera_effects = p_force_camera_effects;
  1654. } else {
  1655. camera_effects = scenario->camera_effects;
  1656. }
  1657. /* PROCESS GEOMETRY AND DRAW SCENE */
  1658. RENDER_TIMESTAMP("Render Scene ");
  1659. 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);
  1660. }
  1661. void RenderingServerScene::render_empty_scene(RID p_render_buffers, RID p_scenario, RID p_shadow_atlas) {
  1662. #ifndef _3D_DISABLED
  1663. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1664. RID environment;
  1665. if (scenario->environment.is_valid())
  1666. environment = scenario->environment;
  1667. else
  1668. environment = scenario->fallback_environment;
  1669. RENDER_TIMESTAMP("Render Empty Scene ");
  1670. RSG::scene_render->render_scene(p_render_buffers, Transform(), CameraMatrix(), true, NULL, 0, NULL, 0, NULL, 0, NULL, 0, environment, RID(), p_shadow_atlas, scenario->reflection_atlas, RID(), 0);
  1671. #endif
  1672. }
  1673. bool RenderingServerScene::_render_reflection_probe_step(Instance *p_instance, int p_step) {
  1674. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  1675. Scenario *scenario = p_instance->scenario;
  1676. ERR_FAIL_COND_V(!scenario, true);
  1677. RenderingServerRaster::redraw_request(); //update, so it updates in editor
  1678. if (p_step == 0) {
  1679. if (!RSG::scene_render->reflection_probe_instance_begin_render(reflection_probe->instance, scenario->reflection_atlas)) {
  1680. return true; //all full
  1681. }
  1682. }
  1683. if (p_step >= 0 && p_step < 6) {
  1684. static const Vector3 view_normals[6] = {
  1685. Vector3(+1, 0, 0),
  1686. Vector3(-1, 0, 0),
  1687. Vector3(0, +1, 0),
  1688. Vector3(0, -1, 0),
  1689. Vector3(0, 0, +1),
  1690. Vector3(0, 0, -1)
  1691. };
  1692. static const Vector3 view_up[6] = {
  1693. Vector3(0, -1, 0),
  1694. Vector3(0, -1, 0),
  1695. Vector3(0, 0, +1),
  1696. Vector3(0, 0, -1),
  1697. Vector3(0, -1, 0),
  1698. Vector3(0, -1, 0)
  1699. };
  1700. Vector3 extents = RSG::storage->reflection_probe_get_extents(p_instance->base);
  1701. Vector3 origin_offset = RSG::storage->reflection_probe_get_origin_offset(p_instance->base);
  1702. float max_distance = RSG::storage->reflection_probe_get_origin_max_distance(p_instance->base);
  1703. Vector3 edge = view_normals[p_step] * extents;
  1704. 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
  1705. max_distance = MAX(max_distance, distance);
  1706. //render cubemap side
  1707. CameraMatrix cm;
  1708. cm.set_perspective(90, 1, 0.01, max_distance);
  1709. Transform local_view;
  1710. local_view.set_look_at(origin_offset, origin_offset + view_normals[p_step], view_up[p_step]);
  1711. Transform xform = p_instance->transform * local_view;
  1712. RID shadow_atlas;
  1713. bool use_shadows = RSG::storage->reflection_probe_renders_shadows(p_instance->base);
  1714. if (use_shadows) {
  1715. shadow_atlas = scenario->reflection_probe_shadow_atlas;
  1716. }
  1717. RENDER_TIMESTAMP("Render Reflection Probe, Step " + itos(p_step));
  1718. _prepare_scene(xform, cm, false, RID(), RID(), RSG::storage->reflection_probe_get_cull_mask(p_instance->base), p_instance->scenario->self, shadow_atlas, reflection_probe->instance, use_shadows);
  1719. _render_scene(RID(), xform, cm, false, RID(), RID(), p_instance->scenario->self, shadow_atlas, reflection_probe->instance, p_step);
  1720. } else {
  1721. //do roughness postprocess step until it believes it's done
  1722. RENDER_TIMESTAMP("Post-Process Reflection Probe, Step " + itos(p_step));
  1723. return RSG::scene_render->reflection_probe_instance_postprocess_step(reflection_probe->instance);
  1724. }
  1725. return false;
  1726. }
  1727. void RenderingServerScene::render_probes() {
  1728. /* REFLECTION PROBES */
  1729. SelfList<InstanceReflectionProbeData> *ref_probe = reflection_probe_render_list.first();
  1730. bool busy = false;
  1731. while (ref_probe) {
  1732. SelfList<InstanceReflectionProbeData> *next = ref_probe->next();
  1733. RID base = ref_probe->self()->owner->base;
  1734. switch (RSG::storage->reflection_probe_get_update_mode(base)) {
  1735. case RS::REFLECTION_PROBE_UPDATE_ONCE: {
  1736. if (busy) //already rendering something
  1737. break;
  1738. bool done = _render_reflection_probe_step(ref_probe->self()->owner, ref_probe->self()->render_step);
  1739. if (done) {
  1740. reflection_probe_render_list.remove(ref_probe);
  1741. } else {
  1742. ref_probe->self()->render_step++;
  1743. }
  1744. busy = true; //do not render another one of this kind
  1745. } break;
  1746. case RS::REFLECTION_PROBE_UPDATE_ALWAYS: {
  1747. int step = 0;
  1748. bool done = false;
  1749. while (!done) {
  1750. done = _render_reflection_probe_step(ref_probe->self()->owner, step);
  1751. step++;
  1752. }
  1753. reflection_probe_render_list.remove(ref_probe);
  1754. } break;
  1755. }
  1756. ref_probe = next;
  1757. }
  1758. /* GI PROBES */
  1759. SelfList<InstanceGIProbeData> *gi_probe = gi_probe_update_list.first();
  1760. if (gi_probe) {
  1761. RENDER_TIMESTAMP("Render GI Probes");
  1762. }
  1763. while (gi_probe) {
  1764. SelfList<InstanceGIProbeData> *next = gi_probe->next();
  1765. InstanceGIProbeData *probe = gi_probe->self();
  1766. //Instance *instance_probe = probe->owner;
  1767. //check if probe must be setup, but don't do if on the lighting thread
  1768. bool cache_dirty = false;
  1769. int cache_count = 0;
  1770. {
  1771. int light_cache_size = probe->light_cache.size();
  1772. const InstanceGIProbeData::LightCache *caches = probe->light_cache.ptr();
  1773. const RID *instance_caches = probe->light_instances.ptr();
  1774. int idx = 0; //must count visible lights
  1775. for (Set<Instance *>::Element *E = probe->lights.front(); E; E = E->next()) {
  1776. Instance *instance = E->get();
  1777. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  1778. if (!instance->visible) {
  1779. continue;
  1780. }
  1781. if (cache_dirty) {
  1782. //do nothing, since idx must count all visible lights anyway
  1783. } else if (idx >= light_cache_size) {
  1784. cache_dirty = true;
  1785. } else {
  1786. const InstanceGIProbeData::LightCache *cache = &caches[idx];
  1787. if (
  1788. instance_caches[idx] != instance_light->instance ||
  1789. cache->has_shadow != RSG::storage->light_has_shadow(instance->base) ||
  1790. cache->type != RSG::storage->light_get_type(instance->base) ||
  1791. cache->transform != instance->transform ||
  1792. cache->color != RSG::storage->light_get_color(instance->base) ||
  1793. cache->energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY) ||
  1794. cache->bake_energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY) ||
  1795. cache->radius != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE) ||
  1796. cache->attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION) ||
  1797. cache->spot_angle != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE) ||
  1798. cache->spot_attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION)) {
  1799. cache_dirty = true;
  1800. }
  1801. }
  1802. idx++;
  1803. }
  1804. for (List<Instance *>::Element *E = probe->owner->scenario->directional_lights.front(); E; E = E->next()) {
  1805. Instance *instance = E->get();
  1806. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  1807. if (!instance->visible) {
  1808. continue;
  1809. }
  1810. if (cache_dirty) {
  1811. //do nothing, since idx must count all visible lights anyway
  1812. } else if (idx >= light_cache_size) {
  1813. cache_dirty = true;
  1814. } else {
  1815. const InstanceGIProbeData::LightCache *cache = &caches[idx];
  1816. if (
  1817. instance_caches[idx] != instance_light->instance ||
  1818. cache->has_shadow != RSG::storage->light_has_shadow(instance->base) ||
  1819. cache->type != RSG::storage->light_get_type(instance->base) ||
  1820. cache->transform != instance->transform ||
  1821. cache->color != RSG::storage->light_get_color(instance->base) ||
  1822. cache->energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY) ||
  1823. cache->bake_energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY) ||
  1824. cache->radius != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE) ||
  1825. cache->attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION) ||
  1826. cache->spot_angle != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE) ||
  1827. cache->spot_attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION)) {
  1828. cache_dirty = true;
  1829. }
  1830. }
  1831. idx++;
  1832. }
  1833. if (idx != light_cache_size) {
  1834. cache_dirty = true;
  1835. }
  1836. cache_count = idx;
  1837. }
  1838. bool update_lights = RSG::scene_render->gi_probe_needs_update(probe->probe_instance);
  1839. if (cache_dirty) {
  1840. probe->light_cache.resize(cache_count);
  1841. probe->light_instances.resize(cache_count);
  1842. if (cache_count) {
  1843. InstanceGIProbeData::LightCache *caches = probe->light_cache.ptrw();
  1844. RID *instance_caches = probe->light_instances.ptrw();
  1845. int idx = 0; //must count visible lights
  1846. for (Set<Instance *>::Element *E = probe->lights.front(); E; E = E->next()) {
  1847. Instance *instance = E->get();
  1848. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  1849. if (!instance->visible) {
  1850. continue;
  1851. }
  1852. InstanceGIProbeData::LightCache *cache = &caches[idx];
  1853. instance_caches[idx] = instance_light->instance;
  1854. cache->has_shadow = RSG::storage->light_has_shadow(instance->base);
  1855. cache->type = RSG::storage->light_get_type(instance->base);
  1856. cache->transform = instance->transform;
  1857. cache->color = RSG::storage->light_get_color(instance->base);
  1858. cache->energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY);
  1859. cache->bake_energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  1860. cache->radius = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE);
  1861. cache->attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION);
  1862. cache->spot_angle = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1863. cache->spot_attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  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. InstanceGIProbeData::LightCache *cache = &caches[idx];
  1873. instance_caches[idx] = instance_light->instance;
  1874. cache->has_shadow = RSG::storage->light_has_shadow(instance->base);
  1875. cache->type = RSG::storage->light_get_type(instance->base);
  1876. cache->transform = instance->transform;
  1877. cache->color = RSG::storage->light_get_color(instance->base);
  1878. cache->energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY);
  1879. cache->bake_energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  1880. cache->radius = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE);
  1881. cache->attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION);
  1882. cache->spot_angle = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1883. cache->spot_attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  1884. idx++;
  1885. }
  1886. }
  1887. update_lights = true;
  1888. }
  1889. instance_cull_count = 0;
  1890. for (List<InstanceGIProbeData::PairInfo>::Element *E = probe->dynamic_geometries.front(); E; E = E->next()) {
  1891. if (instance_cull_count < MAX_INSTANCE_CULL) {
  1892. Instance *ins = E->get().geometry;
  1893. if (!ins->visible) {
  1894. continue;
  1895. }
  1896. InstanceGeometryData *geom = (InstanceGeometryData *)ins->base_data;
  1897. if (geom->gi_probes_dirty) {
  1898. //giprobes may be dirty, so update
  1899. int l = 0;
  1900. //only called when reflection probe AABB enter/exit this geometry
  1901. ins->gi_probe_instances.resize(geom->gi_probes.size());
  1902. for (List<Instance *>::Element *F = geom->gi_probes.front(); F; F = F->next()) {
  1903. InstanceGIProbeData *gi_probe2 = static_cast<InstanceGIProbeData *>(F->get()->base_data);
  1904. ins->gi_probe_instances.write[l++] = gi_probe2->probe_instance;
  1905. }
  1906. geom->gi_probes_dirty = false;
  1907. }
  1908. instance_cull_result[instance_cull_count++] = E->get().geometry;
  1909. }
  1910. }
  1911. RSG::scene_render->gi_probe_update(probe->probe_instance, update_lights, probe->light_instances, instance_cull_count, (RasterizerScene::InstanceBase **)instance_cull_result);
  1912. gi_probe_update_list.remove(gi_probe);
  1913. gi_probe = next;
  1914. }
  1915. }
  1916. void RenderingServerScene::_update_dirty_instance(Instance *p_instance) {
  1917. if (p_instance->update_aabb) {
  1918. _update_instance_aabb(p_instance);
  1919. }
  1920. if (p_instance->update_dependencies) {
  1921. p_instance->instance_increase_version();
  1922. if (p_instance->base.is_valid()) {
  1923. RSG::storage->base_update_dependency(p_instance->base, p_instance);
  1924. }
  1925. if (p_instance->material_override.is_valid()) {
  1926. RSG::storage->material_update_dependency(p_instance->material_override, p_instance);
  1927. }
  1928. if (p_instance->base_type == RS::INSTANCE_MESH) {
  1929. //remove materials no longer used and un-own them
  1930. int new_mat_count = RSG::storage->mesh_get_surface_count(p_instance->base);
  1931. p_instance->materials.resize(new_mat_count);
  1932. int new_blend_shape_count = RSG::storage->mesh_get_blend_shape_count(p_instance->base);
  1933. if (new_blend_shape_count != p_instance->blend_values.size()) {
  1934. p_instance->blend_values.resize(new_blend_shape_count);
  1935. for (int i = 0; i < new_blend_shape_count; i++) {
  1936. p_instance->blend_values.write[i] = 0;
  1937. }
  1938. }
  1939. }
  1940. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1941. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  1942. bool can_cast_shadows = true;
  1943. bool is_animated = false;
  1944. if (p_instance->cast_shadows == RS::SHADOW_CASTING_SETTING_OFF) {
  1945. can_cast_shadows = false;
  1946. } else if (p_instance->material_override.is_valid()) {
  1947. can_cast_shadows = RSG::storage->material_casts_shadows(p_instance->material_override);
  1948. is_animated = RSG::storage->material_is_animated(p_instance->material_override);
  1949. } else {
  1950. if (p_instance->base_type == RS::INSTANCE_MESH) {
  1951. RID mesh = p_instance->base;
  1952. if (mesh.is_valid()) {
  1953. bool cast_shadows = false;
  1954. for (int i = 0; i < p_instance->materials.size(); i++) {
  1955. RID mat = p_instance->materials[i].is_valid() ? p_instance->materials[i] : RSG::storage->mesh_surface_get_material(mesh, i);
  1956. if (!mat.is_valid()) {
  1957. cast_shadows = true;
  1958. } else {
  1959. if (RSG::storage->material_casts_shadows(mat)) {
  1960. cast_shadows = true;
  1961. }
  1962. if (RSG::storage->material_is_animated(mat)) {
  1963. is_animated = true;
  1964. }
  1965. RSG::storage->material_update_dependency(mat, p_instance);
  1966. }
  1967. }
  1968. if (!cast_shadows) {
  1969. can_cast_shadows = false;
  1970. }
  1971. }
  1972. } else if (p_instance->base_type == RS::INSTANCE_MULTIMESH) {
  1973. RID mesh = RSG::storage->multimesh_get_mesh(p_instance->base);
  1974. if (mesh.is_valid()) {
  1975. bool cast_shadows = false;
  1976. int sc = RSG::storage->mesh_get_surface_count(mesh);
  1977. for (int i = 0; i < sc; i++) {
  1978. RID mat = RSG::storage->mesh_surface_get_material(mesh, i);
  1979. if (!mat.is_valid()) {
  1980. cast_shadows = true;
  1981. } else {
  1982. if (RSG::storage->material_casts_shadows(mat)) {
  1983. cast_shadows = true;
  1984. }
  1985. if (RSG::storage->material_is_animated(mat)) {
  1986. is_animated = true;
  1987. }
  1988. RSG::storage->material_update_dependency(mat, p_instance);
  1989. }
  1990. }
  1991. if (!cast_shadows) {
  1992. can_cast_shadows = false;
  1993. }
  1994. RSG::storage->base_update_dependency(mesh, p_instance);
  1995. }
  1996. } else if (p_instance->base_type == RS::INSTANCE_IMMEDIATE) {
  1997. RID mat = RSG::storage->immediate_get_material(p_instance->base);
  1998. can_cast_shadows = !mat.is_valid() || RSG::storage->material_casts_shadows(mat);
  1999. if (mat.is_valid() && RSG::storage->material_is_animated(mat)) {
  2000. is_animated = true;
  2001. }
  2002. if (mat.is_valid()) {
  2003. RSG::storage->material_update_dependency(mat, p_instance);
  2004. }
  2005. } else if (p_instance->base_type == RS::INSTANCE_PARTICLES) {
  2006. bool cast_shadows = false;
  2007. int dp = RSG::storage->particles_get_draw_passes(p_instance->base);
  2008. for (int i = 0; i < dp; i++) {
  2009. RID mesh = RSG::storage->particles_get_draw_pass_mesh(p_instance->base, i);
  2010. if (!mesh.is_valid())
  2011. continue;
  2012. int sc = RSG::storage->mesh_get_surface_count(mesh);
  2013. for (int j = 0; j < sc; j++) {
  2014. RID mat = RSG::storage->mesh_surface_get_material(mesh, j);
  2015. if (!mat.is_valid()) {
  2016. cast_shadows = true;
  2017. } else {
  2018. if (RSG::storage->material_casts_shadows(mat)) {
  2019. cast_shadows = true;
  2020. }
  2021. if (RSG::storage->material_is_animated(mat)) {
  2022. is_animated = true;
  2023. }
  2024. RSG::storage->material_update_dependency(mat, p_instance);
  2025. }
  2026. }
  2027. }
  2028. if (!cast_shadows) {
  2029. can_cast_shadows = false;
  2030. }
  2031. }
  2032. }
  2033. if (can_cast_shadows != geom->can_cast_shadows) {
  2034. //ability to cast shadows change, let lights now
  2035. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  2036. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  2037. light->shadow_dirty = true;
  2038. }
  2039. geom->can_cast_shadows = can_cast_shadows;
  2040. }
  2041. geom->material_is_animated = is_animated;
  2042. }
  2043. if (p_instance->skeleton.is_valid()) {
  2044. RSG::storage->skeleton_update_dependency(p_instance->skeleton, p_instance);
  2045. }
  2046. p_instance->clean_up_dependencies();
  2047. }
  2048. _instance_update_list.remove(&p_instance->update_item);
  2049. _update_instance(p_instance);
  2050. p_instance->update_aabb = false;
  2051. p_instance->update_dependencies = false;
  2052. }
  2053. void RenderingServerScene::update_dirty_instances() {
  2054. RSG::storage->update_dirty_resources();
  2055. while (_instance_update_list.first()) {
  2056. _update_dirty_instance(_instance_update_list.first()->self());
  2057. }
  2058. }
  2059. bool RenderingServerScene::free(RID p_rid) {
  2060. if (camera_owner.owns(p_rid)) {
  2061. Camera *camera = camera_owner.getornull(p_rid);
  2062. camera_owner.free(p_rid);
  2063. memdelete(camera);
  2064. } else if (scenario_owner.owns(p_rid)) {
  2065. Scenario *scenario = scenario_owner.getornull(p_rid);
  2066. while (scenario->instances.first()) {
  2067. instance_set_scenario(scenario->instances.first()->self()->self, RID());
  2068. }
  2069. RSG::scene_render->free(scenario->reflection_probe_shadow_atlas);
  2070. RSG::scene_render->free(scenario->reflection_atlas);
  2071. scenario_owner.free(p_rid);
  2072. memdelete(scenario);
  2073. } else if (instance_owner.owns(p_rid)) {
  2074. // delete the instance
  2075. update_dirty_instances();
  2076. Instance *instance = instance_owner.getornull(p_rid);
  2077. instance_set_use_lightmap(p_rid, RID(), RID());
  2078. instance_set_scenario(p_rid, RID());
  2079. instance_set_base(p_rid, RID());
  2080. instance_geometry_set_material_override(p_rid, RID());
  2081. instance_attach_skeleton(p_rid, RID());
  2082. update_dirty_instances(); //in case something changed this
  2083. instance_owner.free(p_rid);
  2084. memdelete(instance);
  2085. } else {
  2086. return false;
  2087. }
  2088. return true;
  2089. }
  2090. RenderingServerScene *RenderingServerScene::singleton = NULL;
  2091. RenderingServerScene::RenderingServerScene() {
  2092. render_pass = 1;
  2093. singleton = this;
  2094. }
  2095. RenderingServerScene::~RenderingServerScene() {
  2096. }