Model.js 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258
  1. import BoundingSphere from "../../Core/BoundingSphere.js";
  2. import Cartesian3 from "../../Core/Cartesian3.js";
  3. import Cartographic from "../../Core/Cartographic.js";
  4. import Check from "../../Core/Check.js";
  5. import Credit from "../../Core/Credit.js";
  6. import Color from "../../Core/Color.js";
  7. import defined from "../../Core/defined.js";
  8. import defaultValue from "../../Core/defaultValue.js";
  9. import deprecationWarning from "../../Core/deprecationWarning.js";
  10. import DeveloperError from "../../Core/DeveloperError.js";
  11. import destroyObject from "../../Core/destroyObject.js";
  12. import DistanceDisplayCondition from "../../Core/DistanceDisplayCondition.js";
  13. import Event from "../../Core/Event.js";
  14. import Matrix3 from "../../Core/Matrix3.js";
  15. import Matrix4 from "../../Core/Matrix4.js";
  16. import Resource from "../../Core/Resource.js";
  17. import RuntimeError from "../../Core/RuntimeError.js";
  18. import Pass from "../../Renderer/Pass.js";
  19. import ClippingPlaneCollection from "../ClippingPlaneCollection.js";
  20. import ColorBlendMode from "../ColorBlendMode.js";
  21. import GltfLoader from "../GltfLoader.js";
  22. import HeightReference from "../HeightReference.js";
  23. import ImageBasedLighting from "../ImageBasedLighting.js";
  24. import PointCloudShading from "../PointCloudShading.js";
  25. import SceneMode from "../SceneMode.js";
  26. import SceneTransforms from "../SceneTransforms.js";
  27. import ShadowMode from "../ShadowMode.js";
  28. import SplitDirection from "../SplitDirection.js";
  29. import B3dmLoader from "./B3dmLoader.js";
  30. import GeoJsonLoader from "./GeoJsonLoader.js";
  31. import I3dmLoader from "./I3dmLoader.js";
  32. import ModelAnimationCollection from "./ModelAnimationCollection.js";
  33. import ModelFeatureTable from "./ModelFeatureTable.js";
  34. import ModelSceneGraph from "./ModelSceneGraph.js";
  35. import ModelStatistics from "./ModelStatistics.js";
  36. import ModelType from "./ModelType.js";
  37. import ModelUtility from "./ModelUtility.js";
  38. import oneTimeWarning from "../../Core/oneTimeWarning.js";
  39. import PntsLoader from "./PntsLoader.js";
  40. import StyleCommandsNeeded from "./StyleCommandsNeeded.js";
  41. /**
  42. * <div class="notice">
  43. * To construct a Model, call {@link Model.fromGltfAsync}. Do not call the constructor directly.
  44. * </div>
  45. * A 3D model based on glTF, the runtime asset format for WebGL, OpenGL ES, and OpenGL.
  46. * <p>
  47. * Cesium supports glTF assets with the following extensions:
  48. * <ul>
  49. * <li>
  50. * {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/AGI_articulations/README.md|AGI_articulations}
  51. * </li>
  52. * <li>
  53. * {@link https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Vendor/CESIUM_primitive_outline|CESIUM_primitive_outline}
  54. * </li>
  55. * <li>
  56. * {@link https://github.com/KhronosGroup/glTF/blob/master/extensions/1.0/Vendor/CESIUM_RTC/README.md|CESIUM_RTC}
  57. * </li>
  58. * <li>
  59. * {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_instance_features|EXT_instance_features}
  60. * </li>
  61. * <li>
  62. * {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_mesh_features|EXT_mesh_features}
  63. * </li>
  64. * <li>
  65. * {@link https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Vendor/EXT_mesh_gpu_instancing|EXT_mesh_gpu_instancing}
  66. * </li>
  67. * <li>
  68. * {@link https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Vendor/EXT_meshopt_compression|EXT_meshopt_compression}
  69. * </li>
  70. * <li>
  71. * {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_structural_metadata|EXT_structural_metadata}
  72. * </li>
  73. * <li>
  74. * {@link https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Vendor/EXT_texture_webp|EXT_texture_webp}
  75. * </li>
  76. * <li>
  77. * {@link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_draco_mesh_compression/README.md|KHR_draco_mesh_compression}
  78. * </li>
  79. * <li>
  80. * {@link https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Archived/KHR_techniques_webgl/README.md|KHR_techniques_webgl}
  81. * </li>
  82. * <li>
  83. * {@link https://github.com/KhronosGroup/glTF/blob/main/extensions/1.0/Khronos/KHR_materials_common/README.md|KHR_materials_common}
  84. * </li>
  85. * <li>
  86. * {@link https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Archived/KHR_materials_pbrSpecularGlossiness|KHR_materials_pbrSpecularGlossiness}
  87. * </li>
  88. * <li>
  89. * {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit/README.md|KHR_materials_unlit}
  90. * </li>
  91. * <li>
  92. * {@link https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_mesh_quantization|KHR_mesh_quantization}
  93. * </li>
  94. * <li>
  95. * {@link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_texture_basisu|KHR_texture_basisu}
  96. * </li>
  97. * <li>
  98. * {@link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_texture_transform/README.md|KHR_texture_transform}
  99. * </li>
  100. * <li>
  101. * {@link https://github.com/KhronosGroup/glTF/blob/main/extensions/1.0/Vendor/WEB3D_quantized_attributes/README.md|WEB3D_quantized_attributes}
  102. * </li>
  103. * </ul>
  104. * </p>
  105. * <p>
  106. * Note: for models with compressed textures using the KHR_texture_basisu extension, we recommend power of 2 textures in both dimensions
  107. * for maximum compatibility. This is because some samplers require power of 2 textures ({@link https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Using_textures_in_WebGL|Using textures in WebGL})
  108. * and KHR_texture_basisu requires multiple of 4 dimensions ({@link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_texture_basisu/README.md#additional-requirements|KHR_texture_basisu additional requirements}).
  109. * </p>
  110. *
  111. * @alias Model
  112. * @internalConstructor
  113. *
  114. * @privateParam {ResourceLoader} options.loader The loader used to load resources for this model.
  115. * @privateParam {ModelType} options.type Type of this model, to distinguish individual glTF files from 3D Tiles internally.
  116. * @privateParam {object} options Object with the following properties:
  117. * @privateParam {Resource} options.resource The Resource to the 3D model.
  118. * @privateParam {boolean} [options.show=true] Whether or not to render the model.
  119. * @privateParam {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix that transforms the model from model to world coordinates.
  120. * @privateParam {number} [options.scale=1.0] A uniform scale applied to this model.
  121. * @privateParam {number} [options.minimumPixelSize=0.0] The approximate minimum pixel size of the model regardless of zoom.
  122. * @privateParam {number} [options.maximumScale] The maximum scale size of a model. An upper limit for minimumPixelSize.
  123. * @privateParam {object} [options.id] A user-defined object to return when the model is picked with {@link Scene#pick}.
  124. * @privateParam {boolean} [options.allowPicking=true] When <code>true</code>, each primitive is pickable with {@link Scene#pick}.
  125. * @privateParam {boolean} [options.clampAnimations=true] Determines if the model's animations should hold a pose over frames where no keyframes are specified.
  126. * @privateParam {ShadowMode} [options.shadows=ShadowMode.ENABLED] Determines whether the model casts or receives shadows from light sources.
  127. * @privateParam {boolean} [options.debugShowBoundingVolume=false] For debugging only. Draws the bounding sphere for each draw command in the model.
  128. * @privateParam {boolean} [options.enableDebugWireframe=false] For debugging only. This must be set to true for debugWireframe to work in WebGL1. This cannot be set after the model has loaded.
  129. * @privateParam {boolean} [options.debugWireframe=false] For debugging only. Draws the model in wireframe. Will only work for WebGL1 if enableDebugWireframe is set to true.
  130. * @privateParam {boolean} [options.cull=true] Whether or not to cull the model using frustum/horizon culling. If the model is part of a 3D Tiles tileset, this property will always be false, since the 3D Tiles culling system is used.
  131. * @privateParam {boolean} [options.opaquePass=Pass.OPAQUE] The pass to use in the {@link DrawCommand} for the opaque portions of the model.
  132. * @privateParam {CustomShader} [options.customShader] A custom shader. This will add user-defined GLSL code to the vertex and fragment shaders. Using custom shaders with a {@link Cesium3DTileStyle} may lead to undefined behavior.
  133. * @privateParam {Cesium3DTileContent} [options.content] The tile content this model belongs to. This property will be undefined if model is not loaded as part of a tileset.
  134. * @privateParam {HeightReference} [options.heightReference=HeightReference.NONE] Determines how the model is drawn relative to terrain.
  135. * @privateParam {Scene} [options.scene] Must be passed in for models that use the height reference property.
  136. * @privateParam {DistanceDisplayCondition} [options.distanceDisplayCondition] The condition specifying at what distance from the camera that this model will be displayed.
  137. * @privateParam {Color} [options.color] A color that blends with the model's rendered color.
  138. * @privateParam {ColorBlendMode} [options.colorBlendMode=ColorBlendMode.HIGHLIGHT] Defines how the color blends with the model.
  139. * @privateParam {number} [options.colorBlendAmount=0.5] Value used to determine the color strength when the <code>colorBlendMode</code> is <code>MIX</code>. A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with any value in-between resulting in a mix of the two.
  140. * @privateParam {Color} [options.silhouetteColor=Color.RED] The silhouette color. If more than 256 models have silhouettes enabled, there is a small chance that overlapping models will have minor artifacts.
  141. * @privateParam {number} [options.silhouetteSize=0.0] The size of the silhouette in pixels.
  142. * @privateParam {boolean} [options.enableShowOutline=true] Whether to enable outlines for models using the {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/CESIUM_primitive_outline|CESIUM_primitive_outline} extension. This can be set to false to avoid the additional processing of geometry at load time. When false, the showOutlines and outlineColor options are ignored.
  143. * @privateParam {boolean} [options.showOutline=true] Whether to display the outline for models using the {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/CESIUM_primitive_outline|CESIUM_primitive_outline} extension. When true, outlines are displayed. When false, outlines are not displayed.
  144. * @privateParam {Color} [options.outlineColor=Color.BLACK] The color to use when rendering outlines.
  145. * @privateParam {ClippingPlaneCollection} [options.clippingPlanes] The {@link ClippingPlaneCollection} used to selectively disable rendering the model.
  146. * @privateParam {Cartesian3} [options.lightColor] The light color when shading the model. When <code>undefined</code> the scene's light color is used instead.
  147. * @privateParam {ImageBasedLighting} [options.imageBasedLighting] The properties for managing image-based lighting on this model.
  148. * @privateParam {boolean} [options.backFaceCulling=true] Whether to cull back-facing geometry. When true, back face culling is determined by the material's doubleSided property; when false, back face culling is disabled. Back faces are not culled if the model's color is translucent.
  149. * @privateParam {Credit|string} [options.credit] A credit for the data source, which is displayed on the canvas.
  150. * @privateParam {boolean} [options.showCreditsOnScreen=false] Whether to display the credits of this model on screen.
  151. * @privateParam {SplitDirection} [options.splitDirection=SplitDirection.NONE] The {@link SplitDirection} split to apply to this model.
  152. * @privateParam {boolean} [options.projectTo2D=false] Whether to accurately project the model's positions in 2D. If this is true, the model will be projected accurately to 2D, but it will use more memory to do so. If this is false, the model will use less memory and will still render in 2D / CV mode, but its positions may be inaccurate. This disables minimumPixelSize and prevents future modification to the model matrix. This also cannot be set after the model has loaded.
  153. * @privateParam {string|number} [options.featureIdLabel="featureId_0"] Label of the feature ID set to use for picking and styling. For EXT_mesh_features, this is the feature ID's label property, or "featureId_N" (where N is the index in the featureIds array) when not specified. EXT_feature_metadata did not have a label field, so such feature ID sets are always labeled "featureId_N" where N is the index in the list of all feature Ids, where feature ID attributes are listed before feature ID textures. If featureIdLabel is an integer N, it is converted to the string "featureId_N" automatically. If both per-primitive and per-instance feature IDs are present, the instance feature IDs take priority.
  154. * @privateParam {string|number} [options.instanceFeatureIdLabel="instanceFeatureId_0"] Label of the instance feature ID set used for picking and styling. If instanceFeatureIdLabel is set to an integer N, it is converted to the string "instanceFeatureId_N" automatically. If both per-primitive and per-instance feature IDs are present, the instance feature IDs take priority.
  155. * @privateParam {object} [options.pointCloudShading] Options for constructing a {@link PointCloudShading} object to control point attenuation based on geometric error and lighting.
  156. * @privateParam {ClassificationType} [options.classificationType] Determines whether terrain, 3D Tiles or both will be classified by this model. This cannot be set after the model has loaded.
  157. *
  158. * @see Model.fromGltfAsync
  159. *
  160. * @demo {@link https://sandcastle.cesium.com/index.html?src=3D%20Models.html|Cesium Sandcastle Models Demo}
  161. */
  162. function Model(options) {
  163. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  164. //>>includeStart('debug', pragmas.debug);
  165. Check.typeOf.object("options.loader", options.loader);
  166. Check.typeOf.object("options.resource", options.resource);
  167. //>>includeEnd('debug');
  168. /**
  169. * The loader used to load resources for this model.
  170. *
  171. * @type {ResourceLoader}
  172. * @private
  173. */
  174. this._loader = options.loader;
  175. this._resource = options.resource;
  176. /**
  177. * Type of this model, to distinguish individual glTF files from 3D Tiles
  178. * internally.
  179. *
  180. * @type {ModelType}
  181. * @readonly
  182. *
  183. * @private
  184. */
  185. this.type = defaultValue(options.type, ModelType.GLTF);
  186. /**
  187. * The 4x4 transformation matrix that transforms the model from model to world coordinates.
  188. * When this is the identity matrix, the model is drawn in world coordinates, i.e., Earth's Cartesian WGS84 coordinates.
  189. * Local reference frames can be used by providing a different transformation matrix, like that returned
  190. * by {@link Transforms.eastNorthUpToFixedFrame}.
  191. *
  192. * @type {Matrix4}
  193. * @default {@link Matrix4.IDENTITY}
  194. *
  195. * @example
  196. * const origin = Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 200000.0);
  197. * m.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
  198. */
  199. this.modelMatrix = Matrix4.clone(
  200. defaultValue(options.modelMatrix, Matrix4.IDENTITY)
  201. );
  202. this._modelMatrix = Matrix4.clone(this.modelMatrix);
  203. this._scale = defaultValue(options.scale, 1.0);
  204. this._minimumPixelSize = defaultValue(options.minimumPixelSize, 0.0);
  205. this._maximumScale = options.maximumScale;
  206. /**
  207. * The scale value after being clamped by the maximum scale parameter.
  208. * Used to adjust bounding spheres without repeated calculation.
  209. *
  210. * @type {number}
  211. * @private
  212. */
  213. this._clampedScale = defined(this._maximumScale)
  214. ? Math.min(this._scale, this._maximumScale)
  215. : this._scale;
  216. this._computedScale = this._clampedScale;
  217. /**
  218. * Whether or not the ModelSceneGraph should call updateModelMatrix.
  219. * This will be true if any of the model matrix, scale, minimum pixel size, or maximum scale are dirty.
  220. *
  221. * @type {number}
  222. * @private
  223. */
  224. this._updateModelMatrix = false;
  225. /**
  226. * If defined, this matrix is used to transform miscellaneous properties like
  227. * clipping planes and image-based lighting instead of the modelMatrix. This is
  228. * so that when models are part of a tileset, these properties get transformed
  229. * relative to a common reference (such as the root).
  230. *
  231. * @type {Matrix4}
  232. * @private
  233. */
  234. this.referenceMatrix = undefined;
  235. this._iblReferenceFrameMatrix = Matrix3.clone(Matrix3.IDENTITY); // Derived from reference matrix and the current view matrix
  236. this._resourcesLoaded = false;
  237. this._drawCommandsBuilt = false;
  238. this._ready = false;
  239. this._customShader = options.customShader;
  240. this._content = options.content;
  241. this._texturesLoaded = false;
  242. this._defaultTexture = undefined;
  243. this._activeAnimations = new ModelAnimationCollection(this);
  244. this._clampAnimations = defaultValue(options.clampAnimations, true);
  245. // This flag is true when the Cesium API, not a glTF animation, changes
  246. // the transform of a node in the model.
  247. this._userAnimationDirty = false;
  248. this._id = options.id;
  249. this._idDirty = false;
  250. this._color = Color.clone(options.color);
  251. this._colorBlendMode = defaultValue(
  252. options.colorBlendMode,
  253. ColorBlendMode.HIGHLIGHT
  254. );
  255. this._colorBlendAmount = defaultValue(options.colorBlendAmount, 0.5);
  256. const silhouetteColor = defaultValue(options.silhouetteColor, Color.RED);
  257. this._silhouetteColor = Color.clone(silhouetteColor);
  258. this._silhouetteSize = defaultValue(options.silhouetteSize, 0.0);
  259. this._silhouetteDirty = false;
  260. // If silhouettes are used for the model, this will be set to the number
  261. // of the stencil buffer used for rendering the silhouette. This is set
  262. // by ModelSilhouettePipelineStage, not by Model itself.
  263. this._silhouetteId = undefined;
  264. this._cull = defaultValue(options.cull, true);
  265. this._opaquePass = defaultValue(options.opaquePass, Pass.OPAQUE);
  266. this._allowPicking = defaultValue(options.allowPicking, true);
  267. this._show = defaultValue(options.show, true);
  268. this._style = undefined;
  269. this._styleDirty = false;
  270. this._styleCommandsNeeded = undefined;
  271. let featureIdLabel = defaultValue(options.featureIdLabel, "featureId_0");
  272. if (typeof featureIdLabel === "number") {
  273. featureIdLabel = `featureId_${featureIdLabel}`;
  274. }
  275. this._featureIdLabel = featureIdLabel;
  276. let instanceFeatureIdLabel = defaultValue(
  277. options.instanceFeatureIdLabel,
  278. "instanceFeatureId_0"
  279. );
  280. if (typeof instanceFeatureIdLabel === "number") {
  281. instanceFeatureIdLabel = `instanceFeatureId_${instanceFeatureIdLabel}`;
  282. }
  283. this._instanceFeatureIdLabel = instanceFeatureIdLabel;
  284. this._featureTables = [];
  285. this._featureTableId = undefined;
  286. this._featureTableIdDirty = true;
  287. // Keeps track of resources that need to be destroyed when the draw commands are reset.
  288. this._pipelineResources = [];
  289. // Keeps track of resources that need to be destroyed when the Model is destroyed.
  290. this._modelResources = [];
  291. // Keeps track of the pick IDs for this model. These are stored and destroyed in the
  292. // pipeline resources array; the purpose of this array is to separate them from other
  293. // resources and update their ID objects when necessary.
  294. this._pickIds = [];
  295. // The model's bounding sphere and its initial radius are computed
  296. // in ModelSceneGraph.
  297. this._boundingSphere = new BoundingSphere();
  298. this._initialRadius = undefined;
  299. this._heightReference = defaultValue(
  300. options.heightReference,
  301. HeightReference.NONE
  302. );
  303. this._heightDirty = this._heightReference !== HeightReference.NONE;
  304. this._removeUpdateHeightCallback = undefined;
  305. this._clampedModelMatrix = undefined; // For use with height reference
  306. const scene = options.scene;
  307. if (defined(scene) && defined(scene.terrainProviderChanged)) {
  308. this._terrainProviderChangedCallback = scene.terrainProviderChanged.addEventListener(
  309. function () {
  310. this._heightDirty = true;
  311. },
  312. this
  313. );
  314. }
  315. this._scene = scene;
  316. this._distanceDisplayCondition = options.distanceDisplayCondition;
  317. const pointCloudShading = new PointCloudShading(options.pointCloudShading);
  318. this._pointCloudShading = pointCloudShading;
  319. this._attenuation = pointCloudShading.attenuation;
  320. this._pointCloudBackFaceCulling = pointCloudShading.backFaceCulling;
  321. // If the given clipping planes don't have an owner, make this model its owner.
  322. // Otherwise, the clipping planes are passed down from a tileset.
  323. const clippingPlanes = options.clippingPlanes;
  324. if (defined(clippingPlanes) && clippingPlanes.owner === undefined) {
  325. ClippingPlaneCollection.setOwner(clippingPlanes, this, "_clippingPlanes");
  326. } else {
  327. this._clippingPlanes = clippingPlanes;
  328. }
  329. this._clippingPlanesState = 0; // If this value changes, the shaders need to be regenerated.
  330. this._clippingPlanesMatrix = Matrix4.clone(Matrix4.IDENTITY); // Derived from reference matrix and the current view matrix
  331. this._lightColor = Cartesian3.clone(options.lightColor);
  332. this._imageBasedLighting = defined(options.imageBasedLighting)
  333. ? options.imageBasedLighting
  334. : new ImageBasedLighting();
  335. this._shouldDestroyImageBasedLighting = !defined(options.imageBasedLighting);
  336. this._backFaceCulling = defaultValue(options.backFaceCulling, true);
  337. this._backFaceCullingDirty = false;
  338. this._shadows = defaultValue(options.shadows, ShadowMode.ENABLED);
  339. this._shadowsDirty = false;
  340. this._debugShowBoundingVolumeDirty = false;
  341. this._debugShowBoundingVolume = defaultValue(
  342. options.debugShowBoundingVolume,
  343. false
  344. );
  345. this._enableDebugWireframe = defaultValue(
  346. options.enableDebugWireframe,
  347. false
  348. );
  349. this._enableShowOutline = defaultValue(options.enableShowOutline, true);
  350. this._debugWireframe = defaultValue(options.debugWireframe, false);
  351. // Warning for improper setup of debug wireframe
  352. if (
  353. this._debugWireframe === true &&
  354. this._enableDebugWireframe === false &&
  355. this.type === ModelType.GLTF
  356. ) {
  357. oneTimeWarning(
  358. "model-debug-wireframe-ignored",
  359. "enableDebugWireframe must be set to true in Model.fromGltf, otherwise debugWireframe will be ignored."
  360. );
  361. }
  362. // Credit specified by the user.
  363. let credit = options.credit;
  364. if (typeof credit === "string") {
  365. credit = new Credit(credit);
  366. }
  367. this._credit = credit;
  368. // Credits to be added from the Resource (if it is an IonResource)
  369. this._resourceCredits = [];
  370. // Credits parsed from the glTF by GltfLoader.
  371. this._gltfCredits = [];
  372. this._showCreditsOnScreen = defaultValue(options.showCreditsOnScreen, false);
  373. this._showCreditsOnScreenDirty = true;
  374. this._splitDirection = defaultValue(
  375. options.splitDirection,
  376. SplitDirection.NONE
  377. );
  378. this._enableShowOutline = defaultValue(options.enableShowOutline, true);
  379. /**
  380. * Whether to display the outline for models using the
  381. * {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/CESIUM_primitive_outline|CESIUM_primitive_outline} extension.
  382. * When true, outlines are displayed. When false, outlines are not displayed.
  383. *
  384. * @type {boolean}
  385. *
  386. * @default true
  387. */
  388. this.showOutline = defaultValue(options.showOutline, true);
  389. /**
  390. * The color to use when rendering outlines.
  391. *
  392. * @type {Color}
  393. *
  394. * @default Color.BLACK
  395. */
  396. this.outlineColor = defaultValue(options.outlineColor, Color.BLACK);
  397. this._classificationType = options.classificationType;
  398. this._statistics = new ModelStatistics();
  399. this._sceneMode = undefined;
  400. this._projectTo2D = defaultValue(options.projectTo2D, false);
  401. this._skipLevelOfDetail = false;
  402. this._ignoreCommands = defaultValue(options.ignoreCommands, false);
  403. this._texturesLoadedPromise = undefined;
  404. this._completeLoad = undefined;
  405. this._rejectLoad = undefined;
  406. this._completeTexturesLoad = undefined;
  407. this._rejectTexturesLoad = undefined;
  408. // This is for backward compatibility. When readyPromise is removed, this block can be too
  409. if (!defined(this._loader._promise)) {
  410. this._readyPromise = new Promise((resolve, reject) => {
  411. this._completeLoad = () => {
  412. resolve(this);
  413. return false;
  414. };
  415. this._rejectLoad = (error) => {
  416. reject(error);
  417. return false;
  418. };
  419. });
  420. // Transcoded .pnts models do not have textures
  421. if (this._loader instanceof PntsLoader) {
  422. this._texturesLoadedPromise = Promise.resolve(this);
  423. } else {
  424. this._texturesLoadedPromise = new Promise((resolve, reject) => {
  425. this._completeTexturesLoad = () => {
  426. resolve(this);
  427. return false;
  428. };
  429. this._rejectTexturesLoad = (error) => {
  430. reject(error);
  431. return false;
  432. };
  433. });
  434. }
  435. this._loader.load().catch((error) => {
  436. // If the model is destroyed before the promise resolves, then
  437. // the loader will have been destroyed as well. Return early.
  438. if (
  439. this.isDestroyed() ||
  440. !defined(this._loader) ||
  441. this._loader.isDestroyed()
  442. ) {
  443. return;
  444. }
  445. this._rejectLoad = this._rejectLoad(
  446. ModelUtility.getError("model", this._resource, error)
  447. );
  448. });
  449. } else {
  450. this._readyPromise = Promise.resolve(this);
  451. this._texturesLoadedPromise = Promise.resolve(this);
  452. }
  453. this._errorEvent = new Event();
  454. this._readyEvent = new Event();
  455. this._texturesReadyEvent = new Event();
  456. this._sceneGraph = undefined;
  457. this._nodesByName = {}; // Stores the nodes by their names in the glTF.
  458. /**
  459. * Used for picking primitives that wrap a model.
  460. *
  461. * @private
  462. */
  463. this.pickObject = options.pickObject;
  464. }
  465. function handleError(model, error) {
  466. if (model._errorEvent.numberOfListeners > 0) {
  467. model._errorEvent.raiseEvent(error);
  468. return;
  469. }
  470. console.log(error);
  471. }
  472. function createModelFeatureTables(model, structuralMetadata) {
  473. const featureTables = model._featureTables;
  474. const propertyTables = structuralMetadata.propertyTables;
  475. const length = propertyTables.length;
  476. for (let i = 0; i < length; i++) {
  477. const propertyTable = propertyTables[i];
  478. const modelFeatureTable = new ModelFeatureTable({
  479. model: model,
  480. propertyTable: propertyTable,
  481. });
  482. featureTables.push(modelFeatureTable);
  483. }
  484. return featureTables;
  485. }
  486. function selectFeatureTableId(components, model) {
  487. const featureIdLabel = model._featureIdLabel;
  488. const instanceFeatureIdLabel = model._instanceFeatureIdLabel;
  489. let i, j;
  490. let featureIdAttribute;
  491. let node;
  492. // Scan the nodes till we find one with instances, get the feature table ID
  493. // if the feature ID attribute of the user-selected index is present.
  494. for (i = 0; i < components.nodes.length; i++) {
  495. node = components.nodes[i];
  496. if (defined(node.instances)) {
  497. featureIdAttribute = ModelUtility.getFeatureIdsByLabel(
  498. node.instances.featureIds,
  499. instanceFeatureIdLabel
  500. );
  501. if (
  502. defined(featureIdAttribute) &&
  503. defined(featureIdAttribute.propertyTableId)
  504. ) {
  505. return featureIdAttribute.propertyTableId;
  506. }
  507. }
  508. }
  509. // Scan the primitives till we find one with textures or attributes, get the feature table ID
  510. // if the feature ID attribute/texture of the user-selected index is present.
  511. for (i = 0; i < components.nodes.length; i++) {
  512. node = components.nodes[i];
  513. for (j = 0; j < node.primitives.length; j++) {
  514. const primitive = node.primitives[j];
  515. const featureIds = ModelUtility.getFeatureIdsByLabel(
  516. primitive.featureIds,
  517. featureIdLabel
  518. );
  519. if (defined(featureIds)) {
  520. return featureIds.propertyTableId;
  521. }
  522. }
  523. }
  524. // If there's only one feature table, then select it by default. This is
  525. // to ensure backwards compatibility with the older handling of b3dm models.
  526. if (model._featureTables.length === 1) {
  527. return 0;
  528. }
  529. }
  530. /**
  531. * Returns whether the alpha state has changed between invisible,
  532. * translucent, or opaque.
  533. *
  534. * @private
  535. */
  536. function isColorAlphaDirty(currentColor, previousColor) {
  537. if (!defined(currentColor) && !defined(previousColor)) {
  538. return false;
  539. }
  540. if (defined(currentColor) !== defined(previousColor)) {
  541. return true;
  542. }
  543. const currentAlpha = currentColor.alpha;
  544. const previousAlpha = previousColor.alpha;
  545. return (
  546. Math.floor(currentAlpha) !== Math.floor(previousAlpha) ||
  547. Math.ceil(currentAlpha) !== Math.ceil(previousAlpha)
  548. );
  549. }
  550. Object.defineProperties(Model.prototype, {
  551. /**
  552. * When <code>true</code>, this model is ready to render, i.e., the external binary, image,
  553. * and shader files were downloaded and the WebGL resources were created.
  554. *
  555. * @memberof Model.prototype
  556. *
  557. * @type {boolean}
  558. * @readonly
  559. *
  560. * @default false
  561. */
  562. ready: {
  563. get: function () {
  564. return this._ready;
  565. },
  566. },
  567. /**
  568. * Gets an event that is raised when the model encounters an asynchronous rendering error. By subscribing
  569. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  570. * are passed an instance of {@link ModelError}.
  571. * @memberof Model.prototype
  572. * @type {Event}
  573. * @readonly
  574. */
  575. errorEvent: {
  576. get: function () {
  577. return this._errorEvent;
  578. },
  579. },
  580. /**
  581. * Gets an event that is raised when the model is loaded and ready for rendering, i.e. when the external resources
  582. * have been downloaded and the WebGL resources are created. Event listeners
  583. * are passed an instance of the {@link Model}.
  584. *
  585. * <p>
  586. * If {@link Model.incrementallyLoadTextures} is true, this event will be raised before all textures are loaded and ready for rendering. Subscribe to {@link Model.texturesReadyEvent} to be notified when the textures are ready.
  587. * </p>
  588. *
  589. * @memberof Model.prototype
  590. * @type {Event}
  591. * @readonly
  592. */
  593. readyEvent: {
  594. get: function () {
  595. return this._readyEvent;
  596. },
  597. },
  598. /**
  599. * Returns true if textures are loaded separately from the other glTF resources.
  600. *
  601. * @memberof GltfLoader.prototype
  602. *
  603. * @type {boolean}
  604. * @readonly
  605. * @private
  606. */
  607. incrementallyLoadTextures: {
  608. get: function () {
  609. return defaultValue(this._loader.incrementallyLoadTextures, false);
  610. },
  611. },
  612. /**
  613. * Gets an event that, if {@link Model.incrementallyLoadTextures} is true, is raised when the model textures are loaded and ready for rendering, i.e. when the external resources
  614. * have been downloaded and the WebGL resources are created. Event listeners
  615. * are passed an instance of the {@link Model}.
  616. *
  617. * @memberof Model.prototype
  618. * @type {Event}
  619. * @readonly
  620. */
  621. texturesReadyEvent: {
  622. get: function () {
  623. return this._texturesReadyEvent;
  624. },
  625. },
  626. /**
  627. * Gets the promise that will be resolved when this model is ready to render, i.e. when the external resources
  628. * have been downloaded and the WebGL resources are created.
  629. * <p>
  630. * This promise is resolved at the end of the frame before the first frame the model is rendered in.
  631. * </p>
  632. *
  633. * @memberof Model.prototype
  634. *
  635. * @type {Promise<Model>}
  636. * @readonly
  637. * @deprecated
  638. */
  639. readyPromise: {
  640. get: function () {
  641. deprecationWarning(
  642. "Model.readyPromise",
  643. "Model.readyPromise was deprecated in CesiumJS 1.104. It will be removed in 1.107. Use Model.fromGltfAsync and Model.readyEvent instead."
  644. );
  645. return this._readyPromise;
  646. },
  647. },
  648. /**
  649. * A promise that resolves when all textures are loaded.
  650. * When <code>incrementallyLoadTextures</code> is true this may resolve after
  651. * <code>promise</code> resolves.
  652. *
  653. * @memberof Model.prototype
  654. *
  655. * @type {Promise<void>}
  656. * @readonly
  657. * @deprecated
  658. *
  659. * @private
  660. */
  661. texturesLoadedPromise: {
  662. get: function () {
  663. deprecationWarning(
  664. "Model.texturesLoadedPromise",
  665. "Model.texturesLoadedPromise was deprecated in CesiumJS 1.104. It will be removed in 1.107. Use Model.fromGltfAsync and Model.texturesReadyEvent instead."
  666. );
  667. return this._texturesLoadedPromise;
  668. },
  669. },
  670. /**
  671. * @private
  672. */
  673. loader: {
  674. get: function () {
  675. return this._loader;
  676. },
  677. },
  678. /**
  679. * Get the estimated memory usage statistics for this model.
  680. *
  681. * @memberof Model.prototype
  682. *
  683. * @type {ModelStatistics}
  684. * @readonly
  685. *
  686. * @private
  687. */
  688. statistics: {
  689. get: function () {
  690. return this._statistics;
  691. },
  692. },
  693. /**
  694. * The currently playing glTF animations.
  695. *
  696. * @memberof Model.prototype
  697. *
  698. * @type {ModelAnimationCollection}
  699. * @readonly
  700. */
  701. activeAnimations: {
  702. get: function () {
  703. return this._activeAnimations;
  704. },
  705. },
  706. /**
  707. * Determines if the model's animations should hold a pose over frames where no keyframes are specified.
  708. *
  709. * @memberof Model.prototype
  710. * @type {boolean}
  711. *
  712. * @default true
  713. */
  714. clampAnimations: {
  715. get: function () {
  716. return this._clampAnimations;
  717. },
  718. set: function (value) {
  719. this._clampAnimations = value;
  720. },
  721. },
  722. /**
  723. * Whether or not to cull the model using frustum/horizon culling. If the model is part of a 3D Tiles tileset, this property
  724. * will always be false, since the 3D Tiles culling system is used.
  725. *
  726. * @memberof Model.prototype
  727. *
  728. * @type {boolean}
  729. * @readonly
  730. *
  731. * @private
  732. */
  733. cull: {
  734. get: function () {
  735. return this._cull;
  736. },
  737. },
  738. /**
  739. * The pass to use in the {@link DrawCommand} for the opaque portions of the model.
  740. *
  741. * @memberof Model.prototype
  742. *
  743. * @type {Pass}
  744. * @readonly
  745. *
  746. * @private
  747. */
  748. opaquePass: {
  749. get: function () {
  750. return this._opaquePass;
  751. },
  752. },
  753. /**
  754. * Point cloud shading settings for controlling point cloud attenuation
  755. * and lighting. For 3D Tiles, this is inherited from the
  756. * {@link Cesium3DTileset}.
  757. *
  758. * @memberof Model.prototype
  759. *
  760. * @type {PointCloudShading}
  761. */
  762. pointCloudShading: {
  763. get: function () {
  764. return this._pointCloudShading;
  765. },
  766. set: function (value) {
  767. //>>includeStart('debug', pragmas.debug);
  768. Check.defined("pointCloudShading", value);
  769. //>>includeEnd('debug');
  770. if (value !== this._pointCloudShading) {
  771. this.resetDrawCommands();
  772. }
  773. this._pointCloudShading = value;
  774. },
  775. },
  776. /**
  777. * The model's custom shader, if it exists. Using custom shaders with a {@link Cesium3DTileStyle}
  778. * may lead to undefined behavior.
  779. *
  780. * @memberof Model.prototype
  781. *
  782. * @type {CustomShader}
  783. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  784. */
  785. customShader: {
  786. get: function () {
  787. return this._customShader;
  788. },
  789. set: function (value) {
  790. if (value !== this._customShader) {
  791. this.resetDrawCommands();
  792. }
  793. this._customShader = value;
  794. },
  795. },
  796. /**
  797. * The scene graph of this model.
  798. *
  799. * @memberof Model.prototype
  800. *
  801. * @type {ModelSceneGraph}
  802. * @private
  803. */
  804. sceneGraph: {
  805. get: function () {
  806. return this._sceneGraph;
  807. },
  808. },
  809. /**
  810. * The tile content this model belongs to, if it is loaded as part of a {@link Cesium3DTileset}.
  811. *
  812. * @memberof Model.prototype
  813. *
  814. * @type {Cesium3DTileContent}
  815. * @readonly
  816. *
  817. * @private
  818. */
  819. content: {
  820. get: function () {
  821. return this._content;
  822. },
  823. },
  824. /**
  825. * The height reference of the model, which determines how the model is drawn
  826. * relative to terrain.
  827. *
  828. * @memberof Model.prototype
  829. *
  830. * @type {HeightReference}
  831. * @default {HeightReference.NONE}
  832. *
  833. */
  834. heightReference: {
  835. get: function () {
  836. return this._heightReference;
  837. },
  838. set: function (value) {
  839. if (value !== this._heightReference) {
  840. this._heightDirty = true;
  841. }
  842. this._heightReference = value;
  843. },
  844. },
  845. /**
  846. * Gets or sets the distance display condition, which specifies at what distance
  847. * from the camera this model will be displayed.
  848. *
  849. * @memberof Model.prototype
  850. *
  851. * @type {DistanceDisplayCondition}
  852. *
  853. * @default undefined
  854. *
  855. */
  856. distanceDisplayCondition: {
  857. get: function () {
  858. return this._distanceDisplayCondition;
  859. },
  860. set: function (value) {
  861. //>>includeStart('debug', pragmas.debug);
  862. if (defined(value) && value.far <= value.near) {
  863. throw new DeveloperError("far must be greater than near");
  864. }
  865. //>>includeEnd('debug');
  866. this._distanceDisplayCondition = DistanceDisplayCondition.clone(
  867. value,
  868. this._distanceDisplayCondition
  869. );
  870. },
  871. },
  872. /**
  873. * The structural metadata from the EXT_structural_metadata extension
  874. *
  875. * @memberof Model.prototype
  876. *
  877. * @type {StructuralMetadata}
  878. * @readonly
  879. *
  880. * @private
  881. */
  882. structuralMetadata: {
  883. get: function () {
  884. return this._sceneGraph.components.structuralMetadata;
  885. },
  886. },
  887. /**
  888. * The ID for the feature table to use for picking and styling in this model.
  889. *
  890. * @memberof Model.prototype
  891. *
  892. * @type {number}
  893. *
  894. * @private
  895. */
  896. featureTableId: {
  897. get: function () {
  898. return this._featureTableId;
  899. },
  900. set: function (value) {
  901. this._featureTableId = value;
  902. },
  903. },
  904. /**
  905. * The feature tables for this model.
  906. *
  907. * @memberof Model.prototype
  908. *
  909. * @type {Array}
  910. * @readonly
  911. *
  912. * @private
  913. */
  914. featureTables: {
  915. get: function () {
  916. return this._featureTables;
  917. },
  918. set: function (value) {
  919. this._featureTables = value;
  920. },
  921. },
  922. /**
  923. * A user-defined object that is returned when the model is picked.
  924. *
  925. * @memberof Model.prototype
  926. *
  927. * @type {object}
  928. *
  929. * @default undefined
  930. *
  931. * @see Scene#pick
  932. */
  933. id: {
  934. get: function () {
  935. return this._id;
  936. },
  937. set: function (value) {
  938. if (value !== this._id) {
  939. this._idDirty = true;
  940. }
  941. this._id = value;
  942. },
  943. },
  944. /**
  945. * When <code>true</code>, each primitive is pickable with {@link Scene#pick}. When <code>false</code>, GPU memory is saved.
  946. *
  947. * @memberof Model.prototype
  948. *
  949. * @type {boolean}
  950. * @readonly
  951. *
  952. * @private
  953. */
  954. allowPicking: {
  955. get: function () {
  956. return this._allowPicking;
  957. },
  958. },
  959. /**
  960. * The style to apply to the features in the model. Cannot be applied if a {@link CustomShader} is also applied.
  961. *
  962. * @memberof Model.prototype
  963. *
  964. * @type {Cesium3DTileStyle}
  965. */
  966. style: {
  967. get: function () {
  968. return this._style;
  969. },
  970. set: function (value) {
  971. this._style = value;
  972. this._styleDirty = true;
  973. },
  974. },
  975. /**
  976. * The color to blend with the model's rendered color.
  977. *
  978. * @memberof Model.prototype
  979. *
  980. * @type {Color}
  981. *
  982. * @default undefined
  983. */
  984. color: {
  985. get: function () {
  986. return this._color;
  987. },
  988. set: function (value) {
  989. if (isColorAlphaDirty(value, this._color)) {
  990. this.resetDrawCommands();
  991. }
  992. this._color = Color.clone(value, this._color);
  993. },
  994. },
  995. /**
  996. * Defines how the color blends with the model.
  997. *
  998. * @memberof Model.prototype
  999. *
  1000. * @type {Cesium3DTileColorBlendMode|ColorBlendMode}
  1001. *
  1002. * @default ColorBlendMode.HIGHLIGHT
  1003. */
  1004. colorBlendMode: {
  1005. get: function () {
  1006. return this._colorBlendMode;
  1007. },
  1008. set: function (value) {
  1009. this._colorBlendMode = value;
  1010. },
  1011. },
  1012. /**
  1013. * Value used to determine the color strength when the <code>colorBlendMode</code> is <code>MIX</code>. A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with any value in-between resulting in a mix of the two.
  1014. *
  1015. * @memberof Model.prototype
  1016. *
  1017. * @type {number}
  1018. *
  1019. * @default 0.5
  1020. */
  1021. colorBlendAmount: {
  1022. get: function () {
  1023. return this._colorBlendAmount;
  1024. },
  1025. set: function (value) {
  1026. this._colorBlendAmount = value;
  1027. },
  1028. },
  1029. /**
  1030. * The silhouette color.
  1031. *
  1032. * @memberof Model.prototype
  1033. *
  1034. * @type {Color}
  1035. *
  1036. * @default Color.RED
  1037. */
  1038. silhouetteColor: {
  1039. get: function () {
  1040. return this._silhouetteColor;
  1041. },
  1042. set: function (value) {
  1043. if (!Color.equals(value, this._silhouetteColor)) {
  1044. const alphaDirty = isColorAlphaDirty(value, this._silhouetteColor);
  1045. this._silhouetteDirty = this._silhouetteDirty || alphaDirty;
  1046. }
  1047. this._silhouetteColor = Color.clone(value, this._silhouetteColor);
  1048. },
  1049. },
  1050. /**
  1051. * The size of the silhouette in pixels.
  1052. *
  1053. * @memberof Model.prototype
  1054. *
  1055. * @type {number}
  1056. *
  1057. * @default 0.0
  1058. */
  1059. silhouetteSize: {
  1060. get: function () {
  1061. return this._silhouetteSize;
  1062. },
  1063. set: function (value) {
  1064. if (value !== this._silhouetteSize) {
  1065. const currentSize = this._silhouetteSize;
  1066. const sizeDirty =
  1067. (value > 0.0 && currentSize === 0.0) ||
  1068. (value === 0.0 && currentSize > 0.0);
  1069. this._silhouetteDirty = this._silhouetteDirty || sizeDirty;
  1070. // Back-face culling needs to be updated in case the silhouette size
  1071. // is greater than zero.
  1072. this._backFaceCullingDirty = this._backFaceCullingDirty || sizeDirty;
  1073. }
  1074. this._silhouetteSize = value;
  1075. },
  1076. },
  1077. /**
  1078. * Gets the model's bounding sphere in world space. This does not take into account
  1079. * glTF animations, skins, or morph targets. It also does not account for
  1080. * {@link Model#minimumPixelSize}.
  1081. *
  1082. * @memberof Model.prototype
  1083. *
  1084. * @type {BoundingSphere}
  1085. * @readonly
  1086. */
  1087. boundingSphere: {
  1088. get: function () {
  1089. //>>includeStart('debug', pragmas.debug);
  1090. if (!this._ready) {
  1091. throw new DeveloperError(
  1092. "The model is not loaded. Use Model.readyEvent or wait for Model.ready to be true."
  1093. );
  1094. }
  1095. //>>includeEnd('debug');
  1096. const modelMatrix = defined(this._clampedModelMatrix)
  1097. ? this._clampedModelMatrix
  1098. : this.modelMatrix;
  1099. updateBoundingSphere(this, modelMatrix);
  1100. return this._boundingSphere;
  1101. },
  1102. },
  1103. /**
  1104. * This property is for debugging only; it is not for production use nor is it optimized.
  1105. * <p>
  1106. * Draws the bounding sphere for each draw command in the model.
  1107. * </p>
  1108. *
  1109. * @memberof Model.prototype
  1110. *
  1111. * @type {boolean}
  1112. *
  1113. * @default false
  1114. */
  1115. debugShowBoundingVolume: {
  1116. get: function () {
  1117. return this._debugShowBoundingVolume;
  1118. },
  1119. set: function (value) {
  1120. if (this._debugShowBoundingVolume !== value) {
  1121. this._debugShowBoundingVolumeDirty = true;
  1122. }
  1123. this._debugShowBoundingVolume = value;
  1124. },
  1125. },
  1126. /**
  1127. * This property is for debugging only; it is not for production use nor is it optimized.
  1128. * <p>
  1129. * Draws the model in wireframe.
  1130. * </p>
  1131. *
  1132. * @memberof Model.prototype
  1133. *
  1134. * @type {boolean}
  1135. *
  1136. * @default false
  1137. */
  1138. debugWireframe: {
  1139. get: function () {
  1140. return this._debugWireframe;
  1141. },
  1142. set: function (value) {
  1143. if (this._debugWireframe !== value) {
  1144. this.resetDrawCommands();
  1145. }
  1146. this._debugWireframe = value;
  1147. // Warning for improper setup of debug wireframe
  1148. if (
  1149. this._debugWireframe === true &&
  1150. this._enableDebugWireframe === false &&
  1151. this.type === ModelType.GLTF
  1152. ) {
  1153. oneTimeWarning(
  1154. "model-debug-wireframe-ignored",
  1155. "enableDebugWireframe must be set to true in Model.fromGltfAsync, otherwise debugWireframe will be ignored."
  1156. );
  1157. }
  1158. },
  1159. },
  1160. /**
  1161. * Whether or not to render the model.
  1162. *
  1163. * @memberof Model.prototype
  1164. *
  1165. * @type {boolean}
  1166. *
  1167. * @default true
  1168. */
  1169. show: {
  1170. get: function () {
  1171. return this._show;
  1172. },
  1173. set: function (value) {
  1174. this._show = value;
  1175. },
  1176. },
  1177. /**
  1178. * Label of the feature ID set to use for picking and styling.
  1179. * <p>
  1180. * For EXT_mesh_features, this is the feature ID's label property, or
  1181. * "featureId_N" (where N is the index in the featureIds array) when not
  1182. * specified. EXT_feature_metadata did not have a label field, so such
  1183. * feature ID sets are always labeled "featureId_N" where N is the index in
  1184. * the list of all feature Ids, where feature ID attributes are listed before
  1185. * feature ID textures.
  1186. * </p>
  1187. * <p>
  1188. * If featureIdLabel is set to an integer N, it is converted to
  1189. * the string "featureId_N" automatically. If both per-primitive and
  1190. * per-instance feature IDs are present, the instance feature IDs take
  1191. * priority.
  1192. * </p>
  1193. *
  1194. * @memberof Model.prototype
  1195. *
  1196. * @type {string}
  1197. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  1198. */
  1199. featureIdLabel: {
  1200. get: function () {
  1201. return this._featureIdLabel;
  1202. },
  1203. set: function (value) {
  1204. // indices get converted into featureId_N
  1205. if (typeof value === "number") {
  1206. value = `featureId_${value}`;
  1207. }
  1208. //>>includeStart('debug', pragmas.debug);
  1209. Check.typeOf.string("value", value);
  1210. //>>includeEnd('debug');
  1211. if (value !== this._featureIdLabel) {
  1212. this._featureTableIdDirty = true;
  1213. }
  1214. this._featureIdLabel = value;
  1215. },
  1216. },
  1217. /**
  1218. * Label of the instance feature ID set used for picking and styling.
  1219. * <p>
  1220. * If instanceFeatureIdLabel is set to an integer N, it is converted to
  1221. * the string "instanceFeatureId_N" automatically.
  1222. * If both per-primitive and per-instance feature IDs are present, the
  1223. * instance feature IDs take priority.
  1224. * </p>
  1225. *
  1226. * @memberof Model.prototype
  1227. *
  1228. * @type {string}
  1229. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  1230. */
  1231. instanceFeatureIdLabel: {
  1232. get: function () {
  1233. return this._instanceFeatureIdLabel;
  1234. },
  1235. set: function (value) {
  1236. // indices get converted into instanceFeatureId_N
  1237. if (typeof value === "number") {
  1238. value = `instanceFeatureId_${value}`;
  1239. }
  1240. //>>includeStart('debug', pragmas.debug);
  1241. Check.typeOf.string("value", value);
  1242. //>>includeEnd('debug');
  1243. if (value !== this._instanceFeatureIdLabel) {
  1244. this._featureTableIdDirty = true;
  1245. }
  1246. this._instanceFeatureIdLabel = value;
  1247. },
  1248. },
  1249. /**
  1250. * The {@link ClippingPlaneCollection} used to selectively disable rendering the model.
  1251. *
  1252. * @memberof Model.prototype
  1253. *
  1254. * @type {ClippingPlaneCollection}
  1255. */
  1256. clippingPlanes: {
  1257. get: function () {
  1258. return this._clippingPlanes;
  1259. },
  1260. set: function (value) {
  1261. if (value !== this._clippingPlanes) {
  1262. // Handle destroying old clipping planes, new clipping planes ownership
  1263. ClippingPlaneCollection.setOwner(value, this, "_clippingPlanes");
  1264. this.resetDrawCommands();
  1265. }
  1266. },
  1267. },
  1268. /**
  1269. * The light color when shading the model. When <code>undefined</code> the scene's light color is used instead.
  1270. * <p>
  1271. * Disabling additional light sources by setting
  1272. * <code>model.imageBasedLighting.imageBasedLightingFactor = new Cartesian2(0.0, 0.0)</code>
  1273. * will make the model much darker. Here, increasing the intensity of the light source will make the model brighter.
  1274. * </p>
  1275. * @memberof Model.prototype
  1276. *
  1277. * @type {Cartesian3}
  1278. *
  1279. * @default undefined
  1280. */
  1281. lightColor: {
  1282. get: function () {
  1283. return this._lightColor;
  1284. },
  1285. set: function (value) {
  1286. if (defined(value) !== defined(this._lightColor)) {
  1287. this.resetDrawCommands();
  1288. }
  1289. this._lightColor = Cartesian3.clone(value, this._lightColor);
  1290. },
  1291. },
  1292. /**
  1293. * The properties for managing image-based lighting on this model.
  1294. *
  1295. * @memberof Model.prototype
  1296. *
  1297. * @type {ImageBasedLighting}
  1298. */
  1299. imageBasedLighting: {
  1300. get: function () {
  1301. return this._imageBasedLighting;
  1302. },
  1303. set: function (value) {
  1304. //>>includeStart('debug', pragmas.debug);
  1305. Check.typeOf.object("imageBasedLighting", this._imageBasedLighting);
  1306. //>>includeEnd('debug');
  1307. if (value !== this._imageBasedLighting) {
  1308. if (
  1309. this._shouldDestroyImageBasedLighting &&
  1310. !this._imageBasedLighting.isDestroyed()
  1311. ) {
  1312. this._imageBasedLighting.destroy();
  1313. }
  1314. this._imageBasedLighting = value;
  1315. this._shouldDestroyImageBasedLighting = false;
  1316. this.resetDrawCommands();
  1317. }
  1318. },
  1319. },
  1320. /**
  1321. * Whether to cull back-facing geometry. When true, back face culling is
  1322. * determined by the material's doubleSided property; when false, back face
  1323. * culling is disabled. Back faces are not culled if {@link Model#color}
  1324. * is translucent or {@link Model#silhouetteSize} is greater than 0.0.
  1325. *
  1326. * @memberof Model.prototype
  1327. *
  1328. * @type {boolean}
  1329. *
  1330. * @default true
  1331. */
  1332. backFaceCulling: {
  1333. get: function () {
  1334. return this._backFaceCulling;
  1335. },
  1336. set: function (value) {
  1337. if (value !== this._backFaceCulling) {
  1338. this._backFaceCullingDirty = true;
  1339. }
  1340. this._backFaceCulling = value;
  1341. },
  1342. },
  1343. /**
  1344. * A uniform scale applied to this model before the {@link Model#modelMatrix}.
  1345. * Values greater than <code>1.0</code> increase the size of the model; values
  1346. * less than <code>1.0</code> decrease.
  1347. *
  1348. * @memberof Model.prototype
  1349. *
  1350. * @type {number}
  1351. *
  1352. * @default 1.0
  1353. */
  1354. scale: {
  1355. get: function () {
  1356. return this._scale;
  1357. },
  1358. set: function (value) {
  1359. if (value !== this._scale) {
  1360. this._updateModelMatrix = true;
  1361. }
  1362. this._scale = value;
  1363. },
  1364. },
  1365. /**
  1366. * The true scale of the model after being affected by the model's scale,
  1367. * minimum pixel size, and maximum scale parameters.
  1368. *
  1369. * @memberof Model.prototype
  1370. *
  1371. * @type {number}
  1372. * @readonly
  1373. *
  1374. * @private
  1375. */
  1376. computedScale: {
  1377. get: function () {
  1378. return this._computedScale;
  1379. },
  1380. },
  1381. /**
  1382. * The approximate minimum pixel size of the model regardless of zoom.
  1383. * This can be used to ensure that a model is visible even when the viewer
  1384. * zooms out. When <code>0.0</code>, no minimum size is enforced.
  1385. *
  1386. * @memberof Model.prototype
  1387. *
  1388. * @type {number}
  1389. *
  1390. * @default 0.0
  1391. */
  1392. minimumPixelSize: {
  1393. get: function () {
  1394. return this._minimumPixelSize;
  1395. },
  1396. set: function (value) {
  1397. if (value !== this._minimumPixelSize) {
  1398. this._updateModelMatrix = true;
  1399. }
  1400. this._minimumPixelSize = value;
  1401. },
  1402. },
  1403. /**
  1404. * The maximum scale size for a model. This can be used to give
  1405. * an upper limit to the {@link Model#minimumPixelSize}, ensuring that the model
  1406. * is never an unreasonable scale.
  1407. *
  1408. * @memberof Model.prototype
  1409. *
  1410. * @type {number}
  1411. */
  1412. maximumScale: {
  1413. get: function () {
  1414. return this._maximumScale;
  1415. },
  1416. set: function (value) {
  1417. if (value !== this._maximumScale) {
  1418. this._updateModelMatrix = true;
  1419. }
  1420. this._maximumScale = value;
  1421. },
  1422. },
  1423. /**
  1424. * Determines whether the model casts or receives shadows from light sources.
  1425. * @memberof Model.prototype
  1426. *
  1427. * @type {ShadowMode}
  1428. *
  1429. * @default ShadowMode.ENABLED
  1430. */
  1431. shadows: {
  1432. get: function () {
  1433. return this._shadows;
  1434. },
  1435. set: function (value) {
  1436. if (value !== this._shadows) {
  1437. this._shadowsDirty = true;
  1438. }
  1439. this._shadows = value;
  1440. },
  1441. },
  1442. /**
  1443. * Gets the credit that will be displayed for the model.
  1444. *
  1445. * @memberof Model.prototype
  1446. *
  1447. * @type {Credit}
  1448. * @readonly
  1449. */
  1450. credit: {
  1451. get: function () {
  1452. return this._credit;
  1453. },
  1454. },
  1455. /**
  1456. * Gets or sets whether the credits of the model will be displayed
  1457. * on the screen.
  1458. *
  1459. * @memberof Model.prototype
  1460. *
  1461. * @type {boolean}
  1462. *
  1463. * @default false
  1464. */
  1465. showCreditsOnScreen: {
  1466. get: function () {
  1467. return this._showCreditsOnScreen;
  1468. },
  1469. set: function (value) {
  1470. if (this._showCreditsOnScreen !== value) {
  1471. this._showCreditsOnScreenDirty = true;
  1472. }
  1473. this._showCreditsOnScreen = value;
  1474. },
  1475. },
  1476. /**
  1477. * The {@link SplitDirection} to apply to this model.
  1478. *
  1479. * @memberof Model.prototype
  1480. *
  1481. * @type {SplitDirection}
  1482. *
  1483. * @default {@link SplitDirection.NONE}
  1484. */
  1485. splitDirection: {
  1486. get: function () {
  1487. return this._splitDirection;
  1488. },
  1489. set: function (value) {
  1490. if (this._splitDirection !== value) {
  1491. this.resetDrawCommands();
  1492. }
  1493. this._splitDirection = value;
  1494. },
  1495. },
  1496. /**
  1497. * Gets the model's classification type. This determines whether terrain,
  1498. * 3D Tiles, or both will be classified by this model.
  1499. * <p>
  1500. * Additionally, there are a few requirements/limitations:
  1501. * <ul>
  1502. * <li>The glTF cannot contain morph targets, skins, or animations.</li>
  1503. * <li>The glTF cannot contain the <code>EXT_mesh_gpu_instancing</code> extension.</li>
  1504. * <li>Only meshes with TRIANGLES can be used to classify other assets.</li>
  1505. * <li>The position attribute is required.</li>
  1506. * <li>If feature IDs and an index buffer are both present, all indices with the same feature id must occupy contiguous sections of the index buffer.</li>
  1507. * <li>If feature IDs are present without an index buffer, all positions with the same feature id must occupy contiguous sections of the position buffer.</li>
  1508. * </ul>
  1509. * </p>
  1510. *
  1511. * @memberof Model.prototype
  1512. *
  1513. * @type {ClassificationType}
  1514. * @default undefined
  1515. *
  1516. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  1517. * @readonly
  1518. */
  1519. classificationType: {
  1520. get: function () {
  1521. return this._classificationType;
  1522. },
  1523. },
  1524. /**
  1525. * Reference to the pick IDs. This is only used internally, e.g. for
  1526. * per-feature post-processing in {@link PostProcessStage}.
  1527. *
  1528. * @memberof Model.prototype
  1529. *
  1530. * @type {PickId[]}
  1531. * @readonly
  1532. *
  1533. * @private
  1534. */
  1535. pickIds: {
  1536. get: function () {
  1537. return this._pickIds;
  1538. },
  1539. },
  1540. /**
  1541. * The {@link StyleCommandsNeeded} for the style currently applied to
  1542. * the features in the model. This is used internally by the {@link ModelDrawCommand}
  1543. * when determining which commands to submit in an update.
  1544. *
  1545. * @memberof Model.prototype
  1546. *
  1547. * @type {StyleCommandsNeeded}
  1548. * @readonly
  1549. *
  1550. * @private
  1551. */
  1552. styleCommandsNeeded: {
  1553. get: function () {
  1554. return this._styleCommandsNeeded;
  1555. },
  1556. },
  1557. });
  1558. /**
  1559. * Returns the node with the given <code>name</code> in the glTF. This is used to
  1560. * modify a node's transform for user-defined animation.
  1561. *
  1562. * @param {string} name The name of the node in the glTF.
  1563. * @returns {ModelNode} The node, or <code>undefined</code> if no node with the <code>name</code> exists.
  1564. *
  1565. * @exception {DeveloperError} The model is not loaded. Use Model.readyEvent or wait for Model.ready to be true.
  1566. *
  1567. * @example
  1568. * // Apply non-uniform scale to node "Hand"
  1569. * const node = model.getNode("Hand");
  1570. * node.matrix = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(5.0, 1.0, 1.0), node.matrix);
  1571. */
  1572. Model.prototype.getNode = function (name) {
  1573. //>>includeStart('debug', pragmas.debug);
  1574. if (!this._ready) {
  1575. throw new DeveloperError(
  1576. "The model is not loaded. Use Model.readyEvent or wait for Model.ready to be true."
  1577. );
  1578. }
  1579. Check.typeOf.string("name", name);
  1580. //>>includeEnd('debug');
  1581. return this._nodesByName[name];
  1582. };
  1583. /**
  1584. * Sets the current value of an articulation stage. After setting one or
  1585. * multiple stage values, call Model.applyArticulations() to
  1586. * cause the node matrices to be recalculated.
  1587. *
  1588. * @param {string} articulationStageKey The name of the articulation, a space, and the name of the stage.
  1589. * @param {number} value The numeric value of this stage of the articulation.
  1590. *
  1591. * @exception {DeveloperError} The model is not loaded. Use Model.readyEvent or wait for Model.ready to be true.
  1592. *
  1593. * @see Model#applyArticulations
  1594. *
  1595. * @example
  1596. * // Sets the value of the stage named "MoveX" belonging to the articulation named "SampleArticulation"
  1597. * model.setArticulationStage("SampleArticulation MoveX", 50.0);
  1598. */
  1599. Model.prototype.setArticulationStage = function (articulationStageKey, value) {
  1600. //>>includeStart('debug', pragmas.debug);
  1601. Check.typeOf.number("value", value);
  1602. if (!this._ready) {
  1603. throw new DeveloperError(
  1604. "The model is not loaded. Use Model.readyEvent or wait for Model.ready to be true."
  1605. );
  1606. }
  1607. //>>includeEnd('debug');
  1608. this._sceneGraph.setArticulationStage(articulationStageKey, value);
  1609. };
  1610. /**
  1611. * Applies any modified articulation stages to the matrix of each node that
  1612. * participates in any articulation. Note that this will overwrite any node
  1613. * transformations on participating nodes.
  1614. *
  1615. * @exception {DeveloperError} The model is not loaded. Use Model.readyEvent or wait for Model.ready to be true.
  1616. */
  1617. Model.prototype.applyArticulations = function () {
  1618. //>>includeStart('debug', pragmas.debug);
  1619. if (!this._ready) {
  1620. throw new DeveloperError(
  1621. "The model is not loaded. Use Model.readyEvent or wait for Model.ready to be true."
  1622. );
  1623. }
  1624. //>>includeEnd('debug');
  1625. this._sceneGraph.applyArticulations();
  1626. };
  1627. /**
  1628. * Marks the model's {@link Model#style} as dirty, which forces all features
  1629. * to re-evaluate the style in the next frame the model is visible.
  1630. */
  1631. Model.prototype.makeStyleDirty = function () {
  1632. this._styleDirty = true;
  1633. };
  1634. /**
  1635. * Resets the draw commands for this model.
  1636. *
  1637. * @private
  1638. */
  1639. Model.prototype.resetDrawCommands = function () {
  1640. this._drawCommandsBuilt = false;
  1641. };
  1642. const scratchIBLReferenceFrameMatrix4 = new Matrix4();
  1643. const scratchIBLReferenceFrameMatrix3 = new Matrix3();
  1644. const scratchClippingPlanesMatrix = new Matrix4();
  1645. /**
  1646. * Called when {@link Viewer} or {@link CesiumWidget} render the scene to
  1647. * get the draw commands needed to render this primitive.
  1648. * <p>
  1649. * Do not call this function directly. This is documented just to
  1650. * list the exceptions that may be propagated when the scene is rendered:
  1651. * </p>
  1652. *
  1653. * @exception {RuntimeError} Failed to load external reference.
  1654. */
  1655. Model.prototype.update = function (frameState) {
  1656. let finishedProcessing = false;
  1657. try {
  1658. // Keep processing the model every frame until the main resources
  1659. // (buffer views) and textures (which may be loaded asynchronously)
  1660. // are processed.
  1661. finishedProcessing = processLoader(this, frameState);
  1662. } catch (error) {
  1663. if (!this._loader.incrementallyLoadTextures) {
  1664. const runtimeError = ModelUtility.getError(
  1665. "model",
  1666. this._resource,
  1667. error
  1668. );
  1669. handleError(this, runtimeError);
  1670. this._rejectLoad = this._rejectLoad && this._rejectLoad(runtimeError);
  1671. this._rejectTexturesLoad =
  1672. this._rejectTexturesLoad && this._rejectTexturesLoad(runtimeError);
  1673. } else if (error.name === "TextureError") {
  1674. handleError(this, error);
  1675. this._rejectTexturesLoad =
  1676. this._rejectTexturesLoad && this._rejectTexturesLoad(error);
  1677. } else {
  1678. const runtimeError = ModelUtility.getError(
  1679. "model",
  1680. this._resource,
  1681. error
  1682. );
  1683. handleError(this, runtimeError);
  1684. this._rejectLoad = this._rejectLoad && this._rejectLoad(runtimeError);
  1685. }
  1686. }
  1687. // A custom shader may have to load texture uniforms.
  1688. updateCustomShader(this, frameState);
  1689. // The image-based lighting may have to load texture uniforms
  1690. // for specular maps.
  1691. updateImageBasedLighting(this, frameState);
  1692. if (!this._resourcesLoaded && finishedProcessing) {
  1693. this._resourcesLoaded = true;
  1694. const components = this._loader.components;
  1695. if (!defined(components)) {
  1696. if (this._loader.isUnloaded()) {
  1697. return;
  1698. }
  1699. const error = ModelUtility.getError(
  1700. "model",
  1701. this._resource,
  1702. new RuntimeError("Failed to load model.")
  1703. );
  1704. handleError(error);
  1705. this._rejectLoad = this._rejectLoad && this._rejectLoad(error);
  1706. }
  1707. const structuralMetadata = components.structuralMetadata;
  1708. if (
  1709. defined(structuralMetadata) &&
  1710. structuralMetadata.propertyTableCount > 0
  1711. ) {
  1712. createModelFeatureTables(this, structuralMetadata);
  1713. }
  1714. const sceneGraph = new ModelSceneGraph({
  1715. model: this,
  1716. modelComponents: components,
  1717. });
  1718. this._sceneGraph = sceneGraph;
  1719. this._gltfCredits = sceneGraph.components.asset.credits;
  1720. }
  1721. // Short-circuit if the model resources aren't ready or the scene
  1722. // is currently morphing.
  1723. if (!this._resourcesLoaded || frameState.mode === SceneMode.MORPHING) {
  1724. return;
  1725. }
  1726. updateFeatureTableId(this);
  1727. updateStyle(this);
  1728. updateFeatureTables(this, frameState);
  1729. updatePointCloudShading(this);
  1730. updateSilhouette(this, frameState);
  1731. updateSkipLevelOfDetail(this, frameState);
  1732. updateClippingPlanes(this, frameState);
  1733. updateSceneMode(this, frameState);
  1734. this._defaultTexture = frameState.context.defaultTexture;
  1735. buildDrawCommands(this, frameState);
  1736. updateModelMatrix(this, frameState);
  1737. // Many features (e.g. image-based lighting, clipping planes) depend on the model
  1738. // matrix being updated for the current height reference, so update it first.
  1739. updateClamping(this);
  1740. updateBoundingSphereAndScale(this, frameState);
  1741. updateReferenceMatrices(this, frameState);
  1742. // This check occurs after the bounding sphere has been updated so that
  1743. // zooming to the bounding sphere can account for any modifications
  1744. // from the clamp-to-ground setting.
  1745. if (!this._ready) {
  1746. // Set the model as ready after the first frame render since the user might set up events subscribed to
  1747. // the post render event, and the model may not be ready for those past the first frame.
  1748. frameState.afterRender.push(() => {
  1749. this._ready = true;
  1750. this._readyEvent.raiseEvent(this);
  1751. // This is here for backwards compatibility and can be removed when readyPromise is removed.
  1752. this._completeLoad = this._completeLoad && this._completeLoad();
  1753. if (!this._loader.incrementallyLoadTextures) {
  1754. this._texturesLoaded = true;
  1755. this._texturesReadyEvent.raiseEvent(this);
  1756. this._completeTexturesLoad =
  1757. this._completeTexturesLoad && this._completeTexturesLoad();
  1758. }
  1759. });
  1760. // Don't render until the next frame after the ready promise is resolved
  1761. return;
  1762. }
  1763. if (
  1764. this._loader.incrementallyLoadTextures &&
  1765. !this._texturesLoaded &&
  1766. this._loader.texturesLoaded
  1767. ) {
  1768. // Re-run the pipeline so texture memory statistics are re-computed
  1769. this.resetDrawCommands();
  1770. this._texturesLoaded = true;
  1771. this._texturesReadyEvent.raiseEvent(this);
  1772. this._completeTexturesLoad =
  1773. this._completeTexturesLoad && this._completeTexturesLoad();
  1774. }
  1775. updatePickIds(this);
  1776. // Update the scene graph and draw commands for any changes in model's properties
  1777. // (e.g. model matrix, back-face culling)
  1778. updateSceneGraph(this, frameState);
  1779. updateShowCreditsOnScreen(this);
  1780. submitDrawCommands(this, frameState);
  1781. };
  1782. function processLoader(model, frameState) {
  1783. if (!model._resourcesLoaded || !model._texturesLoaded) {
  1784. // Ensures frames continue to render in requestRender mode while resources are processing
  1785. frameState.afterRender.push(() => true);
  1786. return model._loader.process(frameState);
  1787. }
  1788. return true;
  1789. }
  1790. function updateCustomShader(model, frameState) {
  1791. if (defined(model._customShader)) {
  1792. model._customShader.update(frameState);
  1793. }
  1794. }
  1795. function updateImageBasedLighting(model, frameState) {
  1796. model._imageBasedLighting.update(frameState);
  1797. if (model._imageBasedLighting.shouldRegenerateShaders) {
  1798. model.resetDrawCommands();
  1799. }
  1800. }
  1801. function updateFeatureTableId(model) {
  1802. if (!model._featureTableIdDirty) {
  1803. return;
  1804. }
  1805. model._featureTableIdDirty = false;
  1806. const components = model._sceneGraph.components;
  1807. const structuralMetadata = components.structuralMetadata;
  1808. if (
  1809. defined(structuralMetadata) &&
  1810. structuralMetadata.propertyTableCount > 0
  1811. ) {
  1812. model.featureTableId = selectFeatureTableId(components, model);
  1813. // Mark the style dirty to re-apply it and reflect the new feature ID table.
  1814. model._styleDirty = true;
  1815. // Trigger a rebuild of the draw commands.
  1816. model.resetDrawCommands();
  1817. }
  1818. }
  1819. function updateStyle(model) {
  1820. if (model._styleDirty) {
  1821. model.applyStyle(model._style);
  1822. model._styleDirty = false;
  1823. }
  1824. }
  1825. function updateFeatureTables(model, frameState) {
  1826. const featureTables = model._featureTables;
  1827. const length = featureTables.length;
  1828. let styleCommandsNeededDirty = false;
  1829. for (let i = 0; i < length; i++) {
  1830. featureTables[i].update(frameState);
  1831. // Check if the types of style commands needed have changed and trigger a reset of the draw commands
  1832. // to ensure that translucent and opaque features are handled in the correct passes.
  1833. if (featureTables[i].styleCommandsNeededDirty) {
  1834. styleCommandsNeededDirty = true;
  1835. }
  1836. }
  1837. if (styleCommandsNeededDirty) {
  1838. updateStyleCommandsNeeded(model);
  1839. }
  1840. }
  1841. function updateStyleCommandsNeeded(model) {
  1842. const featureTable = model.featureTables[model.featureTableId];
  1843. model._styleCommandsNeeded = StyleCommandsNeeded.getStyleCommandsNeeded(
  1844. featureTable.featuresLength,
  1845. featureTable.batchTexture.translucentFeaturesLength
  1846. );
  1847. }
  1848. function updatePointCloudShading(model) {
  1849. const pointCloudShading = model.pointCloudShading;
  1850. // Check if the shader needs to be updated for point cloud attenuation
  1851. // settings.
  1852. if (pointCloudShading.attenuation !== model._attenuation) {
  1853. model.resetDrawCommands();
  1854. model._attenuation = pointCloudShading.attenuation;
  1855. }
  1856. if (pointCloudShading.backFaceCulling !== model._pointCloudBackFaceCulling) {
  1857. model.resetDrawCommands();
  1858. model._pointCloudBackFaceCulling = pointCloudShading.backFaceCulling;
  1859. }
  1860. }
  1861. function updateSilhouette(model, frameState) {
  1862. if (model._silhouetteDirty) {
  1863. // Only rebuild draw commands if silhouettes are supported in the first place.
  1864. if (supportsSilhouettes(frameState)) {
  1865. model.resetDrawCommands();
  1866. }
  1867. model._silhouetteDirty = false;
  1868. }
  1869. }
  1870. function updateSkipLevelOfDetail(model, frameState) {
  1871. const skipLevelOfDetail = model.hasSkipLevelOfDetail(frameState);
  1872. if (skipLevelOfDetail !== model._skipLevelOfDetail) {
  1873. model.resetDrawCommands();
  1874. model._skipLevelOfDetail = skipLevelOfDetail;
  1875. }
  1876. }
  1877. function updateClippingPlanes(model, frameState) {
  1878. // Update the clipping planes collection / state for this model to detect any changes.
  1879. let currentClippingPlanesState = 0;
  1880. if (model.isClippingEnabled()) {
  1881. if (model._clippingPlanes.owner === model) {
  1882. model._clippingPlanes.update(frameState);
  1883. }
  1884. currentClippingPlanesState = model._clippingPlanes.clippingPlanesState;
  1885. }
  1886. if (currentClippingPlanesState !== model._clippingPlanesState) {
  1887. model.resetDrawCommands();
  1888. model._clippingPlanesState = currentClippingPlanesState;
  1889. }
  1890. }
  1891. function updateSceneMode(model, frameState) {
  1892. if (frameState.mode !== model._sceneMode) {
  1893. if (model._projectTo2D) {
  1894. model.resetDrawCommands();
  1895. } else {
  1896. model._updateModelMatrix = true;
  1897. }
  1898. model._sceneMode = frameState.mode;
  1899. }
  1900. }
  1901. function buildDrawCommands(model, frameState) {
  1902. if (!model._drawCommandsBuilt) {
  1903. model.destroyPipelineResources();
  1904. model._sceneGraph.buildDrawCommands(frameState);
  1905. model._drawCommandsBuilt = true;
  1906. }
  1907. }
  1908. function updateModelMatrix(model, frameState) {
  1909. // This is done without a dirty flag so that the model matrix can be updated in-place
  1910. // without needing to use a setter.
  1911. if (!Matrix4.equals(model.modelMatrix, model._modelMatrix)) {
  1912. //>>includeStart('debug', pragmas.debug);
  1913. if (frameState.mode !== SceneMode.SCENE3D && model._projectTo2D) {
  1914. throw new DeveloperError(
  1915. "Model.modelMatrix cannot be changed in 2D or Columbus View if projectTo2D is true."
  1916. );
  1917. }
  1918. //>>includeEnd('debug');
  1919. model._updateModelMatrix = true;
  1920. model._modelMatrix = Matrix4.clone(model.modelMatrix, model._modelMatrix);
  1921. }
  1922. }
  1923. const scratchPosition = new Cartesian3();
  1924. const scratchCartographic = new Cartographic();
  1925. function updateClamping(model) {
  1926. if (
  1927. !model._updateModelMatrix &&
  1928. !model._heightDirty &&
  1929. model._minimumPixelSize === 0.0
  1930. ) {
  1931. return;
  1932. }
  1933. if (defined(model._removeUpdateHeightCallback)) {
  1934. model._removeUpdateHeightCallback();
  1935. model._removeUpdateHeightCallback = undefined;
  1936. }
  1937. const scene = model._scene;
  1938. if (
  1939. !defined(scene) ||
  1940. !defined(scene.globe) ||
  1941. model.heightReference === HeightReference.NONE
  1942. ) {
  1943. //>>includeStart('debug', pragmas.debug);
  1944. if (model.heightReference !== HeightReference.NONE) {
  1945. throw new DeveloperError(
  1946. "Height reference is not supported without a scene and globe."
  1947. );
  1948. }
  1949. //>>includeEnd('debug');
  1950. model._clampedModelMatrix = undefined;
  1951. return;
  1952. }
  1953. const globe = scene.globe;
  1954. const ellipsoid = globe.ellipsoid;
  1955. // Compute cartographic position so we don't recompute every update
  1956. const modelMatrix = model.modelMatrix;
  1957. scratchPosition.x = modelMatrix[12];
  1958. scratchPosition.y = modelMatrix[13];
  1959. scratchPosition.z = modelMatrix[14];
  1960. const cartoPosition = ellipsoid.cartesianToCartographic(scratchPosition);
  1961. if (!defined(model._clampedModelMatrix)) {
  1962. model._clampedModelMatrix = Matrix4.clone(modelMatrix, new Matrix4());
  1963. }
  1964. // Install callback to handle updating of terrain tiles
  1965. const surface = globe._surface;
  1966. model._removeUpdateHeightCallback = surface.updateHeight(
  1967. cartoPosition,
  1968. getUpdateHeightCallback(model, ellipsoid, cartoPosition)
  1969. );
  1970. // Set the correct height now
  1971. const height = globe.getHeight(cartoPosition);
  1972. if (defined(height)) {
  1973. // Get callback with cartoPosition being the non-clamped position
  1974. const callback = getUpdateHeightCallback(model, ellipsoid, cartoPosition);
  1975. // Compute the clamped cartesian and call updateHeight callback
  1976. Cartographic.clone(cartoPosition, scratchCartographic);
  1977. scratchCartographic.height = height;
  1978. ellipsoid.cartographicToCartesian(scratchCartographic, scratchPosition);
  1979. callback(scratchPosition);
  1980. }
  1981. model._heightDirty = false;
  1982. model._updateModelMatrix = true;
  1983. }
  1984. function updateBoundingSphereAndScale(model, frameState) {
  1985. if (!model._updateModelMatrix && model._minimumPixelSize === 0.0) {
  1986. return;
  1987. }
  1988. const modelMatrix = defined(model._clampedModelMatrix)
  1989. ? model._clampedModelMatrix
  1990. : model.modelMatrix;
  1991. updateBoundingSphere(model, modelMatrix);
  1992. updateComputedScale(model, modelMatrix, frameState);
  1993. }
  1994. function updateBoundingSphere(model, modelMatrix) {
  1995. model._clampedScale = defined(model._maximumScale)
  1996. ? Math.min(model._scale, model._maximumScale)
  1997. : model._scale;
  1998. model._boundingSphere.center = Cartesian3.multiplyByScalar(
  1999. model._sceneGraph.boundingSphere.center,
  2000. model._clampedScale,
  2001. model._boundingSphere.center
  2002. );
  2003. model._boundingSphere.radius = model._initialRadius * model._clampedScale;
  2004. model._boundingSphere = BoundingSphere.transform(
  2005. model._boundingSphere,
  2006. modelMatrix,
  2007. model._boundingSphere
  2008. );
  2009. }
  2010. function updateComputedScale(model, modelMatrix, frameState) {
  2011. let scale = model.scale;
  2012. if (model.minimumPixelSize !== 0.0 && !model._projectTo2D) {
  2013. // Compute size of bounding sphere in pixels
  2014. const context = frameState.context;
  2015. const maxPixelSize = Math.max(
  2016. context.drawingBufferWidth,
  2017. context.drawingBufferHeight
  2018. );
  2019. Matrix4.getTranslation(modelMatrix, scratchPosition);
  2020. if (model._sceneMode !== SceneMode.SCENE3D) {
  2021. SceneTransforms.computeActualWgs84Position(
  2022. frameState,
  2023. scratchPosition,
  2024. scratchPosition
  2025. );
  2026. }
  2027. const radius = model._boundingSphere.radius;
  2028. const metersPerPixel = scaleInPixels(scratchPosition, radius, frameState);
  2029. // metersPerPixel is always > 0.0
  2030. const pixelsPerMeter = 1.0 / metersPerPixel;
  2031. const diameterInPixels = Math.min(
  2032. pixelsPerMeter * (2.0 * radius),
  2033. maxPixelSize
  2034. );
  2035. // Maintain model's minimum pixel size
  2036. if (diameterInPixels < model.minimumPixelSize) {
  2037. scale =
  2038. (model.minimumPixelSize * metersPerPixel) /
  2039. (2.0 * model._initialRadius);
  2040. }
  2041. }
  2042. model._computedScale = defined(model.maximumScale)
  2043. ? Math.min(model.maximumScale, scale)
  2044. : scale;
  2045. }
  2046. function updatePickIds(model) {
  2047. if (!model._idDirty) {
  2048. return;
  2049. }
  2050. model._idDirty = false;
  2051. const id = model._id;
  2052. const pickIds = model._pickIds;
  2053. const length = pickIds.length;
  2054. for (let i = 0; i < length; ++i) {
  2055. pickIds[i].object.id = id;
  2056. }
  2057. }
  2058. function updateReferenceMatrices(model, frameState) {
  2059. const modelMatrix = defined(model._clampedModelMatrix)
  2060. ? model._clampedModelMatrix
  2061. : model.modelMatrix;
  2062. const referenceMatrix = defaultValue(model.referenceMatrix, modelMatrix);
  2063. const context = frameState.context;
  2064. const ibl = model._imageBasedLighting;
  2065. if (ibl.useSphericalHarmonicCoefficients || ibl.useSpecularEnvironmentMaps) {
  2066. let iblReferenceFrameMatrix3 = scratchIBLReferenceFrameMatrix3;
  2067. let iblReferenceFrameMatrix4 = scratchIBLReferenceFrameMatrix4;
  2068. iblReferenceFrameMatrix4 = Matrix4.multiply(
  2069. context.uniformState.view3D,
  2070. referenceMatrix,
  2071. iblReferenceFrameMatrix4
  2072. );
  2073. iblReferenceFrameMatrix3 = Matrix4.getMatrix3(
  2074. iblReferenceFrameMatrix4,
  2075. iblReferenceFrameMatrix3
  2076. );
  2077. iblReferenceFrameMatrix3 = Matrix3.getRotation(
  2078. iblReferenceFrameMatrix3,
  2079. iblReferenceFrameMatrix3
  2080. );
  2081. model._iblReferenceFrameMatrix = Matrix3.transpose(
  2082. iblReferenceFrameMatrix3,
  2083. model._iblReferenceFrameMatrix
  2084. );
  2085. }
  2086. if (model.isClippingEnabled()) {
  2087. let clippingPlanesMatrix = scratchClippingPlanesMatrix;
  2088. clippingPlanesMatrix = Matrix4.multiply(
  2089. context.uniformState.view3D,
  2090. referenceMatrix,
  2091. clippingPlanesMatrix
  2092. );
  2093. clippingPlanesMatrix = Matrix4.multiply(
  2094. clippingPlanesMatrix,
  2095. model._clippingPlanes.modelMatrix,
  2096. clippingPlanesMatrix
  2097. );
  2098. model._clippingPlanesMatrix = Matrix4.inverseTranspose(
  2099. clippingPlanesMatrix,
  2100. model._clippingPlanesMatrix
  2101. );
  2102. }
  2103. }
  2104. function updateSceneGraph(model, frameState) {
  2105. const sceneGraph = model._sceneGraph;
  2106. if (model._updateModelMatrix || model._minimumPixelSize !== 0.0) {
  2107. const modelMatrix = defined(model._clampedModelMatrix)
  2108. ? model._clampedModelMatrix
  2109. : model.modelMatrix;
  2110. sceneGraph.updateModelMatrix(modelMatrix, frameState);
  2111. model._updateModelMatrix = false;
  2112. }
  2113. if (model._backFaceCullingDirty) {
  2114. sceneGraph.updateBackFaceCulling(model._backFaceCulling);
  2115. model._backFaceCullingDirty = false;
  2116. }
  2117. if (model._shadowsDirty) {
  2118. sceneGraph.updateShadows(model._shadows);
  2119. model._shadowsDirty = false;
  2120. }
  2121. if (model._debugShowBoundingVolumeDirty) {
  2122. sceneGraph.updateShowBoundingVolume(model._debugShowBoundingVolume);
  2123. model._debugShowBoundingVolumeDirty = false;
  2124. }
  2125. let updateForAnimations = false;
  2126. // Animations are disabled for classification models.
  2127. if (!defined(model.classificationType)) {
  2128. updateForAnimations =
  2129. model._userAnimationDirty || model._activeAnimations.update(frameState);
  2130. }
  2131. sceneGraph.update(frameState, updateForAnimations);
  2132. model._userAnimationDirty = false;
  2133. }
  2134. function updateShowCreditsOnScreen(model) {
  2135. if (!model._showCreditsOnScreenDirty) {
  2136. return;
  2137. }
  2138. model._showCreditsOnScreenDirty = false;
  2139. const showOnScreen = model._showCreditsOnScreen;
  2140. if (defined(model._credit)) {
  2141. model._credit.showOnScreen = showOnScreen;
  2142. }
  2143. const resourceCredits = model._resourceCredits;
  2144. const resourceCreditsLength = resourceCredits.length;
  2145. for (let i = 0; i < resourceCreditsLength; i++) {
  2146. resourceCredits[i].showOnScreen = showOnScreen;
  2147. }
  2148. const gltfCredits = model._gltfCredits;
  2149. const gltfCreditsLength = gltfCredits.length;
  2150. for (let i = 0; i < gltfCreditsLength; i++) {
  2151. gltfCredits[i].showOnScreen = showOnScreen;
  2152. }
  2153. }
  2154. function submitDrawCommands(model, frameState) {
  2155. // Check that show is true after draw commands are built;
  2156. // we want the user to be able to instantly see the model
  2157. // when show is set to true.
  2158. const displayConditionPassed = passesDistanceDisplayCondition(
  2159. model,
  2160. frameState
  2161. );
  2162. const invisible = model.isInvisible();
  2163. const silhouette = model.hasSilhouette(frameState);
  2164. // If the model is invisible but has a silhouette, it still
  2165. // needs to draw in order to write to the stencil buffer and
  2166. // render the silhouette.
  2167. const showModel =
  2168. model._show &&
  2169. model._computedScale !== 0 &&
  2170. displayConditionPassed &&
  2171. (!invisible || silhouette);
  2172. const passes = frameState.passes;
  2173. const submitCommandsForPass =
  2174. passes.render || (passes.pick && model.allowPicking);
  2175. if (showModel && !model._ignoreCommands && submitCommandsForPass) {
  2176. addCreditsToCreditDisplay(model, frameState);
  2177. model._sceneGraph.pushDrawCommands(frameState);
  2178. }
  2179. }
  2180. const scratchBoundingSphere = new BoundingSphere();
  2181. function scaleInPixels(positionWC, radius, frameState) {
  2182. scratchBoundingSphere.center = positionWC;
  2183. scratchBoundingSphere.radius = radius;
  2184. return frameState.camera.getPixelSize(
  2185. scratchBoundingSphere,
  2186. frameState.context.drawingBufferWidth,
  2187. frameState.context.drawingBufferHeight
  2188. );
  2189. }
  2190. function getUpdateHeightCallback(model, ellipsoid, cartoPosition) {
  2191. return function (clampedPosition) {
  2192. if (model.heightReference === HeightReference.RELATIVE_TO_GROUND) {
  2193. const clampedCart = ellipsoid.cartesianToCartographic(
  2194. clampedPosition,
  2195. scratchCartographic
  2196. );
  2197. clampedCart.height += cartoPosition.height;
  2198. ellipsoid.cartographicToCartesian(clampedCart, clampedPosition);
  2199. }
  2200. const clampedModelMatrix = model._clampedModelMatrix;
  2201. // Modify clamped model matrix to use new height
  2202. Matrix4.clone(model.modelMatrix, clampedModelMatrix);
  2203. clampedModelMatrix[12] = clampedPosition.x;
  2204. clampedModelMatrix[13] = clampedPosition.y;
  2205. clampedModelMatrix[14] = clampedPosition.z;
  2206. model._heightDirty = true;
  2207. };
  2208. }
  2209. const scratchDisplayConditionCartesian = new Cartesian3();
  2210. function passesDistanceDisplayCondition(model, frameState) {
  2211. const condition = model.distanceDisplayCondition;
  2212. if (!defined(condition)) {
  2213. return true;
  2214. }
  2215. const nearSquared = condition.near * condition.near;
  2216. const farSquared = condition.far * condition.far;
  2217. let distanceSquared;
  2218. if (frameState.mode === SceneMode.SCENE2D) {
  2219. const frustum2DWidth =
  2220. frameState.camera.frustum.right - frameState.camera.frustum.left;
  2221. const distance = frustum2DWidth * 0.5;
  2222. distanceSquared = distance * distance;
  2223. } else {
  2224. // Distance to center of primitive's reference frame
  2225. const position = Matrix4.getTranslation(
  2226. model.modelMatrix,
  2227. scratchDisplayConditionCartesian
  2228. );
  2229. // This will project the position if the scene is in Columbus View,
  2230. // but leave the position as-is in 3D mode.
  2231. SceneTransforms.computeActualWgs84Position(frameState, position, position);
  2232. distanceSquared = Cartesian3.distanceSquared(
  2233. position,
  2234. frameState.camera.positionWC
  2235. );
  2236. }
  2237. return distanceSquared >= nearSquared && distanceSquared <= farSquared;
  2238. }
  2239. function addCreditsToCreditDisplay(model, frameState) {
  2240. const creditDisplay = frameState.creditDisplay;
  2241. // Add all credits to the credit display.
  2242. const credit = model._credit;
  2243. if (defined(credit)) {
  2244. creditDisplay.addCreditToNextFrame(credit);
  2245. }
  2246. const resourceCredits = model._resourceCredits;
  2247. const resourceCreditsLength = resourceCredits.length;
  2248. for (let c = 0; c < resourceCreditsLength; c++) {
  2249. creditDisplay.addCreditToNextFrame(resourceCredits[c]);
  2250. }
  2251. const gltfCredits = model._gltfCredits;
  2252. const gltfCreditsLength = gltfCredits.length;
  2253. for (let c = 0; c < gltfCreditsLength; c++) {
  2254. creditDisplay.addCreditToNextFrame(gltfCredits[c]);
  2255. }
  2256. }
  2257. /**
  2258. * Gets whether or not the model is translucent based on its assigned model color.
  2259. * If the model color's alpha is equal to zero, then it is considered invisible,
  2260. * not translucent.
  2261. *
  2262. * @returns {boolean} <code>true</code> if the model is translucent, otherwise <code>false</code>.
  2263. * @private
  2264. */
  2265. Model.prototype.isTranslucent = function () {
  2266. const color = this.color;
  2267. return defined(color) && color.alpha > 0.0 && color.alpha < 1.0;
  2268. };
  2269. /**
  2270. * Gets whether or not the model is invisible, i.e. if the model color's alpha
  2271. * is equal to zero.
  2272. *
  2273. * @returns {boolean} <code>true</code> if the model is invisible, otherwise <code>false</code>.
  2274. * @private
  2275. */
  2276. Model.prototype.isInvisible = function () {
  2277. const color = this.color;
  2278. return defined(color) && color.alpha === 0.0;
  2279. };
  2280. function supportsSilhouettes(frameState) {
  2281. return frameState.context.stencilBuffer;
  2282. }
  2283. /**
  2284. * Gets whether or not the model has a silhouette. This accounts for whether
  2285. * silhouettes are supported (i.e. the context supports stencil buffers).
  2286. * <p>
  2287. * If the model classifies another model, its silhouette will be disabled.
  2288. * </p>
  2289. *
  2290. * @param {FrameState} The frame state.
  2291. * @returns {boolean} <code>true</code> if the model has silhouettes, otherwise <code>false</code>.
  2292. * @private
  2293. */
  2294. Model.prototype.hasSilhouette = function (frameState) {
  2295. return (
  2296. supportsSilhouettes(frameState) &&
  2297. this._silhouetteSize > 0.0 &&
  2298. this._silhouetteColor.alpha > 0.0 &&
  2299. !defined(this._classificationType)
  2300. );
  2301. };
  2302. /**
  2303. * Gets whether or not the model is part of a tileset that uses the
  2304. * skipLevelOfDetail optimization. This accounts for whether skipLevelOfDetail
  2305. * is supported (i.e. the context supports stencil buffers).
  2306. *
  2307. * @param {FrameState} frameState The frame state.
  2308. * @returns {boolean} <code>true</code> if the model is part of a tileset that uses the skipLevelOfDetail optimization, <code>false</code> otherwise.
  2309. * @private
  2310. */
  2311. Model.prototype.hasSkipLevelOfDetail = function (frameState) {
  2312. if (!ModelType.is3DTiles(this.type)) {
  2313. return false;
  2314. }
  2315. const supportsSkipLevelOfDetail = frameState.context.stencilBuffer;
  2316. const tileset = this._content.tileset;
  2317. return supportsSkipLevelOfDetail && tileset.isSkippingLevelOfDetail;
  2318. };
  2319. /**
  2320. * Gets whether or not clipping planes are enabled for this model.
  2321. *
  2322. * @returns {boolean} <code>true</code> if clipping planes are enabled for this model, <code>false</code>.
  2323. * @private
  2324. */
  2325. Model.prototype.isClippingEnabled = function () {
  2326. const clippingPlanes = this._clippingPlanes;
  2327. return (
  2328. defined(clippingPlanes) &&
  2329. clippingPlanes.enabled &&
  2330. clippingPlanes.length !== 0
  2331. );
  2332. };
  2333. /**
  2334. * Returns true if this object was destroyed; otherwise, false.
  2335. * <br /><br />
  2336. * If this object was destroyed, it should not be used; calling any function other than
  2337. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  2338. *
  2339. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  2340. *
  2341. * @see Model#destroy
  2342. */
  2343. Model.prototype.isDestroyed = function () {
  2344. return false;
  2345. };
  2346. /**
  2347. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  2348. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  2349. * <br /><br />
  2350. * Once an object is destroyed, it should not be used; calling any function other than
  2351. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  2352. * assign the return value (<code>undefined</code>) to the object as done in the example.
  2353. *
  2354. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  2355. *
  2356. *
  2357. * @example
  2358. * model = model && model.destroy();
  2359. *
  2360. * @see Model#isDestroyed
  2361. */
  2362. Model.prototype.destroy = function () {
  2363. const loader = this._loader;
  2364. if (defined(loader)) {
  2365. loader.destroy();
  2366. }
  2367. const featureTables = this._featureTables;
  2368. if (defined(featureTables)) {
  2369. const length = featureTables.length;
  2370. for (let i = 0; i < length; i++) {
  2371. featureTables[i].destroy();
  2372. }
  2373. }
  2374. this.destroyPipelineResources();
  2375. this.destroyModelResources();
  2376. // Remove callbacks for height reference behavior.
  2377. if (defined(this._removeUpdateHeightCallback)) {
  2378. this._removeUpdateHeightCallback();
  2379. this._removeUpdateHeightCallback = undefined;
  2380. }
  2381. if (defined(this._terrainProviderChangedCallback)) {
  2382. this._terrainProviderChangedCallback();
  2383. this._terrainProviderChangedCallback = undefined;
  2384. }
  2385. // Only destroy the ClippingPlaneCollection if this is the owner.
  2386. const clippingPlaneCollection = this._clippingPlanes;
  2387. if (
  2388. defined(clippingPlaneCollection) &&
  2389. !clippingPlaneCollection.isDestroyed() &&
  2390. clippingPlaneCollection.owner === this
  2391. ) {
  2392. clippingPlaneCollection.destroy();
  2393. }
  2394. this._clippingPlanes = undefined;
  2395. // Only destroy the ImageBasedLighting if this is the owner.
  2396. if (
  2397. this._shouldDestroyImageBasedLighting &&
  2398. !this._imageBasedLighting.isDestroyed()
  2399. ) {
  2400. this._imageBasedLighting.destroy();
  2401. }
  2402. this._imageBasedLighting = undefined;
  2403. destroyObject(this);
  2404. };
  2405. /**
  2406. * Destroys resources generated in the pipeline stages
  2407. * that must be destroyed when draw commands are rebuilt.
  2408. * @private
  2409. */
  2410. Model.prototype.destroyPipelineResources = function () {
  2411. const resources = this._pipelineResources;
  2412. for (let i = 0; i < resources.length; i++) {
  2413. resources[i].destroy();
  2414. }
  2415. this._pipelineResources.length = 0;
  2416. this._pickIds.length = 0;
  2417. };
  2418. /**
  2419. * Destroys resources generated in the pipeline stages
  2420. * that exist for the lifetime of the model.
  2421. * @private
  2422. */
  2423. Model.prototype.destroyModelResources = function () {
  2424. const resources = this._modelResources;
  2425. for (let i = 0; i < resources.length; i++) {
  2426. resources[i].destroy();
  2427. }
  2428. this._modelResources.length = 0;
  2429. };
  2430. /**
  2431. * <p>
  2432. * Creates a model from a glTF asset. When the model is ready to render, i.e., when the external binary, image,
  2433. * and shader files are downloaded and the WebGL resources are created, the {@link Model#readyPromise} is resolved.
  2434. * </p>
  2435. * <p>
  2436. * The model can be a traditional glTF asset with a .gltf extension or a Binary glTF using the .glb extension.
  2437. *
  2438. * @param {object} options Object with the following properties:
  2439. * @param {string|Resource} options.url The url to the .gltf or .glb file.
  2440. * @param {string|Resource} [options.basePath=''] The base path that paths in the glTF JSON are relative to.
  2441. * @param {boolean} [options.show=true] Whether or not to render the model.
  2442. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix that transforms the model from model to world coordinates.
  2443. * @param {number} [options.scale=1.0] A uniform scale applied to this model.
  2444. * @param {number} [options.minimumPixelSize=0.0] The approximate minimum pixel size of the model regardless of zoom.
  2445. * @param {number} [options.maximumScale] The maximum scale size of a model. An upper limit for minimumPixelSize.
  2446. * @param {object} [options.id] A user-defined object to return when the model is picked with {@link Scene#pick}.
  2447. * @param {boolean} [options.allowPicking=true] When <code>true</code>, each primitive is pickable with {@link Scene#pick}.
  2448. * @param {boolean} [options.incrementallyLoadTextures=true] Determine if textures may continue to stream in after the model is loaded.
  2449. * @param {boolean} [options.asynchronous=true] Determines if model WebGL resource creation will be spread out over several frames or block until completion once all glTF files are loaded.
  2450. * @param {boolean} [options.clampAnimations=true] Determines if the model's animations should hold a pose over frames where no keyframes are specified.
  2451. * @param {ShadowMode} [options.shadows=ShadowMode.ENABLED] Determines whether the model casts or receives shadows from light sources.
  2452. * @param {boolean} [options.releaseGltfJson=false] When true, the glTF JSON is released once the glTF is loaded. This is is especially useful for cases like 3D Tiles, where each .gltf model is unique and caching the glTF JSON is not effective.
  2453. * @param {boolean} [options.debugShowBoundingVolume=false] For debugging only. Draws the bounding sphere for each draw command in the model.
  2454. * @param {boolean} [options.enableDebugWireframe=false] For debugging only. This must be set to true for debugWireframe to work in WebGL1. This cannot be set after the model has loaded.
  2455. * @param {boolean} [options.debugWireframe=false] For debugging only. Draws the model in wireframe. Will only work for WebGL1 if enableDebugWireframe is set to true.
  2456. * @param {boolean} [options.cull=true] Whether or not to cull the model using frustum/horizon culling. If the model is part of a 3D Tiles tileset, this property will always be false, since the 3D Tiles culling system is used.
  2457. * @param {boolean} [options.opaquePass=Pass.OPAQUE] The pass to use in the {@link DrawCommand} for the opaque portions of the model.
  2458. * @param {Axis} [options.upAxis=Axis.Y] The up-axis of the glTF model.
  2459. * @param {Axis} [options.forwardAxis=Axis.Z] The forward-axis of the glTF model.
  2460. * @param {CustomShader} [options.customShader] A custom shader. This will add user-defined GLSL code to the vertex and fragment shaders. Using custom shaders with a {@link Cesium3DTileStyle} may lead to undefined behavior.
  2461. * @param {Cesium3DTileContent} [options.content] The tile content this model belongs to. This property will be undefined if model is not loaded as part of a tileset.
  2462. * @param {HeightReference} [options.heightReference=HeightReference.NONE] Determines how the model is drawn relative to terrain.
  2463. * @param {Scene} [options.scene] Must be passed in for models that use the height reference property.
  2464. * @param {DistanceDisplayCondition} [options.distanceDisplayCondition] The condition specifying at what distance from the camera that this model will be displayed.
  2465. * @param {Color} [options.color] A color that blends with the model's rendered color.
  2466. * @param {ColorBlendMode} [options.colorBlendMode=ColorBlendMode.HIGHLIGHT] Defines how the color blends with the model.
  2467. * @param {number} [options.colorBlendAmount=0.5] Value used to determine the color strength when the <code>colorBlendMode</code> is <code>MIX</code>. A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with any value in-between resulting in a mix of the two.
  2468. * @param {Color} [options.silhouetteColor=Color.RED] The silhouette color. If more than 256 models have silhouettes enabled, there is a small chance that overlapping models will have minor artifacts.
  2469. * @param {number} [options.silhouetteSize=0.0] The size of the silhouette in pixels.
  2470. * @param {boolean} [options.enableShowOutline=true] Whether to enable outlines for models using the {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/CESIUM_primitive_outline|CESIUM_primitive_outline} extension. This can be set false to avoid post-processing geometry at load time. When false, the showOutlines and outlineColor options are ignored.
  2471. * @param {boolean} [options.showOutline=true] Whether to display the outline for models using the {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/CESIUM_primitive_outline|CESIUM_primitive_outline} extension. When true, outlines are displayed. When false, outlines are not displayed.
  2472. * @param {Color} [options.outlineColor=Color.BLACK] The color to use when rendering outlines.
  2473. * @param {ClippingPlaneCollection} [options.clippingPlanes] The {@link ClippingPlaneCollection} used to selectively disable rendering the model.
  2474. * @param {Cartesian3} [options.lightColor] The light color when shading the model. When <code>undefined</code> the scene's light color is used instead.
  2475. * @param {ImageBasedLighting} [options.imageBasedLighting] The properties for managing image-based lighting on this model.
  2476. * @param {boolean} [options.backFaceCulling=true] Whether to cull back-facing geometry. When true, back face culling is determined by the material's doubleSided property; when false, back face culling is disabled. Back faces are not culled if the model's color is translucent.
  2477. * @param {Credit|string} [options.credit] A credit for the data source, which is displayed on the canvas.
  2478. * @param {boolean} [options.showCreditsOnScreen=false] Whether to display the credits of this model on screen.
  2479. * @param {SplitDirection} [options.splitDirection=SplitDirection.NONE] The {@link SplitDirection} split to apply to this model.
  2480. * @param {boolean} [options.projectTo2D=false] Whether to accurately project the model's positions in 2D. If this is true, the model will be projected accurately to 2D, but it will use more memory to do so. If this is false, the model will use less memory and will still render in 2D / CV mode, but its positions may be inaccurate. This disables minimumPixelSize and prevents future modification to the model matrix. This also cannot be set after the model has loaded.
  2481. * @param {string|number} [options.featureIdLabel="featureId_0"] Label of the feature ID set to use for picking and styling. For EXT_mesh_features, this is the feature ID's label property, or "featureId_N" (where N is the index in the featureIds array) when not specified. EXT_feature_metadata did not have a label field, so such feature ID sets are always labeled "featureId_N" where N is the index in the list of all feature Ids, where feature ID attributes are listed before feature ID textures. If featureIdLabel is an integer N, it is converted to the string "featureId_N" automatically. If both per-primitive and per-instance feature IDs are present, the instance feature IDs take priority.
  2482. * @param {string|number} [options.instanceFeatureIdLabel="instanceFeatureId_0"] Label of the instance feature ID set used for picking and styling. If instanceFeatureIdLabel is set to an integer N, it is converted to the string "instanceFeatureId_N" automatically. If both per-primitive and per-instance feature IDs are present, the instance feature IDs take priority.
  2483. * @param {object} [options.pointCloudShading] Options for constructing a {@link PointCloudShading} object to control point attenuation and lighting.
  2484. * @param {ClassificationType} [options.classificationType] Determines whether terrain, 3D Tiles or both will be classified by this model. This cannot be set after the model has loaded.
  2485. *
  2486. * @returns {Model} The newly created model.
  2487. */
  2488. Model.fromGltf = function (options) {
  2489. deprecationWarning(
  2490. "Model.fromGltf",
  2491. "Model.fromGltf was deprecated in CesiumJS 1.104. It will be removed in 1.107. Use Model.fromGltfAsync instead."
  2492. );
  2493. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  2494. //>>includeStart('debug', pragmas.debug);
  2495. if (!defined(options.url) && !defined(options.gltf)) {
  2496. throw new DeveloperError("options.url is required.");
  2497. }
  2498. //>>includeEnd('debug');
  2499. // options.gltf is used internally for 3D Tiles. It can be a Resource, a URL
  2500. // to a glTF/glb file, a binary glTF buffer, or a JSON object containing the
  2501. // glTF contents.
  2502. const gltf = defaultValue(options.url, options.gltf);
  2503. const loaderOptions = {
  2504. releaseGltfJson: options.releaseGltfJson,
  2505. asynchronous: options.asynchronous,
  2506. incrementallyLoadTextures: options.incrementallyLoadTextures,
  2507. upAxis: options.upAxis,
  2508. forwardAxis: options.forwardAxis,
  2509. loadAttributesFor2D: options.projectTo2D,
  2510. loadIndicesForWireframe: options.enableDebugWireframe,
  2511. loadPrimitiveOutline: options.enableShowOutline,
  2512. loadForClassification: defined(options.classificationType),
  2513. };
  2514. const basePath = defaultValue(options.basePath, "");
  2515. const baseResource = Resource.createIfNeeded(basePath);
  2516. if (defined(gltf.asset)) {
  2517. loaderOptions.gltfJson = gltf;
  2518. loaderOptions.baseResource = baseResource;
  2519. loaderOptions.gltfResource = baseResource;
  2520. } else if (gltf instanceof Uint8Array) {
  2521. loaderOptions.typedArray = gltf;
  2522. loaderOptions.baseResource = baseResource;
  2523. loaderOptions.gltfResource = baseResource;
  2524. } else {
  2525. loaderOptions.gltfResource = Resource.createIfNeeded(gltf);
  2526. }
  2527. const loader = new GltfLoader(loaderOptions);
  2528. const is3DTiles = defined(options.content);
  2529. const type = is3DTiles ? ModelType.TILE_GLTF : ModelType.GLTF;
  2530. const modelOptions = makeModelOptions(loader, type, options);
  2531. modelOptions.resource = loaderOptions.gltfResource;
  2532. const model = new Model(modelOptions);
  2533. return model;
  2534. };
  2535. /**
  2536. * <p>
  2537. * Asynchronously creates a model from a glTF asset. This function returns a promise that resolves when the model is ready to render, i.e., when the external binary, image,
  2538. * and shader files are downloaded and the WebGL resources are created.
  2539. * </p>
  2540. * <p>
  2541. * The model can be a traditional glTF asset with a .gltf extension or a Binary glTF using the .glb extension.
  2542. *
  2543. * @param {object} options Object with the following properties:
  2544. * @param {string|Resource} options.url The url to the .gltf or .glb file.
  2545. * @param {string|Resource} [options.basePath=''] The base path that paths in the glTF JSON are relative to.
  2546. * @param {boolean} [options.show=true] Whether or not to render the model.
  2547. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix that transforms the model from model to world coordinates.
  2548. * @param {number} [options.scale=1.0] A uniform scale applied to this model.
  2549. * @param {number} [options.minimumPixelSize=0.0] The approximate minimum pixel size of the model regardless of zoom.
  2550. * @param {number} [options.maximumScale] The maximum scale size of a model. An upper limit for minimumPixelSize.
  2551. * @param {object} [options.id] A user-defined object to return when the model is picked with {@link Scene#pick}.
  2552. * @param {boolean} [options.allowPicking=true] When <code>true</code>, each primitive is pickable with {@link Scene#pick}.
  2553. * @param {boolean} [options.incrementallyLoadTextures=true] Determine if textures may continue to stream in after the model is loaded.
  2554. * @param {boolean} [options.asynchronous=true] Determines if model WebGL resource creation will be spread out over several frames or block until completion once all glTF files are loaded.
  2555. * @param {boolean} [options.clampAnimations=true] Determines if the model's animations should hold a pose over frames where no keyframes are specified.
  2556. * @param {ShadowMode} [options.shadows=ShadowMode.ENABLED] Determines whether the model casts or receives shadows from light sources.
  2557. * @param {boolean} [options.releaseGltfJson=false] When true, the glTF JSON is released once the glTF is loaded. This is is especially useful for cases like 3D Tiles, where each .gltf model is unique and caching the glTF JSON is not effective.
  2558. * @param {boolean} [options.debugShowBoundingVolume=false] For debugging only. Draws the bounding sphere for each draw command in the model.
  2559. * @param {boolean} [options.enableDebugWireframe=false] For debugging only. This must be set to true for debugWireframe to work in WebGL1. This cannot be set after the model has loaded.
  2560. * @param {boolean} [options.debugWireframe=false] For debugging only. Draws the model in wireframe. Will only work for WebGL1 if enableDebugWireframe is set to true.
  2561. * @param {boolean} [options.cull=true] Whether or not to cull the model using frustum/horizon culling. If the model is part of a 3D Tiles tileset, this property will always be false, since the 3D Tiles culling system is used.
  2562. * @param {boolean} [options.opaquePass=Pass.OPAQUE] The pass to use in the {@link DrawCommand} for the opaque portions of the model.
  2563. * @param {Axis} [options.upAxis=Axis.Y] The up-axis of the glTF model.
  2564. * @param {Axis} [options.forwardAxis=Axis.Z] The forward-axis of the glTF model.
  2565. * @param {CustomShader} [options.customShader] A custom shader. This will add user-defined GLSL code to the vertex and fragment shaders. Using custom shaders with a {@link Cesium3DTileStyle} may lead to undefined behavior.
  2566. * @param {Cesium3DTileContent} [options.content] The tile content this model belongs to. This property will be undefined if model is not loaded as part of a tileset.
  2567. * @param {HeightReference} [options.heightReference=HeightReference.NONE] Determines how the model is drawn relative to terrain.
  2568. * @param {Scene} [options.scene] Must be passed in for models that use the height reference property.
  2569. * @param {DistanceDisplayCondition} [options.distanceDisplayCondition] The condition specifying at what distance from the camera that this model will be displayed.
  2570. * @param {Color} [options.color] A color that blends with the model's rendered color.
  2571. * @param {ColorBlendMode} [options.colorBlendMode=ColorBlendMode.HIGHLIGHT] Defines how the color blends with the model.
  2572. * @param {number} [options.colorBlendAmount=0.5] Value used to determine the color strength when the <code>colorBlendMode</code> is <code>MIX</code>. A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with any value in-between resulting in a mix of the two.
  2573. * @param {Color} [options.silhouetteColor=Color.RED] The silhouette color. If more than 256 models have silhouettes enabled, there is a small chance that overlapping models will have minor artifacts.
  2574. * @param {number} [options.silhouetteSize=0.0] The size of the silhouette in pixels.
  2575. * @param {boolean} [options.enableShowOutline=true] Whether to enable outlines for models using the {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/CESIUM_primitive_outline|CESIUM_primitive_outline} extension. This can be set false to avoid post-processing geometry at load time. When false, the showOutlines and outlineColor options are ignored.
  2576. * @param {boolean} [options.showOutline=true] Whether to display the outline for models using the {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/CESIUM_primitive_outline|CESIUM_primitive_outline} extension. When true, outlines are displayed. When false, outlines are not displayed.
  2577. * @param {Color} [options.outlineColor=Color.BLACK] The color to use when rendering outlines.
  2578. * @param {ClippingPlaneCollection} [options.clippingPlanes] The {@link ClippingPlaneCollection} used to selectively disable rendering the model.
  2579. * @param {Cartesian3} [options.lightColor] The light color when shading the model. When <code>undefined</code> the scene's light color is used instead.
  2580. * @param {ImageBasedLighting} [options.imageBasedLighting] The properties for managing image-based lighting on this model.
  2581. * @param {boolean} [options.backFaceCulling=true] Whether to cull back-facing geometry. When true, back face culling is determined by the material's doubleSided property; when false, back face culling is disabled. Back faces are not culled if the model's color is translucent.
  2582. * @param {Credit|string} [options.credit] A credit for the data source, which is displayed on the canvas.
  2583. * @param {boolean} [options.showCreditsOnScreen=false] Whether to display the credits of this model on screen.
  2584. * @param {SplitDirection} [options.splitDirection=SplitDirection.NONE] The {@link SplitDirection} split to apply to this model.
  2585. * @param {boolean} [options.projectTo2D=false] Whether to accurately project the model's positions in 2D. If this is true, the model will be projected accurately to 2D, but it will use more memory to do so. If this is false, the model will use less memory and will still render in 2D / CV mode, but its positions may be inaccurate. This disables minimumPixelSize and prevents future modification to the model matrix. This also cannot be set after the model has loaded.
  2586. * @param {string|number} [options.featureIdLabel="featureId_0"] Label of the feature ID set to use for picking and styling. For EXT_mesh_features, this is the feature ID's label property, or "featureId_N" (where N is the index in the featureIds array) when not specified. EXT_feature_metadata did not have a label field, so such feature ID sets are always labeled "featureId_N" where N is the index in the list of all feature Ids, where feature ID attributes are listed before feature ID textures. If featureIdLabel is an integer N, it is converted to the string "featureId_N" automatically. If both per-primitive and per-instance feature IDs are present, the instance feature IDs take priority.
  2587. * @param {string|number} [options.instanceFeatureIdLabel="instanceFeatureId_0"] Label of the instance feature ID set used for picking and styling. If instanceFeatureIdLabel is set to an integer N, it is converted to the string "instanceFeatureId_N" automatically. If both per-primitive and per-instance feature IDs are present, the instance feature IDs take priority.
  2588. * @param {object} [options.pointCloudShading] Options for constructing a {@link PointCloudShading} object to control point attenuation and lighting.
  2589. * @param {ClassificationType} [options.classificationType] Determines whether terrain, 3D Tiles or both will be classified by this model. This cannot be set after the model has loaded.
  2590. * @param {Model.GltfCallback} [options.gltfCallback] A function that is called with the loaded gltf object once loaded.
  2591. *
  2592. * @returns {Promise<Model>} A promise that resolves to the created model when it is ready to render.
  2593. *
  2594. * @exception {RuntimeError} The model failed to load.
  2595. * @exception {RuntimeError} Unsupported glTF version.
  2596. * @exception {RuntimeError} Unsupported glTF Extension
  2597. *
  2598. * @example
  2599. * // Load a model and add it to the scene
  2600. * try {
  2601. * const model = await Cesium.Model.fromGltfAsync({
  2602. * url: "../../SampleData/models/CesiumMan/Cesium_Man.glb"
  2603. * });
  2604. * viewer.scene.primitives.add(model);
  2605. * } catch (error) {
  2606. * console.log(`Failed to load model. ${error}`);
  2607. * }
  2608. *
  2609. * @example
  2610. * // Position a model with modelMatrix and display it with a minimum size of 128 pixels
  2611. * const position = Cesium.Cartesian3.fromDegrees(
  2612. * -123.0744619,
  2613. * 44.0503706,
  2614. * 5000.0
  2615. * );
  2616. * const headingPositionRoll = new Cesium.HeadingPitchRoll();
  2617. * const fixedFrameTransform = Cesium.Transforms.localFrameToFixedFrameGenerator(
  2618. * "north",
  2619. * "west"
  2620. * );
  2621. * try {
  2622. * const model = await Cesium.Model.fromGltfAsync({
  2623. * url: "../../SampleData/models/CesiumAir/Cesium_Air.glb",
  2624. * modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame(
  2625. * position,
  2626. * headingPositionRoll,
  2627. * Cesium.Ellipsoid.WGS84,
  2628. * fixedFrameTransform
  2629. * ),
  2630. * minimumPixelSize: 128,
  2631. * });
  2632. * viewer.scene.primitives.add(model);
  2633. * } catch (error) {
  2634. * console.log(`Failed to load model. ${error}`);
  2635. * }
  2636. *
  2637. * @example
  2638. * // Load a model and play the last animation at half speed
  2639. * let animations;
  2640. * try {
  2641. * const model = await Cesium.Model.fromGltfAsync({
  2642. * url: "../../SampleData/models/CesiumMan/Cesium_Man.glb",
  2643. * gltfCallback: gltf => {
  2644. * animations = gltf.animations
  2645. * }
  2646. * });
  2647. * viewer.scene.primitives.add(model);
  2648. * model.readyEvent.addEventListener(() => {
  2649. * model.activeAnimations.add({
  2650. * index: animations.length - 1,
  2651. * loop: Cesium.ModelAnimationLoop.REPEAT,
  2652. * multiplier: 0.5,
  2653. * });
  2654. * });
  2655. * } catch (error) {
  2656. * console.log(`Failed to load model. ${error}`);
  2657. * }
  2658. */
  2659. Model.fromGltfAsync = async function (options) {
  2660. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  2661. //>>includeStart('debug', pragmas.debug);
  2662. if (!defined(options.url) && !defined(options.gltf)) {
  2663. throw new DeveloperError("options.url is required.");
  2664. }
  2665. //>>includeEnd('debug');
  2666. // options.gltf is used internally for 3D Tiles. It can be a Resource, a URL
  2667. // to a glTF/glb file, a binary glTF buffer, or a JSON object containing the
  2668. // glTF contents.
  2669. const gltf = defaultValue(options.url, options.gltf);
  2670. const loaderOptions = {
  2671. releaseGltfJson: options.releaseGltfJson,
  2672. asynchronous: options.asynchronous,
  2673. incrementallyLoadTextures: options.incrementallyLoadTextures,
  2674. upAxis: options.upAxis,
  2675. forwardAxis: options.forwardAxis,
  2676. loadAttributesFor2D: options.projectTo2D,
  2677. loadIndicesForWireframe: options.enableDebugWireframe,
  2678. loadPrimitiveOutline: options.enableShowOutline,
  2679. loadForClassification: defined(options.classificationType),
  2680. };
  2681. const basePath = defaultValue(options.basePath, "");
  2682. const baseResource = Resource.createIfNeeded(basePath);
  2683. if (defined(gltf.asset)) {
  2684. loaderOptions.gltfJson = gltf;
  2685. loaderOptions.baseResource = baseResource;
  2686. loaderOptions.gltfResource = baseResource;
  2687. } else if (gltf instanceof Uint8Array) {
  2688. loaderOptions.typedArray = gltf;
  2689. loaderOptions.baseResource = baseResource;
  2690. loaderOptions.gltfResource = baseResource;
  2691. } else {
  2692. loaderOptions.gltfResource = Resource.createIfNeeded(gltf);
  2693. }
  2694. const loader = new GltfLoader(loaderOptions);
  2695. const is3DTiles = defined(options.content);
  2696. const type = is3DTiles ? ModelType.TILE_GLTF : ModelType.GLTF;
  2697. const resource = loaderOptions.gltfResource;
  2698. const modelOptions = makeModelOptions(loader, type, options);
  2699. modelOptions.resource = resource;
  2700. try {
  2701. // This load the gltf JSON and ensures the gltf is valid
  2702. // Further resource loading is handled synchronously in loader.process(), and requires
  2703. // hooking into model's update() as the frameState is needed
  2704. await loader.load();
  2705. } catch (error) {
  2706. loader.destroy();
  2707. throw ModelUtility.getError("model", resource, error);
  2708. }
  2709. const gltfCallback = options.gltfCallback;
  2710. if (defined(gltfCallback)) {
  2711. //>>includeStart('debug', pragmas.debug);
  2712. Check.typeOf.func("options.gltfCallback", gltfCallback);
  2713. //>>includeEnd('debug');
  2714. gltfCallback(loader.gltfJson);
  2715. }
  2716. const model = new Model(modelOptions);
  2717. const resourceCredits = model._resource.credits;
  2718. if (defined(resourceCredits)) {
  2719. const length = resourceCredits.length;
  2720. for (let i = 0; i < length; i++) {
  2721. model._resourceCredits.push(resourceCredits[i]);
  2722. }
  2723. }
  2724. return model;
  2725. };
  2726. /*
  2727. * @private
  2728. */
  2729. Model.fromB3dm = async function (options) {
  2730. const loaderOptions = {
  2731. b3dmResource: options.resource,
  2732. arrayBuffer: options.arrayBuffer,
  2733. byteOffset: options.byteOffset,
  2734. releaseGltfJson: options.releaseGltfJson,
  2735. asynchronous: options.asynchronous,
  2736. incrementallyLoadTextures: options.incrementallyLoadTextures,
  2737. upAxis: options.upAxis,
  2738. forwardAxis: options.forwardAxis,
  2739. loadAttributesFor2D: options.projectTo2D,
  2740. loadIndicesForWireframe: options.enableDebugWireframe,
  2741. loadPrimitiveOutline: options.enableShowOutline,
  2742. loadForClassification: defined(options.classificationType),
  2743. };
  2744. const loader = new B3dmLoader(loaderOptions);
  2745. try {
  2746. await loader.load();
  2747. const modelOptions = makeModelOptions(loader, ModelType.TILE_B3DM, options);
  2748. const model = new Model(modelOptions);
  2749. return model;
  2750. } catch (error) {
  2751. loader.destroy();
  2752. throw error;
  2753. }
  2754. };
  2755. /**
  2756. * @private
  2757. */
  2758. Model.fromPnts = async function (options) {
  2759. const loaderOptions = {
  2760. arrayBuffer: options.arrayBuffer,
  2761. byteOffset: options.byteOffset,
  2762. loadAttributesFor2D: options.projectTo2D,
  2763. };
  2764. const loader = new PntsLoader(loaderOptions);
  2765. try {
  2766. await loader.load();
  2767. const modelOptions = makeModelOptions(loader, ModelType.TILE_PNTS, options);
  2768. const model = new Model(modelOptions);
  2769. return model;
  2770. } catch (error) {
  2771. loader.destroy();
  2772. throw error;
  2773. }
  2774. };
  2775. /*
  2776. * @private
  2777. */
  2778. Model.fromI3dm = async function (options) {
  2779. const loaderOptions = {
  2780. i3dmResource: options.resource,
  2781. arrayBuffer: options.arrayBuffer,
  2782. byteOffset: options.byteOffset,
  2783. releaseGltfJson: options.releaseGltfJson,
  2784. asynchronous: options.asynchronous,
  2785. incrementallyLoadTextures: options.incrementallyLoadTextures,
  2786. upAxis: options.upAxis,
  2787. forwardAxis: options.forwardAxis,
  2788. loadAttributesFor2D: options.projectTo2D,
  2789. loadIndicesForWireframe: options.enableDebugWireframe,
  2790. loadPrimitiveOutline: options.enableShowOutline,
  2791. };
  2792. const loader = new I3dmLoader(loaderOptions);
  2793. try {
  2794. await loader.load();
  2795. const modelOptions = makeModelOptions(loader, ModelType.TILE_I3DM, options);
  2796. const model = new Model(modelOptions);
  2797. return model;
  2798. } catch (error) {
  2799. loader.destroy();
  2800. throw error;
  2801. }
  2802. };
  2803. /*
  2804. * @private
  2805. */
  2806. Model.fromGeoJson = async function (options) {
  2807. const loaderOptions = {
  2808. geoJson: options.geoJson,
  2809. };
  2810. const loader = new GeoJsonLoader(loaderOptions);
  2811. const modelOptions = makeModelOptions(
  2812. loader,
  2813. ModelType.TILE_GEOJSON,
  2814. options
  2815. );
  2816. const model = new Model(modelOptions);
  2817. return model;
  2818. };
  2819. /**
  2820. * @private
  2821. */
  2822. Model.prototype.applyColorAndShow = function (style) {
  2823. const previousColor = this._color;
  2824. const hasColorStyle = defined(style) && defined(style.color);
  2825. const hasShowStyle = defined(style) && defined(style.show);
  2826. this._color = hasColorStyle
  2827. ? style.color.evaluateColor(undefined, this._color)
  2828. : Color.clone(Color.WHITE, this._color);
  2829. this._show = hasShowStyle ? style.show.evaluate(undefined) : true;
  2830. if (isColorAlphaDirty(previousColor, this._color)) {
  2831. this.resetDrawCommands();
  2832. }
  2833. };
  2834. /**
  2835. * @private
  2836. */
  2837. Model.prototype.applyStyle = function (style) {
  2838. const isPnts = this.type === ModelType.TILE_PNTS;
  2839. const hasFeatureTable =
  2840. defined(this.featureTableId) &&
  2841. this.featureTables[this.featureTableId].featuresLength > 0;
  2842. const propertyAttributes = defined(this.structuralMetadata)
  2843. ? this.structuralMetadata.propertyAttributes
  2844. : undefined;
  2845. const hasPropertyAttributes =
  2846. defined(propertyAttributes) && defined(propertyAttributes[0]);
  2847. // Point clouds will be styled on the GPU unless they contain a batch table.
  2848. // That is, CPU styling will not be applied if:
  2849. // - points have no metadata at all, or
  2850. // - points have metadata stored as a property attribute
  2851. if (isPnts && (!hasFeatureTable || hasPropertyAttributes)) {
  2852. // Commands are rebuilt for point cloud styling since the new style may
  2853. // contain different shader functions.
  2854. this.resetDrawCommands();
  2855. return;
  2856. }
  2857. // The style is only set by the ModelFeatureTable. If there are no features,
  2858. // the color and show from the style are directly applied.
  2859. if (hasFeatureTable) {
  2860. const featureTable = this.featureTables[this.featureTableId];
  2861. featureTable.applyStyle(style);
  2862. updateStyleCommandsNeeded(this, style);
  2863. } else {
  2864. this.applyColorAndShow(style);
  2865. this._styleCommandsNeeded = undefined;
  2866. }
  2867. };
  2868. function makeModelOptions(loader, modelType, options) {
  2869. return {
  2870. loader: loader,
  2871. type: modelType,
  2872. resource: options.resource,
  2873. show: options.show,
  2874. modelMatrix: options.modelMatrix,
  2875. scale: options.scale,
  2876. minimumPixelSize: options.minimumPixelSize,
  2877. maximumScale: options.maximumScale,
  2878. id: options.id,
  2879. allowPicking: options.allowPicking,
  2880. clampAnimations: options.clampAnimations,
  2881. shadows: options.shadows,
  2882. debugShowBoundingVolume: options.debugShowBoundingVolume,
  2883. enableDebugWireframe: options.enableDebugWireframe,
  2884. debugWireframe: options.debugWireframe,
  2885. cull: options.cull,
  2886. opaquePass: options.opaquePass,
  2887. customShader: options.customShader,
  2888. content: options.content,
  2889. heightReference: options.heightReference,
  2890. scene: options.scene,
  2891. distanceDisplayCondition: options.distanceDisplayCondition,
  2892. color: options.color,
  2893. colorBlendAmount: options.colorBlendAmount,
  2894. colorBlendMode: options.colorBlendMode,
  2895. silhouetteColor: options.silhouetteColor,
  2896. silhouetteSize: options.silhouetteSize,
  2897. enableShowOutline: options.enableShowOutline,
  2898. showOutline: options.showOutline,
  2899. outlineColor: options.outlineColor,
  2900. clippingPlanes: options.clippingPlanes,
  2901. lightColor: options.lightColor,
  2902. imageBasedLighting: options.imageBasedLighting,
  2903. backFaceCulling: options.backFaceCulling,
  2904. credit: options.credit,
  2905. showCreditsOnScreen: options.showCreditsOnScreen,
  2906. splitDirection: options.splitDirection,
  2907. projectTo2D: options.projectTo2D,
  2908. featureIdLabel: options.featureIdLabel,
  2909. instanceFeatureIdLabel: options.instanceFeatureIdLabel,
  2910. pointCloudShading: options.pointCloudShading,
  2911. classificationType: options.classificationType,
  2912. pickObject: options.pickObject,
  2913. };
  2914. }
  2915. /**
  2916. * Interface for the function that is called with the loaded gltf object once loaded.
  2917. * @callback Model.GltfCallback
  2918. *
  2919. * @param {object} gltf The gltf object
  2920. */
  2921. export default Model;