llenvironment.cpp 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993
  1. /**
  2. * @file llenvironment.cpp
  3. * @brief Extended environment manager class implementation.
  4. *
  5. * $LicenseInfo:firstyear=2018&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2019, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. // "error: 'get_temporary_buffer<...>' is deprecated" seen with clang 18 and
  34. // gcc 12.3 libstdc++ implementation of std::stable_sort(). HB
  35. #if CLANG_VERSION >= 180000
  36. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  37. #endif
  38. #include <deque>
  39. #include "llenvironment.h"
  40. #include "llapp.h"
  41. #include "llcorehttputil.h"
  42. #include "lldispatcher.h"
  43. #include "lldir.h"
  44. #include "llnotifications.h"
  45. #include "llparcel.h"
  46. #include "hbtracy.h"
  47. #include "lltrans.h"
  48. #include "llagent.h"
  49. #include "llenvsettings.h"
  50. #include "llexperiencelog.h"
  51. #include "llpipeline.h"
  52. #include "llsky.h"
  53. #include "llstartup.h" // For getStartupState()
  54. #include "llviewercamera.h"
  55. #include "llviewerdisplay.h" // For gCubeSnapshot
  56. #include "llviewercontrol.h"
  57. #include "llviewermessage.h"
  58. #include "llviewerparcelmgr.h"
  59. #include "llviewerregion.h"
  60. #include "llviewershadermgr.h"
  61. #include "roles_constants.h"
  62. LLEnvironment gEnvironment;
  63. static const std::string KEY_ENVIRONMENT = "environment";
  64. static const std::string KEY_DAYASSET = "day_asset";
  65. static const std::string KEY_DAYCYCLE = "day_cycle";
  66. static const std::string KEY_DAYHASH = "day_hash";
  67. static const std::string KEY_DAYLENGTH = "day_length";
  68. static const std::string KEY_DAYNAME = "day_name";
  69. static const std::string KEY_DAYNAMES = "day_names";
  70. static const std::string KEY_DAYOFFSET = "day_offset";
  71. static const std::string KEY_ENVVERSION = "env_version";
  72. static const std::string KEY_ISDEFAULT = "is_default";
  73. static const std::string KEY_PARCELID = "parcel_id";
  74. static const std::string KEY_REGIONID = "region_id";
  75. static const std::string KEY_TRACKALTS = "track_altitudes";
  76. static const std::string KEY_FLAGS = "flags";
  77. static const std::string MESSAGE_PUSHENVIRONMENT = "PushExpEnvironment";
  78. static const std::string ACTION_CLEARENVIRONMENT = "ClearEnvironment";
  79. static const std::string ACTION_PUSHFULLENVIRONMENT = "PushFullEnvironment";
  80. static const std::string ACTION_PUSHPARTIALENVIRONMENT = "PushPartialEnvironment";
  81. static const std::string KEY_ASSETID = "asset_id";
  82. static const std::string KEY_TRANSITIONTIME = "transition_time";
  83. static const std::string KEY_ACTION = "action";
  84. static const std::string KEY_ACTIONDATA = "action_data";
  85. static const std::string KEY_EXPERIENCEID = "public_id";
  86. // Some of these do not conform to the '_' format but changing them would also
  87. // alter the Experience Log requirements.
  88. static const std::string KEY_OBJECTNAME = "ObjectName";
  89. static const std::string KEY_PARCELNAME = "ParcelName";
  90. static const std::string ENV_KEY_COUNT = "Count";
  91. static const std::string LISTENER_NAME = "LLEnvironmentSingleton";
  92. constexpr F64 DEFAULT_UPDATE_THRESHOLD = 10.0;
  93. constexpr F64 MINIMUM_SPANLENGTH = 0.01f;
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // Helper functions
  96. ///////////////////////////////////////////////////////////////////////////////
  97. LL_INLINE F32 get_wrapping_distance(F32 begin, F32 end)
  98. {
  99. if (begin < end)
  100. {
  101. return end - begin;
  102. }
  103. if (begin > end)
  104. {
  105. return 1.f - begin + end;
  106. }
  107. return 1.f;
  108. }
  109. LLSettingsDay::cycle_track_it_t
  110. get_wrapping_atafter(LLSettingsDay::cycle_track_t& collection, F32 key)
  111. {
  112. if (collection.empty())
  113. {
  114. return collection.end();
  115. }
  116. LLSettingsDay::cycle_track_it_t it = collection.upper_bound(key);
  117. if (it == collection.end())
  118. {
  119. // Wrap around
  120. it = collection.begin();
  121. }
  122. return it;
  123. }
  124. LLSettingsDay::cycle_track_it_t
  125. get_wrapping_atbefore(LLSettingsDay::cycle_track_t& collection, F32 key)
  126. {
  127. if (collection.empty())
  128. {
  129. return collection.end();
  130. }
  131. LLSettingsDay::cycle_track_it_t it = collection.lower_bound(key);
  132. if (it == collection.end())
  133. {
  134. // All keyframes are lower, take the last one.
  135. --it; // We know the range is not empty
  136. }
  137. else if (it->first > key)
  138. {
  139. // The keyframe we are interested in is smaller than the found.
  140. if (it == collection.begin())
  141. {
  142. it = collection.end();
  143. }
  144. --it;
  145. }
  146. return it;
  147. }
  148. LL_INLINE LLSettingsDay::track_bound_t
  149. get_bounding_entries(LLSettingsDay::cycle_track_t& track, F32 keyframe)
  150. {
  151. return LLSettingsDay::track_bound_t(get_wrapping_atbefore(track, keyframe),
  152. get_wrapping_atafter(track, keyframe));
  153. }
  154. // Find normalized track position of given time along full length of cycle
  155. LL_INLINE F32 convert_time_to_position(F64 time, F64 len)
  156. {
  157. return time > 0.0 && len > 0.0 ? F32(fmod(time, len) / len)
  158. : (time <= 0.0 ? 0.f : 1.f);
  159. }
  160. LL_INLINE F64 convert_time_to_blend_factor(F64 time, F64 len,
  161. LLSettingsDay::cycle_track_t& track)
  162. {
  163. F32 position = convert_time_to_position(time, len);
  164. LLSettingsDay::track_bound_t bounds = get_bounding_entries(track,
  165. position);
  166. F32 first_pos = (bounds.first)->first;
  167. F32 spanlength = get_wrapping_distance(first_pos, (bounds.second)->first);
  168. if (position < first_pos)
  169. {
  170. position += 1.f;
  171. }
  172. F32 start = position - first_pos;
  173. return (F64)(start / spanlength);
  174. }
  175. ///////////////////////////////////////////////////////////////////////////////
  176. // Helper classes
  177. ///////////////////////////////////////////////////////////////////////////////
  178. // LLEnvironmentRequest (seen in llwlhanders.h/cpp in LL's viewer).
  179. class LLEnvironmentRequest
  180. {
  181. protected:
  182. LOG_CLASS(LLEnvironmentRequest);
  183. public:
  184. // Returns true if request was successfully sent
  185. static bool initiate(LLEnvironment::env_apply_fn cb);
  186. private:
  187. static void onRegionCapsReceived(const LLUUID& region_id,
  188. LLEnvironment::env_apply_fn cb);
  189. static bool doRequest(LLEnvironment::env_apply_fn cb);
  190. private:
  191. static void environmentRequestCoro(const std::string& url,
  192. LLEnvironment::env_apply_fn cb);
  193. private:
  194. static S32 sLastRequest;
  195. };
  196. S32 LLEnvironmentRequest::sLastRequest = 0;
  197. //static
  198. bool LLEnvironmentRequest::initiate(LLEnvironment::env_apply_fn cb)
  199. {
  200. LLViewerRegion* regionp = gAgent.getRegion();
  201. if (!regionp)
  202. {
  203. // No agent region is set before the STATE_WORLD_INIT step has been
  204. // completed, so only emit a warning when at a subsequent state.
  205. if (LLStartUp::getStartupState() > STATE_WORLD_INIT)
  206. {
  207. llwarns << "Agent region not set. Skipping environment settings request."
  208. << llendl;
  209. }
  210. return false;
  211. }
  212. if (!regionp->capabilitiesReceived())
  213. {
  214. llinfos << "Deferring environment settings request until capabilities are received."
  215. << llendl;
  216. regionp->setCapsReceivedCB(boost::bind(&LLEnvironmentRequest::onRegionCapsReceived,
  217. _1, cb));
  218. return false;
  219. }
  220. return doRequest(cb);
  221. }
  222. //static
  223. void LLEnvironmentRequest::onRegionCapsReceived(const LLUUID& region_id,
  224. LLEnvironment::env_apply_fn cb)
  225. {
  226. LLViewerRegion* regionp = gAgent.getRegion();
  227. if (!regionp || region_id != regionp->getRegionID())
  228. {
  229. llinfos << "Capabilities received for non-agent region. Ignored."
  230. << llendl;
  231. return;
  232. }
  233. doRequest(cb);
  234. }
  235. //static
  236. bool LLEnvironmentRequest::doRequest(LLEnvironment::env_apply_fn cb)
  237. {
  238. const std::string& url = gAgent.getRegionCapability("EnvironmentSettings");
  239. if (url.empty())
  240. {
  241. gEnvironment.setEnvironment(LLEnvironment::ENV_REGION,
  242. LLSettingsDay::getDefaultAssetId());
  243. return false;
  244. }
  245. gCoros.launch("LLEnvironmentRequest::environmentRequestCoro",
  246. boost::bind(&LLEnvironmentRequest::environmentRequestCoro,
  247. url, cb));
  248. return true;
  249. }
  250. //static
  251. void LLEnvironmentRequest::environmentRequestCoro(const std::string& url,
  252. LLEnvironment::env_apply_fn cb)
  253. {
  254. S32 request_id = ++sLastRequest;
  255. LLCoreHttpUtil::HttpCoroutineAdapter adapter("EnvironmentRequest");
  256. LLSD result = adapter.getAndSuspend(url);
  257. llinfos << "Using legacy Windlight capability. Request Id = " << request_id
  258. << llendl;
  259. if (request_id != sLastRequest)
  260. {
  261. llwarns << "Got superseded by another responder; discarding this reply."
  262. << llendl;
  263. return;
  264. }
  265. LLCore::HttpStatus status =
  266. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  267. if (!status)
  268. {
  269. llwarns << "Got an error, not using region windlight... " << llendl;
  270. gEnvironment.setEnvironment(LLEnvironment::ENV_REGION,
  271. LLSettingsDay::getDefaultAssetId());
  272. return;
  273. }
  274. llinfos << "Received region legacy windlight settings." << llendl;
  275. result = result["content"];
  276. LLUUID region_id;
  277. LLViewerRegion* regionp = gAgent.getRegion();
  278. if (regionp)
  279. {
  280. region_id = regionp->getRegionID();
  281. }
  282. if (region_id.notNull() && result[0]["regionID"].asUUID() != region_id)
  283. {
  284. llwarns << "Not in the region from where this data was received (wanting "
  285. << region_id << " but got " << result[0]["regionID"].asUUID()
  286. << "): ignoring." << llendl;
  287. return;
  288. }
  289. if (cb)
  290. {
  291. LLEnvironment::envinfo_ptr_t pinfo =
  292. LLEnvironment::EnvironmentInfo::extractLegacy(result);
  293. cb(INVALID_PARCEL_ID, pinfo);
  294. }
  295. }
  296. // LLTrackBlenderLoopingTime
  297. class LLTrackBlenderLoopingTime final : public LLSettingsBlenderTimeDelta
  298. {
  299. public:
  300. LLTrackBlenderLoopingTime(const LLSettingsBase::ptr_t& target,
  301. const LLSettingsDay::ptr_t& day, S32 trackno,
  302. F64 cyclelength, F64 cycleoffset,
  303. F64 update_threshold)
  304. : LLSettingsBlenderTimeDelta(target, LLSettingsBase::ptr_t(),
  305. LLSettingsBase::ptr_t(), 1.0),
  306. mDay(day),
  307. mCycleLength(cyclelength),
  308. mCycleOffset(cycleoffset)
  309. {
  310. // Must happen prior to getBoundingEntries call...
  311. mTrackNo = selectTrackNumber(trackno);
  312. F64 now = getAdjustedNow();
  313. LLSettingsDay::track_bound_t initial = getBoundingEntries(now);
  314. mInitial = (initial.first)->second;
  315. mFinal = (initial.second)->second;
  316. mBlendSpan = getSpanTime(initial);
  317. initializeTarget(now);
  318. setOnFinished(boost::bind(&LLTrackBlenderLoopingTime::onFinishedSpan,
  319. this));
  320. }
  321. void switchTrack(S32 trackno, F32)
  322. {
  323. S32 use_trackno = selectTrackNumber(trackno);
  324. if (use_trackno == mTrackNo)
  325. {
  326. // This results in no change
  327. return;
  328. }
  329. LLSettingsBase::ptr_t startsetting = mTarget->buildDerivedClone();
  330. mTrackNo = use_trackno;
  331. F64 now = getAdjustedNow() + LLEnvironment::TRANSITION_ALTITUDE;
  332. LLSettingsDay::track_bound_t bounds = getBoundingEntries(now);
  333. F32 pos = (bounds.first)->first;
  334. LLSettingsBase::ptr_t endsetting = (bounds.first)->second->buildDerivedClone();
  335. F32 targetpos = convert_time_to_position(now, mCycleLength) - pos;
  336. F32 targetspan = get_wrapping_distance(pos, (bounds.second)->first);
  337. F64 blendf = calculateBlend(targetpos, targetspan);
  338. endsetting->blend((bounds.second)->second, blendf);
  339. reset(startsetting, endsetting, LLEnvironment::TRANSITION_ALTITUDE);
  340. }
  341. protected:
  342. S32 selectTrackNumber(S32 trackno)
  343. {
  344. if (trackno == 0)
  345. {
  346. // We are dealing with the water track: there is only ever one.
  347. return trackno;
  348. }
  349. for (S32 test = trackno; test != 0; --test)
  350. {
  351. // Find the track below the requested one with data.
  352. LLSettingsDay::cycle_track_t& track = mDay->getCycleTrack(test);
  353. if (!track.empty())
  354. {
  355. return test;
  356. }
  357. }
  358. return 1;
  359. }
  360. LLSettingsDay::track_bound_t getBoundingEntries(F64 time)
  361. {
  362. LLSettingsDay::cycle_track_t& wtrack = mDay->getCycleTrack(mTrackNo);
  363. F32 position = convert_time_to_position(time, mCycleLength);
  364. return get_bounding_entries(wtrack, position);
  365. }
  366. void initializeTarget(F64 time)
  367. {
  368. F64 blendf =
  369. convert_time_to_blend_factor(time, mCycleLength,
  370. mDay->getCycleTrack(mTrackNo));
  371. blendf = llclamp(blendf, 0.0, 0.999);
  372. setTimeSpent(blendf * (F64)mBlendSpan);
  373. setBlendFactor(blendf);
  374. }
  375. LL_INLINE F64 getAdjustedNow() const
  376. {
  377. return LLTimer::getEpochSeconds() + mCycleOffset;
  378. }
  379. F64 getSpanTime(const LLSettingsDay::track_bound_t &bounds) const
  380. {
  381. F64 span = mCycleLength *
  382. get_wrapping_distance((bounds.first)->first,
  383. (bounds.second)->first);
  384. if (span < MINIMUM_SPANLENGTH)
  385. {
  386. // For very short spans set a minimum length.
  387. span = MINIMUM_SPANLENGTH;
  388. }
  389. return span;
  390. }
  391. private:
  392. void onFinishedSpan()
  393. {
  394. F64 adjusted_now = getAdjustedNow();
  395. LLSettingsDay::track_bound_t next = getBoundingEntries(adjusted_now);
  396. F64 nextspan = getSpanTime(next);
  397. reset((next.first)->second, (next.second)->second, nextspan);
  398. // Recalculate (reinitialize) position. Because:
  399. // - 'delta' from applyTimeDelta accumulates errors (probably should be
  400. // fixed/changed to absolute time).
  401. // - Freezes and lag can result in reset being called too late, so we
  402. // need to add missed time.
  403. // - Occasional time corrections can happen.
  404. // - Some transition switches can happen outside applyTimeDelta thus
  405. // causing 'desync' from 'delta' (can be fixed by getting rid of
  406. // delta).
  407. initializeTarget(adjusted_now);
  408. }
  409. private:
  410. LLSettingsDay::ptr_t mDay;
  411. S32 mTrackNo;
  412. F64 mCycleLength;
  413. F64 mCycleOffset;
  414. };
  415. // LLEnvPushDispatchHandler
  416. class LLEnvPushDispatchHandler final : public LLDispatchHandler
  417. {
  418. protected:
  419. LOG_CLASS(LLEnvPushDispatchHandler);
  420. public:
  421. bool operator()(const LLDispatcher*, const std::string& key,
  422. const LLUUID& invoice, const sparam_t& strings) override
  423. {
  424. LLSD message;
  425. sparam_t::const_iterator it = strings.begin();
  426. if (it != strings.end())
  427. {
  428. const std::string& llsd_raw = *it++;
  429. std::istringstream llsdData(llsd_raw);
  430. if (!LLSDSerialize::deserialize(message, llsdData,
  431. llsd_raw.length()))
  432. {
  433. llwarns << "Attempted to read parameter data into LLSD but failed: "
  434. << llsd_raw << llendl;
  435. }
  436. }
  437. message[KEY_EXPERIENCEID] = invoice;
  438. // Object name
  439. if (it != strings.end())
  440. {
  441. message[KEY_OBJECTNAME] = *it++;
  442. }
  443. // Parcel name
  444. if (it != strings.end())
  445. {
  446. message[KEY_PARCELNAME] = *it++;
  447. }
  448. message[ENV_KEY_COUNT] = 1;
  449. gEnvironment.handleEnvironmentPush(message);
  450. return true;
  451. }
  452. };
  453. static LLEnvPushDispatchHandler sEnvPushDispatcher;
  454. // LLSettingsInjected
  455. template<class SETTINGT>
  456. class LLSettingsInjected : public SETTINGT
  457. {
  458. public:
  459. typedef std::shared_ptr<LLSettingsInjected<SETTINGT> > ptr_t;
  460. LLSettingsInjected(typename SETTINGT::ptr_t source)
  461. : SETTINGT(),
  462. mSource(source),
  463. mLastSourceHash(0),
  464. mLastHash(0)
  465. {
  466. }
  467. ~LLSettingsInjected() override {}
  468. LL_INLINE typename SETTINGT::ptr_t getSource() const
  469. {
  470. return mSource;
  471. }
  472. void setSource(const typename SETTINGT::ptr_t& source)
  473. {
  474. if (source.get() == this)
  475. {
  476. // Do not set a source to itself.
  477. return;
  478. }
  479. mSource = source;
  480. this->setDirtyFlag(true);
  481. mLastSourceHash = 0;
  482. }
  483. LL_INLINE bool isDirty() const override
  484. {
  485. return SETTINGT::isDirty() || mSource->isDirty();
  486. }
  487. LL_INLINE bool isVeryDirty() const override
  488. {
  489. return SETTINGT::isVeryDirty() || mSource->isVeryDirty();
  490. }
  491. void injectSetting(const std::string& keyname, const LLSD& value,
  492. const LLUUID& experience_id, F32 transition)
  493. {
  494. if (transition > 0.1f)
  495. {
  496. typename Injection::ptr_t injection =
  497. std::make_shared<Injection>(transition, keyname, value, true,
  498. experience_id);
  499. mInjections.push_back(injection);
  500. std::stable_sort(mInjections.begin(), mInjections.end(),
  501. compare_remaining);
  502. }
  503. else
  504. {
  505. mOverrideValues[keyname] = value;
  506. mOverrideExps[keyname] = experience_id;
  507. this->setDirtyFlag(true);
  508. }
  509. }
  510. void removeInjection(const std::string& keyname, const LLUUID& experience,
  511. F64 transition)
  512. {
  513. injections_t injections_buf;
  514. for (auto it = mInjections.begin(), end = mInjections.end(); it != end;
  515. ++it)
  516. {
  517. if ((keyname.empty() || (*it)->mKeyName == keyname) &&
  518. (experience.isNull() || experience == (*it)->mExperience))
  519. {
  520. if (transition != LLEnvironment::TRANSITION_INSTANT)
  521. {
  522. typename Injection::ptr_t injection =
  523. std::make_shared<Injection>(transition, keyname,
  524. (*it)->mLastValue, false,
  525. LLUUID::null);
  526. injections_buf.push_front(injection);
  527. }
  528. }
  529. else
  530. {
  531. injections_buf.push_front(*it);
  532. }
  533. }
  534. mInjections.clear();
  535. mInjections = injections_buf;
  536. for (key_to_expid_t::iterator it = mOverrideExps.begin();
  537. it != mOverrideExps.end(); )
  538. {
  539. if (experience.isNull() || it->second == experience)
  540. {
  541. if (transition != LLEnvironment::TRANSITION_INSTANT)
  542. {
  543. typename Injection::ptr_t injection =
  544. std::make_shared<Injection>(transition, it->first,
  545. mOverrideValues[it->first],
  546. false, LLUUID::null);
  547. mInjections.push_front(injection);
  548. }
  549. mOverrideValues.erase(it->first);
  550. mOverrideExps.erase(it++);
  551. }
  552. else
  553. {
  554. ++it;
  555. }
  556. }
  557. std::stable_sort(mInjections.begin(), mInjections.end(),
  558. compare_remaining);
  559. }
  560. void removeInjections(const LLUUID& experience_id, F64 transition)
  561. {
  562. removeInjection(LLStringUtil::null, experience_id, transition);
  563. }
  564. void injectExperienceValues(const LLSD& values,
  565. const LLUUID& experience_id, F64 transition)
  566. {
  567. for (LLSD::map_const_iterator it = values.beginMap(),
  568. end = values.endMap();
  569. it != end; ++it)
  570. {
  571. injectSetting(it->first, it->second, experience_id, transition);
  572. }
  573. this->setDirtyFlag(true);
  574. }
  575. void applyInjections(F64 delta)
  576. {
  577. this->mSettings = this->mSource->getSettings();
  578. for (LLSD::map_iterator it = mOverrideValues.beginMap(),
  579. end = mOverrideValues.endMap();
  580. it != end; ++it)
  581. {
  582. this->mSettings[it->first] = it->second;
  583. }
  584. const LLSettingsBase::stringset_t& slerps = this->getSlerpKeys();
  585. const LLSettingsBase::stringset_t& skips =
  586. this->getSkipInterpolateKeys();
  587. const LLSettingsBase::stringset_t& specials = this->getSpecialKeys();
  588. typename injections_t::iterator it;
  589. for (it = mInjections.begin(); it != mInjections.end(); ++it)
  590. {
  591. std::string key_name = (*it)->mKeyName;
  592. LLSD value = this->mSettings[key_name];
  593. LLSD target = (*it)->mValue;
  594. if ((*it)->mFirstTime)
  595. {
  596. (*it)->mFirstTime = false;
  597. }
  598. else
  599. {
  600. (*it)->mTimeRemaining -= delta;
  601. }
  602. F64 mix = 1.0 - (*it)->mTimeRemaining / (*it)->mTransition;
  603. if (mix >= 1.0)
  604. {
  605. if ((*it)->mBlendIn)
  606. {
  607. mOverrideValues[key_name] = target;
  608. mOverrideExps[key_name] = (*it)->mExperience;
  609. this->mSettings[key_name] = target;
  610. }
  611. else
  612. {
  613. this->mSettings.erase(key_name);
  614. }
  615. }
  616. else if (specials.find(key_name) != specials.end())
  617. {
  618. updateSpecial(*it, mix);
  619. }
  620. else if (skips.find(key_name) == skips.end())
  621. {
  622. if (!(*it)->mBlendIn)
  623. {
  624. mix = 1.0 - mix;
  625. }
  626. (*it)->mLastValue =
  627. this->interpolateSDValue(key_name, value, target,
  628. this->getParameterMap(), mix,
  629. slerps);
  630. this->mSettings[key_name] = (*it)->mLastValue;
  631. }
  632. }
  633. size_t hash = this->getHash();
  634. if (hash != mLastHash)
  635. {
  636. this->setDirtyFlag(true);
  637. mLastHash = hash;
  638. }
  639. it = mInjections.begin();
  640. it = std::find_if(mInjections.begin(), mInjections.end(),
  641. has_remaining);
  642. if (it != mInjections.begin())
  643. {
  644. mInjections.erase(mInjections.begin(), mInjections.end());
  645. }
  646. }
  647. LL_INLINE bool hasInjections() const
  648. {
  649. return !mInjections.empty() || mOverrideValues.size() > 0;
  650. }
  651. protected:
  652. struct Injection
  653. {
  654. typedef std::shared_ptr<Injection> ptr_t;
  655. Injection(F64 transition, const std::string& keyname,
  656. const LLSD& value, bool blendin, const LLUUID& experience,
  657. S32 index = -1)
  658. : mTransition(transition),
  659. mTimeRemaining(transition),
  660. mKeyName(keyname),
  661. mValue(value),
  662. mExperience(experience),
  663. mIndex(index),
  664. mBlendIn(blendin),
  665. mFirstTime(true)
  666. {
  667. }
  668. F64 mTransition;
  669. F64 mTimeRemaining;
  670. std::string mKeyName;
  671. LLSD mValue;
  672. LLSD mLastValue;
  673. LLUUID mExperience;
  674. S32 mIndex;
  675. bool mBlendIn;
  676. bool mFirstTime;
  677. };
  678. void updateSettings() override
  679. {
  680. if (!mSource)
  681. {
  682. return;
  683. }
  684. // Clears the dirty flag on this object. Need to prevent triggering
  685. // more calls into this updateSettings.
  686. LLSettingsBase::updateSettings();
  687. resetSpecial();
  688. if (mSource->isDirty())
  689. {
  690. mSource->updateSettings();
  691. }
  692. SETTINGT::updateSettings();
  693. if (!mInjections.empty())
  694. {
  695. this->setDirtyFlag(true);
  696. }
  697. }
  698. const LLSettingsBase::stringset_t& getSpecialKeys() const;
  699. void resetSpecial();
  700. void updateSpecial(const typename Injection::ptr_t& injection, F64 mix);
  701. private:
  702. LL_INLINE static bool compare_remaining(const typename Injection::ptr_t& a,
  703. const typename Injection::ptr_t& b)
  704. {
  705. return a->mTimeRemaining < b->mTimeRemaining;
  706. }
  707. LL_INLINE static bool has_remaining(const typename Injection::ptr_t& a)
  708. {
  709. return a->mTimeRemaining > 0.0;
  710. }
  711. private:
  712. size_t mLastSourceHash;
  713. size_t mLastHash;
  714. typename SETTINGT::ptr_t mSource;
  715. LLSD mOverrideValues;
  716. typedef std::deque<typename Injection::ptr_t> injections_t;
  717. injections_t mInjections;
  718. typedef std::map<std::string, LLUUID> key_to_expid_t;
  719. key_to_expid_t mOverrideExps;
  720. };
  721. template<>
  722. const LLSettingsBase::stringset_t& LLSettingsInjected<LLEnvSettingsSky>::getSpecialKeys() const
  723. {
  724. static LLSettingsBase::stringset_t special_set;
  725. if (special_set.empty())
  726. {
  727. special_set.emplace(SETTING_BLOOM_TEXTUREID);
  728. special_set.emplace(SETTING_RAINBOW_TEXTUREID);
  729. special_set.emplace(SETTING_HALO_TEXTUREID);
  730. special_set.emplace(SETTING_CLOUD_TEXTUREID);
  731. special_set.emplace(SETTING_MOON_TEXTUREID);
  732. special_set.emplace(SETTING_SUN_TEXTUREID);
  733. // Due to being part of skips
  734. special_set.emplace(SETTING_CLOUD_SHADOW);
  735. }
  736. return special_set;
  737. }
  738. template<>
  739. const LLSettingsBase::stringset_t& LLSettingsInjected<LLEnvSettingsWater>::getSpecialKeys() const
  740. {
  741. static stringset_t special_set;
  742. if (special_set.empty())
  743. {
  744. special_set.emplace(SETTING_TRANSPARENT_TEXTURE);
  745. special_set.emplace(SETTING_NORMAL_MAP);
  746. }
  747. return special_set;
  748. }
  749. template<>
  750. void LLSettingsInjected<LLEnvSettingsSky>::resetSpecial()
  751. {
  752. mNextSunTextureId.setNull();
  753. mNextMoonTextureId.setNull();
  754. mNextCloudTextureId.setNull();
  755. mNextBloomTextureId.setNull();
  756. mNextRainbowTextureId.setNull();
  757. mNextHaloTextureId.setNull();
  758. setBlendFactor(0.f);
  759. }
  760. template<>
  761. void LLSettingsInjected<LLEnvSettingsWater>::resetSpecial()
  762. {
  763. mNextNormalMapID.setNull();
  764. mNextTransparentTextureID.setNull();
  765. setBlendFactor(0.f);
  766. }
  767. template<>
  768. void LLSettingsInjected<LLEnvSettingsSky>::updateSpecial(const typename LLSettingsInjected<LLEnvSettingsSky>::Injection::ptr_t& injection,
  769. F64 mix)
  770. {
  771. if (injection->mKeyName == SETTING_SUN_TEXTUREID)
  772. {
  773. mNextSunTextureId = injection->mValue.asUUID();
  774. }
  775. else if (injection->mKeyName == SETTING_MOON_TEXTUREID)
  776. {
  777. mNextMoonTextureId = injection->mValue.asUUID();
  778. }
  779. else if (injection->mKeyName == SETTING_CLOUD_TEXTUREID)
  780. {
  781. mNextCloudTextureId = injection->mValue.asUUID();
  782. }
  783. else if (injection->mKeyName == SETTING_BLOOM_TEXTUREID)
  784. {
  785. mNextBloomTextureId = injection->mValue.asUUID();
  786. }
  787. else if (injection->mKeyName == SETTING_RAINBOW_TEXTUREID)
  788. {
  789. mNextRainbowTextureId = injection->mValue.asUUID();
  790. }
  791. else if (injection->mKeyName == SETTING_HALO_TEXTUREID)
  792. {
  793. mNextHaloTextureId = injection->mValue.asUUID();
  794. }
  795. else if (injection->mKeyName == SETTING_CLOUD_SHADOW)
  796. {
  797. // Special case due to being texture dependent and part of skips
  798. if (!injection->mBlendIn)
  799. {
  800. mix = 1.0 - mix;
  801. }
  802. stringset_t dummy;
  803. LLUUID cloud_noise_id = getCloudNoiseTextureId();
  804. F64 value = mSettings[injection->mKeyName].asReal();
  805. if (cloud_noise_id.isNull())
  806. {
  807. value = 0.0; // There was no texture so start from zero coverage
  808. }
  809. // Ideally we need to check for texture in injection, but in this case
  810. // user is setting value explicitly, potentially with different
  811. // transitions, do not ignore it.
  812. F64 result = lerp(value, injection->mValue.asReal(), mix);
  813. injection->mLastValue = LLSD::Real(result);
  814. mSettings[injection->mKeyName] = injection->mLastValue;
  815. return; // Never set blend factor
  816. }
  817. // Unfortunately I do not have a per-texture blend factor. We will just
  818. // pick the one that is furthest along.
  819. if (getBlendFactor() < mix)
  820. {
  821. setBlendFactor(mix);
  822. }
  823. }
  824. template<>
  825. void LLSettingsInjected<LLEnvSettingsWater>::updateSpecial(const typename LLSettingsInjected<LLEnvSettingsWater>::Injection::ptr_t& injection,
  826. F64 mix)
  827. {
  828. if (injection->mKeyName == SETTING_NORMAL_MAP)
  829. {
  830. mNextNormalMapID = injection->mValue.asUUID();
  831. }
  832. else if (injection->mKeyName == SETTING_TRANSPARENT_TEXTURE)
  833. {
  834. mNextTransparentTextureID = injection->mValue.asUUID();
  835. }
  836. // Unfortunately I do not have a per-texture blend factor. We will just
  837. // pick the one that is furthest along.
  838. if (getBlendFactor() < mix)
  839. {
  840. setBlendFactor(mix);
  841. }
  842. }
  843. typedef LLSettingsInjected<LLEnvSettingsSky> LLSettingsInjectedSky;
  844. typedef LLSettingsInjected<LLEnvSettingsWater> LLSettingsInjectedWater;
  845. // LLDayInjection
  846. class LLDayInjection : public LLEnvironment::DayInstance
  847. {
  848. friend class InjectedTransition;
  849. protected:
  850. LOG_CLASS(LLDayInjection);
  851. public:
  852. typedef std::shared_ptr<LLDayInjection> ptr_t;
  853. typedef std::weak_ptr<LLDayInjection> wptr_t;
  854. LLDayInjection(LLEnvironment::EEnvSelection env);
  855. ~LLDayInjection() override;
  856. bool applyTimeDelta(F64 delta) override;
  857. void setInjectedDay(const LLSettingsDay::ptr_t& dayp,
  858. const LLUUID& experience_id, F64 transition);
  859. void setInjectedSky(const LLSettingsSky::ptr_t& skyp,
  860. const LLUUID& experience_id, F64 transition);
  861. void setInjectedWater(const LLSettingsWater::ptr_t& waterp,
  862. const LLUUID& experience_id, F64 transition);
  863. void injectSkySettings(const LLSD& settings, const LLUUID& experience_id,
  864. F64 transition);
  865. void injectWaterSettings(const LLSD& settings, const LLUUID& experience_id,
  866. F64 transition);
  867. void clearInjections(const LLUUID& experience_id, F64 transition_time);
  868. LL_INLINE void animate() override {}
  869. LL_INLINE LLEnvironment::DayInstance::ptr_t getBaseDayInstance() const
  870. {
  871. return mBaseDayInstance;
  872. }
  873. void setBaseDayInstance(const LLEnvironment::DayInstance::ptr_t& baseday);
  874. LL_INLINE S32 countExperiencesActive() const { return mActiveExperiences.size(); }
  875. LL_INLINE bool isOverriddenSky() const { return mSkyExperience.notNull(); }
  876. LL_INLINE bool isOverriddenWater() const { return mWaterExperience.notNull(); }
  877. LL_INLINE bool hasInjections() const
  878. {
  879. return mSkyExperience.notNull() || mWaterExperience.notNull() ||
  880. mDayExperience.notNull() || mBlenderSky || mBlenderWater ||
  881. mInjectedSky->hasInjections() || mInjectedWater->hasInjections();
  882. }
  883. void testExperiencesOnParcel(S32 parcel_id);
  884. private:
  885. void animateSkyChange(LLSettingsSky::ptr_t skyp, F64 transition);
  886. void animateWaterChange(LLSettingsWater::ptr_t waterp, F64 transition);
  887. void onEnvironmentChanged(LLEnvironment::EEnvSelection env);
  888. void onParcelChange();
  889. void checkExperience();
  890. static void testExperiencesOnParcelCoro(wptr_t that, S32 parcel_id);
  891. class InjectedTransition : public LLEnvironment::DayTransition
  892. {
  893. public:
  894. InjectedTransition(const LLDayInjection::ptr_t& injection,
  895. const LLSettingsSky::ptr_t& skystart,
  896. const LLSettingsWater::ptr_t& waterstart,
  897. DayInstance::ptr_t& end, S32 time)
  898. : LLEnvironment::DayTransition(skystart, waterstart, end, time),
  899. mInjection(injection)
  900. {
  901. }
  902. ~InjectedTransition() override {}
  903. void animate() override;
  904. protected:
  905. LLDayInjection::ptr_t mInjection;
  906. };
  907. private:
  908. LLEnvironment::DayInstance::ptr_t mBaseDayInstance;
  909. LLSettingsInjectedSky::ptr_t mInjectedSky;
  910. LLSettingsInjectedWater::ptr_t mInjectedWater;
  911. uuid_list_t mActiveExperiences;
  912. LLUUID mDayExperience;
  913. LLUUID mSkyExperience;
  914. LLUUID mWaterExperience;
  915. LLEnvironment::connection_t mEnvChangeConnection;
  916. boost::signals2::connection mParcelChangeConnection;
  917. };
  918. LLDayInjection::LLDayInjection(LLEnvironment::EEnvSelection env)
  919. : LLEnvironment::DayInstance(env)
  920. {
  921. mSky = mInjectedSky =
  922. std::make_shared<LLSettingsInjectedSky>(gEnvironment.getCurrentSky());
  923. mWater = mInjectedWater =
  924. std::make_shared<LLSettingsInjectedWater>(gEnvironment.getCurrentWater());
  925. mBaseDayInstance = gEnvironment.getSharedEnvironmentInstance();
  926. mEnvChangeConnection =
  927. gEnvironment.setEnvironmentChanged(boost::bind(&LLDayInjection::onEnvironmentChanged,
  928. this, _1));
  929. mParcelChangeConnection =
  930. gViewerParcelMgr.addAgentParcelChangedCB(boost::bind(&LLDayInjection::onParcelChange,
  931. this));
  932. }
  933. LLDayInjection::~LLDayInjection()
  934. {
  935. if (mEnvChangeConnection.connected())
  936. {
  937. mEnvChangeConnection.disconnect();
  938. }
  939. if (mParcelChangeConnection.connected())
  940. {
  941. mParcelChangeConnection.disconnect();
  942. }
  943. }
  944. bool LLDayInjection::applyTimeDelta(F64 delta)
  945. {
  946. bool changed = false;
  947. if (mBaseDayInstance)
  948. {
  949. changed = mBaseDayInstance->applyTimeDelta(delta);
  950. }
  951. mInjectedSky->applyInjections(delta);
  952. mInjectedWater->applyInjections(delta);
  953. changed |= LLEnvironment::DayInstance::applyTimeDelta(delta);
  954. if (changed)
  955. {
  956. mInjectedSky->setDirtyFlag(true);
  957. mInjectedWater->setDirtyFlag(true);
  958. }
  959. mInjectedSky->update();
  960. mInjectedWater->update();
  961. if (!hasInjections())
  962. {
  963. // There is no injection being managed. This should really go away.
  964. gEnvironment.clearEnvironment(LLEnvironment::ENV_PUSH);
  965. gEnvironment.updateEnvironment(LLEnvironment::TRANSITION_INSTANT);
  966. }
  967. return changed;
  968. }
  969. void LLDayInjection::setBaseDayInstance(const LLEnvironment::DayInstance::ptr_t& baseday)
  970. {
  971. mBaseDayInstance = baseday;
  972. if (mSkyExperience.isNull())
  973. {
  974. mInjectedSky->setSource(mBaseDayInstance->getSky());
  975. }
  976. if (mWaterExperience.isNull())
  977. {
  978. mInjectedWater->setSource(mBaseDayInstance->getWater());
  979. }
  980. }
  981. void LLDayInjection::testExperiencesOnParcel(S32 parcel_id)
  982. {
  983. gCoros.launch("LLDayInjection::testExperiencesOnParcel",
  984. [this, parcel_id]()
  985. {
  986. testExperiencesOnParcelCoro(std::static_pointer_cast<LLDayInjection>(this->shared_from_this()),
  987. parcel_id);
  988. });
  989. }
  990. void LLDayInjection::setInjectedDay(const LLSettingsDay::ptr_t& dayp,
  991. const LLUUID& experience_id, F64 transition)
  992. {
  993. mSkyExperience = experience_id;
  994. mWaterExperience = experience_id;
  995. mDayExperience = experience_id;
  996. mBaseDayInstance = mBaseDayInstance->clone();
  997. mBaseDayInstance->setEnvironmentSelection(LLEnvironment::ENV_NONE);
  998. mBaseDayInstance->setDay(dayp, mBaseDayInstance->getDayLength(),
  999. mBaseDayInstance->getDayOffset());
  1000. animateSkyChange(mBaseDayInstance->getSky(), transition);
  1001. animateWaterChange(mBaseDayInstance->getWater(), transition);
  1002. mActiveExperiences.emplace(experience_id);
  1003. }
  1004. void LLDayInjection::setInjectedSky(const LLSettingsSky::ptr_t& skyp,
  1005. const LLUUID& experience_id,
  1006. F64 transition)
  1007. {
  1008. mSkyExperience = experience_id;
  1009. mActiveExperiences.emplace(experience_id);
  1010. checkExperience();
  1011. animateSkyChange(skyp, transition);
  1012. }
  1013. void LLDayInjection::setInjectedWater(const LLSettingsWater::ptr_t& waterp,
  1014. const LLUUID& experience_id,
  1015. F64 transition)
  1016. {
  1017. mWaterExperience = experience_id;
  1018. mActiveExperiences.emplace(experience_id);
  1019. checkExperience();
  1020. animateWaterChange(waterp, transition);
  1021. }
  1022. void LLDayInjection::injectSkySettings(const LLSD& settings,
  1023. const LLUUID& experience_id,
  1024. F64 transition)
  1025. {
  1026. mInjectedSky->injectExperienceValues(settings, experience_id,
  1027. transition);
  1028. mActiveExperiences.emplace(experience_id);
  1029. }
  1030. void LLDayInjection::injectWaterSettings(const LLSD& settings,
  1031. const LLUUID& experience_id,
  1032. F64 transition)
  1033. {
  1034. mInjectedWater->injectExperienceValues(settings, experience_id,
  1035. transition);
  1036. mActiveExperiences.emplace(experience_id);
  1037. }
  1038. void LLDayInjection::clearInjections(const LLUUID& experience_id,
  1039. F64 transition_time)
  1040. {
  1041. if (experience_id == mDayExperience ||
  1042. (experience_id.isNull() && mDayExperience.notNull()))
  1043. {
  1044. mDayExperience.setNull();
  1045. if (mSkyExperience == experience_id)
  1046. {
  1047. mSkyExperience.setNull();
  1048. }
  1049. if (mWaterExperience == experience_id)
  1050. {
  1051. mWaterExperience.setNull();
  1052. }
  1053. mBaseDayInstance = gEnvironment.getSharedEnvironmentInstance();
  1054. if (mSkyExperience.isNull())
  1055. {
  1056. animateSkyChange(mBaseDayInstance->getSky(), transition_time);
  1057. }
  1058. if (mWaterExperience.isNull())
  1059. {
  1060. animateWaterChange(mBaseDayInstance->getWater(), transition_time);
  1061. }
  1062. }
  1063. if (experience_id == mSkyExperience ||
  1064. (experience_id.isNull() && mSkyExperience.notNull()))
  1065. {
  1066. mSkyExperience.setNull();
  1067. animateSkyChange(mBaseDayInstance->getSky(), transition_time);
  1068. }
  1069. if (experience_id == mWaterExperience ||
  1070. (experience_id.isNull() && mWaterExperience.notNull()))
  1071. {
  1072. mWaterExperience.setNull();
  1073. animateWaterChange(mBaseDayInstance->getWater(), transition_time);
  1074. }
  1075. mInjectedSky->removeInjections(experience_id, transition_time);
  1076. mInjectedWater->removeInjections(experience_id, transition_time);
  1077. if (experience_id.isNull())
  1078. {
  1079. mActiveExperiences.clear();
  1080. }
  1081. else
  1082. {
  1083. mActiveExperiences.erase(experience_id);
  1084. }
  1085. if (transition_time == LLEnvironment::TRANSITION_INSTANT &&
  1086. countExperiencesActive() == 0)
  1087. {
  1088. // Only do this if instant and there are no other experiences injecting
  1089. // values (it will otherwise be handled after transition)
  1090. gEnvironment.clearEnvironment(LLEnvironment::ENV_PUSH);
  1091. gEnvironment.updateEnvironment(LLEnvironment::TRANSITION_INSTANT);
  1092. }
  1093. }
  1094. void LLDayInjection::testExperiencesOnParcelCoro(wptr_t that, S32 parcel_id)
  1095. {
  1096. std::string url = gAgent.getRegionCapability("ExperienceQuery");
  1097. if (url.empty())
  1098. {
  1099. llwarns << "No experience query cap." << llendl;
  1100. return;
  1101. }
  1102. {
  1103. ptr_t thatlock(that);
  1104. if (!thatlock)
  1105. {
  1106. return;
  1107. }
  1108. url += llformat("?parcelid=%d", parcel_id);
  1109. bool first_param = true;
  1110. for (uuid_list_t::iterator it = thatlock->mActiveExperiences.begin(),
  1111. end = thatlock->mActiveExperiences.end();
  1112. it != end; ++it)
  1113. {
  1114. if (first_param)
  1115. {
  1116. url += "&experiences=";
  1117. first_param = false;
  1118. }
  1119. else
  1120. {
  1121. url += ',';
  1122. }
  1123. url += it->asString();
  1124. }
  1125. }
  1126. LLCoreHttpUtil::HttpCoroutineAdapter adapter("testExperiencesOnParcelCoro");
  1127. LLSD result = adapter.getAndSuspend(url);
  1128. LLCore::HttpStatus status =
  1129. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  1130. if (!status)
  1131. {
  1132. llwarns << "Unable to retrieve experience status for parcel Id = "
  1133. << parcel_id << llendl;
  1134. return;
  1135. }
  1136. LLParcel* parcel = gViewerParcelMgr.getAgentParcel();
  1137. if (!parcel || parcel_id != parcel->getLocalID())
  1138. {
  1139. // Agent no longer on queried parcel.
  1140. return;
  1141. }
  1142. ptr_t thatlock(that);
  1143. if (!thatlock)
  1144. {
  1145. return;
  1146. }
  1147. LLSD experiences = result["experiences"];
  1148. for (LLSD::map_iterator it = experiences.beginMap(),
  1149. end = experiences.endMap();
  1150. it != end; ++it)
  1151. {
  1152. if (!it->second.asBoolean())
  1153. {
  1154. thatlock->clearInjections(LLUUID(it->first),
  1155. LLEnvironment::TRANSITION_FAST);
  1156. }
  1157. }
  1158. }
  1159. void LLDayInjection::animateSkyChange(LLSettingsSky::ptr_t skyp,
  1160. F64 transition)
  1161. {
  1162. if (mInjectedSky.get() == skyp.get())
  1163. {
  1164. // An attempt to animate to itself... Do not do it.
  1165. return;
  1166. }
  1167. if (transition == LLEnvironment::TRANSITION_INSTANT)
  1168. {
  1169. mBlenderSky.reset();
  1170. mInjectedSky->setSource(skyp);
  1171. return;
  1172. }
  1173. // Clear reflection probes and pause updates during sky change
  1174. gPipeline.mReflectionMapManager.pause(transition);
  1175. gPipeline.mReflectionMapManager.reset();
  1176. LLSettingsSky::ptr_t start_sky = mInjectedSky->getSource()->buildClone();
  1177. LLSettingsSky::ptr_t target_sky = start_sky->buildClone();
  1178. mInjectedSky->setSource(target_sky);
  1179. mBlenderSky = std::make_shared<LLSettingsBlenderTimeDelta>(target_sky,
  1180. start_sky,
  1181. skyp,
  1182. transition);
  1183. mBlenderSky->setOnFinished([this, skyp](LLSettingsBlender::ptr_t blender)
  1184. {
  1185. mBlenderSky.reset();
  1186. mInjectedSky->setSource(skyp);
  1187. setSky(mInjectedSky);
  1188. if (!mBlenderWater &&
  1189. !countExperiencesActive())
  1190. {
  1191. gEnvironment.clearEnvironment(LLEnvironment::ENV_PUSH);
  1192. gEnvironment.updateEnvironment(LLEnvironment::TRANSITION_INSTANT);
  1193. }
  1194. });
  1195. }
  1196. void LLDayInjection::animateWaterChange(LLSettingsWater::ptr_t waterp,
  1197. F64 transition)
  1198. {
  1199. if (mInjectedWater.get() == waterp.get())
  1200. {
  1201. // An attempt to animate to itself. Bad idea.
  1202. return;
  1203. }
  1204. if (transition == LLEnvironment::TRANSITION_INSTANT)
  1205. {
  1206. mBlenderWater.reset();
  1207. mInjectedWater->setSource(waterp);
  1208. return;
  1209. }
  1210. LLSettingsWater::ptr_t start_water(mInjectedWater->getSource()->buildClone());
  1211. LLSettingsWater::ptr_t scratch_water(start_water->buildClone());
  1212. mInjectedWater->setSource(scratch_water);
  1213. mBlenderWater = std::make_shared<LLSettingsBlenderTimeDelta>(scratch_water,
  1214. start_water,
  1215. waterp,
  1216. transition);
  1217. mBlenderWater->setOnFinished([this, waterp](LLSettingsBlender::ptr_t blender)
  1218. {
  1219. mBlenderWater.reset();
  1220. mInjectedWater->setSource(waterp);
  1221. setWater(mInjectedWater);
  1222. if (!mBlenderSky &&
  1223. !countExperiencesActive())
  1224. {
  1225. gEnvironment.clearEnvironment(LLEnvironment::ENV_PUSH);
  1226. gEnvironment.updateEnvironment(LLEnvironment::TRANSITION_INSTANT);
  1227. }
  1228. });
  1229. }
  1230. void LLDayInjection::onEnvironmentChanged(LLEnvironment::EEnvSelection env)
  1231. {
  1232. if (env < LLEnvironment::ENV_PARCEL)
  1233. {
  1234. return;
  1235. }
  1236. LLEnvironment::EEnvSelection base_env =
  1237. mBaseDayInstance->getEnvironmentSelection();
  1238. LLEnvironment::DayInstance::ptr_t nextbase =
  1239. gEnvironment.getSharedEnvironmentInstance();
  1240. if (base_env == LLEnvironment::ENV_NONE || nextbase == mBaseDayInstance ||
  1241. (mSkyExperience.notNull() && mWaterExperience.notNull()))
  1242. {
  1243. // Base instance completely overridden, or not changed no transition
  1244. // will happen
  1245. return;
  1246. }
  1247. llwarns << "Underlying environment has changed (" << env
  1248. << "). Base env is type " << base_env << llendl;
  1249. LLEnvironment::DayInstance::ptr_t trans =
  1250. std::make_shared<InjectedTransition>(std::static_pointer_cast<LLDayInjection>(shared_from_this()),
  1251. mBaseDayInstance->getSky(),
  1252. mBaseDayInstance->getWater(),
  1253. nextbase,
  1254. LLEnvironment::TRANSITION_DEFAULT);
  1255. trans->animate();
  1256. setBaseDayInstance(trans);
  1257. }
  1258. void LLDayInjection::onParcelChange()
  1259. {
  1260. LLParcel* parcel = gViewerParcelMgr.getAgentParcel();
  1261. if (parcel)
  1262. {
  1263. testExperiencesOnParcel(parcel->getLocalID());
  1264. }
  1265. }
  1266. void LLDayInjection::checkExperience()
  1267. {
  1268. if (mDayExperience.notNull() && mSkyExperience != mDayExperience &&
  1269. mWaterExperience != mDayExperience)
  1270. {
  1271. // There was a day experience but we have replaced it with a water and
  1272. // a sky experience.
  1273. mDayExperience.setNull();
  1274. mBaseDayInstance = gEnvironment.getSharedEnvironmentInstance();
  1275. }
  1276. }
  1277. void LLDayInjection::InjectedTransition::animate()
  1278. {
  1279. if (!mInjection || !mNextInstance)
  1280. {
  1281. llwarns << "NULL pointer detected. Operation aborted" << llendl;
  1282. return;
  1283. }
  1284. mNextInstance->animate();
  1285. if (!mInjection->isOverriddenSky() && mStartSky)
  1286. {
  1287. mSky = mStartSky->buildClone();
  1288. mBlenderSky =
  1289. std::make_shared<LLSettingsBlenderTimeDelta>(mSky, mStartSky,
  1290. mNextInstance->getSky(),
  1291. mTransitionTime);
  1292. mBlenderSky->setOnFinished([this](LLSettingsBlender::ptr_t blender)
  1293. {
  1294. mBlenderSky.reset();
  1295. if (!mBlenderSky && !mBlenderSky)
  1296. {
  1297. mInjection->setBaseDayInstance(mNextInstance);
  1298. }
  1299. else
  1300. {
  1301. mInjection->mInjectedSky->setSource(mNextInstance->getSky());
  1302. }
  1303. });
  1304. }
  1305. else
  1306. {
  1307. mSky = mInjection->getSky();
  1308. mBlenderSky.reset();
  1309. }
  1310. if (mInjection->isOverriddenWater() || !mStartWater)
  1311. {
  1312. mWater = mInjection->getWater();
  1313. mBlenderWater.reset();
  1314. return;
  1315. }
  1316. mWater = mStartWater->buildClone();
  1317. mBlenderWater =
  1318. std::make_shared<LLSettingsBlenderTimeDelta>(mWater, mStartWater,
  1319. mNextInstance->getWater(),
  1320. mTransitionTime);
  1321. mBlenderWater->setOnFinished([this](LLSettingsBlender::ptr_t blender)
  1322. {
  1323. mBlenderWater.reset();
  1324. if (!mBlenderSky && !mBlenderWater)
  1325. {
  1326. mInjection->setBaseDayInstance(mNextInstance);
  1327. }
  1328. else
  1329. {
  1330. mInjection->mInjectedWater->setSource(mNextInstance->getWater());
  1331. }
  1332. });
  1333. }
  1334. ///////////////////////////////////////////////////////////////////////////////
  1335. // LLEnvironment class proper
  1336. ///////////////////////////////////////////////////////////////////////////////
  1337. #if LL_USE_FIXED_SKY_SETTINGS_FOR_DEFAULT_TIME
  1338. const LLUUID LLEnvironment::KNOWN_SKY_SUNRISE("01e41537-ff51-2f1f-8ef7-17e4df760bfb");
  1339. const LLUUID LLEnvironment::KNOWN_SKY_MIDDAY("6c83e853-e7f8-cad7-8ee6-5f31c453721c");
  1340. const LLUUID LLEnvironment::KNOWN_SKY_LEGACY_MIDDAY("cef49723-0292-af49-9b14-9598a616b8a3");
  1341. const LLUUID LLEnvironment::KNOWN_SKY_PBR_MIDDAY("c46226b4-0e43-5a56-9708-d27ca1df3292");
  1342. const LLUUID LLEnvironment::KNOWN_SKY_SUNSET("084e26cd-a900-28e8-08d0-64a9de5c15e2");
  1343. const LLUUID LLEnvironment::KNOWN_SKY_MIDNIGHT("8a01b97a-cb20-c1ea-ac63-f7ea84ad0090");
  1344. #endif
  1345. //static
  1346. S32 LLEnvironment::sBeaconsUsers = 0;
  1347. //static
  1348. void LLEnvironment::delBeaconsUser()
  1349. {
  1350. if (--sBeaconsUsers <= 0)
  1351. {
  1352. sBeaconsUsers = 0;
  1353. gSavedSettings.setBool("sunbeacon", false);
  1354. gSavedSettings.setBool("moonbeacon", false);
  1355. }
  1356. }
  1357. LLEnvironment::LLEnvironment()
  1358. : mInstanceValid(false),
  1359. mCloudScrollPaused(false),
  1360. mSelectedEnvironment(ENV_LOCAL),
  1361. mCurrentTrack(1),
  1362. mLastCamYaw(0.f)
  1363. {
  1364. }
  1365. void LLEnvironment::initClass()
  1366. {
  1367. llinfos << "Initializing." << llendl;
  1368. LLSettingsSky::ptr_t p_default_sky = LLEnvSettingsSky::buildDefaultSky();
  1369. LLSettingsWater::ptr_t p_default_water =
  1370. LLEnvSettingsWater::buildDefaultWater();
  1371. mCurrentEnvironment = std::make_shared<DayInstance>(ENV_DEFAULT);
  1372. mCurrentEnvironment->setSky(p_default_sky);
  1373. mCurrentEnvironment->setWater(p_default_water);
  1374. mEnvironments[ENV_DEFAULT] = mCurrentEnvironment;
  1375. requestParcel(INVALID_PARCEL_ID);
  1376. mRegionChangedConnection =
  1377. gAgent.addRegionChangedCB(boost::bind(&LLEnvironment::onRegionChange,
  1378. this));
  1379. mParcelChangedConnection =
  1380. gViewerParcelMgr.addAgentParcelChangedCB(boost::bind(&LLEnvironment::onParcelChange,
  1381. this));
  1382. mPositionChangedConnection =
  1383. gAgent.setPosChangeCallback(boost::bind(&LLEnvironment::onAgentPositionHasChanged,
  1384. this, _1));
  1385. // Note: a call to LLEnvironment:requestRegion() is also performed on
  1386. // region info updates, from LLViewerRegion::processRegionInfo(). This
  1387. // replaces the LLRegionInfoModel update callback used in LL's viewer.
  1388. if (!gGenericDispatcher.isHandlerPresent(MESSAGE_PUSHENVIRONMENT))
  1389. {
  1390. gGenericDispatcher.addHandler(MESSAGE_PUSHENVIRONMENT,
  1391. &sEnvPushDispatcher);
  1392. }
  1393. LLEventPump& pump = gEventPumps.obtain(LISTENER_NAME);
  1394. pump.listen(LISTENER_NAME,
  1395. boost::bind(&LLEnvironment::listenExperiencePump, this, _1));
  1396. mInstanceValid = true;
  1397. }
  1398. void LLEnvironment::cleanupClass()
  1399. {
  1400. llinfos << "Cleaning up." << llendl;
  1401. mInstanceValid = false;
  1402. LLEventPump& pump = gEventPumps.obtain(PUMP_EXPERIENCE);
  1403. pump.stopListening(LISTENER_NAME);
  1404. mRegionChangedConnection.disconnect();
  1405. mParcelChangedConnection.disconnect();
  1406. mPositionChangedConnection.disconnect();
  1407. }
  1408. const LLSettingsSky::ptr_t& LLEnvironment::getCurrentSky() const
  1409. {
  1410. const LLSettingsSky::ptr_t& skyp = mCurrentEnvironment->getSky();
  1411. if (!skyp && mCurrentEnvironment->getEnvironmentSelection() >= ENV_EDIT)
  1412. {
  1413. for (U32 idx = 0; idx < (U32)ENV_END; ++idx)
  1414. {
  1415. if (mEnvironments[idx]->getSky())
  1416. {
  1417. return mEnvironments[idx]->getSky();
  1418. }
  1419. }
  1420. }
  1421. return skyp;
  1422. }
  1423. const LLSettingsWater::ptr_t& LLEnvironment::getCurrentWater() const
  1424. {
  1425. const LLSettingsWater::ptr_t& waterp = mCurrentEnvironment->getWater();
  1426. if (!waterp && mCurrentEnvironment->getEnvironmentSelection() >= ENV_EDIT)
  1427. {
  1428. for (U32 idx = 0; idx < (U32)ENV_END; ++idx)
  1429. {
  1430. if (mEnvironments[idx]->getWater())
  1431. {
  1432. return mEnvironments[idx]->getWater();
  1433. }
  1434. }
  1435. }
  1436. return waterp;
  1437. }
  1438. //static
  1439. bool LLEnvironment::canAgentUpdateParcelEnvironment(LLParcel* parcel)
  1440. {
  1441. if (!parcel)
  1442. {
  1443. parcel = gViewerParcelMgr.getSelectedOrAgentParcel();
  1444. }
  1445. if (!parcel || !gAgent.hasExtendedEnvironment())
  1446. {
  1447. return false;
  1448. }
  1449. if (gAgent.isGodlike())
  1450. {
  1451. return true;
  1452. }
  1453. return parcel->getRegionAllowEnvironmentOverride() &&
  1454. LLViewerParcelMgr::isParcelModifiableByAgent(parcel,
  1455. GP_LAND_ALLOW_ENVIRONMENT);
  1456. }
  1457. //static
  1458. bool LLEnvironment::canAgentUpdateRegionEnvironment()
  1459. {
  1460. LLViewerRegion* regionp = gAgent.getRegion();
  1461. return regionp && (regionp->canManageEstate() || gAgent.isGodlike());
  1462. }
  1463. void LLEnvironment::onRegionChange()
  1464. {
  1465. // For now environmental experiences do not survive region crossings
  1466. clearExperienceEnvironment(LLUUID::null, TRANSITION_DEFAULT);
  1467. LLViewerRegion* regionp = gAgent.getRegion();
  1468. if (!regionp)
  1469. {
  1470. return;
  1471. }
  1472. if (regionp->capabilitiesReceived())
  1473. {
  1474. requestParcel(INVALID_PARCEL_ID);
  1475. return;
  1476. }
  1477. regionp->setCapsReceivedCB(boost::bind(&LLEnvironment::requestRegion,
  1478. env_apply_fn()));
  1479. }
  1480. void LLEnvironment::onParcelChange()
  1481. {
  1482. S32 parcel_id = INVALID_PARCEL_ID;
  1483. LLParcel* parcel = gViewerParcelMgr.getAgentParcel();
  1484. if (parcel)
  1485. {
  1486. parcel_id = parcel->getLocalID();
  1487. }
  1488. requestParcel(parcel_id);
  1489. }
  1490. F32 LLEnvironment::getCamHeight() const
  1491. {
  1492. #if LL_VARIABLE_SKY_DOME_SIZE
  1493. if (!mCurrentEnvironment || !mCurrentEnvironment->getSky())
  1494. {
  1495. return SKY_DOME_OFFSET * SKY_DOME_RADIUS;
  1496. }
  1497. return mCurrentEnvironment->getSky()->getDomeOffset() *
  1498. mCurrentEnvironment->getSky()->getDomeRadius();
  1499. #else
  1500. return SKY_DOME_OFFSET * SKY_DOME_RADIUS;
  1501. #endif
  1502. }
  1503. bool LLEnvironment::getIsSunUp() const
  1504. {
  1505. return mCurrentEnvironment && mCurrentEnvironment->getSky() &&
  1506. mCurrentEnvironment->getSky()->getIsSunUp();
  1507. }
  1508. bool LLEnvironment::getIsMoonUp() const
  1509. {
  1510. return mCurrentEnvironment && mCurrentEnvironment->getSky() &&
  1511. mCurrentEnvironment->getSky()->getIsMoonUp();
  1512. }
  1513. // Now, the silly thing to get a time-fixed environment is that we MUST use a
  1514. // blender, set its position (=time of day) and immediately apply the resulting
  1515. // sky and water as the new (fixed) environment. The motto at LL is obviously:
  1516. // Let's make simple things complicated ! HB
  1517. void LLEnvironment::setFixedTimeOfDay(F32 position, EEnvSelection env,
  1518. S32 env_version)
  1519. {
  1520. if (position < 0.f || position > 1.f)
  1521. {
  1522. return;
  1523. }
  1524. DayInstance::ptr_t envp = getEnvironmentInstance(env, true);
  1525. S32 sky_track = mCurrentTrack;
  1526. if (mCurrentTrack == LLSettingsDay::TRACK_WATER)
  1527. {
  1528. // Use the ground level track for the sky track if we are currently
  1529. // under water.
  1530. sky_track = LLSettingsDay::TRACK_GROUND_LEVEL;
  1531. }
  1532. LLSettingsSky::ptr_t skyp = envp->getSky();
  1533. LLSettingsWater::ptr_t waterp = envp->getWater();
  1534. LLSettingsDay::ptr_t dayp = envp->getDayCycle();
  1535. if (!dayp)
  1536. {
  1537. #if 1 // Problem: loading the default day cycle also loads the default
  1538. // Windlight sky and water settings... HB
  1539. envp->setDay(LLEnvSettingsDay::buildDefaultDayCycle(), 14400, 0);
  1540. dayp = envp->getDayCycle();
  1541. #else // Sadly, this does not work. FIXME. HB
  1542. LLSD day_cycle = LLSettingsDay::buildCycleFromFixed(skyp, waterp);
  1543. LLEnvSettingsDay::ptr_t cyclep =
  1544. std::make_shared<LLEnvSettingsDay>(day_cycle);
  1545. cyclep->initialize();
  1546. envp->setDay(cyclep->buildClone(), 14400, 0);
  1547. dayp = envp->getDayCycle();
  1548. skyp = envp->getSky();
  1549. waterp = envp->getWater();
  1550. #endif
  1551. }
  1552. LLTrackBlenderLoopingManual::ptr_t sky_blender =
  1553. std::make_shared<LLTrackBlenderLoopingManual>(skyp, dayp, sky_track);
  1554. LLTrackBlenderLoopingManual::ptr_t water_blender =
  1555. std::make_shared<LLTrackBlenderLoopingManual>(waterp, dayp,
  1556. LLSettingsDay::TRACK_WATER);
  1557. sky_blender->setPosition(position);
  1558. water_blender->setPosition(position);
  1559. setEnvironment(env, skyp, waterp, env_version);
  1560. updateEnvironment(TRANSITION_INSTANT, true);
  1561. }
  1562. void LLEnvironment::setLocalEnvFromDefaultWindlightDay(F32 position)
  1563. {
  1564. LLSettingsDay::ptr_t dayp = LLEnvSettingsDay::buildDefaultDayCycle();
  1565. if (!dayp) // Should never happen...
  1566. {
  1567. return;
  1568. }
  1569. if (mCurrentTrack > LLSettingsDay::TRACK_GROUND_LEVEL)
  1570. {
  1571. // There is only one sky track in Windlight...
  1572. mCurrentTrack = LLSettingsDay::TRACK_GROUND_LEVEL;
  1573. }
  1574. setEnvironment(ENV_LOCAL, dayp, 14400, 0);
  1575. updateEnvironment(TRANSITION_INSTANT);
  1576. setSelectedEnvironment(ENV_LOCAL);
  1577. setFixedTimeOfDay(position);
  1578. }
  1579. //static
  1580. void LLEnvironment::setSunrise()
  1581. {
  1582. #if LL_USE_FIXED_SKY_SETTINGS_FOR_DEFAULT_TIME
  1583. if (gAgent.hasExtendedEnvironment())
  1584. {
  1585. gEnvironment.setEnvironment(ENV_LOCAL, KNOWN_SKY_SUNRISE,
  1586. TRANSITION_INSTANT);
  1587. gEnvironment.setSelectedEnvironment(ENV_LOCAL, TRANSITION_INSTANT);
  1588. }
  1589. else
  1590. #endif
  1591. {
  1592. gEnvironment.setLocalEnvFromDefaultWindlightDay(0.25f);
  1593. }
  1594. }
  1595. //static
  1596. void LLEnvironment::setMidday()
  1597. {
  1598. #if LL_USE_FIXED_SKY_SETTINGS_FOR_DEFAULT_TIME
  1599. if (gAgent.hasExtendedEnvironment())
  1600. {
  1601. gEnvironment.setEnvironment(ENV_LOCAL, KNOWN_SKY_MIDDAY,
  1602. TRANSITION_INSTANT);
  1603. gEnvironment.setSelectedEnvironment(ENV_LOCAL, TRANSITION_INSTANT);
  1604. }
  1605. else
  1606. #endif
  1607. {
  1608. gEnvironment.setLocalEnvFromDefaultWindlightDay(0.567f);
  1609. }
  1610. }
  1611. //static
  1612. void LLEnvironment::setMiddayPBR()
  1613. {
  1614. static LLCachedControl<std::string> asset_id(gSavedSettings,
  1615. "PBRSkyMiddayAssetId");
  1616. if (gAgent.hasExtendedEnvironment())
  1617. {
  1618. gEnvironment.setEnvironment(ENV_LOCAL, LLUUID(asset_id()),
  1619. TRANSITION_INSTANT);
  1620. gEnvironment.setSelectedEnvironment(ENV_LOCAL, TRANSITION_INSTANT);
  1621. }
  1622. }
  1623. //static
  1624. void LLEnvironment::setSunset()
  1625. {
  1626. #if LL_USE_FIXED_SKY_SETTINGS_FOR_DEFAULT_TIME
  1627. if (gAgent.hasExtendedEnvironment())
  1628. {
  1629. gEnvironment.setEnvironment(ENV_LOCAL, KNOWN_SKY_SUNSET,
  1630. TRANSITION_INSTANT);
  1631. gEnvironment.setSelectedEnvironment(ENV_LOCAL, TRANSITION_INSTANT);
  1632. }
  1633. else
  1634. #endif
  1635. {
  1636. gEnvironment.setLocalEnvFromDefaultWindlightDay(0.75f);
  1637. }
  1638. }
  1639. //static
  1640. void LLEnvironment::setMidnight()
  1641. {
  1642. #if LL_USE_FIXED_SKY_SETTINGS_FOR_DEFAULT_TIME
  1643. if (gAgent.hasExtendedEnvironment())
  1644. {
  1645. gEnvironment.setEnvironment(ENV_LOCAL, KNOWN_SKY_MIDNIGHT,
  1646. TRANSITION_INSTANT);
  1647. gEnvironment.setSelectedEnvironment(ENV_LOCAL, TRANSITION_INSTANT);
  1648. }
  1649. else
  1650. #endif
  1651. {
  1652. gEnvironment.setLocalEnvFromDefaultWindlightDay(0.f);
  1653. }
  1654. }
  1655. //static
  1656. void LLEnvironment::setRegion()
  1657. {
  1658. if (!gAgent.hasExtendedEnvironment())
  1659. {
  1660. gEnvironment.setLocalEnvFromDefaultWindlightDay();
  1661. return;
  1662. }
  1663. gEnvironment.onParcelChange();
  1664. gEnvironment.clearEnvironment(ENV_LOCAL);
  1665. gEnvironment.setSelectedEnvironment(ENV_PARCEL);
  1666. }
  1667. void LLEnvironment::setSelectedEnvironment(EEnvSelection env, F64 transition,
  1668. bool forced)
  1669. {
  1670. // This is only here so that we can transition from parcel/region to local
  1671. // when applying local settings from inventory, RestrainedLove or Lua. The
  1672. // logic implemented in llviewercontrol.cpp will then care for changing
  1673. // UseParcelEnvironment accordingly and closing the floaters that need to
  1674. // be closed.
  1675. static LLCachedControl<bool> local(gSavedSettings, "UseLocalEnvironment");
  1676. if (env == ENV_LOCAL && !local)
  1677. {
  1678. gSavedSettings.setBool("UseLocalEnvironment", true);
  1679. }
  1680. mSelectedEnvironment = env;
  1681. updateEnvironment(transition, forced);
  1682. }
  1683. bool LLEnvironment::hasEnvironment(EEnvSelection env)
  1684. {
  1685. return env >= ENV_EDIT && env < ENV_DEFAULT && mEnvironments[env];
  1686. }
  1687. LLEnvironment::DayInstance::ptr_t
  1688. LLEnvironment::getEnvironmentInstance(LLEnvironment::EEnvSelection env,
  1689. bool create)
  1690. {
  1691. DayInstance::ptr_t environment = mEnvironments[env];
  1692. if (create)
  1693. {
  1694. if (environment)
  1695. {
  1696. environment = environment->clone();
  1697. }
  1698. else if (env == ENV_PUSH)
  1699. {
  1700. environment = std::make_shared<LLDayInjection>(env);
  1701. }
  1702. else
  1703. {
  1704. environment = std::make_shared<DayInstance>(env);
  1705. }
  1706. mEnvironments[env] = environment;
  1707. }
  1708. return environment;
  1709. }
  1710. void LLEnvironment::setCurrentEnvironmentSelection(EEnvSelection env)
  1711. {
  1712. mCurrentEnvironment->setEnvironmentSelection(env);
  1713. }
  1714. void LLEnvironment::setEnvironment(EEnvSelection env,
  1715. const LLSettingsDay::ptr_t& dayp,
  1716. S32 daylength, S32 dayoffset,
  1717. S32 env_version)
  1718. {
  1719. if (env < ENV_EDIT || env >= ENV_DEFAULT)
  1720. {
  1721. llwarns << "Attempted to change invalid environment selection. Aborted."
  1722. << llendl;
  1723. return;
  1724. }
  1725. DayInstance::ptr_t environment = getEnvironmentInstance(env, true);
  1726. environment->clear();
  1727. environment->setDay(dayp, daylength, dayoffset);
  1728. environment->setSkyTrack(mCurrentTrack);
  1729. environment->animate();
  1730. if (!mSignalEnvChanged.empty())
  1731. {
  1732. mSignalEnvChanged(env, env_version);
  1733. }
  1734. }
  1735. void LLEnvironment::setEnvironment(EEnvSelection env, fixed_env_t fixed,
  1736. S32 env_version)
  1737. {
  1738. if (env < ENV_EDIT || env >= ENV_DEFAULT)
  1739. {
  1740. llwarns << "Attempted to change invalid environment selection. Aborted."
  1741. << llendl;
  1742. return;
  1743. }
  1744. bool reset_probes = false;
  1745. DayInstance::ptr_t environment = getEnvironmentInstance(env, true);
  1746. if (fixed.first)
  1747. {
  1748. reset_probes = environment->setSky(fixed.first);
  1749. environment->setFlags(DayInstance::NO_ANIMATE_SKY);
  1750. }
  1751. else if (!environment->getSky())
  1752. {
  1753. if (mCurrentEnvironment->getEnvironmentSelection() != ENV_NONE)
  1754. {
  1755. // Note: this looks suspicious. Should not we assign whole day if
  1756. // mCurrentEnvironment has whole day ? And then add water/sky on
  1757. // top. This looks like it will result in sky using single keyframe
  1758. // instead of whole day if day is present when setting static water
  1759. // without static sky.
  1760. reset_probes = environment->setSky(mCurrentEnvironment->getSky());
  1761. environment->setFlags(DayInstance::NO_ANIMATE_SKY);
  1762. }
  1763. else
  1764. {
  1765. // Environment is not properly initialized yet, but we should have
  1766. // environment by this point.
  1767. DayInstance::ptr_t substitute = getEnvironmentInstance(ENV_PARCEL,
  1768. true);
  1769. if (!substitute || !substitute->getSky())
  1770. {
  1771. substitute = getEnvironmentInstance(ENV_REGION, true);
  1772. if (!substitute || !substitute->getSky())
  1773. {
  1774. substitute = getEnvironmentInstance(ENV_DEFAULT, true);
  1775. }
  1776. }
  1777. if (substitute && substitute->getSky())
  1778. {
  1779. reset_probes = environment->setSky(substitute->getSky());
  1780. environment->setFlags(DayInstance::NO_ANIMATE_SKY);
  1781. }
  1782. else
  1783. {
  1784. llwarns << "Failed to assign substitute sky: environment is not properly initialized"
  1785. << llendl;
  1786. }
  1787. }
  1788. }
  1789. if (fixed.second)
  1790. {
  1791. environment->setWater(fixed.second);
  1792. environment->setFlags(DayInstance::NO_ANIMATE_WATER);
  1793. }
  1794. else if (!environment->getWater())
  1795. {
  1796. if (mCurrentEnvironment->getEnvironmentSelection() != ENV_NONE)
  1797. {
  1798. // Note: this looks suspicious. Should not we assign whole day if
  1799. // mCurrentEnvironment has whole day ? And then add water/sky on
  1800. // top. This looks like it will result in water using single
  1801. // keyframe instead of whole day if day is present when setting
  1802. // static sky without static water.
  1803. environment->setWater(mCurrentEnvironment->getWater());
  1804. environment->setFlags(DayInstance::NO_ANIMATE_WATER);
  1805. }
  1806. else
  1807. {
  1808. // Environment is not properly initialized yet, but we should have
  1809. // environment by this point.
  1810. DayInstance::ptr_t substitute = getEnvironmentInstance(ENV_PARCEL,
  1811. true);
  1812. if (!substitute || !substitute->getWater())
  1813. {
  1814. substitute = getEnvironmentInstance(ENV_REGION, true);
  1815. if (!substitute || !substitute->getWater())
  1816. {
  1817. substitute = getEnvironmentInstance(ENV_DEFAULT, true);
  1818. }
  1819. }
  1820. if (substitute && substitute->getWater())
  1821. {
  1822. environment->setWater(substitute->getWater());
  1823. environment->setFlags(DayInstance::NO_ANIMATE_WATER);
  1824. }
  1825. else
  1826. {
  1827. llwarns << "Failed to assign substitute water: environment is not properly initialized"
  1828. << llendl;
  1829. }
  1830. }
  1831. }
  1832. if (reset_probes && gUsePBRShaders)
  1833. {
  1834. // The sky changed in a way that merits a reset of reflection probes
  1835. gPipeline.mReflectionMapManager.reset();
  1836. }
  1837. if (!mSignalEnvChanged.empty())
  1838. {
  1839. mSignalEnvChanged(env, env_version);
  1840. }
  1841. }
  1842. void LLEnvironment::setEnvironment(EEnvSelection env,
  1843. const LLSettingsBase::ptr_t& settings,
  1844. S32 env_version)
  1845. {
  1846. if (env == ENV_DEFAULT)
  1847. {
  1848. llwarns << "Attempt to set default environment. Forbidden." << llendl;
  1849. return;
  1850. }
  1851. if (!settings)
  1852. {
  1853. clearEnvironment(env);
  1854. return;
  1855. }
  1856. DayInstance::ptr_t environment = getEnvironmentInstance(env);
  1857. std::string settings_type = settings->getSettingsType();
  1858. if (settings_type == "daycycle")
  1859. {
  1860. S32 daylength = LLSettingsDay::DEFAULT_DAYLENGTH;
  1861. S32 dayoffset = LLSettingsDay::DEFAULT_DAYOFFSET;
  1862. if (environment)
  1863. {
  1864. daylength = environment->getDayLength();
  1865. dayoffset = environment->getDayOffset();
  1866. }
  1867. setEnvironment(env,
  1868. std::static_pointer_cast<LLSettingsDay>(settings),
  1869. daylength, dayoffset);
  1870. }
  1871. else if (settings_type == "sky")
  1872. {
  1873. fixed_env_t fixedenv(std::static_pointer_cast<LLSettingsSky>(settings),
  1874. LLSettingsWater::ptr_t());
  1875. setEnvironment(env, fixedenv);
  1876. }
  1877. else if (settings_type == "water")
  1878. {
  1879. fixed_env_t fixedenv(LLSettingsSky::ptr_t(),
  1880. std::static_pointer_cast<LLSettingsWater>(settings));
  1881. setEnvironment(env, fixedenv);
  1882. }
  1883. }
  1884. void LLEnvironment::setEnvironment(EEnvSelection env, const LLUUID& asset_id,
  1885. S32 transition)
  1886. {
  1887. if (!gAgent.hasExtendedEnvironment())
  1888. {
  1889. return;
  1890. }
  1891. LLEnvSettingsBase::getSettingsAsset(asset_id,
  1892. boost::bind(&LLEnvironment::onSetEnvAssetLoaded,
  1893. this, env, transition,
  1894. _1, _2, _3));
  1895. }
  1896. void LLEnvironment::onSetEnvAssetLoaded(EEnvSelection env, S32 transition,
  1897. const LLUUID& asset_id,
  1898. LLSettingsBase::ptr_t settings,
  1899. S32 status)
  1900. {
  1901. if (!settings || status)
  1902. {
  1903. LLSD args;
  1904. args["DESC"] = LLTrans::getString("asset_id") + ": " +
  1905. asset_id.asString();
  1906. gNotifications.add("FailedToFindSettings", args);
  1907. return;
  1908. }
  1909. setEnvironment(env, settings);
  1910. updateEnvironment(transition);
  1911. }
  1912. void LLEnvironment::clearEnvironment(EEnvSelection env)
  1913. {
  1914. if (env < ENV_EDIT || env >= ENV_DEFAULT)
  1915. {
  1916. llwarns << "Attempt to change invalid environment selection. Aborted."
  1917. << llendl;
  1918. return;
  1919. }
  1920. mEnvironments[env].reset();
  1921. if (!mSignalEnvChanged.empty())
  1922. {
  1923. mSignalEnvChanged(env, VERSION_CLEANUP);
  1924. }
  1925. // *TODO: readjust environment
  1926. }
  1927. LLSettingsDay::ptr_t LLEnvironment::getEnvironmentDay(EEnvSelection env)
  1928. {
  1929. if (env >= ENV_EDIT && env < ENV_DEFAULT)
  1930. {
  1931. DayInstance::ptr_t environment = getEnvironmentInstance(env);
  1932. if (environment)
  1933. {
  1934. return environment->getDayCycle();
  1935. }
  1936. }
  1937. else
  1938. {
  1939. llwarns << "Attempt to retrieve invalid environment selection. Aborted."
  1940. << llendl;
  1941. }
  1942. return LLSettingsDay::ptr_t();
  1943. }
  1944. S32 LLEnvironment::getEnvironmentDayLength(EEnvSelection env)
  1945. {
  1946. if (env >= ENV_EDIT && env < ENV_DEFAULT)
  1947. {
  1948. DayInstance::ptr_t environment = getEnvironmentInstance(env);
  1949. if (environment)
  1950. {
  1951. return environment->getDayLength();
  1952. }
  1953. }
  1954. else
  1955. {
  1956. llwarns << "Attempt to retrieve invalid environment selection. Aborted."
  1957. << llendl;
  1958. }
  1959. return 0;
  1960. }
  1961. S32 LLEnvironment::getEnvironmentDayOffset(EEnvSelection env)
  1962. {
  1963. if (env >= ENV_EDIT && env < ENV_DEFAULT)
  1964. {
  1965. DayInstance::ptr_t environment = getEnvironmentInstance(env);
  1966. if (environment)
  1967. {
  1968. return environment->getDayOffset();
  1969. }
  1970. }
  1971. else
  1972. {
  1973. llwarns << "Attempt to retrieve invalid environment selection. Aborted."
  1974. << llendl;
  1975. }
  1976. return 0;
  1977. }
  1978. LLEnvironment::fixed_env_t LLEnvironment::getEnvironmentFixed(EEnvSelection env,
  1979. bool resolve)
  1980. {
  1981. if (env == ENV_CURRENT || resolve)
  1982. {
  1983. fixed_env_t fixed;
  1984. for (U32 idx = resolve ? env : mSelectedEnvironment;
  1985. idx < (U32)ENV_END; ++idx)
  1986. {
  1987. if (fixed.first && fixed.second)
  1988. {
  1989. break;
  1990. }
  1991. if (idx == ENV_EDIT)
  1992. {
  1993. continue; // Skip the edit environment.
  1994. }
  1995. DayInstance::ptr_t dayp =
  1996. getEnvironmentInstance((EEnvSelection)idx);
  1997. if (dayp)
  1998. {
  1999. if (!fixed.first)
  2000. {
  2001. fixed.first = dayp->getSky();
  2002. }
  2003. if (!fixed.second)
  2004. {
  2005. fixed.second = dayp->getWater();
  2006. }
  2007. }
  2008. }
  2009. if (!fixed.first || !fixed.second)
  2010. {
  2011. llwarns << "Cannot construct complete fixed environment. Missing sky or water."
  2012. << llendl;
  2013. }
  2014. return fixed;
  2015. }
  2016. if (env >= ENV_EDIT && env <= ENV_DEFAULT)
  2017. {
  2018. DayInstance::ptr_t dayp = getEnvironmentInstance(env);
  2019. if (dayp)
  2020. {
  2021. return fixed_env_t(dayp->getSky(), dayp->getWater());
  2022. }
  2023. }
  2024. else
  2025. {
  2026. llwarns << "Attempt to retrieve invalid environment selection. Aborted."
  2027. << llendl;
  2028. }
  2029. return fixed_env_t();
  2030. }
  2031. LLEnvironment::DayInstance::ptr_t LLEnvironment::getSelectedEnvironmentInstance()
  2032. {
  2033. for (U32 idx = mSelectedEnvironment; idx < (U32)ENV_DEFAULT; ++idx)
  2034. {
  2035. if (mEnvironments[idx])
  2036. {
  2037. return mEnvironments[idx];
  2038. }
  2039. }
  2040. return mEnvironments[ENV_DEFAULT];
  2041. }
  2042. LLEnvironment::DayInstance::ptr_t LLEnvironment::getSharedEnvironmentInstance()
  2043. {
  2044. for (U32 idx = ENV_PARCEL; idx < (U32)ENV_DEFAULT; ++idx)
  2045. {
  2046. if (mEnvironments[idx])
  2047. {
  2048. return mEnvironments[idx];
  2049. }
  2050. }
  2051. return mEnvironments[ENV_DEFAULT];
  2052. }
  2053. void LLEnvironment::updateEnvironment(F64 transition, bool forced)
  2054. {
  2055. DayInstance::ptr_t instancep = getSelectedEnvironmentInstance();
  2056. if (!forced && instancep == mCurrentEnvironment)
  2057. {
  2058. return;
  2059. }
  2060. if (transition == TRANSITION_INSTANT)
  2061. {
  2062. mCurrentEnvironment = instancep;
  2063. updateSettingsUniforms();
  2064. return;
  2065. }
  2066. DayInstance::ptr_t trans =
  2067. std::make_shared<DayTransition>(mCurrentEnvironment->getSky(),
  2068. mCurrentEnvironment->getWater(),
  2069. instancep, transition);
  2070. trans->animate();
  2071. mCurrentEnvironment = trans;
  2072. updateSettingsUniforms();
  2073. }
  2074. LLVector3 LLEnvironment::getLightDirection() const
  2075. {
  2076. const LLSettingsSky::ptr_t& skyp = mCurrentEnvironment->getSky();
  2077. return skyp ? skyp->getLightDirection() : LLVector3::z_axis;
  2078. }
  2079. LLVector3 LLEnvironment::getSunDirection() const
  2080. {
  2081. const LLSettingsSky::ptr_t& skyp = mCurrentEnvironment->getSky();
  2082. return skyp ? skyp->getSunDirection() : LLVector3::z_axis;
  2083. }
  2084. LLVector3 LLEnvironment::getMoonDirection() const
  2085. {
  2086. const LLSettingsSky::ptr_t& skyp = mCurrentEnvironment->getSky();
  2087. return skyp ? skyp->getMoonDirection() : -LLVector3::z_axis;
  2088. }
  2089. LLVector4 LLEnvironment::getClampedLightNorm() const
  2090. {
  2091. LLVector3 light_direction = getLightDirection();
  2092. if (light_direction.mV[2] < -0.1f)
  2093. {
  2094. light_direction.mV[2] = -0.1f;
  2095. }
  2096. return toLightNorm(light_direction);
  2097. }
  2098. LLVector4 LLEnvironment::getClampedSunNorm() const
  2099. {
  2100. LLVector3 light_direction = getSunDirection();
  2101. if (light_direction.mV[2] < -0.1f)
  2102. {
  2103. light_direction.mV[2] = -0.1f;
  2104. }
  2105. return toLightNorm(light_direction);
  2106. }
  2107. LLVector4 LLEnvironment::getClampedMoonNorm() const
  2108. {
  2109. LLVector3 light_direction = getMoonDirection();
  2110. if (light_direction.mV[2] < -0.1f)
  2111. {
  2112. light_direction.mV[2] = -0.1f;
  2113. }
  2114. return toLightNorm(light_direction);
  2115. }
  2116. LLVector4 LLEnvironment::getRotatedLightNorm() const
  2117. {
  2118. LLVector3 light_direction = getLightDirection();
  2119. light_direction *= LLQuaternion(-mLastCamYaw, LLVector3::y_axis);
  2120. return toLightNorm(light_direction);
  2121. }
  2122. void LLEnvironment::update()
  2123. {
  2124. LL_TRACY_TIMER(TRC_ENVIRONMENT_UPDATE);
  2125. static LLFrameTimer timer;
  2126. F32 delta = timer.getElapsedTimeAndResetF32();
  2127. // Make sure the current environment does not go away until
  2128. // applyTimeDelta() is done.
  2129. DayInstance::ptr_t keeper = mCurrentEnvironment;
  2130. mCurrentEnvironment->applyTimeDelta(delta);
  2131. // Update clouds, Sun and general
  2132. updateCloudScroll();
  2133. // Cache this for use in rotating the rotated light vec for shader param
  2134. // updates later...
  2135. mLastCamYaw = gViewerCamera.getYaw() + SUN_DELTA_YAW;
  2136. // Cache the environment values for reuse by the render pipeline during the
  2137. // upcomming frame. HB
  2138. gPipeline.cacheEnvironment();
  2139. updateSettingsUniforms();
  2140. dirtyUniforms();
  2141. }
  2142. void LLEnvironment::updateCloudScroll()
  2143. {
  2144. // This is a function of the environment rather than the sky, since it
  2145. // should persist through sky transitions.
  2146. static LLTimer s_cloud_timer;
  2147. if (!mCloudScrollPaused && mCurrentEnvironment &&
  2148. mCurrentEnvironment->getSky())
  2149. {
  2150. F64 delta_t = s_cloud_timer.getElapsedTimeAndResetF64() / 100.f;
  2151. mCloudScrollDelta +=
  2152. mCurrentEnvironment->getSky()->getCloudScrollRate() * delta_t;
  2153. }
  2154. }
  2155. //static
  2156. void LLEnvironment::updateGLVariablesForSettings(LLShaderUniforms* uniforms,
  2157. const LLSettingsBase::ptr_t& settings)
  2158. {
  2159. for (U32 i = 0; i < (U32)LLGLSLShader::SG_COUNT; ++i)
  2160. {
  2161. uniforms[i].clear();
  2162. }
  2163. LLShaderUniforms* shaderp = &uniforms[LLGLSLShader::SG_ANY];
  2164. const LLSettingsBase::parammapping_t& params = settings->getParameterMap();
  2165. LLSD::map_const_iterator settings_end = settings->mSettings.endMap();
  2166. // Check for SETTING_LEGACY_HAZE sub-map: only sky settings may have it. HB
  2167. LLSD::map_const_iterator haze_it, haze_end, param_it;
  2168. bool has_legacy_haze =
  2169. settings->getSettingsTypeValue() == LLSettingsType::ST_SKY;
  2170. if (has_legacy_haze)
  2171. {
  2172. haze_it = settings->mSettings.find(LLSettingsSky::SETTING_LEGACY_HAZE);
  2173. has_legacy_haze = haze_it != settings_end;
  2174. if (has_legacy_haze)
  2175. {
  2176. haze_end = haze_it->second.endMap();
  2177. }
  2178. }
  2179. static LLCachedControl<bool> desaturate(gSavedSettings,
  2180. "RenderSkyReflectionDesaturate");
  2181. static LLCachedControl<F32> desaturation(gSavedSettings,
  2182. "RenderSkyReflectionDesaturation");
  2183. static LLCachedControl<F32> darkening(gSavedSettings,
  2184. "RenderSkyReflectionDarkening");
  2185. bool is_irradiance = gUsePBRShaders &&
  2186. gPipeline.mReflectionMapManager.isRadiancePass();
  2187. for (const auto& it : params)
  2188. {
  2189. const std::string& key = it.first;
  2190. LLSD value;
  2191. // Legacy first since it contains ambient color and we prioritize value
  2192. // from legacy; see getAmbientColor().
  2193. if (has_legacy_haze)
  2194. {
  2195. param_it = haze_it->second.find(key);
  2196. if (param_it != haze_end)
  2197. {
  2198. value = param_it->second;
  2199. }
  2200. }
  2201. if (value.isUndefined())
  2202. {
  2203. param_it = settings->mSettings.find(key);
  2204. if (param_it != settings_end)
  2205. {
  2206. value = param_it->second;
  2207. }
  2208. }
  2209. if (value.isUndefined())
  2210. {
  2211. // We need to reset shaders, use defaults
  2212. value = it.second.getDefaultValue();
  2213. }
  2214. S32 shader_key = it.second.getShaderKey();
  2215. LLSD::Type setting_type = value.type();
  2216. stop_glerror();
  2217. switch (setting_type)
  2218. {
  2219. case LLSD::TypeInteger:
  2220. shaderp->uniform1i(shader_key, value.asInteger());
  2221. break;
  2222. case LLSD::TypeReal:
  2223. shaderp->uniform1f(shader_key, value.asReal());
  2224. break;
  2225. case LLSD::TypeBoolean:
  2226. shaderp->uniform1i(shader_key, value.asBoolean() ? 1 : 0);
  2227. break;
  2228. case LLSD::TypeArray:
  2229. {
  2230. if (!gUsePBRShaders)
  2231. {
  2232. shaderp->uniform4fv(shader_key, LLVector4(value));
  2233. break;
  2234. }
  2235. LLVector4 vect4(value);
  2236. if (gCubeSnapshot &&
  2237. (shader_key == LLShaderMgr::BLUE_HORIZON ||
  2238. shader_key == LLShaderMgr::BLUE_DENSITY))
  2239. {
  2240. LLColor3 color(vect4);
  2241. if (is_irradiance)
  2242. {
  2243. if (desaturate &&
  2244. // Do not apply this fix to mirrors; we do not
  2245. // want desaturating the mirror reflections. HB
  2246. !gPipeline.mHeroProbeManager.isMirrorPass())
  2247. {
  2248. // Attenuate blue hue if this is a reflection probe
  2249. // radiance pass. HB
  2250. color.desaturate(desaturation, darkening);
  2251. }
  2252. vect4.mV[0] = color.mV[0];
  2253. vect4.mV[1] = color.mV[1];
  2254. vect4.mV[2] = color.mV[2];
  2255. }
  2256. else
  2257. {
  2258. // Maximize and remove tinting if this is an irradiance
  2259. // map render pass and the parameter feeds into the sky
  2260. // background color.
  2261. F32 h, s, l;
  2262. color.calcHSL(&h, &s, &l);
  2263. vect4.mV[0] = vect4.mV[1] = vect4.mV[2] = l;
  2264. }
  2265. }
  2266. shaderp->uniform3fv(shader_key, LLVector3(vect4.mV));
  2267. }
  2268. default:
  2269. break;
  2270. }
  2271. }
  2272. settings->applySpecial(uniforms);
  2273. }
  2274. void LLEnvironment::updateShaderUniforms(LLGLSLShader* shaderp)
  2275. {
  2276. // Apply uniforms that should be applied to all shaders
  2277. mSkyUniforms[LLGLSLShader::SG_ANY].apply(shaderp);
  2278. mWaterUniforms[LLGLSLShader::SG_ANY].apply(shaderp);
  2279. // Apply uniforms specific to the given shader's shader group
  2280. S32 group = shaderp->mShaderGroup;
  2281. mSkyUniforms[group].apply(shaderp);
  2282. mWaterUniforms[group].apply(shaderp);
  2283. }
  2284. void LLEnvironment::updateShaderSkyUniforms(LLGLSLShader* shaderp)
  2285. {
  2286. // Apply uniforms that should be applied to all shaders
  2287. mSkyUniforms[LLGLSLShader::SG_ANY].apply(shaderp);
  2288. // Apply uniforms specific to the given shader's shader group
  2289. S32 group = shaderp->mShaderGroup;
  2290. mSkyUniforms[group].apply(shaderp);
  2291. }
  2292. void LLEnvironment::updateSettingsUniforms()
  2293. {
  2294. if (mCurrentEnvironment->getWater())
  2295. {
  2296. updateGLVariablesForSettings(mWaterUniforms,
  2297. mCurrentEnvironment->getWater());
  2298. }
  2299. if (mCurrentEnvironment->getSky())
  2300. {
  2301. updateGLVariablesForSettings(mSkyUniforms,
  2302. mCurrentEnvironment->getSky());
  2303. }
  2304. }
  2305. void LLEnvironment::dirtyUniforms()
  2306. {
  2307. bool can_use_shaders = gPipeline.canUseWindLightShaders();
  2308. if (!can_use_shaders)
  2309. {
  2310. // We still need to update uniforms for these shaders...
  2311. gWLSunProgram.mUniformsDirty = true;
  2312. gWLMoonProgram.mUniformsDirty = true;
  2313. }
  2314. // Now scan the list of shaders that might be interested in environment
  2315. // updates, and mark them as having dirty uniforms.
  2316. const LLViewerShaderMgr::shaders_list_t& shaders =
  2317. gViewerShaderMgrp->getEnvShadersList();
  2318. for (U32 i = 0, count = shaders.size(); i < count; ++i)
  2319. {
  2320. LLGLSLShader* shaderp = shaders[i];
  2321. if (shaderp->mProgramObject &&
  2322. (can_use_shaders ||
  2323. shaderp->mShaderGroup == LLGLSLShader::SG_WATER))
  2324. {
  2325. shaderp->mUniformsDirty = true;
  2326. if (shaderp->mRiggedVariant)
  2327. {
  2328. shaderp->mRiggedVariant->mUniformsDirty = true;
  2329. }
  2330. }
  2331. }
  2332. }
  2333. void LLEnvironment::recordEnvironment(S32 parcel_id, envinfo_ptr_t envinfo,
  2334. F64 transition)
  2335. {
  2336. LLViewerRegion* regionp = gAgent.getRegion();
  2337. if (!regionp) return;
  2338. if (regionp->getRegionID() != envinfo->mRegionId &&
  2339. envinfo->mRegionId.notNull())
  2340. {
  2341. llinfos << "Requested environment region id: " << envinfo->mRegionId
  2342. << " while agent is on: " << regionp->getRegionID() << llendl;
  2343. return;
  2344. }
  2345. if (envinfo->mParcelId == INVALID_PARCEL_ID)
  2346. {
  2347. // The returned info applies to an entire region.
  2348. if (!envinfo->mDayCycle)
  2349. {
  2350. clearEnvironment(ENV_PARCEL);
  2351. setEnvironment(ENV_REGION, LLSettingsDay::getDefaultAssetId());
  2352. updateEnvironment();
  2353. }
  2354. else if (envinfo->mDayCycle->isTrackEmpty(LLSettingsDay::TRACK_WATER) ||
  2355. envinfo->mDayCycle->isTrackEmpty(LLSettingsDay::TRACK_GROUND_LEVEL))
  2356. {
  2357. llwarns << "Invalid day cycle for region" << llendl;
  2358. clearEnvironment(ENV_PARCEL);
  2359. setEnvironment(ENV_REGION, LLSettingsDay::getDefaultAssetId());
  2360. updateEnvironment();
  2361. }
  2362. else
  2363. {
  2364. mTrackAltitudes = envinfo->mAltitudes;
  2365. // Update track selection based on new altitudes
  2366. mCurrentTrack =
  2367. calculateSkyTrackForAltitude(gAgent.getPositionAgent().mV[VZ]);
  2368. setEnvironment(ENV_REGION, envinfo->mDayCycle, envinfo->mDayLength,
  2369. envinfo->mDayOffset, envinfo->mEnvVersion);
  2370. }
  2371. LL_DEBUGS("Environment") << "Altitudes set to {" << mTrackAltitudes[0]
  2372. << ", " << mTrackAltitudes[1] << ", "
  2373. << mTrackAltitudes[2] << ", "
  2374. << mTrackAltitudes[3] << LL_ENDL;
  2375. }
  2376. else
  2377. {
  2378. LLParcel* parcel = gViewerParcelMgr.getAgentParcel();
  2379. LL_DEBUGS("Environment") << "Parcel environment #"
  2380. << envinfo->mParcelId << LL_ENDL;
  2381. if (parcel && parcel->getLocalID() != parcel_id)
  2382. {
  2383. LL_DEBUGS("Environment") << "Requested parcel #" << parcel_id
  2384. << " while agent is on "
  2385. << parcel->getLocalID() << LL_ENDL;
  2386. return;
  2387. }
  2388. if (!envinfo->mDayCycle)
  2389. {
  2390. LL_DEBUGS("Environment") << "Clearing environment on parcel #"
  2391. << parcel_id << LL_ENDL;
  2392. clearEnvironment(ENV_PARCEL);
  2393. }
  2394. else if (envinfo->mDayCycle->isTrackEmpty(LLSettingsDay::TRACK_WATER) ||
  2395. envinfo->mDayCycle->isTrackEmpty(LLSettingsDay::TRACK_GROUND_LEVEL))
  2396. {
  2397. llwarns << "Invalid day cycle for parcel #" << parcel_id << llendl;
  2398. clearEnvironment(ENV_PARCEL);
  2399. }
  2400. else
  2401. {
  2402. setEnvironment(ENV_PARCEL, envinfo->mDayCycle, envinfo->mDayLength,
  2403. envinfo->mDayOffset, envinfo->mEnvVersion);
  2404. }
  2405. }
  2406. updateEnvironment(transition);
  2407. }
  2408. void LLEnvironment::adjustRegionOffset(F32 adjust)
  2409. {
  2410. if (gAgent.hasExtendedEnvironment())
  2411. {
  2412. llwarns << "Attempt to adjust region offset on EEP region. Legacy regions only."
  2413. << llendl;
  2414. }
  2415. if (mEnvironments[ENV_REGION])
  2416. {
  2417. F32 day_length = mEnvironments[ENV_REGION]->getDayLength();
  2418. F32 day_offset = mEnvironments[ENV_REGION]->getDayOffset();
  2419. day_offset += adjust * day_length;
  2420. if (day_offset < 0.f)
  2421. {
  2422. day_offset += day_length;
  2423. }
  2424. mEnvironments[ENV_REGION]->setDayOffset(day_offset);
  2425. }
  2426. }
  2427. //static
  2428. void LLEnvironment::requestRegion(env_apply_fn cb)
  2429. {
  2430. if (gEnvironment.mInstanceValid)
  2431. {
  2432. gEnvironment.requestParcel(INVALID_PARCEL_ID, cb);
  2433. }
  2434. }
  2435. void LLEnvironment::updateRegion(const LLSettingsDay::ptr_t& dayp,
  2436. S32 day_length, S32 day_offset,
  2437. altitudes_vect_t altitudes, env_apply_fn cb)
  2438. {
  2439. updateParcel(INVALID_PARCEL_ID, dayp, day_length, day_offset, altitudes,
  2440. cb);
  2441. }
  2442. void LLEnvironment::updateRegion(const LLUUID& asset_id,
  2443. const std::string& display_name,
  2444. S32 track_num, S32 day_length, S32 day_offset,
  2445. U32 flags, altitudes_vect_t altitudes,
  2446. env_apply_fn cb)
  2447. {
  2448. if (gAgent.hasExtendedEnvironment())
  2449. {
  2450. updateParcel(INVALID_PARCEL_ID, asset_id, display_name, track_num,
  2451. day_length, day_offset, flags, altitudes, cb);
  2452. }
  2453. else
  2454. {
  2455. gNotifications.add("NoEnvironmentSettings");
  2456. }
  2457. }
  2458. void LLEnvironment::updateRegion(const LLSettingsSky::ptr_t& skyp,
  2459. S32 day_length, S32 day_offset,
  2460. altitudes_vect_t altitudes, env_apply_fn cb)
  2461. {
  2462. updateParcel(INVALID_PARCEL_ID, skyp, day_length, day_offset, altitudes,
  2463. cb);
  2464. }
  2465. void LLEnvironment::updateRegion(const LLSettingsWater::ptr_t& waterp,
  2466. S32 day_length, S32 day_offset,
  2467. altitudes_vect_t altitudes, env_apply_fn cb)
  2468. {
  2469. updateParcel(INVALID_PARCEL_ID, waterp, day_length, day_offset, altitudes,
  2470. cb);
  2471. }
  2472. void LLEnvironment::resetRegion(env_apply_fn cb)
  2473. {
  2474. resetParcel(INVALID_PARCEL_ID, cb);
  2475. }
  2476. void LLEnvironment::requestParcel(S32 parcel_id, env_apply_fn cb)
  2477. {
  2478. if (!gAgent.hasExtendedEnvironment())
  2479. {
  2480. if (parcel_id == INVALID_PARCEL_ID)
  2481. {
  2482. if (!cb)
  2483. {
  2484. F64 transition = gAgent.teleportInProgress() ||
  2485. gViewerParcelMgr.waitingForParcelInfo() ?
  2486. TRANSITION_FAST : TRANSITION_DEFAULT;
  2487. cb = [this, transition](S32 pid, envinfo_ptr_t info)
  2488. {
  2489. clearEnvironment(ENV_PARCEL);
  2490. recordEnvironment(pid, info, transition);
  2491. };
  2492. }
  2493. LLEnvironmentRequest::initiate(cb);
  2494. }
  2495. else if (cb)
  2496. {
  2497. cb(parcel_id, envinfo_ptr_t());
  2498. }
  2499. return;
  2500. }
  2501. if (!cb)
  2502. {
  2503. F64 transition = gAgent.teleportInProgress() ||
  2504. gViewerParcelMgr.waitingForParcelInfo() ?
  2505. TRANSITION_FAST : TRANSITION_DEFAULT;
  2506. cb = [this, transition](S32 pid, envinfo_ptr_t info)
  2507. {
  2508. recordEnvironment(pid, info, transition);
  2509. };
  2510. }
  2511. gCoros.launch("LLEnvironment::coroRequestEnvironment",
  2512. boost::bind(&LLEnvironment::coroRequestEnvironment, this,
  2513. parcel_id, cb));
  2514. }
  2515. void LLEnvironment::updateParcel(S32 parcel_id, const LLUUID& asset_id,
  2516. const std::string& display_name,
  2517. S32 track_num, S32 day_length, S32 day_offset,
  2518. U32 flags, altitudes_vect_t altitudes,
  2519. env_apply_fn cb)
  2520. {
  2521. UpdateInfo::ptr_t updates = std::make_shared<UpdateInfo>(asset_id,
  2522. display_name,
  2523. day_length,
  2524. day_offset,
  2525. altitudes,
  2526. flags);
  2527. gCoros.launch("LLEnvironment::coroUpdateEnvironment",
  2528. boost::bind(&LLEnvironment::coroUpdateEnvironment, this,
  2529. parcel_id, track_num, updates, cb));
  2530. }
  2531. void LLEnvironment::onUpdateParcelAssetLoaded(const LLUUID& asset_id,
  2532. LLSettingsBase::ptr_t settings,
  2533. S32 status, S32 parcel_id,
  2534. S32 day_length, S32 day_offset,
  2535. altitudes_vect_t altitudes)
  2536. {
  2537. if (status)
  2538. {
  2539. gNotifications.add("FailedToLoadSettingsApply");
  2540. return;
  2541. }
  2542. LLSettingsDay::ptr_t dayp;
  2543. if (settings->getSettingsType() == "daycycle")
  2544. {
  2545. dayp = std::static_pointer_cast<LLSettingsDay>(settings);
  2546. }
  2547. else
  2548. {
  2549. dayp = createDayCycleFromEnvironment(parcel_id == INVALID_PARCEL_ID ?
  2550. ENV_REGION : ENV_PARCEL,
  2551. settings);
  2552. }
  2553. if (!dayp)
  2554. {
  2555. gNotifications.add("FailedToLoadSettingsApply");
  2556. return;
  2557. }
  2558. updateParcel(parcel_id, dayp, day_length, day_offset, altitudes);
  2559. }
  2560. void LLEnvironment::updateParcel(S32 parcel_id,
  2561. const LLSettingsSky::ptr_t& skyp,
  2562. S32 day_length, S32 day_offset,
  2563. altitudes_vect_t altitudes, env_apply_fn cb)
  2564. {
  2565. LLSettingsDay::ptr_t dayp =
  2566. createDayCycleFromEnvironment(parcel_id == INVALID_PARCEL_ID ?
  2567. ENV_REGION : ENV_PARCEL,
  2568. skyp);
  2569. dayp->setFlag(skyp->getFlags());
  2570. updateParcel(parcel_id, dayp, day_length, day_offset, altitudes, cb);
  2571. }
  2572. void LLEnvironment::updateParcel(S32 parcel_id,
  2573. const LLSettingsWater::ptr_t& waterp,
  2574. S32 day_length, S32 day_offset,
  2575. altitudes_vect_t altitudes, env_apply_fn cb)
  2576. {
  2577. LLSettingsDay::ptr_t dayp =
  2578. createDayCycleFromEnvironment(parcel_id == INVALID_PARCEL_ID ?
  2579. ENV_REGION : ENV_PARCEL,
  2580. waterp);
  2581. dayp->setFlag(waterp->getFlags());
  2582. updateParcel(parcel_id, dayp, day_length, day_offset, altitudes, cb);
  2583. }
  2584. void LLEnvironment::updateParcel(S32 parcel_id,
  2585. const LLSettingsDay::ptr_t& dayp,
  2586. S32 track_num, S32 day_length, S32 day_offset,
  2587. altitudes_vect_t altitudes, env_apply_fn cb)
  2588. {
  2589. UpdateInfo::ptr_t updates = std::make_shared<UpdateInfo>(dayp, day_length,
  2590. day_offset,
  2591. altitudes);
  2592. gCoros.launch("LLEnvironment::coroUpdateEnvironment",
  2593. boost::bind(&LLEnvironment::coroUpdateEnvironment, this,
  2594. parcel_id, track_num, updates, cb));
  2595. }
  2596. void LLEnvironment::updateParcel(S32 parcel_id,
  2597. const LLSettingsDay::ptr_t& dayp,
  2598. S32 day_length, S32 day_offset,
  2599. altitudes_vect_t altitudes, env_apply_fn cb)
  2600. {
  2601. updateParcel(parcel_id, dayp, NO_TRACK, day_length, day_offset, altitudes,
  2602. cb);
  2603. }
  2604. void LLEnvironment::resetParcel(S32 parcel_id, env_apply_fn cb)
  2605. {
  2606. gCoros.launch("LLEnvironment::coroResetEnvironment",
  2607. boost::bind(&LLEnvironment::coroResetEnvironment, this,
  2608. parcel_id, NO_TRACK, cb));
  2609. }
  2610. void LLEnvironment::coroRequestEnvironment(S32 parcel_id, env_apply_fn apply)
  2611. {
  2612. std::string url = gAgent.getRegionCapability("ExtEnvironment");
  2613. if (url.empty())
  2614. {
  2615. return;
  2616. }
  2617. LL_DEBUGS("Environment") << "Requesting for parcel_id=" << parcel_id
  2618. << LL_ENDL;
  2619. if (parcel_id != INVALID_PARCEL_ID)
  2620. {
  2621. url += llformat("?parcelid=%d", parcel_id);
  2622. }
  2623. LLCoreHttpUtil::HttpCoroutineAdapter adapter("RequestEnvironment");
  2624. LLSD result = adapter.getAndSuspend(url);
  2625. if (LLApp::isExiting())
  2626. {
  2627. return;
  2628. }
  2629. // Results that come back may contain the new settings
  2630. LLCore::HttpStatus status =
  2631. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  2632. if (!status)
  2633. {
  2634. llwarns << "Could not retrieve environment settings for "
  2635. << (parcel_id == INVALID_PARCEL_ID ? "region" : "parcel")
  2636. << llendl;
  2637. return;
  2638. }
  2639. LLSD environment = result[KEY_ENVIRONMENT];
  2640. if (environment.isDefined() && apply)
  2641. {
  2642. envinfo_ptr_t envinfo = EnvironmentInfo::extract(environment);
  2643. apply(parcel_id, envinfo);
  2644. }
  2645. }
  2646. void LLEnvironment::coroUpdateEnvironment(S32 parcel_id, S32 track_no,
  2647. UpdateInfo::ptr_t updates,
  2648. env_apply_fn apply)
  2649. {
  2650. std::string url = gAgent.getRegionCapability("ExtEnvironment");
  2651. if (url.empty())
  2652. {
  2653. return;
  2654. }
  2655. LLSD body(LLSD::emptyMap());
  2656. body[KEY_ENVIRONMENT] = LLSD::emptyMap();
  2657. if (track_no == NO_TRACK)
  2658. {
  2659. // Day length and offset are only applicable if we are addressing the
  2660. // entire day cycle.
  2661. if (updates->mDayLength > 0)
  2662. {
  2663. body[KEY_ENVIRONMENT][KEY_DAYLENGTH] = updates->mDayLength;
  2664. }
  2665. if (updates->mDayOffset > 0)
  2666. {
  2667. body[KEY_ENVIRONMENT][KEY_DAYOFFSET] = updates->mDayOffset;
  2668. }
  2669. if (parcel_id == INVALID_PARCEL_ID && updates->mAltitudes.size() == 3)
  2670. {
  2671. // Only test for altitude changes if we are changing the region.
  2672. body[KEY_ENVIRONMENT][KEY_TRACKALTS] = LLSD::emptyArray();
  2673. for (S32 i = 0; i < 3; ++i)
  2674. {
  2675. body[KEY_ENVIRONMENT][KEY_TRACKALTS][i] =
  2676. updates->mAltitudes[i];
  2677. }
  2678. }
  2679. }
  2680. if (updates->mDayp)
  2681. {
  2682. body[KEY_ENVIRONMENT][KEY_DAYCYCLE] = updates->mDayp->getSettings();
  2683. }
  2684. else if (!updates->mSettingsAsset.isNull())
  2685. {
  2686. body[KEY_ENVIRONMENT][KEY_DAYASSET] = updates->mSettingsAsset;
  2687. if (!updates->mDayName.empty())
  2688. {
  2689. body[KEY_ENVIRONMENT][KEY_DAYNAME] = updates->mDayName;
  2690. }
  2691. }
  2692. body[KEY_ENVIRONMENT][KEY_FLAGS] = LLSD::Integer(updates->mFlags);
  2693. if (parcel_id != INVALID_PARCEL_ID || track_no != NO_TRACK)
  2694. {
  2695. url += '?';
  2696. if (parcel_id != INVALID_PARCEL_ID)
  2697. {
  2698. url += llformat("parcelid=%d", parcel_id);
  2699. if (track_no != NO_TRACK)
  2700. {
  2701. url += '&';
  2702. }
  2703. }
  2704. if (track_no != NO_TRACK)
  2705. {
  2706. url += llformat("&trackno=%d", track_no);
  2707. }
  2708. }
  2709. LLCoreHttpUtil::HttpCoroutineAdapter adapter("UpdateEnvironment");
  2710. LLSD result = adapter.putAndSuspend(url, body);
  2711. if (LLApp::isExiting())
  2712. {
  2713. return;
  2714. }
  2715. // Results that come back may contain the new settings
  2716. LLCore::HttpStatus status =
  2717. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  2718. if (!status || !result["success"].asBoolean())
  2719. {
  2720. LLSD notify = LLSD::emptyMap();
  2721. std::string reason = result["message"].asString();
  2722. if (reason.empty())
  2723. {
  2724. reason = status.toString();
  2725. }
  2726. notify["FAIL_REASON"] = reason;
  2727. gNotifications.add("WLRegionApplyFail", notify);
  2728. return;
  2729. }
  2730. LLSD environment = result[KEY_ENVIRONMENT];
  2731. if (environment.isDefined() && apply)
  2732. {
  2733. envinfo_ptr_t envinfo = EnvironmentInfo::extract(environment);
  2734. apply(parcel_id, envinfo);
  2735. }
  2736. }
  2737. void LLEnvironment::coroResetEnvironment(S32 parcel_id, S32 track_no,
  2738. env_apply_fn apply)
  2739. {
  2740. std::string url = gAgent.getRegionCapability("ExtEnvironment");
  2741. if (url.empty())
  2742. {
  2743. return;
  2744. }
  2745. if (parcel_id != INVALID_PARCEL_ID || track_no != NO_TRACK)
  2746. {
  2747. url += '?';
  2748. if (parcel_id != INVALID_PARCEL_ID)
  2749. {
  2750. url += llformat("parcelid=%d", parcel_id);
  2751. if (track_no != NO_TRACK)
  2752. {
  2753. url += '&';
  2754. }
  2755. }
  2756. if (track_no != NO_TRACK)
  2757. {
  2758. url += llformat("trackno=%d", track_no);
  2759. }
  2760. }
  2761. LLCoreHttpUtil::HttpCoroutineAdapter adapter("ResetEnvironment");
  2762. LLSD result = adapter.deleteAndSuspend(url);
  2763. if (LLApp::isExiting())
  2764. {
  2765. return;
  2766. }
  2767. // Results that come back may contain the new settings
  2768. LLCore::HttpStatus status =
  2769. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  2770. if (!status || !result["success"].asBoolean())
  2771. {
  2772. llwarns << "Could not reset Windlight settings in "
  2773. << (parcel_id == INVALID_PARCEL_ID ? "region" : "parcel")
  2774. << llendl;
  2775. LLSD notify = LLSD::emptyMap();
  2776. std::string reason = result["message"].asString();
  2777. if (reason.empty())
  2778. {
  2779. reason = status.toString();
  2780. }
  2781. notify["FAIL_REASON"] = reason;
  2782. gNotifications.add("WLRegionApplyFail", notify);
  2783. return;
  2784. }
  2785. LLSD environment = result[KEY_ENVIRONMENT];
  2786. if (environment.isDefined() && apply)
  2787. {
  2788. envinfo_ptr_t envinfo = EnvironmentInfo::extract(environment);
  2789. apply(parcel_id, envinfo);
  2790. }
  2791. }
  2792. LLEnvironment::EnvironmentInfo::EnvironmentInfo()
  2793. : mParcelId(INVALID_PARCEL_ID),
  2794. mDayLength(0),
  2795. mDayOffset(0),
  2796. mDayHash(0),
  2797. mAltitudes({ { 0.f, 0.f, 0.f, 0.f } }),
  2798. mIsDefault(false),
  2799. mIsLegacy(false),
  2800. mEnvVersion(INVALID_PARCEL_ENVIRONMENT_VERSION)
  2801. {
  2802. }
  2803. LLEnvironment::envinfo_ptr_t LLEnvironment::EnvironmentInfo::extract(LLSD env)
  2804. {
  2805. ptr_t pinfo = std::make_shared<EnvironmentInfo>();
  2806. pinfo->mIsDefault = !env.has(KEY_ISDEFAULT) ||
  2807. env[KEY_ISDEFAULT].asBoolean();
  2808. pinfo->mParcelId = env.has(KEY_PARCELID) ? env[KEY_PARCELID].asInteger()
  2809. : INVALID_PARCEL_ID;
  2810. pinfo->mRegionId = env.has(KEY_REGIONID) ? env[KEY_REGIONID].asUUID()
  2811. : LLUUID::null;
  2812. pinfo->mIsLegacy = false;
  2813. if (env.has(KEY_TRACKALTS))
  2814. {
  2815. for (S32 i = 0; i < 3; ++i)
  2816. {
  2817. pinfo->mAltitudes[i + 1] = env[KEY_TRACKALTS][i].asReal();
  2818. }
  2819. pinfo->mAltitudes[0] = 0.f;
  2820. }
  2821. if (env.has(KEY_DAYCYCLE))
  2822. {
  2823. pinfo->mDayCycle =
  2824. LLEnvSettingsDay::buildFromEnvironmentMessage(env[KEY_DAYCYCLE]);
  2825. pinfo->mDayLength =
  2826. env.has(KEY_DAYLENGTH) ? env[KEY_DAYLENGTH].asInteger() : -1;
  2827. pinfo->mDayOffset =
  2828. env.has(KEY_DAYOFFSET) ? env[KEY_DAYOFFSET].asInteger() : -1;
  2829. pinfo->mDayHash =
  2830. env.has(KEY_DAYHASH) ? env[KEY_DAYHASH].asInteger() : 0;
  2831. }
  2832. else
  2833. {
  2834. pinfo->mDayLength = gEnvironment.getEnvironmentDayLength(ENV_REGION);
  2835. pinfo->mDayOffset = gEnvironment.getEnvironmentDayOffset(ENV_REGION);
  2836. }
  2837. if (env.has(KEY_DAYASSET))
  2838. {
  2839. pinfo->mAssetId = env[KEY_DAYASSET].asUUID();
  2840. }
  2841. if (env.has(KEY_DAYNAMES))
  2842. {
  2843. LLSD daynames = env[KEY_DAYNAMES];
  2844. if (daynames.isArray())
  2845. {
  2846. pinfo->mDayCycleName.clear();
  2847. for (U32 i = 0, count = pinfo->mNameList.size(); i < count; ++i)
  2848. {
  2849. pinfo->mNameList[i] = daynames[i].asString();
  2850. }
  2851. }
  2852. else if (daynames.isString())
  2853. {
  2854. for (std::string& name : pinfo->mNameList)
  2855. {
  2856. name.clear();
  2857. }
  2858. pinfo->mDayCycleName = daynames.asString();
  2859. }
  2860. }
  2861. else if (pinfo->mDayCycle)
  2862. {
  2863. pinfo->mDayCycleName = pinfo->mDayCycle->getName();
  2864. }
  2865. else if (env.has(KEY_DAYNAME))
  2866. {
  2867. pinfo->mDayCycleName = env[KEY_DAYNAME].asString();
  2868. }
  2869. if (env.has(KEY_ENVVERSION))
  2870. {
  2871. LLSD version = env[KEY_ENVVERSION];
  2872. pinfo->mEnvVersion = version.asInteger();
  2873. }
  2874. else
  2875. {
  2876. // Can be used for region, but versions should be same
  2877. pinfo->mEnvVersion =
  2878. pinfo->mIsDefault ? UNSET_PARCEL_ENVIRONMENT_VERSION
  2879. : INVALID_PARCEL_ENVIRONMENT_VERSION;
  2880. }
  2881. return pinfo;
  2882. }
  2883. LLEnvironment::envinfo_ptr_t
  2884. LLEnvironment::EnvironmentInfo::extractLegacy(LLSD legacy)
  2885. {
  2886. if (!legacy.isArray() || !legacy[0].has("regionID"))
  2887. {
  2888. llwarns << "Invalid legacy settings for environment: " << legacy
  2889. << llendl;
  2890. return ptr_t();
  2891. }
  2892. ptr_t pinfo = std::make_shared<EnvironmentInfo>();
  2893. pinfo->mIsDefault = false;
  2894. pinfo->mParcelId = INVALID_PARCEL_ID;
  2895. pinfo->mRegionId = legacy[0]["regionID"].asUUID();
  2896. pinfo->mIsLegacy = true;
  2897. pinfo->mDayLength = LLSettingsDay::DEFAULT_DAYLENGTH;
  2898. pinfo->mDayOffset = LLSettingsDay::DEFAULT_DAYOFFSET;
  2899. pinfo->mDayCycle =
  2900. LLEnvSettingsDay::buildFromLegacyMessage(pinfo->mRegionId, legacy[1],
  2901. legacy[2], legacy[3]);
  2902. if (pinfo->mDayCycle)
  2903. {
  2904. pinfo->mDayHash = pinfo->mDayCycle->getHash();
  2905. }
  2906. pinfo->mAltitudes[0] = pinfo->mAltitudes[1] = 0.f;
  2907. pinfo->mAltitudes[2] = 10001.f;
  2908. pinfo->mAltitudes[3] = 10002.f;
  2909. pinfo->mAltitudes[4] = 10003.f;
  2910. return pinfo;
  2911. }
  2912. LLSettingsWater::ptr_t
  2913. LLEnvironment::createWaterFromLegacyPreset(const std::string& filename,
  2914. LLSD& messages)
  2915. {
  2916. std::string name = gDirUtil.getBaseFileName(filename, true);
  2917. std::string file = gDirUtil.getBaseFileName(filename);
  2918. std::string path = gDirUtil.getDirName(filename);
  2919. LLSettingsWater::ptr_t water =
  2920. LLEnvSettingsWater::buildFromLegacyPresetFile(name, file, path,
  2921. messages);
  2922. if (!water)
  2923. {
  2924. messages["NAME"] = name;
  2925. messages["FILE"] = filename;
  2926. }
  2927. return water;
  2928. }
  2929. LLSettingsSky::ptr_t
  2930. LLEnvironment::createSkyFromLegacyPreset(const std::string& filename,
  2931. LLSD& messages)
  2932. {
  2933. std::string name = gDirUtil.getBaseFileName(filename, true);
  2934. std::string file = gDirUtil.getBaseFileName(filename);
  2935. std::string path = gDirUtil.getDirName(filename);
  2936. LLSettingsSky::ptr_t sky =
  2937. LLEnvSettingsSky::buildFromLegacyPresetFile(name, file, path,
  2938. messages);
  2939. if (!sky)
  2940. {
  2941. messages["NAME"] = name;
  2942. messages["FILE"] = filename;
  2943. }
  2944. return sky;
  2945. }
  2946. LLSettingsDay::ptr_t
  2947. LLEnvironment::createDayCycleFromLegacyPreset(const std::string& filename,
  2948. LLSD& messages)
  2949. {
  2950. std::string name = gDirUtil.getBaseFileName(filename, true);
  2951. std::string file = gDirUtil.getBaseFileName(filename);
  2952. std::string path = gDirUtil.getDirName(filename);
  2953. LLSettingsDay::ptr_t day =
  2954. LLEnvSettingsDay::buildFromLegacyPresetFile(name, file, path,
  2955. messages);
  2956. if (!day)
  2957. {
  2958. messages["NAME"] = name;
  2959. messages["FILE"] = filename;
  2960. }
  2961. return day;
  2962. }
  2963. LLSettingsDay::ptr_t
  2964. LLEnvironment::createDayCycleFromEnvironment(EEnvSelection env,
  2965. LLSettingsBase::ptr_t settings)
  2966. {
  2967. std::string type(settings->getSettingsType());
  2968. if (type == "daycycle")
  2969. {
  2970. return std::static_pointer_cast<LLSettingsDay>(settings);
  2971. }
  2972. if (env != ENV_PARCEL && env != ENV_REGION)
  2973. {
  2974. llwarns << "May only create from parcel or region environment."
  2975. << llendl;
  2976. return LLSettingsDay::ptr_t();
  2977. }
  2978. LLSettingsDay::ptr_t day = getEnvironmentDay(env);
  2979. if (!day && env == ENV_PARCEL)
  2980. {
  2981. day = getEnvironmentDay(ENV_REGION);
  2982. }
  2983. if (!day)
  2984. {
  2985. llwarns << "Could not retrieve existing day settings." << llendl;
  2986. return LLSettingsDay::ptr_t();
  2987. }
  2988. day = day->buildClone();
  2989. if (type == "sky")
  2990. {
  2991. for (S32 i = 1; i < LLSettingsDay::TRACK_MAX; ++i)
  2992. {
  2993. day->clearCycleTrack(i);
  2994. }
  2995. day->setSettingsAtKeyframe(settings, 0.f, 1);
  2996. }
  2997. else if (type == "water")
  2998. {
  2999. day->clearCycleTrack(LLSettingsDay::TRACK_WATER);
  3000. day->setSettingsAtKeyframe(settings, 0.f, LLSettingsDay::TRACK_WATER);
  3001. }
  3002. return day;
  3003. }
  3004. void LLEnvironment::onAgentPositionHasChanged(const LLVector3& localpos)
  3005. {
  3006. S32 trackno = calculateSkyTrackForAltitude(localpos.mV[VZ]);
  3007. if (trackno == mCurrentTrack)
  3008. {
  3009. return;
  3010. }
  3011. mCurrentTrack = trackno;
  3012. if (!gAgent.regionCapabilitiesReceived())
  3013. {
  3014. // Environment not ready, environment will be updated later, do not
  3015. // cause 'blend' yet. But keep mCurrentTrack updated in case we would
  3016. // not get new altitudes for some reason.
  3017. return;
  3018. }
  3019. for (U32 i = ENV_LOCAL; i < (U32)ENV_DEFAULT; ++i)
  3020. {
  3021. if (mEnvironments[i])
  3022. {
  3023. mEnvironments[i]->setSkyTrack(mCurrentTrack);
  3024. }
  3025. }
  3026. }
  3027. S32 LLEnvironment::calculateSkyTrackForAltitude(F64 altitude)
  3028. {
  3029. altitude_list_t::iterator start = mTrackAltitudes.begin();
  3030. altitude_list_t::iterator end = mTrackAltitudes.end();
  3031. altitude_list_t::iterator it =
  3032. std::find_if_not(start, end,
  3033. [altitude](F32 test) { return altitude > test; });
  3034. if (it == start)
  3035. {
  3036. return 1;
  3037. }
  3038. if (it == end)
  3039. {
  3040. return 4;
  3041. }
  3042. return llmin((S32)std::distance(start, it), 4);
  3043. }
  3044. void LLEnvironment::handleEnvironmentPush(LLSD& message)
  3045. {
  3046. // Log the experience message
  3047. LLExperienceLog::getInstance()->handleExperienceMessage(message);
  3048. std::string action = message[KEY_ACTION].asString();
  3049. LLUUID experience_id = message[KEY_EXPERIENCEID].asUUID();
  3050. LLSD action_data = message[KEY_ACTIONDATA];
  3051. F32 transition_time = action_data[KEY_TRANSITIONTIME].asReal();
  3052. // *TODO: check here that the viewer thinks the experience is still valid.
  3053. if (action == ACTION_CLEARENVIRONMENT)
  3054. {
  3055. handleEnvironmentPushClear(experience_id, action_data,
  3056. transition_time);
  3057. }
  3058. else if (action == ACTION_PUSHFULLENVIRONMENT)
  3059. {
  3060. handleEnvironmentPushFull(experience_id, action_data, transition_time);
  3061. }
  3062. else if (action == ACTION_PUSHPARTIALENVIRONMENT)
  3063. {
  3064. handleEnvironmentPushPartial(experience_id, action_data,
  3065. transition_time);
  3066. }
  3067. else
  3068. {
  3069. llwarns << "Unknown environment push action: " << action << llendl;
  3070. }
  3071. }
  3072. void LLEnvironment::handleEnvironmentPushClear(const LLUUID& experience_id,
  3073. LLSD& message, F32 transition)
  3074. {
  3075. clearExperienceEnvironment(experience_id, transition);
  3076. }
  3077. void LLEnvironment::handleEnvironmentPushFull(const LLUUID& experience_id,
  3078. LLSD& message, F32 transition)
  3079. {
  3080. setExperienceEnvironment(experience_id, message[KEY_ASSETID].asUUID(),
  3081. transition);
  3082. }
  3083. void LLEnvironment::handleEnvironmentPushPartial(const LLUUID& experience_id,
  3084. LLSD& message, F32 transition)
  3085. {
  3086. if (message.has("settings"))
  3087. {
  3088. setExperienceEnvironment(experience_id, message["settings"],
  3089. transition);
  3090. }
  3091. }
  3092. void LLEnvironment::clearExperienceEnvironment(const LLUUID& experience_id,
  3093. F64 transition_time)
  3094. {
  3095. LLDayInjection::ptr_t injection =
  3096. std::dynamic_pointer_cast<LLDayInjection>(getEnvironmentInstance(ENV_PUSH));
  3097. if (injection)
  3098. {
  3099. injection->clearInjections(experience_id, transition_time);
  3100. }
  3101. }
  3102. void LLEnvironment::setSharedEnvironment()
  3103. {
  3104. clearEnvironment(ENV_LOCAL);
  3105. setSelectedEnvironment(ENV_LOCAL);
  3106. updateEnvironment();
  3107. }
  3108. void LLEnvironment::setExperienceEnvironment(const LLUUID& experience_id,
  3109. const LLUUID& asset_id,
  3110. F32 transition_time)
  3111. {
  3112. if (!gAgent.hasExtendedEnvironment())
  3113. {
  3114. return;
  3115. }
  3116. LLEnvSettingsBase::getSettingsAsset(asset_id,
  3117. [this, experience_id,
  3118. transition_time](LLUUID asset_id,
  3119. LLSettingsBase::ptr_t settings,
  3120. S32 status, LLExtStat)
  3121. {
  3122. onSetExperienceEnvAssetLoaded(experience_id,
  3123. settings,
  3124. transition_time,
  3125. status);
  3126. });
  3127. }
  3128. void LLEnvironment::onSetExperienceEnvAssetLoaded(const LLUUID& experience_id,
  3129. LLSettingsBase::ptr_t settings,
  3130. F32 transition_time,
  3131. S32 status)
  3132. {
  3133. if (!settings || status)
  3134. {
  3135. LLSD args;
  3136. args["DESC"] = LLTrans::getString("experience_id") + ": " +
  3137. experience_id.asString();
  3138. gNotifications.add("FailedToFindSettings", args);
  3139. return;
  3140. }
  3141. bool update = false;
  3142. LLDayInjection::ptr_t environment =
  3143. std::dynamic_pointer_cast<LLDayInjection>(getEnvironmentInstance(ENV_PUSH));
  3144. if (!environment)
  3145. {
  3146. environment =
  3147. std::dynamic_pointer_cast<LLDayInjection>(getEnvironmentInstance(ENV_PUSH,
  3148. true));
  3149. update = true;
  3150. }
  3151. std::string type = settings->getSettingsType();
  3152. if (type == "daycycle")
  3153. {
  3154. environment->setInjectedDay(std::static_pointer_cast<LLSettingsDay>(settings),
  3155. experience_id, transition_time);
  3156. }
  3157. else if (type == "sky")
  3158. {
  3159. environment->setInjectedSky(std::static_pointer_cast<LLSettingsSky>(settings),
  3160. experience_id, transition_time);
  3161. }
  3162. else if (type == "water")
  3163. {
  3164. environment->setInjectedWater(std::static_pointer_cast<LLSettingsWater>(settings),
  3165. experience_id, transition_time);
  3166. }
  3167. if (update)
  3168. {
  3169. updateEnvironment(TRANSITION_INSTANT, true);
  3170. }
  3171. }
  3172. void LLEnvironment::setExperienceEnvironment(const LLUUID& experience_id,
  3173. LLSD data, F32 transition_time)
  3174. {
  3175. LLSD sky(data["sky"]);
  3176. LLSD water(data["water"]);
  3177. if (sky.isUndefined() && water.isUndefined())
  3178. {
  3179. clearExperienceEnvironment(experience_id, F64(transition_time));
  3180. return;
  3181. }
  3182. bool update = false;
  3183. LLDayInjection::ptr_t environment =
  3184. std::dynamic_pointer_cast<LLDayInjection>(getEnvironmentInstance(ENV_PUSH));
  3185. if (!environment)
  3186. {
  3187. environment =
  3188. std::dynamic_pointer_cast<LLDayInjection>(getEnvironmentInstance(ENV_PUSH,
  3189. true));
  3190. update = true;
  3191. }
  3192. if (!sky.isUndefined())
  3193. {
  3194. environment->injectSkySettings(sky, experience_id, F64(transition_time));
  3195. }
  3196. if (!water.isUndefined())
  3197. {
  3198. environment->injectWaterSettings(water, experience_id,
  3199. F64(transition_time));
  3200. }
  3201. if (update)
  3202. {
  3203. updateEnvironment(TRANSITION_INSTANT, true);
  3204. }
  3205. }
  3206. bool LLEnvironment::listenExperiencePump(const LLSD& message)
  3207. {
  3208. if (!message.has("experience"))
  3209. {
  3210. return false;
  3211. }
  3212. LLUUID experience_id = message["experience"];
  3213. LLSD data = message[experience_id.asString()];
  3214. std::string permission(data["permission"].asString());
  3215. if (permission == "Forget" || permission == "Block")
  3216. {
  3217. clearExperienceEnvironment(experience_id,
  3218. permission == "Block" ? TRANSITION_INSTANT
  3219. : TRANSITION_FAST);
  3220. }
  3221. return false;
  3222. }
  3223. LLEnvironment::DayInstance::DayInstance(EEnvSelection env)
  3224. : mDayLength(LLSettingsDay::DEFAULT_DAYLENGTH),
  3225. mDayOffset(LLSettingsDay::DEFAULT_DAYOFFSET),
  3226. mInitialized(false),
  3227. mType(TYPE_INVALID),
  3228. mSkyTrack(1),
  3229. mEnv(env),
  3230. mAnimateFlags(0)
  3231. {
  3232. }
  3233. LLEnvironment::DayInstance::ptr_t LLEnvironment::DayInstance::clone() const
  3234. {
  3235. ptr_t environment = std::make_shared<DayInstance>(mEnv);
  3236. environment->mDayCycle = mDayCycle;
  3237. environment->mSky = mSky;
  3238. environment->mWater = mWater;
  3239. environment->mDayLength = mDayLength;
  3240. environment->mDayOffset = mDayOffset;
  3241. environment->mBlenderSky = mBlenderSky;
  3242. environment->mBlenderWater = mBlenderWater;
  3243. environment->mInitialized = mInitialized;
  3244. environment->mType = mType;
  3245. environment->mSkyTrack = mSkyTrack;
  3246. environment->mAnimateFlags = mAnimateFlags;
  3247. return environment;
  3248. }
  3249. bool LLEnvironment::DayInstance::applyTimeDelta(F64 delta)
  3250. {
  3251. // Makes sure that this does not go away while it is being worked on.
  3252. ptr_t keeper(shared_from_this());
  3253. if (!mInitialized)
  3254. {
  3255. initialize();
  3256. }
  3257. bool changed = false;
  3258. if (mBlenderSky)
  3259. {
  3260. changed = mBlenderSky->applyTimeDelta(delta);
  3261. }
  3262. if (mBlenderWater)
  3263. {
  3264. changed |= mBlenderWater->applyTimeDelta(delta);
  3265. }
  3266. return changed;
  3267. }
  3268. void LLEnvironment::DayInstance::setDay(const LLSettingsDay::ptr_t& dayp,
  3269. S32 daylength, S32 dayoffset)
  3270. {
  3271. mType = TYPE_CYCLED;
  3272. mInitialized = false;
  3273. mAnimateFlags = 0;
  3274. mDayCycle = dayp;
  3275. mDayLength = daylength;
  3276. mDayOffset = dayoffset;
  3277. mBlenderSky.reset();
  3278. mBlenderWater.reset();
  3279. mSky = LLEnvSettingsSky::buildDefaultSky();
  3280. mWater = LLEnvSettingsWater::buildDefaultWater();
  3281. animate();
  3282. }
  3283. bool LLEnvironment::DayInstance::setSky(const LLSettingsSky::ptr_t& skyp)
  3284. {
  3285. mType = TYPE_FIXED;
  3286. mInitialized = false;
  3287. bool changed = !skyp || !mSky || skyp->getHash() != mSky->getHash();
  3288. bool different_sky = mSky != skyp;
  3289. mSky = skyp;
  3290. mSky->mReplaced |= different_sky;
  3291. mSky->update();
  3292. mBlenderSky.reset();
  3293. return changed;
  3294. }
  3295. void LLEnvironment::DayInstance::setWater(const LLSettingsWater::ptr_t& waterp)
  3296. {
  3297. mType = TYPE_FIXED;
  3298. mInitialized = false;
  3299. bool different_water = mWater != waterp;
  3300. mWater = waterp;
  3301. mWater->mReplaced |= different_water;
  3302. mWater->update();
  3303. mBlenderWater.reset();
  3304. }
  3305. void LLEnvironment::DayInstance::initialize()
  3306. {
  3307. mInitialized = true;
  3308. if (!mWater)
  3309. {
  3310. mWater = LLEnvSettingsWater::buildDefaultWater();
  3311. }
  3312. if (!mSky)
  3313. {
  3314. mSky = LLEnvSettingsSky::buildDefaultSky();
  3315. }
  3316. }
  3317. void LLEnvironment::DayInstance::clear()
  3318. {
  3319. mType = TYPE_INVALID;
  3320. mDayCycle.reset();
  3321. mSky.reset();
  3322. mWater.reset();
  3323. mDayLength = LLSettingsDay::DEFAULT_DAYLENGTH;
  3324. mDayOffset = LLSettingsDay::DEFAULT_DAYOFFSET;
  3325. mBlenderSky.reset();
  3326. mBlenderWater.reset();
  3327. mSkyTrack = 1;
  3328. }
  3329. void LLEnvironment::DayInstance::setSkyTrack(S32 trackno)
  3330. {
  3331. mSkyTrack = trackno;
  3332. if (mBlenderSky)
  3333. {
  3334. mBlenderSky->switchTrack(trackno, 0.0);
  3335. }
  3336. }
  3337. void LLEnvironment::DayInstance::setBlenders(const LLSettingsBlender::ptr_t& skyblend,
  3338. const LLSettingsBlender::ptr_t& waterblend)
  3339. {
  3340. mBlenderSky = skyblend;
  3341. mBlenderWater = waterblend;
  3342. }
  3343. F32 LLEnvironment::DayInstance::getProgress() const
  3344. {
  3345. if (mDayLength <= 0 || !mDayCycle)
  3346. {
  3347. return -1.f; // No actual day cycle.
  3348. }
  3349. F64 now = LLTimer::getEpochSeconds() + (F64)mDayOffset;
  3350. return convert_time_to_position(now, mDayLength);
  3351. }
  3352. F32 LLEnvironment::DayInstance::secondsToKeyframe(S32 seconds)
  3353. {
  3354. return convert_time_to_position(seconds, mDayLength);
  3355. }
  3356. void LLEnvironment::DayInstance::animate()
  3357. {
  3358. if (!mDayCycle)
  3359. {
  3360. return;
  3361. }
  3362. if (!(mAnimateFlags & NO_ANIMATE_WATER))
  3363. {
  3364. LLSettingsDay::cycle_track_t& wtrack = mDayCycle->getCycleTrack(0);
  3365. if (wtrack.empty())
  3366. {
  3367. mWater.reset();
  3368. mBlenderWater.reset();
  3369. }
  3370. else
  3371. {
  3372. mWater = LLEnvSettingsWater::buildDefaultWater();
  3373. mBlenderWater =
  3374. std::make_shared<LLTrackBlenderLoopingTime>(mWater, mDayCycle,
  3375. 0, mDayLength,
  3376. mDayOffset,
  3377. DEFAULT_UPDATE_THRESHOLD);
  3378. }
  3379. }
  3380. if (!(mAnimateFlags & NO_ANIMATE_SKY))
  3381. {
  3382. // Initialize sky to track 1
  3383. LLSettingsDay::cycle_track_t &track = mDayCycle->getCycleTrack(1);
  3384. if (track.empty())
  3385. {
  3386. mSky.reset();
  3387. mBlenderSky.reset();
  3388. }
  3389. else
  3390. {
  3391. mSky = LLEnvSettingsSky::buildDefaultSky();
  3392. mBlenderSky =
  3393. std::make_shared<LLTrackBlenderLoopingTime>(mSky, mDayCycle,
  3394. 1, mDayLength,
  3395. mDayOffset,
  3396. DEFAULT_UPDATE_THRESHOLD);
  3397. mBlenderSky->switchTrack(mSkyTrack, 0.f);
  3398. }
  3399. }
  3400. }
  3401. LLEnvironment::DayTransition::DayTransition(const LLSettingsSky::ptr_t& skystart,
  3402. const LLSettingsWater::ptr_t& waterstart,
  3403. LLEnvironment::DayInstance::ptr_t& end,
  3404. S32 time)
  3405. : DayInstance(ENV_NONE),
  3406. mStartSky(skystart),
  3407. mStartWater(waterstart),
  3408. mNextInstance(end),
  3409. mTransitionTime(time)
  3410. {
  3411. }
  3412. bool LLEnvironment::DayTransition::applyTimeDelta(F64 delta)
  3413. {
  3414. bool changed = mNextInstance->applyTimeDelta(delta);
  3415. changed |= DayInstance::applyTimeDelta(delta);
  3416. return changed;
  3417. }
  3418. void LLEnvironment::DayTransition::animate()
  3419. {
  3420. if (!mNextInstance || !mStartWater || !mStartSky)
  3421. {
  3422. llwarns << "NULL pointer detected. Operation aborted" << llendl;
  3423. return;
  3424. }
  3425. mNextInstance->animate();
  3426. mWater = mStartWater->buildClone();
  3427. mBlenderWater =
  3428. std::make_shared<LLSettingsBlenderTimeDelta>(mWater, mStartWater,
  3429. mNextInstance->getWater(),
  3430. mTransitionTime);
  3431. mBlenderWater->setOnFinished([this](LLSettingsBlender::ptr_t blender)
  3432. {
  3433. mBlenderWater.reset();
  3434. if (!mBlenderSky && !mBlenderWater)
  3435. {
  3436. gEnvironment.mCurrentEnvironment =
  3437. mNextInstance;
  3438. }
  3439. else
  3440. {
  3441. setWater(mNextInstance->getWater());
  3442. }
  3443. });
  3444. gPipeline.mReflectionMapManager.pause(mTransitionTime);
  3445. gPipeline.mReflectionMapManager.reset();
  3446. mSky = mStartSky->buildClone();
  3447. mBlenderSky =
  3448. std::make_shared<LLSettingsBlenderTimeDelta>(mSky, mStartSky,
  3449. mNextInstance->getSky(),
  3450. mTransitionTime);
  3451. mBlenderSky->setOnFinished([this](LLSettingsBlender::ptr_t blender)
  3452. {
  3453. mBlenderSky.reset();
  3454. if (!mBlenderSky && !mBlenderWater)
  3455. {
  3456. gEnvironment.mCurrentEnvironment =
  3457. mNextInstance;
  3458. }
  3459. else
  3460. {
  3461. setSky(mNextInstance->getSky());
  3462. }
  3463. });
  3464. }
  3465. LLTrackBlenderLoopingManual::LLTrackBlenderLoopingManual(const LLSettingsBase::ptr_t& target,
  3466. const LLSettingsDay::ptr_t& day,
  3467. S32 trackno)
  3468. : LLSettingsBlender(target, LLSettingsBase::ptr_t(),
  3469. LLSettingsBase::ptr_t()),
  3470. mDay(day),
  3471. mTrackNo(trackno),
  3472. mPosition(0.0)
  3473. {
  3474. LLSettingsDay::track_bound_t initial = getBoundingEntries(mPosition);
  3475. if (initial.first != mEndMarker)
  3476. {
  3477. // No frames in track
  3478. mInitial = (initial.first)->second;
  3479. mFinal = (initial.second)->second;
  3480. LLSD init = mInitial->getSettings();
  3481. mTarget->replaceSettings(init);
  3482. }
  3483. }
  3484. F64 LLTrackBlenderLoopingManual::setPosition(F32 position)
  3485. {
  3486. mPosition = llclamp(position, 0.f, 1.f);
  3487. LLSettingsDay::track_bound_t bounds = getBoundingEntries(mPosition);
  3488. if (bounds.first == mEndMarker)
  3489. {
  3490. // No frames in track.
  3491. return 0.0;
  3492. }
  3493. mInitial = (bounds.first)->second;
  3494. mFinal = (bounds.second)->second;
  3495. F64 span_length = getSpanLength(bounds);
  3496. F64 span_pos = mPosition;
  3497. if ((bounds.first)->first)
  3498. {
  3499. mPosition += 1.0 - (bounds.first)->first;
  3500. }
  3501. if (span_pos > span_length)
  3502. {
  3503. // We are clamping position to 0-1 and span_length is 1, so do not
  3504. // account for case of span_pos == span_length
  3505. span_pos = fmod(span_pos, span_length);
  3506. }
  3507. return LLSettingsBlender::setBlendFactor(span_pos / span_length);
  3508. }
  3509. void LLTrackBlenderLoopingManual::switchTrack(S32 trackno, F32 position)
  3510. {
  3511. mTrackNo = trackno;
  3512. setPosition(position < 0.f ? mPosition : position);
  3513. }
  3514. LLSettingsDay::track_bound_t LLTrackBlenderLoopingManual::getBoundingEntries(F64 position)
  3515. {
  3516. LLSettingsDay::cycle_track_t& wtrack = mDay->getCycleTrack(mTrackNo);
  3517. mEndMarker = wtrack.end();
  3518. return get_bounding_entries(wtrack, position);
  3519. }
  3520. F64 LLTrackBlenderLoopingManual::getSpanLength(const LLSettingsDay::track_bound_t& bounds) const
  3521. {
  3522. return get_wrapping_distance((bounds.first)->first,
  3523. (bounds.second)->first);
  3524. }