Scene.js 139 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524
  1. import BoundingRectangle from "../Core/BoundingRectangle.js";
  2. import BoundingSphere from "../Core/BoundingSphere.js";
  3. import BoxGeometry from "../Core/BoxGeometry.js";
  4. import Cartesian3 from "../Core/Cartesian3.js";
  5. import Cartographic from "../Core/Cartographic.js";
  6. import clone from "../Core/clone.js";
  7. import Color from "../Core/Color.js";
  8. import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
  9. import createGuid from "../Core/createGuid.js";
  10. import CullingVolume from "../Core/CullingVolume.js";
  11. import defaultValue from "../Core/defaultValue.js";
  12. import defined from "../Core/defined.js";
  13. import deprecationWarning from "../Core/deprecationWarning.js";
  14. import destroyObject from "../Core/destroyObject.js";
  15. import DeveloperError from "../Core/DeveloperError.js";
  16. import EllipsoidGeometry from "../Core/EllipsoidGeometry.js";
  17. import Event from "../Core/Event.js";
  18. import FeatureDetection from "../Core/FeatureDetection.js";
  19. import GeographicProjection from "../Core/GeographicProjection.js";
  20. import GeometryInstance from "../Core/GeometryInstance.js";
  21. import GeometryPipeline from "../Core/GeometryPipeline.js";
  22. import Intersect from "../Core/Intersect.js";
  23. import JulianDate from "../Core/JulianDate.js";
  24. import CesiumMath from "../Core/Math.js";
  25. import Matrix4 from "../Core/Matrix4.js";
  26. import mergeSort from "../Core/mergeSort.js";
  27. import Occluder from "../Core/Occluder.js";
  28. import OrthographicFrustum from "../Core/OrthographicFrustum.js";
  29. import OrthographicOffCenterFrustum from "../Core/OrthographicOffCenterFrustum.js";
  30. import PerspectiveFrustum from "../Core/PerspectiveFrustum.js";
  31. import PerspectiveOffCenterFrustum from "../Core/PerspectiveOffCenterFrustum.js";
  32. import RequestScheduler from "../Core/RequestScheduler.js";
  33. import TaskProcessor from "../Core/TaskProcessor.js";
  34. import Transforms from "../Core/Transforms.js";
  35. import ClearCommand from "../Renderer/ClearCommand.js";
  36. import ComputeEngine from "../Renderer/ComputeEngine.js";
  37. import Context from "../Renderer/Context.js";
  38. import ContextLimits from "../Renderer/ContextLimits.js";
  39. import Pass from "../Renderer/Pass.js";
  40. import RenderState from "../Renderer/RenderState.js";
  41. import BrdfLutGenerator from "./BrdfLutGenerator.js";
  42. import Camera from "./Camera.js";
  43. import Cesium3DTilePass from "./Cesium3DTilePass.js";
  44. import Cesium3DTilePassState from "./Cesium3DTilePassState.js";
  45. import CreditDisplay from "./CreditDisplay.js";
  46. import DebugCameraPrimitive from "./DebugCameraPrimitive.js";
  47. import DepthPlane from "./DepthPlane.js";
  48. import DerivedCommand from "./DerivedCommand.js";
  49. import DeviceOrientationCameraController from "./DeviceOrientationCameraController.js";
  50. import Fog from "./Fog.js";
  51. import FrameState from "./FrameState.js";
  52. import GlobeTranslucencyState from "./GlobeTranslucencyState.js";
  53. import InvertClassification from "./InvertClassification.js";
  54. import JobScheduler from "./JobScheduler.js";
  55. import MapMode2D from "./MapMode2D.js";
  56. import OctahedralProjectedCubeMap from "./OctahedralProjectedCubeMap.js";
  57. import PerformanceDisplay from "./PerformanceDisplay.js";
  58. import PerInstanceColorAppearance from "./PerInstanceColorAppearance.js";
  59. import Picking from "./Picking.js";
  60. import PostProcessStageCollection from "./PostProcessStageCollection.js";
  61. import Primitive from "./Primitive.js";
  62. import PrimitiveCollection from "./PrimitiveCollection.js";
  63. import SceneMode from "./SceneMode.js";
  64. import SceneTransforms from "./SceneTransforms.js";
  65. import SceneTransitioner from "./SceneTransitioner.js";
  66. import ScreenSpaceCameraController from "./ScreenSpaceCameraController.js";
  67. import ShadowMap from "./ShadowMap.js";
  68. import StencilConstants from "./StencilConstants.js";
  69. import SunLight from "./SunLight.js";
  70. import SunPostProcess from "./SunPostProcess.js";
  71. import TweenCollection from "./TweenCollection.js";
  72. import View from "./View.js";
  73. import DebugInspector from "./DebugInspector.js";
  74. const requestRenderAfterFrame = function (scene) {
  75. return function () {
  76. scene.frameState.afterRender.push(function () {
  77. scene.requestRender();
  78. });
  79. };
  80. };
  81. /**
  82. * The container for all 3D graphical objects and state in a Cesium virtual scene. Generally,
  83. * a scene is not created directly; instead, it is implicitly created by {@link CesiumWidget}.
  84. * <p>
  85. * <em><code>contextOptions</code> parameter details:</em>
  86. * </p>
  87. * <p>
  88. * The default values are:
  89. * <code>
  90. * {
  91. * webgl : {
  92. * alpha : false,
  93. * depth : true,
  94. * stencil : false,
  95. * antialias : true,
  96. * powerPreference: 'high-performance',
  97. * premultipliedAlpha : true,
  98. * preserveDrawingBuffer : false,
  99. * failIfMajorPerformanceCaveat : false
  100. * },
  101. * allowTextureFilterAnisotropic : true
  102. * }
  103. * </code>
  104. * </p>
  105. * <p>
  106. * The <code>webgl</code> property corresponds to the {@link http://www.khronos.org/registry/webgl/specs/latest/#5.2|WebGLContextAttributes}
  107. * object used to create the WebGL context.
  108. * </p>
  109. * <p>
  110. * <code>webgl.alpha</code> defaults to false, which can improve performance compared to the standard WebGL default
  111. * of true. If an application needs to composite Cesium above other HTML elements using alpha-blending, set
  112. * <code>webgl.alpha</code> to true.
  113. * </p>
  114. * <p>
  115. * The other <code>webgl</code> properties match the WebGL defaults for {@link http://www.khronos.org/registry/webgl/specs/latest/#5.2|WebGLContextAttributes}.
  116. * </p>
  117. * <p>
  118. * <code>allowTextureFilterAnisotropic</code> defaults to true, which enables anisotropic texture filtering when the
  119. * WebGL extension is supported. Setting this to false will improve performance, but hurt visual quality, especially for horizon views.
  120. * </p>
  121. *
  122. * @alias Scene
  123. * @constructor
  124. *
  125. * @param {Object} options Object with the following properties:
  126. * @param {HTMLCanvasElement} options.canvas The HTML canvas element to create the scene for.
  127. * @param {Object} [options.contextOptions] Context and WebGL creation properties. See details above.
  128. * @param {Element} [options.creditContainer] The HTML element in which the credits will be displayed.
  129. * @param {Element} [options.creditViewport] The HTML element in which to display the credit popup. If not specified, the viewport will be a added as a sibling of the canvas.
  130. * @param {MapProjection} [options.mapProjection=new GeographicProjection()] The map projection to use in 2D and Columbus View modes.
  131. * @param {Boolean} [options.orderIndependentTranslucency=true] If true and the configuration supports it, use order independent translucency.
  132. * @param {Boolean} [options.scene3DOnly=false] If true, optimizes memory use and performance for 3D mode but disables the ability to use 2D or Columbus View.
  133. * @param {Boolean} [options.shadows=false] Determines if shadows are cast by light sources.
  134. * @param {MapMode2D} [options.mapMode2D=MapMode2D.INFINITE_SCROLL] Determines if the 2D map is rotatable or can be scrolled infinitely in the horizontal direction.
  135. * @param {Boolean} [options.requestRenderMode=false] If true, rendering a frame will only occur when needed as determined by changes within the scene. Enabling improves performance of the application, but requires using {@link Scene#requestRender} to render a new frame explicitly in this mode. This will be necessary in many cases after making changes to the scene in other parts of the API. See {@link https://cesium.com/blog/2018/01/24/cesium-scene-rendering-performance/|Improving Performance with Explicit Rendering}.
  136. * @param {Number} [options.maximumRenderTimeChange=0.0] If requestRenderMode is true, this value defines the maximum change in simulation time allowed before a render is requested. See {@link https://cesium.com/blog/2018/01/24/cesium-scene-rendering-performance/|Improving Performance with Explicit Rendering}.
  137. * @param {Number} [depthPlaneEllipsoidOffset=0.0] Adjust the DepthPlane to address rendering artefacts below ellipsoid zero elevation.
  138. * @param {Number} [options.msaaSamples=1] If provided, this value controls the rate of multisample antialiasing. Typical multisampling rates are 2, 4, and sometimes 8 samples per pixel. Higher sampling rates of MSAA may impact performance in exchange for improved visual quality. This value only applies to WebGL2 contexts that support multisample render targets.
  139. *
  140. * @see CesiumWidget
  141. * @see {@link http://www.khronos.org/registry/webgl/specs/latest/#5.2|WebGLContextAttributes}
  142. *
  143. * @exception {DeveloperError} options and options.canvas are required.
  144. *
  145. * @example
  146. * // Create scene without anisotropic texture filtering
  147. * const scene = new Cesium.Scene({
  148. * canvas : canvas,
  149. * contextOptions : {
  150. * allowTextureFilterAnisotropic : false
  151. * }
  152. * });
  153. */
  154. function Scene(options) {
  155. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  156. const canvas = options.canvas;
  157. let creditContainer = options.creditContainer;
  158. let creditViewport = options.creditViewport;
  159. let contextOptions = clone(options.contextOptions);
  160. if (!defined(contextOptions)) {
  161. contextOptions = {};
  162. }
  163. if (!defined(contextOptions.webgl)) {
  164. contextOptions.webgl = {};
  165. }
  166. contextOptions.webgl.powerPreference = defaultValue(
  167. contextOptions.webgl.powerPreference,
  168. "high-performance"
  169. );
  170. //>>includeStart('debug', pragmas.debug);
  171. if (!defined(canvas)) {
  172. throw new DeveloperError("options and options.canvas are required.");
  173. }
  174. //>>includeEnd('debug');
  175. const hasCreditContainer = defined(creditContainer);
  176. const context = new Context(canvas, contextOptions);
  177. if (!hasCreditContainer) {
  178. creditContainer = document.createElement("div");
  179. creditContainer.style.position = "absolute";
  180. creditContainer.style.bottom = "0";
  181. creditContainer.style["text-shadow"] = "0 0 2px #000000";
  182. creditContainer.style.color = "#ffffff";
  183. creditContainer.style["font-size"] = "10px";
  184. creditContainer.style["padding-right"] = "5px";
  185. canvas.parentNode.appendChild(creditContainer);
  186. }
  187. if (!defined(creditViewport)) {
  188. creditViewport = canvas.parentNode;
  189. }
  190. this._id = createGuid();
  191. this._jobScheduler = new JobScheduler();
  192. this._frameState = new FrameState(
  193. context,
  194. new CreditDisplay(creditContainer, " • ", creditViewport),
  195. this._jobScheduler
  196. );
  197. this._frameState.scene3DOnly = defaultValue(options.scene3DOnly, false);
  198. this._removeCreditContainer = !hasCreditContainer;
  199. this._creditContainer = creditContainer;
  200. this._canvas = canvas;
  201. this._context = context;
  202. this._computeEngine = new ComputeEngine(context);
  203. this._globe = undefined;
  204. this._globeTranslucencyState = new GlobeTranslucencyState();
  205. this._primitives = new PrimitiveCollection();
  206. this._groundPrimitives = new PrimitiveCollection();
  207. this._globeHeight = undefined;
  208. this._cameraUnderground = false;
  209. this._logDepthBuffer = context.fragmentDepth;
  210. this._logDepthBufferDirty = true;
  211. this._tweens = new TweenCollection();
  212. this._shaderFrameCount = 0;
  213. this._sunPostProcess = undefined;
  214. this._computeCommandList = [];
  215. this._overlayCommandList = [];
  216. // OIT is temporally disabled by default on iPad and iOS mobile due to https://github.com/CesiumGS/cesium/issues/9827
  217. const defaultOIT = !FeatureDetection.isIPadOrIOS();
  218. this._useOIT = defaultValue(options.orderIndependentTranslucency, defaultOIT);
  219. this._executeOITFunction = undefined;
  220. this._depthPlane = new DepthPlane(options.depthPlaneEllipsoidOffset);
  221. this._clearColorCommand = new ClearCommand({
  222. color: new Color(),
  223. stencil: 0,
  224. owner: this,
  225. });
  226. this._depthClearCommand = new ClearCommand({
  227. depth: 1.0,
  228. owner: this,
  229. });
  230. this._stencilClearCommand = new ClearCommand({
  231. stencil: 0,
  232. });
  233. this._classificationStencilClearCommand = new ClearCommand({
  234. stencil: 0,
  235. renderState: RenderState.fromCache({
  236. stencilMask: StencilConstants.CLASSIFICATION_MASK,
  237. }),
  238. });
  239. this._depthOnlyRenderStateCache = {};
  240. this._transitioner = new SceneTransitioner(this);
  241. this._preUpdate = new Event();
  242. this._postUpdate = new Event();
  243. this._renderError = new Event();
  244. this._preRender = new Event();
  245. this._postRender = new Event();
  246. this._minimumDisableDepthTestDistance = 0.0;
  247. this._debugInspector = new DebugInspector();
  248. this._msaaSamples = defaultValue(options.msaaSamples, 1);
  249. /**
  250. * Exceptions occurring in <code>render</code> are always caught in order to raise the
  251. * <code>renderError</code> event. If this property is true, the error is rethrown
  252. * after the event is raised. If this property is false, the <code>render</code> function
  253. * returns normally after raising the event.
  254. *
  255. * @type {Boolean}
  256. * @default false
  257. */
  258. this.rethrowRenderErrors = false;
  259. /**
  260. * Determines whether or not to instantly complete the
  261. * scene transition animation on user input.
  262. *
  263. * @type {Boolean}
  264. * @default true
  265. */
  266. this.completeMorphOnUserInput = true;
  267. /**
  268. * The event fired at the beginning of a scene transition.
  269. * @type {Event}
  270. * @default Event()
  271. */
  272. this.morphStart = new Event();
  273. /**
  274. * The event fired at the completion of a scene transition.
  275. * @type {Event}
  276. * @default Event()
  277. */
  278. this.morphComplete = new Event();
  279. /**
  280. * The {@link SkyBox} used to draw the stars.
  281. *
  282. * @type {SkyBox}
  283. * @default undefined
  284. *
  285. * @see Scene#backgroundColor
  286. */
  287. this.skyBox = undefined;
  288. /**
  289. * The sky atmosphere drawn around the globe.
  290. *
  291. * @type {SkyAtmosphere}
  292. * @default undefined
  293. */
  294. this.skyAtmosphere = undefined;
  295. /**
  296. * The {@link Sun}.
  297. *
  298. * @type {Sun}
  299. * @default undefined
  300. */
  301. this.sun = undefined;
  302. /**
  303. * Uses a bloom filter on the sun when enabled.
  304. *
  305. * @type {Boolean}
  306. * @default true
  307. */
  308. this.sunBloom = true;
  309. this._sunBloom = undefined;
  310. /**
  311. * The {@link Moon}
  312. *
  313. * @type Moon
  314. * @default undefined
  315. */
  316. this.moon = undefined;
  317. /**
  318. * The background color, which is only visible if there is no sky box, i.e., {@link Scene#skyBox} is undefined.
  319. *
  320. * @type {Color}
  321. * @default {@link Color.BLACK}
  322. *
  323. * @see Scene#skyBox
  324. */
  325. this.backgroundColor = Color.clone(Color.BLACK);
  326. this._mode = SceneMode.SCENE3D;
  327. this._mapProjection = defined(options.mapProjection)
  328. ? options.mapProjection
  329. : new GeographicProjection();
  330. /**
  331. * The current morph transition time between 2D/Columbus View and 3D,
  332. * with 0.0 being 2D or Columbus View and 1.0 being 3D.
  333. *
  334. * @type {Number}
  335. * @default 1.0
  336. */
  337. this.morphTime = 1.0;
  338. /**
  339. * The far-to-near ratio of the multi-frustum when using a normal depth buffer.
  340. * <p>
  341. * This value is used to create the near and far values for each frustum of the multi-frustum. It is only used
  342. * when {@link Scene#logarithmicDepthBuffer} is <code>false</code>. When <code>logarithmicDepthBuffer</code> is
  343. * <code>true</code>, use {@link Scene#logarithmicDepthFarToNearRatio}.
  344. * </p>
  345. *
  346. * @type {Number}
  347. * @default 1000.0
  348. */
  349. this.farToNearRatio = 1000.0;
  350. /**
  351. * The far-to-near ratio of the multi-frustum when using a logarithmic depth buffer.
  352. * <p>
  353. * This value is used to create the near and far values for each frustum of the multi-frustum. It is only used
  354. * when {@link Scene#logarithmicDepthBuffer} is <code>true</code>. When <code>logarithmicDepthBuffer</code> is
  355. * <code>false</code>, use {@link Scene#farToNearRatio}.
  356. * </p>
  357. *
  358. * @type {Number}
  359. * @default 1e9
  360. */
  361. this.logarithmicDepthFarToNearRatio = 1e9;
  362. /**
  363. * Determines the uniform depth size in meters of each frustum of the multifrustum in 2D. If a primitive or model close
  364. * to the surface shows z-fighting, decreasing this will eliminate the artifact, but decrease performance. On the
  365. * other hand, increasing this will increase performance but may cause z-fighting among primitives close to the surface.
  366. *
  367. * @type {Number}
  368. * @default 1.75e6
  369. */
  370. this.nearToFarDistance2D = 1.75e6;
  371. /**
  372. * This property is for debugging only; it is not for production use.
  373. * <p>
  374. * A function that determines what commands are executed. As shown in the examples below,
  375. * the function receives the command's <code>owner</code> as an argument, and returns a boolean indicating if the
  376. * command should be executed.
  377. * </p>
  378. * <p>
  379. * The default is <code>undefined</code>, indicating that all commands are executed.
  380. * </p>
  381. *
  382. * @type Function
  383. *
  384. * @default undefined
  385. *
  386. * @example
  387. * // Do not execute any commands.
  388. * scene.debugCommandFilter = function(command) {
  389. * return false;
  390. * };
  391. *
  392. * // Execute only the billboard's commands. That is, only draw the billboard.
  393. * const billboards = new Cesium.BillboardCollection();
  394. * scene.debugCommandFilter = function(command) {
  395. * return command.owner === billboards;
  396. * };
  397. */
  398. this.debugCommandFilter = undefined;
  399. /**
  400. * This property is for debugging only; it is not for production use.
  401. * <p>
  402. * When <code>true</code>, commands are randomly shaded. This is useful
  403. * for performance analysis to see what parts of a scene or model are
  404. * command-dense and could benefit from batching.
  405. * </p>
  406. *
  407. * @type Boolean
  408. *
  409. * @default false
  410. */
  411. this.debugShowCommands = false;
  412. /**
  413. * This property is for debugging only; it is not for production use.
  414. * <p>
  415. * When <code>true</code>, commands are shaded based on the frustums they
  416. * overlap. Commands in the closest frustum are tinted red, commands in
  417. * the next closest are green, and commands in the farthest frustum are
  418. * blue. If a command overlaps more than one frustum, the color components
  419. * are combined, e.g., a command overlapping the first two frustums is tinted
  420. * yellow.
  421. * </p>
  422. *
  423. * @type Boolean
  424. *
  425. * @default false
  426. */
  427. this.debugShowFrustums = false;
  428. /**
  429. * This property is for debugging only; it is not for production use.
  430. * <p>
  431. * Displays frames per second and time between frames.
  432. * </p>
  433. *
  434. * @type Boolean
  435. *
  436. * @default false
  437. */
  438. this.debugShowFramesPerSecond = false;
  439. /**
  440. * This property is for debugging only; it is not for production use.
  441. * <p>
  442. * Indicates which frustum will have depth information displayed.
  443. * </p>
  444. *
  445. * @type Number
  446. *
  447. * @default 1
  448. */
  449. this.debugShowDepthFrustum = 1;
  450. /**
  451. * This property is for debugging only; it is not for production use.
  452. * <p>
  453. * When <code>true</code>, draws outlines to show the boundaries of the camera frustums
  454. * </p>
  455. *
  456. * @type Boolean
  457. *
  458. * @default false
  459. */
  460. this.debugShowFrustumPlanes = false;
  461. this._debugShowFrustumPlanes = false;
  462. this._debugFrustumPlanes = undefined;
  463. /**
  464. * When <code>true</code>, enables picking using the depth buffer.
  465. *
  466. * @type Boolean
  467. * @default true
  468. */
  469. this.useDepthPicking = true;
  470. /**
  471. * When <code>true</code>, enables picking translucent geometry using the depth buffer. Note that {@link Scene#useDepthPicking} must also be true for enabling this to work.
  472. *
  473. * <p>
  474. * There is a decrease in performance when enabled. There are extra draw calls to write depth for
  475. * translucent geometry.
  476. * </p>
  477. *
  478. * @example
  479. * // picking the position of a translucent primitive
  480. * viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {
  481. * const pickedFeature = viewer.scene.pick(movement.position);
  482. * if (!Cesium.defined(pickedFeature)) {
  483. * // nothing picked
  484. * return;
  485. * }
  486. * const worldPosition = viewer.scene.pickPosition(movement.position);
  487. * }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  488. *
  489. * @type {Boolean}
  490. * @default false
  491. */
  492. this.pickTranslucentDepth = false;
  493. /**
  494. * The time in milliseconds to wait before checking if the camera has not moved and fire the cameraMoveEnd event.
  495. * @type {Number}
  496. * @default 500.0
  497. * @private
  498. */
  499. this.cameraEventWaitTime = 500.0;
  500. /**
  501. * Blends the atmosphere to geometry far from the camera for horizon views. Allows for additional
  502. * performance improvements by rendering less geometry and dispatching less terrain requests.
  503. * @type {Fog}
  504. */
  505. this.fog = new Fog();
  506. this._shadowMapCamera = new Camera(this);
  507. /**
  508. * The shadow map for the scene's light source. When enabled, models, primitives, and the globe may cast and receive shadows.
  509. * @type {ShadowMap}
  510. */
  511. this.shadowMap = new ShadowMap({
  512. context: context,
  513. lightCamera: this._shadowMapCamera,
  514. enabled: defaultValue(options.shadows, false),
  515. });
  516. /**
  517. * When <code>false</code>, 3D Tiles will render normally. When <code>true</code>, classified 3D Tile geometry will render normally and
  518. * unclassified 3D Tile geometry will render with the color multiplied by {@link Scene#invertClassificationColor}.
  519. * @type {Boolean}
  520. * @default false
  521. */
  522. this.invertClassification = false;
  523. /**
  524. * The highlight color of unclassified 3D Tile geometry when {@link Scene#invertClassification} is <code>true</code>.
  525. * <p>When the color's alpha is less than 1.0, the unclassified portions of the 3D Tiles will not blend correctly with the classified positions of the 3D Tiles.</p>
  526. * <p>Also, when the color's alpha is less than 1.0, the WEBGL_depth_texture and EXT_frag_depth WebGL extensions must be supported.</p>
  527. * @type {Color}
  528. * @default Color.WHITE
  529. */
  530. this.invertClassificationColor = Color.clone(Color.WHITE);
  531. this._actualInvertClassificationColor = Color.clone(
  532. this._invertClassificationColor
  533. );
  534. this._invertClassification = new InvertClassification();
  535. /**
  536. * The focal length for use when with cardboard or WebVR.
  537. * @type {Number}
  538. */
  539. this.focalLength = undefined;
  540. /**
  541. * The eye separation distance in meters for use with cardboard or WebVR.
  542. * @type {Number}
  543. */
  544. this.eyeSeparation = undefined;
  545. /**
  546. * Post processing effects applied to the final render.
  547. * @type {PostProcessStageCollection}
  548. */
  549. this.postProcessStages = new PostProcessStageCollection();
  550. this._brdfLutGenerator = new BrdfLutGenerator();
  551. this._performanceDisplay = undefined;
  552. this._debugVolume = undefined;
  553. this._screenSpaceCameraController = new ScreenSpaceCameraController(this);
  554. this._cameraUnderground = false;
  555. this._mapMode2D = defaultValue(options.mapMode2D, MapMode2D.INFINITE_SCROLL);
  556. // Keeps track of the state of a frame. FrameState is the state across
  557. // the primitives of the scene. This state is for internally keeping track
  558. // of celestial and environment effects that need to be updated/rendered in
  559. // a certain order as well as updating/tracking framebuffer usage.
  560. this._environmentState = {
  561. skyBoxCommand: undefined,
  562. skyAtmosphereCommand: undefined,
  563. sunDrawCommand: undefined,
  564. sunComputeCommand: undefined,
  565. moonCommand: undefined,
  566. isSunVisible: false,
  567. isMoonVisible: false,
  568. isReadyForAtmosphere: false,
  569. isSkyAtmosphereVisible: false,
  570. clearGlobeDepth: false,
  571. useDepthPlane: false,
  572. renderTranslucentDepthForPick: false,
  573. originalFramebuffer: undefined,
  574. useGlobeDepthFramebuffer: false,
  575. useOIT: false,
  576. useInvertClassification: false,
  577. usePostProcess: false,
  578. usePostProcessSelected: false,
  579. useWebVR: false,
  580. };
  581. this._useWebVR = false;
  582. this._cameraVR = undefined;
  583. this._aspectRatioVR = undefined;
  584. /**
  585. * When <code>true</code>, rendering a frame will only occur when needed as determined by changes within the scene.
  586. * Enabling improves performance of the application, but requires using {@link Scene#requestRender}
  587. * to render a new frame explicitly in this mode. This will be necessary in many cases after making changes
  588. * to the scene in other parts of the API.
  589. *
  590. * @see {@link https://cesium.com/blog/2018/01/24/cesium-scene-rendering-performance/|Improving Performance with Explicit Rendering}
  591. * @see Scene#maximumRenderTimeChange
  592. * @see Scene#requestRender
  593. *
  594. * @type {Boolean}
  595. * @default false
  596. */
  597. this.requestRenderMode = defaultValue(options.requestRenderMode, false);
  598. this._renderRequested = true;
  599. /**
  600. * If {@link Scene#requestRenderMode} is <code>true</code>, this value defines the maximum change in
  601. * simulation time allowed before a render is requested. Lower values increase the number of frames rendered
  602. * and higher values decrease the number of frames rendered. If <code>undefined</code>, changes to
  603. * the simulation time will never request a render.
  604. * This value impacts the rate of rendering for changes in the scene like lighting, entity property updates,
  605. * and animations.
  606. *
  607. * @see {@link https://cesium.com/blog/2018/01/24/cesium-scene-rendering-performance/|Improving Performance with Explicit Rendering}
  608. * @see Scene#requestRenderMode
  609. *
  610. * @type {Number}
  611. * @default 0.0
  612. */
  613. this.maximumRenderTimeChange = defaultValue(
  614. options.maximumRenderTimeChange,
  615. 0.0
  616. );
  617. this._lastRenderTime = undefined;
  618. this._frameRateMonitor = undefined;
  619. this._removeRequestListenerCallback = RequestScheduler.requestCompletedEvent.addEventListener(
  620. requestRenderAfterFrame(this)
  621. );
  622. this._removeTaskProcessorListenerCallback = TaskProcessor.taskCompletedEvent.addEventListener(
  623. requestRenderAfterFrame(this)
  624. );
  625. this._removeGlobeCallbacks = [];
  626. const viewport = new BoundingRectangle(
  627. 0,
  628. 0,
  629. context.drawingBufferWidth,
  630. context.drawingBufferHeight
  631. );
  632. const camera = new Camera(this);
  633. if (this._logDepthBuffer) {
  634. camera.frustum.near = 0.1;
  635. camera.frustum.far = 10000000000.0;
  636. }
  637. /**
  638. * The camera view for the scene camera flight destination. Used for preloading flight destination tiles.
  639. * @type {Camera}
  640. * @private
  641. */
  642. this.preloadFlightCamera = new Camera(this);
  643. /**
  644. * The culling volume for the scene camera flight destination. Used for preloading flight destination tiles.
  645. * @type {CullingVolume}
  646. * @private
  647. */
  648. this.preloadFlightCullingVolume = undefined;
  649. this._picking = new Picking(this);
  650. this._defaultView = new View(this, camera, viewport);
  651. this._view = this._defaultView;
  652. this._hdr = undefined;
  653. this._hdrDirty = undefined;
  654. this.highDynamicRange = false;
  655. this.gamma = 2.2;
  656. /**
  657. * The spherical harmonic coefficients for image-based lighting of PBR models.
  658. * @type {Cartesian3[]}
  659. */
  660. this.sphericalHarmonicCoefficients = undefined;
  661. /**
  662. * The url to the KTX2 file containing the specular environment map and convoluted mipmaps for image-based lighting of PBR models.
  663. * @type {String}
  664. */
  665. this.specularEnvironmentMaps = undefined;
  666. this._specularEnvironmentMapAtlas = undefined;
  667. /**
  668. * The light source for shading. Defaults to a directional light from the Sun.
  669. * @type {Light}
  670. */
  671. this.light = new SunLight();
  672. // Give frameState, camera, and screen space camera controller initial state before rendering
  673. updateFrameNumber(this, 0.0, JulianDate.now());
  674. this.updateFrameState();
  675. this.initializeFrame();
  676. }
  677. function updateGlobeListeners(scene, globe) {
  678. for (let i = 0; i < scene._removeGlobeCallbacks.length; ++i) {
  679. scene._removeGlobeCallbacks[i]();
  680. }
  681. scene._removeGlobeCallbacks.length = 0;
  682. const removeGlobeCallbacks = [];
  683. if (defined(globe)) {
  684. removeGlobeCallbacks.push(
  685. globe.imageryLayersUpdatedEvent.addEventListener(
  686. requestRenderAfterFrame(scene)
  687. )
  688. );
  689. removeGlobeCallbacks.push(
  690. globe.terrainProviderChanged.addEventListener(
  691. requestRenderAfterFrame(scene)
  692. )
  693. );
  694. }
  695. scene._removeGlobeCallbacks = removeGlobeCallbacks;
  696. }
  697. Object.defineProperties(Scene.prototype, {
  698. /**
  699. * Gets the canvas element to which this scene is bound.
  700. * @memberof Scene.prototype
  701. *
  702. * @type {HTMLCanvasElement}
  703. * @readonly
  704. */
  705. canvas: {
  706. get: function () {
  707. return this._canvas;
  708. },
  709. },
  710. /**
  711. * The drawingBufferHeight of the underlying GL context.
  712. * @memberof Scene.prototype
  713. *
  714. * @type {Number}
  715. * @readonly
  716. *
  717. * @see {@link https://www.khronos.org/registry/webgl/specs/1.0/#DOM-WebGLRenderingContext-drawingBufferHeight|drawingBufferHeight}
  718. */
  719. drawingBufferHeight: {
  720. get: function () {
  721. return this._context.drawingBufferHeight;
  722. },
  723. },
  724. /**
  725. * The drawingBufferHeight of the underlying GL context.
  726. * @memberof Scene.prototype
  727. *
  728. * @type {Number}
  729. * @readonly
  730. *
  731. * @see {@link https://www.khronos.org/registry/webgl/specs/1.0/#DOM-WebGLRenderingContext-drawingBufferHeight|drawingBufferHeight}
  732. */
  733. drawingBufferWidth: {
  734. get: function () {
  735. return this._context.drawingBufferWidth;
  736. },
  737. },
  738. /**
  739. * The maximum aliased line width, in pixels, supported by this WebGL implementation. It will be at least one.
  740. * @memberof Scene.prototype
  741. *
  742. * @type {Number}
  743. * @readonly
  744. *
  745. * @see {@link https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGet.xml|glGet} with <code>ALIASED_LINE_WIDTH_RANGE</code>.
  746. */
  747. maximumAliasedLineWidth: {
  748. get: function () {
  749. return ContextLimits.maximumAliasedLineWidth;
  750. },
  751. },
  752. /**
  753. * The maximum length in pixels of one edge of a cube map, supported by this WebGL implementation. It will be at least 16.
  754. * @memberof Scene.prototype
  755. *
  756. * @type {Number}
  757. * @readonly
  758. *
  759. * @see {@link https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGet.xml|glGet} with <code>GL_MAX_CUBE_MAP_TEXTURE_SIZE</code>.
  760. */
  761. maximumCubeMapSize: {
  762. get: function () {
  763. return ContextLimits.maximumCubeMapSize;
  764. },
  765. },
  766. /**
  767. * Returns <code>true</code> if the {@link Scene#pickPosition} function is supported.
  768. * @memberof Scene.prototype
  769. *
  770. * @type {Boolean}
  771. * @readonly
  772. *
  773. * @see Scene#pickPosition
  774. */
  775. pickPositionSupported: {
  776. get: function () {
  777. return this._context.depthTexture;
  778. },
  779. },
  780. /**
  781. * Returns <code>true</code> if the {@link Scene#sampleHeight} and {@link Scene#sampleHeightMostDetailed} functions are supported.
  782. * @memberof Scene.prototype
  783. *
  784. * @type {Boolean}
  785. * @readonly
  786. *
  787. * @see Scene#sampleHeight
  788. * @see Scene#sampleHeightMostDetailed
  789. */
  790. sampleHeightSupported: {
  791. get: function () {
  792. return this._context.depthTexture;
  793. },
  794. },
  795. /**
  796. * Returns <code>true</code> if the {@link Scene#clampToHeight} and {@link Scene#clampToHeightMostDetailed} functions are supported.
  797. * @memberof Scene.prototype
  798. *
  799. * @type {Boolean}
  800. * @readonly
  801. *
  802. * @see Scene#clampToHeight
  803. * @see Scene#clampToHeightMostDetailed
  804. */
  805. clampToHeightSupported: {
  806. get: function () {
  807. return this._context.depthTexture;
  808. },
  809. },
  810. /**
  811. * Returns <code>true</code> if the {@link Scene#invertClassification} is supported.
  812. * @memberof Scene.prototype
  813. *
  814. * @type {Boolean}
  815. * @readonly
  816. *
  817. * @see Scene#invertClassification
  818. */
  819. invertClassificationSupported: {
  820. get: function () {
  821. return this._context.depthTexture;
  822. },
  823. },
  824. /**
  825. * Returns <code>true</code> if specular environment maps are supported.
  826. * @memberof Scene.prototype
  827. *
  828. * @type {Boolean}
  829. * @readonly
  830. *
  831. * @see Scene#specularEnvironmentMaps
  832. */
  833. specularEnvironmentMapsSupported: {
  834. get: function () {
  835. return OctahedralProjectedCubeMap.isSupported(this._context);
  836. },
  837. },
  838. /**
  839. * Gets or sets the depth-test ellipsoid.
  840. * @memberof Scene.prototype
  841. *
  842. * @type {Globe}
  843. */
  844. globe: {
  845. get: function () {
  846. return this._globe;
  847. },
  848. set: function (globe) {
  849. this._globe = this._globe && this._globe.destroy();
  850. this._globe = globe;
  851. updateGlobeListeners(this, globe);
  852. },
  853. },
  854. /**
  855. * Gets the collection of primitives.
  856. * @memberof Scene.prototype
  857. *
  858. * @type {PrimitiveCollection}
  859. * @readonly
  860. */
  861. primitives: {
  862. get: function () {
  863. return this._primitives;
  864. },
  865. },
  866. /**
  867. * Gets the collection of ground primitives.
  868. * @memberof Scene.prototype
  869. *
  870. * @type {PrimitiveCollection}
  871. * @readonly
  872. */
  873. groundPrimitives: {
  874. get: function () {
  875. return this._groundPrimitives;
  876. },
  877. },
  878. /**
  879. * Gets or sets the camera.
  880. * @memberof Scene.prototype
  881. *
  882. * @type {Camera}
  883. * @readonly
  884. */
  885. camera: {
  886. get: function () {
  887. return this._view.camera;
  888. },
  889. set: function (camera) {
  890. // For internal use only. Documentation is still @readonly.
  891. this._view.camera = camera;
  892. },
  893. },
  894. /**
  895. * Gets or sets the view.
  896. * @memberof Scene.prototype
  897. *
  898. * @type {View}
  899. * @readonly
  900. *
  901. * @private
  902. */
  903. view: {
  904. get: function () {
  905. return this._view;
  906. },
  907. set: function (view) {
  908. // For internal use only. Documentation is still @readonly.
  909. this._view = view;
  910. },
  911. },
  912. /**
  913. * Gets the default view.
  914. * @memberof Scene.prototype
  915. *
  916. * @type {View}
  917. * @readonly
  918. *
  919. * @private
  920. */
  921. defaultView: {
  922. get: function () {
  923. return this._defaultView;
  924. },
  925. },
  926. /**
  927. * Gets picking functions and state
  928. * @memberof Scene.prototype
  929. *
  930. * @type {Picking}
  931. * @readonly
  932. *
  933. * @private
  934. */
  935. picking: {
  936. get: function () {
  937. return this._picking;
  938. },
  939. },
  940. /**
  941. * Gets the controller for camera input handling.
  942. * @memberof Scene.prototype
  943. *
  944. * @type {ScreenSpaceCameraController}
  945. * @readonly
  946. */
  947. screenSpaceCameraController: {
  948. get: function () {
  949. return this._screenSpaceCameraController;
  950. },
  951. },
  952. /**
  953. * Get the map projection to use in 2D and Columbus View modes.
  954. * @memberof Scene.prototype
  955. *
  956. * @type {MapProjection}
  957. * @readonly
  958. *
  959. * @default new GeographicProjection()
  960. */
  961. mapProjection: {
  962. get: function () {
  963. return this._mapProjection;
  964. },
  965. },
  966. /**
  967. * Gets the job scheduler
  968. * @memberof Scene.prototype
  969. * @type {JobScheduler}
  970. * @readonly
  971. *
  972. * @private
  973. */
  974. jobScheduler: {
  975. get: function () {
  976. return this._jobScheduler;
  977. },
  978. },
  979. /**
  980. * Gets state information about the current scene. If called outside of a primitive's <code>update</code>
  981. * function, the previous frame's state is returned.
  982. * @memberof Scene.prototype
  983. *
  984. * @type {FrameState}
  985. * @readonly
  986. *
  987. * @private
  988. */
  989. frameState: {
  990. get: function () {
  991. return this._frameState;
  992. },
  993. },
  994. /**
  995. * Gets the environment state.
  996. * @memberof Scene.prototype
  997. *
  998. * @type {EnvironmentState}
  999. * @readonly
  1000. *
  1001. * @private
  1002. */
  1003. environmentState: {
  1004. get: function () {
  1005. return this._environmentState;
  1006. },
  1007. },
  1008. /**
  1009. * Gets the collection of tweens taking place in the scene.
  1010. * @memberof Scene.prototype
  1011. *
  1012. * @type {TweenCollection}
  1013. * @readonly
  1014. *
  1015. * @private
  1016. */
  1017. tweens: {
  1018. get: function () {
  1019. return this._tweens;
  1020. },
  1021. },
  1022. /**
  1023. * Gets the collection of image layers that will be rendered on the globe.
  1024. * @memberof Scene.prototype
  1025. *
  1026. * @type {ImageryLayerCollection}
  1027. * @readonly
  1028. */
  1029. imageryLayers: {
  1030. get: function () {
  1031. if (!defined(this.globe)) {
  1032. return undefined;
  1033. }
  1034. return this.globe.imageryLayers;
  1035. },
  1036. },
  1037. /**
  1038. * The terrain provider providing surface geometry for the globe.
  1039. * @memberof Scene.prototype
  1040. *
  1041. * @type {TerrainProvider}
  1042. */
  1043. terrainProvider: {
  1044. get: function () {
  1045. if (!defined(this.globe)) {
  1046. return undefined;
  1047. }
  1048. return this.globe.terrainProvider;
  1049. },
  1050. set: function (terrainProvider) {
  1051. if (defined(this.globe)) {
  1052. this.globe.terrainProvider = terrainProvider;
  1053. }
  1054. },
  1055. },
  1056. /**
  1057. * Gets an event that's raised when the terrain provider is changed
  1058. * @memberof Scene.prototype
  1059. *
  1060. * @type {Event}
  1061. * @readonly
  1062. */
  1063. terrainProviderChanged: {
  1064. get: function () {
  1065. if (!defined(this.globe)) {
  1066. return undefined;
  1067. }
  1068. return this.globe.terrainProviderChanged;
  1069. },
  1070. },
  1071. /**
  1072. * Gets the event that will be raised before the scene is updated or rendered. Subscribers to the event
  1073. * receive the Scene instance as the first parameter and the current time as the second parameter.
  1074. * @memberof Scene.prototype
  1075. *
  1076. * @see {@link https://cesium.com/blog/2018/01/24/cesium-scene-rendering-performance/|Improving Performance with Explicit Rendering}
  1077. * @see Scene#postUpdate
  1078. * @see Scene#preRender
  1079. * @see Scene#postRender
  1080. *
  1081. * @type {Event}
  1082. * @readonly
  1083. */
  1084. preUpdate: {
  1085. get: function () {
  1086. return this._preUpdate;
  1087. },
  1088. },
  1089. /**
  1090. * Gets the event that will be raised immediately after the scene is updated and before the scene is rendered.
  1091. * Subscribers to the event receive the Scene instance as the first parameter and the current time as the second
  1092. * parameter.
  1093. * @memberof Scene.prototype
  1094. *
  1095. * @see {@link https://cesium.com/blog/2018/01/24/cesium-scene-rendering-performance/|Improving Performance with Explicit Rendering}
  1096. * @see Scene#preUpdate
  1097. * @see Scene#preRender
  1098. * @see Scene#postRender
  1099. *
  1100. * @type {Event}
  1101. * @readonly
  1102. */
  1103. postUpdate: {
  1104. get: function () {
  1105. return this._postUpdate;
  1106. },
  1107. },
  1108. /**
  1109. * Gets the event that will be raised when an error is thrown inside the <code>render</code> function.
  1110. * The Scene instance and the thrown error are the only two parameters passed to the event handler.
  1111. * By default, errors are not rethrown after this event is raised, but that can be changed by setting
  1112. * the <code>rethrowRenderErrors</code> property.
  1113. * @memberof Scene.prototype
  1114. *
  1115. * @type {Event}
  1116. * @readonly
  1117. */
  1118. renderError: {
  1119. get: function () {
  1120. return this._renderError;
  1121. },
  1122. },
  1123. /**
  1124. * Gets the event that will be raised after the scene is updated and immediately before the scene is rendered.
  1125. * Subscribers to the event receive the Scene instance as the first parameter and the current time as the second
  1126. * parameter.
  1127. * @memberof Scene.prototype
  1128. *
  1129. * @see {@link https://cesium.com/blog/2018/01/24/cesium-scene-rendering-performance/|Improving Performance with Explicit Rendering}
  1130. * @see Scene#preUpdate
  1131. * @see Scene#postUpdate
  1132. * @see Scene#postRender
  1133. *
  1134. * @type {Event}
  1135. * @readonly
  1136. */
  1137. preRender: {
  1138. get: function () {
  1139. return this._preRender;
  1140. },
  1141. },
  1142. /**
  1143. * Gets the event that will be raised immediately after the scene is rendered. Subscribers to the event
  1144. * receive the Scene instance as the first parameter and the current time as the second parameter.
  1145. * @memberof Scene.prototype
  1146. *
  1147. * @see {@link https://cesium.com/blog/2018/01/24/cesium-scene-rendering-performance/|Improving Performance with Explicit Rendering}
  1148. * @see Scene#preUpdate
  1149. * @see Scene#postUpdate
  1150. * @see Scene#postRender
  1151. *
  1152. * @type {Event}
  1153. * @readonly
  1154. */
  1155. postRender: {
  1156. get: function () {
  1157. return this._postRender;
  1158. },
  1159. },
  1160. /**
  1161. * Gets the simulation time when the scene was last rendered. Returns undefined if the scene has not yet been
  1162. * rendered.
  1163. * @memberof Scene.prototype
  1164. *
  1165. * @type {JulianDate}
  1166. * @readonly
  1167. */
  1168. lastRenderTime: {
  1169. get: function () {
  1170. return this._lastRenderTime;
  1171. },
  1172. },
  1173. /**
  1174. * @memberof Scene.prototype
  1175. * @private
  1176. * @readonly
  1177. */
  1178. context: {
  1179. get: function () {
  1180. return this._context;
  1181. },
  1182. },
  1183. /**
  1184. * This property is for debugging only; it is not for production use.
  1185. * <p>
  1186. * When {@link Scene.debugShowFrustums} is <code>true</code>, this contains
  1187. * properties with statistics about the number of command execute per frustum.
  1188. * <code>totalCommands</code> is the total number of commands executed, ignoring
  1189. * overlap. <code>commandsInFrustums</code> is an array with the number of times
  1190. * commands are executed redundantly, e.g., how many commands overlap two or
  1191. * three frustums.
  1192. * </p>
  1193. *
  1194. * @memberof Scene.prototype
  1195. *
  1196. * @type {Object}
  1197. * @readonly
  1198. *
  1199. * @default undefined
  1200. */
  1201. debugFrustumStatistics: {
  1202. get: function () {
  1203. return this._view.debugFrustumStatistics;
  1204. },
  1205. },
  1206. /**
  1207. * Gets whether or not the scene is optimized for 3D only viewing.
  1208. * @memberof Scene.prototype
  1209. * @type {Boolean}
  1210. * @readonly
  1211. */
  1212. scene3DOnly: {
  1213. get: function () {
  1214. return this._frameState.scene3DOnly;
  1215. },
  1216. },
  1217. /**
  1218. * Gets whether or not the scene has order independent translucency enabled.
  1219. * Note that this only reflects the original construction option, and there are
  1220. * other factors that could prevent OIT from functioning on a given system configuration.
  1221. * @memberof Scene.prototype
  1222. * @type {Boolean}
  1223. * @readonly
  1224. */
  1225. orderIndependentTranslucency: {
  1226. get: function () {
  1227. return this._useOIT;
  1228. },
  1229. },
  1230. /**
  1231. * Gets the unique identifier for this scene.
  1232. * @memberof Scene.prototype
  1233. * @type {String}
  1234. * @readonly
  1235. */
  1236. id: {
  1237. get: function () {
  1238. return this._id;
  1239. },
  1240. },
  1241. /**
  1242. * Gets or sets the current mode of the scene.
  1243. * @memberof Scene.prototype
  1244. * @type {SceneMode}
  1245. * @default {@link SceneMode.SCENE3D}
  1246. */
  1247. mode: {
  1248. get: function () {
  1249. return this._mode;
  1250. },
  1251. set: function (value) {
  1252. //>>includeStart('debug', pragmas.debug);
  1253. if (this.scene3DOnly && value !== SceneMode.SCENE3D) {
  1254. throw new DeveloperError(
  1255. "Only SceneMode.SCENE3D is valid when scene3DOnly is true."
  1256. );
  1257. }
  1258. //>>includeEnd('debug');
  1259. if (value === SceneMode.SCENE2D) {
  1260. this.morphTo2D(0);
  1261. } else if (value === SceneMode.SCENE3D) {
  1262. this.morphTo3D(0);
  1263. } else if (value === SceneMode.COLUMBUS_VIEW) {
  1264. this.morphToColumbusView(0);
  1265. //>>includeStart('debug', pragmas.debug);
  1266. } else {
  1267. throw new DeveloperError(
  1268. "value must be a valid SceneMode enumeration."
  1269. );
  1270. //>>includeEnd('debug');
  1271. }
  1272. this._mode = value;
  1273. },
  1274. },
  1275. /**
  1276. * Gets the number of frustums used in the last frame.
  1277. * @memberof Scene.prototype
  1278. * @type {FrustumCommands[]}
  1279. *
  1280. * @private
  1281. */
  1282. frustumCommandsList: {
  1283. get: function () {
  1284. return this._view.frustumCommandsList;
  1285. },
  1286. },
  1287. /**
  1288. * Gets the number of frustums used in the last frame.
  1289. * @memberof Scene.prototype
  1290. * @type {Number}
  1291. *
  1292. * @private
  1293. */
  1294. numberOfFrustums: {
  1295. get: function () {
  1296. return this._view.frustumCommandsList.length;
  1297. },
  1298. },
  1299. /**
  1300. * When <code>true</code>, splits the scene into two viewports with steroscopic views for the left and right eyes.
  1301. * Used for cardboard and WebVR.
  1302. * @memberof Scene.prototype
  1303. * @type {Boolean}
  1304. * @default false
  1305. */
  1306. useWebVR: {
  1307. get: function () {
  1308. return this._useWebVR;
  1309. },
  1310. set: function (value) {
  1311. //>>includeStart('debug', pragmas.debug);
  1312. if (this.camera.frustum instanceof OrthographicFrustum) {
  1313. throw new DeveloperError(
  1314. "VR is unsupported with an orthographic projection."
  1315. );
  1316. }
  1317. //>>includeEnd('debug');
  1318. this._useWebVR = value;
  1319. if (this._useWebVR) {
  1320. this._frameState.creditDisplay.container.style.visibility = "hidden";
  1321. this._cameraVR = new Camera(this);
  1322. if (!defined(this._deviceOrientationCameraController)) {
  1323. this._deviceOrientationCameraController = new DeviceOrientationCameraController(
  1324. this
  1325. );
  1326. }
  1327. this._aspectRatioVR = this.camera.frustum.aspectRatio;
  1328. } else {
  1329. this._frameState.creditDisplay.container.style.visibility = "visible";
  1330. this._cameraVR = undefined;
  1331. this._deviceOrientationCameraController =
  1332. this._deviceOrientationCameraController &&
  1333. !this._deviceOrientationCameraController.isDestroyed() &&
  1334. this._deviceOrientationCameraController.destroy();
  1335. this.camera.frustum.aspectRatio = this._aspectRatioVR;
  1336. this.camera.frustum.xOffset = 0.0;
  1337. }
  1338. },
  1339. },
  1340. /**
  1341. * Determines if the 2D map is rotatable or can be scrolled infinitely in the horizontal direction.
  1342. * @memberof Scene.prototype
  1343. * @type {MapMode2D}
  1344. * @readonly
  1345. */
  1346. mapMode2D: {
  1347. get: function () {
  1348. return this._mapMode2D;
  1349. },
  1350. },
  1351. /**
  1352. * Gets or sets the position of the splitter within the viewport. Valid values are between 0.0 and 1.0.
  1353. * @memberof Scene.prototype
  1354. *
  1355. * @type {Number}
  1356. */
  1357. splitPosition: {
  1358. get: function () {
  1359. return this._frameState.splitPosition;
  1360. },
  1361. set: function (value) {
  1362. this._frameState.splitPosition = value;
  1363. },
  1364. },
  1365. /**
  1366. * Gets or sets the position of the Imagery splitter within the viewport. Valid values are between 0.0 and 1.0.
  1367. * @memberof Scene.prototype
  1368. *
  1369. * @deprecated Use splitPosition instead.
  1370. * @type {Number}
  1371. */
  1372. imagerySplitPosition: {
  1373. get: function () {
  1374. deprecationWarning(
  1375. "Scene.imagerySplitPosition",
  1376. "Scene.imagerySplitPosition has been deprecated in Cesium 1.92. It will be removed in Cesium 1.94. Use splitPosition instead."
  1377. );
  1378. return this._frameState.splitPosition;
  1379. },
  1380. set: function (value) {
  1381. deprecationWarning(
  1382. "Scene.imagerySplitPosition",
  1383. "Scene.imagerySplitPosition has been deprecated in Cesium 1.92. It will be removed in Cesium 1.94. Use splitPosition instead."
  1384. );
  1385. this._frameState.splitPosition = value;
  1386. },
  1387. },
  1388. /**
  1389. * The distance from the camera at which to disable the depth test of billboards, labels and points
  1390. * to, for example, prevent clipping against terrain. When set to zero, the depth test should always
  1391. * be applied. When less than zero, the depth test should never be applied. Setting the disableDepthTestDistance
  1392. * property of a billboard, label or point will override this value.
  1393. * @memberof Scene.prototype
  1394. * @type {Number}
  1395. * @default 0.0
  1396. */
  1397. minimumDisableDepthTestDistance: {
  1398. get: function () {
  1399. return this._minimumDisableDepthTestDistance;
  1400. },
  1401. set: function (value) {
  1402. //>>includeStart('debug', pragmas.debug);
  1403. if (!defined(value) || value < 0.0) {
  1404. throw new DeveloperError(
  1405. "minimumDisableDepthTestDistance must be greater than or equal to 0.0."
  1406. );
  1407. }
  1408. //>>includeEnd('debug');
  1409. this._minimumDisableDepthTestDistance = value;
  1410. },
  1411. },
  1412. /**
  1413. * Whether or not to use a logarithmic depth buffer. Enabling this option will allow for less frustums in the multi-frustum,
  1414. * increasing performance. This property relies on fragmentDepth being supported.
  1415. * @memberof Scene.prototype
  1416. * @type {Boolean}
  1417. */
  1418. logarithmicDepthBuffer: {
  1419. get: function () {
  1420. return this._logDepthBuffer;
  1421. },
  1422. set: function (value) {
  1423. value = this._context.fragmentDepth && value;
  1424. if (this._logDepthBuffer !== value) {
  1425. this._logDepthBuffer = value;
  1426. this._logDepthBufferDirty = true;
  1427. }
  1428. },
  1429. },
  1430. /**
  1431. * The value used for gamma correction. This is only used when rendering with high dynamic range.
  1432. * @memberof Scene.prototype
  1433. * @type {Number}
  1434. * @default 2.2
  1435. */
  1436. gamma: {
  1437. get: function () {
  1438. return this._context.uniformState.gamma;
  1439. },
  1440. set: function (value) {
  1441. this._context.uniformState.gamma = value;
  1442. },
  1443. },
  1444. /**
  1445. * Whether or not to use high dynamic range rendering.
  1446. * @memberof Scene.prototype
  1447. * @type {Boolean}
  1448. * @default true
  1449. */
  1450. highDynamicRange: {
  1451. get: function () {
  1452. return this._hdr;
  1453. },
  1454. set: function (value) {
  1455. const context = this._context;
  1456. const hdr =
  1457. value &&
  1458. context.depthTexture &&
  1459. (context.colorBufferFloat || context.colorBufferHalfFloat);
  1460. this._hdrDirty = hdr !== this._hdr;
  1461. this._hdr = hdr;
  1462. },
  1463. },
  1464. /**
  1465. * Whether or not high dynamic range rendering is supported.
  1466. * @memberof Scene.prototype
  1467. * @type {Boolean}
  1468. * @readonly
  1469. * @default true
  1470. */
  1471. highDynamicRangeSupported: {
  1472. get: function () {
  1473. const context = this._context;
  1474. return (
  1475. context.depthTexture &&
  1476. (context.colorBufferFloat || context.colorBufferHalfFloat)
  1477. );
  1478. },
  1479. },
  1480. /**
  1481. * Whether or not the camera is underneath the globe.
  1482. * @memberof Scene.prototype
  1483. * @type {Boolean}
  1484. * @readonly
  1485. * @default false
  1486. */
  1487. cameraUnderground: {
  1488. get: function () {
  1489. return this._cameraUnderground;
  1490. },
  1491. },
  1492. /**
  1493. * The sample rate of multisample antialiasing (values greater than 1 enable MSAA).
  1494. * @memberof Scene.prototype
  1495. * @type {Number}
  1496. * @default 1
  1497. */
  1498. msaaSamples: {
  1499. get: function () {
  1500. return this._msaaSamples;
  1501. },
  1502. set: function (value) {
  1503. value = Math.min(value, ContextLimits.maximumSamples);
  1504. this._msaaSamples = value;
  1505. },
  1506. },
  1507. /**
  1508. * Returns <code>true</code> if the Scene's context supports MSAA.
  1509. * @memberof Scene.prototype
  1510. * @type {Boolean}
  1511. * @readonly
  1512. */
  1513. msaaSupported: {
  1514. get: function () {
  1515. return this._context.msaa;
  1516. },
  1517. },
  1518. /**
  1519. * Ratio between a pixel and a density-independent pixel. Provides a standard unit of
  1520. * measure for real pixel measurements appropriate to a particular device.
  1521. *
  1522. * @memberof Scene.prototype
  1523. * @type {Number}
  1524. * @default 1.0
  1525. * @private
  1526. */
  1527. pixelRatio: {
  1528. get: function () {
  1529. return this._frameState.pixelRatio;
  1530. },
  1531. set: function (value) {
  1532. this._frameState.pixelRatio = value;
  1533. },
  1534. },
  1535. /**
  1536. * @private
  1537. */
  1538. opaqueFrustumNearOffset: {
  1539. get: function () {
  1540. return 0.9999;
  1541. },
  1542. },
  1543. /**
  1544. * @private
  1545. */
  1546. globeHeight: {
  1547. get: function () {
  1548. return this._globeHeight;
  1549. },
  1550. },
  1551. });
  1552. /**
  1553. * Determines if a compressed texture format is supported.
  1554. * @param {String} format The texture format. May be the name of the format or the WebGL extension name, e.g. s3tc or WEBGL_compressed_texture_s3tc.
  1555. * @return {boolean} Whether or not the format is supported.
  1556. */
  1557. Scene.prototype.getCompressedTextureFormatSupported = function (format) {
  1558. const context = this.context;
  1559. return (
  1560. ((format === "WEBGL_compressed_texture_s3tc" || format === "s3tc") &&
  1561. context.s3tc) ||
  1562. ((format === "WEBGL_compressed_texture_pvrtc" || format === "pvrtc") &&
  1563. context.pvrtc) ||
  1564. ((format === "WEBGL_compressed_texture_etc" || format === "etc") &&
  1565. context.etc) ||
  1566. ((format === "WEBGL_compressed_texture_etc1" || format === "etc1") &&
  1567. context.etc1) ||
  1568. ((format === "WEBGL_compressed_texture_astc" || format === "astc") &&
  1569. context.astc) ||
  1570. ((format === "EXT_texture_compression_bptc" || format === "bc7") &&
  1571. context.bc7)
  1572. );
  1573. };
  1574. function updateDerivedCommands(scene, command, shadowsDirty) {
  1575. const frameState = scene._frameState;
  1576. const context = scene._context;
  1577. const oit = scene._view.oit;
  1578. const lightShadowMaps = frameState.shadowState.lightShadowMaps;
  1579. const lightShadowsEnabled = frameState.shadowState.lightShadowsEnabled;
  1580. let derivedCommands = command.derivedCommands;
  1581. if (defined(command.pickId)) {
  1582. derivedCommands.picking = DerivedCommand.createPickDerivedCommand(
  1583. scene,
  1584. command,
  1585. context,
  1586. derivedCommands.picking
  1587. );
  1588. }
  1589. if (!command.pickOnly) {
  1590. derivedCommands.depth = DerivedCommand.createDepthOnlyDerivedCommand(
  1591. scene,
  1592. command,
  1593. context,
  1594. derivedCommands.depth
  1595. );
  1596. }
  1597. derivedCommands.originalCommand = command;
  1598. if (scene._hdr) {
  1599. derivedCommands.hdr = DerivedCommand.createHdrCommand(
  1600. command,
  1601. context,
  1602. derivedCommands.hdr
  1603. );
  1604. command = derivedCommands.hdr.command;
  1605. derivedCommands = command.derivedCommands;
  1606. }
  1607. if (lightShadowsEnabled && command.receiveShadows) {
  1608. derivedCommands.shadows = ShadowMap.createReceiveDerivedCommand(
  1609. lightShadowMaps,
  1610. command,
  1611. shadowsDirty,
  1612. context,
  1613. derivedCommands.shadows
  1614. );
  1615. }
  1616. if (command.pass === Pass.TRANSLUCENT && defined(oit) && oit.isSupported()) {
  1617. if (lightShadowsEnabled && command.receiveShadows) {
  1618. derivedCommands.oit = defined(derivedCommands.oit)
  1619. ? derivedCommands.oit
  1620. : {};
  1621. derivedCommands.oit.shadows = oit.createDerivedCommands(
  1622. derivedCommands.shadows.receiveCommand,
  1623. context,
  1624. derivedCommands.oit.shadows
  1625. );
  1626. } else {
  1627. derivedCommands.oit = oit.createDerivedCommands(
  1628. command,
  1629. context,
  1630. derivedCommands.oit
  1631. );
  1632. }
  1633. }
  1634. }
  1635. /**
  1636. * @private
  1637. */
  1638. Scene.prototype.updateDerivedCommands = function (command) {
  1639. if (!defined(command.derivedCommands)) {
  1640. // Is not a DrawCommand
  1641. return;
  1642. }
  1643. const frameState = this._frameState;
  1644. const context = this._context;
  1645. // Update derived commands when any shadow maps become dirty
  1646. let shadowsDirty = false;
  1647. const lastDirtyTime = frameState.shadowState.lastDirtyTime;
  1648. if (command.lastDirtyTime !== lastDirtyTime) {
  1649. command.lastDirtyTime = lastDirtyTime;
  1650. command.dirty = true;
  1651. shadowsDirty = true;
  1652. }
  1653. const useLogDepth = frameState.useLogDepth;
  1654. const useHdr = this._hdr;
  1655. const derivedCommands = command.derivedCommands;
  1656. const hasLogDepthDerivedCommands = defined(derivedCommands.logDepth);
  1657. const hasHdrCommands = defined(derivedCommands.hdr);
  1658. const hasDerivedCommands = defined(derivedCommands.originalCommand);
  1659. const needsLogDepthDerivedCommands =
  1660. useLogDepth && !hasLogDepthDerivedCommands;
  1661. const needsHdrCommands = useHdr && !hasHdrCommands;
  1662. const needsDerivedCommands = (!useLogDepth || !useHdr) && !hasDerivedCommands;
  1663. command.dirty =
  1664. command.dirty ||
  1665. needsLogDepthDerivedCommands ||
  1666. needsHdrCommands ||
  1667. needsDerivedCommands;
  1668. if (command.dirty) {
  1669. command.dirty = false;
  1670. const shadowMaps = frameState.shadowState.shadowMaps;
  1671. const shadowsEnabled = frameState.shadowState.shadowsEnabled;
  1672. if (shadowsEnabled && command.castShadows) {
  1673. derivedCommands.shadows = ShadowMap.createCastDerivedCommand(
  1674. shadowMaps,
  1675. command,
  1676. shadowsDirty,
  1677. context,
  1678. derivedCommands.shadows
  1679. );
  1680. }
  1681. if (hasLogDepthDerivedCommands || needsLogDepthDerivedCommands) {
  1682. derivedCommands.logDepth = DerivedCommand.createLogDepthCommand(
  1683. command,
  1684. context,
  1685. derivedCommands.logDepth
  1686. );
  1687. updateDerivedCommands(
  1688. this,
  1689. derivedCommands.logDepth.command,
  1690. shadowsDirty
  1691. );
  1692. }
  1693. if (hasDerivedCommands || needsDerivedCommands) {
  1694. updateDerivedCommands(this, command, shadowsDirty);
  1695. }
  1696. }
  1697. };
  1698. const renderTilesetPassState = new Cesium3DTilePassState({
  1699. pass: Cesium3DTilePass.RENDER,
  1700. });
  1701. const preloadTilesetPassState = new Cesium3DTilePassState({
  1702. pass: Cesium3DTilePass.PRELOAD,
  1703. });
  1704. const preloadFlightTilesetPassState = new Cesium3DTilePassState({
  1705. pass: Cesium3DTilePass.PRELOAD_FLIGHT,
  1706. });
  1707. const requestRenderModeDeferCheckPassState = new Cesium3DTilePassState({
  1708. pass: Cesium3DTilePass.REQUEST_RENDER_MODE_DEFER_CHECK,
  1709. });
  1710. const scratchOccluderBoundingSphere = new BoundingSphere();
  1711. let scratchOccluder;
  1712. function getOccluder(scene) {
  1713. // TODO: The occluder is the top-level globe. When we add
  1714. // support for multiple central bodies, this should be the closest one.
  1715. const globe = scene.globe;
  1716. if (
  1717. scene._mode === SceneMode.SCENE3D &&
  1718. defined(globe) &&
  1719. globe.show &&
  1720. !scene._cameraUnderground &&
  1721. !scene._globeTranslucencyState.translucent
  1722. ) {
  1723. const ellipsoid = globe.ellipsoid;
  1724. const minimumTerrainHeight = scene.frameState.minimumTerrainHeight;
  1725. scratchOccluderBoundingSphere.radius =
  1726. ellipsoid.minimumRadius + minimumTerrainHeight;
  1727. scratchOccluder = Occluder.fromBoundingSphere(
  1728. scratchOccluderBoundingSphere,
  1729. scene.camera.positionWC,
  1730. scratchOccluder
  1731. );
  1732. return scratchOccluder;
  1733. }
  1734. return undefined;
  1735. }
  1736. /**
  1737. * @private
  1738. */
  1739. Scene.prototype.clearPasses = function (passes) {
  1740. passes.render = false;
  1741. passes.pick = false;
  1742. passes.depth = false;
  1743. passes.postProcess = false;
  1744. passes.offscreen = false;
  1745. };
  1746. function updateFrameNumber(scene, frameNumber, time) {
  1747. const frameState = scene._frameState;
  1748. frameState.frameNumber = frameNumber;
  1749. frameState.time = JulianDate.clone(time, frameState.time);
  1750. }
  1751. /**
  1752. * @private
  1753. */
  1754. Scene.prototype.updateFrameState = function () {
  1755. const camera = this.camera;
  1756. const frameState = this._frameState;
  1757. frameState.commandList.length = 0;
  1758. frameState.shadowMaps.length = 0;
  1759. frameState.brdfLutGenerator = this._brdfLutGenerator;
  1760. frameState.environmentMap = this.skyBox && this.skyBox._cubeMap;
  1761. frameState.mode = this._mode;
  1762. frameState.morphTime = this.morphTime;
  1763. frameState.mapProjection = this.mapProjection;
  1764. frameState.camera = camera;
  1765. frameState.cullingVolume = camera.frustum.computeCullingVolume(
  1766. camera.positionWC,
  1767. camera.directionWC,
  1768. camera.upWC
  1769. );
  1770. frameState.occluder = getOccluder(this);
  1771. frameState.minimumTerrainHeight = 0.0;
  1772. frameState.minimumDisableDepthTestDistance = this._minimumDisableDepthTestDistance;
  1773. frameState.invertClassification = this.invertClassification;
  1774. frameState.useLogDepth =
  1775. this._logDepthBuffer &&
  1776. !(
  1777. this.camera.frustum instanceof OrthographicFrustum ||
  1778. this.camera.frustum instanceof OrthographicOffCenterFrustum
  1779. );
  1780. frameState.light = this.light;
  1781. frameState.cameraUnderground = this._cameraUnderground;
  1782. frameState.globeTranslucencyState = this._globeTranslucencyState;
  1783. if (defined(this.globe)) {
  1784. frameState.terrainExaggeration = this.globe.terrainExaggeration;
  1785. frameState.terrainExaggerationRelativeHeight = this.globe.terrainExaggerationRelativeHeight;
  1786. }
  1787. if (
  1788. defined(this._specularEnvironmentMapAtlas) &&
  1789. this._specularEnvironmentMapAtlas.ready
  1790. ) {
  1791. frameState.specularEnvironmentMaps = this._specularEnvironmentMapAtlas.texture;
  1792. frameState.specularEnvironmentMapsMaximumLOD = this._specularEnvironmentMapAtlas.maximumMipmapLevel;
  1793. } else {
  1794. frameState.specularEnvironmentMaps = undefined;
  1795. frameState.specularEnvironmentMapsMaximumLOD = undefined;
  1796. }
  1797. frameState.sphericalHarmonicCoefficients = this.sphericalHarmonicCoefficients;
  1798. this._actualInvertClassificationColor = Color.clone(
  1799. this.invertClassificationColor,
  1800. this._actualInvertClassificationColor
  1801. );
  1802. if (!InvertClassification.isTranslucencySupported(this._context)) {
  1803. this._actualInvertClassificationColor.alpha = 1.0;
  1804. }
  1805. frameState.invertClassificationColor = this._actualInvertClassificationColor;
  1806. if (defined(this.globe)) {
  1807. frameState.maximumScreenSpaceError = this.globe.maximumScreenSpaceError;
  1808. } else {
  1809. frameState.maximumScreenSpaceError = 2;
  1810. }
  1811. this.clearPasses(frameState.passes);
  1812. frameState.tilesetPassState = undefined;
  1813. };
  1814. /**
  1815. * @private
  1816. */
  1817. Scene.prototype.isVisible = function (command, cullingVolume, occluder) {
  1818. return (
  1819. defined(command) &&
  1820. (!defined(command.boundingVolume) ||
  1821. !command.cull ||
  1822. (cullingVolume.computeVisibility(command.boundingVolume) !==
  1823. Intersect.OUTSIDE &&
  1824. (!defined(occluder) ||
  1825. !command.occlude ||
  1826. !command.boundingVolume.isOccluded(occluder))))
  1827. );
  1828. };
  1829. let transformFrom2D = new Matrix4(
  1830. 0.0,
  1831. 0.0,
  1832. 1.0,
  1833. 0.0,
  1834. 1.0,
  1835. 0.0,
  1836. 0.0,
  1837. 0.0,
  1838. 0.0,
  1839. 1.0,
  1840. 0.0,
  1841. 0.0,
  1842. 0.0,
  1843. 0.0,
  1844. 0.0,
  1845. 1.0
  1846. );
  1847. transformFrom2D = Matrix4.inverseTransformation(
  1848. transformFrom2D,
  1849. transformFrom2D
  1850. );
  1851. function debugShowBoundingVolume(command, scene, passState, debugFramebuffer) {
  1852. // Debug code to draw bounding volume for command. Not optimized!
  1853. // Assumes bounding volume is a bounding sphere or box
  1854. const frameState = scene._frameState;
  1855. const context = frameState.context;
  1856. const boundingVolume = command.boundingVolume;
  1857. if (defined(scene._debugVolume)) {
  1858. scene._debugVolume.destroy();
  1859. }
  1860. let geometry;
  1861. let center = Cartesian3.clone(boundingVolume.center);
  1862. if (frameState.mode !== SceneMode.SCENE3D) {
  1863. center = Matrix4.multiplyByPoint(transformFrom2D, center, center);
  1864. const projection = frameState.mapProjection;
  1865. const centerCartographic = projection.unproject(center);
  1866. center = projection.ellipsoid.cartographicToCartesian(centerCartographic);
  1867. }
  1868. if (defined(boundingVolume.radius)) {
  1869. const radius = boundingVolume.radius;
  1870. geometry = GeometryPipeline.toWireframe(
  1871. EllipsoidGeometry.createGeometry(
  1872. new EllipsoidGeometry({
  1873. radii: new Cartesian3(radius, radius, radius),
  1874. vertexFormat: PerInstanceColorAppearance.FLAT_VERTEX_FORMAT,
  1875. })
  1876. )
  1877. );
  1878. scene._debugVolume = new Primitive({
  1879. geometryInstances: new GeometryInstance({
  1880. geometry: geometry,
  1881. modelMatrix: Matrix4.fromTranslation(center),
  1882. attributes: {
  1883. color: new ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 1.0),
  1884. },
  1885. }),
  1886. appearance: new PerInstanceColorAppearance({
  1887. flat: true,
  1888. translucent: false,
  1889. }),
  1890. asynchronous: false,
  1891. });
  1892. } else {
  1893. const halfAxes = boundingVolume.halfAxes;
  1894. geometry = GeometryPipeline.toWireframe(
  1895. BoxGeometry.createGeometry(
  1896. BoxGeometry.fromDimensions({
  1897. dimensions: new Cartesian3(2.0, 2.0, 2.0),
  1898. vertexFormat: PerInstanceColorAppearance.FLAT_VERTEX_FORMAT,
  1899. })
  1900. )
  1901. );
  1902. scene._debugVolume = new Primitive({
  1903. geometryInstances: new GeometryInstance({
  1904. geometry: geometry,
  1905. modelMatrix: Matrix4.fromRotationTranslation(
  1906. halfAxes,
  1907. center,
  1908. new Matrix4()
  1909. ),
  1910. attributes: {
  1911. color: new ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 1.0),
  1912. },
  1913. }),
  1914. appearance: new PerInstanceColorAppearance({
  1915. flat: true,
  1916. translucent: false,
  1917. }),
  1918. asynchronous: false,
  1919. });
  1920. }
  1921. const savedCommandList = frameState.commandList;
  1922. const commandList = (frameState.commandList = []);
  1923. scene._debugVolume.update(frameState);
  1924. command = commandList[0];
  1925. if (frameState.useLogDepth) {
  1926. const logDepth = DerivedCommand.createLogDepthCommand(command, context);
  1927. command = logDepth.command;
  1928. }
  1929. let framebuffer;
  1930. if (defined(debugFramebuffer)) {
  1931. framebuffer = passState.framebuffer;
  1932. passState.framebuffer = debugFramebuffer;
  1933. }
  1934. command.execute(context, passState);
  1935. if (defined(framebuffer)) {
  1936. passState.framebuffer = framebuffer;
  1937. }
  1938. frameState.commandList = savedCommandList;
  1939. }
  1940. function executeCommand(command, scene, context, passState, debugFramebuffer) {
  1941. const frameState = scene._frameState;
  1942. if (defined(scene.debugCommandFilter) && !scene.debugCommandFilter(command)) {
  1943. return;
  1944. }
  1945. if (command instanceof ClearCommand) {
  1946. command.execute(context, passState);
  1947. return;
  1948. }
  1949. if (command.debugShowBoundingVolume && defined(command.boundingVolume)) {
  1950. debugShowBoundingVolume(command, scene, passState, debugFramebuffer);
  1951. }
  1952. if (frameState.useLogDepth && defined(command.derivedCommands.logDepth)) {
  1953. command = command.derivedCommands.logDepth.command;
  1954. }
  1955. const passes = frameState.passes;
  1956. if (
  1957. !passes.pick &&
  1958. !passes.depth &&
  1959. scene._hdr &&
  1960. defined(command.derivedCommands) &&
  1961. defined(command.derivedCommands.hdr)
  1962. ) {
  1963. command = command.derivedCommands.hdr.command;
  1964. }
  1965. if (passes.pick || passes.depth) {
  1966. if (
  1967. passes.pick &&
  1968. !passes.depth &&
  1969. defined(command.derivedCommands.picking)
  1970. ) {
  1971. command = command.derivedCommands.picking.pickCommand;
  1972. command.execute(context, passState);
  1973. return;
  1974. } else if (defined(command.derivedCommands.depth)) {
  1975. command = command.derivedCommands.depth.depthOnlyCommand;
  1976. command.execute(context, passState);
  1977. return;
  1978. }
  1979. }
  1980. if (scene.debugShowCommands || scene.debugShowFrustums) {
  1981. scene._debugInspector.executeDebugShowFrustumsCommand(
  1982. scene,
  1983. command,
  1984. passState
  1985. );
  1986. return;
  1987. }
  1988. if (
  1989. frameState.shadowState.lightShadowsEnabled &&
  1990. command.receiveShadows &&
  1991. defined(command.derivedCommands.shadows)
  1992. ) {
  1993. // If the command receives shadows, execute the derived shadows command.
  1994. // Some commands, such as OIT derived commands, do not have derived shadow commands themselves
  1995. // and instead shadowing is built-in. In this case execute the command regularly below.
  1996. command.derivedCommands.shadows.receiveCommand.execute(context, passState);
  1997. } else {
  1998. command.execute(context, passState);
  1999. }
  2000. }
  2001. function executeIdCommand(command, scene, context, passState) {
  2002. const frameState = scene._frameState;
  2003. let derivedCommands = command.derivedCommands;
  2004. if (!defined(derivedCommands)) {
  2005. return;
  2006. }
  2007. if (frameState.useLogDepth && defined(derivedCommands.logDepth)) {
  2008. command = derivedCommands.logDepth.command;
  2009. }
  2010. derivedCommands = command.derivedCommands;
  2011. if (defined(derivedCommands.picking)) {
  2012. command = derivedCommands.picking.pickCommand;
  2013. command.execute(context, passState);
  2014. } else if (defined(derivedCommands.depth)) {
  2015. command = derivedCommands.depth.depthOnlyCommand;
  2016. command.execute(context, passState);
  2017. }
  2018. }
  2019. function backToFront(a, b, position) {
  2020. return (
  2021. b.boundingVolume.distanceSquaredTo(position) -
  2022. a.boundingVolume.distanceSquaredTo(position)
  2023. );
  2024. }
  2025. function frontToBack(a, b, position) {
  2026. // When distances are equal equal favor sorting b before a. This gives render priority to commands later in the list.
  2027. return (
  2028. a.boundingVolume.distanceSquaredTo(position) -
  2029. b.boundingVolume.distanceSquaredTo(position) +
  2030. CesiumMath.EPSILON12
  2031. );
  2032. }
  2033. function executeTranslucentCommandsBackToFront(
  2034. scene,
  2035. executeFunction,
  2036. passState,
  2037. commands,
  2038. invertClassification
  2039. ) {
  2040. const context = scene.context;
  2041. mergeSort(commands, backToFront, scene.camera.positionWC);
  2042. if (defined(invertClassification)) {
  2043. executeFunction(
  2044. invertClassification.unclassifiedCommand,
  2045. scene,
  2046. context,
  2047. passState
  2048. );
  2049. }
  2050. const length = commands.length;
  2051. for (let i = 0; i < length; ++i) {
  2052. executeFunction(commands[i], scene, context, passState);
  2053. }
  2054. }
  2055. function executeTranslucentCommandsFrontToBack(
  2056. scene,
  2057. executeFunction,
  2058. passState,
  2059. commands,
  2060. invertClassification
  2061. ) {
  2062. const context = scene.context;
  2063. mergeSort(commands, frontToBack, scene.camera.positionWC);
  2064. if (defined(invertClassification)) {
  2065. executeFunction(
  2066. invertClassification.unclassifiedCommand,
  2067. scene,
  2068. context,
  2069. passState
  2070. );
  2071. }
  2072. const length = commands.length;
  2073. for (let i = 0; i < length; ++i) {
  2074. executeFunction(commands[i], scene, context, passState);
  2075. }
  2076. }
  2077. const scratchPerspectiveFrustum = new PerspectiveFrustum();
  2078. const scratchPerspectiveOffCenterFrustum = new PerspectiveOffCenterFrustum();
  2079. const scratchOrthographicFrustum = new OrthographicFrustum();
  2080. const scratchOrthographicOffCenterFrustum = new OrthographicOffCenterFrustum();
  2081. function executeCommands(scene, passState) {
  2082. const camera = scene.camera;
  2083. const context = scene.context;
  2084. const frameState = scene.frameState;
  2085. const us = context.uniformState;
  2086. us.updateCamera(camera);
  2087. // Create a working frustum from the original camera frustum.
  2088. let frustum;
  2089. if (defined(camera.frustum.fov)) {
  2090. frustum = camera.frustum.clone(scratchPerspectiveFrustum);
  2091. } else if (defined(camera.frustum.infiniteProjectionMatrix)) {
  2092. frustum = camera.frustum.clone(scratchPerspectiveOffCenterFrustum);
  2093. } else if (defined(camera.frustum.width)) {
  2094. frustum = camera.frustum.clone(scratchOrthographicFrustum);
  2095. } else {
  2096. frustum = camera.frustum.clone(scratchOrthographicOffCenterFrustum);
  2097. }
  2098. // Ideally, we would render the sky box and atmosphere last for
  2099. // early-z, but we would have to draw it in each frustum
  2100. frustum.near = camera.frustum.near;
  2101. frustum.far = camera.frustum.far;
  2102. us.updateFrustum(frustum);
  2103. us.updatePass(Pass.ENVIRONMENT);
  2104. const passes = frameState.passes;
  2105. const picking = passes.pick;
  2106. const environmentState = scene._environmentState;
  2107. const view = scene._view;
  2108. const renderTranslucentDepthForPick =
  2109. environmentState.renderTranslucentDepthForPick;
  2110. const useWebVR = environmentState.useWebVR;
  2111. // Do not render environment primitives during a pick pass since they do not generate picking commands.
  2112. if (!picking) {
  2113. const skyBoxCommand = environmentState.skyBoxCommand;
  2114. if (defined(skyBoxCommand)) {
  2115. executeCommand(skyBoxCommand, scene, context, passState);
  2116. }
  2117. if (environmentState.isSkyAtmosphereVisible) {
  2118. executeCommand(
  2119. environmentState.skyAtmosphereCommand,
  2120. scene,
  2121. context,
  2122. passState
  2123. );
  2124. }
  2125. if (environmentState.isSunVisible) {
  2126. environmentState.sunDrawCommand.execute(context, passState);
  2127. if (scene.sunBloom && !useWebVR) {
  2128. let framebuffer;
  2129. if (environmentState.useGlobeDepthFramebuffer) {
  2130. framebuffer = view.globeDepth.framebuffer;
  2131. } else if (environmentState.usePostProcess) {
  2132. framebuffer = view.sceneFramebuffer.framebuffer;
  2133. } else {
  2134. framebuffer = environmentState.originalFramebuffer;
  2135. }
  2136. scene._sunPostProcess.execute(context);
  2137. scene._sunPostProcess.copy(context, framebuffer);
  2138. passState.framebuffer = framebuffer;
  2139. }
  2140. }
  2141. // Moon can be seen through the atmosphere, since the sun is rendered after the atmosphere.
  2142. if (environmentState.isMoonVisible) {
  2143. environmentState.moonCommand.execute(context, passState);
  2144. }
  2145. }
  2146. // Determine how translucent surfaces will be handled.
  2147. let executeTranslucentCommands;
  2148. if (environmentState.useOIT) {
  2149. if (!defined(scene._executeOITFunction)) {
  2150. scene._executeOITFunction = function (
  2151. scene,
  2152. executeFunction,
  2153. passState,
  2154. commands,
  2155. invertClassification
  2156. ) {
  2157. view.globeDepth.prepareColorTextures(context);
  2158. view.oit.executeCommands(
  2159. scene,
  2160. executeFunction,
  2161. passState,
  2162. commands,
  2163. invertClassification
  2164. );
  2165. };
  2166. }
  2167. executeTranslucentCommands = scene._executeOITFunction;
  2168. } else if (passes.render) {
  2169. executeTranslucentCommands = executeTranslucentCommandsBackToFront;
  2170. } else {
  2171. executeTranslucentCommands = executeTranslucentCommandsFrontToBack;
  2172. }
  2173. const frustumCommandsList = view.frustumCommandsList;
  2174. const numFrustums = frustumCommandsList.length;
  2175. const clearGlobeDepth = environmentState.clearGlobeDepth;
  2176. const useDepthPlane = environmentState.useDepthPlane;
  2177. const globeTranslucencyState = scene._globeTranslucencyState;
  2178. const globeTranslucent = globeTranslucencyState.translucent;
  2179. const globeTranslucencyFramebuffer = scene._view.globeTranslucencyFramebuffer;
  2180. const clearDepth = scene._depthClearCommand;
  2181. const clearStencil = scene._stencilClearCommand;
  2182. const clearClassificationStencil = scene._classificationStencilClearCommand;
  2183. const depthPlane = scene._depthPlane;
  2184. const usePostProcessSelected = environmentState.usePostProcessSelected;
  2185. const height2D = camera.position.z;
  2186. // Execute commands in each frustum in back to front order
  2187. let j;
  2188. for (let i = 0; i < numFrustums; ++i) {
  2189. const index = numFrustums - i - 1;
  2190. const frustumCommands = frustumCommandsList[index];
  2191. if (scene.mode === SceneMode.SCENE2D) {
  2192. // To avoid z-fighting in 2D, move the camera to just before the frustum
  2193. // and scale the frustum depth to be in [1.0, nearToFarDistance2D].
  2194. camera.position.z = height2D - frustumCommands.near + 1.0;
  2195. frustum.far = Math.max(1.0, frustumCommands.far - frustumCommands.near);
  2196. frustum.near = 1.0;
  2197. us.update(frameState);
  2198. us.updateFrustum(frustum);
  2199. } else {
  2200. // Avoid tearing artifacts between adjacent frustums in the opaque passes
  2201. frustum.near =
  2202. index !== 0
  2203. ? frustumCommands.near * scene.opaqueFrustumNearOffset
  2204. : frustumCommands.near;
  2205. frustum.far = frustumCommands.far;
  2206. us.updateFrustum(frustum);
  2207. }
  2208. clearDepth.execute(context, passState);
  2209. if (context.stencilBuffer) {
  2210. clearStencil.execute(context, passState);
  2211. }
  2212. us.updatePass(Pass.GLOBE);
  2213. let commands = frustumCommands.commands[Pass.GLOBE];
  2214. let length = frustumCommands.indices[Pass.GLOBE];
  2215. if (globeTranslucent) {
  2216. globeTranslucencyState.executeGlobeCommands(
  2217. frustumCommands,
  2218. executeCommand,
  2219. globeTranslucencyFramebuffer,
  2220. scene,
  2221. passState
  2222. );
  2223. } else {
  2224. for (j = 0; j < length; ++j) {
  2225. executeCommand(commands[j], scene, context, passState);
  2226. }
  2227. }
  2228. const globeDepth = view.globeDepth;
  2229. if (defined(globeDepth) && environmentState.useGlobeDepthFramebuffer) {
  2230. globeDepth.executeCopyDepth(context, passState);
  2231. }
  2232. // Draw terrain classification
  2233. if (!environmentState.renderTranslucentDepthForPick) {
  2234. us.updatePass(Pass.TERRAIN_CLASSIFICATION);
  2235. commands = frustumCommands.commands[Pass.TERRAIN_CLASSIFICATION];
  2236. length = frustumCommands.indices[Pass.TERRAIN_CLASSIFICATION];
  2237. if (globeTranslucent) {
  2238. globeTranslucencyState.executeGlobeClassificationCommands(
  2239. frustumCommands,
  2240. executeCommand,
  2241. globeTranslucencyFramebuffer,
  2242. scene,
  2243. passState
  2244. );
  2245. } else {
  2246. for (j = 0; j < length; ++j) {
  2247. executeCommand(commands[j], scene, context, passState);
  2248. }
  2249. }
  2250. }
  2251. if (clearGlobeDepth) {
  2252. clearDepth.execute(context, passState);
  2253. if (useDepthPlane) {
  2254. depthPlane.execute(context, passState);
  2255. }
  2256. }
  2257. if (
  2258. !environmentState.useInvertClassification ||
  2259. picking ||
  2260. environmentState.renderTranslucentDepthForPick
  2261. ) {
  2262. // Common/fastest path. Draw 3D Tiles and classification normally.
  2263. // Draw 3D Tiles
  2264. us.updatePass(Pass.CESIUM_3D_TILE);
  2265. commands = frustumCommands.commands[Pass.CESIUM_3D_TILE];
  2266. length = frustumCommands.indices[Pass.CESIUM_3D_TILE];
  2267. for (j = 0; j < length; ++j) {
  2268. executeCommand(commands[j], scene, context, passState);
  2269. }
  2270. if (length > 0) {
  2271. if (defined(globeDepth) && environmentState.useGlobeDepthFramebuffer) {
  2272. // When clearGlobeDepth is true, executeUpdateDepth needs
  2273. // a globe depth texture with resolved stencil bits.
  2274. globeDepth.prepareColorTextures(context, clearGlobeDepth);
  2275. globeDepth.executeUpdateDepth(
  2276. context,
  2277. passState,
  2278. clearGlobeDepth,
  2279. globeDepth.depthStencilTexture
  2280. );
  2281. }
  2282. // Draw classifications. Modifies 3D Tiles color.
  2283. if (!environmentState.renderTranslucentDepthForPick) {
  2284. us.updatePass(Pass.CESIUM_3D_TILE_CLASSIFICATION);
  2285. commands =
  2286. frustumCommands.commands[Pass.CESIUM_3D_TILE_CLASSIFICATION];
  2287. length = frustumCommands.indices[Pass.CESIUM_3D_TILE_CLASSIFICATION];
  2288. for (j = 0; j < length; ++j) {
  2289. executeCommand(commands[j], scene, context, passState);
  2290. }
  2291. }
  2292. }
  2293. } else {
  2294. // When the invert classification color is opaque:
  2295. // Main FBO (FBO1): Main_Color + Main_DepthStencil
  2296. // Invert classification FBO (FBO2) : Invert_Color + Main_DepthStencil
  2297. //
  2298. // 1. Clear FBO2 color to vec4(0.0) for each frustum
  2299. // 2. Draw 3D Tiles to FBO2
  2300. // 3. Draw classification to FBO2
  2301. // 4. Fullscreen pass to FBO1, draw Invert_Color when:
  2302. // * Main_DepthStencil has the stencil bit set > 0 (classified)
  2303. // 5. Fullscreen pass to FBO1, draw Invert_Color * czm_invertClassificationColor when:
  2304. // * Main_DepthStencil has stencil bit set to 0 (unclassified) and
  2305. // * Invert_Color !== vec4(0.0)
  2306. //
  2307. // When the invert classification color is translucent:
  2308. // Main FBO (FBO1): Main_Color + Main_DepthStencil
  2309. // Invert classification FBO (FBO2): Invert_Color + Invert_DepthStencil
  2310. // IsClassified FBO (FBO3): IsClassified_Color + Invert_DepthStencil
  2311. //
  2312. // 1. Clear FBO2 and FBO3 color to vec4(0.0), stencil to 0, and depth to 1.0
  2313. // 2. Draw 3D Tiles to FBO2
  2314. // 3. Draw classification to FBO2
  2315. // 4. Fullscreen pass to FBO3, draw any color when
  2316. // * Invert_DepthStencil has the stencil bit set > 0 (classified)
  2317. // 5. Fullscreen pass to FBO1, draw Invert_Color when:
  2318. // * Invert_Color !== vec4(0.0) and
  2319. // * IsClassified_Color !== vec4(0.0)
  2320. // 6. Fullscreen pass to FBO1, draw Invert_Color * czm_invertClassificationColor when:
  2321. // * Invert_Color !== vec4(0.0) and
  2322. // * IsClassified_Color === vec4(0.0)
  2323. //
  2324. // NOTE: Step six when translucent invert color occurs after the TRANSLUCENT pass
  2325. //
  2326. scene._invertClassification.clear(context, passState);
  2327. const opaqueClassificationFramebuffer = passState.framebuffer;
  2328. passState.framebuffer = scene._invertClassification._fbo.framebuffer;
  2329. // Draw normally
  2330. us.updatePass(Pass.CESIUM_3D_TILE);
  2331. commands = frustumCommands.commands[Pass.CESIUM_3D_TILE];
  2332. length = frustumCommands.indices[Pass.CESIUM_3D_TILE];
  2333. for (j = 0; j < length; ++j) {
  2334. executeCommand(commands[j], scene, context, passState);
  2335. }
  2336. if (defined(globeDepth) && environmentState.useGlobeDepthFramebuffer) {
  2337. scene._invertClassification.prepareTextures(context);
  2338. globeDepth.executeUpdateDepth(
  2339. context,
  2340. passState,
  2341. clearGlobeDepth,
  2342. scene._invertClassification._fbo.getDepthStencilTexture()
  2343. );
  2344. }
  2345. // Set stencil
  2346. us.updatePass(Pass.CESIUM_3D_TILE_CLASSIFICATION_IGNORE_SHOW);
  2347. commands =
  2348. frustumCommands.commands[
  2349. Pass.CESIUM_3D_TILE_CLASSIFICATION_IGNORE_SHOW
  2350. ];
  2351. length =
  2352. frustumCommands.indices[Pass.CESIUM_3D_TILE_CLASSIFICATION_IGNORE_SHOW];
  2353. for (j = 0; j < length; ++j) {
  2354. executeCommand(commands[j], scene, context, passState);
  2355. }
  2356. passState.framebuffer = opaqueClassificationFramebuffer;
  2357. // Fullscreen pass to copy classified fragments
  2358. scene._invertClassification.executeClassified(context, passState);
  2359. if (frameState.invertClassificationColor.alpha === 1.0) {
  2360. // Fullscreen pass to copy unclassified fragments when alpha == 1.0
  2361. scene._invertClassification.executeUnclassified(context, passState);
  2362. }
  2363. // Clear stencil set by the classification for the next classification pass
  2364. if (length > 0 && context.stencilBuffer) {
  2365. clearClassificationStencil.execute(context, passState);
  2366. }
  2367. // Draw style over classification.
  2368. us.updatePass(Pass.CESIUM_3D_TILE_CLASSIFICATION);
  2369. commands = frustumCommands.commands[Pass.CESIUM_3D_TILE_CLASSIFICATION];
  2370. length = frustumCommands.indices[Pass.CESIUM_3D_TILE_CLASSIFICATION];
  2371. for (j = 0; j < length; ++j) {
  2372. executeCommand(commands[j], scene, context, passState);
  2373. }
  2374. }
  2375. if (length > 0 && context.stencilBuffer) {
  2376. clearStencil.execute(context, passState);
  2377. }
  2378. us.updatePass(Pass.OPAQUE);
  2379. commands = frustumCommands.commands[Pass.OPAQUE];
  2380. length = frustumCommands.indices[Pass.OPAQUE];
  2381. for (j = 0; j < length; ++j) {
  2382. executeCommand(commands[j], scene, context, passState);
  2383. }
  2384. if (index !== 0 && scene.mode !== SceneMode.SCENE2D) {
  2385. // Do not overlap frustums in the translucent pass to avoid blending artifacts
  2386. frustum.near = frustumCommands.near;
  2387. us.updateFrustum(frustum);
  2388. }
  2389. let invertClassification;
  2390. if (
  2391. !picking &&
  2392. environmentState.useInvertClassification &&
  2393. frameState.invertClassificationColor.alpha < 1.0
  2394. ) {
  2395. // Fullscreen pass to copy unclassified fragments when alpha < 1.0.
  2396. // Not executed when undefined.
  2397. invertClassification = scene._invertClassification;
  2398. }
  2399. us.updatePass(Pass.TRANSLUCENT);
  2400. commands = frustumCommands.commands[Pass.TRANSLUCENT];
  2401. commands.length = frustumCommands.indices[Pass.TRANSLUCENT];
  2402. executeTranslucentCommands(
  2403. scene,
  2404. executeCommand,
  2405. passState,
  2406. commands,
  2407. invertClassification
  2408. );
  2409. // Classification for translucent 3D Tiles
  2410. const has3DTilesClassificationCommands =
  2411. frustumCommands.indices[Pass.CESIUM_3D_TILE_CLASSIFICATION] > 0;
  2412. if (
  2413. has3DTilesClassificationCommands &&
  2414. view.translucentTileClassification.isSupported()
  2415. ) {
  2416. view.translucentTileClassification.executeTranslucentCommands(
  2417. scene,
  2418. executeCommand,
  2419. passState,
  2420. commands,
  2421. globeDepth.depthStencilTexture
  2422. );
  2423. view.translucentTileClassification.executeClassificationCommands(
  2424. scene,
  2425. executeCommand,
  2426. passState,
  2427. frustumCommands
  2428. );
  2429. }
  2430. if (
  2431. context.depthTexture &&
  2432. scene.useDepthPicking &&
  2433. (environmentState.useGlobeDepthFramebuffer ||
  2434. renderTranslucentDepthForPick)
  2435. ) {
  2436. // PERFORMANCE_IDEA: Use MRT to avoid the extra copy.
  2437. const depthStencilTexture = globeDepth.depthStencilTexture;
  2438. const pickDepth = scene._picking.getPickDepth(scene, index);
  2439. pickDepth.update(context, depthStencilTexture);
  2440. pickDepth.executeCopyDepth(context, passState);
  2441. }
  2442. if (picking || !usePostProcessSelected) {
  2443. continue;
  2444. }
  2445. const originalFramebuffer = passState.framebuffer;
  2446. passState.framebuffer = view.sceneFramebuffer.getIdFramebuffer();
  2447. // reset frustum
  2448. frustum.near =
  2449. index !== 0
  2450. ? frustumCommands.near * scene.opaqueFrustumNearOffset
  2451. : frustumCommands.near;
  2452. frustum.far = frustumCommands.far;
  2453. us.updateFrustum(frustum);
  2454. us.updatePass(Pass.GLOBE);
  2455. commands = frustumCommands.commands[Pass.GLOBE];
  2456. length = frustumCommands.indices[Pass.GLOBE];
  2457. if (globeTranslucent) {
  2458. globeTranslucencyState.executeGlobeCommands(
  2459. frustumCommands,
  2460. executeIdCommand,
  2461. globeTranslucencyFramebuffer,
  2462. scene,
  2463. passState
  2464. );
  2465. } else {
  2466. for (j = 0; j < length; ++j) {
  2467. executeIdCommand(commands[j], scene, context, passState);
  2468. }
  2469. }
  2470. if (clearGlobeDepth) {
  2471. clearDepth.framebuffer = passState.framebuffer;
  2472. clearDepth.execute(context, passState);
  2473. clearDepth.framebuffer = undefined;
  2474. }
  2475. if (clearGlobeDepth && useDepthPlane) {
  2476. depthPlane.execute(context, passState);
  2477. }
  2478. us.updatePass(Pass.CESIUM_3D_TILE);
  2479. commands = frustumCommands.commands[Pass.CESIUM_3D_TILE];
  2480. length = frustumCommands.indices[Pass.CESIUM_3D_TILE];
  2481. for (j = 0; j < length; ++j) {
  2482. executeIdCommand(commands[j], scene, context, passState);
  2483. }
  2484. us.updatePass(Pass.OPAQUE);
  2485. commands = frustumCommands.commands[Pass.OPAQUE];
  2486. length = frustumCommands.indices[Pass.OPAQUE];
  2487. for (j = 0; j < length; ++j) {
  2488. executeIdCommand(commands[j], scene, context, passState);
  2489. }
  2490. us.updatePass(Pass.TRANSLUCENT);
  2491. commands = frustumCommands.commands[Pass.TRANSLUCENT];
  2492. length = frustumCommands.indices[Pass.TRANSLUCENT];
  2493. for (j = 0; j < length; ++j) {
  2494. executeIdCommand(commands[j], scene, context, passState);
  2495. }
  2496. passState.framebuffer = originalFramebuffer;
  2497. }
  2498. }
  2499. function executeComputeCommands(scene) {
  2500. const us = scene.context.uniformState;
  2501. us.updatePass(Pass.COMPUTE);
  2502. const sunComputeCommand = scene._environmentState.sunComputeCommand;
  2503. if (defined(sunComputeCommand)) {
  2504. sunComputeCommand.execute(scene._computeEngine);
  2505. }
  2506. const commandList = scene._computeCommandList;
  2507. const length = commandList.length;
  2508. for (let i = 0; i < length; ++i) {
  2509. commandList[i].execute(scene._computeEngine);
  2510. }
  2511. }
  2512. function executeOverlayCommands(scene, passState) {
  2513. const us = scene.context.uniformState;
  2514. us.updatePass(Pass.OVERLAY);
  2515. const context = scene.context;
  2516. const commandList = scene._overlayCommandList;
  2517. const length = commandList.length;
  2518. for (let i = 0; i < length; ++i) {
  2519. commandList[i].execute(context, passState);
  2520. }
  2521. }
  2522. function insertShadowCastCommands(scene, commandList, shadowMap) {
  2523. const shadowVolume = shadowMap.shadowMapCullingVolume;
  2524. const isPointLight = shadowMap.isPointLight;
  2525. const passes = shadowMap.passes;
  2526. const numberOfPasses = passes.length;
  2527. const length = commandList.length;
  2528. for (let i = 0; i < length; ++i) {
  2529. const command = commandList[i];
  2530. scene.updateDerivedCommands(command);
  2531. if (
  2532. command.castShadows &&
  2533. (command.pass === Pass.GLOBE ||
  2534. command.pass === Pass.CESIUM_3D_TILE ||
  2535. command.pass === Pass.OPAQUE ||
  2536. command.pass === Pass.TRANSLUCENT)
  2537. ) {
  2538. if (scene.isVisible(command, shadowVolume)) {
  2539. if (isPointLight) {
  2540. for (let k = 0; k < numberOfPasses; ++k) {
  2541. passes[k].commandList.push(command);
  2542. }
  2543. } else if (numberOfPasses === 1) {
  2544. passes[0].commandList.push(command);
  2545. } else {
  2546. let wasVisible = false;
  2547. // Loop over cascades from largest to smallest
  2548. for (let j = numberOfPasses - 1; j >= 0; --j) {
  2549. const cascadeVolume = passes[j].cullingVolume;
  2550. if (scene.isVisible(command, cascadeVolume)) {
  2551. passes[j].commandList.push(command);
  2552. wasVisible = true;
  2553. } else if (wasVisible) {
  2554. // If it was visible in the previous cascade but now isn't
  2555. // then there is no need to check any more cascades
  2556. break;
  2557. }
  2558. }
  2559. }
  2560. }
  2561. }
  2562. }
  2563. }
  2564. function executeShadowMapCastCommands(scene) {
  2565. const frameState = scene.frameState;
  2566. const shadowMaps = frameState.shadowState.shadowMaps;
  2567. const shadowMapLength = shadowMaps.length;
  2568. if (!frameState.shadowState.shadowsEnabled) {
  2569. return;
  2570. }
  2571. const context = scene.context;
  2572. const uniformState = context.uniformState;
  2573. for (let i = 0; i < shadowMapLength; ++i) {
  2574. const shadowMap = shadowMaps[i];
  2575. if (shadowMap.outOfView) {
  2576. continue;
  2577. }
  2578. // Reset the command lists
  2579. const passes = shadowMap.passes;
  2580. const numberOfPasses = passes.length;
  2581. for (let j = 0; j < numberOfPasses; ++j) {
  2582. passes[j].commandList.length = 0;
  2583. }
  2584. // Insert the primitive/model commands into the command lists
  2585. const sceneCommands = scene.frameState.commandList;
  2586. insertShadowCastCommands(scene, sceneCommands, shadowMap);
  2587. for (let j = 0; j < numberOfPasses; ++j) {
  2588. const pass = shadowMap.passes[j];
  2589. uniformState.updateCamera(pass.camera);
  2590. shadowMap.updatePass(context, j);
  2591. const numberOfCommands = pass.commandList.length;
  2592. for (let k = 0; k < numberOfCommands; ++k) {
  2593. const command = pass.commandList[k];
  2594. // Set the correct pass before rendering into the shadow map because some shaders
  2595. // conditionally render based on whether the pass is translucent or opaque.
  2596. uniformState.updatePass(command.pass);
  2597. executeCommand(
  2598. command.derivedCommands.shadows.castCommands[i],
  2599. scene,
  2600. context,
  2601. pass.passState
  2602. );
  2603. }
  2604. }
  2605. }
  2606. }
  2607. const scratchEyeTranslation = new Cartesian3();
  2608. /**
  2609. * @private
  2610. */
  2611. Scene.prototype.updateAndExecuteCommands = function (
  2612. passState,
  2613. backgroundColor
  2614. ) {
  2615. const frameState = this._frameState;
  2616. const mode = frameState.mode;
  2617. const useWebVR = this._environmentState.useWebVR;
  2618. if (useWebVR) {
  2619. executeWebVRCommands(this, passState, backgroundColor);
  2620. } else if (
  2621. mode !== SceneMode.SCENE2D ||
  2622. this._mapMode2D === MapMode2D.ROTATE
  2623. ) {
  2624. executeCommandsInViewport(true, this, passState, backgroundColor);
  2625. } else {
  2626. updateAndClearFramebuffers(this, passState, backgroundColor);
  2627. execute2DViewportCommands(this, passState);
  2628. }
  2629. };
  2630. function executeWebVRCommands(scene, passState, backgroundColor) {
  2631. const view = scene._view;
  2632. const camera = view.camera;
  2633. const environmentState = scene._environmentState;
  2634. const renderTranslucentDepthForPick =
  2635. environmentState.renderTranslucentDepthForPick;
  2636. updateAndClearFramebuffers(scene, passState, backgroundColor);
  2637. updateAndRenderPrimitives(scene);
  2638. view.createPotentiallyVisibleSet(scene);
  2639. executeComputeCommands(scene);
  2640. if (!renderTranslucentDepthForPick) {
  2641. executeShadowMapCastCommands(scene);
  2642. }
  2643. // Based on Calculating Stereo pairs by Paul Bourke
  2644. // http://paulbourke.net/stereographics/stereorender/
  2645. const viewport = passState.viewport;
  2646. viewport.x = 0;
  2647. viewport.y = 0;
  2648. viewport.width = viewport.width * 0.5;
  2649. const savedCamera = Camera.clone(camera, scene._cameraVR);
  2650. savedCamera.frustum = camera.frustum;
  2651. const near = camera.frustum.near;
  2652. const fo = near * defaultValue(scene.focalLength, 5.0);
  2653. const eyeSeparation = defaultValue(scene.eyeSeparation, fo / 30.0);
  2654. const eyeTranslation = Cartesian3.multiplyByScalar(
  2655. savedCamera.right,
  2656. eyeSeparation * 0.5,
  2657. scratchEyeTranslation
  2658. );
  2659. camera.frustum.aspectRatio = viewport.width / viewport.height;
  2660. const offset = (0.5 * eyeSeparation * near) / fo;
  2661. Cartesian3.add(savedCamera.position, eyeTranslation, camera.position);
  2662. camera.frustum.xOffset = offset;
  2663. executeCommands(scene, passState);
  2664. viewport.x = viewport.width;
  2665. Cartesian3.subtract(savedCamera.position, eyeTranslation, camera.position);
  2666. camera.frustum.xOffset = -offset;
  2667. executeCommands(scene, passState);
  2668. Camera.clone(savedCamera, camera);
  2669. }
  2670. const scratch2DViewportCartographic = new Cartographic(
  2671. Math.PI,
  2672. CesiumMath.PI_OVER_TWO
  2673. );
  2674. const scratch2DViewportMaxCoord = new Cartesian3();
  2675. const scratch2DViewportSavedPosition = new Cartesian3();
  2676. const scratch2DViewportTransform = new Matrix4();
  2677. const scratch2DViewportCameraTransform = new Matrix4();
  2678. const scratch2DViewportEyePoint = new Cartesian3();
  2679. const scratch2DViewportWindowCoords = new Cartesian3();
  2680. const scratch2DViewport = new BoundingRectangle();
  2681. function execute2DViewportCommands(scene, passState) {
  2682. const context = scene.context;
  2683. const frameState = scene.frameState;
  2684. const camera = scene.camera;
  2685. const originalViewport = passState.viewport;
  2686. const viewport = BoundingRectangle.clone(originalViewport, scratch2DViewport);
  2687. passState.viewport = viewport;
  2688. const maxCartographic = scratch2DViewportCartographic;
  2689. const maxCoord = scratch2DViewportMaxCoord;
  2690. const projection = scene.mapProjection;
  2691. projection.project(maxCartographic, maxCoord);
  2692. const position = Cartesian3.clone(
  2693. camera.position,
  2694. scratch2DViewportSavedPosition
  2695. );
  2696. const transform = Matrix4.clone(
  2697. camera.transform,
  2698. scratch2DViewportCameraTransform
  2699. );
  2700. const frustum = camera.frustum.clone();
  2701. camera._setTransform(Matrix4.IDENTITY);
  2702. const viewportTransformation = Matrix4.computeViewportTransformation(
  2703. viewport,
  2704. 0.0,
  2705. 1.0,
  2706. scratch2DViewportTransform
  2707. );
  2708. const projectionMatrix = camera.frustum.projectionMatrix;
  2709. const x = camera.positionWC.y;
  2710. const eyePoint = Cartesian3.fromElements(
  2711. CesiumMath.sign(x) * maxCoord.x - x,
  2712. 0.0,
  2713. -camera.positionWC.x,
  2714. scratch2DViewportEyePoint
  2715. );
  2716. const windowCoordinates = Transforms.pointToGLWindowCoordinates(
  2717. projectionMatrix,
  2718. viewportTransformation,
  2719. eyePoint,
  2720. scratch2DViewportWindowCoords
  2721. );
  2722. windowCoordinates.x = Math.floor(windowCoordinates.x);
  2723. const viewportX = viewport.x;
  2724. const viewportWidth = viewport.width;
  2725. if (
  2726. x === 0.0 ||
  2727. windowCoordinates.x <= viewportX ||
  2728. windowCoordinates.x >= viewportX + viewportWidth
  2729. ) {
  2730. executeCommandsInViewport(true, scene, passState);
  2731. } else if (
  2732. Math.abs(viewportX + viewportWidth * 0.5 - windowCoordinates.x) < 1.0
  2733. ) {
  2734. viewport.width = windowCoordinates.x - viewport.x;
  2735. camera.position.x *= CesiumMath.sign(camera.position.x);
  2736. camera.frustum.right = 0.0;
  2737. frameState.cullingVolume = camera.frustum.computeCullingVolume(
  2738. camera.positionWC,
  2739. camera.directionWC,
  2740. camera.upWC
  2741. );
  2742. context.uniformState.update(frameState);
  2743. executeCommandsInViewport(true, scene, passState);
  2744. viewport.x = windowCoordinates.x;
  2745. camera.position.x = -camera.position.x;
  2746. camera.frustum.right = -camera.frustum.left;
  2747. camera.frustum.left = 0.0;
  2748. frameState.cullingVolume = camera.frustum.computeCullingVolume(
  2749. camera.positionWC,
  2750. camera.directionWC,
  2751. camera.upWC
  2752. );
  2753. context.uniformState.update(frameState);
  2754. executeCommandsInViewport(false, scene, passState);
  2755. } else if (windowCoordinates.x > viewportX + viewportWidth * 0.5) {
  2756. viewport.width = windowCoordinates.x - viewportX;
  2757. const right = camera.frustum.right;
  2758. camera.frustum.right = maxCoord.x - x;
  2759. frameState.cullingVolume = camera.frustum.computeCullingVolume(
  2760. camera.positionWC,
  2761. camera.directionWC,
  2762. camera.upWC
  2763. );
  2764. context.uniformState.update(frameState);
  2765. executeCommandsInViewport(true, scene, passState);
  2766. viewport.x = windowCoordinates.x;
  2767. viewport.width = viewportX + viewportWidth - windowCoordinates.x;
  2768. camera.position.x = -camera.position.x;
  2769. camera.frustum.left = -camera.frustum.right;
  2770. camera.frustum.right = right - camera.frustum.right * 2.0;
  2771. frameState.cullingVolume = camera.frustum.computeCullingVolume(
  2772. camera.positionWC,
  2773. camera.directionWC,
  2774. camera.upWC
  2775. );
  2776. context.uniformState.update(frameState);
  2777. executeCommandsInViewport(false, scene, passState);
  2778. } else {
  2779. viewport.x = windowCoordinates.x;
  2780. viewport.width = viewportX + viewportWidth - windowCoordinates.x;
  2781. const left = camera.frustum.left;
  2782. camera.frustum.left = -maxCoord.x - x;
  2783. frameState.cullingVolume = camera.frustum.computeCullingVolume(
  2784. camera.positionWC,
  2785. camera.directionWC,
  2786. camera.upWC
  2787. );
  2788. context.uniformState.update(frameState);
  2789. executeCommandsInViewport(true, scene, passState);
  2790. viewport.x = viewportX;
  2791. viewport.width = windowCoordinates.x - viewportX;
  2792. camera.position.x = -camera.position.x;
  2793. camera.frustum.right = -camera.frustum.left;
  2794. camera.frustum.left = left - camera.frustum.left * 2.0;
  2795. frameState.cullingVolume = camera.frustum.computeCullingVolume(
  2796. camera.positionWC,
  2797. camera.directionWC,
  2798. camera.upWC
  2799. );
  2800. context.uniformState.update(frameState);
  2801. executeCommandsInViewport(false, scene, passState);
  2802. }
  2803. camera._setTransform(transform);
  2804. Cartesian3.clone(position, camera.position);
  2805. camera.frustum = frustum.clone();
  2806. passState.viewport = originalViewport;
  2807. }
  2808. function executeCommandsInViewport(
  2809. firstViewport,
  2810. scene,
  2811. passState,
  2812. backgroundColor
  2813. ) {
  2814. const environmentState = scene._environmentState;
  2815. const view = scene._view;
  2816. const renderTranslucentDepthForPick =
  2817. environmentState.renderTranslucentDepthForPick;
  2818. if (!firstViewport) {
  2819. scene.frameState.commandList.length = 0;
  2820. }
  2821. updateAndRenderPrimitives(scene);
  2822. view.createPotentiallyVisibleSet(scene);
  2823. if (firstViewport) {
  2824. if (defined(backgroundColor)) {
  2825. updateAndClearFramebuffers(scene, passState, backgroundColor);
  2826. }
  2827. executeComputeCommands(scene);
  2828. if (!renderTranslucentDepthForPick) {
  2829. executeShadowMapCastCommands(scene);
  2830. }
  2831. }
  2832. executeCommands(scene, passState);
  2833. }
  2834. const scratchCullingVolume = new CullingVolume();
  2835. /**
  2836. * @private
  2837. */
  2838. Scene.prototype.updateEnvironment = function () {
  2839. const frameState = this._frameState;
  2840. const view = this._view;
  2841. // Update celestial and terrestrial environment effects.
  2842. const environmentState = this._environmentState;
  2843. const renderPass = frameState.passes.render;
  2844. const offscreenPass = frameState.passes.offscreen;
  2845. const skyAtmosphere = this.skyAtmosphere;
  2846. const globe = this.globe;
  2847. const globeTranslucencyState = this._globeTranslucencyState;
  2848. if (
  2849. !renderPass ||
  2850. (this._mode !== SceneMode.SCENE2D &&
  2851. view.camera.frustum instanceof OrthographicFrustum) ||
  2852. !globeTranslucencyState.environmentVisible
  2853. ) {
  2854. environmentState.skyAtmosphereCommand = undefined;
  2855. environmentState.skyBoxCommand = undefined;
  2856. environmentState.sunDrawCommand = undefined;
  2857. environmentState.sunComputeCommand = undefined;
  2858. environmentState.moonCommand = undefined;
  2859. } else {
  2860. if (defined(skyAtmosphere)) {
  2861. if (defined(globe)) {
  2862. skyAtmosphere.setDynamicAtmosphereColor(
  2863. globe.enableLighting && globe.dynamicAtmosphereLighting,
  2864. globe.dynamicAtmosphereLightingFromSun
  2865. );
  2866. environmentState.isReadyForAtmosphere =
  2867. environmentState.isReadyForAtmosphere ||
  2868. globe._surface._tilesToRender.length > 0;
  2869. }
  2870. environmentState.skyAtmosphereCommand = skyAtmosphere.update(
  2871. frameState,
  2872. globe
  2873. );
  2874. if (defined(environmentState.skyAtmosphereCommand)) {
  2875. this.updateDerivedCommands(environmentState.skyAtmosphereCommand);
  2876. }
  2877. } else {
  2878. environmentState.skyAtmosphereCommand = undefined;
  2879. }
  2880. environmentState.skyBoxCommand = defined(this.skyBox)
  2881. ? this.skyBox.update(frameState, this._hdr)
  2882. : undefined;
  2883. const sunCommands = defined(this.sun)
  2884. ? this.sun.update(frameState, view.passState, this._hdr)
  2885. : undefined;
  2886. environmentState.sunDrawCommand = defined(sunCommands)
  2887. ? sunCommands.drawCommand
  2888. : undefined;
  2889. environmentState.sunComputeCommand = defined(sunCommands)
  2890. ? sunCommands.computeCommand
  2891. : undefined;
  2892. environmentState.moonCommand = defined(this.moon)
  2893. ? this.moon.update(frameState)
  2894. : undefined;
  2895. }
  2896. const clearGlobeDepth = (environmentState.clearGlobeDepth =
  2897. defined(globe) &&
  2898. globe.show &&
  2899. (!globe.depthTestAgainstTerrain || this.mode === SceneMode.SCENE2D));
  2900. const useDepthPlane = (environmentState.useDepthPlane =
  2901. clearGlobeDepth &&
  2902. this.mode === SceneMode.SCENE3D &&
  2903. globeTranslucencyState.useDepthPlane);
  2904. if (useDepthPlane) {
  2905. // Update the depth plane that is rendered in 3D when the primitives are
  2906. // not depth tested against terrain so primitives on the backface
  2907. // of the globe are not picked.
  2908. this._depthPlane.update(frameState);
  2909. }
  2910. environmentState.renderTranslucentDepthForPick = false;
  2911. environmentState.useWebVR =
  2912. this._useWebVR && this.mode !== SceneMode.SCENE2D && !offscreenPass;
  2913. const occluder =
  2914. frameState.mode === SceneMode.SCENE3D &&
  2915. !globeTranslucencyState.sunVisibleThroughGlobe
  2916. ? frameState.occluder
  2917. : undefined;
  2918. let cullingVolume = frameState.cullingVolume;
  2919. // get user culling volume minus the far plane.
  2920. const planes = scratchCullingVolume.planes;
  2921. for (let k = 0; k < 5; ++k) {
  2922. planes[k] = cullingVolume.planes[k];
  2923. }
  2924. cullingVolume = scratchCullingVolume;
  2925. // Determine visibility of celestial and terrestrial environment effects.
  2926. environmentState.isSkyAtmosphereVisible =
  2927. defined(environmentState.skyAtmosphereCommand) &&
  2928. environmentState.isReadyForAtmosphere;
  2929. environmentState.isSunVisible = this.isVisible(
  2930. environmentState.sunDrawCommand,
  2931. cullingVolume,
  2932. occluder
  2933. );
  2934. environmentState.isMoonVisible = this.isVisible(
  2935. environmentState.moonCommand,
  2936. cullingVolume,
  2937. occluder
  2938. );
  2939. const envMaps = this.specularEnvironmentMaps;
  2940. let envMapAtlas = this._specularEnvironmentMapAtlas;
  2941. if (
  2942. defined(envMaps) &&
  2943. (!defined(envMapAtlas) || envMapAtlas.url !== envMaps)
  2944. ) {
  2945. envMapAtlas = envMapAtlas && envMapAtlas.destroy();
  2946. this._specularEnvironmentMapAtlas = new OctahedralProjectedCubeMap(envMaps);
  2947. } else if (!defined(envMaps) && defined(envMapAtlas)) {
  2948. envMapAtlas.destroy();
  2949. this._specularEnvironmentMapAtlas = undefined;
  2950. }
  2951. if (defined(this._specularEnvironmentMapAtlas)) {
  2952. this._specularEnvironmentMapAtlas.update(frameState);
  2953. }
  2954. };
  2955. function updateDebugFrustumPlanes(scene) {
  2956. const frameState = scene._frameState;
  2957. if (scene.debugShowFrustumPlanes !== scene._debugShowFrustumPlanes) {
  2958. if (scene.debugShowFrustumPlanes) {
  2959. scene._debugFrustumPlanes = new DebugCameraPrimitive({
  2960. camera: scene.camera,
  2961. updateOnChange: false,
  2962. frustumSplits: frameState.frustumSplits,
  2963. });
  2964. } else {
  2965. scene._debugFrustumPlanes =
  2966. scene._debugFrustumPlanes && scene._debugFrustumPlanes.destroy();
  2967. }
  2968. scene._debugShowFrustumPlanes = scene.debugShowFrustumPlanes;
  2969. }
  2970. if (defined(scene._debugFrustumPlanes)) {
  2971. scene._debugFrustumPlanes.update(frameState);
  2972. }
  2973. }
  2974. function updateShadowMaps(scene) {
  2975. const frameState = scene._frameState;
  2976. const shadowMaps = frameState.shadowMaps;
  2977. const length = shadowMaps.length;
  2978. const shadowsEnabled =
  2979. length > 0 && !frameState.passes.pick && scene.mode === SceneMode.SCENE3D;
  2980. if (shadowsEnabled !== frameState.shadowState.shadowsEnabled) {
  2981. // Update derived commands when shadowsEnabled changes
  2982. ++frameState.shadowState.lastDirtyTime;
  2983. frameState.shadowState.shadowsEnabled = shadowsEnabled;
  2984. }
  2985. frameState.shadowState.lightShadowsEnabled = false;
  2986. if (!shadowsEnabled) {
  2987. return;
  2988. }
  2989. // Check if the shadow maps are different than the shadow maps last frame.
  2990. // If so, the derived commands need to be updated.
  2991. for (let j = 0; j < length; ++j) {
  2992. if (shadowMaps[j] !== frameState.shadowState.shadowMaps[j]) {
  2993. ++frameState.shadowState.lastDirtyTime;
  2994. break;
  2995. }
  2996. }
  2997. frameState.shadowState.shadowMaps.length = 0;
  2998. frameState.shadowState.lightShadowMaps.length = 0;
  2999. for (let i = 0; i < length; ++i) {
  3000. const shadowMap = shadowMaps[i];
  3001. shadowMap.update(frameState);
  3002. frameState.shadowState.shadowMaps.push(shadowMap);
  3003. if (shadowMap.fromLightSource) {
  3004. frameState.shadowState.lightShadowMaps.push(shadowMap);
  3005. frameState.shadowState.lightShadowsEnabled = true;
  3006. }
  3007. if (shadowMap.dirty) {
  3008. ++frameState.shadowState.lastDirtyTime;
  3009. shadowMap.dirty = false;
  3010. }
  3011. }
  3012. }
  3013. function updateAndRenderPrimitives(scene) {
  3014. const frameState = scene._frameState;
  3015. scene._groundPrimitives.update(frameState);
  3016. scene._primitives.update(frameState);
  3017. updateDebugFrustumPlanes(scene);
  3018. updateShadowMaps(scene);
  3019. if (scene._globe) {
  3020. scene._globe.render(frameState);
  3021. }
  3022. }
  3023. function updateAndClearFramebuffers(scene, passState, clearColor) {
  3024. const context = scene._context;
  3025. const frameState = scene._frameState;
  3026. const environmentState = scene._environmentState;
  3027. const view = scene._view;
  3028. const passes = scene._frameState.passes;
  3029. const picking = passes.pick;
  3030. if (defined(view.globeDepth)) {
  3031. view.globeDepth.picking = picking;
  3032. }
  3033. const useWebVR = environmentState.useWebVR;
  3034. // Preserve the reference to the original framebuffer.
  3035. environmentState.originalFramebuffer = passState.framebuffer;
  3036. // Manage sun bloom post-processing effect.
  3037. if (defined(scene.sun) && scene.sunBloom !== scene._sunBloom) {
  3038. if (scene.sunBloom && !useWebVR) {
  3039. scene._sunPostProcess = new SunPostProcess();
  3040. } else if (defined(scene._sunPostProcess)) {
  3041. scene._sunPostProcess = scene._sunPostProcess.destroy();
  3042. }
  3043. scene._sunBloom = scene.sunBloom;
  3044. } else if (!defined(scene.sun) && defined(scene._sunPostProcess)) {
  3045. scene._sunPostProcess = scene._sunPostProcess.destroy();
  3046. scene._sunBloom = false;
  3047. }
  3048. // Clear the pass state framebuffer.
  3049. const clear = scene._clearColorCommand;
  3050. Color.clone(clearColor, clear.color);
  3051. clear.execute(context, passState);
  3052. // Update globe depth rendering based on the current context and clear the globe depth framebuffer.
  3053. // Globe depth is copied for the pick pass to support picking batched geometries in GroundPrimitives.
  3054. const useGlobeDepthFramebuffer = (environmentState.useGlobeDepthFramebuffer = defined(
  3055. view.globeDepth
  3056. ));
  3057. if (useGlobeDepthFramebuffer) {
  3058. view.globeDepth.update(
  3059. context,
  3060. passState,
  3061. view.viewport,
  3062. scene.msaaSamples,
  3063. scene._hdr,
  3064. environmentState.clearGlobeDepth
  3065. );
  3066. view.globeDepth.clear(context, passState, clearColor);
  3067. }
  3068. // If supported, configure OIT to use the globe depth framebuffer and clear the OIT framebuffer.
  3069. const oit = view.oit;
  3070. const useOIT = (environmentState.useOIT =
  3071. !picking && defined(oit) && oit.isSupported());
  3072. if (useOIT) {
  3073. oit.update(
  3074. context,
  3075. passState,
  3076. view.globeDepth.colorFramebufferManager,
  3077. scene._hdr,
  3078. scene.msaaSamples
  3079. );
  3080. oit.clear(context, passState, clearColor);
  3081. environmentState.useOIT = oit.isSupported();
  3082. }
  3083. const postProcess = scene.postProcessStages;
  3084. let usePostProcess = (environmentState.usePostProcess =
  3085. !picking &&
  3086. (scene._hdr ||
  3087. postProcess.length > 0 ||
  3088. postProcess.ambientOcclusion.enabled ||
  3089. postProcess.fxaa.enabled ||
  3090. postProcess.bloom.enabled));
  3091. environmentState.usePostProcessSelected = false;
  3092. if (usePostProcess) {
  3093. view.sceneFramebuffer.update(
  3094. context,
  3095. view.viewport,
  3096. scene._hdr,
  3097. scene.msaaSamples
  3098. );
  3099. view.sceneFramebuffer.clear(context, passState, clearColor);
  3100. postProcess.update(context, frameState.useLogDepth, scene._hdr);
  3101. postProcess.clear(context);
  3102. usePostProcess = environmentState.usePostProcess = postProcess.ready;
  3103. environmentState.usePostProcessSelected =
  3104. usePostProcess && postProcess.hasSelected;
  3105. }
  3106. if (environmentState.isSunVisible && scene.sunBloom && !useWebVR) {
  3107. passState.framebuffer = scene._sunPostProcess.update(passState);
  3108. scene._sunPostProcess.clear(context, passState, clearColor);
  3109. } else if (useGlobeDepthFramebuffer) {
  3110. passState.framebuffer = view.globeDepth.framebuffer;
  3111. } else if (usePostProcess) {
  3112. passState.framebuffer = view.sceneFramebuffer.framebuffer;
  3113. }
  3114. if (defined(passState.framebuffer)) {
  3115. clear.execute(context, passState);
  3116. }
  3117. const useInvertClassification = (environmentState.useInvertClassification =
  3118. !picking && defined(passState.framebuffer) && scene.invertClassification);
  3119. if (useInvertClassification) {
  3120. let depthFramebuffer;
  3121. if (scene.frameState.invertClassificationColor.alpha === 1.0) {
  3122. if (environmentState.useGlobeDepthFramebuffer) {
  3123. depthFramebuffer = view.globeDepth.framebuffer;
  3124. }
  3125. }
  3126. if (defined(depthFramebuffer) || context.depthTexture) {
  3127. scene._invertClassification.previousFramebuffer = depthFramebuffer;
  3128. scene._invertClassification.update(
  3129. context,
  3130. scene.msaaSamples,
  3131. view.globeDepth.colorFramebufferManager
  3132. );
  3133. scene._invertClassification.clear(context, passState);
  3134. if (scene.frameState.invertClassificationColor.alpha < 1.0 && useOIT) {
  3135. const command = scene._invertClassification.unclassifiedCommand;
  3136. const derivedCommands = command.derivedCommands;
  3137. derivedCommands.oit = oit.createDerivedCommands(
  3138. command,
  3139. context,
  3140. derivedCommands.oit
  3141. );
  3142. }
  3143. } else {
  3144. environmentState.useInvertClassification = false;
  3145. }
  3146. }
  3147. if (scene._globeTranslucencyState.translucent) {
  3148. view.globeTranslucencyFramebuffer.updateAndClear(
  3149. scene._hdr,
  3150. view.viewport,
  3151. context,
  3152. passState
  3153. );
  3154. }
  3155. }
  3156. /**
  3157. * @private
  3158. */
  3159. Scene.prototype.resolveFramebuffers = function (passState) {
  3160. const context = this._context;
  3161. const environmentState = this._environmentState;
  3162. const view = this._view;
  3163. const globeDepth = view.globeDepth;
  3164. if (defined(globeDepth)) {
  3165. globeDepth.prepareColorTextures(context);
  3166. }
  3167. const useOIT = environmentState.useOIT;
  3168. const useGlobeDepthFramebuffer = environmentState.useGlobeDepthFramebuffer;
  3169. const usePostProcess = environmentState.usePostProcess;
  3170. const defaultFramebuffer = environmentState.originalFramebuffer;
  3171. const globeFramebuffer = useGlobeDepthFramebuffer
  3172. ? globeDepth.colorFramebufferManager
  3173. : undefined;
  3174. const sceneFramebuffer = view.sceneFramebuffer._colorFramebuffer;
  3175. const idFramebuffer = view.sceneFramebuffer.idFramebuffer;
  3176. if (useOIT) {
  3177. passState.framebuffer = usePostProcess
  3178. ? sceneFramebuffer.framebuffer
  3179. : defaultFramebuffer;
  3180. view.oit.execute(context, passState);
  3181. }
  3182. const translucentTileClassification = view.translucentTileClassification;
  3183. if (
  3184. translucentTileClassification.hasTranslucentDepth &&
  3185. translucentTileClassification.isSupported()
  3186. ) {
  3187. translucentTileClassification.execute(this, passState);
  3188. }
  3189. if (usePostProcess) {
  3190. view.sceneFramebuffer.prepareColorTextures(context);
  3191. let inputFramebuffer = sceneFramebuffer;
  3192. if (useGlobeDepthFramebuffer && !useOIT) {
  3193. inputFramebuffer = globeFramebuffer;
  3194. }
  3195. const postProcess = this.postProcessStages;
  3196. const colorTexture = inputFramebuffer.getColorTexture(0);
  3197. const idTexture = idFramebuffer.getColorTexture(0);
  3198. const depthTexture = defaultValue(
  3199. globeFramebuffer,
  3200. sceneFramebuffer
  3201. ).getDepthStencilTexture();
  3202. postProcess.execute(context, colorTexture, depthTexture, idTexture);
  3203. postProcess.copy(context, defaultFramebuffer);
  3204. }
  3205. if (!useOIT && !usePostProcess && useGlobeDepthFramebuffer) {
  3206. passState.framebuffer = defaultFramebuffer;
  3207. globeDepth.executeCopyColor(context, passState);
  3208. }
  3209. };
  3210. function callAfterRenderFunctions(scene) {
  3211. // Functions are queued up during primitive update and executed here in case
  3212. // the function modifies scene state that should remain constant over the frame.
  3213. const functions = scene._frameState.afterRender;
  3214. for (let i = 0, length = functions.length; i < length; ++i) {
  3215. functions[i]();
  3216. scene.requestRender();
  3217. }
  3218. functions.length = 0;
  3219. }
  3220. function getGlobeHeight(scene) {
  3221. const globe = scene._globe;
  3222. const camera = scene.camera;
  3223. const cartographic = camera.positionCartographic;
  3224. if (defined(globe) && globe.show && defined(cartographic)) {
  3225. return globe.getHeight(cartographic);
  3226. }
  3227. return undefined;
  3228. }
  3229. function isCameraUnderground(scene) {
  3230. const camera = scene.camera;
  3231. const mode = scene._mode;
  3232. const globe = scene.globe;
  3233. const cameraController = scene._screenSpaceCameraController;
  3234. const cartographic = camera.positionCartographic;
  3235. if (!defined(cartographic)) {
  3236. return false;
  3237. }
  3238. if (!cameraController.onMap() && cartographic.height < 0.0) {
  3239. // The camera can go off the map while in Columbus View.
  3240. // Make a best guess as to whether it's underground by checking if its height is less than zero.
  3241. return true;
  3242. }
  3243. if (
  3244. !defined(globe) ||
  3245. !globe.show ||
  3246. mode === SceneMode.SCENE2D ||
  3247. mode === SceneMode.MORPHING
  3248. ) {
  3249. return false;
  3250. }
  3251. const globeHeight = scene._globeHeight;
  3252. return defined(globeHeight) && cartographic.height < globeHeight;
  3253. }
  3254. /**
  3255. * @private
  3256. */
  3257. Scene.prototype.initializeFrame = function () {
  3258. // Destroy released shaders and textures once every 120 frames to avoid thrashing the cache
  3259. if (this._shaderFrameCount++ === 120) {
  3260. this._shaderFrameCount = 0;
  3261. this._context.shaderCache.destroyReleasedShaderPrograms();
  3262. this._context.textureCache.destroyReleasedTextures();
  3263. }
  3264. this._tweens.update();
  3265. this._globeHeight = getGlobeHeight(this);
  3266. this._cameraUnderground = isCameraUnderground(this);
  3267. this._globeTranslucencyState.update(this);
  3268. this._screenSpaceCameraController.update();
  3269. if (defined(this._deviceOrientationCameraController)) {
  3270. this._deviceOrientationCameraController.update();
  3271. }
  3272. this.camera.update(this._mode);
  3273. this.camera._updateCameraChanged();
  3274. };
  3275. function updateDebugShowFramesPerSecond(scene, renderedThisFrame) {
  3276. if (scene.debugShowFramesPerSecond) {
  3277. if (!defined(scene._performanceDisplay)) {
  3278. const performanceContainer = document.createElement("div");
  3279. performanceContainer.className =
  3280. "cesium-performanceDisplay-defaultContainer";
  3281. const container = scene._canvas.parentNode;
  3282. container.appendChild(performanceContainer);
  3283. const performanceDisplay = new PerformanceDisplay({
  3284. container: performanceContainer,
  3285. });
  3286. scene._performanceDisplay = performanceDisplay;
  3287. scene._performanceContainer = performanceContainer;
  3288. }
  3289. scene._performanceDisplay.throttled = scene.requestRenderMode;
  3290. scene._performanceDisplay.update(renderedThisFrame);
  3291. } else if (defined(scene._performanceDisplay)) {
  3292. scene._performanceDisplay =
  3293. scene._performanceDisplay && scene._performanceDisplay.destroy();
  3294. scene._performanceContainer.parentNode.removeChild(
  3295. scene._performanceContainer
  3296. );
  3297. }
  3298. }
  3299. function prePassesUpdate(scene) {
  3300. scene._jobScheduler.resetBudgets();
  3301. const frameState = scene._frameState;
  3302. const primitives = scene.primitives;
  3303. primitives.prePassesUpdate(frameState);
  3304. if (defined(scene.globe)) {
  3305. scene.globe.update(frameState);
  3306. }
  3307. scene._picking.update();
  3308. frameState.creditDisplay.update();
  3309. }
  3310. function postPassesUpdate(scene) {
  3311. const frameState = scene._frameState;
  3312. const primitives = scene.primitives;
  3313. primitives.postPassesUpdate(frameState);
  3314. RequestScheduler.update();
  3315. }
  3316. const scratchBackgroundColor = new Color();
  3317. function render(scene) {
  3318. const frameState = scene._frameState;
  3319. const context = scene.context;
  3320. const us = context.uniformState;
  3321. const view = scene._defaultView;
  3322. scene._view = view;
  3323. scene.updateFrameState();
  3324. frameState.passes.render = true;
  3325. frameState.passes.postProcess = scene.postProcessStages.hasSelected;
  3326. frameState.tilesetPassState = renderTilesetPassState;
  3327. let backgroundColor = defaultValue(scene.backgroundColor, Color.BLACK);
  3328. if (scene._hdr) {
  3329. backgroundColor = Color.clone(backgroundColor, scratchBackgroundColor);
  3330. backgroundColor.red = Math.pow(backgroundColor.red, scene.gamma);
  3331. backgroundColor.green = Math.pow(backgroundColor.green, scene.gamma);
  3332. backgroundColor.blue = Math.pow(backgroundColor.blue, scene.gamma);
  3333. }
  3334. frameState.backgroundColor = backgroundColor;
  3335. scene.fog.update(frameState);
  3336. us.update(frameState);
  3337. const shadowMap = scene.shadowMap;
  3338. if (defined(shadowMap) && shadowMap.enabled) {
  3339. if (!defined(scene.light) || scene.light instanceof SunLight) {
  3340. // Negate the sun direction so that it is from the Sun, not to the Sun
  3341. Cartesian3.negate(us.sunDirectionWC, scene._shadowMapCamera.direction);
  3342. } else {
  3343. Cartesian3.clone(scene.light.direction, scene._shadowMapCamera.direction);
  3344. }
  3345. frameState.shadowMaps.push(shadowMap);
  3346. }
  3347. scene._computeCommandList.length = 0;
  3348. scene._overlayCommandList.length = 0;
  3349. const viewport = view.viewport;
  3350. viewport.x = 0;
  3351. viewport.y = 0;
  3352. viewport.width = context.drawingBufferWidth;
  3353. viewport.height = context.drawingBufferHeight;
  3354. const passState = view.passState;
  3355. passState.framebuffer = undefined;
  3356. passState.blendingEnabled = undefined;
  3357. passState.scissorTest = undefined;
  3358. passState.viewport = BoundingRectangle.clone(viewport, passState.viewport);
  3359. if (defined(scene.globe)) {
  3360. scene.globe.beginFrame(frameState);
  3361. }
  3362. scene.updateEnvironment();
  3363. scene.updateAndExecuteCommands(passState, backgroundColor);
  3364. scene.resolveFramebuffers(passState);
  3365. passState.framebuffer = undefined;
  3366. executeOverlayCommands(scene, passState);
  3367. if (defined(scene.globe)) {
  3368. scene.globe.endFrame(frameState);
  3369. if (!scene.globe.tilesLoaded) {
  3370. scene._renderRequested = true;
  3371. }
  3372. }
  3373. context.endFrame();
  3374. }
  3375. function tryAndCatchError(scene, functionToExecute) {
  3376. try {
  3377. functionToExecute(scene);
  3378. } catch (error) {
  3379. scene._renderError.raiseEvent(scene, error);
  3380. if (scene.rethrowRenderErrors) {
  3381. throw error;
  3382. }
  3383. }
  3384. }
  3385. function updateMostDetailedRayPicks(scene) {
  3386. return scene._picking.updateMostDetailedRayPicks(scene);
  3387. }
  3388. /**
  3389. * Update and render the scene. It is usually not necessary to call this function
  3390. * directly because {@link CesiumWidget} or {@link Viewer} do it automatically.
  3391. * @param {JulianDate} [time] The simulation time at which to render.
  3392. */
  3393. Scene.prototype.render = function (time) {
  3394. /**
  3395. *
  3396. * Pre passes update. Execute any pass invariant code that should run before the passes here.
  3397. *
  3398. */
  3399. this._preUpdate.raiseEvent(this, time);
  3400. const frameState = this._frameState;
  3401. frameState.newFrame = false;
  3402. if (!defined(time)) {
  3403. time = JulianDate.now();
  3404. }
  3405. // Determine if shouldRender
  3406. const cameraChanged = this._view.checkForCameraUpdates(this);
  3407. let shouldRender =
  3408. !this.requestRenderMode ||
  3409. this._renderRequested ||
  3410. cameraChanged ||
  3411. this._logDepthBufferDirty ||
  3412. this._hdrDirty ||
  3413. this.mode === SceneMode.MORPHING;
  3414. if (
  3415. !shouldRender &&
  3416. defined(this.maximumRenderTimeChange) &&
  3417. defined(this._lastRenderTime)
  3418. ) {
  3419. const difference = Math.abs(
  3420. JulianDate.secondsDifference(this._lastRenderTime, time)
  3421. );
  3422. shouldRender = shouldRender || difference > this.maximumRenderTimeChange;
  3423. }
  3424. if (shouldRender) {
  3425. this._lastRenderTime = JulianDate.clone(time, this._lastRenderTime);
  3426. this._renderRequested = false;
  3427. this._logDepthBufferDirty = false;
  3428. this._hdrDirty = false;
  3429. const frameNumber = CesiumMath.incrementWrap(
  3430. frameState.frameNumber,
  3431. 15000000.0,
  3432. 1.0
  3433. );
  3434. updateFrameNumber(this, frameNumber, time);
  3435. frameState.newFrame = true;
  3436. }
  3437. tryAndCatchError(this, prePassesUpdate);
  3438. /**
  3439. *
  3440. * Passes update. Add any passes here
  3441. *
  3442. */
  3443. if (this.primitives.show) {
  3444. tryAndCatchError(this, updateMostDetailedRayPicks);
  3445. tryAndCatchError(this, updatePreloadPass);
  3446. tryAndCatchError(this, updatePreloadFlightPass);
  3447. if (!shouldRender) {
  3448. tryAndCatchError(this, updateRequestRenderModeDeferCheckPass);
  3449. }
  3450. }
  3451. this._postUpdate.raiseEvent(this, time);
  3452. if (shouldRender) {
  3453. this._preRender.raiseEvent(this, time);
  3454. frameState.creditDisplay.beginFrame();
  3455. tryAndCatchError(this, render);
  3456. }
  3457. /**
  3458. *
  3459. * Post passes update. Execute any pass invariant code that should run after the passes here.
  3460. *
  3461. */
  3462. updateDebugShowFramesPerSecond(this, shouldRender);
  3463. tryAndCatchError(this, postPassesUpdate);
  3464. // Often used to trigger events (so don't want in trycatch) that the user might be subscribed to. Things like the tile load events, ready promises, etc.
  3465. // We don't want those events to resolve during the render loop because the events might add new primitives
  3466. callAfterRenderFunctions(this);
  3467. if (shouldRender) {
  3468. this._postRender.raiseEvent(this, time);
  3469. frameState.creditDisplay.endFrame();
  3470. }
  3471. };
  3472. /**
  3473. * Update and render the scene. Always forces a new render frame regardless of whether a render was
  3474. * previously requested.
  3475. * @param {JulianDate} [time] The simulation time at which to render.
  3476. *
  3477. * @private
  3478. */
  3479. Scene.prototype.forceRender = function (time) {
  3480. this._renderRequested = true;
  3481. this.render(time);
  3482. };
  3483. /**
  3484. * Requests a new rendered frame when {@link Scene#requestRenderMode} is set to <code>true</code>.
  3485. * The render rate will not exceed the {@link CesiumWidget#targetFrameRate}.
  3486. *
  3487. * @see Scene#requestRenderMode
  3488. */
  3489. Scene.prototype.requestRender = function () {
  3490. this._renderRequested = true;
  3491. };
  3492. /**
  3493. * @private
  3494. */
  3495. Scene.prototype.clampLineWidth = function (width) {
  3496. return Math.max(
  3497. ContextLimits.minimumAliasedLineWidth,
  3498. Math.min(width, ContextLimits.maximumAliasedLineWidth)
  3499. );
  3500. };
  3501. /**
  3502. * Returns an object with a `primitive` property that contains the first (top) primitive in the scene
  3503. * at a particular window coordinate or undefined if nothing is at the location. Other properties may
  3504. * potentially be set depending on the type of primitive and may be used to further identify the picked object.
  3505. * <p>
  3506. * When a feature of a 3D Tiles tileset is picked, <code>pick</code> returns a {@link Cesium3DTileFeature} object.
  3507. * </p>
  3508. *
  3509. * @example
  3510. * // On mouse over, color the feature yellow.
  3511. * handler.setInputAction(function(movement) {
  3512. * const feature = scene.pick(movement.endPosition);
  3513. * if (feature instanceof Cesium.Cesium3DTileFeature) {
  3514. * feature.color = Cesium.Color.YELLOW;
  3515. * }
  3516. * }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  3517. *
  3518. * @param {Cartesian2} windowPosition Window coordinates to perform picking on.
  3519. * @param {Number} [width=3] Width of the pick rectangle.
  3520. * @param {Number} [height=3] Height of the pick rectangle.
  3521. * @returns {Object} Object containing the picked primitive.
  3522. */
  3523. Scene.prototype.pick = function (windowPosition, width, height) {
  3524. return this._picking.pick(this, windowPosition, width, height);
  3525. };
  3526. /**
  3527. * Returns the cartesian position reconstructed from the depth buffer and window position.
  3528. * The returned position is in world coordinates. Used internally by camera functions to
  3529. * prevent conversion to projected 2D coordinates and then back.
  3530. * <p>
  3531. * Set {@link Scene#pickTranslucentDepth} to <code>true</code> to include the depth of
  3532. * translucent primitives; otherwise, this essentially picks through translucent primitives.
  3533. * </p>
  3534. *
  3535. * @private
  3536. *
  3537. * @param {Cartesian2} windowPosition Window coordinates to perform picking on.
  3538. * @param {Cartesian3} [result] The object on which to restore the result.
  3539. * @returns {Cartesian3} The cartesian position in world coordinates.
  3540. *
  3541. * @exception {DeveloperError} Picking from the depth buffer is not supported. Check pickPositionSupported.
  3542. */
  3543. Scene.prototype.pickPositionWorldCoordinates = function (
  3544. windowPosition,
  3545. result
  3546. ) {
  3547. return this._picking.pickPositionWorldCoordinates(
  3548. this,
  3549. windowPosition,
  3550. result
  3551. );
  3552. };
  3553. /**
  3554. * Returns the cartesian position reconstructed from the depth buffer and window position.
  3555. * <p>
  3556. * The position reconstructed from the depth buffer in 2D may be slightly different from those
  3557. * reconstructed in 3D and Columbus view. This is caused by the difference in the distribution
  3558. * of depth values of perspective and orthographic projection.
  3559. * </p>
  3560. * <p>
  3561. * Set {@link Scene#pickTranslucentDepth} to <code>true</code> to include the depth of
  3562. * translucent primitives; otherwise, this essentially picks through translucent primitives.
  3563. * </p>
  3564. *
  3565. * @param {Cartesian2} windowPosition Window coordinates to perform picking on.
  3566. * @param {Cartesian3} [result] The object on which to restore the result.
  3567. * @returns {Cartesian3} The cartesian position.
  3568. *
  3569. * @exception {DeveloperError} Picking from the depth buffer is not supported. Check pickPositionSupported.
  3570. */
  3571. Scene.prototype.pickPosition = function (windowPosition, result) {
  3572. return this._picking.pickPosition(this, windowPosition, result);
  3573. };
  3574. /**
  3575. * Returns a list of objects, each containing a `primitive` property, for all primitives at
  3576. * a particular window coordinate position. Other properties may also be set depending on the
  3577. * type of primitive and may be used to further identify the picked object. The primitives in
  3578. * the list are ordered by their visual order in the scene (front to back).
  3579. *
  3580. * @param {Cartesian2} windowPosition Window coordinates to perform picking on.
  3581. * @param {Number} [limit] If supplied, stop drilling after collecting this many picks.
  3582. * @param {Number} [width=3] Width of the pick rectangle.
  3583. * @param {Number} [height=3] Height of the pick rectangle.
  3584. * @returns {Array.<*>} Array of objects, each containing 1 picked primitives.
  3585. *
  3586. * @exception {DeveloperError} windowPosition is undefined.
  3587. *
  3588. * @example
  3589. * const pickedObjects = scene.drillPick(new Cesium.Cartesian2(100.0, 200.0));
  3590. *
  3591. * @see Scene#pick
  3592. */
  3593. Scene.prototype.drillPick = function (windowPosition, limit, width, height) {
  3594. return this._picking.drillPick(this, windowPosition, limit, width, height);
  3595. };
  3596. function updatePreloadPass(scene) {
  3597. const frameState = scene._frameState;
  3598. preloadTilesetPassState.camera = frameState.camera;
  3599. preloadTilesetPassState.cullingVolume = frameState.cullingVolume;
  3600. const primitives = scene.primitives;
  3601. primitives.updateForPass(frameState, preloadTilesetPassState);
  3602. }
  3603. function updatePreloadFlightPass(scene) {
  3604. const frameState = scene._frameState;
  3605. const camera = frameState.camera;
  3606. if (!camera.canPreloadFlight()) {
  3607. return;
  3608. }
  3609. preloadFlightTilesetPassState.camera = scene.preloadFlightCamera;
  3610. preloadFlightTilesetPassState.cullingVolume =
  3611. scene.preloadFlightCullingVolume;
  3612. const primitives = scene.primitives;
  3613. primitives.updateForPass(frameState, preloadFlightTilesetPassState);
  3614. }
  3615. function updateRequestRenderModeDeferCheckPass(scene) {
  3616. // Check if any ignored requests are ready to go (to wake rendering up again)
  3617. scene.primitives.updateForPass(
  3618. scene._frameState,
  3619. requestRenderModeDeferCheckPassState
  3620. );
  3621. }
  3622. /**
  3623. * Returns an object containing the first object intersected by the ray and the position of intersection,
  3624. * or <code>undefined</code> if there were no intersections. The intersected object has a <code>primitive</code>
  3625. * property that contains the intersected primitive. Other properties may be set depending on the type of primitive
  3626. * and may be used to further identify the picked object. The ray must be given in world coordinates.
  3627. * <p>
  3628. * This function only picks globe tiles and 3D Tiles that are rendered in the current view. Picks all other
  3629. * primitives regardless of their visibility.
  3630. * </p>
  3631. *
  3632. * @private
  3633. *
  3634. * @param {Ray} ray The ray.
  3635. * @param {Object[]} [objectsToExclude] A list of primitives, entities, or 3D Tiles features to exclude from the ray intersection.
  3636. * @param {Number} [width=0.1] Width of the intersection volume in meters.
  3637. * @returns {Object} An object containing the object and position of the first intersection.
  3638. *
  3639. * @exception {DeveloperError} Ray intersections are only supported in 3D mode.
  3640. */
  3641. Scene.prototype.pickFromRay = function (ray, objectsToExclude, width) {
  3642. return this._picking.pickFromRay(this, ray, objectsToExclude, width);
  3643. };
  3644. /**
  3645. * Returns a list of objects, each containing the object intersected by the ray and the position of intersection.
  3646. * The intersected object has a <code>primitive</code> property that contains the intersected primitive. Other
  3647. * properties may also be set depending on the type of primitive and may be used to further identify the picked object.
  3648. * The primitives in the list are ordered by first intersection to last intersection. The ray must be given in
  3649. * world coordinates.
  3650. * <p>
  3651. * This function only picks globe tiles and 3D Tiles that are rendered in the current view. Picks all other
  3652. * primitives regardless of their visibility.
  3653. * </p>
  3654. *
  3655. * @private
  3656. *
  3657. * @param {Ray} ray The ray.
  3658. * @param {Number} [limit=Number.MAX_VALUE] If supplied, stop finding intersections after this many intersections.
  3659. * @param {Object[]} [objectsToExclude] A list of primitives, entities, or 3D Tiles features to exclude from the ray intersection.
  3660. * @param {Number} [width=0.1] Width of the intersection volume in meters.
  3661. * @returns {Object[]} List of objects containing the object and position of each intersection.
  3662. *
  3663. * @exception {DeveloperError} Ray intersections are only supported in 3D mode.
  3664. */
  3665. Scene.prototype.drillPickFromRay = function (
  3666. ray,
  3667. limit,
  3668. objectsToExclude,
  3669. width
  3670. ) {
  3671. return this._picking.drillPickFromRay(
  3672. this,
  3673. ray,
  3674. limit,
  3675. objectsToExclude,
  3676. width
  3677. );
  3678. };
  3679. /**
  3680. * Initiates an asynchronous {@link Scene#pickFromRay} request using the maximum level of detail for 3D Tilesets
  3681. * regardless of visibility.
  3682. *
  3683. * @private
  3684. *
  3685. * @param {Ray} ray The ray.
  3686. * @param {Object[]} [objectsToExclude] A list of primitives, entities, or 3D Tiles features to exclude from the ray intersection.
  3687. * @param {Number} [width=0.1] Width of the intersection volume in meters.
  3688. * @returns {Promise.<Object>} A promise that resolves to an object containing the object and position of the first intersection.
  3689. *
  3690. * @exception {DeveloperError} Ray intersections are only supported in 3D mode.
  3691. */
  3692. Scene.prototype.pickFromRayMostDetailed = function (
  3693. ray,
  3694. objectsToExclude,
  3695. width
  3696. ) {
  3697. return this._picking.pickFromRayMostDetailed(
  3698. this,
  3699. ray,
  3700. objectsToExclude,
  3701. width
  3702. );
  3703. };
  3704. /**
  3705. * Initiates an asynchronous {@link Scene#drillPickFromRay} request using the maximum level of detail for 3D Tilesets
  3706. * regardless of visibility.
  3707. *
  3708. * @private
  3709. *
  3710. * @param {Ray} ray The ray.
  3711. * @param {Number} [limit=Number.MAX_VALUE] If supplied, stop finding intersections after this many intersections.
  3712. * @param {Object[]} [objectsToExclude] A list of primitives, entities, or 3D Tiles features to exclude from the ray intersection.
  3713. * @param {Number} [width=0.1] Width of the intersection volume in meters.
  3714. * @returns {Promise.<Object[]>} A promise that resolves to a list of objects containing the object and position of each intersection.
  3715. *
  3716. * @exception {DeveloperError} Ray intersections are only supported in 3D mode.
  3717. */
  3718. Scene.prototype.drillPickFromRayMostDetailed = function (
  3719. ray,
  3720. limit,
  3721. objectsToExclude,
  3722. width
  3723. ) {
  3724. return this._picking.drillPickFromRayMostDetailed(
  3725. this,
  3726. ray,
  3727. limit,
  3728. objectsToExclude,
  3729. width
  3730. );
  3731. };
  3732. /**
  3733. * Returns the height of scene geometry at the given cartographic position or <code>undefined</code> if there was no
  3734. * scene geometry to sample height from. The height of the input position is ignored. May be used to clamp objects to
  3735. * the globe, 3D Tiles, or primitives in the scene.
  3736. * <p>
  3737. * This function only samples height from globe tiles and 3D Tiles that are rendered in the current view. Samples height
  3738. * from all other primitives regardless of their visibility.
  3739. * </p>
  3740. *
  3741. * @param {Cartographic} position The cartographic position to sample height from.
  3742. * @param {Object[]} [objectsToExclude] A list of primitives, entities, or 3D Tiles features to not sample height from.
  3743. * @param {Number} [width=0.1] Width of the intersection volume in meters.
  3744. * @returns {Number} The height. This may be <code>undefined</code> if there was no scene geometry to sample height from.
  3745. *
  3746. * @example
  3747. * const position = new Cesium.Cartographic(-1.31968, 0.698874);
  3748. * const height = viewer.scene.sampleHeight(position);
  3749. * console.log(height);
  3750. *
  3751. * @see Scene#clampToHeight
  3752. * @see Scene#clampToHeightMostDetailed
  3753. * @see Scene#sampleHeightMostDetailed
  3754. *
  3755. * @exception {DeveloperError} sampleHeight is only supported in 3D mode.
  3756. * @exception {DeveloperError} sampleHeight requires depth texture support. Check sampleHeightSupported.
  3757. */
  3758. Scene.prototype.sampleHeight = function (position, objectsToExclude, width) {
  3759. return this._picking.sampleHeight(this, position, objectsToExclude, width);
  3760. };
  3761. /**
  3762. * Clamps the given cartesian position to the scene geometry along the geodetic surface normal. Returns the
  3763. * clamped position or <code>undefined</code> if there was no scene geometry to clamp to. May be used to clamp
  3764. * objects to the globe, 3D Tiles, or primitives in the scene.
  3765. * <p>
  3766. * This function only clamps to globe tiles and 3D Tiles that are rendered in the current view. Clamps to
  3767. * all other primitives regardless of their visibility.
  3768. * </p>
  3769. *
  3770. * @param {Cartesian3} cartesian The cartesian position.
  3771. * @param {Object[]} [objectsToExclude] A list of primitives, entities, or 3D Tiles features to not clamp to.
  3772. * @param {Number} [width=0.1] Width of the intersection volume in meters.
  3773. * @param {Cartesian3} [result] An optional object to return the clamped position.
  3774. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. This may be <code>undefined</code> if there was no scene geometry to clamp to.
  3775. *
  3776. * @example
  3777. * // Clamp an entity to the underlying scene geometry
  3778. * const position = entity.position.getValue(Cesium.JulianDate.now());
  3779. * entity.position = viewer.scene.clampToHeight(position);
  3780. *
  3781. * @see Scene#sampleHeight
  3782. * @see Scene#sampleHeightMostDetailed
  3783. * @see Scene#clampToHeightMostDetailed
  3784. *
  3785. * @exception {DeveloperError} clampToHeight is only supported in 3D mode.
  3786. * @exception {DeveloperError} clampToHeight requires depth texture support. Check clampToHeightSupported.
  3787. */
  3788. Scene.prototype.clampToHeight = function (
  3789. cartesian,
  3790. objectsToExclude,
  3791. width,
  3792. result
  3793. ) {
  3794. return this._picking.clampToHeight(
  3795. this,
  3796. cartesian,
  3797. objectsToExclude,
  3798. width,
  3799. result
  3800. );
  3801. };
  3802. /**
  3803. * Initiates an asynchronous {@link Scene#sampleHeight} query for an array of {@link Cartographic} positions
  3804. * using the maximum level of detail for 3D Tilesets in the scene. The height of the input positions is ignored.
  3805. * Returns a promise that is resolved when the query completes. Each point height is modified in place.
  3806. * If a height cannot be determined because no geometry can be sampled at that location, or another error occurs,
  3807. * the height is set to undefined.
  3808. *
  3809. * @param {Cartographic[]} positions The cartographic positions to update with sampled heights.
  3810. * @param {Object[]} [objectsToExclude] A list of primitives, entities, or 3D Tiles features to not sample height from.
  3811. * @param {Number} [width=0.1] Width of the intersection volume in meters.
  3812. * @returns {Promise.<Cartographic[]>} A promise that resolves to the provided list of positions when the query has completed.
  3813. *
  3814. * @example
  3815. * const positions = [
  3816. * new Cesium.Cartographic(-1.31968, 0.69887),
  3817. * new Cesium.Cartographic(-1.10489, 0.83923)
  3818. * ];
  3819. * const promise = viewer.scene.sampleHeightMostDetailed(positions);
  3820. * promise.then(function(updatedPosition) {
  3821. * // positions[0].height and positions[1].height have been updated.
  3822. * // updatedPositions is just a reference to positions.
  3823. * }
  3824. *
  3825. * @see Scene#sampleHeight
  3826. *
  3827. * @exception {DeveloperError} sampleHeightMostDetailed is only supported in 3D mode.
  3828. * @exception {DeveloperError} sampleHeightMostDetailed requires depth texture support. Check sampleHeightSupported.
  3829. */
  3830. Scene.prototype.sampleHeightMostDetailed = function (
  3831. positions,
  3832. objectsToExclude,
  3833. width
  3834. ) {
  3835. return this._picking.sampleHeightMostDetailed(
  3836. this,
  3837. positions,
  3838. objectsToExclude,
  3839. width
  3840. );
  3841. };
  3842. /**
  3843. * Initiates an asynchronous {@link Scene#clampToHeight} query for an array of {@link Cartesian3} positions
  3844. * using the maximum level of detail for 3D Tilesets in the scene. Returns a promise that is resolved when
  3845. * the query completes. Each position is modified in place. If a position cannot be clamped because no geometry
  3846. * can be sampled at that location, or another error occurs, the element in the array is set to undefined.
  3847. *
  3848. * @param {Cartesian3[]} cartesians The cartesian positions to update with clamped positions.
  3849. * @param {Object[]} [objectsToExclude] A list of primitives, entities, or 3D Tiles features to not clamp to.
  3850. * @param {Number} [width=0.1] Width of the intersection volume in meters.
  3851. * @returns {Promise.<Cartesian3[]>} A promise that resolves to the provided list of positions when the query has completed.
  3852. *
  3853. * @example
  3854. * const cartesians = [
  3855. * entities[0].position.getValue(Cesium.JulianDate.now()),
  3856. * entities[1].position.getValue(Cesium.JulianDate.now())
  3857. * ];
  3858. * const promise = viewer.scene.clampToHeightMostDetailed(cartesians);
  3859. * promise.then(function(updatedCartesians) {
  3860. * entities[0].position = updatedCartesians[0];
  3861. * entities[1].position = updatedCartesians[1];
  3862. * }
  3863. *
  3864. * @see Scene#clampToHeight
  3865. *
  3866. * @exception {DeveloperError} clampToHeightMostDetailed is only supported in 3D mode.
  3867. * @exception {DeveloperError} clampToHeightMostDetailed requires depth texture support. Check clampToHeightSupported.
  3868. */
  3869. Scene.prototype.clampToHeightMostDetailed = function (
  3870. cartesians,
  3871. objectsToExclude,
  3872. width
  3873. ) {
  3874. return this._picking.clampToHeightMostDetailed(
  3875. this,
  3876. cartesians,
  3877. objectsToExclude,
  3878. width
  3879. );
  3880. };
  3881. /**
  3882. * Transforms a position in cartesian coordinates to canvas coordinates. This is commonly used to place an
  3883. * HTML element at the same screen position as an object in the scene.
  3884. *
  3885. * @param {Cartesian3} position The position in cartesian coordinates.
  3886. * @param {Cartesian2} [result] An optional object to return the input position transformed to canvas coordinates.
  3887. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. This may be <code>undefined</code> if the input position is near the center of the ellipsoid.
  3888. *
  3889. * @example
  3890. * // Output the canvas position of longitude/latitude (0, 0) every time the mouse moves.
  3891. * const scene = widget.scene;
  3892. * const ellipsoid = scene.globe.ellipsoid;
  3893. * const position = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  3894. * const handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  3895. * handler.setInputAction(function(movement) {
  3896. * console.log(scene.cartesianToCanvasCoordinates(position));
  3897. * }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  3898. */
  3899. Scene.prototype.cartesianToCanvasCoordinates = function (position, result) {
  3900. return SceneTransforms.wgs84ToWindowCoordinates(this, position, result);
  3901. };
  3902. /**
  3903. * Instantly completes an active transition.
  3904. */
  3905. Scene.prototype.completeMorph = function () {
  3906. this._transitioner.completeMorph();
  3907. };
  3908. /**
  3909. * Asynchronously transitions the scene to 2D.
  3910. * @param {Number} [duration=2.0] The amount of time, in seconds, for transition animations to complete.
  3911. */
  3912. Scene.prototype.morphTo2D = function (duration) {
  3913. let ellipsoid;
  3914. const globe = this.globe;
  3915. if (defined(globe)) {
  3916. ellipsoid = globe.ellipsoid;
  3917. } else {
  3918. ellipsoid = this.mapProjection.ellipsoid;
  3919. }
  3920. duration = defaultValue(duration, 2.0);
  3921. this._transitioner.morphTo2D(duration, ellipsoid);
  3922. };
  3923. /**
  3924. * Asynchronously transitions the scene to Columbus View.
  3925. * @param {Number} [duration=2.0] The amount of time, in seconds, for transition animations to complete.
  3926. */
  3927. Scene.prototype.morphToColumbusView = function (duration) {
  3928. let ellipsoid;
  3929. const globe = this.globe;
  3930. if (defined(globe)) {
  3931. ellipsoid = globe.ellipsoid;
  3932. } else {
  3933. ellipsoid = this.mapProjection.ellipsoid;
  3934. }
  3935. duration = defaultValue(duration, 2.0);
  3936. this._transitioner.morphToColumbusView(duration, ellipsoid);
  3937. };
  3938. /**
  3939. * Asynchronously transitions the scene to 3D.
  3940. * @param {Number} [duration=2.0] The amount of time, in seconds, for transition animations to complete.
  3941. */
  3942. Scene.prototype.morphTo3D = function (duration) {
  3943. let ellipsoid;
  3944. const globe = this.globe;
  3945. if (defined(globe)) {
  3946. ellipsoid = globe.ellipsoid;
  3947. } else {
  3948. ellipsoid = this.mapProjection.ellipsoid;
  3949. }
  3950. duration = defaultValue(duration, 2.0);
  3951. this._transitioner.morphTo3D(duration, ellipsoid);
  3952. };
  3953. /**
  3954. * Returns true if this object was destroyed; otherwise, false.
  3955. * <br /><br />
  3956. * If this object was destroyed, it should not be used; calling any function other than
  3957. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  3958. *
  3959. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  3960. *
  3961. * @see Scene#destroy
  3962. */
  3963. Scene.prototype.isDestroyed = function () {
  3964. return false;
  3965. };
  3966. /**
  3967. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  3968. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  3969. * <br /><br />
  3970. * Once an object is destroyed, it should not be used; calling any function other than
  3971. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  3972. * assign the return value (<code>undefined</code>) to the object as done in the example.
  3973. *
  3974. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  3975. *
  3976. *
  3977. * @example
  3978. * scene = scene && scene.destroy();
  3979. *
  3980. * @see Scene#isDestroyed
  3981. */
  3982. Scene.prototype.destroy = function () {
  3983. this._tweens.removeAll();
  3984. this._computeEngine = this._computeEngine && this._computeEngine.destroy();
  3985. this._screenSpaceCameraController =
  3986. this._screenSpaceCameraController &&
  3987. this._screenSpaceCameraController.destroy();
  3988. this._deviceOrientationCameraController =
  3989. this._deviceOrientationCameraController &&
  3990. !this._deviceOrientationCameraController.isDestroyed() &&
  3991. this._deviceOrientationCameraController.destroy();
  3992. this._primitives = this._primitives && this._primitives.destroy();
  3993. this._groundPrimitives =
  3994. this._groundPrimitives && this._groundPrimitives.destroy();
  3995. this._globe = this._globe && this._globe.destroy();
  3996. this.skyBox = this.skyBox && this.skyBox.destroy();
  3997. this.skyAtmosphere = this.skyAtmosphere && this.skyAtmosphere.destroy();
  3998. this._debugSphere = this._debugSphere && this._debugSphere.destroy();
  3999. this.sun = this.sun && this.sun.destroy();
  4000. this._sunPostProcess = this._sunPostProcess && this._sunPostProcess.destroy();
  4001. this._depthPlane = this._depthPlane && this._depthPlane.destroy();
  4002. this._transitioner = this._transitioner && this._transitioner.destroy();
  4003. this._debugFrustumPlanes =
  4004. this._debugFrustumPlanes && this._debugFrustumPlanes.destroy();
  4005. this._brdfLutGenerator =
  4006. this._brdfLutGenerator && this._brdfLutGenerator.destroy();
  4007. this._picking = this._picking && this._picking.destroy();
  4008. this._defaultView = this._defaultView && this._defaultView.destroy();
  4009. this._view = undefined;
  4010. if (this._removeCreditContainer) {
  4011. this._canvas.parentNode.removeChild(this._creditContainer);
  4012. }
  4013. this.postProcessStages =
  4014. this.postProcessStages && this.postProcessStages.destroy();
  4015. this._context = this._context && this._context.destroy();
  4016. this._frameState.creditDisplay =
  4017. this._frameState.creditDisplay && this._frameState.creditDisplay.destroy();
  4018. if (defined(this._performanceDisplay)) {
  4019. this._performanceDisplay =
  4020. this._performanceDisplay && this._performanceDisplay.destroy();
  4021. this._performanceContainer.parentNode.removeChild(
  4022. this._performanceContainer
  4023. );
  4024. }
  4025. this._removeRequestListenerCallback();
  4026. this._removeTaskProcessorListenerCallback();
  4027. for (let i = 0; i < this._removeGlobeCallbacks.length; ++i) {
  4028. this._removeGlobeCallbacks[i]();
  4029. }
  4030. this._removeGlobeCallbacks.length = 0;
  4031. return destroyObject(this);
  4032. };
  4033. export default Scene;