babylonjs.loaders.js 294 KB

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