babylonjs.loaders.js 297 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853
  1. (function universalModuleDefinition(root, factory) {
  2. var amdDependencies = [];
  3. var BABYLON = root.BABYLON || this.BABYLON;
  4. if(typeof exports === 'object' && typeof module === 'object') {
  5. BABYLON = BABYLON || require("babylonjs");
  6. module.exports = factory(BABYLON);
  7. } else if(typeof define === 'function' && define.amd) {
  8. amdDependencies.push("babylonjs");
  9. define("babylonjs-loaders", amdDependencies, factory);
  10. } else if(typeof exports === 'object') {
  11. BABYLON = BABYLON || require("babylonjs");
  12. exports["babylonjs-loaders"] = factory(BABYLON);
  13. } else {
  14. root["BABYLON"] = factory(BABYLON);
  15. }
  16. })(this, function(BABYLON) {
  17. BABYLON = BABYLON || this.BABYLON;
  18. var __decorate=this&&this.__decorate||function(e,t,r,c){var o,f=arguments.length,n=f<3?t:null===c?c=Object.getOwnPropertyDescriptor(t,r):c;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,r,c);else for(var l=e.length-1;l>=0;l--)(o=e[l])&&(n=(f<3?o(n):f>3?o(t,r,n):o(t,r))||n);return f>3&&n&&Object.defineProperty(t,r,n),n};
  19. var __extends=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,o){t.__proto__=o}||function(t,o){for(var n in o)o.hasOwnProperty(n)&&(t[n]=o[n])};return function(o,n){function r(){this.constructor=o}t(o,n),o.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}();
  20. var BABYLON;
  21. (function (BABYLON) {
  22. var STLFileLoader = /** @class */ (function () {
  23. function STLFileLoader() {
  24. this.solidPattern = /solid (\S*)([\S\s]*)endsolid[ ]*(\S*)/g;
  25. this.facetsPattern = /facet([\s\S]*?)endfacet/g;
  26. this.normalPattern = /normal[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
  27. this.vertexPattern = /vertex[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
  28. this.name = "stl";
  29. // force data to come in as an ArrayBuffer
  30. // we'll convert to string if it looks like it's an ASCII .stl
  31. this.extensions = {
  32. ".stl": { isBinary: true },
  33. };
  34. }
  35. STLFileLoader.prototype.importMesh = function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
  36. var matches;
  37. if (this.isBinary(data)) {
  38. // binary .stl
  39. var babylonMesh = new BABYLON.Mesh("stlmesh", scene);
  40. this.parseBinary(babylonMesh, data);
  41. if (meshes) {
  42. meshes.push(babylonMesh);
  43. }
  44. return true;
  45. }
  46. // ASCII .stl
  47. // convert to string
  48. var array_buffer = new Uint8Array(data);
  49. var str = '';
  50. for (var i = 0; i < data.byteLength; i++) {
  51. str += String.fromCharCode(array_buffer[i]); // implicitly assumes little-endian
  52. }
  53. data = str;
  54. while (matches = this.solidPattern.exec(data)) {
  55. var meshName = matches[1];
  56. var meshNameFromEnd = matches[3];
  57. if (meshName != meshNameFromEnd) {
  58. BABYLON.Tools.Error("Error in STL, solid name != endsolid name");
  59. return false;
  60. }
  61. // check meshesNames
  62. if (meshesNames && meshName) {
  63. if (meshesNames instanceof Array) {
  64. if (!meshesNames.indexOf(meshName)) {
  65. continue;
  66. }
  67. }
  68. else {
  69. if (meshName !== meshesNames) {
  70. continue;
  71. }
  72. }
  73. }
  74. // stl mesh name can be empty as well
  75. meshName = meshName || "stlmesh";
  76. var babylonMesh = new BABYLON.Mesh(meshName, scene);
  77. this.parseASCII(babylonMesh, matches[2]);
  78. if (meshes) {
  79. meshes.push(babylonMesh);
  80. }
  81. }
  82. return true;
  83. };
  84. STLFileLoader.prototype.load = function (scene, data, rootUrl) {
  85. var result = this.importMesh(null, scene, data, rootUrl, null, null, null);
  86. if (result) {
  87. scene.createDefaultCameraOrLight();
  88. }
  89. return result;
  90. };
  91. STLFileLoader.prototype.loadAssetContainer = function (scene, data, rootUrl, onError) {
  92. var container = new BABYLON.AssetContainer(scene);
  93. this.importMesh(null, scene, data, rootUrl, container.meshes, null, null);
  94. container.removeAllFromScene();
  95. return container;
  96. };
  97. STLFileLoader.prototype.isBinary = function (data) {
  98. // check if file size is correct for binary stl
  99. var faceSize, nFaces, reader;
  100. reader = new DataView(data);
  101. faceSize = (32 / 8 * 3) + ((32 / 8 * 3) * 3) + (16 / 8);
  102. nFaces = reader.getUint32(80, true);
  103. if (80 + (32 / 8) + (nFaces * faceSize) === reader.byteLength) {
  104. return true;
  105. }
  106. // check characters higher than ASCII to confirm binary
  107. var fileLength = reader.byteLength;
  108. for (var index = 0; index < fileLength; index++) {
  109. if (reader.getUint8(index) > 127) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. };
  115. STLFileLoader.prototype.parseBinary = function (mesh, data) {
  116. var reader = new DataView(data);
  117. var faces = reader.getUint32(80, true);
  118. var dataOffset = 84;
  119. var faceLength = 12 * 4 + 2;
  120. var offset = 0;
  121. var positions = new Float32Array(faces * 3 * 3);
  122. var normals = new Float32Array(faces * 3 * 3);
  123. var indices = new Uint32Array(faces * 3);
  124. var indicesCount = 0;
  125. for (var face = 0; face < faces; face++) {
  126. var start = dataOffset + face * faceLength;
  127. var normalX = reader.getFloat32(start, true);
  128. var normalY = reader.getFloat32(start + 4, true);
  129. var normalZ = reader.getFloat32(start + 8, true);
  130. for (var i = 1; i <= 3; i++) {
  131. var vertexstart = start + i * 12;
  132. // ordering is intentional to match ascii import
  133. positions[offset] = reader.getFloat32(vertexstart, true);
  134. positions[offset + 2] = reader.getFloat32(vertexstart + 4, true);
  135. positions[offset + 1] = reader.getFloat32(vertexstart + 8, true);
  136. normals[offset] = normalX;
  137. normals[offset + 2] = normalY;
  138. normals[offset + 1] = normalZ;
  139. offset += 3;
  140. }
  141. indices[indicesCount] = indicesCount++;
  142. indices[indicesCount] = indicesCount++;
  143. indices[indicesCount] = indicesCount++;
  144. }
  145. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
  146. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
  147. mesh.setIndices(indices);
  148. mesh.computeWorldMatrix(true);
  149. };
  150. STLFileLoader.prototype.parseASCII = function (mesh, solidData) {
  151. var positions = [];
  152. var normals = [];
  153. var indices = [];
  154. var indicesCount = 0;
  155. //load facets, ignoring loop as the standard doesn't define it can contain more than vertices
  156. var matches;
  157. while (matches = this.facetsPattern.exec(solidData)) {
  158. var facet = matches[1];
  159. //one normal per face
  160. var normalMatches = this.normalPattern.exec(facet);
  161. this.normalPattern.lastIndex = 0;
  162. if (!normalMatches) {
  163. continue;
  164. }
  165. var normal = [Number(normalMatches[1]), Number(normalMatches[5]), Number(normalMatches[3])];
  166. var vertexMatch;
  167. while (vertexMatch = this.vertexPattern.exec(facet)) {
  168. positions.push(Number(vertexMatch[1]), Number(vertexMatch[5]), Number(vertexMatch[3]));
  169. normals.push(normal[0], normal[1], normal[2]);
  170. }
  171. indices.push(indicesCount++, indicesCount++, indicesCount++);
  172. this.vertexPattern.lastIndex = 0;
  173. }
  174. this.facetsPattern.lastIndex = 0;
  175. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
  176. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
  177. mesh.setIndices(indices);
  178. mesh.computeWorldMatrix(true);
  179. };
  180. return STLFileLoader;
  181. }());
  182. BABYLON.STLFileLoader = STLFileLoader;
  183. if (BABYLON.SceneLoader) {
  184. BABYLON.SceneLoader.RegisterPlugin(new STLFileLoader());
  185. }
  186. })(BABYLON || (BABYLON = {}));
  187. //# sourceMappingURL=babylon.stlFileLoader.js.map
  188. var BABYLON;
  189. (function (BABYLON) {
  190. /**
  191. * Class reading and parsing the MTL file bundled with the obj file.
  192. */
  193. var MTLFileLoader = /** @class */ (function () {
  194. function MTLFileLoader() {
  195. // All material loaded from the mtl will be set here
  196. this.materials = [];
  197. }
  198. /**
  199. * This function will read the mtl file and create each material described inside
  200. * This function could be improve by adding :
  201. * -some component missing (Ni, Tf...)
  202. * -including the specific options available
  203. *
  204. * @param scene
  205. * @param data
  206. * @param rootUrl
  207. */
  208. MTLFileLoader.prototype.parseMTL = function (scene, data, rootUrl) {
  209. if (data instanceof ArrayBuffer) {
  210. return;
  211. }
  212. //Split the lines from the file
  213. var lines = data.split('\n');
  214. //Space char
  215. var delimiter_pattern = /\s+/;
  216. //Array with RGB colors
  217. var color;
  218. //New material
  219. var material = null;
  220. //Look at each line
  221. for (var i = 0; i < lines.length; i++) {
  222. var line = lines[i].trim();
  223. // Blank line or comment
  224. if (line.length === 0 || line.charAt(0) === '#') {
  225. continue;
  226. }
  227. //Get the first parameter (keyword)
  228. var pos = line.indexOf(' ');
  229. var key = (pos >= 0) ? line.substring(0, pos) : line;
  230. key = key.toLowerCase();
  231. //Get the data following the key
  232. var value = (pos >= 0) ? line.substring(pos + 1).trim() : "";
  233. //This mtl keyword will create the new material
  234. if (key === "newmtl") {
  235. //Check if it is the first material.
  236. // Materials specifications are described after this keyword.
  237. if (material) {
  238. //Add the previous material in the material array.
  239. this.materials.push(material);
  240. }
  241. //Create a new material.
  242. // value is the name of the material read in the mtl file
  243. material = new BABYLON.StandardMaterial(value, scene);
  244. }
  245. else if (key === "kd" && material) {
  246. // Diffuse color (color under white light) using RGB values
  247. //value = "r g b"
  248. color = value.split(delimiter_pattern, 3).map(parseFloat);
  249. //color = [r,g,b]
  250. //Set tghe color into the material
  251. material.diffuseColor = BABYLON.Color3.FromArray(color);
  252. }
  253. else if (key === "ka" && material) {
  254. // Ambient color (color under shadow) using RGB values
  255. //value = "r g b"
  256. color = value.split(delimiter_pattern, 3).map(parseFloat);
  257. //color = [r,g,b]
  258. //Set tghe color into the material
  259. material.ambientColor = BABYLON.Color3.FromArray(color);
  260. }
  261. else if (key === "ks" && material) {
  262. // Specular color (color when light is reflected from shiny surface) using RGB values
  263. //value = "r g b"
  264. color = value.split(delimiter_pattern, 3).map(parseFloat);
  265. //color = [r,g,b]
  266. //Set the color into the material
  267. material.specularColor = BABYLON.Color3.FromArray(color);
  268. }
  269. else if (key === "ke" && material) {
  270. // Emissive color using RGB values
  271. color = value.split(delimiter_pattern, 3).map(parseFloat);
  272. material.emissiveColor = BABYLON.Color3.FromArray(color);
  273. }
  274. else if (key === "ns" && material) {
  275. //value = "Integer"
  276. material.specularPower = parseFloat(value);
  277. }
  278. else if (key === "d" && material) {
  279. //d is dissolve for current material. It mean alpha for BABYLON
  280. material.alpha = parseFloat(value);
  281. //Texture
  282. //This part can be improved by adding the possible options of texture
  283. }
  284. else if (key === "map_ka" && material) {
  285. // ambient texture map with a loaded image
  286. //We must first get the folder of the image
  287. material.ambientTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  288. }
  289. else if (key === "map_kd" && material) {
  290. // Diffuse texture map with a loaded image
  291. material.diffuseTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  292. }
  293. else if (key === "map_ks" && material) {
  294. // Specular texture map with a loaded image
  295. //We must first get the folder of the image
  296. material.specularTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  297. }
  298. else if (key === "map_ns") {
  299. //Specular
  300. //Specular highlight component
  301. //We must first get the folder of the image
  302. //
  303. //Not supported by BABYLON
  304. //
  305. // continue;
  306. }
  307. else if (key === "map_bump" && material) {
  308. //The bump texture
  309. material.bumpTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  310. }
  311. else if (key === "map_d" && material) {
  312. // The dissolve of the material
  313. material.opacityTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  314. //Options for illumination
  315. }
  316. else if (key === "illum") {
  317. //Illumination
  318. if (value === "0") {
  319. //That mean Kd == Kd
  320. }
  321. else if (value === "1") {
  322. //Color on and Ambient on
  323. }
  324. else if (value === "2") {
  325. //Highlight on
  326. }
  327. else if (value === "3") {
  328. //Reflection on and Ray trace on
  329. }
  330. else if (value === "4") {
  331. //Transparency: Glass on, Reflection: Ray trace on
  332. }
  333. else if (value === "5") {
  334. //Reflection: Fresnel on and Ray trace on
  335. }
  336. else if (value === "6") {
  337. //Transparency: Refraction on, Reflection: Fresnel off and Ray trace on
  338. }
  339. else if (value === "7") {
  340. //Transparency: Refraction on, Reflection: Fresnel on and Ray trace on
  341. }
  342. else if (value === "8") {
  343. //Reflection on and Ray trace off
  344. }
  345. else if (value === "9") {
  346. //Transparency: Glass on, Reflection: Ray trace off
  347. }
  348. else if (value === "10") {
  349. //Casts shadows onto invisible surfaces
  350. }
  351. }
  352. else {
  353. // console.log("Unhandled expression at line : " + i +'\n' + "with value : " + line);
  354. }
  355. }
  356. //At the end of the file, add the last material
  357. if (material) {
  358. this.materials.push(material);
  359. }
  360. };
  361. /**
  362. * Gets the texture for the material.
  363. *
  364. * If the material is imported from input file,
  365. * We sanitize the url to ensure it takes the textre from aside the material.
  366. *
  367. * @param rootUrl The root url to load from
  368. * @param value The value stored in the mtl
  369. * @return The Texture
  370. */
  371. MTLFileLoader._getTexture = function (rootUrl, value, scene) {
  372. if (!value) {
  373. return null;
  374. }
  375. var url = rootUrl;
  376. // Load from input file.
  377. if (rootUrl === "file:") {
  378. var lastDelimiter = value.lastIndexOf("\\");
  379. if (lastDelimiter === -1) {
  380. lastDelimiter = value.lastIndexOf("/");
  381. }
  382. if (lastDelimiter > -1) {
  383. url += value.substr(lastDelimiter + 1);
  384. }
  385. else {
  386. url += value;
  387. }
  388. }
  389. // Not from input file.
  390. else {
  391. url += value;
  392. }
  393. return new BABYLON.Texture(url, scene);
  394. };
  395. return MTLFileLoader;
  396. }());
  397. BABYLON.MTLFileLoader = MTLFileLoader;
  398. var OBJFileLoader = /** @class */ (function () {
  399. function OBJFileLoader() {
  400. this.name = "obj";
  401. this.extensions = ".obj";
  402. this.obj = /^o/;
  403. this.group = /^g/;
  404. this.mtllib = /^mtllib /;
  405. this.usemtl = /^usemtl /;
  406. this.smooth = /^s /;
  407. this.vertexPattern = /v( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
  408. // vn float float float
  409. this.normalPattern = /vn( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
  410. // vt float float
  411. this.uvPattern = /vt( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
  412. // f vertex vertex vertex ...
  413. this.facePattern1 = /f\s+(([\d]{1,}[\s]?){3,})+/;
  414. // f vertex/uvs vertex/uvs vertex/uvs ...
  415. this.facePattern2 = /f\s+((([\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
  416. // f vertex/uvs/normal vertex/uvs/normal vertex/uvs/normal ...
  417. this.facePattern3 = /f\s+((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
  418. // f vertex//normal vertex//normal vertex//normal ...
  419. this.facePattern4 = /f\s+((([\d]{1,}\/\/[\d]{1,}[\s]?){3,})+)/;
  420. }
  421. /**
  422. * Calls synchronously the MTL file attached to this obj.
  423. * Load function or importMesh function don't enable to load 2 files in the same time asynchronously.
  424. * Without this function materials are not displayed in the first frame (but displayed after).
  425. * In consequence it is impossible to get material information in your HTML file
  426. *
  427. * @param url The URL of the MTL file
  428. * @param rootUrl
  429. * @param onSuccess Callback function to be called when the MTL file is loaded
  430. * @private
  431. */
  432. OBJFileLoader.prototype._loadMTL = function (url, rootUrl, onSuccess) {
  433. //The complete path to the mtl file
  434. var pathOfFile = BABYLON.Tools.BaseUrl + rootUrl + url;
  435. // Loads through the babylon tools to allow fileInput search.
  436. BABYLON.Tools.LoadFile(pathOfFile, onSuccess, undefined, undefined, false, function () { console.warn("Error - Unable to load " + pathOfFile); });
  437. };
  438. OBJFileLoader.prototype.importMesh = function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
  439. //get the meshes from OBJ file
  440. var loadedMeshes = this._parseSolid(meshesNames, scene, data, rootUrl);
  441. //Push meshes from OBJ file into the variable mesh of this function
  442. if (meshes) {
  443. loadedMeshes.forEach(function (mesh) {
  444. meshes.push(mesh);
  445. });
  446. }
  447. return true;
  448. };
  449. OBJFileLoader.prototype.load = function (scene, data, rootUrl) {
  450. //Get the 3D model
  451. return this.importMesh(null, scene, data, rootUrl, null, null, null);
  452. };
  453. OBJFileLoader.prototype.loadAssetContainer = function (scene, data, rootUrl, onError) {
  454. var container = new BABYLON.AssetContainer(scene);
  455. this.importMesh(null, scene, data, rootUrl, container.meshes, null, null);
  456. container.removeAllFromScene();
  457. return container;
  458. };
  459. /**
  460. * Read the OBJ file and create an Array of meshes.
  461. * Each mesh contains all information given by the OBJ and the MTL file.
  462. * i.e. vertices positions and indices, optional normals values, optional UV values, optional material
  463. *
  464. * @param meshesNames
  465. * @param scene BABYLON.Scene The scene where are displayed the data
  466. * @param data String The content of the obj file
  467. * @param rootUrl String The path to the folder
  468. * @returns Array<AbstractMesh>
  469. * @private
  470. */
  471. OBJFileLoader.prototype._parseSolid = function (meshesNames, scene, data, rootUrl) {
  472. var positions = []; //values for the positions of vertices
  473. var normals = []; //Values for the normals
  474. var uvs = []; //Values for the textures
  475. var meshesFromObj = []; //[mesh] Contains all the obj meshes
  476. var handledMesh; //The current mesh of meshes array
  477. var indicesForBabylon = []; //The list of indices for VertexData
  478. var wrappedPositionForBabylon = []; //The list of position in vectors
  479. var wrappedUvsForBabylon = []; //Array with all value of uvs to match with the indices
  480. var wrappedNormalsForBabylon = []; //Array with all value of normals to match with the indices
  481. var tuplePosNorm = []; //Create a tuple with indice of Position, Normal, UV [pos, norm, uvs]
  482. var curPositionInIndices = 0;
  483. var hasMeshes = false; //Meshes are defined in the file
  484. var unwrappedPositionsForBabylon = []; //Value of positionForBabylon w/o Vector3() [x,y,z]
  485. var unwrappedNormalsForBabylon = []; //Value of normalsForBabylon w/o Vector3() [x,y,z]
  486. var unwrappedUVForBabylon = []; //Value of uvsForBabylon w/o Vector3() [x,y,z]
  487. var triangles = []; //Indices from new triangles coming from polygons
  488. var materialNameFromObj = ""; //The name of the current material
  489. var fileToLoad = ""; //The name of the mtlFile to load
  490. var materialsFromMTLFile = new MTLFileLoader();
  491. var objMeshName = ""; //The name of the current obj mesh
  492. var increment = 1; //Id for meshes created by the multimaterial
  493. var isFirstMaterial = true;
  494. /**
  495. * Search for obj in the given array.
  496. * This function is called to check if a couple of data already exists in an array.
  497. *
  498. * If found, returns the index of the founded tuple index. Returns -1 if not found
  499. * @param arr Array<{ normals: Array<number>, idx: Array<number> }>
  500. * @param obj Array<number>
  501. * @returns {boolean}
  502. */
  503. var isInArray = function (arr, obj) {
  504. if (!arr[obj[0]])
  505. arr[obj[0]] = { normals: [], idx: [] };
  506. var idx = arr[obj[0]].normals.indexOf(obj[1]);
  507. return idx === -1 ? -1 : arr[obj[0]].idx[idx];
  508. };
  509. var isInArrayUV = function (arr, obj) {
  510. if (!arr[obj[0]])
  511. arr[obj[0]] = { normals: [], idx: [], uv: [] };
  512. var idx = arr[obj[0]].normals.indexOf(obj[1]);
  513. if (idx != 1 && (obj[2] == arr[obj[0]].uv[idx])) {
  514. return arr[obj[0]].idx[idx];
  515. }
  516. return -1;
  517. };
  518. /**
  519. * This function set the data for each triangle.
  520. * Data are position, normals and uvs
  521. * If a tuple of (position, normal) is not set, add the data into the corresponding array
  522. * If the tuple already exist, add only their indice
  523. *
  524. * @param indicePositionFromObj Integer The index in positions array
  525. * @param indiceUvsFromObj Integer The index in uvs array
  526. * @param indiceNormalFromObj Integer The index in normals array
  527. * @param positionVectorFromOBJ Vector3 The value of position at index objIndice
  528. * @param textureVectorFromOBJ Vector3 The value of uvs
  529. * @param normalsVectorFromOBJ Vector3 The value of normals at index objNormale
  530. */
  531. var setData = function (indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positionVectorFromOBJ, textureVectorFromOBJ, normalsVectorFromOBJ) {
  532. //Check if this tuple already exists in the list of tuples
  533. var _index;
  534. if (OBJFileLoader.OPTIMIZE_WITH_UV) {
  535. _index = isInArrayUV(tuplePosNorm, [
  536. indicePositionFromObj,
  537. indiceNormalFromObj,
  538. indiceUvsFromObj
  539. ]);
  540. }
  541. else {
  542. _index = isInArray(tuplePosNorm, [
  543. indicePositionFromObj,
  544. indiceNormalFromObj
  545. ]);
  546. }
  547. //If it not exists
  548. if (_index == -1) {
  549. //Add an new indice.
  550. //The array of indices is only an array with his length equal to the number of triangles - 1.
  551. //We add vertices data in this order
  552. indicesForBabylon.push(wrappedPositionForBabylon.length);
  553. //Push the position of vertice for Babylon
  554. //Each element is a BABYLON.Vector3(x,y,z)
  555. wrappedPositionForBabylon.push(positionVectorFromOBJ);
  556. //Push the uvs for Babylon
  557. //Each element is a BABYLON.Vector3(u,v)
  558. wrappedUvsForBabylon.push(textureVectorFromOBJ);
  559. //Push the normals for Babylon
  560. //Each element is a BABYLON.Vector3(x,y,z)
  561. wrappedNormalsForBabylon.push(normalsVectorFromOBJ);
  562. //Add the tuple in the comparison list
  563. tuplePosNorm[indicePositionFromObj].normals.push(indiceNormalFromObj);
  564. tuplePosNorm[indicePositionFromObj].idx.push(curPositionInIndices++);
  565. if (OBJFileLoader.OPTIMIZE_WITH_UV)
  566. tuplePosNorm[indicePositionFromObj].uv.push(indiceUvsFromObj);
  567. }
  568. else {
  569. //The tuple already exists
  570. //Add the index of the already existing tuple
  571. //At this index we can get the value of position, normal and uvs of vertex
  572. indicesForBabylon.push(_index);
  573. }
  574. };
  575. /**
  576. * Transform BABYLON.Vector() object onto 3 digits in an array
  577. */
  578. var unwrapData = function () {
  579. //Every array has the same length
  580. for (var l = 0; l < wrappedPositionForBabylon.length; l++) {
  581. //Push the x, y, z values of each element in the unwrapped array
  582. unwrappedPositionsForBabylon.push(wrappedPositionForBabylon[l].x, wrappedPositionForBabylon[l].y, wrappedPositionForBabylon[l].z);
  583. unwrappedNormalsForBabylon.push(wrappedNormalsForBabylon[l].x, wrappedNormalsForBabylon[l].y, wrappedNormalsForBabylon[l].z);
  584. unwrappedUVForBabylon.push(wrappedUvsForBabylon[l].x, wrappedUvsForBabylon[l].y); //z is an optional value not supported by BABYLON
  585. }
  586. // Reset arrays for the next new meshes
  587. wrappedPositionForBabylon = [];
  588. wrappedNormalsForBabylon = [];
  589. wrappedUvsForBabylon = [];
  590. tuplePosNorm = [];
  591. curPositionInIndices = 0;
  592. };
  593. /**
  594. * Create triangles from polygons by recursion
  595. * The best to understand how it works is to draw it in the same time you get the recursion.
  596. * It is important to notice that a triangle is a polygon
  597. * We get 4 patterns of face defined in OBJ File :
  598. * facePattern1 = ["1","2","3","4","5","6"]
  599. * facePattern2 = ["1/1","2/2","3/3","4/4","5/5","6/6"]
  600. * facePattern3 = ["1/1/1","2/2/2","3/3/3","4/4/4","5/5/5","6/6/6"]
  601. * facePattern4 = ["1//1","2//2","3//3","4//4","5//5","6//6"]
  602. * Each pattern is divided by the same method
  603. * @param face Array[String] The indices of elements
  604. * @param v Integer The variable to increment
  605. */
  606. var getTriangles = function (face, v) {
  607. //Work for each element of the array
  608. if (v + 1 < face.length) {
  609. //Add on the triangle variable the indexes to obtain triangles
  610. triangles.push(face[0], face[v], face[v + 1]);
  611. //Incrementation for recursion
  612. v += 1;
  613. //Recursion
  614. getTriangles(face, v);
  615. }
  616. //Result obtained after 2 iterations:
  617. //Pattern1 => triangle = ["1","2","3","1","3","4"];
  618. //Pattern2 => triangle = ["1/1","2/2","3/3","1/1","3/3","4/4"];
  619. //Pattern3 => triangle = ["1/1/1","2/2/2","3/3/3","1/1/1","3/3/3","4/4/4"];
  620. //Pattern4 => triangle = ["1//1","2//2","3//3","1//1","3//3","4//4"];
  621. };
  622. /**
  623. * Create triangles and push the data for each polygon for the pattern 1
  624. * In this pattern we get vertice positions
  625. * @param face
  626. * @param v
  627. */
  628. var setDataForCurrentFaceWithPattern1 = function (face, v) {
  629. //Get the indices of triangles for each polygon
  630. getTriangles(face, v);
  631. //For each element in the triangles array.
  632. //This var could contains 1 to an infinity of triangles
  633. for (var k = 0; k < triangles.length; k++) {
  634. // Set position indice
  635. var indicePositionFromObj = parseInt(triangles[k]) - 1;
  636. setData(indicePositionFromObj, 0, 0, //In the pattern 1, normals and uvs are not defined
  637. positions[indicePositionFromObj], //Get the vectors data
  638. BABYLON.Vector2.Zero(), BABYLON.Vector3.Up() //Create default vectors
  639. );
  640. }
  641. //Reset variable for the next line
  642. triangles = [];
  643. };
  644. /**
  645. * Create triangles and push the data for each polygon for the pattern 2
  646. * In this pattern we get vertice positions and uvsu
  647. * @param face
  648. * @param v
  649. */
  650. var setDataForCurrentFaceWithPattern2 = function (face, v) {
  651. //Get the indices of triangles for each polygon
  652. getTriangles(face, v);
  653. for (var k = 0; k < triangles.length; k++) {
  654. //triangle[k] = "1/1"
  655. //Split the data for getting position and uv
  656. var point = triangles[k].split("/"); // ["1", "1"]
  657. //Set position indice
  658. var indicePositionFromObj = parseInt(point[0]) - 1;
  659. //Set uv indice
  660. var indiceUvsFromObj = parseInt(point[1]) - 1;
  661. setData(indicePositionFromObj, indiceUvsFromObj, 0, //Default value for normals
  662. positions[indicePositionFromObj], //Get the values for each element
  663. uvs[indiceUvsFromObj], BABYLON.Vector3.Up() //Default value for normals
  664. );
  665. }
  666. //Reset variable for the next line
  667. triangles = [];
  668. };
  669. /**
  670. * Create triangles and push the data for each polygon for the pattern 3
  671. * In this pattern we get vertice positions, uvs and normals
  672. * @param face
  673. * @param v
  674. */
  675. var setDataForCurrentFaceWithPattern3 = function (face, v) {
  676. //Get the indices of triangles for each polygon
  677. getTriangles(face, v);
  678. for (var k = 0; k < triangles.length; k++) {
  679. //triangle[k] = "1/1/1"
  680. //Split the data for getting position, uv, and normals
  681. var point = triangles[k].split("/"); // ["1", "1", "1"]
  682. // Set position indice
  683. var indicePositionFromObj = parseInt(point[0]) - 1;
  684. // Set uv indice
  685. var indiceUvsFromObj = parseInt(point[1]) - 1;
  686. // Set normal indice
  687. var indiceNormalFromObj = parseInt(point[2]) - 1;
  688. setData(indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positions[indicePositionFromObj], uvs[indiceUvsFromObj], normals[indiceNormalFromObj] //Set the vector for each component
  689. );
  690. }
  691. //Reset variable for the next line
  692. triangles = [];
  693. };
  694. /**
  695. * Create triangles and push the data for each polygon for the pattern 4
  696. * In this pattern we get vertice positions and normals
  697. * @param face
  698. * @param v
  699. */
  700. var setDataForCurrentFaceWithPattern4 = function (face, v) {
  701. getTriangles(face, v);
  702. for (var k = 0; k < triangles.length; k++) {
  703. //triangle[k] = "1//1"
  704. //Split the data for getting position and normals
  705. var point = triangles[k].split("//"); // ["1", "1"]
  706. // We check indices, and normals
  707. var indicePositionFromObj = parseInt(point[0]) - 1;
  708. var indiceNormalFromObj = parseInt(point[1]) - 1;
  709. setData(indicePositionFromObj, 1, //Default value for uv
  710. indiceNormalFromObj, positions[indicePositionFromObj], //Get each vector of data
  711. BABYLON.Vector2.Zero(), normals[indiceNormalFromObj]);
  712. }
  713. //Reset variable for the next line
  714. triangles = [];
  715. };
  716. var addPreviousObjMesh = function () {
  717. //Check if it is not the first mesh. Otherwise we don't have data.
  718. if (meshesFromObj.length > 0) {
  719. //Get the previous mesh for applying the data about the faces
  720. //=> in obj file, faces definition append after the name of the mesh
  721. handledMesh = meshesFromObj[meshesFromObj.length - 1];
  722. //Set the data into Array for the mesh
  723. unwrapData();
  724. // Reverse tab. Otherwise face are displayed in the wrong sens
  725. indicesForBabylon.reverse();
  726. //Set the information for the mesh
  727. //Slice the array to avoid rewriting because of the fact this is the same var which be rewrited
  728. handledMesh.indices = indicesForBabylon.slice();
  729. handledMesh.positions = unwrappedPositionsForBabylon.slice();
  730. handledMesh.normals = unwrappedNormalsForBabylon.slice();
  731. handledMesh.uvs = unwrappedUVForBabylon.slice();
  732. //Reset the array for the next mesh
  733. indicesForBabylon = [];
  734. unwrappedPositionsForBabylon = [];
  735. unwrappedNormalsForBabylon = [];
  736. unwrappedUVForBabylon = [];
  737. }
  738. };
  739. //Main function
  740. //Split the file into lines
  741. var lines = data.split('\n');
  742. //Look at each line
  743. for (var i = 0; i < lines.length; i++) {
  744. var line = lines[i].trim();
  745. var result;
  746. //Comment or newLine
  747. if (line.length === 0 || line.charAt(0) === '#') {
  748. continue;
  749. //Get information about one position possible for the vertices
  750. }
  751. else if ((result = this.vertexPattern.exec(line)) !== null) {
  752. //Create a Vector3 with the position x, y, z
  753. //Value of result:
  754. // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  755. //Add the Vector in the list of positions
  756. positions.push(new BABYLON.Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
  757. }
  758. else if ((result = this.normalPattern.exec(line)) !== null) {
  759. //Create a Vector3 with the normals x, y, z
  760. //Value of result
  761. // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  762. //Add the Vector in the list of normals
  763. normals.push(new BABYLON.Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
  764. }
  765. else if ((result = this.uvPattern.exec(line)) !== null) {
  766. //Create a Vector2 with the normals u, v
  767. //Value of result
  768. // ["vt 0.1 0.2 0.3", "0.1", "0.2"]
  769. //Add the Vector in the list of uvs
  770. uvs.push(new BABYLON.Vector2(parseFloat(result[1]), parseFloat(result[2])));
  771. //Identify patterns of faces
  772. //Face could be defined in different type of pattern
  773. }
  774. else if ((result = this.facePattern3.exec(line)) !== null) {
  775. //Value of result:
  776. //["f 1/1/1 2/2/2 3/3/3", "1/1/1 2/2/2 3/3/3"...]
  777. //Set the data for this face
  778. setDataForCurrentFaceWithPattern3(result[1].trim().split(" "), // ["1/1/1", "2/2/2", "3/3/3"]
  779. 1);
  780. }
  781. else if ((result = this.facePattern4.exec(line)) !== null) {
  782. //Value of result:
  783. //["f 1//1 2//2 3//3", "1//1 2//2 3//3"...]
  784. //Set the data for this face
  785. setDataForCurrentFaceWithPattern4(result[1].trim().split(" "), // ["1//1", "2//2", "3//3"]
  786. 1);
  787. }
  788. else if ((result = this.facePattern2.exec(line)) !== null) {
  789. //Value of result:
  790. //["f 1/1 2/2 3/3", "1/1 2/2 3/3"...]
  791. //Set the data for this face
  792. setDataForCurrentFaceWithPattern2(result[1].trim().split(" "), // ["1/1", "2/2", "3/3"]
  793. 1);
  794. }
  795. else if ((result = this.facePattern1.exec(line)) !== null) {
  796. //Value of result
  797. //["f 1 2 3", "1 2 3"...]
  798. //Set the data for this face
  799. setDataForCurrentFaceWithPattern1(result[1].trim().split(" "), // ["1", "2", "3"]
  800. 1);
  801. //Define a mesh or an object
  802. //Each time this keyword is analysed, create a new Object with all data for creating a babylonMesh
  803. }
  804. else if (this.group.test(line) || this.obj.test(line)) {
  805. //Create a new mesh corresponding to the name of the group.
  806. //Definition of the mesh
  807. var objMesh =
  808. //Set the name of the current obj mesh
  809. {
  810. name: line.substring(2).trim(),
  811. indices: undefined,
  812. positions: undefined,
  813. normals: undefined,
  814. uvs: undefined,
  815. materialName: ""
  816. };
  817. addPreviousObjMesh();
  818. //Push the last mesh created with only the name
  819. meshesFromObj.push(objMesh);
  820. //Set this variable to indicate that now meshesFromObj has objects defined inside
  821. hasMeshes = true;
  822. isFirstMaterial = true;
  823. increment = 1;
  824. //Keyword for applying a material
  825. }
  826. else if (this.usemtl.test(line)) {
  827. //Get the name of the material
  828. materialNameFromObj = line.substring(7).trim();
  829. //If this new material is in the same mesh
  830. if (!isFirstMaterial) {
  831. //Set the data for the previous mesh
  832. addPreviousObjMesh();
  833. //Create a new mesh
  834. var objMesh =
  835. //Set the name of the current obj mesh
  836. {
  837. name: objMeshName + "_mm" + increment.toString(),
  838. indices: undefined,
  839. positions: undefined,
  840. normals: undefined,
  841. uvs: undefined,
  842. materialName: materialNameFromObj
  843. };
  844. increment++;
  845. //If meshes are already defined
  846. meshesFromObj.push(objMesh);
  847. }
  848. //Set the material name if the previous line define a mesh
  849. if (hasMeshes && isFirstMaterial) {
  850. //Set the material name to the previous mesh (1 material per mesh)
  851. meshesFromObj[meshesFromObj.length - 1].materialName = materialNameFromObj;
  852. isFirstMaterial = false;
  853. }
  854. //Keyword for loading the mtl file
  855. }
  856. else if (this.mtllib.test(line)) {
  857. //Get the name of mtl file
  858. fileToLoad = line.substring(7).trim();
  859. //Apply smoothing
  860. }
  861. else if (this.smooth.test(line)) {
  862. // smooth shading => apply smoothing
  863. //Toda y I don't know it work with babylon and with obj.
  864. //With the obj file an integer is set
  865. }
  866. else {
  867. //If there is another possibility
  868. console.log("Unhandled expression at line : " + line);
  869. }
  870. }
  871. //At the end of the file, add the last mesh into the meshesFromObj array
  872. if (hasMeshes) {
  873. //Set the data for the last mesh
  874. handledMesh = meshesFromObj[meshesFromObj.length - 1];
  875. //Reverse indices for displaying faces in the good sens
  876. indicesForBabylon.reverse();
  877. //Get the good array
  878. unwrapData();
  879. //Set array
  880. handledMesh.indices = indicesForBabylon;
  881. handledMesh.positions = unwrappedPositionsForBabylon;
  882. handledMesh.normals = unwrappedNormalsForBabylon;
  883. handledMesh.uvs = unwrappedUVForBabylon;
  884. }
  885. //If any o or g keyword found, create a mesj with a random id
  886. if (!hasMeshes) {
  887. // reverse tab of indices
  888. indicesForBabylon.reverse();
  889. //Get positions normals uvs
  890. unwrapData();
  891. //Set data for one mesh
  892. meshesFromObj.push({
  893. name: BABYLON.Geometry.RandomId(),
  894. indices: indicesForBabylon,
  895. positions: unwrappedPositionsForBabylon,
  896. normals: unwrappedNormalsForBabylon,
  897. uvs: unwrappedUVForBabylon,
  898. materialName: materialNameFromObj
  899. });
  900. }
  901. //Create a BABYLON.Mesh list
  902. var babylonMeshesArray = []; //The mesh for babylon
  903. var materialToUse = new Array();
  904. //Set data for each mesh
  905. for (var j = 0; j < meshesFromObj.length; j++) {
  906. //check meshesNames (stlFileLoader)
  907. if (meshesNames && meshesFromObj[j].name) {
  908. if (meshesNames instanceof Array) {
  909. if (meshesNames.indexOf(meshesFromObj[j].name) == -1) {
  910. continue;
  911. }
  912. }
  913. else {
  914. if (meshesFromObj[j].name !== meshesNames) {
  915. continue;
  916. }
  917. }
  918. }
  919. //Get the current mesh
  920. //Set the data with VertexBuffer for each mesh
  921. handledMesh = meshesFromObj[j];
  922. //Create a BABYLON.Mesh with the name of the obj mesh
  923. var babylonMesh = new BABYLON.Mesh(meshesFromObj[j].name, scene);
  924. //Push the name of the material to an array
  925. //This is indispensable for the importMesh function
  926. materialToUse.push(meshesFromObj[j].materialName);
  927. var vertexData = new BABYLON.VertexData(); //The container for the values
  928. //Set the data for the babylonMesh
  929. vertexData.positions = handledMesh.positions;
  930. vertexData.normals = handledMesh.normals;
  931. vertexData.uvs = handledMesh.uvs;
  932. vertexData.indices = handledMesh.indices;
  933. //Set the data from the VertexBuffer to the current BABYLON.Mesh
  934. vertexData.applyToMesh(babylonMesh);
  935. if (OBJFileLoader.INVERT_Y) {
  936. babylonMesh.scaling.y *= -1;
  937. }
  938. //Push the mesh into an array
  939. babylonMeshesArray.push(babylonMesh);
  940. }
  941. //load the materials
  942. //Check if we have a file to load
  943. if (fileToLoad !== "") {
  944. //Load the file synchronously
  945. this._loadMTL(fileToLoad, rootUrl, function (dataLoaded) {
  946. //Create materials thanks MTLLoader function
  947. materialsFromMTLFile.parseMTL(scene, dataLoaded, rootUrl);
  948. //Look at each material loaded in the mtl file
  949. for (var n = 0; n < materialsFromMTLFile.materials.length; n++) {
  950. //Three variables to get all meshes with the same material
  951. var startIndex = 0;
  952. var _indices = [];
  953. var _index;
  954. //The material from MTL file is used in the meshes loaded
  955. //Push the indice in an array
  956. //Check if the material is not used for another mesh
  957. while ((_index = materialToUse.indexOf(materialsFromMTLFile.materials[n].name, startIndex)) > -1) {
  958. _indices.push(_index);
  959. startIndex = _index + 1;
  960. }
  961. //If the material is not used dispose it
  962. if (_index == -1 && _indices.length == 0) {
  963. //If the material is not needed, remove it
  964. materialsFromMTLFile.materials[n].dispose();
  965. }
  966. else {
  967. for (var o = 0; o < _indices.length; o++) {
  968. //Apply the material to the BABYLON.Mesh for each mesh with the material
  969. babylonMeshesArray[_indices[o]].material = materialsFromMTLFile.materials[n];
  970. }
  971. }
  972. }
  973. });
  974. }
  975. //Return an array with all BABYLON.Mesh
  976. return babylonMeshesArray;
  977. };
  978. OBJFileLoader.OPTIMIZE_WITH_UV = false;
  979. OBJFileLoader.INVERT_Y = false;
  980. return OBJFileLoader;
  981. }());
  982. BABYLON.OBJFileLoader = OBJFileLoader;
  983. if (BABYLON.SceneLoader) {
  984. //Add this loader into the register plugin
  985. BABYLON.SceneLoader.RegisterPlugin(new OBJFileLoader());
  986. }
  987. })(BABYLON || (BABYLON = {}));
  988. //# sourceMappingURL=babylon.objFileLoader.js.map
  989. var BABYLON;
  990. (function (BABYLON) {
  991. /**
  992. * Mode that determines the coordinate system to use.
  993. */
  994. var GLTFLoaderCoordinateSystemMode;
  995. (function (GLTFLoaderCoordinateSystemMode) {
  996. /**
  997. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  998. */
  999. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
  1000. /**
  1001. * Sets the useRightHandedSystem flag on the scene.
  1002. */
  1003. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
  1004. })(GLTFLoaderCoordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode || (BABYLON.GLTFLoaderCoordinateSystemMode = {}));
  1005. /**
  1006. * Mode that determines what animations will start.
  1007. */
  1008. var GLTFLoaderAnimationStartMode;
  1009. (function (GLTFLoaderAnimationStartMode) {
  1010. /**
  1011. * No animation will start.
  1012. */
  1013. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
  1014. /**
  1015. * The first animation will start.
  1016. */
  1017. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
  1018. /**
  1019. * All animations will start.
  1020. */
  1021. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
  1022. })(GLTFLoaderAnimationStartMode = BABYLON.GLTFLoaderAnimationStartMode || (BABYLON.GLTFLoaderAnimationStartMode = {}));
  1023. /**
  1024. * Loader state.
  1025. */
  1026. var GLTFLoaderState;
  1027. (function (GLTFLoaderState) {
  1028. /**
  1029. * The asset is loading.
  1030. */
  1031. GLTFLoaderState[GLTFLoaderState["LOADING"] = 0] = "LOADING";
  1032. /**
  1033. * The asset is ready for rendering.
  1034. */
  1035. GLTFLoaderState[GLTFLoaderState["READY"] = 1] = "READY";
  1036. /**
  1037. * The asset is completely loaded.
  1038. */
  1039. GLTFLoaderState[GLTFLoaderState["COMPLETE"] = 2] = "COMPLETE";
  1040. })(GLTFLoaderState = BABYLON.GLTFLoaderState || (BABYLON.GLTFLoaderState = {}));
  1041. /**
  1042. * File loader for loading glTF files into a scene.
  1043. */
  1044. var GLTFFileLoader = /** @class */ (function () {
  1045. function GLTFFileLoader() {
  1046. // #region Common options
  1047. /**
  1048. * Raised when the asset has been parsed
  1049. */
  1050. this.onParsedObservable = new BABYLON.Observable();
  1051. // #endregion
  1052. // #region V2 options
  1053. /**
  1054. * The coordinate system mode. Defaults to AUTO.
  1055. */
  1056. this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
  1057. /**
  1058. * The animation start mode. Defaults to FIRST.
  1059. */
  1060. this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
  1061. /**
  1062. * Defines if the loader should compile materials before raising the success callback. Defaults to false.
  1063. */
  1064. this.compileMaterials = false;
  1065. /**
  1066. * Defines if the loader should also compile materials with clip planes. Defaults to false.
  1067. */
  1068. this.useClipPlane = false;
  1069. /**
  1070. * Defines if the loader should compile shadow generators before raising the success callback. Defaults to false.
  1071. */
  1072. this.compileShadowGenerators = false;
  1073. /**
  1074. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  1075. */
  1076. this.onMeshLoadedObservable = new BABYLON.Observable();
  1077. /**
  1078. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  1079. */
  1080. this.onTextureLoadedObservable = new BABYLON.Observable();
  1081. /**
  1082. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  1083. */
  1084. this.onMaterialLoadedObservable = new BABYLON.Observable();
  1085. /**
  1086. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  1087. * For assets with LODs, raised when all of the LODs are complete.
  1088. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  1089. */
  1090. this.onCompleteObservable = new BABYLON.Observable();
  1091. /**
  1092. * Observable raised after the loader is disposed.
  1093. */
  1094. this.onDisposeObservable = new BABYLON.Observable();
  1095. /**
  1096. * Observable raised after a loader extension is created.
  1097. * Set additional options for a loader extension in this event.
  1098. */
  1099. this.onExtensionLoadedObservable = new BABYLON.Observable();
  1100. // #endregion
  1101. this._loader = null;
  1102. /**
  1103. * Name of the loader ("gltf")
  1104. */
  1105. this.name = "gltf";
  1106. /**
  1107. * Supported file extensions of the loader (.gltf, .glb)
  1108. */
  1109. this.extensions = {
  1110. ".gltf": { isBinary: false },
  1111. ".glb": { isBinary: true }
  1112. };
  1113. }
  1114. Object.defineProperty(GLTFFileLoader.prototype, "onParsed", {
  1115. /**
  1116. * Raised when the asset has been parsed
  1117. */
  1118. set: function (callback) {
  1119. if (this._onParsedObserver) {
  1120. this.onParsedObservable.remove(this._onParsedObserver);
  1121. }
  1122. this._onParsedObserver = this.onParsedObservable.add(callback);
  1123. },
  1124. enumerable: true,
  1125. configurable: true
  1126. });
  1127. Object.defineProperty(GLTFFileLoader.prototype, "onMeshLoaded", {
  1128. /**
  1129. * Callback raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  1130. */
  1131. set: function (callback) {
  1132. if (this._onMeshLoadedObserver) {
  1133. this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver);
  1134. }
  1135. this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback);
  1136. },
  1137. enumerable: true,
  1138. configurable: true
  1139. });
  1140. Object.defineProperty(GLTFFileLoader.prototype, "onTextureLoaded", {
  1141. /**
  1142. * Callback raised when the loader creates a texture after parsing the glTF properties of the texture.
  1143. */
  1144. set: function (callback) {
  1145. if (this._onTextureLoadedObserver) {
  1146. this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver);
  1147. }
  1148. this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback);
  1149. },
  1150. enumerable: true,
  1151. configurable: true
  1152. });
  1153. Object.defineProperty(GLTFFileLoader.prototype, "onMaterialLoaded", {
  1154. /**
  1155. * Callback raised when the loader creates a material after parsing the glTF properties of the material.
  1156. */
  1157. set: function (callback) {
  1158. if (this._onMaterialLoadedObserver) {
  1159. this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver);
  1160. }
  1161. this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback);
  1162. },
  1163. enumerable: true,
  1164. configurable: true
  1165. });
  1166. Object.defineProperty(GLTFFileLoader.prototype, "onComplete", {
  1167. /**
  1168. * Callback raised when the asset is completely loaded, immediately before the loader is disposed.
  1169. */
  1170. set: function (callback) {
  1171. if (this._onCompleteObserver) {
  1172. this.onCompleteObservable.remove(this._onCompleteObserver);
  1173. }
  1174. this._onCompleteObserver = this.onCompleteObservable.add(callback);
  1175. },
  1176. enumerable: true,
  1177. configurable: true
  1178. });
  1179. Object.defineProperty(GLTFFileLoader.prototype, "onDispose", {
  1180. /**
  1181. * Callback raised after the loader is disposed.
  1182. */
  1183. set: function (callback) {
  1184. if (this._onDisposeObserver) {
  1185. this.onDisposeObservable.remove(this._onDisposeObserver);
  1186. }
  1187. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  1188. },
  1189. enumerable: true,
  1190. configurable: true
  1191. });
  1192. Object.defineProperty(GLTFFileLoader.prototype, "onExtensionLoaded", {
  1193. /**
  1194. * Callback raised after a loader extension is created.
  1195. */
  1196. set: function (callback) {
  1197. if (this._onExtensionLoadedObserver) {
  1198. this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver);
  1199. }
  1200. this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback);
  1201. },
  1202. enumerable: true,
  1203. configurable: true
  1204. });
  1205. /**
  1206. * Returns a promise that resolves when the asset is completely loaded.
  1207. * @returns a promise that resolves when the asset is completely loaded.
  1208. */
  1209. GLTFFileLoader.prototype.whenCompleteAsync = function () {
  1210. var _this = this;
  1211. return new Promise(function (resolve) {
  1212. _this.onCompleteObservable.add(function () {
  1213. resolve();
  1214. }, undefined, undefined, undefined, true);
  1215. });
  1216. };
  1217. Object.defineProperty(GLTFFileLoader.prototype, "loaderState", {
  1218. /**
  1219. * The loader state or null if the loader is not active.
  1220. */
  1221. get: function () {
  1222. return this._loader ? this._loader.state : null;
  1223. },
  1224. enumerable: true,
  1225. configurable: true
  1226. });
  1227. /**
  1228. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  1229. */
  1230. GLTFFileLoader.prototype.dispose = function () {
  1231. if (this._loader) {
  1232. this._loader.dispose();
  1233. this._loader = null;
  1234. }
  1235. this.onMeshLoadedObservable.clear();
  1236. this.onTextureLoadedObservable.clear();
  1237. this.onMaterialLoadedObservable.clear();
  1238. this.onDisposeObservable.notifyObservers(this);
  1239. this.onDisposeObservable.clear();
  1240. };
  1241. /**
  1242. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  1243. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  1244. * @param scene the scene the meshes should be added to
  1245. * @param data the glTF data to load
  1246. * @param rootUrl root url to load from
  1247. * @param onProgress event that fires when loading progress has occured
  1248. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  1249. */
  1250. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  1251. var _this = this;
  1252. return Promise.resolve().then(function () {
  1253. var loaderData = _this._parse(data);
  1254. _this._loader = _this._getLoader(loaderData);
  1255. return _this._loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onProgress);
  1256. });
  1257. };
  1258. /**
  1259. * Imports all objects from the loaded glTF data and adds them to the scene
  1260. * @param scene the scene the objects should be added to
  1261. * @param data the glTF data to load
  1262. * @param rootUrl root url to load from
  1263. * @param onProgress event that fires when loading progress has occured
  1264. * @returns a promise which completes when objects have been loaded to the scene
  1265. */
  1266. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  1267. var _this = this;
  1268. return Promise.resolve().then(function () {
  1269. var loaderData = _this._parse(data);
  1270. _this._loader = _this._getLoader(loaderData);
  1271. return _this._loader.loadAsync(scene, loaderData, rootUrl, onProgress);
  1272. });
  1273. };
  1274. /**
  1275. * Load into an asset container.
  1276. * @param scene The scene to load into
  1277. * @param data The data to import
  1278. * @param rootUrl The root url for scene and resources
  1279. * @param onProgress The callback when the load progresses
  1280. * @returns The loaded asset container
  1281. */
  1282. GLTFFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress) {
  1283. var _this = this;
  1284. return Promise.resolve().then(function () {
  1285. var loaderData = _this._parse(data);
  1286. _this._loader = _this._getLoader(loaderData);
  1287. return _this._loader.importMeshAsync(null, scene, loaderData, rootUrl, onProgress).then(function (result) {
  1288. var container = new BABYLON.AssetContainer(scene);
  1289. Array.prototype.push.apply(container.meshes, result.meshes);
  1290. Array.prototype.push.apply(container.particleSystems, result.particleSystems);
  1291. Array.prototype.push.apply(container.skeletons, result.skeletons);
  1292. Array.prototype.push.apply(container.animationGroups, result.animationGroups);
  1293. container.removeAllFromScene();
  1294. return container;
  1295. });
  1296. });
  1297. };
  1298. /**
  1299. * If the data string can be loaded directly.
  1300. * @param data string contianing the file data
  1301. * @returns if the data can be loaded directly
  1302. */
  1303. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  1304. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  1305. };
  1306. /**
  1307. * Instantiates a glTF file loader plugin.
  1308. * @returns the created plugin
  1309. */
  1310. GLTFFileLoader.prototype.createPlugin = function () {
  1311. return new GLTFFileLoader();
  1312. };
  1313. GLTFFileLoader.prototype._parse = function (data) {
  1314. var parsedData;
  1315. if (data instanceof ArrayBuffer) {
  1316. parsedData = GLTFFileLoader._parseBinary(data);
  1317. }
  1318. else {
  1319. parsedData = {
  1320. json: JSON.parse(data),
  1321. bin: null
  1322. };
  1323. }
  1324. this.onParsedObservable.notifyObservers(parsedData);
  1325. this.onParsedObservable.clear();
  1326. return parsedData;
  1327. };
  1328. GLTFFileLoader.prototype._getLoader = function (loaderData) {
  1329. var _this = this;
  1330. var loaderVersion = { major: 2, minor: 0 };
  1331. var asset = loaderData.json.asset || {};
  1332. var version = GLTFFileLoader._parseVersion(asset.version);
  1333. if (!version) {
  1334. throw new Error("Invalid version: " + asset.version);
  1335. }
  1336. if (asset.minVersion !== undefined) {
  1337. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  1338. if (!minVersion) {
  1339. throw new Error("Invalid minimum version: " + asset.minVersion);
  1340. }
  1341. if (GLTFFileLoader._compareVersion(minVersion, loaderVersion) > 0) {
  1342. throw new Error("Incompatible minimum version: " + asset.minVersion);
  1343. }
  1344. }
  1345. var createLoaders = {
  1346. 1: GLTFFileLoader.CreateGLTFLoaderV1,
  1347. 2: GLTFFileLoader.CreateGLTFLoaderV2
  1348. };
  1349. var createLoader = createLoaders[version.major];
  1350. if (!createLoader) {
  1351. throw new Error("Unsupported version: " + asset.version);
  1352. }
  1353. var loader = createLoader();
  1354. loader.coordinateSystemMode = this.coordinateSystemMode;
  1355. loader.animationStartMode = this.animationStartMode;
  1356. loader.compileMaterials = this.compileMaterials;
  1357. loader.useClipPlane = this.useClipPlane;
  1358. loader.compileShadowGenerators = this.compileShadowGenerators;
  1359. loader.onMeshLoadedObservable.add(function (mesh) { return _this.onMeshLoadedObservable.notifyObservers(mesh); });
  1360. loader.onTextureLoadedObservable.add(function (texture) { return _this.onTextureLoadedObservable.notifyObservers(texture); });
  1361. loader.onMaterialLoadedObservable.add(function (material) { return _this.onMaterialLoadedObservable.notifyObservers(material); });
  1362. loader.onExtensionLoadedObservable.add(function (extension) { return _this.onExtensionLoadedObservable.notifyObservers(extension); });
  1363. loader.onCompleteObservable.add(function () {
  1364. _this.onMeshLoadedObservable.clear();
  1365. _this.onTextureLoadedObservable.clear();
  1366. _this.onMaterialLoadedObservable.clear();
  1367. _this.onCompleteObservable.notifyObservers(_this);
  1368. _this.onCompleteObservable.clear();
  1369. });
  1370. return loader;
  1371. };
  1372. GLTFFileLoader._parseBinary = function (data) {
  1373. var Binary = {
  1374. Magic: 0x46546C67
  1375. };
  1376. var binaryReader = new BinaryReader(data);
  1377. var magic = binaryReader.readUint32();
  1378. if (magic !== Binary.Magic) {
  1379. throw new Error("Unexpected magic: " + magic);
  1380. }
  1381. var version = binaryReader.readUint32();
  1382. switch (version) {
  1383. case 1: return GLTFFileLoader._parseV1(binaryReader);
  1384. case 2: return GLTFFileLoader._parseV2(binaryReader);
  1385. }
  1386. throw new Error("Unsupported version: " + version);
  1387. };
  1388. GLTFFileLoader._parseV1 = function (binaryReader) {
  1389. var ContentFormat = {
  1390. JSON: 0
  1391. };
  1392. var length = binaryReader.readUint32();
  1393. if (length != binaryReader.getLength()) {
  1394. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  1395. }
  1396. var contentLength = binaryReader.readUint32();
  1397. var contentFormat = binaryReader.readUint32();
  1398. var content;
  1399. switch (contentFormat) {
  1400. case ContentFormat.JSON: {
  1401. content = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength)));
  1402. break;
  1403. }
  1404. default: {
  1405. throw new Error("Unexpected content format: " + contentFormat);
  1406. }
  1407. }
  1408. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  1409. var body = binaryReader.readUint8Array(bytesRemaining);
  1410. return {
  1411. json: content,
  1412. bin: body
  1413. };
  1414. };
  1415. GLTFFileLoader._parseV2 = function (binaryReader) {
  1416. var ChunkFormat = {
  1417. JSON: 0x4E4F534A,
  1418. BIN: 0x004E4942
  1419. };
  1420. var length = binaryReader.readUint32();
  1421. if (length !== binaryReader.getLength()) {
  1422. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  1423. }
  1424. // JSON chunk
  1425. var chunkLength = binaryReader.readUint32();
  1426. var chunkFormat = binaryReader.readUint32();
  1427. if (chunkFormat !== ChunkFormat.JSON) {
  1428. throw new Error("First chunk format is not JSON");
  1429. }
  1430. var json = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength)));
  1431. // Look for BIN chunk
  1432. var bin = null;
  1433. while (binaryReader.getPosition() < binaryReader.getLength()) {
  1434. var chunkLength_1 = binaryReader.readUint32();
  1435. var chunkFormat_1 = binaryReader.readUint32();
  1436. switch (chunkFormat_1) {
  1437. case ChunkFormat.JSON: {
  1438. throw new Error("Unexpected JSON chunk");
  1439. }
  1440. case ChunkFormat.BIN: {
  1441. bin = binaryReader.readUint8Array(chunkLength_1);
  1442. break;
  1443. }
  1444. default: {
  1445. // ignore unrecognized chunkFormat
  1446. binaryReader.skipBytes(chunkLength_1);
  1447. break;
  1448. }
  1449. }
  1450. }
  1451. return {
  1452. json: json,
  1453. bin: bin
  1454. };
  1455. };
  1456. GLTFFileLoader._parseVersion = function (version) {
  1457. if (version === "1.0" || version === "1.0.1") {
  1458. return {
  1459. major: 1,
  1460. minor: 0
  1461. };
  1462. }
  1463. var match = (version + "").match(/^(\d+)\.(\d+)/);
  1464. if (!match) {
  1465. return null;
  1466. }
  1467. return {
  1468. major: parseInt(match[1]),
  1469. minor: parseInt(match[2])
  1470. };
  1471. };
  1472. GLTFFileLoader._compareVersion = function (a, b) {
  1473. if (a.major > b.major)
  1474. return 1;
  1475. if (a.major < b.major)
  1476. return -1;
  1477. if (a.minor > b.minor)
  1478. return 1;
  1479. if (a.minor < b.minor)
  1480. return -1;
  1481. return 0;
  1482. };
  1483. GLTFFileLoader._decodeBufferToText = function (buffer) {
  1484. var result = "";
  1485. var length = buffer.byteLength;
  1486. for (var i = 0; i < length; i++) {
  1487. result += String.fromCharCode(buffer[i]);
  1488. }
  1489. return result;
  1490. };
  1491. // #endregion
  1492. // #region V1 options
  1493. /**
  1494. * Set this property to false to disable incremental loading which delays the loader from calling the success callback until after loading the meshes and shaders.
  1495. * Textures always loads asynchronously. For example, the success callback can compute the bounding information of the loaded meshes when incremental loading is disabled.
  1496. * Defaults to true.
  1497. */
  1498. GLTFFileLoader.IncrementalLoading = true;
  1499. /**
  1500. * Set this property to true in order to work with homogeneous coordinates, available with some converters and exporters.
  1501. * Defaults to false. See https://en.wikipedia.org/wiki/Homogeneous_coordinates.
  1502. */
  1503. GLTFFileLoader.HomogeneousCoordinates = false;
  1504. return GLTFFileLoader;
  1505. }());
  1506. BABYLON.GLTFFileLoader = GLTFFileLoader;
  1507. var BinaryReader = /** @class */ (function () {
  1508. function BinaryReader(arrayBuffer) {
  1509. this._arrayBuffer = arrayBuffer;
  1510. this._dataView = new DataView(arrayBuffer);
  1511. this._byteOffset = 0;
  1512. }
  1513. BinaryReader.prototype.getPosition = function () {
  1514. return this._byteOffset;
  1515. };
  1516. BinaryReader.prototype.getLength = function () {
  1517. return this._arrayBuffer.byteLength;
  1518. };
  1519. BinaryReader.prototype.readUint32 = function () {
  1520. var value = this._dataView.getUint32(this._byteOffset, true);
  1521. this._byteOffset += 4;
  1522. return value;
  1523. };
  1524. BinaryReader.prototype.readUint8Array = function (length) {
  1525. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  1526. this._byteOffset += length;
  1527. return value;
  1528. };
  1529. BinaryReader.prototype.skipBytes = function (length) {
  1530. this._byteOffset += length;
  1531. };
  1532. return BinaryReader;
  1533. }());
  1534. if (BABYLON.SceneLoader) {
  1535. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  1536. }
  1537. })(BABYLON || (BABYLON = {}));
  1538. //# sourceMappingURL=babylon.glTFFileLoader.js.map
  1539. var BABYLON;
  1540. (function (BABYLON) {
  1541. var GLTF1;
  1542. (function (GLTF1) {
  1543. /**
  1544. * Enums
  1545. */
  1546. var EComponentType;
  1547. (function (EComponentType) {
  1548. EComponentType[EComponentType["BYTE"] = 5120] = "BYTE";
  1549. EComponentType[EComponentType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  1550. EComponentType[EComponentType["SHORT"] = 5122] = "SHORT";
  1551. EComponentType[EComponentType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  1552. EComponentType[EComponentType["FLOAT"] = 5126] = "FLOAT";
  1553. })(EComponentType = GLTF1.EComponentType || (GLTF1.EComponentType = {}));
  1554. var EShaderType;
  1555. (function (EShaderType) {
  1556. EShaderType[EShaderType["FRAGMENT"] = 35632] = "FRAGMENT";
  1557. EShaderType[EShaderType["VERTEX"] = 35633] = "VERTEX";
  1558. })(EShaderType = GLTF1.EShaderType || (GLTF1.EShaderType = {}));
  1559. var EParameterType;
  1560. (function (EParameterType) {
  1561. EParameterType[EParameterType["BYTE"] = 5120] = "BYTE";
  1562. EParameterType[EParameterType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  1563. EParameterType[EParameterType["SHORT"] = 5122] = "SHORT";
  1564. EParameterType[EParameterType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  1565. EParameterType[EParameterType["INT"] = 5124] = "INT";
  1566. EParameterType[EParameterType["UNSIGNED_INT"] = 5125] = "UNSIGNED_INT";
  1567. EParameterType[EParameterType["FLOAT"] = 5126] = "FLOAT";
  1568. EParameterType[EParameterType["FLOAT_VEC2"] = 35664] = "FLOAT_VEC2";
  1569. EParameterType[EParameterType["FLOAT_VEC3"] = 35665] = "FLOAT_VEC3";
  1570. EParameterType[EParameterType["FLOAT_VEC4"] = 35666] = "FLOAT_VEC4";
  1571. EParameterType[EParameterType["INT_VEC2"] = 35667] = "INT_VEC2";
  1572. EParameterType[EParameterType["INT_VEC3"] = 35668] = "INT_VEC3";
  1573. EParameterType[EParameterType["INT_VEC4"] = 35669] = "INT_VEC4";
  1574. EParameterType[EParameterType["BOOL"] = 35670] = "BOOL";
  1575. EParameterType[EParameterType["BOOL_VEC2"] = 35671] = "BOOL_VEC2";
  1576. EParameterType[EParameterType["BOOL_VEC3"] = 35672] = "BOOL_VEC3";
  1577. EParameterType[EParameterType["BOOL_VEC4"] = 35673] = "BOOL_VEC4";
  1578. EParameterType[EParameterType["FLOAT_MAT2"] = 35674] = "FLOAT_MAT2";
  1579. EParameterType[EParameterType["FLOAT_MAT3"] = 35675] = "FLOAT_MAT3";
  1580. EParameterType[EParameterType["FLOAT_MAT4"] = 35676] = "FLOAT_MAT4";
  1581. EParameterType[EParameterType["SAMPLER_2D"] = 35678] = "SAMPLER_2D";
  1582. })(EParameterType = GLTF1.EParameterType || (GLTF1.EParameterType = {}));
  1583. var ETextureWrapMode;
  1584. (function (ETextureWrapMode) {
  1585. ETextureWrapMode[ETextureWrapMode["CLAMP_TO_EDGE"] = 33071] = "CLAMP_TO_EDGE";
  1586. ETextureWrapMode[ETextureWrapMode["MIRRORED_REPEAT"] = 33648] = "MIRRORED_REPEAT";
  1587. ETextureWrapMode[ETextureWrapMode["REPEAT"] = 10497] = "REPEAT";
  1588. })(ETextureWrapMode = GLTF1.ETextureWrapMode || (GLTF1.ETextureWrapMode = {}));
  1589. var ETextureFilterType;
  1590. (function (ETextureFilterType) {
  1591. ETextureFilterType[ETextureFilterType["NEAREST"] = 9728] = "NEAREST";
  1592. ETextureFilterType[ETextureFilterType["LINEAR"] = 9728] = "LINEAR";
  1593. ETextureFilterType[ETextureFilterType["NEAREST_MIPMAP_NEAREST"] = 9984] = "NEAREST_MIPMAP_NEAREST";
  1594. ETextureFilterType[ETextureFilterType["LINEAR_MIPMAP_NEAREST"] = 9985] = "LINEAR_MIPMAP_NEAREST";
  1595. ETextureFilterType[ETextureFilterType["NEAREST_MIPMAP_LINEAR"] = 9986] = "NEAREST_MIPMAP_LINEAR";
  1596. ETextureFilterType[ETextureFilterType["LINEAR_MIPMAP_LINEAR"] = 9987] = "LINEAR_MIPMAP_LINEAR";
  1597. })(ETextureFilterType = GLTF1.ETextureFilterType || (GLTF1.ETextureFilterType = {}));
  1598. var ETextureFormat;
  1599. (function (ETextureFormat) {
  1600. ETextureFormat[ETextureFormat["ALPHA"] = 6406] = "ALPHA";
  1601. ETextureFormat[ETextureFormat["RGB"] = 6407] = "RGB";
  1602. ETextureFormat[ETextureFormat["RGBA"] = 6408] = "RGBA";
  1603. ETextureFormat[ETextureFormat["LUMINANCE"] = 6409] = "LUMINANCE";
  1604. ETextureFormat[ETextureFormat["LUMINANCE_ALPHA"] = 6410] = "LUMINANCE_ALPHA";
  1605. })(ETextureFormat = GLTF1.ETextureFormat || (GLTF1.ETextureFormat = {}));
  1606. var ECullingType;
  1607. (function (ECullingType) {
  1608. ECullingType[ECullingType["FRONT"] = 1028] = "FRONT";
  1609. ECullingType[ECullingType["BACK"] = 1029] = "BACK";
  1610. ECullingType[ECullingType["FRONT_AND_BACK"] = 1032] = "FRONT_AND_BACK";
  1611. })(ECullingType = GLTF1.ECullingType || (GLTF1.ECullingType = {}));
  1612. var EBlendingFunction;
  1613. (function (EBlendingFunction) {
  1614. EBlendingFunction[EBlendingFunction["ZERO"] = 0] = "ZERO";
  1615. EBlendingFunction[EBlendingFunction["ONE"] = 1] = "ONE";
  1616. EBlendingFunction[EBlendingFunction["SRC_COLOR"] = 768] = "SRC_COLOR";
  1617. EBlendingFunction[EBlendingFunction["ONE_MINUS_SRC_COLOR"] = 769] = "ONE_MINUS_SRC_COLOR";
  1618. EBlendingFunction[EBlendingFunction["DST_COLOR"] = 774] = "DST_COLOR";
  1619. EBlendingFunction[EBlendingFunction["ONE_MINUS_DST_COLOR"] = 775] = "ONE_MINUS_DST_COLOR";
  1620. EBlendingFunction[EBlendingFunction["SRC_ALPHA"] = 770] = "SRC_ALPHA";
  1621. EBlendingFunction[EBlendingFunction["ONE_MINUS_SRC_ALPHA"] = 771] = "ONE_MINUS_SRC_ALPHA";
  1622. EBlendingFunction[EBlendingFunction["DST_ALPHA"] = 772] = "DST_ALPHA";
  1623. EBlendingFunction[EBlendingFunction["ONE_MINUS_DST_ALPHA"] = 773] = "ONE_MINUS_DST_ALPHA";
  1624. EBlendingFunction[EBlendingFunction["CONSTANT_COLOR"] = 32769] = "CONSTANT_COLOR";
  1625. EBlendingFunction[EBlendingFunction["ONE_MINUS_CONSTANT_COLOR"] = 32770] = "ONE_MINUS_CONSTANT_COLOR";
  1626. EBlendingFunction[EBlendingFunction["CONSTANT_ALPHA"] = 32771] = "CONSTANT_ALPHA";
  1627. EBlendingFunction[EBlendingFunction["ONE_MINUS_CONSTANT_ALPHA"] = 32772] = "ONE_MINUS_CONSTANT_ALPHA";
  1628. EBlendingFunction[EBlendingFunction["SRC_ALPHA_SATURATE"] = 776] = "SRC_ALPHA_SATURATE";
  1629. })(EBlendingFunction = GLTF1.EBlendingFunction || (GLTF1.EBlendingFunction = {}));
  1630. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  1631. })(BABYLON || (BABYLON = {}));
  1632. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  1633. var BABYLON;
  1634. (function (BABYLON) {
  1635. var GLTF1;
  1636. (function (GLTF1) {
  1637. /**
  1638. * Tokenizer. Used for shaders compatibility
  1639. * Automatically map world, view, projection, worldViewProjection, attributes and so on
  1640. */
  1641. var ETokenType;
  1642. (function (ETokenType) {
  1643. ETokenType[ETokenType["IDENTIFIER"] = 1] = "IDENTIFIER";
  1644. ETokenType[ETokenType["UNKNOWN"] = 2] = "UNKNOWN";
  1645. ETokenType[ETokenType["END_OF_INPUT"] = 3] = "END_OF_INPUT";
  1646. })(ETokenType || (ETokenType = {}));
  1647. var Tokenizer = /** @class */ (function () {
  1648. function Tokenizer(toParse) {
  1649. this._pos = 0;
  1650. this.currentToken = ETokenType.UNKNOWN;
  1651. this.currentIdentifier = "";
  1652. this.currentString = "";
  1653. this.isLetterOrDigitPattern = /^[a-zA-Z0-9]+$/;
  1654. this._toParse = toParse;
  1655. this._maxPos = toParse.length;
  1656. }
  1657. Tokenizer.prototype.getNextToken = function () {
  1658. if (this.isEnd())
  1659. return ETokenType.END_OF_INPUT;
  1660. this.currentString = this.read();
  1661. this.currentToken = ETokenType.UNKNOWN;
  1662. if (this.currentString === "_" || this.isLetterOrDigitPattern.test(this.currentString)) {
  1663. this.currentToken = ETokenType.IDENTIFIER;
  1664. this.currentIdentifier = this.currentString;
  1665. while (!this.isEnd() && (this.isLetterOrDigitPattern.test(this.currentString = this.peek()) || this.currentString === "_")) {
  1666. this.currentIdentifier += this.currentString;
  1667. this.forward();
  1668. }
  1669. }
  1670. return this.currentToken;
  1671. };
  1672. Tokenizer.prototype.peek = function () {
  1673. return this._toParse[this._pos];
  1674. };
  1675. Tokenizer.prototype.read = function () {
  1676. return this._toParse[this._pos++];
  1677. };
  1678. Tokenizer.prototype.forward = function () {
  1679. this._pos++;
  1680. };
  1681. Tokenizer.prototype.isEnd = function () {
  1682. return this._pos >= this._maxPos;
  1683. };
  1684. return Tokenizer;
  1685. }());
  1686. /**
  1687. * Values
  1688. */
  1689. var glTFTransforms = ["MODEL", "VIEW", "PROJECTION", "MODELVIEW", "MODELVIEWPROJECTION", "JOINTMATRIX"];
  1690. var babylonTransforms = ["world", "view", "projection", "worldView", "worldViewProjection", "mBones"];
  1691. var glTFAnimationPaths = ["translation", "rotation", "scale"];
  1692. var babylonAnimationPaths = ["position", "rotationQuaternion", "scaling"];
  1693. /**
  1694. * Parse
  1695. */
  1696. var parseBuffers = function (parsedBuffers, gltfRuntime) {
  1697. for (var buf in parsedBuffers) {
  1698. var parsedBuffer = parsedBuffers[buf];
  1699. gltfRuntime.buffers[buf] = parsedBuffer;
  1700. gltfRuntime.buffersCount++;
  1701. }
  1702. };
  1703. var parseShaders = function (parsedShaders, gltfRuntime) {
  1704. for (var sha in parsedShaders) {
  1705. var parsedShader = parsedShaders[sha];
  1706. gltfRuntime.shaders[sha] = parsedShader;
  1707. gltfRuntime.shaderscount++;
  1708. }
  1709. };
  1710. var parseObject = function (parsedObjects, runtimeProperty, gltfRuntime) {
  1711. for (var object in parsedObjects) {
  1712. var parsedObject = parsedObjects[object];
  1713. gltfRuntime[runtimeProperty][object] = parsedObject;
  1714. }
  1715. };
  1716. /**
  1717. * Utils
  1718. */
  1719. var normalizeUVs = function (buffer) {
  1720. if (!buffer) {
  1721. return;
  1722. }
  1723. for (var i = 0; i < buffer.length / 2; i++) {
  1724. buffer[i * 2 + 1] = 1.0 - buffer[i * 2 + 1];
  1725. }
  1726. };
  1727. var getAttribute = function (attributeParameter) {
  1728. if (attributeParameter.semantic === "NORMAL") {
  1729. return "normal";
  1730. }
  1731. else if (attributeParameter.semantic === "POSITION") {
  1732. return "position";
  1733. }
  1734. else if (attributeParameter.semantic === "JOINT") {
  1735. return "matricesIndices";
  1736. }
  1737. else if (attributeParameter.semantic === "WEIGHT") {
  1738. return "matricesWeights";
  1739. }
  1740. else if (attributeParameter.semantic === "COLOR") {
  1741. return "color";
  1742. }
  1743. else if (attributeParameter.semantic && attributeParameter.semantic.indexOf("TEXCOORD_") !== -1) {
  1744. var channel = Number(attributeParameter.semantic.split("_")[1]);
  1745. return "uv" + (channel === 0 ? "" : channel + 1);
  1746. }
  1747. return null;
  1748. };
  1749. /**
  1750. * Loads and creates animations
  1751. */
  1752. var loadAnimations = function (gltfRuntime) {
  1753. for (var anim in gltfRuntime.animations) {
  1754. var animation = gltfRuntime.animations[anim];
  1755. if (!animation.channels || !animation.samplers) {
  1756. continue;
  1757. }
  1758. var lastAnimation = null;
  1759. for (var i = 0; i < animation.channels.length; i++) {
  1760. // Get parameters and load buffers
  1761. var channel = animation.channels[i];
  1762. var sampler = animation.samplers[channel.sampler];
  1763. if (!sampler) {
  1764. continue;
  1765. }
  1766. var inputData = null;
  1767. var outputData = null;
  1768. if (animation.parameters) {
  1769. inputData = animation.parameters[sampler.input];
  1770. outputData = animation.parameters[sampler.output];
  1771. }
  1772. else {
  1773. inputData = sampler.input;
  1774. outputData = sampler.output;
  1775. }
  1776. var bufferInput = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[inputData]);
  1777. var bufferOutput = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[outputData]);
  1778. var targetID = channel.target.id;
  1779. var targetNode = gltfRuntime.scene.getNodeByID(targetID);
  1780. if (targetNode === null) {
  1781. targetNode = gltfRuntime.scene.getNodeByName(targetID);
  1782. }
  1783. if (targetNode === null) {
  1784. BABYLON.Tools.Warn("Creating animation named " + anim + ". But cannot find node named " + targetID + " to attach to");
  1785. continue;
  1786. }
  1787. var isBone = targetNode instanceof BABYLON.Bone;
  1788. // Get target path (position, rotation or scaling)
  1789. var targetPath = channel.target.path;
  1790. var targetPathIndex = glTFAnimationPaths.indexOf(targetPath);
  1791. if (targetPathIndex !== -1) {
  1792. targetPath = babylonAnimationPaths[targetPathIndex];
  1793. }
  1794. // Determine animation type
  1795. var animationType = BABYLON.Animation.ANIMATIONTYPE_MATRIX;
  1796. if (!isBone) {
  1797. if (targetPath === "rotationQuaternion") {
  1798. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  1799. targetNode.rotationQuaternion = new BABYLON.Quaternion();
  1800. }
  1801. else {
  1802. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1803. }
  1804. }
  1805. // Create animation and key frames
  1806. var babylonAnimation = null;
  1807. var keys = [];
  1808. var arrayOffset = 0;
  1809. var modifyKey = false;
  1810. if (isBone && lastAnimation && lastAnimation.getKeys().length === bufferInput.length) {
  1811. babylonAnimation = lastAnimation;
  1812. modifyKey = true;
  1813. }
  1814. if (!modifyKey) {
  1815. babylonAnimation = new BABYLON.Animation(anim, isBone ? "_matrix" : targetPath, 1, animationType, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  1816. }
  1817. // For each frame
  1818. for (var j = 0; j < bufferInput.length; j++) {
  1819. var value = null;
  1820. if (targetPath === "rotationQuaternion") { // VEC4
  1821. value = BABYLON.Quaternion.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2], bufferOutput[arrayOffset + 3]]);
  1822. arrayOffset += 4;
  1823. }
  1824. else { // Position and scaling are VEC3
  1825. value = BABYLON.Vector3.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2]]);
  1826. arrayOffset += 3;
  1827. }
  1828. if (isBone) {
  1829. var bone = targetNode;
  1830. var translation = BABYLON.Vector3.Zero();
  1831. var rotationQuaternion = new BABYLON.Quaternion();
  1832. var scaling = BABYLON.Vector3.Zero();
  1833. // Warning on decompose
  1834. var mat = bone.getBaseMatrix();
  1835. if (modifyKey && lastAnimation) {
  1836. mat = lastAnimation.getKeys()[j].value;
  1837. }
  1838. mat.decompose(scaling, rotationQuaternion, translation);
  1839. if (targetPath === "position") {
  1840. translation = value;
  1841. }
  1842. else if (targetPath === "rotationQuaternion") {
  1843. rotationQuaternion = value;
  1844. }
  1845. else {
  1846. scaling = value;
  1847. }
  1848. value = BABYLON.Matrix.Compose(scaling, rotationQuaternion, translation);
  1849. }
  1850. if (!modifyKey) {
  1851. keys.push({
  1852. frame: bufferInput[j],
  1853. value: value
  1854. });
  1855. }
  1856. else if (lastAnimation) {
  1857. lastAnimation.getKeys()[j].value = value;
  1858. }
  1859. }
  1860. // Finish
  1861. if (!modifyKey && babylonAnimation) {
  1862. babylonAnimation.setKeys(keys);
  1863. targetNode.animations.push(babylonAnimation);
  1864. }
  1865. lastAnimation = babylonAnimation;
  1866. gltfRuntime.scene.stopAnimation(targetNode);
  1867. gltfRuntime.scene.beginAnimation(targetNode, 0, bufferInput[bufferInput.length - 1], true, 1.0);
  1868. }
  1869. }
  1870. };
  1871. /**
  1872. * Returns the bones transformation matrix
  1873. */
  1874. var configureBoneTransformation = function (node) {
  1875. var mat = null;
  1876. if (node.translation || node.rotation || node.scale) {
  1877. var scale = BABYLON.Vector3.FromArray(node.scale || [1, 1, 1]);
  1878. var rotation = BABYLON.Quaternion.FromArray(node.rotation || [0, 0, 0, 1]);
  1879. var position = BABYLON.Vector3.FromArray(node.translation || [0, 0, 0]);
  1880. mat = BABYLON.Matrix.Compose(scale, rotation, position);
  1881. }
  1882. else {
  1883. mat = BABYLON.Matrix.FromArray(node.matrix);
  1884. }
  1885. return mat;
  1886. };
  1887. /**
  1888. * Returns the parent bone
  1889. */
  1890. var getParentBone = function (gltfRuntime, skins, jointName, newSkeleton) {
  1891. // Try to find
  1892. for (var i = 0; i < newSkeleton.bones.length; i++) {
  1893. if (newSkeleton.bones[i].name === jointName) {
  1894. return newSkeleton.bones[i];
  1895. }
  1896. }
  1897. // Not found, search in gltf nodes
  1898. var nodes = gltfRuntime.nodes;
  1899. for (var nde in nodes) {
  1900. var node = nodes[nde];
  1901. if (!node.jointName) {
  1902. continue;
  1903. }
  1904. var children = node.children;
  1905. for (var i = 0; i < children.length; i++) {
  1906. var child = gltfRuntime.nodes[children[i]];
  1907. if (!child.jointName) {
  1908. continue;
  1909. }
  1910. if (child.jointName === jointName) {
  1911. var mat = configureBoneTransformation(node);
  1912. var bone = new BABYLON.Bone(node.name || "", newSkeleton, getParentBone(gltfRuntime, skins, node.jointName, newSkeleton), mat);
  1913. bone.id = nde;
  1914. return bone;
  1915. }
  1916. }
  1917. }
  1918. return null;
  1919. };
  1920. /**
  1921. * Returns the appropriate root node
  1922. */
  1923. var getNodeToRoot = function (nodesToRoot, id) {
  1924. for (var i = 0; i < nodesToRoot.length; i++) {
  1925. var nodeToRoot = nodesToRoot[i];
  1926. for (var j = 0; j < nodeToRoot.node.children.length; j++) {
  1927. var child = nodeToRoot.node.children[j];
  1928. if (child === id) {
  1929. return nodeToRoot.bone;
  1930. }
  1931. }
  1932. }
  1933. return null;
  1934. };
  1935. /**
  1936. * Returns the node with the joint name
  1937. */
  1938. var getJointNode = function (gltfRuntime, jointName) {
  1939. var nodes = gltfRuntime.nodes;
  1940. var node = nodes[jointName];
  1941. if (node) {
  1942. return {
  1943. node: node,
  1944. id: jointName
  1945. };
  1946. }
  1947. for (var nde in nodes) {
  1948. node = nodes[nde];
  1949. if (node.jointName === jointName) {
  1950. return {
  1951. node: node,
  1952. id: nde
  1953. };
  1954. }
  1955. }
  1956. return null;
  1957. };
  1958. /**
  1959. * Checks if a nodes is in joints
  1960. */
  1961. var nodeIsInJoints = function (skins, id) {
  1962. for (var i = 0; i < skins.jointNames.length; i++) {
  1963. if (skins.jointNames[i] === id) {
  1964. return true;
  1965. }
  1966. }
  1967. return false;
  1968. };
  1969. /**
  1970. * Fills the nodes to root for bones and builds hierarchy
  1971. */
  1972. var getNodesToRoot = function (gltfRuntime, newSkeleton, skins, nodesToRoot) {
  1973. // Creates nodes for root
  1974. for (var nde in gltfRuntime.nodes) {
  1975. var node = gltfRuntime.nodes[nde];
  1976. var id = nde;
  1977. if (!node.jointName || nodeIsInJoints(skins, node.jointName)) {
  1978. continue;
  1979. }
  1980. // Create node to root bone
  1981. var mat = configureBoneTransformation(node);
  1982. var bone = new BABYLON.Bone(node.name || "", newSkeleton, null, mat);
  1983. bone.id = id;
  1984. nodesToRoot.push({ bone: bone, node: node, id: id });
  1985. }
  1986. // Parenting
  1987. for (var i = 0; i < nodesToRoot.length; i++) {
  1988. var nodeToRoot = nodesToRoot[i];
  1989. var children = nodeToRoot.node.children;
  1990. for (var j = 0; j < children.length; j++) {
  1991. var child = null;
  1992. for (var k = 0; k < nodesToRoot.length; k++) {
  1993. if (nodesToRoot[k].id === children[j]) {
  1994. child = nodesToRoot[k];
  1995. break;
  1996. }
  1997. }
  1998. if (child) {
  1999. child.bone._parent = nodeToRoot.bone;
  2000. nodeToRoot.bone.children.push(child.bone);
  2001. }
  2002. }
  2003. }
  2004. };
  2005. /**
  2006. * Imports a skeleton
  2007. */
  2008. var importSkeleton = function (gltfRuntime, skins, mesh, newSkeleton, id) {
  2009. if (!newSkeleton) {
  2010. newSkeleton = new BABYLON.Skeleton(skins.name || "", "", gltfRuntime.scene);
  2011. }
  2012. if (!skins.babylonSkeleton) {
  2013. return newSkeleton;
  2014. }
  2015. // Find the root bones
  2016. var nodesToRoot = [];
  2017. var nodesToRootToAdd = [];
  2018. getNodesToRoot(gltfRuntime, newSkeleton, skins, nodesToRoot);
  2019. newSkeleton.bones = [];
  2020. // Joints
  2021. for (var i = 0; i < skins.jointNames.length; i++) {
  2022. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  2023. if (!jointNode) {
  2024. continue;
  2025. }
  2026. var node = jointNode.node;
  2027. if (!node) {
  2028. BABYLON.Tools.Warn("Joint named " + skins.jointNames[i] + " does not exist");
  2029. continue;
  2030. }
  2031. var id = jointNode.id;
  2032. // Optimize, if the bone already exists...
  2033. var existingBone = gltfRuntime.scene.getBoneByID(id);
  2034. if (existingBone) {
  2035. newSkeleton.bones.push(existingBone);
  2036. continue;
  2037. }
  2038. // Search for parent bone
  2039. var foundBone = false;
  2040. var parentBone = null;
  2041. for (var j = 0; j < i; j++) {
  2042. var jointNode_1 = getJointNode(gltfRuntime, skins.jointNames[j]);
  2043. if (!jointNode_1) {
  2044. continue;
  2045. }
  2046. var joint = jointNode_1.node;
  2047. if (!joint) {
  2048. BABYLON.Tools.Warn("Joint named " + skins.jointNames[j] + " does not exist when looking for parent");
  2049. continue;
  2050. }
  2051. var children = joint.children;
  2052. if (!children) {
  2053. continue;
  2054. }
  2055. foundBone = false;
  2056. for (var k = 0; k < children.length; k++) {
  2057. if (children[k] === id) {
  2058. parentBone = getParentBone(gltfRuntime, skins, skins.jointNames[j], newSkeleton);
  2059. foundBone = true;
  2060. break;
  2061. }
  2062. }
  2063. if (foundBone) {
  2064. break;
  2065. }
  2066. }
  2067. // Create bone
  2068. var mat = configureBoneTransformation(node);
  2069. if (!parentBone && nodesToRoot.length > 0) {
  2070. parentBone = getNodeToRoot(nodesToRoot, id);
  2071. if (parentBone) {
  2072. if (nodesToRootToAdd.indexOf(parentBone) === -1) {
  2073. nodesToRootToAdd.push(parentBone);
  2074. }
  2075. }
  2076. }
  2077. var bone = new BABYLON.Bone(node.jointName || "", newSkeleton, parentBone, mat);
  2078. bone.id = id;
  2079. }
  2080. // Polish
  2081. var bones = newSkeleton.bones;
  2082. newSkeleton.bones = [];
  2083. for (var i = 0; i < skins.jointNames.length; i++) {
  2084. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  2085. if (!jointNode) {
  2086. continue;
  2087. }
  2088. for (var j = 0; j < bones.length; j++) {
  2089. if (bones[j].id === jointNode.id) {
  2090. newSkeleton.bones.push(bones[j]);
  2091. break;
  2092. }
  2093. }
  2094. }
  2095. newSkeleton.prepare();
  2096. // Finish
  2097. for (var i = 0; i < nodesToRootToAdd.length; i++) {
  2098. newSkeleton.bones.push(nodesToRootToAdd[i]);
  2099. }
  2100. return newSkeleton;
  2101. };
  2102. /**
  2103. * Imports a mesh and its geometries
  2104. */
  2105. var importMesh = function (gltfRuntime, node, meshes, id, newMesh) {
  2106. if (!newMesh) {
  2107. newMesh = new BABYLON.Mesh(node.name || "", gltfRuntime.scene);
  2108. newMesh.id = id;
  2109. }
  2110. if (!node.babylonNode) {
  2111. return newMesh;
  2112. }
  2113. var subMaterials = [];
  2114. var vertexData = null;
  2115. var verticesStarts = new Array();
  2116. var verticesCounts = new Array();
  2117. var indexStarts = new Array();
  2118. var indexCounts = new Array();
  2119. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  2120. var meshID = meshes[meshIndex];
  2121. var mesh = gltfRuntime.meshes[meshID];
  2122. if (!mesh) {
  2123. continue;
  2124. }
  2125. // Positions, normals and UVs
  2126. for (var i = 0; i < mesh.primitives.length; i++) {
  2127. // Temporary vertex data
  2128. var tempVertexData = new BABYLON.VertexData();
  2129. var primitive = mesh.primitives[i];
  2130. if (primitive.mode !== 4) {
  2131. // continue;
  2132. }
  2133. var attributes = primitive.attributes;
  2134. var accessor = null;
  2135. var buffer = null;
  2136. // Set positions, normal and uvs
  2137. for (var semantic in attributes) {
  2138. // Link accessor and buffer view
  2139. accessor = gltfRuntime.accessors[attributes[semantic]];
  2140. buffer = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  2141. if (semantic === "NORMAL") {
  2142. tempVertexData.normals = new Float32Array(buffer.length);
  2143. tempVertexData.normals.set(buffer);
  2144. }
  2145. else if (semantic === "POSITION") {
  2146. if (BABYLON.GLTFFileLoader.HomogeneousCoordinates) {
  2147. tempVertexData.positions = new Float32Array(buffer.length - buffer.length / 4);
  2148. for (var j = 0; j < buffer.length; j += 4) {
  2149. tempVertexData.positions[j] = buffer[j];
  2150. tempVertexData.positions[j + 1] = buffer[j + 1];
  2151. tempVertexData.positions[j + 2] = buffer[j + 2];
  2152. }
  2153. }
  2154. else {
  2155. tempVertexData.positions = new Float32Array(buffer.length);
  2156. tempVertexData.positions.set(buffer);
  2157. }
  2158. verticesCounts.push(tempVertexData.positions.length);
  2159. }
  2160. else if (semantic.indexOf("TEXCOORD_") !== -1) {
  2161. var channel = Number(semantic.split("_")[1]);
  2162. var uvKind = BABYLON.VertexBuffer.UVKind + (channel === 0 ? "" : (channel + 1));
  2163. var uvs = new Float32Array(buffer.length);
  2164. uvs.set(buffer);
  2165. normalizeUVs(uvs);
  2166. tempVertexData.set(uvs, uvKind);
  2167. }
  2168. else if (semantic === "JOINT") {
  2169. tempVertexData.matricesIndices = new Float32Array(buffer.length);
  2170. tempVertexData.matricesIndices.set(buffer);
  2171. }
  2172. else if (semantic === "WEIGHT") {
  2173. tempVertexData.matricesWeights = new Float32Array(buffer.length);
  2174. tempVertexData.matricesWeights.set(buffer);
  2175. }
  2176. else if (semantic === "COLOR") {
  2177. tempVertexData.colors = new Float32Array(buffer.length);
  2178. tempVertexData.colors.set(buffer);
  2179. }
  2180. }
  2181. // Indices
  2182. accessor = gltfRuntime.accessors[primitive.indices];
  2183. if (accessor) {
  2184. buffer = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  2185. tempVertexData.indices = new Int32Array(buffer.length);
  2186. tempVertexData.indices.set(buffer);
  2187. indexCounts.push(tempVertexData.indices.length);
  2188. }
  2189. else {
  2190. // Set indices on the fly
  2191. var indices = [];
  2192. for (var j = 0; j < tempVertexData.positions.length / 3; j++) {
  2193. indices.push(j);
  2194. }
  2195. tempVertexData.indices = new Int32Array(indices);
  2196. indexCounts.push(tempVertexData.indices.length);
  2197. }
  2198. if (!vertexData) {
  2199. vertexData = tempVertexData;
  2200. }
  2201. else {
  2202. vertexData.merge(tempVertexData);
  2203. }
  2204. // Sub material
  2205. var material_1 = gltfRuntime.scene.getMaterialByID(primitive.material);
  2206. subMaterials.push(material_1 === null ? GLTF1.GLTFUtils.GetDefaultMaterial(gltfRuntime.scene) : material_1);
  2207. // Update vertices start and index start
  2208. verticesStarts.push(verticesStarts.length === 0 ? 0 : verticesStarts[verticesStarts.length - 1] + verticesCounts[verticesCounts.length - 2]);
  2209. indexStarts.push(indexStarts.length === 0 ? 0 : indexStarts[indexStarts.length - 1] + indexCounts[indexCounts.length - 2]);
  2210. }
  2211. }
  2212. var material;
  2213. if (subMaterials.length > 1) {
  2214. material = new BABYLON.MultiMaterial("multimat" + id, gltfRuntime.scene);
  2215. material.subMaterials = subMaterials;
  2216. }
  2217. else {
  2218. material = new BABYLON.StandardMaterial("multimat" + id, gltfRuntime.scene);
  2219. }
  2220. if (subMaterials.length === 1) {
  2221. material = subMaterials[0];
  2222. }
  2223. if (!newMesh.material) {
  2224. newMesh.material = material;
  2225. }
  2226. // Apply geometry
  2227. new BABYLON.Geometry(id, gltfRuntime.scene, vertexData, false, newMesh);
  2228. newMesh.computeWorldMatrix(true);
  2229. // Apply submeshes
  2230. newMesh.subMeshes = [];
  2231. var index = 0;
  2232. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  2233. var meshID = meshes[meshIndex];
  2234. var mesh = gltfRuntime.meshes[meshID];
  2235. if (!mesh) {
  2236. continue;
  2237. }
  2238. for (var i = 0; i < mesh.primitives.length; i++) {
  2239. if (mesh.primitives[i].mode !== 4) {
  2240. //continue;
  2241. }
  2242. BABYLON.SubMesh.AddToMesh(index, verticesStarts[index], verticesCounts[index], indexStarts[index], indexCounts[index], newMesh, newMesh, true);
  2243. index++;
  2244. }
  2245. }
  2246. // Finish
  2247. return newMesh;
  2248. };
  2249. /**
  2250. * Configure node transformation from position, rotation and scaling
  2251. */
  2252. var configureNode = function (newNode, position, rotation, scaling) {
  2253. if (newNode.position) {
  2254. newNode.position = position;
  2255. }
  2256. if (newNode.rotationQuaternion || newNode.rotation) {
  2257. newNode.rotationQuaternion = rotation;
  2258. }
  2259. if (newNode.scaling) {
  2260. newNode.scaling = scaling;
  2261. }
  2262. };
  2263. /**
  2264. * Configures node from transformation matrix
  2265. */
  2266. var configureNodeFromMatrix = function (newNode, node, parent) {
  2267. if (node.matrix) {
  2268. var position = new BABYLON.Vector3(0, 0, 0);
  2269. var rotation = new BABYLON.Quaternion();
  2270. var scaling = new BABYLON.Vector3(0, 0, 0);
  2271. var mat = BABYLON.Matrix.FromArray(node.matrix);
  2272. mat.decompose(scaling, rotation, position);
  2273. configureNode(newNode, position, rotation, scaling);
  2274. }
  2275. else if (node.translation && node.rotation && node.scale) {
  2276. configureNode(newNode, BABYLON.Vector3.FromArray(node.translation), BABYLON.Quaternion.FromArray(node.rotation), BABYLON.Vector3.FromArray(node.scale));
  2277. }
  2278. newNode.computeWorldMatrix(true);
  2279. };
  2280. /**
  2281. * Imports a node
  2282. */
  2283. var importNode = function (gltfRuntime, node, id, parent) {
  2284. var lastNode = null;
  2285. if (gltfRuntime.importOnlyMeshes && (node.skin || node.meshes)) {
  2286. if (gltfRuntime.importMeshesNames && gltfRuntime.importMeshesNames.length > 0 && gltfRuntime.importMeshesNames.indexOf(node.name || "") === -1) {
  2287. return null;
  2288. }
  2289. }
  2290. // Meshes
  2291. if (node.skin) {
  2292. if (node.meshes) {
  2293. var skin = gltfRuntime.skins[node.skin];
  2294. var newMesh = importMesh(gltfRuntime, node, node.meshes, id, node.babylonNode);
  2295. newMesh.skeleton = gltfRuntime.scene.getLastSkeletonByID(node.skin);
  2296. if (newMesh.skeleton === null) {
  2297. newMesh.skeleton = importSkeleton(gltfRuntime, skin, newMesh, skin.babylonSkeleton, node.skin);
  2298. if (!skin.babylonSkeleton) {
  2299. skin.babylonSkeleton = newMesh.skeleton;
  2300. }
  2301. }
  2302. lastNode = newMesh;
  2303. }
  2304. }
  2305. else if (node.meshes) {
  2306. /**
  2307. * Improve meshes property
  2308. */
  2309. var newMesh = importMesh(gltfRuntime, node, node.mesh ? [node.mesh] : node.meshes, id, node.babylonNode);
  2310. lastNode = newMesh;
  2311. }
  2312. // Lights
  2313. else if (node.light && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  2314. var light = gltfRuntime.lights[node.light];
  2315. if (light) {
  2316. if (light.type === "ambient") {
  2317. var ambienLight = light[light.type];
  2318. var hemiLight = new BABYLON.HemisphericLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  2319. hemiLight.name = node.name || "";
  2320. if (ambienLight.color) {
  2321. hemiLight.diffuse = BABYLON.Color3.FromArray(ambienLight.color);
  2322. }
  2323. lastNode = hemiLight;
  2324. }
  2325. else if (light.type === "directional") {
  2326. var directionalLight = light[light.type];
  2327. var dirLight = new BABYLON.DirectionalLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  2328. dirLight.name = node.name || "";
  2329. if (directionalLight.color) {
  2330. dirLight.diffuse = BABYLON.Color3.FromArray(directionalLight.color);
  2331. }
  2332. lastNode = dirLight;
  2333. }
  2334. else if (light.type === "point") {
  2335. var pointLight = light[light.type];
  2336. var ptLight = new BABYLON.PointLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  2337. ptLight.name = node.name || "";
  2338. if (pointLight.color) {
  2339. ptLight.diffuse = BABYLON.Color3.FromArray(pointLight.color);
  2340. }
  2341. lastNode = ptLight;
  2342. }
  2343. else if (light.type === "spot") {
  2344. var spotLight = light[light.type];
  2345. var spLight = new BABYLON.SpotLight(node.light, BABYLON.Vector3.Zero(), BABYLON.Vector3.Zero(), 0, 0, gltfRuntime.scene);
  2346. spLight.name = node.name || "";
  2347. if (spotLight.color) {
  2348. spLight.diffuse = BABYLON.Color3.FromArray(spotLight.color);
  2349. }
  2350. if (spotLight.fallOfAngle) {
  2351. spLight.angle = spotLight.fallOfAngle;
  2352. }
  2353. if (spotLight.fallOffExponent) {
  2354. spLight.exponent = spotLight.fallOffExponent;
  2355. }
  2356. lastNode = spLight;
  2357. }
  2358. }
  2359. }
  2360. // Cameras
  2361. else if (node.camera && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  2362. var camera = gltfRuntime.cameras[node.camera];
  2363. if (camera) {
  2364. if (camera.type === "orthographic") {
  2365. var orthoCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  2366. orthoCamera.name = node.name || "";
  2367. orthoCamera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
  2368. orthoCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  2369. lastNode = orthoCamera;
  2370. }
  2371. else if (camera.type === "perspective") {
  2372. var perspectiveCamera = camera[camera.type];
  2373. var persCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  2374. persCamera.name = node.name || "";
  2375. persCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  2376. if (!perspectiveCamera.aspectRatio) {
  2377. perspectiveCamera.aspectRatio = gltfRuntime.scene.getEngine().getRenderWidth() / gltfRuntime.scene.getEngine().getRenderHeight();
  2378. }
  2379. if (perspectiveCamera.znear && perspectiveCamera.zfar) {
  2380. persCamera.maxZ = perspectiveCamera.zfar;
  2381. persCamera.minZ = perspectiveCamera.znear;
  2382. }
  2383. lastNode = persCamera;
  2384. }
  2385. }
  2386. }
  2387. // Empty node
  2388. if (!node.jointName) {
  2389. if (node.babylonNode) {
  2390. return node.babylonNode;
  2391. }
  2392. else if (lastNode === null) {
  2393. var dummy = new BABYLON.Mesh(node.name || "", gltfRuntime.scene);
  2394. node.babylonNode = dummy;
  2395. lastNode = dummy;
  2396. }
  2397. }
  2398. if (lastNode !== null) {
  2399. if (node.matrix && lastNode instanceof BABYLON.Mesh) {
  2400. configureNodeFromMatrix(lastNode, node, parent);
  2401. }
  2402. else {
  2403. var translation = node.translation || [0, 0, 0];
  2404. var rotation = node.rotation || [0, 0, 0, 1];
  2405. var scale = node.scale || [1, 1, 1];
  2406. configureNode(lastNode, BABYLON.Vector3.FromArray(translation), BABYLON.Quaternion.FromArray(rotation), BABYLON.Vector3.FromArray(scale));
  2407. }
  2408. lastNode.updateCache(true);
  2409. node.babylonNode = lastNode;
  2410. }
  2411. return lastNode;
  2412. };
  2413. /**
  2414. * Traverses nodes and creates them
  2415. */
  2416. var traverseNodes = function (gltfRuntime, id, parent, meshIncluded) {
  2417. if (meshIncluded === void 0) { meshIncluded = false; }
  2418. var node = gltfRuntime.nodes[id];
  2419. var newNode = null;
  2420. if (gltfRuntime.importOnlyMeshes && !meshIncluded && gltfRuntime.importMeshesNames) {
  2421. if (gltfRuntime.importMeshesNames.indexOf(node.name || "") !== -1 || gltfRuntime.importMeshesNames.length === 0) {
  2422. meshIncluded = true;
  2423. }
  2424. else {
  2425. meshIncluded = false;
  2426. }
  2427. }
  2428. else {
  2429. meshIncluded = true;
  2430. }
  2431. if (!node.jointName && meshIncluded) {
  2432. newNode = importNode(gltfRuntime, node, id, parent);
  2433. if (newNode !== null) {
  2434. newNode.id = id;
  2435. newNode.parent = parent;
  2436. }
  2437. }
  2438. if (node.children) {
  2439. for (var i = 0; i < node.children.length; i++) {
  2440. traverseNodes(gltfRuntime, node.children[i], newNode, meshIncluded);
  2441. }
  2442. }
  2443. };
  2444. /**
  2445. * do stuff after buffers, shaders are loaded (e.g. hook up materials, load animations, etc.)
  2446. */
  2447. var postLoad = function (gltfRuntime) {
  2448. // Nodes
  2449. var currentScene = gltfRuntime.currentScene;
  2450. if (currentScene) {
  2451. for (var i = 0; i < currentScene.nodes.length; i++) {
  2452. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  2453. }
  2454. }
  2455. else {
  2456. for (var thing in gltfRuntime.scenes) {
  2457. currentScene = gltfRuntime.scenes[thing];
  2458. for (var i = 0; i < currentScene.nodes.length; i++) {
  2459. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  2460. }
  2461. }
  2462. }
  2463. // Set animations
  2464. loadAnimations(gltfRuntime);
  2465. for (var i = 0; i < gltfRuntime.scene.skeletons.length; i++) {
  2466. var skeleton = gltfRuntime.scene.skeletons[i];
  2467. gltfRuntime.scene.beginAnimation(skeleton, 0, Number.MAX_VALUE, true, 1.0);
  2468. }
  2469. };
  2470. /**
  2471. * onBind shaderrs callback to set uniforms and matrices
  2472. */
  2473. var onBindShaderMaterial = function (mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess) {
  2474. var materialValues = material.values || technique.parameters;
  2475. for (var unif in unTreatedUniforms) {
  2476. var uniform = unTreatedUniforms[unif];
  2477. var type = uniform.type;
  2478. if (type === GLTF1.EParameterType.FLOAT_MAT2 || type === GLTF1.EParameterType.FLOAT_MAT3 || type === GLTF1.EParameterType.FLOAT_MAT4) {
  2479. if (uniform.semantic && !uniform.source && !uniform.node) {
  2480. GLTF1.GLTFUtils.SetMatrix(gltfRuntime.scene, mesh, uniform, unif, shaderMaterial.getEffect());
  2481. }
  2482. else if (uniform.semantic && (uniform.source || uniform.node)) {
  2483. var source = gltfRuntime.scene.getNodeByName(uniform.source || uniform.node || "");
  2484. if (source === null) {
  2485. source = gltfRuntime.scene.getNodeByID(uniform.source || uniform.node || "");
  2486. }
  2487. if (source === null) {
  2488. continue;
  2489. }
  2490. GLTF1.GLTFUtils.SetMatrix(gltfRuntime.scene, source, uniform, unif, shaderMaterial.getEffect());
  2491. }
  2492. }
  2493. else {
  2494. var value = materialValues[technique.uniforms[unif]];
  2495. if (!value) {
  2496. continue;
  2497. }
  2498. if (type === GLTF1.EParameterType.SAMPLER_2D) {
  2499. var texture = gltfRuntime.textures[material.values ? value : uniform.value].babylonTexture;
  2500. if (texture === null || texture === undefined) {
  2501. continue;
  2502. }
  2503. shaderMaterial.getEffect().setTexture(unif, texture);
  2504. }
  2505. else {
  2506. GLTF1.GLTFUtils.SetUniform((shaderMaterial.getEffect()), unif, value, type);
  2507. }
  2508. }
  2509. }
  2510. onSuccess(shaderMaterial);
  2511. };
  2512. /**
  2513. * Prepare uniforms to send the only one time
  2514. * Loads the appropriate textures
  2515. */
  2516. var prepareShaderMaterialUniforms = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms) {
  2517. var materialValues = material.values || technique.parameters;
  2518. var techniqueUniforms = technique.uniforms;
  2519. /**
  2520. * Prepare values here (not matrices)
  2521. */
  2522. for (var unif in unTreatedUniforms) {
  2523. var uniform = unTreatedUniforms[unif];
  2524. var type = uniform.type;
  2525. var value = materialValues[techniqueUniforms[unif]];
  2526. if (value === undefined) {
  2527. // In case the value is the same for all materials
  2528. value = uniform.value;
  2529. }
  2530. if (!value) {
  2531. continue;
  2532. }
  2533. var onLoadTexture = function (uniformName) {
  2534. return function (texture) {
  2535. if (uniform.value && uniformName) {
  2536. // Static uniform
  2537. shaderMaterial.setTexture(uniformName, texture);
  2538. delete unTreatedUniforms[uniformName];
  2539. }
  2540. };
  2541. };
  2542. // Texture (sampler2D)
  2543. if (type === GLTF1.EParameterType.SAMPLER_2D) {
  2544. GLTF1.GLTFLoaderExtension.LoadTextureAsync(gltfRuntime, material.values ? value : uniform.value, onLoadTexture(unif), function () { return onLoadTexture(null); });
  2545. }
  2546. // Others
  2547. else {
  2548. if (uniform.value && GLTF1.GLTFUtils.SetUniform(shaderMaterial, unif, material.values ? value : uniform.value, type)) {
  2549. // Static uniform
  2550. delete unTreatedUniforms[unif];
  2551. }
  2552. }
  2553. }
  2554. };
  2555. /**
  2556. * Shader compilation failed
  2557. */
  2558. var onShaderCompileError = function (program, shaderMaterial, onError) {
  2559. return function (effect, error) {
  2560. shaderMaterial.dispose(true);
  2561. onError("Cannot compile program named " + program.name + ". Error: " + error + ". Default material will be applied");
  2562. };
  2563. };
  2564. /**
  2565. * Shader compilation success
  2566. */
  2567. var onShaderCompileSuccess = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess) {
  2568. return function (_) {
  2569. prepareShaderMaterialUniforms(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms);
  2570. shaderMaterial.onBind = function (mesh) {
  2571. onBindShaderMaterial(mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess);
  2572. };
  2573. };
  2574. };
  2575. /**
  2576. * Returns the appropriate uniform if already handled by babylon
  2577. */
  2578. var parseShaderUniforms = function (tokenizer, technique, unTreatedUniforms) {
  2579. for (var unif in technique.uniforms) {
  2580. var uniform = technique.uniforms[unif];
  2581. var uniformParameter = technique.parameters[uniform];
  2582. if (tokenizer.currentIdentifier === unif) {
  2583. if (uniformParameter.semantic && !uniformParameter.source && !uniformParameter.node) {
  2584. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  2585. if (transformIndex !== -1) {
  2586. delete unTreatedUniforms[unif];
  2587. return babylonTransforms[transformIndex];
  2588. }
  2589. }
  2590. }
  2591. }
  2592. return tokenizer.currentIdentifier;
  2593. };
  2594. /**
  2595. * All shaders loaded. Create materials one by one
  2596. */
  2597. var importMaterials = function (gltfRuntime) {
  2598. // Create materials
  2599. for (var mat in gltfRuntime.materials) {
  2600. GLTF1.GLTFLoaderExtension.LoadMaterialAsync(gltfRuntime, mat, function (material) { }, function () { });
  2601. }
  2602. };
  2603. /**
  2604. * Implementation of the base glTF spec
  2605. */
  2606. var GLTFLoaderBase = /** @class */ (function () {
  2607. function GLTFLoaderBase() {
  2608. }
  2609. GLTFLoaderBase.CreateRuntime = function (parsedData, scene, rootUrl) {
  2610. var gltfRuntime = {
  2611. extensions: {},
  2612. accessors: {},
  2613. buffers: {},
  2614. bufferViews: {},
  2615. meshes: {},
  2616. lights: {},
  2617. cameras: {},
  2618. nodes: {},
  2619. images: {},
  2620. textures: {},
  2621. shaders: {},
  2622. programs: {},
  2623. samplers: {},
  2624. techniques: {},
  2625. materials: {},
  2626. animations: {},
  2627. skins: {},
  2628. extensionsUsed: [],
  2629. scenes: {},
  2630. buffersCount: 0,
  2631. shaderscount: 0,
  2632. scene: scene,
  2633. rootUrl: rootUrl,
  2634. loadedBufferCount: 0,
  2635. loadedBufferViews: {},
  2636. loadedShaderCount: 0,
  2637. importOnlyMeshes: false,
  2638. dummyNodes: []
  2639. };
  2640. // Parse
  2641. if (parsedData.extensions) {
  2642. parseObject(parsedData.extensions, "extensions", gltfRuntime);
  2643. }
  2644. if (parsedData.extensionsUsed) {
  2645. parseObject(parsedData.extensionsUsed, "extensionsUsed", gltfRuntime);
  2646. }
  2647. if (parsedData.buffers) {
  2648. parseBuffers(parsedData.buffers, gltfRuntime);
  2649. }
  2650. if (parsedData.bufferViews) {
  2651. parseObject(parsedData.bufferViews, "bufferViews", gltfRuntime);
  2652. }
  2653. if (parsedData.accessors) {
  2654. parseObject(parsedData.accessors, "accessors", gltfRuntime);
  2655. }
  2656. if (parsedData.meshes) {
  2657. parseObject(parsedData.meshes, "meshes", gltfRuntime);
  2658. }
  2659. if (parsedData.lights) {
  2660. parseObject(parsedData.lights, "lights", gltfRuntime);
  2661. }
  2662. if (parsedData.cameras) {
  2663. parseObject(parsedData.cameras, "cameras", gltfRuntime);
  2664. }
  2665. if (parsedData.nodes) {
  2666. parseObject(parsedData.nodes, "nodes", gltfRuntime);
  2667. }
  2668. if (parsedData.images) {
  2669. parseObject(parsedData.images, "images", gltfRuntime);
  2670. }
  2671. if (parsedData.textures) {
  2672. parseObject(parsedData.textures, "textures", gltfRuntime);
  2673. }
  2674. if (parsedData.shaders) {
  2675. parseShaders(parsedData.shaders, gltfRuntime);
  2676. }
  2677. if (parsedData.programs) {
  2678. parseObject(parsedData.programs, "programs", gltfRuntime);
  2679. }
  2680. if (parsedData.samplers) {
  2681. parseObject(parsedData.samplers, "samplers", gltfRuntime);
  2682. }
  2683. if (parsedData.techniques) {
  2684. parseObject(parsedData.techniques, "techniques", gltfRuntime);
  2685. }
  2686. if (parsedData.materials) {
  2687. parseObject(parsedData.materials, "materials", gltfRuntime);
  2688. }
  2689. if (parsedData.animations) {
  2690. parseObject(parsedData.animations, "animations", gltfRuntime);
  2691. }
  2692. if (parsedData.skins) {
  2693. parseObject(parsedData.skins, "skins", gltfRuntime);
  2694. }
  2695. if (parsedData.scenes) {
  2696. gltfRuntime.scenes = parsedData.scenes;
  2697. }
  2698. if (parsedData.scene && parsedData.scenes) {
  2699. gltfRuntime.currentScene = parsedData.scenes[parsedData.scene];
  2700. }
  2701. return gltfRuntime;
  2702. };
  2703. GLTFLoaderBase.LoadBufferAsync = function (gltfRuntime, id, onSuccess, onError, onProgress) {
  2704. var buffer = gltfRuntime.buffers[id];
  2705. if (BABYLON.Tools.IsBase64(buffer.uri)) {
  2706. setTimeout(function () { return onSuccess(new Uint8Array(BABYLON.Tools.DecodeBase64(buffer.uri))); });
  2707. }
  2708. else {
  2709. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + buffer.uri, function (data) { return onSuccess(new Uint8Array(data)); }, onProgress, undefined, true, function (request) {
  2710. if (request) {
  2711. onError(request.status + " " + request.statusText);
  2712. }
  2713. });
  2714. }
  2715. };
  2716. GLTFLoaderBase.LoadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  2717. var texture = gltfRuntime.textures[id];
  2718. if (!texture || !texture.source) {
  2719. onError("");
  2720. return;
  2721. }
  2722. if (texture.babylonTexture) {
  2723. onSuccess(null);
  2724. return;
  2725. }
  2726. var source = gltfRuntime.images[texture.source];
  2727. if (BABYLON.Tools.IsBase64(source.uri)) {
  2728. setTimeout(function () { return onSuccess(new Uint8Array(BABYLON.Tools.DecodeBase64(source.uri))); });
  2729. }
  2730. else {
  2731. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + source.uri, function (data) { return onSuccess(new Uint8Array(data)); }, undefined, undefined, true, function (request) {
  2732. if (request) {
  2733. onError(request.status + " " + request.statusText);
  2734. }
  2735. });
  2736. }
  2737. };
  2738. GLTFLoaderBase.CreateTextureAsync = function (gltfRuntime, id, buffer, onSuccess, onError) {
  2739. var texture = gltfRuntime.textures[id];
  2740. if (texture.babylonTexture) {
  2741. onSuccess(texture.babylonTexture);
  2742. return;
  2743. }
  2744. var sampler = gltfRuntime.samplers[texture.sampler];
  2745. var createMipMaps = (sampler.minFilter === GLTF1.ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  2746. (sampler.minFilter === GLTF1.ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  2747. (sampler.minFilter === GLTF1.ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  2748. (sampler.minFilter === GLTF1.ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  2749. var samplingMode = BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  2750. var blob = new Blob([buffer]);
  2751. var blobURL = URL.createObjectURL(blob);
  2752. var revokeBlobURL = function () { return URL.revokeObjectURL(blobURL); };
  2753. var newTexture = new BABYLON.Texture(blobURL, gltfRuntime.scene, !createMipMaps, true, samplingMode, revokeBlobURL, revokeBlobURL);
  2754. if (sampler.wrapS !== undefined) {
  2755. newTexture.wrapU = GLTF1.GLTFUtils.GetWrapMode(sampler.wrapS);
  2756. }
  2757. if (sampler.wrapT !== undefined) {
  2758. newTexture.wrapV = GLTF1.GLTFUtils.GetWrapMode(sampler.wrapT);
  2759. }
  2760. newTexture.name = id;
  2761. texture.babylonTexture = newTexture;
  2762. onSuccess(newTexture);
  2763. };
  2764. GLTFLoaderBase.LoadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  2765. var shader = gltfRuntime.shaders[id];
  2766. if (BABYLON.Tools.IsBase64(shader.uri)) {
  2767. var shaderString = atob(shader.uri.split(",")[1]);
  2768. if (onSuccess) {
  2769. onSuccess(shaderString);
  2770. }
  2771. }
  2772. else {
  2773. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + shader.uri, onSuccess, undefined, undefined, false, function (request) {
  2774. if (request && onError) {
  2775. onError(request.status + " " + request.statusText);
  2776. }
  2777. });
  2778. }
  2779. };
  2780. GLTFLoaderBase.LoadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  2781. var material = gltfRuntime.materials[id];
  2782. if (!material.technique) {
  2783. if (onError) {
  2784. onError("No technique found.");
  2785. }
  2786. return;
  2787. }
  2788. var technique = gltfRuntime.techniques[material.technique];
  2789. if (!technique) {
  2790. var defaultMaterial = new BABYLON.StandardMaterial(id, gltfRuntime.scene);
  2791. defaultMaterial.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  2792. defaultMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  2793. onSuccess(defaultMaterial);
  2794. return;
  2795. }
  2796. var program = gltfRuntime.programs[technique.program];
  2797. var states = technique.states;
  2798. var vertexShader = BABYLON.Effect.ShadersStore[program.vertexShader + "VertexShader"];
  2799. var pixelShader = BABYLON.Effect.ShadersStore[program.fragmentShader + "PixelShader"];
  2800. var newVertexShader = "";
  2801. var newPixelShader = "";
  2802. var vertexTokenizer = new Tokenizer(vertexShader);
  2803. var pixelTokenizer = new Tokenizer(pixelShader);
  2804. var unTreatedUniforms = {};
  2805. var uniforms = [];
  2806. var attributes = [];
  2807. var samplers = [];
  2808. // Fill uniform, sampler2D and attributes
  2809. for (var unif in technique.uniforms) {
  2810. var uniform = technique.uniforms[unif];
  2811. var uniformParameter = technique.parameters[uniform];
  2812. unTreatedUniforms[unif] = uniformParameter;
  2813. if (uniformParameter.semantic && !uniformParameter.node && !uniformParameter.source) {
  2814. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  2815. if (transformIndex !== -1) {
  2816. uniforms.push(babylonTransforms[transformIndex]);
  2817. delete unTreatedUniforms[unif];
  2818. }
  2819. else {
  2820. uniforms.push(unif);
  2821. }
  2822. }
  2823. else if (uniformParameter.type === GLTF1.EParameterType.SAMPLER_2D) {
  2824. samplers.push(unif);
  2825. }
  2826. else {
  2827. uniforms.push(unif);
  2828. }
  2829. }
  2830. for (var attr in technique.attributes) {
  2831. var attribute = technique.attributes[attr];
  2832. var attributeParameter = technique.parameters[attribute];
  2833. if (attributeParameter.semantic) {
  2834. attributes.push(getAttribute(attributeParameter));
  2835. }
  2836. }
  2837. // Configure vertex shader
  2838. while (!vertexTokenizer.isEnd() && vertexTokenizer.getNextToken()) {
  2839. var tokenType = vertexTokenizer.currentToken;
  2840. if (tokenType !== ETokenType.IDENTIFIER) {
  2841. newVertexShader += vertexTokenizer.currentString;
  2842. continue;
  2843. }
  2844. var foundAttribute = false;
  2845. for (var attr in technique.attributes) {
  2846. var attribute = technique.attributes[attr];
  2847. var attributeParameter = technique.parameters[attribute];
  2848. if (vertexTokenizer.currentIdentifier === attr && attributeParameter.semantic) {
  2849. newVertexShader += getAttribute(attributeParameter);
  2850. foundAttribute = true;
  2851. break;
  2852. }
  2853. }
  2854. if (foundAttribute) {
  2855. continue;
  2856. }
  2857. newVertexShader += parseShaderUniforms(vertexTokenizer, technique, unTreatedUniforms);
  2858. }
  2859. // Configure pixel shader
  2860. while (!pixelTokenizer.isEnd() && pixelTokenizer.getNextToken()) {
  2861. var tokenType = pixelTokenizer.currentToken;
  2862. if (tokenType !== ETokenType.IDENTIFIER) {
  2863. newPixelShader += pixelTokenizer.currentString;
  2864. continue;
  2865. }
  2866. newPixelShader += parseShaderUniforms(pixelTokenizer, technique, unTreatedUniforms);
  2867. }
  2868. // Create shader material
  2869. var shaderPath = {
  2870. vertex: program.vertexShader + id,
  2871. fragment: program.fragmentShader + id
  2872. };
  2873. var options = {
  2874. attributes: attributes,
  2875. uniforms: uniforms,
  2876. samplers: samplers,
  2877. needAlphaBlending: states && states.enable && states.enable.indexOf(3042) !== -1
  2878. };
  2879. BABYLON.Effect.ShadersStore[program.vertexShader + id + "VertexShader"] = newVertexShader;
  2880. BABYLON.Effect.ShadersStore[program.fragmentShader + id + "PixelShader"] = newPixelShader;
  2881. var shaderMaterial = new BABYLON.ShaderMaterial(id, gltfRuntime.scene, shaderPath, options);
  2882. shaderMaterial.onError = onShaderCompileError(program, shaderMaterial, onError);
  2883. shaderMaterial.onCompiled = onShaderCompileSuccess(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess);
  2884. shaderMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  2885. if (states && states.functions) {
  2886. var functions = states.functions;
  2887. if (functions.cullFace && functions.cullFace[0] !== GLTF1.ECullingType.BACK) {
  2888. shaderMaterial.backFaceCulling = false;
  2889. }
  2890. var blendFunc = functions.blendFuncSeparate;
  2891. if (blendFunc) {
  2892. if (blendFunc[0] === GLTF1.EBlendingFunction.SRC_ALPHA && blendFunc[1] === GLTF1.EBlendingFunction.ONE_MINUS_SRC_ALPHA && blendFunc[2] === GLTF1.EBlendingFunction.ONE && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  2893. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_COMBINE;
  2894. }
  2895. else if (blendFunc[0] === GLTF1.EBlendingFunction.ONE && blendFunc[1] === GLTF1.EBlendingFunction.ONE && blendFunc[2] === GLTF1.EBlendingFunction.ZERO && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  2896. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ONEONE;
  2897. }
  2898. else if (blendFunc[0] === GLTF1.EBlendingFunction.SRC_ALPHA && blendFunc[1] === GLTF1.EBlendingFunction.ONE && blendFunc[2] === GLTF1.EBlendingFunction.ZERO && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  2899. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ADD;
  2900. }
  2901. else if (blendFunc[0] === GLTF1.EBlendingFunction.ZERO && blendFunc[1] === GLTF1.EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === GLTF1.EBlendingFunction.ONE && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  2902. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_SUBTRACT;
  2903. }
  2904. else if (blendFunc[0] === GLTF1.EBlendingFunction.DST_COLOR && blendFunc[1] === GLTF1.EBlendingFunction.ZERO && blendFunc[2] === GLTF1.EBlendingFunction.ONE && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  2905. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MULTIPLY;
  2906. }
  2907. else if (blendFunc[0] === GLTF1.EBlendingFunction.SRC_ALPHA && blendFunc[1] === GLTF1.EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === GLTF1.EBlendingFunction.ONE && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  2908. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MAXIMIZED;
  2909. }
  2910. }
  2911. }
  2912. };
  2913. return GLTFLoaderBase;
  2914. }());
  2915. GLTF1.GLTFLoaderBase = GLTFLoaderBase;
  2916. /**
  2917. * glTF V1 Loader
  2918. */
  2919. var GLTFLoader = /** @class */ (function () {
  2920. function GLTFLoader() {
  2921. // #region Stubs for IGLTFLoader interface
  2922. this.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.AUTO;
  2923. this.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.FIRST;
  2924. this.compileMaterials = false;
  2925. this.useClipPlane = false;
  2926. this.compileShadowGenerators = false;
  2927. this.onDisposeObservable = new BABYLON.Observable();
  2928. this.onMeshLoadedObservable = new BABYLON.Observable();
  2929. this.onTextureLoadedObservable = new BABYLON.Observable();
  2930. this.onMaterialLoadedObservable = new BABYLON.Observable();
  2931. this.onCompleteObservable = new BABYLON.Observable();
  2932. this.onExtensionLoadedObservable = new BABYLON.Observable();
  2933. /**
  2934. * State of the loader
  2935. */
  2936. this.state = null;
  2937. }
  2938. GLTFLoader.RegisterExtension = function (extension) {
  2939. if (GLTFLoader.Extensions[extension.name]) {
  2940. BABYLON.Tools.Error("Tool with the same name \"" + extension.name + "\" already exists");
  2941. return;
  2942. }
  2943. GLTFLoader.Extensions[extension.name] = extension;
  2944. };
  2945. GLTFLoader.prototype.dispose = function () { };
  2946. // #endregion
  2947. GLTFLoader.prototype._importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  2948. var _this = this;
  2949. scene.useRightHandedSystem = true;
  2950. GLTF1.GLTFLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  2951. gltfRuntime.importOnlyMeshes = true;
  2952. if (meshesNames === "") {
  2953. gltfRuntime.importMeshesNames = [];
  2954. }
  2955. else if (typeof meshesNames === "string") {
  2956. gltfRuntime.importMeshesNames = [meshesNames];
  2957. }
  2958. else if (meshesNames && !(meshesNames instanceof Array)) {
  2959. gltfRuntime.importMeshesNames = [meshesNames];
  2960. }
  2961. else {
  2962. gltfRuntime.importMeshesNames = [];
  2963. BABYLON.Tools.Warn("Argument meshesNames must be of type string or string[]");
  2964. }
  2965. // Create nodes
  2966. _this._createNodes(gltfRuntime);
  2967. var meshes = new Array();
  2968. var skeletons = new Array();
  2969. // Fill arrays of meshes and skeletons
  2970. for (var nde in gltfRuntime.nodes) {
  2971. var node = gltfRuntime.nodes[nde];
  2972. if (node.babylonNode instanceof BABYLON.AbstractMesh) {
  2973. meshes.push(node.babylonNode);
  2974. }
  2975. }
  2976. for (var skl in gltfRuntime.skins) {
  2977. var skin = gltfRuntime.skins[skl];
  2978. if (skin.babylonSkeleton instanceof BABYLON.Skeleton) {
  2979. skeletons.push(skin.babylonSkeleton);
  2980. }
  2981. }
  2982. // Load buffers, shaders, materials, etc.
  2983. _this._loadBuffersAsync(gltfRuntime, function () {
  2984. _this._loadShadersAsync(gltfRuntime, function () {
  2985. importMaterials(gltfRuntime);
  2986. postLoad(gltfRuntime);
  2987. if (!BABYLON.GLTFFileLoader.IncrementalLoading && onSuccess) {
  2988. onSuccess(meshes, skeletons);
  2989. }
  2990. });
  2991. }, onProgress);
  2992. if (BABYLON.GLTFFileLoader.IncrementalLoading && onSuccess) {
  2993. onSuccess(meshes, skeletons);
  2994. }
  2995. }, onError);
  2996. return true;
  2997. };
  2998. /**
  2999. * Imports one or more meshes from a loaded gltf file and adds them to the scene
  3000. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  3001. * @param scene the scene the meshes should be added to
  3002. * @param data gltf data containing information of the meshes in a loaded file
  3003. * @param rootUrl root url to load from
  3004. * @param onProgress event that fires when loading progress has occured
  3005. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  3006. */
  3007. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  3008. var _this = this;
  3009. return new Promise(function (resolve, reject) {
  3010. _this._importMeshAsync(meshesNames, scene, data, rootUrl, function (meshes, skeletons) {
  3011. resolve({
  3012. meshes: meshes,
  3013. particleSystems: [],
  3014. skeletons: skeletons,
  3015. animationGroups: []
  3016. });
  3017. }, onProgress, function (message) {
  3018. reject(new Error(message));
  3019. });
  3020. });
  3021. };
  3022. GLTFLoader.prototype._loadAsync = function (scene, data, rootUrl, onSuccess, onProgress, onError) {
  3023. var _this = this;
  3024. scene.useRightHandedSystem = true;
  3025. GLTF1.GLTFLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  3026. // Load runtime extensios
  3027. GLTF1.GLTFLoaderExtension.LoadRuntimeExtensionsAsync(gltfRuntime, function () {
  3028. // Create nodes
  3029. _this._createNodes(gltfRuntime);
  3030. // Load buffers, shaders, materials, etc.
  3031. _this._loadBuffersAsync(gltfRuntime, function () {
  3032. _this._loadShadersAsync(gltfRuntime, function () {
  3033. importMaterials(gltfRuntime);
  3034. postLoad(gltfRuntime);
  3035. if (!BABYLON.GLTFFileLoader.IncrementalLoading) {
  3036. onSuccess();
  3037. }
  3038. });
  3039. });
  3040. if (BABYLON.GLTFFileLoader.IncrementalLoading) {
  3041. onSuccess();
  3042. }
  3043. }, onError);
  3044. }, onError);
  3045. };
  3046. /**
  3047. * Imports all objects from a loaded gltf file and adds them to the scene
  3048. * @param scene the scene the objects should be added to
  3049. * @param data gltf data containing information of the meshes in a loaded file
  3050. * @param rootUrl root url to load from
  3051. * @param onProgress event that fires when loading progress has occured
  3052. * @returns a promise which completes when objects have been loaded to the scene
  3053. */
  3054. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  3055. var _this = this;
  3056. return new Promise(function (resolve, reject) {
  3057. _this._loadAsync(scene, data, rootUrl, function () {
  3058. resolve();
  3059. }, onProgress, function (message) {
  3060. reject(new Error(message));
  3061. });
  3062. });
  3063. };
  3064. GLTFLoader.prototype._loadShadersAsync = function (gltfRuntime, onload) {
  3065. var hasShaders = false;
  3066. var processShader = function (sha, shader) {
  3067. GLTF1.GLTFLoaderExtension.LoadShaderStringAsync(gltfRuntime, sha, function (shaderString) {
  3068. if (shaderString instanceof ArrayBuffer) {
  3069. return;
  3070. }
  3071. gltfRuntime.loadedShaderCount++;
  3072. if (shaderString) {
  3073. BABYLON.Effect.ShadersStore[sha + (shader.type === GLTF1.EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = shaderString;
  3074. }
  3075. if (gltfRuntime.loadedShaderCount === gltfRuntime.shaderscount) {
  3076. onload();
  3077. }
  3078. }, function () {
  3079. BABYLON.Tools.Error("Error when loading shader program named " + sha + " located at " + shader.uri);
  3080. });
  3081. };
  3082. for (var sha in gltfRuntime.shaders) {
  3083. hasShaders = true;
  3084. var shader = gltfRuntime.shaders[sha];
  3085. if (shader) {
  3086. processShader.bind(this, sha, shader)();
  3087. }
  3088. else {
  3089. BABYLON.Tools.Error("No shader named: " + sha);
  3090. }
  3091. }
  3092. if (!hasShaders) {
  3093. onload();
  3094. }
  3095. };
  3096. ;
  3097. GLTFLoader.prototype._loadBuffersAsync = function (gltfRuntime, onLoad, onProgress) {
  3098. var hasBuffers = false;
  3099. var processBuffer = function (buf, buffer) {
  3100. GLTF1.GLTFLoaderExtension.LoadBufferAsync(gltfRuntime, buf, function (bufferView) {
  3101. gltfRuntime.loadedBufferCount++;
  3102. if (bufferView) {
  3103. if (bufferView.byteLength != gltfRuntime.buffers[buf].byteLength) {
  3104. BABYLON.Tools.Error("Buffer named " + buf + " is length " + bufferView.byteLength + ". Expected: " + buffer.byteLength); // Improve error message
  3105. }
  3106. gltfRuntime.loadedBufferViews[buf] = bufferView;
  3107. }
  3108. if (gltfRuntime.loadedBufferCount === gltfRuntime.buffersCount) {
  3109. onLoad();
  3110. }
  3111. }, function () {
  3112. BABYLON.Tools.Error("Error when loading buffer named " + buf + " located at " + buffer.uri);
  3113. });
  3114. };
  3115. for (var buf in gltfRuntime.buffers) {
  3116. hasBuffers = true;
  3117. var buffer = gltfRuntime.buffers[buf];
  3118. if (buffer) {
  3119. processBuffer.bind(this, buf, buffer)();
  3120. }
  3121. else {
  3122. BABYLON.Tools.Error("No buffer named: " + buf);
  3123. }
  3124. }
  3125. if (!hasBuffers) {
  3126. onLoad();
  3127. }
  3128. };
  3129. GLTFLoader.prototype._createNodes = function (gltfRuntime) {
  3130. var currentScene = gltfRuntime.currentScene;
  3131. if (currentScene) {
  3132. // Only one scene even if multiple scenes are defined
  3133. for (var i = 0; i < currentScene.nodes.length; i++) {
  3134. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  3135. }
  3136. }
  3137. else {
  3138. // Load all scenes
  3139. for (var thing in gltfRuntime.scenes) {
  3140. currentScene = gltfRuntime.scenes[thing];
  3141. for (var i = 0; i < currentScene.nodes.length; i++) {
  3142. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  3143. }
  3144. }
  3145. }
  3146. };
  3147. GLTFLoader.Extensions = {};
  3148. return GLTFLoader;
  3149. }());
  3150. GLTF1.GLTFLoader = GLTFLoader;
  3151. ;
  3152. BABYLON.GLTFFileLoader.CreateGLTFLoaderV1 = function () { return new GLTFLoader(); };
  3153. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  3154. })(BABYLON || (BABYLON = {}));
  3155. //# sourceMappingURL=babylon.glTFLoader.js.map
  3156. var BABYLON;
  3157. (function (BABYLON) {
  3158. var GLTF1;
  3159. (function (GLTF1) {
  3160. /**
  3161. * Utils functions for GLTF
  3162. */
  3163. var GLTFUtils = /** @class */ (function () {
  3164. function GLTFUtils() {
  3165. }
  3166. /**
  3167. * Sets the given "parameter" matrix
  3168. * @param scene: the {BABYLON.Scene} object
  3169. * @param source: the source node where to pick the matrix
  3170. * @param parameter: the GLTF technique parameter
  3171. * @param uniformName: the name of the shader's uniform
  3172. * @param shaderMaterial: the shader material
  3173. */
  3174. GLTFUtils.SetMatrix = function (scene, source, parameter, uniformName, shaderMaterial) {
  3175. var mat = null;
  3176. if (parameter.semantic === "MODEL") {
  3177. mat = source.getWorldMatrix();
  3178. }
  3179. else if (parameter.semantic === "PROJECTION") {
  3180. mat = scene.getProjectionMatrix();
  3181. }
  3182. else if (parameter.semantic === "VIEW") {
  3183. mat = scene.getViewMatrix();
  3184. }
  3185. else if (parameter.semantic === "MODELVIEWINVERSETRANSPOSE") {
  3186. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().multiply(scene.getViewMatrix()).invert());
  3187. }
  3188. else if (parameter.semantic === "MODELVIEW") {
  3189. mat = source.getWorldMatrix().multiply(scene.getViewMatrix());
  3190. }
  3191. else if (parameter.semantic === "MODELVIEWPROJECTION") {
  3192. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix());
  3193. }
  3194. else if (parameter.semantic === "MODELINVERSE") {
  3195. mat = source.getWorldMatrix().invert();
  3196. }
  3197. else if (parameter.semantic === "VIEWINVERSE") {
  3198. mat = scene.getViewMatrix().invert();
  3199. }
  3200. else if (parameter.semantic === "PROJECTIONINVERSE") {
  3201. mat = scene.getProjectionMatrix().invert();
  3202. }
  3203. else if (parameter.semantic === "MODELVIEWINVERSE") {
  3204. mat = source.getWorldMatrix().multiply(scene.getViewMatrix()).invert();
  3205. }
  3206. else if (parameter.semantic === "MODELVIEWPROJECTIONINVERSE") {
  3207. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix()).invert();
  3208. }
  3209. else if (parameter.semantic === "MODELINVERSETRANSPOSE") {
  3210. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().invert());
  3211. }
  3212. else {
  3213. debugger;
  3214. }
  3215. if (mat) {
  3216. switch (parameter.type) {
  3217. case GLTF1.EParameterType.FLOAT_MAT2:
  3218. shaderMaterial.setMatrix2x2(uniformName, BABYLON.Matrix.GetAsMatrix2x2(mat));
  3219. break;
  3220. case GLTF1.EParameterType.FLOAT_MAT3:
  3221. shaderMaterial.setMatrix3x3(uniformName, BABYLON.Matrix.GetAsMatrix3x3(mat));
  3222. break;
  3223. case GLTF1.EParameterType.FLOAT_MAT4:
  3224. shaderMaterial.setMatrix(uniformName, mat);
  3225. break;
  3226. default: break;
  3227. }
  3228. }
  3229. };
  3230. /**
  3231. * Sets the given "parameter" matrix
  3232. * @param shaderMaterial: the shader material
  3233. * @param uniform: the name of the shader's uniform
  3234. * @param value: the value of the uniform
  3235. * @param type: the uniform's type (EParameterType FLOAT, VEC2, VEC3 or VEC4)
  3236. */
  3237. GLTFUtils.SetUniform = function (shaderMaterial, uniform, value, type) {
  3238. switch (type) {
  3239. case GLTF1.EParameterType.FLOAT:
  3240. shaderMaterial.setFloat(uniform, value);
  3241. return true;
  3242. case GLTF1.EParameterType.FLOAT_VEC2:
  3243. shaderMaterial.setVector2(uniform, BABYLON.Vector2.FromArray(value));
  3244. return true;
  3245. case GLTF1.EParameterType.FLOAT_VEC3:
  3246. shaderMaterial.setVector3(uniform, BABYLON.Vector3.FromArray(value));
  3247. return true;
  3248. case GLTF1.EParameterType.FLOAT_VEC4:
  3249. shaderMaterial.setVector4(uniform, BABYLON.Vector4.FromArray(value));
  3250. return true;
  3251. default: return false;
  3252. }
  3253. };
  3254. /**
  3255. * Returns the wrap mode of the texture
  3256. * @param mode: the mode value
  3257. */
  3258. GLTFUtils.GetWrapMode = function (mode) {
  3259. switch (mode) {
  3260. case GLTF1.ETextureWrapMode.CLAMP_TO_EDGE: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  3261. case GLTF1.ETextureWrapMode.MIRRORED_REPEAT: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  3262. case GLTF1.ETextureWrapMode.REPEAT: return BABYLON.Texture.WRAP_ADDRESSMODE;
  3263. default: return BABYLON.Texture.WRAP_ADDRESSMODE;
  3264. }
  3265. };
  3266. /**
  3267. * Returns the byte stride giving an accessor
  3268. * @param accessor: the GLTF accessor objet
  3269. */
  3270. GLTFUtils.GetByteStrideFromType = function (accessor) {
  3271. // Needs this function since "byteStride" isn't requiered in glTF format
  3272. var type = accessor.type;
  3273. switch (type) {
  3274. case "VEC2": return 2;
  3275. case "VEC3": return 3;
  3276. case "VEC4": return 4;
  3277. case "MAT2": return 4;
  3278. case "MAT3": return 9;
  3279. case "MAT4": return 16;
  3280. default: return 1;
  3281. }
  3282. };
  3283. /**
  3284. * Returns the texture filter mode giving a mode value
  3285. * @param mode: the filter mode value
  3286. */
  3287. GLTFUtils.GetTextureFilterMode = function (mode) {
  3288. switch (mode) {
  3289. case GLTF1.ETextureFilterType.LINEAR:
  3290. case GLTF1.ETextureFilterType.LINEAR_MIPMAP_NEAREST:
  3291. case GLTF1.ETextureFilterType.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  3292. case GLTF1.ETextureFilterType.NEAREST:
  3293. case GLTF1.ETextureFilterType.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_SAMPLINGMODE;
  3294. default: return BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  3295. }
  3296. };
  3297. GLTFUtils.GetBufferFromBufferView = function (gltfRuntime, bufferView, byteOffset, byteLength, componentType) {
  3298. var byteOffset = bufferView.byteOffset + byteOffset;
  3299. var loadedBufferView = gltfRuntime.loadedBufferViews[bufferView.buffer];
  3300. if (byteOffset + byteLength > loadedBufferView.byteLength) {
  3301. throw new Error("Buffer access is out of range");
  3302. }
  3303. var buffer = loadedBufferView.buffer;
  3304. byteOffset += loadedBufferView.byteOffset;
  3305. switch (componentType) {
  3306. case GLTF1.EComponentType.BYTE: return new Int8Array(buffer, byteOffset, byteLength);
  3307. case GLTF1.EComponentType.UNSIGNED_BYTE: return new Uint8Array(buffer, byteOffset, byteLength);
  3308. case GLTF1.EComponentType.SHORT: return new Int16Array(buffer, byteOffset, byteLength);
  3309. case GLTF1.EComponentType.UNSIGNED_SHORT: return new Uint16Array(buffer, byteOffset, byteLength);
  3310. default: return new Float32Array(buffer, byteOffset, byteLength);
  3311. }
  3312. };
  3313. /**
  3314. * Returns a buffer from its accessor
  3315. * @param gltfRuntime: the GLTF runtime
  3316. * @param accessor: the GLTF accessor
  3317. */
  3318. GLTFUtils.GetBufferFromAccessor = function (gltfRuntime, accessor) {
  3319. var bufferView = gltfRuntime.bufferViews[accessor.bufferView];
  3320. var byteLength = accessor.count * GLTFUtils.GetByteStrideFromType(accessor);
  3321. return GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, accessor.byteOffset, byteLength, accessor.componentType);
  3322. };
  3323. /**
  3324. * Decodes a buffer view into a string
  3325. * @param view: the buffer view
  3326. */
  3327. GLTFUtils.DecodeBufferToText = function (view) {
  3328. var result = "";
  3329. var length = view.byteLength;
  3330. for (var i = 0; i < length; ++i) {
  3331. result += String.fromCharCode(view[i]);
  3332. }
  3333. return result;
  3334. };
  3335. /**
  3336. * Returns the default material of gltf. Related to
  3337. * https://github.com/KhronosGroup/glTF/tree/master/specification/1.0#appendix-a-default-material
  3338. * @param scene: the Babylon.js scene
  3339. */
  3340. GLTFUtils.GetDefaultMaterial = function (scene) {
  3341. if (!GLTFUtils._DefaultMaterial) {
  3342. BABYLON.Effect.ShadersStore["GLTFDefaultMaterialVertexShader"] = [
  3343. "precision highp float;",
  3344. "",
  3345. "uniform mat4 worldView;",
  3346. "uniform mat4 projection;",
  3347. "",
  3348. "attribute vec3 position;",
  3349. "",
  3350. "void main(void)",
  3351. "{",
  3352. " gl_Position = projection * worldView * vec4(position, 1.0);",
  3353. "}"
  3354. ].join("\n");
  3355. BABYLON.Effect.ShadersStore["GLTFDefaultMaterialPixelShader"] = [
  3356. "precision highp float;",
  3357. "",
  3358. "uniform vec4 u_emission;",
  3359. "",
  3360. "void main(void)",
  3361. "{",
  3362. " gl_FragColor = u_emission;",
  3363. "}"
  3364. ].join("\n");
  3365. var shaderPath = {
  3366. vertex: "GLTFDefaultMaterial",
  3367. fragment: "GLTFDefaultMaterial"
  3368. };
  3369. var options = {
  3370. attributes: ["position"],
  3371. uniforms: ["worldView", "projection", "u_emission"],
  3372. samplers: new Array(),
  3373. needAlphaBlending: false
  3374. };
  3375. GLTFUtils._DefaultMaterial = new BABYLON.ShaderMaterial("GLTFDefaultMaterial", scene, shaderPath, options);
  3376. GLTFUtils._DefaultMaterial.setColor4("u_emission", new BABYLON.Color4(0.5, 0.5, 0.5, 1.0));
  3377. }
  3378. return GLTFUtils._DefaultMaterial;
  3379. };
  3380. // The GLTF default material
  3381. GLTFUtils._DefaultMaterial = null;
  3382. return GLTFUtils;
  3383. }());
  3384. GLTF1.GLTFUtils = GLTFUtils;
  3385. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  3386. })(BABYLON || (BABYLON = {}));
  3387. //# sourceMappingURL=babylon.glTFLoaderUtils.js.map
  3388. var BABYLON;
  3389. (function (BABYLON) {
  3390. var GLTF1;
  3391. (function (GLTF1) {
  3392. var GLTFLoaderExtension = /** @class */ (function () {
  3393. function GLTFLoaderExtension(name) {
  3394. this._name = name;
  3395. }
  3396. Object.defineProperty(GLTFLoaderExtension.prototype, "name", {
  3397. get: function () {
  3398. return this._name;
  3399. },
  3400. enumerable: true,
  3401. configurable: true
  3402. });
  3403. /**
  3404. * Defines an override for loading the runtime
  3405. * Return true to stop further extensions from loading the runtime
  3406. */
  3407. GLTFLoaderExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  3408. return false;
  3409. };
  3410. /**
  3411. * Defines an onverride for creating gltf runtime
  3412. * Return true to stop further extensions from creating the runtime
  3413. */
  3414. GLTFLoaderExtension.prototype.loadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
  3415. return false;
  3416. };
  3417. /**
  3418. * Defines an override for loading buffers
  3419. * Return true to stop further extensions from loading this buffer
  3420. */
  3421. GLTFLoaderExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError, onProgress) {
  3422. return false;
  3423. };
  3424. /**
  3425. * Defines an override for loading texture buffers
  3426. * Return true to stop further extensions from loading this texture data
  3427. */
  3428. GLTFLoaderExtension.prototype.loadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  3429. return false;
  3430. };
  3431. /**
  3432. * Defines an override for creating textures
  3433. * Return true to stop further extensions from loading this texture
  3434. */
  3435. GLTFLoaderExtension.prototype.createTextureAsync = function (gltfRuntime, id, buffer, onSuccess, onError) {
  3436. return false;
  3437. };
  3438. /**
  3439. * Defines an override for loading shader strings
  3440. * Return true to stop further extensions from loading this shader data
  3441. */
  3442. GLTFLoaderExtension.prototype.loadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  3443. return false;
  3444. };
  3445. /**
  3446. * Defines an override for loading materials
  3447. * Return true to stop further extensions from loading this material
  3448. */
  3449. GLTFLoaderExtension.prototype.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  3450. return false;
  3451. };
  3452. // ---------
  3453. // Utilities
  3454. // ---------
  3455. GLTFLoaderExtension.LoadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  3456. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  3457. return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
  3458. }, function () {
  3459. setTimeout(function () {
  3460. if (!onSuccess) {
  3461. return;
  3462. }
  3463. onSuccess(GLTF1.GLTFLoaderBase.CreateRuntime(data.json, scene, rootUrl));
  3464. });
  3465. });
  3466. };
  3467. GLTFLoaderExtension.LoadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
  3468. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  3469. return loaderExtension.loadRuntimeExtensionsAsync(gltfRuntime, onSuccess, onError);
  3470. }, function () {
  3471. setTimeout(function () {
  3472. onSuccess();
  3473. });
  3474. });
  3475. };
  3476. GLTFLoaderExtension.LoadBufferAsync = function (gltfRuntime, id, onSuccess, onError, onProgress) {
  3477. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  3478. return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
  3479. }, function () {
  3480. GLTF1.GLTFLoaderBase.LoadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
  3481. });
  3482. };
  3483. GLTFLoaderExtension.LoadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  3484. GLTFLoaderExtension.LoadTextureBufferAsync(gltfRuntime, id, function (buffer) {
  3485. if (buffer) {
  3486. GLTFLoaderExtension.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  3487. }
  3488. }, onError);
  3489. };
  3490. GLTFLoaderExtension.LoadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  3491. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  3492. return loaderExtension.loadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  3493. }, function () {
  3494. GLTF1.GLTFLoaderBase.LoadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  3495. });
  3496. };
  3497. GLTFLoaderExtension.LoadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  3498. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  3499. return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  3500. }, function () {
  3501. GLTF1.GLTFLoaderBase.LoadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  3502. });
  3503. };
  3504. GLTFLoaderExtension.LoadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  3505. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  3506. return loaderExtension.loadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  3507. }, function () {
  3508. GLTF1.GLTFLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  3509. });
  3510. };
  3511. GLTFLoaderExtension.CreateTextureAsync = function (gltfRuntime, id, buffer, onSuccess, onError) {
  3512. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  3513. return loaderExtension.createTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  3514. }, function () {
  3515. GLTF1.GLTFLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  3516. });
  3517. };
  3518. GLTFLoaderExtension.ApplyExtensions = function (func, defaultFunc) {
  3519. for (var extensionName in GLTF1.GLTFLoader.Extensions) {
  3520. var loaderExtension = GLTF1.GLTFLoader.Extensions[extensionName];
  3521. if (func(loaderExtension)) {
  3522. return;
  3523. }
  3524. }
  3525. defaultFunc();
  3526. };
  3527. return GLTFLoaderExtension;
  3528. }());
  3529. GLTF1.GLTFLoaderExtension = GLTFLoaderExtension;
  3530. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  3531. })(BABYLON || (BABYLON = {}));
  3532. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  3533. var BABYLON;
  3534. (function (BABYLON) {
  3535. var GLTF1;
  3536. (function (GLTF1) {
  3537. var BinaryExtensionBufferName = "binary_glTF";
  3538. ;
  3539. ;
  3540. var GLTFBinaryExtension = /** @class */ (function (_super) {
  3541. __extends(GLTFBinaryExtension, _super);
  3542. function GLTFBinaryExtension() {
  3543. return _super.call(this, "KHR_binary_glTF") || this;
  3544. }
  3545. GLTFBinaryExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  3546. var extensionsUsed = data.json.extensionsUsed;
  3547. if (!extensionsUsed || extensionsUsed.indexOf(this.name) === -1 || !data.bin) {
  3548. return false;
  3549. }
  3550. this._bin = data.bin;
  3551. onSuccess(GLTF1.GLTFLoaderBase.CreateRuntime(data.json, scene, rootUrl));
  3552. return true;
  3553. };
  3554. GLTFBinaryExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  3555. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  3556. return false;
  3557. }
  3558. if (id !== BinaryExtensionBufferName) {
  3559. return false;
  3560. }
  3561. onSuccess(this._bin);
  3562. return true;
  3563. };
  3564. GLTFBinaryExtension.prototype.loadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  3565. var texture = gltfRuntime.textures[id];
  3566. var source = gltfRuntime.images[texture.source];
  3567. if (!source.extensions || !(this.name in source.extensions)) {
  3568. return false;
  3569. }
  3570. var sourceExt = source.extensions[this.name];
  3571. var bufferView = gltfRuntime.bufferViews[sourceExt.bufferView];
  3572. var buffer = GLTF1.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, GLTF1.EComponentType.UNSIGNED_BYTE);
  3573. onSuccess(buffer);
  3574. return true;
  3575. };
  3576. GLTFBinaryExtension.prototype.loadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  3577. var shader = gltfRuntime.shaders[id];
  3578. if (!shader.extensions || !(this.name in shader.extensions)) {
  3579. return false;
  3580. }
  3581. var binaryExtensionShader = shader.extensions[this.name];
  3582. var bufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
  3583. var shaderBytes = GLTF1.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, GLTF1.EComponentType.UNSIGNED_BYTE);
  3584. setTimeout(function () {
  3585. var shaderString = GLTF1.GLTFUtils.DecodeBufferToText(shaderBytes);
  3586. onSuccess(shaderString);
  3587. });
  3588. return true;
  3589. };
  3590. return GLTFBinaryExtension;
  3591. }(GLTF1.GLTFLoaderExtension));
  3592. GLTF1.GLTFBinaryExtension = GLTFBinaryExtension;
  3593. GLTF1.GLTFLoader.RegisterExtension(new GLTFBinaryExtension());
  3594. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  3595. })(BABYLON || (BABYLON = {}));
  3596. //# sourceMappingURL=babylon.glTFBinaryExtension.js.map
  3597. var BABYLON;
  3598. (function (BABYLON) {
  3599. var GLTF1;
  3600. (function (GLTF1) {
  3601. ;
  3602. ;
  3603. ;
  3604. var GLTFMaterialsCommonExtension = /** @class */ (function (_super) {
  3605. __extends(GLTFMaterialsCommonExtension, _super);
  3606. function GLTFMaterialsCommonExtension() {
  3607. return _super.call(this, "KHR_materials_common") || this;
  3608. }
  3609. GLTFMaterialsCommonExtension.prototype.loadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
  3610. if (!gltfRuntime.extensions)
  3611. return false;
  3612. var extension = gltfRuntime.extensions[this.name];
  3613. if (!extension)
  3614. return false;
  3615. // Create lights
  3616. var lights = extension.lights;
  3617. if (lights) {
  3618. for (var thing in lights) {
  3619. var light = lights[thing];
  3620. switch (light.type) {
  3621. case "ambient":
  3622. var ambientLight = new BABYLON.HemisphericLight(light.name, new BABYLON.Vector3(0, 1, 0), gltfRuntime.scene);
  3623. var ambient = light.ambient;
  3624. if (ambient) {
  3625. ambientLight.diffuse = BABYLON.Color3.FromArray(ambient.color || [1, 1, 1]);
  3626. }
  3627. break;
  3628. case "point":
  3629. var pointLight = new BABYLON.PointLight(light.name, new BABYLON.Vector3(10, 10, 10), gltfRuntime.scene);
  3630. var point = light.point;
  3631. if (point) {
  3632. pointLight.diffuse = BABYLON.Color3.FromArray(point.color || [1, 1, 1]);
  3633. }
  3634. break;
  3635. case "directional":
  3636. var dirLight = new BABYLON.DirectionalLight(light.name, new BABYLON.Vector3(0, -1, 0), gltfRuntime.scene);
  3637. var directional = light.directional;
  3638. if (directional) {
  3639. dirLight.diffuse = BABYLON.Color3.FromArray(directional.color || [1, 1, 1]);
  3640. }
  3641. break;
  3642. case "spot":
  3643. var spot = light.spot;
  3644. if (spot) {
  3645. var spotLight = new BABYLON.SpotLight(light.name, new BABYLON.Vector3(0, 10, 0), new BABYLON.Vector3(0, -1, 0), spot.fallOffAngle || Math.PI, spot.fallOffExponent || 0.0, gltfRuntime.scene);
  3646. spotLight.diffuse = BABYLON.Color3.FromArray(spot.color || [1, 1, 1]);
  3647. }
  3648. break;
  3649. default:
  3650. BABYLON.Tools.Warn("GLTF Material Common extension: light type \"" + light.type + "\” not supported");
  3651. break;
  3652. }
  3653. }
  3654. }
  3655. return false;
  3656. };
  3657. GLTFMaterialsCommonExtension.prototype.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  3658. var material = gltfRuntime.materials[id];
  3659. if (!material || !material.extensions)
  3660. return false;
  3661. var extension = material.extensions[this.name];
  3662. if (!extension)
  3663. return false;
  3664. var standardMaterial = new BABYLON.StandardMaterial(id, gltfRuntime.scene);
  3665. standardMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  3666. if (extension.technique === "CONSTANT") {
  3667. standardMaterial.disableLighting = true;
  3668. }
  3669. standardMaterial.backFaceCulling = extension.doubleSided === undefined ? false : !extension.doubleSided;
  3670. standardMaterial.alpha = extension.values.transparency === undefined ? 1.0 : extension.values.transparency;
  3671. standardMaterial.specularPower = extension.values.shininess === undefined ? 0.0 : extension.values.shininess;
  3672. // Ambient
  3673. if (typeof extension.values.ambient === "string") {
  3674. this._loadTexture(gltfRuntime, extension.values.ambient, standardMaterial, "ambientTexture", onError);
  3675. }
  3676. else {
  3677. standardMaterial.ambientColor = BABYLON.Color3.FromArray(extension.values.ambient || [0, 0, 0]);
  3678. }
  3679. // Diffuse
  3680. if (typeof extension.values.diffuse === "string") {
  3681. this._loadTexture(gltfRuntime, extension.values.diffuse, standardMaterial, "diffuseTexture", onError);
  3682. }
  3683. else {
  3684. standardMaterial.diffuseColor = BABYLON.Color3.FromArray(extension.values.diffuse || [0, 0, 0]);
  3685. }
  3686. // Emission
  3687. if (typeof extension.values.emission === "string") {
  3688. this._loadTexture(gltfRuntime, extension.values.emission, standardMaterial, "emissiveTexture", onError);
  3689. }
  3690. else {
  3691. standardMaterial.emissiveColor = BABYLON.Color3.FromArray(extension.values.emission || [0, 0, 0]);
  3692. }
  3693. // Specular
  3694. if (typeof extension.values.specular === "string") {
  3695. this._loadTexture(gltfRuntime, extension.values.specular, standardMaterial, "specularTexture", onError);
  3696. }
  3697. else {
  3698. standardMaterial.specularColor = BABYLON.Color3.FromArray(extension.values.specular || [0, 0, 0]);
  3699. }
  3700. return true;
  3701. };
  3702. GLTFMaterialsCommonExtension.prototype._loadTexture = function (gltfRuntime, id, material, propertyPath, onError) {
  3703. // Create buffer from texture url
  3704. GLTF1.GLTFLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, function (buffer) {
  3705. // Create texture from buffer
  3706. GLTF1.GLTFLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, function (texture) { return material[propertyPath] = texture; }, onError);
  3707. }, onError);
  3708. };
  3709. return GLTFMaterialsCommonExtension;
  3710. }(GLTF1.GLTFLoaderExtension));
  3711. GLTF1.GLTFMaterialsCommonExtension = GLTFMaterialsCommonExtension;
  3712. GLTF1.GLTFLoader.RegisterExtension(new GLTFMaterialsCommonExtension());
  3713. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  3714. })(BABYLON || (BABYLON = {}));
  3715. //# sourceMappingURL=babylon.glTFMaterialsCommonExtension.js.map
  3716. var BABYLON;
  3717. (function (BABYLON) {
  3718. var GLTF2;
  3719. (function (GLTF2) {
  3720. /** @hidden */
  3721. var _ArrayItem = /** @class */ (function () {
  3722. function _ArrayItem() {
  3723. }
  3724. /** @hidden */
  3725. _ArrayItem.Assign = function (values) {
  3726. if (values) {
  3727. for (var index = 0; index < values.length; index++) {
  3728. values[index]._index = index;
  3729. }
  3730. }
  3731. };
  3732. return _ArrayItem;
  3733. }());
  3734. GLTF2._ArrayItem = _ArrayItem;
  3735. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  3736. })(BABYLON || (BABYLON = {}));
  3737. //# sourceMappingURL=babylon.glTFLoaderUtilities.js.map
  3738. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  3739. /**
  3740. * Defines the module used to import/export glTF 2.0 assets
  3741. */
  3742. var BABYLON;
  3743. (function (BABYLON) {
  3744. var GLTF2;
  3745. (function (GLTF2) {
  3746. /**
  3747. * Loader for loading a glTF 2.0 asset
  3748. */
  3749. var GLTFLoader = /** @class */ (function () {
  3750. function GLTFLoader() {
  3751. /** @hidden */
  3752. this._completePromises = new Array();
  3753. this._disposed = false;
  3754. this._state = null;
  3755. this._extensions = {};
  3756. this._defaultSampler = {};
  3757. this._defaultBabylonMaterials = {};
  3758. this._requests = new Array();
  3759. /**
  3760. * Mode that determines the coordinate system to use.
  3761. */
  3762. this.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.AUTO;
  3763. /**
  3764. * Mode that determines what animations will start.
  3765. */
  3766. this.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.FIRST;
  3767. /**
  3768. * Defines if the loader should compile materials.
  3769. */
  3770. this.compileMaterials = false;
  3771. /**
  3772. * Defines if the loader should also compile materials with clip planes.
  3773. */
  3774. this.useClipPlane = false;
  3775. /**
  3776. * Defines if the loader should compile shadow generators.
  3777. */
  3778. this.compileShadowGenerators = false;
  3779. /**
  3780. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  3781. */
  3782. this.onMeshLoadedObservable = new BABYLON.Observable();
  3783. /**
  3784. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  3785. */
  3786. this.onTextureLoadedObservable = new BABYLON.Observable();
  3787. /**
  3788. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  3789. */
  3790. this.onMaterialLoadedObservable = new BABYLON.Observable();
  3791. /**
  3792. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  3793. * For assets with LODs, raised when all of the LODs are complete.
  3794. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  3795. */
  3796. this.onCompleteObservable = new BABYLON.Observable();
  3797. /**
  3798. * Observable raised after the loader is disposed.
  3799. */
  3800. this.onDisposeObservable = new BABYLON.Observable();
  3801. /**
  3802. * Observable raised after a loader extension is created.
  3803. * Set additional options for a loader extension in this event.
  3804. */
  3805. this.onExtensionLoadedObservable = new BABYLON.Observable();
  3806. }
  3807. /** @hidden */
  3808. GLTFLoader._Register = function (name, factory) {
  3809. if (GLTFLoader._Factories[name]) {
  3810. BABYLON.Tools.Error("Extension with the name '" + name + "' already exists");
  3811. return;
  3812. }
  3813. GLTFLoader._Factories[name] = factory;
  3814. // Keep the order of registration so that extensions registered first are called first.
  3815. GLTFLoader._Names.push(name);
  3816. };
  3817. Object.defineProperty(GLTFLoader.prototype, "state", {
  3818. /**
  3819. * Loader state or null if the loader is not active.
  3820. */
  3821. get: function () {
  3822. return this._state;
  3823. },
  3824. enumerable: true,
  3825. configurable: true
  3826. });
  3827. /**
  3828. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  3829. */
  3830. GLTFLoader.prototype.dispose = function () {
  3831. if (this._disposed) {
  3832. return;
  3833. }
  3834. this._disposed = true;
  3835. this.onDisposeObservable.notifyObservers(this);
  3836. this.onDisposeObservable.clear();
  3837. this._clear();
  3838. };
  3839. /**
  3840. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  3841. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  3842. * @param scene the scene the meshes should be added to
  3843. * @param data the glTF data to load
  3844. * @param rootUrl root url to load from
  3845. * @param onProgress event that fires when loading progress has occured
  3846. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  3847. */
  3848. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  3849. var _this = this;
  3850. return Promise.resolve().then(function () {
  3851. var nodes = null;
  3852. if (meshesNames) {
  3853. var nodeMap_1 = {};
  3854. if (_this._gltf.nodes) {
  3855. for (var _i = 0, _a = _this._gltf.nodes; _i < _a.length; _i++) {
  3856. var node = _a[_i];
  3857. if (node.name) {
  3858. nodeMap_1[node.name] = node;
  3859. }
  3860. }
  3861. }
  3862. var names = (meshesNames instanceof Array) ? meshesNames : [meshesNames];
  3863. nodes = names.map(function (name) {
  3864. var node = nodeMap_1[name];
  3865. if (!node) {
  3866. throw new Error("Failed to find node '" + name + "'");
  3867. }
  3868. return node;
  3869. });
  3870. }
  3871. return _this._loadAsync(nodes, scene, data, rootUrl, onProgress).then(function () {
  3872. return {
  3873. meshes: _this._getMeshes(),
  3874. particleSystems: [],
  3875. skeletons: _this._getSkeletons(),
  3876. animationGroups: _this._getAnimationGroups()
  3877. };
  3878. });
  3879. });
  3880. };
  3881. /**
  3882. * Imports all objects from the loaded glTF data and adds them to the scene
  3883. * @param scene the scene the objects should be added to
  3884. * @param data the glTF data to load
  3885. * @param rootUrl root url to load from
  3886. * @param onProgress event that fires when loading progress has occured
  3887. * @returns a promise which completes when objects have been loaded to the scene
  3888. */
  3889. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  3890. return this._loadAsync(null, scene, data, rootUrl, onProgress);
  3891. };
  3892. GLTFLoader.prototype._loadAsync = function (nodes, scene, data, rootUrl, onProgress) {
  3893. var _this = this;
  3894. return Promise.resolve().then(function () {
  3895. _this._loadExtensions();
  3896. _this._babylonScene = scene;
  3897. _this._rootUrl = rootUrl;
  3898. _this._progressCallback = onProgress;
  3899. _this._state = BABYLON.GLTFLoaderState.LOADING;
  3900. _this._loadData(data);
  3901. _this._checkExtensions();
  3902. var promises = new Array();
  3903. if (nodes) {
  3904. promises.push(_this._loadNodesAsync(nodes));
  3905. }
  3906. else {
  3907. var scene_1 = GLTFLoader._GetProperty("#/scene", _this._gltf.scenes, _this._gltf.scene || 0);
  3908. promises.push(_this._loadSceneAsync("#/scenes/" + scene_1._index, scene_1));
  3909. }
  3910. if (_this.compileMaterials) {
  3911. promises.push(_this._compileMaterialsAsync());
  3912. }
  3913. if (_this.compileShadowGenerators) {
  3914. promises.push(_this._compileShadowGeneratorsAsync());
  3915. }
  3916. var resultPromise = Promise.all(promises).then(function () {
  3917. _this._state = BABYLON.GLTFLoaderState.READY;
  3918. _this._startAnimations();
  3919. });
  3920. resultPromise.then(function () {
  3921. _this._rootBabylonMesh.setEnabled(true);
  3922. BABYLON.Tools.SetImmediate(function () {
  3923. if (!_this._disposed) {
  3924. Promise.all(_this._completePromises).then(function () {
  3925. _this._state = BABYLON.GLTFLoaderState.COMPLETE;
  3926. _this.onCompleteObservable.notifyObservers(_this);
  3927. _this.onCompleteObservable.clear();
  3928. _this._clear();
  3929. }).catch(function (error) {
  3930. BABYLON.Tools.Error("glTF Loader: " + error.message);
  3931. _this._clear();
  3932. });
  3933. }
  3934. });
  3935. });
  3936. return resultPromise;
  3937. }).catch(function (error) {
  3938. BABYLON.Tools.Error("glTF Loader: " + error.message);
  3939. _this._clear();
  3940. throw error;
  3941. });
  3942. };
  3943. GLTFLoader.prototype._loadExtensions = function () {
  3944. for (var _i = 0, _a = GLTFLoader._Names; _i < _a.length; _i++) {
  3945. var name_1 = _a[_i];
  3946. var extension = GLTFLoader._Factories[name_1](this);
  3947. this._extensions[name_1] = extension;
  3948. this.onExtensionLoadedObservable.notifyObservers(extension);
  3949. }
  3950. this.onExtensionLoadedObservable.clear();
  3951. };
  3952. GLTFLoader.prototype._loadData = function (data) {
  3953. this._gltf = data.json;
  3954. this._setupData();
  3955. if (data.bin) {
  3956. var buffers = this._gltf.buffers;
  3957. if (buffers && buffers[0] && !buffers[0].uri) {
  3958. var binaryBuffer = buffers[0];
  3959. if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
  3960. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  3961. }
  3962. binaryBuffer._data = Promise.resolve(data.bin);
  3963. }
  3964. else {
  3965. BABYLON.Tools.Warn("Unexpected BIN chunk");
  3966. }
  3967. }
  3968. };
  3969. GLTFLoader.prototype._setupData = function () {
  3970. GLTF2._ArrayItem.Assign(this._gltf.accessors);
  3971. GLTF2._ArrayItem.Assign(this._gltf.animations);
  3972. GLTF2._ArrayItem.Assign(this._gltf.buffers);
  3973. GLTF2._ArrayItem.Assign(this._gltf.bufferViews);
  3974. GLTF2._ArrayItem.Assign(this._gltf.cameras);
  3975. GLTF2._ArrayItem.Assign(this._gltf.images);
  3976. GLTF2._ArrayItem.Assign(this._gltf.materials);
  3977. GLTF2._ArrayItem.Assign(this._gltf.meshes);
  3978. GLTF2._ArrayItem.Assign(this._gltf.nodes);
  3979. GLTF2._ArrayItem.Assign(this._gltf.samplers);
  3980. GLTF2._ArrayItem.Assign(this._gltf.scenes);
  3981. GLTF2._ArrayItem.Assign(this._gltf.skins);
  3982. GLTF2._ArrayItem.Assign(this._gltf.textures);
  3983. if (this._gltf.nodes) {
  3984. var nodeParents = {};
  3985. for (var _i = 0, _a = this._gltf.nodes; _i < _a.length; _i++) {
  3986. var node = _a[_i];
  3987. if (node.children) {
  3988. for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
  3989. var index = _c[_b];
  3990. nodeParents[index] = node._index;
  3991. }
  3992. }
  3993. }
  3994. var rootNode = this._createRootNode();
  3995. for (var _d = 0, _e = this._gltf.nodes; _d < _e.length; _d++) {
  3996. var node = _e[_d];
  3997. var parentIndex = nodeParents[node._index];
  3998. node._parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
  3999. }
  4000. }
  4001. };
  4002. GLTFLoader.prototype._checkExtensions = function () {
  4003. if (this._gltf.extensionsRequired) {
  4004. for (var _i = 0, _a = this._gltf.extensionsRequired; _i < _a.length; _i++) {
  4005. var name_2 = _a[_i];
  4006. var extension = this._extensions[name_2];
  4007. if (!extension || !extension.enabled) {
  4008. throw new Error("Require extension " + name_2 + " is not available");
  4009. }
  4010. }
  4011. }
  4012. };
  4013. GLTFLoader.prototype._createRootNode = function () {
  4014. this._rootBabylonMesh = new BABYLON.Mesh("__root__", this._babylonScene);
  4015. this._rootBabylonMesh.setEnabled(false);
  4016. var rootNode = { _babylonMesh: this._rootBabylonMesh };
  4017. switch (this.coordinateSystemMode) {
  4018. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO: {
  4019. if (!this._babylonScene.useRightHandedSystem) {
  4020. rootNode.rotation = [0, 1, 0, 0];
  4021. rootNode.scale = [1, 1, -1];
  4022. GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
  4023. }
  4024. break;
  4025. }
  4026. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
  4027. this._babylonScene.useRightHandedSystem = true;
  4028. break;
  4029. }
  4030. default: {
  4031. throw new Error("Invalid coordinate system mode (" + this.coordinateSystemMode + ")");
  4032. }
  4033. }
  4034. this.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
  4035. return rootNode;
  4036. };
  4037. GLTFLoader.prototype._loadNodesAsync = function (nodes) {
  4038. var promises = new Array();
  4039. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  4040. var node = nodes_1[_i];
  4041. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  4042. }
  4043. promises.push(this._loadAnimationsAsync());
  4044. return Promise.all(promises).then(function () { });
  4045. };
  4046. /** @hidden */
  4047. GLTFLoader.prototype._loadSceneAsync = function (context, scene) {
  4048. var promise = GLTF2.GLTFLoaderExtension._LoadSceneAsync(this, context, scene);
  4049. if (promise) {
  4050. return promise;
  4051. }
  4052. var promises = new Array();
  4053. for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
  4054. var index = _a[_i];
  4055. var node = GLTFLoader._GetProperty(context + "/nodes/" + index, this._gltf.nodes, index);
  4056. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  4057. }
  4058. promises.push(this._loadAnimationsAsync());
  4059. return Promise.all(promises).then(function () { });
  4060. };
  4061. GLTFLoader.prototype._forEachPrimitive = function (node, callback) {
  4062. if (node._primitiveBabylonMeshes) {
  4063. for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
  4064. var babylonMesh = _a[_i];
  4065. callback(babylonMesh);
  4066. }
  4067. }
  4068. else {
  4069. callback(node._babylonMesh);
  4070. }
  4071. };
  4072. GLTFLoader.prototype._getMeshes = function () {
  4073. var meshes = new Array();
  4074. // Root mesh is always first.
  4075. meshes.push(this._rootBabylonMesh);
  4076. var nodes = this._gltf.nodes;
  4077. if (nodes) {
  4078. for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
  4079. var node = nodes_2[_i];
  4080. if (node._babylonMesh) {
  4081. meshes.push(node._babylonMesh);
  4082. }
  4083. if (node._primitiveBabylonMeshes) {
  4084. for (var _a = 0, _b = node._primitiveBabylonMeshes; _a < _b.length; _a++) {
  4085. var babylonMesh = _b[_a];
  4086. meshes.push(babylonMesh);
  4087. }
  4088. }
  4089. }
  4090. }
  4091. return meshes;
  4092. };
  4093. GLTFLoader.prototype._getSkeletons = function () {
  4094. var skeletons = new Array();
  4095. var skins = this._gltf.skins;
  4096. if (skins) {
  4097. for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
  4098. var skin = skins_1[_i];
  4099. if (skin._babylonSkeleton) {
  4100. skeletons.push(skin._babylonSkeleton);
  4101. }
  4102. }
  4103. }
  4104. return skeletons;
  4105. };
  4106. GLTFLoader.prototype._getAnimationGroups = function () {
  4107. var animationGroups = new Array();
  4108. var animations = this._gltf.animations;
  4109. if (animations) {
  4110. for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
  4111. var animation = animations_1[_i];
  4112. if (animation._babylonAnimationGroup) {
  4113. animationGroups.push(animation._babylonAnimationGroup);
  4114. }
  4115. }
  4116. }
  4117. return animationGroups;
  4118. };
  4119. GLTFLoader.prototype._startAnimations = function () {
  4120. switch (this.animationStartMode) {
  4121. case BABYLON.GLTFLoaderAnimationStartMode.NONE: {
  4122. // do nothing
  4123. break;
  4124. }
  4125. case BABYLON.GLTFLoaderAnimationStartMode.FIRST: {
  4126. var babylonAnimationGroups = this._getAnimationGroups();
  4127. if (babylonAnimationGroups.length !== 0) {
  4128. babylonAnimationGroups[0].start(true);
  4129. }
  4130. break;
  4131. }
  4132. case BABYLON.GLTFLoaderAnimationStartMode.ALL: {
  4133. var babylonAnimationGroups = this._getAnimationGroups();
  4134. for (var _i = 0, babylonAnimationGroups_1 = babylonAnimationGroups; _i < babylonAnimationGroups_1.length; _i++) {
  4135. var babylonAnimationGroup = babylonAnimationGroups_1[_i];
  4136. babylonAnimationGroup.start(true);
  4137. }
  4138. break;
  4139. }
  4140. default: {
  4141. BABYLON.Tools.Error("Invalid animation start mode (" + this.animationStartMode + ")");
  4142. return;
  4143. }
  4144. }
  4145. };
  4146. /** @hidden */
  4147. GLTFLoader.prototype._loadNodeAsync = function (context, node) {
  4148. var promise = GLTF2.GLTFLoaderExtension._LoadNodeAsync(this, context, node);
  4149. if (promise) {
  4150. return promise;
  4151. }
  4152. if (node._babylonMesh) {
  4153. throw new Error(context + ": Invalid recursive node hierarchy");
  4154. }
  4155. var promises = new Array();
  4156. var babylonMesh = new BABYLON.Mesh(node.name || "node" + node._index, this._babylonScene, node._parent._babylonMesh);
  4157. node._babylonMesh = babylonMesh;
  4158. node._babylonAnimationTargets = node._babylonAnimationTargets || [];
  4159. node._babylonAnimationTargets.push(babylonMesh);
  4160. GLTFLoader._LoadTransform(node, babylonMesh);
  4161. if (node.mesh != undefined) {
  4162. var mesh = GLTFLoader._GetProperty(context + "/mesh", this._gltf.meshes, node.mesh);
  4163. promises.push(this._loadMeshAsync("#/meshes/" + mesh._index, node, mesh, babylonMesh));
  4164. }
  4165. if (node.children) {
  4166. for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
  4167. var index = _a[_i];
  4168. var childNode = GLTFLoader._GetProperty(context + "/children/" + index, this._gltf.nodes, index);
  4169. promises.push(this._loadNodeAsync("#/nodes/" + index, childNode));
  4170. }
  4171. }
  4172. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  4173. return Promise.all(promises).then(function () { });
  4174. };
  4175. GLTFLoader.prototype._loadMeshAsync = function (context, node, mesh, babylonMesh) {
  4176. var _this = this;
  4177. var promises = new Array();
  4178. var primitives = mesh.primitives;
  4179. if (!primitives || primitives.length === 0) {
  4180. throw new Error(context + ": Primitives are missing");
  4181. }
  4182. GLTF2._ArrayItem.Assign(primitives);
  4183. if (primitives.length === 1) {
  4184. var primitive = primitives[0];
  4185. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, babylonMesh));
  4186. }
  4187. else {
  4188. node._primitiveBabylonMeshes = [];
  4189. for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
  4190. var primitive = primitives_1[_i];
  4191. var primitiveBabylonMesh = new BABYLON.Mesh((mesh.name || babylonMesh.name) + "_" + primitive._index, this._babylonScene, babylonMesh);
  4192. node._primitiveBabylonMeshes.push(primitiveBabylonMesh);
  4193. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, primitiveBabylonMesh));
  4194. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  4195. }
  4196. }
  4197. if (node.skin != undefined) {
  4198. var skin = GLTFLoader._GetProperty(context + "/skin", this._gltf.skins, node.skin);
  4199. promises.push(this._loadSkinAsync("#/skins/" + skin._index, node, mesh, skin));
  4200. }
  4201. return Promise.all(promises).then(function () {
  4202. _this._forEachPrimitive(node, function (babylonMesh) {
  4203. babylonMesh._refreshBoundingInfo(true);
  4204. });
  4205. });
  4206. };
  4207. GLTFLoader.prototype._loadPrimitiveAsync = function (context, node, mesh, primitive, babylonMesh) {
  4208. var _this = this;
  4209. var promises = new Array();
  4210. this._createMorphTargets(context, node, mesh, primitive, babylonMesh);
  4211. promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh).then(function (babylonGeometry) {
  4212. return _this._loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry).then(function () {
  4213. babylonGeometry.applyToMesh(babylonMesh);
  4214. });
  4215. }));
  4216. var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
  4217. if (primitive.material == undefined) {
  4218. babylonMesh.material = this._getDefaultMaterial(babylonDrawMode);
  4219. }
  4220. else {
  4221. var material = GLTFLoader._GetProperty(context + "/material}", this._gltf.materials, primitive.material);
  4222. promises.push(this._loadMaterialAsync("#/materials/" + material._index, material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
  4223. babylonMesh.material = babylonMaterial;
  4224. }));
  4225. }
  4226. return Promise.all(promises).then(function () { });
  4227. };
  4228. GLTFLoader.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  4229. var _this = this;
  4230. var promise = GLTF2.GLTFLoaderExtension._LoadVertexDataAsync(this, context, primitive, babylonMesh);
  4231. if (promise) {
  4232. return promise;
  4233. }
  4234. var attributes = primitive.attributes;
  4235. if (!attributes) {
  4236. throw new Error(context + ": Attributes are missing");
  4237. }
  4238. var promises = new Array();
  4239. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, this._babylonScene);
  4240. if (primitive.indices == undefined) {
  4241. babylonMesh.isUnIndexed = true;
  4242. }
  4243. else {
  4244. var accessor = GLTFLoader._GetProperty(context + "/indices", this._gltf.accessors, primitive.indices);
  4245. promises.push(this._loadIndicesAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  4246. babylonGeometry.setIndices(data);
  4247. }));
  4248. }
  4249. var loadAttribute = function (attribute, kind, callback) {
  4250. if (attributes[attribute] == undefined) {
  4251. return;
  4252. }
  4253. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  4254. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  4255. babylonMesh._delayInfo.push(kind);
  4256. }
  4257. var accessor = GLTFLoader._GetProperty(context + "/attributes/" + attribute, _this._gltf.accessors, attributes[attribute]);
  4258. promises.push(_this._loadVertexAccessorAsync("#/accessors/" + accessor._index, accessor, kind).then(function (babylonVertexBuffer) {
  4259. babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
  4260. }));
  4261. if (callback) {
  4262. callback(accessor);
  4263. }
  4264. };
  4265. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  4266. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  4267. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  4268. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  4269. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  4270. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  4271. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  4272. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind, function (accessor) {
  4273. if (accessor.type === "VEC4" /* VEC4 */) {
  4274. babylonMesh.hasVertexAlpha = true;
  4275. }
  4276. });
  4277. return Promise.all(promises).then(function () {
  4278. return babylonGeometry;
  4279. });
  4280. };
  4281. GLTFLoader.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
  4282. if (!primitive.targets) {
  4283. return;
  4284. }
  4285. if (node._numMorphTargets == undefined) {
  4286. node._numMorphTargets = primitive.targets.length;
  4287. }
  4288. else if (primitive.targets.length !== node._numMorphTargets) {
  4289. throw new Error(context + ": Primitives do not have the same number of targets");
  4290. }
  4291. babylonMesh.morphTargetManager = new BABYLON.MorphTargetManager();
  4292. for (var index = 0; index < primitive.targets.length; index++) {
  4293. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  4294. babylonMesh.morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  4295. // TODO: tell the target whether it has positions, normals, tangents
  4296. }
  4297. };
  4298. GLTFLoader.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonGeometry) {
  4299. if (!primitive.targets) {
  4300. return Promise.resolve();
  4301. }
  4302. var promises = new Array();
  4303. var morphTargetManager = babylonMesh.morphTargetManager;
  4304. for (var index = 0; index < morphTargetManager.numTargets; index++) {
  4305. var babylonMorphTarget = morphTargetManager.getTarget(index);
  4306. promises.push(this._loadMorphTargetVertexDataAsync(context + "/targets/" + index, babylonGeometry, primitive.targets[index], babylonMorphTarget));
  4307. }
  4308. return Promise.all(promises).then(function () { });
  4309. };
  4310. GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, babylonGeometry, attributes, babylonMorphTarget) {
  4311. var _this = this;
  4312. var promises = new Array();
  4313. var loadAttribute = function (attribute, kind, setData) {
  4314. if (attributes[attribute] == undefined) {
  4315. return;
  4316. }
  4317. var babylonVertexBuffer = babylonGeometry.getVertexBuffer(kind);
  4318. if (!babylonVertexBuffer) {
  4319. return;
  4320. }
  4321. var accessor = GLTFLoader._GetProperty(context + "/" + attribute, _this._gltf.accessors, attributes[attribute]);
  4322. promises.push(_this._loadFloatAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  4323. setData(babylonVertexBuffer, data);
  4324. }));
  4325. };
  4326. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind, function (babylonVertexBuffer, data) {
  4327. babylonVertexBuffer.forEach(data.length, function (value, index) {
  4328. data[index] += value;
  4329. });
  4330. babylonMorphTarget.setPositions(data);
  4331. });
  4332. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind, function (babylonVertexBuffer, data) {
  4333. babylonVertexBuffer.forEach(data.length, function (value, index) {
  4334. data[index] += value;
  4335. });
  4336. babylonMorphTarget.setNormals(data);
  4337. });
  4338. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind, function (babylonVertexBuffer, data) {
  4339. var dataIndex = 0;
  4340. babylonVertexBuffer.forEach(data.length, function (value, index) {
  4341. // Tangent data for morph targets is stored as xyz delta.
  4342. // The vertexData.tangent is stored as xyzw.
  4343. // So we need to skip every fourth vertexData.tangent.
  4344. if (((index + 1) % 4) !== 0) {
  4345. data[dataIndex++] += value;
  4346. }
  4347. });
  4348. babylonMorphTarget.setTangents(data);
  4349. });
  4350. return Promise.all(promises).then(function () { });
  4351. };
  4352. GLTFLoader._LoadTransform = function (node, babylonNode) {
  4353. var position = BABYLON.Vector3.Zero();
  4354. var rotation = BABYLON.Quaternion.Identity();
  4355. var scaling = BABYLON.Vector3.One();
  4356. if (node.matrix) {
  4357. var matrix = BABYLON.Matrix.FromArray(node.matrix);
  4358. matrix.decompose(scaling, rotation, position);
  4359. }
  4360. else {
  4361. if (node.translation)
  4362. position = BABYLON.Vector3.FromArray(node.translation);
  4363. if (node.rotation)
  4364. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  4365. if (node.scale)
  4366. scaling = BABYLON.Vector3.FromArray(node.scale);
  4367. }
  4368. babylonNode.position = position;
  4369. babylonNode.rotationQuaternion = rotation;
  4370. babylonNode.scaling = scaling;
  4371. };
  4372. GLTFLoader.prototype._loadSkinAsync = function (context, node, mesh, skin) {
  4373. var _this = this;
  4374. var assignSkeleton = function (skeleton) {
  4375. _this._forEachPrimitive(node, function (babylonMesh) {
  4376. babylonMesh.skeleton = skeleton;
  4377. });
  4378. // Ignore the TRS of skinned nodes.
  4379. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  4380. node._babylonMesh.parent = _this._rootBabylonMesh;
  4381. node._babylonMesh.position = BABYLON.Vector3.Zero();
  4382. node._babylonMesh.rotationQuaternion = BABYLON.Quaternion.Identity();
  4383. node._babylonMesh.scaling = BABYLON.Vector3.One();
  4384. };
  4385. if (skin._loaded) {
  4386. return skin._loaded.then(function () {
  4387. assignSkeleton(skin._babylonSkeleton);
  4388. });
  4389. }
  4390. var skeletonId = "skeleton" + skin._index;
  4391. var babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
  4392. skin._babylonSkeleton = babylonSkeleton;
  4393. this._loadBones(context, skin);
  4394. assignSkeleton(babylonSkeleton);
  4395. return (skin._loaded = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
  4396. _this._updateBoneMatrices(babylonSkeleton, inverseBindMatricesData);
  4397. }));
  4398. };
  4399. GLTFLoader.prototype._loadBones = function (context, skin) {
  4400. var babylonBones = {};
  4401. for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
  4402. var index = _a[_i];
  4403. var node = GLTFLoader._GetProperty(context + "/joints/" + index, this._gltf.nodes, index);
  4404. this._loadBone(node, skin, babylonBones);
  4405. }
  4406. };
  4407. GLTFLoader.prototype._loadBone = function (node, skin, babylonBones) {
  4408. var babylonBone = babylonBones[node._index];
  4409. if (babylonBone) {
  4410. return babylonBone;
  4411. }
  4412. var babylonParentBone = null;
  4413. if (node._parent._babylonMesh !== this._rootBabylonMesh) {
  4414. babylonParentBone = this._loadBone(node._parent, skin, babylonBones);
  4415. }
  4416. var boneIndex = skin.joints.indexOf(node._index);
  4417. babylonBone = new BABYLON.Bone(node.name || "joint" + node._index, skin._babylonSkeleton, babylonParentBone, this._getNodeMatrix(node), null, null, boneIndex);
  4418. babylonBones[node._index] = babylonBone;
  4419. node._babylonAnimationTargets = node._babylonAnimationTargets || [];
  4420. node._babylonAnimationTargets.push(babylonBone);
  4421. return babylonBone;
  4422. };
  4423. GLTFLoader.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
  4424. if (skin.inverseBindMatrices == undefined) {
  4425. return Promise.resolve(null);
  4426. }
  4427. var accessor = GLTFLoader._GetProperty(context + "/inverseBindMatrices", this._gltf.accessors, skin.inverseBindMatrices);
  4428. return this._loadFloatAccessorAsync("#/accessors/" + accessor._index, accessor);
  4429. };
  4430. GLTFLoader.prototype._updateBoneMatrices = function (babylonSkeleton, inverseBindMatricesData) {
  4431. for (var _i = 0, _a = babylonSkeleton.bones; _i < _a.length; _i++) {
  4432. var babylonBone = _a[_i];
  4433. var baseMatrix = BABYLON.Matrix.Identity();
  4434. var boneIndex = babylonBone._index;
  4435. if (inverseBindMatricesData && boneIndex !== -1) {
  4436. BABYLON.Matrix.FromArrayToRef(inverseBindMatricesData, boneIndex * 16, baseMatrix);
  4437. baseMatrix.invertToRef(baseMatrix);
  4438. }
  4439. var babylonParentBone = babylonBone.getParent();
  4440. if (babylonParentBone) {
  4441. baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
  4442. }
  4443. babylonBone.updateMatrix(baseMatrix, false, false);
  4444. babylonBone._updateDifferenceMatrix(undefined, false);
  4445. }
  4446. };
  4447. GLTFLoader.prototype._getNodeMatrix = function (node) {
  4448. return node.matrix ?
  4449. BABYLON.Matrix.FromArray(node.matrix) :
  4450. BABYLON.Matrix.Compose(node.scale ? BABYLON.Vector3.FromArray(node.scale) : BABYLON.Vector3.One(), node.rotation ? BABYLON.Quaternion.FromArray(node.rotation) : BABYLON.Quaternion.Identity(), node.translation ? BABYLON.Vector3.FromArray(node.translation) : BABYLON.Vector3.Zero());
  4451. };
  4452. GLTFLoader.prototype._loadAnimationsAsync = function () {
  4453. var animations = this._gltf.animations;
  4454. if (!animations) {
  4455. return Promise.resolve();
  4456. }
  4457. var promises = new Array();
  4458. for (var index = 0; index < animations.length; index++) {
  4459. var animation = animations[index];
  4460. promises.push(this._loadAnimationAsync("#/animations/" + index, animation));
  4461. }
  4462. return Promise.all(promises).then(function () { });
  4463. };
  4464. GLTFLoader.prototype._loadAnimationAsync = function (context, animation) {
  4465. var babylonAnimationGroup = new BABYLON.AnimationGroup(animation.name || "animation" + animation._index, this._babylonScene);
  4466. animation._babylonAnimationGroup = babylonAnimationGroup;
  4467. var promises = new Array();
  4468. GLTF2._ArrayItem.Assign(animation.channels);
  4469. GLTF2._ArrayItem.Assign(animation.samplers);
  4470. for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
  4471. var channel = _a[_i];
  4472. promises.push(this._loadAnimationChannelAsync(context + "/channels/" + channel._index, context, animation, channel, babylonAnimationGroup));
  4473. }
  4474. return Promise.all(promises).then(function () {
  4475. babylonAnimationGroup.normalize();
  4476. });
  4477. };
  4478. GLTFLoader.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup) {
  4479. var _this = this;
  4480. var targetNode = GLTFLoader._GetProperty(context + "/target/node", this._gltf.nodes, channel.target.node);
  4481. if (!targetNode._babylonMesh) {
  4482. return Promise.resolve();
  4483. }
  4484. // Ignore animations targeting TRS of skinned nodes.
  4485. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  4486. if (targetNode.skin != undefined && channel.target.path !== "weights" /* WEIGHTS */) {
  4487. return Promise.resolve();
  4488. }
  4489. var sampler = GLTFLoader._GetProperty(context + "/sampler", animation.samplers, channel.sampler);
  4490. return this._loadAnimationSamplerAsync(animationContext + "/samplers/" + channel.sampler, sampler).then(function (data) {
  4491. var targetPath;
  4492. var animationType;
  4493. switch (channel.target.path) {
  4494. case "translation" /* TRANSLATION */: {
  4495. targetPath = "position";
  4496. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  4497. break;
  4498. }
  4499. case "rotation" /* ROTATION */: {
  4500. targetPath = "rotationQuaternion";
  4501. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  4502. break;
  4503. }
  4504. case "scale" /* SCALE */: {
  4505. targetPath = "scaling";
  4506. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  4507. break;
  4508. }
  4509. case "weights" /* WEIGHTS */: {
  4510. targetPath = "influence";
  4511. animationType = BABYLON.Animation.ANIMATIONTYPE_FLOAT;
  4512. break;
  4513. }
  4514. default: {
  4515. throw new Error(context + ": Invalid target path (" + channel.target.path + ")");
  4516. }
  4517. }
  4518. var outputBufferOffset = 0;
  4519. var getNextOutputValue;
  4520. switch (targetPath) {
  4521. case "position": {
  4522. getNextOutputValue = function () {
  4523. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  4524. outputBufferOffset += 3;
  4525. return value;
  4526. };
  4527. break;
  4528. }
  4529. case "rotationQuaternion": {
  4530. getNextOutputValue = function () {
  4531. var value = BABYLON.Quaternion.FromArray(data.output, outputBufferOffset);
  4532. outputBufferOffset += 4;
  4533. return value;
  4534. };
  4535. break;
  4536. }
  4537. case "scaling": {
  4538. getNextOutputValue = function () {
  4539. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  4540. outputBufferOffset += 3;
  4541. return value;
  4542. };
  4543. break;
  4544. }
  4545. case "influence": {
  4546. getNextOutputValue = function () {
  4547. var value = new Array(targetNode._numMorphTargets);
  4548. for (var i = 0; i < targetNode._numMorphTargets; i++) {
  4549. value[i] = data.output[outputBufferOffset++];
  4550. }
  4551. return value;
  4552. };
  4553. break;
  4554. }
  4555. }
  4556. var getNextKey;
  4557. switch (data.interpolation) {
  4558. case "STEP" /* STEP */: {
  4559. getNextKey = function (frameIndex) { return ({
  4560. frame: data.input[frameIndex],
  4561. value: getNextOutputValue(),
  4562. interpolation: BABYLON.AnimationKeyInterpolation.STEP
  4563. }); };
  4564. break;
  4565. }
  4566. case "LINEAR" /* LINEAR */: {
  4567. getNextKey = function (frameIndex) { return ({
  4568. frame: data.input[frameIndex],
  4569. value: getNextOutputValue()
  4570. }); };
  4571. break;
  4572. }
  4573. case "CUBICSPLINE" /* CUBICSPLINE */: {
  4574. getNextKey = function (frameIndex) { return ({
  4575. frame: data.input[frameIndex],
  4576. inTangent: getNextOutputValue(),
  4577. value: getNextOutputValue(),
  4578. outTangent: getNextOutputValue()
  4579. }); };
  4580. break;
  4581. }
  4582. }
  4583. var keys = new Array(data.input.length);
  4584. for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
  4585. keys[frameIndex] = getNextKey(frameIndex);
  4586. }
  4587. if (targetPath === "influence") {
  4588. var _loop_1 = function (targetIndex) {
  4589. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  4590. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  4591. babylonAnimation.setKeys(keys.map(function (key) { return ({
  4592. frame: key.frame,
  4593. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  4594. value: key.value[targetIndex],
  4595. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  4596. }); }));
  4597. var morphTargets = new Array();
  4598. _this._forEachPrimitive(targetNode, function (babylonMesh) {
  4599. var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
  4600. morphTarget.animations.push(babylonAnimation);
  4601. morphTargets.push(morphTarget);
  4602. });
  4603. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, morphTargets);
  4604. };
  4605. for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
  4606. _loop_1(targetIndex);
  4607. }
  4608. }
  4609. else {
  4610. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  4611. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  4612. babylonAnimation.setKeys(keys);
  4613. if (targetNode._babylonAnimationTargets) {
  4614. for (var _i = 0, _a = targetNode._babylonAnimationTargets; _i < _a.length; _i++) {
  4615. var babylonAnimationTarget = _a[_i];
  4616. babylonAnimationTarget.animations.push(babylonAnimation);
  4617. }
  4618. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, targetNode._babylonAnimationTargets);
  4619. }
  4620. }
  4621. });
  4622. };
  4623. GLTFLoader.prototype._loadAnimationSamplerAsync = function (context, sampler) {
  4624. if (sampler._data) {
  4625. return sampler._data;
  4626. }
  4627. var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
  4628. switch (interpolation) {
  4629. case "STEP" /* STEP */:
  4630. case "LINEAR" /* LINEAR */:
  4631. case "CUBICSPLINE" /* CUBICSPLINE */: {
  4632. break;
  4633. }
  4634. default: {
  4635. throw new Error(context + ": Invalid interpolation (" + sampler.interpolation + ")");
  4636. }
  4637. }
  4638. var inputAccessor = GLTFLoader._GetProperty(context + "/input", this._gltf.accessors, sampler.input);
  4639. var outputAccessor = GLTFLoader._GetProperty(context + "/output", this._gltf.accessors, sampler.output);
  4640. sampler._data = Promise.all([
  4641. this._loadFloatAccessorAsync("#/accessors/" + inputAccessor._index, inputAccessor),
  4642. this._loadFloatAccessorAsync("#/accessors/" + outputAccessor._index, outputAccessor)
  4643. ]).then(function (_a) {
  4644. var inputData = _a[0], outputData = _a[1];
  4645. return {
  4646. input: inputData,
  4647. interpolation: interpolation,
  4648. output: outputData,
  4649. };
  4650. });
  4651. return sampler._data;
  4652. };
  4653. GLTFLoader.prototype._loadBufferAsync = function (context, buffer) {
  4654. if (buffer._data) {
  4655. return buffer._data;
  4656. }
  4657. if (!buffer.uri) {
  4658. throw new Error(context + ": Uri is missing");
  4659. }
  4660. buffer._data = this._loadUriAsync(context, buffer.uri);
  4661. return buffer._data;
  4662. };
  4663. /** @hidden */
  4664. GLTFLoader.prototype._loadBufferViewAsync = function (context, bufferView) {
  4665. if (bufferView._data) {
  4666. return bufferView._data;
  4667. }
  4668. var buffer = GLTFLoader._GetProperty(context + "/buffer", this._gltf.buffers, bufferView.buffer);
  4669. bufferView._data = this._loadBufferAsync("#/buffers/" + buffer._index, buffer).then(function (data) {
  4670. try {
  4671. return new Uint8Array(data.buffer, data.byteOffset + (bufferView.byteOffset || 0), bufferView.byteLength);
  4672. }
  4673. catch (e) {
  4674. throw new Error(context + ": " + e.message);
  4675. }
  4676. });
  4677. return bufferView._data;
  4678. };
  4679. GLTFLoader.prototype._loadIndicesAccessorAsync = function (context, accessor) {
  4680. if (accessor.type !== "SCALAR" /* SCALAR */) {
  4681. throw new Error(context + ": Invalid type " + accessor.type);
  4682. }
  4683. if (accessor.componentType !== 5121 /* UNSIGNED_BYTE */ &&
  4684. accessor.componentType !== 5123 /* UNSIGNED_SHORT */ &&
  4685. accessor.componentType !== 5125 /* UNSIGNED_INT */) {
  4686. throw new Error(context + ": Invalid component type " + accessor.componentType);
  4687. }
  4688. if (accessor._data) {
  4689. return accessor._data;
  4690. }
  4691. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  4692. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  4693. return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, accessor.count);
  4694. });
  4695. return accessor._data;
  4696. };
  4697. GLTFLoader.prototype._loadFloatAccessorAsync = function (context, accessor) {
  4698. // TODO: support normalized and stride
  4699. var _this = this;
  4700. if (accessor.componentType !== 5126 /* FLOAT */) {
  4701. throw new Error("Invalid component type " + accessor.componentType);
  4702. }
  4703. if (accessor._data) {
  4704. return accessor._data;
  4705. }
  4706. var numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
  4707. var length = numComponents * accessor.count;
  4708. if (accessor.bufferView == undefined) {
  4709. accessor._data = Promise.resolve(new Float32Array(length));
  4710. }
  4711. else {
  4712. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  4713. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  4714. return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, length);
  4715. });
  4716. }
  4717. if (accessor.sparse) {
  4718. var sparse_1 = accessor.sparse;
  4719. accessor._data = accessor._data.then(function (data) {
  4720. var indicesBufferView = GLTFLoader._GetProperty(context + "/sparse/indices/bufferView", _this._gltf.bufferViews, sparse_1.indices.bufferView);
  4721. var valuesBufferView = GLTFLoader._GetProperty(context + "/sparse/values/bufferView", _this._gltf.bufferViews, sparse_1.values.bufferView);
  4722. return Promise.all([
  4723. _this._loadBufferViewAsync("#/bufferViews/" + indicesBufferView._index, indicesBufferView),
  4724. _this._loadBufferViewAsync("#/bufferViews/" + valuesBufferView._index, valuesBufferView)
  4725. ]).then(function (_a) {
  4726. var indicesData = _a[0], valuesData = _a[1];
  4727. var indices = GLTFLoader._GetTypedArray(context + "/sparse/indices", sparse_1.indices.componentType, indicesData, sparse_1.indices.byteOffset, sparse_1.count);
  4728. var values = GLTFLoader._GetTypedArray(context + "/sparse/values", accessor.componentType, valuesData, sparse_1.values.byteOffset, numComponents * sparse_1.count);
  4729. var valuesIndex = 0;
  4730. for (var indicesIndex = 0; indicesIndex < indices.length; indicesIndex++) {
  4731. var dataIndex = indices[indicesIndex] * numComponents;
  4732. for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
  4733. data[dataIndex++] = values[valuesIndex++];
  4734. }
  4735. }
  4736. return data;
  4737. });
  4738. });
  4739. }
  4740. return accessor._data;
  4741. };
  4742. /** @hidden */
  4743. GLTFLoader.prototype._loadVertexBufferViewAsync = function (context, bufferView, kind) {
  4744. var _this = this;
  4745. if (bufferView._babylonBuffer) {
  4746. return bufferView._babylonBuffer;
  4747. }
  4748. bufferView._babylonBuffer = this._loadBufferViewAsync(context, bufferView).then(function (data) {
  4749. return new BABYLON.Buffer(_this._babylonScene.getEngine(), data, false);
  4750. });
  4751. return bufferView._babylonBuffer;
  4752. };
  4753. GLTFLoader.prototype._loadVertexAccessorAsync = function (context, accessor, kind) {
  4754. var _this = this;
  4755. if (accessor._babylonVertexBuffer) {
  4756. return accessor._babylonVertexBuffer;
  4757. }
  4758. if (accessor.sparse) {
  4759. accessor._babylonVertexBuffer = this._loadFloatAccessorAsync(context, accessor).then(function (data) {
  4760. return new BABYLON.VertexBuffer(_this._babylonScene.getEngine(), data, kind, false);
  4761. });
  4762. }
  4763. else {
  4764. var bufferView_1 = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  4765. accessor._babylonVertexBuffer = this._loadVertexBufferViewAsync("#/bufferViews/" + bufferView_1._index, bufferView_1, kind).then(function (buffer) {
  4766. var size = GLTFLoader._GetNumComponents(context, accessor.type);
  4767. return new BABYLON.VertexBuffer(_this._babylonScene.getEngine(), buffer, kind, false, false, bufferView_1.byteStride, false, accessor.byteOffset, size, accessor.componentType, accessor.normalized, true);
  4768. });
  4769. }
  4770. return accessor._babylonVertexBuffer;
  4771. };
  4772. GLTFLoader.prototype._getDefaultMaterial = function (drawMode) {
  4773. var babylonMaterial = this._defaultBabylonMaterials[drawMode];
  4774. if (!babylonMaterial) {
  4775. babylonMaterial = this._createMaterial(BABYLON.PBRMaterial, "__gltf_default", drawMode);
  4776. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  4777. babylonMaterial.metallic = 1;
  4778. babylonMaterial.roughness = 1;
  4779. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  4780. }
  4781. return babylonMaterial;
  4782. };
  4783. GLTFLoader.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, material, babylonMaterial) {
  4784. var promises = new Array();
  4785. // Ensure metallic workflow
  4786. babylonMaterial.metallic = 1;
  4787. babylonMaterial.roughness = 1;
  4788. var properties = material.pbrMetallicRoughness;
  4789. if (properties) {
  4790. if (properties.baseColorFactor) {
  4791. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  4792. babylonMaterial.alpha = properties.baseColorFactor[3];
  4793. }
  4794. else {
  4795. babylonMaterial.albedoColor = BABYLON.Color3.White();
  4796. }
  4797. babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
  4798. babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
  4799. if (properties.baseColorTexture) {
  4800. promises.push(this._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  4801. babylonMaterial.albedoTexture = texture;
  4802. }));
  4803. }
  4804. if (properties.metallicRoughnessTexture) {
  4805. promises.push(this._loadTextureAsync(context + "/metallicRoughnessTexture", properties.metallicRoughnessTexture, function (texture) {
  4806. babylonMaterial.metallicTexture = texture;
  4807. }));
  4808. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  4809. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  4810. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  4811. }
  4812. }
  4813. this._loadMaterialAlphaProperties(context, material, babylonMaterial);
  4814. return Promise.all(promises).then(function () { });
  4815. };
  4816. /** @hidden */
  4817. GLTFLoader.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  4818. var promise = GLTF2.GLTFLoaderExtension._LoadMaterialAsync(this, context, material, babylonMesh, babylonDrawMode, assign);
  4819. if (promise) {
  4820. return promise;
  4821. }
  4822. material._babylonData = material._babylonData || {};
  4823. var babylonData = material._babylonData[babylonDrawMode];
  4824. if (!babylonData) {
  4825. var promises = new Array();
  4826. var name_3 = material.name || "materialSG_" + material._index;
  4827. var babylonMaterial = this._createMaterial(BABYLON.PBRMaterial, name_3, babylonDrawMode);
  4828. promises.push(this._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  4829. promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync(context, material, babylonMaterial));
  4830. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  4831. babylonData = {
  4832. material: babylonMaterial,
  4833. meshes: [],
  4834. loaded: Promise.all(promises).then(function () { })
  4835. };
  4836. material._babylonData[babylonDrawMode] = babylonData;
  4837. }
  4838. babylonData.meshes.push(babylonMesh);
  4839. assign(babylonData.material);
  4840. return babylonData.loaded;
  4841. };
  4842. /** @hidden */
  4843. GLTFLoader.prototype._createMaterial = function (type, name, drawMode) {
  4844. var babylonMaterial = new type(name, this._babylonScene);
  4845. babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? BABYLON.Material.CounterClockWiseSideOrientation : BABYLON.Material.ClockWiseSideOrientation;
  4846. babylonMaterial.fillMode = drawMode;
  4847. return babylonMaterial;
  4848. };
  4849. /** @hidden */
  4850. GLTFLoader.prototype._loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
  4851. var promises = new Array();
  4852. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  4853. if (material.doubleSided) {
  4854. babylonMaterial.backFaceCulling = false;
  4855. babylonMaterial.twoSidedLighting = true;
  4856. }
  4857. if (material.normalTexture) {
  4858. promises.push(this._loadTextureAsync(context + "/normalTexture", material.normalTexture, function (texture) {
  4859. babylonMaterial.bumpTexture = texture;
  4860. }));
  4861. babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
  4862. babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
  4863. if (material.normalTexture.scale != undefined) {
  4864. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  4865. }
  4866. }
  4867. if (material.occlusionTexture) {
  4868. promises.push(this._loadTextureAsync(context + "/occlusionTexture", material.occlusionTexture, function (texture) {
  4869. babylonMaterial.ambientTexture = texture;
  4870. }));
  4871. babylonMaterial.useAmbientInGrayScale = true;
  4872. if (material.occlusionTexture.strength != undefined) {
  4873. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  4874. }
  4875. }
  4876. if (material.emissiveTexture) {
  4877. promises.push(this._loadTextureAsync(context + "/emissiveTexture", material.emissiveTexture, function (texture) {
  4878. babylonMaterial.emissiveTexture = texture;
  4879. }));
  4880. }
  4881. return Promise.all(promises).then(function () { });
  4882. };
  4883. /** @hidden */
  4884. GLTFLoader.prototype._loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
  4885. var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
  4886. switch (alphaMode) {
  4887. case "OPAQUE" /* OPAQUE */: {
  4888. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  4889. break;
  4890. }
  4891. case "MASK" /* MASK */: {
  4892. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  4893. babylonMaterial.alphaCutOff = (material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff);
  4894. if (babylonMaterial.albedoTexture) {
  4895. babylonMaterial.albedoTexture.hasAlpha = true;
  4896. }
  4897. break;
  4898. }
  4899. case "BLEND" /* BLEND */: {
  4900. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  4901. if (babylonMaterial.albedoTexture) {
  4902. babylonMaterial.albedoTexture.hasAlpha = true;
  4903. babylonMaterial.useAlphaFromAlbedoTexture = true;
  4904. }
  4905. break;
  4906. }
  4907. default: {
  4908. throw new Error(context + ": Invalid alpha mode (" + material.alphaMode + ")");
  4909. }
  4910. }
  4911. };
  4912. /** @hidden */
  4913. GLTFLoader.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  4914. var _this = this;
  4915. var texture = GLTFLoader._GetProperty(context + "/index", this._gltf.textures, textureInfo.index);
  4916. context = "#/textures/" + textureInfo.index;
  4917. var promises = new Array();
  4918. var sampler = (texture.sampler == undefined ? this._defaultSampler : GLTFLoader._GetProperty(context + "/sampler", this._gltf.samplers, texture.sampler));
  4919. var samplerData = this._loadSampler("#/samplers/" + sampler._index, sampler);
  4920. var deferred = new BABYLON.Deferred();
  4921. var babylonTexture = new BABYLON.Texture(null, this._babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, function () {
  4922. if (!_this._disposed) {
  4923. deferred.resolve();
  4924. }
  4925. }, function (message, exception) {
  4926. if (!_this._disposed) {
  4927. deferred.reject(new Error(context + ": " + ((exception && exception.message) ? exception.message : message || "Failed to load texture")));
  4928. }
  4929. });
  4930. promises.push(deferred.promise);
  4931. babylonTexture.name = texture.name || "texture" + texture._index;
  4932. babylonTexture.wrapU = samplerData.wrapU;
  4933. babylonTexture.wrapV = samplerData.wrapV;
  4934. babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
  4935. var image = GLTFLoader._GetProperty(context + "/source", this._gltf.images, texture.source);
  4936. promises.push(this._loadImageAsync("#/images/" + image._index, image).then(function (objectURL) {
  4937. babylonTexture.updateURL(objectURL);
  4938. }));
  4939. assign(babylonTexture);
  4940. this.onTextureLoadedObservable.notifyObservers(babylonTexture);
  4941. return Promise.all(promises).then(function () { });
  4942. };
  4943. GLTFLoader.prototype._loadSampler = function (context, sampler) {
  4944. if (!sampler._data) {
  4945. sampler._data = {
  4946. noMipMaps: (sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */),
  4947. samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler.magFilter, sampler.minFilter),
  4948. wrapU: GLTFLoader._GetTextureWrapMode(context, sampler.wrapS),
  4949. wrapV: GLTFLoader._GetTextureWrapMode(context, sampler.wrapT)
  4950. };
  4951. }
  4952. ;
  4953. return sampler._data;
  4954. };
  4955. GLTFLoader.prototype._loadImageAsync = function (context, image) {
  4956. if (image._objectURL) {
  4957. return image._objectURL;
  4958. }
  4959. var promise;
  4960. if (image.uri) {
  4961. promise = this._loadUriAsync(context, image.uri);
  4962. }
  4963. else {
  4964. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, image.bufferView);
  4965. promise = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView);
  4966. }
  4967. image._objectURL = promise.then(function (data) {
  4968. return URL.createObjectURL(new Blob([data], { type: image.mimeType }));
  4969. });
  4970. return image._objectURL;
  4971. };
  4972. /** @hidden */
  4973. GLTFLoader.prototype._loadUriAsync = function (context, uri) {
  4974. var _this = this;
  4975. var promise = GLTF2.GLTFLoaderExtension._LoadUriAsync(this, context, uri);
  4976. if (promise) {
  4977. return promise;
  4978. }
  4979. if (!GLTFLoader._ValidateUri(uri)) {
  4980. throw new Error(context + ": Uri '" + uri + "' is invalid");
  4981. }
  4982. if (BABYLON.Tools.IsBase64(uri)) {
  4983. return Promise.resolve(new Uint8Array(BABYLON.Tools.DecodeBase64(uri)));
  4984. }
  4985. return new Promise(function (resolve, reject) {
  4986. var request = BABYLON.Tools.LoadFile(_this._rootUrl + uri, function (data) {
  4987. if (!_this._disposed) {
  4988. resolve(new Uint8Array(data));
  4989. }
  4990. }, function (event) {
  4991. if (!_this._disposed) {
  4992. try {
  4993. if (request && _this._state === BABYLON.GLTFLoaderState.LOADING) {
  4994. request._lengthComputable = event.lengthComputable;
  4995. request._loaded = event.loaded;
  4996. request._total = event.total;
  4997. _this._onProgress();
  4998. }
  4999. }
  5000. catch (e) {
  5001. reject(e);
  5002. }
  5003. }
  5004. }, _this._babylonScene.database, true, function (request, exception) {
  5005. if (!_this._disposed) {
  5006. reject(new BABYLON.LoadFileError(context + ": Failed to load '" + uri + "'" + (request ? ": " + request.status + " " + request.statusText : ""), request));
  5007. }
  5008. });
  5009. _this._requests.push(request);
  5010. });
  5011. };
  5012. GLTFLoader.prototype._onProgress = function () {
  5013. if (!this._progressCallback) {
  5014. return;
  5015. }
  5016. var lengthComputable = true;
  5017. var loaded = 0;
  5018. var total = 0;
  5019. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  5020. var request = _a[_i];
  5021. if (request._lengthComputable === undefined || request._loaded === undefined || request._total === undefined) {
  5022. return;
  5023. }
  5024. lengthComputable = lengthComputable && request._lengthComputable;
  5025. loaded += request._loaded;
  5026. total += request._total;
  5027. }
  5028. this._progressCallback(new BABYLON.SceneLoaderProgressEvent(lengthComputable, loaded, lengthComputable ? total : 0));
  5029. };
  5030. /** @hidden */
  5031. GLTFLoader._GetProperty = function (context, array, index) {
  5032. if (!array || index == undefined || !array[index]) {
  5033. throw new Error(context + ": Failed to find index (" + index + ")");
  5034. }
  5035. return array[index];
  5036. };
  5037. GLTFLoader._GetTextureWrapMode = function (context, mode) {
  5038. // Set defaults if undefined
  5039. mode = mode == undefined ? 10497 /* REPEAT */ : mode;
  5040. switch (mode) {
  5041. case 33071 /* CLAMP_TO_EDGE */: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  5042. case 33648 /* MIRRORED_REPEAT */: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  5043. case 10497 /* REPEAT */: return BABYLON.Texture.WRAP_ADDRESSMODE;
  5044. default:
  5045. BABYLON.Tools.Warn(context + ": Invalid texture wrap mode (" + mode + ")");
  5046. return BABYLON.Texture.WRAP_ADDRESSMODE;
  5047. }
  5048. };
  5049. GLTFLoader._GetTextureSamplingMode = function (context, magFilter, minFilter) {
  5050. // Set defaults if undefined
  5051. magFilter = magFilter == undefined ? 9729 /* LINEAR */ : magFilter;
  5052. minFilter = minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : minFilter;
  5053. if (magFilter === 9729 /* LINEAR */) {
  5054. switch (minFilter) {
  5055. case 9728 /* NEAREST */: return BABYLON.Texture.LINEAR_NEAREST;
  5056. case 9729 /* LINEAR */: return BABYLON.Texture.LINEAR_LINEAR;
  5057. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_NEAREST_MIPNEAREST;
  5058. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_LINEAR_MIPNEAREST;
  5059. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_NEAREST_MIPLINEAR;
  5060. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  5061. default:
  5062. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  5063. return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  5064. }
  5065. }
  5066. else {
  5067. if (magFilter !== 9728 /* NEAREST */) {
  5068. BABYLON.Tools.Warn(context + ": Invalid texture magnification filter (" + magFilter + ")");
  5069. }
  5070. switch (minFilter) {
  5071. case 9728 /* NEAREST */: return BABYLON.Texture.NEAREST_NEAREST;
  5072. case 9729 /* LINEAR */: return BABYLON.Texture.NEAREST_LINEAR;
  5073. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  5074. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_LINEAR_MIPNEAREST;
  5075. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_NEAREST_MIPLINEAR;
  5076. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_LINEAR_MIPLINEAR;
  5077. default:
  5078. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  5079. return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  5080. }
  5081. }
  5082. };
  5083. GLTFLoader._GetTypedArray = function (context, componentType, bufferView, byteOffset, length) {
  5084. var buffer = bufferView.buffer;
  5085. byteOffset = bufferView.byteOffset + (byteOffset || 0);
  5086. try {
  5087. switch (componentType) {
  5088. case 5120 /* BYTE */: return new Int8Array(buffer, byteOffset, length);
  5089. case 5121 /* UNSIGNED_BYTE */: return new Uint8Array(buffer, byteOffset, length);
  5090. case 5122 /* SHORT */: return new Int16Array(buffer, byteOffset, length);
  5091. case 5123 /* UNSIGNED_SHORT */: return new Uint16Array(buffer, byteOffset, length);
  5092. case 5125 /* UNSIGNED_INT */: return new Uint32Array(buffer, byteOffset, length);
  5093. case 5126 /* FLOAT */: return new Float32Array(buffer, byteOffset, length);
  5094. default: throw new Error("Invalid component type " + componentType);
  5095. }
  5096. }
  5097. catch (e) {
  5098. throw new Error(context + ": " + e);
  5099. }
  5100. };
  5101. GLTFLoader._GetNumComponents = function (context, type) {
  5102. switch (type) {
  5103. case "SCALAR": return 1;
  5104. case "VEC2": return 2;
  5105. case "VEC3": return 3;
  5106. case "VEC4": return 4;
  5107. case "MAT2": return 4;
  5108. case "MAT3": return 9;
  5109. case "MAT4": return 16;
  5110. }
  5111. throw new Error(context + ": Invalid type (" + type + ")");
  5112. };
  5113. GLTFLoader._ValidateUri = function (uri) {
  5114. return (BABYLON.Tools.IsBase64(uri) || uri.indexOf("..") === -1);
  5115. };
  5116. GLTFLoader._GetDrawMode = function (context, mode) {
  5117. if (mode == undefined) {
  5118. mode = 4 /* TRIANGLES */;
  5119. }
  5120. switch (mode) {
  5121. case 0 /* POINTS */: return BABYLON.Material.PointListDrawMode;
  5122. case 1 /* LINES */: return BABYLON.Material.LineListDrawMode;
  5123. case 2 /* LINE_LOOP */: return BABYLON.Material.LineLoopDrawMode;
  5124. case 3 /* LINE_STRIP */: return BABYLON.Material.LineStripDrawMode;
  5125. case 4 /* TRIANGLES */: return BABYLON.Material.TriangleFillMode;
  5126. case 5 /* TRIANGLE_STRIP */: return BABYLON.Material.TriangleStripDrawMode;
  5127. case 6 /* TRIANGLE_FAN */: return BABYLON.Material.TriangleFanDrawMode;
  5128. }
  5129. throw new Error(context + ": Invalid mesh primitive mode (" + mode + ")");
  5130. };
  5131. GLTFLoader.prototype._compileMaterialsAsync = function () {
  5132. var promises = new Array();
  5133. if (this._gltf.materials) {
  5134. for (var _i = 0, _a = this._gltf.materials; _i < _a.length; _i++) {
  5135. var material = _a[_i];
  5136. if (material._babylonData) {
  5137. for (var babylonDrawMode in material._babylonData) {
  5138. var babylonData = material._babylonData[babylonDrawMode];
  5139. for (var _b = 0, _c = babylonData.meshes; _b < _c.length; _b++) {
  5140. var babylonMesh = _c[_b];
  5141. // Ensure nonUniformScaling is set if necessary.
  5142. babylonMesh.computeWorldMatrix(true);
  5143. var babylonMaterial = babylonData.material;
  5144. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
  5145. if (this.useClipPlane) {
  5146. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
  5147. }
  5148. }
  5149. }
  5150. }
  5151. }
  5152. }
  5153. return Promise.all(promises).then(function () { });
  5154. };
  5155. GLTFLoader.prototype._compileShadowGeneratorsAsync = function () {
  5156. var promises = new Array();
  5157. var lights = this._babylonScene.lights;
  5158. for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
  5159. var light = lights_1[_i];
  5160. var generator = light.getShadowGenerator();
  5161. if (generator) {
  5162. promises.push(generator.forceCompilationAsync());
  5163. }
  5164. }
  5165. return Promise.all(promises).then(function () { });
  5166. };
  5167. GLTFLoader.prototype._clear = function () {
  5168. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  5169. var request = _a[_i];
  5170. request.abort();
  5171. }
  5172. this._requests.length = 0;
  5173. if (this._gltf && this._gltf.images) {
  5174. for (var _b = 0, _c = this._gltf.images; _b < _c.length; _b++) {
  5175. var image = _c[_b];
  5176. if (image._objectURL) {
  5177. image._objectURL.then(function (value) {
  5178. URL.revokeObjectURL(value);
  5179. });
  5180. image._objectURL = undefined;
  5181. }
  5182. }
  5183. }
  5184. delete this._gltf;
  5185. delete this._babylonScene;
  5186. this._completePromises.length = 0;
  5187. for (var name_4 in this._extensions) {
  5188. this._extensions[name_4].dispose();
  5189. }
  5190. this._extensions = {};
  5191. delete this._rootBabylonMesh;
  5192. delete this._progressCallback;
  5193. this.onMeshLoadedObservable.clear();
  5194. this.onTextureLoadedObservable.clear();
  5195. this.onMaterialLoadedObservable.clear();
  5196. };
  5197. /** @hidden */
  5198. GLTFLoader.prototype._applyExtensions = function (actionAsync) {
  5199. for (var _i = 0, _a = GLTFLoader._Names; _i < _a.length; _i++) {
  5200. var name_5 = _a[_i];
  5201. var extension = this._extensions[name_5];
  5202. if (extension.enabled) {
  5203. var promise = actionAsync(extension);
  5204. if (promise) {
  5205. return promise;
  5206. }
  5207. }
  5208. }
  5209. return null;
  5210. };
  5211. GLTFLoader._Names = new Array();
  5212. GLTFLoader._Factories = {};
  5213. return GLTFLoader;
  5214. }());
  5215. GLTF2.GLTFLoader = GLTFLoader;
  5216. BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = function () { return new GLTFLoader(); };
  5217. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  5218. })(BABYLON || (BABYLON = {}));
  5219. //# sourceMappingURL=babylon.glTFLoader.js.map
  5220. var BABYLON;
  5221. (function (BABYLON) {
  5222. var GLTF2;
  5223. (function (GLTF2) {
  5224. /**
  5225. * Abstract class that can be implemented to extend existing glTF loader behavior.
  5226. */
  5227. var GLTFLoaderExtension = /** @class */ (function () {
  5228. function GLTFLoaderExtension(loader) {
  5229. this.enabled = true;
  5230. this._loader = loader;
  5231. }
  5232. GLTFLoaderExtension.prototype.dispose = function () {
  5233. delete this._loader;
  5234. };
  5235. // #region Overridable Methods
  5236. /** Override this method to modify the default behavior for loading scenes. */
  5237. GLTFLoaderExtension.prototype._loadSceneAsync = function (context, node) { return null; };
  5238. /** Override this method to modify the default behavior for loading nodes. */
  5239. GLTFLoaderExtension.prototype._loadNodeAsync = function (context, node) { return null; };
  5240. /** Override this method to modify the default behavior for loading mesh primitive vertex data. */
  5241. GLTFLoaderExtension.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) { return null; };
  5242. /** Override this method to modify the default behavior for loading materials. */
  5243. GLTFLoaderExtension.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) { return null; };
  5244. /** Override this method to modify the default behavior for loading uris. */
  5245. GLTFLoaderExtension.prototype._loadUriAsync = function (context, uri) { return null; };
  5246. // #endregion
  5247. /** Helper method called by a loader extension to load an glTF extension. */
  5248. GLTFLoaderExtension.prototype._loadExtensionAsync = function (context, property, actionAsync) {
  5249. if (!property.extensions) {
  5250. return null;
  5251. }
  5252. var extensions = property.extensions;
  5253. var extension = extensions[this.name];
  5254. if (!extension) {
  5255. return null;
  5256. }
  5257. // Clear out the extension before executing the action to avoid recursing into the same property.
  5258. delete extensions[this.name];
  5259. try {
  5260. return actionAsync(context + "/extensions/" + this.name, extension);
  5261. }
  5262. finally {
  5263. // Restore the extension after executing the action.
  5264. extensions[this.name] = extension;
  5265. }
  5266. };
  5267. /** Helper method called by the loader to allow extensions to override loading scenes. */
  5268. GLTFLoaderExtension._LoadSceneAsync = function (loader, context, scene) {
  5269. return loader._applyExtensions(function (extension) { return extension._loadSceneAsync(context, scene); });
  5270. };
  5271. /** Helper method called by the loader to allow extensions to override loading nodes. */
  5272. GLTFLoaderExtension._LoadNodeAsync = function (loader, context, node) {
  5273. return loader._applyExtensions(function (extension) { return extension._loadNodeAsync(context, node); });
  5274. };
  5275. /** Helper method called by the loader to allow extensions to override loading mesh primitive vertex data. */
  5276. GLTFLoaderExtension._LoadVertexDataAsync = function (loader, context, primitive, babylonMesh) {
  5277. return loader._applyExtensions(function (extension) { return extension._loadVertexDataAsync(context, primitive, babylonMesh); });
  5278. };
  5279. /** Helper method called by the loader to allow extensions to override loading materials. */
  5280. GLTFLoaderExtension._LoadMaterialAsync = function (loader, context, material, babylonMesh, babylonDrawMode, assign) {
  5281. return loader._applyExtensions(function (extension) { return extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
  5282. };
  5283. /** Helper method called by the loader to allow extensions to override loading uris. */
  5284. GLTFLoaderExtension._LoadUriAsync = function (loader, context, uri) {
  5285. return loader._applyExtensions(function (extension) { return extension._loadUriAsync(context, uri); });
  5286. };
  5287. return GLTFLoaderExtension;
  5288. }());
  5289. GLTF2.GLTFLoaderExtension = GLTFLoaderExtension;
  5290. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  5291. })(BABYLON || (BABYLON = {}));
  5292. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  5293. var BABYLON;
  5294. (function (BABYLON) {
  5295. var GLTF2;
  5296. (function (GLTF2) {
  5297. var Extensions;
  5298. (function (Extensions) {
  5299. var NAME = "MSFT_lod";
  5300. /**
  5301. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/MSFT_lod)
  5302. */
  5303. var MSFT_lod = /** @class */ (function (_super) {
  5304. __extends(MSFT_lod, _super);
  5305. function MSFT_lod() {
  5306. var _this = _super !== null && _super.apply(this, arguments) || this;
  5307. _this.name = NAME;
  5308. /**
  5309. * Maximum number of LODs to load, starting from the lowest LOD.
  5310. */
  5311. _this.maxLODsToLoad = Number.MAX_VALUE;
  5312. _this._loadingNodeLOD = null;
  5313. _this._loadNodeSignals = {};
  5314. _this._loadingMaterialLOD = null;
  5315. _this._loadMaterialSignals = {};
  5316. return _this;
  5317. }
  5318. MSFT_lod.prototype._loadNodeAsync = function (context, node) {
  5319. var _this = this;
  5320. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  5321. var firstPromise;
  5322. var nodeLODs = _this._getLODs(extensionContext, node, _this._loader._gltf.nodes, extension.ids);
  5323. var _loop_1 = function (indexLOD) {
  5324. var nodeLOD = nodeLODs[indexLOD];
  5325. if (indexLOD !== 0) {
  5326. _this._loadingNodeLOD = nodeLOD;
  5327. if (!_this._loadNodeSignals[nodeLOD._index]) {
  5328. _this._loadNodeSignals[nodeLOD._index] = new BABYLON.Deferred();
  5329. }
  5330. }
  5331. var promise = _this._loader._loadNodeAsync("#/nodes/" + nodeLOD._index, nodeLOD).then(function () {
  5332. if (indexLOD !== 0) {
  5333. var previousNodeLOD = nodeLODs[indexLOD - 1];
  5334. if (previousNodeLOD._babylonMesh) {
  5335. previousNodeLOD._babylonMesh.dispose(false, true);
  5336. delete previousNodeLOD._babylonMesh;
  5337. }
  5338. }
  5339. if (indexLOD !== nodeLODs.length - 1) {
  5340. var nodeIndex = nodeLODs[indexLOD + 1]._index;
  5341. if (_this._loadNodeSignals[nodeIndex]) {
  5342. _this._loadNodeSignals[nodeIndex].resolve();
  5343. delete _this._loadNodeSignals[nodeIndex];
  5344. }
  5345. }
  5346. });
  5347. if (indexLOD === 0) {
  5348. firstPromise = promise;
  5349. }
  5350. else {
  5351. _this._loader._completePromises.push(promise);
  5352. _this._loadingNodeLOD = null;
  5353. }
  5354. };
  5355. for (var indexLOD = 0; indexLOD < nodeLODs.length; indexLOD++) {
  5356. _loop_1(indexLOD);
  5357. }
  5358. return firstPromise;
  5359. });
  5360. };
  5361. MSFT_lod.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  5362. var _this = this;
  5363. // Don't load material LODs if already loading a node LOD.
  5364. if (this._loadingNodeLOD) {
  5365. return null;
  5366. }
  5367. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  5368. var firstPromise;
  5369. var materialLODs = _this._getLODs(extensionContext, material, _this._loader._gltf.materials, extension.ids);
  5370. var _loop_2 = function (indexLOD) {
  5371. var materialLOD = materialLODs[indexLOD];
  5372. if (indexLOD !== 0) {
  5373. _this._loadingMaterialLOD = materialLOD;
  5374. if (!_this._loadMaterialSignals[materialLOD._index]) {
  5375. _this._loadMaterialSignals[materialLOD._index] = new BABYLON.Deferred();
  5376. }
  5377. }
  5378. var promise = _this._loader._loadMaterialAsync("#/materials/" + materialLOD._index, materialLOD, babylonMesh, babylonDrawMode, indexLOD === 0 ? assign : function () { }).then(function () {
  5379. if (indexLOD !== 0) {
  5380. var babylonDataLOD = materialLOD._babylonData;
  5381. assign(babylonDataLOD[babylonDrawMode].material);
  5382. var previousBabylonDataLOD = materialLODs[indexLOD - 1]._babylonData;
  5383. if (previousBabylonDataLOD[babylonDrawMode]) {
  5384. previousBabylonDataLOD[babylonDrawMode].material.dispose();
  5385. delete previousBabylonDataLOD[babylonDrawMode];
  5386. }
  5387. }
  5388. if (indexLOD !== materialLODs.length - 1) {
  5389. var materialIndex = materialLODs[indexLOD + 1]._index;
  5390. if (_this._loadMaterialSignals[materialIndex]) {
  5391. _this._loadMaterialSignals[materialIndex].resolve();
  5392. delete _this._loadMaterialSignals[materialIndex];
  5393. }
  5394. }
  5395. });
  5396. if (indexLOD === 0) {
  5397. firstPromise = promise;
  5398. }
  5399. else {
  5400. _this._loader._completePromises.push(promise);
  5401. _this._loadingMaterialLOD = null;
  5402. }
  5403. };
  5404. for (var indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  5405. _loop_2(indexLOD);
  5406. }
  5407. return firstPromise;
  5408. });
  5409. };
  5410. MSFT_lod.prototype._loadUriAsync = function (context, uri) {
  5411. var _this = this;
  5412. // Defer the loading of uris if loading a material or node LOD.
  5413. if (this._loadingMaterialLOD) {
  5414. var index = this._loadingMaterialLOD._index;
  5415. return this._loadMaterialSignals[index].promise.then(function () {
  5416. return _this._loader._loadUriAsync(context, uri);
  5417. });
  5418. }
  5419. else if (this._loadingNodeLOD) {
  5420. var index = this._loadingNodeLOD._index;
  5421. return this._loadNodeSignals[index].promise.then(function () {
  5422. return _this._loader._loadUriAsync(context, uri);
  5423. });
  5424. }
  5425. return null;
  5426. };
  5427. /**
  5428. * Gets an array of LOD properties from lowest to highest.
  5429. */
  5430. MSFT_lod.prototype._getLODs = function (context, property, array, ids) {
  5431. if (this.maxLODsToLoad <= 0) {
  5432. throw new Error("maxLODsToLoad must be greater than zero");
  5433. }
  5434. var properties = new Array();
  5435. for (var i = ids.length - 1; i >= 0; i--) {
  5436. properties.push(GLTF2.GLTFLoader._GetProperty(context + "/ids/" + ids[i], array, ids[i]));
  5437. if (properties.length === this.maxLODsToLoad) {
  5438. return properties;
  5439. }
  5440. }
  5441. properties.push(property);
  5442. return properties;
  5443. };
  5444. return MSFT_lod;
  5445. }(GLTF2.GLTFLoaderExtension));
  5446. Extensions.MSFT_lod = MSFT_lod;
  5447. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_lod(loader); });
  5448. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  5449. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  5450. })(BABYLON || (BABYLON = {}));
  5451. //# sourceMappingURL=MSFT_lod.js.map
  5452. var BABYLON;
  5453. (function (BABYLON) {
  5454. var GLTF2;
  5455. (function (GLTF2) {
  5456. var Extensions;
  5457. (function (Extensions) {
  5458. var NAME = "KHR_draco_mesh_compression";
  5459. /**
  5460. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression)
  5461. */
  5462. var KHR_draco_mesh_compression = /** @class */ (function (_super) {
  5463. __extends(KHR_draco_mesh_compression, _super);
  5464. function KHR_draco_mesh_compression(loader) {
  5465. var _this = _super.call(this, loader) || this;
  5466. _this.name = NAME;
  5467. _this._dracoCompression = null;
  5468. // Disable extension if decoder is not available.
  5469. if (!BABYLON.DracoCompression.DecoderAvailable) {
  5470. _this.enabled = false;
  5471. }
  5472. return _this;
  5473. }
  5474. KHR_draco_mesh_compression.prototype.dispose = function () {
  5475. if (this._dracoCompression) {
  5476. this._dracoCompression.dispose();
  5477. }
  5478. _super.prototype.dispose.call(this);
  5479. };
  5480. KHR_draco_mesh_compression.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  5481. var _this = this;
  5482. return this._loadExtensionAsync(context, primitive, function (extensionContext, extension) {
  5483. if (primitive.mode != undefined) {
  5484. if (primitive.mode !== 5 /* TRIANGLE_STRIP */ &&
  5485. primitive.mode !== 4 /* TRIANGLES */) {
  5486. throw new Error(context + ": Unsupported mode " + primitive.mode);
  5487. }
  5488. // TODO: handle triangle strips
  5489. if (primitive.mode === 5 /* TRIANGLE_STRIP */) {
  5490. throw new Error(context + ": Mode " + primitive.mode + " is not currently supported");
  5491. }
  5492. }
  5493. var attributes = {};
  5494. var loadAttribute = function (name, kind) {
  5495. var uniqueId = extension.attributes[name];
  5496. if (uniqueId == undefined) {
  5497. return;
  5498. }
  5499. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  5500. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  5501. babylonMesh._delayInfo.push(kind);
  5502. }
  5503. attributes[kind] = uniqueId;
  5504. };
  5505. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  5506. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  5507. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  5508. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  5509. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  5510. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  5511. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  5512. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind);
  5513. var bufferView = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._loader._gltf.bufferViews, extension.bufferView);
  5514. if (!bufferView._dracoBabylonGeometry) {
  5515. bufferView._dracoBabylonGeometry = _this._loader._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  5516. if (!_this._dracoCompression) {
  5517. _this._dracoCompression = new BABYLON.DracoCompression();
  5518. }
  5519. return _this._dracoCompression.decodeMeshAsync(data, attributes).then(function (babylonVertexData) {
  5520. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, _this._loader._babylonScene);
  5521. babylonVertexData.applyToGeometry(babylonGeometry);
  5522. return babylonGeometry;
  5523. }).catch(function (error) {
  5524. throw new Error(context + ": " + error.message);
  5525. });
  5526. });
  5527. }
  5528. return bufferView._dracoBabylonGeometry;
  5529. });
  5530. };
  5531. return KHR_draco_mesh_compression;
  5532. }(GLTF2.GLTFLoaderExtension));
  5533. Extensions.KHR_draco_mesh_compression = KHR_draco_mesh_compression;
  5534. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_draco_mesh_compression(loader); });
  5535. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  5536. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  5537. })(BABYLON || (BABYLON = {}));
  5538. //# sourceMappingURL=KHR_draco_mesh_compression.js.map
  5539. var BABYLON;
  5540. (function (BABYLON) {
  5541. var GLTF2;
  5542. (function (GLTF2) {
  5543. var Extensions;
  5544. (function (Extensions) {
  5545. var NAME = "KHR_materials_pbrSpecularGlossiness";
  5546. /**
  5547. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness)
  5548. */
  5549. var KHR_materials_pbrSpecularGlossiness = /** @class */ (function (_super) {
  5550. __extends(KHR_materials_pbrSpecularGlossiness, _super);
  5551. function KHR_materials_pbrSpecularGlossiness() {
  5552. var _this = _super !== null && _super.apply(this, arguments) || this;
  5553. _this.name = NAME;
  5554. return _this;
  5555. }
  5556. KHR_materials_pbrSpecularGlossiness.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  5557. var _this = this;
  5558. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  5559. material._babylonData = material._babylonData || {};
  5560. var babylonData = material._babylonData[babylonDrawMode];
  5561. if (!babylonData) {
  5562. var promises = new Array();
  5563. var name_1 = material.name || "materialSG_" + material._index;
  5564. var babylonMaterial = _this._loader._createMaterial(BABYLON.PBRMaterial, name_1, babylonDrawMode);
  5565. promises.push(_this._loader._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  5566. promises.push(_this._loadSpecularGlossinessPropertiesAsync(extensionContext, material, extension, babylonMaterial));
  5567. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  5568. babylonData = {
  5569. material: babylonMaterial,
  5570. meshes: [],
  5571. loaded: Promise.all(promises).then(function () { })
  5572. };
  5573. material._babylonData[babylonDrawMode] = babylonData;
  5574. }
  5575. babylonData.meshes.push(babylonMesh);
  5576. assign(babylonData.material);
  5577. return babylonData.loaded;
  5578. });
  5579. };
  5580. KHR_materials_pbrSpecularGlossiness.prototype._loadSpecularGlossinessPropertiesAsync = function (context, material, properties, babylonMaterial) {
  5581. var promises = new Array();
  5582. if (properties.diffuseFactor) {
  5583. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.diffuseFactor);
  5584. babylonMaterial.alpha = properties.diffuseFactor[3];
  5585. }
  5586. else {
  5587. babylonMaterial.albedoColor = BABYLON.Color3.White();
  5588. }
  5589. babylonMaterial.reflectivityColor = properties.specularFactor ? BABYLON.Color3.FromArray(properties.specularFactor) : BABYLON.Color3.White();
  5590. babylonMaterial.microSurface = properties.glossinessFactor == undefined ? 1 : properties.glossinessFactor;
  5591. if (properties.diffuseTexture) {
  5592. promises.push(this._loader._loadTextureAsync(context + "/diffuseTexture", properties.diffuseTexture, function (texture) {
  5593. babylonMaterial.albedoTexture = texture;
  5594. }));
  5595. }
  5596. if (properties.specularGlossinessTexture) {
  5597. promises.push(this._loader._loadTextureAsync(context + "/specularGlossinessTexture", properties.specularGlossinessTexture, function (texture) {
  5598. babylonMaterial.reflectivityTexture = texture;
  5599. }));
  5600. babylonMaterial.reflectivityTexture.hasAlpha = true;
  5601. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  5602. }
  5603. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  5604. return Promise.all(promises).then(function () { });
  5605. };
  5606. return KHR_materials_pbrSpecularGlossiness;
  5607. }(GLTF2.GLTFLoaderExtension));
  5608. Extensions.KHR_materials_pbrSpecularGlossiness = KHR_materials_pbrSpecularGlossiness;
  5609. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_pbrSpecularGlossiness(loader); });
  5610. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  5611. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  5612. })(BABYLON || (BABYLON = {}));
  5613. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map
  5614. var BABYLON;
  5615. (function (BABYLON) {
  5616. var GLTF2;
  5617. (function (GLTF2) {
  5618. var Extensions;
  5619. (function (Extensions) {
  5620. var NAME = "KHR_materials_unlit";
  5621. /**
  5622. * [Specification](https://github.com/donmccurdy/glTF/tree/feat-khr-materials-cmnConstant/extensions/2.0/Khronos/KHR_materials_unlit) (Experimental)
  5623. */
  5624. var KHR_materials_unlit = /** @class */ (function (_super) {
  5625. __extends(KHR_materials_unlit, _super);
  5626. function KHR_materials_unlit() {
  5627. var _this = _super !== null && _super.apply(this, arguments) || this;
  5628. _this.name = NAME;
  5629. return _this;
  5630. }
  5631. KHR_materials_unlit.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  5632. var _this = this;
  5633. return this._loadExtensionAsync(context, material, function () {
  5634. material._babylonData = material._babylonData || {};
  5635. var babylonData = material._babylonData[babylonDrawMode];
  5636. if (!babylonData) {
  5637. var name_1 = material.name || "materialUnlit_" + material._index;
  5638. var babylonMaterial = _this._loader._createMaterial(BABYLON.PBRMaterial, name_1, babylonDrawMode);
  5639. babylonMaterial.unlit = true;
  5640. var promise = _this._loadUnlitPropertiesAsync(context, material, babylonMaterial);
  5641. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  5642. babylonData = {
  5643. material: babylonMaterial,
  5644. meshes: [],
  5645. loaded: promise
  5646. };
  5647. material._babylonData[babylonDrawMode] = babylonData;
  5648. }
  5649. babylonData.meshes.push(babylonMesh);
  5650. assign(babylonData.material);
  5651. return babylonData.loaded;
  5652. });
  5653. };
  5654. KHR_materials_unlit.prototype._loadUnlitPropertiesAsync = function (context, material, babylonMaterial) {
  5655. var promises = new Array();
  5656. // Ensure metallic workflow
  5657. babylonMaterial.metallic = 1;
  5658. babylonMaterial.roughness = 1;
  5659. var properties = material.pbrMetallicRoughness;
  5660. if (properties) {
  5661. if (properties.baseColorFactor) {
  5662. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  5663. babylonMaterial.alpha = properties.baseColorFactor[3];
  5664. }
  5665. else {
  5666. babylonMaterial.albedoColor = BABYLON.Color3.White();
  5667. }
  5668. if (properties.baseColorTexture) {
  5669. promises.push(this._loader._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  5670. babylonMaterial.albedoTexture = texture;
  5671. }));
  5672. }
  5673. }
  5674. if (material.doubleSided) {
  5675. babylonMaterial.backFaceCulling = false;
  5676. babylonMaterial.twoSidedLighting = true;
  5677. }
  5678. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  5679. return Promise.all(promises).then(function () { });
  5680. };
  5681. return KHR_materials_unlit;
  5682. }(GLTF2.GLTFLoaderExtension));
  5683. Extensions.KHR_materials_unlit = KHR_materials_unlit;
  5684. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_unlit(loader); });
  5685. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  5686. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  5687. })(BABYLON || (BABYLON = {}));
  5688. //# sourceMappingURL=KHR_materials_unlit.js.map
  5689. var BABYLON;
  5690. (function (BABYLON) {
  5691. var GLTF2;
  5692. (function (GLTF2) {
  5693. var Extensions;
  5694. (function (Extensions) {
  5695. var NAME = "KHR_lights";
  5696. var LightType;
  5697. (function (LightType) {
  5698. LightType["AMBIENT"] = "ambient";
  5699. LightType["DIRECTIONAL"] = "directional";
  5700. LightType["POINT"] = "point";
  5701. LightType["SPOT"] = "spot";
  5702. })(LightType || (LightType = {}));
  5703. /**
  5704. * [Specification](https://github.com/MiiBond/glTF/tree/khr_lights_v1/extensions/Khronos/KHR_lights) (Experimental)
  5705. */
  5706. var KHR_lights = /** @class */ (function (_super) {
  5707. __extends(KHR_lights, _super);
  5708. function KHR_lights() {
  5709. var _this = _super !== null && _super.apply(this, arguments) || this;
  5710. _this.name = NAME;
  5711. return _this;
  5712. }
  5713. KHR_lights.prototype._loadSceneAsync = function (context, scene) {
  5714. var _this = this;
  5715. return this._loadExtensionAsync(context, scene, function (extensionContext, extension) {
  5716. var promise = _this._loader._loadSceneAsync(extensionContext, scene);
  5717. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  5718. if (light.type !== LightType.AMBIENT) {
  5719. throw new Error(extensionContext + ": Only ambient lights are allowed on a scene");
  5720. }
  5721. _this._loader._babylonScene.ambientColor = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.Black();
  5722. return promise;
  5723. });
  5724. };
  5725. KHR_lights.prototype._loadNodeAsync = function (context, node) {
  5726. var _this = this;
  5727. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  5728. var promise = _this._loader._loadNodeAsync(extensionContext, node);
  5729. var babylonLight;
  5730. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  5731. var name = node._babylonMesh.name;
  5732. switch (light.type) {
  5733. case LightType.AMBIENT: {
  5734. throw new Error(extensionContext + ": Ambient lights are not allowed on a node");
  5735. }
  5736. case LightType.DIRECTIONAL: {
  5737. babylonLight = new BABYLON.DirectionalLight(name, BABYLON.Vector3.Forward(), _this._loader._babylonScene);
  5738. break;
  5739. }
  5740. case LightType.POINT: {
  5741. babylonLight = new BABYLON.PointLight(name, BABYLON.Vector3.Zero(), _this._loader._babylonScene);
  5742. break;
  5743. }
  5744. case LightType.SPOT: {
  5745. var spotLight = light;
  5746. // TODO: support inner and outer cone angles
  5747. //const innerConeAngle = spotLight.innerConeAngle || 0;
  5748. var outerConeAngle = spotLight.outerConeAngle || Math.PI / 4;
  5749. babylonLight = new BABYLON.SpotLight(name, BABYLON.Vector3.Zero(), BABYLON.Vector3.Forward(), outerConeAngle, 2, _this._loader._babylonScene);
  5750. break;
  5751. }
  5752. default: {
  5753. throw new Error(extensionContext + ": Invalid light type (" + light.type + ")");
  5754. }
  5755. }
  5756. babylonLight.diffuse = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.White();
  5757. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  5758. babylonLight.parent = node._babylonMesh;
  5759. return promise;
  5760. });
  5761. };
  5762. Object.defineProperty(KHR_lights.prototype, "_lights", {
  5763. get: function () {
  5764. var extensions = this._loader._gltf.extensions;
  5765. if (!extensions || !extensions[this.name]) {
  5766. throw new Error("#/extensions: '" + this.name + "' not found");
  5767. }
  5768. var extension = extensions[this.name];
  5769. return extension.lights;
  5770. },
  5771. enumerable: true,
  5772. configurable: true
  5773. });
  5774. return KHR_lights;
  5775. }(GLTF2.GLTFLoaderExtension));
  5776. Extensions.KHR_lights = KHR_lights;
  5777. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_lights(loader); });
  5778. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  5779. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  5780. })(BABYLON || (BABYLON = {}));
  5781. //# sourceMappingURL=KHR_lights.js.map
  5782. return BABYLON;
  5783. });