babylon.glTF2FileLoader.js 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. /**
  5. * Mode that determines the coordinate system to use.
  6. */
  7. var GLTFLoaderCoordinateSystemMode;
  8. (function (GLTFLoaderCoordinateSystemMode) {
  9. /**
  10. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  11. */
  12. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
  13. /**
  14. * Sets the useRightHandedSystem flag on the scene.
  15. */
  16. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
  17. })(GLTFLoaderCoordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode || (BABYLON.GLTFLoaderCoordinateSystemMode = {}));
  18. /**
  19. * Mode that determines what animations will start.
  20. */
  21. var GLTFLoaderAnimationStartMode;
  22. (function (GLTFLoaderAnimationStartMode) {
  23. /**
  24. * No animation will start.
  25. */
  26. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
  27. /**
  28. * The first animation will start.
  29. */
  30. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
  31. /**
  32. * All animations will start.
  33. */
  34. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
  35. })(GLTFLoaderAnimationStartMode = BABYLON.GLTFLoaderAnimationStartMode || (BABYLON.GLTFLoaderAnimationStartMode = {}));
  36. /**
  37. * Loader state.
  38. */
  39. var GLTFLoaderState;
  40. (function (GLTFLoaderState) {
  41. /**
  42. * The asset is loading.
  43. */
  44. GLTFLoaderState[GLTFLoaderState["LOADING"] = 0] = "LOADING";
  45. /**
  46. * The asset is ready for rendering.
  47. */
  48. GLTFLoaderState[GLTFLoaderState["READY"] = 1] = "READY";
  49. /**
  50. * The asset is completely loaded.
  51. */
  52. GLTFLoaderState[GLTFLoaderState["COMPLETE"] = 2] = "COMPLETE";
  53. })(GLTFLoaderState = BABYLON.GLTFLoaderState || (BABYLON.GLTFLoaderState = {}));
  54. /**
  55. * File loader for loading glTF files into a scene.
  56. */
  57. var GLTFFileLoader = /** @class */ (function () {
  58. function GLTFFileLoader() {
  59. // #region Common options
  60. /**
  61. * Raised when the asset has been parsed
  62. */
  63. this.onParsedObservable = new BABYLON.Observable();
  64. // #endregion
  65. // #region V2 options
  66. /**
  67. * The coordinate system mode. Defaults to AUTO.
  68. */
  69. this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
  70. /**
  71. * The animation start mode. Defaults to FIRST.
  72. */
  73. this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
  74. /**
  75. * Defines if the loader should compile materials before raising the success callback. Defaults to false.
  76. */
  77. this.compileMaterials = false;
  78. /**
  79. * Defines if the loader should also compile materials with clip planes. Defaults to false.
  80. */
  81. this.useClipPlane = false;
  82. /**
  83. * Defines if the loader should compile shadow generators before raising the success callback. Defaults to false.
  84. */
  85. this.compileShadowGenerators = false;
  86. /**
  87. * Defines if the Alpha blended materials are only applied as coverage.
  88. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
  89. * If true, no extra effects are applied to transparent pixels.
  90. */
  91. this.transparencyAsCoverage = false;
  92. /** @hidden */
  93. this._normalizeAnimationGroupsToBeginAtZero = true;
  94. /**
  95. * Function called before loading a url referenced by the asset.
  96. */
  97. this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
  98. /**
  99. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  100. */
  101. this.onMeshLoadedObservable = new BABYLON.Observable();
  102. /**
  103. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  104. */
  105. this.onTextureLoadedObservable = new BABYLON.Observable();
  106. /**
  107. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  108. */
  109. this.onMaterialLoadedObservable = new BABYLON.Observable();
  110. /**
  111. * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
  112. */
  113. this.onCameraLoadedObservable = new BABYLON.Observable();
  114. /**
  115. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  116. * For assets with LODs, raised when all of the LODs are complete.
  117. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  118. */
  119. this.onCompleteObservable = new BABYLON.Observable();
  120. /**
  121. * Observable raised after the loader is disposed.
  122. */
  123. this.onDisposeObservable = new BABYLON.Observable();
  124. /**
  125. * Observable raised after a loader extension is created.
  126. * Set additional options for a loader extension in this event.
  127. */
  128. this.onExtensionLoadedObservable = new BABYLON.Observable();
  129. // #endregion
  130. this._loader = null;
  131. /**
  132. * Name of the loader ("gltf")
  133. */
  134. this.name = "gltf";
  135. /**
  136. * Supported file extensions of the loader (.gltf, .glb)
  137. */
  138. this.extensions = {
  139. ".gltf": { isBinary: false },
  140. ".glb": { isBinary: true }
  141. };
  142. }
  143. Object.defineProperty(GLTFFileLoader.prototype, "onParsed", {
  144. /**
  145. * Raised when the asset has been parsed
  146. */
  147. set: function (callback) {
  148. if (this._onParsedObserver) {
  149. this.onParsedObservable.remove(this._onParsedObserver);
  150. }
  151. this._onParsedObserver = this.onParsedObservable.add(callback);
  152. },
  153. enumerable: true,
  154. configurable: true
  155. });
  156. Object.defineProperty(GLTFFileLoader.prototype, "onMeshLoaded", {
  157. /**
  158. * Callback raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  159. */
  160. set: function (callback) {
  161. if (this._onMeshLoadedObserver) {
  162. this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver);
  163. }
  164. this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback);
  165. },
  166. enumerable: true,
  167. configurable: true
  168. });
  169. Object.defineProperty(GLTFFileLoader.prototype, "onTextureLoaded", {
  170. /**
  171. * Callback raised when the loader creates a texture after parsing the glTF properties of the texture.
  172. */
  173. set: function (callback) {
  174. if (this._onTextureLoadedObserver) {
  175. this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver);
  176. }
  177. this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback);
  178. },
  179. enumerable: true,
  180. configurable: true
  181. });
  182. Object.defineProperty(GLTFFileLoader.prototype, "onMaterialLoaded", {
  183. /**
  184. * Callback raised when the loader creates a material after parsing the glTF properties of the material.
  185. */
  186. set: function (callback) {
  187. if (this._onMaterialLoadedObserver) {
  188. this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver);
  189. }
  190. this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback);
  191. },
  192. enumerable: true,
  193. configurable: true
  194. });
  195. Object.defineProperty(GLTFFileLoader.prototype, "onCameraLoaded", {
  196. /**
  197. * Callback raised when the loader creates a camera after parsing the glTF properties of the camera.
  198. */
  199. set: function (callback) {
  200. if (this._onCameraLoadedObserver) {
  201. this.onCameraLoadedObservable.remove(this._onCameraLoadedObserver);
  202. }
  203. this._onCameraLoadedObserver = this.onCameraLoadedObservable.add(callback);
  204. },
  205. enumerable: true,
  206. configurable: true
  207. });
  208. Object.defineProperty(GLTFFileLoader.prototype, "onComplete", {
  209. /**
  210. * Callback raised when the asset is completely loaded, immediately before the loader is disposed.
  211. */
  212. set: function (callback) {
  213. if (this._onCompleteObserver) {
  214. this.onCompleteObservable.remove(this._onCompleteObserver);
  215. }
  216. this._onCompleteObserver = this.onCompleteObservable.add(callback);
  217. },
  218. enumerable: true,
  219. configurable: true
  220. });
  221. Object.defineProperty(GLTFFileLoader.prototype, "onDispose", {
  222. /**
  223. * Callback raised after the loader is disposed.
  224. */
  225. set: function (callback) {
  226. if (this._onDisposeObserver) {
  227. this.onDisposeObservable.remove(this._onDisposeObserver);
  228. }
  229. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  230. },
  231. enumerable: true,
  232. configurable: true
  233. });
  234. Object.defineProperty(GLTFFileLoader.prototype, "onExtensionLoaded", {
  235. /**
  236. * Callback raised after a loader extension is created.
  237. */
  238. set: function (callback) {
  239. if (this._onExtensionLoadedObserver) {
  240. this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver);
  241. }
  242. this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback);
  243. },
  244. enumerable: true,
  245. configurable: true
  246. });
  247. /**
  248. * Returns a promise that resolves when the asset is completely loaded.
  249. * @returns a promise that resolves when the asset is completely loaded.
  250. */
  251. GLTFFileLoader.prototype.whenCompleteAsync = function () {
  252. var _this = this;
  253. return new Promise(function (resolve) {
  254. _this.onCompleteObservable.addOnce(function () {
  255. resolve();
  256. });
  257. });
  258. };
  259. Object.defineProperty(GLTFFileLoader.prototype, "loaderState", {
  260. /**
  261. * The loader state or null if the loader is not active.
  262. */
  263. get: function () {
  264. return this._loader ? this._loader.state : null;
  265. },
  266. enumerable: true,
  267. configurable: true
  268. });
  269. /**
  270. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  271. */
  272. GLTFFileLoader.prototype.dispose = function () {
  273. if (this._loader) {
  274. this._loader.dispose();
  275. this._loader = null;
  276. }
  277. this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
  278. this.onMeshLoadedObservable.clear();
  279. this.onTextureLoadedObservable.clear();
  280. this.onMaterialLoadedObservable.clear();
  281. this.onCameraLoadedObservable.clear();
  282. this.onCompleteObservable.clear();
  283. this.onExtensionLoadedObservable.clear();
  284. this.onDisposeObservable.notifyObservers(this);
  285. this.onDisposeObservable.clear();
  286. };
  287. /**
  288. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  289. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  290. * @param scene the scene the meshes should be added to
  291. * @param data the glTF data to load
  292. * @param rootUrl root url to load from
  293. * @param onProgress event that fires when loading progress has occured
  294. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  295. */
  296. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  297. var _this = this;
  298. return Promise.resolve().then(function () {
  299. var loaderData = _this._parse(data);
  300. _this._loader = _this._getLoader(loaderData);
  301. return _this._loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onProgress);
  302. });
  303. };
  304. /**
  305. * Imports all objects from the loaded glTF data and adds them to the scene
  306. * @param scene the scene the objects should be added to
  307. * @param data the glTF data to load
  308. * @param rootUrl root url to load from
  309. * @param onProgress event that fires when loading progress has occured
  310. * @returns a promise which completes when objects have been loaded to the scene
  311. */
  312. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  313. var _this = this;
  314. return Promise.resolve().then(function () {
  315. var loaderData = _this._parse(data);
  316. _this._loader = _this._getLoader(loaderData);
  317. return _this._loader.loadAsync(scene, loaderData, rootUrl, onProgress);
  318. });
  319. };
  320. /**
  321. * Load into an asset container.
  322. * @param scene The scene to load into
  323. * @param data The data to import
  324. * @param rootUrl The root url for scene and resources
  325. * @param onProgress The callback when the load progresses
  326. * @returns The loaded asset container
  327. */
  328. GLTFFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress) {
  329. var _this = this;
  330. return Promise.resolve().then(function () {
  331. var loaderData = _this._parse(data);
  332. _this._loader = _this._getLoader(loaderData);
  333. return _this._loader.importMeshAsync(null, scene, loaderData, rootUrl, onProgress).then(function (result) {
  334. var container = new BABYLON.AssetContainer(scene);
  335. Array.prototype.push.apply(container.meshes, result.meshes);
  336. Array.prototype.push.apply(container.particleSystems, result.particleSystems);
  337. Array.prototype.push.apply(container.skeletons, result.skeletons);
  338. Array.prototype.push.apply(container.animationGroups, result.animationGroups);
  339. container.removeAllFromScene();
  340. return container;
  341. });
  342. });
  343. };
  344. /**
  345. * If the data string can be loaded directly.
  346. * @param data string contianing the file data
  347. * @returns if the data can be loaded directly
  348. */
  349. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  350. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  351. };
  352. /**
  353. * Instantiates a glTF file loader plugin.
  354. * @returns the created plugin
  355. */
  356. GLTFFileLoader.prototype.createPlugin = function () {
  357. return new GLTFFileLoader();
  358. };
  359. GLTFFileLoader.prototype._parse = function (data) {
  360. var parsedData;
  361. if (data instanceof ArrayBuffer) {
  362. parsedData = GLTFFileLoader._parseBinary(data);
  363. }
  364. else {
  365. parsedData = {
  366. json: JSON.parse(data),
  367. bin: null
  368. };
  369. }
  370. this.onParsedObservable.notifyObservers(parsedData);
  371. this.onParsedObservable.clear();
  372. return parsedData;
  373. };
  374. GLTFFileLoader.prototype._getLoader = function (loaderData) {
  375. var _this = this;
  376. var loaderVersion = { major: 2, minor: 0 };
  377. var asset = loaderData.json.asset || {};
  378. var version = GLTFFileLoader._parseVersion(asset.version);
  379. if (!version) {
  380. throw new Error("Invalid version: " + asset.version);
  381. }
  382. if (asset.minVersion !== undefined) {
  383. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  384. if (!minVersion) {
  385. throw new Error("Invalid minimum version: " + asset.minVersion);
  386. }
  387. if (GLTFFileLoader._compareVersion(minVersion, loaderVersion) > 0) {
  388. throw new Error("Incompatible minimum version: " + asset.minVersion);
  389. }
  390. }
  391. var createLoaders = {
  392. 1: GLTFFileLoader.CreateGLTFLoaderV1,
  393. 2: GLTFFileLoader.CreateGLTFLoaderV2
  394. };
  395. var createLoader = createLoaders[version.major];
  396. if (!createLoader) {
  397. throw new Error("Unsupported version: " + asset.version);
  398. }
  399. var loader = createLoader();
  400. loader.coordinateSystemMode = this.coordinateSystemMode;
  401. loader.animationStartMode = this.animationStartMode;
  402. loader.compileMaterials = this.compileMaterials;
  403. loader.useClipPlane = this.useClipPlane;
  404. loader.compileShadowGenerators = this.compileShadowGenerators;
  405. loader.transparencyAsCoverage = this.transparencyAsCoverage;
  406. loader._normalizeAnimationGroupsToBeginAtZero = this._normalizeAnimationGroupsToBeginAtZero;
  407. loader.preprocessUrlAsync = this.preprocessUrlAsync;
  408. loader.onMeshLoadedObservable.add(function (mesh) { return _this.onMeshLoadedObservable.notifyObservers(mesh); });
  409. loader.onTextureLoadedObservable.add(function (texture) { return _this.onTextureLoadedObservable.notifyObservers(texture); });
  410. loader.onMaterialLoadedObservable.add(function (material) { return _this.onMaterialLoadedObservable.notifyObservers(material); });
  411. loader.onCameraLoadedObservable.add(function (camera) { return _this.onCameraLoadedObservable.notifyObservers(camera); });
  412. loader.onExtensionLoadedObservable.add(function (extension) { return _this.onExtensionLoadedObservable.notifyObservers(extension); });
  413. loader.onCompleteObservable.add(function () {
  414. _this.onMeshLoadedObservable.clear();
  415. _this.onTextureLoadedObservable.clear();
  416. _this.onMaterialLoadedObservable.clear();
  417. _this.onCameraLoadedObservable.clear();
  418. _this.onExtensionLoadedObservable.clear();
  419. _this.onCompleteObservable.notifyObservers(_this);
  420. _this.onCompleteObservable.clear();
  421. });
  422. return loader;
  423. };
  424. GLTFFileLoader._parseBinary = function (data) {
  425. var Binary = {
  426. Magic: 0x46546C67
  427. };
  428. var binaryReader = new BinaryReader(data);
  429. var magic = binaryReader.readUint32();
  430. if (magic !== Binary.Magic) {
  431. throw new Error("Unexpected magic: " + magic);
  432. }
  433. var version = binaryReader.readUint32();
  434. switch (version) {
  435. case 1: return GLTFFileLoader._parseV1(binaryReader);
  436. case 2: return GLTFFileLoader._parseV2(binaryReader);
  437. }
  438. throw new Error("Unsupported version: " + version);
  439. };
  440. GLTFFileLoader._parseV1 = function (binaryReader) {
  441. var ContentFormat = {
  442. JSON: 0
  443. };
  444. var length = binaryReader.readUint32();
  445. if (length != binaryReader.getLength()) {
  446. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  447. }
  448. var contentLength = binaryReader.readUint32();
  449. var contentFormat = binaryReader.readUint32();
  450. var content;
  451. switch (contentFormat) {
  452. case ContentFormat.JSON: {
  453. content = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength)));
  454. break;
  455. }
  456. default: {
  457. throw new Error("Unexpected content format: " + contentFormat);
  458. }
  459. }
  460. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  461. var body = binaryReader.readUint8Array(bytesRemaining);
  462. return {
  463. json: content,
  464. bin: body
  465. };
  466. };
  467. GLTFFileLoader._parseV2 = function (binaryReader) {
  468. var ChunkFormat = {
  469. JSON: 0x4E4F534A,
  470. BIN: 0x004E4942
  471. };
  472. var length = binaryReader.readUint32();
  473. if (length !== binaryReader.getLength()) {
  474. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  475. }
  476. // JSON chunk
  477. var chunkLength = binaryReader.readUint32();
  478. var chunkFormat = binaryReader.readUint32();
  479. if (chunkFormat !== ChunkFormat.JSON) {
  480. throw new Error("First chunk format is not JSON");
  481. }
  482. var json = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength)));
  483. // Look for BIN chunk
  484. var bin = null;
  485. while (binaryReader.getPosition() < binaryReader.getLength()) {
  486. var chunkLength_1 = binaryReader.readUint32();
  487. var chunkFormat_1 = binaryReader.readUint32();
  488. switch (chunkFormat_1) {
  489. case ChunkFormat.JSON: {
  490. throw new Error("Unexpected JSON chunk");
  491. }
  492. case ChunkFormat.BIN: {
  493. bin = binaryReader.readUint8Array(chunkLength_1);
  494. break;
  495. }
  496. default: {
  497. // ignore unrecognized chunkFormat
  498. binaryReader.skipBytes(chunkLength_1);
  499. break;
  500. }
  501. }
  502. }
  503. return {
  504. json: json,
  505. bin: bin
  506. };
  507. };
  508. GLTFFileLoader._parseVersion = function (version) {
  509. if (version === "1.0" || version === "1.0.1") {
  510. return {
  511. major: 1,
  512. minor: 0
  513. };
  514. }
  515. var match = (version + "").match(/^(\d+)\.(\d+)/);
  516. if (!match) {
  517. return null;
  518. }
  519. return {
  520. major: parseInt(match[1]),
  521. minor: parseInt(match[2])
  522. };
  523. };
  524. GLTFFileLoader._compareVersion = function (a, b) {
  525. if (a.major > b.major)
  526. return 1;
  527. if (a.major < b.major)
  528. return -1;
  529. if (a.minor > b.minor)
  530. return 1;
  531. if (a.minor < b.minor)
  532. return -1;
  533. return 0;
  534. };
  535. GLTFFileLoader._decodeBufferToText = function (buffer) {
  536. var result = "";
  537. var length = buffer.byteLength;
  538. for (var i = 0; i < length; i++) {
  539. result += String.fromCharCode(buffer[i]);
  540. }
  541. return result;
  542. };
  543. // #endregion
  544. // #region V1 options
  545. /**
  546. * Set this property to false to disable incremental loading which delays the loader from calling the success callback until after loading the meshes and shaders.
  547. * Textures always loads asynchronously. For example, the success callback can compute the bounding information of the loaded meshes when incremental loading is disabled.
  548. * Defaults to true.
  549. */
  550. GLTFFileLoader.IncrementalLoading = true;
  551. /**
  552. * Set this property to true in order to work with homogeneous coordinates, available with some converters and exporters.
  553. * Defaults to false. See https://en.wikipedia.org/wiki/Homogeneous_coordinates.
  554. */
  555. GLTFFileLoader.HomogeneousCoordinates = false;
  556. return GLTFFileLoader;
  557. }());
  558. BABYLON.GLTFFileLoader = GLTFFileLoader;
  559. var BinaryReader = /** @class */ (function () {
  560. function BinaryReader(arrayBuffer) {
  561. this._arrayBuffer = arrayBuffer;
  562. this._dataView = new DataView(arrayBuffer);
  563. this._byteOffset = 0;
  564. }
  565. BinaryReader.prototype.getPosition = function () {
  566. return this._byteOffset;
  567. };
  568. BinaryReader.prototype.getLength = function () {
  569. return this._arrayBuffer.byteLength;
  570. };
  571. BinaryReader.prototype.readUint32 = function () {
  572. var value = this._dataView.getUint32(this._byteOffset, true);
  573. this._byteOffset += 4;
  574. return value;
  575. };
  576. BinaryReader.prototype.readUint8Array = function (length) {
  577. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  578. this._byteOffset += length;
  579. return value;
  580. };
  581. BinaryReader.prototype.skipBytes = function (length) {
  582. this._byteOffset += length;
  583. };
  584. return BinaryReader;
  585. }());
  586. if (BABYLON.SceneLoader) {
  587. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  588. }
  589. })(BABYLON || (BABYLON = {}));
  590. //# sourceMappingURL=babylon.glTFFileLoader.js.map
  591. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  592. var BABYLON;
  593. (function (BABYLON) {
  594. var GLTF2;
  595. (function (GLTF2) {
  596. /** @hidden */
  597. var _ArrayItem = /** @class */ (function () {
  598. function _ArrayItem() {
  599. }
  600. /** @hidden */
  601. _ArrayItem.Assign = function (values) {
  602. if (values) {
  603. for (var index = 0; index < values.length; index++) {
  604. values[index]._index = index;
  605. }
  606. }
  607. };
  608. return _ArrayItem;
  609. }());
  610. GLTF2._ArrayItem = _ArrayItem;
  611. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  612. })(BABYLON || (BABYLON = {}));
  613. //# sourceMappingURL=babylon.glTFLoaderUtilities.js.map
  614. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  615. /// <reference path="../../../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"/>
  616. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  617. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  618. /**
  619. * Defines the module used to import/export glTF 2.0 assets
  620. */
  621. var BABYLON;
  622. (function (BABYLON) {
  623. var GLTF2;
  624. (function (GLTF2) {
  625. /**
  626. * Loader for loading a glTF 2.0 asset
  627. */
  628. var GLTFLoader = /** @class */ (function () {
  629. function GLTFLoader() {
  630. /** @hidden */
  631. this._completePromises = new Array();
  632. /** @hidden */
  633. this._onReadyObservable = new BABYLON.Observable();
  634. this._disposed = false;
  635. this._state = null;
  636. this._extensions = {};
  637. this._defaultSampler = {};
  638. this._defaultBabylonMaterials = {};
  639. this._requests = new Array();
  640. /**
  641. * Mode that determines the coordinate system to use.
  642. */
  643. this.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.AUTO;
  644. /**
  645. * Mode that determines what animations will start.
  646. */
  647. this.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.FIRST;
  648. /**
  649. * Defines if the loader should compile materials.
  650. */
  651. this.compileMaterials = false;
  652. /**
  653. * Defines if the loader should also compile materials with clip planes.
  654. */
  655. this.useClipPlane = false;
  656. /**
  657. * Defines if the loader should compile shadow generators.
  658. */
  659. this.compileShadowGenerators = false;
  660. /**
  661. * Defines if the Alpha blended materials are only applied as coverage.
  662. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
  663. * If true, no extra effects are applied to transparent pixels.
  664. */
  665. this.transparencyAsCoverage = false;
  666. /** @hidden */
  667. this._normalizeAnimationGroupsToBeginAtZero = true;
  668. /**
  669. * Function called before loading a url referenced by the asset.
  670. */
  671. this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
  672. /**
  673. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  674. */
  675. this.onMeshLoadedObservable = new BABYLON.Observable();
  676. /**
  677. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  678. */
  679. this.onTextureLoadedObservable = new BABYLON.Observable();
  680. /**
  681. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  682. */
  683. this.onMaterialLoadedObservable = new BABYLON.Observable();
  684. /**
  685. * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
  686. */
  687. this.onCameraLoadedObservable = new BABYLON.Observable();
  688. /**
  689. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  690. * For assets with LODs, raised when all of the LODs are complete.
  691. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  692. */
  693. this.onCompleteObservable = new BABYLON.Observable();
  694. /**
  695. * Observable raised after the loader is disposed.
  696. */
  697. this.onDisposeObservable = new BABYLON.Observable();
  698. /**
  699. * Observable raised after a loader extension is created.
  700. * Set additional options for a loader extension in this event.
  701. */
  702. this.onExtensionLoadedObservable = new BABYLON.Observable();
  703. }
  704. /** @hidden */
  705. GLTFLoader._Register = function (name, factory) {
  706. if (GLTFLoader._ExtensionFactories[name]) {
  707. BABYLON.Tools.Error("Extension with the name '" + name + "' already exists");
  708. return;
  709. }
  710. GLTFLoader._ExtensionFactories[name] = factory;
  711. // Keep the order of registration so that extensions registered first are called first.
  712. GLTFLoader._ExtensionNames.push(name);
  713. };
  714. Object.defineProperty(GLTFLoader.prototype, "state", {
  715. /**
  716. * Loader state or null if the loader is not active.
  717. */
  718. get: function () {
  719. return this._state;
  720. },
  721. enumerable: true,
  722. configurable: true
  723. });
  724. /**
  725. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  726. */
  727. GLTFLoader.prototype.dispose = function () {
  728. if (this._disposed) {
  729. return;
  730. }
  731. this._disposed = true;
  732. this.onDisposeObservable.notifyObservers(this);
  733. this.onDisposeObservable.clear();
  734. this._clear();
  735. };
  736. /**
  737. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  738. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  739. * @param scene the scene the meshes should be added to
  740. * @param data the glTF data to load
  741. * @param rootUrl root url to load from
  742. * @param onProgress event that fires when loading progress has occured
  743. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  744. */
  745. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  746. var _this = this;
  747. return Promise.resolve().then(function () {
  748. var nodes = null;
  749. if (meshesNames) {
  750. var nodeMap_1 = {};
  751. if (_this._gltf.nodes) {
  752. for (var _i = 0, _a = _this._gltf.nodes; _i < _a.length; _i++) {
  753. var node = _a[_i];
  754. if (node.name) {
  755. nodeMap_1[node.name] = node;
  756. }
  757. }
  758. }
  759. var names = (meshesNames instanceof Array) ? meshesNames : [meshesNames];
  760. nodes = names.map(function (name) {
  761. var node = nodeMap_1[name];
  762. if (!node) {
  763. throw new Error("Failed to find node '" + name + "'");
  764. }
  765. return node;
  766. });
  767. }
  768. return _this._loadAsync(nodes, scene, data, rootUrl, onProgress).then(function () {
  769. return {
  770. meshes: _this._getMeshes(),
  771. particleSystems: [],
  772. skeletons: _this._getSkeletons(),
  773. animationGroups: _this._getAnimationGroups()
  774. };
  775. });
  776. });
  777. };
  778. /**
  779. * Imports all objects from the loaded glTF data and adds them to the scene
  780. * @param scene the scene the objects should be added to
  781. * @param data the glTF data to load
  782. * @param rootUrl root url to load from
  783. * @param onProgress event that fires when loading progress has occured
  784. * @returns a promise which completes when objects have been loaded to the scene
  785. */
  786. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  787. return this._loadAsync(null, scene, data, rootUrl, onProgress);
  788. };
  789. GLTFLoader.prototype._loadAsync = function (nodes, scene, data, rootUrl, onProgress) {
  790. var _this = this;
  791. return Promise.resolve().then(function () {
  792. _this._babylonScene = scene;
  793. _this._rootUrl = rootUrl;
  794. _this._progressCallback = onProgress;
  795. _this._state = BABYLON.GLTFLoaderState.LOADING;
  796. _this._loadData(data);
  797. _this._loadExtensions();
  798. _this._checkExtensions();
  799. var promises = new Array();
  800. if (nodes) {
  801. promises.push(_this._loadNodesAsync(nodes));
  802. }
  803. else {
  804. var scene_1 = GLTFLoader._GetProperty("#/scene", _this._gltf.scenes, _this._gltf.scene || 0);
  805. promises.push(_this._loadSceneAsync("#/scenes/" + scene_1._index, scene_1));
  806. }
  807. if (_this.compileMaterials) {
  808. promises.push(_this._compileMaterialsAsync());
  809. }
  810. if (_this.compileShadowGenerators) {
  811. promises.push(_this._compileShadowGeneratorsAsync());
  812. }
  813. var resultPromise = Promise.all(promises).then(function () {
  814. _this._state = BABYLON.GLTFLoaderState.READY;
  815. _this._onReadyObservable.notifyObservers(_this);
  816. _this._startAnimations();
  817. });
  818. resultPromise.then(function () {
  819. _this._rootBabylonMesh.setEnabled(true);
  820. BABYLON.Tools.SetImmediate(function () {
  821. if (!_this._disposed) {
  822. Promise.all(_this._completePromises).then(function () {
  823. _this._state = BABYLON.GLTFLoaderState.COMPLETE;
  824. _this.onCompleteObservable.notifyObservers(_this);
  825. _this.onCompleteObservable.clear();
  826. _this._clear();
  827. }).catch(function (error) {
  828. BABYLON.Tools.Error("glTF Loader: " + error.message);
  829. _this._clear();
  830. });
  831. }
  832. });
  833. });
  834. return resultPromise;
  835. }).catch(function (error) {
  836. if (!_this._disposed) {
  837. BABYLON.Tools.Error("glTF Loader: " + error.message);
  838. _this._clear();
  839. throw error;
  840. }
  841. });
  842. };
  843. GLTFLoader.prototype._loadData = function (data) {
  844. this._gltf = data.json;
  845. this._setupData();
  846. if (data.bin) {
  847. var buffers = this._gltf.buffers;
  848. if (buffers && buffers[0] && !buffers[0].uri) {
  849. var binaryBuffer = buffers[0];
  850. if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
  851. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  852. }
  853. binaryBuffer._data = Promise.resolve(data.bin);
  854. }
  855. else {
  856. BABYLON.Tools.Warn("Unexpected BIN chunk");
  857. }
  858. }
  859. };
  860. GLTFLoader.prototype._setupData = function () {
  861. GLTF2._ArrayItem.Assign(this._gltf.accessors);
  862. GLTF2._ArrayItem.Assign(this._gltf.animations);
  863. GLTF2._ArrayItem.Assign(this._gltf.buffers);
  864. GLTF2._ArrayItem.Assign(this._gltf.bufferViews);
  865. GLTF2._ArrayItem.Assign(this._gltf.cameras);
  866. GLTF2._ArrayItem.Assign(this._gltf.images);
  867. GLTF2._ArrayItem.Assign(this._gltf.materials);
  868. GLTF2._ArrayItem.Assign(this._gltf.meshes);
  869. GLTF2._ArrayItem.Assign(this._gltf.nodes);
  870. GLTF2._ArrayItem.Assign(this._gltf.samplers);
  871. GLTF2._ArrayItem.Assign(this._gltf.scenes);
  872. GLTF2._ArrayItem.Assign(this._gltf.skins);
  873. GLTF2._ArrayItem.Assign(this._gltf.textures);
  874. if (this._gltf.nodes) {
  875. var nodeParents = {};
  876. for (var _i = 0, _a = this._gltf.nodes; _i < _a.length; _i++) {
  877. var node = _a[_i];
  878. if (node.children) {
  879. for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
  880. var index = _c[_b];
  881. nodeParents[index] = node._index;
  882. }
  883. }
  884. }
  885. var rootNode = this._createRootNode();
  886. for (var _d = 0, _e = this._gltf.nodes; _d < _e.length; _d++) {
  887. var node = _e[_d];
  888. var parentIndex = nodeParents[node._index];
  889. node._parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
  890. }
  891. }
  892. };
  893. GLTFLoader.prototype._loadExtensions = function () {
  894. for (var _i = 0, _a = GLTFLoader._ExtensionNames; _i < _a.length; _i++) {
  895. var name_1 = _a[_i];
  896. var extension = GLTFLoader._ExtensionFactories[name_1](this);
  897. this._extensions[name_1] = extension;
  898. this.onExtensionLoadedObservable.notifyObservers(extension);
  899. }
  900. this.onExtensionLoadedObservable.clear();
  901. };
  902. GLTFLoader.prototype._checkExtensions = function () {
  903. if (this._gltf.extensionsRequired) {
  904. for (var _i = 0, _a = this._gltf.extensionsRequired; _i < _a.length; _i++) {
  905. var name_2 = _a[_i];
  906. var extension = this._extensions[name_2];
  907. if (!extension || !extension.enabled) {
  908. throw new Error("Require extension " + name_2 + " is not available");
  909. }
  910. }
  911. }
  912. };
  913. GLTFLoader.prototype._createRootNode = function () {
  914. this._rootBabylonMesh = new BABYLON.Mesh("__root__", this._babylonScene);
  915. this._rootBabylonMesh.setEnabled(false);
  916. var rootNode = { _babylonMesh: this._rootBabylonMesh };
  917. switch (this.coordinateSystemMode) {
  918. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO: {
  919. if (!this._babylonScene.useRightHandedSystem) {
  920. rootNode.rotation = [0, 1, 0, 0];
  921. rootNode.scale = [1, 1, -1];
  922. GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
  923. }
  924. break;
  925. }
  926. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
  927. this._babylonScene.useRightHandedSystem = true;
  928. break;
  929. }
  930. default: {
  931. throw new Error("Invalid coordinate system mode (" + this.coordinateSystemMode + ")");
  932. }
  933. }
  934. this.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
  935. return rootNode;
  936. };
  937. GLTFLoader.prototype._loadNodesAsync = function (nodes) {
  938. var promises = new Array();
  939. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  940. var node = nodes_1[_i];
  941. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  942. }
  943. promises.push(this._loadAnimationsAsync());
  944. return Promise.all(promises).then(function () { });
  945. };
  946. /** @hidden */
  947. GLTFLoader.prototype._loadSceneAsync = function (context, scene) {
  948. var promise = GLTF2.GLTFLoaderExtension._LoadSceneAsync(this, context, scene);
  949. if (promise) {
  950. return promise;
  951. }
  952. var promises = new Array();
  953. for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
  954. var index = _a[_i];
  955. var node = GLTFLoader._GetProperty(context + "/nodes/" + index, this._gltf.nodes, index);
  956. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  957. }
  958. promises.push(this._loadAnimationsAsync());
  959. return Promise.all(promises).then(function () { });
  960. };
  961. GLTFLoader.prototype._forEachPrimitive = function (node, callback) {
  962. if (node._primitiveBabylonMeshes) {
  963. for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
  964. var babylonMesh = _a[_i];
  965. callback(babylonMesh);
  966. }
  967. }
  968. else {
  969. callback(node._babylonMesh);
  970. }
  971. };
  972. GLTFLoader.prototype._getMeshes = function () {
  973. var meshes = new Array();
  974. // Root mesh is always first.
  975. meshes.push(this._rootBabylonMesh);
  976. var nodes = this._gltf.nodes;
  977. if (nodes) {
  978. for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
  979. var node = nodes_2[_i];
  980. if (node._babylonMesh) {
  981. meshes.push(node._babylonMesh);
  982. }
  983. if (node._primitiveBabylonMeshes) {
  984. for (var _a = 0, _b = node._primitiveBabylonMeshes; _a < _b.length; _a++) {
  985. var babylonMesh = _b[_a];
  986. meshes.push(babylonMesh);
  987. }
  988. }
  989. }
  990. }
  991. return meshes;
  992. };
  993. GLTFLoader.prototype._getSkeletons = function () {
  994. var skeletons = new Array();
  995. var skins = this._gltf.skins;
  996. if (skins) {
  997. for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
  998. var skin = skins_1[_i];
  999. if (skin._babylonSkeleton) {
  1000. skeletons.push(skin._babylonSkeleton);
  1001. }
  1002. }
  1003. }
  1004. return skeletons;
  1005. };
  1006. GLTFLoader.prototype._getAnimationGroups = function () {
  1007. var animationGroups = new Array();
  1008. var animations = this._gltf.animations;
  1009. if (animations) {
  1010. for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
  1011. var animation = animations_1[_i];
  1012. if (animation._babylonAnimationGroup) {
  1013. animationGroups.push(animation._babylonAnimationGroup);
  1014. }
  1015. }
  1016. }
  1017. return animationGroups;
  1018. };
  1019. GLTFLoader.prototype._startAnimations = function () {
  1020. switch (this.animationStartMode) {
  1021. case BABYLON.GLTFLoaderAnimationStartMode.NONE: {
  1022. // do nothing
  1023. break;
  1024. }
  1025. case BABYLON.GLTFLoaderAnimationStartMode.FIRST: {
  1026. var babylonAnimationGroups = this._getAnimationGroups();
  1027. if (babylonAnimationGroups.length !== 0) {
  1028. babylonAnimationGroups[0].start(true);
  1029. }
  1030. break;
  1031. }
  1032. case BABYLON.GLTFLoaderAnimationStartMode.ALL: {
  1033. var babylonAnimationGroups = this._getAnimationGroups();
  1034. for (var _i = 0, babylonAnimationGroups_1 = babylonAnimationGroups; _i < babylonAnimationGroups_1.length; _i++) {
  1035. var babylonAnimationGroup = babylonAnimationGroups_1[_i];
  1036. babylonAnimationGroup.start(true);
  1037. }
  1038. break;
  1039. }
  1040. default: {
  1041. BABYLON.Tools.Error("Invalid animation start mode (" + this.animationStartMode + ")");
  1042. return;
  1043. }
  1044. }
  1045. };
  1046. /** @hidden */
  1047. GLTFLoader.prototype._loadNodeAsync = function (context, node) {
  1048. var promise = GLTF2.GLTFLoaderExtension._LoadNodeAsync(this, context, node);
  1049. if (promise) {
  1050. return promise;
  1051. }
  1052. if (node._babylonMesh) {
  1053. throw new Error(context + ": Invalid recursive node hierarchy");
  1054. }
  1055. var promises = new Array();
  1056. var babylonMesh = new BABYLON.Mesh(node.name || "node" + node._index, this._babylonScene, node._parent._babylonMesh);
  1057. node._babylonMesh = babylonMesh;
  1058. GLTFLoader._LoadTransform(node, babylonMesh);
  1059. if (node.mesh != undefined) {
  1060. var mesh = GLTFLoader._GetProperty(context + "/mesh", this._gltf.meshes, node.mesh);
  1061. promises.push(this._loadMeshAsync("#/meshes/" + mesh._index, node, mesh, babylonMesh));
  1062. }
  1063. if (node.camera != undefined) {
  1064. var camera = GLTFLoader._GetProperty(context + "/camera", this._gltf.cameras, node.camera);
  1065. this._loadCamera("#/cameras/" + camera._index, camera, babylonMesh);
  1066. }
  1067. if (node.children) {
  1068. for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
  1069. var index = _a[_i];
  1070. var childNode = GLTFLoader._GetProperty(context + "/children/" + index, this._gltf.nodes, index);
  1071. promises.push(this._loadNodeAsync("#/nodes/" + index, childNode));
  1072. }
  1073. }
  1074. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  1075. return Promise.all(promises).then(function () { });
  1076. };
  1077. GLTFLoader.prototype._loadMeshAsync = function (context, node, mesh, babylonMesh) {
  1078. var _this = this;
  1079. var promises = new Array();
  1080. var primitives = mesh.primitives;
  1081. if (!primitives || primitives.length === 0) {
  1082. throw new Error(context + ": Primitives are missing");
  1083. }
  1084. GLTF2._ArrayItem.Assign(primitives);
  1085. if (primitives.length === 1) {
  1086. var primitive = primitives[0];
  1087. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, babylonMesh));
  1088. }
  1089. else {
  1090. node._primitiveBabylonMeshes = [];
  1091. for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
  1092. var primitive = primitives_1[_i];
  1093. var primitiveBabylonMesh = new BABYLON.Mesh((mesh.name || babylonMesh.name) + "_" + primitive._index, this._babylonScene, babylonMesh);
  1094. node._primitiveBabylonMeshes.push(primitiveBabylonMesh);
  1095. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, primitiveBabylonMesh));
  1096. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  1097. }
  1098. }
  1099. if (node.skin != undefined) {
  1100. var skin = GLTFLoader._GetProperty(context + "/skin", this._gltf.skins, node.skin);
  1101. promises.push(this._loadSkinAsync("#/skins/" + skin._index, node, mesh, skin));
  1102. }
  1103. return Promise.all(promises).then(function () {
  1104. _this._forEachPrimitive(node, function (babylonMesh) {
  1105. babylonMesh._refreshBoundingInfo(true);
  1106. });
  1107. });
  1108. };
  1109. GLTFLoader.prototype._loadPrimitiveAsync = function (context, node, mesh, primitive, babylonMesh) {
  1110. var _this = this;
  1111. var promises = new Array();
  1112. this._createMorphTargets(context, node, mesh, primitive, babylonMesh);
  1113. promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh).then(function (babylonGeometry) {
  1114. return _this._loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry).then(function () {
  1115. babylonGeometry.applyToMesh(babylonMesh);
  1116. });
  1117. }));
  1118. var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
  1119. if (primitive.material == undefined) {
  1120. babylonMesh.material = this._getDefaultMaterial(babylonDrawMode);
  1121. }
  1122. else {
  1123. var material = GLTFLoader._GetProperty(context + "/material}", this._gltf.materials, primitive.material);
  1124. promises.push(this._loadMaterialAsync("#/materials/" + material._index, material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
  1125. babylonMesh.material = babylonMaterial;
  1126. }));
  1127. }
  1128. return Promise.all(promises).then(function () { });
  1129. };
  1130. GLTFLoader.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  1131. var _this = this;
  1132. var promise = GLTF2.GLTFLoaderExtension._LoadVertexDataAsync(this, context, primitive, babylonMesh);
  1133. if (promise) {
  1134. return promise;
  1135. }
  1136. var attributes = primitive.attributes;
  1137. if (!attributes) {
  1138. throw new Error(context + ": Attributes are missing");
  1139. }
  1140. var promises = new Array();
  1141. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, this._babylonScene);
  1142. if (primitive.indices == undefined) {
  1143. babylonMesh.isUnIndexed = true;
  1144. }
  1145. else {
  1146. var accessor = GLTFLoader._GetProperty(context + "/indices", this._gltf.accessors, primitive.indices);
  1147. promises.push(this._loadIndicesAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1148. babylonGeometry.setIndices(data);
  1149. }));
  1150. }
  1151. var loadAttribute = function (attribute, kind, callback) {
  1152. if (attributes[attribute] == undefined) {
  1153. return;
  1154. }
  1155. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  1156. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  1157. babylonMesh._delayInfo.push(kind);
  1158. }
  1159. var accessor = GLTFLoader._GetProperty(context + "/attributes/" + attribute, _this._gltf.accessors, attributes[attribute]);
  1160. promises.push(_this._loadVertexAccessorAsync("#/accessors/" + accessor._index, accessor, kind).then(function (babylonVertexBuffer) {
  1161. babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
  1162. }));
  1163. if (callback) {
  1164. callback(accessor);
  1165. }
  1166. };
  1167. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  1168. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  1169. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  1170. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  1171. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  1172. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  1173. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  1174. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind, function (accessor) {
  1175. if (accessor.type === "VEC4" /* VEC4 */) {
  1176. babylonMesh.hasVertexAlpha = true;
  1177. }
  1178. });
  1179. return Promise.all(promises).then(function () {
  1180. return babylonGeometry;
  1181. });
  1182. };
  1183. GLTFLoader.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
  1184. if (!primitive.targets) {
  1185. return;
  1186. }
  1187. if (node._numMorphTargets == undefined) {
  1188. node._numMorphTargets = primitive.targets.length;
  1189. }
  1190. else if (primitive.targets.length !== node._numMorphTargets) {
  1191. throw new Error(context + ": Primitives do not have the same number of targets");
  1192. }
  1193. babylonMesh.morphTargetManager = new BABYLON.MorphTargetManager();
  1194. for (var index = 0; index < primitive.targets.length; index++) {
  1195. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  1196. babylonMesh.morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  1197. // TODO: tell the target whether it has positions, normals, tangents
  1198. }
  1199. };
  1200. GLTFLoader.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonGeometry) {
  1201. if (!primitive.targets) {
  1202. return Promise.resolve();
  1203. }
  1204. var promises = new Array();
  1205. var morphTargetManager = babylonMesh.morphTargetManager;
  1206. for (var index = 0; index < morphTargetManager.numTargets; index++) {
  1207. var babylonMorphTarget = morphTargetManager.getTarget(index);
  1208. promises.push(this._loadMorphTargetVertexDataAsync(context + "/targets/" + index, babylonGeometry, primitive.targets[index], babylonMorphTarget));
  1209. }
  1210. return Promise.all(promises).then(function () { });
  1211. };
  1212. GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, babylonGeometry, attributes, babylonMorphTarget) {
  1213. var _this = this;
  1214. var promises = new Array();
  1215. var loadAttribute = function (attribute, kind, setData) {
  1216. if (attributes[attribute] == undefined) {
  1217. return;
  1218. }
  1219. var babylonVertexBuffer = babylonGeometry.getVertexBuffer(kind);
  1220. if (!babylonVertexBuffer) {
  1221. return;
  1222. }
  1223. var accessor = GLTFLoader._GetProperty(context + "/" + attribute, _this._gltf.accessors, attributes[attribute]);
  1224. promises.push(_this._loadFloatAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1225. setData(babylonVertexBuffer, data);
  1226. }));
  1227. };
  1228. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind, function (babylonVertexBuffer, data) {
  1229. babylonVertexBuffer.forEach(data.length, function (value, index) {
  1230. data[index] += value;
  1231. });
  1232. babylonMorphTarget.setPositions(data);
  1233. });
  1234. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind, function (babylonVertexBuffer, data) {
  1235. babylonVertexBuffer.forEach(data.length, function (value, index) {
  1236. data[index] += value;
  1237. });
  1238. babylonMorphTarget.setNormals(data);
  1239. });
  1240. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind, function (babylonVertexBuffer, data) {
  1241. var dataIndex = 0;
  1242. babylonVertexBuffer.forEach(data.length / 3 * 4, function (value, index) {
  1243. // Tangent data for morph targets is stored as xyz delta.
  1244. // The vertexData.tangent is stored as xyzw.
  1245. // So we need to skip every fourth vertexData.tangent.
  1246. if (((index + 1) % 4) !== 0) {
  1247. data[dataIndex++] += value;
  1248. }
  1249. });
  1250. babylonMorphTarget.setTangents(data);
  1251. });
  1252. return Promise.all(promises).then(function () { });
  1253. };
  1254. GLTFLoader._LoadTransform = function (node, babylonNode) {
  1255. var position = BABYLON.Vector3.Zero();
  1256. var rotation = BABYLON.Quaternion.Identity();
  1257. var scaling = BABYLON.Vector3.One();
  1258. if (node.matrix) {
  1259. var matrix = BABYLON.Matrix.FromArray(node.matrix);
  1260. matrix.decompose(scaling, rotation, position);
  1261. }
  1262. else {
  1263. if (node.translation)
  1264. position = BABYLON.Vector3.FromArray(node.translation);
  1265. if (node.rotation)
  1266. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  1267. if (node.scale)
  1268. scaling = BABYLON.Vector3.FromArray(node.scale);
  1269. }
  1270. babylonNode.position = position;
  1271. babylonNode.rotationQuaternion = rotation;
  1272. babylonNode.scaling = scaling;
  1273. };
  1274. GLTFLoader.prototype._loadSkinAsync = function (context, node, mesh, skin) {
  1275. var _this = this;
  1276. var assignSkeleton = function (skeleton) {
  1277. _this._forEachPrimitive(node, function (babylonMesh) {
  1278. babylonMesh.skeleton = skeleton;
  1279. });
  1280. // Ignore the TRS of skinned nodes.
  1281. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  1282. node._babylonMesh.parent = _this._rootBabylonMesh;
  1283. node._babylonMesh.position = BABYLON.Vector3.Zero();
  1284. node._babylonMesh.rotationQuaternion = BABYLON.Quaternion.Identity();
  1285. node._babylonMesh.scaling = BABYLON.Vector3.One();
  1286. };
  1287. if (skin._loaded) {
  1288. return skin._loaded.then(function () {
  1289. assignSkeleton(skin._babylonSkeleton);
  1290. });
  1291. }
  1292. var skeletonId = "skeleton" + skin._index;
  1293. var babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
  1294. skin._babylonSkeleton = babylonSkeleton;
  1295. this._loadBones(context, skin);
  1296. assignSkeleton(babylonSkeleton);
  1297. return (skin._loaded = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
  1298. _this._updateBoneMatrices(babylonSkeleton, inverseBindMatricesData);
  1299. }));
  1300. };
  1301. GLTFLoader.prototype._loadBones = function (context, skin) {
  1302. var babylonBones = {};
  1303. for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
  1304. var index = _a[_i];
  1305. var node = GLTFLoader._GetProperty(context + "/joints/" + index, this._gltf.nodes, index);
  1306. this._loadBone(node, skin, babylonBones);
  1307. }
  1308. };
  1309. GLTFLoader.prototype._loadBone = function (node, skin, babylonBones) {
  1310. var babylonBone = babylonBones[node._index];
  1311. if (babylonBone) {
  1312. return babylonBone;
  1313. }
  1314. var babylonParentBone = null;
  1315. if (node._parent._babylonMesh !== this._rootBabylonMesh) {
  1316. babylonParentBone = this._loadBone(node._parent, skin, babylonBones);
  1317. }
  1318. var boneIndex = skin.joints.indexOf(node._index);
  1319. babylonBone = new BABYLON.Bone(node.name || "joint" + node._index, skin._babylonSkeleton, babylonParentBone, this._getNodeMatrix(node), null, null, boneIndex);
  1320. babylonBones[node._index] = babylonBone;
  1321. node._babylonBones = node._babylonBones || [];
  1322. node._babylonBones.push(babylonBone);
  1323. return babylonBone;
  1324. };
  1325. GLTFLoader.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
  1326. if (skin.inverseBindMatrices == undefined) {
  1327. return Promise.resolve(null);
  1328. }
  1329. var accessor = GLTFLoader._GetProperty(context + "/inverseBindMatrices", this._gltf.accessors, skin.inverseBindMatrices);
  1330. return this._loadFloatAccessorAsync("#/accessors/" + accessor._index, accessor);
  1331. };
  1332. GLTFLoader.prototype._updateBoneMatrices = function (babylonSkeleton, inverseBindMatricesData) {
  1333. for (var _i = 0, _a = babylonSkeleton.bones; _i < _a.length; _i++) {
  1334. var babylonBone = _a[_i];
  1335. var baseMatrix = BABYLON.Matrix.Identity();
  1336. var boneIndex = babylonBone._index;
  1337. if (inverseBindMatricesData && boneIndex !== -1) {
  1338. BABYLON.Matrix.FromArrayToRef(inverseBindMatricesData, boneIndex * 16, baseMatrix);
  1339. baseMatrix.invertToRef(baseMatrix);
  1340. }
  1341. var babylonParentBone = babylonBone.getParent();
  1342. if (babylonParentBone) {
  1343. baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
  1344. }
  1345. babylonBone.updateMatrix(baseMatrix, false, false);
  1346. babylonBone._updateDifferenceMatrix(undefined, false);
  1347. }
  1348. };
  1349. GLTFLoader.prototype._getNodeMatrix = function (node) {
  1350. return node.matrix ?
  1351. BABYLON.Matrix.FromArray(node.matrix) :
  1352. BABYLON.Matrix.Compose(node.scale ? BABYLON.Vector3.FromArray(node.scale) : BABYLON.Vector3.One(), node.rotation ? BABYLON.Quaternion.FromArray(node.rotation) : BABYLON.Quaternion.Identity(), node.translation ? BABYLON.Vector3.FromArray(node.translation) : BABYLON.Vector3.Zero());
  1353. };
  1354. GLTFLoader.prototype._loadCamera = function (context, camera, babylonMesh) {
  1355. var babylonCamera = new BABYLON.FreeCamera(camera.name || "camera" + camera._index, BABYLON.Vector3.Zero(), this._babylonScene, false);
  1356. babylonCamera.parent = babylonMesh;
  1357. babylonCamera.rotation = new BABYLON.Vector3(0, Math.PI, 0);
  1358. switch (camera.type) {
  1359. case "perspective" /* PERSPECTIVE */: {
  1360. var perspective = camera.perspective;
  1361. if (!perspective) {
  1362. throw new Error(context + ": Camera perspective properties are missing");
  1363. }
  1364. babylonCamera.fov = perspective.yfov;
  1365. babylonCamera.minZ = perspective.znear;
  1366. babylonCamera.maxZ = perspective.zfar || Number.MAX_VALUE;
  1367. break;
  1368. }
  1369. case "orthographic" /* ORTHOGRAPHIC */: {
  1370. if (!camera.orthographic) {
  1371. throw new Error(context + ": Camera orthographic properties are missing");
  1372. }
  1373. babylonCamera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
  1374. babylonCamera.orthoLeft = -camera.orthographic.xmag;
  1375. babylonCamera.orthoRight = camera.orthographic.xmag;
  1376. babylonCamera.orthoBottom = -camera.orthographic.ymag;
  1377. babylonCamera.orthoTop = camera.orthographic.ymag;
  1378. babylonCamera.minZ = camera.orthographic.znear;
  1379. babylonCamera.maxZ = camera.orthographic.zfar;
  1380. break;
  1381. }
  1382. default: {
  1383. throw new Error(context + ": Invalid camera type (" + camera.type + ")");
  1384. }
  1385. }
  1386. this.onCameraLoadedObservable.notifyObservers(babylonCamera);
  1387. };
  1388. GLTFLoader.prototype._loadAnimationsAsync = function () {
  1389. var animations = this._gltf.animations;
  1390. if (!animations) {
  1391. return Promise.resolve();
  1392. }
  1393. var promises = new Array();
  1394. for (var index = 0; index < animations.length; index++) {
  1395. var animation = animations[index];
  1396. promises.push(this._loadAnimationAsync("#/animations/" + index, animation));
  1397. }
  1398. return Promise.all(promises).then(function () { });
  1399. };
  1400. GLTFLoader.prototype._loadAnimationAsync = function (context, animation) {
  1401. var _this = this;
  1402. var babylonAnimationGroup = new BABYLON.AnimationGroup(animation.name || "animation" + animation._index, this._babylonScene);
  1403. animation._babylonAnimationGroup = babylonAnimationGroup;
  1404. var promises = new Array();
  1405. GLTF2._ArrayItem.Assign(animation.channels);
  1406. GLTF2._ArrayItem.Assign(animation.samplers);
  1407. for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
  1408. var channel = _a[_i];
  1409. promises.push(this._loadAnimationChannelAsync(context + "/channels/" + channel._index, context, animation, channel, babylonAnimationGroup));
  1410. }
  1411. return Promise.all(promises).then(function () {
  1412. babylonAnimationGroup.normalize(_this._normalizeAnimationGroupsToBeginAtZero ? 0 : null);
  1413. });
  1414. };
  1415. GLTFLoader.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup) {
  1416. var _this = this;
  1417. var targetNode = GLTFLoader._GetProperty(context + "/target/node", this._gltf.nodes, channel.target.node);
  1418. // Ignore animations that have no animation targets.
  1419. if ((channel.target.path === "weights" /* WEIGHTS */ && !targetNode._numMorphTargets) ||
  1420. (channel.target.path !== "weights" /* WEIGHTS */ && !targetNode._babylonMesh)) {
  1421. return Promise.resolve();
  1422. }
  1423. // Ignore animations targeting TRS of skinned nodes.
  1424. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  1425. if (targetNode.skin != undefined && channel.target.path !== "weights" /* WEIGHTS */) {
  1426. return Promise.resolve();
  1427. }
  1428. var sampler = GLTFLoader._GetProperty(context + "/sampler", animation.samplers, channel.sampler);
  1429. return this._loadAnimationSamplerAsync(animationContext + "/samplers/" + channel.sampler, sampler).then(function (data) {
  1430. var targetPath;
  1431. var animationType;
  1432. switch (channel.target.path) {
  1433. case "translation" /* TRANSLATION */: {
  1434. targetPath = "position";
  1435. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1436. break;
  1437. }
  1438. case "rotation" /* ROTATION */: {
  1439. targetPath = "rotationQuaternion";
  1440. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  1441. break;
  1442. }
  1443. case "scale" /* SCALE */: {
  1444. targetPath = "scaling";
  1445. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1446. break;
  1447. }
  1448. case "weights" /* WEIGHTS */: {
  1449. targetPath = "influence";
  1450. animationType = BABYLON.Animation.ANIMATIONTYPE_FLOAT;
  1451. break;
  1452. }
  1453. default: {
  1454. throw new Error(context + ": Invalid target path (" + channel.target.path + ")");
  1455. }
  1456. }
  1457. var outputBufferOffset = 0;
  1458. var getNextOutputValue;
  1459. switch (targetPath) {
  1460. case "position": {
  1461. getNextOutputValue = function () {
  1462. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1463. outputBufferOffset += 3;
  1464. return value;
  1465. };
  1466. break;
  1467. }
  1468. case "rotationQuaternion": {
  1469. getNextOutputValue = function () {
  1470. var value = BABYLON.Quaternion.FromArray(data.output, outputBufferOffset);
  1471. outputBufferOffset += 4;
  1472. return value;
  1473. };
  1474. break;
  1475. }
  1476. case "scaling": {
  1477. getNextOutputValue = function () {
  1478. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1479. outputBufferOffset += 3;
  1480. return value;
  1481. };
  1482. break;
  1483. }
  1484. case "influence": {
  1485. getNextOutputValue = function () {
  1486. var value = new Array(targetNode._numMorphTargets);
  1487. for (var i = 0; i < targetNode._numMorphTargets; i++) {
  1488. value[i] = data.output[outputBufferOffset++];
  1489. }
  1490. return value;
  1491. };
  1492. break;
  1493. }
  1494. }
  1495. var getNextKey;
  1496. switch (data.interpolation) {
  1497. case "STEP" /* STEP */: {
  1498. getNextKey = function (frameIndex) { return ({
  1499. frame: data.input[frameIndex],
  1500. value: getNextOutputValue(),
  1501. interpolation: BABYLON.AnimationKeyInterpolation.STEP
  1502. }); };
  1503. break;
  1504. }
  1505. case "LINEAR" /* LINEAR */: {
  1506. getNextKey = function (frameIndex) { return ({
  1507. frame: data.input[frameIndex],
  1508. value: getNextOutputValue()
  1509. }); };
  1510. break;
  1511. }
  1512. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1513. getNextKey = function (frameIndex) { return ({
  1514. frame: data.input[frameIndex],
  1515. inTangent: getNextOutputValue(),
  1516. value: getNextOutputValue(),
  1517. outTangent: getNextOutputValue()
  1518. }); };
  1519. break;
  1520. }
  1521. }
  1522. var keys = new Array(data.input.length);
  1523. for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
  1524. keys[frameIndex] = getNextKey(frameIndex);
  1525. }
  1526. if (targetPath === "influence") {
  1527. var _loop_1 = function (targetIndex) {
  1528. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1529. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1530. babylonAnimation.setKeys(keys.map(function (key) { return ({
  1531. frame: key.frame,
  1532. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  1533. value: key.value[targetIndex],
  1534. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  1535. }); }));
  1536. _this._forEachPrimitive(targetNode, function (babylonMesh) {
  1537. var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
  1538. var babylonAnimationClone = babylonAnimation.clone();
  1539. morphTarget.animations.push(babylonAnimationClone);
  1540. babylonAnimationGroup.addTargetedAnimation(babylonAnimationClone, morphTarget);
  1541. });
  1542. };
  1543. for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
  1544. _loop_1(targetIndex);
  1545. }
  1546. }
  1547. else {
  1548. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1549. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1550. babylonAnimation.setKeys(keys);
  1551. if (targetNode._babylonBones) {
  1552. var babylonAnimationTargets = [targetNode._babylonMesh].concat(targetNode._babylonBones);
  1553. for (var _i = 0, babylonAnimationTargets_1 = babylonAnimationTargets; _i < babylonAnimationTargets_1.length; _i++) {
  1554. var babylonAnimationTarget = babylonAnimationTargets_1[_i];
  1555. babylonAnimationTarget.animations.push(babylonAnimation);
  1556. }
  1557. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, babylonAnimationTargets);
  1558. }
  1559. else {
  1560. targetNode._babylonMesh.animations.push(babylonAnimation);
  1561. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, targetNode._babylonMesh);
  1562. }
  1563. }
  1564. });
  1565. };
  1566. GLTFLoader.prototype._loadAnimationSamplerAsync = function (context, sampler) {
  1567. if (sampler._data) {
  1568. return sampler._data;
  1569. }
  1570. var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
  1571. switch (interpolation) {
  1572. case "STEP" /* STEP */:
  1573. case "LINEAR" /* LINEAR */:
  1574. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1575. break;
  1576. }
  1577. default: {
  1578. throw new Error(context + ": Invalid interpolation (" + sampler.interpolation + ")");
  1579. }
  1580. }
  1581. var inputAccessor = GLTFLoader._GetProperty(context + "/input", this._gltf.accessors, sampler.input);
  1582. var outputAccessor = GLTFLoader._GetProperty(context + "/output", this._gltf.accessors, sampler.output);
  1583. sampler._data = Promise.all([
  1584. this._loadFloatAccessorAsync("#/accessors/" + inputAccessor._index, inputAccessor),
  1585. this._loadFloatAccessorAsync("#/accessors/" + outputAccessor._index, outputAccessor)
  1586. ]).then(function (_a) {
  1587. var inputData = _a[0], outputData = _a[1];
  1588. return {
  1589. input: inputData,
  1590. interpolation: interpolation,
  1591. output: outputData,
  1592. };
  1593. });
  1594. return sampler._data;
  1595. };
  1596. GLTFLoader.prototype._loadBufferAsync = function (context, buffer) {
  1597. if (buffer._data) {
  1598. return buffer._data;
  1599. }
  1600. if (!buffer.uri) {
  1601. throw new Error(context + ": Uri is missing");
  1602. }
  1603. buffer._data = this._loadUriAsync(context, buffer.uri);
  1604. return buffer._data;
  1605. };
  1606. /** @hidden */
  1607. GLTFLoader.prototype._loadBufferViewAsync = function (context, bufferView) {
  1608. if (bufferView._data) {
  1609. return bufferView._data;
  1610. }
  1611. var buffer = GLTFLoader._GetProperty(context + "/buffer", this._gltf.buffers, bufferView.buffer);
  1612. bufferView._data = this._loadBufferAsync("#/buffers/" + buffer._index, buffer).then(function (data) {
  1613. try {
  1614. return new Uint8Array(data.buffer, data.byteOffset + (bufferView.byteOffset || 0), bufferView.byteLength);
  1615. }
  1616. catch (e) {
  1617. throw new Error(context + ": " + e.message);
  1618. }
  1619. });
  1620. return bufferView._data;
  1621. };
  1622. GLTFLoader.prototype._loadIndicesAccessorAsync = function (context, accessor) {
  1623. if (accessor.type !== "SCALAR" /* SCALAR */) {
  1624. throw new Error(context + ": Invalid type " + accessor.type);
  1625. }
  1626. if (accessor.componentType !== 5121 /* UNSIGNED_BYTE */ &&
  1627. accessor.componentType !== 5123 /* UNSIGNED_SHORT */ &&
  1628. accessor.componentType !== 5125 /* UNSIGNED_INT */) {
  1629. throw new Error(context + ": Invalid component type " + accessor.componentType);
  1630. }
  1631. if (accessor._data) {
  1632. return accessor._data;
  1633. }
  1634. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1635. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  1636. return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, accessor.count);
  1637. });
  1638. return accessor._data;
  1639. };
  1640. GLTFLoader.prototype._loadFloatAccessorAsync = function (context, accessor) {
  1641. // TODO: support normalized and stride
  1642. var _this = this;
  1643. if (accessor.componentType !== 5126 /* FLOAT */) {
  1644. throw new Error("Invalid component type " + accessor.componentType);
  1645. }
  1646. if (accessor._data) {
  1647. return accessor._data;
  1648. }
  1649. var numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
  1650. var length = numComponents * accessor.count;
  1651. if (accessor.bufferView == undefined) {
  1652. accessor._data = Promise.resolve(new Float32Array(length));
  1653. }
  1654. else {
  1655. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1656. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  1657. return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, length);
  1658. });
  1659. }
  1660. if (accessor.sparse) {
  1661. var sparse_1 = accessor.sparse;
  1662. accessor._data = accessor._data.then(function (data) {
  1663. var indicesBufferView = GLTFLoader._GetProperty(context + "/sparse/indices/bufferView", _this._gltf.bufferViews, sparse_1.indices.bufferView);
  1664. var valuesBufferView = GLTFLoader._GetProperty(context + "/sparse/values/bufferView", _this._gltf.bufferViews, sparse_1.values.bufferView);
  1665. return Promise.all([
  1666. _this._loadBufferViewAsync("#/bufferViews/" + indicesBufferView._index, indicesBufferView),
  1667. _this._loadBufferViewAsync("#/bufferViews/" + valuesBufferView._index, valuesBufferView)
  1668. ]).then(function (_a) {
  1669. var indicesData = _a[0], valuesData = _a[1];
  1670. var indices = GLTFLoader._GetTypedArray(context + "/sparse/indices", sparse_1.indices.componentType, indicesData, sparse_1.indices.byteOffset, sparse_1.count);
  1671. var values = GLTFLoader._GetTypedArray(context + "/sparse/values", accessor.componentType, valuesData, sparse_1.values.byteOffset, numComponents * sparse_1.count);
  1672. var valuesIndex = 0;
  1673. for (var indicesIndex = 0; indicesIndex < indices.length; indicesIndex++) {
  1674. var dataIndex = indices[indicesIndex] * numComponents;
  1675. for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
  1676. data[dataIndex++] = values[valuesIndex++];
  1677. }
  1678. }
  1679. return data;
  1680. });
  1681. });
  1682. }
  1683. return accessor._data;
  1684. };
  1685. /** @hidden */
  1686. GLTFLoader.prototype._loadVertexBufferViewAsync = function (context, bufferView, kind) {
  1687. var _this = this;
  1688. if (bufferView._babylonBuffer) {
  1689. return bufferView._babylonBuffer;
  1690. }
  1691. bufferView._babylonBuffer = this._loadBufferViewAsync(context, bufferView).then(function (data) {
  1692. return new BABYLON.Buffer(_this._babylonScene.getEngine(), data, false);
  1693. });
  1694. return bufferView._babylonBuffer;
  1695. };
  1696. GLTFLoader.prototype._loadVertexAccessorAsync = function (context, accessor, kind) {
  1697. var _this = this;
  1698. if (accessor._babylonVertexBuffer) {
  1699. return accessor._babylonVertexBuffer;
  1700. }
  1701. if (accessor.sparse) {
  1702. accessor._babylonVertexBuffer = this._loadFloatAccessorAsync(context, accessor).then(function (data) {
  1703. return new BABYLON.VertexBuffer(_this._babylonScene.getEngine(), data, kind, false);
  1704. });
  1705. }
  1706. else {
  1707. var bufferView_1 = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1708. accessor._babylonVertexBuffer = this._loadVertexBufferViewAsync("#/bufferViews/" + bufferView_1._index, bufferView_1, kind).then(function (buffer) {
  1709. var size = GLTFLoader._GetNumComponents(context, accessor.type);
  1710. return new BABYLON.VertexBuffer(_this._babylonScene.getEngine(), buffer, kind, false, false, bufferView_1.byteStride, false, accessor.byteOffset, size, accessor.componentType, accessor.normalized, true);
  1711. });
  1712. }
  1713. return accessor._babylonVertexBuffer;
  1714. };
  1715. GLTFLoader.prototype._getDefaultMaterial = function (drawMode) {
  1716. var babylonMaterial = this._defaultBabylonMaterials[drawMode];
  1717. if (!babylonMaterial) {
  1718. babylonMaterial = this._createMaterial("__gltf_default", drawMode);
  1719. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1720. babylonMaterial.metallic = 1;
  1721. babylonMaterial.roughness = 1;
  1722. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1723. }
  1724. return babylonMaterial;
  1725. };
  1726. GLTFLoader.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, material, babylonMaterial) {
  1727. var promises = new Array();
  1728. // Ensure metallic workflow
  1729. babylonMaterial.metallic = 1;
  1730. babylonMaterial.roughness = 1;
  1731. var properties = material.pbrMetallicRoughness;
  1732. if (properties) {
  1733. if (properties.baseColorFactor) {
  1734. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  1735. babylonMaterial.alpha = properties.baseColorFactor[3];
  1736. }
  1737. else {
  1738. babylonMaterial.albedoColor = BABYLON.Color3.White();
  1739. }
  1740. babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
  1741. babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
  1742. if (properties.baseColorTexture) {
  1743. promises.push(this._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  1744. babylonMaterial.albedoTexture = texture;
  1745. }));
  1746. }
  1747. if (properties.metallicRoughnessTexture) {
  1748. promises.push(this._loadTextureAsync(context + "/metallicRoughnessTexture", properties.metallicRoughnessTexture, function (texture) {
  1749. babylonMaterial.metallicTexture = texture;
  1750. }));
  1751. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  1752. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  1753. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  1754. }
  1755. }
  1756. this._loadMaterialAlphaProperties(context, material, babylonMaterial);
  1757. return Promise.all(promises).then(function () { });
  1758. };
  1759. /** @hidden */
  1760. GLTFLoader.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  1761. var promise = GLTF2.GLTFLoaderExtension._LoadMaterialAsync(this, context, material, babylonMesh, babylonDrawMode, assign);
  1762. if (promise) {
  1763. return promise;
  1764. }
  1765. material._babylonData = material._babylonData || {};
  1766. var babylonData = material._babylonData[babylonDrawMode];
  1767. if (!babylonData) {
  1768. var promises = new Array();
  1769. var name_3 = material.name || "materialSG_" + material._index;
  1770. var babylonMaterial = this._createMaterial(name_3, babylonDrawMode);
  1771. promises.push(this._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  1772. promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync(context, material, babylonMaterial));
  1773. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1774. babylonData = {
  1775. material: babylonMaterial,
  1776. meshes: [],
  1777. loaded: Promise.all(promises).then(function () { })
  1778. };
  1779. material._babylonData[babylonDrawMode] = babylonData;
  1780. }
  1781. babylonData.meshes.push(babylonMesh);
  1782. assign(babylonData.material);
  1783. return babylonData.loaded;
  1784. };
  1785. /** @hidden */
  1786. GLTFLoader.prototype._createMaterial = function (name, drawMode) {
  1787. var babylonMaterial = new BABYLON.PBRMaterial(name, this._babylonScene);
  1788. babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? BABYLON.Material.CounterClockWiseSideOrientation : BABYLON.Material.ClockWiseSideOrientation;
  1789. babylonMaterial.fillMode = drawMode;
  1790. babylonMaterial.enableSpecularAntiAliasing = true;
  1791. babylonMaterial.useRadianceOverAlpha = !this.transparencyAsCoverage;
  1792. babylonMaterial.useSpecularOverAlpha = !this.transparencyAsCoverage;
  1793. return babylonMaterial;
  1794. };
  1795. /** @hidden */
  1796. GLTFLoader.prototype._loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
  1797. var promises = new Array();
  1798. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  1799. if (material.doubleSided) {
  1800. babylonMaterial.backFaceCulling = false;
  1801. babylonMaterial.twoSidedLighting = true;
  1802. }
  1803. if (material.normalTexture) {
  1804. promises.push(this._loadTextureAsync(context + "/normalTexture", material.normalTexture, function (texture) {
  1805. babylonMaterial.bumpTexture = texture;
  1806. }));
  1807. babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
  1808. babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
  1809. if (material.normalTexture.scale != undefined) {
  1810. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  1811. }
  1812. }
  1813. if (material.occlusionTexture) {
  1814. promises.push(this._loadTextureAsync(context + "/occlusionTexture", material.occlusionTexture, function (texture) {
  1815. babylonMaterial.ambientTexture = texture;
  1816. }));
  1817. babylonMaterial.useAmbientInGrayScale = true;
  1818. if (material.occlusionTexture.strength != undefined) {
  1819. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  1820. }
  1821. }
  1822. if (material.emissiveTexture) {
  1823. promises.push(this._loadTextureAsync(context + "/emissiveTexture", material.emissiveTexture, function (texture) {
  1824. babylonMaterial.emissiveTexture = texture;
  1825. }));
  1826. }
  1827. return Promise.all(promises).then(function () { });
  1828. };
  1829. /** @hidden */
  1830. GLTFLoader.prototype._loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
  1831. var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
  1832. switch (alphaMode) {
  1833. case "OPAQUE" /* OPAQUE */: {
  1834. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1835. break;
  1836. }
  1837. case "MASK" /* MASK */: {
  1838. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  1839. babylonMaterial.alphaCutOff = (material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff);
  1840. if (babylonMaterial.albedoTexture) {
  1841. babylonMaterial.albedoTexture.hasAlpha = true;
  1842. }
  1843. break;
  1844. }
  1845. case "BLEND" /* BLEND */: {
  1846. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  1847. if (babylonMaterial.albedoTexture) {
  1848. babylonMaterial.albedoTexture.hasAlpha = true;
  1849. babylonMaterial.useAlphaFromAlbedoTexture = true;
  1850. }
  1851. break;
  1852. }
  1853. default: {
  1854. throw new Error(context + ": Invalid alpha mode (" + material.alphaMode + ")");
  1855. }
  1856. }
  1857. };
  1858. /** @hidden */
  1859. GLTFLoader.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  1860. var _this = this;
  1861. var promise = GLTF2.GLTFLoaderExtension._LoadTextureAsync(this, context, textureInfo, assign);
  1862. if (promise) {
  1863. return promise;
  1864. }
  1865. var texture = GLTFLoader._GetProperty(context + "/index", this._gltf.textures, textureInfo.index);
  1866. context = "#/textures/" + textureInfo.index;
  1867. var promises = new Array();
  1868. var sampler = (texture.sampler == undefined ? this._defaultSampler : GLTFLoader._GetProperty(context + "/sampler", this._gltf.samplers, texture.sampler));
  1869. var samplerData = this._loadSampler("#/samplers/" + sampler._index, sampler);
  1870. var deferred = new BABYLON.Deferred();
  1871. var babylonTexture = new BABYLON.Texture(null, this._babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, function () {
  1872. if (!_this._disposed) {
  1873. deferred.resolve();
  1874. }
  1875. }, function (message, exception) {
  1876. if (!_this._disposed) {
  1877. deferred.reject(new Error(context + ": " + ((exception && exception.message) ? exception.message : message || "Failed to load texture")));
  1878. }
  1879. });
  1880. promises.push(deferred.promise);
  1881. babylonTexture.name = texture.name || "texture" + texture._index;
  1882. babylonTexture.wrapU = samplerData.wrapU;
  1883. babylonTexture.wrapV = samplerData.wrapV;
  1884. babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
  1885. var image = GLTFLoader._GetProperty(context + "/source", this._gltf.images, texture.source);
  1886. promises.push(this._loadImageAsync("#/images/" + image._index, image).then(function (blob) {
  1887. var dataUrl = "data:" + _this._rootUrl + (image.uri || "image" + image._index);
  1888. babylonTexture.updateURL(dataUrl, blob);
  1889. }));
  1890. assign(babylonTexture);
  1891. this.onTextureLoadedObservable.notifyObservers(babylonTexture);
  1892. return Promise.all(promises).then(function () { });
  1893. };
  1894. GLTFLoader.prototype._loadSampler = function (context, sampler) {
  1895. if (!sampler._data) {
  1896. sampler._data = {
  1897. noMipMaps: (sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */),
  1898. samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler.magFilter, sampler.minFilter),
  1899. wrapU: GLTFLoader._GetTextureWrapMode(context, sampler.wrapS),
  1900. wrapV: GLTFLoader._GetTextureWrapMode(context, sampler.wrapT)
  1901. };
  1902. }
  1903. ;
  1904. return sampler._data;
  1905. };
  1906. GLTFLoader.prototype._loadImageAsync = function (context, image) {
  1907. if (image._blob) {
  1908. return image._blob;
  1909. }
  1910. var promise;
  1911. if (image.uri) {
  1912. promise = this._loadUriAsync(context, image.uri);
  1913. }
  1914. else {
  1915. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, image.bufferView);
  1916. promise = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView);
  1917. }
  1918. image._blob = promise.then(function (data) {
  1919. return new Blob([data], { type: image.mimeType });
  1920. });
  1921. return image._blob;
  1922. };
  1923. /** @hidden */
  1924. GLTFLoader.prototype._loadUriAsync = function (context, uri) {
  1925. var _this = this;
  1926. var promise = GLTF2.GLTFLoaderExtension._LoadUriAsync(this, context, uri);
  1927. if (promise) {
  1928. return promise;
  1929. }
  1930. if (!GLTFLoader._ValidateUri(uri)) {
  1931. throw new Error(context + ": Uri '" + uri + "' is invalid");
  1932. }
  1933. if (BABYLON.Tools.IsBase64(uri)) {
  1934. return Promise.resolve(new Uint8Array(BABYLON.Tools.DecodeBase64(uri)));
  1935. }
  1936. return this.preprocessUrlAsync(this._rootUrl + uri).then(function (url) {
  1937. return new Promise(function (resolve, reject) {
  1938. if (!_this._disposed) {
  1939. var request_1 = BABYLON.Tools.LoadFile(url, function (data) {
  1940. if (!_this._disposed) {
  1941. resolve(new Uint8Array(data));
  1942. }
  1943. }, function (event) {
  1944. if (!_this._disposed) {
  1945. try {
  1946. if (request_1 && _this._state === BABYLON.GLTFLoaderState.LOADING) {
  1947. request_1._lengthComputable = event.lengthComputable;
  1948. request_1._loaded = event.loaded;
  1949. request_1._total = event.total;
  1950. _this._onProgress();
  1951. }
  1952. }
  1953. catch (e) {
  1954. reject(e);
  1955. }
  1956. }
  1957. }, _this._babylonScene.database, true, function (request, exception) {
  1958. if (!_this._disposed) {
  1959. reject(new BABYLON.LoadFileError(context + ": Failed to load '" + uri + "'" + (request ? ": " + request.status + " " + request.statusText : ""), request));
  1960. }
  1961. });
  1962. _this._requests.push(request_1);
  1963. }
  1964. });
  1965. });
  1966. };
  1967. GLTFLoader.prototype._onProgress = function () {
  1968. if (!this._progressCallback) {
  1969. return;
  1970. }
  1971. var lengthComputable = true;
  1972. var loaded = 0;
  1973. var total = 0;
  1974. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  1975. var request = _a[_i];
  1976. if (request._lengthComputable === undefined || request._loaded === undefined || request._total === undefined) {
  1977. return;
  1978. }
  1979. lengthComputable = lengthComputable && request._lengthComputable;
  1980. loaded += request._loaded;
  1981. total += request._total;
  1982. }
  1983. this._progressCallback(new BABYLON.SceneLoaderProgressEvent(lengthComputable, loaded, lengthComputable ? total : 0));
  1984. };
  1985. /** @hidden */
  1986. GLTFLoader._GetProperty = function (context, array, index) {
  1987. if (!array || index == undefined || !array[index]) {
  1988. throw new Error(context + ": Failed to find index (" + index + ")");
  1989. }
  1990. return array[index];
  1991. };
  1992. GLTFLoader._GetTextureWrapMode = function (context, mode) {
  1993. // Set defaults if undefined
  1994. mode = mode == undefined ? 10497 /* REPEAT */ : mode;
  1995. switch (mode) {
  1996. case 33071 /* CLAMP_TO_EDGE */: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  1997. case 33648 /* MIRRORED_REPEAT */: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  1998. case 10497 /* REPEAT */: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1999. default:
  2000. BABYLON.Tools.Warn(context + ": Invalid texture wrap mode (" + mode + ")");
  2001. return BABYLON.Texture.WRAP_ADDRESSMODE;
  2002. }
  2003. };
  2004. GLTFLoader._GetTextureSamplingMode = function (context, magFilter, minFilter) {
  2005. // Set defaults if undefined
  2006. magFilter = magFilter == undefined ? 9729 /* LINEAR */ : magFilter;
  2007. minFilter = minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : minFilter;
  2008. if (magFilter === 9729 /* LINEAR */) {
  2009. switch (minFilter) {
  2010. case 9728 /* NEAREST */: return BABYLON.Texture.LINEAR_NEAREST;
  2011. case 9729 /* LINEAR */: return BABYLON.Texture.LINEAR_LINEAR;
  2012. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_NEAREST_MIPNEAREST;
  2013. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_LINEAR_MIPNEAREST;
  2014. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_NEAREST_MIPLINEAR;
  2015. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  2016. default:
  2017. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  2018. return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  2019. }
  2020. }
  2021. else {
  2022. if (magFilter !== 9728 /* NEAREST */) {
  2023. BABYLON.Tools.Warn(context + ": Invalid texture magnification filter (" + magFilter + ")");
  2024. }
  2025. switch (minFilter) {
  2026. case 9728 /* NEAREST */: return BABYLON.Texture.NEAREST_NEAREST;
  2027. case 9729 /* LINEAR */: return BABYLON.Texture.NEAREST_LINEAR;
  2028. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  2029. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_LINEAR_MIPNEAREST;
  2030. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_NEAREST_MIPLINEAR;
  2031. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_LINEAR_MIPLINEAR;
  2032. default:
  2033. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  2034. return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  2035. }
  2036. }
  2037. };
  2038. GLTFLoader._GetTypedArray = function (context, componentType, bufferView, byteOffset, length) {
  2039. var buffer = bufferView.buffer;
  2040. byteOffset = bufferView.byteOffset + (byteOffset || 0);
  2041. try {
  2042. switch (componentType) {
  2043. case 5120 /* BYTE */: return new Int8Array(buffer, byteOffset, length);
  2044. case 5121 /* UNSIGNED_BYTE */: return new Uint8Array(buffer, byteOffset, length);
  2045. case 5122 /* SHORT */: return new Int16Array(buffer, byteOffset, length);
  2046. case 5123 /* UNSIGNED_SHORT */: return new Uint16Array(buffer, byteOffset, length);
  2047. case 5125 /* UNSIGNED_INT */: return new Uint32Array(buffer, byteOffset, length);
  2048. case 5126 /* FLOAT */: return new Float32Array(buffer, byteOffset, length);
  2049. default: throw new Error("Invalid component type " + componentType);
  2050. }
  2051. }
  2052. catch (e) {
  2053. throw new Error(context + ": " + e);
  2054. }
  2055. };
  2056. GLTFLoader._GetNumComponents = function (context, type) {
  2057. switch (type) {
  2058. case "SCALAR": return 1;
  2059. case "VEC2": return 2;
  2060. case "VEC3": return 3;
  2061. case "VEC4": return 4;
  2062. case "MAT2": return 4;
  2063. case "MAT3": return 9;
  2064. case "MAT4": return 16;
  2065. }
  2066. throw new Error(context + ": Invalid type (" + type + ")");
  2067. };
  2068. GLTFLoader._ValidateUri = function (uri) {
  2069. return (BABYLON.Tools.IsBase64(uri) || uri.indexOf("..") === -1);
  2070. };
  2071. GLTFLoader._GetDrawMode = function (context, mode) {
  2072. if (mode == undefined) {
  2073. mode = 4 /* TRIANGLES */;
  2074. }
  2075. switch (mode) {
  2076. case 0 /* POINTS */: return BABYLON.Material.PointListDrawMode;
  2077. case 1 /* LINES */: return BABYLON.Material.LineListDrawMode;
  2078. case 2 /* LINE_LOOP */: return BABYLON.Material.LineLoopDrawMode;
  2079. case 3 /* LINE_STRIP */: return BABYLON.Material.LineStripDrawMode;
  2080. case 4 /* TRIANGLES */: return BABYLON.Material.TriangleFillMode;
  2081. case 5 /* TRIANGLE_STRIP */: return BABYLON.Material.TriangleStripDrawMode;
  2082. case 6 /* TRIANGLE_FAN */: return BABYLON.Material.TriangleFanDrawMode;
  2083. }
  2084. throw new Error(context + ": Invalid mesh primitive mode (" + mode + ")");
  2085. };
  2086. GLTFLoader.prototype._compileMaterialsAsync = function () {
  2087. var promises = new Array();
  2088. if (this._gltf.materials) {
  2089. for (var _i = 0, _a = this._gltf.materials; _i < _a.length; _i++) {
  2090. var material = _a[_i];
  2091. if (material._babylonData) {
  2092. for (var babylonDrawMode in material._babylonData) {
  2093. var babylonData = material._babylonData[babylonDrawMode];
  2094. for (var _b = 0, _c = babylonData.meshes; _b < _c.length; _b++) {
  2095. var babylonMesh = _c[_b];
  2096. // Ensure nonUniformScaling is set if necessary.
  2097. babylonMesh.computeWorldMatrix(true);
  2098. var babylonMaterial = babylonData.material;
  2099. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
  2100. if (this.useClipPlane) {
  2101. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
  2102. }
  2103. }
  2104. }
  2105. }
  2106. }
  2107. }
  2108. return Promise.all(promises).then(function () { });
  2109. };
  2110. GLTFLoader.prototype._compileShadowGeneratorsAsync = function () {
  2111. var promises = new Array();
  2112. var lights = this._babylonScene.lights;
  2113. for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
  2114. var light = lights_1[_i];
  2115. var generator = light.getShadowGenerator();
  2116. if (generator) {
  2117. promises.push(generator.forceCompilationAsync());
  2118. }
  2119. }
  2120. return Promise.all(promises).then(function () { });
  2121. };
  2122. GLTFLoader.prototype._clear = function () {
  2123. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  2124. var request = _a[_i];
  2125. request.abort();
  2126. }
  2127. this._requests.length = 0;
  2128. delete this._gltf;
  2129. delete this._babylonScene;
  2130. this._completePromises.length = 0;
  2131. this._onReadyObservable.clear();
  2132. for (var name_4 in this._extensions) {
  2133. this._extensions[name_4].dispose();
  2134. }
  2135. this._extensions = {};
  2136. delete this._rootBabylonMesh;
  2137. delete this._progressCallback;
  2138. this.onMeshLoadedObservable.clear();
  2139. this.onTextureLoadedObservable.clear();
  2140. this.onMaterialLoadedObservable.clear();
  2141. this.onCameraLoadedObservable.clear();
  2142. };
  2143. /** @hidden */
  2144. GLTFLoader.prototype._applyExtensions = function (actionAsync) {
  2145. for (var _i = 0, _a = GLTFLoader._ExtensionNames; _i < _a.length; _i++) {
  2146. var name_5 = _a[_i];
  2147. var extension = this._extensions[name_5];
  2148. if (extension.enabled) {
  2149. var promise = actionAsync(extension);
  2150. if (promise) {
  2151. return promise;
  2152. }
  2153. }
  2154. }
  2155. return null;
  2156. };
  2157. GLTFLoader._ExtensionNames = new Array();
  2158. GLTFLoader._ExtensionFactories = {};
  2159. return GLTFLoader;
  2160. }());
  2161. GLTF2.GLTFLoader = GLTFLoader;
  2162. BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = function () { return new GLTFLoader(); };
  2163. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2164. })(BABYLON || (BABYLON = {}));
  2165. //# sourceMappingURL=babylon.glTFLoader.js.map
  2166. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2167. var BABYLON;
  2168. (function (BABYLON) {
  2169. var GLTF2;
  2170. (function (GLTF2) {
  2171. /**
  2172. * Abstract class that can be implemented to extend existing glTF loader behavior.
  2173. */
  2174. var GLTFLoaderExtension = /** @class */ (function () {
  2175. function GLTFLoaderExtension(loader) {
  2176. this.enabled = true;
  2177. this._loader = loader;
  2178. }
  2179. GLTFLoaderExtension.prototype.dispose = function () {
  2180. delete this._loader;
  2181. };
  2182. // #region Overridable Methods
  2183. /** Override this method to modify the default behavior for loading scenes. */
  2184. GLTFLoaderExtension.prototype._loadSceneAsync = function (context, node) { return null; };
  2185. /** Override this method to modify the default behavior for loading nodes. */
  2186. GLTFLoaderExtension.prototype._loadNodeAsync = function (context, node) { return null; };
  2187. /** Override this method to modify the default behavior for loading mesh primitive vertex data. */
  2188. GLTFLoaderExtension.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) { return null; };
  2189. /** Override this method to modify the default behavior for loading materials. */
  2190. GLTFLoaderExtension.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) { return null; };
  2191. /** Override this method to modify the default behavior for loading textures. */
  2192. GLTFLoaderExtension.prototype._loadTextureAsync = function (context, textureInfo, assign) { return null; };
  2193. /** Override this method to modify the default behavior for loading uris. */
  2194. GLTFLoaderExtension.prototype._loadUriAsync = function (context, uri) { return null; };
  2195. // #endregion
  2196. /** Helper method called by a loader extension to load an glTF extension. */
  2197. GLTFLoaderExtension.prototype._loadExtensionAsync = function (context, property, actionAsync) {
  2198. if (!property.extensions) {
  2199. return null;
  2200. }
  2201. var extensions = property.extensions;
  2202. var extension = extensions[this.name];
  2203. if (!extension) {
  2204. return null;
  2205. }
  2206. // Clear out the extension before executing the action to avoid recursing into the same property.
  2207. delete extensions[this.name];
  2208. try {
  2209. return actionAsync(context + "/extensions/" + this.name, extension);
  2210. }
  2211. finally {
  2212. // Restore the extension after executing the action.
  2213. extensions[this.name] = extension;
  2214. }
  2215. };
  2216. /** Helper method called by the loader to allow extensions to override loading scenes. */
  2217. GLTFLoaderExtension._LoadSceneAsync = function (loader, context, scene) {
  2218. return loader._applyExtensions(function (extension) { return extension._loadSceneAsync(context, scene); });
  2219. };
  2220. /** Helper method called by the loader to allow extensions to override loading nodes. */
  2221. GLTFLoaderExtension._LoadNodeAsync = function (loader, context, node) {
  2222. return loader._applyExtensions(function (extension) { return extension._loadNodeAsync(context, node); });
  2223. };
  2224. /** Helper method called by the loader to allow extensions to override loading mesh primitive vertex data. */
  2225. GLTFLoaderExtension._LoadVertexDataAsync = function (loader, context, primitive, babylonMesh) {
  2226. return loader._applyExtensions(function (extension) { return extension._loadVertexDataAsync(context, primitive, babylonMesh); });
  2227. };
  2228. /** Helper method called by the loader to allow extensions to override loading materials. */
  2229. GLTFLoaderExtension._LoadMaterialAsync = function (loader, context, material, babylonMesh, babylonDrawMode, assign) {
  2230. return loader._applyExtensions(function (extension) { return extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
  2231. };
  2232. /** Helper method called by the loader to allow extensions to override loading textures. */
  2233. GLTFLoaderExtension._LoadTextureAsync = function (loader, context, textureInfo, assign) {
  2234. return loader._applyExtensions(function (extension) { return extension._loadTextureAsync(context, textureInfo, assign); });
  2235. };
  2236. /** Helper method called by the loader to allow extensions to override loading uris. */
  2237. GLTFLoaderExtension._LoadUriAsync = function (loader, context, uri) {
  2238. return loader._applyExtensions(function (extension) { return extension._loadUriAsync(context, uri); });
  2239. };
  2240. return GLTFLoaderExtension;
  2241. }());
  2242. GLTF2.GLTFLoaderExtension = GLTFLoaderExtension;
  2243. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2244. })(BABYLON || (BABYLON = {}));
  2245. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  2246. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2247. var __extends = (this && this.__extends) || (function () {
  2248. var extendStatics = Object.setPrototypeOf ||
  2249. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2250. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2251. return function (d, b) {
  2252. extendStatics(d, b);
  2253. function __() { this.constructor = d; }
  2254. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2255. };
  2256. })();
  2257. var BABYLON;
  2258. (function (BABYLON) {
  2259. var GLTF2;
  2260. (function (GLTF2) {
  2261. var Extensions;
  2262. (function (Extensions) {
  2263. var NAME = "MSFT_lod";
  2264. /**
  2265. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/MSFT_lod)
  2266. */
  2267. var MSFT_lod = /** @class */ (function (_super) {
  2268. __extends(MSFT_lod, _super);
  2269. function MSFT_lod(loader) {
  2270. var _this = _super.call(this, loader) || this;
  2271. _this.name = NAME;
  2272. /**
  2273. * Maximum number of LODs to load, starting from the lowest LOD.
  2274. */
  2275. _this.maxLODsToLoad = Number.MAX_VALUE;
  2276. /**
  2277. * Observable raised when all node LODs of one level are loaded.
  2278. * The event data is the index of the loaded LOD starting from zero.
  2279. * Dispose the loader to cancel the loading of the next level of LODs.
  2280. */
  2281. _this.onNodeLODsLoadedObservable = new BABYLON.Observable();
  2282. /**
  2283. * Observable raised when all material LODs of one level are loaded.
  2284. * The event data is the index of the loaded LOD starting from zero.
  2285. * Dispose the loader to cancel the loading of the next level of LODs.
  2286. */
  2287. _this.onMaterialLODsLoadedObservable = new BABYLON.Observable();
  2288. _this._loadingNodeLOD = null;
  2289. _this._loadNodeSignals = {};
  2290. _this._loadNodePromises = new Array();
  2291. _this._loadingMaterialLOD = null;
  2292. _this._loadMaterialSignals = {};
  2293. _this._loadMaterialPromises = new Array();
  2294. _this._loader._onReadyObservable.addOnce(function () {
  2295. var _loop_1 = function (indexLOD) {
  2296. Promise.all(_this._loadNodePromises[indexLOD]).then(function () {
  2297. _this.onNodeLODsLoadedObservable.notifyObservers(indexLOD);
  2298. });
  2299. };
  2300. for (var indexLOD = 0; indexLOD < _this._loadNodePromises.length; indexLOD++) {
  2301. _loop_1(indexLOD);
  2302. }
  2303. var _loop_2 = function (indexLOD) {
  2304. Promise.all(_this._loadMaterialPromises[indexLOD]).then(function () {
  2305. _this.onMaterialLODsLoadedObservable.notifyObservers(indexLOD);
  2306. });
  2307. };
  2308. for (var indexLOD = 0; indexLOD < _this._loadMaterialPromises.length; indexLOD++) {
  2309. _loop_2(indexLOD);
  2310. }
  2311. });
  2312. return _this;
  2313. }
  2314. MSFT_lod.prototype.dispose = function () {
  2315. _super.prototype.dispose.call(this);
  2316. this._loadingNodeLOD = null;
  2317. this._loadNodeSignals = {};
  2318. this._loadingMaterialLOD = null;
  2319. this._loadMaterialSignals = {};
  2320. this.onMaterialLODsLoadedObservable.clear();
  2321. this.onNodeLODsLoadedObservable.clear();
  2322. };
  2323. MSFT_lod.prototype._loadNodeAsync = function (context, node) {
  2324. var _this = this;
  2325. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  2326. var firstPromise;
  2327. var nodeLODs = _this._getLODs(extensionContext, node, _this._loader._gltf.nodes, extension.ids);
  2328. var _loop_3 = function (indexLOD) {
  2329. var nodeLOD = nodeLODs[indexLOD];
  2330. if (indexLOD !== 0) {
  2331. _this._loadingNodeLOD = nodeLOD;
  2332. if (!_this._loadNodeSignals[nodeLOD._index]) {
  2333. _this._loadNodeSignals[nodeLOD._index] = new BABYLON.Deferred();
  2334. }
  2335. }
  2336. var promise = _this._loader._loadNodeAsync("#/nodes/" + nodeLOD._index, nodeLOD).then(function () {
  2337. if (indexLOD !== 0) {
  2338. var previousNodeLOD = nodeLODs[indexLOD - 1];
  2339. if (previousNodeLOD._babylonMesh) {
  2340. previousNodeLOD._babylonMesh.dispose(false, true);
  2341. delete previousNodeLOD._babylonMesh;
  2342. }
  2343. }
  2344. if (indexLOD !== nodeLODs.length - 1) {
  2345. var nodeIndex = nodeLODs[indexLOD + 1]._index;
  2346. if (_this._loadNodeSignals[nodeIndex]) {
  2347. _this._loadNodeSignals[nodeIndex].resolve();
  2348. delete _this._loadNodeSignals[nodeIndex];
  2349. }
  2350. }
  2351. });
  2352. if (indexLOD === 0) {
  2353. firstPromise = promise;
  2354. }
  2355. else {
  2356. _this._loader._completePromises.push(promise);
  2357. _this._loadingNodeLOD = null;
  2358. }
  2359. _this._loadNodePromises[indexLOD] = _this._loadNodePromises[indexLOD] || [];
  2360. _this._loadNodePromises[indexLOD].push(promise);
  2361. };
  2362. for (var indexLOD = 0; indexLOD < nodeLODs.length; indexLOD++) {
  2363. _loop_3(indexLOD);
  2364. }
  2365. return firstPromise;
  2366. });
  2367. };
  2368. MSFT_lod.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2369. var _this = this;
  2370. // Don't load material LODs if already loading a node LOD.
  2371. if (this._loadingNodeLOD) {
  2372. return null;
  2373. }
  2374. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  2375. var firstPromise;
  2376. var materialLODs = _this._getLODs(extensionContext, material, _this._loader._gltf.materials, extension.ids);
  2377. var _loop_4 = function (indexLOD) {
  2378. var materialLOD = materialLODs[indexLOD];
  2379. if (indexLOD !== 0) {
  2380. _this._loadingMaterialLOD = materialLOD;
  2381. if (!_this._loadMaterialSignals[materialLOD._index]) {
  2382. _this._loadMaterialSignals[materialLOD._index] = new BABYLON.Deferred();
  2383. }
  2384. }
  2385. var promise = _this._loader._loadMaterialAsync("#/materials/" + materialLOD._index, materialLOD, babylonMesh, babylonDrawMode, indexLOD === 0 ? assign : function () { }).then(function () {
  2386. if (indexLOD !== 0) {
  2387. var babylonDataLOD = materialLOD._babylonData;
  2388. assign(babylonDataLOD[babylonDrawMode].material);
  2389. var previousBabylonDataLOD = materialLODs[indexLOD - 1]._babylonData;
  2390. if (previousBabylonDataLOD[babylonDrawMode]) {
  2391. previousBabylonDataLOD[babylonDrawMode].material.dispose();
  2392. delete previousBabylonDataLOD[babylonDrawMode];
  2393. }
  2394. }
  2395. if (indexLOD !== materialLODs.length - 1) {
  2396. var materialIndex = materialLODs[indexLOD + 1]._index;
  2397. if (_this._loadMaterialSignals[materialIndex]) {
  2398. _this._loadMaterialSignals[materialIndex].resolve();
  2399. delete _this._loadMaterialSignals[materialIndex];
  2400. }
  2401. }
  2402. });
  2403. if (indexLOD === 0) {
  2404. firstPromise = promise;
  2405. }
  2406. else {
  2407. _this._loader._completePromises.push(promise);
  2408. _this._loadingMaterialLOD = null;
  2409. }
  2410. _this._loadMaterialPromises[indexLOD] = _this._loadMaterialPromises[indexLOD] || [];
  2411. _this._loadMaterialPromises[indexLOD].push(promise);
  2412. };
  2413. for (var indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  2414. _loop_4(indexLOD);
  2415. }
  2416. return firstPromise;
  2417. });
  2418. };
  2419. MSFT_lod.prototype._loadUriAsync = function (context, uri) {
  2420. var _this = this;
  2421. // Defer the loading of uris if loading a material or node LOD.
  2422. if (this._loadingMaterialLOD) {
  2423. var index = this._loadingMaterialLOD._index;
  2424. return this._loadMaterialSignals[index].promise.then(function () {
  2425. return _this._loader._loadUriAsync(context, uri);
  2426. });
  2427. }
  2428. else if (this._loadingNodeLOD) {
  2429. var index = this._loadingNodeLOD._index;
  2430. return this._loadNodeSignals[index].promise.then(function () {
  2431. return _this._loader._loadUriAsync(context, uri);
  2432. });
  2433. }
  2434. return null;
  2435. };
  2436. /**
  2437. * Gets an array of LOD properties from lowest to highest.
  2438. */
  2439. MSFT_lod.prototype._getLODs = function (context, property, array, ids) {
  2440. if (this.maxLODsToLoad <= 0) {
  2441. throw new Error("maxLODsToLoad must be greater than zero");
  2442. }
  2443. var properties = new Array();
  2444. for (var i = ids.length - 1; i >= 0; i--) {
  2445. properties.push(GLTF2.GLTFLoader._GetProperty(context + "/ids/" + ids[i], array, ids[i]));
  2446. if (properties.length === this.maxLODsToLoad) {
  2447. return properties;
  2448. }
  2449. }
  2450. properties.push(property);
  2451. return properties;
  2452. };
  2453. return MSFT_lod;
  2454. }(GLTF2.GLTFLoaderExtension));
  2455. Extensions.MSFT_lod = MSFT_lod;
  2456. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_lod(loader); });
  2457. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2458. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2459. })(BABYLON || (BABYLON = {}));
  2460. //# sourceMappingURL=MSFT_lod.js.map
  2461. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2462. var __extends = (this && this.__extends) || (function () {
  2463. var extendStatics = Object.setPrototypeOf ||
  2464. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2465. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2466. return function (d, b) {
  2467. extendStatics(d, b);
  2468. function __() { this.constructor = d; }
  2469. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2470. };
  2471. })();
  2472. var BABYLON;
  2473. (function (BABYLON) {
  2474. var GLTF2;
  2475. (function (GLTF2) {
  2476. var Extensions;
  2477. (function (Extensions) {
  2478. var NAME = "MSFT_minecraftMesh";
  2479. /** @hidden */
  2480. var MSFT_minecraftMesh = /** @class */ (function (_super) {
  2481. __extends(MSFT_minecraftMesh, _super);
  2482. function MSFT_minecraftMesh(loader) {
  2483. var _this = _super.call(this, loader) || this;
  2484. _this.name = NAME;
  2485. _this._onMaterialLoaded = function (material) {
  2486. if (material.needAlphaBlending()) {
  2487. material.forceDepthWrite = true;
  2488. material.separateCullingPass = true;
  2489. }
  2490. material.backFaceCulling = material.forceDepthWrite;
  2491. material.twoSidedLighting = true;
  2492. };
  2493. var meshes = loader._gltf.meshes;
  2494. if (meshes && meshes.length) {
  2495. for (var _i = 0, meshes_1 = meshes; _i < meshes_1.length; _i++) {
  2496. var mesh = meshes_1[_i];
  2497. if (mesh && mesh.extras && mesh.extras.MSFT_minecraftMesh) {
  2498. _this._loader.onMaterialLoadedObservable.add(_this._onMaterialLoaded);
  2499. break;
  2500. }
  2501. }
  2502. }
  2503. return _this;
  2504. }
  2505. return MSFT_minecraftMesh;
  2506. }(GLTF2.GLTFLoaderExtension));
  2507. Extensions.MSFT_minecraftMesh = MSFT_minecraftMesh;
  2508. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_minecraftMesh(loader); });
  2509. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2510. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2511. })(BABYLON || (BABYLON = {}));
  2512. //# sourceMappingURL=MSFT_minecraftMesh.js.map
  2513. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2514. var __extends = (this && this.__extends) || (function () {
  2515. var extendStatics = Object.setPrototypeOf ||
  2516. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2517. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2518. return function (d, b) {
  2519. extendStatics(d, b);
  2520. function __() { this.constructor = d; }
  2521. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2522. };
  2523. })();
  2524. var BABYLON;
  2525. (function (BABYLON) {
  2526. var GLTF2;
  2527. (function (GLTF2) {
  2528. var Extensions;
  2529. (function (Extensions) {
  2530. var NAME = "MSFT_sRGBFactors";
  2531. /** @hidden */
  2532. var MSFT_sRGBFactors = /** @class */ (function (_super) {
  2533. __extends(MSFT_sRGBFactors, _super);
  2534. function MSFT_sRGBFactors(loader) {
  2535. var _this = _super.call(this, loader) || this;
  2536. _this.name = NAME;
  2537. _this._onMaterialLoaded = function (material) {
  2538. if (!material.albedoTexture) {
  2539. material.albedoColor.toLinearSpaceToRef(material.albedoColor);
  2540. }
  2541. if (!material.reflectivityTexture) {
  2542. material.reflectivityColor.toLinearSpaceToRef(material.reflectivityColor);
  2543. }
  2544. };
  2545. var materials = loader._gltf.materials;
  2546. if (materials && materials.length) {
  2547. for (var _i = 0, materials_1 = materials; _i < materials_1.length; _i++) {
  2548. var material = materials_1[_i];
  2549. if (material && material.extras && material.extras.MSFT_sRGBFactors) {
  2550. _this._loader.onMaterialLoadedObservable.add(_this._onMaterialLoaded);
  2551. break;
  2552. }
  2553. }
  2554. }
  2555. return _this;
  2556. }
  2557. return MSFT_sRGBFactors;
  2558. }(GLTF2.GLTFLoaderExtension));
  2559. Extensions.MSFT_sRGBFactors = MSFT_sRGBFactors;
  2560. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_sRGBFactors(loader); });
  2561. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2562. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2563. })(BABYLON || (BABYLON = {}));
  2564. //# sourceMappingURL=MSFT_sRGBFactors.js.map
  2565. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2566. var __extends = (this && this.__extends) || (function () {
  2567. var extendStatics = Object.setPrototypeOf ||
  2568. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2569. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2570. return function (d, b) {
  2571. extendStatics(d, b);
  2572. function __() { this.constructor = d; }
  2573. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2574. };
  2575. })();
  2576. var BABYLON;
  2577. (function (BABYLON) {
  2578. var GLTF2;
  2579. (function (GLTF2) {
  2580. var Extensions;
  2581. (function (Extensions) {
  2582. var NAME = "KHR_draco_mesh_compression";
  2583. /**
  2584. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression)
  2585. */
  2586. var KHR_draco_mesh_compression = /** @class */ (function (_super) {
  2587. __extends(KHR_draco_mesh_compression, _super);
  2588. function KHR_draco_mesh_compression(loader) {
  2589. var _this = _super.call(this, loader) || this;
  2590. _this.name = NAME;
  2591. _this._dracoCompression = null;
  2592. // Disable extension if decoder is not available.
  2593. if (!BABYLON.DracoCompression.DecoderAvailable) {
  2594. _this.enabled = false;
  2595. }
  2596. return _this;
  2597. }
  2598. KHR_draco_mesh_compression.prototype.dispose = function () {
  2599. if (this._dracoCompression) {
  2600. this._dracoCompression.dispose();
  2601. }
  2602. _super.prototype.dispose.call(this);
  2603. };
  2604. KHR_draco_mesh_compression.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  2605. var _this = this;
  2606. return this._loadExtensionAsync(context, primitive, function (extensionContext, extension) {
  2607. if (primitive.mode != undefined) {
  2608. if (primitive.mode !== 5 /* TRIANGLE_STRIP */ &&
  2609. primitive.mode !== 4 /* TRIANGLES */) {
  2610. throw new Error(context + ": Unsupported mode " + primitive.mode);
  2611. }
  2612. // TODO: handle triangle strips
  2613. if (primitive.mode === 5 /* TRIANGLE_STRIP */) {
  2614. throw new Error(context + ": Mode " + primitive.mode + " is not currently supported");
  2615. }
  2616. }
  2617. var attributes = {};
  2618. var loadAttribute = function (name, kind) {
  2619. var uniqueId = extension.attributes[name];
  2620. if (uniqueId == undefined) {
  2621. return;
  2622. }
  2623. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  2624. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  2625. babylonMesh._delayInfo.push(kind);
  2626. }
  2627. attributes[kind] = uniqueId;
  2628. };
  2629. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  2630. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  2631. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  2632. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  2633. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  2634. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  2635. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  2636. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind);
  2637. var bufferView = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._loader._gltf.bufferViews, extension.bufferView);
  2638. if (!bufferView._dracoBabylonGeometry) {
  2639. bufferView._dracoBabylonGeometry = _this._loader._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  2640. if (!_this._dracoCompression) {
  2641. _this._dracoCompression = new BABYLON.DracoCompression();
  2642. }
  2643. return _this._dracoCompression.decodeMeshAsync(data, attributes).then(function (babylonVertexData) {
  2644. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, _this._loader._babylonScene);
  2645. babylonVertexData.applyToGeometry(babylonGeometry);
  2646. return babylonGeometry;
  2647. }).catch(function (error) {
  2648. throw new Error(context + ": " + error.message);
  2649. });
  2650. });
  2651. }
  2652. return bufferView._dracoBabylonGeometry;
  2653. });
  2654. };
  2655. return KHR_draco_mesh_compression;
  2656. }(GLTF2.GLTFLoaderExtension));
  2657. Extensions.KHR_draco_mesh_compression = KHR_draco_mesh_compression;
  2658. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_draco_mesh_compression(loader); });
  2659. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2660. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2661. })(BABYLON || (BABYLON = {}));
  2662. //# sourceMappingURL=KHR_draco_mesh_compression.js.map
  2663. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2664. var __extends = (this && this.__extends) || (function () {
  2665. var extendStatics = Object.setPrototypeOf ||
  2666. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2667. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2668. return function (d, b) {
  2669. extendStatics(d, b);
  2670. function __() { this.constructor = d; }
  2671. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2672. };
  2673. })();
  2674. var BABYLON;
  2675. (function (BABYLON) {
  2676. var GLTF2;
  2677. (function (GLTF2) {
  2678. var Extensions;
  2679. (function (Extensions) {
  2680. var NAME = "KHR_materials_pbrSpecularGlossiness";
  2681. /**
  2682. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness)
  2683. */
  2684. var KHR_materials_pbrSpecularGlossiness = /** @class */ (function (_super) {
  2685. __extends(KHR_materials_pbrSpecularGlossiness, _super);
  2686. function KHR_materials_pbrSpecularGlossiness() {
  2687. var _this = _super !== null && _super.apply(this, arguments) || this;
  2688. _this.name = NAME;
  2689. return _this;
  2690. }
  2691. KHR_materials_pbrSpecularGlossiness.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2692. var _this = this;
  2693. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  2694. material._babylonData = material._babylonData || {};
  2695. var babylonData = material._babylonData[babylonDrawMode];
  2696. if (!babylonData) {
  2697. var promises = new Array();
  2698. var name_1 = material.name || "materialSG_" + material._index;
  2699. var babylonMaterial = _this._loader._createMaterial(name_1, babylonDrawMode);
  2700. promises.push(_this._loader._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  2701. promises.push(_this._loadSpecularGlossinessPropertiesAsync(extensionContext, material, extension, babylonMaterial));
  2702. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  2703. babylonData = {
  2704. material: babylonMaterial,
  2705. meshes: [],
  2706. loaded: Promise.all(promises).then(function () { })
  2707. };
  2708. material._babylonData[babylonDrawMode] = babylonData;
  2709. }
  2710. babylonData.meshes.push(babylonMesh);
  2711. assign(babylonData.material);
  2712. return babylonData.loaded;
  2713. });
  2714. };
  2715. KHR_materials_pbrSpecularGlossiness.prototype._loadSpecularGlossinessPropertiesAsync = function (context, material, properties, babylonMaterial) {
  2716. var promises = new Array();
  2717. if (properties.diffuseFactor) {
  2718. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.diffuseFactor);
  2719. babylonMaterial.alpha = properties.diffuseFactor[3];
  2720. }
  2721. else {
  2722. babylonMaterial.albedoColor = BABYLON.Color3.White();
  2723. }
  2724. babylonMaterial.reflectivityColor = properties.specularFactor ? BABYLON.Color3.FromArray(properties.specularFactor) : BABYLON.Color3.White();
  2725. babylonMaterial.microSurface = properties.glossinessFactor == undefined ? 1 : properties.glossinessFactor;
  2726. if (properties.diffuseTexture) {
  2727. promises.push(this._loader._loadTextureAsync(context + "/diffuseTexture", properties.diffuseTexture, function (texture) {
  2728. babylonMaterial.albedoTexture = texture;
  2729. }));
  2730. }
  2731. if (properties.specularGlossinessTexture) {
  2732. promises.push(this._loader._loadTextureAsync(context + "/specularGlossinessTexture", properties.specularGlossinessTexture, function (texture) {
  2733. babylonMaterial.reflectivityTexture = texture;
  2734. }));
  2735. babylonMaterial.reflectivityTexture.hasAlpha = true;
  2736. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  2737. }
  2738. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  2739. return Promise.all(promises).then(function () { });
  2740. };
  2741. return KHR_materials_pbrSpecularGlossiness;
  2742. }(GLTF2.GLTFLoaderExtension));
  2743. Extensions.KHR_materials_pbrSpecularGlossiness = KHR_materials_pbrSpecularGlossiness;
  2744. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_pbrSpecularGlossiness(loader); });
  2745. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2746. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2747. })(BABYLON || (BABYLON = {}));
  2748. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map
  2749. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2750. var __extends = (this && this.__extends) || (function () {
  2751. var extendStatics = Object.setPrototypeOf ||
  2752. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2753. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2754. return function (d, b) {
  2755. extendStatics(d, b);
  2756. function __() { this.constructor = d; }
  2757. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2758. };
  2759. })();
  2760. var BABYLON;
  2761. (function (BABYLON) {
  2762. var GLTF2;
  2763. (function (GLTF2) {
  2764. var Extensions;
  2765. (function (Extensions) {
  2766. var NAME = "KHR_materials_unlit";
  2767. /**
  2768. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit)
  2769. */
  2770. var KHR_materials_unlit = /** @class */ (function (_super) {
  2771. __extends(KHR_materials_unlit, _super);
  2772. function KHR_materials_unlit() {
  2773. var _this = _super !== null && _super.apply(this, arguments) || this;
  2774. _this.name = NAME;
  2775. return _this;
  2776. }
  2777. KHR_materials_unlit.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2778. var _this = this;
  2779. return this._loadExtensionAsync(context, material, function () {
  2780. material._babylonData = material._babylonData || {};
  2781. var babylonData = material._babylonData[babylonDrawMode];
  2782. if (!babylonData) {
  2783. var name_1 = material.name || "materialUnlit_" + material._index;
  2784. var babylonMaterial = _this._loader._createMaterial(name_1, babylonDrawMode);
  2785. babylonMaterial.unlit = true;
  2786. var promise = _this._loadUnlitPropertiesAsync(context, material, babylonMaterial);
  2787. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  2788. babylonData = {
  2789. material: babylonMaterial,
  2790. meshes: [],
  2791. loaded: promise
  2792. };
  2793. material._babylonData[babylonDrawMode] = babylonData;
  2794. }
  2795. babylonData.meshes.push(babylonMesh);
  2796. assign(babylonData.material);
  2797. return babylonData.loaded;
  2798. });
  2799. };
  2800. KHR_materials_unlit.prototype._loadUnlitPropertiesAsync = function (context, material, babylonMaterial) {
  2801. var promises = new Array();
  2802. // Ensure metallic workflow
  2803. babylonMaterial.metallic = 1;
  2804. babylonMaterial.roughness = 1;
  2805. var properties = material.pbrMetallicRoughness;
  2806. if (properties) {
  2807. if (properties.baseColorFactor) {
  2808. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  2809. babylonMaterial.alpha = properties.baseColorFactor[3];
  2810. }
  2811. else {
  2812. babylonMaterial.albedoColor = BABYLON.Color3.White();
  2813. }
  2814. if (properties.baseColorTexture) {
  2815. promises.push(this._loader._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  2816. babylonMaterial.albedoTexture = texture;
  2817. }));
  2818. }
  2819. }
  2820. if (material.doubleSided) {
  2821. babylonMaterial.backFaceCulling = false;
  2822. babylonMaterial.twoSidedLighting = true;
  2823. }
  2824. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  2825. return Promise.all(promises).then(function () { });
  2826. };
  2827. return KHR_materials_unlit;
  2828. }(GLTF2.GLTFLoaderExtension));
  2829. Extensions.KHR_materials_unlit = KHR_materials_unlit;
  2830. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_unlit(loader); });
  2831. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2832. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2833. })(BABYLON || (BABYLON = {}));
  2834. //# sourceMappingURL=KHR_materials_unlit.js.map
  2835. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2836. var __extends = (this && this.__extends) || (function () {
  2837. var extendStatics = Object.setPrototypeOf ||
  2838. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2839. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2840. return function (d, b) {
  2841. extendStatics(d, b);
  2842. function __() { this.constructor = d; }
  2843. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2844. };
  2845. })();
  2846. var BABYLON;
  2847. (function (BABYLON) {
  2848. var GLTF2;
  2849. (function (GLTF2) {
  2850. var Extensions;
  2851. (function (Extensions) {
  2852. var NAME = "KHR_lights";
  2853. var LightType;
  2854. (function (LightType) {
  2855. LightType["AMBIENT"] = "ambient";
  2856. LightType["DIRECTIONAL"] = "directional";
  2857. LightType["POINT"] = "point";
  2858. LightType["SPOT"] = "spot";
  2859. })(LightType || (LightType = {}));
  2860. /**
  2861. * [Specification](https://github.com/MiiBond/glTF/tree/khr_lights_v1/extensions/Khronos/KHR_lights) (Experimental)
  2862. */
  2863. var KHR_lights = /** @class */ (function (_super) {
  2864. __extends(KHR_lights, _super);
  2865. function KHR_lights() {
  2866. var _this = _super !== null && _super.apply(this, arguments) || this;
  2867. _this.name = NAME;
  2868. return _this;
  2869. }
  2870. KHR_lights.prototype._loadSceneAsync = function (context, scene) {
  2871. var _this = this;
  2872. return this._loadExtensionAsync(context, scene, function (extensionContext, extension) {
  2873. var promise = _this._loader._loadSceneAsync(extensionContext, scene);
  2874. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  2875. if (light.type !== LightType.AMBIENT) {
  2876. throw new Error(extensionContext + ": Only ambient lights are allowed on a scene");
  2877. }
  2878. _this._loader._babylonScene.ambientColor = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.Black();
  2879. return promise;
  2880. });
  2881. };
  2882. KHR_lights.prototype._loadNodeAsync = function (context, node) {
  2883. var _this = this;
  2884. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  2885. var promise = _this._loader._loadNodeAsync(extensionContext, node);
  2886. var babylonLight;
  2887. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  2888. var name = node._babylonMesh.name;
  2889. switch (light.type) {
  2890. case LightType.AMBIENT: {
  2891. throw new Error(extensionContext + ": Ambient lights are not allowed on a node");
  2892. }
  2893. case LightType.DIRECTIONAL: {
  2894. babylonLight = new BABYLON.DirectionalLight(name, BABYLON.Vector3.Forward(), _this._loader._babylonScene);
  2895. break;
  2896. }
  2897. case LightType.POINT: {
  2898. babylonLight = new BABYLON.PointLight(name, BABYLON.Vector3.Zero(), _this._loader._babylonScene);
  2899. break;
  2900. }
  2901. case LightType.SPOT: {
  2902. var spotLight = light;
  2903. // TODO: support inner and outer cone angles
  2904. //const innerConeAngle = spotLight.innerConeAngle || 0;
  2905. var outerConeAngle = spotLight.outerConeAngle || Math.PI / 4;
  2906. babylonLight = new BABYLON.SpotLight(name, BABYLON.Vector3.Zero(), BABYLON.Vector3.Forward(), outerConeAngle, 2, _this._loader._babylonScene);
  2907. break;
  2908. }
  2909. default: {
  2910. throw new Error(extensionContext + ": Invalid light type (" + light.type + ")");
  2911. }
  2912. }
  2913. babylonLight.diffuse = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.White();
  2914. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  2915. babylonLight.parent = node._babylonMesh;
  2916. return promise;
  2917. });
  2918. };
  2919. Object.defineProperty(KHR_lights.prototype, "_lights", {
  2920. get: function () {
  2921. var extensions = this._loader._gltf.extensions;
  2922. if (!extensions || !extensions[this.name]) {
  2923. throw new Error("#/extensions: '" + this.name + "' not found");
  2924. }
  2925. var extension = extensions[this.name];
  2926. return extension.lights;
  2927. },
  2928. enumerable: true,
  2929. configurable: true
  2930. });
  2931. return KHR_lights;
  2932. }(GLTF2.GLTFLoaderExtension));
  2933. Extensions.KHR_lights = KHR_lights;
  2934. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_lights(loader); });
  2935. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2936. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2937. })(BABYLON || (BABYLON = {}));
  2938. //# sourceMappingURL=KHR_lights.js.map
  2939. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2940. var __extends = (this && this.__extends) || (function () {
  2941. var extendStatics = Object.setPrototypeOf ||
  2942. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2943. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2944. return function (d, b) {
  2945. extendStatics(d, b);
  2946. function __() { this.constructor = d; }
  2947. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2948. };
  2949. })();
  2950. var BABYLON;
  2951. (function (BABYLON) {
  2952. var GLTF2;
  2953. (function (GLTF2) {
  2954. var Extensions;
  2955. (function (Extensions) {
  2956. var NAME = "KHR_texture_transform";
  2957. /**
  2958. * [Specification](https://github.com/AltspaceVR/glTF/blob/avr-sampler-offset-tile/extensions/2.0/Khronos/KHR_texture_transform/README.md) (Experimental)
  2959. */
  2960. var KHR_texture_transform = /** @class */ (function (_super) {
  2961. __extends(KHR_texture_transform, _super);
  2962. function KHR_texture_transform() {
  2963. var _this = _super !== null && _super.apply(this, arguments) || this;
  2964. _this.name = NAME;
  2965. return _this;
  2966. }
  2967. KHR_texture_transform.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  2968. var _this = this;
  2969. return this._loadExtensionAsync(context, textureInfo, function (extensionContext, extension) {
  2970. return _this._loader._loadTextureAsync(context, textureInfo, function (babylonTexture) {
  2971. if (extension.offset) {
  2972. babylonTexture.uOffset = extension.offset[0];
  2973. babylonTexture.vOffset = extension.offset[1];
  2974. }
  2975. // Always rotate around the origin.
  2976. babylonTexture.uRotationCenter = 0;
  2977. babylonTexture.vRotationCenter = 0;
  2978. if (extension.rotation) {
  2979. babylonTexture.wAng = -extension.rotation;
  2980. }
  2981. if (extension.scale) {
  2982. babylonTexture.uScale = extension.scale[0];
  2983. babylonTexture.vScale = extension.scale[1];
  2984. }
  2985. if (extension.texCoord != undefined) {
  2986. babylonTexture.coordinatesIndex = extension.texCoord;
  2987. }
  2988. assign(babylonTexture);
  2989. });
  2990. });
  2991. };
  2992. return KHR_texture_transform;
  2993. }(GLTF2.GLTFLoaderExtension));
  2994. Extensions.KHR_texture_transform = KHR_texture_transform;
  2995. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_texture_transform(loader); });
  2996. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2997. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2998. })(BABYLON || (BABYLON = {}));
  2999. //# sourceMappingURL=KHR_texture_transform.js.map