babylonjs.serializers.js 247 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665
  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-serializers", ["babylonjs"], factory);
  6. else if(typeof exports === 'object')
  7. exports["babylonjs-serializers"] = factory(require("babylonjs"));
  8. else
  9. root["SERIALIZERS"] = factory(root["BABYLON"]);
  10. })((typeof self !== "undefined" ? self : typeof global !== "undefined" ? global : this), function(__WEBPACK_EXTERNAL_MODULE_babylonjs_Maths_math__) {
  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.ts");
  95. /******/ })
  96. /************************************************************************/
  97. /******/ ({
  98. /***/ "../../node_modules/webpack/buildin/global.js":
  99. /*!***********************************!*\
  100. !*** (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 || new Function("return 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. /***/ "./OBJ/index.ts":
  122. /*!**********************!*\
  123. !*** ./OBJ/index.ts ***!
  124. \**********************/
  125. /*! exports provided: OBJExport */
  126. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  127. "use strict";
  128. __webpack_require__.r(__webpack_exports__);
  129. /* harmony import */ var _objSerializer__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./objSerializer */ "./OBJ/objSerializer.ts");
  130. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "OBJExport", function() { return _objSerializer__WEBPACK_IMPORTED_MODULE_0__["OBJExport"]; });
  131. /***/ }),
  132. /***/ "./OBJ/objSerializer.ts":
  133. /*!******************************!*\
  134. !*** ./OBJ/objSerializer.ts ***!
  135. \******************************/
  136. /*! exports provided: OBJExport */
  137. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  138. "use strict";
  139. __webpack_require__.r(__webpack_exports__);
  140. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "OBJExport", function() { return OBJExport; });
  141. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Maths/math */ "babylonjs/Maths/math");
  142. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__);
  143. /**
  144. * Class for generating OBJ data from a Babylon scene.
  145. */
  146. var OBJExport = /** @class */ (function () {
  147. function OBJExport() {
  148. }
  149. /**
  150. * Exports the geometry of a Mesh array in .OBJ file format (text)
  151. * @param mesh defines the list of meshes to serialize
  152. * @param materials defines if materials should be exported
  153. * @param matlibname defines the name of the associated mtl file
  154. * @param globalposition defines if the exported positions are globals or local to the exported mesh
  155. * @returns the OBJ content
  156. */
  157. OBJExport.OBJ = function (mesh, materials, matlibname, globalposition) {
  158. var output = [];
  159. var v = 1;
  160. if (materials) {
  161. if (!matlibname) {
  162. matlibname = 'mat';
  163. }
  164. output.push("mtllib " + matlibname + ".mtl");
  165. }
  166. for (var j = 0; j < mesh.length; j++) {
  167. output.push("g object" + j);
  168. output.push("o object_" + j);
  169. //Uses the position of the item in the scene, to the file (this back to normal in the end)
  170. var lastMatrix = null;
  171. if (globalposition) {
  172. var newMatrix = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Matrix"].Translation(mesh[j].position.x, mesh[j].position.y, mesh[j].position.z);
  173. lastMatrix = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Matrix"].Translation(-(mesh[j].position.x), -(mesh[j].position.y), -(mesh[j].position.z));
  174. mesh[j].bakeTransformIntoVertices(newMatrix);
  175. }
  176. //TODO: submeshes (groups)
  177. //TODO: smoothing groups (s 1, s off);
  178. if (materials) {
  179. var mat = mesh[j].material;
  180. if (mat) {
  181. output.push("usemtl " + mat.id);
  182. }
  183. }
  184. var g = mesh[j].geometry;
  185. if (!g) {
  186. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("No geometry is present on the mesh");
  187. continue;
  188. }
  189. var trunkVerts = g.getVerticesData('position');
  190. var trunkNormals = g.getVerticesData('normal');
  191. var trunkUV = g.getVerticesData('uv');
  192. var trunkFaces = g.getIndices();
  193. var curV = 0;
  194. if (!trunkVerts || !trunkFaces) {
  195. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("There are no position vertices or indices on the mesh!");
  196. continue;
  197. }
  198. for (var i = 0; i < trunkVerts.length; i += 3) {
  199. output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
  200. curV++;
  201. }
  202. if (trunkNormals != null) {
  203. for (i = 0; i < trunkNormals.length; i += 3) {
  204. output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
  205. }
  206. }
  207. if (trunkUV != null) {
  208. for (i = 0; i < trunkUV.length; i += 2) {
  209. output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
  210. }
  211. }
  212. for (i = 0; i < trunkFaces.length; i += 3) {
  213. var indices = [String(trunkFaces[i + 2] + v), String(trunkFaces[i + 1] + v), String(trunkFaces[i] + v)];
  214. var blanks = ["", "", ""];
  215. var facePositions = indices;
  216. var faceUVs = trunkUV != null ? indices : blanks;
  217. var faceNormals = trunkNormals != null ? indices : blanks;
  218. output.push("f " + facePositions[0] + "/" + faceUVs[0] + "/" + faceNormals[0] +
  219. " " + facePositions[1] + "/" + faceUVs[1] + "/" + faceNormals[1] +
  220. " " + facePositions[2] + "/" + faceUVs[2] + "/" + faceNormals[2]);
  221. }
  222. //back de previous matrix, to not change the original mesh in the scene
  223. if (globalposition && lastMatrix) {
  224. mesh[j].bakeTransformIntoVertices(lastMatrix);
  225. }
  226. v += curV;
  227. }
  228. var text = output.join("\n");
  229. return (text);
  230. };
  231. /**
  232. * Exports the material(s) of a mesh in .MTL file format (text)
  233. * @param mesh defines the mesh to extract the material from
  234. * @returns the mtl content
  235. */
  236. //TODO: Export the materials of mesh array
  237. OBJExport.MTL = function (mesh) {
  238. var output = [];
  239. var m = mesh.material;
  240. output.push("newmtl mat1");
  241. output.push(" Ns " + m.specularPower.toFixed(4));
  242. output.push(" Ni 1.5000");
  243. output.push(" d " + m.alpha.toFixed(4));
  244. output.push(" Tr 0.0000");
  245. output.push(" Tf 1.0000 1.0000 1.0000");
  246. output.push(" illum 2");
  247. output.push(" Ka " + m.ambientColor.r.toFixed(4) + " " + m.ambientColor.g.toFixed(4) + " " + m.ambientColor.b.toFixed(4));
  248. output.push(" Kd " + m.diffuseColor.r.toFixed(4) + " " + m.diffuseColor.g.toFixed(4) + " " + m.diffuseColor.b.toFixed(4));
  249. output.push(" Ks " + m.specularColor.r.toFixed(4) + " " + m.specularColor.g.toFixed(4) + " " + m.specularColor.b.toFixed(4));
  250. output.push(" Ke " + m.emissiveColor.r.toFixed(4) + " " + m.emissiveColor.g.toFixed(4) + " " + m.emissiveColor.b.toFixed(4));
  251. //TODO: uv scale, offset, wrap
  252. //TODO: UV mirrored in Blender? second UV channel? lightMap? reflection textures?
  253. var uvscale = "";
  254. if (m.ambientTexture) {
  255. output.push(" map_Ka " + uvscale + m.ambientTexture.name);
  256. }
  257. if (m.diffuseTexture) {
  258. output.push(" map_Kd " + uvscale + m.diffuseTexture.name);
  259. //TODO: alpha testing, opacity in diffuse texture alpha channel (diffuseTexture.hasAlpha -> map_d)
  260. }
  261. if (m.specularTexture) {
  262. output.push(" map_Ks " + uvscale + m.specularTexture.name);
  263. /* TODO: glossiness = specular highlight component is in alpha channel of specularTexture. (???)
  264. if (m.useGlossinessFromSpecularMapAlpha) {
  265. output.push(" map_Ns "+uvscale + m.specularTexture.name);
  266. }
  267. */
  268. }
  269. /* TODO: emissive texture not in .MAT format (???)
  270. if (m.emissiveTexture) {
  271. output.push(" map_d "+uvscale+m.emissiveTexture.name);
  272. }
  273. */
  274. if (m.bumpTexture) {
  275. output.push(" map_bump -imfchan z " + uvscale + m.bumpTexture.name);
  276. }
  277. if (m.opacityTexture) {
  278. output.push(" map_d " + uvscale + m.opacityTexture.name);
  279. }
  280. var text = output.join("\n");
  281. return (text);
  282. };
  283. return OBJExport;
  284. }());
  285. /***/ }),
  286. /***/ "./glTF/2.0/Extensions/KHR_lights_punctual.ts":
  287. /*!****************************************************!*\
  288. !*** ./glTF/2.0/Extensions/KHR_lights_punctual.ts ***!
  289. \****************************************************/
  290. /*! exports provided: KHR_lights_punctual */
  291. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  292. "use strict";
  293. __webpack_require__.r(__webpack_exports__);
  294. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "KHR_lights_punctual", function() { return KHR_lights_punctual; });
  295. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Maths/math */ "babylonjs/Maths/math");
  296. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__);
  297. /* harmony import */ var _glTFExporter__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../glTFExporter */ "./glTF/2.0/glTFExporter.ts");
  298. /* harmony import */ var _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../glTFUtilities */ "./glTF/2.0/glTFUtilities.ts");
  299. var NAME = "KHR_lights_punctual";
  300. var LightType;
  301. (function (LightType) {
  302. LightType["DIRECTIONAL"] = "directional";
  303. LightType["POINT"] = "point";
  304. LightType["SPOT"] = "spot";
  305. })(LightType || (LightType = {}));
  306. /**
  307. * [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_lights_punctual/README.md)
  308. */
  309. var KHR_lights_punctual = /** @class */ (function () {
  310. /** @hidden */
  311. function KHR_lights_punctual(exporter) {
  312. /** The name of this extension. */
  313. this.name = NAME;
  314. /** Defines whether this extension is enabled. */
  315. this.enabled = true;
  316. /** Defines whether this extension is required */
  317. this.required = false;
  318. this._exporter = exporter;
  319. }
  320. /** @hidden */
  321. KHR_lights_punctual.prototype.dispose = function () {
  322. delete this._exporter;
  323. delete this._lights;
  324. };
  325. /** @hidden */
  326. KHR_lights_punctual.prototype.onExporting = function () {
  327. if (this._lights) {
  328. if (this._exporter._glTF.extensionsUsed == null) {
  329. this._exporter._glTF.extensionsUsed = [];
  330. }
  331. if (this._exporter._glTF.extensionsUsed.indexOf(NAME) === -1) {
  332. this._exporter._glTF.extensionsUsed.push(NAME);
  333. }
  334. if (this.required) {
  335. if (this._exporter._glTF.extensionsRequired == null) {
  336. this._exporter._glTF.extensionsRequired = [];
  337. }
  338. if (this._exporter._glTF.extensionsRequired.indexOf(NAME) === -1) {
  339. this._exporter._glTF.extensionsRequired.push(NAME);
  340. }
  341. }
  342. if (this._exporter._glTF.extensions == null) {
  343. this._exporter._glTF.extensions = {};
  344. }
  345. this._exporter._glTF.extensions[NAME] = this._lights;
  346. }
  347. };
  348. /**
  349. * Define this method to modify the default behavior when exporting a node
  350. * @param context The context when exporting the node
  351. * @param node glTF node
  352. * @param babylonNode BabylonJS node
  353. * @returns nullable INode promise
  354. */
  355. KHR_lights_punctual.prototype.postExportNodeAsync = function (context, node, babylonNode) {
  356. var _this = this;
  357. return new Promise(function (resolve, reject) {
  358. if (babylonNode instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["ShadowLight"]) {
  359. var babylonLight = babylonNode;
  360. var light = void 0;
  361. var lightType = (babylonLight.getTypeID() == babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Light"].LIGHTTYPEID_POINTLIGHT ? LightType.POINT : (babylonLight.getTypeID() == babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Light"].LIGHTTYPEID_DIRECTIONALLIGHT ? LightType.DIRECTIONAL : (babylonLight.getTypeID() == babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Light"].LIGHTTYPEID_SPOTLIGHT ? LightType.SPOT : null)));
  362. if (lightType == null) {
  363. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Logger"].Warn(context + ": Light " + babylonLight.name + " is not supported in " + NAME);
  364. }
  365. else {
  366. var lightPosition = babylonLight.position.clone();
  367. if (!lightPosition.equals(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].Zero())) {
  368. if (_this._exporter._convertToRightHandedSystem) {
  369. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedPositionVector3FromRef(lightPosition);
  370. }
  371. node.translation = lightPosition.asArray();
  372. }
  373. if (lightType !== LightType.POINT) {
  374. var localAxis = babylonLight.direction;
  375. var yaw = -Math.atan2(localAxis.z, localAxis.x) + Math.PI / 2;
  376. var len = Math.sqrt(localAxis.x * localAxis.x + localAxis.z * localAxis.z);
  377. var pitch = -Math.atan2(localAxis.y, len);
  378. var lightRotationQuaternion = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].RotationYawPitchRoll(yaw, pitch, 0);
  379. if (_this._exporter._convertToRightHandedSystem) {
  380. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedQuaternionFromRef(lightRotationQuaternion);
  381. }
  382. if (!lightRotationQuaternion.equals(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].Identity())) {
  383. node.rotation = lightRotationQuaternion.asArray();
  384. }
  385. }
  386. if (babylonLight.falloffType !== babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Light"].FALLOFF_GLTF) {
  387. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Logger"].Warn(context + ": Light falloff for " + babylonLight.name + " does not match the " + NAME + " specification!");
  388. }
  389. light = {
  390. type: lightType
  391. };
  392. if (!babylonLight.diffuse.equals(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].White())) {
  393. light.color = babylonLight.diffuse.asArray();
  394. }
  395. if (babylonLight.intensity !== 1.0) {
  396. light.intensity = babylonLight.intensity;
  397. }
  398. if (babylonLight.range !== Number.MAX_VALUE) {
  399. light.range = babylonLight.range;
  400. }
  401. if (lightType === LightType.SPOT) {
  402. var babylonSpotLight = babylonLight;
  403. if (babylonSpotLight.angle !== Math.PI / 2.0) {
  404. if (light.spot == null) {
  405. light.spot = {};
  406. }
  407. light.spot.outerConeAngle = babylonSpotLight.angle / 2.0;
  408. }
  409. if (babylonSpotLight.innerAngle !== 0) {
  410. if (light.spot == null) {
  411. light.spot = {};
  412. }
  413. light.spot.innerConeAngle = babylonSpotLight.innerAngle / 2.0;
  414. }
  415. }
  416. if (_this._lights == null) {
  417. _this._lights = {
  418. lights: []
  419. };
  420. }
  421. _this._lights.lights.push(light);
  422. if (node.extensions == null) {
  423. node.extensions = {};
  424. }
  425. var lightReference = {
  426. light: _this._lights.lights.length - 1
  427. };
  428. node.extensions[NAME] = lightReference;
  429. }
  430. }
  431. resolve(node);
  432. });
  433. };
  434. return KHR_lights_punctual;
  435. }());
  436. _glTFExporter__WEBPACK_IMPORTED_MODULE_1__["_Exporter"].RegisterExtension(NAME, function (exporter) { return new KHR_lights_punctual(exporter); });
  437. /***/ }),
  438. /***/ "./glTF/2.0/Extensions/KHR_texture_transform.ts":
  439. /*!******************************************************!*\
  440. !*** ./glTF/2.0/Extensions/KHR_texture_transform.ts ***!
  441. \******************************************************/
  442. /*! exports provided: KHR_texture_transform */
  443. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  444. "use strict";
  445. __webpack_require__.r(__webpack_exports__);
  446. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "KHR_texture_transform", function() { return KHR_texture_transform; });
  447. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Maths/math */ "babylonjs/Maths/math");
  448. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__);
  449. /* harmony import */ var _glTFExporter__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../glTFExporter */ "./glTF/2.0/glTFExporter.ts");
  450. /* harmony import */ var _shaders_textureTransform_fragment__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../shaders/textureTransform.fragment */ "./glTF/2.0/shaders/textureTransform.fragment.ts");
  451. var NAME = "KHR_texture_transform";
  452. /**
  453. * @hidden
  454. */
  455. var KHR_texture_transform = /** @class */ (function () {
  456. function KHR_texture_transform(exporter) {
  457. /** Name of this extension */
  458. this.name = NAME;
  459. /** Defines whether this extension is enabled */
  460. this.enabled = true;
  461. /** Defines whether this extension is required */
  462. this.required = false;
  463. this._exporter = exporter;
  464. }
  465. KHR_texture_transform.prototype.dispose = function () {
  466. delete this._exporter;
  467. };
  468. KHR_texture_transform.prototype.preExportTextureAsync = function (context, babylonTexture, mimeType) {
  469. var _this = this;
  470. return new Promise(function (resolve, reject) {
  471. var texture_transform_extension = {};
  472. if (babylonTexture.uOffset !== 0 || babylonTexture.vOffset !== 0) {
  473. texture_transform_extension.offset = [babylonTexture.uOffset, babylonTexture.vOffset];
  474. }
  475. if (babylonTexture.uScale !== 1 || babylonTexture.vScale !== 1) {
  476. texture_transform_extension.scale = [babylonTexture.uScale, babylonTexture.vScale];
  477. }
  478. if (babylonTexture.wAng !== 0) {
  479. texture_transform_extension.rotation = babylonTexture.wAng;
  480. }
  481. if (!Object.keys(texture_transform_extension).length) {
  482. resolve(babylonTexture);
  483. }
  484. var scale = texture_transform_extension.scale ? new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"](texture_transform_extension.scale[0], texture_transform_extension.scale[1]) : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"].One();
  485. var rotation = texture_transform_extension.rotation != null ? texture_transform_extension.rotation : 0;
  486. var offset = texture_transform_extension.offset ? new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"](texture_transform_extension.offset[0], texture_transform_extension.offset[1]) : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"].Zero();
  487. var scene = babylonTexture.getScene();
  488. if (!scene) {
  489. reject(context + ": \"scene\" is not defined for Babylon texture " + babylonTexture.name + "!");
  490. }
  491. else {
  492. _this.textureTransformTextureAsync(babylonTexture, offset, rotation, scale, scene).then(function (texture) {
  493. resolve(texture);
  494. });
  495. }
  496. });
  497. };
  498. /**
  499. * Transform the babylon texture by the offset, rotation and scale parameters using a procedural texture
  500. * @param babylonTexture
  501. * @param offset
  502. * @param rotation
  503. * @param scale
  504. * @param scene
  505. */
  506. KHR_texture_transform.prototype.textureTransformTextureAsync = function (babylonTexture, offset, rotation, scale, scene) {
  507. return new Promise(function (resolve, reject) {
  508. var proceduralTexture = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["ProceduralTexture"]("" + babylonTexture.name, babylonTexture.getSize(), "textureTransform", scene);
  509. if (!proceduralTexture) {
  510. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Log("Cannot create procedural texture for " + babylonTexture.name + "!");
  511. resolve(babylonTexture);
  512. }
  513. proceduralTexture.setTexture("textureSampler", babylonTexture);
  514. proceduralTexture.setMatrix("textureTransformMat", babylonTexture.getTextureMatrix());
  515. // isReady trigger creation of effect if it doesnt exist yet
  516. if (proceduralTexture.isReady()) {
  517. proceduralTexture.render();
  518. resolve(proceduralTexture);
  519. }
  520. else {
  521. proceduralTexture.getEffect().executeWhenCompiled(function () {
  522. proceduralTexture.render();
  523. resolve(proceduralTexture);
  524. });
  525. }
  526. });
  527. };
  528. return KHR_texture_transform;
  529. }());
  530. _glTFExporter__WEBPACK_IMPORTED_MODULE_1__["_Exporter"].RegisterExtension(NAME, function (exporter) { return new KHR_texture_transform(exporter); });
  531. /***/ }),
  532. /***/ "./glTF/2.0/Extensions/index.ts":
  533. /*!**************************************!*\
  534. !*** ./glTF/2.0/Extensions/index.ts ***!
  535. \**************************************/
  536. /*! exports provided: KHR_texture_transform, KHR_lights_punctual */
  537. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  538. "use strict";
  539. __webpack_require__.r(__webpack_exports__);
  540. /* harmony import */ var _KHR_texture_transform__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./KHR_texture_transform */ "./glTF/2.0/Extensions/KHR_texture_transform.ts");
  541. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_texture_transform", function() { return _KHR_texture_transform__WEBPACK_IMPORTED_MODULE_0__["KHR_texture_transform"]; });
  542. /* harmony import */ var _KHR_lights_punctual__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./KHR_lights_punctual */ "./glTF/2.0/Extensions/KHR_lights_punctual.ts");
  543. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_lights_punctual", function() { return _KHR_lights_punctual__WEBPACK_IMPORTED_MODULE_1__["KHR_lights_punctual"]; });
  544. /***/ }),
  545. /***/ "./glTF/2.0/glTFAnimation.ts":
  546. /*!***********************************!*\
  547. !*** ./glTF/2.0/glTFAnimation.ts ***!
  548. \***********************************/
  549. /*! exports provided: _GLTFAnimation */
  550. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  551. "use strict";
  552. __webpack_require__.r(__webpack_exports__);
  553. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "_GLTFAnimation", function() { return _GLTFAnimation; });
  554. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Maths/math */ "babylonjs/Maths/math");
  555. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__);
  556. /* harmony import */ var _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./glTFUtilities */ "./glTF/2.0/glTFUtilities.ts");
  557. /**
  558. * @hidden
  559. * Enum for handling in tangent and out tangent.
  560. */
  561. var _TangentType;
  562. (function (_TangentType) {
  563. /**
  564. * Specifies that input tangents are used.
  565. */
  566. _TangentType[_TangentType["INTANGENT"] = 0] = "INTANGENT";
  567. /**
  568. * Specifies that output tangents are used.
  569. */
  570. _TangentType[_TangentType["OUTTANGENT"] = 1] = "OUTTANGENT";
  571. })(_TangentType || (_TangentType = {}));
  572. /**
  573. * @hidden
  574. * Utility class for generating glTF animation data from BabylonJS.
  575. */
  576. var _GLTFAnimation = /** @class */ (function () {
  577. function _GLTFAnimation() {
  578. }
  579. /**
  580. * @ignore
  581. *
  582. * Creates glTF channel animation from BabylonJS animation.
  583. * @param babylonTransformNode - BabylonJS mesh.
  584. * @param animation - animation.
  585. * @param animationChannelTargetPath - The target animation channel.
  586. * @param convertToRightHandedSystem - Specifies if the values should be converted to right-handed.
  587. * @param useQuaternion - Specifies if quaternions are used.
  588. * @returns nullable IAnimationData
  589. */
  590. _GLTFAnimation._CreateNodeAnimation = function (babylonTransformNode, animation, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion, animationSampleRate) {
  591. var inputs = [];
  592. var outputs = [];
  593. var keyFrames = animation.getKeys();
  594. var minMaxKeyFrames = _GLTFAnimation.calculateMinMaxKeyFrames(keyFrames);
  595. var interpolationOrBake = _GLTFAnimation._DeduceInterpolation(keyFrames, animationChannelTargetPath, useQuaternion);
  596. var frameDelta = minMaxKeyFrames.max - minMaxKeyFrames.min;
  597. var interpolation = interpolationOrBake.interpolationType;
  598. var shouldBakeAnimation = interpolationOrBake.shouldBakeAnimation;
  599. if (shouldBakeAnimation) {
  600. _GLTFAnimation._CreateBakedAnimation(babylonTransformNode, animation, animationChannelTargetPath, minMaxKeyFrames.min, minMaxKeyFrames.max, animation.framePerSecond, animationSampleRate, inputs, outputs, minMaxKeyFrames, convertToRightHandedSystem, useQuaternion);
  601. }
  602. else {
  603. if (interpolation === "LINEAR" /* LINEAR */ || interpolation === "STEP" /* STEP */) {
  604. _GLTFAnimation._CreateLinearOrStepAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  605. }
  606. else if (interpolation === "CUBICSPLINE" /* CUBICSPLINE */) {
  607. _GLTFAnimation._CreateCubicSplineAnimation(babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  608. }
  609. else {
  610. _GLTFAnimation._CreateBakedAnimation(babylonTransformNode, animation, animationChannelTargetPath, minMaxKeyFrames.min, minMaxKeyFrames.max, animation.framePerSecond, animationSampleRate, inputs, outputs, minMaxKeyFrames, convertToRightHandedSystem, useQuaternion);
  611. }
  612. }
  613. if (inputs.length && outputs.length) {
  614. var result = {
  615. inputs: inputs,
  616. outputs: outputs,
  617. samplerInterpolation: interpolation,
  618. inputsMin: shouldBakeAnimation ? minMaxKeyFrames.min : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].FloatRound(minMaxKeyFrames.min / animation.framePerSecond),
  619. inputsMax: shouldBakeAnimation ? minMaxKeyFrames.max : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].FloatRound(minMaxKeyFrames.max / animation.framePerSecond)
  620. };
  621. return result;
  622. }
  623. return null;
  624. };
  625. _GLTFAnimation._DeduceAnimationInfo = function (animation) {
  626. var animationChannelTargetPath = null;
  627. var dataAccessorType = "VEC3" /* VEC3 */;
  628. var useQuaternion = false;
  629. var property = animation.targetProperty.split('.');
  630. switch (property[0]) {
  631. case 'scaling': {
  632. animationChannelTargetPath = "scale" /* SCALE */;
  633. break;
  634. }
  635. case 'position': {
  636. animationChannelTargetPath = "translation" /* TRANSLATION */;
  637. break;
  638. }
  639. case 'rotation': {
  640. dataAccessorType = "VEC4" /* VEC4 */;
  641. animationChannelTargetPath = "rotation" /* ROTATION */;
  642. break;
  643. }
  644. case 'rotationQuaternion': {
  645. dataAccessorType = "VEC4" /* VEC4 */;
  646. useQuaternion = true;
  647. animationChannelTargetPath = "rotation" /* ROTATION */;
  648. break;
  649. }
  650. default: {
  651. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("Unsupported animatable property " + property[0]);
  652. }
  653. }
  654. if (animationChannelTargetPath) {
  655. return { animationChannelTargetPath: animationChannelTargetPath, dataAccessorType: dataAccessorType, useQuaternion: useQuaternion };
  656. }
  657. else {
  658. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error('animation channel target path and data accessor type could be deduced');
  659. }
  660. return null;
  661. };
  662. /**
  663. * @ignore
  664. * Create node animations from the transform node animations
  665. * @param babylonNode
  666. * @param runtimeGLTFAnimation
  667. * @param idleGLTFAnimations
  668. * @param nodeMap
  669. * @param nodes
  670. * @param binaryWriter
  671. * @param bufferViews
  672. * @param accessors
  673. * @param convertToRightHandedSystem
  674. */
  675. _GLTFAnimation._CreateNodeAnimationFromNodeAnimations = function (babylonNode, runtimeGLTFAnimation, idleGLTFAnimations, nodeMap, nodes, binaryWriter, bufferViews, accessors, convertToRightHandedSystem, animationSampleRate) {
  676. var glTFAnimation;
  677. if (babylonNode instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["TransformNode"]) {
  678. if (babylonNode.animations) {
  679. for (var _i = 0, _a = babylonNode.animations; _i < _a.length; _i++) {
  680. var animation = _a[_i];
  681. var animationInfo = _GLTFAnimation._DeduceAnimationInfo(animation);
  682. if (animationInfo) {
  683. glTFAnimation = {
  684. name: animation.name,
  685. samplers: [],
  686. channels: []
  687. };
  688. _GLTFAnimation.AddAnimation("" + animation.name, animation.hasRunningRuntimeAnimations ? runtimeGLTFAnimation : glTFAnimation, babylonNode, animation, animationInfo.dataAccessorType, animationInfo.animationChannelTargetPath, nodeMap, binaryWriter, bufferViews, accessors, convertToRightHandedSystem, animationInfo.useQuaternion, animationSampleRate);
  689. if (glTFAnimation.samplers.length && glTFAnimation.channels.length) {
  690. idleGLTFAnimations.push(glTFAnimation);
  691. }
  692. }
  693. }
  694. }
  695. }
  696. };
  697. /**
  698. * @ignore
  699. * Create node animations from the animation groups
  700. * @param babylonScene
  701. * @param glTFAnimations
  702. * @param nodeMap
  703. * @param nodes
  704. * @param binaryWriter
  705. * @param bufferViews
  706. * @param accessors
  707. * @param convertToRightHandedSystem
  708. */
  709. _GLTFAnimation._CreateNodeAnimationFromAnimationGroups = function (babylonScene, glTFAnimations, nodeMap, nodes, binaryWriter, bufferViews, accessors, convertToRightHandedSystem, animationSampleRate) {
  710. var glTFAnimation;
  711. if (babylonScene.animationGroups) {
  712. var animationGroups = babylonScene.animationGroups;
  713. for (var _i = 0, animationGroups_1 = animationGroups; _i < animationGroups_1.length; _i++) {
  714. var animationGroup = animationGroups_1[_i];
  715. glTFAnimation = {
  716. name: animationGroup.name,
  717. channels: [],
  718. samplers: []
  719. };
  720. for (var _a = 0, _b = animationGroup.targetedAnimations; _a < _b.length; _a++) {
  721. var targetAnimation = _b[_a];
  722. var target = targetAnimation.target;
  723. var animation = targetAnimation.animation;
  724. if (target instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Mesh"] || target.length === 1 && target[0] instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Mesh"]) { // TODO: Update to support bones
  725. var animationInfo = _GLTFAnimation._DeduceAnimationInfo(targetAnimation.animation);
  726. if (animationInfo) {
  727. var babylonMesh = target instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Mesh"] ? target : target[0];
  728. _GLTFAnimation.AddAnimation("" + animation.name, glTFAnimation, babylonMesh, animation, animationInfo.dataAccessorType, animationInfo.animationChannelTargetPath, nodeMap, binaryWriter, bufferViews, accessors, convertToRightHandedSystem, animationInfo.useQuaternion, animationSampleRate);
  729. }
  730. }
  731. }
  732. if (glTFAnimation.channels.length && glTFAnimation.samplers.length) {
  733. glTFAnimations.push(glTFAnimation);
  734. }
  735. }
  736. }
  737. };
  738. _GLTFAnimation.AddAnimation = function (name, glTFAnimation, babylonTransformNode, animation, dataAccessorType, animationChannelTargetPath, nodeMap, binaryWriter, bufferViews, accessors, convertToRightHandedSystem, useQuaternion, animationSampleRate) {
  739. var animationData = _GLTFAnimation._CreateNodeAnimation(babylonTransformNode, animation, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion, animationSampleRate);
  740. var bufferView;
  741. var accessor;
  742. var keyframeAccessorIndex;
  743. var dataAccessorIndex;
  744. var outputLength;
  745. var animationSampler;
  746. var animationChannel;
  747. if (animationData) {
  748. var nodeIndex = nodeMap[babylonTransformNode.uniqueId];
  749. // Creates buffer view and accessor for key frames.
  750. var byteLength = animationData.inputs.length * 4;
  751. bufferView = _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, name + " keyframe data view");
  752. bufferViews.push(bufferView);
  753. animationData.inputs.forEach(function (input) {
  754. binaryWriter.setFloat32(input);
  755. });
  756. accessor = _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._CreateAccessor(bufferViews.length - 1, name + " keyframes", "SCALAR" /* SCALAR */, 5126 /* FLOAT */, animationData.inputs.length, null, [animationData.inputsMin], [animationData.inputsMax]);
  757. accessors.push(accessor);
  758. keyframeAccessorIndex = accessors.length - 1;
  759. // create bufferview and accessor for keyed values.
  760. outputLength = animationData.outputs.length;
  761. byteLength = dataAccessorType === "VEC3" /* VEC3 */ ? animationData.outputs.length * 12 : animationData.outputs.length * 16;
  762. // check for in and out tangents
  763. bufferView = _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, name + " data view");
  764. bufferViews.push(bufferView);
  765. animationData.outputs.forEach(function (output) {
  766. output.forEach(function (entry) {
  767. binaryWriter.setFloat32(entry);
  768. });
  769. });
  770. accessor = _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._CreateAccessor(bufferViews.length - 1, name + " data", dataAccessorType, 5126 /* FLOAT */, outputLength, null, null, null);
  771. accessors.push(accessor);
  772. dataAccessorIndex = accessors.length - 1;
  773. // create sampler
  774. animationSampler = {
  775. interpolation: animationData.samplerInterpolation,
  776. input: keyframeAccessorIndex,
  777. output: dataAccessorIndex
  778. };
  779. glTFAnimation.samplers.push(animationSampler);
  780. // create channel
  781. animationChannel = {
  782. sampler: glTFAnimation.samplers.length - 1,
  783. target: {
  784. node: nodeIndex,
  785. path: animationChannelTargetPath
  786. }
  787. };
  788. glTFAnimation.channels.push(animationChannel);
  789. }
  790. };
  791. /**
  792. * Create a baked animation
  793. * @param babylonTransformNode BabylonJS mesh
  794. * @param animation BabylonJS animation corresponding to the BabylonJS mesh
  795. * @param animationChannelTargetPath animation target channel
  796. * @param minFrame minimum animation frame
  797. * @param maxFrame maximum animation frame
  798. * @param fps frames per second of the animation
  799. * @param inputs input key frames of the animation
  800. * @param outputs output key frame data of the animation
  801. * @param convertToRightHandedSystem converts the values to right-handed
  802. * @param useQuaternion specifies if quaternions should be used
  803. */
  804. _GLTFAnimation._CreateBakedAnimation = function (babylonTransformNode, animation, animationChannelTargetPath, minFrame, maxFrame, fps, sampleRate, inputs, outputs, minMaxFrames, convertToRightHandedSystem, useQuaternion) {
  805. var value;
  806. var quaternionCache = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].Identity();
  807. var previousTime = null;
  808. var time;
  809. var maxUsedFrame = null;
  810. var currKeyFrame = null;
  811. var nextKeyFrame = null;
  812. var prevKeyFrame = null;
  813. var endFrame = null;
  814. minMaxFrames.min = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].FloatRound(minFrame / fps);
  815. var keyFrames = animation.getKeys();
  816. for (var i = 0, length_1 = keyFrames.length; i < length_1; ++i) {
  817. endFrame = null;
  818. currKeyFrame = keyFrames[i];
  819. if (i + 1 < length_1) {
  820. nextKeyFrame = keyFrames[i + 1];
  821. if (currKeyFrame.value.equals(nextKeyFrame.value)) {
  822. if (i === 0) { // set the first frame to itself
  823. endFrame = currKeyFrame.frame;
  824. }
  825. else {
  826. continue;
  827. }
  828. }
  829. else {
  830. endFrame = nextKeyFrame.frame;
  831. }
  832. }
  833. else {
  834. // at the last key frame
  835. prevKeyFrame = keyFrames[i - 1];
  836. if (currKeyFrame.value.equals(prevKeyFrame.value)) {
  837. continue;
  838. }
  839. else {
  840. endFrame = maxFrame;
  841. }
  842. }
  843. if (endFrame) {
  844. for (var f = currKeyFrame.frame; f <= endFrame; f += sampleRate) {
  845. time = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].FloatRound(f / fps);
  846. if (time === previousTime) {
  847. continue;
  848. }
  849. previousTime = time;
  850. maxUsedFrame = time;
  851. var state = {
  852. key: 0,
  853. repeatCount: 0,
  854. loopMode: animation.loopMode
  855. };
  856. value = animation._interpolate(f, state);
  857. _GLTFAnimation._SetInterpolatedValue(babylonTransformNode, value, time, animation, animationChannelTargetPath, quaternionCache, inputs, outputs, convertToRightHandedSystem, useQuaternion);
  858. }
  859. }
  860. }
  861. if (maxUsedFrame) {
  862. minMaxFrames.max = maxUsedFrame;
  863. }
  864. };
  865. _GLTFAnimation._ConvertFactorToVector3OrQuaternion = function (factor, babylonTransformNode, animation, animationType, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion) {
  866. var property;
  867. var componentName;
  868. var value = null;
  869. var basePositionRotationOrScale = _GLTFAnimation._GetBasePositionRotationOrScale(babylonTransformNode, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  870. if (animationType === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Animation"].ANIMATIONTYPE_FLOAT) { // handles single component x, y, z or w component animation by using a base property and animating over a component.
  871. property = animation.targetProperty.split('.');
  872. componentName = property ? property[1] : ''; // x, y, or z component
  873. value = useQuaternion ? babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray(basePositionRotationOrScale).normalize() : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(basePositionRotationOrScale);
  874. switch (componentName) {
  875. case 'x': {
  876. value[componentName] = (convertToRightHandedSystem && useQuaternion && (animationChannelTargetPath !== "scale" /* SCALE */)) ? -factor : factor;
  877. break;
  878. }
  879. case 'y': {
  880. value[componentName] = (convertToRightHandedSystem && useQuaternion && (animationChannelTargetPath !== "scale" /* SCALE */)) ? -factor : factor;
  881. break;
  882. }
  883. case 'z': {
  884. value[componentName] = (convertToRightHandedSystem && !useQuaternion && (animationChannelTargetPath !== "scale" /* SCALE */)) ? -factor : factor;
  885. break;
  886. }
  887. case 'w': {
  888. value.w = factor;
  889. break;
  890. }
  891. default: {
  892. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("glTFAnimation: Unsupported component type \"" + componentName + "\" for scale animation!");
  893. }
  894. }
  895. }
  896. return value;
  897. };
  898. _GLTFAnimation._SetInterpolatedValue = function (babylonTransformNode, value, time, animation, animationChannelTargetPath, quaternionCache, inputs, outputs, convertToRightHandedSystem, useQuaternion) {
  899. var animationType = animation.dataType;
  900. var cacheValue;
  901. inputs.push(time);
  902. if (typeof value === "number") {
  903. value = this._ConvertFactorToVector3OrQuaternion(value, babylonTransformNode, animation, animationType, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  904. }
  905. if (value) {
  906. if (animationChannelTargetPath === "rotation" /* ROTATION */) {
  907. if (useQuaternion) {
  908. quaternionCache = value;
  909. }
  910. else {
  911. cacheValue = value;
  912. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].RotationYawPitchRollToRef(cacheValue.y, cacheValue.x, cacheValue.z, quaternionCache);
  913. }
  914. if (convertToRightHandedSystem) {
  915. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedQuaternionFromRef(quaternionCache);
  916. if (!babylonTransformNode.parent) {
  917. quaternionCache = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray([0, 1, 0, 0]).multiply(quaternionCache);
  918. }
  919. }
  920. outputs.push(quaternionCache.asArray());
  921. }
  922. else {
  923. cacheValue = value;
  924. if (convertToRightHandedSystem && (animationChannelTargetPath !== "scale" /* SCALE */)) {
  925. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedPositionVector3FromRef(cacheValue);
  926. if (!babylonTransformNode.parent) {
  927. cacheValue.x *= -1;
  928. cacheValue.z *= -1;
  929. }
  930. }
  931. outputs.push(cacheValue.asArray());
  932. }
  933. }
  934. };
  935. /**
  936. * Creates linear animation from the animation key frames
  937. * @param babylonTransformNode BabylonJS mesh
  938. * @param animation BabylonJS animation
  939. * @param animationChannelTargetPath The target animation channel
  940. * @param frameDelta The difference between the last and first frame of the animation
  941. * @param inputs Array to store the key frame times
  942. * @param outputs Array to store the key frame data
  943. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  944. * @param useQuaternion Specifies if quaternions are used in the animation
  945. */
  946. _GLTFAnimation._CreateLinearOrStepAnimation = function (babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion) {
  947. for (var _i = 0, _a = animation.getKeys(); _i < _a.length; _i++) {
  948. var keyFrame = _a[_i];
  949. inputs.push(keyFrame.frame / animation.framePerSecond); // keyframes in seconds.
  950. _GLTFAnimation._AddKeyframeValue(keyFrame, animation, outputs, animationChannelTargetPath, babylonTransformNode, convertToRightHandedSystem, useQuaternion);
  951. }
  952. };
  953. /**
  954. * Creates cubic spline animation from the animation key frames
  955. * @param babylonTransformNode BabylonJS mesh
  956. * @param animation BabylonJS animation
  957. * @param animationChannelTargetPath The target animation channel
  958. * @param frameDelta The difference between the last and first frame of the animation
  959. * @param inputs Array to store the key frame times
  960. * @param outputs Array to store the key frame data
  961. * @param convertToRightHandedSystem Specifies if the position data should be converted to right handed
  962. * @param useQuaternion Specifies if quaternions are used in the animation
  963. */
  964. _GLTFAnimation._CreateCubicSplineAnimation = function (babylonTransformNode, animation, animationChannelTargetPath, frameDelta, inputs, outputs, convertToRightHandedSystem, useQuaternion) {
  965. animation.getKeys().forEach(function (keyFrame) {
  966. inputs.push(keyFrame.frame / animation.framePerSecond); // keyframes in seconds.
  967. _GLTFAnimation.AddSplineTangent(babylonTransformNode, _TangentType.INTANGENT, outputs, animationChannelTargetPath, "CUBICSPLINE" /* CUBICSPLINE */, keyFrame, frameDelta, useQuaternion, convertToRightHandedSystem);
  968. _GLTFAnimation._AddKeyframeValue(keyFrame, animation, outputs, animationChannelTargetPath, babylonTransformNode, convertToRightHandedSystem, useQuaternion);
  969. _GLTFAnimation.AddSplineTangent(babylonTransformNode, _TangentType.OUTTANGENT, outputs, animationChannelTargetPath, "CUBICSPLINE" /* CUBICSPLINE */, keyFrame, frameDelta, useQuaternion, convertToRightHandedSystem);
  970. });
  971. };
  972. _GLTFAnimation._GetBasePositionRotationOrScale = function (babylonTransformNode, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion) {
  973. var basePositionRotationOrScale;
  974. if (animationChannelTargetPath === "rotation" /* ROTATION */) {
  975. if (useQuaternion) {
  976. if (babylonTransformNode.rotationQuaternion) {
  977. basePositionRotationOrScale = babylonTransformNode.rotationQuaternion.asArray();
  978. if (convertToRightHandedSystem) {
  979. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedQuaternionArrayFromRef(basePositionRotationOrScale);
  980. if (!babylonTransformNode.parent) {
  981. basePositionRotationOrScale = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray([0, 1, 0, 0]).multiply(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray(basePositionRotationOrScale)).asArray();
  982. }
  983. }
  984. }
  985. else {
  986. basePositionRotationOrScale = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].Identity().asArray();
  987. }
  988. }
  989. else {
  990. basePositionRotationOrScale = babylonTransformNode.rotation.asArray();
  991. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedNormalArray3FromRef(basePositionRotationOrScale);
  992. }
  993. }
  994. else if (animationChannelTargetPath === "translation" /* TRANSLATION */) {
  995. basePositionRotationOrScale = babylonTransformNode.position.asArray();
  996. if (convertToRightHandedSystem) {
  997. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedPositionArray3FromRef(basePositionRotationOrScale);
  998. }
  999. }
  1000. else { // scale
  1001. basePositionRotationOrScale = babylonTransformNode.scaling.asArray();
  1002. }
  1003. return basePositionRotationOrScale;
  1004. };
  1005. /**
  1006. * Adds a key frame value
  1007. * @param keyFrame
  1008. * @param animation
  1009. * @param outputs
  1010. * @param animationChannelTargetPath
  1011. * @param basePositionRotationOrScale
  1012. * @param convertToRightHandedSystem
  1013. * @param useQuaternion
  1014. */
  1015. _GLTFAnimation._AddKeyframeValue = function (keyFrame, animation, outputs, animationChannelTargetPath, babylonTransformNode, convertToRightHandedSystem, useQuaternion) {
  1016. var value;
  1017. var newPositionRotationOrScale;
  1018. var animationType = animation.dataType;
  1019. if (animationType === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Animation"].ANIMATIONTYPE_VECTOR3) {
  1020. value = keyFrame.value.asArray();
  1021. if (animationChannelTargetPath === "rotation" /* ROTATION */) {
  1022. var array = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(value);
  1023. var rotationQuaternion = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].RotationYawPitchRoll(array.y, array.x, array.z);
  1024. if (convertToRightHandedSystem) {
  1025. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedQuaternionFromRef(rotationQuaternion);
  1026. if (!babylonTransformNode.parent) {
  1027. rotationQuaternion = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray([0, 1, 0, 0]).multiply(rotationQuaternion);
  1028. }
  1029. }
  1030. value = rotationQuaternion.asArray();
  1031. }
  1032. else if (animationChannelTargetPath === "translation" /* TRANSLATION */) {
  1033. if (convertToRightHandedSystem) {
  1034. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedNormalArray3FromRef(value);
  1035. if (!babylonTransformNode.parent) {
  1036. value[0] *= -1;
  1037. value[2] *= -1;
  1038. }
  1039. }
  1040. }
  1041. outputs.push(value); // scale vector.
  1042. }
  1043. else if (animationType === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Animation"].ANIMATIONTYPE_FLOAT) { // handles single component x, y, z or w component animation by using a base property and animating over a component.
  1044. newPositionRotationOrScale = this._ConvertFactorToVector3OrQuaternion(keyFrame.value, babylonTransformNode, animation, animationType, animationChannelTargetPath, convertToRightHandedSystem, useQuaternion);
  1045. if (newPositionRotationOrScale) {
  1046. if (animationChannelTargetPath === "rotation" /* ROTATION */) {
  1047. var posRotScale = useQuaternion ? newPositionRotationOrScale : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].RotationYawPitchRoll(newPositionRotationOrScale.y, newPositionRotationOrScale.x, newPositionRotationOrScale.z).normalize();
  1048. if (convertToRightHandedSystem) {
  1049. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedQuaternionFromRef(posRotScale);
  1050. if (!babylonTransformNode.parent) {
  1051. posRotScale = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray([0, 1, 0, 0]).multiply(posRotScale);
  1052. }
  1053. }
  1054. outputs.push(posRotScale.asArray());
  1055. }
  1056. else if (animationChannelTargetPath === "translation" /* TRANSLATION */) {
  1057. if (convertToRightHandedSystem) {
  1058. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedNormalVector3FromRef(newPositionRotationOrScale);
  1059. if (!babylonTransformNode.parent) {
  1060. newPositionRotationOrScale.x *= -1;
  1061. newPositionRotationOrScale.z *= -1;
  1062. }
  1063. }
  1064. }
  1065. outputs.push(newPositionRotationOrScale.asArray());
  1066. }
  1067. }
  1068. else if (animationType === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Animation"].ANIMATIONTYPE_QUATERNION) {
  1069. value = keyFrame.value.normalize().asArray();
  1070. if (convertToRightHandedSystem) {
  1071. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedQuaternionArrayFromRef(value);
  1072. if (!babylonTransformNode.parent) {
  1073. value = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray([0, 1, 0, 0]).multiply(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray(value)).asArray();
  1074. }
  1075. }
  1076. outputs.push(value);
  1077. }
  1078. else {
  1079. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error('glTFAnimation: Unsupported key frame values for animation!');
  1080. }
  1081. };
  1082. /**
  1083. * Determine the interpolation based on the key frames
  1084. * @param keyFrames
  1085. * @param animationChannelTargetPath
  1086. * @param useQuaternion
  1087. */
  1088. _GLTFAnimation._DeduceInterpolation = function (keyFrames, animationChannelTargetPath, useQuaternion) {
  1089. var interpolationType;
  1090. var shouldBakeAnimation = false;
  1091. var key;
  1092. if (animationChannelTargetPath === "rotation" /* ROTATION */ && !useQuaternion) {
  1093. return { interpolationType: "LINEAR" /* LINEAR */, shouldBakeAnimation: true };
  1094. }
  1095. for (var i = 0, length_2 = keyFrames.length; i < length_2; ++i) {
  1096. key = keyFrames[i];
  1097. if (key.inTangent || key.outTangent) {
  1098. if (interpolationType) {
  1099. if (interpolationType !== "CUBICSPLINE" /* CUBICSPLINE */) {
  1100. interpolationType = "LINEAR" /* LINEAR */;
  1101. shouldBakeAnimation = true;
  1102. break;
  1103. }
  1104. }
  1105. else {
  1106. interpolationType = "CUBICSPLINE" /* CUBICSPLINE */;
  1107. }
  1108. }
  1109. else {
  1110. if (interpolationType) {
  1111. if (interpolationType === "CUBICSPLINE" /* CUBICSPLINE */ ||
  1112. (key.interpolation && (key.interpolation === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["AnimationKeyInterpolation"].STEP) && interpolationType !== "STEP" /* STEP */)) {
  1113. interpolationType = "LINEAR" /* LINEAR */;
  1114. shouldBakeAnimation = true;
  1115. break;
  1116. }
  1117. }
  1118. else {
  1119. if (key.interpolation && (key.interpolation === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["AnimationKeyInterpolation"].STEP)) {
  1120. interpolationType = "STEP" /* STEP */;
  1121. }
  1122. else {
  1123. interpolationType = "LINEAR" /* LINEAR */;
  1124. }
  1125. }
  1126. }
  1127. }
  1128. if (!interpolationType) {
  1129. interpolationType = "LINEAR" /* LINEAR */;
  1130. }
  1131. return { interpolationType: interpolationType, shouldBakeAnimation: shouldBakeAnimation };
  1132. };
  1133. /**
  1134. * Adds an input tangent or output tangent to the output data
  1135. * If an input tangent or output tangent is missing, it uses the zero vector or zero quaternion
  1136. * @param tangentType Specifies which type of tangent to handle (inTangent or outTangent)
  1137. * @param outputs The animation data by keyframe
  1138. * @param animationChannelTargetPath The target animation channel
  1139. * @param interpolation The interpolation type
  1140. * @param keyFrame The key frame with the animation data
  1141. * @param frameDelta Time difference between two frames used to scale the tangent by the frame delta
  1142. * @param useQuaternion Specifies if quaternions are used
  1143. * @param convertToRightHandedSystem Specifies if the values should be converted to right-handed
  1144. */
  1145. _GLTFAnimation.AddSplineTangent = function (babylonTransformNode, tangentType, outputs, animationChannelTargetPath, interpolation, keyFrame, frameDelta, useQuaternion, convertToRightHandedSystem) {
  1146. var tangent;
  1147. var tangentValue = tangentType === _TangentType.INTANGENT ? keyFrame.inTangent : keyFrame.outTangent;
  1148. if (interpolation === "CUBICSPLINE" /* CUBICSPLINE */) {
  1149. if (animationChannelTargetPath === "rotation" /* ROTATION */) {
  1150. if (tangentValue) {
  1151. if (useQuaternion) {
  1152. tangent = tangentValue.scale(frameDelta).asArray();
  1153. }
  1154. else {
  1155. var array = tangentValue.scale(frameDelta);
  1156. tangent = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].RotationYawPitchRoll(array.y, array.x, array.z).asArray();
  1157. }
  1158. if (convertToRightHandedSystem) {
  1159. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedQuaternionArrayFromRef(tangent);
  1160. if (!babylonTransformNode.parent) {
  1161. tangent = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray([0, 1, 0, 0]).multiply(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray(tangent)).asArray();
  1162. }
  1163. }
  1164. }
  1165. else {
  1166. tangent = [0, 0, 0, 0];
  1167. }
  1168. }
  1169. else {
  1170. if (tangentValue) {
  1171. tangent = tangentValue.scale(frameDelta).asArray();
  1172. if (convertToRightHandedSystem) {
  1173. if (animationChannelTargetPath === "translation" /* TRANSLATION */) {
  1174. _glTFUtilities__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]._GetRightHandedPositionArray3FromRef(tangent);
  1175. if (!babylonTransformNode.parent) {
  1176. tangent[0] *= -1; // x
  1177. tangent[2] *= -1; // z
  1178. }
  1179. }
  1180. }
  1181. }
  1182. else {
  1183. tangent = [0, 0, 0];
  1184. }
  1185. }
  1186. outputs.push(tangent);
  1187. }
  1188. };
  1189. /**
  1190. * Get the minimum and maximum key frames' frame values
  1191. * @param keyFrames animation key frames
  1192. * @returns the minimum and maximum key frame value
  1193. */
  1194. _GLTFAnimation.calculateMinMaxKeyFrames = function (keyFrames) {
  1195. var min = Infinity;
  1196. var max = -Infinity;
  1197. keyFrames.forEach(function (keyFrame) {
  1198. min = Math.min(min, keyFrame.frame);
  1199. max = Math.max(max, keyFrame.frame);
  1200. });
  1201. return { min: min, max: max };
  1202. };
  1203. return _GLTFAnimation;
  1204. }());
  1205. /***/ }),
  1206. /***/ "./glTF/2.0/glTFData.ts":
  1207. /*!******************************!*\
  1208. !*** ./glTF/2.0/glTFData.ts ***!
  1209. \******************************/
  1210. /*! exports provided: GLTFData */
  1211. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  1212. "use strict";
  1213. __webpack_require__.r(__webpack_exports__);
  1214. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "GLTFData", function() { return GLTFData; });
  1215. /**
  1216. * Class for holding and downloading glTF file data
  1217. */
  1218. var GLTFData = /** @class */ (function () {
  1219. /**
  1220. * Initializes the glTF file object
  1221. */
  1222. function GLTFData() {
  1223. this.glTFFiles = {};
  1224. }
  1225. /**
  1226. * Downloads the glTF data as files based on their names and data
  1227. */
  1228. GLTFData.prototype.downloadFiles = function () {
  1229. /**
  1230. * Checks for a matching suffix at the end of a string (for ES5 and lower)
  1231. * @param str Source string
  1232. * @param suffix Suffix to search for in the source string
  1233. * @returns Boolean indicating whether the suffix was found (true) or not (false)
  1234. */
  1235. function endsWith(str, suffix) {
  1236. return str.indexOf(suffix, str.length - suffix.length) !== -1;
  1237. }
  1238. for (var key in this.glTFFiles) {
  1239. var link = document.createElement('a');
  1240. document.body.appendChild(link);
  1241. link.setAttribute("type", "hidden");
  1242. link.download = key;
  1243. var blob = this.glTFFiles[key];
  1244. var mimeType = void 0;
  1245. if (endsWith(key, ".glb")) {
  1246. mimeType = { type: "model/gltf-binary" };
  1247. }
  1248. else if (endsWith(key, ".bin")) {
  1249. mimeType = { type: "application/octet-stream" };
  1250. }
  1251. else if (endsWith(key, ".gltf")) {
  1252. mimeType = { type: "model/gltf+json" };
  1253. }
  1254. else if (endsWith(key, ".jpeg" || false)) {
  1255. mimeType = { type: "image/jpeg" /* JPEG */ };
  1256. }
  1257. else if (endsWith(key, ".png")) {
  1258. mimeType = { type: "image/png" /* PNG */ };
  1259. }
  1260. link.href = window.URL.createObjectURL(new Blob([blob], mimeType));
  1261. link.click();
  1262. }
  1263. };
  1264. return GLTFData;
  1265. }());
  1266. /***/ }),
  1267. /***/ "./glTF/2.0/glTFExporter.ts":
  1268. /*!**********************************!*\
  1269. !*** ./glTF/2.0/glTFExporter.ts ***!
  1270. \**********************************/
  1271. /*! exports provided: _Exporter, _BinaryWriter */
  1272. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  1273. "use strict";
  1274. __webpack_require__.r(__webpack_exports__);
  1275. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "_Exporter", function() { return _Exporter; });
  1276. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "_BinaryWriter", function() { return _BinaryWriter; });
  1277. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Maths/math */ "babylonjs/Maths/math");
  1278. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__);
  1279. /* harmony import */ var _glTFMaterialExporter__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./glTFMaterialExporter */ "./glTF/2.0/glTFMaterialExporter.ts");
  1280. /* harmony import */ var _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./glTFUtilities */ "./glTF/2.0/glTFUtilities.ts");
  1281. /* harmony import */ var _glTFData__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./glTFData */ "./glTF/2.0/glTFData.ts");
  1282. /* harmony import */ var _glTFAnimation__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./glTFAnimation */ "./glTF/2.0/glTFAnimation.ts");
  1283. /**
  1284. * Converts Babylon Scene into glTF 2.0.
  1285. * @hidden
  1286. */
  1287. var _Exporter = /** @class */ (function () {
  1288. /**
  1289. * Creates a glTF Exporter instance, which can accept optional exporter options
  1290. * @param babylonScene Babylon scene object
  1291. * @param options Options to modify the behavior of the exporter
  1292. */
  1293. function _Exporter(babylonScene, options) {
  1294. this._extensions = {};
  1295. this._glTF = {
  1296. asset: { generator: "BabylonJS", version: "2.0" }
  1297. };
  1298. this._babylonScene = babylonScene;
  1299. this._bufferViews = [];
  1300. this._accessors = [];
  1301. this._meshes = [];
  1302. this._scenes = [];
  1303. this._nodes = [];
  1304. this._images = [];
  1305. this._materials = [];
  1306. this._materialMap = [];
  1307. this._textures = [];
  1308. this._samplers = [];
  1309. this._animations = [];
  1310. this._imageData = {};
  1311. this._convertToRightHandedSystem = this._babylonScene.useRightHandedSystem ? false : true;
  1312. var _options = options || {};
  1313. this._shouldExportNode = _options.shouldExportNode ? _options.shouldExportNode : function (babylonNode) { return true; };
  1314. this._animationSampleRate = _options.animationSampleRate ? _options.animationSampleRate : 1 / 60;
  1315. this._glTFMaterialExporter = new _glTFMaterialExporter__WEBPACK_IMPORTED_MODULE_1__["_GLTFMaterialExporter"](this);
  1316. this._loadExtensions();
  1317. }
  1318. _Exporter.prototype._applyExtensions = function (property, actionAsync) {
  1319. for (var _i = 0, _a = _Exporter._ExtensionNames; _i < _a.length; _i++) {
  1320. var name_1 = _a[_i];
  1321. var extension = this._extensions[name_1];
  1322. if (extension.enabled) {
  1323. var exporterProperty = property;
  1324. exporterProperty._activeLoaderExtensions = exporterProperty._activeLoaderExtensions || {};
  1325. var activeLoaderExtensions = exporterProperty._activeLoaderExtensions;
  1326. if (!activeLoaderExtensions[name_1]) {
  1327. activeLoaderExtensions[name_1] = true;
  1328. try {
  1329. var result = actionAsync(extension);
  1330. if (result) {
  1331. return result;
  1332. }
  1333. }
  1334. finally {
  1335. delete activeLoaderExtensions[name_1];
  1336. delete exporterProperty._activeLoaderExtensions;
  1337. }
  1338. }
  1339. }
  1340. }
  1341. return null;
  1342. };
  1343. _Exporter.prototype._extensionsPreExportTextureAsync = function (context, babylonTexture, mimeType) {
  1344. return this._applyExtensions(babylonTexture, function (extension) { return extension.preExportTextureAsync && extension.preExportTextureAsync(context, babylonTexture, mimeType); });
  1345. };
  1346. _Exporter.prototype._extensionsPostExportMeshPrimitiveAsync = function (context, meshPrimitive, babylonSubMesh, binaryWriter) {
  1347. return this._applyExtensions(meshPrimitive, function (extension) { return extension.postExportMeshPrimitiveAsync && extension.postExportMeshPrimitiveAsync(context, meshPrimitive, babylonSubMesh, binaryWriter); });
  1348. };
  1349. _Exporter.prototype._extensionsPostExportNodeAsync = function (context, node, babylonNode) {
  1350. return this._applyExtensions(node, function (extension) { return extension.postExportNodeAsync && extension.postExportNodeAsync(context, node, babylonNode); });
  1351. };
  1352. _Exporter.prototype._forEachExtensions = function (action) {
  1353. for (var _i = 0, _a = _Exporter._ExtensionNames; _i < _a.length; _i++) {
  1354. var name_2 = _a[_i];
  1355. var extension = this._extensions[name_2];
  1356. if (extension.enabled) {
  1357. action(extension);
  1358. }
  1359. }
  1360. };
  1361. _Exporter.prototype._extensionsOnExporting = function () {
  1362. this._forEachExtensions(function (extension) { return extension.onExporting && extension.onExporting(); });
  1363. };
  1364. /**
  1365. * Load glTF serializer extensions
  1366. */
  1367. _Exporter.prototype._loadExtensions = function () {
  1368. for (var _i = 0, _a = _Exporter._ExtensionNames; _i < _a.length; _i++) {
  1369. var name_3 = _a[_i];
  1370. var extension = _Exporter._ExtensionFactories[name_3](this);
  1371. this._extensions[name_3] = extension;
  1372. }
  1373. };
  1374. /**
  1375. * Registers a glTF exporter extension
  1376. * @param name Name of the extension to export
  1377. * @param factory The factory function that creates the exporter extension
  1378. */
  1379. _Exporter.RegisterExtension = function (name, factory) {
  1380. if (_Exporter.UnregisterExtension(name)) {
  1381. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("Extension with the name " + name + " already exists");
  1382. }
  1383. _Exporter._ExtensionFactories[name] = factory;
  1384. _Exporter._ExtensionNames.push(name);
  1385. };
  1386. /**
  1387. * Un-registers an exporter extension
  1388. * @param name The name fo the exporter extension
  1389. * @returns A boolean indicating whether the extension has been un-registered
  1390. */
  1391. _Exporter.UnregisterExtension = function (name) {
  1392. if (!_Exporter._ExtensionFactories[name]) {
  1393. return false;
  1394. }
  1395. delete _Exporter._ExtensionFactories[name];
  1396. var index = _Exporter._ExtensionNames.indexOf(name);
  1397. if (index !== -1) {
  1398. _Exporter._ExtensionNames.splice(index, 1);
  1399. }
  1400. return true;
  1401. };
  1402. /**
  1403. * Lazy load a local engine with premultiplied alpha set to false
  1404. */
  1405. _Exporter.prototype._getLocalEngine = function () {
  1406. if (!this._localEngine) {
  1407. var localCanvas = document.createElement('canvas');
  1408. localCanvas.id = "WriteCanvas";
  1409. localCanvas.width = 2048;
  1410. localCanvas.height = 2048;
  1411. this._localEngine = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Engine"](localCanvas, true, { premultipliedAlpha: false, preserveDrawingBuffer: true });
  1412. this._localEngine.setViewport(new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Viewport"](0, 0, 1, 1));
  1413. }
  1414. return this._localEngine;
  1415. };
  1416. _Exporter.prototype.reorderIndicesBasedOnPrimitiveMode = function (submesh, primitiveMode, babylonIndices, byteOffset, binaryWriter) {
  1417. switch (primitiveMode) {
  1418. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleFillMode: {
  1419. if (!byteOffset) {
  1420. byteOffset = 0;
  1421. }
  1422. for (var i = submesh.indexStart, length_1 = submesh.indexStart + submesh.indexCount; i < length_1; i = i + 3) {
  1423. var index = byteOffset + i * 4;
  1424. // swap the second and third indices
  1425. var secondIndex = binaryWriter.getUInt32(index + 4);
  1426. var thirdIndex = binaryWriter.getUInt32(index + 8);
  1427. binaryWriter.setUInt32(thirdIndex, index + 4);
  1428. binaryWriter.setUInt32(secondIndex, index + 8);
  1429. }
  1430. break;
  1431. }
  1432. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleFanDrawMode: {
  1433. for (var i = submesh.indexStart + submesh.indexCount - 1, start = submesh.indexStart; i >= start; --i) {
  1434. binaryWriter.setUInt32(babylonIndices[i], byteOffset);
  1435. byteOffset += 4;
  1436. }
  1437. break;
  1438. }
  1439. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleStripDrawMode: {
  1440. if (submesh.indexCount >= 3) {
  1441. binaryWriter.setUInt32(babylonIndices[submesh.indexStart + 2], byteOffset + 4);
  1442. binaryWriter.setUInt32(babylonIndices[submesh.indexStart + 1], byteOffset + 8);
  1443. }
  1444. break;
  1445. }
  1446. }
  1447. };
  1448. /**
  1449. * Reorders the vertex attribute data based on the primitive mode. This is necessary when indices are not available and the winding order is
  1450. * clock-wise during export to glTF
  1451. * @param submesh BabylonJS submesh
  1452. * @param primitiveMode Primitive mode of the mesh
  1453. * @param sideOrientation the winding order of the submesh
  1454. * @param vertexBufferKind The type of vertex attribute
  1455. * @param meshAttributeArray The vertex attribute data
  1456. * @param byteOffset The offset to the binary data
  1457. * @param binaryWriter The binary data for the glTF file
  1458. */
  1459. _Exporter.prototype.reorderVertexAttributeDataBasedOnPrimitiveMode = function (submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter) {
  1460. if (this._convertToRightHandedSystem && sideOrientation === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].ClockWiseSideOrientation) {
  1461. switch (primitiveMode) {
  1462. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleFillMode: {
  1463. this.reorderTriangleFillMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  1464. break;
  1465. }
  1466. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleStripDrawMode: {
  1467. this.reorderTriangleStripDrawMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  1468. break;
  1469. }
  1470. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleFanDrawMode: {
  1471. this.reorderTriangleFanMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter);
  1472. break;
  1473. }
  1474. }
  1475. }
  1476. };
  1477. /**
  1478. * Reorders the vertex attributes in the correct triangle mode order . This is necessary when indices are not available and the winding order is
  1479. * clock-wise during export to glTF
  1480. * @param submesh BabylonJS submesh
  1481. * @param primitiveMode Primitive mode of the mesh
  1482. * @param sideOrientation the winding order of the submesh
  1483. * @param vertexBufferKind The type of vertex attribute
  1484. * @param meshAttributeArray The vertex attribute data
  1485. * @param byteOffset The offset to the binary data
  1486. * @param binaryWriter The binary data for the glTF file
  1487. */
  1488. _Exporter.prototype.reorderTriangleFillMode = function (submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter) {
  1489. var vertexBuffer = this.getVertexBufferFromMesh(vertexBufferKind, submesh.getMesh());
  1490. if (vertexBuffer) {
  1491. var stride = vertexBuffer.byteStride / babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].GetTypeByteLength(vertexBuffer.type);
  1492. if (submesh.verticesCount % 3 !== 0) {
  1493. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error('The submesh vertices for the triangle fill mode is not divisible by 3!');
  1494. }
  1495. else {
  1496. var vertexData = [];
  1497. var index = 0;
  1498. switch (vertexBufferKind) {
  1499. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind:
  1500. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind: {
  1501. for (var x = submesh.verticesStart; x < submesh.verticesStart + submesh.verticesCount; x = x + 3) {
  1502. index = x * stride;
  1503. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index));
  1504. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index + 2 * stride));
  1505. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index + stride));
  1506. }
  1507. break;
  1508. }
  1509. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].TangentKind: {
  1510. for (var x = submesh.verticesStart; x < submesh.verticesStart + submesh.verticesCount; x = x + 3) {
  1511. index = x * stride;
  1512. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index));
  1513. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index + 2 * stride));
  1514. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index + stride));
  1515. }
  1516. break;
  1517. }
  1518. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].ColorKind: {
  1519. var size = vertexBuffer.getSize();
  1520. for (var x = submesh.verticesStart; x < submesh.verticesStart + submesh.verticesCount; x = x + size) {
  1521. index = x * stride;
  1522. if (size === 4) {
  1523. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index));
  1524. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index + 2 * stride));
  1525. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index + stride));
  1526. }
  1527. else {
  1528. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index));
  1529. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index + 2 * stride));
  1530. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index + stride));
  1531. }
  1532. }
  1533. break;
  1534. }
  1535. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UVKind:
  1536. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UV2Kind: {
  1537. for (var x = submesh.verticesStart; x < submesh.verticesStart + submesh.verticesCount; x = x + 3) {
  1538. index = x * stride;
  1539. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"].FromArray(meshAttributeArray, index));
  1540. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"].FromArray(meshAttributeArray, index + 2 * stride));
  1541. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"].FromArray(meshAttributeArray, index + stride));
  1542. }
  1543. break;
  1544. }
  1545. default: {
  1546. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("Unsupported Vertex Buffer type: " + vertexBufferKind);
  1547. }
  1548. }
  1549. this.writeVertexAttributeData(vertexData, byteOffset, vertexBufferKind, meshAttributeArray, binaryWriter);
  1550. }
  1551. }
  1552. else {
  1553. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("reorderTriangleFillMode: Vertex Buffer Kind " + vertexBufferKind + " not present!");
  1554. }
  1555. };
  1556. /**
  1557. * Reorders the vertex attributes in the correct triangle strip order. This is necessary when indices are not available and the winding order is
  1558. * clock-wise during export to glTF
  1559. * @param submesh BabylonJS submesh
  1560. * @param primitiveMode Primitive mode of the mesh
  1561. * @param sideOrientation the winding order of the submesh
  1562. * @param vertexBufferKind The type of vertex attribute
  1563. * @param meshAttributeArray The vertex attribute data
  1564. * @param byteOffset The offset to the binary data
  1565. * @param binaryWriter The binary data for the glTF file
  1566. */
  1567. _Exporter.prototype.reorderTriangleStripDrawMode = function (submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter) {
  1568. var vertexBuffer = this.getVertexBufferFromMesh(vertexBufferKind, submesh.getMesh());
  1569. if (vertexBuffer) {
  1570. var stride = vertexBuffer.byteStride / babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].GetTypeByteLength(vertexBuffer.type);
  1571. var vertexData = [];
  1572. var index = 0;
  1573. switch (vertexBufferKind) {
  1574. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind:
  1575. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind: {
  1576. index = submesh.verticesStart;
  1577. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index + 2 * stride));
  1578. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index + stride));
  1579. break;
  1580. }
  1581. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].TangentKind: {
  1582. for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
  1583. index = x * stride;
  1584. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index));
  1585. }
  1586. break;
  1587. }
  1588. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].ColorKind: {
  1589. for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
  1590. index = x * stride;
  1591. vertexBuffer.getSize() === 4 ? vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index)) : vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index));
  1592. }
  1593. break;
  1594. }
  1595. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UVKind:
  1596. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UV2Kind: {
  1597. for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
  1598. index = x * stride;
  1599. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"].FromArray(meshAttributeArray, index));
  1600. }
  1601. break;
  1602. }
  1603. default: {
  1604. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("Unsupported Vertex Buffer type: " + vertexBufferKind);
  1605. }
  1606. }
  1607. this.writeVertexAttributeData(vertexData, byteOffset + 12, vertexBufferKind, meshAttributeArray, binaryWriter);
  1608. }
  1609. else {
  1610. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("reorderTriangleStripDrawMode: Vertex buffer kind " + vertexBufferKind + " not present!");
  1611. }
  1612. };
  1613. /**
  1614. * Reorders the vertex attributes in the correct triangle fan order. This is necessary when indices are not available and the winding order is
  1615. * clock-wise during export to glTF
  1616. * @param submesh BabylonJS submesh
  1617. * @param primitiveMode Primitive mode of the mesh
  1618. * @param sideOrientation the winding order of the submesh
  1619. * @param vertexBufferKind The type of vertex attribute
  1620. * @param meshAttributeArray The vertex attribute data
  1621. * @param byteOffset The offset to the binary data
  1622. * @param binaryWriter The binary data for the glTF file
  1623. */
  1624. _Exporter.prototype.reorderTriangleFanMode = function (submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter) {
  1625. var vertexBuffer = this.getVertexBufferFromMesh(vertexBufferKind, submesh.getMesh());
  1626. if (vertexBuffer) {
  1627. var stride = vertexBuffer.byteStride / babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].GetTypeByteLength(vertexBuffer.type);
  1628. var vertexData = [];
  1629. var index = 0;
  1630. switch (vertexBufferKind) {
  1631. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind:
  1632. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind: {
  1633. for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
  1634. index = x * stride;
  1635. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index));
  1636. }
  1637. break;
  1638. }
  1639. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].TangentKind: {
  1640. for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
  1641. index = x * stride;
  1642. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index));
  1643. }
  1644. break;
  1645. }
  1646. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].ColorKind: {
  1647. for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
  1648. index = x * stride;
  1649. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index));
  1650. vertexBuffer.getSize() === 4 ? vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index)) : vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index));
  1651. }
  1652. break;
  1653. }
  1654. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UVKind:
  1655. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UV2Kind: {
  1656. for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
  1657. index = x * stride;
  1658. vertexData.push(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"].FromArray(meshAttributeArray, index));
  1659. }
  1660. break;
  1661. }
  1662. default: {
  1663. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("Unsupported Vertex Buffer type: " + vertexBufferKind);
  1664. }
  1665. }
  1666. this.writeVertexAttributeData(vertexData, byteOffset, vertexBufferKind, meshAttributeArray, binaryWriter);
  1667. }
  1668. else {
  1669. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("reorderTriangleFanMode: Vertex buffer kind " + vertexBufferKind + " not present!");
  1670. }
  1671. };
  1672. /**
  1673. * Writes the vertex attribute data to binary
  1674. * @param vertices The vertices to write to the binary writer
  1675. * @param byteOffset The offset into the binary writer to overwrite binary data
  1676. * @param vertexAttributeKind The vertex attribute type
  1677. * @param meshAttributeArray The vertex attribute data
  1678. * @param binaryWriter The writer containing the binary data
  1679. */
  1680. _Exporter.prototype.writeVertexAttributeData = function (vertices, byteOffset, vertexAttributeKind, meshAttributeArray, binaryWriter) {
  1681. for (var _i = 0, vertices_1 = vertices; _i < vertices_1.length; _i++) {
  1682. var vertex = vertices_1[_i];
  1683. if (this._convertToRightHandedSystem && !(vertexAttributeKind === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].ColorKind) && !(vertex instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"])) {
  1684. if (vertex instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"]) {
  1685. if (vertexAttributeKind === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind) {
  1686. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedNormalVector3FromRef(vertex);
  1687. }
  1688. else if (vertexAttributeKind === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind) {
  1689. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedPositionVector3FromRef(vertex);
  1690. }
  1691. else {
  1692. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error('Unsupported vertex attribute kind!');
  1693. }
  1694. }
  1695. else {
  1696. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedVector4FromRef(vertex);
  1697. }
  1698. }
  1699. if (vertexAttributeKind === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind) {
  1700. vertex.normalize();
  1701. }
  1702. else if (vertexAttributeKind === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].TangentKind && vertex instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"]) {
  1703. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._NormalizeTangentFromRef(vertex);
  1704. }
  1705. for (var _a = 0, _b = vertex.asArray(); _a < _b.length; _a++) {
  1706. var component = _b[_a];
  1707. binaryWriter.setFloat32(component, byteOffset);
  1708. byteOffset += 4;
  1709. }
  1710. }
  1711. };
  1712. /**
  1713. * Writes mesh attribute data to a data buffer
  1714. * Returns the bytelength of the data
  1715. * @param vertexBufferKind Indicates what kind of vertex data is being passed in
  1716. * @param meshAttributeArray Array containing the attribute data
  1717. * @param binaryWriter The buffer to write the binary data to
  1718. * @param indices Used to specify the order of the vertex data
  1719. */
  1720. _Exporter.prototype.writeAttributeData = function (vertexBufferKind, meshAttributeArray, byteStride, binaryWriter) {
  1721. var stride = byteStride / 4;
  1722. var vertexAttributes = [];
  1723. var index;
  1724. switch (vertexBufferKind) {
  1725. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind: {
  1726. for (var k = 0, length_2 = meshAttributeArray.length / stride; k < length_2; ++k) {
  1727. index = k * stride;
  1728. var vertexData = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index);
  1729. if (this._convertToRightHandedSystem) {
  1730. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedPositionVector3FromRef(vertexData);
  1731. }
  1732. vertexAttributes.push(vertexData.asArray());
  1733. }
  1734. break;
  1735. }
  1736. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind: {
  1737. for (var k = 0, length_3 = meshAttributeArray.length / stride; k < length_3; ++k) {
  1738. index = k * stride;
  1739. var vertexData = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index);
  1740. if (this._convertToRightHandedSystem) {
  1741. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedNormalVector3FromRef(vertexData);
  1742. }
  1743. vertexData.normalize();
  1744. vertexAttributes.push(vertexData.asArray());
  1745. }
  1746. break;
  1747. }
  1748. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].TangentKind: {
  1749. for (var k = 0, length_4 = meshAttributeArray.length / stride; k < length_4; ++k) {
  1750. index = k * stride;
  1751. var vertexData = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index);
  1752. if (this._convertToRightHandedSystem) {
  1753. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedVector4FromRef(vertexData);
  1754. }
  1755. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._NormalizeTangentFromRef(vertexData);
  1756. vertexAttributes.push(vertexData.asArray());
  1757. }
  1758. break;
  1759. }
  1760. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].ColorKind: {
  1761. for (var k = 0, length_5 = meshAttributeArray.length / stride; k < length_5; ++k) {
  1762. index = k * stride;
  1763. var vertexData = stride === 3 ? babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(meshAttributeArray, index) : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector4"].FromArray(meshAttributeArray, index);
  1764. vertexAttributes.push(vertexData.asArray());
  1765. }
  1766. break;
  1767. }
  1768. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UVKind:
  1769. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UV2Kind: {
  1770. for (var k = 0, length_6 = meshAttributeArray.length / stride; k < length_6; ++k) {
  1771. index = k * stride;
  1772. vertexAttributes.push(this._convertToRightHandedSystem ? [meshAttributeArray[index], meshAttributeArray[index + 1]] : [meshAttributeArray[index], meshAttributeArray[index + 1]]);
  1773. }
  1774. break;
  1775. }
  1776. default: {
  1777. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("Unsupported Vertex Buffer Type: " + vertexBufferKind);
  1778. vertexAttributes = [];
  1779. }
  1780. }
  1781. for (var _i = 0, vertexAttributes_1 = vertexAttributes; _i < vertexAttributes_1.length; _i++) {
  1782. var vertexAttribute = vertexAttributes_1[_i];
  1783. for (var _a = 0, vertexAttribute_1 = vertexAttribute; _a < vertexAttribute_1.length; _a++) {
  1784. var component = vertexAttribute_1[_a];
  1785. binaryWriter.setFloat32(component);
  1786. }
  1787. }
  1788. };
  1789. /**
  1790. * Generates glTF json data
  1791. * @param shouldUseGlb Indicates whether the json should be written for a glb file
  1792. * @param glTFPrefix Text to use when prefixing a glTF file
  1793. * @param prettyPrint Indicates whether the json file should be pretty printed (true) or not (false)
  1794. * @returns json data as string
  1795. */
  1796. _Exporter.prototype.generateJSON = function (shouldUseGlb, glTFPrefix, prettyPrint) {
  1797. var _this = this;
  1798. var buffer = { byteLength: this._totalByteLength };
  1799. var imageName;
  1800. var imageData;
  1801. var bufferView;
  1802. var byteOffset = this._totalByteLength;
  1803. if (buffer.byteLength) {
  1804. this._glTF.buffers = [buffer];
  1805. }
  1806. if (this._nodes && this._nodes.length) {
  1807. this._glTF.nodes = this._nodes;
  1808. }
  1809. if (this._meshes && this._meshes.length) {
  1810. this._glTF.meshes = this._meshes;
  1811. }
  1812. if (this._scenes && this._scenes.length) {
  1813. this._glTF.scenes = this._scenes;
  1814. this._glTF.scene = 0;
  1815. }
  1816. if (this._bufferViews && this._bufferViews.length) {
  1817. this._glTF.bufferViews = this._bufferViews;
  1818. }
  1819. if (this._accessors && this._accessors.length) {
  1820. this._glTF.accessors = this._accessors;
  1821. }
  1822. if (this._animations && this._animations.length) {
  1823. this._glTF.animations = this._animations;
  1824. }
  1825. if (this._materials && this._materials.length) {
  1826. this._glTF.materials = this._materials;
  1827. }
  1828. if (this._textures && this._textures.length) {
  1829. this._glTF.textures = this._textures;
  1830. }
  1831. if (this._samplers && this._samplers.length) {
  1832. this._glTF.samplers = this._samplers;
  1833. }
  1834. if (this._images && this._images.length) {
  1835. if (!shouldUseGlb) {
  1836. this._glTF.images = this._images;
  1837. }
  1838. else {
  1839. this._glTF.images = [];
  1840. this._images.forEach(function (image) {
  1841. if (image.uri) {
  1842. imageData = _this._imageData[image.uri];
  1843. imageName = image.uri.split('.')[0] + " image";
  1844. bufferView = _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._CreateBufferView(0, byteOffset, imageData.data.length, undefined, imageName);
  1845. byteOffset += imageData.data.buffer.byteLength;
  1846. _this._bufferViews.push(bufferView);
  1847. image.bufferView = _this._bufferViews.length - 1;
  1848. image.name = imageName;
  1849. image.mimeType = imageData.mimeType;
  1850. image.uri = undefined;
  1851. if (!_this._glTF.images) {
  1852. _this._glTF.images = [];
  1853. }
  1854. _this._glTF.images.push(image);
  1855. }
  1856. });
  1857. // Replace uri with bufferview and mime type for glb
  1858. buffer.byteLength = byteOffset;
  1859. }
  1860. }
  1861. if (!shouldUseGlb) {
  1862. buffer.uri = glTFPrefix + ".bin";
  1863. }
  1864. var jsonText = prettyPrint ? JSON.stringify(this._glTF, null, 2) : JSON.stringify(this._glTF);
  1865. return jsonText;
  1866. };
  1867. /**
  1868. * Generates data for .gltf and .bin files based on the glTF prefix string
  1869. * @param glTFPrefix Text to use when prefixing a glTF file
  1870. * @returns GLTFData with glTF file data
  1871. */
  1872. _Exporter.prototype._generateGLTFAsync = function (glTFPrefix) {
  1873. var _this = this;
  1874. return this._generateBinaryAsync().then(function (binaryBuffer) {
  1875. _this._extensionsOnExporting();
  1876. var jsonText = _this.generateJSON(false, glTFPrefix, true);
  1877. var bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
  1878. var glTFFileName = glTFPrefix + '.gltf';
  1879. var glTFBinFile = glTFPrefix + '.bin';
  1880. var container = new _glTFData__WEBPACK_IMPORTED_MODULE_3__["GLTFData"]();
  1881. container.glTFFiles[glTFFileName] = jsonText;
  1882. container.glTFFiles[glTFBinFile] = bin;
  1883. if (_this._imageData) {
  1884. for (var image in _this._imageData) {
  1885. container.glTFFiles[image] = new Blob([_this._imageData[image].data], { type: _this._imageData[image].mimeType });
  1886. }
  1887. }
  1888. return container;
  1889. });
  1890. };
  1891. /**
  1892. * Creates a binary buffer for glTF
  1893. * @returns array buffer for binary data
  1894. */
  1895. _Exporter.prototype._generateBinaryAsync = function () {
  1896. var _this = this;
  1897. var binaryWriter = new _BinaryWriter(4);
  1898. return this.createSceneAsync(this._babylonScene, binaryWriter).then(function () {
  1899. if (_this._localEngine) {
  1900. _this._localEngine.dispose();
  1901. }
  1902. return binaryWriter.getArrayBuffer();
  1903. });
  1904. };
  1905. /**
  1906. * Pads the number to a multiple of 4
  1907. * @param num number to pad
  1908. * @returns padded number
  1909. */
  1910. _Exporter.prototype._getPadding = function (num) {
  1911. var remainder = num % 4;
  1912. var padding = remainder === 0 ? remainder : 4 - remainder;
  1913. return padding;
  1914. };
  1915. /**
  1916. * Generates a glb file from the json and binary data
  1917. * Returns an object with the glb file name as the key and data as the value
  1918. * @param glTFPrefix
  1919. * @returns object with glb filename as key and data as value
  1920. */
  1921. _Exporter.prototype._generateGLBAsync = function (glTFPrefix) {
  1922. var _this = this;
  1923. return this._generateBinaryAsync().then(function (binaryBuffer) {
  1924. _this._extensionsOnExporting();
  1925. var jsonText = _this.generateJSON(true);
  1926. var glbFileName = glTFPrefix + '.glb';
  1927. var headerLength = 12;
  1928. var chunkLengthPrefix = 8;
  1929. var jsonLength = jsonText.length;
  1930. var imageByteLength = 0;
  1931. for (var key in _this._imageData) {
  1932. imageByteLength += _this._imageData[key].data.byteLength;
  1933. }
  1934. var jsonPadding = _this._getPadding(jsonLength);
  1935. var binPadding = _this._getPadding(binaryBuffer.byteLength);
  1936. var imagePadding = _this._getPadding(imageByteLength);
  1937. var byteLength = headerLength + (2 * chunkLengthPrefix) + jsonLength + jsonPadding + binaryBuffer.byteLength + binPadding + imageByteLength + imagePadding;
  1938. //header
  1939. var headerBuffer = new ArrayBuffer(headerLength);
  1940. var headerBufferView = new DataView(headerBuffer);
  1941. headerBufferView.setUint32(0, 0x46546C67, true); //glTF
  1942. headerBufferView.setUint32(4, 2, true); // version
  1943. headerBufferView.setUint32(8, byteLength, true); // total bytes in file
  1944. //json chunk
  1945. var jsonChunkBuffer = new ArrayBuffer(chunkLengthPrefix + jsonLength + jsonPadding);
  1946. var jsonChunkBufferView = new DataView(jsonChunkBuffer);
  1947. jsonChunkBufferView.setUint32(0, jsonLength + jsonPadding, true);
  1948. jsonChunkBufferView.setUint32(4, 0x4E4F534A, true);
  1949. //json chunk bytes
  1950. var jsonData = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix);
  1951. for (var i = 0; i < jsonLength; ++i) {
  1952. jsonData[i] = jsonText.charCodeAt(i);
  1953. }
  1954. //json padding
  1955. var jsonPaddingView = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix + jsonLength);
  1956. for (var i = 0; i < jsonPadding; ++i) {
  1957. jsonPaddingView[i] = 0x20;
  1958. }
  1959. //binary chunk
  1960. var binaryChunkBuffer = new ArrayBuffer(chunkLengthPrefix);
  1961. var binaryChunkBufferView = new DataView(binaryChunkBuffer);
  1962. binaryChunkBufferView.setUint32(0, binaryBuffer.byteLength + imageByteLength + imagePadding, true);
  1963. binaryChunkBufferView.setUint32(4, 0x004E4942, true);
  1964. // binary padding
  1965. var binPaddingBuffer = new ArrayBuffer(binPadding);
  1966. var binPaddingView = new Uint8Array(binPaddingBuffer);
  1967. for (var i = 0; i < binPadding; ++i) {
  1968. binPaddingView[i] = 0;
  1969. }
  1970. var imagePaddingBuffer = new ArrayBuffer(imagePadding);
  1971. var imagePaddingView = new Uint8Array(imagePaddingBuffer);
  1972. for (var i = 0; i < imagePadding; ++i) {
  1973. imagePaddingView[i] = 0;
  1974. }
  1975. var glbData = [headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer];
  1976. // binary data
  1977. for (var key in _this._imageData) {
  1978. glbData.push(_this._imageData[key].data.buffer);
  1979. }
  1980. glbData.push(binPaddingBuffer);
  1981. glbData.push(imagePaddingBuffer);
  1982. var glbFile = new Blob(glbData, { type: 'application/octet-stream' });
  1983. var container = new _glTFData__WEBPACK_IMPORTED_MODULE_3__["GLTFData"]();
  1984. container.glTFFiles[glbFileName] = glbFile;
  1985. if (_this._localEngine != null) {
  1986. _this._localEngine.dispose();
  1987. }
  1988. return container;
  1989. });
  1990. };
  1991. /**
  1992. * Sets the TRS for each node
  1993. * @param node glTF Node for storing the transformation data
  1994. * @param babylonTransformNode Babylon mesh used as the source for the transformation data
  1995. */
  1996. _Exporter.prototype.setNodeTransformation = function (node, babylonTransformNode) {
  1997. if (!babylonTransformNode.getPivotPoint().equalsToFloats(0, 0, 0)) {
  1998. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("Pivot points are not supported in the glTF serializer");
  1999. }
  2000. if (!babylonTransformNode.position.equalsToFloats(0, 0, 0)) {
  2001. node.translation = this._convertToRightHandedSystem ? _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedPositionVector3(babylonTransformNode.position).asArray() : babylonTransformNode.position.asArray();
  2002. }
  2003. if (!babylonTransformNode.scaling.equalsToFloats(1, 1, 1)) {
  2004. node.scale = babylonTransformNode.scaling.asArray();
  2005. }
  2006. var rotationQuaternion = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].RotationYawPitchRoll(babylonTransformNode.rotation.y, babylonTransformNode.rotation.x, babylonTransformNode.rotation.z);
  2007. if (babylonTransformNode.rotationQuaternion) {
  2008. rotationQuaternion.multiplyInPlace(babylonTransformNode.rotationQuaternion);
  2009. }
  2010. if (!(rotationQuaternion.x === 0 && rotationQuaternion.y === 0 && rotationQuaternion.z === 0 && rotationQuaternion.w === 1)) {
  2011. if (this._convertToRightHandedSystem) {
  2012. _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._GetRightHandedQuaternionFromRef(rotationQuaternion);
  2013. }
  2014. node.rotation = rotationQuaternion.normalize().asArray();
  2015. }
  2016. };
  2017. _Exporter.prototype.getVertexBufferFromMesh = function (attributeKind, bufferMesh) {
  2018. if (bufferMesh.isVerticesDataPresent(attributeKind)) {
  2019. var vertexBuffer = bufferMesh.getVertexBuffer(attributeKind);
  2020. if (vertexBuffer) {
  2021. return vertexBuffer;
  2022. }
  2023. }
  2024. return null;
  2025. };
  2026. /**
  2027. * Creates a bufferview based on the vertices type for the Babylon mesh
  2028. * @param kind Indicates the type of vertices data
  2029. * @param babylonTransformNode The Babylon mesh to get the vertices data from
  2030. * @param binaryWriter The buffer to write the bufferview data to
  2031. */
  2032. _Exporter.prototype.createBufferViewKind = function (kind, babylonTransformNode, binaryWriter, byteStride) {
  2033. var bufferMesh = babylonTransformNode instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Mesh"] ?
  2034. babylonTransformNode : babylonTransformNode instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["InstancedMesh"] ?
  2035. babylonTransformNode.sourceMesh : null;
  2036. if (bufferMesh) {
  2037. var vertexData = bufferMesh.getVerticesData(kind);
  2038. if (vertexData) {
  2039. var byteLength = vertexData.length * 4;
  2040. var bufferView = _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, byteStride, kind + " - " + bufferMesh.name);
  2041. this._bufferViews.push(bufferView);
  2042. this.writeAttributeData(kind, vertexData, byteStride, binaryWriter);
  2043. }
  2044. }
  2045. };
  2046. /**
  2047. * The primitive mode of the Babylon mesh
  2048. * @param babylonMesh The BabylonJS mesh
  2049. */
  2050. _Exporter.prototype.getMeshPrimitiveMode = function (babylonMesh) {
  2051. if (babylonMesh instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["LinesMesh"]) {
  2052. return babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].LineListDrawMode;
  2053. }
  2054. return babylonMesh.material ? babylonMesh.material.fillMode : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleFillMode;
  2055. };
  2056. /**
  2057. * Sets the primitive mode of the glTF mesh primitive
  2058. * @param meshPrimitive glTF mesh primitive
  2059. * @param primitiveMode The primitive mode
  2060. */
  2061. _Exporter.prototype.setPrimitiveMode = function (meshPrimitive, primitiveMode) {
  2062. switch (primitiveMode) {
  2063. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleFillMode: {
  2064. // glTF defaults to using Triangle Mode
  2065. break;
  2066. }
  2067. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleStripDrawMode: {
  2068. meshPrimitive.mode = 5 /* TRIANGLE_STRIP */;
  2069. break;
  2070. }
  2071. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].TriangleFanDrawMode: {
  2072. meshPrimitive.mode = 6 /* TRIANGLE_FAN */;
  2073. break;
  2074. }
  2075. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].PointListDrawMode: {
  2076. meshPrimitive.mode = 0 /* POINTS */;
  2077. }
  2078. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].PointFillMode: {
  2079. meshPrimitive.mode = 0 /* POINTS */;
  2080. break;
  2081. }
  2082. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].LineLoopDrawMode: {
  2083. meshPrimitive.mode = 2 /* LINE_LOOP */;
  2084. break;
  2085. }
  2086. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].LineListDrawMode: {
  2087. meshPrimitive.mode = 1 /* LINES */;
  2088. break;
  2089. }
  2090. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].LineStripDrawMode: {
  2091. meshPrimitive.mode = 3 /* LINE_STRIP */;
  2092. break;
  2093. }
  2094. }
  2095. };
  2096. /**
  2097. * Sets the vertex attribute accessor based of the glTF mesh primitive
  2098. * @param meshPrimitive glTF mesh primitive
  2099. * @param attributeKind vertex attribute
  2100. * @returns boolean specifying if uv coordinates are present
  2101. */
  2102. _Exporter.prototype.setAttributeKind = function (meshPrimitive, attributeKind) {
  2103. switch (attributeKind) {
  2104. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind: {
  2105. meshPrimitive.attributes.POSITION = this._accessors.length - 1;
  2106. break;
  2107. }
  2108. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind: {
  2109. meshPrimitive.attributes.NORMAL = this._accessors.length - 1;
  2110. break;
  2111. }
  2112. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].ColorKind: {
  2113. meshPrimitive.attributes.COLOR_0 = this._accessors.length - 1;
  2114. break;
  2115. }
  2116. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].TangentKind: {
  2117. meshPrimitive.attributes.TANGENT = this._accessors.length - 1;
  2118. break;
  2119. }
  2120. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UVKind: {
  2121. meshPrimitive.attributes.TEXCOORD_0 = this._accessors.length - 1;
  2122. break;
  2123. }
  2124. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UV2Kind: {
  2125. meshPrimitive.attributes.TEXCOORD_1 = this._accessors.length - 1;
  2126. break;
  2127. }
  2128. default: {
  2129. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("Unsupported Vertex Buffer Type: " + attributeKind);
  2130. }
  2131. }
  2132. };
  2133. /**
  2134. * Sets data for the primitive attributes of each submesh
  2135. * @param mesh glTF Mesh object to store the primitive attribute information
  2136. * @param babylonTransformNode Babylon mesh to get the primitive attribute data from
  2137. * @param binaryWriter Buffer to write the attribute data to
  2138. */
  2139. _Exporter.prototype.setPrimitiveAttributesAsync = function (mesh, babylonTransformNode, binaryWriter) {
  2140. var promises = [];
  2141. var bufferMesh = null;
  2142. var bufferView;
  2143. var uvCoordsPresent;
  2144. var minMax;
  2145. if (babylonTransformNode instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Mesh"]) {
  2146. bufferMesh = babylonTransformNode;
  2147. }
  2148. else if (babylonTransformNode instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["InstancedMesh"]) {
  2149. bufferMesh = babylonTransformNode.sourceMesh;
  2150. }
  2151. var attributeData = [
  2152. { kind: babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind, accessorType: "VEC3" /* VEC3 */, byteStride: 12 },
  2153. { kind: babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind, accessorType: "VEC3" /* VEC3 */, byteStride: 12 },
  2154. { kind: babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].ColorKind, accessorType: "VEC4" /* VEC4 */, byteStride: 16 },
  2155. { kind: babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].TangentKind, accessorType: "VEC4" /* VEC4 */, byteStride: 16 },
  2156. { kind: babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UVKind, accessorType: "VEC2" /* VEC2 */, byteStride: 8 },
  2157. { kind: babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UV2Kind, accessorType: "VEC2" /* VEC2 */, byteStride: 8 },
  2158. ];
  2159. if (bufferMesh) {
  2160. var indexBufferViewIndex = null;
  2161. var primitiveMode = this.getMeshPrimitiveMode(bufferMesh);
  2162. var vertexAttributeBufferViews = {};
  2163. // For each BabylonMesh, create bufferviews for each 'kind'
  2164. for (var _i = 0, attributeData_1 = attributeData; _i < attributeData_1.length; _i++) {
  2165. var attribute = attributeData_1[_i];
  2166. var attributeKind = attribute.kind;
  2167. if (bufferMesh.isVerticesDataPresent(attributeKind)) {
  2168. var vertexBuffer = this.getVertexBufferFromMesh(attributeKind, bufferMesh);
  2169. attribute.byteStride = vertexBuffer ? vertexBuffer.getSize() * 4 : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].DeduceStride(attributeKind) * 4;
  2170. if (attribute.byteStride === 12) {
  2171. attribute.accessorType = "VEC3" /* VEC3 */;
  2172. }
  2173. this.createBufferViewKind(attributeKind, babylonTransformNode, binaryWriter, attribute.byteStride);
  2174. attribute.bufferViewIndex = this._bufferViews.length - 1;
  2175. vertexAttributeBufferViews[attributeKind] = attribute.bufferViewIndex;
  2176. }
  2177. }
  2178. if (bufferMesh.getTotalIndices()) {
  2179. var indices = bufferMesh.getIndices();
  2180. if (indices) {
  2181. var byteLength = indices.length * 4;
  2182. bufferView = _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, "Indices - " + bufferMesh.name);
  2183. this._bufferViews.push(bufferView);
  2184. indexBufferViewIndex = this._bufferViews.length - 1;
  2185. for (var k = 0, length_7 = indices.length; k < length_7; ++k) {
  2186. binaryWriter.setUInt32(indices[k]);
  2187. }
  2188. }
  2189. }
  2190. if (bufferMesh.subMeshes) {
  2191. // go through all mesh primitives (submeshes)
  2192. for (var _a = 0, _b = bufferMesh.subMeshes; _a < _b.length; _a++) {
  2193. var submesh = _b[_a];
  2194. uvCoordsPresent = false;
  2195. var babylonMaterial = submesh.getMaterial() || bufferMesh.getScene().defaultMaterial;
  2196. var materialIndex = null;
  2197. if (babylonMaterial) {
  2198. if (bufferMesh instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["LinesMesh"]) {
  2199. // get the color from the lines mesh and set it in the material
  2200. var material = {
  2201. name: bufferMesh.name + ' material'
  2202. };
  2203. if (!bufferMesh.color.equals(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].White()) || bufferMesh.alpha < 1) {
  2204. material.pbrMetallicRoughness = {
  2205. baseColorFactor: bufferMesh.color.asArray().concat([bufferMesh.alpha])
  2206. };
  2207. }
  2208. this._materials.push(material);
  2209. materialIndex = this._materials.length - 1;
  2210. }
  2211. else if (babylonMaterial instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["MultiMaterial"]) {
  2212. var subMaterial = babylonMaterial.subMaterials[submesh.materialIndex];
  2213. if (subMaterial) {
  2214. babylonMaterial = subMaterial;
  2215. materialIndex = this._materialMap[babylonMaterial.uniqueId];
  2216. }
  2217. }
  2218. else {
  2219. materialIndex = this._materialMap[babylonMaterial.uniqueId];
  2220. }
  2221. }
  2222. var glTFMaterial = materialIndex != null ? this._materials[materialIndex] : null;
  2223. var meshPrimitive = { attributes: {} };
  2224. this.setPrimitiveMode(meshPrimitive, primitiveMode);
  2225. for (var _c = 0, attributeData_2 = attributeData; _c < attributeData_2.length; _c++) {
  2226. var attribute = attributeData_2[_c];
  2227. var attributeKind = attribute.kind;
  2228. if (attributeKind === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UVKind || attributeKind === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].UV2Kind) {
  2229. if (glTFMaterial && !this._glTFMaterialExporter._hasTexturesPresent(glTFMaterial)) {
  2230. continue;
  2231. }
  2232. }
  2233. var vertexData = bufferMesh.getVerticesData(attributeKind);
  2234. if (vertexData) {
  2235. var vertexBuffer = this.getVertexBufferFromMesh(attributeKind, bufferMesh);
  2236. if (vertexBuffer) {
  2237. var stride = vertexBuffer.getSize();
  2238. var bufferViewIndex = attribute.bufferViewIndex;
  2239. if (bufferViewIndex != undefined) { // check to see if bufferviewindex has a numeric value assigned.
  2240. minMax = { min: null, max: null };
  2241. if (attributeKind == babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind) {
  2242. minMax = _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._CalculateMinMaxPositions(vertexData, 0, vertexData.length / stride, this._convertToRightHandedSystem);
  2243. }
  2244. var accessor = _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._CreateAccessor(bufferViewIndex, attributeKind + " - " + babylonTransformNode.name, attribute.accessorType, 5126 /* FLOAT */, vertexData.length / stride, 0, minMax.min, minMax.max);
  2245. this._accessors.push(accessor);
  2246. this.setAttributeKind(meshPrimitive, attributeKind);
  2247. if (meshPrimitive.attributes.TEXCOORD_0 != null || meshPrimitive.attributes.TEXCOORD_1 != null) {
  2248. uvCoordsPresent = true;
  2249. }
  2250. }
  2251. }
  2252. }
  2253. }
  2254. if (indexBufferViewIndex) {
  2255. // Create accessor
  2256. var accessor = _glTFUtilities__WEBPACK_IMPORTED_MODULE_2__["_GLTFUtilities"]._CreateAccessor(indexBufferViewIndex, "indices - " + babylonTransformNode.name, "SCALAR" /* SCALAR */, 5125 /* UNSIGNED_INT */, submesh.indexCount, submesh.indexStart * 4, null, null);
  2257. this._accessors.push(accessor);
  2258. meshPrimitive.indices = this._accessors.length - 1;
  2259. }
  2260. if (materialIndex != null && Object.keys(meshPrimitive.attributes).length > 0) {
  2261. var sideOrientation = babylonMaterial.sideOrientation;
  2262. // Only reverse the winding if we have a clockwise winding
  2263. if (sideOrientation === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Material"].ClockWiseSideOrientation) {
  2264. var byteOffset = indexBufferViewIndex != null ? this._bufferViews[indexBufferViewIndex].byteOffset : null;
  2265. if (byteOffset == null) {
  2266. byteOffset = 0;
  2267. }
  2268. var babylonIndices = null;
  2269. if (indexBufferViewIndex != null) {
  2270. babylonIndices = bufferMesh.getIndices();
  2271. }
  2272. if (babylonIndices) {
  2273. this.reorderIndicesBasedOnPrimitiveMode(submesh, primitiveMode, babylonIndices, byteOffset, binaryWriter);
  2274. }
  2275. else {
  2276. for (var _d = 0, attributeData_3 = attributeData; _d < attributeData_3.length; _d++) {
  2277. var attribute = attributeData_3[_d];
  2278. var vertexData = bufferMesh.getVerticesData(attribute.kind);
  2279. if (vertexData) {
  2280. var byteOffset_1 = this._bufferViews[vertexAttributeBufferViews[attribute.kind]].byteOffset;
  2281. if (!byteOffset_1) {
  2282. byteOffset_1 = 0;
  2283. }
  2284. this.reorderVertexAttributeDataBasedOnPrimitiveMode(submesh, primitiveMode, sideOrientation, attribute.kind, vertexData, byteOffset_1, binaryWriter);
  2285. }
  2286. }
  2287. }
  2288. }
  2289. if (!uvCoordsPresent && this._glTFMaterialExporter._hasTexturesPresent(this._materials[materialIndex])) {
  2290. var newMat = this._glTFMaterialExporter._stripTexturesFromMaterial(this._materials[materialIndex]);
  2291. this._materials.push(newMat);
  2292. materialIndex = this._materials.length - 1;
  2293. }
  2294. meshPrimitive.material = materialIndex;
  2295. }
  2296. mesh.primitives.push(meshPrimitive);
  2297. var promise = this._extensionsPostExportMeshPrimitiveAsync("postExport", meshPrimitive, submesh, binaryWriter);
  2298. if (promise) {
  2299. promises.push();
  2300. }
  2301. }
  2302. }
  2303. }
  2304. return Promise.all(promises).then(function () {
  2305. /* do nothing */
  2306. });
  2307. };
  2308. /**
  2309. * Creates a glTF scene based on the array of meshes
  2310. * Returns the the total byte offset
  2311. * @param babylonScene Babylon scene to get the mesh data from
  2312. * @param binaryWriter Buffer to write binary data to
  2313. */
  2314. _Exporter.prototype.createSceneAsync = function (babylonScene, binaryWriter) {
  2315. var _this = this;
  2316. var scene = { nodes: [] };
  2317. var glTFNodeIndex;
  2318. var glTFNode;
  2319. var directDescendents;
  2320. var nodes = babylonScene.transformNodes.concat(babylonScene.meshes, babylonScene.lights);
  2321. return this._glTFMaterialExporter._convertMaterialsToGLTFAsync(babylonScene.materials, "image/png" /* PNG */, true).then(function () {
  2322. return _this.createNodeMapAndAnimationsAsync(babylonScene, nodes, _this._shouldExportNode, binaryWriter).then(function (nodeMap) {
  2323. _this._nodeMap = nodeMap;
  2324. _this._totalByteLength = binaryWriter.getByteOffset();
  2325. if (_this._totalByteLength == undefined) {
  2326. throw new Error("undefined byte length!");
  2327. }
  2328. // Build Hierarchy with the node map.
  2329. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  2330. var babylonNode = nodes_1[_i];
  2331. glTFNodeIndex = _this._nodeMap[babylonNode.uniqueId];
  2332. if (glTFNodeIndex !== undefined) {
  2333. glTFNode = _this._nodes[glTFNodeIndex];
  2334. if (!babylonNode.parent) {
  2335. if (!_this._shouldExportNode(babylonNode)) {
  2336. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Log("Omitting " + babylonNode.name + " from scene.");
  2337. }
  2338. else {
  2339. if (_this._convertToRightHandedSystem) {
  2340. if (glTFNode.translation) {
  2341. glTFNode.translation[2] *= -1;
  2342. glTFNode.translation[0] *= -1;
  2343. }
  2344. glTFNode.rotation = glTFNode.rotation ? babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray([0, 1, 0, 0]).multiply(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray(glTFNode.rotation)).asArray() : (babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Quaternion"].FromArray([0, 1, 0, 0])).asArray();
  2345. }
  2346. scene.nodes.push(glTFNodeIndex);
  2347. }
  2348. }
  2349. directDescendents = babylonNode.getDescendants(true);
  2350. if (!glTFNode.children && directDescendents && directDescendents.length) {
  2351. var children = [];
  2352. for (var _a = 0, directDescendents_1 = directDescendents; _a < directDescendents_1.length; _a++) {
  2353. var descendent = directDescendents_1[_a];
  2354. if (_this._nodeMap[descendent.uniqueId] != null) {
  2355. children.push(_this._nodeMap[descendent.uniqueId]);
  2356. }
  2357. }
  2358. if (children.length) {
  2359. glTFNode.children = children;
  2360. }
  2361. }
  2362. }
  2363. }
  2364. if (scene.nodes.length) {
  2365. _this._scenes.push(scene);
  2366. }
  2367. });
  2368. });
  2369. };
  2370. /**
  2371. * Creates a mapping of Node unique id to node index and handles animations
  2372. * @param babylonScene Babylon Scene
  2373. * @param nodes Babylon transform nodes
  2374. * @param shouldExportNode Callback specifying if a transform node should be exported
  2375. * @param binaryWriter Buffer to write binary data to
  2376. * @returns Node mapping of unique id to index
  2377. */
  2378. _Exporter.prototype.createNodeMapAndAnimationsAsync = function (babylonScene, nodes, shouldExportNode, binaryWriter) {
  2379. var _this = this;
  2380. var promiseChain = Promise.resolve();
  2381. var nodeMap = {};
  2382. var nodeIndex;
  2383. var runtimeGLTFAnimation = {
  2384. name: 'runtime animations',
  2385. channels: [],
  2386. samplers: []
  2387. };
  2388. var idleGLTFAnimations = [];
  2389. var _loop_1 = function (babylonNode) {
  2390. if (shouldExportNode(babylonNode)) {
  2391. promiseChain = promiseChain.then(function () {
  2392. return _this.createNodeAsync(babylonNode, binaryWriter).then(function (node) {
  2393. var promise = _this._extensionsPostExportNodeAsync("createNodeAsync", node, babylonNode);
  2394. if (promise == null) {
  2395. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("Not exporting node " + babylonNode.name);
  2396. return Promise.resolve();
  2397. }
  2398. else {
  2399. return promise.then(function (node) {
  2400. var directDescendents = babylonNode.getDescendants(true, function (node) { return (node instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Node"]); });
  2401. if (directDescendents.length || node.mesh != null || (node.extensions)) {
  2402. _this._nodes.push(node);
  2403. nodeIndex = _this._nodes.length - 1;
  2404. nodeMap[babylonNode.uniqueId] = nodeIndex;
  2405. }
  2406. if (!babylonScene.animationGroups.length && babylonNode.animations.length) {
  2407. _glTFAnimation__WEBPACK_IMPORTED_MODULE_4__["_GLTFAnimation"]._CreateNodeAnimationFromNodeAnimations(babylonNode, runtimeGLTFAnimation, idleGLTFAnimations, nodeMap, _this._nodes, binaryWriter, _this._bufferViews, _this._accessors, _this._convertToRightHandedSystem, _this._animationSampleRate);
  2408. }
  2409. });
  2410. }
  2411. });
  2412. });
  2413. }
  2414. else {
  2415. "Excluding node " + babylonNode.name;
  2416. }
  2417. };
  2418. for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
  2419. var babylonNode = nodes_2[_i];
  2420. _loop_1(babylonNode);
  2421. }
  2422. return promiseChain.then(function () {
  2423. if (runtimeGLTFAnimation.channels.length && runtimeGLTFAnimation.samplers.length) {
  2424. _this._animations.push(runtimeGLTFAnimation);
  2425. }
  2426. idleGLTFAnimations.forEach(function (idleGLTFAnimation) {
  2427. if (idleGLTFAnimation.channels.length && idleGLTFAnimation.samplers.length) {
  2428. _this._animations.push(idleGLTFAnimation);
  2429. }
  2430. });
  2431. if (babylonScene.animationGroups.length) {
  2432. _glTFAnimation__WEBPACK_IMPORTED_MODULE_4__["_GLTFAnimation"]._CreateNodeAnimationFromAnimationGroups(babylonScene, _this._animations, nodeMap, _this._nodes, binaryWriter, _this._bufferViews, _this._accessors, _this._convertToRightHandedSystem, _this._animationSampleRate);
  2433. }
  2434. return nodeMap;
  2435. });
  2436. };
  2437. /**
  2438. * Creates a glTF node from a Babylon mesh
  2439. * @param babylonMesh Source Babylon mesh
  2440. * @param binaryWriter Buffer for storing geometry data
  2441. * @returns glTF node
  2442. */
  2443. _Exporter.prototype.createNodeAsync = function (babylonNode, binaryWriter) {
  2444. var _this = this;
  2445. return Promise.resolve().then(function () {
  2446. // create node to hold translation/rotation/scale and the mesh
  2447. var node = {};
  2448. // create mesh
  2449. var mesh = { primitives: [] };
  2450. if (babylonNode.name) {
  2451. node.name = babylonNode.name;
  2452. }
  2453. if (babylonNode instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["TransformNode"]) {
  2454. // Set transformation
  2455. _this.setNodeTransformation(node, babylonNode);
  2456. return _this.setPrimitiveAttributesAsync(mesh, babylonNode, binaryWriter).then(function () {
  2457. if (mesh.primitives.length) {
  2458. _this._meshes.push(mesh);
  2459. node.mesh = _this._meshes.length - 1;
  2460. }
  2461. return node;
  2462. });
  2463. }
  2464. else {
  2465. return node;
  2466. }
  2467. });
  2468. };
  2469. _Exporter._ExtensionNames = new Array();
  2470. _Exporter._ExtensionFactories = {};
  2471. return _Exporter;
  2472. }());
  2473. /**
  2474. * @hidden
  2475. *
  2476. * Stores glTF binary data. If the array buffer byte length is exceeded, it doubles in size dynamically
  2477. */
  2478. var _BinaryWriter = /** @class */ (function () {
  2479. /**
  2480. * Initialize binary writer with an initial byte length
  2481. * @param byteLength Initial byte length of the array buffer
  2482. */
  2483. function _BinaryWriter(byteLength) {
  2484. this._arrayBuffer = new ArrayBuffer(byteLength);
  2485. this._dataView = new DataView(this._arrayBuffer);
  2486. this._byteOffset = 0;
  2487. }
  2488. /**
  2489. * Resize the array buffer to the specified byte length
  2490. * @param byteLength
  2491. */
  2492. _BinaryWriter.prototype.resizeBuffer = function (byteLength) {
  2493. var newBuffer = new ArrayBuffer(byteLength);
  2494. var oldUint8Array = new Uint8Array(this._arrayBuffer);
  2495. var newUint8Array = new Uint8Array(newBuffer);
  2496. for (var i = 0, length_8 = newUint8Array.byteLength; i < length_8; ++i) {
  2497. newUint8Array[i] = oldUint8Array[i];
  2498. }
  2499. this._arrayBuffer = newBuffer;
  2500. this._dataView = new DataView(this._arrayBuffer);
  2501. return newBuffer;
  2502. };
  2503. /**
  2504. * Get an array buffer with the length of the byte offset
  2505. * @returns ArrayBuffer resized to the byte offset
  2506. */
  2507. _BinaryWriter.prototype.getArrayBuffer = function () {
  2508. return this.resizeBuffer(this.getByteOffset());
  2509. };
  2510. /**
  2511. * Get the byte offset of the array buffer
  2512. * @returns byte offset
  2513. */
  2514. _BinaryWriter.prototype.getByteOffset = function () {
  2515. if (this._byteOffset == undefined) {
  2516. throw new Error("Byte offset is undefined!");
  2517. }
  2518. return this._byteOffset;
  2519. };
  2520. /**
  2521. * Stores an UInt8 in the array buffer
  2522. * @param entry
  2523. * @param byteOffset If defined, specifies where to set the value as an offset.
  2524. */
  2525. _BinaryWriter.prototype.setUInt8 = function (entry, byteOffset) {
  2526. if (byteOffset != null) {
  2527. if (byteOffset < this._byteOffset) {
  2528. this._dataView.setUint8(byteOffset, entry);
  2529. }
  2530. else {
  2531. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error('BinaryWriter: byteoffset is greater than the current binary buffer length!');
  2532. }
  2533. }
  2534. else {
  2535. if (this._byteOffset + 1 > this._arrayBuffer.byteLength) {
  2536. this.resizeBuffer(this._arrayBuffer.byteLength * 2);
  2537. }
  2538. this._dataView.setUint8(this._byteOffset++, entry);
  2539. }
  2540. };
  2541. /**
  2542. * Gets an UInt32 in the array buffer
  2543. * @param entry
  2544. * @param byteOffset If defined, specifies where to set the value as an offset.
  2545. */
  2546. _BinaryWriter.prototype.getUInt32 = function (byteOffset) {
  2547. if (byteOffset < this._byteOffset) {
  2548. return this._dataView.getUint32(byteOffset, true);
  2549. }
  2550. else {
  2551. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error('BinaryWriter: byteoffset is greater than the current binary buffer length!');
  2552. throw new Error('BinaryWriter: byteoffset is greater than the current binary buffer length!');
  2553. }
  2554. };
  2555. _BinaryWriter.prototype.getVector3Float32FromRef = function (vector3, byteOffset) {
  2556. if (byteOffset + 8 > this._byteOffset) {
  2557. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("BinaryWriter: byteoffset is greater than the current binary buffer length!");
  2558. }
  2559. else {
  2560. vector3.x = this._dataView.getFloat32(byteOffset, true);
  2561. vector3.y = this._dataView.getFloat32(byteOffset + 4, true);
  2562. vector3.z = this._dataView.getFloat32(byteOffset + 8, true);
  2563. }
  2564. };
  2565. _BinaryWriter.prototype.setVector3Float32FromRef = function (vector3, byteOffset) {
  2566. if (byteOffset + 8 > this._byteOffset) {
  2567. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("BinaryWriter: byteoffset is greater than the current binary buffer length!");
  2568. }
  2569. else {
  2570. this._dataView.setFloat32(byteOffset, vector3.x, true);
  2571. this._dataView.setFloat32(byteOffset + 4, vector3.y, true);
  2572. this._dataView.setFloat32(byteOffset + 8, vector3.z, true);
  2573. }
  2574. };
  2575. _BinaryWriter.prototype.getVector4Float32FromRef = function (vector4, byteOffset) {
  2576. if (byteOffset + 12 > this._byteOffset) {
  2577. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("BinaryWriter: byteoffset is greater than the current binary buffer length!");
  2578. }
  2579. else {
  2580. vector4.x = this._dataView.getFloat32(byteOffset, true);
  2581. vector4.y = this._dataView.getFloat32(byteOffset + 4, true);
  2582. vector4.z = this._dataView.getFloat32(byteOffset + 8, true);
  2583. vector4.w = this._dataView.getFloat32(byteOffset + 12, true);
  2584. }
  2585. };
  2586. _BinaryWriter.prototype.setVector4Float32FromRef = function (vector4, byteOffset) {
  2587. if (byteOffset + 12 > this._byteOffset) {
  2588. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("BinaryWriter: byteoffset is greater than the current binary buffer length!");
  2589. }
  2590. else {
  2591. this._dataView.setFloat32(byteOffset, vector4.x, true);
  2592. this._dataView.setFloat32(byteOffset + 4, vector4.y, true);
  2593. this._dataView.setFloat32(byteOffset + 8, vector4.z, true);
  2594. this._dataView.setFloat32(byteOffset + 12, vector4.w, true);
  2595. }
  2596. };
  2597. /**
  2598. * Stores a Float32 in the array buffer
  2599. * @param entry
  2600. */
  2601. _BinaryWriter.prototype.setFloat32 = function (entry, byteOffset) {
  2602. if (isNaN(entry)) {
  2603. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error('Invalid data being written!');
  2604. }
  2605. if (byteOffset != null) {
  2606. if (byteOffset < this._byteOffset) {
  2607. this._dataView.setFloat32(byteOffset, entry, true);
  2608. }
  2609. else {
  2610. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error('BinaryWriter: byteoffset is greater than the current binary length!');
  2611. }
  2612. }
  2613. if (this._byteOffset + 4 > this._arrayBuffer.byteLength) {
  2614. this.resizeBuffer(this._arrayBuffer.byteLength * 2);
  2615. }
  2616. this._dataView.setFloat32(this._byteOffset, entry, true);
  2617. this._byteOffset += 4;
  2618. };
  2619. /**
  2620. * Stores an UInt32 in the array buffer
  2621. * @param entry
  2622. * @param byteOffset If defined, specifies where to set the value as an offset.
  2623. */
  2624. _BinaryWriter.prototype.setUInt32 = function (entry, byteOffset) {
  2625. if (byteOffset != null) {
  2626. if (byteOffset < this._byteOffset) {
  2627. this._dataView.setUint32(byteOffset, entry, true);
  2628. }
  2629. else {
  2630. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error('BinaryWriter: byteoffset is greater than the current binary buffer length!');
  2631. }
  2632. }
  2633. else {
  2634. if (this._byteOffset + 4 > this._arrayBuffer.byteLength) {
  2635. this.resizeBuffer(this._arrayBuffer.byteLength * 2);
  2636. }
  2637. this._dataView.setUint32(this._byteOffset, entry, true);
  2638. this._byteOffset += 4;
  2639. }
  2640. };
  2641. return _BinaryWriter;
  2642. }());
  2643. /***/ }),
  2644. /***/ "./glTF/2.0/glTFExporterExtension.ts":
  2645. /*!*******************************************!*\
  2646. !*** ./glTF/2.0/glTFExporterExtension.ts ***!
  2647. \*******************************************/
  2648. /*! exports provided: __IGLTFExporterExtensionV2 */
  2649. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  2650. "use strict";
  2651. __webpack_require__.r(__webpack_exports__);
  2652. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtensionV2", function() { return __IGLTFExporterExtensionV2; });
  2653. /** @hidden */
  2654. var __IGLTFExporterExtensionV2 = 0; // I am here to allow dts to be created
  2655. /***/ }),
  2656. /***/ "./glTF/2.0/glTFMaterialExporter.ts":
  2657. /*!******************************************!*\
  2658. !*** ./glTF/2.0/glTFMaterialExporter.ts ***!
  2659. \******************************************/
  2660. /*! exports provided: _GLTFMaterialExporter */
  2661. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  2662. "use strict";
  2663. __webpack_require__.r(__webpack_exports__);
  2664. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "_GLTFMaterialExporter", function() { return _GLTFMaterialExporter; });
  2665. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Maths/math */ "babylonjs/Maths/math");
  2666. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__);
  2667. /**
  2668. * Utility methods for working with glTF material conversion properties. This class should only be used internally
  2669. * @hidden
  2670. */
  2671. var _GLTFMaterialExporter = /** @class */ (function () {
  2672. function _GLTFMaterialExporter(exporter) {
  2673. /**
  2674. * Mapping to store textures
  2675. */
  2676. this._textureMap = {};
  2677. this._textureMap = {};
  2678. this._exporter = exporter;
  2679. }
  2680. /**
  2681. * Specifies if two colors are approximately equal in value
  2682. * @param color1 first color to compare to
  2683. * @param color2 second color to compare to
  2684. * @param epsilon threshold value
  2685. */
  2686. _GLTFMaterialExporter.FuzzyEquals = function (color1, color2, epsilon) {
  2687. return babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Scalar"].WithinEpsilon(color1.r, color2.r, epsilon) &&
  2688. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Scalar"].WithinEpsilon(color1.g, color2.g, epsilon) &&
  2689. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Scalar"].WithinEpsilon(color1.b, color2.b, epsilon);
  2690. };
  2691. /**
  2692. * Gets the materials from a Babylon scene and converts them to glTF materials
  2693. * @param scene babylonjs scene
  2694. * @param mimeType texture mime type
  2695. * @param images array of images
  2696. * @param textures array of textures
  2697. * @param materials array of materials
  2698. * @param imageData mapping of texture names to base64 textures
  2699. * @param hasTextureCoords specifies if texture coordinates are present on the material
  2700. */
  2701. _GLTFMaterialExporter.prototype._convertMaterialsToGLTFAsync = function (babylonMaterials, mimeType, hasTextureCoords) {
  2702. var promises = [];
  2703. for (var _i = 0, babylonMaterials_1 = babylonMaterials; _i < babylonMaterials_1.length; _i++) {
  2704. var babylonMaterial = babylonMaterials_1[_i];
  2705. if (babylonMaterial instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["StandardMaterial"]) {
  2706. promises.push(this._convertStandardMaterialAsync(babylonMaterial, mimeType, hasTextureCoords));
  2707. }
  2708. else if (babylonMaterial instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["PBRMetallicRoughnessMaterial"]) {
  2709. promises.push(this._convertPBRMetallicRoughnessMaterialAsync(babylonMaterial, mimeType, hasTextureCoords));
  2710. }
  2711. else if (babylonMaterial instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["PBRMaterial"]) {
  2712. promises.push(this._convertPBRMaterialAsync(babylonMaterial, mimeType, hasTextureCoords));
  2713. }
  2714. else {
  2715. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("Unsupported material type: " + babylonMaterial.name);
  2716. }
  2717. }
  2718. return Promise.all(promises).then(function () { });
  2719. };
  2720. /**
  2721. * Makes a copy of the glTF material without the texture parameters
  2722. * @param originalMaterial original glTF material
  2723. * @returns glTF material without texture parameters
  2724. */
  2725. _GLTFMaterialExporter.prototype._stripTexturesFromMaterial = function (originalMaterial) {
  2726. var newMaterial = {};
  2727. if (originalMaterial) {
  2728. newMaterial.name = originalMaterial.name;
  2729. newMaterial.doubleSided = originalMaterial.doubleSided;
  2730. newMaterial.alphaMode = originalMaterial.alphaMode;
  2731. newMaterial.alphaCutoff = originalMaterial.alphaCutoff;
  2732. newMaterial.emissiveFactor = originalMaterial.emissiveFactor;
  2733. var originalPBRMetallicRoughness = originalMaterial.pbrMetallicRoughness;
  2734. if (originalPBRMetallicRoughness) {
  2735. newMaterial.pbrMetallicRoughness = {};
  2736. newMaterial.pbrMetallicRoughness.baseColorFactor = originalPBRMetallicRoughness.baseColorFactor;
  2737. newMaterial.pbrMetallicRoughness.metallicFactor = originalPBRMetallicRoughness.metallicFactor;
  2738. newMaterial.pbrMetallicRoughness.roughnessFactor = originalPBRMetallicRoughness.roughnessFactor;
  2739. }
  2740. }
  2741. return newMaterial;
  2742. };
  2743. /**
  2744. * Specifies if the material has any texture parameters present
  2745. * @param material glTF Material
  2746. * @returns boolean specifying if texture parameters are present
  2747. */
  2748. _GLTFMaterialExporter.prototype._hasTexturesPresent = function (material) {
  2749. if (material.emissiveTexture || material.normalTexture || material.occlusionTexture) {
  2750. return true;
  2751. }
  2752. var pbrMat = material.pbrMetallicRoughness;
  2753. if (pbrMat) {
  2754. if (pbrMat.baseColorTexture || pbrMat.metallicRoughnessTexture) {
  2755. return true;
  2756. }
  2757. }
  2758. return false;
  2759. };
  2760. /**
  2761. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material
  2762. * @param babylonStandardMaterial
  2763. * @returns glTF Metallic Roughness Material representation
  2764. */
  2765. _GLTFMaterialExporter.prototype._convertToGLTFPBRMetallicRoughness = function (babylonStandardMaterial) {
  2766. var P0 = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"](0, 1);
  2767. var P1 = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"](0, 0.1);
  2768. var P2 = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"](0, 0.1);
  2769. var P3 = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector2"](1300, 0.1);
  2770. /**
  2771. * Given the control points, solve for x based on a given t for a cubic bezier curve
  2772. * @param t a value between 0 and 1
  2773. * @param p0 first control point
  2774. * @param p1 second control point
  2775. * @param p2 third control point
  2776. * @param p3 fourth control point
  2777. * @returns number result of cubic bezier curve at the specified t
  2778. */
  2779. function _cubicBezierCurve(t, p0, p1, p2, p3) {
  2780. return ((1 - t) * (1 - t) * (1 - t) * p0 +
  2781. 3 * (1 - t) * (1 - t) * t * p1 +
  2782. 3 * (1 - t) * t * t * p2 +
  2783. t * t * t * p3);
  2784. }
  2785. /**
  2786. * Evaluates a specified specular power value to determine the appropriate roughness value,
  2787. * based on a pre-defined cubic bezier curve with specular on the abscissa axis (x-axis)
  2788. * and roughness on the ordinant axis (y-axis)
  2789. * @param specularPower specular power of standard material
  2790. * @returns Number representing the roughness value
  2791. */
  2792. function _solveForRoughness(specularPower) {
  2793. var t = Math.pow(specularPower / P3.x, 0.333333);
  2794. return _cubicBezierCurve(t, P0.y, P1.y, P2.y, P3.y);
  2795. }
  2796. var diffuse = babylonStandardMaterial.diffuseColor.toLinearSpace().scale(0.5);
  2797. var opacity = babylonStandardMaterial.alpha;
  2798. var specularPower = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Scalar"].Clamp(babylonStandardMaterial.specularPower, 0, _GLTFMaterialExporter._MaxSpecularPower);
  2799. var roughness = _solveForRoughness(specularPower);
  2800. var glTFPbrMetallicRoughness = {
  2801. baseColorFactor: [
  2802. diffuse.r,
  2803. diffuse.g,
  2804. diffuse.b,
  2805. opacity
  2806. ],
  2807. metallicFactor: 0,
  2808. roughnessFactor: roughness,
  2809. };
  2810. return glTFPbrMetallicRoughness;
  2811. };
  2812. /**
  2813. * Computes the metallic factor
  2814. * @param diffuse diffused value
  2815. * @param specular specular value
  2816. * @param oneMinusSpecularStrength one minus the specular strength
  2817. * @returns metallic value
  2818. */
  2819. _GLTFMaterialExporter._SolveMetallic = function (diffuse, specular, oneMinusSpecularStrength) {
  2820. if (specular < this._DielectricSpecular.r) {
  2821. this._DielectricSpecular;
  2822. return 0;
  2823. }
  2824. var a = this._DielectricSpecular.r;
  2825. var b = diffuse * oneMinusSpecularStrength / (1.0 - this._DielectricSpecular.r) + specular - 2.0 * this._DielectricSpecular.r;
  2826. var c = this._DielectricSpecular.r - specular;
  2827. var D = b * b - 4.0 * a * c;
  2828. return babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Scalar"].Clamp((-b + Math.sqrt(D)) / (2.0 * a), 0, 1);
  2829. };
  2830. /**
  2831. * Gets the glTF alpha mode from the Babylon Material
  2832. * @param babylonMaterial Babylon Material
  2833. * @returns The Babylon alpha mode value
  2834. */
  2835. _GLTFMaterialExporter.prototype._getAlphaMode = function (babylonMaterial) {
  2836. if (babylonMaterial.needAlphaBlending()) {
  2837. return "BLEND" /* BLEND */;
  2838. }
  2839. else if (babylonMaterial.needAlphaTesting()) {
  2840. return "MASK" /* MASK */;
  2841. }
  2842. else {
  2843. return "OPAQUE" /* OPAQUE */;
  2844. }
  2845. };
  2846. /**
  2847. * Converts a Babylon Standard Material to a glTF Material
  2848. * @param babylonStandardMaterial BJS Standard Material
  2849. * @param mimeType mime type to use for the textures
  2850. * @param images array of glTF image interfaces
  2851. * @param textures array of glTF texture interfaces
  2852. * @param materials array of glTF material interfaces
  2853. * @param imageData map of image file name to data
  2854. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  2855. */
  2856. _GLTFMaterialExporter.prototype._convertStandardMaterialAsync = function (babylonStandardMaterial, mimeType, hasTextureCoords) {
  2857. var materialMap = this._exporter._materialMap;
  2858. var materials = this._exporter._materials;
  2859. var alphaMode = this._getAlphaMode(babylonStandardMaterial);
  2860. var promises = [];
  2861. var glTFPbrMetallicRoughness = this._convertToGLTFPBRMetallicRoughness(babylonStandardMaterial);
  2862. var glTFMaterial = { name: babylonStandardMaterial.name };
  2863. if (babylonStandardMaterial.backFaceCulling != null && !babylonStandardMaterial.backFaceCulling) {
  2864. if (!babylonStandardMaterial.twoSidedLighting) {
  2865. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn(babylonStandardMaterial.name + ": Back-face culling enabled and two-sided lighting disabled is not supported in glTF.");
  2866. }
  2867. glTFMaterial.doubleSided = true;
  2868. }
  2869. if (hasTextureCoords) {
  2870. if (babylonStandardMaterial.diffuseTexture) {
  2871. promises.push(this._exportTextureAsync(babylonStandardMaterial.diffuseTexture, mimeType).then(function (glTFTexture) {
  2872. if (glTFTexture) {
  2873. glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
  2874. }
  2875. }));
  2876. }
  2877. if (babylonStandardMaterial.bumpTexture) {
  2878. promises.push(this._exportTextureAsync(babylonStandardMaterial.bumpTexture, mimeType).then(function (glTFTexture) {
  2879. if (glTFTexture) {
  2880. glTFMaterial.normalTexture = glTFTexture;
  2881. if (babylonStandardMaterial.bumpTexture != null && babylonStandardMaterial.bumpTexture.level !== 1) {
  2882. glTFMaterial.normalTexture.scale = babylonStandardMaterial.bumpTexture.level;
  2883. }
  2884. }
  2885. }));
  2886. }
  2887. if (babylonStandardMaterial.emissiveTexture) {
  2888. glTFMaterial.emissiveFactor = [1.0, 1.0, 1.0];
  2889. promises.push(this._exportTextureAsync(babylonStandardMaterial.emissiveTexture, mimeType).then(function (glTFEmissiveTexture) {
  2890. if (glTFEmissiveTexture) {
  2891. glTFMaterial.emissiveTexture = glTFEmissiveTexture;
  2892. }
  2893. }));
  2894. }
  2895. if (babylonStandardMaterial.ambientTexture) {
  2896. promises.push(this._exportTextureAsync(babylonStandardMaterial.ambientTexture, mimeType).then(function (glTFTexture) {
  2897. if (glTFTexture) {
  2898. var occlusionTexture = {
  2899. index: glTFTexture.index
  2900. };
  2901. glTFMaterial.occlusionTexture = occlusionTexture;
  2902. occlusionTexture.strength = 1.0;
  2903. }
  2904. }));
  2905. }
  2906. }
  2907. if (babylonStandardMaterial.alpha < 1.0 || babylonStandardMaterial.opacityTexture) {
  2908. if (babylonStandardMaterial.alphaMode === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Engine"].ALPHA_COMBINE) {
  2909. glTFMaterial.alphaMode = "BLEND" /* BLEND */;
  2910. }
  2911. else {
  2912. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn(babylonStandardMaterial.name + ": glTF 2.0 does not support alpha mode: " + babylonStandardMaterial.alphaMode.toString());
  2913. }
  2914. }
  2915. if (babylonStandardMaterial.emissiveColor && !_GLTFMaterialExporter.FuzzyEquals(babylonStandardMaterial.emissiveColor, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].Black(), _GLTFMaterialExporter._Epsilon)) {
  2916. glTFMaterial.emissiveFactor = babylonStandardMaterial.emissiveColor.asArray();
  2917. }
  2918. glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
  2919. if (alphaMode !== "OPAQUE" /* OPAQUE */) {
  2920. switch (alphaMode) {
  2921. case "BLEND" /* BLEND */: {
  2922. glTFMaterial.alphaMode = "BLEND" /* BLEND */;
  2923. break;
  2924. }
  2925. case "MASK" /* MASK */: {
  2926. glTFMaterial.alphaMode = "MASK" /* MASK */;
  2927. glTFMaterial.alphaCutoff = babylonStandardMaterial.alphaCutOff;
  2928. break;
  2929. }
  2930. default: {
  2931. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn("Unsupported alpha mode " + alphaMode);
  2932. }
  2933. }
  2934. }
  2935. materials.push(glTFMaterial);
  2936. materialMap[babylonStandardMaterial.uniqueId] = materials.length - 1;
  2937. return Promise.all(promises).then(function () { });
  2938. };
  2939. /**
  2940. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  2941. * @param babylonPBRMetalRoughMaterial BJS PBR Metallic Roughness Material
  2942. * @param mimeType mime type to use for the textures
  2943. * @param images array of glTF image interfaces
  2944. * @param textures array of glTF texture interfaces
  2945. * @param materials array of glTF material interfaces
  2946. * @param imageData map of image file name to data
  2947. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  2948. */
  2949. _GLTFMaterialExporter.prototype._convertPBRMetallicRoughnessMaterialAsync = function (babylonPBRMetalRoughMaterial, mimeType, hasTextureCoords) {
  2950. var materialMap = this._exporter._materialMap;
  2951. var materials = this._exporter._materials;
  2952. var promises = [];
  2953. var glTFPbrMetallicRoughness = {};
  2954. if (babylonPBRMetalRoughMaterial.baseColor) {
  2955. glTFPbrMetallicRoughness.baseColorFactor = [
  2956. babylonPBRMetalRoughMaterial.baseColor.r,
  2957. babylonPBRMetalRoughMaterial.baseColor.g,
  2958. babylonPBRMetalRoughMaterial.baseColor.b,
  2959. babylonPBRMetalRoughMaterial.alpha
  2960. ];
  2961. }
  2962. if (babylonPBRMetalRoughMaterial.metallic != null && babylonPBRMetalRoughMaterial.metallic !== 1) {
  2963. glTFPbrMetallicRoughness.metallicFactor = babylonPBRMetalRoughMaterial.metallic;
  2964. }
  2965. if (babylonPBRMetalRoughMaterial.roughness != null && babylonPBRMetalRoughMaterial.roughness !== 1) {
  2966. glTFPbrMetallicRoughness.roughnessFactor = babylonPBRMetalRoughMaterial.roughness;
  2967. }
  2968. var glTFMaterial = {
  2969. name: babylonPBRMetalRoughMaterial.name
  2970. };
  2971. if (babylonPBRMetalRoughMaterial.doubleSided) {
  2972. glTFMaterial.doubleSided = babylonPBRMetalRoughMaterial.doubleSided;
  2973. }
  2974. var alphaMode = null;
  2975. if (babylonPBRMetalRoughMaterial.transparencyMode != null) {
  2976. alphaMode = this._getAlphaMode(babylonPBRMetalRoughMaterial);
  2977. if (alphaMode) {
  2978. if (alphaMode !== "OPAQUE" /* OPAQUE */) { //glTF defaults to opaque
  2979. glTFMaterial.alphaMode = alphaMode;
  2980. if (alphaMode === "MASK" /* MASK */) {
  2981. glTFMaterial.alphaCutoff = babylonPBRMetalRoughMaterial.alphaCutOff;
  2982. }
  2983. }
  2984. }
  2985. }
  2986. if (hasTextureCoords) {
  2987. if (babylonPBRMetalRoughMaterial.baseTexture != null) {
  2988. promises.push(this._exportTextureAsync(babylonPBRMetalRoughMaterial.baseTexture, mimeType).then(function (glTFTexture) {
  2989. if (glTFTexture) {
  2990. glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
  2991. }
  2992. }));
  2993. }
  2994. if (babylonPBRMetalRoughMaterial.normalTexture) {
  2995. promises.push(this._exportTextureAsync(babylonPBRMetalRoughMaterial.normalTexture, mimeType).then(function (glTFTexture) {
  2996. if (glTFTexture) {
  2997. glTFMaterial.normalTexture = glTFTexture;
  2998. if (babylonPBRMetalRoughMaterial.normalTexture.level !== 1) {
  2999. glTFMaterial.normalTexture.scale = babylonPBRMetalRoughMaterial.normalTexture.level;
  3000. }
  3001. }
  3002. }));
  3003. }
  3004. if (babylonPBRMetalRoughMaterial.occlusionTexture) {
  3005. promises.push(this._exportTextureAsync(babylonPBRMetalRoughMaterial.occlusionTexture, mimeType).then(function (glTFTexture) {
  3006. if (glTFTexture) {
  3007. glTFMaterial.occlusionTexture = glTFTexture;
  3008. if (babylonPBRMetalRoughMaterial.occlusionStrength != null) {
  3009. glTFMaterial.occlusionTexture.strength = babylonPBRMetalRoughMaterial.occlusionStrength;
  3010. }
  3011. }
  3012. }));
  3013. }
  3014. if (babylonPBRMetalRoughMaterial.emissiveTexture) {
  3015. promises.push(this._exportTextureAsync(babylonPBRMetalRoughMaterial.emissiveTexture, mimeType).then(function (glTFTexture) {
  3016. if (glTFTexture) {
  3017. glTFMaterial.emissiveTexture = glTFTexture;
  3018. }
  3019. }));
  3020. }
  3021. }
  3022. if (_GLTFMaterialExporter.FuzzyEquals(babylonPBRMetalRoughMaterial.emissiveColor, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].Black(), _GLTFMaterialExporter._Epsilon)) {
  3023. glTFMaterial.emissiveFactor = babylonPBRMetalRoughMaterial.emissiveColor.asArray();
  3024. }
  3025. glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
  3026. materials.push(glTFMaterial);
  3027. materialMap[babylonPBRMetalRoughMaterial.uniqueId] = materials.length - 1;
  3028. return Promise.all(promises).then(function () { });
  3029. };
  3030. /**
  3031. * Converts an image typed array buffer to a base64 image
  3032. * @param buffer typed array buffer
  3033. * @param width width of the image
  3034. * @param height height of the image
  3035. * @param mimeType mimetype of the image
  3036. * @returns base64 image string
  3037. */
  3038. _GLTFMaterialExporter.prototype._createBase64FromCanvasAsync = function (buffer, width, height, mimeType) {
  3039. var _this = this;
  3040. return new Promise(function (resolve, reject) {
  3041. var hostingScene;
  3042. var textureType = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Engine"].TEXTURETYPE_UNSIGNED_INT;
  3043. var engine = _this._exporter._getLocalEngine();
  3044. hostingScene = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Scene"](engine);
  3045. // Create a temporary texture with the texture buffer data
  3046. var tempTexture = engine.createRawTexture(buffer, width, height, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Engine"].TEXTUREFORMAT_RGBA, false, true, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].NEAREST_SAMPLINGMODE, null, textureType);
  3047. var postProcess = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["PostProcess"]("pass", "pass", null, null, 1, null, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].NEAREST_SAMPLINGMODE, engine, false, undefined, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Engine"].TEXTURETYPE_UNSIGNED_INT, undefined, null, false);
  3048. postProcess.getEffect().executeWhenCompiled(function () {
  3049. postProcess.onApply = function (effect) {
  3050. effect._bindTexture("textureSampler", tempTexture);
  3051. };
  3052. // Set the size of the texture
  3053. engine.setSize(width, height);
  3054. hostingScene.postProcessManager.directRender([postProcess], null);
  3055. postProcess.dispose();
  3056. tempTexture.dispose();
  3057. // Read data from WebGL
  3058. var canvas = engine.getRenderingCanvas();
  3059. if (canvas) {
  3060. if (!canvas.toBlob) { // fallback for browsers without "canvas.toBlob"
  3061. var dataURL = canvas.toDataURL();
  3062. resolve(dataURL);
  3063. }
  3064. else {
  3065. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].ToBlob(canvas, function (blob) {
  3066. if (blob) {
  3067. var fileReader = new FileReader();
  3068. fileReader.onload = function (event) {
  3069. var base64String = event.target.result;
  3070. hostingScene.dispose();
  3071. resolve(base64String);
  3072. };
  3073. fileReader.readAsDataURL(blob);
  3074. }
  3075. else {
  3076. reject("gltfMaterialExporter: Failed to get blob from image canvas!");
  3077. }
  3078. });
  3079. }
  3080. }
  3081. else {
  3082. reject("Engine is missing a canvas!");
  3083. }
  3084. });
  3085. });
  3086. };
  3087. /**
  3088. * Generates a white texture based on the specified width and height
  3089. * @param width width of the texture in pixels
  3090. * @param height height of the texture in pixels
  3091. * @param scene babylonjs scene
  3092. * @returns white texture
  3093. */
  3094. _GLTFMaterialExporter.prototype._createWhiteTexture = function (width, height, scene) {
  3095. var data = new Uint8Array(width * height * 4);
  3096. for (var i = 0; i < data.length; i = i + 4) {
  3097. data[i] = data[i + 1] = data[i + 2] = data[i + 3] = 0xFF;
  3098. }
  3099. var rawTexture = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["RawTexture"].CreateRGBATexture(data, width, height, scene);
  3100. return rawTexture;
  3101. };
  3102. /**
  3103. * Resizes the two source textures to the same dimensions. If a texture is null, a default white texture is generated. If both textures are null, returns null
  3104. * @param texture1 first texture to resize
  3105. * @param texture2 second texture to resize
  3106. * @param scene babylonjs scene
  3107. * @returns resized textures or null
  3108. */
  3109. _GLTFMaterialExporter.prototype._resizeTexturesToSameDimensions = function (texture1, texture2, scene) {
  3110. var texture1Size = texture1 ? texture1.getSize() : { width: 0, height: 0 };
  3111. var texture2Size = texture2 ? texture2.getSize() : { width: 0, height: 0 };
  3112. var resizedTexture1;
  3113. var resizedTexture2;
  3114. if (texture1Size.width < texture2Size.width) {
  3115. if (texture1 && texture1 instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"]) {
  3116. resizedTexture1 = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["TextureTools"].CreateResizedCopy(texture1, texture2Size.width, texture2Size.height, true);
  3117. }
  3118. else {
  3119. resizedTexture1 = this._createWhiteTexture(texture2Size.width, texture2Size.height, scene);
  3120. }
  3121. resizedTexture2 = texture2;
  3122. }
  3123. else if (texture1Size.width > texture2Size.width) {
  3124. if (texture2 && texture2 instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"]) {
  3125. resizedTexture2 = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["TextureTools"].CreateResizedCopy(texture2, texture1Size.width, texture1Size.height, true);
  3126. }
  3127. else {
  3128. resizedTexture2 = this._createWhiteTexture(texture1Size.width, texture1Size.height, scene);
  3129. }
  3130. resizedTexture1 = texture1;
  3131. }
  3132. else {
  3133. resizedTexture1 = texture1;
  3134. resizedTexture2 = texture2;
  3135. }
  3136. return {
  3137. "texture1": resizedTexture1,
  3138. "texture2": resizedTexture2
  3139. };
  3140. };
  3141. /**
  3142. * Converts an array of pixels to a Float32Array
  3143. * Throws an error if the pixel format is not supported
  3144. * @param pixels - array buffer containing pixel values
  3145. * @returns Float32 of pixels
  3146. */
  3147. _GLTFMaterialExporter.prototype._convertPixelArrayToFloat32 = function (pixels) {
  3148. if (pixels instanceof Uint8Array) {
  3149. var length_1 = pixels.length;
  3150. var buffer = new Float32Array(pixels.length);
  3151. for (var i = 0; i < length_1; ++i) {
  3152. buffer[i] = pixels[i] / 255;
  3153. }
  3154. return buffer;
  3155. }
  3156. else if (pixels instanceof Float32Array) {
  3157. return pixels;
  3158. }
  3159. else {
  3160. throw new Error('Unsupported pixel format!');
  3161. }
  3162. };
  3163. /**
  3164. * Convert Specular Glossiness Textures to Metallic Roughness
  3165. * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
  3166. * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  3167. * @param diffuseTexture texture used to store diffuse information
  3168. * @param specularGlossinessTexture texture used to store specular and glossiness information
  3169. * @param factors specular glossiness material factors
  3170. * @param mimeType the mime type to use for the texture
  3171. * @returns pbr metallic roughness interface or null
  3172. */
  3173. _GLTFMaterialExporter.prototype._convertSpecularGlossinessTexturesToMetallicRoughnessAsync = function (diffuseTexture, specularGlossinessTexture, factors, mimeType) {
  3174. var promises = [];
  3175. if (!(diffuseTexture || specularGlossinessTexture)) {
  3176. return Promise.reject('_ConvertSpecularGlosinessTexturesToMetallicRoughness: diffuse and specular glossiness textures are not defined!');
  3177. }
  3178. var scene = diffuseTexture ? diffuseTexture.getScene() : specularGlossinessTexture ? specularGlossinessTexture.getScene() : null;
  3179. if (scene) {
  3180. var resizedTextures = this._resizeTexturesToSameDimensions(diffuseTexture, specularGlossinessTexture, scene);
  3181. var diffuseSize = resizedTextures.texture1.getSize();
  3182. var diffuseBuffer = void 0;
  3183. var specularGlossinessBuffer = void 0;
  3184. var width = diffuseSize.width;
  3185. var height = diffuseSize.height;
  3186. var diffusePixels = resizedTextures.texture1.readPixels();
  3187. var specularPixels = resizedTextures.texture2.readPixels();
  3188. if (diffusePixels) {
  3189. diffuseBuffer = this._convertPixelArrayToFloat32(diffusePixels);
  3190. }
  3191. else {
  3192. return Promise.reject("Failed to retrieve pixels from diffuse texture!");
  3193. }
  3194. if (specularPixels) {
  3195. specularGlossinessBuffer = this._convertPixelArrayToFloat32(specularPixels);
  3196. }
  3197. else {
  3198. return Promise.reject("Failed to retrieve pixels from specular glossiness texture!");
  3199. }
  3200. var byteLength = specularGlossinessBuffer.byteLength;
  3201. var metallicRoughnessBuffer = new Uint8Array(byteLength);
  3202. var baseColorBuffer = new Uint8Array(byteLength);
  3203. var strideSize = 4;
  3204. var maxBaseColor = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].Black();
  3205. var maxMetallic = 0;
  3206. var maxRoughness = 0;
  3207. for (var h = 0; h < height; ++h) {
  3208. for (var w = 0; w < width; ++w) {
  3209. var offset = (width * h + w) * strideSize;
  3210. var diffuseColor = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"](diffuseBuffer[offset], diffuseBuffer[offset + 1], diffuseBuffer[offset + 2]).toLinearSpace().multiply(factors.diffuseColor);
  3211. var specularColor = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"](specularGlossinessBuffer[offset], specularGlossinessBuffer[offset + 1], specularGlossinessBuffer[offset + 2]).toLinearSpace().multiply(factors.specularColor);
  3212. var glossiness = (specularGlossinessBuffer[offset + 3]) * factors.glossiness;
  3213. var specularGlossiness = {
  3214. diffuseColor: diffuseColor,
  3215. specularColor: specularColor,
  3216. glossiness: glossiness
  3217. };
  3218. var metallicRoughness = this._convertSpecularGlossinessToMetallicRoughness(specularGlossiness);
  3219. maxBaseColor.r = Math.max(maxBaseColor.r, metallicRoughness.baseColor.r);
  3220. maxBaseColor.g = Math.max(maxBaseColor.g, metallicRoughness.baseColor.g);
  3221. maxBaseColor.b = Math.max(maxBaseColor.b, metallicRoughness.baseColor.b);
  3222. maxMetallic = Math.max(maxMetallic, metallicRoughness.metallic);
  3223. maxRoughness = Math.max(maxRoughness, metallicRoughness.roughness);
  3224. baseColorBuffer[offset] = metallicRoughness.baseColor.r * 255;
  3225. baseColorBuffer[offset + 1] = metallicRoughness.baseColor.g * 255;
  3226. baseColorBuffer[offset + 2] = metallicRoughness.baseColor.b * 255;
  3227. baseColorBuffer[offset + 3] = resizedTextures.texture1.hasAlpha ? diffuseBuffer[offset + 3] * 255 : 255;
  3228. metallicRoughnessBuffer[offset] = 0;
  3229. metallicRoughnessBuffer[offset + 1] = metallicRoughness.roughness * 255;
  3230. metallicRoughnessBuffer[offset + 2] = metallicRoughness.metallic * 255;
  3231. metallicRoughnessBuffer[offset + 3] = 255;
  3232. }
  3233. }
  3234. // Retrieves the metallic roughness factors from the maximum texture values.
  3235. var metallicRoughnessFactors_1 = {
  3236. baseColor: maxBaseColor,
  3237. metallic: maxMetallic,
  3238. roughness: maxRoughness
  3239. };
  3240. var writeOutMetallicRoughnessTexture = false;
  3241. var writeOutBaseColorTexture = false;
  3242. for (var h = 0; h < height; ++h) {
  3243. for (var w = 0; w < width; ++w) {
  3244. var destinationOffset = (width * h + w) * strideSize;
  3245. baseColorBuffer[destinationOffset] /= metallicRoughnessFactors_1.baseColor.r > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.baseColor.r : 1;
  3246. baseColorBuffer[destinationOffset + 1] /= metallicRoughnessFactors_1.baseColor.g > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.baseColor.g : 1;
  3247. baseColorBuffer[destinationOffset + 2] /= metallicRoughnessFactors_1.baseColor.b > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.baseColor.b : 1;
  3248. var linearBaseColorPixel = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].FromInts(baseColorBuffer[destinationOffset], baseColorBuffer[destinationOffset + 1], baseColorBuffer[destinationOffset + 2]);
  3249. var sRGBBaseColorPixel = linearBaseColorPixel.toGammaSpace();
  3250. baseColorBuffer[destinationOffset] = sRGBBaseColorPixel.r * 255;
  3251. baseColorBuffer[destinationOffset + 1] = sRGBBaseColorPixel.g * 255;
  3252. baseColorBuffer[destinationOffset + 2] = sRGBBaseColorPixel.b * 255;
  3253. if (!_GLTFMaterialExporter.FuzzyEquals(sRGBBaseColorPixel, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].White(), _GLTFMaterialExporter._Epsilon)) {
  3254. writeOutBaseColorTexture = true;
  3255. }
  3256. metallicRoughnessBuffer[destinationOffset + 1] /= metallicRoughnessFactors_1.roughness > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.roughness : 1;
  3257. metallicRoughnessBuffer[destinationOffset + 2] /= metallicRoughnessFactors_1.metallic > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.metallic : 1;
  3258. var metallicRoughnessPixel = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].FromInts(255, metallicRoughnessBuffer[destinationOffset + 1], metallicRoughnessBuffer[destinationOffset + 2]);
  3259. if (!_GLTFMaterialExporter.FuzzyEquals(metallicRoughnessPixel, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].White(), _GLTFMaterialExporter._Epsilon)) {
  3260. writeOutMetallicRoughnessTexture = true;
  3261. }
  3262. }
  3263. }
  3264. if (writeOutMetallicRoughnessTexture) {
  3265. var promise = this._createBase64FromCanvasAsync(metallicRoughnessBuffer, width, height, mimeType).then(function (metallicRoughnessBase64) {
  3266. metallicRoughnessFactors_1.metallicRoughnessTextureBase64 = metallicRoughnessBase64;
  3267. });
  3268. promises.push(promise);
  3269. }
  3270. if (writeOutBaseColorTexture) {
  3271. var promise = this._createBase64FromCanvasAsync(baseColorBuffer, width, height, mimeType).then(function (baseColorBase64) {
  3272. metallicRoughnessFactors_1.baseColorTextureBase64 = baseColorBase64;
  3273. });
  3274. promises.push(promise);
  3275. }
  3276. return Promise.all(promises).then(function () {
  3277. return metallicRoughnessFactors_1;
  3278. });
  3279. }
  3280. else {
  3281. return Promise.reject("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!");
  3282. }
  3283. };
  3284. /**
  3285. * Converts specular glossiness material properties to metallic roughness
  3286. * @param specularGlossiness interface with specular glossiness material properties
  3287. * @returns interface with metallic roughness material properties
  3288. */
  3289. _GLTFMaterialExporter.prototype._convertSpecularGlossinessToMetallicRoughness = function (specularGlossiness) {
  3290. var diffusePerceivedBrightness = this._getPerceivedBrightness(specularGlossiness.diffuseColor);
  3291. var specularPerceivedBrightness = this._getPerceivedBrightness(specularGlossiness.specularColor);
  3292. var oneMinusSpecularStrength = 1 - this._getMaxComponent(specularGlossiness.specularColor);
  3293. var metallic = _GLTFMaterialExporter._SolveMetallic(diffusePerceivedBrightness, specularPerceivedBrightness, oneMinusSpecularStrength);
  3294. var baseColorFromDiffuse = specularGlossiness.diffuseColor.scale(oneMinusSpecularStrength / (1.0 - _GLTFMaterialExporter._DielectricSpecular.r) / Math.max(1 - metallic, _GLTFMaterialExporter._Epsilon));
  3295. var baseColorFromSpecular = specularGlossiness.specularColor.subtract(_GLTFMaterialExporter._DielectricSpecular.scale(1 - metallic)).scale(1 / Math.max(metallic, _GLTFMaterialExporter._Epsilon));
  3296. var baseColor = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].Lerp(baseColorFromDiffuse, baseColorFromSpecular, metallic * metallic);
  3297. baseColor = baseColor.clampToRef(0, 1, baseColor);
  3298. var metallicRoughness = {
  3299. baseColor: baseColor,
  3300. metallic: metallic,
  3301. roughness: 1 - specularGlossiness.glossiness
  3302. };
  3303. return metallicRoughness;
  3304. };
  3305. /**
  3306. * Calculates the surface reflectance, independent of lighting conditions
  3307. * @param color Color source to calculate brightness from
  3308. * @returns number representing the perceived brightness, or zero if color is undefined
  3309. */
  3310. _GLTFMaterialExporter.prototype._getPerceivedBrightness = function (color) {
  3311. if (color) {
  3312. return Math.sqrt(0.299 * color.r * color.r + 0.587 * color.g * color.g + 0.114 * color.b * color.b);
  3313. }
  3314. return 0;
  3315. };
  3316. /**
  3317. * Returns the maximum color component value
  3318. * @param color
  3319. * @returns maximum color component value, or zero if color is null or undefined
  3320. */
  3321. _GLTFMaterialExporter.prototype._getMaxComponent = function (color) {
  3322. if (color) {
  3323. return Math.max(color.r, Math.max(color.g, color.b));
  3324. }
  3325. return 0;
  3326. };
  3327. /**
  3328. * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
  3329. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  3330. * @param mimeType mime type to use for the textures
  3331. * @param images array of glTF image interfaces
  3332. * @param textures array of glTF texture interfaces
  3333. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  3334. * @param imageData map of image file name to data
  3335. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  3336. * @returns glTF PBR Metallic Roughness factors
  3337. */
  3338. _GLTFMaterialExporter.prototype._convertMetalRoughFactorsToMetallicRoughnessAsync = function (babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords) {
  3339. var promises = [];
  3340. var metallicRoughness = {
  3341. baseColor: babylonPBRMaterial.albedoColor,
  3342. metallic: babylonPBRMaterial.metallic,
  3343. roughness: babylonPBRMaterial.roughness
  3344. };
  3345. if (hasTextureCoords) {
  3346. if (babylonPBRMaterial.albedoTexture) {
  3347. promises.push(this._exportTextureAsync(babylonPBRMaterial.albedoTexture, mimeType).then(function (glTFTexture) {
  3348. if (glTFTexture) {
  3349. glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
  3350. }
  3351. }));
  3352. }
  3353. if (babylonPBRMaterial.metallicTexture) {
  3354. promises.push(this._exportTextureAsync(babylonPBRMaterial.metallicTexture, mimeType).then(function (glTFTexture) {
  3355. if (glTFTexture) {
  3356. glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFTexture;
  3357. }
  3358. }));
  3359. }
  3360. }
  3361. return Promise.all(promises).then(function () {
  3362. return metallicRoughness;
  3363. });
  3364. };
  3365. _GLTFMaterialExporter.prototype._getGLTFTextureSampler = function (texture) {
  3366. var sampler = this._getGLTFTextureWrapModesSampler(texture);
  3367. var samplingMode = texture instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"] ? texture.samplingMode : null;
  3368. if (samplingMode != null) {
  3369. switch (samplingMode) {
  3370. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].LINEAR_LINEAR: {
  3371. sampler.magFilter = 9729 /* LINEAR */;
  3372. sampler.minFilter = 9729 /* LINEAR */;
  3373. break;
  3374. }
  3375. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].LINEAR_NEAREST: {
  3376. sampler.magFilter = 9729 /* LINEAR */;
  3377. sampler.minFilter = 9728 /* NEAREST */;
  3378. break;
  3379. }
  3380. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].NEAREST_LINEAR: {
  3381. sampler.magFilter = 9728 /* NEAREST */;
  3382. sampler.minFilter = 9729 /* LINEAR */;
  3383. break;
  3384. }
  3385. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].NEAREST_LINEAR_MIPLINEAR: {
  3386. sampler.magFilter = 9728 /* NEAREST */;
  3387. sampler.minFilter = 9987 /* LINEAR_MIPMAP_LINEAR */;
  3388. break;
  3389. }
  3390. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].NEAREST_NEAREST: {
  3391. sampler.magFilter = 9728 /* NEAREST */;
  3392. sampler.minFilter = 9728 /* NEAREST */;
  3393. break;
  3394. }
  3395. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].NEAREST_LINEAR_MIPNEAREST: {
  3396. sampler.magFilter = 9728 /* NEAREST */;
  3397. sampler.minFilter = 9985 /* LINEAR_MIPMAP_NEAREST */;
  3398. break;
  3399. }
  3400. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].LINEAR_NEAREST_MIPNEAREST: {
  3401. sampler.magFilter = 9729 /* LINEAR */;
  3402. sampler.minFilter = 9984 /* NEAREST_MIPMAP_NEAREST */;
  3403. break;
  3404. }
  3405. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].LINEAR_NEAREST_MIPLINEAR: {
  3406. sampler.magFilter = 9729 /* LINEAR */;
  3407. sampler.minFilter = 9986 /* NEAREST_MIPMAP_LINEAR */;
  3408. break;
  3409. }
  3410. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].NEAREST_NEAREST_MIPLINEAR: {
  3411. sampler.magFilter = 9728 /* NEAREST */;
  3412. sampler.minFilter = 9986 /* NEAREST_MIPMAP_LINEAR */;
  3413. break;
  3414. }
  3415. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].LINEAR_LINEAR_MIPLINEAR: {
  3416. sampler.magFilter = 9729 /* LINEAR */;
  3417. sampler.minFilter = 9987 /* LINEAR_MIPMAP_LINEAR */;
  3418. break;
  3419. }
  3420. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].LINEAR_LINEAR_MIPNEAREST: {
  3421. sampler.magFilter = 9729 /* LINEAR */;
  3422. sampler.minFilter = 9985 /* LINEAR_MIPMAP_NEAREST */;
  3423. break;
  3424. }
  3425. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].NEAREST_NEAREST_MIPNEAREST: {
  3426. sampler.magFilter = 9728 /* NEAREST */;
  3427. sampler.minFilter = 9984 /* NEAREST_MIPMAP_NEAREST */;
  3428. break;
  3429. }
  3430. }
  3431. }
  3432. return sampler;
  3433. };
  3434. _GLTFMaterialExporter.prototype._getGLTFTextureWrapMode = function (wrapMode) {
  3435. switch (wrapMode) {
  3436. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].WRAP_ADDRESSMODE: {
  3437. return 10497 /* REPEAT */;
  3438. }
  3439. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].CLAMP_ADDRESSMODE: {
  3440. return 33071 /* CLAMP_TO_EDGE */;
  3441. }
  3442. case babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].MIRROR_ADDRESSMODE: {
  3443. return 33648 /* MIRRORED_REPEAT */;
  3444. }
  3445. default: {
  3446. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("Unsupported Texture Wrap Mode " + wrapMode + "!");
  3447. return 10497 /* REPEAT */;
  3448. }
  3449. }
  3450. };
  3451. _GLTFMaterialExporter.prototype._getGLTFTextureWrapModesSampler = function (texture) {
  3452. var wrapS = this._getGLTFTextureWrapMode(texture instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"] ? texture.wrapU : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].WRAP_ADDRESSMODE);
  3453. var wrapT = this._getGLTFTextureWrapMode(texture instanceof babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"] ? texture.wrapV : babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Texture"].WRAP_ADDRESSMODE);
  3454. if (wrapS === 10497 /* REPEAT */ && wrapT === 10497 /* REPEAT */) { // default wrapping mode in glTF, so omitting
  3455. return {};
  3456. }
  3457. return { wrapS: wrapS, wrapT: wrapT };
  3458. };
  3459. /**
  3460. * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors
  3461. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  3462. * @param mimeType mime type to use for the textures
  3463. * @param images array of glTF image interfaces
  3464. * @param textures array of glTF texture interfaces
  3465. * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
  3466. * @param imageData map of image file name to data
  3467. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  3468. * @returns glTF PBR Metallic Roughness factors
  3469. */
  3470. _GLTFMaterialExporter.prototype._convertSpecGlossFactorsToMetallicRoughnessAsync = function (babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords) {
  3471. var _this = this;
  3472. return Promise.resolve().then(function () {
  3473. var samplers = _this._exporter._samplers;
  3474. var textures = _this._exporter._textures;
  3475. var specGloss = {
  3476. diffuseColor: babylonPBRMaterial.albedoColor || babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].White(),
  3477. specularColor: babylonPBRMaterial.reflectivityColor || babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].White(),
  3478. glossiness: babylonPBRMaterial.microSurface || 1,
  3479. };
  3480. var samplerIndex = null;
  3481. var sampler = _this._getGLTFTextureSampler(babylonPBRMaterial.albedoTexture);
  3482. if (sampler.magFilter != null && sampler.minFilter != null && sampler.wrapS != null && sampler.wrapT != null) {
  3483. samplers.push(sampler);
  3484. samplerIndex = samplers.length - 1;
  3485. }
  3486. if (babylonPBRMaterial.reflectivityTexture && !babylonPBRMaterial.useMicroSurfaceFromReflectivityMapAlpha) {
  3487. return Promise.reject("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture are currently not supported");
  3488. }
  3489. if ((babylonPBRMaterial.albedoTexture || babylonPBRMaterial.reflectivityTexture) && hasTextureCoords) {
  3490. return _this._convertSpecularGlossinessTexturesToMetallicRoughnessAsync(babylonPBRMaterial.albedoTexture, babylonPBRMaterial.reflectivityTexture, specGloss, mimeType).then(function (metallicRoughnessFactors) {
  3491. if (metallicRoughnessFactors.baseColorTextureBase64) {
  3492. var glTFBaseColorTexture = _this._getTextureInfoFromBase64(metallicRoughnessFactors.baseColorTextureBase64, "bjsBaseColorTexture_" + (textures.length) + ".png", mimeType, babylonPBRMaterial.albedoTexture ? babylonPBRMaterial.albedoTexture.coordinatesIndex : null, samplerIndex);
  3493. if (glTFBaseColorTexture) {
  3494. glTFPbrMetallicRoughness.baseColorTexture = glTFBaseColorTexture;
  3495. }
  3496. }
  3497. if (metallicRoughnessFactors.metallicRoughnessTextureBase64) {
  3498. var glTFMRColorTexture = _this._getTextureInfoFromBase64(metallicRoughnessFactors.metallicRoughnessTextureBase64, "bjsMetallicRoughnessTexture_" + (textures.length) + ".png", mimeType, babylonPBRMaterial.reflectivityTexture ? babylonPBRMaterial.reflectivityTexture.coordinatesIndex : null, samplerIndex);
  3499. if (glTFMRColorTexture) {
  3500. glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFMRColorTexture;
  3501. }
  3502. }
  3503. return metallicRoughnessFactors;
  3504. });
  3505. }
  3506. else {
  3507. return _this._convertSpecularGlossinessToMetallicRoughness(specGloss);
  3508. }
  3509. });
  3510. };
  3511. /**
  3512. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
  3513. * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
  3514. * @param mimeType mime type to use for the textures
  3515. * @param images array of glTF image interfaces
  3516. * @param textures array of glTF texture interfaces
  3517. * @param materials array of glTF material interfaces
  3518. * @param imageData map of image file name to data
  3519. * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
  3520. */
  3521. _GLTFMaterialExporter.prototype._convertPBRMaterialAsync = function (babylonPBRMaterial, mimeType, hasTextureCoords) {
  3522. var _this = this;
  3523. var glTFPbrMetallicRoughness = {};
  3524. var glTFMaterial = {
  3525. name: babylonPBRMaterial.name
  3526. };
  3527. var useMetallicRoughness = babylonPBRMaterial.isMetallicWorkflow();
  3528. if (useMetallicRoughness) {
  3529. if (babylonPBRMaterial.albedoColor) {
  3530. glTFPbrMetallicRoughness.baseColorFactor = [
  3531. babylonPBRMaterial.albedoColor.r,
  3532. babylonPBRMaterial.albedoColor.g,
  3533. babylonPBRMaterial.albedoColor.b,
  3534. babylonPBRMaterial.alpha
  3535. ];
  3536. }
  3537. return this._convertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords).then(function (metallicRoughness) {
  3538. return _this.setMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords);
  3539. });
  3540. }
  3541. else {
  3542. return this._convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords).then(function (metallicRoughness) {
  3543. return _this.setMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords);
  3544. });
  3545. }
  3546. };
  3547. _GLTFMaterialExporter.prototype.setMetallicRoughnessPbrMaterial = function (metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords) {
  3548. var materialMap = this._exporter._materialMap;
  3549. var materials = this._exporter._materials;
  3550. var promises = [];
  3551. if (metallicRoughness) {
  3552. var alphaMode = null;
  3553. if (babylonPBRMaterial.transparencyMode != null) {
  3554. alphaMode = this._getAlphaMode(babylonPBRMaterial);
  3555. if (alphaMode) {
  3556. if (alphaMode !== "OPAQUE" /* OPAQUE */) { //glTF defaults to opaque
  3557. glTFMaterial.alphaMode = alphaMode;
  3558. if (alphaMode === "MASK" /* MASK */) {
  3559. glTFMaterial.alphaCutoff = babylonPBRMaterial.alphaCutOff;
  3560. }
  3561. }
  3562. }
  3563. }
  3564. if (!(_GLTFMaterialExporter.FuzzyEquals(metallicRoughness.baseColor, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].White(), _GLTFMaterialExporter._Epsilon) && babylonPBRMaterial.alpha >= _GLTFMaterialExporter._Epsilon)) {
  3565. glTFPbrMetallicRoughness.baseColorFactor = [
  3566. metallicRoughness.baseColor.r,
  3567. metallicRoughness.baseColor.g,
  3568. metallicRoughness.baseColor.b,
  3569. babylonPBRMaterial.alpha
  3570. ];
  3571. }
  3572. if (metallicRoughness.metallic != null && metallicRoughness.metallic !== 1) {
  3573. glTFPbrMetallicRoughness.metallicFactor = metallicRoughness.metallic;
  3574. }
  3575. if (metallicRoughness.roughness != null && metallicRoughness.roughness !== 1) {
  3576. glTFPbrMetallicRoughness.roughnessFactor = metallicRoughness.roughness;
  3577. }
  3578. if (babylonPBRMaterial.backFaceCulling != null && !babylonPBRMaterial.backFaceCulling) {
  3579. if (!babylonPBRMaterial.twoSidedLighting) {
  3580. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Warn(babylonPBRMaterial.name + ": Back-face culling enabled and two-sided lighting disabled is not supported in glTF.");
  3581. }
  3582. glTFMaterial.doubleSided = true;
  3583. }
  3584. if (hasTextureCoords) {
  3585. if (babylonPBRMaterial.bumpTexture) {
  3586. var promise = this._exportTextureAsync(babylonPBRMaterial.bumpTexture, mimeType).then(function (glTFTexture) {
  3587. if (glTFTexture) {
  3588. glTFMaterial.normalTexture = glTFTexture;
  3589. if (babylonPBRMaterial.bumpTexture.level !== 1) {
  3590. glTFMaterial.normalTexture.scale = babylonPBRMaterial.bumpTexture.level;
  3591. }
  3592. }
  3593. });
  3594. promises.push(promise);
  3595. }
  3596. if (babylonPBRMaterial.ambientTexture) {
  3597. var promise = this._exportTextureAsync(babylonPBRMaterial.ambientTexture, mimeType).then(function (glTFTexture) {
  3598. if (glTFTexture) {
  3599. var occlusionTexture = {
  3600. index: glTFTexture.index
  3601. };
  3602. glTFMaterial.occlusionTexture = occlusionTexture;
  3603. if (babylonPBRMaterial.ambientTextureStrength) {
  3604. occlusionTexture.strength = babylonPBRMaterial.ambientTextureStrength;
  3605. }
  3606. }
  3607. });
  3608. promises.push(promise);
  3609. }
  3610. if (babylonPBRMaterial.emissiveTexture) {
  3611. var promise = this._exportTextureAsync(babylonPBRMaterial.emissiveTexture, mimeType).then(function (glTFTexture) {
  3612. if (glTFTexture) {
  3613. glTFMaterial.emissiveTexture = glTFTexture;
  3614. }
  3615. });
  3616. promises.push(promise);
  3617. }
  3618. }
  3619. if (!_GLTFMaterialExporter.FuzzyEquals(babylonPBRMaterial.emissiveColor, babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"].Black(), _GLTFMaterialExporter._Epsilon)) {
  3620. glTFMaterial.emissiveFactor = babylonPBRMaterial.emissiveColor.asArray();
  3621. }
  3622. glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
  3623. materials.push(glTFMaterial);
  3624. materialMap[babylonPBRMaterial.uniqueId] = materials.length - 1;
  3625. }
  3626. return Promise.all(promises).then(function (result) { });
  3627. };
  3628. _GLTFMaterialExporter.prototype.getPixelsFromTexture = function (babylonTexture) {
  3629. var pixels = babylonTexture.textureType === babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Engine"].TEXTURETYPE_UNSIGNED_INT ? babylonTexture.readPixels() : babylonTexture.readPixels();
  3630. return pixels;
  3631. };
  3632. /**
  3633. * Extracts a texture from a Babylon texture into file data and glTF data
  3634. * @param babylonTexture Babylon texture to extract
  3635. * @param mimeType Mime Type of the babylonTexture
  3636. * @return glTF texture info, or null if the texture format is not supported
  3637. */
  3638. _GLTFMaterialExporter.prototype._exportTextureAsync = function (babylonTexture, mimeType) {
  3639. var _this = this;
  3640. var extensionPromise = this._exporter._extensionsPreExportTextureAsync("exporter", babylonTexture, mimeType);
  3641. if (!extensionPromise) {
  3642. return this._exportTextureInfoAsync(babylonTexture, mimeType);
  3643. }
  3644. return extensionPromise.then(function (texture) {
  3645. if (!texture) {
  3646. return _this._exportTextureInfoAsync(babylonTexture, mimeType);
  3647. }
  3648. return _this._exportTextureInfoAsync(texture, mimeType);
  3649. });
  3650. };
  3651. _GLTFMaterialExporter.prototype._exportTextureInfoAsync = function (babylonTexture, mimeType) {
  3652. var _this = this;
  3653. return Promise.resolve().then(function () {
  3654. var textureUid = babylonTexture.uid;
  3655. if (textureUid in _this._textureMap) {
  3656. return _this._textureMap[textureUid];
  3657. }
  3658. else {
  3659. var samplers = _this._exporter._samplers;
  3660. var sampler = _this._getGLTFTextureSampler(babylonTexture);
  3661. var samplerIndex_1 = null;
  3662. // if a pre-existing sampler with identical parameters exists, then reuse the previous sampler
  3663. var foundSamplerIndex = null;
  3664. for (var i = 0; i < samplers.length; ++i) {
  3665. var s = samplers[i];
  3666. if (s.minFilter === sampler.minFilter && s.magFilter === sampler.magFilter &&
  3667. s.wrapS === sampler.wrapS && s.wrapT === sampler.wrapT) {
  3668. foundSamplerIndex = i;
  3669. break;
  3670. }
  3671. }
  3672. if (foundSamplerIndex == null) {
  3673. samplers.push(sampler);
  3674. samplerIndex_1 = samplers.length - 1;
  3675. }
  3676. else {
  3677. samplerIndex_1 = foundSamplerIndex;
  3678. }
  3679. var pixels = _this.getPixelsFromTexture(babylonTexture);
  3680. var size = babylonTexture.getSize();
  3681. return _this._createBase64FromCanvasAsync(pixels, size.width, size.height, mimeType).then(function (base64Data) {
  3682. var textureInfo = _this._getTextureInfoFromBase64(base64Data, babylonTexture.name.replace(/\.\/|\/|\.\\|\\/g, "_"), mimeType, babylonTexture.coordinatesIndex, samplerIndex_1);
  3683. if (textureInfo) {
  3684. _this._textureMap[textureUid] = textureInfo;
  3685. }
  3686. return textureInfo;
  3687. });
  3688. }
  3689. });
  3690. };
  3691. /**
  3692. * Builds a texture from base64 string
  3693. * @param base64Texture base64 texture string
  3694. * @param baseTextureName Name to use for the texture
  3695. * @param mimeType image mime type for the texture
  3696. * @param images array of images
  3697. * @param textures array of textures
  3698. * @param imageData map of image data
  3699. * @returns glTF texture info, or null if the texture format is not supported
  3700. */
  3701. _GLTFMaterialExporter.prototype._getTextureInfoFromBase64 = function (base64Texture, baseTextureName, mimeType, texCoordIndex, samplerIndex) {
  3702. var textures = this._exporter._textures;
  3703. var images = this._exporter._images;
  3704. var imageData = this._exporter._imageData;
  3705. var textureInfo = null;
  3706. var glTFTexture = {
  3707. source: images.length,
  3708. name: baseTextureName
  3709. };
  3710. if (samplerIndex != null) {
  3711. glTFTexture.sampler = samplerIndex;
  3712. }
  3713. var binStr = atob(base64Texture.split(',')[1]);
  3714. var arrBuff = new ArrayBuffer(binStr.length);
  3715. var arr = new Uint8Array(arrBuff);
  3716. for (var i = 0, length_2 = binStr.length; i < length_2; ++i) {
  3717. arr[i] = binStr.charCodeAt(i);
  3718. }
  3719. var imageValues = { data: arr, mimeType: mimeType };
  3720. var extension = mimeType === "image/jpeg" /* JPEG */ ? '.jpeg' : '.png';
  3721. var textureName = baseTextureName + extension;
  3722. if (textureName in imageData) {
  3723. textureName = baseTextureName + "_" + babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].RandomId() + extension;
  3724. }
  3725. imageData[textureName] = imageValues;
  3726. if (mimeType === "image/jpeg" /* JPEG */ || mimeType === "image/png" /* PNG */) {
  3727. var glTFImage = {
  3728. name: baseTextureName,
  3729. uri: textureName
  3730. };
  3731. var foundIndex = null;
  3732. for (var i = 0; i < images.length; ++i) {
  3733. if (images[i].uri === textureName) {
  3734. foundIndex = i;
  3735. break;
  3736. }
  3737. }
  3738. if (foundIndex == null) {
  3739. images.push(glTFImage);
  3740. glTFTexture.source = images.length - 1;
  3741. }
  3742. else {
  3743. glTFTexture.source = foundIndex;
  3744. }
  3745. textures.push(glTFTexture);
  3746. textureInfo = {
  3747. index: textures.length - 1
  3748. };
  3749. if (texCoordIndex != null) {
  3750. textureInfo.texCoord = texCoordIndex;
  3751. }
  3752. }
  3753. else {
  3754. babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("Unsupported texture mime type " + mimeType);
  3755. }
  3756. return textureInfo;
  3757. };
  3758. /**
  3759. * Represents the dielectric specular values for R, G and B
  3760. */
  3761. _GLTFMaterialExporter._DielectricSpecular = new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Color3"](0.04, 0.04, 0.04);
  3762. /**
  3763. * Allows the maximum specular power to be defined for material calculations
  3764. */
  3765. _GLTFMaterialExporter._MaxSpecularPower = 1024;
  3766. /**
  3767. * Numeric tolerance value
  3768. */
  3769. _GLTFMaterialExporter._Epsilon = 1e-6;
  3770. return _GLTFMaterialExporter;
  3771. }());
  3772. /***/ }),
  3773. /***/ "./glTF/2.0/glTFSerializer.ts":
  3774. /*!************************************!*\
  3775. !*** ./glTF/2.0/glTFSerializer.ts ***!
  3776. \************************************/
  3777. /*! exports provided: GLTF2Export */
  3778. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  3779. "use strict";
  3780. __webpack_require__.r(__webpack_exports__);
  3781. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "GLTF2Export", function() { return GLTF2Export; });
  3782. /* harmony import */ var _glTFExporter__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./glTFExporter */ "./glTF/2.0/glTFExporter.ts");
  3783. /**
  3784. * Class for generating glTF data from a Babylon scene.
  3785. */
  3786. var GLTF2Export = /** @class */ (function () {
  3787. function GLTF2Export() {
  3788. }
  3789. /**
  3790. * Exports the geometry of the scene to .gltf file format asynchronously
  3791. * @param scene Babylon scene with scene hierarchy information
  3792. * @param filePrefix File prefix to use when generating the glTF file
  3793. * @param options Exporter options
  3794. * @returns Returns an object with a .gltf file and associates texture names
  3795. * as keys and their data and paths as values
  3796. */
  3797. GLTF2Export.GLTFAsync = function (scene, filePrefix, options) {
  3798. return scene.whenReadyAsync().then(function () {
  3799. var glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  3800. var gltfGenerator = new _glTFExporter__WEBPACK_IMPORTED_MODULE_0__["_Exporter"](scene, options);
  3801. return gltfGenerator._generateGLTFAsync(glTFPrefix);
  3802. });
  3803. };
  3804. GLTF2Export._PreExportAsync = function (scene, options) {
  3805. return Promise.resolve().then(function () {
  3806. if (options && options.exportWithoutWaitingForScene) {
  3807. return Promise.resolve();
  3808. }
  3809. else {
  3810. return scene.whenReadyAsync();
  3811. }
  3812. });
  3813. };
  3814. GLTF2Export._PostExportAsync = function (scene, glTFData, options) {
  3815. return Promise.resolve().then(function () {
  3816. if (options && options.exportWithoutWaitingForScene) {
  3817. return glTFData;
  3818. }
  3819. else {
  3820. return glTFData;
  3821. }
  3822. });
  3823. };
  3824. /**
  3825. * Exports the geometry of the scene to .glb file format asychronously
  3826. * @param scene Babylon scene with scene hierarchy information
  3827. * @param filePrefix File prefix to use when generating glb file
  3828. * @param options Exporter options
  3829. * @returns Returns an object with a .glb filename as key and data as value
  3830. */
  3831. GLTF2Export.GLBAsync = function (scene, filePrefix, options) {
  3832. var _this = this;
  3833. return this._PreExportAsync(scene, options).then(function () {
  3834. var glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  3835. var gltfGenerator = new _glTFExporter__WEBPACK_IMPORTED_MODULE_0__["_Exporter"](scene, options);
  3836. return gltfGenerator._generateGLBAsync(glTFPrefix).then(function (glTFData) {
  3837. return _this._PostExportAsync(scene, glTFData, options);
  3838. });
  3839. });
  3840. };
  3841. return GLTF2Export;
  3842. }());
  3843. /***/ }),
  3844. /***/ "./glTF/2.0/glTFUtilities.ts":
  3845. /*!***********************************!*\
  3846. !*** ./glTF/2.0/glTFUtilities.ts ***!
  3847. \***********************************/
  3848. /*! exports provided: _GLTFUtilities */
  3849. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  3850. "use strict";
  3851. __webpack_require__.r(__webpack_exports__);
  3852. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "_GLTFUtilities", function() { return _GLTFUtilities; });
  3853. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Maths/math */ "babylonjs/Maths/math");
  3854. /* harmony import */ var babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__);
  3855. /**
  3856. * @hidden
  3857. */
  3858. var _GLTFUtilities = /** @class */ (function () {
  3859. function _GLTFUtilities() {
  3860. }
  3861. /**
  3862. * Creates a buffer view based on the supplied arguments
  3863. * @param bufferIndex index value of the specified buffer
  3864. * @param byteOffset byte offset value
  3865. * @param byteLength byte length of the bufferView
  3866. * @param byteStride byte distance between conequential elements
  3867. * @param name name of the buffer view
  3868. * @returns bufferView for glTF
  3869. */
  3870. _GLTFUtilities._CreateBufferView = function (bufferIndex, byteOffset, byteLength, byteStride, name) {
  3871. var bufferview = { buffer: bufferIndex, byteLength: byteLength };
  3872. if (byteOffset) {
  3873. bufferview.byteOffset = byteOffset;
  3874. }
  3875. if (name) {
  3876. bufferview.name = name;
  3877. }
  3878. if (byteStride) {
  3879. bufferview.byteStride = byteStride;
  3880. }
  3881. return bufferview;
  3882. };
  3883. /**
  3884. * Creates an accessor based on the supplied arguments
  3885. * @param bufferviewIndex The index of the bufferview referenced by this accessor
  3886. * @param name The name of the accessor
  3887. * @param type The type of the accessor
  3888. * @param componentType The datatype of components in the attribute
  3889. * @param count The number of attributes referenced by this accessor
  3890. * @param byteOffset The offset relative to the start of the bufferView in bytes
  3891. * @param min Minimum value of each component in this attribute
  3892. * @param max Maximum value of each component in this attribute
  3893. * @returns accessor for glTF
  3894. */
  3895. _GLTFUtilities._CreateAccessor = function (bufferviewIndex, name, type, componentType, count, byteOffset, min, max) {
  3896. var accessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
  3897. if (min != null) {
  3898. accessor.min = min;
  3899. }
  3900. if (max != null) {
  3901. accessor.max = max;
  3902. }
  3903. if (byteOffset != null) {
  3904. accessor.byteOffset = byteOffset;
  3905. }
  3906. return accessor;
  3907. };
  3908. /**
  3909. * Calculates the minimum and maximum values of an array of position floats
  3910. * @param positions Positions array of a mesh
  3911. * @param vertexStart Starting vertex offset to calculate min and max values
  3912. * @param vertexCount Number of vertices to check for min and max values
  3913. * @returns min number array and max number array
  3914. */
  3915. _GLTFUtilities._CalculateMinMaxPositions = function (positions, vertexStart, vertexCount, convertToRightHandedSystem) {
  3916. var min = [Infinity, Infinity, Infinity];
  3917. var max = [-Infinity, -Infinity, -Infinity];
  3918. var positionStrideSize = 3;
  3919. var indexOffset;
  3920. var position;
  3921. var vector;
  3922. if (vertexCount) {
  3923. for (var i = vertexStart, length_1 = vertexStart + vertexCount; i < length_1; ++i) {
  3924. indexOffset = positionStrideSize * i;
  3925. position = babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"].FromArray(positions, indexOffset);
  3926. if (convertToRightHandedSystem) {
  3927. _GLTFUtilities._GetRightHandedPositionVector3FromRef(position);
  3928. }
  3929. vector = position.asArray();
  3930. for (var j = 0; j < positionStrideSize; ++j) {
  3931. var num = vector[j];
  3932. if (num < min[j]) {
  3933. min[j] = num;
  3934. }
  3935. if (num > max[j]) {
  3936. max[j] = num;
  3937. }
  3938. ++indexOffset;
  3939. }
  3940. }
  3941. }
  3942. return { min: min, max: max };
  3943. };
  3944. /**
  3945. * Converts a new right-handed Vector3
  3946. * @param vector vector3 array
  3947. * @returns right-handed Vector3
  3948. */
  3949. _GLTFUtilities._GetRightHandedPositionVector3 = function (vector) {
  3950. return new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"](vector.x, vector.y, -vector.z);
  3951. };
  3952. /**
  3953. * Converts a Vector3 to right-handed
  3954. * @param vector Vector3 to convert to right-handed
  3955. */
  3956. _GLTFUtilities._GetRightHandedPositionVector3FromRef = function (vector) {
  3957. vector.z *= -1;
  3958. };
  3959. /**
  3960. * Converts a three element number array to right-handed
  3961. * @param vector number array to convert to right-handed
  3962. */
  3963. _GLTFUtilities._GetRightHandedPositionArray3FromRef = function (vector) {
  3964. vector[2] *= -1;
  3965. };
  3966. /**
  3967. * Converts a new right-handed Vector3
  3968. * @param vector vector3 array
  3969. * @returns right-handed Vector3
  3970. */
  3971. _GLTFUtilities._GetRightHandedNormalVector3 = function (vector) {
  3972. return new babylonjs_Maths_math__WEBPACK_IMPORTED_MODULE_0__["Vector3"](vector.x, vector.y, -vector.z);
  3973. };
  3974. /**
  3975. * Converts a Vector3 to right-handed
  3976. * @param vector Vector3 to convert to right-handed
  3977. */
  3978. _GLTFUtilities._GetRightHandedNormalVector3FromRef = function (vector) {
  3979. vector.z *= -1;
  3980. };
  3981. /**
  3982. * Converts a three element number array to right-handed
  3983. * @param vector number array to convert to right-handed
  3984. */
  3985. _GLTFUtilities._GetRightHandedNormalArray3FromRef = function (vector) {
  3986. vector[2] *= -1;
  3987. };
  3988. /**
  3989. * Converts a Vector4 to right-handed
  3990. * @param vector Vector4 to convert to right-handed
  3991. */
  3992. _GLTFUtilities._GetRightHandedVector4FromRef = function (vector) {
  3993. vector.z *= -1;
  3994. vector.w *= -1;
  3995. };
  3996. /**
  3997. * Converts a Vector4 to right-handed
  3998. * @param vector Vector4 to convert to right-handed
  3999. */
  4000. _GLTFUtilities._GetRightHandedArray4FromRef = function (vector) {
  4001. vector[2] *= -1;
  4002. vector[3] *= -1;
  4003. };
  4004. /**
  4005. * Converts a Quaternion to right-handed
  4006. * @param quaternion Source quaternion to convert to right-handed
  4007. */
  4008. _GLTFUtilities._GetRightHandedQuaternionFromRef = function (quaternion) {
  4009. quaternion.x *= -1;
  4010. quaternion.y *= -1;
  4011. };
  4012. /**
  4013. * Converts a Quaternion to right-handed
  4014. * @param quaternion Source quaternion to convert to right-handed
  4015. */
  4016. _GLTFUtilities._GetRightHandedQuaternionArrayFromRef = function (quaternion) {
  4017. quaternion[0] *= -1;
  4018. quaternion[1] *= -1;
  4019. };
  4020. _GLTFUtilities._NormalizeTangentFromRef = function (tangent) {
  4021. var length = Math.sqrt(tangent.x * tangent.x + tangent.y * tangent.y + tangent.z * tangent.z);
  4022. if (length > 0) {
  4023. tangent.x /= length;
  4024. tangent.y /= length;
  4025. tangent.z /= length;
  4026. }
  4027. };
  4028. return _GLTFUtilities;
  4029. }());
  4030. /***/ }),
  4031. /***/ "./glTF/2.0/index.ts":
  4032. /*!***************************!*\
  4033. !*** ./glTF/2.0/index.ts ***!
  4034. \***************************/
  4035. /*! exports provided: GLTFData, GLTF2Export, _GLTFAnimation, _Exporter, _BinaryWriter, __IGLTFExporterExtensionV2, _GLTFMaterialExporter, _GLTFUtilities, KHR_texture_transform, KHR_lights_punctual */
  4036. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4037. "use strict";
  4038. __webpack_require__.r(__webpack_exports__);
  4039. /* harmony import */ var _glTFAnimation__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./glTFAnimation */ "./glTF/2.0/glTFAnimation.ts");
  4040. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFAnimation", function() { return _glTFAnimation__WEBPACK_IMPORTED_MODULE_0__["_GLTFAnimation"]; });
  4041. /* harmony import */ var _glTFData__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./glTFData */ "./glTF/2.0/glTFData.ts");
  4042. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTFData", function() { return _glTFData__WEBPACK_IMPORTED_MODULE_1__["GLTFData"]; });
  4043. /* harmony import */ var _glTFExporter__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./glTFExporter */ "./glTF/2.0/glTFExporter.ts");
  4044. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_Exporter", function() { return _glTFExporter__WEBPACK_IMPORTED_MODULE_2__["_Exporter"]; });
  4045. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_BinaryWriter", function() { return _glTFExporter__WEBPACK_IMPORTED_MODULE_2__["_BinaryWriter"]; });
  4046. /* harmony import */ var _glTFExporterExtension__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./glTFExporterExtension */ "./glTF/2.0/glTFExporterExtension.ts");
  4047. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtensionV2", function() { return _glTFExporterExtension__WEBPACK_IMPORTED_MODULE_3__["__IGLTFExporterExtensionV2"]; });
  4048. /* harmony import */ var _glTFMaterialExporter__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./glTFMaterialExporter */ "./glTF/2.0/glTFMaterialExporter.ts");
  4049. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFMaterialExporter", function() { return _glTFMaterialExporter__WEBPACK_IMPORTED_MODULE_4__["_GLTFMaterialExporter"]; });
  4050. /* harmony import */ var _glTFSerializer__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./glTFSerializer */ "./glTF/2.0/glTFSerializer.ts");
  4051. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTF2Export", function() { return _glTFSerializer__WEBPACK_IMPORTED_MODULE_5__["GLTF2Export"]; });
  4052. /* harmony import */ var _glTFUtilities__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./glTFUtilities */ "./glTF/2.0/glTFUtilities.ts");
  4053. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFUtilities", function() { return _glTFUtilities__WEBPACK_IMPORTED_MODULE_6__["_GLTFUtilities"]; });
  4054. /* harmony import */ var _Extensions__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./Extensions */ "./glTF/2.0/Extensions/index.ts");
  4055. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_texture_transform", function() { return _Extensions__WEBPACK_IMPORTED_MODULE_7__["KHR_texture_transform"]; });
  4056. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_lights_punctual", function() { return _Extensions__WEBPACK_IMPORTED_MODULE_7__["KHR_lights_punctual"]; });
  4057. /***/ }),
  4058. /***/ "./glTF/2.0/shaders/textureTransform.fragment.ts":
  4059. /*!*******************************************************!*\
  4060. !*** ./glTF/2.0/shaders/textureTransform.fragment.ts ***!
  4061. \*******************************************************/
  4062. /*! exports provided: textureTransformPixelShader */
  4063. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4064. "use strict";
  4065. __webpack_require__.r(__webpack_exports__);
  4066. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "textureTransformPixelShader", function() { return textureTransformPixelShader; });
  4067. /* harmony import */ var babylonjs_Materials_effect__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Materials/effect */ "babylonjs/Maths/math");
  4068. /* harmony import */ var babylonjs_Materials_effect__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Materials_effect__WEBPACK_IMPORTED_MODULE_0__);
  4069. var name = 'textureTransformPixelShader';
  4070. var shader = "precision highp float;\nvarying vec2 vUV;\nuniform sampler2D textureSampler;\nuniform mat4 textureTransformMat;\nvoid main(void) {\nvec2 uvTransformed=(textureTransformMat*vec4(vUV.xy,1,1)).xy;\ngl_FragColor=texture2D(textureSampler,uvTransformed);\n}";
  4071. babylonjs_Materials_effect__WEBPACK_IMPORTED_MODULE_0__["Effect"].ShadersStore[name] = shader;
  4072. /** @hidden */
  4073. var textureTransformPixelShader = { name: name, shader: shader };
  4074. /***/ }),
  4075. /***/ "./glTF/glTFFileExporter.ts":
  4076. /*!**********************************!*\
  4077. !*** ./glTF/glTFFileExporter.ts ***!
  4078. \**********************************/
  4079. /*! exports provided: __IGLTFExporterExtension */
  4080. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4081. "use strict";
  4082. __webpack_require__.r(__webpack_exports__);
  4083. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtension", function() { return __IGLTFExporterExtension; });
  4084. /** @hidden */
  4085. var __IGLTFExporterExtension = 0; // I am here to allow dts to be created
  4086. /***/ }),
  4087. /***/ "./glTF/index.ts":
  4088. /*!***********************!*\
  4089. !*** ./glTF/index.ts ***!
  4090. \***********************/
  4091. /*! exports provided: __IGLTFExporterExtension, GLTFData, GLTF2Export, _GLTFAnimation, _Exporter, _BinaryWriter, __IGLTFExporterExtensionV2, _GLTFMaterialExporter, _GLTFUtilities, KHR_texture_transform, KHR_lights_punctual */
  4092. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4093. "use strict";
  4094. __webpack_require__.r(__webpack_exports__);
  4095. /* harmony import */ var _glTFFileExporter__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./glTFFileExporter */ "./glTF/glTFFileExporter.ts");
  4096. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtension", function() { return _glTFFileExporter__WEBPACK_IMPORTED_MODULE_0__["__IGLTFExporterExtension"]; });
  4097. /* harmony import */ var _2_0__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./2.0 */ "./glTF/2.0/index.ts");
  4098. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTFData", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["GLTFData"]; });
  4099. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTF2Export", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["GLTF2Export"]; });
  4100. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFAnimation", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["_GLTFAnimation"]; });
  4101. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_Exporter", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["_Exporter"]; });
  4102. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_BinaryWriter", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["_BinaryWriter"]; });
  4103. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtensionV2", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["__IGLTFExporterExtensionV2"]; });
  4104. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFMaterialExporter", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["_GLTFMaterialExporter"]; });
  4105. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFUtilities", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]; });
  4106. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_texture_transform", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["KHR_texture_transform"]; });
  4107. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_lights_punctual", function() { return _2_0__WEBPACK_IMPORTED_MODULE_1__["KHR_lights_punctual"]; });
  4108. /***/ }),
  4109. /***/ "./index.ts":
  4110. /*!******************!*\
  4111. !*** ./index.ts ***!
  4112. \******************/
  4113. /*! exports provided: __IGLTFExporterExtension, GLTFData, GLTF2Export, OBJExport, _GLTFAnimation, _Exporter, _BinaryWriter, __IGLTFExporterExtensionV2, _GLTFMaterialExporter, _GLTFUtilities, STLExport, KHR_texture_transform, KHR_lights_punctual */
  4114. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4115. "use strict";
  4116. __webpack_require__.r(__webpack_exports__);
  4117. /* harmony import */ var _OBJ__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./OBJ */ "./OBJ/index.ts");
  4118. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "OBJExport", function() { return _OBJ__WEBPACK_IMPORTED_MODULE_0__["OBJExport"]; });
  4119. /* harmony import */ var _glTF__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./glTF */ "./glTF/index.ts");
  4120. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtension", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["__IGLTFExporterExtension"]; });
  4121. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTFData", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["GLTFData"]; });
  4122. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTF2Export", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["GLTF2Export"]; });
  4123. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFAnimation", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["_GLTFAnimation"]; });
  4124. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_Exporter", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["_Exporter"]; });
  4125. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_BinaryWriter", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["_BinaryWriter"]; });
  4126. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtensionV2", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["__IGLTFExporterExtensionV2"]; });
  4127. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFMaterialExporter", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["_GLTFMaterialExporter"]; });
  4128. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFUtilities", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]; });
  4129. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_texture_transform", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["KHR_texture_transform"]; });
  4130. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_lights_punctual", function() { return _glTF__WEBPACK_IMPORTED_MODULE_1__["KHR_lights_punctual"]; });
  4131. /* harmony import */ var _stl__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./stl */ "./stl/index.ts");
  4132. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "STLExport", function() { return _stl__WEBPACK_IMPORTED_MODULE_2__["STLExport"]; });
  4133. /***/ }),
  4134. /***/ "./legacy/legacy-glTF2Serializer.ts":
  4135. /*!******************************************!*\
  4136. !*** ./legacy/legacy-glTF2Serializer.ts ***!
  4137. \******************************************/
  4138. /*! exports provided: __IGLTFExporterExtension, GLTFData, GLTF2Export, _GLTFAnimation, _Exporter, _BinaryWriter, __IGLTFExporterExtensionV2, _GLTFMaterialExporter, _GLTFUtilities, KHR_texture_transform, KHR_lights_punctual */
  4139. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4140. "use strict";
  4141. __webpack_require__.r(__webpack_exports__);
  4142. /* WEBPACK VAR INJECTION */(function(global) {/* harmony import */ var _glTF_glTFFileExporter__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../glTF/glTFFileExporter */ "./glTF/glTFFileExporter.ts");
  4143. /* harmony import */ var _glTF_2_0_glTFData__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../glTF/2.0/glTFData */ "./glTF/2.0/glTFData.ts");
  4144. /* harmony import */ var _glTF_2_0_glTFSerializer__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../glTF/2.0/glTFSerializer */ "./glTF/2.0/glTFSerializer.ts");
  4145. /* harmony import */ var _glTF_2_0_Extensions__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../glTF/2.0/Extensions */ "./glTF/2.0/Extensions/index.ts");
  4146. /* harmony import */ var _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../glTF/2.0 */ "./glTF/2.0/index.ts");
  4147. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtension", function() { return _glTF_glTFFileExporter__WEBPACK_IMPORTED_MODULE_0__["__IGLTFExporterExtension"]; });
  4148. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTFData", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["GLTFData"]; });
  4149. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTF2Export", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["GLTF2Export"]; });
  4150. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFAnimation", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["_GLTFAnimation"]; });
  4151. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_Exporter", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["_Exporter"]; });
  4152. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_BinaryWriter", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["_BinaryWriter"]; });
  4153. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtensionV2", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["__IGLTFExporterExtensionV2"]; });
  4154. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFMaterialExporter", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["_GLTFMaterialExporter"]; });
  4155. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFUtilities", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["_GLTFUtilities"]; });
  4156. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_texture_transform", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["KHR_texture_transform"]; });
  4157. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_lights_punctual", function() { return _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__["KHR_lights_punctual"]; });
  4158. /**
  4159. * This is the entry point for the UMD module.
  4160. * The entry point for a future ESM package should be index.ts
  4161. */
  4162. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  4163. if (typeof globalObject !== "undefined") {
  4164. globalObject.BABYLON = globalObject.BABYLON || {};
  4165. var BABYLON = globalObject.BABYLON;
  4166. BABYLON.GLTF2 = BABYLON.GLTF2 || {};
  4167. BABYLON.GLTF2.Exporter = BABYLON.GLTF2.Exporter || {};
  4168. BABYLON.GLTF2.Exporter.Extensions = BABYLON.GLTF2.Exporter.Extensions || {};
  4169. var keys = [];
  4170. for (var key in _glTF_glTFFileExporter__WEBPACK_IMPORTED_MODULE_0__) {
  4171. BABYLON[key] = _glTF_glTFFileExporter__WEBPACK_IMPORTED_MODULE_0__[key];
  4172. keys.push(key);
  4173. }
  4174. for (var key in _glTF_2_0_glTFData__WEBPACK_IMPORTED_MODULE_1__) {
  4175. BABYLON[key] = _glTF_2_0_glTFData__WEBPACK_IMPORTED_MODULE_1__[key];
  4176. keys.push(key);
  4177. }
  4178. for (var key in _glTF_2_0_glTFSerializer__WEBPACK_IMPORTED_MODULE_2__) {
  4179. BABYLON[key] = _glTF_2_0_glTFSerializer__WEBPACK_IMPORTED_MODULE_2__[key];
  4180. keys.push(key);
  4181. }
  4182. for (var key in _glTF_2_0_Extensions__WEBPACK_IMPORTED_MODULE_3__) {
  4183. BABYLON.GLTF2.Exporter.Extensions[key] = _glTF_2_0_Extensions__WEBPACK_IMPORTED_MODULE_3__[key];
  4184. keys.push(key);
  4185. }
  4186. for (var key in _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__) {
  4187. // Prevent Reassignment.
  4188. if (keys.indexOf(key) > -1) {
  4189. continue;
  4190. }
  4191. BABYLON.GLTF2.Exporter[key] = _glTF_2_0__WEBPACK_IMPORTED_MODULE_4__[key];
  4192. }
  4193. }
  4194. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../../node_modules/webpack/buildin/global.js */ "../../node_modules/webpack/buildin/global.js")))
  4195. /***/ }),
  4196. /***/ "./legacy/legacy-objSerializer.ts":
  4197. /*!****************************************!*\
  4198. !*** ./legacy/legacy-objSerializer.ts ***!
  4199. \****************************************/
  4200. /*! exports provided: OBJExport */
  4201. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4202. "use strict";
  4203. __webpack_require__.r(__webpack_exports__);
  4204. /* WEBPACK VAR INJECTION */(function(global) {/* harmony import */ var _OBJ__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../OBJ */ "./OBJ/index.ts");
  4205. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "OBJExport", function() { return _OBJ__WEBPACK_IMPORTED_MODULE_0__["OBJExport"]; });
  4206. /**
  4207. * This is the entry point for the UMD module.
  4208. * The entry point for a future ESM package should be index.ts
  4209. */
  4210. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  4211. if (typeof globalObject !== "undefined") {
  4212. for (var serializer in _OBJ__WEBPACK_IMPORTED_MODULE_0__) {
  4213. globalObject.BABYLON[serializer] = _OBJ__WEBPACK_IMPORTED_MODULE_0__[serializer];
  4214. }
  4215. }
  4216. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../../node_modules/webpack/buildin/global.js */ "../../node_modules/webpack/buildin/global.js")))
  4217. /***/ }),
  4218. /***/ "./legacy/legacy-stlSerializer.ts":
  4219. /*!****************************************!*\
  4220. !*** ./legacy/legacy-stlSerializer.ts ***!
  4221. \****************************************/
  4222. /*! exports provided: STLExport */
  4223. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4224. "use strict";
  4225. __webpack_require__.r(__webpack_exports__);
  4226. /* WEBPACK VAR INJECTION */(function(global) {/* harmony import */ var _stl__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../stl */ "./stl/index.ts");
  4227. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "STLExport", function() { return _stl__WEBPACK_IMPORTED_MODULE_0__["STLExport"]; });
  4228. /**
  4229. * This is the entry point for the UMD module.
  4230. * The entry point for a future ESM package should be index.ts
  4231. */
  4232. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  4233. if (typeof globalObject !== "undefined") {
  4234. for (var serializer in _stl__WEBPACK_IMPORTED_MODULE_0__) {
  4235. globalObject.BABYLON[serializer] = _stl__WEBPACK_IMPORTED_MODULE_0__[serializer];
  4236. }
  4237. }
  4238. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../../node_modules/webpack/buildin/global.js */ "../../node_modules/webpack/buildin/global.js")))
  4239. /***/ }),
  4240. /***/ "./legacy/legacy.ts":
  4241. /*!**************************!*\
  4242. !*** ./legacy/legacy.ts ***!
  4243. \**************************/
  4244. /*! exports provided: __IGLTFExporterExtension, GLTFData, GLTF2Export, _GLTFAnimation, _Exporter, _BinaryWriter, __IGLTFExporterExtensionV2, _GLTFMaterialExporter, _GLTFUtilities, OBJExport, STLExport, KHR_texture_transform, KHR_lights_punctual */
  4245. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4246. "use strict";
  4247. __webpack_require__.r(__webpack_exports__);
  4248. /* harmony import */ var _index__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../index */ "./index.ts");
  4249. /* harmony import */ var _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./legacy-glTF2Serializer */ "./legacy/legacy-glTF2Serializer.ts");
  4250. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtension", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["__IGLTFExporterExtension"]; });
  4251. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTFData", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["GLTFData"]; });
  4252. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GLTF2Export", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["GLTF2Export"]; });
  4253. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFAnimation", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["_GLTFAnimation"]; });
  4254. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_Exporter", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["_Exporter"]; });
  4255. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_BinaryWriter", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["_BinaryWriter"]; });
  4256. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "__IGLTFExporterExtensionV2", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["__IGLTFExporterExtensionV2"]; });
  4257. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFMaterialExporter", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["_GLTFMaterialExporter"]; });
  4258. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "_GLTFUtilities", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["_GLTFUtilities"]; });
  4259. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_texture_transform", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["KHR_texture_transform"]; });
  4260. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "KHR_lights_punctual", function() { return _legacy_glTF2Serializer__WEBPACK_IMPORTED_MODULE_1__["KHR_lights_punctual"]; });
  4261. /* harmony import */ var _legacy_objSerializer__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./legacy-objSerializer */ "./legacy/legacy-objSerializer.ts");
  4262. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "OBJExport", function() { return _legacy_objSerializer__WEBPACK_IMPORTED_MODULE_2__["OBJExport"]; });
  4263. /* harmony import */ var _legacy_stlSerializer__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./legacy-stlSerializer */ "./legacy/legacy-stlSerializer.ts");
  4264. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "STLExport", function() { return _legacy_stlSerializer__WEBPACK_IMPORTED_MODULE_3__["STLExport"]; });
  4265. /***/ }),
  4266. /***/ "./stl/index.ts":
  4267. /*!**********************!*\
  4268. !*** ./stl/index.ts ***!
  4269. \**********************/
  4270. /*! exports provided: STLExport */
  4271. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4272. "use strict";
  4273. __webpack_require__.r(__webpack_exports__);
  4274. /* harmony import */ var _stlSerializer__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./stlSerializer */ "./stl/stlSerializer.ts");
  4275. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "STLExport", function() { return _stlSerializer__WEBPACK_IMPORTED_MODULE_0__["STLExport"]; });
  4276. /***/ }),
  4277. /***/ "./stl/stlSerializer.ts":
  4278. /*!******************************!*\
  4279. !*** ./stl/stlSerializer.ts ***!
  4280. \******************************/
  4281. /*! exports provided: STLExport */
  4282. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  4283. "use strict";
  4284. __webpack_require__.r(__webpack_exports__);
  4285. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "STLExport", function() { return STLExport; });
  4286. /* harmony import */ var babylonjs_Meshes_buffer__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Meshes/buffer */ "babylonjs/Maths/math");
  4287. /* harmony import */ var babylonjs_Meshes_buffer__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Meshes_buffer__WEBPACK_IMPORTED_MODULE_0__);
  4288. /**
  4289. * Class for generating STL data from a Babylon scene.
  4290. */
  4291. var STLExport = /** @class */ (function () {
  4292. function STLExport() {
  4293. }
  4294. /**
  4295. * Exports the geometry of a Mesh array in .STL file format (ASCII)
  4296. * @param meshes list defines the mesh to serialize
  4297. * @param download triggers the automatic download of the file.
  4298. * @param fileName changes the downloads fileName.
  4299. * @param binary changes the STL to a binary type.
  4300. * @param isLittleEndian toggle for binary type exporter.
  4301. * @returns the STL as UTF8 string
  4302. */
  4303. STLExport.CreateSTL = function (meshes, download, fileName, binary, isLittleEndian) {
  4304. //Binary support adapted from https://gist.github.com/paulkaplan/6d5f0ab2c7e8fdc68a61
  4305. if (download === void 0) { download = true; }
  4306. if (fileName === void 0) { fileName = 'STL_Mesh'; }
  4307. if (binary === void 0) { binary = false; }
  4308. if (isLittleEndian === void 0) { isLittleEndian = true; }
  4309. var getFaceData = function (indices, vertices, i) {
  4310. var id = [indices[i] * 3, indices[i + 1] * 3, indices[i + 2] * 3];
  4311. var v = [
  4312. new babylonjs_Meshes_buffer__WEBPACK_IMPORTED_MODULE_0__["Vector3"](vertices[id[0]], vertices[id[0] + 1], vertices[id[0] + 2]),
  4313. new babylonjs_Meshes_buffer__WEBPACK_IMPORTED_MODULE_0__["Vector3"](vertices[id[1]], vertices[id[1] + 1], vertices[id[1] + 2]),
  4314. new babylonjs_Meshes_buffer__WEBPACK_IMPORTED_MODULE_0__["Vector3"](vertices[id[2]], vertices[id[2] + 1], vertices[id[2] + 2])
  4315. ];
  4316. var p1p2 = v[0].subtract(v[1]);
  4317. var p3p2 = v[2].subtract(v[1]);
  4318. var n = (babylonjs_Meshes_buffer__WEBPACK_IMPORTED_MODULE_0__["Vector3"].Cross(p1p2, p3p2)).normalize();
  4319. return { v: v, n: n };
  4320. };
  4321. var writeVector = function (dataview, offset, vector, isLittleEndian) {
  4322. offset = writeFloat(dataview, offset, vector.x, isLittleEndian);
  4323. offset = writeFloat(dataview, offset, vector.y, isLittleEndian);
  4324. return writeFloat(dataview, offset, vector.z, isLittleEndian);
  4325. };
  4326. var writeFloat = function (dataview, offset, value, isLittleEndian) {
  4327. dataview.setFloat32(offset, value, isLittleEndian);
  4328. return offset + 4;
  4329. };
  4330. var data;
  4331. var faceCount = 0;
  4332. var offset = 0;
  4333. if (binary) {
  4334. for (var i = 0; i < meshes.length; i++) {
  4335. var mesh = meshes[i];
  4336. var indices = mesh.getIndices();
  4337. faceCount += indices ? indices.length : 0;
  4338. }
  4339. var bufferSize = 84 + (50 * faceCount);
  4340. var buffer = new ArrayBuffer(bufferSize);
  4341. data = new DataView(buffer);
  4342. offset += 80;
  4343. data.setUint32(offset, faceCount, isLittleEndian);
  4344. offset += 4;
  4345. }
  4346. else {
  4347. data = 'solid exportedMesh\r\n';
  4348. }
  4349. for (var i = 0; i < meshes.length; i++) {
  4350. var mesh = meshes[i];
  4351. mesh.bakeCurrentTransformIntoVertices();
  4352. var vertices = mesh.getVerticesData(babylonjs_Meshes_buffer__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind) || [];
  4353. var indices = mesh.getIndices() || [];
  4354. for (var i_1 = 0; i_1 < indices.length; i_1 += 3) {
  4355. var fd = getFaceData(indices, vertices, i_1);
  4356. if (binary) {
  4357. offset = writeVector(data, offset, fd.n, isLittleEndian);
  4358. offset = writeVector(data, offset, fd.v[0], isLittleEndian);
  4359. offset = writeVector(data, offset, fd.v[1], isLittleEndian);
  4360. offset = writeVector(data, offset, fd.v[2], isLittleEndian);
  4361. offset += 2;
  4362. }
  4363. else {
  4364. data += 'facet normal ' + fd.n.x + ' ' + fd.n.y + ' ' + fd.n.z + '\r\n';
  4365. data += '\touter loop\r\n';
  4366. data += '\t\tvertex ' + fd.v[0].x + ' ' + fd.v[0].y + ' ' + fd.v[0].z + '\r\n';
  4367. data += '\t\tvertex ' + fd.v[1].x + ' ' + fd.v[1].y + ' ' + fd.v[1].z + '\r\n';
  4368. data += '\t\tvertex ' + fd.v[2].x + ' ' + fd.v[2].y + ' ' + fd.v[2].z + '\r\n';
  4369. data += '\tendloop\r\n';
  4370. data += 'endfacet\r\n';
  4371. }
  4372. }
  4373. }
  4374. if (!binary) {
  4375. data += 'endsolid exportedMesh';
  4376. }
  4377. if (download) {
  4378. var a = document.createElement('a');
  4379. var blob = new Blob([data], { 'type': 'application/octet-stream' });
  4380. a.href = window.URL.createObjectURL(blob);
  4381. if (!fileName) {
  4382. fileName = "STL_Mesh";
  4383. }
  4384. a.download = fileName + ".stl";
  4385. a.click();
  4386. }
  4387. return data;
  4388. };
  4389. return STLExport;
  4390. }());
  4391. /***/ }),
  4392. /***/ "babylonjs/Maths/math":
  4393. /*!****************************************************************************************************!*\
  4394. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  4395. \****************************************************************************************************/
  4396. /*! no static exports found */
  4397. /***/ (function(module, exports) {
  4398. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs_Maths_math__;
  4399. /***/ })
  4400. /******/ });
  4401. });
  4402. //# sourceMappingURL=babylonjs.serializers.js.map