babylon.glTFFileLoader.js 242 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739
  1. "use strict";
  2. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  3. var BABYLON;
  4. (function (BABYLON) {
  5. var GLTFLoaderCoordinateSystemMode;
  6. (function (GLTFLoaderCoordinateSystemMode) {
  7. /**
  8. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  9. */
  10. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
  11. /**
  12. * Sets the useRightHandedSystem flag on the scene.
  13. */
  14. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
  15. })(GLTFLoaderCoordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode || (BABYLON.GLTFLoaderCoordinateSystemMode = {}));
  16. var GLTFLoaderAnimationStartMode;
  17. (function (GLTFLoaderAnimationStartMode) {
  18. /**
  19. * No animation will start.
  20. */
  21. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
  22. /**
  23. * The first animation will start.
  24. */
  25. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
  26. /**
  27. * All animations will start.
  28. */
  29. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
  30. })(GLTFLoaderAnimationStartMode = BABYLON.GLTFLoaderAnimationStartMode || (BABYLON.GLTFLoaderAnimationStartMode = {}));
  31. var GLTFLoaderState;
  32. (function (GLTFLoaderState) {
  33. /**
  34. * The asset is loading.
  35. */
  36. GLTFLoaderState[GLTFLoaderState["LOADING"] = 0] = "LOADING";
  37. /**
  38. * The asset is ready for rendering.
  39. */
  40. GLTFLoaderState[GLTFLoaderState["READY"] = 1] = "READY";
  41. /**
  42. * The asset is completely loaded.
  43. */
  44. GLTFLoaderState[GLTFLoaderState["COMPLETE"] = 2] = "COMPLETE";
  45. })(GLTFLoaderState = BABYLON.GLTFLoaderState || (BABYLON.GLTFLoaderState = {}));
  46. var GLTFFileLoader = /** @class */ (function () {
  47. function GLTFFileLoader() {
  48. // #region Common options
  49. /**
  50. * Raised when the asset has been parsed.
  51. * The data.json property stores the glTF JSON.
  52. * The data.bin property stores the BIN chunk from a glTF binary or null if the input is not a glTF binary.
  53. */
  54. this.onParsedObservable = new BABYLON.Observable();
  55. // #endregion
  56. // #region V2 options
  57. /**
  58. * The coordinate system mode (AUTO, FORCE_RIGHT_HANDED).
  59. */
  60. this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
  61. /**
  62. * The animation start mode (NONE, FIRST, ALL).
  63. */
  64. this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
  65. /**
  66. * Set to true to compile materials before raising the success callback.
  67. */
  68. this.compileMaterials = false;
  69. /**
  70. * Set to true to also compile materials with clip planes.
  71. */
  72. this.useClipPlane = false;
  73. /**
  74. * Set to true to compile shadow generators before raising the success callback.
  75. */
  76. this.compileShadowGenerators = false;
  77. /**
  78. * Raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  79. */
  80. this.onMeshLoadedObservable = new BABYLON.Observable();
  81. /**
  82. * Raised when the loader creates a texture after parsing the glTF properties of the texture.
  83. */
  84. this.onTextureLoadedObservable = new BABYLON.Observable();
  85. /**
  86. * Raised when the loader creates a material after parsing the glTF properties of the material.
  87. */
  88. this.onMaterialLoadedObservable = new BABYLON.Observable();
  89. /**
  90. * Raised when the asset is completely loaded, immediately before the loader is disposed.
  91. * For assets with LODs, raised when all of the LODs are complete.
  92. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  93. */
  94. this.onCompleteObservable = new BABYLON.Observable();
  95. /**
  96. * Raised after the loader is disposed.
  97. */
  98. this.onDisposeObservable = new BABYLON.Observable();
  99. /**
  100. * Raised after a loader extension is created.
  101. * Set additional options for a loader extension in this event.
  102. */
  103. this.onExtensionLoadedObservable = new BABYLON.Observable();
  104. // #endregion
  105. this._loader = null;
  106. this.name = "gltf";
  107. this.extensions = {
  108. ".gltf": { isBinary: false },
  109. ".glb": { isBinary: true }
  110. };
  111. }
  112. Object.defineProperty(GLTFFileLoader.prototype, "onParsed", {
  113. set: function (callback) {
  114. if (this._onParsedObserver) {
  115. this.onParsedObservable.remove(this._onParsedObserver);
  116. }
  117. this._onParsedObserver = this.onParsedObservable.add(callback);
  118. },
  119. enumerable: true,
  120. configurable: true
  121. });
  122. Object.defineProperty(GLTFFileLoader.prototype, "onMeshLoaded", {
  123. set: function (callback) {
  124. if (this._onMeshLoadedObserver) {
  125. this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver);
  126. }
  127. this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback);
  128. },
  129. enumerable: true,
  130. configurable: true
  131. });
  132. Object.defineProperty(GLTFFileLoader.prototype, "onTextureLoaded", {
  133. set: function (callback) {
  134. if (this._onTextureLoadedObserver) {
  135. this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver);
  136. }
  137. this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback);
  138. },
  139. enumerable: true,
  140. configurable: true
  141. });
  142. Object.defineProperty(GLTFFileLoader.prototype, "onMaterialLoaded", {
  143. set: function (callback) {
  144. if (this._onMaterialLoadedObserver) {
  145. this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver);
  146. }
  147. this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback);
  148. },
  149. enumerable: true,
  150. configurable: true
  151. });
  152. Object.defineProperty(GLTFFileLoader.prototype, "onComplete", {
  153. set: function (callback) {
  154. if (this._onCompleteObserver) {
  155. this.onCompleteObservable.remove(this._onCompleteObserver);
  156. }
  157. this._onCompleteObserver = this.onCompleteObservable.add(callback);
  158. },
  159. enumerable: true,
  160. configurable: true
  161. });
  162. Object.defineProperty(GLTFFileLoader.prototype, "onDispose", {
  163. set: function (callback) {
  164. if (this._onDisposeObserver) {
  165. this.onDisposeObservable.remove(this._onDisposeObserver);
  166. }
  167. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  168. },
  169. enumerable: true,
  170. configurable: true
  171. });
  172. Object.defineProperty(GLTFFileLoader.prototype, "onExtensionLoaded", {
  173. set: function (callback) {
  174. if (this._onExtensionLoadedObserver) {
  175. this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver);
  176. }
  177. this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback);
  178. },
  179. enumerable: true,
  180. configurable: true
  181. });
  182. /**
  183. * Returns a promise that resolves when the asset is completely loaded.
  184. * @returns A promise that resolves when the asset is completely loaded.
  185. */
  186. GLTFFileLoader.prototype.whenCompleteAsync = function () {
  187. var _this = this;
  188. return new Promise(function (resolve) {
  189. _this.onCompleteObservable.add(function () {
  190. resolve();
  191. }, undefined, undefined, undefined, true);
  192. });
  193. };
  194. Object.defineProperty(GLTFFileLoader.prototype, "loaderState", {
  195. /**
  196. * The loader state (LOADING, READY, COMPLETE) or null if the loader is not active.
  197. */
  198. get: function () {
  199. return this._loader ? this._loader.state : null;
  200. },
  201. enumerable: true,
  202. configurable: true
  203. });
  204. /**
  205. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  206. */
  207. GLTFFileLoader.prototype.dispose = function () {
  208. if (this._loader) {
  209. this._loader.dispose();
  210. this._loader = null;
  211. }
  212. this.onMeshLoadedObservable.clear();
  213. this.onTextureLoadedObservable.clear();
  214. this.onMaterialLoadedObservable.clear();
  215. this.onDisposeObservable.notifyObservers(this);
  216. this.onDisposeObservable.clear();
  217. };
  218. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  219. var _this = this;
  220. return Promise.resolve().then(function () {
  221. var loaderData = _this._parse(data);
  222. _this._loader = _this._getLoader(loaderData);
  223. return _this._loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onProgress);
  224. });
  225. };
  226. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  227. var _this = this;
  228. return Promise.resolve().then(function () {
  229. var loaderData = _this._parse(data);
  230. _this._loader = _this._getLoader(loaderData);
  231. return _this._loader.loadAsync(scene, loaderData, rootUrl, onProgress);
  232. });
  233. };
  234. GLTFFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress) {
  235. var _this = this;
  236. return Promise.resolve().then(function () {
  237. var loaderData = _this._parse(data);
  238. _this._loader = _this._getLoader(loaderData);
  239. return _this._loader.importMeshAsync(null, scene, loaderData, rootUrl, onProgress).then(function (result) {
  240. var container = new BABYLON.AssetContainer(scene);
  241. Array.prototype.push.apply(container.meshes, result.meshes);
  242. Array.prototype.push.apply(container.particleSystems, result.particleSystems);
  243. Array.prototype.push.apply(container.skeletons, result.skeletons);
  244. Array.prototype.push.apply(container.animationGroups, result.animationGroups);
  245. container.removeAllFromScene();
  246. return container;
  247. });
  248. });
  249. };
  250. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  251. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  252. };
  253. GLTFFileLoader.prototype.createPlugin = function () {
  254. return new GLTFFileLoader();
  255. };
  256. GLTFFileLoader.prototype._parse = function (data) {
  257. var parsedData;
  258. if (data instanceof ArrayBuffer) {
  259. parsedData = GLTFFileLoader._parseBinary(data);
  260. }
  261. else {
  262. parsedData = {
  263. json: JSON.parse(data),
  264. bin: null
  265. };
  266. }
  267. this.onParsedObservable.notifyObservers(parsedData);
  268. this.onParsedObservable.clear();
  269. return parsedData;
  270. };
  271. GLTFFileLoader.prototype._getLoader = function (loaderData) {
  272. var _this = this;
  273. var loaderVersion = { major: 2, minor: 0 };
  274. var asset = loaderData.json.asset || {};
  275. var version = GLTFFileLoader._parseVersion(asset.version);
  276. if (!version) {
  277. throw new Error("Invalid version: " + asset.version);
  278. }
  279. if (asset.minVersion !== undefined) {
  280. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  281. if (!minVersion) {
  282. throw new Error("Invalid minimum version: " + asset.minVersion);
  283. }
  284. if (GLTFFileLoader._compareVersion(minVersion, loaderVersion) > 0) {
  285. throw new Error("Incompatible minimum version: " + asset.minVersion);
  286. }
  287. }
  288. var createLoaders = {
  289. 1: GLTFFileLoader.CreateGLTFLoaderV1,
  290. 2: GLTFFileLoader.CreateGLTFLoaderV2
  291. };
  292. var createLoader = createLoaders[version.major];
  293. if (!createLoader) {
  294. throw new Error("Unsupported version: " + asset.version);
  295. }
  296. var loader = createLoader();
  297. loader.coordinateSystemMode = this.coordinateSystemMode;
  298. loader.animationStartMode = this.animationStartMode;
  299. loader.compileMaterials = this.compileMaterials;
  300. loader.useClipPlane = this.useClipPlane;
  301. loader.compileShadowGenerators = this.compileShadowGenerators;
  302. loader.onMeshLoadedObservable.add(function (mesh) { return _this.onMeshLoadedObservable.notifyObservers(mesh); });
  303. loader.onTextureLoadedObservable.add(function (texture) { return _this.onTextureLoadedObservable.notifyObservers(texture); });
  304. loader.onMaterialLoadedObservable.add(function (material) { return _this.onMaterialLoadedObservable.notifyObservers(material); });
  305. loader.onExtensionLoadedObservable.add(function (extension) { return _this.onExtensionLoadedObservable.notifyObservers(extension); });
  306. loader.onCompleteObservable.add(function () {
  307. _this.onMeshLoadedObservable.clear();
  308. _this.onTextureLoadedObservable.clear();
  309. _this.onMaterialLoadedObservable.clear();
  310. _this.onCompleteObservable.notifyObservers(_this);
  311. _this.onCompleteObservable.clear();
  312. });
  313. return loader;
  314. };
  315. GLTFFileLoader._parseBinary = function (data) {
  316. var Binary = {
  317. Magic: 0x46546C67
  318. };
  319. var binaryReader = new BinaryReader(data);
  320. var magic = binaryReader.readUint32();
  321. if (magic !== Binary.Magic) {
  322. throw new Error("Unexpected magic: " + magic);
  323. }
  324. var version = binaryReader.readUint32();
  325. switch (version) {
  326. case 1: return GLTFFileLoader._parseV1(binaryReader);
  327. case 2: return GLTFFileLoader._parseV2(binaryReader);
  328. }
  329. throw new Error("Unsupported version: " + version);
  330. };
  331. GLTFFileLoader._parseV1 = function (binaryReader) {
  332. var ContentFormat = {
  333. JSON: 0
  334. };
  335. var length = binaryReader.readUint32();
  336. if (length != binaryReader.getLength()) {
  337. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  338. }
  339. var contentLength = binaryReader.readUint32();
  340. var contentFormat = binaryReader.readUint32();
  341. var content;
  342. switch (contentFormat) {
  343. case ContentFormat.JSON: {
  344. content = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength)));
  345. break;
  346. }
  347. default: {
  348. throw new Error("Unexpected content format: " + contentFormat);
  349. }
  350. }
  351. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  352. var body = binaryReader.readUint8Array(bytesRemaining);
  353. return {
  354. json: content,
  355. bin: body
  356. };
  357. };
  358. GLTFFileLoader._parseV2 = function (binaryReader) {
  359. var ChunkFormat = {
  360. JSON: 0x4E4F534A,
  361. BIN: 0x004E4942
  362. };
  363. var length = binaryReader.readUint32();
  364. if (length !== binaryReader.getLength()) {
  365. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  366. }
  367. // JSON chunk
  368. var chunkLength = binaryReader.readUint32();
  369. var chunkFormat = binaryReader.readUint32();
  370. if (chunkFormat !== ChunkFormat.JSON) {
  371. throw new Error("First chunk format is not JSON");
  372. }
  373. var json = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength)));
  374. // Look for BIN chunk
  375. var bin = null;
  376. while (binaryReader.getPosition() < binaryReader.getLength()) {
  377. var chunkLength_1 = binaryReader.readUint32();
  378. var chunkFormat_1 = binaryReader.readUint32();
  379. switch (chunkFormat_1) {
  380. case ChunkFormat.JSON: {
  381. throw new Error("Unexpected JSON chunk");
  382. }
  383. case ChunkFormat.BIN: {
  384. bin = binaryReader.readUint8Array(chunkLength_1);
  385. break;
  386. }
  387. default: {
  388. // ignore unrecognized chunkFormat
  389. binaryReader.skipBytes(chunkLength_1);
  390. break;
  391. }
  392. }
  393. }
  394. return {
  395. json: json,
  396. bin: bin
  397. };
  398. };
  399. GLTFFileLoader._parseVersion = function (version) {
  400. if (version === "1.0" || version === "1.0.1") {
  401. return {
  402. major: 1,
  403. minor: 0
  404. };
  405. }
  406. var match = (version + "").match(/^(\d+)\.(\d+)/);
  407. if (!match) {
  408. return null;
  409. }
  410. return {
  411. major: parseInt(match[1]),
  412. minor: parseInt(match[2])
  413. };
  414. };
  415. GLTFFileLoader._compareVersion = function (a, b) {
  416. if (a.major > b.major)
  417. return 1;
  418. if (a.major < b.major)
  419. return -1;
  420. if (a.minor > b.minor)
  421. return 1;
  422. if (a.minor < b.minor)
  423. return -1;
  424. return 0;
  425. };
  426. GLTFFileLoader._decodeBufferToText = function (buffer) {
  427. var result = "";
  428. var length = buffer.byteLength;
  429. for (var i = 0; i < length; i++) {
  430. result += String.fromCharCode(buffer[i]);
  431. }
  432. return result;
  433. };
  434. // #endregion
  435. // #region V1 options
  436. GLTFFileLoader.IncrementalLoading = true;
  437. GLTFFileLoader.HomogeneousCoordinates = false;
  438. return GLTFFileLoader;
  439. }());
  440. BABYLON.GLTFFileLoader = GLTFFileLoader;
  441. var BinaryReader = /** @class */ (function () {
  442. function BinaryReader(arrayBuffer) {
  443. this._arrayBuffer = arrayBuffer;
  444. this._dataView = new DataView(arrayBuffer);
  445. this._byteOffset = 0;
  446. }
  447. BinaryReader.prototype.getPosition = function () {
  448. return this._byteOffset;
  449. };
  450. BinaryReader.prototype.getLength = function () {
  451. return this._arrayBuffer.byteLength;
  452. };
  453. BinaryReader.prototype.readUint32 = function () {
  454. var value = this._dataView.getUint32(this._byteOffset, true);
  455. this._byteOffset += 4;
  456. return value;
  457. };
  458. BinaryReader.prototype.readUint8Array = function (length) {
  459. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  460. this._byteOffset += length;
  461. return value;
  462. };
  463. BinaryReader.prototype.skipBytes = function (length) {
  464. this._byteOffset += length;
  465. };
  466. return BinaryReader;
  467. }());
  468. if (BABYLON.SceneLoader) {
  469. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  470. }
  471. })(BABYLON || (BABYLON = {}));
  472. //# sourceMappingURL=babylon.glTFFileLoader.js.map
  473. "use strict";
  474. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  475. var BABYLON;
  476. (function (BABYLON) {
  477. var GLTF1;
  478. (function (GLTF1) {
  479. /**
  480. * Enums
  481. */
  482. var EComponentType;
  483. (function (EComponentType) {
  484. EComponentType[EComponentType["BYTE"] = 5120] = "BYTE";
  485. EComponentType[EComponentType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  486. EComponentType[EComponentType["SHORT"] = 5122] = "SHORT";
  487. EComponentType[EComponentType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  488. EComponentType[EComponentType["FLOAT"] = 5126] = "FLOAT";
  489. })(EComponentType = GLTF1.EComponentType || (GLTF1.EComponentType = {}));
  490. var EShaderType;
  491. (function (EShaderType) {
  492. EShaderType[EShaderType["FRAGMENT"] = 35632] = "FRAGMENT";
  493. EShaderType[EShaderType["VERTEX"] = 35633] = "VERTEX";
  494. })(EShaderType = GLTF1.EShaderType || (GLTF1.EShaderType = {}));
  495. var EParameterType;
  496. (function (EParameterType) {
  497. EParameterType[EParameterType["BYTE"] = 5120] = "BYTE";
  498. EParameterType[EParameterType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  499. EParameterType[EParameterType["SHORT"] = 5122] = "SHORT";
  500. EParameterType[EParameterType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  501. EParameterType[EParameterType["INT"] = 5124] = "INT";
  502. EParameterType[EParameterType["UNSIGNED_INT"] = 5125] = "UNSIGNED_INT";
  503. EParameterType[EParameterType["FLOAT"] = 5126] = "FLOAT";
  504. EParameterType[EParameterType["FLOAT_VEC2"] = 35664] = "FLOAT_VEC2";
  505. EParameterType[EParameterType["FLOAT_VEC3"] = 35665] = "FLOAT_VEC3";
  506. EParameterType[EParameterType["FLOAT_VEC4"] = 35666] = "FLOAT_VEC4";
  507. EParameterType[EParameterType["INT_VEC2"] = 35667] = "INT_VEC2";
  508. EParameterType[EParameterType["INT_VEC3"] = 35668] = "INT_VEC3";
  509. EParameterType[EParameterType["INT_VEC4"] = 35669] = "INT_VEC4";
  510. EParameterType[EParameterType["BOOL"] = 35670] = "BOOL";
  511. EParameterType[EParameterType["BOOL_VEC2"] = 35671] = "BOOL_VEC2";
  512. EParameterType[EParameterType["BOOL_VEC3"] = 35672] = "BOOL_VEC3";
  513. EParameterType[EParameterType["BOOL_VEC4"] = 35673] = "BOOL_VEC4";
  514. EParameterType[EParameterType["FLOAT_MAT2"] = 35674] = "FLOAT_MAT2";
  515. EParameterType[EParameterType["FLOAT_MAT3"] = 35675] = "FLOAT_MAT3";
  516. EParameterType[EParameterType["FLOAT_MAT4"] = 35676] = "FLOAT_MAT4";
  517. EParameterType[EParameterType["SAMPLER_2D"] = 35678] = "SAMPLER_2D";
  518. })(EParameterType = GLTF1.EParameterType || (GLTF1.EParameterType = {}));
  519. var ETextureWrapMode;
  520. (function (ETextureWrapMode) {
  521. ETextureWrapMode[ETextureWrapMode["CLAMP_TO_EDGE"] = 33071] = "CLAMP_TO_EDGE";
  522. ETextureWrapMode[ETextureWrapMode["MIRRORED_REPEAT"] = 33648] = "MIRRORED_REPEAT";
  523. ETextureWrapMode[ETextureWrapMode["REPEAT"] = 10497] = "REPEAT";
  524. })(ETextureWrapMode = GLTF1.ETextureWrapMode || (GLTF1.ETextureWrapMode = {}));
  525. var ETextureFilterType;
  526. (function (ETextureFilterType) {
  527. ETextureFilterType[ETextureFilterType["NEAREST"] = 9728] = "NEAREST";
  528. ETextureFilterType[ETextureFilterType["LINEAR"] = 9728] = "LINEAR";
  529. ETextureFilterType[ETextureFilterType["NEAREST_MIPMAP_NEAREST"] = 9984] = "NEAREST_MIPMAP_NEAREST";
  530. ETextureFilterType[ETextureFilterType["LINEAR_MIPMAP_NEAREST"] = 9985] = "LINEAR_MIPMAP_NEAREST";
  531. ETextureFilterType[ETextureFilterType["NEAREST_MIPMAP_LINEAR"] = 9986] = "NEAREST_MIPMAP_LINEAR";
  532. ETextureFilterType[ETextureFilterType["LINEAR_MIPMAP_LINEAR"] = 9987] = "LINEAR_MIPMAP_LINEAR";
  533. })(ETextureFilterType = GLTF1.ETextureFilterType || (GLTF1.ETextureFilterType = {}));
  534. var ETextureFormat;
  535. (function (ETextureFormat) {
  536. ETextureFormat[ETextureFormat["ALPHA"] = 6406] = "ALPHA";
  537. ETextureFormat[ETextureFormat["RGB"] = 6407] = "RGB";
  538. ETextureFormat[ETextureFormat["RGBA"] = 6408] = "RGBA";
  539. ETextureFormat[ETextureFormat["LUMINANCE"] = 6409] = "LUMINANCE";
  540. ETextureFormat[ETextureFormat["LUMINANCE_ALPHA"] = 6410] = "LUMINANCE_ALPHA";
  541. })(ETextureFormat = GLTF1.ETextureFormat || (GLTF1.ETextureFormat = {}));
  542. var ECullingType;
  543. (function (ECullingType) {
  544. ECullingType[ECullingType["FRONT"] = 1028] = "FRONT";
  545. ECullingType[ECullingType["BACK"] = 1029] = "BACK";
  546. ECullingType[ECullingType["FRONT_AND_BACK"] = 1032] = "FRONT_AND_BACK";
  547. })(ECullingType = GLTF1.ECullingType || (GLTF1.ECullingType = {}));
  548. var EBlendingFunction;
  549. (function (EBlendingFunction) {
  550. EBlendingFunction[EBlendingFunction["ZERO"] = 0] = "ZERO";
  551. EBlendingFunction[EBlendingFunction["ONE"] = 1] = "ONE";
  552. EBlendingFunction[EBlendingFunction["SRC_COLOR"] = 768] = "SRC_COLOR";
  553. EBlendingFunction[EBlendingFunction["ONE_MINUS_SRC_COLOR"] = 769] = "ONE_MINUS_SRC_COLOR";
  554. EBlendingFunction[EBlendingFunction["DST_COLOR"] = 774] = "DST_COLOR";
  555. EBlendingFunction[EBlendingFunction["ONE_MINUS_DST_COLOR"] = 775] = "ONE_MINUS_DST_COLOR";
  556. EBlendingFunction[EBlendingFunction["SRC_ALPHA"] = 770] = "SRC_ALPHA";
  557. EBlendingFunction[EBlendingFunction["ONE_MINUS_SRC_ALPHA"] = 771] = "ONE_MINUS_SRC_ALPHA";
  558. EBlendingFunction[EBlendingFunction["DST_ALPHA"] = 772] = "DST_ALPHA";
  559. EBlendingFunction[EBlendingFunction["ONE_MINUS_DST_ALPHA"] = 773] = "ONE_MINUS_DST_ALPHA";
  560. EBlendingFunction[EBlendingFunction["CONSTANT_COLOR"] = 32769] = "CONSTANT_COLOR";
  561. EBlendingFunction[EBlendingFunction["ONE_MINUS_CONSTANT_COLOR"] = 32770] = "ONE_MINUS_CONSTANT_COLOR";
  562. EBlendingFunction[EBlendingFunction["CONSTANT_ALPHA"] = 32771] = "CONSTANT_ALPHA";
  563. EBlendingFunction[EBlendingFunction["ONE_MINUS_CONSTANT_ALPHA"] = 32772] = "ONE_MINUS_CONSTANT_ALPHA";
  564. EBlendingFunction[EBlendingFunction["SRC_ALPHA_SATURATE"] = 776] = "SRC_ALPHA_SATURATE";
  565. })(EBlendingFunction = GLTF1.EBlendingFunction || (GLTF1.EBlendingFunction = {}));
  566. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  567. })(BABYLON || (BABYLON = {}));
  568. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  569. "use strict";
  570. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  571. var BABYLON;
  572. (function (BABYLON) {
  573. var GLTF1;
  574. (function (GLTF1) {
  575. /**
  576. * Tokenizer. Used for shaders compatibility
  577. * Automatically map world, view, projection, worldViewProjection, attributes and so on
  578. */
  579. var ETokenType;
  580. (function (ETokenType) {
  581. ETokenType[ETokenType["IDENTIFIER"] = 1] = "IDENTIFIER";
  582. ETokenType[ETokenType["UNKNOWN"] = 2] = "UNKNOWN";
  583. ETokenType[ETokenType["END_OF_INPUT"] = 3] = "END_OF_INPUT";
  584. })(ETokenType || (ETokenType = {}));
  585. var Tokenizer = /** @class */ (function () {
  586. function Tokenizer(toParse) {
  587. this._pos = 0;
  588. this.currentToken = ETokenType.UNKNOWN;
  589. this.currentIdentifier = "";
  590. this.currentString = "";
  591. this.isLetterOrDigitPattern = /^[a-zA-Z0-9]+$/;
  592. this._toParse = toParse;
  593. this._maxPos = toParse.length;
  594. }
  595. Tokenizer.prototype.getNextToken = function () {
  596. if (this.isEnd())
  597. return ETokenType.END_OF_INPUT;
  598. this.currentString = this.read();
  599. this.currentToken = ETokenType.UNKNOWN;
  600. if (this.currentString === "_" || this.isLetterOrDigitPattern.test(this.currentString)) {
  601. this.currentToken = ETokenType.IDENTIFIER;
  602. this.currentIdentifier = this.currentString;
  603. while (!this.isEnd() && (this.isLetterOrDigitPattern.test(this.currentString = this.peek()) || this.currentString === "_")) {
  604. this.currentIdentifier += this.currentString;
  605. this.forward();
  606. }
  607. }
  608. return this.currentToken;
  609. };
  610. Tokenizer.prototype.peek = function () {
  611. return this._toParse[this._pos];
  612. };
  613. Tokenizer.prototype.read = function () {
  614. return this._toParse[this._pos++];
  615. };
  616. Tokenizer.prototype.forward = function () {
  617. this._pos++;
  618. };
  619. Tokenizer.prototype.isEnd = function () {
  620. return this._pos >= this._maxPos;
  621. };
  622. return Tokenizer;
  623. }());
  624. /**
  625. * Values
  626. */
  627. var glTFTransforms = ["MODEL", "VIEW", "PROJECTION", "MODELVIEW", "MODELVIEWPROJECTION", "JOINTMATRIX"];
  628. var babylonTransforms = ["world", "view", "projection", "worldView", "worldViewProjection", "mBones"];
  629. var glTFAnimationPaths = ["translation", "rotation", "scale"];
  630. var babylonAnimationPaths = ["position", "rotationQuaternion", "scaling"];
  631. /**
  632. * Parse
  633. */
  634. var parseBuffers = function (parsedBuffers, gltfRuntime) {
  635. for (var buf in parsedBuffers) {
  636. var parsedBuffer = parsedBuffers[buf];
  637. gltfRuntime.buffers[buf] = parsedBuffer;
  638. gltfRuntime.buffersCount++;
  639. }
  640. };
  641. var parseShaders = function (parsedShaders, gltfRuntime) {
  642. for (var sha in parsedShaders) {
  643. var parsedShader = parsedShaders[sha];
  644. gltfRuntime.shaders[sha] = parsedShader;
  645. gltfRuntime.shaderscount++;
  646. }
  647. };
  648. var parseObject = function (parsedObjects, runtimeProperty, gltfRuntime) {
  649. for (var object in parsedObjects) {
  650. var parsedObject = parsedObjects[object];
  651. gltfRuntime[runtimeProperty][object] = parsedObject;
  652. }
  653. };
  654. /**
  655. * Utils
  656. */
  657. var normalizeUVs = function (buffer) {
  658. if (!buffer) {
  659. return;
  660. }
  661. for (var i = 0; i < buffer.length / 2; i++) {
  662. buffer[i * 2 + 1] = 1.0 - buffer[i * 2 + 1];
  663. }
  664. };
  665. var getAttribute = function (attributeParameter) {
  666. if (attributeParameter.semantic === "NORMAL") {
  667. return "normal";
  668. }
  669. else if (attributeParameter.semantic === "POSITION") {
  670. return "position";
  671. }
  672. else if (attributeParameter.semantic === "JOINT") {
  673. return "matricesIndices";
  674. }
  675. else if (attributeParameter.semantic === "WEIGHT") {
  676. return "matricesWeights";
  677. }
  678. else if (attributeParameter.semantic === "COLOR") {
  679. return "color";
  680. }
  681. else if (attributeParameter.semantic && attributeParameter.semantic.indexOf("TEXCOORD_") !== -1) {
  682. var channel = Number(attributeParameter.semantic.split("_")[1]);
  683. return "uv" + (channel === 0 ? "" : channel + 1);
  684. }
  685. return null;
  686. };
  687. /**
  688. * Loads and creates animations
  689. */
  690. var loadAnimations = function (gltfRuntime) {
  691. for (var anim in gltfRuntime.animations) {
  692. var animation = gltfRuntime.animations[anim];
  693. if (!animation.channels || !animation.samplers) {
  694. continue;
  695. }
  696. var lastAnimation = null;
  697. for (var i = 0; i < animation.channels.length; i++) {
  698. // Get parameters and load buffers
  699. var channel = animation.channels[i];
  700. var sampler = animation.samplers[channel.sampler];
  701. if (!sampler) {
  702. continue;
  703. }
  704. var inputData = null;
  705. var outputData = null;
  706. if (animation.parameters) {
  707. inputData = animation.parameters[sampler.input];
  708. outputData = animation.parameters[sampler.output];
  709. }
  710. else {
  711. inputData = sampler.input;
  712. outputData = sampler.output;
  713. }
  714. var bufferInput = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[inputData]);
  715. var bufferOutput = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[outputData]);
  716. var targetID = channel.target.id;
  717. var targetNode = gltfRuntime.scene.getNodeByID(targetID);
  718. if (targetNode === null) {
  719. targetNode = gltfRuntime.scene.getNodeByName(targetID);
  720. }
  721. if (targetNode === null) {
  722. BABYLON.Tools.Warn("Creating animation named " + anim + ". But cannot find node named " + targetID + " to attach to");
  723. continue;
  724. }
  725. var isBone = targetNode instanceof BABYLON.Bone;
  726. // Get target path (position, rotation or scaling)
  727. var targetPath = channel.target.path;
  728. var targetPathIndex = glTFAnimationPaths.indexOf(targetPath);
  729. if (targetPathIndex !== -1) {
  730. targetPath = babylonAnimationPaths[targetPathIndex];
  731. }
  732. // Determine animation type
  733. var animationType = BABYLON.Animation.ANIMATIONTYPE_MATRIX;
  734. if (!isBone) {
  735. if (targetPath === "rotationQuaternion") {
  736. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  737. targetNode.rotationQuaternion = new BABYLON.Quaternion();
  738. }
  739. else {
  740. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  741. }
  742. }
  743. // Create animation and key frames
  744. var babylonAnimation = null;
  745. var keys = [];
  746. var arrayOffset = 0;
  747. var modifyKey = false;
  748. if (isBone && lastAnimation && lastAnimation.getKeys().length === bufferInput.length) {
  749. babylonAnimation = lastAnimation;
  750. modifyKey = true;
  751. }
  752. if (!modifyKey) {
  753. babylonAnimation = new BABYLON.Animation(anim, isBone ? "_matrix" : targetPath, 1, animationType, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  754. }
  755. // For each frame
  756. for (var j = 0; j < bufferInput.length; j++) {
  757. var value = null;
  758. if (targetPath === "rotationQuaternion") {
  759. value = BABYLON.Quaternion.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2], bufferOutput[arrayOffset + 3]]);
  760. arrayOffset += 4;
  761. }
  762. else {
  763. value = BABYLON.Vector3.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2]]);
  764. arrayOffset += 3;
  765. }
  766. if (isBone) {
  767. var bone = targetNode;
  768. var translation = BABYLON.Vector3.Zero();
  769. var rotationQuaternion = new BABYLON.Quaternion();
  770. var scaling = BABYLON.Vector3.Zero();
  771. // Warning on decompose
  772. var mat = bone.getBaseMatrix();
  773. if (modifyKey && lastAnimation) {
  774. mat = lastAnimation.getKeys()[j].value;
  775. }
  776. mat.decompose(scaling, rotationQuaternion, translation);
  777. if (targetPath === "position") {
  778. translation = value;
  779. }
  780. else if (targetPath === "rotationQuaternion") {
  781. rotationQuaternion = value;
  782. }
  783. else {
  784. scaling = value;
  785. }
  786. value = BABYLON.Matrix.Compose(scaling, rotationQuaternion, translation);
  787. }
  788. if (!modifyKey) {
  789. keys.push({
  790. frame: bufferInput[j],
  791. value: value
  792. });
  793. }
  794. else if (lastAnimation) {
  795. lastAnimation.getKeys()[j].value = value;
  796. }
  797. }
  798. // Finish
  799. if (!modifyKey && babylonAnimation) {
  800. babylonAnimation.setKeys(keys);
  801. targetNode.animations.push(babylonAnimation);
  802. }
  803. lastAnimation = babylonAnimation;
  804. gltfRuntime.scene.stopAnimation(targetNode);
  805. gltfRuntime.scene.beginAnimation(targetNode, 0, bufferInput[bufferInput.length - 1], true, 1.0);
  806. }
  807. }
  808. };
  809. /**
  810. * Returns the bones transformation matrix
  811. */
  812. var configureBoneTransformation = function (node) {
  813. var mat = null;
  814. if (node.translation || node.rotation || node.scale) {
  815. var scale = BABYLON.Vector3.FromArray(node.scale || [1, 1, 1]);
  816. var rotation = BABYLON.Quaternion.FromArray(node.rotation || [0, 0, 0, 1]);
  817. var position = BABYLON.Vector3.FromArray(node.translation || [0, 0, 0]);
  818. mat = BABYLON.Matrix.Compose(scale, rotation, position);
  819. }
  820. else {
  821. mat = BABYLON.Matrix.FromArray(node.matrix);
  822. }
  823. return mat;
  824. };
  825. /**
  826. * Returns the parent bone
  827. */
  828. var getParentBone = function (gltfRuntime, skins, jointName, newSkeleton) {
  829. // Try to find
  830. for (var i = 0; i < newSkeleton.bones.length; i++) {
  831. if (newSkeleton.bones[i].name === jointName) {
  832. return newSkeleton.bones[i];
  833. }
  834. }
  835. // Not found, search in gltf nodes
  836. var nodes = gltfRuntime.nodes;
  837. for (var nde in nodes) {
  838. var node = nodes[nde];
  839. if (!node.jointName) {
  840. continue;
  841. }
  842. var children = node.children;
  843. for (var i = 0; i < children.length; i++) {
  844. var child = gltfRuntime.nodes[children[i]];
  845. if (!child.jointName) {
  846. continue;
  847. }
  848. if (child.jointName === jointName) {
  849. var mat = configureBoneTransformation(node);
  850. var bone = new BABYLON.Bone(node.name || "", newSkeleton, getParentBone(gltfRuntime, skins, node.jointName, newSkeleton), mat);
  851. bone.id = nde;
  852. return bone;
  853. }
  854. }
  855. }
  856. return null;
  857. };
  858. /**
  859. * Returns the appropriate root node
  860. */
  861. var getNodeToRoot = function (nodesToRoot, id) {
  862. for (var i = 0; i < nodesToRoot.length; i++) {
  863. var nodeToRoot = nodesToRoot[i];
  864. for (var j = 0; j < nodeToRoot.node.children.length; j++) {
  865. var child = nodeToRoot.node.children[j];
  866. if (child === id) {
  867. return nodeToRoot.bone;
  868. }
  869. }
  870. }
  871. return null;
  872. };
  873. /**
  874. * Returns the node with the joint name
  875. */
  876. var getJointNode = function (gltfRuntime, jointName) {
  877. var nodes = gltfRuntime.nodes;
  878. var node = nodes[jointName];
  879. if (node) {
  880. return {
  881. node: node,
  882. id: jointName
  883. };
  884. }
  885. for (var nde in nodes) {
  886. node = nodes[nde];
  887. if (node.jointName === jointName) {
  888. return {
  889. node: node,
  890. id: nde
  891. };
  892. }
  893. }
  894. return null;
  895. };
  896. /**
  897. * Checks if a nodes is in joints
  898. */
  899. var nodeIsInJoints = function (skins, id) {
  900. for (var i = 0; i < skins.jointNames.length; i++) {
  901. if (skins.jointNames[i] === id) {
  902. return true;
  903. }
  904. }
  905. return false;
  906. };
  907. /**
  908. * Fills the nodes to root for bones and builds hierarchy
  909. */
  910. var getNodesToRoot = function (gltfRuntime, newSkeleton, skins, nodesToRoot) {
  911. // Creates nodes for root
  912. for (var nde in gltfRuntime.nodes) {
  913. var node = gltfRuntime.nodes[nde];
  914. var id = nde;
  915. if (!node.jointName || nodeIsInJoints(skins, node.jointName)) {
  916. continue;
  917. }
  918. // Create node to root bone
  919. var mat = configureBoneTransformation(node);
  920. var bone = new BABYLON.Bone(node.name || "", newSkeleton, null, mat);
  921. bone.id = id;
  922. nodesToRoot.push({ bone: bone, node: node, id: id });
  923. }
  924. // Parenting
  925. for (var i = 0; i < nodesToRoot.length; i++) {
  926. var nodeToRoot = nodesToRoot[i];
  927. var children = nodeToRoot.node.children;
  928. for (var j = 0; j < children.length; j++) {
  929. var child = null;
  930. for (var k = 0; k < nodesToRoot.length; k++) {
  931. if (nodesToRoot[k].id === children[j]) {
  932. child = nodesToRoot[k];
  933. break;
  934. }
  935. }
  936. if (child) {
  937. child.bone._parent = nodeToRoot.bone;
  938. nodeToRoot.bone.children.push(child.bone);
  939. }
  940. }
  941. }
  942. };
  943. /**
  944. * Imports a skeleton
  945. */
  946. var importSkeleton = function (gltfRuntime, skins, mesh, newSkeleton, id) {
  947. if (!newSkeleton) {
  948. newSkeleton = new BABYLON.Skeleton(skins.name || "", "", gltfRuntime.scene);
  949. }
  950. if (!skins.babylonSkeleton) {
  951. return newSkeleton;
  952. }
  953. // Find the root bones
  954. var nodesToRoot = [];
  955. var nodesToRootToAdd = [];
  956. getNodesToRoot(gltfRuntime, newSkeleton, skins, nodesToRoot);
  957. newSkeleton.bones = [];
  958. // Joints
  959. for (var i = 0; i < skins.jointNames.length; i++) {
  960. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  961. if (!jointNode) {
  962. continue;
  963. }
  964. var node = jointNode.node;
  965. if (!node) {
  966. BABYLON.Tools.Warn("Joint named " + skins.jointNames[i] + " does not exist");
  967. continue;
  968. }
  969. var id = jointNode.id;
  970. // Optimize, if the bone already exists...
  971. var existingBone = gltfRuntime.scene.getBoneByID(id);
  972. if (existingBone) {
  973. newSkeleton.bones.push(existingBone);
  974. continue;
  975. }
  976. // Search for parent bone
  977. var foundBone = false;
  978. var parentBone = null;
  979. for (var j = 0; j < i; j++) {
  980. var jointNode_1 = getJointNode(gltfRuntime, skins.jointNames[j]);
  981. if (!jointNode_1) {
  982. continue;
  983. }
  984. var joint = jointNode_1.node;
  985. if (!joint) {
  986. BABYLON.Tools.Warn("Joint named " + skins.jointNames[j] + " does not exist when looking for parent");
  987. continue;
  988. }
  989. var children = joint.children;
  990. if (!children) {
  991. continue;
  992. }
  993. foundBone = false;
  994. for (var k = 0; k < children.length; k++) {
  995. if (children[k] === id) {
  996. parentBone = getParentBone(gltfRuntime, skins, skins.jointNames[j], newSkeleton);
  997. foundBone = true;
  998. break;
  999. }
  1000. }
  1001. if (foundBone) {
  1002. break;
  1003. }
  1004. }
  1005. // Create bone
  1006. var mat = configureBoneTransformation(node);
  1007. if (!parentBone && nodesToRoot.length > 0) {
  1008. parentBone = getNodeToRoot(nodesToRoot, id);
  1009. if (parentBone) {
  1010. if (nodesToRootToAdd.indexOf(parentBone) === -1) {
  1011. nodesToRootToAdd.push(parentBone);
  1012. }
  1013. }
  1014. }
  1015. var bone = new BABYLON.Bone(node.jointName || "", newSkeleton, parentBone, mat);
  1016. bone.id = id;
  1017. }
  1018. // Polish
  1019. var bones = newSkeleton.bones;
  1020. newSkeleton.bones = [];
  1021. for (var i = 0; i < skins.jointNames.length; i++) {
  1022. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  1023. if (!jointNode) {
  1024. continue;
  1025. }
  1026. for (var j = 0; j < bones.length; j++) {
  1027. if (bones[j].id === jointNode.id) {
  1028. newSkeleton.bones.push(bones[j]);
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. newSkeleton.prepare();
  1034. // Finish
  1035. for (var i = 0; i < nodesToRootToAdd.length; i++) {
  1036. newSkeleton.bones.push(nodesToRootToAdd[i]);
  1037. }
  1038. return newSkeleton;
  1039. };
  1040. /**
  1041. * Imports a mesh and its geometries
  1042. */
  1043. var importMesh = function (gltfRuntime, node, meshes, id, newMesh) {
  1044. if (!newMesh) {
  1045. newMesh = new BABYLON.Mesh(node.name || "", gltfRuntime.scene);
  1046. newMesh.id = id;
  1047. }
  1048. if (!node.babylonNode) {
  1049. return newMesh;
  1050. }
  1051. var subMaterials = [];
  1052. var vertexData = null;
  1053. var verticesStarts = new Array();
  1054. var verticesCounts = new Array();
  1055. var indexStarts = new Array();
  1056. var indexCounts = new Array();
  1057. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  1058. var meshID = meshes[meshIndex];
  1059. var mesh = gltfRuntime.meshes[meshID];
  1060. if (!mesh) {
  1061. continue;
  1062. }
  1063. // Positions, normals and UVs
  1064. for (var i = 0; i < mesh.primitives.length; i++) {
  1065. // Temporary vertex data
  1066. var tempVertexData = new BABYLON.VertexData();
  1067. var primitive = mesh.primitives[i];
  1068. if (primitive.mode !== 4) {
  1069. // continue;
  1070. }
  1071. var attributes = primitive.attributes;
  1072. var accessor = null;
  1073. var buffer = null;
  1074. // Set positions, normal and uvs
  1075. for (var semantic in attributes) {
  1076. // Link accessor and buffer view
  1077. accessor = gltfRuntime.accessors[attributes[semantic]];
  1078. buffer = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  1079. if (semantic === "NORMAL") {
  1080. tempVertexData.normals = new Float32Array(buffer.length);
  1081. tempVertexData.normals.set(buffer);
  1082. }
  1083. else if (semantic === "POSITION") {
  1084. if (BABYLON.GLTFFileLoader.HomogeneousCoordinates) {
  1085. tempVertexData.positions = new Float32Array(buffer.length - buffer.length / 4);
  1086. for (var j = 0; j < buffer.length; j += 4) {
  1087. tempVertexData.positions[j] = buffer[j];
  1088. tempVertexData.positions[j + 1] = buffer[j + 1];
  1089. tempVertexData.positions[j + 2] = buffer[j + 2];
  1090. }
  1091. }
  1092. else {
  1093. tempVertexData.positions = new Float32Array(buffer.length);
  1094. tempVertexData.positions.set(buffer);
  1095. }
  1096. verticesCounts.push(tempVertexData.positions.length);
  1097. }
  1098. else if (semantic.indexOf("TEXCOORD_") !== -1) {
  1099. var channel = Number(semantic.split("_")[1]);
  1100. var uvKind = BABYLON.VertexBuffer.UVKind + (channel === 0 ? "" : (channel + 1));
  1101. var uvs = new Float32Array(buffer.length);
  1102. uvs.set(buffer);
  1103. normalizeUVs(uvs);
  1104. tempVertexData.set(uvs, uvKind);
  1105. }
  1106. else if (semantic === "JOINT") {
  1107. tempVertexData.matricesIndices = new Float32Array(buffer.length);
  1108. tempVertexData.matricesIndices.set(buffer);
  1109. }
  1110. else if (semantic === "WEIGHT") {
  1111. tempVertexData.matricesWeights = new Float32Array(buffer.length);
  1112. tempVertexData.matricesWeights.set(buffer);
  1113. }
  1114. else if (semantic === "COLOR") {
  1115. tempVertexData.colors = new Float32Array(buffer.length);
  1116. tempVertexData.colors.set(buffer);
  1117. }
  1118. }
  1119. // Indices
  1120. accessor = gltfRuntime.accessors[primitive.indices];
  1121. if (accessor) {
  1122. buffer = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  1123. tempVertexData.indices = new Int32Array(buffer.length);
  1124. tempVertexData.indices.set(buffer);
  1125. indexCounts.push(tempVertexData.indices.length);
  1126. }
  1127. else {
  1128. // Set indices on the fly
  1129. var indices = [];
  1130. for (var j = 0; j < tempVertexData.positions.length / 3; j++) {
  1131. indices.push(j);
  1132. }
  1133. tempVertexData.indices = new Int32Array(indices);
  1134. indexCounts.push(tempVertexData.indices.length);
  1135. }
  1136. if (!vertexData) {
  1137. vertexData = tempVertexData;
  1138. }
  1139. else {
  1140. vertexData.merge(tempVertexData);
  1141. }
  1142. // Sub material
  1143. var material_1 = gltfRuntime.scene.getMaterialByID(primitive.material);
  1144. subMaterials.push(material_1 === null ? GLTF1.GLTFUtils.GetDefaultMaterial(gltfRuntime.scene) : material_1);
  1145. // Update vertices start and index start
  1146. verticesStarts.push(verticesStarts.length === 0 ? 0 : verticesStarts[verticesStarts.length - 1] + verticesCounts[verticesCounts.length - 2]);
  1147. indexStarts.push(indexStarts.length === 0 ? 0 : indexStarts[indexStarts.length - 1] + indexCounts[indexCounts.length - 2]);
  1148. }
  1149. }
  1150. var material;
  1151. if (subMaterials.length > 1) {
  1152. material = new BABYLON.MultiMaterial("multimat" + id, gltfRuntime.scene);
  1153. material.subMaterials = subMaterials;
  1154. }
  1155. else {
  1156. material = new BABYLON.StandardMaterial("multimat" + id, gltfRuntime.scene);
  1157. }
  1158. if (subMaterials.length === 1) {
  1159. material = subMaterials[0];
  1160. }
  1161. if (!newMesh.material) {
  1162. newMesh.material = material;
  1163. }
  1164. // Apply geometry
  1165. new BABYLON.Geometry(id, gltfRuntime.scene, vertexData, false, newMesh);
  1166. newMesh.computeWorldMatrix(true);
  1167. // Apply submeshes
  1168. newMesh.subMeshes = [];
  1169. var index = 0;
  1170. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  1171. var meshID = meshes[meshIndex];
  1172. var mesh = gltfRuntime.meshes[meshID];
  1173. if (!mesh) {
  1174. continue;
  1175. }
  1176. for (var i = 0; i < mesh.primitives.length; i++) {
  1177. if (mesh.primitives[i].mode !== 4) {
  1178. //continue;
  1179. }
  1180. BABYLON.SubMesh.AddToMesh(index, verticesStarts[index], verticesCounts[index], indexStarts[index], indexCounts[index], newMesh, newMesh, true);
  1181. index++;
  1182. }
  1183. }
  1184. // Finish
  1185. return newMesh;
  1186. };
  1187. /**
  1188. * Configure node transformation from position, rotation and scaling
  1189. */
  1190. var configureNode = function (newNode, position, rotation, scaling) {
  1191. if (newNode.position) {
  1192. newNode.position = position;
  1193. }
  1194. if (newNode.rotationQuaternion || newNode.rotation) {
  1195. newNode.rotationQuaternion = rotation;
  1196. }
  1197. if (newNode.scaling) {
  1198. newNode.scaling = scaling;
  1199. }
  1200. };
  1201. /**
  1202. * Configures node from transformation matrix
  1203. */
  1204. var configureNodeFromMatrix = function (newNode, node, parent) {
  1205. if (node.matrix) {
  1206. var position = new BABYLON.Vector3(0, 0, 0);
  1207. var rotation = new BABYLON.Quaternion();
  1208. var scaling = new BABYLON.Vector3(0, 0, 0);
  1209. var mat = BABYLON.Matrix.FromArray(node.matrix);
  1210. mat.decompose(scaling, rotation, position);
  1211. configureNode(newNode, position, rotation, scaling);
  1212. }
  1213. else if (node.translation && node.rotation && node.scale) {
  1214. configureNode(newNode, BABYLON.Vector3.FromArray(node.translation), BABYLON.Quaternion.FromArray(node.rotation), BABYLON.Vector3.FromArray(node.scale));
  1215. }
  1216. newNode.computeWorldMatrix(true);
  1217. };
  1218. /**
  1219. * Imports a node
  1220. */
  1221. var importNode = function (gltfRuntime, node, id, parent) {
  1222. var lastNode = null;
  1223. if (gltfRuntime.importOnlyMeshes && (node.skin || node.meshes)) {
  1224. if (gltfRuntime.importMeshesNames && gltfRuntime.importMeshesNames.length > 0 && gltfRuntime.importMeshesNames.indexOf(node.name || "") === -1) {
  1225. return null;
  1226. }
  1227. }
  1228. // Meshes
  1229. if (node.skin) {
  1230. if (node.meshes) {
  1231. var skin = gltfRuntime.skins[node.skin];
  1232. var newMesh = importMesh(gltfRuntime, node, node.meshes, id, node.babylonNode);
  1233. newMesh.skeleton = gltfRuntime.scene.getLastSkeletonByID(node.skin);
  1234. if (newMesh.skeleton === null) {
  1235. newMesh.skeleton = importSkeleton(gltfRuntime, skin, newMesh, skin.babylonSkeleton, node.skin);
  1236. if (!skin.babylonSkeleton) {
  1237. skin.babylonSkeleton = newMesh.skeleton;
  1238. }
  1239. }
  1240. lastNode = newMesh;
  1241. }
  1242. }
  1243. else if (node.meshes) {
  1244. /**
  1245. * Improve meshes property
  1246. */
  1247. var newMesh = importMesh(gltfRuntime, node, node.mesh ? [node.mesh] : node.meshes, id, node.babylonNode);
  1248. lastNode = newMesh;
  1249. }
  1250. else if (node.light && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  1251. var light = gltfRuntime.lights[node.light];
  1252. if (light) {
  1253. if (light.type === "ambient") {
  1254. var ambienLight = light[light.type];
  1255. var hemiLight = new BABYLON.HemisphericLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1256. hemiLight.name = node.name || "";
  1257. if (ambienLight.color) {
  1258. hemiLight.diffuse = BABYLON.Color3.FromArray(ambienLight.color);
  1259. }
  1260. lastNode = hemiLight;
  1261. }
  1262. else if (light.type === "directional") {
  1263. var directionalLight = light[light.type];
  1264. var dirLight = new BABYLON.DirectionalLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1265. dirLight.name = node.name || "";
  1266. if (directionalLight.color) {
  1267. dirLight.diffuse = BABYLON.Color3.FromArray(directionalLight.color);
  1268. }
  1269. lastNode = dirLight;
  1270. }
  1271. else if (light.type === "point") {
  1272. var pointLight = light[light.type];
  1273. var ptLight = new BABYLON.PointLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1274. ptLight.name = node.name || "";
  1275. if (pointLight.color) {
  1276. ptLight.diffuse = BABYLON.Color3.FromArray(pointLight.color);
  1277. }
  1278. lastNode = ptLight;
  1279. }
  1280. else if (light.type === "spot") {
  1281. var spotLight = light[light.type];
  1282. var spLight = new BABYLON.SpotLight(node.light, BABYLON.Vector3.Zero(), BABYLON.Vector3.Zero(), 0, 0, gltfRuntime.scene);
  1283. spLight.name = node.name || "";
  1284. if (spotLight.color) {
  1285. spLight.diffuse = BABYLON.Color3.FromArray(spotLight.color);
  1286. }
  1287. if (spotLight.fallOfAngle) {
  1288. spLight.angle = spotLight.fallOfAngle;
  1289. }
  1290. if (spotLight.fallOffExponent) {
  1291. spLight.exponent = spotLight.fallOffExponent;
  1292. }
  1293. lastNode = spLight;
  1294. }
  1295. }
  1296. }
  1297. else if (node.camera && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  1298. var camera = gltfRuntime.cameras[node.camera];
  1299. if (camera) {
  1300. if (camera.type === "orthographic") {
  1301. var orthoCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1302. orthoCamera.name = node.name || "";
  1303. orthoCamera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
  1304. orthoCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  1305. lastNode = orthoCamera;
  1306. }
  1307. else if (camera.type === "perspective") {
  1308. var perspectiveCamera = camera[camera.type];
  1309. var persCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1310. persCamera.name = node.name || "";
  1311. persCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  1312. if (!perspectiveCamera.aspectRatio) {
  1313. perspectiveCamera.aspectRatio = gltfRuntime.scene.getEngine().getRenderWidth() / gltfRuntime.scene.getEngine().getRenderHeight();
  1314. }
  1315. if (perspectiveCamera.znear && perspectiveCamera.zfar) {
  1316. persCamera.maxZ = perspectiveCamera.zfar;
  1317. persCamera.minZ = perspectiveCamera.znear;
  1318. }
  1319. lastNode = persCamera;
  1320. }
  1321. }
  1322. }
  1323. // Empty node
  1324. if (!node.jointName) {
  1325. if (node.babylonNode) {
  1326. return node.babylonNode;
  1327. }
  1328. else if (lastNode === null) {
  1329. var dummy = new BABYLON.Mesh(node.name || "", gltfRuntime.scene);
  1330. node.babylonNode = dummy;
  1331. lastNode = dummy;
  1332. }
  1333. }
  1334. if (lastNode !== null) {
  1335. if (node.matrix && lastNode instanceof BABYLON.Mesh) {
  1336. configureNodeFromMatrix(lastNode, node, parent);
  1337. }
  1338. else {
  1339. var translation = node.translation || [0, 0, 0];
  1340. var rotation = node.rotation || [0, 0, 0, 1];
  1341. var scale = node.scale || [1, 1, 1];
  1342. configureNode(lastNode, BABYLON.Vector3.FromArray(translation), BABYLON.Quaternion.FromArray(rotation), BABYLON.Vector3.FromArray(scale));
  1343. }
  1344. lastNode.updateCache(true);
  1345. node.babylonNode = lastNode;
  1346. }
  1347. return lastNode;
  1348. };
  1349. /**
  1350. * Traverses nodes and creates them
  1351. */
  1352. var traverseNodes = function (gltfRuntime, id, parent, meshIncluded) {
  1353. if (meshIncluded === void 0) { meshIncluded = false; }
  1354. var node = gltfRuntime.nodes[id];
  1355. var newNode = null;
  1356. if (gltfRuntime.importOnlyMeshes && !meshIncluded && gltfRuntime.importMeshesNames) {
  1357. if (gltfRuntime.importMeshesNames.indexOf(node.name || "") !== -1 || gltfRuntime.importMeshesNames.length === 0) {
  1358. meshIncluded = true;
  1359. }
  1360. else {
  1361. meshIncluded = false;
  1362. }
  1363. }
  1364. else {
  1365. meshIncluded = true;
  1366. }
  1367. if (!node.jointName && meshIncluded) {
  1368. newNode = importNode(gltfRuntime, node, id, parent);
  1369. if (newNode !== null) {
  1370. newNode.id = id;
  1371. newNode.parent = parent;
  1372. }
  1373. }
  1374. if (node.children) {
  1375. for (var i = 0; i < node.children.length; i++) {
  1376. traverseNodes(gltfRuntime, node.children[i], newNode, meshIncluded);
  1377. }
  1378. }
  1379. };
  1380. /**
  1381. * do stuff after buffers, shaders are loaded (e.g. hook up materials, load animations, etc.)
  1382. */
  1383. var postLoad = function (gltfRuntime) {
  1384. // Nodes
  1385. var currentScene = gltfRuntime.currentScene;
  1386. if (currentScene) {
  1387. for (var i = 0; i < currentScene.nodes.length; i++) {
  1388. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1389. }
  1390. }
  1391. else {
  1392. for (var thing in gltfRuntime.scenes) {
  1393. currentScene = gltfRuntime.scenes[thing];
  1394. for (var i = 0; i < currentScene.nodes.length; i++) {
  1395. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1396. }
  1397. }
  1398. }
  1399. // Set animations
  1400. loadAnimations(gltfRuntime);
  1401. for (var i = 0; i < gltfRuntime.scene.skeletons.length; i++) {
  1402. var skeleton = gltfRuntime.scene.skeletons[i];
  1403. gltfRuntime.scene.beginAnimation(skeleton, 0, Number.MAX_VALUE, true, 1.0);
  1404. }
  1405. };
  1406. /**
  1407. * onBind shaderrs callback to set uniforms and matrices
  1408. */
  1409. var onBindShaderMaterial = function (mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess) {
  1410. var materialValues = material.values || technique.parameters;
  1411. for (var unif in unTreatedUniforms) {
  1412. var uniform = unTreatedUniforms[unif];
  1413. var type = uniform.type;
  1414. if (type === GLTF1.EParameterType.FLOAT_MAT2 || type === GLTF1.EParameterType.FLOAT_MAT3 || type === GLTF1.EParameterType.FLOAT_MAT4) {
  1415. if (uniform.semantic && !uniform.source && !uniform.node) {
  1416. GLTF1.GLTFUtils.SetMatrix(gltfRuntime.scene, mesh, uniform, unif, shaderMaterial.getEffect());
  1417. }
  1418. else if (uniform.semantic && (uniform.source || uniform.node)) {
  1419. var source = gltfRuntime.scene.getNodeByName(uniform.source || uniform.node || "");
  1420. if (source === null) {
  1421. source = gltfRuntime.scene.getNodeByID(uniform.source || uniform.node || "");
  1422. }
  1423. if (source === null) {
  1424. continue;
  1425. }
  1426. GLTF1.GLTFUtils.SetMatrix(gltfRuntime.scene, source, uniform, unif, shaderMaterial.getEffect());
  1427. }
  1428. }
  1429. else {
  1430. var value = materialValues[technique.uniforms[unif]];
  1431. if (!value) {
  1432. continue;
  1433. }
  1434. if (type === GLTF1.EParameterType.SAMPLER_2D) {
  1435. var texture = gltfRuntime.textures[material.values ? value : uniform.value].babylonTexture;
  1436. if (texture === null || texture === undefined) {
  1437. continue;
  1438. }
  1439. shaderMaterial.getEffect().setTexture(unif, texture);
  1440. }
  1441. else {
  1442. GLTF1.GLTFUtils.SetUniform((shaderMaterial.getEffect()), unif, value, type);
  1443. }
  1444. }
  1445. }
  1446. onSuccess(shaderMaterial);
  1447. };
  1448. /**
  1449. * Prepare uniforms to send the only one time
  1450. * Loads the appropriate textures
  1451. */
  1452. var prepareShaderMaterialUniforms = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms) {
  1453. var materialValues = material.values || technique.parameters;
  1454. var techniqueUniforms = technique.uniforms;
  1455. /**
  1456. * Prepare values here (not matrices)
  1457. */
  1458. for (var unif in unTreatedUniforms) {
  1459. var uniform = unTreatedUniforms[unif];
  1460. var type = uniform.type;
  1461. var value = materialValues[techniqueUniforms[unif]];
  1462. if (value === undefined) {
  1463. // In case the value is the same for all materials
  1464. value = uniform.value;
  1465. }
  1466. if (!value) {
  1467. continue;
  1468. }
  1469. var onLoadTexture = function (uniformName) {
  1470. return function (texture) {
  1471. if (uniform.value && uniformName) {
  1472. // Static uniform
  1473. shaderMaterial.setTexture(uniformName, texture);
  1474. delete unTreatedUniforms[uniformName];
  1475. }
  1476. };
  1477. };
  1478. // Texture (sampler2D)
  1479. if (type === GLTF1.EParameterType.SAMPLER_2D) {
  1480. GLTF1.GLTFLoaderExtension.LoadTextureAsync(gltfRuntime, material.values ? value : uniform.value, onLoadTexture(unif), function () { return onLoadTexture(null); });
  1481. }
  1482. else {
  1483. if (uniform.value && GLTF1.GLTFUtils.SetUniform(shaderMaterial, unif, material.values ? value : uniform.value, type)) {
  1484. // Static uniform
  1485. delete unTreatedUniforms[unif];
  1486. }
  1487. }
  1488. }
  1489. };
  1490. /**
  1491. * Shader compilation failed
  1492. */
  1493. var onShaderCompileError = function (program, shaderMaterial, onError) {
  1494. return function (effect, error) {
  1495. shaderMaterial.dispose(true);
  1496. onError("Cannot compile program named " + program.name + ". Error: " + error + ". Default material will be applied");
  1497. };
  1498. };
  1499. /**
  1500. * Shader compilation success
  1501. */
  1502. var onShaderCompileSuccess = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess) {
  1503. return function (_) {
  1504. prepareShaderMaterialUniforms(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms);
  1505. shaderMaterial.onBind = function (mesh) {
  1506. onBindShaderMaterial(mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess);
  1507. };
  1508. };
  1509. };
  1510. /**
  1511. * Returns the appropriate uniform if already handled by babylon
  1512. */
  1513. var parseShaderUniforms = function (tokenizer, technique, unTreatedUniforms) {
  1514. for (var unif in technique.uniforms) {
  1515. var uniform = technique.uniforms[unif];
  1516. var uniformParameter = technique.parameters[uniform];
  1517. if (tokenizer.currentIdentifier === unif) {
  1518. if (uniformParameter.semantic && !uniformParameter.source && !uniformParameter.node) {
  1519. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1520. if (transformIndex !== -1) {
  1521. delete unTreatedUniforms[unif];
  1522. return babylonTransforms[transformIndex];
  1523. }
  1524. }
  1525. }
  1526. }
  1527. return tokenizer.currentIdentifier;
  1528. };
  1529. /**
  1530. * All shaders loaded. Create materials one by one
  1531. */
  1532. var importMaterials = function (gltfRuntime) {
  1533. // Create materials
  1534. for (var mat in gltfRuntime.materials) {
  1535. GLTF1.GLTFLoaderExtension.LoadMaterialAsync(gltfRuntime, mat, function (material) { }, function () { });
  1536. }
  1537. };
  1538. /**
  1539. * Implementation of the base glTF spec
  1540. */
  1541. var GLTFLoaderBase = /** @class */ (function () {
  1542. function GLTFLoaderBase() {
  1543. }
  1544. GLTFLoaderBase.CreateRuntime = function (parsedData, scene, rootUrl) {
  1545. var gltfRuntime = {
  1546. extensions: {},
  1547. accessors: {},
  1548. buffers: {},
  1549. bufferViews: {},
  1550. meshes: {},
  1551. lights: {},
  1552. cameras: {},
  1553. nodes: {},
  1554. images: {},
  1555. textures: {},
  1556. shaders: {},
  1557. programs: {},
  1558. samplers: {},
  1559. techniques: {},
  1560. materials: {},
  1561. animations: {},
  1562. skins: {},
  1563. extensionsUsed: [],
  1564. scenes: {},
  1565. buffersCount: 0,
  1566. shaderscount: 0,
  1567. scene: scene,
  1568. rootUrl: rootUrl,
  1569. loadedBufferCount: 0,
  1570. loadedBufferViews: {},
  1571. loadedShaderCount: 0,
  1572. importOnlyMeshes: false,
  1573. dummyNodes: []
  1574. };
  1575. // Parse
  1576. if (parsedData.extensions) {
  1577. parseObject(parsedData.extensions, "extensions", gltfRuntime);
  1578. }
  1579. if (parsedData.extensionsUsed) {
  1580. parseObject(parsedData.extensionsUsed, "extensionsUsed", gltfRuntime);
  1581. }
  1582. if (parsedData.buffers) {
  1583. parseBuffers(parsedData.buffers, gltfRuntime);
  1584. }
  1585. if (parsedData.bufferViews) {
  1586. parseObject(parsedData.bufferViews, "bufferViews", gltfRuntime);
  1587. }
  1588. if (parsedData.accessors) {
  1589. parseObject(parsedData.accessors, "accessors", gltfRuntime);
  1590. }
  1591. if (parsedData.meshes) {
  1592. parseObject(parsedData.meshes, "meshes", gltfRuntime);
  1593. }
  1594. if (parsedData.lights) {
  1595. parseObject(parsedData.lights, "lights", gltfRuntime);
  1596. }
  1597. if (parsedData.cameras) {
  1598. parseObject(parsedData.cameras, "cameras", gltfRuntime);
  1599. }
  1600. if (parsedData.nodes) {
  1601. parseObject(parsedData.nodes, "nodes", gltfRuntime);
  1602. }
  1603. if (parsedData.images) {
  1604. parseObject(parsedData.images, "images", gltfRuntime);
  1605. }
  1606. if (parsedData.textures) {
  1607. parseObject(parsedData.textures, "textures", gltfRuntime);
  1608. }
  1609. if (parsedData.shaders) {
  1610. parseShaders(parsedData.shaders, gltfRuntime);
  1611. }
  1612. if (parsedData.programs) {
  1613. parseObject(parsedData.programs, "programs", gltfRuntime);
  1614. }
  1615. if (parsedData.samplers) {
  1616. parseObject(parsedData.samplers, "samplers", gltfRuntime);
  1617. }
  1618. if (parsedData.techniques) {
  1619. parseObject(parsedData.techniques, "techniques", gltfRuntime);
  1620. }
  1621. if (parsedData.materials) {
  1622. parseObject(parsedData.materials, "materials", gltfRuntime);
  1623. }
  1624. if (parsedData.animations) {
  1625. parseObject(parsedData.animations, "animations", gltfRuntime);
  1626. }
  1627. if (parsedData.skins) {
  1628. parseObject(parsedData.skins, "skins", gltfRuntime);
  1629. }
  1630. if (parsedData.scenes) {
  1631. gltfRuntime.scenes = parsedData.scenes;
  1632. }
  1633. if (parsedData.scene && parsedData.scenes) {
  1634. gltfRuntime.currentScene = parsedData.scenes[parsedData.scene];
  1635. }
  1636. return gltfRuntime;
  1637. };
  1638. GLTFLoaderBase.LoadBufferAsync = function (gltfRuntime, id, onSuccess, onError, onProgress) {
  1639. var buffer = gltfRuntime.buffers[id];
  1640. if (BABYLON.Tools.IsBase64(buffer.uri)) {
  1641. setTimeout(function () { return onSuccess(new Uint8Array(BABYLON.Tools.DecodeBase64(buffer.uri))); });
  1642. }
  1643. else {
  1644. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + buffer.uri, function (data) { return onSuccess(new Uint8Array(data)); }, onProgress, undefined, true, function (request) {
  1645. if (request) {
  1646. onError(request.status + " " + request.statusText);
  1647. }
  1648. });
  1649. }
  1650. };
  1651. GLTFLoaderBase.LoadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  1652. var texture = gltfRuntime.textures[id];
  1653. if (!texture || !texture.source) {
  1654. onError("");
  1655. return;
  1656. }
  1657. if (texture.babylonTexture) {
  1658. onSuccess(null);
  1659. return;
  1660. }
  1661. var source = gltfRuntime.images[texture.source];
  1662. if (BABYLON.Tools.IsBase64(source.uri)) {
  1663. setTimeout(function () { return onSuccess(new Uint8Array(BABYLON.Tools.DecodeBase64(source.uri))); });
  1664. }
  1665. else {
  1666. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + source.uri, function (data) { return onSuccess(new Uint8Array(data)); }, undefined, undefined, true, function (request) {
  1667. if (request) {
  1668. onError(request.status + " " + request.statusText);
  1669. }
  1670. });
  1671. }
  1672. };
  1673. GLTFLoaderBase.CreateTextureAsync = function (gltfRuntime, id, buffer, onSuccess, onError) {
  1674. var texture = gltfRuntime.textures[id];
  1675. if (texture.babylonTexture) {
  1676. onSuccess(texture.babylonTexture);
  1677. return;
  1678. }
  1679. var sampler = gltfRuntime.samplers[texture.sampler];
  1680. var createMipMaps = (sampler.minFilter === GLTF1.ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  1681. (sampler.minFilter === GLTF1.ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  1682. (sampler.minFilter === GLTF1.ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  1683. (sampler.minFilter === GLTF1.ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  1684. var samplingMode = BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  1685. var blob = new Blob([buffer]);
  1686. var blobURL = URL.createObjectURL(blob);
  1687. var revokeBlobURL = function () { return URL.revokeObjectURL(blobURL); };
  1688. var newTexture = new BABYLON.Texture(blobURL, gltfRuntime.scene, !createMipMaps, true, samplingMode, revokeBlobURL, revokeBlobURL);
  1689. if (sampler.wrapS !== undefined) {
  1690. newTexture.wrapU = GLTF1.GLTFUtils.GetWrapMode(sampler.wrapS);
  1691. }
  1692. if (sampler.wrapT !== undefined) {
  1693. newTexture.wrapV = GLTF1.GLTFUtils.GetWrapMode(sampler.wrapT);
  1694. }
  1695. newTexture.name = id;
  1696. texture.babylonTexture = newTexture;
  1697. onSuccess(newTexture);
  1698. };
  1699. GLTFLoaderBase.LoadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  1700. var shader = gltfRuntime.shaders[id];
  1701. if (BABYLON.Tools.IsBase64(shader.uri)) {
  1702. var shaderString = atob(shader.uri.split(",")[1]);
  1703. if (onSuccess) {
  1704. onSuccess(shaderString);
  1705. }
  1706. }
  1707. else {
  1708. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + shader.uri, onSuccess, undefined, undefined, false, function (request) {
  1709. if (request && onError) {
  1710. onError(request.status + " " + request.statusText);
  1711. }
  1712. });
  1713. }
  1714. };
  1715. GLTFLoaderBase.LoadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  1716. var material = gltfRuntime.materials[id];
  1717. if (!material.technique) {
  1718. if (onError) {
  1719. onError("No technique found.");
  1720. }
  1721. return;
  1722. }
  1723. var technique = gltfRuntime.techniques[material.technique];
  1724. if (!technique) {
  1725. var defaultMaterial = new BABYLON.StandardMaterial(id, gltfRuntime.scene);
  1726. defaultMaterial.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  1727. defaultMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1728. onSuccess(defaultMaterial);
  1729. return;
  1730. }
  1731. var program = gltfRuntime.programs[technique.program];
  1732. var states = technique.states;
  1733. var vertexShader = BABYLON.Effect.ShadersStore[program.vertexShader + "VertexShader"];
  1734. var pixelShader = BABYLON.Effect.ShadersStore[program.fragmentShader + "PixelShader"];
  1735. var newVertexShader = "";
  1736. var newPixelShader = "";
  1737. var vertexTokenizer = new Tokenizer(vertexShader);
  1738. var pixelTokenizer = new Tokenizer(pixelShader);
  1739. var unTreatedUniforms = {};
  1740. var uniforms = [];
  1741. var attributes = [];
  1742. var samplers = [];
  1743. // Fill uniform, sampler2D and attributes
  1744. for (var unif in technique.uniforms) {
  1745. var uniform = technique.uniforms[unif];
  1746. var uniformParameter = technique.parameters[uniform];
  1747. unTreatedUniforms[unif] = uniformParameter;
  1748. if (uniformParameter.semantic && !uniformParameter.node && !uniformParameter.source) {
  1749. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1750. if (transformIndex !== -1) {
  1751. uniforms.push(babylonTransforms[transformIndex]);
  1752. delete unTreatedUniforms[unif];
  1753. }
  1754. else {
  1755. uniforms.push(unif);
  1756. }
  1757. }
  1758. else if (uniformParameter.type === GLTF1.EParameterType.SAMPLER_2D) {
  1759. samplers.push(unif);
  1760. }
  1761. else {
  1762. uniforms.push(unif);
  1763. }
  1764. }
  1765. for (var attr in technique.attributes) {
  1766. var attribute = technique.attributes[attr];
  1767. var attributeParameter = technique.parameters[attribute];
  1768. if (attributeParameter.semantic) {
  1769. attributes.push(getAttribute(attributeParameter));
  1770. }
  1771. }
  1772. // Configure vertex shader
  1773. while (!vertexTokenizer.isEnd() && vertexTokenizer.getNextToken()) {
  1774. var tokenType = vertexTokenizer.currentToken;
  1775. if (tokenType !== ETokenType.IDENTIFIER) {
  1776. newVertexShader += vertexTokenizer.currentString;
  1777. continue;
  1778. }
  1779. var foundAttribute = false;
  1780. for (var attr in technique.attributes) {
  1781. var attribute = technique.attributes[attr];
  1782. var attributeParameter = technique.parameters[attribute];
  1783. if (vertexTokenizer.currentIdentifier === attr && attributeParameter.semantic) {
  1784. newVertexShader += getAttribute(attributeParameter);
  1785. foundAttribute = true;
  1786. break;
  1787. }
  1788. }
  1789. if (foundAttribute) {
  1790. continue;
  1791. }
  1792. newVertexShader += parseShaderUniforms(vertexTokenizer, technique, unTreatedUniforms);
  1793. }
  1794. // Configure pixel shader
  1795. while (!pixelTokenizer.isEnd() && pixelTokenizer.getNextToken()) {
  1796. var tokenType = pixelTokenizer.currentToken;
  1797. if (tokenType !== ETokenType.IDENTIFIER) {
  1798. newPixelShader += pixelTokenizer.currentString;
  1799. continue;
  1800. }
  1801. newPixelShader += parseShaderUniforms(pixelTokenizer, technique, unTreatedUniforms);
  1802. }
  1803. // Create shader material
  1804. var shaderPath = {
  1805. vertex: program.vertexShader + id,
  1806. fragment: program.fragmentShader + id
  1807. };
  1808. var options = {
  1809. attributes: attributes,
  1810. uniforms: uniforms,
  1811. samplers: samplers,
  1812. needAlphaBlending: states && states.enable && states.enable.indexOf(3042) !== -1
  1813. };
  1814. BABYLON.Effect.ShadersStore[program.vertexShader + id + "VertexShader"] = newVertexShader;
  1815. BABYLON.Effect.ShadersStore[program.fragmentShader + id + "PixelShader"] = newPixelShader;
  1816. var shaderMaterial = new BABYLON.ShaderMaterial(id, gltfRuntime.scene, shaderPath, options);
  1817. shaderMaterial.onError = onShaderCompileError(program, shaderMaterial, onError);
  1818. shaderMaterial.onCompiled = onShaderCompileSuccess(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess);
  1819. shaderMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1820. if (states && states.functions) {
  1821. var functions = states.functions;
  1822. if (functions.cullFace && functions.cullFace[0] !== GLTF1.ECullingType.BACK) {
  1823. shaderMaterial.backFaceCulling = false;
  1824. }
  1825. var blendFunc = functions.blendFuncSeparate;
  1826. if (blendFunc) {
  1827. 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) {
  1828. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_COMBINE;
  1829. }
  1830. else if (blendFunc[0] === GLTF1.EBlendingFunction.ONE && blendFunc[1] === GLTF1.EBlendingFunction.ONE && blendFunc[2] === GLTF1.EBlendingFunction.ZERO && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  1831. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ONEONE;
  1832. }
  1833. else if (blendFunc[0] === GLTF1.EBlendingFunction.SRC_ALPHA && blendFunc[1] === GLTF1.EBlendingFunction.ONE && blendFunc[2] === GLTF1.EBlendingFunction.ZERO && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  1834. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ADD;
  1835. }
  1836. 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) {
  1837. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_SUBTRACT;
  1838. }
  1839. else if (blendFunc[0] === GLTF1.EBlendingFunction.DST_COLOR && blendFunc[1] === GLTF1.EBlendingFunction.ZERO && blendFunc[2] === GLTF1.EBlendingFunction.ONE && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  1840. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MULTIPLY;
  1841. }
  1842. 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) {
  1843. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MAXIMIZED;
  1844. }
  1845. }
  1846. }
  1847. };
  1848. return GLTFLoaderBase;
  1849. }());
  1850. GLTF1.GLTFLoaderBase = GLTFLoaderBase;
  1851. /**
  1852. * glTF V1 Loader
  1853. */
  1854. var GLTFLoader = /** @class */ (function () {
  1855. function GLTFLoader() {
  1856. // #region Stubs for IGLTFLoader interface
  1857. this.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.AUTO;
  1858. this.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.FIRST;
  1859. this.compileMaterials = false;
  1860. this.useClipPlane = false;
  1861. this.compileShadowGenerators = false;
  1862. this.onDisposeObservable = new BABYLON.Observable();
  1863. this.onMeshLoadedObservable = new BABYLON.Observable();
  1864. this.onTextureLoadedObservable = new BABYLON.Observable();
  1865. this.onMaterialLoadedObservable = new BABYLON.Observable();
  1866. this.onCompleteObservable = new BABYLON.Observable();
  1867. this.onExtensionLoadedObservable = new BABYLON.Observable();
  1868. this.state = null;
  1869. }
  1870. GLTFLoader.RegisterExtension = function (extension) {
  1871. if (GLTFLoader.Extensions[extension.name]) {
  1872. BABYLON.Tools.Error("Tool with the same name \"" + extension.name + "\" already exists");
  1873. return;
  1874. }
  1875. GLTFLoader.Extensions[extension.name] = extension;
  1876. };
  1877. GLTFLoader.prototype.dispose = function () { };
  1878. // #endregion
  1879. GLTFLoader.prototype._importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  1880. var _this = this;
  1881. scene.useRightHandedSystem = true;
  1882. GLTF1.GLTFLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  1883. gltfRuntime.importOnlyMeshes = true;
  1884. if (meshesNames === "") {
  1885. gltfRuntime.importMeshesNames = [];
  1886. }
  1887. else if (typeof meshesNames === "string") {
  1888. gltfRuntime.importMeshesNames = [meshesNames];
  1889. }
  1890. else if (meshesNames && !(meshesNames instanceof Array)) {
  1891. gltfRuntime.importMeshesNames = [meshesNames];
  1892. }
  1893. else {
  1894. gltfRuntime.importMeshesNames = [];
  1895. BABYLON.Tools.Warn("Argument meshesNames must be of type string or string[]");
  1896. }
  1897. // Create nodes
  1898. _this._createNodes(gltfRuntime);
  1899. var meshes = new Array();
  1900. var skeletons = new Array();
  1901. // Fill arrays of meshes and skeletons
  1902. for (var nde in gltfRuntime.nodes) {
  1903. var node = gltfRuntime.nodes[nde];
  1904. if (node.babylonNode instanceof BABYLON.AbstractMesh) {
  1905. meshes.push(node.babylonNode);
  1906. }
  1907. }
  1908. for (var skl in gltfRuntime.skins) {
  1909. var skin = gltfRuntime.skins[skl];
  1910. if (skin.babylonSkeleton instanceof BABYLON.Skeleton) {
  1911. skeletons.push(skin.babylonSkeleton);
  1912. }
  1913. }
  1914. // Load buffers, shaders, materials, etc.
  1915. _this._loadBuffersAsync(gltfRuntime, function () {
  1916. _this._loadShadersAsync(gltfRuntime, function () {
  1917. importMaterials(gltfRuntime);
  1918. postLoad(gltfRuntime);
  1919. if (!BABYLON.GLTFFileLoader.IncrementalLoading && onSuccess) {
  1920. onSuccess(meshes, skeletons);
  1921. }
  1922. });
  1923. }, onProgress);
  1924. if (BABYLON.GLTFFileLoader.IncrementalLoading && onSuccess) {
  1925. onSuccess(meshes, skeletons);
  1926. }
  1927. }, onError);
  1928. return true;
  1929. };
  1930. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  1931. var _this = this;
  1932. return new Promise(function (resolve, reject) {
  1933. _this._importMeshAsync(meshesNames, scene, data, rootUrl, function (meshes, skeletons) {
  1934. resolve({
  1935. meshes: meshes,
  1936. particleSystems: [],
  1937. skeletons: skeletons,
  1938. animationGroups: []
  1939. });
  1940. }, onProgress, function (message) {
  1941. reject(new Error(message));
  1942. });
  1943. });
  1944. };
  1945. GLTFLoader.prototype._loadAsync = function (scene, data, rootUrl, onSuccess, onProgress, onError) {
  1946. var _this = this;
  1947. scene.useRightHandedSystem = true;
  1948. GLTF1.GLTFLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  1949. // Load runtime extensios
  1950. GLTF1.GLTFLoaderExtension.LoadRuntimeExtensionsAsync(gltfRuntime, function () {
  1951. // Create nodes
  1952. _this._createNodes(gltfRuntime);
  1953. // Load buffers, shaders, materials, etc.
  1954. _this._loadBuffersAsync(gltfRuntime, function () {
  1955. _this._loadShadersAsync(gltfRuntime, function () {
  1956. importMaterials(gltfRuntime);
  1957. postLoad(gltfRuntime);
  1958. if (!BABYLON.GLTFFileLoader.IncrementalLoading) {
  1959. onSuccess();
  1960. }
  1961. });
  1962. });
  1963. if (BABYLON.GLTFFileLoader.IncrementalLoading) {
  1964. onSuccess();
  1965. }
  1966. }, onError);
  1967. }, onError);
  1968. };
  1969. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  1970. var _this = this;
  1971. return new Promise(function (resolve, reject) {
  1972. _this._loadAsync(scene, data, rootUrl, function () {
  1973. resolve();
  1974. }, onProgress, function (message) {
  1975. reject(new Error(message));
  1976. });
  1977. });
  1978. };
  1979. GLTFLoader.prototype._loadShadersAsync = function (gltfRuntime, onload) {
  1980. var hasShaders = false;
  1981. var processShader = function (sha, shader) {
  1982. GLTF1.GLTFLoaderExtension.LoadShaderStringAsync(gltfRuntime, sha, function (shaderString) {
  1983. if (shaderString instanceof ArrayBuffer) {
  1984. return;
  1985. }
  1986. gltfRuntime.loadedShaderCount++;
  1987. if (shaderString) {
  1988. BABYLON.Effect.ShadersStore[sha + (shader.type === GLTF1.EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = shaderString;
  1989. }
  1990. if (gltfRuntime.loadedShaderCount === gltfRuntime.shaderscount) {
  1991. onload();
  1992. }
  1993. }, function () {
  1994. BABYLON.Tools.Error("Error when loading shader program named " + sha + " located at " + shader.uri);
  1995. });
  1996. };
  1997. for (var sha in gltfRuntime.shaders) {
  1998. hasShaders = true;
  1999. var shader = gltfRuntime.shaders[sha];
  2000. if (shader) {
  2001. processShader.bind(this, sha, shader)();
  2002. }
  2003. else {
  2004. BABYLON.Tools.Error("No shader named: " + sha);
  2005. }
  2006. }
  2007. if (!hasShaders) {
  2008. onload();
  2009. }
  2010. };
  2011. ;
  2012. GLTFLoader.prototype._loadBuffersAsync = function (gltfRuntime, onLoad, onProgress) {
  2013. var hasBuffers = false;
  2014. var processBuffer = function (buf, buffer) {
  2015. GLTF1.GLTFLoaderExtension.LoadBufferAsync(gltfRuntime, buf, function (bufferView) {
  2016. gltfRuntime.loadedBufferCount++;
  2017. if (bufferView) {
  2018. if (bufferView.byteLength != gltfRuntime.buffers[buf].byteLength) {
  2019. BABYLON.Tools.Error("Buffer named " + buf + " is length " + bufferView.byteLength + ". Expected: " + buffer.byteLength); // Improve error message
  2020. }
  2021. gltfRuntime.loadedBufferViews[buf] = bufferView;
  2022. }
  2023. if (gltfRuntime.loadedBufferCount === gltfRuntime.buffersCount) {
  2024. onLoad();
  2025. }
  2026. }, function () {
  2027. BABYLON.Tools.Error("Error when loading buffer named " + buf + " located at " + buffer.uri);
  2028. });
  2029. };
  2030. for (var buf in gltfRuntime.buffers) {
  2031. hasBuffers = true;
  2032. var buffer = gltfRuntime.buffers[buf];
  2033. if (buffer) {
  2034. processBuffer.bind(this, buf, buffer)();
  2035. }
  2036. else {
  2037. BABYLON.Tools.Error("No buffer named: " + buf);
  2038. }
  2039. }
  2040. if (!hasBuffers) {
  2041. onLoad();
  2042. }
  2043. };
  2044. GLTFLoader.prototype._createNodes = function (gltfRuntime) {
  2045. var currentScene = gltfRuntime.currentScene;
  2046. if (currentScene) {
  2047. // Only one scene even if multiple scenes are defined
  2048. for (var i = 0; i < currentScene.nodes.length; i++) {
  2049. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  2050. }
  2051. }
  2052. else {
  2053. // Load all scenes
  2054. for (var thing in gltfRuntime.scenes) {
  2055. currentScene = gltfRuntime.scenes[thing];
  2056. for (var i = 0; i < currentScene.nodes.length; i++) {
  2057. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  2058. }
  2059. }
  2060. }
  2061. };
  2062. GLTFLoader.Extensions = {};
  2063. return GLTFLoader;
  2064. }());
  2065. GLTF1.GLTFLoader = GLTFLoader;
  2066. ;
  2067. BABYLON.GLTFFileLoader.CreateGLTFLoaderV1 = function () { return new GLTFLoader(); };
  2068. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  2069. })(BABYLON || (BABYLON = {}));
  2070. //# sourceMappingURL=babylon.glTFLoader.js.map
  2071. "use strict";
  2072. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2073. var BABYLON;
  2074. (function (BABYLON) {
  2075. var GLTF1;
  2076. (function (GLTF1) {
  2077. /**
  2078. * Utils functions for GLTF
  2079. */
  2080. var GLTFUtils = /** @class */ (function () {
  2081. function GLTFUtils() {
  2082. }
  2083. /**
  2084. * Sets the given "parameter" matrix
  2085. * @param scene: the {BABYLON.Scene} object
  2086. * @param source: the source node where to pick the matrix
  2087. * @param parameter: the GLTF technique parameter
  2088. * @param uniformName: the name of the shader's uniform
  2089. * @param shaderMaterial: the shader material
  2090. */
  2091. GLTFUtils.SetMatrix = function (scene, source, parameter, uniformName, shaderMaterial) {
  2092. var mat = null;
  2093. if (parameter.semantic === "MODEL") {
  2094. mat = source.getWorldMatrix();
  2095. }
  2096. else if (parameter.semantic === "PROJECTION") {
  2097. mat = scene.getProjectionMatrix();
  2098. }
  2099. else if (parameter.semantic === "VIEW") {
  2100. mat = scene.getViewMatrix();
  2101. }
  2102. else if (parameter.semantic === "MODELVIEWINVERSETRANSPOSE") {
  2103. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().multiply(scene.getViewMatrix()).invert());
  2104. }
  2105. else if (parameter.semantic === "MODELVIEW") {
  2106. mat = source.getWorldMatrix().multiply(scene.getViewMatrix());
  2107. }
  2108. else if (parameter.semantic === "MODELVIEWPROJECTION") {
  2109. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix());
  2110. }
  2111. else if (parameter.semantic === "MODELINVERSE") {
  2112. mat = source.getWorldMatrix().invert();
  2113. }
  2114. else if (parameter.semantic === "VIEWINVERSE") {
  2115. mat = scene.getViewMatrix().invert();
  2116. }
  2117. else if (parameter.semantic === "PROJECTIONINVERSE") {
  2118. mat = scene.getProjectionMatrix().invert();
  2119. }
  2120. else if (parameter.semantic === "MODELVIEWINVERSE") {
  2121. mat = source.getWorldMatrix().multiply(scene.getViewMatrix()).invert();
  2122. }
  2123. else if (parameter.semantic === "MODELVIEWPROJECTIONINVERSE") {
  2124. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix()).invert();
  2125. }
  2126. else if (parameter.semantic === "MODELINVERSETRANSPOSE") {
  2127. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().invert());
  2128. }
  2129. else {
  2130. debugger;
  2131. }
  2132. if (mat) {
  2133. switch (parameter.type) {
  2134. case GLTF1.EParameterType.FLOAT_MAT2:
  2135. shaderMaterial.setMatrix2x2(uniformName, BABYLON.Matrix.GetAsMatrix2x2(mat));
  2136. break;
  2137. case GLTF1.EParameterType.FLOAT_MAT3:
  2138. shaderMaterial.setMatrix3x3(uniformName, BABYLON.Matrix.GetAsMatrix3x3(mat));
  2139. break;
  2140. case GLTF1.EParameterType.FLOAT_MAT4:
  2141. shaderMaterial.setMatrix(uniformName, mat);
  2142. break;
  2143. default: break;
  2144. }
  2145. }
  2146. };
  2147. /**
  2148. * Sets the given "parameter" matrix
  2149. * @param shaderMaterial: the shader material
  2150. * @param uniform: the name of the shader's uniform
  2151. * @param value: the value of the uniform
  2152. * @param type: the uniform's type (EParameterType FLOAT, VEC2, VEC3 or VEC4)
  2153. */
  2154. GLTFUtils.SetUniform = function (shaderMaterial, uniform, value, type) {
  2155. switch (type) {
  2156. case GLTF1.EParameterType.FLOAT:
  2157. shaderMaterial.setFloat(uniform, value);
  2158. return true;
  2159. case GLTF1.EParameterType.FLOAT_VEC2:
  2160. shaderMaterial.setVector2(uniform, BABYLON.Vector2.FromArray(value));
  2161. return true;
  2162. case GLTF1.EParameterType.FLOAT_VEC3:
  2163. shaderMaterial.setVector3(uniform, BABYLON.Vector3.FromArray(value));
  2164. return true;
  2165. case GLTF1.EParameterType.FLOAT_VEC4:
  2166. shaderMaterial.setVector4(uniform, BABYLON.Vector4.FromArray(value));
  2167. return true;
  2168. default: return false;
  2169. }
  2170. };
  2171. /**
  2172. * Returns the wrap mode of the texture
  2173. * @param mode: the mode value
  2174. */
  2175. GLTFUtils.GetWrapMode = function (mode) {
  2176. switch (mode) {
  2177. case GLTF1.ETextureWrapMode.CLAMP_TO_EDGE: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  2178. case GLTF1.ETextureWrapMode.MIRRORED_REPEAT: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  2179. case GLTF1.ETextureWrapMode.REPEAT: return BABYLON.Texture.WRAP_ADDRESSMODE;
  2180. default: return BABYLON.Texture.WRAP_ADDRESSMODE;
  2181. }
  2182. };
  2183. /**
  2184. * Returns the byte stride giving an accessor
  2185. * @param accessor: the GLTF accessor objet
  2186. */
  2187. GLTFUtils.GetByteStrideFromType = function (accessor) {
  2188. // Needs this function since "byteStride" isn't requiered in glTF format
  2189. var type = accessor.type;
  2190. switch (type) {
  2191. case "VEC2": return 2;
  2192. case "VEC3": return 3;
  2193. case "VEC4": return 4;
  2194. case "MAT2": return 4;
  2195. case "MAT3": return 9;
  2196. case "MAT4": return 16;
  2197. default: return 1;
  2198. }
  2199. };
  2200. /**
  2201. * Returns the texture filter mode giving a mode value
  2202. * @param mode: the filter mode value
  2203. */
  2204. GLTFUtils.GetTextureFilterMode = function (mode) {
  2205. switch (mode) {
  2206. case GLTF1.ETextureFilterType.LINEAR:
  2207. case GLTF1.ETextureFilterType.LINEAR_MIPMAP_NEAREST:
  2208. case GLTF1.ETextureFilterType.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  2209. case GLTF1.ETextureFilterType.NEAREST:
  2210. case GLTF1.ETextureFilterType.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_SAMPLINGMODE;
  2211. default: return BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  2212. }
  2213. };
  2214. GLTFUtils.GetBufferFromBufferView = function (gltfRuntime, bufferView, byteOffset, byteLength, componentType) {
  2215. var byteOffset = bufferView.byteOffset + byteOffset;
  2216. var loadedBufferView = gltfRuntime.loadedBufferViews[bufferView.buffer];
  2217. if (byteOffset + byteLength > loadedBufferView.byteLength) {
  2218. throw new Error("Buffer access is out of range");
  2219. }
  2220. var buffer = loadedBufferView.buffer;
  2221. byteOffset += loadedBufferView.byteOffset;
  2222. switch (componentType) {
  2223. case GLTF1.EComponentType.BYTE: return new Int8Array(buffer, byteOffset, byteLength);
  2224. case GLTF1.EComponentType.UNSIGNED_BYTE: return new Uint8Array(buffer, byteOffset, byteLength);
  2225. case GLTF1.EComponentType.SHORT: return new Int16Array(buffer, byteOffset, byteLength);
  2226. case GLTF1.EComponentType.UNSIGNED_SHORT: return new Uint16Array(buffer, byteOffset, byteLength);
  2227. default: return new Float32Array(buffer, byteOffset, byteLength);
  2228. }
  2229. };
  2230. /**
  2231. * Returns a buffer from its accessor
  2232. * @param gltfRuntime: the GLTF runtime
  2233. * @param accessor: the GLTF accessor
  2234. */
  2235. GLTFUtils.GetBufferFromAccessor = function (gltfRuntime, accessor) {
  2236. var bufferView = gltfRuntime.bufferViews[accessor.bufferView];
  2237. var byteLength = accessor.count * GLTFUtils.GetByteStrideFromType(accessor);
  2238. return GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, accessor.byteOffset, byteLength, accessor.componentType);
  2239. };
  2240. /**
  2241. * Decodes a buffer view into a string
  2242. * @param view: the buffer view
  2243. */
  2244. GLTFUtils.DecodeBufferToText = function (view) {
  2245. var result = "";
  2246. var length = view.byteLength;
  2247. for (var i = 0; i < length; ++i) {
  2248. result += String.fromCharCode(view[i]);
  2249. }
  2250. return result;
  2251. };
  2252. /**
  2253. * Returns the default material of gltf. Related to
  2254. * https://github.com/KhronosGroup/glTF/tree/master/specification/1.0#appendix-a-default-material
  2255. * @param scene: the Babylon.js scene
  2256. */
  2257. GLTFUtils.GetDefaultMaterial = function (scene) {
  2258. if (!GLTFUtils._DefaultMaterial) {
  2259. BABYLON.Effect.ShadersStore["GLTFDefaultMaterialVertexShader"] = [
  2260. "precision highp float;",
  2261. "",
  2262. "uniform mat4 worldView;",
  2263. "uniform mat4 projection;",
  2264. "",
  2265. "attribute vec3 position;",
  2266. "",
  2267. "void main(void)",
  2268. "{",
  2269. " gl_Position = projection * worldView * vec4(position, 1.0);",
  2270. "}"
  2271. ].join("\n");
  2272. BABYLON.Effect.ShadersStore["GLTFDefaultMaterialPixelShader"] = [
  2273. "precision highp float;",
  2274. "",
  2275. "uniform vec4 u_emission;",
  2276. "",
  2277. "void main(void)",
  2278. "{",
  2279. " gl_FragColor = u_emission;",
  2280. "}"
  2281. ].join("\n");
  2282. var shaderPath = {
  2283. vertex: "GLTFDefaultMaterial",
  2284. fragment: "GLTFDefaultMaterial"
  2285. };
  2286. var options = {
  2287. attributes: ["position"],
  2288. uniforms: ["worldView", "projection", "u_emission"],
  2289. samplers: new Array(),
  2290. needAlphaBlending: false
  2291. };
  2292. GLTFUtils._DefaultMaterial = new BABYLON.ShaderMaterial("GLTFDefaultMaterial", scene, shaderPath, options);
  2293. GLTFUtils._DefaultMaterial.setColor4("u_emission", new BABYLON.Color4(0.5, 0.5, 0.5, 1.0));
  2294. }
  2295. return GLTFUtils._DefaultMaterial;
  2296. };
  2297. // The GLTF default material
  2298. GLTFUtils._DefaultMaterial = null;
  2299. return GLTFUtils;
  2300. }());
  2301. GLTF1.GLTFUtils = GLTFUtils;
  2302. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  2303. })(BABYLON || (BABYLON = {}));
  2304. //# sourceMappingURL=babylon.glTFLoaderUtils.js.map
  2305. "use strict";
  2306. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2307. var BABYLON;
  2308. (function (BABYLON) {
  2309. var GLTF1;
  2310. (function (GLTF1) {
  2311. var GLTFLoaderExtension = /** @class */ (function () {
  2312. function GLTFLoaderExtension(name) {
  2313. this._name = name;
  2314. }
  2315. Object.defineProperty(GLTFLoaderExtension.prototype, "name", {
  2316. get: function () {
  2317. return this._name;
  2318. },
  2319. enumerable: true,
  2320. configurable: true
  2321. });
  2322. /**
  2323. * Defines an override for loading the runtime
  2324. * Return true to stop further extensions from loading the runtime
  2325. */
  2326. GLTFLoaderExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  2327. return false;
  2328. };
  2329. /**
  2330. * Defines an onverride for creating gltf runtime
  2331. * Return true to stop further extensions from creating the runtime
  2332. */
  2333. GLTFLoaderExtension.prototype.loadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
  2334. return false;
  2335. };
  2336. /**
  2337. * Defines an override for loading buffers
  2338. * Return true to stop further extensions from loading this buffer
  2339. */
  2340. GLTFLoaderExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError, onProgress) {
  2341. return false;
  2342. };
  2343. /**
  2344. * Defines an override for loading texture buffers
  2345. * Return true to stop further extensions from loading this texture data
  2346. */
  2347. GLTFLoaderExtension.prototype.loadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  2348. return false;
  2349. };
  2350. /**
  2351. * Defines an override for creating textures
  2352. * Return true to stop further extensions from loading this texture
  2353. */
  2354. GLTFLoaderExtension.prototype.createTextureAsync = function (gltfRuntime, id, buffer, onSuccess, onError) {
  2355. return false;
  2356. };
  2357. /**
  2358. * Defines an override for loading shader strings
  2359. * Return true to stop further extensions from loading this shader data
  2360. */
  2361. GLTFLoaderExtension.prototype.loadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  2362. return false;
  2363. };
  2364. /**
  2365. * Defines an override for loading materials
  2366. * Return true to stop further extensions from loading this material
  2367. */
  2368. GLTFLoaderExtension.prototype.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  2369. return false;
  2370. };
  2371. // ---------
  2372. // Utilities
  2373. // ---------
  2374. GLTFLoaderExtension.LoadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  2375. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2376. return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
  2377. }, function () {
  2378. setTimeout(function () {
  2379. if (!onSuccess) {
  2380. return;
  2381. }
  2382. onSuccess(GLTF1.GLTFLoaderBase.CreateRuntime(data.json, scene, rootUrl));
  2383. });
  2384. });
  2385. };
  2386. GLTFLoaderExtension.LoadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
  2387. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2388. return loaderExtension.loadRuntimeExtensionsAsync(gltfRuntime, onSuccess, onError);
  2389. }, function () {
  2390. setTimeout(function () {
  2391. onSuccess();
  2392. });
  2393. });
  2394. };
  2395. GLTFLoaderExtension.LoadBufferAsync = function (gltfRuntime, id, onSuccess, onError, onProgress) {
  2396. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2397. return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
  2398. }, function () {
  2399. GLTF1.GLTFLoaderBase.LoadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
  2400. });
  2401. };
  2402. GLTFLoaderExtension.LoadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  2403. GLTFLoaderExtension.LoadTextureBufferAsync(gltfRuntime, id, function (buffer) {
  2404. if (buffer) {
  2405. GLTFLoaderExtension.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  2406. }
  2407. }, onError);
  2408. };
  2409. GLTFLoaderExtension.LoadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  2410. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2411. return loaderExtension.loadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  2412. }, function () {
  2413. GLTF1.GLTFLoaderBase.LoadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  2414. });
  2415. };
  2416. GLTFLoaderExtension.LoadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  2417. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2418. return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  2419. }, function () {
  2420. GLTF1.GLTFLoaderBase.LoadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  2421. });
  2422. };
  2423. GLTFLoaderExtension.LoadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  2424. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2425. return loaderExtension.loadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  2426. }, function () {
  2427. GLTF1.GLTFLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  2428. });
  2429. };
  2430. GLTFLoaderExtension.CreateTextureAsync = function (gltfRuntime, id, buffer, onSuccess, onError) {
  2431. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2432. return loaderExtension.createTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  2433. }, function () {
  2434. GLTF1.GLTFLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  2435. });
  2436. };
  2437. GLTFLoaderExtension.ApplyExtensions = function (func, defaultFunc) {
  2438. for (var extensionName in GLTF1.GLTFLoader.Extensions) {
  2439. var loaderExtension = GLTF1.GLTFLoader.Extensions[extensionName];
  2440. if (func(loaderExtension)) {
  2441. return;
  2442. }
  2443. }
  2444. defaultFunc();
  2445. };
  2446. return GLTFLoaderExtension;
  2447. }());
  2448. GLTF1.GLTFLoaderExtension = GLTFLoaderExtension;
  2449. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  2450. })(BABYLON || (BABYLON = {}));
  2451. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  2452. "use strict";
  2453. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2454. var __extends = (this && this.__extends) || (function () {
  2455. var extendStatics = Object.setPrototypeOf ||
  2456. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2457. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2458. return function (d, b) {
  2459. extendStatics(d, b);
  2460. function __() { this.constructor = d; }
  2461. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2462. };
  2463. })();
  2464. var BABYLON;
  2465. (function (BABYLON) {
  2466. var GLTF1;
  2467. (function (GLTF1) {
  2468. var BinaryExtensionBufferName = "binary_glTF";
  2469. ;
  2470. ;
  2471. var GLTFBinaryExtension = /** @class */ (function (_super) {
  2472. __extends(GLTFBinaryExtension, _super);
  2473. function GLTFBinaryExtension() {
  2474. return _super.call(this, "KHR_binary_glTF") || this;
  2475. }
  2476. GLTFBinaryExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  2477. var extensionsUsed = data.json.extensionsUsed;
  2478. if (!extensionsUsed || extensionsUsed.indexOf(this.name) === -1 || !data.bin) {
  2479. return false;
  2480. }
  2481. this._bin = data.bin;
  2482. onSuccess(GLTF1.GLTFLoaderBase.CreateRuntime(data.json, scene, rootUrl));
  2483. return true;
  2484. };
  2485. GLTFBinaryExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  2486. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  2487. return false;
  2488. }
  2489. if (id !== BinaryExtensionBufferName) {
  2490. return false;
  2491. }
  2492. onSuccess(this._bin);
  2493. return true;
  2494. };
  2495. GLTFBinaryExtension.prototype.loadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  2496. var texture = gltfRuntime.textures[id];
  2497. var source = gltfRuntime.images[texture.source];
  2498. if (!source.extensions || !(this.name in source.extensions)) {
  2499. return false;
  2500. }
  2501. var sourceExt = source.extensions[this.name];
  2502. var bufferView = gltfRuntime.bufferViews[sourceExt.bufferView];
  2503. var buffer = GLTF1.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, GLTF1.EComponentType.UNSIGNED_BYTE);
  2504. onSuccess(buffer);
  2505. return true;
  2506. };
  2507. GLTFBinaryExtension.prototype.loadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  2508. var shader = gltfRuntime.shaders[id];
  2509. if (!shader.extensions || !(this.name in shader.extensions)) {
  2510. return false;
  2511. }
  2512. var binaryExtensionShader = shader.extensions[this.name];
  2513. var bufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
  2514. var shaderBytes = GLTF1.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, GLTF1.EComponentType.UNSIGNED_BYTE);
  2515. setTimeout(function () {
  2516. var shaderString = GLTF1.GLTFUtils.DecodeBufferToText(shaderBytes);
  2517. onSuccess(shaderString);
  2518. });
  2519. return true;
  2520. };
  2521. return GLTFBinaryExtension;
  2522. }(GLTF1.GLTFLoaderExtension));
  2523. GLTF1.GLTFBinaryExtension = GLTFBinaryExtension;
  2524. GLTF1.GLTFLoader.RegisterExtension(new GLTFBinaryExtension());
  2525. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  2526. })(BABYLON || (BABYLON = {}));
  2527. //# sourceMappingURL=babylon.glTFBinaryExtension.js.map
  2528. "use strict";
  2529. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2530. var __extends = (this && this.__extends) || (function () {
  2531. var extendStatics = Object.setPrototypeOf ||
  2532. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2533. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2534. return function (d, b) {
  2535. extendStatics(d, b);
  2536. function __() { this.constructor = d; }
  2537. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2538. };
  2539. })();
  2540. var BABYLON;
  2541. (function (BABYLON) {
  2542. var GLTF1;
  2543. (function (GLTF1) {
  2544. ;
  2545. ;
  2546. ;
  2547. var GLTFMaterialsCommonExtension = /** @class */ (function (_super) {
  2548. __extends(GLTFMaterialsCommonExtension, _super);
  2549. function GLTFMaterialsCommonExtension() {
  2550. return _super.call(this, "KHR_materials_common") || this;
  2551. }
  2552. GLTFMaterialsCommonExtension.prototype.loadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
  2553. if (!gltfRuntime.extensions)
  2554. return false;
  2555. var extension = gltfRuntime.extensions[this.name];
  2556. if (!extension)
  2557. return false;
  2558. // Create lights
  2559. var lights = extension.lights;
  2560. if (lights) {
  2561. for (var thing in lights) {
  2562. var light = lights[thing];
  2563. switch (light.type) {
  2564. case "ambient":
  2565. var ambientLight = new BABYLON.HemisphericLight(light.name, new BABYLON.Vector3(0, 1, 0), gltfRuntime.scene);
  2566. var ambient = light.ambient;
  2567. if (ambient) {
  2568. ambientLight.diffuse = BABYLON.Color3.FromArray(ambient.color || [1, 1, 1]);
  2569. }
  2570. break;
  2571. case "point":
  2572. var pointLight = new BABYLON.PointLight(light.name, new BABYLON.Vector3(10, 10, 10), gltfRuntime.scene);
  2573. var point = light.point;
  2574. if (point) {
  2575. pointLight.diffuse = BABYLON.Color3.FromArray(point.color || [1, 1, 1]);
  2576. }
  2577. break;
  2578. case "directional":
  2579. var dirLight = new BABYLON.DirectionalLight(light.name, new BABYLON.Vector3(0, -1, 0), gltfRuntime.scene);
  2580. var directional = light.directional;
  2581. if (directional) {
  2582. dirLight.diffuse = BABYLON.Color3.FromArray(directional.color || [1, 1, 1]);
  2583. }
  2584. break;
  2585. case "spot":
  2586. var spot = light.spot;
  2587. if (spot) {
  2588. 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);
  2589. spotLight.diffuse = BABYLON.Color3.FromArray(spot.color || [1, 1, 1]);
  2590. }
  2591. break;
  2592. default:
  2593. BABYLON.Tools.Warn("GLTF Material Common extension: light type \"" + light.type + "\” not supported");
  2594. break;
  2595. }
  2596. }
  2597. }
  2598. return false;
  2599. };
  2600. GLTFMaterialsCommonExtension.prototype.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  2601. var material = gltfRuntime.materials[id];
  2602. if (!material || !material.extensions)
  2603. return false;
  2604. var extension = material.extensions[this.name];
  2605. if (!extension)
  2606. return false;
  2607. var standardMaterial = new BABYLON.StandardMaterial(id, gltfRuntime.scene);
  2608. standardMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  2609. if (extension.technique === "CONSTANT") {
  2610. standardMaterial.disableLighting = true;
  2611. }
  2612. standardMaterial.backFaceCulling = extension.doubleSided === undefined ? false : !extension.doubleSided;
  2613. standardMaterial.alpha = extension.values.transparency === undefined ? 1.0 : extension.values.transparency;
  2614. standardMaterial.specularPower = extension.values.shininess === undefined ? 0.0 : extension.values.shininess;
  2615. // Ambient
  2616. if (typeof extension.values.ambient === "string") {
  2617. this._loadTexture(gltfRuntime, extension.values.ambient, standardMaterial, "ambientTexture", onError);
  2618. }
  2619. else {
  2620. standardMaterial.ambientColor = BABYLON.Color3.FromArray(extension.values.ambient || [0, 0, 0]);
  2621. }
  2622. // Diffuse
  2623. if (typeof extension.values.diffuse === "string") {
  2624. this._loadTexture(gltfRuntime, extension.values.diffuse, standardMaterial, "diffuseTexture", onError);
  2625. }
  2626. else {
  2627. standardMaterial.diffuseColor = BABYLON.Color3.FromArray(extension.values.diffuse || [0, 0, 0]);
  2628. }
  2629. // Emission
  2630. if (typeof extension.values.emission === "string") {
  2631. this._loadTexture(gltfRuntime, extension.values.emission, standardMaterial, "emissiveTexture", onError);
  2632. }
  2633. else {
  2634. standardMaterial.emissiveColor = BABYLON.Color3.FromArray(extension.values.emission || [0, 0, 0]);
  2635. }
  2636. // Specular
  2637. if (typeof extension.values.specular === "string") {
  2638. this._loadTexture(gltfRuntime, extension.values.specular, standardMaterial, "specularTexture", onError);
  2639. }
  2640. else {
  2641. standardMaterial.specularColor = BABYLON.Color3.FromArray(extension.values.specular || [0, 0, 0]);
  2642. }
  2643. return true;
  2644. };
  2645. GLTFMaterialsCommonExtension.prototype._loadTexture = function (gltfRuntime, id, material, propertyPath, onError) {
  2646. // Create buffer from texture url
  2647. GLTF1.GLTFLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, function (buffer) {
  2648. // Create texture from buffer
  2649. GLTF1.GLTFLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, function (texture) { return material[propertyPath] = texture; }, onError);
  2650. }, onError);
  2651. };
  2652. return GLTFMaterialsCommonExtension;
  2653. }(GLTF1.GLTFLoaderExtension));
  2654. GLTF1.GLTFMaterialsCommonExtension = GLTFMaterialsCommonExtension;
  2655. GLTF1.GLTFLoader.RegisterExtension(new GLTFMaterialsCommonExtension());
  2656. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  2657. })(BABYLON || (BABYLON = {}));
  2658. //# sourceMappingURL=babylon.glTFMaterialsCommonExtension.js.map
  2659. "use strict";
  2660. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2661. var BABYLON;
  2662. (function (BABYLON) {
  2663. var GLTF2;
  2664. (function (GLTF2) {
  2665. var ArrayItem = /** @class */ (function () {
  2666. function ArrayItem() {
  2667. }
  2668. ArrayItem.Assign = function (values) {
  2669. if (values) {
  2670. for (var index = 0; index < values.length; index++) {
  2671. values[index]._index = index;
  2672. }
  2673. }
  2674. };
  2675. return ArrayItem;
  2676. }());
  2677. GLTF2.ArrayItem = ArrayItem;
  2678. var AnimationMultiTarget = /** @class */ (function () {
  2679. function AnimationMultiTarget() {
  2680. this.subTargets = new Array();
  2681. }
  2682. Object.defineProperty(AnimationMultiTarget.prototype, "position", {
  2683. set: function (value) {
  2684. for (var _i = 0, _a = this.subTargets; _i < _a.length; _i++) {
  2685. var subTarget = _a[_i];
  2686. subTarget.position = value;
  2687. }
  2688. },
  2689. enumerable: true,
  2690. configurable: true
  2691. });
  2692. Object.defineProperty(AnimationMultiTarget.prototype, "rotationQuaternion", {
  2693. set: function (value) {
  2694. for (var _i = 0, _a = this.subTargets; _i < _a.length; _i++) {
  2695. var subTarget = _a[_i];
  2696. subTarget.rotationQuaternion = value;
  2697. }
  2698. },
  2699. enumerable: true,
  2700. configurable: true
  2701. });
  2702. Object.defineProperty(AnimationMultiTarget.prototype, "scaling", {
  2703. set: function (value) {
  2704. for (var _i = 0, _a = this.subTargets; _i < _a.length; _i++) {
  2705. var subTarget = _a[_i];
  2706. subTarget.scaling = value;
  2707. }
  2708. },
  2709. enumerable: true,
  2710. configurable: true
  2711. });
  2712. Object.defineProperty(AnimationMultiTarget.prototype, "influence", {
  2713. set: function (value) {
  2714. for (var _i = 0, _a = this.subTargets; _i < _a.length; _i++) {
  2715. var subTarget = _a[_i];
  2716. subTarget.influence = value;
  2717. }
  2718. },
  2719. enumerable: true,
  2720. configurable: true
  2721. });
  2722. return AnimationMultiTarget;
  2723. }());
  2724. GLTF2.AnimationMultiTarget = AnimationMultiTarget;
  2725. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2726. })(BABYLON || (BABYLON = {}));
  2727. //# sourceMappingURL=babylon.glTFLoaderUtilities.js.map
  2728. "use strict";
  2729. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2730. /// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
  2731. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  2732. "use strict";
  2733. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2734. var BABYLON;
  2735. (function (BABYLON) {
  2736. var GLTF2;
  2737. (function (GLTF2) {
  2738. var GLTFLoader = /** @class */ (function () {
  2739. function GLTFLoader() {
  2740. this._completePromises = new Array();
  2741. this._disposed = false;
  2742. this._state = null;
  2743. this._extensions = {};
  2744. this._defaultSampler = {};
  2745. this._defaultBabylonMaterials = {};
  2746. this._requests = new Array();
  2747. this.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.AUTO;
  2748. this.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.FIRST;
  2749. this.compileMaterials = false;
  2750. this.useClipPlane = false;
  2751. this.compileShadowGenerators = false;
  2752. this.onDisposeObservable = new BABYLON.Observable();
  2753. this.onMeshLoadedObservable = new BABYLON.Observable();
  2754. this.onTextureLoadedObservable = new BABYLON.Observable();
  2755. this.onMaterialLoadedObservable = new BABYLON.Observable();
  2756. this.onExtensionLoadedObservable = new BABYLON.Observable();
  2757. this.onCompleteObservable = new BABYLON.Observable();
  2758. }
  2759. GLTFLoader._Register = function (name, factory) {
  2760. if (GLTFLoader._Factories[name]) {
  2761. BABYLON.Tools.Error("Extension with the name '" + name + "' already exists");
  2762. return;
  2763. }
  2764. GLTFLoader._Factories[name] = factory;
  2765. // Keep the order of registration so that extensions registered first are called first.
  2766. GLTFLoader._Names.push(name);
  2767. };
  2768. Object.defineProperty(GLTFLoader.prototype, "state", {
  2769. get: function () {
  2770. return this._state;
  2771. },
  2772. enumerable: true,
  2773. configurable: true
  2774. });
  2775. GLTFLoader.prototype.dispose = function () {
  2776. if (this._disposed) {
  2777. return;
  2778. }
  2779. this._disposed = true;
  2780. this.onDisposeObservable.notifyObservers(this);
  2781. this.onDisposeObservable.clear();
  2782. this._clear();
  2783. };
  2784. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  2785. var _this = this;
  2786. return Promise.resolve().then(function () {
  2787. var nodes = null;
  2788. if (meshesNames) {
  2789. var nodeMap_1 = {};
  2790. if (_this._gltf.nodes) {
  2791. for (var _i = 0, _a = _this._gltf.nodes; _i < _a.length; _i++) {
  2792. var node = _a[_i];
  2793. if (node.name) {
  2794. nodeMap_1[node.name] = node;
  2795. }
  2796. }
  2797. }
  2798. var names = (meshesNames instanceof Array) ? meshesNames : [meshesNames];
  2799. nodes = names.map(function (name) {
  2800. var node = nodeMap_1[name];
  2801. if (!node) {
  2802. throw new Error("Failed to find node '" + name + "'");
  2803. }
  2804. return node;
  2805. });
  2806. }
  2807. return _this._loadAsync(nodes, scene, data, rootUrl, onProgress).then(function () {
  2808. return {
  2809. meshes: _this._getMeshes(),
  2810. particleSystems: [],
  2811. skeletons: _this._getSkeletons(),
  2812. animationGroups: _this._getAnimationGroups()
  2813. };
  2814. });
  2815. });
  2816. };
  2817. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  2818. return this._loadAsync(null, scene, data, rootUrl, onProgress);
  2819. };
  2820. GLTFLoader.prototype._loadAsync = function (nodes, scene, data, rootUrl, onProgress) {
  2821. var _this = this;
  2822. return Promise.resolve().then(function () {
  2823. _this._loadExtensions();
  2824. _this._babylonScene = scene;
  2825. _this._rootUrl = rootUrl;
  2826. _this._progressCallback = onProgress;
  2827. _this._state = BABYLON.GLTFLoaderState.LOADING;
  2828. _this._loadData(data);
  2829. _this._checkExtensions();
  2830. var promises = new Array();
  2831. if (nodes) {
  2832. promises.push(_this._loadNodesAsync(nodes));
  2833. }
  2834. else {
  2835. var scene_1 = GLTFLoader._GetProperty("#/scene", _this._gltf.scenes, _this._gltf.scene || 0);
  2836. promises.push(_this._loadSceneAsync("#/scenes/" + scene_1._index, scene_1));
  2837. }
  2838. if (_this.compileMaterials) {
  2839. promises.push(_this._compileMaterialsAsync());
  2840. }
  2841. if (_this.compileShadowGenerators) {
  2842. promises.push(_this._compileShadowGeneratorsAsync());
  2843. }
  2844. var resultPromise = Promise.all(promises).then(function () {
  2845. _this._state = BABYLON.GLTFLoaderState.READY;
  2846. _this._startAnimations();
  2847. });
  2848. resultPromise.then(function () {
  2849. _this._rootBabylonMesh.setEnabled(true);
  2850. BABYLON.Tools.SetImmediate(function () {
  2851. if (!_this._disposed) {
  2852. Promise.all(_this._completePromises).then(function () {
  2853. _this._state = BABYLON.GLTFLoaderState.COMPLETE;
  2854. _this.onCompleteObservable.notifyObservers(_this);
  2855. _this.onCompleteObservable.clear();
  2856. _this._clear();
  2857. }).catch(function (error) {
  2858. BABYLON.Tools.Error("glTF Loader: " + error.message);
  2859. _this._clear();
  2860. });
  2861. }
  2862. });
  2863. });
  2864. return resultPromise;
  2865. }).catch(function (error) {
  2866. BABYLON.Tools.Error("glTF Loader: " + error.message);
  2867. _this._clear();
  2868. throw error;
  2869. });
  2870. };
  2871. GLTFLoader.prototype._loadExtensions = function () {
  2872. for (var _i = 0, _a = GLTFLoader._Names; _i < _a.length; _i++) {
  2873. var name_1 = _a[_i];
  2874. var extension = GLTFLoader._Factories[name_1](this);
  2875. this._extensions[name_1] = extension;
  2876. this.onExtensionLoadedObservable.notifyObservers(extension);
  2877. }
  2878. this.onExtensionLoadedObservable.clear();
  2879. };
  2880. GLTFLoader.prototype._loadData = function (data) {
  2881. this._gltf = data.json;
  2882. this._setupData();
  2883. if (data.bin) {
  2884. var buffers = this._gltf.buffers;
  2885. if (buffers && buffers[0] && !buffers[0].uri) {
  2886. var binaryBuffer = buffers[0];
  2887. if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
  2888. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  2889. }
  2890. binaryBuffer._data = Promise.resolve(data.bin);
  2891. }
  2892. else {
  2893. BABYLON.Tools.Warn("Unexpected BIN chunk");
  2894. }
  2895. }
  2896. };
  2897. GLTFLoader.prototype._setupData = function () {
  2898. GLTF2.ArrayItem.Assign(this._gltf.accessors);
  2899. GLTF2.ArrayItem.Assign(this._gltf.animations);
  2900. GLTF2.ArrayItem.Assign(this._gltf.buffers);
  2901. GLTF2.ArrayItem.Assign(this._gltf.bufferViews);
  2902. GLTF2.ArrayItem.Assign(this._gltf.cameras);
  2903. GLTF2.ArrayItem.Assign(this._gltf.images);
  2904. GLTF2.ArrayItem.Assign(this._gltf.materials);
  2905. GLTF2.ArrayItem.Assign(this._gltf.meshes);
  2906. GLTF2.ArrayItem.Assign(this._gltf.nodes);
  2907. GLTF2.ArrayItem.Assign(this._gltf.samplers);
  2908. GLTF2.ArrayItem.Assign(this._gltf.scenes);
  2909. GLTF2.ArrayItem.Assign(this._gltf.skins);
  2910. GLTF2.ArrayItem.Assign(this._gltf.textures);
  2911. if (this._gltf.nodes) {
  2912. var nodeParents = {};
  2913. for (var _i = 0, _a = this._gltf.nodes; _i < _a.length; _i++) {
  2914. var node = _a[_i];
  2915. if (node.children) {
  2916. for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
  2917. var index = _c[_b];
  2918. nodeParents[index] = node._index;
  2919. }
  2920. }
  2921. }
  2922. var rootNode = this._createRootNode();
  2923. for (var _d = 0, _e = this._gltf.nodes; _d < _e.length; _d++) {
  2924. var node = _e[_d];
  2925. var parentIndex = nodeParents[node._index];
  2926. node._parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
  2927. }
  2928. }
  2929. };
  2930. GLTFLoader.prototype._checkExtensions = function () {
  2931. if (this._gltf.extensionsRequired) {
  2932. for (var _i = 0, _a = this._gltf.extensionsRequired; _i < _a.length; _i++) {
  2933. var name_2 = _a[_i];
  2934. var extension = this._extensions[name_2];
  2935. if (!extension || !extension.enabled) {
  2936. throw new Error("Require extension " + name_2 + " is not available");
  2937. }
  2938. }
  2939. }
  2940. };
  2941. GLTFLoader.prototype._createRootNode = function () {
  2942. this._rootBabylonMesh = new BABYLON.Mesh("__root__", this._babylonScene);
  2943. this._rootBabylonMesh.setEnabled(false);
  2944. var rootNode = { _babylonMesh: this._rootBabylonMesh };
  2945. switch (this.coordinateSystemMode) {
  2946. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO: {
  2947. if (!this._babylonScene.useRightHandedSystem) {
  2948. rootNode.rotation = [0, 1, 0, 0];
  2949. rootNode.scale = [1, 1, -1];
  2950. GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
  2951. }
  2952. break;
  2953. }
  2954. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
  2955. this._babylonScene.useRightHandedSystem = true;
  2956. break;
  2957. }
  2958. default: {
  2959. throw new Error("Invalid coordinate system mode (" + this.coordinateSystemMode + ")");
  2960. }
  2961. }
  2962. this.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
  2963. return rootNode;
  2964. };
  2965. GLTFLoader.prototype._loadNodesAsync = function (nodes) {
  2966. var promises = new Array();
  2967. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  2968. var node = nodes_1[_i];
  2969. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  2970. }
  2971. promises.push(this._loadAnimationsAsync());
  2972. return Promise.all(promises).then(function () { });
  2973. };
  2974. GLTFLoader.prototype._loadSceneAsync = function (context, scene) {
  2975. var promise = GLTF2.GLTFLoaderExtension._LoadSceneAsync(this, context, scene);
  2976. if (promise) {
  2977. return promise;
  2978. }
  2979. var promises = new Array();
  2980. for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
  2981. var index = _a[_i];
  2982. var node = GLTFLoader._GetProperty(context + "/nodes/" + index, this._gltf.nodes, index);
  2983. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  2984. }
  2985. promises.push(this._loadAnimationsAsync());
  2986. return Promise.all(promises).then(function () { });
  2987. };
  2988. GLTFLoader.prototype._forEachPrimitive = function (node, callback) {
  2989. if (node._primitiveBabylonMeshes) {
  2990. for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
  2991. var babylonMesh = _a[_i];
  2992. callback(babylonMesh);
  2993. }
  2994. }
  2995. else {
  2996. callback(node._babylonMesh);
  2997. }
  2998. };
  2999. GLTFLoader.prototype._getMeshes = function () {
  3000. var meshes = new Array();
  3001. // Root mesh is always first.
  3002. meshes.push(this._rootBabylonMesh);
  3003. var nodes = this._gltf.nodes;
  3004. if (nodes) {
  3005. for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
  3006. var node = nodes_2[_i];
  3007. if (node._babylonMesh) {
  3008. meshes.push(node._babylonMesh);
  3009. }
  3010. if (node._primitiveBabylonMeshes) {
  3011. for (var _a = 0, _b = node._primitiveBabylonMeshes; _a < _b.length; _a++) {
  3012. var babylonMesh = _b[_a];
  3013. meshes.push(babylonMesh);
  3014. }
  3015. }
  3016. }
  3017. }
  3018. return meshes;
  3019. };
  3020. GLTFLoader.prototype._getSkeletons = function () {
  3021. var skeletons = new Array();
  3022. var skins = this._gltf.skins;
  3023. if (skins) {
  3024. for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
  3025. var skin = skins_1[_i];
  3026. if (skin._babylonSkeleton) {
  3027. skeletons.push(skin._babylonSkeleton);
  3028. }
  3029. }
  3030. }
  3031. return skeletons;
  3032. };
  3033. GLTFLoader.prototype._getAnimationGroups = function () {
  3034. var animationGroups = new Array();
  3035. var animations = this._gltf.animations;
  3036. if (animations) {
  3037. for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
  3038. var animation = animations_1[_i];
  3039. if (animation._babylonAnimationGroup) {
  3040. animationGroups.push(animation._babylonAnimationGroup);
  3041. }
  3042. }
  3043. }
  3044. return animationGroups;
  3045. };
  3046. GLTFLoader.prototype._startAnimations = function () {
  3047. switch (this.animationStartMode) {
  3048. case BABYLON.GLTFLoaderAnimationStartMode.NONE: {
  3049. // do nothing
  3050. break;
  3051. }
  3052. case BABYLON.GLTFLoaderAnimationStartMode.FIRST: {
  3053. var babylonAnimationGroups = this._getAnimationGroups();
  3054. if (babylonAnimationGroups.length !== 0) {
  3055. babylonAnimationGroups[0].start(true);
  3056. }
  3057. break;
  3058. }
  3059. case BABYLON.GLTFLoaderAnimationStartMode.ALL: {
  3060. var babylonAnimationGroups = this._getAnimationGroups();
  3061. for (var _i = 0, babylonAnimationGroups_1 = babylonAnimationGroups; _i < babylonAnimationGroups_1.length; _i++) {
  3062. var babylonAnimationGroup = babylonAnimationGroups_1[_i];
  3063. babylonAnimationGroup.start(true);
  3064. }
  3065. break;
  3066. }
  3067. default: {
  3068. BABYLON.Tools.Error("Invalid animation start mode (" + this.animationStartMode + ")");
  3069. return;
  3070. }
  3071. }
  3072. };
  3073. GLTFLoader.prototype._loadNodeAsync = function (context, node) {
  3074. var promise = GLTF2.GLTFLoaderExtension._LoadNodeAsync(this, context, node);
  3075. if (promise) {
  3076. return promise;
  3077. }
  3078. if (node._babylonMesh) {
  3079. throw new Error(context + ": Invalid recursive node hierarchy");
  3080. }
  3081. var promises = new Array();
  3082. var babylonMesh = new BABYLON.Mesh(node.name || "node" + node._index, this._babylonScene, node._parent._babylonMesh);
  3083. node._babylonMesh = babylonMesh;
  3084. node._babylonAnimationTargets = node._babylonAnimationTargets || [];
  3085. node._babylonAnimationTargets.push(babylonMesh);
  3086. GLTFLoader._LoadTransform(node, babylonMesh);
  3087. if (node.mesh != undefined) {
  3088. var mesh = GLTFLoader._GetProperty(context + "/mesh", this._gltf.meshes, node.mesh);
  3089. promises.push(this._loadMeshAsync("#/meshes/" + mesh._index, node, mesh, babylonMesh));
  3090. }
  3091. if (node.children) {
  3092. for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
  3093. var index = _a[_i];
  3094. var childNode = GLTFLoader._GetProperty(context + "/children/" + index, this._gltf.nodes, index);
  3095. promises.push(this._loadNodeAsync("#/nodes/" + index, childNode));
  3096. }
  3097. }
  3098. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  3099. return Promise.all(promises).then(function () { });
  3100. };
  3101. GLTFLoader.prototype._loadMeshAsync = function (context, node, mesh, babylonMesh) {
  3102. // TODO: instancing
  3103. var _this = this;
  3104. var promises = new Array();
  3105. var primitives = mesh.primitives;
  3106. if (!primitives || primitives.length === 0) {
  3107. throw new Error(context + ": Primitives are missing");
  3108. }
  3109. GLTF2.ArrayItem.Assign(primitives);
  3110. if (primitives.length === 1) {
  3111. var primitive = primitives[0];
  3112. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, babylonMesh));
  3113. }
  3114. else {
  3115. node._primitiveBabylonMeshes = [];
  3116. for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
  3117. var primitive = primitives_1[_i];
  3118. var primitiveBabylonMesh = new BABYLON.Mesh((mesh.name || babylonMesh.name) + "_" + primitive._index, this._babylonScene, babylonMesh);
  3119. node._primitiveBabylonMeshes.push(primitiveBabylonMesh);
  3120. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, primitiveBabylonMesh));
  3121. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  3122. }
  3123. }
  3124. if (node.skin != undefined) {
  3125. var skin = GLTFLoader._GetProperty(context + "/skin", this._gltf.skins, node.skin);
  3126. promises.push(this._loadSkinAsync("#/skins/" + skin._index, node, mesh, skin));
  3127. }
  3128. return Promise.all(promises).then(function () {
  3129. _this._forEachPrimitive(node, function (babylonMesh) {
  3130. babylonMesh._refreshBoundingInfo(true);
  3131. });
  3132. });
  3133. };
  3134. GLTFLoader.prototype._loadPrimitiveAsync = function (context, node, mesh, primitive, babylonMesh) {
  3135. var _this = this;
  3136. var promises = new Array();
  3137. this._createMorphTargets(context, node, mesh, primitive, babylonMesh);
  3138. promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh).then(function (babylonGeometry) {
  3139. return _this._loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry).then(function () {
  3140. babylonGeometry.applyToMesh(babylonMesh);
  3141. });
  3142. }));
  3143. var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
  3144. if (primitive.material == undefined) {
  3145. babylonMesh.material = this._getDefaultMaterial(babylonDrawMode);
  3146. }
  3147. else {
  3148. var material = GLTFLoader._GetProperty(context + "/material}", this._gltf.materials, primitive.material);
  3149. promises.push(this._loadMaterialAsync("#/materials/" + material._index, material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
  3150. babylonMesh.material = babylonMaterial;
  3151. }));
  3152. }
  3153. return Promise.all(promises).then(function () { });
  3154. };
  3155. GLTFLoader.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  3156. var _this = this;
  3157. var promise = GLTF2.GLTFLoaderExtension._LoadVertexDataAsync(this, context, primitive, babylonMesh);
  3158. if (promise) {
  3159. return promise;
  3160. }
  3161. var attributes = primitive.attributes;
  3162. if (!attributes) {
  3163. throw new Error(context + ": Attributes are missing");
  3164. }
  3165. var promises = new Array();
  3166. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, this._babylonScene);
  3167. if (primitive.indices == undefined) {
  3168. babylonMesh.isUnIndexed = true;
  3169. }
  3170. else {
  3171. var accessor = GLTFLoader._GetProperty(context + "/indices", this._gltf.accessors, primitive.indices);
  3172. promises.push(this._loadAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  3173. babylonGeometry.setIndices(data);
  3174. }));
  3175. }
  3176. var loadAttribute = function (attribute, kind, callback) {
  3177. if (attributes[attribute] == undefined) {
  3178. return;
  3179. }
  3180. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  3181. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  3182. babylonMesh._delayInfo.push(kind);
  3183. }
  3184. var accessor = GLTFLoader._GetProperty(context + "/attributes/" + attribute, _this._gltf.accessors, attributes[attribute]);
  3185. promises.push(_this._loadVertexAccessorAsync("#/accessors/" + accessor._index, accessor, kind).then(function (babylonVertexBuffer) {
  3186. babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
  3187. }));
  3188. if (callback) {
  3189. callback(accessor);
  3190. }
  3191. };
  3192. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  3193. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  3194. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  3195. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  3196. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  3197. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  3198. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  3199. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind, function (accessor) {
  3200. if (accessor.type === "VEC4" /* VEC4 */) {
  3201. babylonMesh.hasVertexAlpha = true;
  3202. }
  3203. });
  3204. return Promise.all(promises).then(function () {
  3205. return babylonGeometry;
  3206. });
  3207. };
  3208. GLTFLoader.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
  3209. if (!primitive.targets) {
  3210. return;
  3211. }
  3212. if (node._numMorphTargets == undefined) {
  3213. node._numMorphTargets = primitive.targets.length;
  3214. }
  3215. else if (primitive.targets.length !== node._numMorphTargets) {
  3216. throw new Error(context + ": Primitives do not have the same number of targets");
  3217. }
  3218. babylonMesh.morphTargetManager = new BABYLON.MorphTargetManager();
  3219. for (var index = 0; index < primitive.targets.length; index++) {
  3220. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  3221. babylonMesh.morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  3222. // TODO: tell the target whether it has positions, normals, tangents
  3223. }
  3224. };
  3225. GLTFLoader.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonGeometry) {
  3226. if (!primitive.targets) {
  3227. return Promise.resolve();
  3228. }
  3229. var promises = new Array();
  3230. var morphTargetManager = babylonMesh.morphTargetManager;
  3231. for (var index = 0; index < morphTargetManager.numTargets; index++) {
  3232. var babylonMorphTarget = morphTargetManager.getTarget(index);
  3233. promises.push(this._loadMorphTargetVertexDataAsync(context + "/targets/" + index, babylonGeometry, primitive.targets[index], babylonMorphTarget));
  3234. }
  3235. return Promise.all(promises).then(function () { });
  3236. };
  3237. GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, babylonGeometry, attributes, babylonMorphTarget) {
  3238. var _this = this;
  3239. var promises = new Array();
  3240. var loadAttribute = function (attribute, kind, setData) {
  3241. if (attributes[attribute] == undefined) {
  3242. return;
  3243. }
  3244. var babylonVertexBuffer = babylonGeometry.getVertexBuffer(kind);
  3245. if (!babylonVertexBuffer) {
  3246. return;
  3247. }
  3248. var accessor = GLTFLoader._GetProperty(context + "/" + attribute, _this._gltf.accessors, attributes[attribute]);
  3249. promises.push(_this._loadAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  3250. if (!(data instanceof Float32Array)) {
  3251. throw new Error(context + ": Morph target accessor must have float data");
  3252. }
  3253. setData(babylonVertexBuffer, data);
  3254. }));
  3255. };
  3256. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind, function (babylonVertexBuffer, data) {
  3257. babylonVertexBuffer.forEach(data.length, function (value, index) {
  3258. data[index] += value;
  3259. });
  3260. babylonMorphTarget.setPositions(data);
  3261. });
  3262. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind, function (babylonVertexBuffer, data) {
  3263. babylonVertexBuffer.forEach(data.length, function (value, index) {
  3264. data[index] += value;
  3265. });
  3266. babylonMorphTarget.setNormals(data);
  3267. });
  3268. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind, function (babylonVertexBuffer, data) {
  3269. var dataIndex = 0;
  3270. babylonVertexBuffer.forEach(data.length, function (value, index) {
  3271. // Tangent data for morph targets is stored as xyz delta.
  3272. // The vertexData.tangent is stored as xyzw.
  3273. // So we need to skip every fourth vertexData.tangent.
  3274. if (((index + 1) % 4) !== 0) {
  3275. data[dataIndex++] += value;
  3276. }
  3277. });
  3278. babylonMorphTarget.setTangents(data);
  3279. });
  3280. return Promise.all(promises).then(function () { });
  3281. };
  3282. GLTFLoader._LoadTransform = function (node, babylonNode) {
  3283. var position = BABYLON.Vector3.Zero();
  3284. var rotation = BABYLON.Quaternion.Identity();
  3285. var scaling = BABYLON.Vector3.One();
  3286. if (node.matrix) {
  3287. var matrix = BABYLON.Matrix.FromArray(node.matrix);
  3288. matrix.decompose(scaling, rotation, position);
  3289. }
  3290. else {
  3291. if (node.translation)
  3292. position = BABYLON.Vector3.FromArray(node.translation);
  3293. if (node.rotation)
  3294. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  3295. if (node.scale)
  3296. scaling = BABYLON.Vector3.FromArray(node.scale);
  3297. }
  3298. babylonNode.position = position;
  3299. babylonNode.rotationQuaternion = rotation;
  3300. babylonNode.scaling = scaling;
  3301. };
  3302. GLTFLoader.prototype._loadSkinAsync = function (context, node, mesh, skin) {
  3303. var _this = this;
  3304. var assignSkeleton = function () {
  3305. _this._forEachPrimitive(node, function (babylonMesh) {
  3306. babylonMesh.skeleton = skin._babylonSkeleton;
  3307. });
  3308. // Ignore the TRS of skinned nodes.
  3309. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  3310. node._babylonMesh.parent = _this._rootBabylonMesh;
  3311. node._babylonMesh.position = BABYLON.Vector3.Zero();
  3312. node._babylonMesh.rotationQuaternion = BABYLON.Quaternion.Identity();
  3313. node._babylonMesh.scaling = BABYLON.Vector3.One();
  3314. };
  3315. if (skin._loaded) {
  3316. return skin._loaded.then(function () {
  3317. assignSkeleton();
  3318. });
  3319. }
  3320. // TODO: split into two parts so that bones are created before inverseBindMatricesData is loaded (for compiling materials).
  3321. return (skin._loaded = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
  3322. var skeletonId = "skeleton" + skin._index;
  3323. var babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, _this._babylonScene);
  3324. skin._babylonSkeleton = babylonSkeleton;
  3325. _this._loadBones(context, skin, inverseBindMatricesData);
  3326. assignSkeleton();
  3327. }));
  3328. };
  3329. GLTFLoader.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
  3330. if (skin.inverseBindMatrices == undefined) {
  3331. return Promise.resolve(null);
  3332. }
  3333. var accessor = GLTFLoader._GetProperty(context + "/inverseBindMatrices", this._gltf.accessors, skin.inverseBindMatrices);
  3334. return this._loadAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  3335. return data;
  3336. });
  3337. };
  3338. GLTFLoader.prototype._createBone = function (node, skin, parent, localMatrix, baseMatrix, index) {
  3339. var babylonBone = new BABYLON.Bone(node.name || "joint" + node._index, skin._babylonSkeleton, parent, localMatrix, null, baseMatrix, index);
  3340. node._babylonAnimationTargets = node._babylonAnimationTargets || [];
  3341. node._babylonAnimationTargets.push(babylonBone);
  3342. return babylonBone;
  3343. };
  3344. GLTFLoader.prototype._loadBones = function (context, skin, inverseBindMatricesData) {
  3345. var babylonBones = {};
  3346. for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
  3347. var index = _a[_i];
  3348. var node = GLTFLoader._GetProperty(context + "/joints/" + index, this._gltf.nodes, index);
  3349. this._loadBone(node, skin, inverseBindMatricesData, babylonBones);
  3350. }
  3351. };
  3352. GLTFLoader.prototype._loadBone = function (node, skin, inverseBindMatricesData, babylonBones) {
  3353. var babylonBone = babylonBones[node._index];
  3354. if (babylonBone) {
  3355. return babylonBone;
  3356. }
  3357. var boneIndex = skin.joints.indexOf(node._index);
  3358. var baseMatrix = BABYLON.Matrix.Identity();
  3359. if (inverseBindMatricesData && boneIndex !== -1) {
  3360. baseMatrix = BABYLON.Matrix.FromArray(inverseBindMatricesData, boneIndex * 16);
  3361. baseMatrix.invertToRef(baseMatrix);
  3362. }
  3363. var babylonParentBone = null;
  3364. if (node._parent._babylonMesh !== this._rootBabylonMesh) {
  3365. babylonParentBone = this._loadBone(node._parent, skin, inverseBindMatricesData, babylonBones);
  3366. baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
  3367. }
  3368. babylonBone = this._createBone(node, skin, babylonParentBone, this._getNodeMatrix(node), baseMatrix, boneIndex);
  3369. babylonBones[node._index] = babylonBone;
  3370. return babylonBone;
  3371. };
  3372. GLTFLoader.prototype._getNodeMatrix = function (node) {
  3373. return node.matrix ?
  3374. BABYLON.Matrix.FromArray(node.matrix) :
  3375. 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());
  3376. };
  3377. GLTFLoader.prototype._loadAnimationsAsync = function () {
  3378. var animations = this._gltf.animations;
  3379. if (!animations) {
  3380. return Promise.resolve();
  3381. }
  3382. var promises = new Array();
  3383. for (var index = 0; index < animations.length; index++) {
  3384. var animation = animations[index];
  3385. promises.push(this._loadAnimationAsync("#/animations/" + index, animation));
  3386. }
  3387. return Promise.all(promises).then(function () { });
  3388. };
  3389. GLTFLoader.prototype._loadAnimationAsync = function (context, animation) {
  3390. var babylonAnimationGroup = new BABYLON.AnimationGroup(animation.name || "animation" + animation._index, this._babylonScene);
  3391. animation._babylonAnimationGroup = babylonAnimationGroup;
  3392. var promises = new Array();
  3393. GLTF2.ArrayItem.Assign(animation.channels);
  3394. GLTF2.ArrayItem.Assign(animation.samplers);
  3395. for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
  3396. var channel = _a[_i];
  3397. promises.push(this._loadAnimationChannelAsync(context + "/channels/" + channel._index, context, animation, channel, babylonAnimationGroup));
  3398. }
  3399. return Promise.all(promises).then(function () {
  3400. babylonAnimationGroup.normalize();
  3401. });
  3402. };
  3403. GLTFLoader.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup) {
  3404. var _this = this;
  3405. var targetNode = GLTFLoader._GetProperty(context + "/target/node", this._gltf.nodes, channel.target.node);
  3406. if (!targetNode._babylonMesh) {
  3407. return Promise.resolve();
  3408. }
  3409. // Ignore animations targeting TRS of skinned nodes.
  3410. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  3411. if (targetNode.skin != undefined && channel.target.path !== "weights" /* WEIGHTS */) {
  3412. return Promise.resolve();
  3413. }
  3414. var sampler = GLTFLoader._GetProperty(context + "/sampler", animation.samplers, channel.sampler);
  3415. return this._loadAnimationSamplerAsync(animationContext + "/samplers/" + channel.sampler, sampler).then(function (data) {
  3416. var targetPath;
  3417. var animationType;
  3418. switch (channel.target.path) {
  3419. case "translation" /* TRANSLATION */: {
  3420. targetPath = "position";
  3421. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  3422. break;
  3423. }
  3424. case "rotation" /* ROTATION */: {
  3425. targetPath = "rotationQuaternion";
  3426. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  3427. break;
  3428. }
  3429. case "scale" /* SCALE */: {
  3430. targetPath = "scaling";
  3431. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  3432. break;
  3433. }
  3434. case "weights" /* WEIGHTS */: {
  3435. targetPath = "influence";
  3436. animationType = BABYLON.Animation.ANIMATIONTYPE_FLOAT;
  3437. break;
  3438. }
  3439. default: {
  3440. throw new Error(context + ": Invalid target path (" + channel.target.path + ")");
  3441. }
  3442. }
  3443. var outputBufferOffset = 0;
  3444. var getNextOutputValue;
  3445. switch (targetPath) {
  3446. case "position": {
  3447. getNextOutputValue = function () {
  3448. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  3449. outputBufferOffset += 3;
  3450. return value;
  3451. };
  3452. break;
  3453. }
  3454. case "rotationQuaternion": {
  3455. getNextOutputValue = function () {
  3456. var value = BABYLON.Quaternion.FromArray(data.output, outputBufferOffset);
  3457. outputBufferOffset += 4;
  3458. return value;
  3459. };
  3460. break;
  3461. }
  3462. case "scaling": {
  3463. getNextOutputValue = function () {
  3464. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  3465. outputBufferOffset += 3;
  3466. return value;
  3467. };
  3468. break;
  3469. }
  3470. case "influence": {
  3471. getNextOutputValue = function () {
  3472. var value = new Array(targetNode._numMorphTargets);
  3473. for (var i = 0; i < targetNode._numMorphTargets; i++) {
  3474. value[i] = data.output[outputBufferOffset++];
  3475. }
  3476. return value;
  3477. };
  3478. break;
  3479. }
  3480. }
  3481. var getNextKey;
  3482. switch (data.interpolation) {
  3483. case "STEP" /* STEP */: {
  3484. getNextKey = function (frameIndex) { return ({
  3485. frame: data.input[frameIndex],
  3486. value: getNextOutputValue(),
  3487. interpolation: BABYLON.AnimationKeyInterpolation.STEP
  3488. }); };
  3489. break;
  3490. }
  3491. case "LINEAR" /* LINEAR */: {
  3492. getNextKey = function (frameIndex) { return ({
  3493. frame: data.input[frameIndex],
  3494. value: getNextOutputValue()
  3495. }); };
  3496. break;
  3497. }
  3498. case "CUBICSPLINE" /* CUBICSPLINE */: {
  3499. getNextKey = function (frameIndex) { return ({
  3500. frame: data.input[frameIndex],
  3501. inTangent: getNextOutputValue(),
  3502. value: getNextOutputValue(),
  3503. outTangent: getNextOutputValue()
  3504. }); };
  3505. break;
  3506. }
  3507. }
  3508. var keys = new Array(data.input.length);
  3509. for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
  3510. keys[frameIndex] = getNextKey(frameIndex);
  3511. }
  3512. if (targetPath === "influence") {
  3513. var _loop_1 = function (targetIndex) {
  3514. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  3515. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  3516. babylonAnimation.setKeys(keys.map(function (key) { return ({
  3517. frame: key.frame,
  3518. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  3519. value: key.value[targetIndex],
  3520. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  3521. }); }));
  3522. var multiTarget = new GLTF2.AnimationMultiTarget();
  3523. _this._forEachPrimitive(targetNode, function (babylonMesh) {
  3524. var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
  3525. multiTarget.subTargets.push(morphTarget);
  3526. });
  3527. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, multiTarget);
  3528. };
  3529. for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
  3530. _loop_1(targetIndex);
  3531. }
  3532. }
  3533. else {
  3534. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  3535. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  3536. babylonAnimation.setKeys(keys);
  3537. if (targetNode._babylonAnimationTargets) {
  3538. var multiTarget = new GLTF2.AnimationMultiTarget();
  3539. for (var _i = 0, _a = targetNode._babylonAnimationTargets; _i < _a.length; _i++) {
  3540. var target = _a[_i];
  3541. multiTarget.subTargets.push(target);
  3542. }
  3543. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, multiTarget);
  3544. }
  3545. }
  3546. });
  3547. };
  3548. GLTFLoader.prototype._loadAnimationSamplerAsync = function (context, sampler) {
  3549. if (sampler._data) {
  3550. return sampler._data;
  3551. }
  3552. var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
  3553. switch (interpolation) {
  3554. case "STEP" /* STEP */:
  3555. case "LINEAR" /* LINEAR */:
  3556. case "CUBICSPLINE" /* CUBICSPLINE */: {
  3557. break;
  3558. }
  3559. default: {
  3560. throw new Error(context + ": Invalid interpolation (" + sampler.interpolation + ")");
  3561. }
  3562. }
  3563. var inputData;
  3564. var outputData;
  3565. var inputAccessor = GLTFLoader._GetProperty(context + "/input", this._gltf.accessors, sampler.input);
  3566. var outputAccessor = GLTFLoader._GetProperty(context + "/output", this._gltf.accessors, sampler.output);
  3567. sampler._data = Promise.all([
  3568. this._loadAccessorAsync("#/accessors/" + inputAccessor._index, inputAccessor).then(function (data) {
  3569. inputData = data;
  3570. }),
  3571. this._loadAccessorAsync("#/accessors/" + outputAccessor._index, outputAccessor).then(function (data) {
  3572. outputData = data;
  3573. })
  3574. ]).then(function () {
  3575. return {
  3576. input: inputData,
  3577. interpolation: interpolation,
  3578. output: outputData,
  3579. };
  3580. });
  3581. return sampler._data;
  3582. };
  3583. GLTFLoader.prototype._loadBufferAsync = function (context, buffer) {
  3584. if (buffer._data) {
  3585. return buffer._data;
  3586. }
  3587. if (!buffer.uri) {
  3588. throw new Error(context + ": Uri is missing");
  3589. }
  3590. buffer._data = this._loadUriAsync(context, buffer.uri);
  3591. return buffer._data;
  3592. };
  3593. GLTFLoader.prototype._loadBufferViewAsync = function (context, bufferView) {
  3594. if (bufferView._data) {
  3595. return bufferView._data;
  3596. }
  3597. var buffer = GLTFLoader._GetProperty(context + "/buffer", this._gltf.buffers, bufferView.buffer);
  3598. bufferView._data = this._loadBufferAsync("#/buffers/" + buffer._index, buffer).then(function (data) {
  3599. try {
  3600. return new Uint8Array(data.buffer, data.byteOffset + (bufferView.byteOffset || 0), bufferView.byteLength);
  3601. }
  3602. catch (e) {
  3603. throw new Error(context + ": " + e.message);
  3604. }
  3605. });
  3606. return bufferView._data;
  3607. };
  3608. GLTFLoader.prototype._loadAccessorAsync = function (context, accessor) {
  3609. if (accessor.sparse) {
  3610. throw new Error(context + ": Sparse accessors are not currently supported");
  3611. }
  3612. if (accessor._data) {
  3613. return accessor._data;
  3614. }
  3615. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  3616. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  3617. var buffer = data.buffer;
  3618. var byteOffset = data.byteOffset + (accessor.byteOffset || 0);
  3619. var length = GLTFLoader._GetNumComponents(context, accessor.type) * accessor.count;
  3620. try {
  3621. switch (accessor.componentType) {
  3622. case 5120 /* BYTE */: {
  3623. return new Int8Array(buffer, byteOffset, length);
  3624. }
  3625. case 5121 /* UNSIGNED_BYTE */: {
  3626. return new Uint8Array(buffer, byteOffset, length);
  3627. }
  3628. case 5122 /* SHORT */: {
  3629. return new Int16Array(buffer, byteOffset, length);
  3630. }
  3631. case 5123 /* UNSIGNED_SHORT */: {
  3632. return new Uint16Array(buffer, byteOffset, length);
  3633. }
  3634. case 5125 /* UNSIGNED_INT */: {
  3635. return new Uint32Array(buffer, byteOffset, length);
  3636. }
  3637. case 5126 /* FLOAT */: {
  3638. return new Float32Array(buffer, byteOffset, length);
  3639. }
  3640. default: {
  3641. throw new Error(context + ": Invalid accessor component type " + accessor.componentType);
  3642. }
  3643. }
  3644. }
  3645. catch (e) {
  3646. throw new Error(context + ": " + e);
  3647. }
  3648. });
  3649. return accessor._data;
  3650. };
  3651. GLTFLoader.prototype._loadVertexBufferViewAsync = function (context, bufferView, kind) {
  3652. var _this = this;
  3653. if (bufferView._babylonBuffer) {
  3654. return bufferView._babylonBuffer;
  3655. }
  3656. bufferView._babylonBuffer = this._loadBufferViewAsync(context, bufferView).then(function (data) {
  3657. return new BABYLON.Buffer(_this._babylonScene.getEngine(), data, false);
  3658. });
  3659. return bufferView._babylonBuffer;
  3660. };
  3661. GLTFLoader.prototype._loadVertexAccessorAsync = function (context, accessor, kind) {
  3662. var _this = this;
  3663. if (accessor.sparse) {
  3664. throw new Error(context + ": Sparse accessors are not currently supported");
  3665. }
  3666. if (accessor._babylonVertexBuffer) {
  3667. return accessor._babylonVertexBuffer;
  3668. }
  3669. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  3670. accessor._babylonVertexBuffer = this._loadVertexBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView, kind).then(function (buffer) {
  3671. var size = GLTFLoader._GetNumComponents(context, accessor.type);
  3672. return new BABYLON.VertexBuffer(_this._babylonScene.getEngine(), buffer, kind, false, false, bufferView.byteStride, false, accessor.byteOffset, size, accessor.componentType, accessor.normalized, true);
  3673. });
  3674. return accessor._babylonVertexBuffer;
  3675. };
  3676. GLTFLoader.prototype._getDefaultMaterial = function (drawMode) {
  3677. var babylonMaterial = this._defaultBabylonMaterials[drawMode];
  3678. if (!babylonMaterial) {
  3679. babylonMaterial = this._createMaterial(BABYLON.PBRMaterial, "__gltf_default", drawMode);
  3680. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  3681. babylonMaterial.metallic = 1;
  3682. babylonMaterial.roughness = 1;
  3683. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  3684. }
  3685. return babylonMaterial;
  3686. };
  3687. GLTFLoader.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, material, babylonMaterial) {
  3688. var promises = new Array();
  3689. // Ensure metallic workflow
  3690. babylonMaterial.metallic = 1;
  3691. babylonMaterial.roughness = 1;
  3692. var properties = material.pbrMetallicRoughness;
  3693. if (properties) {
  3694. if (properties.baseColorFactor) {
  3695. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  3696. babylonMaterial.alpha = properties.baseColorFactor[3];
  3697. }
  3698. else {
  3699. babylonMaterial.albedoColor = BABYLON.Color3.White();
  3700. }
  3701. babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
  3702. babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
  3703. if (properties.baseColorTexture) {
  3704. promises.push(this._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  3705. babylonMaterial.albedoTexture = texture;
  3706. }));
  3707. }
  3708. if (properties.metallicRoughnessTexture) {
  3709. promises.push(this._loadTextureAsync(context + "/metallicRoughnessTexture", properties.metallicRoughnessTexture, function (texture) {
  3710. babylonMaterial.metallicTexture = texture;
  3711. }));
  3712. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  3713. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  3714. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  3715. }
  3716. }
  3717. this._loadMaterialAlphaProperties(context, material, babylonMaterial);
  3718. return Promise.all(promises).then(function () { });
  3719. };
  3720. GLTFLoader.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  3721. var promise = GLTF2.GLTFLoaderExtension._LoadMaterialAsync(this, context, material, babylonMesh, babylonDrawMode, assign);
  3722. if (promise) {
  3723. return promise;
  3724. }
  3725. material._babylonData = material._babylonData || {};
  3726. var babylonData = material._babylonData[babylonDrawMode];
  3727. if (!babylonData) {
  3728. var promises = new Array();
  3729. var name_3 = material.name || "materialSG_" + material._index;
  3730. var babylonMaterial = this._createMaterial(BABYLON.PBRMaterial, name_3, babylonDrawMode);
  3731. promises.push(this._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  3732. promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync(context, material, babylonMaterial));
  3733. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  3734. babylonData = {
  3735. material: babylonMaterial,
  3736. meshes: [],
  3737. loaded: Promise.all(promises).then(function () { })
  3738. };
  3739. material._babylonData[babylonDrawMode] = babylonData;
  3740. }
  3741. babylonData.meshes.push(babylonMesh);
  3742. assign(babylonData.material);
  3743. return babylonData.loaded;
  3744. };
  3745. GLTFLoader.prototype._createMaterial = function (type, name, drawMode) {
  3746. var babylonMaterial = new type(name, this._babylonScene);
  3747. babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? BABYLON.Material.CounterClockWiseSideOrientation : BABYLON.Material.ClockWiseSideOrientation;
  3748. babylonMaterial.fillMode = drawMode;
  3749. return babylonMaterial;
  3750. };
  3751. GLTFLoader.prototype._loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
  3752. var promises = new Array();
  3753. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  3754. if (material.doubleSided) {
  3755. babylonMaterial.backFaceCulling = false;
  3756. babylonMaterial.twoSidedLighting = true;
  3757. }
  3758. if (material.normalTexture) {
  3759. promises.push(this._loadTextureAsync(context + "/normalTexture", material.normalTexture, function (texture) {
  3760. babylonMaterial.bumpTexture = texture;
  3761. }));
  3762. babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
  3763. babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
  3764. if (material.normalTexture.scale != undefined) {
  3765. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  3766. }
  3767. }
  3768. if (material.occlusionTexture) {
  3769. promises.push(this._loadTextureAsync(context + "/occlusionTexture", material.occlusionTexture, function (texture) {
  3770. babylonMaterial.ambientTexture = texture;
  3771. }));
  3772. babylonMaterial.useAmbientInGrayScale = true;
  3773. if (material.occlusionTexture.strength != undefined) {
  3774. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  3775. }
  3776. }
  3777. if (material.emissiveTexture) {
  3778. promises.push(this._loadTextureAsync(context + "/emissiveTexture", material.emissiveTexture, function (texture) {
  3779. babylonMaterial.emissiveTexture = texture;
  3780. }));
  3781. }
  3782. return Promise.all(promises).then(function () { });
  3783. };
  3784. GLTFLoader.prototype._loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
  3785. var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
  3786. switch (alphaMode) {
  3787. case "OPAQUE" /* OPAQUE */: {
  3788. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  3789. break;
  3790. }
  3791. case "MASK" /* MASK */: {
  3792. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  3793. babylonMaterial.alphaCutOff = (material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff);
  3794. if (babylonMaterial.albedoTexture) {
  3795. babylonMaterial.albedoTexture.hasAlpha = true;
  3796. }
  3797. break;
  3798. }
  3799. case "BLEND" /* BLEND */: {
  3800. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  3801. if (babylonMaterial.albedoTexture) {
  3802. babylonMaterial.albedoTexture.hasAlpha = true;
  3803. babylonMaterial.useAlphaFromAlbedoTexture = true;
  3804. }
  3805. break;
  3806. }
  3807. default: {
  3808. throw new Error(context + ": Invalid alpha mode (" + material.alphaMode + ")");
  3809. }
  3810. }
  3811. };
  3812. GLTFLoader.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  3813. var _this = this;
  3814. var texture = GLTFLoader._GetProperty(context + "/index", this._gltf.textures, textureInfo.index);
  3815. context = "#/textures/" + textureInfo.index;
  3816. var promises = new Array();
  3817. var sampler = (texture.sampler == undefined ? this._defaultSampler : GLTFLoader._GetProperty(context + "/sampler", this._gltf.samplers, texture.sampler));
  3818. var samplerData = this._loadSampler("#/samplers/" + sampler._index, sampler);
  3819. var deferred = new BABYLON.Deferred();
  3820. var babylonTexture = new BABYLON.Texture(null, this._babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, function () {
  3821. if (!_this._disposed) {
  3822. deferred.resolve();
  3823. }
  3824. }, function (message, exception) {
  3825. if (!_this._disposed) {
  3826. deferred.reject(new Error(context + ": " + ((exception && exception.message) ? exception.message : message || "Failed to load texture")));
  3827. }
  3828. });
  3829. promises.push(deferred.promise);
  3830. babylonTexture.name = texture.name || "texture" + texture._index;
  3831. babylonTexture.wrapU = samplerData.wrapU;
  3832. babylonTexture.wrapV = samplerData.wrapV;
  3833. babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
  3834. var image = GLTFLoader._GetProperty(context + "/source", this._gltf.images, texture.source);
  3835. promises.push(this._loadImageAsync("#/images/" + image._index, image).then(function (objectURL) {
  3836. babylonTexture.updateURL(objectURL);
  3837. }));
  3838. assign(babylonTexture);
  3839. this.onTextureLoadedObservable.notifyObservers(babylonTexture);
  3840. return Promise.all(promises).then(function () { });
  3841. };
  3842. GLTFLoader.prototype._loadSampler = function (context, sampler) {
  3843. if (!sampler._data) {
  3844. sampler._data = {
  3845. noMipMaps: (sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */),
  3846. samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler.magFilter, sampler.minFilter),
  3847. wrapU: GLTFLoader._GetTextureWrapMode(context, sampler.wrapS),
  3848. wrapV: GLTFLoader._GetTextureWrapMode(context, sampler.wrapT)
  3849. };
  3850. }
  3851. ;
  3852. return sampler._data;
  3853. };
  3854. GLTFLoader.prototype._loadImageAsync = function (context, image) {
  3855. if (image._objectURL) {
  3856. return image._objectURL;
  3857. }
  3858. var promise;
  3859. if (image.uri) {
  3860. promise = this._loadUriAsync(context, image.uri);
  3861. }
  3862. else {
  3863. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, image.bufferView);
  3864. promise = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView);
  3865. }
  3866. image._objectURL = promise.then(function (data) {
  3867. return URL.createObjectURL(new Blob([data], { type: image.mimeType }));
  3868. });
  3869. return image._objectURL;
  3870. };
  3871. GLTFLoader.prototype._loadUriAsync = function (context, uri) {
  3872. var _this = this;
  3873. var promise = GLTF2.GLTFLoaderExtension._LoadUriAsync(this, context, uri);
  3874. if (promise) {
  3875. return promise;
  3876. }
  3877. if (!GLTFLoader._ValidateUri(uri)) {
  3878. throw new Error(context + ": Uri '" + uri + "' is invalid");
  3879. }
  3880. if (BABYLON.Tools.IsBase64(uri)) {
  3881. return Promise.resolve(new Uint8Array(BABYLON.Tools.DecodeBase64(uri)));
  3882. }
  3883. return new Promise(function (resolve, reject) {
  3884. var request = BABYLON.Tools.LoadFile(_this._rootUrl + uri, function (data) {
  3885. if (!_this._disposed) {
  3886. resolve(new Uint8Array(data));
  3887. }
  3888. }, function (event) {
  3889. if (!_this._disposed) {
  3890. try {
  3891. if (request && _this._state === BABYLON.GLTFLoaderState.LOADING) {
  3892. request._lengthComputable = event.lengthComputable;
  3893. request._loaded = event.loaded;
  3894. request._total = event.total;
  3895. _this._onProgress();
  3896. }
  3897. }
  3898. catch (e) {
  3899. reject(e);
  3900. }
  3901. }
  3902. }, _this._babylonScene.database, true, function (request, exception) {
  3903. if (!_this._disposed) {
  3904. reject(new BABYLON.LoadFileError(context + ": Failed to load '" + uri + "'" + (request ? ": " + request.status + " " + request.statusText : ""), request));
  3905. }
  3906. });
  3907. _this._requests.push(request);
  3908. });
  3909. };
  3910. GLTFLoader.prototype._onProgress = function () {
  3911. if (!this._progressCallback) {
  3912. return;
  3913. }
  3914. var lengthComputable = true;
  3915. var loaded = 0;
  3916. var total = 0;
  3917. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  3918. var request = _a[_i];
  3919. if (request._lengthComputable === undefined || request._loaded === undefined || request._total === undefined) {
  3920. return;
  3921. }
  3922. lengthComputable = lengthComputable && request._lengthComputable;
  3923. loaded += request._loaded;
  3924. total += request._total;
  3925. }
  3926. this._progressCallback(new BABYLON.SceneLoaderProgressEvent(lengthComputable, loaded, lengthComputable ? total : 0));
  3927. };
  3928. GLTFLoader._GetProperty = function (context, array, index) {
  3929. if (!array || index == undefined || !array[index]) {
  3930. throw new Error(context + ": Failed to find index (" + index + ")");
  3931. }
  3932. return array[index];
  3933. };
  3934. GLTFLoader._GetTextureWrapMode = function (context, mode) {
  3935. // Set defaults if undefined
  3936. mode = mode == undefined ? 10497 /* REPEAT */ : mode;
  3937. switch (mode) {
  3938. case 33071 /* CLAMP_TO_EDGE */: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  3939. case 33648 /* MIRRORED_REPEAT */: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  3940. case 10497 /* REPEAT */: return BABYLON.Texture.WRAP_ADDRESSMODE;
  3941. default:
  3942. BABYLON.Tools.Warn(context + ": Invalid texture wrap mode (" + mode + ")");
  3943. return BABYLON.Texture.WRAP_ADDRESSMODE;
  3944. }
  3945. };
  3946. GLTFLoader._GetTextureSamplingMode = function (context, magFilter, minFilter) {
  3947. // Set defaults if undefined
  3948. magFilter = magFilter == undefined ? 9729 /* LINEAR */ : magFilter;
  3949. minFilter = minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : minFilter;
  3950. if (magFilter === 9729 /* LINEAR */) {
  3951. switch (minFilter) {
  3952. case 9728 /* NEAREST */: return BABYLON.Texture.LINEAR_NEAREST;
  3953. case 9729 /* LINEAR */: return BABYLON.Texture.LINEAR_LINEAR;
  3954. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_NEAREST_MIPNEAREST;
  3955. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_LINEAR_MIPNEAREST;
  3956. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_NEAREST_MIPLINEAR;
  3957. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  3958. default:
  3959. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  3960. return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  3961. }
  3962. }
  3963. else {
  3964. if (magFilter !== 9728 /* NEAREST */) {
  3965. BABYLON.Tools.Warn(context + ": Invalid texture magnification filter (" + magFilter + ")");
  3966. }
  3967. switch (minFilter) {
  3968. case 9728 /* NEAREST */: return BABYLON.Texture.NEAREST_NEAREST;
  3969. case 9729 /* LINEAR */: return BABYLON.Texture.NEAREST_LINEAR;
  3970. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  3971. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_LINEAR_MIPNEAREST;
  3972. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_NEAREST_MIPLINEAR;
  3973. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_LINEAR_MIPLINEAR;
  3974. default:
  3975. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  3976. return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  3977. }
  3978. }
  3979. };
  3980. GLTFLoader._GetNumComponents = function (context, type) {
  3981. switch (type) {
  3982. case "SCALAR": return 1;
  3983. case "VEC2": return 2;
  3984. case "VEC3": return 3;
  3985. case "VEC4": return 4;
  3986. case "MAT2": return 4;
  3987. case "MAT3": return 9;
  3988. case "MAT4": return 16;
  3989. }
  3990. throw new Error(context + ": Invalid type (" + type + ")");
  3991. };
  3992. GLTFLoader._ValidateUri = function (uri) {
  3993. return (BABYLON.Tools.IsBase64(uri) || uri.indexOf("..") === -1);
  3994. };
  3995. GLTFLoader._GetDrawMode = function (context, mode) {
  3996. if (mode == undefined) {
  3997. mode = 4 /* TRIANGLES */;
  3998. }
  3999. switch (mode) {
  4000. case 0 /* POINTS */: return BABYLON.Material.PointListDrawMode;
  4001. case 1 /* LINES */: return BABYLON.Material.LineListDrawMode;
  4002. case 2 /* LINE_LOOP */: return BABYLON.Material.LineLoopDrawMode;
  4003. case 3 /* LINE_STRIP */: return BABYLON.Material.LineStripDrawMode;
  4004. case 4 /* TRIANGLES */: return BABYLON.Material.TriangleFillMode;
  4005. case 5 /* TRIANGLE_STRIP */: return BABYLON.Material.TriangleStripDrawMode;
  4006. case 6 /* TRIANGLE_FAN */: return BABYLON.Material.TriangleFanDrawMode;
  4007. }
  4008. throw new Error(context + ": Invalid mesh primitive mode (" + mode + ")");
  4009. };
  4010. GLTFLoader.prototype._compileMaterialsAsync = function () {
  4011. var promises = new Array();
  4012. if (this._gltf.materials) {
  4013. for (var _i = 0, _a = this._gltf.materials; _i < _a.length; _i++) {
  4014. var material = _a[_i];
  4015. if (material._babylonData) {
  4016. for (var babylonDrawMode in material._babylonData) {
  4017. var babylonData = material._babylonData[babylonDrawMode];
  4018. for (var _b = 0, _c = babylonData.meshes; _b < _c.length; _b++) {
  4019. var babylonMesh = _c[_b];
  4020. // Ensure nonUniformScaling is set if necessary.
  4021. babylonMesh.computeWorldMatrix(true);
  4022. var babylonMaterial = babylonData.material;
  4023. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
  4024. if (this.useClipPlane) {
  4025. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
  4026. }
  4027. }
  4028. }
  4029. }
  4030. }
  4031. }
  4032. return Promise.all(promises).then(function () { });
  4033. };
  4034. GLTFLoader.prototype._compileShadowGeneratorsAsync = function () {
  4035. var promises = new Array();
  4036. var lights = this._babylonScene.lights;
  4037. for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
  4038. var light = lights_1[_i];
  4039. var generator = light.getShadowGenerator();
  4040. if (generator) {
  4041. promises.push(generator.forceCompilationAsync());
  4042. }
  4043. }
  4044. return Promise.all(promises).then(function () { });
  4045. };
  4046. GLTFLoader.prototype._clear = function () {
  4047. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  4048. var request = _a[_i];
  4049. request.abort();
  4050. }
  4051. this._requests.length = 0;
  4052. if (this._gltf && this._gltf.images) {
  4053. for (var _b = 0, _c = this._gltf.images; _b < _c.length; _b++) {
  4054. var image = _c[_b];
  4055. if (image._objectURL) {
  4056. image._objectURL.then(function (value) {
  4057. URL.revokeObjectURL(value);
  4058. });
  4059. image._objectURL = undefined;
  4060. }
  4061. }
  4062. }
  4063. delete this._gltf;
  4064. delete this._babylonScene;
  4065. this._completePromises.length = 0;
  4066. for (var name_4 in this._extensions) {
  4067. this._extensions[name_4].dispose();
  4068. }
  4069. this._extensions = {};
  4070. delete this._rootBabylonMesh;
  4071. delete this._progressCallback;
  4072. this.onMeshLoadedObservable.clear();
  4073. this.onTextureLoadedObservable.clear();
  4074. this.onMaterialLoadedObservable.clear();
  4075. };
  4076. GLTFLoader.prototype._applyExtensions = function (actionAsync) {
  4077. for (var _i = 0, _a = GLTFLoader._Names; _i < _a.length; _i++) {
  4078. var name_5 = _a[_i];
  4079. var extension = this._extensions[name_5];
  4080. if (extension.enabled) {
  4081. var promise = actionAsync(extension);
  4082. if (promise) {
  4083. return promise;
  4084. }
  4085. }
  4086. }
  4087. return null;
  4088. };
  4089. GLTFLoader._Names = new Array();
  4090. GLTFLoader._Factories = {};
  4091. return GLTFLoader;
  4092. }());
  4093. GLTF2.GLTFLoader = GLTFLoader;
  4094. BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = function () { return new GLTFLoader(); };
  4095. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  4096. })(BABYLON || (BABYLON = {}));
  4097. //# sourceMappingURL=babylon.glTFLoader.js.map
  4098. "use strict";
  4099. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  4100. var BABYLON;
  4101. (function (BABYLON) {
  4102. var GLTF2;
  4103. (function (GLTF2) {
  4104. var GLTFLoaderExtension = /** @class */ (function () {
  4105. function GLTFLoaderExtension(loader) {
  4106. this.enabled = true;
  4107. this._loader = loader;
  4108. }
  4109. GLTFLoaderExtension.prototype.dispose = function () {
  4110. delete this._loader;
  4111. };
  4112. // #region Overridable Methods
  4113. /** Override this method to modify the default behavior for loading scenes. */
  4114. GLTFLoaderExtension.prototype._loadSceneAsync = function (context, node) { return null; };
  4115. /** Override this method to modify the default behavior for loading nodes. */
  4116. GLTFLoaderExtension.prototype._loadNodeAsync = function (context, node) { return null; };
  4117. /** Override this method to modify the default behavior for loading mesh primitive vertex data. */
  4118. GLTFLoaderExtension.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) { return null; };
  4119. /** Override this method to modify the default behavior for loading materials. */
  4120. GLTFLoaderExtension.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) { return null; };
  4121. /** Override this method to modify the default behavior for loading uris. */
  4122. GLTFLoaderExtension.prototype._loadUriAsync = function (context, uri) { return null; };
  4123. // #endregion
  4124. /** Helper method called by a loader extension to load an glTF extension. */
  4125. GLTFLoaderExtension.prototype._loadExtensionAsync = function (context, property, actionAsync) {
  4126. if (!property.extensions) {
  4127. return null;
  4128. }
  4129. var extensions = property.extensions;
  4130. var extension = extensions[this.name];
  4131. if (!extension) {
  4132. return null;
  4133. }
  4134. // Clear out the extension before executing the action to avoid recursing into the same property.
  4135. delete extensions[this.name];
  4136. try {
  4137. return actionAsync(context + "/extensions/" + this.name, extension);
  4138. }
  4139. finally {
  4140. // Restore the extension after executing the action.
  4141. extensions[this.name] = extension;
  4142. }
  4143. };
  4144. /** Helper method called by the loader to allow extensions to override loading scenes. */
  4145. GLTFLoaderExtension._LoadSceneAsync = function (loader, context, scene) {
  4146. return loader._applyExtensions(function (extension) { return extension._loadSceneAsync(context, scene); });
  4147. };
  4148. /** Helper method called by the loader to allow extensions to override loading nodes. */
  4149. GLTFLoaderExtension._LoadNodeAsync = function (loader, context, node) {
  4150. return loader._applyExtensions(function (extension) { return extension._loadNodeAsync(context, node); });
  4151. };
  4152. /** Helper method called by the loader to allow extensions to override loading mesh primitive vertex data. */
  4153. GLTFLoaderExtension._LoadVertexDataAsync = function (loader, context, primitive, babylonMesh) {
  4154. return loader._applyExtensions(function (extension) { return extension._loadVertexDataAsync(context, primitive, babylonMesh); });
  4155. };
  4156. /** Helper method called by the loader to allow extensions to override loading materials. */
  4157. GLTFLoaderExtension._LoadMaterialAsync = function (loader, context, material, babylonMesh, babylonDrawMode, assign) {
  4158. return loader._applyExtensions(function (extension) { return extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
  4159. };
  4160. /** Helper method called by the loader to allow extensions to override loading uris. */
  4161. GLTFLoaderExtension._LoadUriAsync = function (loader, context, uri) {
  4162. return loader._applyExtensions(function (extension) { return extension._loadUriAsync(context, uri); });
  4163. };
  4164. return GLTFLoaderExtension;
  4165. }());
  4166. GLTF2.GLTFLoaderExtension = GLTFLoaderExtension;
  4167. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  4168. })(BABYLON || (BABYLON = {}));
  4169. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  4170. "use strict";
  4171. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  4172. var __extends = (this && this.__extends) || (function () {
  4173. var extendStatics = Object.setPrototypeOf ||
  4174. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4175. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  4176. return function (d, b) {
  4177. extendStatics(d, b);
  4178. function __() { this.constructor = d; }
  4179. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  4180. };
  4181. })();
  4182. var BABYLON;
  4183. (function (BABYLON) {
  4184. var GLTF2;
  4185. (function (GLTF2) {
  4186. var Extensions;
  4187. (function (Extensions) {
  4188. // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/MSFT_lod
  4189. var NAME = "MSFT_lod";
  4190. var MSFT_lod = /** @class */ (function (_super) {
  4191. __extends(MSFT_lod, _super);
  4192. function MSFT_lod() {
  4193. var _this = _super !== null && _super.apply(this, arguments) || this;
  4194. _this.name = NAME;
  4195. /**
  4196. * Maximum number of LODs to load, starting from the lowest LOD.
  4197. */
  4198. _this.maxLODsToLoad = Number.MAX_VALUE;
  4199. _this._loadingNodeLOD = null;
  4200. _this._loadNodeSignals = {};
  4201. _this._loadingMaterialLOD = null;
  4202. _this._loadMaterialSignals = {};
  4203. return _this;
  4204. }
  4205. MSFT_lod.prototype._loadNodeAsync = function (context, node) {
  4206. var _this = this;
  4207. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  4208. var firstPromise;
  4209. var nodeLODs = _this._getLODs(extensionContext, node, _this._loader._gltf.nodes, extension.ids);
  4210. var _loop_1 = function (indexLOD) {
  4211. var nodeLOD = nodeLODs[indexLOD];
  4212. if (indexLOD !== 0) {
  4213. _this._loadingNodeLOD = nodeLOD;
  4214. if (!_this._loadNodeSignals[nodeLOD._index]) {
  4215. _this._loadNodeSignals[nodeLOD._index] = new BABYLON.Deferred();
  4216. }
  4217. }
  4218. var promise = _this._loader._loadNodeAsync("#/nodes/" + nodeLOD._index, nodeLOD).then(function () {
  4219. if (indexLOD !== 0) {
  4220. var previousNodeLOD = nodeLODs[indexLOD - 1];
  4221. if (previousNodeLOD._babylonMesh) {
  4222. previousNodeLOD._babylonMesh.dispose(false, true);
  4223. delete previousNodeLOD._babylonMesh;
  4224. }
  4225. }
  4226. if (indexLOD !== nodeLODs.length - 1) {
  4227. var nodeIndex = nodeLODs[indexLOD + 1]._index;
  4228. if (_this._loadNodeSignals[nodeIndex]) {
  4229. _this._loadNodeSignals[nodeIndex].resolve();
  4230. delete _this._loadNodeSignals[nodeIndex];
  4231. }
  4232. }
  4233. });
  4234. if (indexLOD === 0) {
  4235. firstPromise = promise;
  4236. }
  4237. else {
  4238. _this._loader._completePromises.push(promise);
  4239. _this._loadingNodeLOD = null;
  4240. }
  4241. };
  4242. for (var indexLOD = 0; indexLOD < nodeLODs.length; indexLOD++) {
  4243. _loop_1(indexLOD);
  4244. }
  4245. return firstPromise;
  4246. });
  4247. };
  4248. MSFT_lod.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  4249. var _this = this;
  4250. // Don't load material LODs if already loading a node LOD.
  4251. if (this._loadingNodeLOD) {
  4252. return null;
  4253. }
  4254. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  4255. var firstPromise;
  4256. var materialLODs = _this._getLODs(extensionContext, material, _this._loader._gltf.materials, extension.ids);
  4257. var _loop_2 = function (indexLOD) {
  4258. var materialLOD = materialLODs[indexLOD];
  4259. if (indexLOD !== 0) {
  4260. _this._loadingMaterialLOD = materialLOD;
  4261. if (!_this._loadMaterialSignals[materialLOD._index]) {
  4262. _this._loadMaterialSignals[materialLOD._index] = new BABYLON.Deferred();
  4263. }
  4264. }
  4265. var promise = _this._loader._loadMaterialAsync("#/materials/" + materialLOD._index, materialLOD, babylonMesh, babylonDrawMode, indexLOD === 0 ? assign : function () { }).then(function () {
  4266. if (indexLOD !== 0) {
  4267. var babylonDataLOD = materialLOD._babylonData;
  4268. assign(babylonDataLOD[babylonDrawMode].material);
  4269. var previousBabylonDataLOD = materialLODs[indexLOD - 1]._babylonData;
  4270. if (previousBabylonDataLOD[babylonDrawMode]) {
  4271. previousBabylonDataLOD[babylonDrawMode].material.dispose();
  4272. delete previousBabylonDataLOD[babylonDrawMode];
  4273. }
  4274. }
  4275. if (indexLOD !== materialLODs.length - 1) {
  4276. var materialIndex = materialLODs[indexLOD + 1]._index;
  4277. if (_this._loadMaterialSignals[materialIndex]) {
  4278. _this._loadMaterialSignals[materialIndex].resolve();
  4279. delete _this._loadMaterialSignals[materialIndex];
  4280. }
  4281. }
  4282. });
  4283. if (indexLOD === 0) {
  4284. firstPromise = promise;
  4285. }
  4286. else {
  4287. _this._loader._completePromises.push(promise);
  4288. _this._loadingMaterialLOD = null;
  4289. }
  4290. };
  4291. for (var indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  4292. _loop_2(indexLOD);
  4293. }
  4294. return firstPromise;
  4295. });
  4296. };
  4297. MSFT_lod.prototype._loadUriAsync = function (context, uri) {
  4298. var _this = this;
  4299. // Defer the loading of uris if loading a material or node LOD.
  4300. if (this._loadingMaterialLOD) {
  4301. var index = this._loadingMaterialLOD._index;
  4302. return this._loadMaterialSignals[index].promise.then(function () {
  4303. return _this._loader._loadUriAsync(context, uri);
  4304. });
  4305. }
  4306. else if (this._loadingNodeLOD) {
  4307. var index = this._loadingNodeLOD._index;
  4308. return this._loadNodeSignals[index].promise.then(function () {
  4309. return _this._loader._loadUriAsync(context, uri);
  4310. });
  4311. }
  4312. return null;
  4313. };
  4314. /**
  4315. * Gets an array of LOD properties from lowest to highest.
  4316. */
  4317. MSFT_lod.prototype._getLODs = function (context, property, array, ids) {
  4318. if (this.maxLODsToLoad <= 0) {
  4319. throw new Error("maxLODsToLoad must be greater than zero");
  4320. }
  4321. var properties = new Array();
  4322. for (var i = ids.length - 1; i >= 0; i--) {
  4323. properties.push(GLTF2.GLTFLoader._GetProperty(context + "/ids/" + ids[i], array, ids[i]));
  4324. if (properties.length === this.maxLODsToLoad) {
  4325. return properties;
  4326. }
  4327. }
  4328. properties.push(property);
  4329. return properties;
  4330. };
  4331. return MSFT_lod;
  4332. }(GLTF2.GLTFLoaderExtension));
  4333. Extensions.MSFT_lod = MSFT_lod;
  4334. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_lod(loader); });
  4335. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  4336. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  4337. })(BABYLON || (BABYLON = {}));
  4338. //# sourceMappingURL=MSFT_lod.js.map
  4339. "use strict";
  4340. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  4341. var __extends = (this && this.__extends) || (function () {
  4342. var extendStatics = Object.setPrototypeOf ||
  4343. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4344. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  4345. return function (d, b) {
  4346. extendStatics(d, b);
  4347. function __() { this.constructor = d; }
  4348. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  4349. };
  4350. })();
  4351. var BABYLON;
  4352. (function (BABYLON) {
  4353. var GLTF2;
  4354. (function (GLTF2) {
  4355. var Extensions;
  4356. (function (Extensions) {
  4357. // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression
  4358. var NAME = "KHR_draco_mesh_compression";
  4359. var KHR_draco_mesh_compression = /** @class */ (function (_super) {
  4360. __extends(KHR_draco_mesh_compression, _super);
  4361. function KHR_draco_mesh_compression(loader) {
  4362. var _this = _super.call(this, loader) || this;
  4363. _this.name = NAME;
  4364. _this._dracoCompression = null;
  4365. // Disable extension if decoder is not available.
  4366. if (!BABYLON.DracoCompression.DecoderUrl) {
  4367. _this.enabled = false;
  4368. }
  4369. return _this;
  4370. }
  4371. KHR_draco_mesh_compression.prototype.dispose = function () {
  4372. if (this._dracoCompression) {
  4373. this._dracoCompression.dispose();
  4374. }
  4375. _super.prototype.dispose.call(this);
  4376. };
  4377. KHR_draco_mesh_compression.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  4378. var _this = this;
  4379. return this._loadExtensionAsync(context, primitive, function (extensionContext, extension) {
  4380. if (primitive.mode != undefined) {
  4381. if (primitive.mode !== 5 /* TRIANGLE_STRIP */ &&
  4382. primitive.mode !== 4 /* TRIANGLES */) {
  4383. throw new Error(context + ": Unsupported mode " + primitive.mode);
  4384. }
  4385. // TODO: handle triangle strips
  4386. if (primitive.mode === 5 /* TRIANGLE_STRIP */) {
  4387. throw new Error(context + ": Mode " + primitive.mode + " is not currently supported");
  4388. }
  4389. }
  4390. var attributes = {};
  4391. var loadAttribute = function (name, kind) {
  4392. var uniqueId = extension.attributes[name];
  4393. if (uniqueId == undefined) {
  4394. return;
  4395. }
  4396. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  4397. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  4398. babylonMesh._delayInfo.push(kind);
  4399. }
  4400. attributes[kind] = uniqueId;
  4401. };
  4402. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  4403. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  4404. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  4405. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  4406. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  4407. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  4408. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  4409. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind);
  4410. var bufferView = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._loader._gltf.bufferViews, extension.bufferView);
  4411. return _this._loader._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  4412. try {
  4413. if (!_this._dracoCompression) {
  4414. _this._dracoCompression = new BABYLON.DracoCompression();
  4415. }
  4416. return _this._dracoCompression.decodeMeshAsync(data, attributes).then(function (babylonVertexData) {
  4417. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, _this._loader._babylonScene);
  4418. babylonVertexData.applyToGeometry(babylonGeometry);
  4419. return babylonGeometry;
  4420. });
  4421. }
  4422. catch (e) {
  4423. throw new Error(context + ": " + e.message);
  4424. }
  4425. });
  4426. });
  4427. };
  4428. return KHR_draco_mesh_compression;
  4429. }(GLTF2.GLTFLoaderExtension));
  4430. Extensions.KHR_draco_mesh_compression = KHR_draco_mesh_compression;
  4431. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_draco_mesh_compression(loader); });
  4432. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  4433. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  4434. })(BABYLON || (BABYLON = {}));
  4435. //# sourceMappingURL=KHR_draco_mesh_compression.js.map
  4436. "use strict";
  4437. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  4438. var __extends = (this && this.__extends) || (function () {
  4439. var extendStatics = Object.setPrototypeOf ||
  4440. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4441. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  4442. return function (d, b) {
  4443. extendStatics(d, b);
  4444. function __() { this.constructor = d; }
  4445. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  4446. };
  4447. })();
  4448. var BABYLON;
  4449. (function (BABYLON) {
  4450. var GLTF2;
  4451. (function (GLTF2) {
  4452. var Extensions;
  4453. (function (Extensions) {
  4454. // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness
  4455. var NAME = "KHR_materials_pbrSpecularGlossiness";
  4456. var KHR_materials_pbrSpecularGlossiness = /** @class */ (function (_super) {
  4457. __extends(KHR_materials_pbrSpecularGlossiness, _super);
  4458. function KHR_materials_pbrSpecularGlossiness() {
  4459. var _this = _super !== null && _super.apply(this, arguments) || this;
  4460. _this.name = NAME;
  4461. return _this;
  4462. }
  4463. KHR_materials_pbrSpecularGlossiness.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  4464. var _this = this;
  4465. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  4466. material._babylonData = material._babylonData || {};
  4467. var babylonData = material._babylonData[babylonDrawMode];
  4468. if (!babylonData) {
  4469. var promises = new Array();
  4470. var name_1 = material.name || "materialSG_" + material._index;
  4471. var babylonMaterial = _this._loader._createMaterial(BABYLON.PBRMaterial, name_1, babylonDrawMode);
  4472. promises.push(_this._loader._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  4473. promises.push(_this._loadSpecularGlossinessPropertiesAsync(extensionContext, material, extension, babylonMaterial));
  4474. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  4475. babylonData = {
  4476. material: babylonMaterial,
  4477. meshes: [],
  4478. loaded: Promise.all(promises).then(function () { })
  4479. };
  4480. material._babylonData[babylonDrawMode] = babylonData;
  4481. }
  4482. babylonData.meshes.push(babylonMesh);
  4483. assign(babylonData.material);
  4484. return babylonData.loaded;
  4485. });
  4486. };
  4487. KHR_materials_pbrSpecularGlossiness.prototype._loadSpecularGlossinessPropertiesAsync = function (context, material, properties, babylonMaterial) {
  4488. var promises = new Array();
  4489. if (properties.diffuseFactor) {
  4490. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.diffuseFactor);
  4491. babylonMaterial.alpha = properties.diffuseFactor[3];
  4492. }
  4493. else {
  4494. babylonMaterial.albedoColor = BABYLON.Color3.White();
  4495. }
  4496. babylonMaterial.reflectivityColor = properties.specularFactor ? BABYLON.Color3.FromArray(properties.specularFactor) : BABYLON.Color3.White();
  4497. babylonMaterial.microSurface = properties.glossinessFactor == undefined ? 1 : properties.glossinessFactor;
  4498. if (properties.diffuseTexture) {
  4499. promises.push(this._loader._loadTextureAsync(context + "/diffuseTexture", properties.diffuseTexture, function (texture) {
  4500. babylonMaterial.albedoTexture = texture;
  4501. }));
  4502. }
  4503. if (properties.specularGlossinessTexture) {
  4504. promises.push(this._loader._loadTextureAsync(context + "/specularGlossinessTexture", properties.specularGlossinessTexture, function (texture) {
  4505. babylonMaterial.reflectivityTexture = texture;
  4506. }));
  4507. babylonMaterial.reflectivityTexture.hasAlpha = true;
  4508. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  4509. }
  4510. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  4511. return Promise.all(promises).then(function () { });
  4512. };
  4513. return KHR_materials_pbrSpecularGlossiness;
  4514. }(GLTF2.GLTFLoaderExtension));
  4515. Extensions.KHR_materials_pbrSpecularGlossiness = KHR_materials_pbrSpecularGlossiness;
  4516. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_pbrSpecularGlossiness(loader); });
  4517. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  4518. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  4519. })(BABYLON || (BABYLON = {}));
  4520. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map
  4521. "use strict";
  4522. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  4523. var __extends = (this && this.__extends) || (function () {
  4524. var extendStatics = Object.setPrototypeOf ||
  4525. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4526. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  4527. return function (d, b) {
  4528. extendStatics(d, b);
  4529. function __() { this.constructor = d; }
  4530. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  4531. };
  4532. })();
  4533. var BABYLON;
  4534. (function (BABYLON) {
  4535. var GLTF2;
  4536. (function (GLTF2) {
  4537. var Extensions;
  4538. (function (Extensions) {
  4539. // https://github.com/donmccurdy/glTF/tree/feat-khr-materials-cmnConstant/extensions/2.0/Khronos/KHR_materials_unlit
  4540. var NAME = "KHR_materials_unlit";
  4541. var KHR_materials_unlit = /** @class */ (function (_super) {
  4542. __extends(KHR_materials_unlit, _super);
  4543. function KHR_materials_unlit() {
  4544. var _this = _super !== null && _super.apply(this, arguments) || this;
  4545. _this.name = NAME;
  4546. return _this;
  4547. }
  4548. KHR_materials_unlit.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  4549. var _this = this;
  4550. return this._loadExtensionAsync(context, material, function () {
  4551. material._babylonData = material._babylonData || {};
  4552. var babylonData = material._babylonData[babylonDrawMode];
  4553. if (!babylonData) {
  4554. var name_1 = material.name || "materialUnlit_" + material._index;
  4555. var babylonMaterial = _this._loader._createMaterial(BABYLON.PBRMaterial, name_1, babylonDrawMode);
  4556. babylonMaterial.unlit = true;
  4557. var promise = _this._loadUnlitPropertiesAsync(context, material, babylonMaterial);
  4558. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  4559. babylonData = {
  4560. material: babylonMaterial,
  4561. meshes: [],
  4562. loaded: promise
  4563. };
  4564. material._babylonData[babylonDrawMode] = babylonData;
  4565. }
  4566. babylonData.meshes.push(babylonMesh);
  4567. assign(babylonData.material);
  4568. return babylonData.loaded;
  4569. });
  4570. };
  4571. KHR_materials_unlit.prototype._loadUnlitPropertiesAsync = function (context, material, babylonMaterial) {
  4572. var promises = new Array();
  4573. // Ensure metallic workflow
  4574. babylonMaterial.metallic = 1;
  4575. babylonMaterial.roughness = 1;
  4576. var properties = material.pbrMetallicRoughness;
  4577. if (properties) {
  4578. if (properties.baseColorFactor) {
  4579. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  4580. babylonMaterial.alpha = properties.baseColorFactor[3];
  4581. }
  4582. else {
  4583. babylonMaterial.albedoColor = BABYLON.Color3.White();
  4584. }
  4585. if (properties.baseColorTexture) {
  4586. promises.push(this._loader._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  4587. babylonMaterial.albedoTexture = texture;
  4588. }));
  4589. }
  4590. }
  4591. if (material.doubleSided) {
  4592. babylonMaterial.backFaceCulling = false;
  4593. babylonMaterial.twoSidedLighting = true;
  4594. }
  4595. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  4596. return Promise.all(promises).then(function () { });
  4597. };
  4598. return KHR_materials_unlit;
  4599. }(GLTF2.GLTFLoaderExtension));
  4600. Extensions.KHR_materials_unlit = KHR_materials_unlit;
  4601. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_unlit(loader); });
  4602. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  4603. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  4604. })(BABYLON || (BABYLON = {}));
  4605. //# sourceMappingURL=KHR_materials_unlit.js.map
  4606. "use strict";
  4607. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  4608. var __extends = (this && this.__extends) || (function () {
  4609. var extendStatics = Object.setPrototypeOf ||
  4610. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4611. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  4612. return function (d, b) {
  4613. extendStatics(d, b);
  4614. function __() { this.constructor = d; }
  4615. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  4616. };
  4617. })();
  4618. var BABYLON;
  4619. (function (BABYLON) {
  4620. var GLTF2;
  4621. (function (GLTF2) {
  4622. var Extensions;
  4623. (function (Extensions) {
  4624. // https://github.com/MiiBond/glTF/tree/khr_lights_v1/extensions/Khronos/KHR_lights
  4625. var NAME = "KHR_lights";
  4626. var LightType;
  4627. (function (LightType) {
  4628. LightType["AMBIENT"] = "ambient";
  4629. LightType["DIRECTIONAL"] = "directional";
  4630. LightType["POINT"] = "point";
  4631. LightType["SPOT"] = "spot";
  4632. })(LightType || (LightType = {}));
  4633. var KHR_lights = /** @class */ (function (_super) {
  4634. __extends(KHR_lights, _super);
  4635. function KHR_lights() {
  4636. var _this = _super !== null && _super.apply(this, arguments) || this;
  4637. _this.name = NAME;
  4638. return _this;
  4639. }
  4640. KHR_lights.prototype._loadSceneAsync = function (context, scene) {
  4641. var _this = this;
  4642. return this._loadExtensionAsync(context, scene, function (extensionContext, extension) {
  4643. var promise = _this._loader._loadSceneAsync(extensionContext, scene);
  4644. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  4645. if (light.type !== LightType.AMBIENT) {
  4646. throw new Error(extensionContext + ": Only ambient lights are allowed on a scene");
  4647. }
  4648. _this._loader._babylonScene.ambientColor = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.Black();
  4649. return promise;
  4650. });
  4651. };
  4652. KHR_lights.prototype._loadNodeAsync = function (context, node) {
  4653. var _this = this;
  4654. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  4655. var promise = _this._loader._loadNodeAsync(extensionContext, node);
  4656. var babylonLight;
  4657. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  4658. var name = node._babylonMesh.name;
  4659. switch (light.type) {
  4660. case LightType.AMBIENT: {
  4661. throw new Error(extensionContext + ": Ambient lights are not allowed on a node");
  4662. }
  4663. case LightType.DIRECTIONAL: {
  4664. babylonLight = new BABYLON.DirectionalLight(name, BABYLON.Vector3.Forward(), _this._loader._babylonScene);
  4665. break;
  4666. }
  4667. case LightType.POINT: {
  4668. babylonLight = new BABYLON.PointLight(name, BABYLON.Vector3.Zero(), _this._loader._babylonScene);
  4669. break;
  4670. }
  4671. case LightType.SPOT: {
  4672. var spotLight = light;
  4673. // TODO: support inner and outer cone angles
  4674. //const innerConeAngle = spotLight.innerConeAngle || 0;
  4675. var outerConeAngle = spotLight.outerConeAngle || Math.PI / 4;
  4676. babylonLight = new BABYLON.SpotLight(name, BABYLON.Vector3.Zero(), BABYLON.Vector3.Forward(), outerConeAngle, 2, _this._loader._babylonScene);
  4677. break;
  4678. }
  4679. default: {
  4680. throw new Error(extensionContext + ": Invalid light type (" + light.type + ")");
  4681. }
  4682. }
  4683. babylonLight.diffuse = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.White();
  4684. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  4685. babylonLight.parent = node._babylonMesh;
  4686. return promise;
  4687. });
  4688. };
  4689. Object.defineProperty(KHR_lights.prototype, "_lights", {
  4690. get: function () {
  4691. var extensions = this._loader._gltf.extensions;
  4692. if (!extensions || !extensions[this.name]) {
  4693. throw new Error("#/extensions: '" + this.name + "' not found");
  4694. }
  4695. var extension = extensions[this.name];
  4696. return extension.lights;
  4697. },
  4698. enumerable: true,
  4699. configurable: true
  4700. });
  4701. return KHR_lights;
  4702. }(GLTF2.GLTFLoaderExtension));
  4703. Extensions.KHR_lights = KHR_lights;
  4704. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_lights(loader); });
  4705. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  4706. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  4707. })(BABYLON || (BABYLON = {}));
  4708. //# sourceMappingURL=KHR_lights.js.map