babylonjs.serializers.js 245 KB

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