babylon.glTF2FileLoader.js 185 KB

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