babylon.glTF2FileLoader.js 198 KB

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