babylon.glTF2FileLoader.js 183 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory(require("babylonjs"));
  4. else if(typeof define === 'function' && define.amd)
  5. define("babylonjs-loaders", ["babylonjs"], factory);
  6. else if(typeof exports === 'object')
  7. exports["babylonjs-loaders"] = factory(require("babylonjs"));
  8. else
  9. root["LOADERS"] = factory(root["BABYLON"]);
  10. })(window, function(__WEBPACK_EXTERNAL_MODULE_babylonjs__) {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/
  15. /******/ // The require function
  16. /******/ function __webpack_require__(moduleId) {
  17. /******/
  18. /******/ // Check if module is in cache
  19. /******/ if(installedModules[moduleId]) {
  20. /******/ return installedModules[moduleId].exports;
  21. /******/ }
  22. /******/ // Create a new module (and put it into the cache)
  23. /******/ var module = installedModules[moduleId] = {
  24. /******/ i: moduleId,
  25. /******/ l: false,
  26. /******/ exports: {}
  27. /******/ };
  28. /******/
  29. /******/ // Execute the module function
  30. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  31. /******/
  32. /******/ // Flag the module as loaded
  33. /******/ module.l = true;
  34. /******/
  35. /******/ // Return the exports of the module
  36. /******/ return module.exports;
  37. /******/ }
  38. /******/
  39. /******/
  40. /******/ // expose the modules object (__webpack_modules__)
  41. /******/ __webpack_require__.m = modules;
  42. /******/
  43. /******/ // expose the module cache
  44. /******/ __webpack_require__.c = installedModules;
  45. /******/
  46. /******/ // define getter function for harmony exports
  47. /******/ __webpack_require__.d = function(exports, name, getter) {
  48. /******/ if(!__webpack_require__.o(exports, name)) {
  49. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  50. /******/ }
  51. /******/ };
  52. /******/
  53. /******/ // define __esModule on exports
  54. /******/ __webpack_require__.r = function(exports) {
  55. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  56. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  57. /******/ }
  58. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  59. /******/ };
  60. /******/
  61. /******/ // create a fake namespace object
  62. /******/ // mode & 1: value is a module id, require it
  63. /******/ // mode & 2: merge all properties of value into the ns
  64. /******/ // mode & 4: return value when already ns object
  65. /******/ // mode & 8|1: behave like require
  66. /******/ __webpack_require__.t = function(value, mode) {
  67. /******/ if(mode & 1) value = __webpack_require__(value);
  68. /******/ if(mode & 8) return value;
  69. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  70. /******/ var ns = Object.create(null);
  71. /******/ __webpack_require__.r(ns);
  72. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  73. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  74. /******/ return ns;
  75. /******/ };
  76. /******/
  77. /******/ // getDefaultExport function for compatibility with non-harmony modules
  78. /******/ __webpack_require__.n = function(module) {
  79. /******/ var getter = module && module.__esModule ?
  80. /******/ function getDefault() { return module['default']; } :
  81. /******/ function getModuleExports() { return module; };
  82. /******/ __webpack_require__.d(getter, 'a', getter);
  83. /******/ return getter;
  84. /******/ };
  85. /******/
  86. /******/ // Object.prototype.hasOwnProperty.call
  87. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  88. /******/
  89. /******/ // __webpack_public_path__
  90. /******/ __webpack_require__.p = "";
  91. /******/
  92. /******/
  93. /******/ // Load entry module and return exports
  94. /******/ return __webpack_require__(__webpack_require__.s = "./legacy/legacy-glTF2FileLoader.ts");
  95. /******/ })
  96. /************************************************************************/
  97. /******/ ({
  98. /***/ "../node_modules/webpack/buildin/global.js":
  99. /*!*************************************************!*\
  100. !*** ../node_modules/webpack/buildin/global.js ***!
  101. \*************************************************/
  102. /*! no static exports found */
  103. /***/ (function(module, exports) {
  104. var g;
  105. // This works in non-strict mode
  106. g = (function() {
  107. return this;
  108. })();
  109. try {
  110. // This works if eval is allowed (see CSP)
  111. g = g || Function("return this")() || (1, eval)("this");
  112. } catch (e) {
  113. // This works if the window reference is available
  114. if (typeof window === "object") g = window;
  115. }
  116. // g can still be undefined, but nothing to do about it...
  117. // We return undefined, instead of nothing here, so it's
  118. // easier to handle this case. if(!global) { ...}
  119. module.exports = g;
  120. /***/ }),
  121. /***/ "./legacy/legacy-glTF2FileLoader.ts":
  122. /*!******************************************!*\
  123. !*** ./legacy/legacy-glTF2FileLoader.ts ***!
  124. \******************************************/
  125. /*! no static exports found */
  126. /***/ (function(module, exports, __webpack_require__) {
  127. "use strict";
  128. /* WEBPACK VAR INJECTION */(function(global) {
  129. function __export(m) {
  130. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  131. }
  132. Object.defineProperty(exports, "__esModule", { value: true });
  133. var FileLoader = __webpack_require__(/*! ../src/glTF/glTFFileLoader */ "./src/glTF/glTFFileLoader.ts");
  134. var LoadersV2 = __webpack_require__(/*! ../src/glTF/2.0 */ "./src/glTF/2.0/index.ts");
  135. /**
  136. * This is the entry point for the UMD module.
  137. * The entry point for a future ESM package should be index.ts
  138. */
  139. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  140. if (typeof globalObject !== "undefined") {
  141. globalObject.BABYLON = globalObject.BABYLON || {};
  142. for (var key in FileLoader) {
  143. globalObject.BABYLON[key] = FileLoader[key];
  144. }
  145. for (var key in LoadersV2) {
  146. globalObject.BABYLON[key] = LoadersV2[key];
  147. }
  148. }
  149. __export(__webpack_require__(/*! ../src/glTF/glTFFileLoader */ "./src/glTF/glTFFileLoader.ts"));
  150. __export(__webpack_require__(/*! ../src/glTF/2.0 */ "./src/glTF/2.0/index.ts"));
  151. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../node_modules/webpack/buildin/global.js */ "../node_modules/webpack/buildin/global.js")))
  152. /***/ }),
  153. /***/ "./src/glTF/2.0/Extensions/EXT_lights_image_based.ts":
  154. /*!***********************************************************!*\
  155. !*** ./src/glTF/2.0/Extensions/EXT_lights_image_based.ts ***!
  156. \***********************************************************/
  157. /*! no static exports found */
  158. /***/ (function(module, exports, __webpack_require__) {
  159. "use strict";
  160. Object.defineProperty(exports, "__esModule", { value: true });
  161. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  162. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  163. var NAME = "EXT_lights_image_based";
  164. /**
  165. * [Specification](https://github.com/KhronosGroup/glTF/blob/eb3e32332042e04691a5f35103f8c261e50d8f1e/extensions/2.0/Khronos/EXT_lights_image_based/README.md) (Experimental)
  166. */
  167. var EXT_lights_image_based = /** @class */ (function () {
  168. /** @hidden */
  169. function EXT_lights_image_based(loader) {
  170. /** The name of this extension. */
  171. this.name = NAME;
  172. /** Defines whether this extension is enabled. */
  173. this.enabled = true;
  174. this._loader = loader;
  175. }
  176. /** @hidden */
  177. EXT_lights_image_based.prototype.dispose = function () {
  178. delete this._loader;
  179. delete this._lights;
  180. };
  181. /** @hidden */
  182. EXT_lights_image_based.prototype.onLoading = function () {
  183. var extensions = this._loader.gltf.extensions;
  184. if (extensions && extensions[this.name]) {
  185. var extension = extensions[this.name];
  186. this._lights = extension.lights;
  187. }
  188. };
  189. /** @hidden */
  190. EXT_lights_image_based.prototype.loadSceneAsync = function (context, scene) {
  191. var _this = this;
  192. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, scene, this.name, function (extensionContext, extension) {
  193. var promises = new Array();
  194. promises.push(_this._loader.loadSceneAsync(context, scene));
  195. _this._loader.logOpen("" + extensionContext);
  196. var light = glTFLoader_1.ArrayItem.Get(extensionContext + "/light", _this._lights, extension.light);
  197. promises.push(_this._loadLightAsync("#/extensions/" + _this.name + "/lights/" + extension.light, light).then(function (texture) {
  198. _this._loader.babylonScene.environmentTexture = texture;
  199. }));
  200. _this._loader.logClose();
  201. return Promise.all(promises).then(function () { });
  202. });
  203. };
  204. EXT_lights_image_based.prototype._loadLightAsync = function (context, light) {
  205. var _this = this;
  206. if (!light._loaded) {
  207. var promises = new Array();
  208. this._loader.logOpen("" + context);
  209. var imageData_1 = new Array(light.specularImages.length);
  210. var _loop_1 = function (mipmap) {
  211. var faces = light.specularImages[mipmap];
  212. imageData_1[mipmap] = new Array(faces.length);
  213. var _loop_2 = function (face) {
  214. var specularImageContext = context + "/specularImages/" + mipmap + "/" + face;
  215. this_1._loader.logOpen("" + specularImageContext);
  216. var index = faces[face];
  217. var image = glTFLoader_1.ArrayItem.Get(specularImageContext, this_1._loader.gltf.images, index);
  218. promises.push(this_1._loader.loadImageAsync("#/images/" + index, image).then(function (data) {
  219. imageData_1[mipmap][face] = data;
  220. }));
  221. this_1._loader.logClose();
  222. };
  223. for (var face = 0; face < faces.length; face++) {
  224. _loop_2(face);
  225. }
  226. };
  227. var this_1 = this;
  228. for (var mipmap = 0; mipmap < light.specularImages.length; mipmap++) {
  229. _loop_1(mipmap);
  230. }
  231. this._loader.logClose();
  232. light._loaded = Promise.all(promises).then(function () {
  233. var babylonTexture = new babylonjs_1.RawCubeTexture(_this._loader.babylonScene, null, light.specularImageSize);
  234. light._babylonTexture = babylonTexture;
  235. if (light.intensity != undefined) {
  236. babylonTexture.level = light.intensity;
  237. }
  238. if (light.rotation) {
  239. var rotation = babylonjs_1.Quaternion.FromArray(light.rotation);
  240. // Invert the rotation so that positive rotation is counter-clockwise.
  241. if (!_this._loader.babylonScene.useRightHandedSystem) {
  242. rotation = babylonjs_1.Quaternion.Inverse(rotation);
  243. }
  244. babylonjs_1.Matrix.FromQuaternionToRef(rotation, babylonTexture.getReflectionTextureMatrix());
  245. }
  246. var sphericalHarmonics = babylonjs_1.SphericalHarmonics.FromArray(light.irradianceCoefficients);
  247. sphericalHarmonics.scale(light.intensity);
  248. sphericalHarmonics.convertIrradianceToLambertianRadiance();
  249. var sphericalPolynomial = babylonjs_1.SphericalPolynomial.FromHarmonics(sphericalHarmonics);
  250. // Compute the lod generation scale to fit exactly to the number of levels available.
  251. var lodGenerationScale = (imageData_1.length - 1) / babylonjs_1.Scalar.Log2(light.specularImageSize);
  252. return babylonTexture.updateRGBDAsync(imageData_1, sphericalPolynomial, lodGenerationScale);
  253. });
  254. }
  255. return light._loaded.then(function () {
  256. return light._babylonTexture;
  257. });
  258. };
  259. return EXT_lights_image_based;
  260. }());
  261. exports.EXT_lights_image_based = EXT_lights_image_based;
  262. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new EXT_lights_image_based(loader); });
  263. /***/ }),
  264. /***/ "./src/glTF/2.0/Extensions/KHR_draco_mesh_compression.ts":
  265. /*!***************************************************************!*\
  266. !*** ./src/glTF/2.0/Extensions/KHR_draco_mesh_compression.ts ***!
  267. \***************************************************************/
  268. /*! no static exports found */
  269. /***/ (function(module, exports, __webpack_require__) {
  270. "use strict";
  271. Object.defineProperty(exports, "__esModule", { value: true });
  272. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  273. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  274. var NAME = "KHR_draco_mesh_compression";
  275. /**
  276. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression)
  277. */
  278. var KHR_draco_mesh_compression = /** @class */ (function () {
  279. /** @hidden */
  280. function KHR_draco_mesh_compression(loader) {
  281. /** The name of this extension. */
  282. this.name = NAME;
  283. /** Defines whether this extension is enabled. */
  284. this.enabled = babylonjs_1.DracoCompression.DecoderAvailable;
  285. this._loader = loader;
  286. }
  287. /** @hidden */
  288. KHR_draco_mesh_compression.prototype.dispose = function () {
  289. if (this._dracoCompression) {
  290. this._dracoCompression.dispose();
  291. delete this._dracoCompression;
  292. }
  293. delete this._loader;
  294. };
  295. /** @hidden */
  296. KHR_draco_mesh_compression.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  297. var _this = this;
  298. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, primitive, this.name, function (extensionContext, extension) {
  299. if (primitive.mode != undefined) {
  300. if (primitive.mode !== 5 /* TRIANGLE_STRIP */ &&
  301. primitive.mode !== 4 /* TRIANGLES */) {
  302. throw new Error(context + ": Unsupported mode " + primitive.mode);
  303. }
  304. // TODO: handle triangle strips
  305. if (primitive.mode === 5 /* TRIANGLE_STRIP */) {
  306. throw new Error(context + ": Mode " + primitive.mode + " is not currently supported");
  307. }
  308. }
  309. var attributes = {};
  310. var loadAttribute = function (name, kind) {
  311. var uniqueId = extension.attributes[name];
  312. if (uniqueId == undefined) {
  313. return;
  314. }
  315. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  316. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  317. babylonMesh._delayInfo.push(kind);
  318. }
  319. attributes[kind] = uniqueId;
  320. };
  321. loadAttribute("POSITION", babylonjs_1.VertexBuffer.PositionKind);
  322. loadAttribute("NORMAL", babylonjs_1.VertexBuffer.NormalKind);
  323. loadAttribute("TANGENT", babylonjs_1.VertexBuffer.TangentKind);
  324. loadAttribute("TEXCOORD_0", babylonjs_1.VertexBuffer.UVKind);
  325. loadAttribute("TEXCOORD_1", babylonjs_1.VertexBuffer.UV2Kind);
  326. loadAttribute("JOINTS_0", babylonjs_1.VertexBuffer.MatricesIndicesKind);
  327. loadAttribute("WEIGHTS_0", babylonjs_1.VertexBuffer.MatricesWeightsKind);
  328. loadAttribute("COLOR_0", babylonjs_1.VertexBuffer.ColorKind);
  329. var bufferView = glTFLoader_1.ArrayItem.Get(extensionContext, _this._loader.gltf.bufferViews, extension.bufferView);
  330. if (!bufferView._dracoBabylonGeometry) {
  331. bufferView._dracoBabylonGeometry = _this._loader.loadBufferViewAsync("#/bufferViews/" + bufferView.index, bufferView).then(function (data) {
  332. if (!_this._dracoCompression) {
  333. _this._dracoCompression = new babylonjs_1.DracoCompression();
  334. }
  335. return _this._dracoCompression.decodeMeshAsync(data, attributes).then(function (babylonVertexData) {
  336. var babylonGeometry = new babylonjs_1.Geometry(babylonMesh.name, _this._loader.babylonScene);
  337. babylonVertexData.applyToGeometry(babylonGeometry);
  338. return babylonGeometry;
  339. }).catch(function (error) {
  340. throw new Error(context + ": " + error.message);
  341. });
  342. });
  343. }
  344. return bufferView._dracoBabylonGeometry;
  345. });
  346. };
  347. return KHR_draco_mesh_compression;
  348. }());
  349. exports.KHR_draco_mesh_compression = KHR_draco_mesh_compression;
  350. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new KHR_draco_mesh_compression(loader); });
  351. /***/ }),
  352. /***/ "./src/glTF/2.0/Extensions/KHR_lights_punctual.ts":
  353. /*!********************************************************!*\
  354. !*** ./src/glTF/2.0/Extensions/KHR_lights_punctual.ts ***!
  355. \********************************************************/
  356. /*! no static exports found */
  357. /***/ (function(module, exports, __webpack_require__) {
  358. "use strict";
  359. Object.defineProperty(exports, "__esModule", { value: true });
  360. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  361. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  362. var NAME = "KHR_lights_punctual";
  363. var LightType;
  364. (function (LightType) {
  365. LightType["DIRECTIONAL"] = "directional";
  366. LightType["POINT"] = "point";
  367. LightType["SPOT"] = "spot";
  368. })(LightType || (LightType = {}));
  369. /**
  370. * [Specification](https://github.com/KhronosGroup/glTF/blob/1048d162a44dbcb05aefc1874bfd423cf60135a6/extensions/2.0/Khronos/KHR_lights_punctual/README.md) (Experimental)
  371. */
  372. var KHR_lights = /** @class */ (function () {
  373. /** @hidden */
  374. function KHR_lights(loader) {
  375. /** The name of this extension. */
  376. this.name = NAME;
  377. /** Defines whether this extension is enabled. */
  378. this.enabled = true;
  379. this._loader = loader;
  380. }
  381. /** @hidden */
  382. KHR_lights.prototype.dispose = function () {
  383. delete this._loader;
  384. delete this._lights;
  385. };
  386. /** @hidden */
  387. KHR_lights.prototype.onLoading = function () {
  388. var extensions = this._loader.gltf.extensions;
  389. if (extensions && extensions[this.name]) {
  390. var extension = extensions[this.name];
  391. this._lights = extension.lights;
  392. }
  393. };
  394. /** @hidden */
  395. KHR_lights.prototype.loadNodeAsync = function (context, node, assign) {
  396. var _this = this;
  397. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, node, this.name, function (extensionContext, extension) {
  398. return _this._loader.loadNodeAsync(context, node, function (babylonMesh) {
  399. var babylonLight;
  400. var light = glTFLoader_1.ArrayItem.Get(extensionContext, _this._lights, extension.light);
  401. var name = light.name || babylonMesh.name;
  402. switch (light.type) {
  403. case LightType.DIRECTIONAL: {
  404. babylonLight = new babylonjs_1.DirectionalLight(name, babylonjs_1.Vector3.Backward(), _this._loader.babylonScene);
  405. break;
  406. }
  407. case LightType.POINT: {
  408. babylonLight = new babylonjs_1.PointLight(name, babylonjs_1.Vector3.Zero(), _this._loader.babylonScene);
  409. break;
  410. }
  411. case LightType.SPOT: {
  412. var babylonSpotLight = new babylonjs_1.SpotLight(name, babylonjs_1.Vector3.Zero(), babylonjs_1.Vector3.Backward(), 0, 1, _this._loader.babylonScene);
  413. babylonSpotLight.angle = ((light.spot && light.spot.outerConeAngle) || Math.PI / 4) * 2;
  414. babylonSpotLight.innerAngle = ((light.spot && light.spot.innerConeAngle) || 0) * 2;
  415. babylonLight = babylonSpotLight;
  416. break;
  417. }
  418. default: {
  419. throw new Error(extensionContext + ": Invalid light type (" + light.type + ")");
  420. }
  421. }
  422. babylonLight.falloffType = babylonjs_1.Light.FALLOFF_GLTF;
  423. babylonLight.diffuse = light.color ? babylonjs_1.Color3.FromArray(light.color) : babylonjs_1.Color3.White();
  424. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  425. babylonLight.range = light.range == undefined ? Number.MAX_VALUE : light.range;
  426. babylonLight.parent = babylonMesh;
  427. assign(babylonMesh);
  428. });
  429. });
  430. };
  431. return KHR_lights;
  432. }());
  433. exports.KHR_lights = KHR_lights;
  434. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new KHR_lights(loader); });
  435. /***/ }),
  436. /***/ "./src/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.ts":
  437. /*!************************************************************************!*\
  438. !*** ./src/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.ts ***!
  439. \************************************************************************/
  440. /*! no static exports found */
  441. /***/ (function(module, exports, __webpack_require__) {
  442. "use strict";
  443. Object.defineProperty(exports, "__esModule", { value: true });
  444. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  445. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  446. var NAME = "KHR_materials_pbrSpecularGlossiness";
  447. /**
  448. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness)
  449. */
  450. var KHR_materials_pbrSpecularGlossiness = /** @class */ (function () {
  451. /** @hidden */
  452. function KHR_materials_pbrSpecularGlossiness(loader) {
  453. /** The name of this extension. */
  454. this.name = NAME;
  455. /** Defines whether this extension is enabled. */
  456. this.enabled = true;
  457. this._loader = loader;
  458. }
  459. /** @hidden */
  460. KHR_materials_pbrSpecularGlossiness.prototype.dispose = function () {
  461. delete this._loader;
  462. };
  463. /** @hidden */
  464. KHR_materials_pbrSpecularGlossiness.prototype.loadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
  465. var _this = this;
  466. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, material, this.name, function (extensionContext, extension) {
  467. var promises = new Array();
  468. promises.push(_this._loader.loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  469. promises.push(_this._loadSpecularGlossinessPropertiesAsync(extensionContext, material, extension, babylonMaterial));
  470. _this._loader.loadMaterialAlphaProperties(context, material, babylonMaterial);
  471. return Promise.all(promises).then(function () { });
  472. });
  473. };
  474. KHR_materials_pbrSpecularGlossiness.prototype._loadSpecularGlossinessPropertiesAsync = function (context, material, properties, babylonMaterial) {
  475. if (!(babylonMaterial instanceof babylonjs_1.PBRMaterial)) {
  476. throw new Error(context + ": Material type not supported");
  477. }
  478. var promises = new Array();
  479. babylonMaterial.metallic = null;
  480. babylonMaterial.roughness = null;
  481. if (properties.diffuseFactor) {
  482. babylonMaterial.albedoColor = babylonjs_1.Color3.FromArray(properties.diffuseFactor);
  483. babylonMaterial.alpha = properties.diffuseFactor[3];
  484. }
  485. else {
  486. babylonMaterial.albedoColor = babylonjs_1.Color3.White();
  487. }
  488. babylonMaterial.reflectivityColor = properties.specularFactor ? babylonjs_1.Color3.FromArray(properties.specularFactor) : babylonjs_1.Color3.White();
  489. babylonMaterial.microSurface = properties.glossinessFactor == undefined ? 1 : properties.glossinessFactor;
  490. if (properties.diffuseTexture) {
  491. promises.push(this._loader.loadTextureInfoAsync(context + "/diffuseTexture", properties.diffuseTexture, function (texture) {
  492. babylonMaterial.albedoTexture = texture;
  493. return Promise.resolve();
  494. }));
  495. }
  496. if (properties.specularGlossinessTexture) {
  497. promises.push(this._loader.loadTextureInfoAsync(context + "/specularGlossinessTexture", properties.specularGlossinessTexture, function (texture) {
  498. babylonMaterial.reflectivityTexture = texture;
  499. return Promise.resolve();
  500. }));
  501. babylonMaterial.reflectivityTexture.hasAlpha = true;
  502. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  503. }
  504. return Promise.all(promises).then(function () { });
  505. };
  506. return KHR_materials_pbrSpecularGlossiness;
  507. }());
  508. exports.KHR_materials_pbrSpecularGlossiness = KHR_materials_pbrSpecularGlossiness;
  509. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new KHR_materials_pbrSpecularGlossiness(loader); });
  510. /***/ }),
  511. /***/ "./src/glTF/2.0/Extensions/KHR_materials_unlit.ts":
  512. /*!********************************************************!*\
  513. !*** ./src/glTF/2.0/Extensions/KHR_materials_unlit.ts ***!
  514. \********************************************************/
  515. /*! no static exports found */
  516. /***/ (function(module, exports, __webpack_require__) {
  517. "use strict";
  518. Object.defineProperty(exports, "__esModule", { value: true });
  519. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  520. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  521. var NAME = "KHR_materials_unlit";
  522. /**
  523. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit)
  524. */
  525. var KHR_materials_unlit = /** @class */ (function () {
  526. /** @hidden */
  527. function KHR_materials_unlit(loader) {
  528. /** The name of this extension. */
  529. this.name = NAME;
  530. /** Defines whether this extension is enabled. */
  531. this.enabled = true;
  532. this._loader = loader;
  533. }
  534. /** @hidden */
  535. KHR_materials_unlit.prototype.dispose = function () {
  536. delete this._loader;
  537. };
  538. /** @hidden */
  539. KHR_materials_unlit.prototype.loadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
  540. var _this = this;
  541. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, material, this.name, function () {
  542. return _this._loadUnlitPropertiesAsync(context, material, babylonMaterial);
  543. });
  544. };
  545. KHR_materials_unlit.prototype._loadUnlitPropertiesAsync = function (context, material, babylonMaterial) {
  546. if (!(babylonMaterial instanceof babylonjs_1.PBRMaterial)) {
  547. throw new Error(context + ": Material type not supported");
  548. }
  549. var promises = new Array();
  550. babylonMaterial.unlit = true;
  551. var properties = material.pbrMetallicRoughness;
  552. if (properties) {
  553. if (properties.baseColorFactor) {
  554. babylonMaterial.albedoColor = babylonjs_1.Color3.FromArray(properties.baseColorFactor);
  555. babylonMaterial.alpha = properties.baseColorFactor[3];
  556. }
  557. else {
  558. babylonMaterial.albedoColor = babylonjs_1.Color3.White();
  559. }
  560. if (properties.baseColorTexture) {
  561. promises.push(this._loader.loadTextureInfoAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  562. babylonMaterial.albedoTexture = texture;
  563. return Promise.resolve();
  564. }));
  565. }
  566. }
  567. if (material.doubleSided) {
  568. babylonMaterial.backFaceCulling = false;
  569. babylonMaterial.twoSidedLighting = true;
  570. }
  571. this._loader.loadMaterialAlphaProperties(context, material, babylonMaterial);
  572. return Promise.all(promises).then(function () { });
  573. };
  574. return KHR_materials_unlit;
  575. }());
  576. exports.KHR_materials_unlit = KHR_materials_unlit;
  577. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new KHR_materials_unlit(loader); });
  578. /***/ }),
  579. /***/ "./src/glTF/2.0/Extensions/KHR_texture_transform.ts":
  580. /*!**********************************************************!*\
  581. !*** ./src/glTF/2.0/Extensions/KHR_texture_transform.ts ***!
  582. \**********************************************************/
  583. /*! no static exports found */
  584. /***/ (function(module, exports, __webpack_require__) {
  585. "use strict";
  586. Object.defineProperty(exports, "__esModule", { value: true });
  587. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  588. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  589. var NAME = "KHR_texture_transform";
  590. /**
  591. * [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_texture_transform/README.md)
  592. */
  593. var KHR_texture_transform = /** @class */ (function () {
  594. /** @hidden */
  595. function KHR_texture_transform(loader) {
  596. /** The name of this extension. */
  597. this.name = NAME;
  598. /** Defines whether this extension is enabled. */
  599. this.enabled = true;
  600. this._loader = loader;
  601. }
  602. /** @hidden */
  603. KHR_texture_transform.prototype.dispose = function () {
  604. delete this._loader;
  605. };
  606. /** @hidden */
  607. KHR_texture_transform.prototype.loadTextureInfoAsync = function (context, textureInfo, assign) {
  608. var _this = this;
  609. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, textureInfo, this.name, function (extensionContext, extension) {
  610. return _this._loader.loadTextureInfoAsync(context, textureInfo, function (babylonTexture) {
  611. if (!(babylonTexture instanceof babylonjs_1.Texture)) {
  612. throw new Error(extensionContext + ": Texture type not supported");
  613. }
  614. if (extension.offset) {
  615. babylonTexture.uOffset = extension.offset[0];
  616. babylonTexture.vOffset = extension.offset[1];
  617. }
  618. // Always rotate around the origin.
  619. babylonTexture.uRotationCenter = 0;
  620. babylonTexture.vRotationCenter = 0;
  621. if (extension.rotation) {
  622. babylonTexture.wAng = -extension.rotation;
  623. }
  624. if (extension.scale) {
  625. babylonTexture.uScale = extension.scale[0];
  626. babylonTexture.vScale = extension.scale[1];
  627. }
  628. if (extension.texCoord != undefined) {
  629. babylonTexture.coordinatesIndex = extension.texCoord;
  630. }
  631. assign(babylonTexture);
  632. });
  633. });
  634. };
  635. return KHR_texture_transform;
  636. }());
  637. exports.KHR_texture_transform = KHR_texture_transform;
  638. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new KHR_texture_transform(loader); });
  639. /***/ }),
  640. /***/ "./src/glTF/2.0/Extensions/MSFT_audio_emitter.ts":
  641. /*!*******************************************************!*\
  642. !*** ./src/glTF/2.0/Extensions/MSFT_audio_emitter.ts ***!
  643. \*******************************************************/
  644. /*! no static exports found */
  645. /***/ (function(module, exports, __webpack_require__) {
  646. "use strict";
  647. Object.defineProperty(exports, "__esModule", { value: true });
  648. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  649. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  650. var NAME = "MSFT_audio_emitter";
  651. /**
  652. * [Specification](https://github.com/najadojo/glTF/tree/MSFT_audio_emitter/extensions/2.0/Vendor/MSFT_audio_emitter)
  653. */
  654. var MSFT_audio_emitter = /** @class */ (function () {
  655. /** @hidden */
  656. function MSFT_audio_emitter(loader) {
  657. /** The name of this extension. */
  658. this.name = NAME;
  659. /** Defines whether this extension is enabled. */
  660. this.enabled = true;
  661. this._loader = loader;
  662. }
  663. /** @hidden */
  664. MSFT_audio_emitter.prototype.dispose = function () {
  665. delete this._loader;
  666. delete this._clips;
  667. delete this._emitters;
  668. };
  669. /** @hidden */
  670. MSFT_audio_emitter.prototype.onLoading = function () {
  671. var extensions = this._loader.gltf.extensions;
  672. if (extensions && extensions[this.name]) {
  673. var extension = extensions[this.name];
  674. this._clips = extension.clips;
  675. this._emitters = extension.emitters;
  676. glTFLoader_1.ArrayItem.Assign(this._clips);
  677. glTFLoader_1.ArrayItem.Assign(this._emitters);
  678. }
  679. };
  680. /** @hidden */
  681. MSFT_audio_emitter.prototype.loadSceneAsync = function (context, scene) {
  682. var _this = this;
  683. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, scene, this.name, function (extensionContext, extension) {
  684. var promises = new Array();
  685. promises.push(_this._loader.loadSceneAsync(context, scene));
  686. for (var _i = 0, _a = extension.emitters; _i < _a.length; _i++) {
  687. var emitterIndex = _a[_i];
  688. var emitter = glTFLoader_1.ArrayItem.Get(extensionContext + "/emitters", _this._emitters, emitterIndex);
  689. if (emitter.refDistance != undefined || emitter.maxDistance != undefined || emitter.rolloffFactor != undefined ||
  690. emitter.distanceModel != undefined || emitter.innerAngle != undefined || emitter.outerAngle != undefined) {
  691. throw new Error(extensionContext + ": Direction or Distance properties are not allowed on emitters attached to a scene");
  692. }
  693. promises.push(_this._loadEmitterAsync(extensionContext + "/emitters/" + emitter.index, emitter));
  694. }
  695. return Promise.all(promises).then(function () { });
  696. });
  697. };
  698. /** @hidden */
  699. MSFT_audio_emitter.prototype.loadNodeAsync = function (context, node, assign) {
  700. var _this = this;
  701. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, node, this.name, function (extensionContext, extension) {
  702. var promises = new Array();
  703. return _this._loader.loadNodeAsync(extensionContext, node, function (babylonMesh) {
  704. var _loop_1 = function (emitterIndex) {
  705. var emitter = glTFLoader_1.ArrayItem.Get(extensionContext + "/emitters", _this._emitters, emitterIndex);
  706. promises.push(_this._loadEmitterAsync(extensionContext + "/emitters/" + emitter.index, emitter).then(function () {
  707. for (var _i = 0, _a = emitter._babylonSounds; _i < _a.length; _i++) {
  708. var sound = _a[_i];
  709. sound.attachToMesh(babylonMesh);
  710. if (emitter.innerAngle != undefined || emitter.outerAngle != undefined) {
  711. sound.setLocalDirectionToMesh(babylonjs_1.Vector3.Forward());
  712. sound.setDirectionalCone(2 * babylonjs_1.Tools.ToDegrees(emitter.innerAngle == undefined ? Math.PI : emitter.innerAngle), 2 * babylonjs_1.Tools.ToDegrees(emitter.outerAngle == undefined ? Math.PI : emitter.outerAngle), 0);
  713. }
  714. }
  715. }));
  716. };
  717. for (var _i = 0, _a = extension.emitters; _i < _a.length; _i++) {
  718. var emitterIndex = _a[_i];
  719. _loop_1(emitterIndex);
  720. }
  721. assign(babylonMesh);
  722. }).then(function (babylonMesh) {
  723. return Promise.all(promises).then(function () {
  724. return babylonMesh;
  725. });
  726. });
  727. });
  728. };
  729. /** @hidden */
  730. MSFT_audio_emitter.prototype.loadAnimationAsync = function (context, animation) {
  731. var _this = this;
  732. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, animation, this.name, function (extensionContext, extension) {
  733. return _this._loader.loadAnimationAsync(context, animation).then(function (babylonAnimationGroup) {
  734. var promises = new Array();
  735. glTFLoader_1.ArrayItem.Assign(extension.events);
  736. for (var _i = 0, _a = extension.events; _i < _a.length; _i++) {
  737. var event_1 = _a[_i];
  738. promises.push(_this._loadAnimationEventAsync(extensionContext + "/events/" + event_1.index, context, animation, event_1, babylonAnimationGroup));
  739. }
  740. return Promise.all(promises).then(function () {
  741. return babylonAnimationGroup;
  742. });
  743. });
  744. });
  745. };
  746. MSFT_audio_emitter.prototype._loadClipAsync = function (context, clip) {
  747. if (clip._objectURL) {
  748. return clip._objectURL;
  749. }
  750. var promise;
  751. if (clip.uri) {
  752. promise = this._loader.loadUriAsync(context, clip.uri);
  753. }
  754. else {
  755. var bufferView = glTFLoader_1.ArrayItem.Get(context + "/bufferView", this._loader.gltf.bufferViews, clip.bufferView);
  756. promise = this._loader.loadBufferViewAsync("#/bufferViews/" + bufferView.index, bufferView);
  757. }
  758. clip._objectURL = promise.then(function (data) {
  759. return URL.createObjectURL(new Blob([data], { type: clip.mimeType }));
  760. });
  761. return clip._objectURL;
  762. };
  763. MSFT_audio_emitter.prototype._loadEmitterAsync = function (context, emitter) {
  764. var _this = this;
  765. emitter._babylonSounds = emitter._babylonSounds || [];
  766. if (!emitter._babylonData) {
  767. var clipPromises = new Array();
  768. var name_1 = emitter.name || "emitter" + emitter.index;
  769. var options_1 = {
  770. loop: false,
  771. autoplay: false,
  772. volume: emitter.volume == undefined ? 1 : emitter.volume,
  773. };
  774. var _loop_2 = function (i) {
  775. var clipContext = "#/extensions/" + this_1.name + "/clips";
  776. var clip = glTFLoader_1.ArrayItem.Get(clipContext, this_1._clips, emitter.clips[i].clip);
  777. clipPromises.push(this_1._loadClipAsync(clipContext + "/" + emitter.clips[i].clip, clip).then(function (objectURL) {
  778. var sound = emitter._babylonSounds[i] = new babylonjs_1.Sound(name_1, objectURL, _this._loader.babylonScene, null, options_1);
  779. sound.refDistance = emitter.refDistance || 1;
  780. sound.maxDistance = emitter.maxDistance || 256;
  781. sound.rolloffFactor = emitter.rolloffFactor || 1;
  782. sound.distanceModel = emitter.distanceModel || 'exponential';
  783. sound._positionInEmitterSpace = true;
  784. }));
  785. };
  786. var this_1 = this;
  787. for (var i = 0; i < emitter.clips.length; i++) {
  788. _loop_2(i);
  789. }
  790. var promise = Promise.all(clipPromises).then(function () {
  791. var weights = emitter.clips.map(function (clip) { return clip.weight || 1; });
  792. var weightedSound = new babylonjs_1.WeightedSound(emitter.loop || false, emitter._babylonSounds, weights);
  793. if (emitter.innerAngle) {
  794. weightedSound.directionalConeInnerAngle = 2 * babylonjs_1.Tools.ToDegrees(emitter.innerAngle);
  795. }
  796. if (emitter.outerAngle) {
  797. weightedSound.directionalConeOuterAngle = 2 * babylonjs_1.Tools.ToDegrees(emitter.outerAngle);
  798. }
  799. if (emitter.volume) {
  800. weightedSound.volume = emitter.volume;
  801. }
  802. emitter._babylonData.sound = weightedSound;
  803. });
  804. emitter._babylonData = {
  805. loaded: promise
  806. };
  807. }
  808. return emitter._babylonData.loaded;
  809. };
  810. MSFT_audio_emitter.prototype._getEventAction = function (context, sound, action, time, startOffset) {
  811. switch (action) {
  812. case "play" /* play */: {
  813. return function (currentFrame) {
  814. var frameOffset = (startOffset || 0) + (currentFrame - time);
  815. sound.play(frameOffset);
  816. };
  817. }
  818. case "stop" /* stop */: {
  819. return function (currentFrame) {
  820. sound.stop();
  821. };
  822. }
  823. case "pause" /* pause */: {
  824. return function (currentFrame) {
  825. sound.pause();
  826. };
  827. }
  828. default: {
  829. throw new Error(context + ": Unsupported action " + action);
  830. }
  831. }
  832. };
  833. MSFT_audio_emitter.prototype._loadAnimationEventAsync = function (context, animationContext, animation, event, babylonAnimationGroup) {
  834. var _this = this;
  835. if (babylonAnimationGroup.targetedAnimations.length == 0) {
  836. return Promise.resolve();
  837. }
  838. var babylonAnimation = babylonAnimationGroup.targetedAnimations[0];
  839. var emitterIndex = event.emitter;
  840. var emitter = glTFLoader_1.ArrayItem.Get("#/extensions/" + this.name + "/emitters", this._emitters, emitterIndex);
  841. return this._loadEmitterAsync(context, emitter).then(function () {
  842. var sound = emitter._babylonData.sound;
  843. if (sound) {
  844. var babylonAnimationEvent = new babylonjs_1.AnimationEvent(event.time, _this._getEventAction(context, sound, event.action, event.time, event.startOffset));
  845. babylonAnimation.animation.addEvent(babylonAnimationEvent);
  846. // Make sure all started audio stops when this animation is terminated.
  847. babylonAnimationGroup.onAnimationGroupEndObservable.add(function () {
  848. sound.stop();
  849. });
  850. babylonAnimationGroup.onAnimationGroupPauseObservable.add(function () {
  851. sound.pause();
  852. });
  853. }
  854. });
  855. };
  856. return MSFT_audio_emitter;
  857. }());
  858. exports.MSFT_audio_emitter = MSFT_audio_emitter;
  859. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new MSFT_audio_emitter(loader); });
  860. /***/ }),
  861. /***/ "./src/glTF/2.0/Extensions/MSFT_lod.ts":
  862. /*!*********************************************!*\
  863. !*** ./src/glTF/2.0/Extensions/MSFT_lod.ts ***!
  864. \*********************************************/
  865. /*! no static exports found */
  866. /***/ (function(module, exports, __webpack_require__) {
  867. "use strict";
  868. Object.defineProperty(exports, "__esModule", { value: true });
  869. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  870. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  871. var NAME = "MSFT_lod";
  872. /**
  873. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/MSFT_lod)
  874. */
  875. var MSFT_lod = /** @class */ (function () {
  876. /** @hidden */
  877. function MSFT_lod(loader) {
  878. /** The name of this extension. */
  879. this.name = NAME;
  880. /** Defines whether this extension is enabled. */
  881. this.enabled = true;
  882. /**
  883. * Maximum number of LODs to load, starting from the lowest LOD.
  884. */
  885. this.maxLODsToLoad = Number.MAX_VALUE;
  886. /**
  887. * Observable raised when all node LODs of one level are loaded.
  888. * The event data is the index of the loaded LOD starting from zero.
  889. * Dispose the loader to cancel the loading of the next level of LODs.
  890. */
  891. this.onNodeLODsLoadedObservable = new babylonjs_1.Observable();
  892. /**
  893. * Observable raised when all material LODs of one level are loaded.
  894. * The event data is the index of the loaded LOD starting from zero.
  895. * Dispose the loader to cancel the loading of the next level of LODs.
  896. */
  897. this.onMaterialLODsLoadedObservable = new babylonjs_1.Observable();
  898. this._nodeIndexLOD = null;
  899. this._nodeSignalLODs = new Array();
  900. this._nodePromiseLODs = new Array();
  901. this._materialIndexLOD = null;
  902. this._materialSignalLODs = new Array();
  903. this._materialPromiseLODs = new Array();
  904. this._loader = loader;
  905. }
  906. /** @hidden */
  907. MSFT_lod.prototype.dispose = function () {
  908. delete this._loader;
  909. this._nodeIndexLOD = null;
  910. this._nodeSignalLODs.length = 0;
  911. this._nodePromiseLODs.length = 0;
  912. this._materialIndexLOD = null;
  913. this._materialSignalLODs.length = 0;
  914. this._materialPromiseLODs.length = 0;
  915. this.onMaterialLODsLoadedObservable.clear();
  916. this.onNodeLODsLoadedObservable.clear();
  917. };
  918. /** @hidden */
  919. MSFT_lod.prototype.onReady = function () {
  920. var _this = this;
  921. var _loop_1 = function (indexLOD) {
  922. var promise = Promise.all(this_1._nodePromiseLODs[indexLOD]).then(function () {
  923. if (indexLOD !== 0) {
  924. _this._loader.endPerformanceCounter("Node LOD " + indexLOD);
  925. }
  926. _this._loader.log("Loaded node LOD " + indexLOD);
  927. _this.onNodeLODsLoadedObservable.notifyObservers(indexLOD);
  928. if (indexLOD !== _this._nodePromiseLODs.length - 1) {
  929. _this._loader.startPerformanceCounter("Node LOD " + (indexLOD + 1));
  930. if (_this._nodeSignalLODs[indexLOD]) {
  931. _this._nodeSignalLODs[indexLOD].resolve();
  932. }
  933. }
  934. });
  935. this_1._loader._completePromises.push(promise);
  936. };
  937. var this_1 = this;
  938. for (var indexLOD = 0; indexLOD < this._nodePromiseLODs.length; indexLOD++) {
  939. _loop_1(indexLOD);
  940. }
  941. var _loop_2 = function (indexLOD) {
  942. var promise = Promise.all(this_2._materialPromiseLODs[indexLOD]).then(function () {
  943. if (indexLOD !== 0) {
  944. _this._loader.endPerformanceCounter("Material LOD " + indexLOD);
  945. }
  946. _this._loader.log("Loaded material LOD " + indexLOD);
  947. _this.onMaterialLODsLoadedObservable.notifyObservers(indexLOD);
  948. if (indexLOD !== _this._materialPromiseLODs.length - 1) {
  949. _this._loader.startPerformanceCounter("Material LOD " + (indexLOD + 1));
  950. if (_this._materialSignalLODs[indexLOD]) {
  951. _this._materialSignalLODs[indexLOD].resolve();
  952. }
  953. }
  954. });
  955. this_2._loader._completePromises.push(promise);
  956. };
  957. var this_2 = this;
  958. for (var indexLOD = 0; indexLOD < this._materialPromiseLODs.length; indexLOD++) {
  959. _loop_2(indexLOD);
  960. }
  961. };
  962. /** @hidden */
  963. MSFT_lod.prototype.loadNodeAsync = function (context, node, assign) {
  964. var _this = this;
  965. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, node, this.name, function (extensionContext, extension) {
  966. var firstPromise;
  967. var nodeLODs = _this._getLODs(extensionContext, node, _this._loader.gltf.nodes, extension.ids);
  968. _this._loader.logOpen("" + extensionContext);
  969. var _loop_3 = function (indexLOD) {
  970. var nodeLOD = nodeLODs[indexLOD];
  971. if (indexLOD !== 0) {
  972. _this._nodeIndexLOD = indexLOD;
  973. _this._nodeSignalLODs[indexLOD] = _this._nodeSignalLODs[indexLOD] || new babylonjs_1.Deferred();
  974. }
  975. var promise = _this._loader.loadNodeAsync("#/nodes/" + nodeLOD.index, nodeLOD).then(function (babylonMesh) {
  976. if (indexLOD !== 0) {
  977. // TODO: should not rely on _babylonMesh
  978. var previousNodeLOD = nodeLODs[indexLOD - 1];
  979. if (previousNodeLOD._babylonMesh) {
  980. previousNodeLOD._babylonMesh.dispose();
  981. delete previousNodeLOD._babylonMesh;
  982. _this._disposeUnusedMaterials();
  983. }
  984. }
  985. return babylonMesh;
  986. });
  987. if (indexLOD === 0) {
  988. firstPromise = promise;
  989. }
  990. else {
  991. _this._nodeIndexLOD = null;
  992. }
  993. _this._nodePromiseLODs[indexLOD] = _this._nodePromiseLODs[indexLOD] || [];
  994. _this._nodePromiseLODs[indexLOD].push(promise);
  995. };
  996. for (var indexLOD = 0; indexLOD < nodeLODs.length; indexLOD++) {
  997. _loop_3(indexLOD);
  998. }
  999. _this._loader.logClose();
  1000. return firstPromise;
  1001. });
  1002. };
  1003. /** @hidden */
  1004. MSFT_lod.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  1005. var _this = this;
  1006. // Don't load material LODs if already loading a node LOD.
  1007. if (this._nodeIndexLOD) {
  1008. return null;
  1009. }
  1010. return glTFLoader_1.GLTFLoaderV2.LoadExtensionAsync(context, material, this.name, function (extensionContext, extension) {
  1011. var firstPromise;
  1012. var materialLODs = _this._getLODs(extensionContext, material, _this._loader.gltf.materials, extension.ids);
  1013. _this._loader.logOpen("" + extensionContext);
  1014. var _loop_4 = function (indexLOD) {
  1015. var materialLOD = materialLODs[indexLOD];
  1016. if (indexLOD !== 0) {
  1017. _this._materialIndexLOD = indexLOD;
  1018. }
  1019. var promise = _this._loader._loadMaterialAsync("#/materials/" + materialLOD.index, materialLOD, babylonMesh, babylonDrawMode, function (babylonMaterial) {
  1020. if (indexLOD === 0) {
  1021. assign(babylonMaterial);
  1022. }
  1023. }).then(function (babylonMaterial) {
  1024. if (indexLOD !== 0) {
  1025. assign(babylonMaterial);
  1026. // TODO: should not rely on _babylonData
  1027. var previousBabylonDataLOD = materialLODs[indexLOD - 1]._babylonData;
  1028. if (previousBabylonDataLOD[babylonDrawMode]) {
  1029. previousBabylonDataLOD[babylonDrawMode].material.dispose();
  1030. delete previousBabylonDataLOD[babylonDrawMode];
  1031. }
  1032. }
  1033. return babylonMaterial;
  1034. });
  1035. if (indexLOD === 0) {
  1036. firstPromise = promise;
  1037. }
  1038. else {
  1039. _this._materialIndexLOD = null;
  1040. }
  1041. _this._materialPromiseLODs[indexLOD] = _this._materialPromiseLODs[indexLOD] || [];
  1042. _this._materialPromiseLODs[indexLOD].push(promise);
  1043. };
  1044. for (var indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  1045. _loop_4(indexLOD);
  1046. }
  1047. _this._loader.logClose();
  1048. return firstPromise;
  1049. });
  1050. };
  1051. /** @hidden */
  1052. MSFT_lod.prototype._loadUriAsync = function (context, uri) {
  1053. var _this = this;
  1054. // Defer the loading of uris if loading a material or node LOD.
  1055. if (this._materialIndexLOD !== null) {
  1056. this._loader.log("deferred");
  1057. var previousIndexLOD = this._materialIndexLOD - 1;
  1058. this._materialSignalLODs[previousIndexLOD] = this._materialSignalLODs[previousIndexLOD] || new babylonjs_1.Deferred();
  1059. return this._materialSignalLODs[previousIndexLOD].promise.then(function () {
  1060. return _this._loader.loadUriAsync(context, uri);
  1061. });
  1062. }
  1063. else if (this._nodeIndexLOD !== null) {
  1064. this._loader.log("deferred");
  1065. var previousIndexLOD = this._nodeIndexLOD - 1;
  1066. this._nodeSignalLODs[previousIndexLOD] = this._nodeSignalLODs[previousIndexLOD] || new babylonjs_1.Deferred();
  1067. return this._nodeSignalLODs[this._nodeIndexLOD - 1].promise.then(function () {
  1068. return _this._loader.loadUriAsync(context, uri);
  1069. });
  1070. }
  1071. return null;
  1072. };
  1073. /**
  1074. * Gets an array of LOD properties from lowest to highest.
  1075. */
  1076. MSFT_lod.prototype._getLODs = function (context, property, array, ids) {
  1077. if (this.maxLODsToLoad <= 0) {
  1078. throw new Error("maxLODsToLoad must be greater than zero");
  1079. }
  1080. var properties = new Array();
  1081. for (var i = ids.length - 1; i >= 0; i--) {
  1082. properties.push(glTFLoader_1.ArrayItem.Get(context + "/ids/" + ids[i], array, ids[i]));
  1083. if (properties.length === this.maxLODsToLoad) {
  1084. return properties;
  1085. }
  1086. }
  1087. properties.push(property);
  1088. return properties;
  1089. };
  1090. MSFT_lod.prototype._disposeUnusedMaterials = function () {
  1091. // TODO: should not rely on _babylonData
  1092. var materials = this._loader.gltf.materials;
  1093. if (materials) {
  1094. for (var _i = 0, materials_1 = materials; _i < materials_1.length; _i++) {
  1095. var material = materials_1[_i];
  1096. if (material._babylonData) {
  1097. for (var drawMode in material._babylonData) {
  1098. var babylonData = material._babylonData[drawMode];
  1099. if (babylonData.meshes.length === 0) {
  1100. babylonData.material.dispose(false, true);
  1101. delete material._babylonData[drawMode];
  1102. }
  1103. }
  1104. }
  1105. }
  1106. }
  1107. };
  1108. return MSFT_lod;
  1109. }());
  1110. exports.MSFT_lod = MSFT_lod;
  1111. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new MSFT_lod(loader); });
  1112. /***/ }),
  1113. /***/ "./src/glTF/2.0/Extensions/MSFT_minecraftMesh.ts":
  1114. /*!*******************************************************!*\
  1115. !*** ./src/glTF/2.0/Extensions/MSFT_minecraftMesh.ts ***!
  1116. \*******************************************************/
  1117. /*! no static exports found */
  1118. /***/ (function(module, exports, __webpack_require__) {
  1119. "use strict";
  1120. Object.defineProperty(exports, "__esModule", { value: true });
  1121. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  1122. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  1123. var NAME = "MSFT_minecraftMesh";
  1124. /** @hidden */
  1125. var MSFT_minecraftMesh = /** @class */ (function () {
  1126. function MSFT_minecraftMesh(loader) {
  1127. this.name = NAME;
  1128. this.enabled = true;
  1129. this._loader = loader;
  1130. }
  1131. MSFT_minecraftMesh.prototype.dispose = function () {
  1132. delete this._loader;
  1133. };
  1134. MSFT_minecraftMesh.prototype.loadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
  1135. var _this = this;
  1136. return glTFLoader_1.GLTFLoaderV2.LoadExtraAsync(context, material, this.name, function (extraContext, extra) {
  1137. if (extra) {
  1138. if (!(babylonMaterial instanceof babylonjs_1.PBRMaterial)) {
  1139. throw new Error(extraContext + ": Material type not supported");
  1140. }
  1141. var promise = _this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial);
  1142. if (babylonMaterial.needAlphaBlending()) {
  1143. babylonMaterial.forceDepthWrite = true;
  1144. babylonMaterial.separateCullingPass = true;
  1145. }
  1146. babylonMaterial.backFaceCulling = babylonMaterial.forceDepthWrite;
  1147. babylonMaterial.twoSidedLighting = true;
  1148. return promise;
  1149. }
  1150. return null;
  1151. });
  1152. };
  1153. return MSFT_minecraftMesh;
  1154. }());
  1155. exports.MSFT_minecraftMesh = MSFT_minecraftMesh;
  1156. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new MSFT_minecraftMesh(loader); });
  1157. /***/ }),
  1158. /***/ "./src/glTF/2.0/Extensions/MSFT_sRGBFactors.ts":
  1159. /*!*****************************************************!*\
  1160. !*** ./src/glTF/2.0/Extensions/MSFT_sRGBFactors.ts ***!
  1161. \*****************************************************/
  1162. /*! no static exports found */
  1163. /***/ (function(module, exports, __webpack_require__) {
  1164. "use strict";
  1165. Object.defineProperty(exports, "__esModule", { value: true });
  1166. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  1167. var glTFLoader_1 = __webpack_require__(/*! ../glTFLoader */ "./src/glTF/2.0/glTFLoader.ts");
  1168. var NAME = "MSFT_sRGBFactors";
  1169. /** @hidden */
  1170. var MSFT_sRGBFactors = /** @class */ (function () {
  1171. function MSFT_sRGBFactors(loader) {
  1172. this.name = NAME;
  1173. this.enabled = true;
  1174. this._loader = loader;
  1175. }
  1176. MSFT_sRGBFactors.prototype.dispose = function () {
  1177. delete this._loader;
  1178. };
  1179. MSFT_sRGBFactors.prototype.loadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
  1180. var _this = this;
  1181. return glTFLoader_1.GLTFLoaderV2.LoadExtraAsync(context, material, this.name, function (extraContext, extra) {
  1182. if (extra) {
  1183. if (!(babylonMaterial instanceof babylonjs_1.PBRMaterial)) {
  1184. throw new Error(extraContext + ": Material type not supported");
  1185. }
  1186. var promise = _this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial);
  1187. if (!babylonMaterial.albedoTexture) {
  1188. babylonMaterial.albedoColor.toLinearSpaceToRef(babylonMaterial.albedoColor);
  1189. }
  1190. if (!babylonMaterial.reflectivityTexture) {
  1191. babylonMaterial.reflectivityColor.toLinearSpaceToRef(babylonMaterial.reflectivityColor);
  1192. }
  1193. return promise;
  1194. }
  1195. return null;
  1196. });
  1197. };
  1198. return MSFT_sRGBFactors;
  1199. }());
  1200. exports.MSFT_sRGBFactors = MSFT_sRGBFactors;
  1201. glTFLoader_1.GLTFLoaderV2.RegisterExtension(NAME, function (loader) { return new MSFT_sRGBFactors(loader); });
  1202. /***/ }),
  1203. /***/ "./src/glTF/2.0/Extensions/index.ts":
  1204. /*!******************************************!*\
  1205. !*** ./src/glTF/2.0/Extensions/index.ts ***!
  1206. \******************************************/
  1207. /*! no static exports found */
  1208. /***/ (function(module, exports, __webpack_require__) {
  1209. "use strict";
  1210. function __export(m) {
  1211. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  1212. }
  1213. Object.defineProperty(exports, "__esModule", { value: true });
  1214. __export(__webpack_require__(/*! ./EXT_lights_image_based */ "./src/glTF/2.0/Extensions/EXT_lights_image_based.ts"));
  1215. __export(__webpack_require__(/*! ./KHR_draco_mesh_compression */ "./src/glTF/2.0/Extensions/KHR_draco_mesh_compression.ts"));
  1216. __export(__webpack_require__(/*! ./KHR_lights_punctual */ "./src/glTF/2.0/Extensions/KHR_lights_punctual.ts"));
  1217. __export(__webpack_require__(/*! ./KHR_materials_pbrSpecularGlossiness */ "./src/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.ts"));
  1218. __export(__webpack_require__(/*! ./KHR_materials_unlit */ "./src/glTF/2.0/Extensions/KHR_materials_unlit.ts"));
  1219. __export(__webpack_require__(/*! ./KHR_texture_transform */ "./src/glTF/2.0/Extensions/KHR_texture_transform.ts"));
  1220. __export(__webpack_require__(/*! ./MSFT_audio_emitter */ "./src/glTF/2.0/Extensions/MSFT_audio_emitter.ts"));
  1221. __export(__webpack_require__(/*! ./MSFT_lod */ "./src/glTF/2.0/Extensions/MSFT_lod.ts"));
  1222. __export(__webpack_require__(/*! ./MSFT_minecraftMesh */ "./src/glTF/2.0/Extensions/MSFT_minecraftMesh.ts"));
  1223. __export(__webpack_require__(/*! ./MSFT_sRGBFactors */ "./src/glTF/2.0/Extensions/MSFT_sRGBFactors.ts"));
  1224. /***/ }),
  1225. /***/ "./src/glTF/2.0/glTFLoader.ts":
  1226. /*!************************************!*\
  1227. !*** ./src/glTF/2.0/glTFLoader.ts ***!
  1228. \************************************/
  1229. /*! no static exports found */
  1230. /***/ (function(module, exports, __webpack_require__) {
  1231. "use strict";
  1232. Object.defineProperty(exports, "__esModule", { value: true });
  1233. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  1234. var glTFFileLoader_1 = __webpack_require__(/*! ../glTFFileLoader */ "./src/glTF/glTFFileLoader.ts");
  1235. /**
  1236. * Helper class for working with arrays when loading the glTF asset
  1237. */
  1238. var ArrayItem = /** @class */ (function () {
  1239. function ArrayItem() {
  1240. }
  1241. /**
  1242. * Gets an item from the given array.
  1243. * @param context The context when loading the asset
  1244. * @param array The array to get the item from
  1245. * @param index The index to the array
  1246. * @returns The array item
  1247. */
  1248. ArrayItem.Get = function (context, array, index) {
  1249. if (!array || index == undefined || !array[index]) {
  1250. throw new Error(context + ": Failed to find index (" + index + ")");
  1251. }
  1252. return array[index];
  1253. };
  1254. /**
  1255. * Assign an `index` field to each item of the given array.
  1256. * @param array The array of items
  1257. */
  1258. ArrayItem.Assign = function (array) {
  1259. if (array) {
  1260. for (var index = 0; index < array.length; index++) {
  1261. array[index].index = index;
  1262. }
  1263. }
  1264. };
  1265. return ArrayItem;
  1266. }());
  1267. exports.ArrayItem = ArrayItem;
  1268. /**
  1269. * The glTF 2.0 loader
  1270. */
  1271. var GLTFLoaderV2 = /** @class */ (function () {
  1272. /** @hidden */
  1273. function GLTFLoaderV2(parent) {
  1274. /** @hidden */
  1275. this._completePromises = new Array();
  1276. this._disposed = false;
  1277. this._state = null;
  1278. this._extensions = {};
  1279. this._defaultBabylonMaterialData = {};
  1280. this._requests = new Array();
  1281. this._parent = parent;
  1282. }
  1283. /**
  1284. * Registers a loader extension.
  1285. * @param name The name of the loader extension.
  1286. * @param factory The factory function that creates the loader extension.
  1287. */
  1288. GLTFLoaderV2.RegisterExtension = function (name, factory) {
  1289. if (GLTFLoaderV2.UnregisterExtension(name)) {
  1290. babylonjs_1.Tools.Warn("Extension with the name '" + name + "' already exists");
  1291. }
  1292. GLTFLoaderV2._ExtensionFactories[name] = factory;
  1293. // Keep the order of registration so that extensions registered first are called first.
  1294. GLTFLoaderV2._ExtensionNames.push(name);
  1295. };
  1296. /**
  1297. * Unregisters a loader extension.
  1298. * @param name The name of the loader extenion.
  1299. * @returns A boolean indicating whether the extension has been unregistered
  1300. */
  1301. GLTFLoaderV2.UnregisterExtension = function (name) {
  1302. if (!GLTFLoaderV2._ExtensionFactories[name]) {
  1303. return false;
  1304. }
  1305. delete GLTFLoaderV2._ExtensionFactories[name];
  1306. var index = GLTFLoaderV2._ExtensionNames.indexOf(name);
  1307. if (index !== -1) {
  1308. GLTFLoaderV2._ExtensionNames.splice(index, 1);
  1309. }
  1310. return true;
  1311. };
  1312. Object.defineProperty(GLTFLoaderV2.prototype, "state", {
  1313. /**
  1314. * Gets the loader state.
  1315. */
  1316. get: function () {
  1317. return this._state;
  1318. },
  1319. enumerable: true,
  1320. configurable: true
  1321. });
  1322. /** @hidden */
  1323. GLTFLoaderV2.prototype.dispose = function () {
  1324. if (this._disposed) {
  1325. return;
  1326. }
  1327. this._disposed = true;
  1328. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  1329. var request = _a[_i];
  1330. request.abort();
  1331. }
  1332. this._requests.length = 0;
  1333. delete this.gltf;
  1334. delete this.babylonScene;
  1335. this._completePromises.length = 0;
  1336. for (var name_1 in this._extensions) {
  1337. var extension = this._extensions[name_1];
  1338. if (extension.dispose) {
  1339. this._extensions[name_1].dispose();
  1340. }
  1341. }
  1342. this._extensions = {};
  1343. delete this._rootBabylonMesh;
  1344. delete this._progressCallback;
  1345. this._parent._clear();
  1346. };
  1347. /** @hidden */
  1348. GLTFLoaderV2.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress, fileName) {
  1349. var _this = this;
  1350. return Promise.resolve().then(function () {
  1351. _this.babylonScene = scene;
  1352. _this._rootUrl = rootUrl;
  1353. _this._fileName = fileName || "scene";
  1354. _this._progressCallback = onProgress;
  1355. _this._loadData(data);
  1356. var nodes = null;
  1357. if (meshesNames) {
  1358. var nodeMap_1 = {};
  1359. if (_this.gltf.nodes) {
  1360. for (var _i = 0, _a = _this.gltf.nodes; _i < _a.length; _i++) {
  1361. var node = _a[_i];
  1362. if (node.name) {
  1363. nodeMap_1[node.name] = node.index;
  1364. }
  1365. }
  1366. }
  1367. var names = (meshesNames instanceof Array) ? meshesNames : [meshesNames];
  1368. nodes = names.map(function (name) {
  1369. var node = nodeMap_1[name];
  1370. if (node === undefined) {
  1371. throw new Error("Failed to find node '" + name + "'");
  1372. }
  1373. return node;
  1374. });
  1375. }
  1376. return _this._loadAsync(nodes, function () {
  1377. return {
  1378. meshes: _this._getMeshes(),
  1379. particleSystems: [],
  1380. skeletons: _this._getSkeletons(),
  1381. animationGroups: _this._getAnimationGroups()
  1382. };
  1383. });
  1384. });
  1385. };
  1386. /** @hidden */
  1387. GLTFLoaderV2.prototype.loadAsync = function (scene, data, rootUrl, onProgress, fileName) {
  1388. var _this = this;
  1389. return Promise.resolve().then(function () {
  1390. _this.babylonScene = scene;
  1391. _this._rootUrl = rootUrl;
  1392. _this._fileName = fileName || "scene";
  1393. _this._progressCallback = onProgress;
  1394. _this._loadData(data);
  1395. return _this._loadAsync(null, function () { return undefined; });
  1396. });
  1397. };
  1398. GLTFLoaderV2.prototype._loadAsync = function (nodes, resultFunc) {
  1399. var _this = this;
  1400. return Promise.resolve().then(function () {
  1401. _this._uniqueRootUrl = (_this._rootUrl.indexOf("file:") === -1 && _this._fileName) ? _this._rootUrl : "" + _this._rootUrl + Date.now() + "/";
  1402. _this._loadExtensions();
  1403. _this._checkExtensions();
  1404. var loadingToReadyCounterName = glTFFileLoader_1.GLTFLoaderState[glTFFileLoader_1.GLTFLoaderState.LOADING] + " => " + glTFFileLoader_1.GLTFLoaderState[glTFFileLoader_1.GLTFLoaderState.READY];
  1405. var loadingToCompleteCounterName = glTFFileLoader_1.GLTFLoaderState[glTFFileLoader_1.GLTFLoaderState.LOADING] + " => " + glTFFileLoader_1.GLTFLoaderState[glTFFileLoader_1.GLTFLoaderState.COMPLETE];
  1406. _this._parent._startPerformanceCounter(loadingToReadyCounterName);
  1407. _this._parent._startPerformanceCounter(loadingToCompleteCounterName);
  1408. _this._setState(glTFFileLoader_1.GLTFLoaderState.LOADING);
  1409. _this._extensionsOnLoading();
  1410. var promises = new Array();
  1411. if (nodes) {
  1412. promises.push(_this.loadSceneAsync("#/nodes", { nodes: nodes, index: -1 }));
  1413. }
  1414. else {
  1415. var scene = ArrayItem.Get("#/scene", _this.gltf.scenes, _this.gltf.scene || 0);
  1416. promises.push(_this.loadSceneAsync("#/scenes/" + scene.index, scene));
  1417. }
  1418. if (_this._parent.compileMaterials) {
  1419. promises.push(_this._compileMaterialsAsync());
  1420. }
  1421. if (_this._parent.compileShadowGenerators) {
  1422. promises.push(_this._compileShadowGeneratorsAsync());
  1423. }
  1424. var resultPromise = Promise.all(promises).then(function () {
  1425. _this._setState(glTFFileLoader_1.GLTFLoaderState.READY);
  1426. _this._extensionsOnReady();
  1427. _this._startAnimations();
  1428. return resultFunc();
  1429. });
  1430. resultPromise.then(function () {
  1431. _this._parent._endPerformanceCounter(loadingToReadyCounterName);
  1432. babylonjs_1.Tools.SetImmediate(function () {
  1433. if (!_this._disposed) {
  1434. Promise.all(_this._completePromises).then(function () {
  1435. _this._parent._endPerformanceCounter(loadingToCompleteCounterName);
  1436. _this._setState(glTFFileLoader_1.GLTFLoaderState.COMPLETE);
  1437. _this._parent.onCompleteObservable.notifyObservers(undefined);
  1438. _this._parent.onCompleteObservable.clear();
  1439. _this.dispose();
  1440. }, function (error) {
  1441. _this._parent.onErrorObservable.notifyObservers(error);
  1442. _this._parent.onErrorObservable.clear();
  1443. _this.dispose();
  1444. });
  1445. }
  1446. });
  1447. });
  1448. return resultPromise;
  1449. }, function (error) {
  1450. if (!_this._disposed) {
  1451. _this._parent.onErrorObservable.notifyObservers(error);
  1452. _this._parent.onErrorObservable.clear();
  1453. _this.dispose();
  1454. }
  1455. throw error;
  1456. });
  1457. };
  1458. GLTFLoaderV2.prototype._loadData = function (data) {
  1459. this.gltf = data.json;
  1460. this._setupData();
  1461. if (data.bin) {
  1462. var buffers = this.gltf.buffers;
  1463. if (buffers && buffers[0] && !buffers[0].uri) {
  1464. var binaryBuffer = buffers[0];
  1465. if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
  1466. babylonjs_1.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  1467. }
  1468. binaryBuffer._data = Promise.resolve(data.bin);
  1469. }
  1470. else {
  1471. babylonjs_1.Tools.Warn("Unexpected BIN chunk");
  1472. }
  1473. }
  1474. };
  1475. GLTFLoaderV2.prototype._setupData = function () {
  1476. ArrayItem.Assign(this.gltf.accessors);
  1477. ArrayItem.Assign(this.gltf.animations);
  1478. ArrayItem.Assign(this.gltf.buffers);
  1479. ArrayItem.Assign(this.gltf.bufferViews);
  1480. ArrayItem.Assign(this.gltf.cameras);
  1481. ArrayItem.Assign(this.gltf.images);
  1482. ArrayItem.Assign(this.gltf.materials);
  1483. ArrayItem.Assign(this.gltf.meshes);
  1484. ArrayItem.Assign(this.gltf.nodes);
  1485. ArrayItem.Assign(this.gltf.samplers);
  1486. ArrayItem.Assign(this.gltf.scenes);
  1487. ArrayItem.Assign(this.gltf.skins);
  1488. ArrayItem.Assign(this.gltf.textures);
  1489. if (this.gltf.nodes) {
  1490. var nodeParents = {};
  1491. for (var _i = 0, _a = this.gltf.nodes; _i < _a.length; _i++) {
  1492. var node = _a[_i];
  1493. if (node.children) {
  1494. for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
  1495. var index = _c[_b];
  1496. nodeParents[index] = node.index;
  1497. }
  1498. }
  1499. }
  1500. var rootNode = this._createRootNode();
  1501. for (var _d = 0, _e = this.gltf.nodes; _d < _e.length; _d++) {
  1502. var node = _e[_d];
  1503. var parentIndex = nodeParents[node.index];
  1504. node.parent = parentIndex === undefined ? rootNode : this.gltf.nodes[parentIndex];
  1505. }
  1506. }
  1507. };
  1508. GLTFLoaderV2.prototype._loadExtensions = function () {
  1509. for (var _i = 0, _a = GLTFLoaderV2._ExtensionNames; _i < _a.length; _i++) {
  1510. var name_2 = _a[_i];
  1511. var extension = GLTFLoaderV2._ExtensionFactories[name_2](this);
  1512. this._extensions[name_2] = extension;
  1513. this._parent.onExtensionLoadedObservable.notifyObservers(extension);
  1514. }
  1515. this._parent.onExtensionLoadedObservable.clear();
  1516. };
  1517. GLTFLoaderV2.prototype._checkExtensions = function () {
  1518. if (this.gltf.extensionsRequired) {
  1519. for (var _i = 0, _a = this.gltf.extensionsRequired; _i < _a.length; _i++) {
  1520. var name_3 = _a[_i];
  1521. var extension = this._extensions[name_3];
  1522. if (!extension || !extension.enabled) {
  1523. throw new Error("Require extension " + name_3 + " is not available");
  1524. }
  1525. }
  1526. }
  1527. };
  1528. GLTFLoaderV2.prototype._setState = function (state) {
  1529. this._state = state;
  1530. this.log(glTFFileLoader_1.GLTFLoaderState[this._state]);
  1531. };
  1532. GLTFLoaderV2.prototype._createRootNode = function () {
  1533. this._rootBabylonMesh = new babylonjs_1.Mesh("__root__", this.babylonScene);
  1534. var rootNode = {
  1535. _babylonMesh: this._rootBabylonMesh,
  1536. index: -1
  1537. };
  1538. switch (this._parent.coordinateSystemMode) {
  1539. case glTFFileLoader_1.GLTFLoaderCoordinateSystemMode.AUTO: {
  1540. if (!this.babylonScene.useRightHandedSystem) {
  1541. rootNode.rotation = [0, 1, 0, 0];
  1542. rootNode.scale = [1, 1, -1];
  1543. GLTFLoaderV2._LoadTransform(rootNode, this._rootBabylonMesh);
  1544. }
  1545. break;
  1546. }
  1547. case glTFFileLoader_1.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
  1548. this.babylonScene.useRightHandedSystem = true;
  1549. break;
  1550. }
  1551. default: {
  1552. throw new Error("Invalid coordinate system mode (" + this._parent.coordinateSystemMode + ")");
  1553. }
  1554. }
  1555. this._parent.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
  1556. return rootNode;
  1557. };
  1558. /**
  1559. * Loads a glTF scene.
  1560. * @param context The context when loading the asset
  1561. * @param scene The glTF scene property
  1562. * @returns A promise that resolves when the load is complete
  1563. */
  1564. GLTFLoaderV2.prototype.loadSceneAsync = function (context, scene) {
  1565. var _this = this;
  1566. var extensionPromise = this._extensionsLoadSceneAsync(context, scene);
  1567. if (extensionPromise) {
  1568. return extensionPromise;
  1569. }
  1570. var promises = new Array();
  1571. this.logOpen(context + " " + (scene.name || ""));
  1572. if (scene.nodes) {
  1573. for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
  1574. var index = _a[_i];
  1575. var node = ArrayItem.Get(context + "/nodes/" + index, this.gltf.nodes, index);
  1576. promises.push(this.loadNodeAsync("#/nodes/" + node.index, node, function (babylonMesh) {
  1577. babylonMesh.parent = _this._rootBabylonMesh;
  1578. }));
  1579. }
  1580. }
  1581. promises.push(this._loadAnimationsAsync());
  1582. this.logClose();
  1583. return Promise.all(promises).then(function () { });
  1584. };
  1585. GLTFLoaderV2.prototype._forEachPrimitive = function (node, callback) {
  1586. if (node._primitiveBabylonMeshes) {
  1587. for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
  1588. var babylonMesh = _a[_i];
  1589. callback(babylonMesh);
  1590. }
  1591. }
  1592. else {
  1593. callback(node._babylonMesh);
  1594. }
  1595. };
  1596. GLTFLoaderV2.prototype._getMeshes = function () {
  1597. var meshes = new Array();
  1598. // Root mesh is always first.
  1599. meshes.push(this._rootBabylonMesh);
  1600. var nodes = this.gltf.nodes;
  1601. if (nodes) {
  1602. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  1603. var node = nodes_1[_i];
  1604. if (node._babylonMesh) {
  1605. meshes.push(node._babylonMesh);
  1606. }
  1607. if (node._primitiveBabylonMeshes) {
  1608. for (var _a = 0, _b = node._primitiveBabylonMeshes; _a < _b.length; _a++) {
  1609. var babylonMesh = _b[_a];
  1610. meshes.push(babylonMesh);
  1611. }
  1612. }
  1613. }
  1614. }
  1615. return meshes;
  1616. };
  1617. GLTFLoaderV2.prototype._getSkeletons = function () {
  1618. var skeletons = new Array();
  1619. var skins = this.gltf.skins;
  1620. if (skins) {
  1621. for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
  1622. var skin = skins_1[_i];
  1623. if (skin._babylonSkeleton) {
  1624. skeletons.push(skin._babylonSkeleton);
  1625. }
  1626. }
  1627. }
  1628. return skeletons;
  1629. };
  1630. GLTFLoaderV2.prototype._getAnimationGroups = function () {
  1631. var animationGroups = new Array();
  1632. var animations = this.gltf.animations;
  1633. if (animations) {
  1634. for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
  1635. var animation = animations_1[_i];
  1636. if (animation._babylonAnimationGroup) {
  1637. animationGroups.push(animation._babylonAnimationGroup);
  1638. }
  1639. }
  1640. }
  1641. return animationGroups;
  1642. };
  1643. GLTFLoaderV2.prototype._startAnimations = function () {
  1644. switch (this._parent.animationStartMode) {
  1645. case glTFFileLoader_1.GLTFLoaderAnimationStartMode.NONE: {
  1646. // do nothing
  1647. break;
  1648. }
  1649. case glTFFileLoader_1.GLTFLoaderAnimationStartMode.FIRST: {
  1650. var babylonAnimationGroups = this._getAnimationGroups();
  1651. if (babylonAnimationGroups.length !== 0) {
  1652. babylonAnimationGroups[0].start(true);
  1653. }
  1654. break;
  1655. }
  1656. case glTFFileLoader_1.GLTFLoaderAnimationStartMode.ALL: {
  1657. var babylonAnimationGroups = this._getAnimationGroups();
  1658. for (var _i = 0, babylonAnimationGroups_1 = babylonAnimationGroups; _i < babylonAnimationGroups_1.length; _i++) {
  1659. var babylonAnimationGroup = babylonAnimationGroups_1[_i];
  1660. babylonAnimationGroup.start(true);
  1661. }
  1662. break;
  1663. }
  1664. default: {
  1665. babylonjs_1.Tools.Error("Invalid animation start mode (" + this._parent.animationStartMode + ")");
  1666. return;
  1667. }
  1668. }
  1669. };
  1670. /**
  1671. * Loads a glTF node.
  1672. * @param context The context when loading the asset
  1673. * @param node The glTF node property
  1674. * @param assign A function called synchronously after parsing the glTF properties
  1675. * @returns A promise that resolves with the loaded Babylon mesh when the load is complete
  1676. */
  1677. GLTFLoaderV2.prototype.loadNodeAsync = function (context, node, assign) {
  1678. var _this = this;
  1679. if (assign === void 0) { assign = function () { }; }
  1680. var extensionPromise = this._extensionsLoadNodeAsync(context, node, assign);
  1681. if (extensionPromise) {
  1682. return extensionPromise;
  1683. }
  1684. if (node._babylonMesh) {
  1685. throw new Error(context + ": Invalid recursive node hierarchy");
  1686. }
  1687. var promises = new Array();
  1688. this.logOpen(context + " " + (node.name || ""));
  1689. var babylonMesh = new babylonjs_1.Mesh(node.name || "node" + node.index, this.babylonScene);
  1690. node._babylonMesh = babylonMesh;
  1691. babylonMesh.setEnabled(false);
  1692. GLTFLoaderV2._LoadTransform(node, babylonMesh);
  1693. if (node.mesh != undefined) {
  1694. var mesh = ArrayItem.Get(context + "/mesh", this.gltf.meshes, node.mesh);
  1695. promises.push(this._loadMeshAsync("#/meshes/" + mesh.index, node, mesh, babylonMesh));
  1696. }
  1697. if (node.camera != undefined) {
  1698. var camera = ArrayItem.Get(context + "/camera", this.gltf.cameras, node.camera);
  1699. promises.push(this.loadCameraAsync("#/cameras/" + camera.index, camera, function (babylonCamera) {
  1700. babylonCamera.parent = babylonMesh;
  1701. }));
  1702. }
  1703. if (node.children) {
  1704. var _loop_1 = function (index) {
  1705. var childNode = ArrayItem.Get(context + "/children/" + index, this_1.gltf.nodes, index);
  1706. promises.push(this_1.loadNodeAsync("#/nodes/" + node.index, childNode, function (childBabylonMesh) {
  1707. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  1708. if (childNode.skin != undefined) {
  1709. childBabylonMesh.parent = _this._rootBabylonMesh;
  1710. return;
  1711. }
  1712. childBabylonMesh.parent = babylonMesh;
  1713. }));
  1714. };
  1715. var this_1 = this;
  1716. for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
  1717. var index = _a[_i];
  1718. _loop_1(index);
  1719. }
  1720. }
  1721. assign(babylonMesh);
  1722. this._parent.onMeshLoadedObservable.notifyObservers(babylonMesh);
  1723. this.logClose();
  1724. return Promise.all(promises).then(function () {
  1725. babylonMesh.setEnabled(true);
  1726. return babylonMesh;
  1727. });
  1728. };
  1729. GLTFLoaderV2.prototype._loadMeshAsync = function (context, node, mesh, babylonMesh) {
  1730. var _this = this;
  1731. var promises = new Array();
  1732. this.logOpen(context + " " + (mesh.name || ""));
  1733. var primitives = mesh.primitives;
  1734. if (!primitives || primitives.length === 0) {
  1735. throw new Error(context + ": Primitives are missing");
  1736. }
  1737. ArrayItem.Assign(primitives);
  1738. if (primitives.length === 1) {
  1739. var primitive = primitives[0];
  1740. promises.push(this._loadMeshPrimitiveAsync(context + "/primitives/" + primitive.index, node, mesh, primitive, babylonMesh));
  1741. }
  1742. else {
  1743. node._primitiveBabylonMeshes = [];
  1744. for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
  1745. var primitive = primitives_1[_i];
  1746. var primitiveBabylonMesh = new babylonjs_1.Mesh((mesh.name || babylonMesh.name) + "_" + primitive.index, this.babylonScene, babylonMesh);
  1747. node._primitiveBabylonMeshes.push(primitiveBabylonMesh);
  1748. promises.push(this._loadMeshPrimitiveAsync(context + "/primitives/" + primitive.index, node, mesh, primitive, primitiveBabylonMesh));
  1749. this._parent.onMeshLoadedObservable.notifyObservers(babylonMesh);
  1750. }
  1751. }
  1752. if (node.skin != undefined) {
  1753. var skin = ArrayItem.Get(context + "/skin", this.gltf.skins, node.skin);
  1754. promises.push(this._loadSkinAsync("#/skins/" + skin.index, node, skin));
  1755. }
  1756. this.logClose();
  1757. return Promise.all(promises).then(function () {
  1758. _this._forEachPrimitive(node, function (babylonMesh) {
  1759. babylonMesh._refreshBoundingInfo(true);
  1760. });
  1761. });
  1762. };
  1763. GLTFLoaderV2.prototype._loadMeshPrimitiveAsync = function (context, node, mesh, primitive, babylonMesh) {
  1764. var _this = this;
  1765. var promises = new Array();
  1766. this.logOpen("" + context);
  1767. this._createMorphTargets(context, node, mesh, primitive, babylonMesh);
  1768. promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh).then(function (babylonGeometry) {
  1769. return _this._loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry).then(function () {
  1770. babylonGeometry.applyToMesh(babylonMesh);
  1771. });
  1772. }));
  1773. var babylonDrawMode = GLTFLoaderV2._GetDrawMode(context, primitive.mode);
  1774. if (primitive.material == undefined) {
  1775. var babylonMaterial = this._defaultBabylonMaterialData[babylonDrawMode];
  1776. if (!babylonMaterial) {
  1777. babylonMaterial = this._createDefaultMaterial("__gltf_default", babylonDrawMode);
  1778. this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1779. this._defaultBabylonMaterialData[babylonDrawMode] = babylonMaterial;
  1780. }
  1781. babylonMesh.material = babylonMaterial;
  1782. }
  1783. else {
  1784. var material = ArrayItem.Get(context + "/material", this.gltf.materials, primitive.material);
  1785. promises.push(this._loadMaterialAsync("#/materials/" + material.index, material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
  1786. babylonMesh.material = babylonMaterial;
  1787. }));
  1788. }
  1789. this.logClose();
  1790. return Promise.all(promises).then(function () { });
  1791. };
  1792. GLTFLoaderV2.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  1793. var _this = this;
  1794. var extensionPromise = this._extensionsLoadVertexDataAsync(context, primitive, babylonMesh);
  1795. if (extensionPromise) {
  1796. return extensionPromise;
  1797. }
  1798. var attributes = primitive.attributes;
  1799. if (!attributes) {
  1800. throw new Error(context + ": Attributes are missing");
  1801. }
  1802. var promises = new Array();
  1803. var babylonGeometry = new babylonjs_1.Geometry(babylonMesh.name, this.babylonScene);
  1804. if (primitive.indices == undefined) {
  1805. babylonMesh.isUnIndexed = true;
  1806. }
  1807. else {
  1808. var accessor = ArrayItem.Get(context + "/indices", this.gltf.accessors, primitive.indices);
  1809. promises.push(this._loadIndicesAccessorAsync("#/accessors/" + accessor.index, accessor).then(function (data) {
  1810. babylonGeometry.setIndices(data);
  1811. }));
  1812. }
  1813. var loadAttribute = function (attribute, kind, callback) {
  1814. if (attributes[attribute] == undefined) {
  1815. return;
  1816. }
  1817. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  1818. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  1819. babylonMesh._delayInfo.push(kind);
  1820. }
  1821. var accessor = ArrayItem.Get(context + "/attributes/" + attribute, _this.gltf.accessors, attributes[attribute]);
  1822. promises.push(_this._loadVertexAccessorAsync("#/accessors/" + accessor.index, accessor, kind).then(function (babylonVertexBuffer) {
  1823. babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
  1824. }));
  1825. if (callback) {
  1826. callback(accessor);
  1827. }
  1828. };
  1829. loadAttribute("POSITION", babylonjs_1.VertexBuffer.PositionKind);
  1830. loadAttribute("NORMAL", babylonjs_1.VertexBuffer.NormalKind);
  1831. loadAttribute("TANGENT", babylonjs_1.VertexBuffer.TangentKind);
  1832. loadAttribute("TEXCOORD_0", babylonjs_1.VertexBuffer.UVKind);
  1833. loadAttribute("TEXCOORD_1", babylonjs_1.VertexBuffer.UV2Kind);
  1834. loadAttribute("JOINTS_0", babylonjs_1.VertexBuffer.MatricesIndicesKind);
  1835. loadAttribute("WEIGHTS_0", babylonjs_1.VertexBuffer.MatricesWeightsKind);
  1836. loadAttribute("COLOR_0", babylonjs_1.VertexBuffer.ColorKind, function (accessor) {
  1837. if (accessor.type === "VEC4" /* VEC4 */) {
  1838. babylonMesh.hasVertexAlpha = true;
  1839. }
  1840. });
  1841. return Promise.all(promises).then(function () {
  1842. return babylonGeometry;
  1843. });
  1844. };
  1845. GLTFLoaderV2.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
  1846. if (!primitive.targets) {
  1847. return;
  1848. }
  1849. if (node._numMorphTargets == undefined) {
  1850. node._numMorphTargets = primitive.targets.length;
  1851. }
  1852. else if (primitive.targets.length !== node._numMorphTargets) {
  1853. throw new Error(context + ": Primitives do not have the same number of targets");
  1854. }
  1855. babylonMesh.morphTargetManager = new babylonjs_1.MorphTargetManager();
  1856. for (var index = 0; index < primitive.targets.length; index++) {
  1857. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  1858. babylonMesh.morphTargetManager.addTarget(new babylonjs_1.MorphTarget("morphTarget" + index, weight));
  1859. // TODO: tell the target whether it has positions, normals, tangents
  1860. }
  1861. };
  1862. GLTFLoaderV2.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonGeometry) {
  1863. if (!primitive.targets) {
  1864. return Promise.resolve();
  1865. }
  1866. var promises = new Array();
  1867. var morphTargetManager = babylonMesh.morphTargetManager;
  1868. for (var index = 0; index < morphTargetManager.numTargets; index++) {
  1869. var babylonMorphTarget = morphTargetManager.getTarget(index);
  1870. promises.push(this._loadMorphTargetVertexDataAsync(context + "/targets/" + index, babylonGeometry, primitive.targets[index], babylonMorphTarget));
  1871. }
  1872. return Promise.all(promises).then(function () { });
  1873. };
  1874. GLTFLoaderV2.prototype._loadMorphTargetVertexDataAsync = function (context, babylonGeometry, attributes, babylonMorphTarget) {
  1875. var _this = this;
  1876. var promises = new Array();
  1877. var loadAttribute = function (attribute, kind, setData) {
  1878. if (attributes[attribute] == undefined) {
  1879. return;
  1880. }
  1881. var babylonVertexBuffer = babylonGeometry.getVertexBuffer(kind);
  1882. if (!babylonVertexBuffer) {
  1883. return;
  1884. }
  1885. var accessor = ArrayItem.Get(context + "/" + attribute, _this.gltf.accessors, attributes[attribute]);
  1886. promises.push(_this._loadFloatAccessorAsync("#/accessors/" + accessor.index, accessor).then(function (data) {
  1887. setData(babylonVertexBuffer, data);
  1888. }));
  1889. };
  1890. loadAttribute("POSITION", babylonjs_1.VertexBuffer.PositionKind, function (babylonVertexBuffer, data) {
  1891. babylonVertexBuffer.forEach(data.length, function (value, index) {
  1892. data[index] += value;
  1893. });
  1894. babylonMorphTarget.setPositions(data);
  1895. });
  1896. loadAttribute("NORMAL", babylonjs_1.VertexBuffer.NormalKind, function (babylonVertexBuffer, data) {
  1897. babylonVertexBuffer.forEach(data.length, function (value, index) {
  1898. data[index] += value;
  1899. });
  1900. babylonMorphTarget.setNormals(data);
  1901. });
  1902. loadAttribute("TANGENT", babylonjs_1.VertexBuffer.TangentKind, function (babylonVertexBuffer, data) {
  1903. var dataIndex = 0;
  1904. babylonVertexBuffer.forEach(data.length / 3 * 4, function (value, index) {
  1905. // Tangent data for morph targets is stored as xyz delta.
  1906. // The vertexData.tangent is stored as xyzw.
  1907. // So we need to skip every fourth vertexData.tangent.
  1908. if (((index + 1) % 4) !== 0) {
  1909. data[dataIndex++] += value;
  1910. }
  1911. });
  1912. babylonMorphTarget.setTangents(data);
  1913. });
  1914. return Promise.all(promises).then(function () { });
  1915. };
  1916. GLTFLoaderV2._LoadTransform = function (node, babylonNode) {
  1917. var position = babylonjs_1.Vector3.Zero();
  1918. var rotation = babylonjs_1.Quaternion.Identity();
  1919. var scaling = babylonjs_1.Vector3.One();
  1920. if (node.matrix) {
  1921. var matrix = babylonjs_1.Matrix.FromArray(node.matrix);
  1922. matrix.decompose(scaling, rotation, position);
  1923. }
  1924. else {
  1925. if (node.translation) {
  1926. position = babylonjs_1.Vector3.FromArray(node.translation);
  1927. }
  1928. if (node.rotation) {
  1929. rotation = babylonjs_1.Quaternion.FromArray(node.rotation);
  1930. }
  1931. if (node.scale) {
  1932. scaling = babylonjs_1.Vector3.FromArray(node.scale);
  1933. }
  1934. }
  1935. babylonNode.position = position;
  1936. babylonNode.rotationQuaternion = rotation;
  1937. babylonNode.scaling = scaling;
  1938. };
  1939. GLTFLoaderV2.prototype._loadSkinAsync = function (context, node, skin) {
  1940. var _this = this;
  1941. var assignSkeleton = function (skeleton) {
  1942. _this._forEachPrimitive(node, function (babylonMesh) {
  1943. babylonMesh.skeleton = skeleton;
  1944. });
  1945. // Ignore the TRS of skinned nodes.
  1946. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  1947. node._babylonMesh.position = babylonjs_1.Vector3.Zero();
  1948. node._babylonMesh.rotationQuaternion = babylonjs_1.Quaternion.Identity();
  1949. node._babylonMesh.scaling = babylonjs_1.Vector3.One();
  1950. };
  1951. if (skin._promise) {
  1952. return skin._promise.then(function () {
  1953. assignSkeleton(skin._babylonSkeleton);
  1954. });
  1955. }
  1956. var skeletonId = "skeleton" + skin.index;
  1957. var babylonSkeleton = new babylonjs_1.Skeleton(skin.name || skeletonId, skeletonId, this.babylonScene);
  1958. skin._babylonSkeleton = babylonSkeleton;
  1959. this._loadBones(context, skin);
  1960. assignSkeleton(babylonSkeleton);
  1961. return (skin._promise = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
  1962. _this._updateBoneMatrices(babylonSkeleton, inverseBindMatricesData);
  1963. }));
  1964. };
  1965. GLTFLoaderV2.prototype._loadBones = function (context, skin) {
  1966. var babylonBones = {};
  1967. for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
  1968. var index = _a[_i];
  1969. var node = ArrayItem.Get(context + "/joints/" + index, this.gltf.nodes, index);
  1970. this._loadBone(node, skin, babylonBones);
  1971. }
  1972. };
  1973. GLTFLoaderV2.prototype._loadBone = function (node, skin, babylonBones) {
  1974. var babylonBone = babylonBones[node.index];
  1975. if (babylonBone) {
  1976. return babylonBone;
  1977. }
  1978. var babylonParentBone = null;
  1979. if (node.parent && node.parent._babylonMesh !== this._rootBabylonMesh) {
  1980. babylonParentBone = this._loadBone(node.parent, skin, babylonBones);
  1981. }
  1982. var boneIndex = skin.joints.indexOf(node.index);
  1983. babylonBone = new babylonjs_1.Bone(node.name || "joint" + node.index, skin._babylonSkeleton, babylonParentBone, this._getNodeMatrix(node), null, null, boneIndex);
  1984. babylonBones[node.index] = babylonBone;
  1985. node._babylonBones = node._babylonBones || [];
  1986. node._babylonBones.push(babylonBone);
  1987. return babylonBone;
  1988. };
  1989. GLTFLoaderV2.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
  1990. if (skin.inverseBindMatrices == undefined) {
  1991. return Promise.resolve(null);
  1992. }
  1993. var accessor = ArrayItem.Get(context + "/inverseBindMatrices", this.gltf.accessors, skin.inverseBindMatrices);
  1994. return this._loadFloatAccessorAsync("#/accessors/" + accessor.index, accessor);
  1995. };
  1996. GLTFLoaderV2.prototype._updateBoneMatrices = function (babylonSkeleton, inverseBindMatricesData) {
  1997. for (var _i = 0, _a = babylonSkeleton.bones; _i < _a.length; _i++) {
  1998. var babylonBone = _a[_i];
  1999. var baseMatrix = babylonjs_1.Matrix.Identity();
  2000. var boneIndex = babylonBone._index;
  2001. if (inverseBindMatricesData && boneIndex !== -1) {
  2002. babylonjs_1.Matrix.FromArrayToRef(inverseBindMatricesData, boneIndex * 16, baseMatrix);
  2003. baseMatrix.invertToRef(baseMatrix);
  2004. }
  2005. var babylonParentBone = babylonBone.getParent();
  2006. if (babylonParentBone) {
  2007. baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
  2008. }
  2009. babylonBone.updateMatrix(baseMatrix, false, false);
  2010. babylonBone._updateDifferenceMatrix(undefined, false);
  2011. }
  2012. };
  2013. GLTFLoaderV2.prototype._getNodeMatrix = function (node) {
  2014. return node.matrix ?
  2015. babylonjs_1.Matrix.FromArray(node.matrix) :
  2016. babylonjs_1.Matrix.Compose(node.scale ? babylonjs_1.Vector3.FromArray(node.scale) : babylonjs_1.Vector3.One(), node.rotation ? babylonjs_1.Quaternion.FromArray(node.rotation) : babylonjs_1.Quaternion.Identity(), node.translation ? babylonjs_1.Vector3.FromArray(node.translation) : babylonjs_1.Vector3.Zero());
  2017. };
  2018. /**
  2019. * Loads a glTF camera.
  2020. * @param context The context when loading the asset
  2021. * @param camera The glTF camera property
  2022. * @param assign A function called synchronously after parsing the glTF properties
  2023. * @returns A promise that resolves with the loaded Babylon camera when the load is complete
  2024. */
  2025. GLTFLoaderV2.prototype.loadCameraAsync = function (context, camera, assign) {
  2026. if (assign === void 0) { assign = function () { }; }
  2027. var extensionPromise = this._extensionsLoadCameraAsync(context, camera, assign);
  2028. if (extensionPromise) {
  2029. return extensionPromise;
  2030. }
  2031. var promises = new Array();
  2032. this.logOpen(context + " " + (camera.name || ""));
  2033. var babylonCamera = new babylonjs_1.FreeCamera(camera.name || "camera" + camera.index, babylonjs_1.Vector3.Zero(), this.babylonScene, false);
  2034. babylonCamera.rotation = new babylonjs_1.Vector3(0, Math.PI, 0);
  2035. switch (camera.type) {
  2036. case "perspective" /* PERSPECTIVE */: {
  2037. var perspective = camera.perspective;
  2038. if (!perspective) {
  2039. throw new Error(context + ": Camera perspective properties are missing");
  2040. }
  2041. babylonCamera.fov = perspective.yfov;
  2042. babylonCamera.minZ = perspective.znear;
  2043. babylonCamera.maxZ = perspective.zfar || Number.MAX_VALUE;
  2044. break;
  2045. }
  2046. case "orthographic" /* ORTHOGRAPHIC */: {
  2047. if (!camera.orthographic) {
  2048. throw new Error(context + ": Camera orthographic properties are missing");
  2049. }
  2050. babylonCamera.mode = babylonjs_1.Camera.ORTHOGRAPHIC_CAMERA;
  2051. babylonCamera.orthoLeft = -camera.orthographic.xmag;
  2052. babylonCamera.orthoRight = camera.orthographic.xmag;
  2053. babylonCamera.orthoBottom = -camera.orthographic.ymag;
  2054. babylonCamera.orthoTop = camera.orthographic.ymag;
  2055. babylonCamera.minZ = camera.orthographic.znear;
  2056. babylonCamera.maxZ = camera.orthographic.zfar;
  2057. break;
  2058. }
  2059. default: {
  2060. throw new Error(context + ": Invalid camera type (" + camera.type + ")");
  2061. }
  2062. }
  2063. assign(babylonCamera);
  2064. this._parent.onCameraLoadedObservable.notifyObservers(babylonCamera);
  2065. return Promise.all(promises).then(function () {
  2066. return babylonCamera;
  2067. });
  2068. };
  2069. GLTFLoaderV2.prototype._loadAnimationsAsync = function () {
  2070. var animations = this.gltf.animations;
  2071. if (!animations) {
  2072. return Promise.resolve();
  2073. }
  2074. var promises = new Array();
  2075. for (var index = 0; index < animations.length; index++) {
  2076. var animation = animations[index];
  2077. promises.push(this.loadAnimationAsync("#/animations/" + animation.index, animation));
  2078. }
  2079. return Promise.all(promises).then(function () { });
  2080. };
  2081. /**
  2082. * Loads a glTF animation.
  2083. * @param context The context when loading the asset
  2084. * @param animation The glTF animation property
  2085. * @returns A promise that resolves with the loaded Babylon animation group when the load is complete
  2086. */
  2087. GLTFLoaderV2.prototype.loadAnimationAsync = function (context, animation) {
  2088. var promise = this._extensionsLoadAnimationAsync(context, animation);
  2089. if (promise) {
  2090. return promise;
  2091. }
  2092. var babylonAnimationGroup = new babylonjs_1.AnimationGroup(animation.name || "animation" + animation.index, this.babylonScene);
  2093. animation._babylonAnimationGroup = babylonAnimationGroup;
  2094. var promises = new Array();
  2095. ArrayItem.Assign(animation.channels);
  2096. ArrayItem.Assign(animation.samplers);
  2097. for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
  2098. var channel = _a[_i];
  2099. promises.push(this._loadAnimationChannelAsync(context + "/channels/" + channel.index, context, animation, channel, babylonAnimationGroup));
  2100. }
  2101. return Promise.all(promises).then(function () {
  2102. babylonAnimationGroup.normalize(0);
  2103. return babylonAnimationGroup;
  2104. });
  2105. };
  2106. GLTFLoaderV2.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup) {
  2107. var _this = this;
  2108. var targetNode = ArrayItem.Get(context + "/target/node", this.gltf.nodes, channel.target.node);
  2109. // Ignore animations that have no animation targets.
  2110. if ((channel.target.path === "weights" /* WEIGHTS */ && !targetNode._numMorphTargets) ||
  2111. (channel.target.path !== "weights" /* WEIGHTS */ && !targetNode._babylonMesh)) {
  2112. return Promise.resolve();
  2113. }
  2114. // Ignore animations targeting TRS of skinned nodes.
  2115. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  2116. if (targetNode.skin != undefined && channel.target.path !== "weights" /* WEIGHTS */) {
  2117. return Promise.resolve();
  2118. }
  2119. var sampler = ArrayItem.Get(context + "/sampler", animation.samplers, channel.sampler);
  2120. return this._loadAnimationSamplerAsync(animationContext + "/samplers/" + channel.sampler, sampler).then(function (data) {
  2121. var targetPath;
  2122. var animationType;
  2123. switch (channel.target.path) {
  2124. case "translation" /* TRANSLATION */: {
  2125. targetPath = "position";
  2126. animationType = babylonjs_1.Animation.ANIMATIONTYPE_VECTOR3;
  2127. break;
  2128. }
  2129. case "rotation" /* ROTATION */: {
  2130. targetPath = "rotationQuaternion";
  2131. animationType = babylonjs_1.Animation.ANIMATIONTYPE_QUATERNION;
  2132. break;
  2133. }
  2134. case "scale" /* SCALE */: {
  2135. targetPath = "scaling";
  2136. animationType = babylonjs_1.Animation.ANIMATIONTYPE_VECTOR3;
  2137. break;
  2138. }
  2139. case "weights" /* WEIGHTS */: {
  2140. targetPath = "influence";
  2141. animationType = babylonjs_1.Animation.ANIMATIONTYPE_FLOAT;
  2142. break;
  2143. }
  2144. default: {
  2145. throw new Error(context + "/target/path: Invalid value (" + channel.target.path + ")");
  2146. }
  2147. }
  2148. var outputBufferOffset = 0;
  2149. var getNextOutputValue;
  2150. switch (targetPath) {
  2151. case "position": {
  2152. getNextOutputValue = function () {
  2153. var value = babylonjs_1.Vector3.FromArray(data.output, outputBufferOffset);
  2154. outputBufferOffset += 3;
  2155. return value;
  2156. };
  2157. break;
  2158. }
  2159. case "rotationQuaternion": {
  2160. getNextOutputValue = function () {
  2161. var value = babylonjs_1.Quaternion.FromArray(data.output, outputBufferOffset);
  2162. outputBufferOffset += 4;
  2163. return value;
  2164. };
  2165. break;
  2166. }
  2167. case "scaling": {
  2168. getNextOutputValue = function () {
  2169. var value = babylonjs_1.Vector3.FromArray(data.output, outputBufferOffset);
  2170. outputBufferOffset += 3;
  2171. return value;
  2172. };
  2173. break;
  2174. }
  2175. case "influence": {
  2176. getNextOutputValue = function () {
  2177. var value = new Array(targetNode._numMorphTargets);
  2178. for (var i = 0; i < targetNode._numMorphTargets; i++) {
  2179. value[i] = data.output[outputBufferOffset++];
  2180. }
  2181. return value;
  2182. };
  2183. break;
  2184. }
  2185. }
  2186. var getNextKey;
  2187. switch (data.interpolation) {
  2188. case "STEP" /* STEP */: {
  2189. getNextKey = function (frameIndex) { return ({
  2190. frame: data.input[frameIndex],
  2191. value: getNextOutputValue(),
  2192. interpolation: babylonjs_1.AnimationKeyInterpolation.STEP
  2193. }); };
  2194. break;
  2195. }
  2196. case "LINEAR" /* LINEAR */: {
  2197. getNextKey = function (frameIndex) { return ({
  2198. frame: data.input[frameIndex],
  2199. value: getNextOutputValue()
  2200. }); };
  2201. break;
  2202. }
  2203. case "CUBICSPLINE" /* CUBICSPLINE */: {
  2204. getNextKey = function (frameIndex) { return ({
  2205. frame: data.input[frameIndex],
  2206. inTangent: getNextOutputValue(),
  2207. value: getNextOutputValue(),
  2208. outTangent: getNextOutputValue()
  2209. }); };
  2210. break;
  2211. }
  2212. }
  2213. var keys = new Array(data.input.length);
  2214. for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
  2215. keys[frameIndex] = getNextKey(frameIndex);
  2216. }
  2217. if (targetPath === "influence") {
  2218. var _loop_2 = function (targetIndex) {
  2219. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  2220. var babylonAnimation = new babylonjs_1.Animation(animationName, targetPath, 1, animationType);
  2221. babylonAnimation.setKeys(keys.map(function (key) { return ({
  2222. frame: key.frame,
  2223. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  2224. value: key.value[targetIndex],
  2225. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  2226. }); }));
  2227. _this._forEachPrimitive(targetNode, function (babylonMesh) {
  2228. var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
  2229. var babylonAnimationClone = babylonAnimation.clone();
  2230. morphTarget.animations.push(babylonAnimationClone);
  2231. babylonAnimationGroup.addTargetedAnimation(babylonAnimationClone, morphTarget);
  2232. });
  2233. };
  2234. for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
  2235. _loop_2(targetIndex);
  2236. }
  2237. }
  2238. else {
  2239. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  2240. var babylonAnimation = new babylonjs_1.Animation(animationName, targetPath, 1, animationType);
  2241. babylonAnimation.setKeys(keys);
  2242. if (targetNode._babylonBones) {
  2243. var babylonAnimationTargets = [targetNode._babylonMesh].concat(targetNode._babylonBones);
  2244. for (var _i = 0, babylonAnimationTargets_1 = babylonAnimationTargets; _i < babylonAnimationTargets_1.length; _i++) {
  2245. var babylonAnimationTarget = babylonAnimationTargets_1[_i];
  2246. babylonAnimationTarget.animations.push(babylonAnimation);
  2247. }
  2248. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, babylonAnimationTargets);
  2249. }
  2250. else {
  2251. targetNode._babylonMesh.animations.push(babylonAnimation);
  2252. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, targetNode._babylonMesh);
  2253. }
  2254. }
  2255. });
  2256. };
  2257. GLTFLoaderV2.prototype._loadAnimationSamplerAsync = function (context, sampler) {
  2258. if (sampler._data) {
  2259. return sampler._data;
  2260. }
  2261. var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
  2262. switch (interpolation) {
  2263. case "STEP" /* STEP */:
  2264. case "LINEAR" /* LINEAR */:
  2265. case "CUBICSPLINE" /* CUBICSPLINE */: {
  2266. break;
  2267. }
  2268. default: {
  2269. throw new Error(context + "/interpolation: Invalid value (" + sampler.interpolation + ")");
  2270. }
  2271. }
  2272. var inputAccessor = ArrayItem.Get(context + "/input", this.gltf.accessors, sampler.input);
  2273. var outputAccessor = ArrayItem.Get(context + "/output", this.gltf.accessors, sampler.output);
  2274. sampler._data = Promise.all([
  2275. this._loadFloatAccessorAsync("#/accessors/" + inputAccessor.index, inputAccessor),
  2276. this._loadFloatAccessorAsync("#/accessors/" + outputAccessor.index, outputAccessor)
  2277. ]).then(function (_a) {
  2278. var inputData = _a[0], outputData = _a[1];
  2279. return {
  2280. input: inputData,
  2281. interpolation: interpolation,
  2282. output: outputData,
  2283. };
  2284. });
  2285. return sampler._data;
  2286. };
  2287. GLTFLoaderV2.prototype._loadBufferAsync = function (context, buffer) {
  2288. if (buffer._data) {
  2289. return buffer._data;
  2290. }
  2291. if (!buffer.uri) {
  2292. throw new Error(context + "/uri: Value is missing");
  2293. }
  2294. buffer._data = this.loadUriAsync(context + "/uri", buffer.uri);
  2295. return buffer._data;
  2296. };
  2297. /**
  2298. * Loads a glTF buffer view.
  2299. * @param context The context when loading the asset
  2300. * @param bufferView The glTF buffer view property
  2301. * @returns A promise that resolves with the loaded data when the load is complete
  2302. */
  2303. GLTFLoaderV2.prototype.loadBufferViewAsync = function (context, bufferView) {
  2304. if (bufferView._data) {
  2305. return bufferView._data;
  2306. }
  2307. var buffer = ArrayItem.Get(context + "/buffer", this.gltf.buffers, bufferView.buffer);
  2308. bufferView._data = this._loadBufferAsync("#/buffers/" + buffer.index, buffer).then(function (data) {
  2309. try {
  2310. return new Uint8Array(data.buffer, data.byteOffset + (bufferView.byteOffset || 0), bufferView.byteLength);
  2311. }
  2312. catch (e) {
  2313. throw new Error(context + ": " + e.message);
  2314. }
  2315. });
  2316. return bufferView._data;
  2317. };
  2318. GLTFLoaderV2.prototype._loadIndicesAccessorAsync = function (context, accessor) {
  2319. if (accessor.type !== "SCALAR" /* SCALAR */) {
  2320. throw new Error(context + "/type: Invalid value " + accessor.type);
  2321. }
  2322. if (accessor.componentType !== 5121 /* UNSIGNED_BYTE */ &&
  2323. accessor.componentType !== 5123 /* UNSIGNED_SHORT */ &&
  2324. accessor.componentType !== 5125 /* UNSIGNED_INT */) {
  2325. throw new Error(context + "/componentType: Invalid value " + accessor.componentType);
  2326. }
  2327. if (accessor._data) {
  2328. return accessor._data;
  2329. }
  2330. var bufferView = ArrayItem.Get(context + "/bufferView", this.gltf.bufferViews, accessor.bufferView);
  2331. accessor._data = this.loadBufferViewAsync("#/bufferViews/" + bufferView.index, bufferView).then(function (data) {
  2332. return GLTFLoaderV2._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, accessor.count);
  2333. });
  2334. return accessor._data;
  2335. };
  2336. GLTFLoaderV2.prototype._loadFloatAccessorAsync = function (context, accessor) {
  2337. // TODO: support normalized and stride
  2338. var _this = this;
  2339. if (accessor.componentType !== 5126 /* FLOAT */) {
  2340. throw new Error("Invalid component type " + accessor.componentType);
  2341. }
  2342. if (accessor._data) {
  2343. return accessor._data;
  2344. }
  2345. var numComponents = GLTFLoaderV2._GetNumComponents(context, accessor.type);
  2346. var length = numComponents * accessor.count;
  2347. if (accessor.bufferView == undefined) {
  2348. accessor._data = Promise.resolve(new Float32Array(length));
  2349. }
  2350. else {
  2351. var bufferView = ArrayItem.Get(context + "/bufferView", this.gltf.bufferViews, accessor.bufferView);
  2352. accessor._data = this.loadBufferViewAsync("#/bufferViews/" + bufferView.index, bufferView).then(function (data) {
  2353. return GLTFLoaderV2._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, length);
  2354. });
  2355. }
  2356. if (accessor.sparse) {
  2357. var sparse_1 = accessor.sparse;
  2358. accessor._data = accessor._data.then(function (data) {
  2359. var indicesBufferView = ArrayItem.Get(context + "/sparse/indices/bufferView", _this.gltf.bufferViews, sparse_1.indices.bufferView);
  2360. var valuesBufferView = ArrayItem.Get(context + "/sparse/values/bufferView", _this.gltf.bufferViews, sparse_1.values.bufferView);
  2361. return Promise.all([
  2362. _this.loadBufferViewAsync("#/bufferViews/" + indicesBufferView.index, indicesBufferView),
  2363. _this.loadBufferViewAsync("#/bufferViews/" + valuesBufferView.index, valuesBufferView)
  2364. ]).then(function (_a) {
  2365. var indicesData = _a[0], valuesData = _a[1];
  2366. var indices = GLTFLoaderV2._GetTypedArray(context + "/sparse/indices", sparse_1.indices.componentType, indicesData, sparse_1.indices.byteOffset, sparse_1.count);
  2367. var values = GLTFLoaderV2._GetTypedArray(context + "/sparse/values", accessor.componentType, valuesData, sparse_1.values.byteOffset, numComponents * sparse_1.count);
  2368. var valuesIndex = 0;
  2369. for (var indicesIndex = 0; indicesIndex < indices.length; indicesIndex++) {
  2370. var dataIndex = indices[indicesIndex] * numComponents;
  2371. for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
  2372. data[dataIndex++] = values[valuesIndex++];
  2373. }
  2374. }
  2375. return data;
  2376. });
  2377. });
  2378. }
  2379. return accessor._data;
  2380. };
  2381. GLTFLoaderV2.prototype._loadVertexBufferViewAsync = function (bufferView, kind) {
  2382. var _this = this;
  2383. if (bufferView._babylonBuffer) {
  2384. return bufferView._babylonBuffer;
  2385. }
  2386. bufferView._babylonBuffer = this.loadBufferViewAsync("#/bufferViews/" + bufferView.index, bufferView).then(function (data) {
  2387. return new babylonjs_1.Buffer(_this.babylonScene.getEngine(), data, false);
  2388. });
  2389. return bufferView._babylonBuffer;
  2390. };
  2391. GLTFLoaderV2.prototype._loadVertexAccessorAsync = function (context, accessor, kind) {
  2392. var _this = this;
  2393. if (accessor._babylonVertexBuffer) {
  2394. return accessor._babylonVertexBuffer;
  2395. }
  2396. if (accessor.sparse) {
  2397. accessor._babylonVertexBuffer = this._loadFloatAccessorAsync("#/accessors/" + accessor.index, accessor).then(function (data) {
  2398. return new babylonjs_1.VertexBuffer(_this.babylonScene.getEngine(), data, kind, false);
  2399. });
  2400. }
  2401. // HACK: If byte offset is not a multiple of component type byte length then load as a float array instead of using Babylon buffers.
  2402. else if (accessor.byteOffset && accessor.byteOffset % babylonjs_1.VertexBuffer.GetTypeByteLength(accessor.componentType) !== 0) {
  2403. babylonjs_1.Tools.Warn("Accessor byte offset is not a multiple of component type byte length");
  2404. accessor._babylonVertexBuffer = this._loadFloatAccessorAsync("#/accessors/" + accessor.index, accessor).then(function (data) {
  2405. return new babylonjs_1.VertexBuffer(_this.babylonScene.getEngine(), data, kind, false);
  2406. });
  2407. }
  2408. else {
  2409. var bufferView_1 = ArrayItem.Get(context + "/bufferView", this.gltf.bufferViews, accessor.bufferView);
  2410. accessor._babylonVertexBuffer = this._loadVertexBufferViewAsync(bufferView_1, kind).then(function (babylonBuffer) {
  2411. var size = GLTFLoaderV2._GetNumComponents(context, accessor.type);
  2412. return new babylonjs_1.VertexBuffer(_this.babylonScene.getEngine(), babylonBuffer, kind, false, false, bufferView_1.byteStride, false, accessor.byteOffset, size, accessor.componentType, accessor.normalized, true);
  2413. });
  2414. }
  2415. return accessor._babylonVertexBuffer;
  2416. };
  2417. GLTFLoaderV2.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, properties, babylonMaterial) {
  2418. if (!(babylonMaterial instanceof babylonjs_1.PBRMaterial)) {
  2419. throw new Error(context + ": Material type not supported");
  2420. }
  2421. var promises = new Array();
  2422. if (properties) {
  2423. if (properties.baseColorFactor) {
  2424. babylonMaterial.albedoColor = babylonjs_1.Color3.FromArray(properties.baseColorFactor);
  2425. babylonMaterial.alpha = properties.baseColorFactor[3];
  2426. }
  2427. else {
  2428. babylonMaterial.albedoColor = babylonjs_1.Color3.White();
  2429. }
  2430. babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
  2431. babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
  2432. if (properties.baseColorTexture) {
  2433. promises.push(this.loadTextureInfoAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  2434. babylonMaterial.albedoTexture = texture;
  2435. }));
  2436. }
  2437. if (properties.metallicRoughnessTexture) {
  2438. promises.push(this.loadTextureInfoAsync(context + "/metallicRoughnessTexture", properties.metallicRoughnessTexture, function (texture) {
  2439. babylonMaterial.metallicTexture = texture;
  2440. }));
  2441. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  2442. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  2443. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  2444. }
  2445. }
  2446. return Promise.all(promises).then(function () { });
  2447. };
  2448. /** @hidden */
  2449. GLTFLoaderV2.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2450. if (assign === void 0) { assign = function () { }; }
  2451. var extensionPromise = this._extensionsLoadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign);
  2452. if (extensionPromise) {
  2453. return extensionPromise;
  2454. }
  2455. material._babylonData = material._babylonData || {};
  2456. var babylonData = material._babylonData[babylonDrawMode];
  2457. if (!babylonData) {
  2458. this.logOpen(context + " " + (material.name || ""));
  2459. var babylonMaterial = this.createMaterial(context, material, babylonDrawMode);
  2460. babylonData = {
  2461. material: babylonMaterial,
  2462. meshes: [],
  2463. promise: this.loadMaterialPropertiesAsync(context, material, babylonMaterial)
  2464. };
  2465. material._babylonData[babylonDrawMode] = babylonData;
  2466. this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  2467. this.logClose();
  2468. }
  2469. babylonData.meshes.push(babylonMesh);
  2470. babylonMesh.onDisposeObservable.addOnce(function () {
  2471. var index = babylonData.meshes.indexOf(babylonMesh);
  2472. if (index !== -1) {
  2473. babylonData.meshes.splice(index, 1);
  2474. }
  2475. });
  2476. assign(babylonData.material);
  2477. return babylonData.promise.then(function () {
  2478. return babylonData.material;
  2479. });
  2480. };
  2481. GLTFLoaderV2.prototype._createDefaultMaterial = function (name, babylonDrawMode) {
  2482. var babylonMaterial = new babylonjs_1.PBRMaterial(name, this.babylonScene);
  2483. babylonMaterial.sideOrientation = this.babylonScene.useRightHandedSystem ? babylonjs_1.Material.CounterClockWiseSideOrientation : babylonjs_1.Material.ClockWiseSideOrientation;
  2484. babylonMaterial.fillMode = babylonDrawMode;
  2485. babylonMaterial.enableSpecularAntiAliasing = true;
  2486. babylonMaterial.useRadianceOverAlpha = !this._parent.transparencyAsCoverage;
  2487. babylonMaterial.useSpecularOverAlpha = !this._parent.transparencyAsCoverage;
  2488. babylonMaterial.transparencyMode = babylonjs_1.PBRMaterial.PBRMATERIAL_OPAQUE;
  2489. babylonMaterial.metallic = 1;
  2490. babylonMaterial.roughness = 1;
  2491. return babylonMaterial;
  2492. };
  2493. /**
  2494. * Creates a Babylon material from a glTF material.
  2495. * @param context The context when loading the asset
  2496. * @param material The glTF material property
  2497. * @param babylonDrawMode The draw mode for the Babylon material
  2498. * @returns The Babylon material
  2499. */
  2500. GLTFLoaderV2.prototype.createMaterial = function (context, material, babylonDrawMode) {
  2501. var extensionPromise = this._extensionsCreateMaterial(context, material, babylonDrawMode);
  2502. if (extensionPromise) {
  2503. return extensionPromise;
  2504. }
  2505. var name = material.name || "material" + material.index;
  2506. return this._createDefaultMaterial(name, babylonDrawMode);
  2507. };
  2508. /**
  2509. * Loads properties from a glTF material into a Babylon material.
  2510. * @param context The context when loading the asset
  2511. * @param material The glTF material property
  2512. * @param babylonMaterial The Babylon material
  2513. * @returns A promise that resolves when the load is complete
  2514. */
  2515. GLTFLoaderV2.prototype.loadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
  2516. var extensionPromise = this._extensionsLoadMaterialPropertiesAsync(context, material, babylonMaterial);
  2517. if (extensionPromise) {
  2518. return extensionPromise;
  2519. }
  2520. var promises = new Array();
  2521. promises.push(this.loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  2522. if (material.pbrMetallicRoughness) {
  2523. promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync(context + "/pbrMetallicRoughness", material.pbrMetallicRoughness, babylonMaterial));
  2524. }
  2525. this.loadMaterialAlphaProperties(context, material, babylonMaterial);
  2526. return Promise.all(promises).then(function () { });
  2527. };
  2528. /**
  2529. * Loads the normal, occlusion, and emissive properties from a glTF material into a Babylon material.
  2530. * @param context The context when loading the asset
  2531. * @param material The glTF material property
  2532. * @param babylonMaterial The Babylon material
  2533. * @returns A promise that resolves when the load is complete
  2534. */
  2535. GLTFLoaderV2.prototype.loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
  2536. if (!(babylonMaterial instanceof babylonjs_1.PBRMaterial)) {
  2537. throw new Error(context + ": Material type not supported");
  2538. }
  2539. var promises = new Array();
  2540. babylonMaterial.emissiveColor = material.emissiveFactor ? babylonjs_1.Color3.FromArray(material.emissiveFactor) : new babylonjs_1.Color3(0, 0, 0);
  2541. if (material.doubleSided) {
  2542. babylonMaterial.backFaceCulling = false;
  2543. babylonMaterial.twoSidedLighting = true;
  2544. }
  2545. if (material.normalTexture) {
  2546. promises.push(this.loadTextureInfoAsync(context + "/normalTexture", material.normalTexture, function (texture) {
  2547. babylonMaterial.bumpTexture = texture;
  2548. }));
  2549. babylonMaterial.invertNormalMapX = !this.babylonScene.useRightHandedSystem;
  2550. babylonMaterial.invertNormalMapY = this.babylonScene.useRightHandedSystem;
  2551. if (material.normalTexture.scale != undefined) {
  2552. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  2553. }
  2554. }
  2555. if (material.occlusionTexture) {
  2556. promises.push(this.loadTextureInfoAsync(context + "/occlusionTexture", material.occlusionTexture, function (texture) {
  2557. babylonMaterial.ambientTexture = texture;
  2558. }));
  2559. babylonMaterial.useAmbientInGrayScale = true;
  2560. if (material.occlusionTexture.strength != undefined) {
  2561. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  2562. }
  2563. }
  2564. if (material.emissiveTexture) {
  2565. promises.push(this.loadTextureInfoAsync(context + "/emissiveTexture", material.emissiveTexture, function (texture) {
  2566. babylonMaterial.emissiveTexture = texture;
  2567. }));
  2568. }
  2569. return Promise.all(promises).then(function () { });
  2570. };
  2571. /**
  2572. * Loads the alpha properties from a glTF material into a Babylon material.
  2573. * Must be called after the setting the albedo texture of the Babylon material when the material has an albedo texture.
  2574. * @param context The context when loading the asset
  2575. * @param material The glTF material property
  2576. * @param babylonMaterial The Babylon material
  2577. */
  2578. GLTFLoaderV2.prototype.loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
  2579. if (!(babylonMaterial instanceof babylonjs_1.PBRMaterial)) {
  2580. throw new Error(context + ": Material type not supported");
  2581. }
  2582. var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
  2583. switch (alphaMode) {
  2584. case "OPAQUE" /* OPAQUE */: {
  2585. babylonMaterial.transparencyMode = babylonjs_1.PBRMaterial.PBRMATERIAL_OPAQUE;
  2586. break;
  2587. }
  2588. case "MASK" /* MASK */: {
  2589. babylonMaterial.transparencyMode = babylonjs_1.PBRMaterial.PBRMATERIAL_ALPHATEST;
  2590. babylonMaterial.alphaCutOff = (material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff);
  2591. if (babylonMaterial.albedoTexture) {
  2592. babylonMaterial.albedoTexture.hasAlpha = true;
  2593. }
  2594. break;
  2595. }
  2596. case "BLEND" /* BLEND */: {
  2597. babylonMaterial.transparencyMode = babylonjs_1.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  2598. if (babylonMaterial.albedoTexture) {
  2599. babylonMaterial.albedoTexture.hasAlpha = true;
  2600. babylonMaterial.useAlphaFromAlbedoTexture = true;
  2601. }
  2602. break;
  2603. }
  2604. default: {
  2605. throw new Error(context + "/alphaMode: Invalid value (" + material.alphaMode + ")");
  2606. }
  2607. }
  2608. };
  2609. /**
  2610. * Loads a glTF texture info.
  2611. * @param context The context when loading the asset
  2612. * @param textureInfo The glTF texture info property
  2613. * @param assign A function called synchronously after parsing the glTF properties
  2614. * @returns A promise that resolves with the loaded Babylon texture when the load is complete
  2615. */
  2616. GLTFLoaderV2.prototype.loadTextureInfoAsync = function (context, textureInfo, assign) {
  2617. if (assign === void 0) { assign = function () { }; }
  2618. var extensionPromise = this._extensionsLoadTextureInfoAsync(context, textureInfo, assign);
  2619. if (extensionPromise) {
  2620. return extensionPromise;
  2621. }
  2622. this.logOpen("" + context);
  2623. var texture = ArrayItem.Get(context + "/index", this.gltf.textures, textureInfo.index);
  2624. var promise = this._loadTextureAsync("#/textures/" + textureInfo.index, texture, function (babylonTexture) {
  2625. babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
  2626. assign(babylonTexture);
  2627. });
  2628. this.logClose();
  2629. return promise;
  2630. };
  2631. GLTFLoaderV2.prototype._loadTextureAsync = function (context, texture, assign) {
  2632. var _this = this;
  2633. if (assign === void 0) { assign = function () { }; }
  2634. var promises = new Array();
  2635. this.logOpen(context + " " + (texture.name || ""));
  2636. var sampler = (texture.sampler == undefined ? GLTFLoaderV2._DefaultSampler : ArrayItem.Get(context + "/sampler", this.gltf.samplers, texture.sampler));
  2637. var samplerData = this._loadSampler("#/samplers/" + sampler.index, sampler);
  2638. var deferred = new babylonjs_1.Deferred();
  2639. var babylonTexture = new babylonjs_1.Texture(null, this.babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, function () {
  2640. if (!_this._disposed) {
  2641. deferred.resolve();
  2642. }
  2643. }, function (message, exception) {
  2644. if (!_this._disposed) {
  2645. deferred.reject(new Error(context + ": " + ((exception && exception.message) ? exception.message : message || "Failed to load texture")));
  2646. }
  2647. });
  2648. promises.push(deferred.promise);
  2649. babylonTexture.name = texture.name || "texture" + texture.index;
  2650. babylonTexture.wrapU = samplerData.wrapU;
  2651. babylonTexture.wrapV = samplerData.wrapV;
  2652. var image = ArrayItem.Get(context + "/source", this.gltf.images, texture.source);
  2653. promises.push(this.loadImageAsync("#/images/" + image.index, image).then(function (data) {
  2654. var name = image.uri || _this._fileName + "#image" + image.index;
  2655. var dataUrl = "data:" + _this._uniqueRootUrl + name;
  2656. babylonTexture.updateURL(dataUrl, new Blob([data], { type: image.mimeType }));
  2657. }));
  2658. assign(babylonTexture);
  2659. this._parent.onTextureLoadedObservable.notifyObservers(babylonTexture);
  2660. this.logClose();
  2661. return Promise.all(promises).then(function () {
  2662. return babylonTexture;
  2663. });
  2664. };
  2665. GLTFLoaderV2.prototype._loadSampler = function (context, sampler) {
  2666. if (!sampler._data) {
  2667. sampler._data = {
  2668. noMipMaps: (sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */),
  2669. samplingMode: GLTFLoaderV2._GetTextureSamplingMode(context, sampler),
  2670. wrapU: GLTFLoaderV2._GetTextureWrapMode(context + "/wrapS", sampler.wrapS),
  2671. wrapV: GLTFLoaderV2._GetTextureWrapMode(context + "/wrapT", sampler.wrapT)
  2672. };
  2673. }
  2674. return sampler._data;
  2675. };
  2676. /**
  2677. * Loads a glTF image.
  2678. * @param context The context when loading the asset
  2679. * @param image The glTF image property
  2680. * @returns A promise that resolves with the loaded data when the load is complete
  2681. */
  2682. GLTFLoaderV2.prototype.loadImageAsync = function (context, image) {
  2683. if (!image._data) {
  2684. this.logOpen(context + " " + (image.name || ""));
  2685. if (image.uri) {
  2686. image._data = this.loadUriAsync(context + "/uri", image.uri);
  2687. }
  2688. else {
  2689. var bufferView = ArrayItem.Get(context + "/bufferView", this.gltf.bufferViews, image.bufferView);
  2690. image._data = this.loadBufferViewAsync("#/bufferViews/" + bufferView.index, bufferView);
  2691. }
  2692. this.logClose();
  2693. }
  2694. return image._data;
  2695. };
  2696. /**
  2697. * Loads a glTF uri.
  2698. * @param context The context when loading the asset
  2699. * @param uri The base64 or relative uri
  2700. * @returns A promise that resolves with the loaded data when the load is complete
  2701. */
  2702. GLTFLoaderV2.prototype.loadUriAsync = function (context, uri) {
  2703. var _this = this;
  2704. var extensionPromise = this._extensionsLoadUriAsync(context, uri);
  2705. if (extensionPromise) {
  2706. return extensionPromise;
  2707. }
  2708. if (!GLTFLoaderV2._ValidateUri(uri)) {
  2709. throw new Error(context + ": '" + uri + "' is invalid");
  2710. }
  2711. if (babylonjs_1.Tools.IsBase64(uri)) {
  2712. var data = new Uint8Array(babylonjs_1.Tools.DecodeBase64(uri));
  2713. this.log("Decoded " + uri.substr(0, 64) + "... (" + data.length + " bytes)");
  2714. return Promise.resolve(data);
  2715. }
  2716. this.log("Loading " + uri);
  2717. return this._parent.preprocessUrlAsync(this._rootUrl + uri).then(function (url) {
  2718. return new Promise(function (resolve, reject) {
  2719. if (!_this._disposed) {
  2720. var request_1 = babylonjs_1.Tools.LoadFile(url, function (fileData) {
  2721. if (!_this._disposed) {
  2722. var data = new Uint8Array(fileData);
  2723. _this.log("Loaded " + uri + " (" + data.length + " bytes)");
  2724. resolve(data);
  2725. }
  2726. }, function (event) {
  2727. if (!_this._disposed) {
  2728. if (request_1) {
  2729. request_1._lengthComputable = event.lengthComputable;
  2730. request_1._loaded = event.loaded;
  2731. request_1._total = event.total;
  2732. }
  2733. if (_this._state === glTFFileLoader_1.GLTFLoaderState.LOADING) {
  2734. try {
  2735. _this._onProgress();
  2736. }
  2737. catch (e) {
  2738. reject(e);
  2739. }
  2740. }
  2741. }
  2742. }, _this.babylonScene.offlineProvider, true, function (request, exception) {
  2743. if (!_this._disposed) {
  2744. reject(new babylonjs_1.LoadFileError(context + ": Failed to load '" + uri + "'" + (request ? ": " + request.status + " " + request.statusText : ""), request));
  2745. }
  2746. });
  2747. _this._requests.push(request_1);
  2748. }
  2749. });
  2750. });
  2751. };
  2752. GLTFLoaderV2.prototype._onProgress = function () {
  2753. if (!this._progressCallback) {
  2754. return;
  2755. }
  2756. var lengthComputable = true;
  2757. var loaded = 0;
  2758. var total = 0;
  2759. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  2760. var request = _a[_i];
  2761. if (request._lengthComputable === undefined || request._loaded === undefined || request._total === undefined) {
  2762. return;
  2763. }
  2764. lengthComputable = lengthComputable && request._lengthComputable;
  2765. loaded += request._loaded;
  2766. total += request._total;
  2767. }
  2768. this._progressCallback(new babylonjs_1.SceneLoaderProgressEvent(lengthComputable, loaded, lengthComputable ? total : 0));
  2769. };
  2770. GLTFLoaderV2._GetTextureWrapMode = function (context, mode) {
  2771. // Set defaults if undefined
  2772. mode = mode == undefined ? 10497 /* REPEAT */ : mode;
  2773. switch (mode) {
  2774. case 33071 /* CLAMP_TO_EDGE */: return babylonjs_1.Texture.CLAMP_ADDRESSMODE;
  2775. case 33648 /* MIRRORED_REPEAT */: return babylonjs_1.Texture.MIRROR_ADDRESSMODE;
  2776. case 10497 /* REPEAT */: return babylonjs_1.Texture.WRAP_ADDRESSMODE;
  2777. default:
  2778. babylonjs_1.Tools.Warn(context + ": Invalid value (" + mode + ")");
  2779. return babylonjs_1.Texture.WRAP_ADDRESSMODE;
  2780. }
  2781. };
  2782. GLTFLoaderV2._GetTextureSamplingMode = function (context, sampler) {
  2783. // Set defaults if undefined
  2784. var magFilter = sampler.magFilter == undefined ? 9729 /* LINEAR */ : sampler.magFilter;
  2785. var minFilter = sampler.minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : sampler.minFilter;
  2786. if (magFilter === 9729 /* LINEAR */) {
  2787. switch (minFilter) {
  2788. case 9728 /* NEAREST */: return babylonjs_1.Texture.LINEAR_NEAREST;
  2789. case 9729 /* LINEAR */: return babylonjs_1.Texture.LINEAR_LINEAR;
  2790. case 9984 /* NEAREST_MIPMAP_NEAREST */: return babylonjs_1.Texture.LINEAR_NEAREST_MIPNEAREST;
  2791. case 9985 /* LINEAR_MIPMAP_NEAREST */: return babylonjs_1.Texture.LINEAR_LINEAR_MIPNEAREST;
  2792. case 9986 /* NEAREST_MIPMAP_LINEAR */: return babylonjs_1.Texture.LINEAR_NEAREST_MIPLINEAR;
  2793. case 9987 /* LINEAR_MIPMAP_LINEAR */: return babylonjs_1.Texture.LINEAR_LINEAR_MIPLINEAR;
  2794. default:
  2795. babylonjs_1.Tools.Warn(context + "/minFilter: Invalid value (" + minFilter + ")");
  2796. return babylonjs_1.Texture.LINEAR_LINEAR_MIPLINEAR;
  2797. }
  2798. }
  2799. else {
  2800. if (magFilter !== 9728 /* NEAREST */) {
  2801. babylonjs_1.Tools.Warn(context + "/magFilter: Invalid value (" + magFilter + ")");
  2802. }
  2803. switch (minFilter) {
  2804. case 9728 /* NEAREST */: return babylonjs_1.Texture.NEAREST_NEAREST;
  2805. case 9729 /* LINEAR */: return babylonjs_1.Texture.NEAREST_LINEAR;
  2806. case 9984 /* NEAREST_MIPMAP_NEAREST */: return babylonjs_1.Texture.NEAREST_NEAREST_MIPNEAREST;
  2807. case 9985 /* LINEAR_MIPMAP_NEAREST */: return babylonjs_1.Texture.NEAREST_LINEAR_MIPNEAREST;
  2808. case 9986 /* NEAREST_MIPMAP_LINEAR */: return babylonjs_1.Texture.NEAREST_NEAREST_MIPLINEAR;
  2809. case 9987 /* LINEAR_MIPMAP_LINEAR */: return babylonjs_1.Texture.NEAREST_LINEAR_MIPLINEAR;
  2810. default:
  2811. babylonjs_1.Tools.Warn(context + "/minFilter: Invalid value (" + minFilter + ")");
  2812. return babylonjs_1.Texture.NEAREST_NEAREST_MIPNEAREST;
  2813. }
  2814. }
  2815. };
  2816. GLTFLoaderV2._GetTypedArray = function (context, componentType, bufferView, byteOffset, length) {
  2817. var buffer = bufferView.buffer;
  2818. byteOffset = bufferView.byteOffset + (byteOffset || 0);
  2819. try {
  2820. switch (componentType) {
  2821. case 5120 /* BYTE */: return new Int8Array(buffer, byteOffset, length);
  2822. case 5121 /* UNSIGNED_BYTE */: return new Uint8Array(buffer, byteOffset, length);
  2823. case 5122 /* SHORT */: return new Int16Array(buffer, byteOffset, length);
  2824. case 5123 /* UNSIGNED_SHORT */: return new Uint16Array(buffer, byteOffset, length);
  2825. case 5125 /* UNSIGNED_INT */: return new Uint32Array(buffer, byteOffset, length);
  2826. case 5126 /* FLOAT */: return new Float32Array(buffer, byteOffset, length);
  2827. default: throw new Error("Invalid component type " + componentType);
  2828. }
  2829. }
  2830. catch (e) {
  2831. throw new Error(context + ": " + e);
  2832. }
  2833. };
  2834. GLTFLoaderV2._GetNumComponents = function (context, type) {
  2835. switch (type) {
  2836. case "SCALAR": return 1;
  2837. case "VEC2": return 2;
  2838. case "VEC3": return 3;
  2839. case "VEC4": return 4;
  2840. case "MAT2": return 4;
  2841. case "MAT3": return 9;
  2842. case "MAT4": return 16;
  2843. }
  2844. throw new Error(context + ": Invalid type (" + type + ")");
  2845. };
  2846. GLTFLoaderV2._ValidateUri = function (uri) {
  2847. return (babylonjs_1.Tools.IsBase64(uri) || uri.indexOf("..") === -1);
  2848. };
  2849. GLTFLoaderV2._GetDrawMode = function (context, mode) {
  2850. if (mode == undefined) {
  2851. mode = 4 /* TRIANGLES */;
  2852. }
  2853. switch (mode) {
  2854. case 0 /* POINTS */: return babylonjs_1.Material.PointListDrawMode;
  2855. case 1 /* LINES */: return babylonjs_1.Material.LineListDrawMode;
  2856. case 2 /* LINE_LOOP */: return babylonjs_1.Material.LineLoopDrawMode;
  2857. case 3 /* LINE_STRIP */: return babylonjs_1.Material.LineStripDrawMode;
  2858. case 4 /* TRIANGLES */: return babylonjs_1.Material.TriangleFillMode;
  2859. case 5 /* TRIANGLE_STRIP */: return babylonjs_1.Material.TriangleStripDrawMode;
  2860. case 6 /* TRIANGLE_FAN */: return babylonjs_1.Material.TriangleFanDrawMode;
  2861. }
  2862. throw new Error(context + ": Invalid mesh primitive mode (" + mode + ")");
  2863. };
  2864. GLTFLoaderV2.prototype._compileMaterialsAsync = function () {
  2865. var _this = this;
  2866. this._parent._startPerformanceCounter("Compile materials");
  2867. var promises = new Array();
  2868. if (this.gltf.materials) {
  2869. for (var _i = 0, _a = this.gltf.materials; _i < _a.length; _i++) {
  2870. var material = _a[_i];
  2871. if (material._babylonData) {
  2872. for (var babylonDrawMode in material._babylonData) {
  2873. var babylonData = material._babylonData[babylonDrawMode];
  2874. for (var _b = 0, _c = babylonData.meshes; _b < _c.length; _b++) {
  2875. var babylonMesh = _c[_b];
  2876. // Ensure nonUniformScaling is set if necessary.
  2877. babylonMesh.computeWorldMatrix(true);
  2878. var babylonMaterial = babylonData.material;
  2879. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
  2880. if (this._parent.useClipPlane) {
  2881. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
  2882. }
  2883. }
  2884. }
  2885. }
  2886. }
  2887. }
  2888. return Promise.all(promises).then(function () {
  2889. _this._parent._endPerformanceCounter("Compile materials");
  2890. });
  2891. };
  2892. GLTFLoaderV2.prototype._compileShadowGeneratorsAsync = function () {
  2893. var _this = this;
  2894. this._parent._startPerformanceCounter("Compile shadow generators");
  2895. var promises = new Array();
  2896. var lights = this.babylonScene.lights;
  2897. for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
  2898. var light = lights_1[_i];
  2899. var generator = light.getShadowGenerator();
  2900. if (generator) {
  2901. promises.push(generator.forceCompilationAsync());
  2902. }
  2903. }
  2904. return Promise.all(promises).then(function () {
  2905. _this._parent._endPerformanceCounter("Compile shadow generators");
  2906. });
  2907. };
  2908. GLTFLoaderV2.prototype._forEachExtensions = function (action) {
  2909. for (var _i = 0, _a = GLTFLoaderV2._ExtensionNames; _i < _a.length; _i++) {
  2910. var name_4 = _a[_i];
  2911. var extension = this._extensions[name_4];
  2912. if (extension.enabled) {
  2913. action(extension);
  2914. }
  2915. }
  2916. };
  2917. GLTFLoaderV2.prototype._applyExtensions = function (property, actionAsync) {
  2918. for (var _i = 0, _a = GLTFLoaderV2._ExtensionNames; _i < _a.length; _i++) {
  2919. var name_5 = _a[_i];
  2920. var extension = this._extensions[name_5];
  2921. if (extension.enabled) {
  2922. var loaderProperty = property;
  2923. loaderProperty._activeLoaderExtensions = loaderProperty._activeLoaderExtensions || {};
  2924. var activeLoaderExtensions = loaderProperty._activeLoaderExtensions;
  2925. if (!activeLoaderExtensions[name_5]) {
  2926. activeLoaderExtensions[name_5] = true;
  2927. try {
  2928. var result = actionAsync(extension);
  2929. if (result) {
  2930. return result;
  2931. }
  2932. }
  2933. finally {
  2934. delete activeLoaderExtensions[name_5];
  2935. }
  2936. }
  2937. }
  2938. }
  2939. return null;
  2940. };
  2941. GLTFLoaderV2.prototype._extensionsOnLoading = function () {
  2942. this._forEachExtensions(function (extension) { return extension.onLoading && extension.onLoading(); });
  2943. };
  2944. GLTFLoaderV2.prototype._extensionsOnReady = function () {
  2945. this._forEachExtensions(function (extension) { return extension.onReady && extension.onReady(); });
  2946. };
  2947. GLTFLoaderV2.prototype._extensionsLoadSceneAsync = function (context, scene) {
  2948. return this._applyExtensions(scene, function (extension) { return extension.loadSceneAsync && extension.loadSceneAsync(context, scene); });
  2949. };
  2950. GLTFLoaderV2.prototype._extensionsLoadNodeAsync = function (context, node, assign) {
  2951. return this._applyExtensions(node, function (extension) { return extension.loadNodeAsync && extension.loadNodeAsync(context, node, assign); });
  2952. };
  2953. GLTFLoaderV2.prototype._extensionsLoadCameraAsync = function (context, camera, assign) {
  2954. return this._applyExtensions(camera, function (extension) { return extension.loadCameraAsync && extension.loadCameraAsync(context, camera, assign); });
  2955. };
  2956. GLTFLoaderV2.prototype._extensionsLoadVertexDataAsync = function (context, primitive, babylonMesh) {
  2957. return this._applyExtensions(primitive, function (extension) { return extension._loadVertexDataAsync && extension._loadVertexDataAsync(context, primitive, babylonMesh); });
  2958. };
  2959. GLTFLoaderV2.prototype._extensionsLoadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2960. return this._applyExtensions(material, function (extension) { return extension._loadMaterialAsync && extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
  2961. };
  2962. GLTFLoaderV2.prototype._extensionsCreateMaterial = function (context, material, babylonDrawMode) {
  2963. return this._applyExtensions({}, function (extension) { return extension.createMaterial && extension.createMaterial(context, material, babylonDrawMode); });
  2964. };
  2965. GLTFLoaderV2.prototype._extensionsLoadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
  2966. return this._applyExtensions(material, function (extension) { return extension.loadMaterialPropertiesAsync && extension.loadMaterialPropertiesAsync(context, material, babylonMaterial); });
  2967. };
  2968. GLTFLoaderV2.prototype._extensionsLoadTextureInfoAsync = function (context, textureInfo, assign) {
  2969. return this._applyExtensions(textureInfo, function (extension) { return extension.loadTextureInfoAsync && extension.loadTextureInfoAsync(context, textureInfo, assign); });
  2970. };
  2971. GLTFLoaderV2.prototype._extensionsLoadAnimationAsync = function (context, animation) {
  2972. return this._applyExtensions(animation, function (extension) { return extension.loadAnimationAsync && extension.loadAnimationAsync(context, animation); });
  2973. };
  2974. GLTFLoaderV2.prototype._extensionsLoadUriAsync = function (context, uri) {
  2975. return this._applyExtensions({}, function (extension) { return extension._loadUriAsync && extension._loadUriAsync(context, uri); });
  2976. };
  2977. /**
  2978. * Helper method called by a loader extension to load an glTF extension.
  2979. * @param context The context when loading the asset
  2980. * @param property The glTF property to load the extension from
  2981. * @param extensionName The name of the extension to load
  2982. * @param actionAsync The action to run
  2983. * @returns The promise returned by actionAsync or null if the extension does not exist
  2984. */
  2985. GLTFLoaderV2.LoadExtensionAsync = function (context, property, extensionName, actionAsync) {
  2986. if (!property.extensions) {
  2987. return null;
  2988. }
  2989. var extensions = property.extensions;
  2990. var extension = extensions[extensionName];
  2991. if (!extension) {
  2992. return null;
  2993. }
  2994. return actionAsync(context + "/extensions/" + extensionName, extension);
  2995. };
  2996. /**
  2997. * Helper method called by a loader extension to load a glTF extra.
  2998. * @param context The context when loading the asset
  2999. * @param property The glTF property to load the extra from
  3000. * @param extensionName The name of the extension to load
  3001. * @param actionAsync The action to run
  3002. * @returns The promise returned by actionAsync or null if the extra does not exist
  3003. */
  3004. GLTFLoaderV2.LoadExtraAsync = function (context, property, extensionName, actionAsync) {
  3005. if (!property.extras) {
  3006. return null;
  3007. }
  3008. var extras = property.extras;
  3009. var extra = extras[extensionName];
  3010. if (!extra) {
  3011. return null;
  3012. }
  3013. return actionAsync(context + "/extras/" + extensionName, extra);
  3014. };
  3015. /**
  3016. * Increments the indentation level and logs a message.
  3017. * @param message The message to log
  3018. */
  3019. GLTFLoaderV2.prototype.logOpen = function (message) {
  3020. this._parent._logOpen(message);
  3021. };
  3022. /**
  3023. * Decrements the indentation level.
  3024. */
  3025. GLTFLoaderV2.prototype.logClose = function () {
  3026. this._parent._logClose();
  3027. };
  3028. /**
  3029. * Logs a message
  3030. * @param message The message to log
  3031. */
  3032. GLTFLoaderV2.prototype.log = function (message) {
  3033. this._parent._log(message);
  3034. };
  3035. /**
  3036. * Starts a performance counter.
  3037. * @param counterName The name of the performance counter
  3038. */
  3039. GLTFLoaderV2.prototype.startPerformanceCounter = function (counterName) {
  3040. this._parent._startPerformanceCounter(counterName);
  3041. };
  3042. /**
  3043. * Ends a performance counter.
  3044. * @param counterName The name of the performance counter
  3045. */
  3046. GLTFLoaderV2.prototype.endPerformanceCounter = function (counterName) {
  3047. this._parent._endPerformanceCounter(counterName);
  3048. };
  3049. GLTFLoaderV2._DefaultSampler = { index: -1 };
  3050. GLTFLoaderV2._ExtensionNames = new Array();
  3051. GLTFLoaderV2._ExtensionFactories = {};
  3052. return GLTFLoaderV2;
  3053. }());
  3054. exports.GLTFLoaderV2 = GLTFLoaderV2;
  3055. glTFFileLoader_1.GLTFFileLoader._CreateGLTFLoaderV2 = function (parent) { return new GLTFLoaderV2(parent); };
  3056. /***/ }),
  3057. /***/ "./src/glTF/2.0/glTFLoaderExtension.ts":
  3058. /*!*********************************************!*\
  3059. !*** ./src/glTF/2.0/glTFLoaderExtension.ts ***!
  3060. \*********************************************/
  3061. /*! no static exports found */
  3062. /***/ (function(module, exports, __webpack_require__) {
  3063. "use strict";
  3064. Object.defineProperty(exports, "__esModule", { value: true });
  3065. /** @hidden */
  3066. exports.__IGLTFLoaderExtensionV2 = 0; // I am here to allow dts to be created
  3067. /***/ }),
  3068. /***/ "./src/glTF/2.0/glTFLoaderInterfaces.ts":
  3069. /*!**********************************************!*\
  3070. !*** ./src/glTF/2.0/glTFLoaderInterfaces.ts ***!
  3071. \**********************************************/
  3072. /*! no static exports found */
  3073. /***/ (function(module, exports, __webpack_require__) {
  3074. "use strict";
  3075. Object.defineProperty(exports, "__esModule", { value: true });
  3076. /** @hidden */
  3077. exports.__IGLTFLoaderInterfacesV2 = 0; // I am here to allow dts to be created
  3078. /***/ }),
  3079. /***/ "./src/glTF/2.0/index.ts":
  3080. /*!*******************************!*\
  3081. !*** ./src/glTF/2.0/index.ts ***!
  3082. \*******************************/
  3083. /*! no static exports found */
  3084. /***/ (function(module, exports, __webpack_require__) {
  3085. "use strict";
  3086. function __export(m) {
  3087. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  3088. }
  3089. Object.defineProperty(exports, "__esModule", { value: true });
  3090. __export(__webpack_require__(/*! ./glTFLoader */ "./src/glTF/2.0/glTFLoader.ts"));
  3091. __export(__webpack_require__(/*! ./glTFLoaderExtension */ "./src/glTF/2.0/glTFLoaderExtension.ts"));
  3092. __export(__webpack_require__(/*! ./glTFLoaderInterfaces */ "./src/glTF/2.0/glTFLoaderInterfaces.ts"));
  3093. __export(__webpack_require__(/*! ./Extensions */ "./src/glTF/2.0/Extensions/index.ts"));
  3094. /***/ }),
  3095. /***/ "./src/glTF/glTFFileLoader.ts":
  3096. /*!************************************!*\
  3097. !*** ./src/glTF/glTFFileLoader.ts ***!
  3098. \************************************/
  3099. /*! no static exports found */
  3100. /***/ (function(module, exports, __webpack_require__) {
  3101. "use strict";
  3102. Object.defineProperty(exports, "__esModule", { value: true });
  3103. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  3104. /**
  3105. * Mode that determines the coordinate system to use.
  3106. */
  3107. var GLTFLoaderCoordinateSystemMode;
  3108. (function (GLTFLoaderCoordinateSystemMode) {
  3109. /**
  3110. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  3111. */
  3112. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
  3113. /**
  3114. * Sets the useRightHandedSystem flag on the scene.
  3115. */
  3116. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
  3117. })(GLTFLoaderCoordinateSystemMode = exports.GLTFLoaderCoordinateSystemMode || (exports.GLTFLoaderCoordinateSystemMode = {}));
  3118. /**
  3119. * Mode that determines what animations will start.
  3120. */
  3121. var GLTFLoaderAnimationStartMode;
  3122. (function (GLTFLoaderAnimationStartMode) {
  3123. /**
  3124. * No animation will start.
  3125. */
  3126. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
  3127. /**
  3128. * The first animation will start.
  3129. */
  3130. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
  3131. /**
  3132. * All animations will start.
  3133. */
  3134. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
  3135. })(GLTFLoaderAnimationStartMode = exports.GLTFLoaderAnimationStartMode || (exports.GLTFLoaderAnimationStartMode = {}));
  3136. /**
  3137. * Loader state.
  3138. */
  3139. var GLTFLoaderState;
  3140. (function (GLTFLoaderState) {
  3141. /**
  3142. * The asset is loading.
  3143. */
  3144. GLTFLoaderState[GLTFLoaderState["LOADING"] = 0] = "LOADING";
  3145. /**
  3146. * The asset is ready for rendering.
  3147. */
  3148. GLTFLoaderState[GLTFLoaderState["READY"] = 1] = "READY";
  3149. /**
  3150. * The asset is completely loaded.
  3151. */
  3152. GLTFLoaderState[GLTFLoaderState["COMPLETE"] = 2] = "COMPLETE";
  3153. })(GLTFLoaderState = exports.GLTFLoaderState || (exports.GLTFLoaderState = {}));
  3154. /**
  3155. * File loader for loading glTF files into a scene.
  3156. */
  3157. var GLTFFileLoader = /** @class */ (function () {
  3158. function GLTFFileLoader() {
  3159. // --------------
  3160. // Common options
  3161. // --------------
  3162. /**
  3163. * Raised when the asset has been parsed
  3164. */
  3165. this.onParsedObservable = new babylonjs_1.Observable();
  3166. // ----------
  3167. // V2 options
  3168. // ----------
  3169. /**
  3170. * The coordinate system mode. Defaults to AUTO.
  3171. */
  3172. this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
  3173. /**
  3174. * The animation start mode. Defaults to FIRST.
  3175. */
  3176. this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
  3177. /**
  3178. * Defines if the loader should compile materials before raising the success callback. Defaults to false.
  3179. */
  3180. this.compileMaterials = false;
  3181. /**
  3182. * Defines if the loader should also compile materials with clip planes. Defaults to false.
  3183. */
  3184. this.useClipPlane = false;
  3185. /**
  3186. * Defines if the loader should compile shadow generators before raising the success callback. Defaults to false.
  3187. */
  3188. this.compileShadowGenerators = false;
  3189. /**
  3190. * Defines if the Alpha blended materials are only applied as coverage.
  3191. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
  3192. * If true, no extra effects are applied to transparent pixels.
  3193. */
  3194. this.transparencyAsCoverage = false;
  3195. /**
  3196. * Function called before loading a url referenced by the asset.
  3197. */
  3198. this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
  3199. /**
  3200. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  3201. */
  3202. this.onMeshLoadedObservable = new babylonjs_1.Observable();
  3203. /**
  3204. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  3205. */
  3206. this.onTextureLoadedObservable = new babylonjs_1.Observable();
  3207. /**
  3208. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  3209. */
  3210. this.onMaterialLoadedObservable = new babylonjs_1.Observable();
  3211. /**
  3212. * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
  3213. */
  3214. this.onCameraLoadedObservable = new babylonjs_1.Observable();
  3215. /**
  3216. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  3217. * For assets with LODs, raised when all of the LODs are complete.
  3218. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  3219. */
  3220. this.onCompleteObservable = new babylonjs_1.Observable();
  3221. /**
  3222. * Observable raised when an error occurs.
  3223. */
  3224. this.onErrorObservable = new babylonjs_1.Observable();
  3225. /**
  3226. * Observable raised after the loader is disposed.
  3227. */
  3228. this.onDisposeObservable = new babylonjs_1.Observable();
  3229. /**
  3230. * Observable raised after a loader extension is created.
  3231. * Set additional options for a loader extension in this event.
  3232. */
  3233. this.onExtensionLoadedObservable = new babylonjs_1.Observable();
  3234. /**
  3235. * Defines if the loader should validate the asset.
  3236. */
  3237. this.validate = false;
  3238. /**
  3239. * Observable raised after validation when validate is set to true. The event data is the result of the validation.
  3240. */
  3241. this.onValidatedObservable = new babylonjs_1.Observable();
  3242. this._loader = null;
  3243. /**
  3244. * Name of the loader ("gltf")
  3245. */
  3246. this.name = "gltf";
  3247. /**
  3248. * Supported file extensions of the loader (.gltf, .glb)
  3249. */
  3250. this.extensions = {
  3251. ".gltf": { isBinary: false },
  3252. ".glb": { isBinary: true }
  3253. };
  3254. this._logIndentLevel = 0;
  3255. this._loggingEnabled = false;
  3256. /** @hidden */
  3257. this._log = this._logDisabled;
  3258. this._capturePerformanceCounters = false;
  3259. /** @hidden */
  3260. this._startPerformanceCounter = this._startPerformanceCounterDisabled;
  3261. /** @hidden */
  3262. this._endPerformanceCounter = this._endPerformanceCounterDisabled;
  3263. }
  3264. Object.defineProperty(GLTFFileLoader.prototype, "onParsed", {
  3265. /**
  3266. * Raised when the asset has been parsed
  3267. */
  3268. set: function (callback) {
  3269. if (this._onParsedObserver) {
  3270. this.onParsedObservable.remove(this._onParsedObserver);
  3271. }
  3272. this._onParsedObserver = this.onParsedObservable.add(callback);
  3273. },
  3274. enumerable: true,
  3275. configurable: true
  3276. });
  3277. Object.defineProperty(GLTFFileLoader.prototype, "onMeshLoaded", {
  3278. /**
  3279. * Callback raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  3280. */
  3281. set: function (callback) {
  3282. if (this._onMeshLoadedObserver) {
  3283. this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver);
  3284. }
  3285. this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback);
  3286. },
  3287. enumerable: true,
  3288. configurable: true
  3289. });
  3290. Object.defineProperty(GLTFFileLoader.prototype, "onTextureLoaded", {
  3291. /**
  3292. * Callback raised when the loader creates a texture after parsing the glTF properties of the texture.
  3293. */
  3294. set: function (callback) {
  3295. if (this._onTextureLoadedObserver) {
  3296. this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver);
  3297. }
  3298. this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback);
  3299. },
  3300. enumerable: true,
  3301. configurable: true
  3302. });
  3303. Object.defineProperty(GLTFFileLoader.prototype, "onMaterialLoaded", {
  3304. /**
  3305. * Callback raised when the loader creates a material after parsing the glTF properties of the material.
  3306. */
  3307. set: function (callback) {
  3308. if (this._onMaterialLoadedObserver) {
  3309. this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver);
  3310. }
  3311. this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback);
  3312. },
  3313. enumerable: true,
  3314. configurable: true
  3315. });
  3316. Object.defineProperty(GLTFFileLoader.prototype, "onCameraLoaded", {
  3317. /**
  3318. * Callback raised when the loader creates a camera after parsing the glTF properties of the camera.
  3319. */
  3320. set: function (callback) {
  3321. if (this._onCameraLoadedObserver) {
  3322. this.onCameraLoadedObservable.remove(this._onCameraLoadedObserver);
  3323. }
  3324. this._onCameraLoadedObserver = this.onCameraLoadedObservable.add(callback);
  3325. },
  3326. enumerable: true,
  3327. configurable: true
  3328. });
  3329. Object.defineProperty(GLTFFileLoader.prototype, "onComplete", {
  3330. /**
  3331. * Callback raised when the asset is completely loaded, immediately before the loader is disposed.
  3332. * For assets with LODs, raised when all of the LODs are complete.
  3333. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  3334. */
  3335. set: function (callback) {
  3336. if (this._onCompleteObserver) {
  3337. this.onCompleteObservable.remove(this._onCompleteObserver);
  3338. }
  3339. this._onCompleteObserver = this.onCompleteObservable.add(callback);
  3340. },
  3341. enumerable: true,
  3342. configurable: true
  3343. });
  3344. Object.defineProperty(GLTFFileLoader.prototype, "onError", {
  3345. /**
  3346. * Callback raised when an error occurs.
  3347. */
  3348. set: function (callback) {
  3349. if (this._onErrorObserver) {
  3350. this.onErrorObservable.remove(this._onErrorObserver);
  3351. }
  3352. this._onErrorObserver = this.onErrorObservable.add(callback);
  3353. },
  3354. enumerable: true,
  3355. configurable: true
  3356. });
  3357. Object.defineProperty(GLTFFileLoader.prototype, "onDispose", {
  3358. /**
  3359. * Callback raised after the loader is disposed.
  3360. */
  3361. set: function (callback) {
  3362. if (this._onDisposeObserver) {
  3363. this.onDisposeObservable.remove(this._onDisposeObserver);
  3364. }
  3365. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  3366. },
  3367. enumerable: true,
  3368. configurable: true
  3369. });
  3370. Object.defineProperty(GLTFFileLoader.prototype, "onExtensionLoaded", {
  3371. /**
  3372. * Callback raised after a loader extension is created.
  3373. */
  3374. set: function (callback) {
  3375. if (this._onExtensionLoadedObserver) {
  3376. this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver);
  3377. }
  3378. this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback);
  3379. },
  3380. enumerable: true,
  3381. configurable: true
  3382. });
  3383. Object.defineProperty(GLTFFileLoader.prototype, "loggingEnabled", {
  3384. /**
  3385. * Defines if the loader logging is enabled.
  3386. */
  3387. get: function () {
  3388. return this._loggingEnabled;
  3389. },
  3390. set: function (value) {
  3391. if (this._loggingEnabled === value) {
  3392. return;
  3393. }
  3394. this._loggingEnabled = value;
  3395. if (this._loggingEnabled) {
  3396. this._log = this._logEnabled;
  3397. }
  3398. else {
  3399. this._log = this._logDisabled;
  3400. }
  3401. },
  3402. enumerable: true,
  3403. configurable: true
  3404. });
  3405. Object.defineProperty(GLTFFileLoader.prototype, "capturePerformanceCounters", {
  3406. /**
  3407. * Defines if the loader should capture performance counters.
  3408. */
  3409. get: function () {
  3410. return this._capturePerformanceCounters;
  3411. },
  3412. set: function (value) {
  3413. if (this._capturePerformanceCounters === value) {
  3414. return;
  3415. }
  3416. this._capturePerformanceCounters = value;
  3417. if (this._capturePerformanceCounters) {
  3418. this._startPerformanceCounter = this._startPerformanceCounterEnabled;
  3419. this._endPerformanceCounter = this._endPerformanceCounterEnabled;
  3420. }
  3421. else {
  3422. this._startPerformanceCounter = this._startPerformanceCounterDisabled;
  3423. this._endPerformanceCounter = this._endPerformanceCounterDisabled;
  3424. }
  3425. },
  3426. enumerable: true,
  3427. configurable: true
  3428. });
  3429. Object.defineProperty(GLTFFileLoader.prototype, "onValidated", {
  3430. /**
  3431. * Callback raised after a loader extension is created.
  3432. */
  3433. set: function (callback) {
  3434. if (this._onValidatedObserver) {
  3435. this.onValidatedObservable.remove(this._onValidatedObserver);
  3436. }
  3437. this._onValidatedObserver = this.onValidatedObservable.add(callback);
  3438. },
  3439. enumerable: true,
  3440. configurable: true
  3441. });
  3442. /**
  3443. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  3444. */
  3445. GLTFFileLoader.prototype.dispose = function () {
  3446. if (this._loader) {
  3447. this._loader.dispose();
  3448. this._loader = null;
  3449. }
  3450. this._clear();
  3451. this.onDisposeObservable.notifyObservers(undefined);
  3452. this.onDisposeObservable.clear();
  3453. };
  3454. /** @hidden */
  3455. GLTFFileLoader.prototype._clear = function () {
  3456. this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
  3457. this.onMeshLoadedObservable.clear();
  3458. this.onTextureLoadedObservable.clear();
  3459. this.onMaterialLoadedObservable.clear();
  3460. this.onCameraLoadedObservable.clear();
  3461. this.onCompleteObservable.clear();
  3462. this.onExtensionLoadedObservable.clear();
  3463. };
  3464. /**
  3465. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  3466. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  3467. * @param scene the scene the meshes should be added to
  3468. * @param data the glTF data to load
  3469. * @param rootUrl root url to load from
  3470. * @param onProgress event that fires when loading progress has occured
  3471. * @param fileName Defines the name of the file to load
  3472. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  3473. */
  3474. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress, fileName) {
  3475. var _this = this;
  3476. return this._parseAsync(scene, data, rootUrl, fileName).then(function (loaderData) {
  3477. _this._log("Loading " + (fileName || ""));
  3478. _this._loader = _this._getLoader(loaderData);
  3479. return _this._loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onProgress, fileName);
  3480. });
  3481. };
  3482. /**
  3483. * Imports all objects from the loaded glTF data and adds them to the scene
  3484. * @param scene the scene the objects should be added to
  3485. * @param data the glTF data to load
  3486. * @param rootUrl root url to load from
  3487. * @param onProgress event that fires when loading progress has occured
  3488. * @param fileName Defines the name of the file to load
  3489. * @returns a promise which completes when objects have been loaded to the scene
  3490. */
  3491. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress, fileName) {
  3492. var _this = this;
  3493. return this._parseAsync(scene, data, rootUrl, fileName).then(function (loaderData) {
  3494. _this._log("Loading " + (fileName || ""));
  3495. _this._loader = _this._getLoader(loaderData);
  3496. return _this._loader.loadAsync(scene, loaderData, rootUrl, onProgress, fileName);
  3497. });
  3498. };
  3499. /**
  3500. * Load into an asset container.
  3501. * @param scene The scene to load into
  3502. * @param data The data to import
  3503. * @param rootUrl The root url for scene and resources
  3504. * @param onProgress The callback when the load progresses
  3505. * @param fileName Defines the name of the file to load
  3506. * @returns The loaded asset container
  3507. */
  3508. GLTFFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress, fileName) {
  3509. var _this = this;
  3510. return this._parseAsync(scene, data, rootUrl, fileName).then(function (loaderData) {
  3511. _this._log("Loading " + (fileName || ""));
  3512. _this._loader = _this._getLoader(loaderData);
  3513. return _this._loader.importMeshAsync(null, scene, loaderData, rootUrl, onProgress, fileName).then(function (result) {
  3514. var container = new babylonjs_1.AssetContainer(scene);
  3515. Array.prototype.push.apply(container.meshes, result.meshes);
  3516. Array.prototype.push.apply(container.particleSystems, result.particleSystems);
  3517. Array.prototype.push.apply(container.skeletons, result.skeletons);
  3518. Array.prototype.push.apply(container.animationGroups, result.animationGroups);
  3519. container.removeAllFromScene();
  3520. return container;
  3521. });
  3522. });
  3523. };
  3524. /**
  3525. * If the data string can be loaded directly.
  3526. * @param data string contianing the file data
  3527. * @returns if the data can be loaded directly
  3528. */
  3529. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  3530. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  3531. };
  3532. /**
  3533. * Instantiates a glTF file loader plugin.
  3534. * @returns the created plugin
  3535. */
  3536. GLTFFileLoader.prototype.createPlugin = function () {
  3537. return new GLTFFileLoader();
  3538. };
  3539. Object.defineProperty(GLTFFileLoader.prototype, "loaderState", {
  3540. /**
  3541. * The loader state or null if the loader is not active.
  3542. */
  3543. get: function () {
  3544. return this._loader ? this._loader.state : null;
  3545. },
  3546. enumerable: true,
  3547. configurable: true
  3548. });
  3549. /**
  3550. * Returns a promise that resolves when the asset is completely loaded.
  3551. * @returns a promise that resolves when the asset is completely loaded.
  3552. */
  3553. GLTFFileLoader.prototype.whenCompleteAsync = function () {
  3554. var _this = this;
  3555. return new Promise(function (resolve, reject) {
  3556. _this.onCompleteObservable.addOnce(function () {
  3557. resolve();
  3558. });
  3559. _this.onErrorObservable.addOnce(function (reason) {
  3560. reject(reason);
  3561. });
  3562. });
  3563. };
  3564. GLTFFileLoader.prototype._parseAsync = function (scene, data, rootUrl, fileName) {
  3565. var _this = this;
  3566. return Promise.resolve().then(function () {
  3567. var unpacked = (data instanceof ArrayBuffer) ? _this._unpackBinary(data) : { json: data, bin: null };
  3568. return _this._validateAsync(scene, unpacked.json, rootUrl, fileName).then(function () {
  3569. _this._startPerformanceCounter("Parse JSON");
  3570. _this._log("JSON length: " + unpacked.json.length);
  3571. var loaderData = {
  3572. json: JSON.parse(unpacked.json),
  3573. bin: unpacked.bin
  3574. };
  3575. _this._endPerformanceCounter("Parse JSON");
  3576. _this.onParsedObservable.notifyObservers(loaderData);
  3577. _this.onParsedObservable.clear();
  3578. return loaderData;
  3579. });
  3580. });
  3581. };
  3582. GLTFFileLoader.prototype._validateAsync = function (scene, json, rootUrl, fileName) {
  3583. var _this = this;
  3584. if (!this.validate || typeof GLTFValidator === "undefined") {
  3585. return Promise.resolve();
  3586. }
  3587. this._startPerformanceCounter("Validate JSON");
  3588. var options = {
  3589. externalResourceFunction: function (uri) {
  3590. return _this.preprocessUrlAsync(rootUrl + uri)
  3591. .then(function (url) { return scene._loadFileAsync(url, true, true); })
  3592. .then(function (data) { return new Uint8Array(data); });
  3593. }
  3594. };
  3595. if (fileName && fileName.substr(0, 5) !== "data:") {
  3596. options.uri = (rootUrl === "file:" ? fileName : "" + rootUrl + fileName);
  3597. }
  3598. return GLTFValidator.validateString(json, options).then(function (result) {
  3599. _this._endPerformanceCounter("Validate JSON");
  3600. _this.onValidatedObservable.notifyObservers(result);
  3601. _this.onValidatedObservable.clear();
  3602. }, function (reason) {
  3603. _this._endPerformanceCounter("Validate JSON");
  3604. babylonjs_1.Tools.Warn("Failed to validate: " + reason);
  3605. _this.onValidatedObservable.clear();
  3606. });
  3607. };
  3608. GLTFFileLoader.prototype._getLoader = function (loaderData) {
  3609. var asset = loaderData.json.asset || {};
  3610. this._log("Asset version: " + asset.version);
  3611. asset.minVersion && this._log("Asset minimum version: " + asset.minVersion);
  3612. asset.generator && this._log("Asset generator: " + asset.generator);
  3613. var version = GLTFFileLoader._parseVersion(asset.version);
  3614. if (!version) {
  3615. throw new Error("Invalid version: " + asset.version);
  3616. }
  3617. if (asset.minVersion !== undefined) {
  3618. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  3619. if (!minVersion) {
  3620. throw new Error("Invalid minimum version: " + asset.minVersion);
  3621. }
  3622. if (GLTFFileLoader._compareVersion(minVersion, { major: 2, minor: 0 }) > 0) {
  3623. throw new Error("Incompatible minimum version: " + asset.minVersion);
  3624. }
  3625. }
  3626. var createLoaders = {
  3627. 1: GLTFFileLoader._CreateGLTF1Loader,
  3628. 2: GLTFFileLoader._CreateGLTFLoaderV2
  3629. };
  3630. var createLoader = createLoaders[version.major];
  3631. if (!createLoader) {
  3632. throw new Error("Unsupported version: " + asset.version);
  3633. }
  3634. return createLoader(this);
  3635. };
  3636. GLTFFileLoader.prototype._unpackBinary = function (data) {
  3637. this._startPerformanceCounter("Unpack binary");
  3638. this._log("Binary length: " + data.byteLength);
  3639. var Binary = {
  3640. Magic: 0x46546C67
  3641. };
  3642. var binaryReader = new BinaryReader(data);
  3643. var magic = binaryReader.readUint32();
  3644. if (magic !== Binary.Magic) {
  3645. throw new Error("Unexpected magic: " + magic);
  3646. }
  3647. var version = binaryReader.readUint32();
  3648. if (this.loggingEnabled) {
  3649. this._log("Binary version: " + version);
  3650. }
  3651. var unpacked;
  3652. switch (version) {
  3653. case 1: {
  3654. unpacked = this._unpackBinaryV1(binaryReader);
  3655. break;
  3656. }
  3657. case 2: {
  3658. unpacked = this._unpackBinaryV2(binaryReader);
  3659. break;
  3660. }
  3661. default: {
  3662. throw new Error("Unsupported version: " + version);
  3663. }
  3664. }
  3665. this._endPerformanceCounter("Unpack binary");
  3666. return unpacked;
  3667. };
  3668. GLTFFileLoader.prototype._unpackBinaryV1 = function (binaryReader) {
  3669. var ContentFormat = {
  3670. JSON: 0
  3671. };
  3672. var length = binaryReader.readUint32();
  3673. if (length != binaryReader.getLength()) {
  3674. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  3675. }
  3676. var contentLength = binaryReader.readUint32();
  3677. var contentFormat = binaryReader.readUint32();
  3678. var content;
  3679. switch (contentFormat) {
  3680. case ContentFormat.JSON: {
  3681. content = GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength));
  3682. break;
  3683. }
  3684. default: {
  3685. throw new Error("Unexpected content format: " + contentFormat);
  3686. }
  3687. }
  3688. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  3689. var body = binaryReader.readUint8Array(bytesRemaining);
  3690. return {
  3691. json: content,
  3692. bin: body
  3693. };
  3694. };
  3695. GLTFFileLoader.prototype._unpackBinaryV2 = function (binaryReader) {
  3696. var ChunkFormat = {
  3697. JSON: 0x4E4F534A,
  3698. BIN: 0x004E4942
  3699. };
  3700. var length = binaryReader.readUint32();
  3701. if (length !== binaryReader.getLength()) {
  3702. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  3703. }
  3704. // JSON chunk
  3705. var chunkLength = binaryReader.readUint32();
  3706. var chunkFormat = binaryReader.readUint32();
  3707. if (chunkFormat !== ChunkFormat.JSON) {
  3708. throw new Error("First chunk format is not JSON");
  3709. }
  3710. var json = GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength));
  3711. // Look for BIN chunk
  3712. var bin = null;
  3713. while (binaryReader.getPosition() < binaryReader.getLength()) {
  3714. var chunkLength_1 = binaryReader.readUint32();
  3715. var chunkFormat_1 = binaryReader.readUint32();
  3716. switch (chunkFormat_1) {
  3717. case ChunkFormat.JSON: {
  3718. throw new Error("Unexpected JSON chunk");
  3719. }
  3720. case ChunkFormat.BIN: {
  3721. bin = binaryReader.readUint8Array(chunkLength_1);
  3722. break;
  3723. }
  3724. default: {
  3725. // ignore unrecognized chunkFormat
  3726. binaryReader.skipBytes(chunkLength_1);
  3727. break;
  3728. }
  3729. }
  3730. }
  3731. return {
  3732. json: json,
  3733. bin: bin
  3734. };
  3735. };
  3736. GLTFFileLoader._parseVersion = function (version) {
  3737. if (version === "1.0" || version === "1.0.1") {
  3738. return {
  3739. major: 1,
  3740. minor: 0
  3741. };
  3742. }
  3743. var match = (version + "").match(/^(\d+)\.(\d+)/);
  3744. if (!match) {
  3745. return null;
  3746. }
  3747. return {
  3748. major: parseInt(match[1]),
  3749. minor: parseInt(match[2])
  3750. };
  3751. };
  3752. GLTFFileLoader._compareVersion = function (a, b) {
  3753. if (a.major > b.major) {
  3754. return 1;
  3755. }
  3756. if (a.major < b.major) {
  3757. return -1;
  3758. }
  3759. if (a.minor > b.minor) {
  3760. return 1;
  3761. }
  3762. if (a.minor < b.minor) {
  3763. return -1;
  3764. }
  3765. return 0;
  3766. };
  3767. GLTFFileLoader._decodeBufferToText = function (buffer) {
  3768. var result = "";
  3769. var length = buffer.byteLength;
  3770. for (var i = 0; i < length; i++) {
  3771. result += String.fromCharCode(buffer[i]);
  3772. }
  3773. return result;
  3774. };
  3775. /** @hidden */
  3776. GLTFFileLoader.prototype._logOpen = function (message) {
  3777. this._log(message);
  3778. this._logIndentLevel++;
  3779. };
  3780. /** @hidden */
  3781. GLTFFileLoader.prototype._logClose = function () {
  3782. --this._logIndentLevel;
  3783. };
  3784. GLTFFileLoader.prototype._logEnabled = function (message) {
  3785. var spaces = GLTFFileLoader._logSpaces.substr(0, this._logIndentLevel * 2);
  3786. babylonjs_1.Tools.Log("" + spaces + message);
  3787. };
  3788. GLTFFileLoader.prototype._logDisabled = function (message) {
  3789. };
  3790. GLTFFileLoader.prototype._startPerformanceCounterEnabled = function (counterName) {
  3791. babylonjs_1.Tools.StartPerformanceCounter(counterName);
  3792. };
  3793. GLTFFileLoader.prototype._startPerformanceCounterDisabled = function (counterName) {
  3794. };
  3795. GLTFFileLoader.prototype._endPerformanceCounterEnabled = function (counterName) {
  3796. babylonjs_1.Tools.EndPerformanceCounter(counterName);
  3797. };
  3798. GLTFFileLoader.prototype._endPerformanceCounterDisabled = function (counterName) {
  3799. };
  3800. // ----------
  3801. // V1 options
  3802. // ----------
  3803. /**
  3804. * 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.
  3805. * Textures always loads asynchronously. For example, the success callback can compute the bounding information of the loaded meshes when incremental loading is disabled.
  3806. * Defaults to true.
  3807. * @hidden
  3808. */
  3809. GLTFFileLoader.IncrementalLoading = true;
  3810. /**
  3811. * Set this property to true in order to work with homogeneous coordinates, available with some converters and exporters.
  3812. * Defaults to false. See https://en.wikipedia.org/wiki/Homogeneous_coordinates.
  3813. * @hidden
  3814. */
  3815. GLTFFileLoader.HomogeneousCoordinates = false;
  3816. GLTFFileLoader._logSpaces = " ";
  3817. return GLTFFileLoader;
  3818. }());
  3819. exports.GLTFFileLoader = GLTFFileLoader;
  3820. var BinaryReader = /** @class */ (function () {
  3821. function BinaryReader(arrayBuffer) {
  3822. this._arrayBuffer = arrayBuffer;
  3823. this._dataView = new DataView(arrayBuffer);
  3824. this._byteOffset = 0;
  3825. }
  3826. BinaryReader.prototype.getPosition = function () {
  3827. return this._byteOffset;
  3828. };
  3829. BinaryReader.prototype.getLength = function () {
  3830. return this._arrayBuffer.byteLength;
  3831. };
  3832. BinaryReader.prototype.readUint32 = function () {
  3833. var value = this._dataView.getUint32(this._byteOffset, true);
  3834. this._byteOffset += 4;
  3835. return value;
  3836. };
  3837. BinaryReader.prototype.readUint8Array = function (length) {
  3838. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  3839. this._byteOffset += length;
  3840. return value;
  3841. };
  3842. BinaryReader.prototype.skipBytes = function (length) {
  3843. this._byteOffset += length;
  3844. };
  3845. return BinaryReader;
  3846. }());
  3847. if (babylonjs_1.SceneLoader) {
  3848. babylonjs_1.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  3849. }
  3850. /***/ }),
  3851. /***/ "babylonjs":
  3852. /*!****************************************************************************************************!*\
  3853. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  3854. \****************************************************************************************************/
  3855. /*! no static exports found */
  3856. /***/ (function(module, exports) {
  3857. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  3858. /***/ })
  3859. /******/ });
  3860. });
  3861. //# sourceMappingURL=babylon.glTF2FileLoader.js.map