智慧水务管理系统 - 精河县供水工程综合管理平台

GltfLoader.js 96KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285
  1. import ArticulationStageType from "../Core/ArticulationStageType.js";
  2. import Cartesian2 from "../Core/Cartesian2.js";
  3. import Cartesian3 from "../Core/Cartesian3.js";
  4. import Cartesian4 from "../Core/Cartesian4.js";
  5. import Check from "../Core/Check.js";
  6. import ComponentDatatype from "../Core/ComponentDatatype.js";
  7. import Credit from "../Core/Credit.js";
  8. import Frozen from "../Core/Frozen.js";
  9. import defined from "../Core/defined.js";
  10. import FeatureDetection from "../Core/FeatureDetection.js";
  11. import InterpolationType from "../Core/InterpolationType.js";
  12. import Matrix4 from "../Core/Matrix4.js";
  13. import PrimitiveType from "../Core/PrimitiveType.js";
  14. import Quaternion from "../Core/Quaternion.js";
  15. import RuntimeError from "../Core/RuntimeError.js";
  16. import Sampler from "../Renderer/Sampler.js";
  17. import getAccessorByteStride from "./GltfPipeline/getAccessorByteStride.js";
  18. import getComponentReader from "./GltfPipeline/getComponentReader.js";
  19. import numberOfComponentsForType from "./GltfPipeline/numberOfComponentsForType.js";
  20. import GltfStructuralMetadataLoader from "./GltfStructuralMetadataLoader.js";
  21. import AttributeType from "./AttributeType.js";
  22. import Axis from "./Axis.js";
  23. import GltfLoaderUtil from "./GltfLoaderUtil.js";
  24. import hasExtension from "./hasExtension.js";
  25. import InstanceAttributeSemantic from "./InstanceAttributeSemantic.js";
  26. import ModelComponents from "./ModelComponents.js";
  27. import PrimitiveLoadPlan from "./PrimitiveLoadPlan.js";
  28. import ResourceCache from "./ResourceCache.js";
  29. import ResourceLoader from "./ResourceLoader.js";
  30. import SupportedImageFormats from "./SupportedImageFormats.js";
  31. import VertexAttributeSemantic from "./VertexAttributeSemantic.js";
  32. import GltfGpmLoader from "./Model/Extensions/Gpm/GltfGpmLoader.js";
  33. import GltfMeshPrimitiveGpmLoader from "./Model/Extensions/Gpm/GltfMeshPrimitiveGpmLoader.js";
  34. import oneTimeWarning from "../Core/oneTimeWarning.js";
  35. import addAllToArray from "../Core/addAllToArray.js";
  36. import getMeshPrimitives from "./getMeshPrimitives.js";
  37. const {
  38. Attribute,
  39. Indices,
  40. FeatureIdAttribute,
  41. FeatureIdTexture,
  42. FeatureIdImplicitRange,
  43. MorphTarget,
  44. Primitive,
  45. Instances,
  46. Skin,
  47. Node,
  48. AnimatedPropertyType,
  49. AnimationSampler,
  50. AnimationTarget,
  51. AnimationChannel,
  52. Animation,
  53. ArticulationStage,
  54. Articulation,
  55. Asset,
  56. Scene,
  57. Components,
  58. MetallicRoughness,
  59. SpecularGlossiness,
  60. Specular,
  61. Anisotropy,
  62. Clearcoat,
  63. LineStyle,
  64. Material,
  65. Vector,
  66. Polygon,
  67. } = ModelComponents;
  68. /**
  69. * States of the glTF loading process. These states also apply to
  70. * asynchronous texture loading unless otherwise noted
  71. *
  72. * @enum {number}
  73. *
  74. * @private
  75. */
  76. const GltfLoaderState = {
  77. /**
  78. * The initial state of the glTF loader before load() is called.
  79. *
  80. * @type {number}
  81. * @constant
  82. *
  83. * @private
  84. */
  85. NOT_LOADED: 0,
  86. /**
  87. * The state of the loader while waiting for the glTF JSON loader promise
  88. * to resolve.
  89. *
  90. * @type {number}
  91. * @constant
  92. *
  93. * @private
  94. */
  95. LOADING: 1,
  96. /**
  97. * The state of the loader once the glTF JSON is loaded but before
  98. * process() is called.
  99. *
  100. * @type {number}
  101. * @constant
  102. *
  103. * @private
  104. */
  105. LOADED: 2,
  106. /**
  107. * The state of the loader while parsing the glTF and creating GPU resources
  108. * as needed.
  109. *
  110. * @type {number}
  111. * @constant
  112. *
  113. * @private
  114. */
  115. PROCESSING: 3,
  116. /**
  117. * For some features like handling CESIUM_primitive_outlines, the geometry
  118. * must be modified after it is loaded. The post-processing state handles
  119. * any geometry modification (if needed).
  120. * <p>
  121. * This state is not used for asynchronous texture loading.
  122. * </p>
  123. *
  124. * @type {number}
  125. * @constant
  126. *
  127. * @private
  128. */
  129. POST_PROCESSING: 4,
  130. /**
  131. * Once the processing/post-processing states are finished, the loader
  132. * enters the processed state (sometimes from a promise chain). The next
  133. * call to process() will advance to the ready state.
  134. *
  135. * @type {number}
  136. * @constant
  137. *
  138. * @private
  139. */
  140. PROCESSED: 5,
  141. /**
  142. * When the loader reaches the ready state, the loaders' promise will be
  143. * resolved.
  144. *
  145. * @type {number}
  146. * @constant
  147. *
  148. * @private
  149. */
  150. READY: 6,
  151. /**
  152. * If an error occurs at any point, the loader switches to the failed state.
  153. *
  154. * @type {number}
  155. * @constant
  156. *
  157. * @private
  158. */
  159. FAILED: 7,
  160. /**
  161. * If unload() is called, the loader switches to the unloaded state.
  162. *
  163. * @type {number}
  164. * @constant
  165. *
  166. * @private
  167. */
  168. UNLOADED: 8,
  169. };
  170. /**
  171. * Loads a glTF model.
  172. * <p>
  173. * Implements the {@link ResourceLoader} interface.
  174. * </p>
  175. *
  176. * @private
  177. */
  178. class GltfLoader extends ResourceLoader {
  179. /**
  180. * @param {object} options Object with the following properties:
  181. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF. This is often the path of the .gltf or .glb file, but may also be the path of the .b3dm, .i3dm, or .cmpt file containing the embedded glb. .cmpt resources should have a URI fragment indicating the index of the inner content to which the glb belongs in order to individually identify the glb in the cache, e.g. http://example.com/tile.cmpt#index=2.
  182. * @param {Resource} [options.baseResource] The {@link Resource} that paths in the glTF JSON are relative to.
  183. * @param {Uint8Array} [options.typedArray] The typed array containing the glTF contents, e.g. from a .b3dm, .i3dm, or .cmpt file.
  184. * @param {object} [options.gltfJson] A parsed glTF JSON file instead of passing it in as a typed array.
  185. * @param {boolean} [options.releaseGltfJson=false] When true, the glTF JSON is released once the glTF is loaded. This is especially useful for cases like 3D Tiles, where each .gltf model is unique and caching the glTF JSON is not effective.
  186. * @param {boolean} [options.asynchronous=true] Determines if WebGL resource creation will be spread out over several frames or block until all WebGL resources are created.
  187. * @param {boolean} [options.incrementallyLoadTextures=true] Determine if textures may continue to stream in after the glTF is loaded.
  188. * @param {Axis} [options.upAxis=Axis.Y] The up-axis of the glTF model.
  189. * @param {Axis} [options.forwardAxis=Axis.Z] The forward-axis of the glTF model.
  190. * @param {boolean} [options.loadAttributesAsTypedArray=false] Load all attributes and indices as typed arrays instead of GPU buffers. If the attributes are interleaved in the glTF they will be de-interleaved in the typed array.
  191. * @param {boolean} [options.loadAttributesFor2D=false] If <code>true</code>, load the positions buffer and any instanced attribute buffers as typed arrays for accurately projecting models to 2D.
  192. * @param {boolean} [options.enablePick=false] If <code>true</code>, load the positions buffer, any instanced attribute buffers, and index buffer as typed arrays for CPU-enabled picking in WebGL1.
  193. * @param {boolean} [options.loadIndicesForWireframe=false] If <code>true</code>, load the index buffer as both a buffer and typed array. The latter is useful for creating wireframe indices in WebGL1.
  194. * @param {boolean} [options.loadPrimitiveOutline=true] If <code>true</code>, load outlines from 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.
  195. * @param {boolean} [options.loadForClassification=false] If <code>true</code> and if the model has feature IDs, load the feature IDs and indices as typed arrays. This is useful for batching features for classification.
  196. * @param {boolean} [options.renameBatchIdSemantic=false] If <code>true</code>, rename _BATCHID or BATCHID to _FEATURE_ID_0. This is used for .b3dm models
  197. */
  198. constructor(options) {
  199. super();
  200. options = options ?? Frozen.EMPTY_OBJECT;
  201. const {
  202. gltfResource,
  203. typedArray,
  204. releaseGltfJson = false,
  205. asynchronous = true,
  206. incrementallyLoadTextures = true,
  207. upAxis = Axis.Y,
  208. forwardAxis = Axis.Z,
  209. loadAttributesAsTypedArray = false,
  210. loadAttributesFor2D = false,
  211. enablePick = false,
  212. loadIndicesForWireframe = false,
  213. loadPrimitiveOutline = true,
  214. loadForClassification = false,
  215. renameBatchIdSemantic = false,
  216. } = options;
  217. //>>includeStart('debug', pragmas.debug);
  218. Check.typeOf.object("options.gltfResource", gltfResource);
  219. //>>includeEnd('debug');
  220. const { baseResource = gltfResource.clone() } = options;
  221. this._gltfJson = options.gltfJson;
  222. this._gltfResource = gltfResource;
  223. this._baseResource = baseResource;
  224. this._typedArray = typedArray;
  225. this._releaseGltfJson = releaseGltfJson;
  226. this._asynchronous = asynchronous;
  227. this._incrementallyLoadTextures = incrementallyLoadTextures;
  228. this._upAxis = upAxis;
  229. this._forwardAxis = forwardAxis;
  230. this._loadAttributesAsTypedArray = loadAttributesAsTypedArray;
  231. this._loadAttributesFor2D = loadAttributesFor2D;
  232. this._enablePick = enablePick;
  233. this._loadIndicesForWireframe = loadIndicesForWireframe;
  234. this._loadPrimitiveOutline = loadPrimitiveOutline;
  235. this._loadForClassification = loadForClassification;
  236. this._renameBatchIdSemantic = renameBatchIdSemantic;
  237. // When loading EXT_feature_metadata, the feature tables and textures
  238. // are now stored as arrays like the newer EXT_structural_metadata extension.
  239. // This requires sorting the dictionary keys for a consistent ordering.
  240. this._sortedPropertyTableIds = undefined;
  241. this._sortedFeatureTextureIds = undefined;
  242. this._gltfJsonLoader = undefined;
  243. this._state = GltfLoaderState.NOT_LOADED;
  244. this._textureState = GltfLoaderState.NOT_LOADED;
  245. this._promise = undefined;
  246. this._processError = undefined;
  247. this._textureErrors = [];
  248. // Information about whether to load primitives as typed arrays or buffers,
  249. // and whether post-processing is needed after loading (e.g. for
  250. // generating outlines)
  251. this._primitiveLoadPlans = [];
  252. // Loaders that need to be processed before the glTF becomes ready
  253. this._loaderPromises = [];
  254. this._textureLoaders = [];
  255. this._texturesPromises = [];
  256. this._textureCallbacks = [];
  257. this._bufferViewLoaders = [];
  258. this._geometryLoaders = [];
  259. this._geometryCallbacks = [];
  260. this._structuralMetadataLoader = undefined;
  261. this._meshPrimitiveGpmLoader = undefined;
  262. this._loadResourcesPromise = undefined;
  263. this._resourcesLoaded = false;
  264. this._texturesLoaded = false;
  265. this._supportedImageFormats = undefined;
  266. // In some cases where geometry post-processing is needed (like generating
  267. // outlines) new attributes are added that may have GPU resources attached.
  268. // The GltfLoader will own the resources and store them here.
  269. this._postProcessBuffers = [];
  270. // Loaded results
  271. this._components = undefined;
  272. }
  273. /**
  274. * The cache key of the resource.
  275. *
  276. *
  277. * @type {string}
  278. * @readonly
  279. * @private
  280. */
  281. get cacheKey() {
  282. return undefined;
  283. }
  284. /**
  285. * The loaded components.
  286. *
  287. *
  288. * @type {ModelComponents.Components}
  289. * @readonly
  290. * @private
  291. */
  292. get components() {
  293. return this._components;
  294. }
  295. /**
  296. * The loaded glTF json.
  297. *
  298. *
  299. * @type {object}
  300. * @readonly
  301. * @private
  302. */
  303. get gltfJson() {
  304. if (defined(this._gltfJsonLoader)) {
  305. return this._gltfJsonLoader.gltf;
  306. }
  307. return this._gltfJson;
  308. }
  309. /**
  310. * Returns true if textures are loaded separately from the other glTF resources.
  311. *
  312. *
  313. * @type {boolean}
  314. * @readonly
  315. * @private
  316. */
  317. get incrementallyLoadTextures() {
  318. return this._incrementallyLoadTextures;
  319. }
  320. /**
  321. * true if textures are loaded, useful when incrementallyLoadTextures is true
  322. *
  323. *
  324. * @type {boolean}
  325. * @readonly
  326. * @private
  327. */
  328. get texturesLoaded() {
  329. return this._texturesLoaded;
  330. }
  331. /**
  332. * Loads the resource.
  333. * @returns {Promise.<GltfLoader>} A promise which resolves to the loader when the resource loading is completed.
  334. * @exception {RuntimeError} Unsupported glTF version
  335. * @exception {RuntimeError} Unsupported glTF Extension
  336. * @private
  337. */
  338. async load() {
  339. if (defined(this._promise)) {
  340. return this._promise;
  341. }
  342. this._promise = loadGltfJson(this);
  343. return this._promise;
  344. }
  345. /**
  346. * Process loaders other than textures
  347. * @private
  348. */
  349. _process(frameState) {
  350. if (this._state === GltfLoaderState.READY) {
  351. return true;
  352. }
  353. if (this._state === GltfLoaderState.PROCESSING) {
  354. processLoaders(this, frameState);
  355. }
  356. if (
  357. this._resourcesLoaded &&
  358. this._state === GltfLoaderState.POST_PROCESSING
  359. ) {
  360. postProcessGeometry(this, frameState.context);
  361. this._state = GltfLoaderState.PROCESSED;
  362. }
  363. if (this._resourcesLoaded && this._state === GltfLoaderState.PROCESSED) {
  364. // The buffer views can be unloaded once the data is copied.
  365. unloadBufferViewLoaders(this);
  366. // Similarly, if the glTF was loaded from a typed array, release the memory
  367. this._typedArray = undefined;
  368. this._state = GltfLoaderState.READY;
  369. return true;
  370. }
  371. return false;
  372. }
  373. /**
  374. * Process textures other than textures
  375. * @private
  376. */
  377. _processTextures(frameState) {
  378. if (this._textureState === GltfLoaderState.READY) {
  379. return true;
  380. }
  381. if (this._textureState !== GltfLoaderState.PROCESSING) {
  382. return false;
  383. }
  384. let ready = true;
  385. const textureLoaders = this._textureLoaders;
  386. for (let i = 0; i < textureLoaders.length; ++i) {
  387. const textureReady = textureLoaders[i].process(frameState);
  388. if (textureReady && defined(this._textureCallbacks[i])) {
  389. this._textureCallbacks[i]();
  390. this._textureCallbacks[i] = undefined;
  391. }
  392. ready = ready && textureReady;
  393. }
  394. if (!ready) {
  395. return false;
  396. }
  397. this._textureState = GltfLoaderState.READY;
  398. this._texturesLoaded = true;
  399. return true;
  400. }
  401. /**
  402. * Processes the resource until it becomes ready.
  403. *
  404. * @param {FrameState} frameState The frame state.
  405. * @private
  406. */
  407. process(frameState) {
  408. //>>includeStart('debug', pragmas.debug);
  409. Check.typeOf.object("frameState", frameState);
  410. //>>includeEnd('debug');
  411. if (
  412. this._state === GltfLoaderState.LOADED &&
  413. !defined(this._loadResourcesPromise)
  414. ) {
  415. this._loadResourcesPromise = loadResources(this, frameState)
  416. .then(() => {
  417. this._resourcesLoaded = true;
  418. })
  419. .catch((error) => {
  420. this._processError = error;
  421. });
  422. }
  423. if (defined(this._processError)) {
  424. this._state = GltfLoaderState.FAILED;
  425. const error = this._processError;
  426. this._processError = undefined;
  427. handleError(this, error);
  428. }
  429. // Pop the next error of the list in case there are multiple
  430. const textureError = this._textureErrors.pop();
  431. if (defined(textureError)) {
  432. // There shouldn't be the need to completely unload in this case. Just throw the error.
  433. const error = this.getError("Failed to load glTF texture", textureError);
  434. error.name = "TextureError";
  435. throw error;
  436. }
  437. if (this._state === GltfLoaderState.FAILED) {
  438. return false;
  439. }
  440. let ready = false;
  441. try {
  442. ready = this._process(frameState);
  443. } catch (error) {
  444. this._state = GltfLoaderState.FAILED;
  445. handleError(this, error);
  446. }
  447. // Since textures can be loaded independently and are handled through a separate promise, they are processed in their own function
  448. let texturesReady = false;
  449. try {
  450. texturesReady = this._processTextures(frameState);
  451. } catch (error) {
  452. this._textureState = GltfLoaderState.FAILED;
  453. handleError(this, error);
  454. }
  455. if (this._incrementallyLoadTextures) {
  456. return ready;
  457. }
  458. return ready && texturesReady;
  459. }
  460. /**
  461. * Returns whether the resource has been unloaded.
  462. * @private
  463. */
  464. isUnloaded() {
  465. return this._state === GltfLoaderState.UNLOADED;
  466. }
  467. /**
  468. * Unloads the resource.
  469. * @private
  470. */
  471. unload() {
  472. if (defined(this._gltfJsonLoader) && !this._gltfJsonLoader.isDestroyed()) {
  473. ResourceCache.unload(this._gltfJsonLoader);
  474. }
  475. this._gltfJsonLoader = undefined;
  476. unloadTextures(this);
  477. unloadBufferViewLoaders(this);
  478. unloadGeometry(this);
  479. unloadGeneratedAttributes(this);
  480. unloadStructuralMetadata(this);
  481. unloadMeshPrimitiveGpm(this);
  482. this._components = undefined;
  483. this._typedArray = undefined;
  484. this._state = GltfLoaderState.UNLOADED;
  485. }
  486. }
  487. /**
  488. * Loads the gltf object
  489. */
  490. async function loadGltfJson(loader) {
  491. loader._state = GltfLoaderState.LOADING;
  492. loader._textureState = GltfLoaderState.LOADING;
  493. try {
  494. const gltfJsonLoader = ResourceCache.getGltfJsonLoader({
  495. gltfResource: loader._gltfResource,
  496. baseResource: loader._baseResource,
  497. typedArray: loader._typedArray,
  498. gltfJson: loader._gltfJson,
  499. });
  500. loader._gltfJsonLoader = gltfJsonLoader;
  501. await gltfJsonLoader.load();
  502. if (
  503. loader.isDestroyed() ||
  504. loader.isUnloaded() ||
  505. gltfJsonLoader.isDestroyed()
  506. ) {
  507. return;
  508. }
  509. loader._state = GltfLoaderState.LOADED;
  510. loader._textureState = GltfLoaderState.LOADED;
  511. return loader;
  512. } catch (error) {
  513. if (loader.isDestroyed()) {
  514. return;
  515. }
  516. loader._state = GltfLoaderState.FAILED;
  517. loader._textureState = GltfLoaderState.FAILED;
  518. handleError(loader, error);
  519. }
  520. }
  521. async function loadResources(loader, frameState) {
  522. if (!FeatureDetection.supportsWebP.initialized) {
  523. await FeatureDetection.supportsWebP.initialize();
  524. }
  525. loader._supportedImageFormats = new SupportedImageFormats({
  526. webp: FeatureDetection.supportsWebP(),
  527. basis: frameState.context.supportsBasis,
  528. });
  529. // Loaders that create GPU resources need to be processed every frame until they become
  530. // ready since the JobScheduler is not able to execute all jobs in a single
  531. // frame. Any promise failures are collected, and will be handled synchronously in process().
  532. // Also note that it's fine to call process before a loader is ready to process or
  533. // after it has failed; nothing will happen.
  534. const promise = parse(loader, frameState);
  535. // All resource loaders have been created, so we can begin processing
  536. loader._state = GltfLoaderState.PROCESSING;
  537. loader._textureState = GltfLoaderState.PROCESSING;
  538. if (defined(loader._gltfJsonLoader) && loader._releaseGltfJson) {
  539. // Check that the glTF JSON loader is still defined before trying to unload it.
  540. // It can be unloaded if the glTF loader is destroyed.
  541. ResourceCache.unload(loader._gltfJsonLoader);
  542. loader._gltfJsonLoader = undefined;
  543. }
  544. return promise;
  545. }
  546. function handleError(gltfLoader, error) {
  547. gltfLoader.unload();
  548. const errorMessage = "Failed to load glTF";
  549. throw gltfLoader.getError(errorMessage, error);
  550. }
  551. function processLoaders(loader, frameState) {
  552. let ready = true;
  553. const geometryLoaders = loader._geometryLoaders;
  554. for (let i = 0; i < geometryLoaders.length; ++i) {
  555. const geometryReady = geometryLoaders[i].process(frameState);
  556. if (geometryReady && defined(loader._geometryCallbacks[i])) {
  557. loader._geometryCallbacks[i]();
  558. loader._geometryCallbacks[i] = undefined;
  559. }
  560. ready = ready && geometryReady;
  561. }
  562. const structuralMetadataLoader = loader._structuralMetadataLoader;
  563. if (defined(structuralMetadataLoader)) {
  564. const metadataReady = structuralMetadataLoader.process(frameState);
  565. if (metadataReady) {
  566. loader._components.structuralMetadata =
  567. structuralMetadataLoader.structuralMetadata;
  568. }
  569. ready = ready && metadataReady;
  570. }
  571. const meshPrimitiveGpmLoader = loader._meshPrimitiveGpmLoader;
  572. if (defined(meshPrimitiveGpmLoader)) {
  573. const metadataReady = meshPrimitiveGpmLoader.process(frameState);
  574. if (metadataReady) {
  575. if (defined(loader._components.structuralMetadata)) {
  576. oneTimeWarning(
  577. "structural-metadata-gpm",
  578. "The model defines both the 'EXT_structural_metadata' extension and the " +
  579. "'NGA_gpm_local' extension. The data from the 'EXT_structural_metadata' " +
  580. "extension will be replaced with the data from the 'NGA_gpm_local' extension, " +
  581. "and will no longer be available for styling and picking.",
  582. );
  583. }
  584. loader._components.structuralMetadata =
  585. meshPrimitiveGpmLoader.structuralMetadata;
  586. }
  587. ready = ready && metadataReady;
  588. }
  589. if (ready) {
  590. // Geometry requires further processing
  591. loader._state = GltfLoaderState.POST_PROCESSING;
  592. }
  593. }
  594. function postProcessGeometry(loader, context) {
  595. // Apply post-processing steps on geometry such as
  596. // updating attributes for rendering outlines.
  597. const loadPlans = loader._primitiveLoadPlans;
  598. for (let i = 0; i < loadPlans.length; i++) {
  599. const loadPlan = loadPlans[i];
  600. loadPlan.postProcess(context);
  601. if (loadPlan.needsOutlines || loadPlan.needsGaussianSplats) {
  602. // The glTF loader takes ownership of any buffers generated in the
  603. // post-process stage since they were created after the geometry loaders
  604. // finished. This way they can be destroyed when the loader is destroyed.
  605. gatherPostProcessBuffers(loader, loadPlan);
  606. }
  607. }
  608. }
  609. function gatherPostProcessBuffers(loader, primitiveLoadPlan) {
  610. const buffers = loader._postProcessBuffers;
  611. const primitive = primitiveLoadPlan.primitive;
  612. const outlineCoordinates = primitive.outlineCoordinates;
  613. if (defined(outlineCoordinates)) {
  614. // outline coordinates are always loaded as a buffer.
  615. buffers.push(outlineCoordinates.buffer);
  616. }
  617. // to do post-processing, all the attributes are loaded as typed arrays
  618. // so if a buffer exists, it was newly generated
  619. const attributes = primitive.attributes;
  620. for (let i = 0; i < attributes.length; i++) {
  621. const attribute = attributes[i];
  622. if (defined(attribute.buffer)) {
  623. buffers.push(attribute.buffer);
  624. }
  625. }
  626. // Similarly for the indices.
  627. const indices = primitive.indices;
  628. if (defined(indices) && defined(indices.buffer)) {
  629. buffers.push(indices.buffer);
  630. }
  631. }
  632. function getVertexBufferLoader(
  633. loader,
  634. accessorId,
  635. semantic,
  636. primitive,
  637. draco,
  638. spzExtension,
  639. loadBuffer,
  640. loadTypedArray,
  641. frameState,
  642. ) {
  643. const gltf = loader.gltfJson;
  644. const accessor = gltf.accessors[accessorId];
  645. const bufferViewId = accessor.bufferView;
  646. const vertexBufferLoader = ResourceCache.getVertexBufferLoader({
  647. gltf: gltf,
  648. gltfResource: loader._gltfResource,
  649. baseResource: loader._baseResource,
  650. frameState: frameState,
  651. bufferViewId: bufferViewId,
  652. primitive: primitive,
  653. draco: draco,
  654. spz: spzExtension,
  655. attributeSemantic: semantic,
  656. accessorId: accessorId,
  657. asynchronous: loader._asynchronous,
  658. loadBuffer: loadBuffer,
  659. loadTypedArray: loadTypedArray,
  660. });
  661. return vertexBufferLoader;
  662. }
  663. function getIndexBufferLoader(
  664. loader,
  665. accessorId,
  666. primitive,
  667. draco,
  668. loadBuffer,
  669. loadTypedArray,
  670. frameState,
  671. ) {
  672. const indexBufferLoader = ResourceCache.getIndexBufferLoader({
  673. gltf: loader.gltfJson,
  674. accessorId: accessorId,
  675. gltfResource: loader._gltfResource,
  676. baseResource: loader._baseResource,
  677. frameState: frameState,
  678. primitive: primitive,
  679. draco: draco,
  680. asynchronous: loader._asynchronous,
  681. loadBuffer: loadBuffer,
  682. loadTypedArray: loadTypedArray,
  683. });
  684. return indexBufferLoader;
  685. }
  686. function getBufferViewLoader(loader, bufferViewId) {
  687. const bufferViewLoader = ResourceCache.getBufferViewLoader({
  688. gltf: loader.gltfJson,
  689. bufferViewId: bufferViewId,
  690. gltfResource: loader._gltfResource,
  691. baseResource: loader._baseResource,
  692. });
  693. loader._bufferViewLoaders.push(bufferViewLoader);
  694. return bufferViewLoader;
  695. }
  696. function getPackedTypedArray(gltf, accessor, bufferViewTypedArray) {
  697. let byteOffset = accessor.byteOffset;
  698. const byteStride = getAccessorByteStride(gltf, accessor);
  699. const count = accessor.count;
  700. const componentCount = numberOfComponentsForType(accessor.type);
  701. const componentType = accessor.componentType;
  702. const componentByteLength = ComponentDatatype.getSizeInBytes(componentType);
  703. const defaultByteStride = componentByteLength * componentCount;
  704. const componentsLength = count * componentCount;
  705. if (byteStride === defaultByteStride) {
  706. // Copy the typed array and let the underlying ArrayBuffer be freed
  707. bufferViewTypedArray = new Uint8Array(bufferViewTypedArray);
  708. return ComponentDatatype.createArrayBufferView(
  709. componentType,
  710. bufferViewTypedArray.buffer,
  711. bufferViewTypedArray.byteOffset + byteOffset,
  712. componentsLength,
  713. );
  714. }
  715. const accessorTypedArray = ComponentDatatype.createTypedArray(
  716. componentType,
  717. componentsLength,
  718. );
  719. const dataView = new DataView(bufferViewTypedArray.buffer);
  720. const components = new Array(componentCount);
  721. const componentReader = getComponentReader(accessor.componentType);
  722. byteOffset = bufferViewTypedArray.byteOffset + byteOffset;
  723. for (let i = 0; i < count; ++i) {
  724. componentReader(
  725. dataView,
  726. byteOffset,
  727. componentCount,
  728. componentByteLength,
  729. components,
  730. );
  731. for (let j = 0; j < componentCount; ++j) {
  732. accessorTypedArray[i * componentCount + j] = components[j];
  733. }
  734. byteOffset += byteStride;
  735. }
  736. return accessorTypedArray;
  737. }
  738. function loadDefaultAccessorValues(accessor, values) {
  739. const accessorType = accessor.type;
  740. if (accessorType === AttributeType.SCALAR) {
  741. return values.fill(0);
  742. }
  743. const MathType = AttributeType.getMathType(accessorType);
  744. return values.fill(MathType.clone(MathType.ZERO));
  745. }
  746. function loadAccessorValues(accessor, typedArray, values, useQuaternion) {
  747. const accessorType = accessor.type;
  748. const accessorCount = accessor.count;
  749. if (accessorType === AttributeType.SCALAR) {
  750. for (let i = 0; i < accessorCount; i++) {
  751. values[i] = typedArray[i];
  752. }
  753. } else if (accessorType === AttributeType.VEC4 && useQuaternion) {
  754. for (let i = 0; i < accessorCount; i++) {
  755. values[i] = Quaternion.unpack(typedArray, i * 4);
  756. }
  757. } else {
  758. const MathType = AttributeType.getMathType(accessorType);
  759. const numberOfComponents =
  760. AttributeType.getNumberOfComponents(accessorType);
  761. for (let i = 0; i < accessorCount; i++) {
  762. values[i] = MathType.unpack(typedArray, i * numberOfComponents);
  763. }
  764. }
  765. return values;
  766. }
  767. async function loadAccessorBufferView(
  768. loader,
  769. bufferViewLoader,
  770. accessor,
  771. useQuaternion,
  772. values,
  773. ) {
  774. // Save a link to the gltfJson, which is removed after bufferViewLoader.load()
  775. const { gltfJson } = loader;
  776. await bufferViewLoader.load();
  777. if (loader.isDestroyed()) {
  778. return;
  779. }
  780. const typedArray = getPackedTypedArray(
  781. gltfJson,
  782. accessor,
  783. bufferViewLoader.typedArray,
  784. );
  785. useQuaternion = useQuaternion ?? false;
  786. loadAccessorValues(accessor, typedArray, values, useQuaternion);
  787. }
  788. function loadAccessor(loader, accessor, useQuaternion) {
  789. const values = new Array(accessor.count);
  790. const bufferViewId = accessor.bufferView;
  791. if (defined(bufferViewId)) {
  792. const bufferViewLoader = getBufferViewLoader(loader, bufferViewId);
  793. const promise = loadAccessorBufferView(
  794. loader,
  795. bufferViewLoader,
  796. accessor,
  797. useQuaternion,
  798. values,
  799. );
  800. loader._loaderPromises.push(promise);
  801. return values;
  802. }
  803. return loadDefaultAccessorValues(accessor, values);
  804. }
  805. function loadAccessorTypedArray(loader, accessor) {
  806. const values = ComponentDatatype.createTypedArray(
  807. accessor.componentType,
  808. accessor.count * AttributeType.getNumberOfComponents(accessor.type),
  809. );
  810. if (!defined(accessor.bufferView)) {
  811. return values;
  812. }
  813. const bufferViewLoader = getBufferViewLoader(loader, accessor.bufferView);
  814. // Save a link to the gltfJson, which is removed after bufferViewLoader.load()
  815. const { gltfJson } = loader;
  816. const promise = bufferViewLoader.load().then(() => {
  817. if (loader.isDestroyed()) {
  818. return;
  819. }
  820. const result = getPackedTypedArray(
  821. gltfJson,
  822. accessor,
  823. bufferViewLoader.typedArray,
  824. );
  825. values.set(result);
  826. });
  827. loader._loaderPromises.push(promise);
  828. return values;
  829. }
  830. function fromArray(MathType, values) {
  831. if (!defined(values)) {
  832. return undefined;
  833. }
  834. if (MathType === Number) {
  835. return values[0];
  836. }
  837. return MathType.unpack(values);
  838. }
  839. function getDefault(MathType) {
  840. if (MathType === Number) {
  841. return 0.0;
  842. }
  843. return new MathType(); // defaults to 0.0 for all types
  844. }
  845. function getQuantizationDivisor(componentDatatype) {
  846. switch (componentDatatype) {
  847. case ComponentDatatype.BYTE:
  848. return 127;
  849. case ComponentDatatype.UNSIGNED_BYTE:
  850. return 255;
  851. case ComponentDatatype.SHORT:
  852. return 32767;
  853. case ComponentDatatype.UNSIGNED_SHORT:
  854. return 65535;
  855. default:
  856. return 1.0;
  857. }
  858. }
  859. const minimumBoundsByType = {
  860. VEC2: new Cartesian2(-1.0, -1.0),
  861. VEC3: new Cartesian3(-1.0, -1.0, -1.0),
  862. VEC4: new Cartesian4(-1.0, -1.0, -1.0, -1.0),
  863. };
  864. function dequantizeMinMax(attribute, VectorType) {
  865. const divisor = getQuantizationDivisor(attribute.componentDatatype);
  866. const minimumBound = minimumBoundsByType[attribute.type];
  867. // dequantized = max(quantized / divisor, -1.0)
  868. let min = attribute.min;
  869. if (defined(min)) {
  870. min = VectorType.divideByScalar(min, divisor, min);
  871. min = VectorType.maximumByComponent(min, minimumBound, min);
  872. }
  873. let max = attribute.max;
  874. if (defined(max)) {
  875. max = VectorType.divideByScalar(max, divisor, max);
  876. max = VectorType.maximumByComponent(max, minimumBound, max);
  877. }
  878. attribute.min = min;
  879. attribute.max = max;
  880. }
  881. function setQuantizationFromWeb3dQuantizedAttributes(
  882. extension,
  883. attribute,
  884. MathType,
  885. ) {
  886. const decodeMatrix = extension.decodeMatrix;
  887. const decodedMin = fromArray(MathType, extension.decodedMin);
  888. const decodedMax = fromArray(MathType, extension.decodedMax);
  889. if (defined(decodedMin) && defined(decodedMax)) {
  890. attribute.min = decodedMin;
  891. attribute.max = decodedMax;
  892. }
  893. const quantization = new ModelComponents.Quantization();
  894. quantization.componentDatatype = attribute.componentDatatype;
  895. quantization.type = attribute.type;
  896. if (decodeMatrix.length === 4) {
  897. quantization.quantizedVolumeOffset = decodeMatrix[2];
  898. quantization.quantizedVolumeStepSize = decodeMatrix[0];
  899. } else if (decodeMatrix.length === 9) {
  900. quantization.quantizedVolumeOffset = new Cartesian2(
  901. decodeMatrix[6],
  902. decodeMatrix[7],
  903. );
  904. quantization.quantizedVolumeStepSize = new Cartesian2(
  905. decodeMatrix[0],
  906. decodeMatrix[4],
  907. );
  908. } else if (decodeMatrix.length === 16) {
  909. quantization.quantizedVolumeOffset = new Cartesian3(
  910. decodeMatrix[12],
  911. decodeMatrix[13],
  912. decodeMatrix[14],
  913. );
  914. quantization.quantizedVolumeStepSize = new Cartesian3(
  915. decodeMatrix[0],
  916. decodeMatrix[5],
  917. decodeMatrix[10],
  918. );
  919. } else if (decodeMatrix.length === 25) {
  920. quantization.quantizedVolumeOffset = new Cartesian4(
  921. decodeMatrix[20],
  922. decodeMatrix[21],
  923. decodeMatrix[22],
  924. decodeMatrix[23],
  925. );
  926. quantization.quantizedVolumeStepSize = new Cartesian4(
  927. decodeMatrix[0],
  928. decodeMatrix[6],
  929. decodeMatrix[12],
  930. decodeMatrix[18],
  931. );
  932. }
  933. attribute.quantization = quantization;
  934. }
  935. function createAttribute(gltf, accessorId, name, semantic, setIndex) {
  936. const accessor = gltf.accessors[accessorId];
  937. const MathType = AttributeType.getMathType(accessor.type);
  938. const normalized = accessor.normalized ?? false;
  939. const attribute = new Attribute();
  940. attribute.name = name;
  941. attribute.semantic = semantic;
  942. attribute.setIndex = setIndex;
  943. attribute.constant = getDefault(MathType);
  944. attribute.componentDatatype = accessor.componentType;
  945. attribute.normalized = normalized;
  946. attribute.count = accessor.count;
  947. attribute.type = accessor.type;
  948. attribute.min = fromArray(MathType, accessor.min);
  949. attribute.max = fromArray(MathType, accessor.max);
  950. attribute.byteOffset = accessor.byteOffset;
  951. attribute.byteStride = getAccessorByteStride(gltf, accessor);
  952. if (hasExtension(accessor, "WEB3D_quantized_attributes")) {
  953. setQuantizationFromWeb3dQuantizedAttributes(
  954. accessor.extensions.WEB3D_quantized_attributes,
  955. attribute,
  956. MathType,
  957. );
  958. }
  959. const isQuantizable =
  960. attribute.semantic === VertexAttributeSemantic.POSITION ||
  961. attribute.semantic === VertexAttributeSemantic.NORMAL ||
  962. attribute.semantic === VertexAttributeSemantic.TANGENT ||
  963. attribute.semantic === VertexAttributeSemantic.TEXCOORD ||
  964. attribute.semantic === VertexAttributeSemantic.FEATURE_ID ||
  965. attribute.semantic === VertexAttributeSemantic.SCALE ||
  966. attribute.semantic === VertexAttributeSemantic.ROTATION;
  967. // In the glTF 2.0 spec, min and max are not affected by the normalized flag.
  968. // However, for KHR_mesh_quantization, min and max must be dequantized for
  969. // normalized values, else the bounding sphere will be computed incorrectly.
  970. const hasKhrMeshQuantization = gltf.extensionsRequired?.includes(
  971. "KHR_mesh_quantization",
  972. );
  973. if (hasKhrMeshQuantization && normalized && isQuantizable) {
  974. dequantizeMinMax(attribute, MathType);
  975. }
  976. return attribute;
  977. }
  978. function getSetIndex(gltfSemantic) {
  979. const setIndexRegex = /^\w+_(\d+)$/;
  980. const setIndexMatch = setIndexRegex.exec(gltfSemantic);
  981. if (setIndexMatch !== null) {
  982. return parseInt(setIndexMatch[1]);
  983. }
  984. return undefined;
  985. }
  986. const scratchSemanticInfo = {
  987. gltfSemantic: undefined,
  988. renamedSemantic: undefined,
  989. modelSemantic: undefined,
  990. };
  991. function getSemanticInfo(loader, semanticType, gltfSemantic) {
  992. // For .b3dm, rename _BATCHID (or the legacy BATCHID) to _FEATURE_ID_0
  993. // in the generated model components for compatibility with EXT_mesh_features
  994. let renamedSemantic = gltfSemantic;
  995. if (
  996. loader._renameBatchIdSemantic &&
  997. (gltfSemantic === "_BATCHID" || gltfSemantic === "BATCHID")
  998. ) {
  999. renamedSemantic = "_FEATURE_ID_0";
  1000. }
  1001. const modelSemantic = semanticType.fromGltfSemantic(renamedSemantic);
  1002. const semanticInfo = scratchSemanticInfo;
  1003. semanticInfo.gltfSemantic = gltfSemantic;
  1004. semanticInfo.renamedSemantic = renamedSemantic;
  1005. semanticInfo.modelSemantic = modelSemantic;
  1006. return semanticInfo;
  1007. }
  1008. function isClassificationAttribute(attributeSemantic) {
  1009. // Classification models only use the position, texcoord, and feature ID attributes.
  1010. const isPositionAttribute =
  1011. attributeSemantic === VertexAttributeSemantic.POSITION;
  1012. const isFeatureIdAttribute =
  1013. attributeSemantic === VertexAttributeSemantic.FEATURE_ID;
  1014. const isTexcoordAttribute =
  1015. attributeSemantic === VertexAttributeSemantic.TEXCOORD;
  1016. return isPositionAttribute || isFeatureIdAttribute || isTexcoordAttribute;
  1017. }
  1018. function finalizeDracoAttribute(
  1019. attribute,
  1020. vertexBufferLoader,
  1021. loadBuffer,
  1022. loadTypedArray,
  1023. ) {
  1024. // The accessor's byteOffset and byteStride should be ignored for draco.
  1025. // Each attribute is tightly packed in its own buffer after decode.
  1026. attribute.byteOffset = 0;
  1027. attribute.byteStride = undefined;
  1028. attribute.quantization = vertexBufferLoader.quantization;
  1029. if (loadBuffer) {
  1030. attribute.buffer = vertexBufferLoader.buffer;
  1031. }
  1032. if (loadTypedArray) {
  1033. const componentDatatype = defined(vertexBufferLoader.quantization)
  1034. ? vertexBufferLoader.quantization.componentDatatype
  1035. : attribute.componentDatatype;
  1036. attribute.typedArray = ComponentDatatype.createArrayBufferView(
  1037. componentDatatype,
  1038. vertexBufferLoader.typedArray.buffer,
  1039. );
  1040. }
  1041. }
  1042. function finalizeSpzAttribute(
  1043. attribute,
  1044. vertexBufferLoader,
  1045. loadBuffer,
  1046. loadTypedArray,
  1047. ) {
  1048. attribute.byteOffset = 0;
  1049. attribute.byteStride = undefined;
  1050. if (loadBuffer) {
  1051. attribute.buffer = vertexBufferLoader.buffer;
  1052. }
  1053. if (loadTypedArray && defined(vertexBufferLoader.typedArray)) {
  1054. attribute.typedArray = ComponentDatatype.createArrayBufferView(
  1055. attribute.componentDatatype,
  1056. vertexBufferLoader.typedArray.buffer,
  1057. );
  1058. }
  1059. if (attribute.semantic === VertexAttributeSemantic.POSITION) {
  1060. const findMinMaxXY = (flatArray) => {
  1061. let minX = Infinity;
  1062. let maxX = -Infinity;
  1063. let minY = Infinity;
  1064. let maxY = -Infinity;
  1065. let minZ = Infinity;
  1066. let maxZ = -Infinity;
  1067. for (let i = 0; i < flatArray.length; i += 3) {
  1068. const x = flatArray[i];
  1069. const y = flatArray[i + 1];
  1070. const z = flatArray[i + 2];
  1071. minX = Math.min(minX, x);
  1072. maxX = Math.max(maxX, x);
  1073. minY = Math.min(minY, y);
  1074. maxY = Math.max(maxY, y);
  1075. minZ = Math.min(minZ, z);
  1076. maxZ = Math.max(maxZ, z);
  1077. }
  1078. return [
  1079. new Cartesian3(minX, minY, minZ),
  1080. new Cartesian3(maxX, maxY, maxZ),
  1081. ];
  1082. };
  1083. const buffer = attribute.typedArray;
  1084. [attribute.min, attribute.max] = findMinMaxXY(buffer);
  1085. }
  1086. }
  1087. function finalizeAttribute(
  1088. gltf,
  1089. accessor,
  1090. attribute,
  1091. vertexBufferLoader,
  1092. loadBuffer,
  1093. loadTypedArray,
  1094. ) {
  1095. if (loadBuffer) {
  1096. attribute.buffer = vertexBufferLoader.buffer;
  1097. }
  1098. if (loadTypedArray) {
  1099. const bufferViewTypedArray = vertexBufferLoader.typedArray;
  1100. attribute.typedArray = getPackedTypedArray(
  1101. gltf,
  1102. accessor,
  1103. bufferViewTypedArray,
  1104. );
  1105. if (!loadBuffer) {
  1106. // If the buffer isn't loaded, then the accessor's byteOffset and
  1107. // byteStride should be ignored, since values are only available in a
  1108. // tightly packed typed array
  1109. attribute.byteOffset = 0;
  1110. attribute.byteStride = undefined;
  1111. }
  1112. }
  1113. }
  1114. function loadAttribute(
  1115. loader,
  1116. accessorId,
  1117. semanticInfo,
  1118. primitive,
  1119. draco,
  1120. spz,
  1121. loadBuffer,
  1122. loadTypedArray,
  1123. frameState,
  1124. ) {
  1125. const gltf = loader.gltfJson;
  1126. const accessor = gltf.accessors[accessorId];
  1127. const bufferViewId = accessor.bufferView;
  1128. const gltfSemantic = semanticInfo.gltfSemantic;
  1129. const renamedSemantic = semanticInfo.renamedSemantic;
  1130. const modelSemantic = semanticInfo.modelSemantic;
  1131. const setIndex = defined(modelSemantic)
  1132. ? getSetIndex(renamedSemantic)
  1133. : undefined;
  1134. const name = gltfSemantic;
  1135. const attribute = createAttribute(
  1136. gltf,
  1137. accessorId,
  1138. name,
  1139. modelSemantic,
  1140. setIndex,
  1141. );
  1142. if (!defined(draco) && !defined(bufferViewId) && !defined(spz)) {
  1143. return attribute;
  1144. }
  1145. const vertexBufferLoader = getVertexBufferLoader(
  1146. loader,
  1147. accessorId,
  1148. gltfSemantic,
  1149. primitive,
  1150. draco,
  1151. spz,
  1152. loadBuffer,
  1153. loadTypedArray,
  1154. frameState,
  1155. );
  1156. const index = loader._geometryLoaders.length;
  1157. loader._geometryLoaders.push(vertexBufferLoader);
  1158. const promise = vertexBufferLoader.load();
  1159. loader._loaderPromises.push(promise);
  1160. // This can only execute once vertexBufferLoader.process() has run and returns true
  1161. // Save this finish callback by the loader index so it can be called
  1162. // in process().
  1163. loader._geometryCallbacks[index] = () => {
  1164. if (
  1165. defined(draco) &&
  1166. defined(draco.attributes) &&
  1167. defined(draco.attributes[gltfSemantic])
  1168. ) {
  1169. finalizeDracoAttribute(
  1170. attribute,
  1171. vertexBufferLoader,
  1172. loadBuffer,
  1173. loadTypedArray,
  1174. );
  1175. } else if (defined(spz)) {
  1176. finalizeSpzAttribute(
  1177. attribute,
  1178. vertexBufferLoader,
  1179. loadBuffer,
  1180. loadTypedArray,
  1181. );
  1182. } else {
  1183. finalizeAttribute(
  1184. gltf,
  1185. accessor,
  1186. attribute,
  1187. vertexBufferLoader,
  1188. loadBuffer,
  1189. loadTypedArray,
  1190. );
  1191. }
  1192. };
  1193. return attribute;
  1194. }
  1195. function loadVertexAttribute(
  1196. loader,
  1197. accessorId,
  1198. semanticInfo,
  1199. primitive,
  1200. draco,
  1201. spz,
  1202. hasInstances,
  1203. needsPostProcessing,
  1204. frameState,
  1205. ) {
  1206. const modelSemantic = semanticInfo.modelSemantic;
  1207. const isPositionAttribute =
  1208. modelSemantic === VertexAttributeSemantic.POSITION;
  1209. const isFeatureIdAttribute =
  1210. modelSemantic === VertexAttributeSemantic.FEATURE_ID;
  1211. const loadTypedArrayFor2D =
  1212. isPositionAttribute &&
  1213. !hasInstances &&
  1214. loader._loadAttributesFor2D &&
  1215. !frameState.scene3DOnly;
  1216. const loadTypedArrayForPicking =
  1217. isPositionAttribute && loader._enablePick && !frameState.context.webgl2;
  1218. const loadTypedArrayForClassification =
  1219. loader._loadForClassification && isFeatureIdAttribute;
  1220. // Whether the final output should be a buffer or typed array
  1221. // after loading and post-processing.
  1222. const outputTypedArrayOnly = loader._loadAttributesAsTypedArray;
  1223. const outputBuffer = !outputTypedArrayOnly;
  1224. const outputTypedArray =
  1225. outputTypedArrayOnly ||
  1226. loadTypedArrayFor2D ||
  1227. loadTypedArrayForPicking ||
  1228. loadTypedArrayForClassification;
  1229. // Determine what to load right now:
  1230. //
  1231. // - If post-processing is needed, load a packed typed array for
  1232. // further processing, and defer the buffer loading until later.
  1233. // - On the other hand, if post-processing is not needed,
  1234. // set the load flags directly
  1235. const loadBuffer = needsPostProcessing ? false : outputBuffer;
  1236. const loadTypedArray = needsPostProcessing ? true : outputTypedArray;
  1237. const attribute = loadAttribute(
  1238. loader,
  1239. accessorId,
  1240. semanticInfo,
  1241. primitive,
  1242. draco,
  1243. spz,
  1244. loadBuffer,
  1245. loadTypedArray,
  1246. frameState,
  1247. );
  1248. const attributePlan = new PrimitiveLoadPlan.AttributeLoadPlan(attribute);
  1249. attributePlan.loadBuffer = outputBuffer;
  1250. attributePlan.loadTypedArray = outputTypedArray;
  1251. return attributePlan;
  1252. }
  1253. function loadInstancedAttribute(
  1254. loader,
  1255. accessorId,
  1256. attributes,
  1257. gltfSemantic,
  1258. frameState,
  1259. ) {
  1260. const accessors = loader.gltfJson.accessors;
  1261. const hasRotation = defined(attributes.ROTATION);
  1262. const hasTranslationMinMax =
  1263. defined(attributes.TRANSLATION) &&
  1264. defined(accessors[attributes.TRANSLATION].min) &&
  1265. defined(accessors[attributes.TRANSLATION].max);
  1266. const semanticInfo = getSemanticInfo(
  1267. loader,
  1268. InstanceAttributeSemantic,
  1269. gltfSemantic,
  1270. );
  1271. const modelSemantic = semanticInfo.modelSemantic;
  1272. const isTransformAttribute =
  1273. modelSemantic === InstanceAttributeSemantic.TRANSLATION ||
  1274. modelSemantic === InstanceAttributeSemantic.ROTATION ||
  1275. modelSemantic === InstanceAttributeSemantic.SCALE;
  1276. const isTranslationAttribute =
  1277. modelSemantic === InstanceAttributeSemantic.TRANSLATION;
  1278. // Load the attributes as typed arrays only if:
  1279. // - loadAttributesAsTypedArray is true
  1280. // - the instances have rotations. This only applies to the transform attributes,
  1281. // since The instance matrices are computed on the CPU. This avoids the
  1282. // expensive quaternion -> rotation matrix conversion in the shader.
  1283. // - GPU instancing is not supported.
  1284. const loadAsTypedArrayOnly =
  1285. loader._loadAttributesAsTypedArray ||
  1286. (hasRotation && isTransformAttribute) ||
  1287. !frameState.context.instancedArrays;
  1288. const loadTypedArrayForPicking =
  1289. loader._enablePick && !frameState.context.webgl2;
  1290. const loadBuffer = !loadAsTypedArrayOnly;
  1291. // Load the translations as a typed array in addition to the buffer if
  1292. // - the accessor does not have a min and max. The values will be used
  1293. // for computing an accurate bounding volume.
  1294. // - the model will be projected to 2D.
  1295. const loadFor2D = loader._loadAttributesFor2D && !frameState.scene3DOnly;
  1296. const loadTranslationAsTypedArray =
  1297. isTranslationAttribute &&
  1298. (!hasTranslationMinMax || loadFor2D || loadTypedArrayForPicking);
  1299. const loadTypedArray = loadAsTypedArrayOnly || loadTranslationAsTypedArray;
  1300. // Don't pass in primitive or draco object since instanced attributes can't be draco compressed
  1301. return loadAttribute(
  1302. loader,
  1303. accessorId,
  1304. semanticInfo,
  1305. undefined,
  1306. undefined,
  1307. undefined,
  1308. loadBuffer,
  1309. loadTypedArray,
  1310. frameState,
  1311. );
  1312. }
  1313. function loadIndices(
  1314. loader,
  1315. accessorId,
  1316. primitive,
  1317. hasFeatureIds,
  1318. needsPostProcessing,
  1319. frameState,
  1320. ) {
  1321. const accessor = loader.gltfJson.accessors[accessorId];
  1322. const bufferViewId = accessor.bufferView;
  1323. // Infer compression / extensions directly from the glTF primitive instead of passing in flags
  1324. const extensions = primitive.extensions ?? Frozen.EMPTY_OBJECT;
  1325. const draco = extensions.KHR_draco_mesh_compression;
  1326. const hasEdgeVisibility = defined(
  1327. extensions.EXT_mesh_primitive_edge_visibility,
  1328. );
  1329. if (!defined(draco) && !defined(bufferViewId)) {
  1330. return undefined;
  1331. }
  1332. const indices = new Indices();
  1333. indices.count = accessor.count;
  1334. const loadAttributesAsTypedArray = loader._loadAttributesAsTypedArray;
  1335. // Load the index buffer as a typed array to generate wireframes or pick in WebGL1.
  1336. const loadForCpuOperations =
  1337. (loader._loadIndicesForWireframe || loader._enablePick) &&
  1338. !frameState.context.webgl2;
  1339. // Load the index buffer as a typed array to batch features together for classification.
  1340. const loadForClassification = loader._loadForClassification && hasFeatureIds;
  1341. // Whether the final output should be a buffer or typed array
  1342. // after loading and post-processing.
  1343. const outputTypedArrayOnly = loadAttributesAsTypedArray;
  1344. const outputBuffer = !outputTypedArrayOnly;
  1345. const outputTypedArray =
  1346. loadAttributesAsTypedArray ||
  1347. loadForCpuOperations ||
  1348. loadForClassification ||
  1349. hasEdgeVisibility;
  1350. // Determine what to load right now:
  1351. //
  1352. // - If post-processing is needed, load a packed typed array for
  1353. // further processing, and defer the buffer loading until later.
  1354. // - On the other hand, if post-processing is not needed, set the load
  1355. // flags directly
  1356. const loadBuffer = needsPostProcessing ? false : outputBuffer;
  1357. const loadTypedArray = needsPostProcessing ? true : outputTypedArray;
  1358. const indexBufferLoader = getIndexBufferLoader(
  1359. loader,
  1360. accessorId,
  1361. primitive,
  1362. draco,
  1363. loadBuffer,
  1364. loadTypedArray,
  1365. frameState,
  1366. );
  1367. const index = loader._geometryLoaders.length;
  1368. loader._geometryLoaders.push(indexBufferLoader);
  1369. const promise = indexBufferLoader.load();
  1370. loader._loaderPromises.push(promise);
  1371. // This can only execute once indexBufferLoader.process() has run and returns true
  1372. // Save this finish callback by the loader index so it can be called
  1373. // in process().
  1374. loader._geometryCallbacks[index] = () => {
  1375. indices.indexDatatype = indexBufferLoader.indexDatatype;
  1376. indices.buffer = indexBufferLoader.buffer;
  1377. indices.typedArray = indexBufferLoader.typedArray;
  1378. };
  1379. const indicesPlan = new PrimitiveLoadPlan.IndicesLoadPlan(indices);
  1380. indicesPlan.loadBuffer = outputBuffer;
  1381. indicesPlan.loadTypedArray = outputTypedArray;
  1382. return indicesPlan;
  1383. }
  1384. function loadTexture(loader, textureInfo, frameState, samplerOverride) {
  1385. const gltf = loader.gltfJson;
  1386. const imageId = GltfLoaderUtil.getImageIdFromTexture({
  1387. gltf: gltf,
  1388. textureId: textureInfo.index,
  1389. supportedImageFormats: loader._supportedImageFormats,
  1390. });
  1391. if (!defined(imageId)) {
  1392. return undefined;
  1393. }
  1394. const textureLoader = ResourceCache.getTextureLoader({
  1395. gltf: gltf,
  1396. textureInfo: textureInfo,
  1397. gltfResource: loader._gltfResource,
  1398. baseResource: loader._baseResource,
  1399. supportedImageFormats: loader._supportedImageFormats,
  1400. frameState: frameState,
  1401. asynchronous: loader._asynchronous,
  1402. });
  1403. const textureReader = GltfLoaderUtil.createModelTextureReader({
  1404. textureInfo: textureInfo,
  1405. });
  1406. const index = loader._textureLoaders.length;
  1407. loader._textureLoaders.push(textureLoader);
  1408. const promise = textureLoader.load().catch((error) => {
  1409. if (loader.isDestroyed()) {
  1410. return;
  1411. }
  1412. if (!loader._incrementallyLoadTextures) {
  1413. // If incrementallyLoadTextures is false, throw the error to ensure the loader state
  1414. // immediately is set to have failed
  1415. throw error;
  1416. }
  1417. // Otherwise, save the error so it can be thrown next
  1418. loader._textureState = GltfLoaderState.FAILED;
  1419. loader._textureErrors.push(error);
  1420. });
  1421. loader._texturesPromises.push(promise);
  1422. // This can only execute once textureLoader.process() has run and returns true
  1423. // Save this finish callback by the loader index so it can be called
  1424. // in process().
  1425. loader._textureCallbacks[index] = () => {
  1426. textureReader.texture = textureLoader.texture;
  1427. if (defined(samplerOverride)) {
  1428. textureReader.texture.sampler = samplerOverride;
  1429. }
  1430. };
  1431. return textureReader;
  1432. }
  1433. /**
  1434. * Load textures and parse factors for the KHR_materials_pbrSpecularGlossiness extension
  1435. * @param {GltfLoader} loader
  1436. * @param {object} specularGlossinessInfo The contents of the KHR_materials_pbrSpecularGlossiness extension in the parsed glTF JSON
  1437. * @param {FrameState} frameState
  1438. * @returns {ModelComponents.SpecularGlossiness}
  1439. * @private
  1440. */
  1441. function loadSpecularGlossiness(loader, specularGlossinessInfo, frameState) {
  1442. const {
  1443. diffuseTexture,
  1444. specularGlossinessTexture,
  1445. diffuseFactor,
  1446. specularFactor,
  1447. glossinessFactor,
  1448. } = specularGlossinessInfo;
  1449. const specularGlossiness = new SpecularGlossiness();
  1450. if (defined(diffuseTexture)) {
  1451. specularGlossiness.diffuseTexture = loadTexture(
  1452. loader,
  1453. diffuseTexture,
  1454. frameState,
  1455. );
  1456. }
  1457. if (defined(specularGlossinessTexture)) {
  1458. specularGlossiness.specularGlossinessTexture = loadTexture(
  1459. loader,
  1460. specularGlossinessTexture,
  1461. frameState,
  1462. );
  1463. }
  1464. specularGlossiness.diffuseFactor = fromArray(Cartesian4, diffuseFactor);
  1465. specularGlossiness.specularFactor = fromArray(Cartesian3, specularFactor);
  1466. specularGlossiness.glossinessFactor = glossinessFactor;
  1467. return specularGlossiness;
  1468. }
  1469. /**
  1470. * Load textures and parse factors for a metallic-roughness PBR model in a glTF material
  1471. * @param {GltfLoader} loader
  1472. * @param {object} metallicRoughnessInfo The contents of a pbrMetallicRoughness property in the parsed glTF JSON
  1473. * @param {FrameState} frameState
  1474. * @returns {ModelComponents.MetallicRoughness}
  1475. * @private
  1476. */
  1477. function loadMetallicRoughness(loader, metallicRoughnessInfo, frameState) {
  1478. const {
  1479. baseColorTexture,
  1480. metallicRoughnessTexture,
  1481. baseColorFactor,
  1482. metallicFactor,
  1483. roughnessFactor,
  1484. } = metallicRoughnessInfo;
  1485. const metallicRoughness = new MetallicRoughness();
  1486. if (defined(baseColorTexture)) {
  1487. metallicRoughness.baseColorTexture = loadTexture(
  1488. loader,
  1489. baseColorTexture,
  1490. frameState,
  1491. );
  1492. }
  1493. if (defined(metallicRoughnessTexture)) {
  1494. metallicRoughness.metallicRoughnessTexture = loadTexture(
  1495. loader,
  1496. metallicRoughnessTexture,
  1497. frameState,
  1498. );
  1499. }
  1500. metallicRoughness.baseColorFactor = fromArray(Cartesian4, baseColorFactor);
  1501. metallicRoughness.metallicFactor = metallicFactor;
  1502. metallicRoughness.roughnessFactor = roughnessFactor;
  1503. return metallicRoughness;
  1504. }
  1505. function loadSpecular(loader, specularInfo, frameState) {
  1506. const {
  1507. specularFactor,
  1508. specularTexture,
  1509. specularColorFactor,
  1510. specularColorTexture,
  1511. } = specularInfo;
  1512. const specular = new Specular();
  1513. if (defined(specularTexture)) {
  1514. specular.specularTexture = loadTexture(loader, specularTexture, frameState);
  1515. }
  1516. if (defined(specularColorTexture)) {
  1517. specular.specularColorTexture = loadTexture(
  1518. loader,
  1519. specularColorTexture,
  1520. frameState,
  1521. );
  1522. }
  1523. specular.specularFactor = specularFactor;
  1524. specular.specularColorFactor = fromArray(Cartesian3, specularColorFactor);
  1525. return specular;
  1526. }
  1527. function loadAnisotropy(loader, anisotropyInfo, frameState) {
  1528. const {
  1529. anisotropyStrength = Anisotropy.DEFAULT_ANISOTROPY_STRENGTH,
  1530. anisotropyRotation = Anisotropy.DEFAULT_ANISOTROPY_ROTATION,
  1531. anisotropyTexture,
  1532. } = anisotropyInfo;
  1533. const anisotropy = new Anisotropy();
  1534. if (defined(anisotropyTexture)) {
  1535. anisotropy.anisotropyTexture = loadTexture(
  1536. loader,
  1537. anisotropyTexture,
  1538. frameState,
  1539. );
  1540. }
  1541. anisotropy.anisotropyStrength = anisotropyStrength;
  1542. anisotropy.anisotropyRotation = anisotropyRotation;
  1543. return anisotropy;
  1544. }
  1545. function loadClearcoat(loader, clearcoatInfo, frameState) {
  1546. const {
  1547. clearcoatFactor = Clearcoat.DEFAULT_CLEARCOAT_FACTOR,
  1548. clearcoatTexture,
  1549. clearcoatRoughnessFactor = Clearcoat.DEFAULT_CLEARCOAT_ROUGHNESS_FACTOR,
  1550. clearcoatRoughnessTexture,
  1551. clearcoatNormalTexture,
  1552. } = clearcoatInfo;
  1553. const clearcoat = new Clearcoat();
  1554. if (defined(clearcoatTexture)) {
  1555. clearcoat.clearcoatTexture = loadTexture(
  1556. loader,
  1557. clearcoatTexture,
  1558. frameState,
  1559. );
  1560. }
  1561. if (defined(clearcoatRoughnessTexture)) {
  1562. clearcoat.clearcoatRoughnessTexture = loadTexture(
  1563. loader,
  1564. clearcoatRoughnessTexture,
  1565. frameState,
  1566. );
  1567. }
  1568. if (defined(clearcoatNormalTexture)) {
  1569. clearcoat.clearcoatNormalTexture = loadTexture(
  1570. loader,
  1571. clearcoatNormalTexture,
  1572. frameState,
  1573. );
  1574. }
  1575. clearcoat.clearcoatFactor = clearcoatFactor;
  1576. clearcoat.clearcoatRoughnessFactor = clearcoatRoughnessFactor;
  1577. return clearcoat;
  1578. }
  1579. function loadLineStyle(lineStyleInfo) {
  1580. if (!defined(lineStyleInfo)) {
  1581. return undefined;
  1582. }
  1583. const lineStyle = new LineStyle();
  1584. if (defined(lineStyleInfo.width)) {
  1585. const width = lineStyleInfo.width;
  1586. if (width > 0 && Math.floor(width) === width) {
  1587. lineStyle.width = width;
  1588. }
  1589. }
  1590. if (defined(lineStyleInfo.pattern)) {
  1591. const pattern = lineStyleInfo.pattern;
  1592. if (pattern >= 0 && pattern <= 65535 && Math.floor(pattern) === pattern) {
  1593. lineStyle.pattern = pattern;
  1594. }
  1595. }
  1596. if (!defined(lineStyle.width) && !defined(lineStyle.pattern)) {
  1597. return undefined;
  1598. }
  1599. return lineStyle;
  1600. }
  1601. /**
  1602. * Load textures and parse factors and flags for a glTF material
  1603. *
  1604. * @param {GltfLoader} loader
  1605. * @param {object} gltfMaterial An entry from the <code>.materials</code> array in the glTF JSON
  1606. * @param {FrameState} frameState
  1607. * @returns {ModelComponents.Material}
  1608. * @private
  1609. */
  1610. function loadMaterial(loader, gltfMaterial, frameState) {
  1611. const material = new Material();
  1612. const extensions = gltfMaterial.extensions ?? Frozen.EMPTY_OBJECT;
  1613. const pbrSpecularGlossiness = extensions.KHR_materials_pbrSpecularGlossiness;
  1614. const pbrSpecular = extensions.KHR_materials_specular;
  1615. const pbrAnisotropy = extensions.KHR_materials_anisotropy;
  1616. const pbrClearcoat = extensions.KHR_materials_clearcoat;
  1617. const pbrMetallicRoughness = gltfMaterial.pbrMetallicRoughness;
  1618. material.unlit = defined(extensions.KHR_materials_unlit);
  1619. if (defined(pbrSpecularGlossiness)) {
  1620. material.specularGlossiness = loadSpecularGlossiness(
  1621. loader,
  1622. pbrSpecularGlossiness,
  1623. frameState,
  1624. );
  1625. } else {
  1626. if (defined(pbrMetallicRoughness)) {
  1627. material.metallicRoughness = loadMetallicRoughness(
  1628. loader,
  1629. pbrMetallicRoughness,
  1630. frameState,
  1631. );
  1632. }
  1633. if (defined(pbrSpecular) && !material.unlit) {
  1634. material.specular = loadSpecular(loader, pbrSpecular, frameState);
  1635. }
  1636. if (defined(pbrAnisotropy) && !material.unlit) {
  1637. material.anisotropy = loadAnisotropy(loader, pbrAnisotropy, frameState);
  1638. }
  1639. if (defined(pbrClearcoat) && !material.unlit) {
  1640. material.clearcoat = loadClearcoat(loader, pbrClearcoat, frameState);
  1641. }
  1642. }
  1643. // Top level textures
  1644. if (defined(gltfMaterial.emissiveTexture)) {
  1645. material.emissiveTexture = loadTexture(
  1646. loader,
  1647. gltfMaterial.emissiveTexture,
  1648. frameState,
  1649. );
  1650. }
  1651. // Normals aren't used for classification, so don't load the normal texture.
  1652. if (defined(gltfMaterial.normalTexture) && !loader._loadForClassification) {
  1653. material.normalTexture = loadTexture(
  1654. loader,
  1655. gltfMaterial.normalTexture,
  1656. frameState,
  1657. );
  1658. }
  1659. if (defined(gltfMaterial.occlusionTexture)) {
  1660. material.occlusionTexture = loadTexture(
  1661. loader,
  1662. gltfMaterial.occlusionTexture,
  1663. frameState,
  1664. );
  1665. }
  1666. material.emissiveFactor = fromArray(Cartesian3, gltfMaterial.emissiveFactor);
  1667. material.alphaMode = gltfMaterial.alphaMode;
  1668. material.alphaCutoff = gltfMaterial.alphaCutoff;
  1669. material.doubleSided = gltfMaterial.doubleSided;
  1670. // BENTLEY_materials_point_style extension
  1671. const pointStyleExtension = extensions.BENTLEY_materials_point_style;
  1672. if (defined(pointStyleExtension) && defined(pointStyleExtension.diameter)) {
  1673. const diameter = pointStyleExtension.diameter;
  1674. // The spec requires a positive integer, but we floor non-integers as a best effort.
  1675. if (diameter >= 1) {
  1676. material.pointDiameter = Math.floor(diameter);
  1677. }
  1678. }
  1679. material.lineStyle = loadLineStyle(extensions.BENTLEY_materials_line_style);
  1680. return material;
  1681. }
  1682. // for EXT_mesh_features
  1683. function loadFeatureIdAttribute(featureIds, positionalLabel) {
  1684. const featureIdAttribute = new FeatureIdAttribute();
  1685. featureIdAttribute.featureCount = featureIds.featureCount;
  1686. featureIdAttribute.nullFeatureId = featureIds.nullFeatureId;
  1687. featureIdAttribute.propertyTableId = featureIds.propertyTable;
  1688. featureIdAttribute.setIndex = featureIds.attribute;
  1689. featureIdAttribute.label = featureIds.label;
  1690. featureIdAttribute.positionalLabel = positionalLabel;
  1691. return featureIdAttribute;
  1692. }
  1693. // for backwards compatibility with EXT_feature_metadata
  1694. function loadFeatureIdAttributeLegacy(
  1695. gltfFeatureIdAttribute,
  1696. featureTableId,
  1697. featureCount,
  1698. positionalLabel,
  1699. ) {
  1700. const featureIdAttribute = new FeatureIdAttribute();
  1701. const featureIds = gltfFeatureIdAttribute.featureIds;
  1702. featureIdAttribute.featureCount = featureCount;
  1703. featureIdAttribute.propertyTableId = featureTableId;
  1704. featureIdAttribute.setIndex = getSetIndex(featureIds.attribute);
  1705. featureIdAttribute.positionalLabel = positionalLabel;
  1706. return featureIdAttribute;
  1707. }
  1708. // implicit ranges do not exist in EXT_mesh_features and EXT_instance_features,
  1709. // but both default to the vertex/instance ID which is like
  1710. // an implicit range of {offset: 0, repeat: 1}
  1711. function loadDefaultFeatureIds(featureIds, positionalLabel) {
  1712. const featureIdRange = new FeatureIdImplicitRange();
  1713. featureIdRange.propertyTableId = featureIds.propertyTable;
  1714. featureIdRange.featureCount = featureIds.featureCount;
  1715. featureIdRange.nullFeatureId = featureIds.nullFeatureId;
  1716. featureIdRange.label = featureIds.label;
  1717. featureIdRange.positionalLabel = positionalLabel;
  1718. featureIdRange.offset = 0;
  1719. featureIdRange.repeat = 1;
  1720. return featureIdRange;
  1721. }
  1722. // for backwards compatibility with EXT_feature_metadata
  1723. function loadFeatureIdImplicitRangeLegacy(
  1724. gltfFeatureIdAttribute,
  1725. featureTableId,
  1726. featureCount,
  1727. positionalLabel,
  1728. ) {
  1729. const featureIdRange = new FeatureIdImplicitRange();
  1730. const featureIds = gltfFeatureIdAttribute.featureIds;
  1731. featureIdRange.propertyTableId = featureTableId;
  1732. featureIdRange.featureCount = featureCount;
  1733. // constant/divisor was renamed to offset/repeat
  1734. featureIdRange.offset = featureIds.constant ?? 0;
  1735. // The default is now undefined
  1736. const divisor = featureIds.divisor ?? 0;
  1737. featureIdRange.repeat = divisor === 0 ? undefined : divisor;
  1738. featureIdRange.positionalLabel = positionalLabel;
  1739. return featureIdRange;
  1740. }
  1741. // for EXT_mesh_features
  1742. function loadFeatureIdTexture(
  1743. loader,
  1744. gltfFeatureIdTexture,
  1745. frameState,
  1746. positionalLabel,
  1747. ) {
  1748. const featureIdTexture = new FeatureIdTexture();
  1749. featureIdTexture.featureCount = gltfFeatureIdTexture.featureCount;
  1750. featureIdTexture.nullFeatureId = gltfFeatureIdTexture.nullFeatureId;
  1751. featureIdTexture.propertyTableId = gltfFeatureIdTexture.propertyTable;
  1752. featureIdTexture.label = gltfFeatureIdTexture.label;
  1753. featureIdTexture.positionalLabel = positionalLabel;
  1754. const textureInfo = gltfFeatureIdTexture.texture;
  1755. featureIdTexture.textureReader = loadTexture(
  1756. loader,
  1757. textureInfo,
  1758. frameState,
  1759. Sampler.NEAREST, // Feature ID textures require nearest sampling
  1760. );
  1761. // Though the new channel index is more future-proof, this implementation
  1762. // only supports RGBA textures. At least for now, the string representation
  1763. // is more useful for generating shader code.
  1764. const channels = defined(textureInfo.channels) ? textureInfo.channels : [0];
  1765. const channelString = channels
  1766. .map(function (channelIndex) {
  1767. return "rgba".charAt(channelIndex);
  1768. })
  1769. .join("");
  1770. featureIdTexture.textureReader.channels = channelString;
  1771. return featureIdTexture;
  1772. }
  1773. // for backwards compatibility with EXT_feature_metadata
  1774. function loadFeatureIdTextureLegacy(
  1775. loader,
  1776. gltfFeatureIdTexture,
  1777. featureTableId,
  1778. frameState,
  1779. featureCount,
  1780. positionalLabel,
  1781. ) {
  1782. const featureIdTexture = new FeatureIdTexture();
  1783. const featureIds = gltfFeatureIdTexture.featureIds;
  1784. const textureInfo = featureIds.texture;
  1785. featureIdTexture.featureCount = featureCount;
  1786. featureIdTexture.propertyTableId = featureTableId;
  1787. featureIdTexture.textureReader = loadTexture(
  1788. loader,
  1789. textureInfo,
  1790. frameState,
  1791. Sampler.NEAREST, // Feature ID textures require nearest sampling
  1792. );
  1793. featureIdTexture.textureReader.channels = featureIds.channels;
  1794. featureIdTexture.positionalLabel = positionalLabel;
  1795. return featureIdTexture;
  1796. }
  1797. function loadMorphTarget(
  1798. loader,
  1799. target,
  1800. needsPostProcessing,
  1801. primitiveLoadPlan,
  1802. frameState,
  1803. ) {
  1804. const morphTarget = new MorphTarget();
  1805. // Don't pass in primitive or draco object since morph targets can't be draco compressed
  1806. const primitive = undefined;
  1807. const draco = undefined;
  1808. const spz = undefined;
  1809. const hasInstances = false;
  1810. for (const semantic in target) {
  1811. if (!target.hasOwnProperty(semantic)) {
  1812. continue;
  1813. }
  1814. const accessorId = target[semantic];
  1815. const semanticInfo = getSemanticInfo(
  1816. loader,
  1817. VertexAttributeSemantic,
  1818. semantic,
  1819. );
  1820. const attributePlan = loadVertexAttribute(
  1821. loader,
  1822. accessorId,
  1823. semanticInfo,
  1824. primitive,
  1825. draco,
  1826. spz,
  1827. hasInstances,
  1828. needsPostProcessing,
  1829. frameState,
  1830. );
  1831. morphTarget.attributes.push(attributePlan.attribute);
  1832. // The load plan doesn't need to distinguish morph target attributes from
  1833. // regular attributes
  1834. primitiveLoadPlan.attributePlans.push(attributePlan);
  1835. }
  1836. return morphTarget;
  1837. }
  1838. function fetchSpzExtensionFrom(extensions) {
  1839. const gaussianSplatting = extensions?.KHR_gaussian_splatting;
  1840. const gsExtensions = gaussianSplatting?.extensions;
  1841. const spz = gsExtensions?.KHR_gaussian_splatting_compression_spz_2;
  1842. if (defined(spz)) {
  1843. return spz;
  1844. }
  1845. return undefined;
  1846. }
  1847. function getEdgeVisibilityMaterialColor(loader, materialIndex) {
  1848. if (!defined(materialIndex)) {
  1849. return undefined;
  1850. }
  1851. const materials = loader.gltfJson.materials;
  1852. if (
  1853. !defined(materials) ||
  1854. materialIndex < 0 ||
  1855. materialIndex >= materials.length
  1856. ) {
  1857. return undefined;
  1858. }
  1859. const material = materials[materialIndex];
  1860. if (!defined(material)) {
  1861. return undefined;
  1862. }
  1863. const metallicRoughness =
  1864. material.pbrMetallicRoughness ?? Frozen.EMPTY_OBJECT;
  1865. const color = fromArray(Cartesian4, metallicRoughness.baseColorFactor);
  1866. if (defined(color)) {
  1867. return color;
  1868. }
  1869. return new Cartesian4(1.0, 1.0, 1.0, 1.0);
  1870. }
  1871. function getLineStringPrimitiveRestartValue(componentType) {
  1872. switch (componentType) {
  1873. case ComponentDatatype.UNSIGNED_BYTE:
  1874. return 255;
  1875. case ComponentDatatype.UNSIGNED_SHORT:
  1876. return 65535;
  1877. case ComponentDatatype.UNSIGNED_INT:
  1878. return 4294967295;
  1879. default:
  1880. throw new RuntimeError(
  1881. "EXT_mesh_primitive_edge_visibility line strings indices must use unsigned scalar component types.",
  1882. );
  1883. }
  1884. }
  1885. function loadEdgeVisibilityLineStrings(
  1886. loader,
  1887. lineStringDefinitions,
  1888. defaultMaterialIndex,
  1889. ) {
  1890. if (!defined(lineStringDefinitions) || lineStringDefinitions.length === 0) {
  1891. return undefined;
  1892. }
  1893. const result = new Array(lineStringDefinitions.length);
  1894. for (let i = 0; i < lineStringDefinitions.length; i++) {
  1895. const definition = lineStringDefinitions[i] ?? Frozen.EMPTY_OBJECT;
  1896. const accessorId = definition.indices;
  1897. const accessor = loader.gltfJson.accessors[accessorId];
  1898. if (!defined(accessor)) {
  1899. throw new RuntimeError("Edge visibility line string accessor not found!");
  1900. }
  1901. const indices = loadAccessor(loader, accessor);
  1902. const restartIndex = getLineStringPrimitiveRestartValue(
  1903. accessor.componentType,
  1904. );
  1905. const materialIndex = defined(definition.material)
  1906. ? definition.material
  1907. : defaultMaterialIndex;
  1908. result[i] = {
  1909. indices: indices,
  1910. restartIndex: restartIndex,
  1911. componentType: accessor.componentType,
  1912. materialColor: getEdgeVisibilityMaterialColor(loader, materialIndex),
  1913. };
  1914. }
  1915. return result;
  1916. }
  1917. function loadEdgeVisibility(loader, edgeVisibilityExtension) {
  1918. if (!defined(edgeVisibilityExtension)) {
  1919. return undefined;
  1920. }
  1921. const edgeVisibility = {};
  1922. const visibilityAccessorId = edgeVisibilityExtension.visibility;
  1923. if (defined(visibilityAccessorId)) {
  1924. const visibilityAccessor = loader.gltfJson.accessors[visibilityAccessorId];
  1925. if (!defined(visibilityAccessor)) {
  1926. throw new RuntimeError("Edge visibility accessor not found!");
  1927. }
  1928. edgeVisibility.visibility = loadAccessor(loader, visibilityAccessor);
  1929. }
  1930. edgeVisibility.materialColor = getEdgeVisibilityMaterialColor(
  1931. loader,
  1932. edgeVisibilityExtension.material,
  1933. );
  1934. if (defined(edgeVisibilityExtension.silhouetteNormals)) {
  1935. const silhouetteNormalsAccessor =
  1936. loader.gltfJson.accessors[edgeVisibilityExtension.silhouetteNormals];
  1937. if (defined(silhouetteNormalsAccessor)) {
  1938. edgeVisibility.silhouetteNormals = loadAccessor(
  1939. loader,
  1940. silhouetteNormalsAccessor,
  1941. );
  1942. }
  1943. }
  1944. if (defined(edgeVisibilityExtension.lineStrings)) {
  1945. edgeVisibility.lineStrings = loadEdgeVisibilityLineStrings(
  1946. loader,
  1947. edgeVisibilityExtension.lineStrings,
  1948. edgeVisibilityExtension.material,
  1949. );
  1950. }
  1951. return edgeVisibility;
  1952. }
  1953. /**
  1954. * Load resources associated with a mesh primitive for a glTF node
  1955. * @param {GltfLoader} loader
  1956. * @param {object} gltfPrimitive One of the primitives in a mesh
  1957. * @param {boolean} hasInstances True if the node using this mesh has instances
  1958. * @param {FrameState} frameState
  1959. * @returns {ModelComponents.Primitive}
  1960. * @private
  1961. */
  1962. function loadPrimitive(loader, gltfPrimitive, hasInstances, frameState) {
  1963. const primitive = new Primitive();
  1964. const primitivePlan = new PrimitiveLoadPlan(primitive);
  1965. loader._primitiveLoadPlans.push(primitivePlan);
  1966. const materialId = gltfPrimitive.material;
  1967. if (defined(materialId)) {
  1968. primitive.material = loadMaterial(
  1969. loader,
  1970. loader.gltfJson.materials[materialId],
  1971. frameState,
  1972. );
  1973. }
  1974. const extensions = gltfPrimitive.extensions ?? Frozen.EMPTY_OBJECT;
  1975. const polygonExtension = extensions.EXT_mesh_polygon;
  1976. if (defined(polygonExtension)) {
  1977. primitive.polygon = loadMeshPolygonExtension(
  1978. loader,
  1979. gltfPrimitive,
  1980. polygonExtension,
  1981. );
  1982. }
  1983. // @deprecated CESIUM_mesh_vector to be removed after v1.142 release.
  1984. const meshVectorExtension = extensions.CESIUM_mesh_vector;
  1985. if (defined(meshVectorExtension)) {
  1986. primitive.vector = loadMeshVectorExtension(loader, meshVectorExtension);
  1987. }
  1988. let needsPostProcessing = false;
  1989. const outlineExtension = extensions.CESIUM_primitive_outline;
  1990. if (loader._loadPrimitiveOutline && defined(outlineExtension)) {
  1991. needsPostProcessing = true;
  1992. primitivePlan.needsOutlines = true;
  1993. primitivePlan.outlineIndices = loadPrimitiveOutline(
  1994. loader,
  1995. outlineExtension,
  1996. primitivePlan,
  1997. );
  1998. }
  1999. primitive.edgeVisibility = loadEdgeVisibility(
  2000. loader,
  2001. extensions.EXT_mesh_primitive_edge_visibility,
  2002. );
  2003. //support the latest glTF spec and the legacy extension
  2004. const spzExtension = fetchSpzExtensionFrom(extensions);
  2005. if (defined(spzExtension)) {
  2006. needsPostProcessing = true;
  2007. primitivePlan.needsGaussianSplats = true;
  2008. }
  2009. const loadForClassification = loader._loadForClassification;
  2010. const draco = extensions.KHR_draco_mesh_compression;
  2011. let hasFeatureIds = false;
  2012. const attributes = gltfPrimitive.attributes;
  2013. if (defined(attributes)) {
  2014. for (const semantic in attributes) {
  2015. if (!attributes.hasOwnProperty(semantic)) {
  2016. continue;
  2017. }
  2018. const accessorId = attributes[semantic];
  2019. const semanticInfo = getSemanticInfo(
  2020. loader,
  2021. VertexAttributeSemantic,
  2022. semantic,
  2023. );
  2024. const modelSemantic = semanticInfo.modelSemantic;
  2025. if (loadForClassification && !isClassificationAttribute(modelSemantic)) {
  2026. continue;
  2027. }
  2028. if (modelSemantic === VertexAttributeSemantic.FEATURE_ID) {
  2029. hasFeatureIds = true;
  2030. }
  2031. const attributePlan = loadVertexAttribute(
  2032. loader,
  2033. accessorId,
  2034. semanticInfo,
  2035. gltfPrimitive,
  2036. draco,
  2037. spzExtension,
  2038. hasInstances,
  2039. needsPostProcessing,
  2040. frameState,
  2041. );
  2042. primitivePlan.attributePlans.push(attributePlan);
  2043. primitive.attributes.push(attributePlan.attribute);
  2044. }
  2045. }
  2046. const targets = gltfPrimitive.targets;
  2047. // Morph targets are disabled for classification models.
  2048. if (defined(targets) && !loadForClassification) {
  2049. for (let i = 0; i < targets.length; ++i) {
  2050. primitive.morphTargets.push(
  2051. loadMorphTarget(
  2052. loader,
  2053. targets[i],
  2054. needsPostProcessing,
  2055. primitivePlan,
  2056. frameState,
  2057. ),
  2058. );
  2059. }
  2060. }
  2061. const indices = gltfPrimitive.indices;
  2062. if (defined(indices)) {
  2063. const indicesPlan = loadIndices(
  2064. loader,
  2065. indices,
  2066. gltfPrimitive,
  2067. hasFeatureIds,
  2068. needsPostProcessing,
  2069. frameState,
  2070. );
  2071. if (defined(indicesPlan)) {
  2072. primitivePlan.indicesPlan = indicesPlan;
  2073. primitive.indices = indicesPlan.indices;
  2074. }
  2075. }
  2076. // With the latest revision, feature IDs are defined in EXT_mesh_features
  2077. // while EXT_structural_metadata is for defining property textures and
  2078. // property mappings. In the legacy EXT_feature_metadata, these concepts
  2079. // were all in one extension.
  2080. const structuralMetadata = extensions.EXT_structural_metadata;
  2081. const meshFeatures = extensions.EXT_mesh_features;
  2082. const featureMetadataLegacy = extensions.EXT_feature_metadata;
  2083. const hasFeatureMetadataLegacy = defined(featureMetadataLegacy);
  2084. const gpmLocal = extensions.NGA_gpm_local;
  2085. // Load feature Ids
  2086. if (defined(meshFeatures)) {
  2087. loadPrimitiveFeatures(loader, primitive, meshFeatures, frameState);
  2088. } else if (hasFeatureMetadataLegacy) {
  2089. loadPrimitiveFeaturesLegacy(
  2090. loader,
  2091. primitive,
  2092. featureMetadataLegacy,
  2093. frameState,
  2094. );
  2095. }
  2096. // Load structural metadata
  2097. if (defined(structuralMetadata)) {
  2098. loadPrimitiveMetadata(primitive, structuralMetadata);
  2099. } else if (hasFeatureMetadataLegacy) {
  2100. loadPrimitiveMetadataLegacy(loader, primitive, featureMetadataLegacy);
  2101. }
  2102. if (defined(gpmLocal)) {
  2103. loadGpmExtension(primitive, gpmLocal);
  2104. }
  2105. const primitiveType = gltfPrimitive.mode;
  2106. if (loadForClassification && primitiveType !== PrimitiveType.TRIANGLES) {
  2107. throw new RuntimeError(
  2108. "Only triangle meshes can be used for classification.",
  2109. );
  2110. }
  2111. primitive.primitiveType = primitiveType;
  2112. return primitive;
  2113. }
  2114. function loadPrimitiveOutline(loader, outlineExtension) {
  2115. const accessorId = outlineExtension.indices;
  2116. const accessor = loader.gltfJson.accessors[accessorId];
  2117. const useQuaternion = false;
  2118. return loadAccessor(loader, accessor, useQuaternion);
  2119. }
  2120. /**
  2121. * @typedef {object} EXTMeshPolygonExtension
  2122. * @property {number} count
  2123. * @property {number} indicesOffsets
  2124. * @property {number} [loopIndices]
  2125. * @property {number} [loopIndicesOffsets]
  2126. * @property {number} [triangleIndices]
  2127. * @property {number} [triangleIndicesOffsets]
  2128. */
  2129. /**
  2130. * Load EXT_mesh_polygon.
  2131. * @param {GltfLoader} loader
  2132. * @param {object} gltfPrimitive
  2133. * @param {EXTMeshPolygonExtension} polygonExtension
  2134. * @returns {ModelComponents.Polygon}
  2135. * @ignore
  2136. */
  2137. function loadMeshPolygonExtension(loader, gltfPrimitive, polygonExtension) {
  2138. const result = new Polygon();
  2139. const accessors = loader.gltfJson.accessors;
  2140. result.count = polygonExtension.count;
  2141. // See ModelComponents.Polygon definition.
  2142. if (gltfPrimitive.mode === PrimitiveType.LINE_LOOP) {
  2143. result.loopIndices = loadAccessorTypedArray(
  2144. loader,
  2145. accessors[gltfPrimitive.indices],
  2146. );
  2147. result.loopIndicesOffsets = loadAccessorTypedArray(
  2148. loader,
  2149. accessors[polygonExtension.indicesOffsets],
  2150. );
  2151. result.triangleIndices = loadAccessorTypedArray(
  2152. loader,
  2153. accessors[polygonExtension.triangleIndices],
  2154. );
  2155. result.triangleIndicesOffsets = loadAccessorTypedArray(
  2156. loader,
  2157. accessors[polygonExtension.triangleIndicesOffsets],
  2158. );
  2159. } else if (gltfPrimitive.mode === PrimitiveType.TRIANGLES) {
  2160. result.loopIndices = loadAccessorTypedArray(
  2161. loader,
  2162. accessors[polygonExtension.loopIndices],
  2163. );
  2164. result.loopIndicesOffsets = loadAccessorTypedArray(
  2165. loader,
  2166. accessors[polygonExtension.loopIndicesOffsets],
  2167. );
  2168. result.triangleIndices = loadAccessorTypedArray(
  2169. loader,
  2170. accessors[gltfPrimitive.indices],
  2171. );
  2172. result.triangleIndicesOffsets = loadAccessorTypedArray(
  2173. loader,
  2174. accessors[polygonExtension.indicesOffsets],
  2175. );
  2176. }
  2177. return result;
  2178. }
  2179. /**
  2180. * Load CESIUM_mesh_vector.
  2181. * @param {GltfLoader} loader
  2182. * @param {*} meshVectorExtension
  2183. * @returns {ModelComponents.Vector}
  2184. * @ignore
  2185. * @deprecated
  2186. */
  2187. function loadMeshVectorExtension(loader, meshVectorExtension) {
  2188. if (!defined(meshVectorExtension)) {
  2189. return undefined;
  2190. }
  2191. const result = new Vector();
  2192. result.vector = meshVectorExtension.vector;
  2193. result.count = meshVectorExtension.count;
  2194. const accessors = loader.gltfJson.accessors;
  2195. function loadVectorAccessor(accessorId, name) {
  2196. if (!defined(accessorId)) {
  2197. return undefined;
  2198. }
  2199. return loadAccessor(loader, accessors[accessorId]);
  2200. }
  2201. result.polygonAttributeOffsets = loadVectorAccessor(
  2202. meshVectorExtension.polygonAttributeOffsets,
  2203. "polygonAttributeOffsets",
  2204. );
  2205. result.polygonHoleCounts = loadVectorAccessor(
  2206. meshVectorExtension.polygonHoleCounts,
  2207. "polygonHoleCounts",
  2208. );
  2209. result.polygonHoleOffsets = loadVectorAccessor(
  2210. meshVectorExtension.polygonHoleOffsets,
  2211. "polygonHoleOffsets",
  2212. );
  2213. result.polygonIndicesOffsets = loadVectorAccessor(
  2214. meshVectorExtension.polygonIndicesOffsets,
  2215. "polygonIndicesOffsets",
  2216. );
  2217. return result;
  2218. }
  2219. // For EXT_mesh_features
  2220. function loadPrimitiveFeatures(
  2221. loader,
  2222. primitive,
  2223. meshFeaturesExtension,
  2224. frameState,
  2225. ) {
  2226. let featureIdsArray;
  2227. if (
  2228. defined(meshFeaturesExtension) &&
  2229. defined(meshFeaturesExtension.featureIds)
  2230. ) {
  2231. featureIdsArray = meshFeaturesExtension.featureIds;
  2232. } else {
  2233. featureIdsArray = [];
  2234. }
  2235. for (let i = 0; i < featureIdsArray.length; i++) {
  2236. const featureIds = featureIdsArray[i];
  2237. const label = `featureId_${i}`;
  2238. let featureIdComponent;
  2239. if (defined(featureIds.texture)) {
  2240. featureIdComponent = loadFeatureIdTexture(
  2241. loader,
  2242. featureIds,
  2243. frameState,
  2244. label,
  2245. );
  2246. } else if (defined(featureIds.attribute)) {
  2247. featureIdComponent = loadFeatureIdAttribute(featureIds, label);
  2248. } else {
  2249. // default to vertex ID, in other words an implicit range with
  2250. // offset: 0, repeat: 1
  2251. featureIdComponent = loadDefaultFeatureIds(featureIds, label);
  2252. }
  2253. primitive.featureIds.push(featureIdComponent);
  2254. }
  2255. }
  2256. // For EXT_feature_metadata
  2257. function loadPrimitiveFeaturesLegacy(
  2258. loader,
  2259. primitive,
  2260. metadataExtension,
  2261. frameState,
  2262. ) {
  2263. // For looking up the featureCount for each set of feature IDs
  2264. const { featureTables } = loader.gltfJson.extensions.EXT_feature_metadata;
  2265. let nextFeatureIdIndex = 0;
  2266. // Feature ID Attributes
  2267. const featureIdAttributes = metadataExtension.featureIdAttributes;
  2268. if (defined(featureIdAttributes)) {
  2269. for (let i = 0; i < featureIdAttributes.length; ++i) {
  2270. const featureIdAttribute = featureIdAttributes[i];
  2271. const featureTableId = featureIdAttribute.featureTable;
  2272. const propertyTableId =
  2273. loader._sortedPropertyTableIds.indexOf(featureTableId);
  2274. const featureCount = featureTables[featureTableId].count;
  2275. const label = `featureId_${nextFeatureIdIndex}`;
  2276. nextFeatureIdIndex++;
  2277. let featureIdComponent;
  2278. if (defined(featureIdAttribute.featureIds.attribute)) {
  2279. featureIdComponent = loadFeatureIdAttributeLegacy(
  2280. featureIdAttribute,
  2281. propertyTableId,
  2282. featureCount,
  2283. label,
  2284. );
  2285. } else {
  2286. featureIdComponent = loadFeatureIdImplicitRangeLegacy(
  2287. featureIdAttribute,
  2288. propertyTableId,
  2289. featureCount,
  2290. label,
  2291. );
  2292. }
  2293. primitive.featureIds.push(featureIdComponent);
  2294. }
  2295. }
  2296. // Feature ID Textures
  2297. const featureIdTextures = metadataExtension.featureIdTextures;
  2298. if (defined(featureIdTextures)) {
  2299. for (let i = 0; i < featureIdTextures.length; ++i) {
  2300. const featureIdTexture = featureIdTextures[i];
  2301. const featureTableId = featureIdTexture.featureTable;
  2302. const propertyTableId =
  2303. loader._sortedPropertyTableIds.indexOf(featureTableId);
  2304. const featureCount = featureTables[featureTableId].count;
  2305. const featureIdLabel = `featureId_${nextFeatureIdIndex}`;
  2306. nextFeatureIdIndex++;
  2307. const featureIdComponent = loadFeatureIdTextureLegacy(
  2308. loader,
  2309. featureIdTexture,
  2310. propertyTableId,
  2311. frameState,
  2312. featureCount,
  2313. featureIdLabel,
  2314. );
  2315. // Feature ID textures are added after feature ID attributes in the list
  2316. primitive.featureIds.push(featureIdComponent);
  2317. }
  2318. }
  2319. }
  2320. // For primitive-level EXT_structural_metadata
  2321. function loadPrimitiveMetadata(primitive, structuralMetadataExtension) {
  2322. if (!defined(structuralMetadataExtension)) {
  2323. return;
  2324. }
  2325. const { propertyTextures, propertyAttributes } = structuralMetadataExtension;
  2326. if (defined(propertyTextures)) {
  2327. primitive.propertyTextureIds = propertyTextures;
  2328. }
  2329. if (defined(propertyAttributes)) {
  2330. primitive.propertyAttributeIds = propertyAttributes;
  2331. }
  2332. }
  2333. // For EXT_feature_metadata
  2334. function loadPrimitiveMetadataLegacy(loader, primitive, metadataExtension) {
  2335. // Feature Textures
  2336. if (defined(metadataExtension.featureTextures)) {
  2337. // feature textures are now identified by an integer index. To convert the
  2338. // string IDs to integers, find their place in the sorted list of feature
  2339. // table names
  2340. primitive.propertyTextureIds = metadataExtension.featureTextures.map(
  2341. function (id) {
  2342. return loader._sortedFeatureTextureIds.indexOf(id);
  2343. },
  2344. );
  2345. }
  2346. }
  2347. function loadGpmExtension(primitive, gpmLocal) {
  2348. const ppeTextures = gpmLocal.ppeTextures ?? [];
  2349. for (let i = 0; i < ppeTextures.length; i++) {
  2350. primitive.propertyTextureIds.push(i);
  2351. }
  2352. }
  2353. function loadInstances(loader, nodeExtensions, frameState) {
  2354. const instancingExtension = nodeExtensions.EXT_mesh_gpu_instancing;
  2355. const instances = new Instances();
  2356. const attributes = instancingExtension.attributes;
  2357. if (defined(attributes)) {
  2358. for (const semantic in attributes) {
  2359. if (!attributes.hasOwnProperty(semantic)) {
  2360. continue;
  2361. }
  2362. const accessorId = attributes[semantic];
  2363. instances.attributes.push(
  2364. loadInstancedAttribute(
  2365. loader,
  2366. accessorId,
  2367. attributes,
  2368. semantic,
  2369. frameState,
  2370. ),
  2371. );
  2372. }
  2373. }
  2374. const instancingExtExtensions =
  2375. instancingExtension.extensions ?? Frozen.EMPTY_OBJECT;
  2376. const instanceFeatures = nodeExtensions.EXT_instance_features;
  2377. const featureMetadataLegacy = instancingExtExtensions.EXT_feature_metadata;
  2378. if (defined(instanceFeatures)) {
  2379. loadInstanceFeatures(instances, instanceFeatures);
  2380. } else if (defined(featureMetadataLegacy)) {
  2381. loadInstanceFeaturesLegacy(
  2382. loader.gltfJson,
  2383. instances,
  2384. featureMetadataLegacy,
  2385. loader._sortedPropertyTableIds,
  2386. );
  2387. }
  2388. return instances;
  2389. }
  2390. // For EXT_mesh_features
  2391. function loadInstanceFeatures(instances, instanceFeaturesExtension) {
  2392. // feature IDs are required in EXT_instance_features
  2393. const featureIdsArray = instanceFeaturesExtension.featureIds;
  2394. for (let i = 0; i < featureIdsArray.length; i++) {
  2395. const featureIds = featureIdsArray[i];
  2396. const label = `instanceFeatureId_${i}`;
  2397. let featureIdComponent;
  2398. if (defined(featureIds.attribute)) {
  2399. featureIdComponent = loadFeatureIdAttribute(featureIds, label);
  2400. } else {
  2401. // in EXT_instance_features, the default is to assign IDs by instance
  2402. // ID. This can be expressed with offset: 0, repeat: 1
  2403. featureIdComponent = loadDefaultFeatureIds(featureIds, label);
  2404. }
  2405. instances.featureIds.push(featureIdComponent);
  2406. }
  2407. }
  2408. // For backwards-compatibility with EXT_feature_metadata
  2409. function loadInstanceFeaturesLegacy(
  2410. gltf,
  2411. instances,
  2412. metadataExtension,
  2413. sortedPropertyTableIds,
  2414. ) {
  2415. // For looking up the featureCount for each set of feature IDs
  2416. const featureTables = gltf.extensions.EXT_feature_metadata.featureTables;
  2417. const featureIdAttributes = metadataExtension.featureIdAttributes;
  2418. if (defined(featureIdAttributes)) {
  2419. for (let i = 0; i < featureIdAttributes.length; ++i) {
  2420. const featureIdAttribute = featureIdAttributes[i];
  2421. const featureTableId = featureIdAttribute.featureTable;
  2422. const propertyTableId = sortedPropertyTableIds.indexOf(featureTableId);
  2423. const featureCount = featureTables[featureTableId].count;
  2424. const label = `instanceFeatureId_${i}`;
  2425. let featureIdComponent;
  2426. if (defined(featureIdAttribute.featureIds.attribute)) {
  2427. featureIdComponent = loadFeatureIdAttributeLegacy(
  2428. featureIdAttribute,
  2429. propertyTableId,
  2430. featureCount,
  2431. label,
  2432. );
  2433. } else {
  2434. featureIdComponent = loadFeatureIdImplicitRangeLegacy(
  2435. featureIdAttribute,
  2436. propertyTableId,
  2437. featureCount,
  2438. label,
  2439. );
  2440. }
  2441. instances.featureIds.push(featureIdComponent);
  2442. }
  2443. }
  2444. }
  2445. /**
  2446. * Load resources associated with one node from a glTF JSON
  2447. * @param {GltfLoader} loader
  2448. * @param {object} gltfNode An entry from the <code>.nodes</code> array in the glTF JSON
  2449. * @param {FrameState} frameState
  2450. * @returns {ModelComponents.Node}
  2451. * @private
  2452. */
  2453. function loadNode(loader, gltfNode, frameState) {
  2454. const node = new Node();
  2455. node.name = gltfNode.name;
  2456. node.matrix = fromArray(Matrix4, gltfNode.matrix);
  2457. node.translation = fromArray(Cartesian3, gltfNode.translation);
  2458. node.rotation = fromArray(Quaternion, gltfNode.rotation);
  2459. node.scale = fromArray(Cartesian3, gltfNode.scale);
  2460. const nodeExtensions = gltfNode.extensions ?? Frozen.EMPTY_OBJECT;
  2461. const instancingExtension = nodeExtensions.EXT_mesh_gpu_instancing;
  2462. const articulationsExtension = nodeExtensions.AGI_articulations;
  2463. const meshVectorExtension = nodeExtensions.CESIUM_mesh_vector;
  2464. if (defined(instancingExtension)) {
  2465. if (loader._loadForClassification) {
  2466. throw new RuntimeError(
  2467. "Models with the EXT_mesh_gpu_instancing extension cannot be used for classification.",
  2468. );
  2469. }
  2470. node.instances = loadInstances(loader, nodeExtensions, frameState);
  2471. }
  2472. if (defined(articulationsExtension)) {
  2473. node.articulationName = articulationsExtension.articulationName;
  2474. }
  2475. if (defined(meshVectorExtension)) {
  2476. node.meshVector = meshVectorExtension;
  2477. }
  2478. const meshId = gltfNode.mesh;
  2479. if (defined(meshId)) {
  2480. const mesh = loader.gltfJson.meshes[meshId];
  2481. const primitives = getMeshPrimitives(mesh);
  2482. for (let i = 0; i < primitives.length; ++i) {
  2483. node.primitives.push(
  2484. loadPrimitive(
  2485. loader,
  2486. primitives[i],
  2487. defined(node.instances),
  2488. frameState,
  2489. ),
  2490. );
  2491. }
  2492. // If the node has no weights array, it will look for the weights array provided
  2493. // by the mesh. If both are undefined, it will default to an array of zero weights.
  2494. const morphWeights = gltfNode.weights ?? mesh.weights;
  2495. const targets = node.primitives[0].morphTargets;
  2496. // Since meshes are not stored as separate components, the mesh weights will still
  2497. // be stored at the node level.
  2498. node.morphWeights = defined(morphWeights)
  2499. ? morphWeights.slice()
  2500. : new Array(targets.length).fill(0.0);
  2501. }
  2502. return node;
  2503. }
  2504. /**
  2505. * Load resources associated with the nodes in a glTF JSON
  2506. * @param {GltfLoader} loader
  2507. * @param {FrameState} frameState
  2508. * @returns {ModelComponents.Node[]}
  2509. * @private
  2510. */
  2511. function loadNodes(loader, frameState) {
  2512. const nodeJsons = loader.gltfJson.nodes;
  2513. if (!defined(nodeJsons)) {
  2514. return [];
  2515. }
  2516. const loadedNodes = nodeJsons.map(function (nodeJson, i) {
  2517. const node = loadNode(loader, nodeJson, frameState);
  2518. node.index = i;
  2519. return node;
  2520. });
  2521. for (let i = 0; i < loadedNodes.length; ++i) {
  2522. const childrenNodeIds = nodeJsons[i].children;
  2523. if (defined(childrenNodeIds)) {
  2524. for (let j = 0; j < childrenNodeIds.length; ++j) {
  2525. loadedNodes[i].children.push(loadedNodes[childrenNodeIds[j]]);
  2526. }
  2527. }
  2528. }
  2529. return loadedNodes;
  2530. }
  2531. function loadSkin(loader, gltfSkin, nodes) {
  2532. const skin = new Skin();
  2533. const jointIds = gltfSkin.joints;
  2534. skin.joints = jointIds.map((jointId) => nodes[jointId]);
  2535. const inverseBindMatricesAccessorId = gltfSkin.inverseBindMatrices;
  2536. if (defined(inverseBindMatricesAccessorId)) {
  2537. const accessor = loader.gltfJson.accessors[inverseBindMatricesAccessorId];
  2538. skin.inverseBindMatrices = loadAccessor(loader, accessor);
  2539. } else {
  2540. skin.inverseBindMatrices = new Array(jointIds.length).fill(
  2541. Matrix4.IDENTITY,
  2542. );
  2543. }
  2544. return skin;
  2545. }
  2546. function loadSkins(loader, nodes) {
  2547. const skinJsons = loader.gltfJson.skins;
  2548. // Skins are disabled for classification models.
  2549. if (loader._loadForClassification || !defined(skinJsons)) {
  2550. return [];
  2551. }
  2552. const loadedSkins = skinJsons.map(function (skinJson, i) {
  2553. const skin = loadSkin(loader, skinJson, nodes);
  2554. skin.index = i;
  2555. return skin;
  2556. });
  2557. const nodeJsons = loader.gltfJson.nodes;
  2558. for (let i = 0; i < nodes.length; ++i) {
  2559. const skinId = nodeJsons[i].skin;
  2560. if (defined(skinId)) {
  2561. nodes[i].skin = loadedSkins[skinId];
  2562. }
  2563. }
  2564. return loadedSkins;
  2565. }
  2566. async function loadStructuralMetadata(
  2567. loader,
  2568. extension,
  2569. extensionLegacy,
  2570. frameState,
  2571. ) {
  2572. const structuralMetadataLoader = new GltfStructuralMetadataLoader({
  2573. gltf: loader.gltfJson,
  2574. extension: extension,
  2575. extensionLegacy: extensionLegacy,
  2576. gltfResource: loader._gltfResource,
  2577. baseResource: loader._baseResource,
  2578. supportedImageFormats: loader._supportedImageFormats,
  2579. frameState: frameState,
  2580. asynchronous: loader._asynchronous,
  2581. });
  2582. loader._structuralMetadataLoader = structuralMetadataLoader;
  2583. return structuralMetadataLoader.load();
  2584. }
  2585. async function loadMeshPrimitiveGpm(loader, gltf, extension, frameState) {
  2586. const meshPrimitiveGpmLoader = new GltfMeshPrimitiveGpmLoader({
  2587. gltf: gltf,
  2588. extension: extension,
  2589. gltfResource: loader._gltfResource,
  2590. baseResource: loader._baseResource,
  2591. supportedImageFormats: loader._supportedImageFormats,
  2592. frameState: frameState,
  2593. asynchronous: loader._asynchronous,
  2594. });
  2595. loader._meshPrimitiveGpmLoader = meshPrimitiveGpmLoader;
  2596. return meshPrimitiveGpmLoader.load();
  2597. }
  2598. function loadAnimationSampler(loader, gltfSampler) {
  2599. const animationSampler = new AnimationSampler();
  2600. const accessors = loader.gltfJson.accessors;
  2601. const inputAccessor = accessors[gltfSampler.input];
  2602. animationSampler.input = loadAccessor(loader, inputAccessor);
  2603. const gltfInterpolation = gltfSampler.interpolation;
  2604. animationSampler.interpolation =
  2605. InterpolationType[gltfInterpolation] ?? InterpolationType.LINEAR;
  2606. const outputAccessor = accessors[gltfSampler.output];
  2607. animationSampler.output = loadAccessor(loader, outputAccessor, true);
  2608. return animationSampler;
  2609. }
  2610. function loadAnimationTarget(gltfTarget, nodes) {
  2611. const animationTarget = new AnimationTarget();
  2612. const nodeIndex = gltfTarget.node;
  2613. // If the node isn't defined, the animation channel should be ignored.
  2614. // It's easiest to signal this by returning undefined.
  2615. if (!defined(nodeIndex)) {
  2616. return undefined;
  2617. }
  2618. animationTarget.node = nodes[nodeIndex];
  2619. const path = gltfTarget.path.toUpperCase();
  2620. animationTarget.path = AnimatedPropertyType[path];
  2621. return animationTarget;
  2622. }
  2623. function loadAnimationChannel(gltfChannel, samplers, nodes) {
  2624. const animationChannel = new AnimationChannel();
  2625. const samplerIndex = gltfChannel.sampler;
  2626. animationChannel.sampler = samplers[samplerIndex];
  2627. animationChannel.target = loadAnimationTarget(gltfChannel.target, nodes);
  2628. return animationChannel;
  2629. }
  2630. function loadAnimation(loader, animationJson, nodes) {
  2631. const animation = new Animation();
  2632. animation.name = animationJson.name;
  2633. const samplers = animationJson.samplers.map(function (samplerJson, i) {
  2634. const sampler = loadAnimationSampler(loader, samplerJson);
  2635. sampler.index = i;
  2636. return sampler;
  2637. });
  2638. const channels = animationJson.channels.map(function (channelJson) {
  2639. return loadAnimationChannel(channelJson, samplers, nodes);
  2640. });
  2641. animation.samplers = samplers;
  2642. animation.channels = channels;
  2643. return animation;
  2644. }
  2645. function loadAnimations(loader, nodes) {
  2646. const animationJsons = loader.gltfJson.animations;
  2647. // Animations are disabled for classification models.
  2648. if (loader._loadForClassification || !defined(animationJsons)) {
  2649. return [];
  2650. }
  2651. const animations = animationJsons.map(function (animationJson, i) {
  2652. const animation = loadAnimation(loader, animationJson, nodes);
  2653. animation.index = i;
  2654. return animation;
  2655. });
  2656. return animations;
  2657. }
  2658. function loadArticulationStage(gltfStage) {
  2659. const stage = new ArticulationStage();
  2660. stage.name = gltfStage.name;
  2661. const type = gltfStage.type.toUpperCase();
  2662. stage.type = ArticulationStageType[type];
  2663. stage.minimumValue = gltfStage.minimumValue;
  2664. stage.maximumValue = gltfStage.maximumValue;
  2665. stage.initialValue = gltfStage.initialValue;
  2666. return stage;
  2667. }
  2668. function loadArticulation(articulationJson) {
  2669. const articulation = new Articulation();
  2670. articulation.name = articulationJson.name;
  2671. articulation.stages = articulationJson.stages.map(loadArticulationStage);
  2672. return articulation;
  2673. }
  2674. function loadArticulations(gltf) {
  2675. const extensions = gltf.extensions ?? Frozen.EMPTY_OBJECT;
  2676. const articulationJsons = extensions.AGI_articulations?.articulations;
  2677. if (!defined(articulationJsons)) {
  2678. return [];
  2679. }
  2680. return articulationJsons.map(loadArticulation);
  2681. }
  2682. function getSceneNodeIds(gltf) {
  2683. let nodesIds;
  2684. if (defined(gltf.scenes) && defined(gltf.scene)) {
  2685. nodesIds = gltf.scenes[gltf.scene].nodes;
  2686. }
  2687. nodesIds = nodesIds ?? gltf.nodes;
  2688. nodesIds = defined(nodesIds) ? nodesIds : [];
  2689. return nodesIds;
  2690. }
  2691. function loadScene(gltf, nodes) {
  2692. const scene = new Scene();
  2693. const sceneNodeIds = getSceneNodeIds(gltf);
  2694. scene.nodes = sceneNodeIds.map(function (sceneNodeId) {
  2695. return nodes[sceneNodeId];
  2696. });
  2697. return scene;
  2698. }
  2699. const scratchCenter = new Cartesian3();
  2700. /**
  2701. * Parse the glTF which populates the loaders arrays. Loading promises will be created, and will
  2702. * resolve once the loaders are ready (i.e. all external resources
  2703. * have been fetched and all GPU resources have been created). Loaders that
  2704. * create GPU resources need to be processed every frame until they become
  2705. * ready since the JobScheduler is not able to execute all jobs in a single
  2706. * frame. Any promise failures are collected, and will be handled synchronously in process().
  2707. * Also note that it's fine to call process before a loader is ready to process or
  2708. * after it has failed; nothing will happen.
  2709. *
  2710. * @param {GltfLoader} loader
  2711. * @param {FrameState} frameState
  2712. * @returns {Promise} A Promise that resolves when all loaders are ready
  2713. * @private
  2714. */
  2715. function parse(loader, frameState) {
  2716. const gltf = loader.gltfJson;
  2717. const extensions = gltf.extensions ?? Frozen.EMPTY_OBJECT;
  2718. const structuralMetadataExtension = extensions.EXT_structural_metadata;
  2719. const featureMetadataExtensionLegacy = extensions.EXT_feature_metadata;
  2720. const cesiumRtcExtension = extensions.CESIUM_RTC;
  2721. if (defined(featureMetadataExtensionLegacy)) {
  2722. // If the old EXT_feature_metadata extension is present, sort the IDs of the
  2723. // feature tables and feature textures so we don't have to do this once
  2724. // per primitive.
  2725. //
  2726. // This must run before loadNodes so these IDs are available when
  2727. // attributes are processed.
  2728. const featureTables = featureMetadataExtensionLegacy.featureTables;
  2729. const featureTextures = featureMetadataExtensionLegacy.featureTextures;
  2730. const allPropertyTableIds = defined(featureTables) ? featureTables : [];
  2731. const allFeatureTextureIds = defined(featureTextures)
  2732. ? featureTextures
  2733. : [];
  2734. loader._sortedPropertyTableIds = Object.keys(allPropertyTableIds).sort();
  2735. loader._sortedFeatureTextureIds = Object.keys(allFeatureTextureIds).sort();
  2736. }
  2737. const nodes = loadNodes(loader, frameState);
  2738. const skins = loadSkins(loader, nodes);
  2739. const animations = loadAnimations(loader, nodes);
  2740. const articulations = loadArticulations(gltf);
  2741. const scene = loadScene(gltf, nodes);
  2742. const components = new Components();
  2743. const asset = new Asset();
  2744. const copyright = gltf.asset.copyright;
  2745. if (defined(copyright)) {
  2746. const credits = copyright.split(";").map(function (string) {
  2747. return new Credit(string.trim());
  2748. });
  2749. asset.credits = credits;
  2750. }
  2751. components.asset = asset;
  2752. components.scene = scene;
  2753. components.nodes = nodes;
  2754. components.skins = skins;
  2755. components.animations = animations;
  2756. components.articulations = articulations;
  2757. components.upAxis = loader._upAxis;
  2758. components.forwardAxis = loader._forwardAxis;
  2759. if (defined(cesiumRtcExtension)) {
  2760. // CESIUM_RTC is almost always WGS84 coordinates so no axis conversion needed
  2761. const center = Cartesian3.fromArray(
  2762. cesiumRtcExtension.center,
  2763. 0,
  2764. scratchCenter,
  2765. );
  2766. components.transform = Matrix4.fromTranslation(
  2767. center,
  2768. components.transform,
  2769. );
  2770. }
  2771. loader._components = components;
  2772. // Load structural metadata (property tables and property textures)
  2773. if (
  2774. defined(structuralMetadataExtension) ||
  2775. defined(featureMetadataExtensionLegacy)
  2776. ) {
  2777. const promise = loadStructuralMetadata(
  2778. loader,
  2779. structuralMetadataExtension,
  2780. featureMetadataExtensionLegacy,
  2781. frameState,
  2782. );
  2783. loader._loaderPromises.push(promise);
  2784. }
  2785. // Load NGA_gpm_local from root object
  2786. const gpmExtension = extensions.NGA_gpm_local;
  2787. if (defined(gpmExtension)) {
  2788. const gltfGpmLocal = GltfGpmLoader.load(gpmExtension);
  2789. loader._components.extensions["NGA_gpm_local"] = gltfGpmLocal;
  2790. }
  2791. // Load NGA_gpm_local from mesh primitives
  2792. const meshes = gltf.meshes;
  2793. if (defined(meshes)) {
  2794. for (const mesh of meshes) {
  2795. const primitives = mesh.primitives;
  2796. if (defined(primitives)) {
  2797. for (const primitive of primitives) {
  2798. const primitiveExtensions = primitive.extensions;
  2799. if (defined(primitiveExtensions)) {
  2800. const meshPrimitiveGpmExtension = primitiveExtensions.NGA_gpm_local;
  2801. if (defined(meshPrimitiveGpmExtension)) {
  2802. const promise = loadMeshPrimitiveGpm(
  2803. loader,
  2804. gltf,
  2805. meshPrimitiveGpmExtension,
  2806. frameState,
  2807. );
  2808. loader._loaderPromises.push(promise);
  2809. }
  2810. }
  2811. }
  2812. }
  2813. }
  2814. }
  2815. // Gather promises and handle any errors
  2816. const readyPromises = [];
  2817. addAllToArray(readyPromises, loader._loaderPromises);
  2818. // When incrementallyLoadTextures is true, the errors are caught and thrown individually
  2819. // since it doesn't affect the overall loader state
  2820. if (!loader._incrementallyLoadTextures) {
  2821. addAllToArray(readyPromises, loader._texturesPromises);
  2822. }
  2823. return Promise.all(readyPromises);
  2824. }
  2825. function unloadTextures(loader) {
  2826. const textureLoaders = loader._textureLoaders;
  2827. for (let i = 0; i < textureLoaders.length; ++i) {
  2828. textureLoaders[i] =
  2829. !textureLoaders[i].isDestroyed() &&
  2830. ResourceCache.unload(textureLoaders[i]);
  2831. }
  2832. loader._textureLoaders.length = 0;
  2833. }
  2834. function unloadBufferViewLoaders(loader) {
  2835. const bufferViewLoaders = loader._bufferViewLoaders;
  2836. for (let i = 0; i < bufferViewLoaders.length; ++i) {
  2837. bufferViewLoaders[i] =
  2838. !bufferViewLoaders[i].isDestroyed() &&
  2839. ResourceCache.unload(bufferViewLoaders[i]);
  2840. }
  2841. loader._bufferViewLoaders.length = 0;
  2842. }
  2843. function unloadGeometry(loader) {
  2844. const geometryLoaders = loader._geometryLoaders;
  2845. for (let i = 0; i < geometryLoaders.length; ++i) {
  2846. geometryLoaders[i] =
  2847. !geometryLoaders[i].isDestroyed() &&
  2848. ResourceCache.unload(geometryLoaders[i]);
  2849. }
  2850. loader._geometryLoaders.length = 0;
  2851. }
  2852. function unloadGeneratedAttributes(loader) {
  2853. const buffers = loader._postProcessBuffers;
  2854. for (let i = 0; i < buffers.length; i++) {
  2855. const buffer = buffers[i];
  2856. if (!buffer.isDestroyed()) {
  2857. buffer.destroy();
  2858. }
  2859. }
  2860. buffers.length = 0;
  2861. }
  2862. function unloadStructuralMetadata(loader) {
  2863. if (
  2864. defined(loader._structuralMetadataLoader) &&
  2865. !loader._structuralMetadataLoader.isDestroyed()
  2866. ) {
  2867. loader._structuralMetadataLoader.destroy();
  2868. loader._structuralMetadataLoader = undefined;
  2869. }
  2870. }
  2871. function unloadMeshPrimitiveGpm(loader) {
  2872. if (
  2873. defined(loader._meshPrimitiveGpmLoader) &&
  2874. !loader._meshPrimitiveGpmLoader.isDestroyed()
  2875. ) {
  2876. loader._meshPrimitiveGpmLoader.destroy();
  2877. loader._meshPrimitiveGpmLoader = undefined;
  2878. }
  2879. }
  2880. export default GltfLoader;