babylon.glTF2FileLoader.js 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. /**
  5. * Mode that determines the coordinate system to use.
  6. */
  7. var GLTFLoaderCoordinateSystemMode;
  8. (function (GLTFLoaderCoordinateSystemMode) {
  9. /**
  10. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  11. */
  12. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
  13. /**
  14. * Sets the useRightHandedSystem flag on the scene.
  15. */
  16. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
  17. })(GLTFLoaderCoordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode || (BABYLON.GLTFLoaderCoordinateSystemMode = {}));
  18. /**
  19. * Mode that determines what animations will start.
  20. */
  21. var GLTFLoaderAnimationStartMode;
  22. (function (GLTFLoaderAnimationStartMode) {
  23. /**
  24. * No animation will start.
  25. */
  26. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
  27. /**
  28. * The first animation will start.
  29. */
  30. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
  31. /**
  32. * All animations will start.
  33. */
  34. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
  35. })(GLTFLoaderAnimationStartMode = BABYLON.GLTFLoaderAnimationStartMode || (BABYLON.GLTFLoaderAnimationStartMode = {}));
  36. /**
  37. * Loader state.
  38. */
  39. var GLTFLoaderState;
  40. (function (GLTFLoaderState) {
  41. /**
  42. * The asset is loading.
  43. */
  44. GLTFLoaderState[GLTFLoaderState["LOADING"] = 0] = "LOADING";
  45. /**
  46. * The asset is ready for rendering.
  47. */
  48. GLTFLoaderState[GLTFLoaderState["READY"] = 1] = "READY";
  49. /**
  50. * The asset is completely loaded.
  51. */
  52. GLTFLoaderState[GLTFLoaderState["COMPLETE"] = 2] = "COMPLETE";
  53. })(GLTFLoaderState = BABYLON.GLTFLoaderState || (BABYLON.GLTFLoaderState = {}));
  54. /**
  55. * File loader for loading glTF files into a scene.
  56. */
  57. var GLTFFileLoader = /** @class */ (function () {
  58. function GLTFFileLoader() {
  59. // #region Common options
  60. /**
  61. * Raised when the asset has been parsed
  62. */
  63. this.onParsedObservable = new BABYLON.Observable();
  64. // #endregion
  65. // #region V2 options
  66. /**
  67. * The coordinate system mode. Defaults to AUTO.
  68. */
  69. this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
  70. /**
  71. * The animation start mode. Defaults to FIRST.
  72. */
  73. this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
  74. /**
  75. * Defines if the loader should compile materials before raising the success callback. Defaults to false.
  76. */
  77. this.compileMaterials = false;
  78. /**
  79. * Defines if the loader should also compile materials with clip planes. Defaults to false.
  80. */
  81. this.useClipPlane = false;
  82. /**
  83. * Defines if the loader should compile shadow generators before raising the success callback. Defaults to false.
  84. */
  85. this.compileShadowGenerators = false;
  86. /**
  87. * Defines if the Alpha blended materials are only applied as coverage.
  88. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
  89. * If true, no extra effects are applied to transparent pixels.
  90. */
  91. this.transparencyAsCoverage = false;
  92. /** @hidden */
  93. this._normalizeAnimationGroupsToBeginAtZero = true;
  94. /**
  95. * Function called before loading a url referenced by the asset.
  96. */
  97. this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
  98. /**
  99. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  100. */
  101. this.onMeshLoadedObservable = new BABYLON.Observable();
  102. /**
  103. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  104. */
  105. this.onTextureLoadedObservable = new BABYLON.Observable();
  106. /**
  107. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  108. */
  109. this.onMaterialLoadedObservable = new BABYLON.Observable();
  110. /**
  111. * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
  112. */
  113. this.onCameraLoadedObservable = new BABYLON.Observable();
  114. /**
  115. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  116. * For assets with LODs, raised when all of the LODs are complete.
  117. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  118. */
  119. this.onCompleteObservable = new BABYLON.Observable();
  120. /**
  121. * Observable raised after the loader is disposed.
  122. */
  123. this.onDisposeObservable = new BABYLON.Observable();
  124. /**
  125. * Observable raised after a loader extension is created.
  126. * Set additional options for a loader extension in this event.
  127. */
  128. this.onExtensionLoadedObservable = new BABYLON.Observable();
  129. // #endregion
  130. this._loader = null;
  131. /**
  132. * Name of the loader ("gltf")
  133. */
  134. this.name = "gltf";
  135. /**
  136. * Supported file extensions of the loader (.gltf, .glb)
  137. */
  138. this.extensions = {
  139. ".gltf": { isBinary: false },
  140. ".glb": { isBinary: true }
  141. };
  142. }
  143. Object.defineProperty(GLTFFileLoader.prototype, "onParsed", {
  144. /**
  145. * Raised when the asset has been parsed
  146. */
  147. set: function (callback) {
  148. if (this._onParsedObserver) {
  149. this.onParsedObservable.remove(this._onParsedObserver);
  150. }
  151. this._onParsedObserver = this.onParsedObservable.add(callback);
  152. },
  153. enumerable: true,
  154. configurable: true
  155. });
  156. Object.defineProperty(GLTFFileLoader.prototype, "onMeshLoaded", {
  157. /**
  158. * Callback raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  159. */
  160. set: function (callback) {
  161. if (this._onMeshLoadedObserver) {
  162. this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver);
  163. }
  164. this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback);
  165. },
  166. enumerable: true,
  167. configurable: true
  168. });
  169. Object.defineProperty(GLTFFileLoader.prototype, "onTextureLoaded", {
  170. /**
  171. * Callback raised when the loader creates a texture after parsing the glTF properties of the texture.
  172. */
  173. set: function (callback) {
  174. if (this._onTextureLoadedObserver) {
  175. this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver);
  176. }
  177. this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback);
  178. },
  179. enumerable: true,
  180. configurable: true
  181. });
  182. Object.defineProperty(GLTFFileLoader.prototype, "onMaterialLoaded", {
  183. /**
  184. * Callback raised when the loader creates a material after parsing the glTF properties of the material.
  185. */
  186. set: function (callback) {
  187. if (this._onMaterialLoadedObserver) {
  188. this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver);
  189. }
  190. this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback);
  191. },
  192. enumerable: true,
  193. configurable: true
  194. });
  195. Object.defineProperty(GLTFFileLoader.prototype, "onCameraLoaded", {
  196. /**
  197. * Callback raised when the loader creates a camera after parsing the glTF properties of the camera.
  198. */
  199. set: function (callback) {
  200. if (this._onCameraLoadedObserver) {
  201. this.onCameraLoadedObservable.remove(this._onCameraLoadedObserver);
  202. }
  203. this._onCameraLoadedObserver = this.onCameraLoadedObservable.add(callback);
  204. },
  205. enumerable: true,
  206. configurable: true
  207. });
  208. Object.defineProperty(GLTFFileLoader.prototype, "onComplete", {
  209. /**
  210. * Callback raised when the asset is completely loaded, immediately before the loader is disposed.
  211. */
  212. set: function (callback) {
  213. if (this._onCompleteObserver) {
  214. this.onCompleteObservable.remove(this._onCompleteObserver);
  215. }
  216. this._onCompleteObserver = this.onCompleteObservable.add(callback);
  217. },
  218. enumerable: true,
  219. configurable: true
  220. });
  221. Object.defineProperty(GLTFFileLoader.prototype, "onDispose", {
  222. /**
  223. * Callback raised after the loader is disposed.
  224. */
  225. set: function (callback) {
  226. if (this._onDisposeObserver) {
  227. this.onDisposeObservable.remove(this._onDisposeObserver);
  228. }
  229. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  230. },
  231. enumerable: true,
  232. configurable: true
  233. });
  234. Object.defineProperty(GLTFFileLoader.prototype, "onExtensionLoaded", {
  235. /**
  236. * Callback raised after a loader extension is created.
  237. */
  238. set: function (callback) {
  239. if (this._onExtensionLoadedObserver) {
  240. this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver);
  241. }
  242. this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback);
  243. },
  244. enumerable: true,
  245. configurable: true
  246. });
  247. /**
  248. * Returns a promise that resolves when the asset is completely loaded.
  249. * @returns a promise that resolves when the asset is completely loaded.
  250. */
  251. GLTFFileLoader.prototype.whenCompleteAsync = function () {
  252. var _this = this;
  253. return new Promise(function (resolve) {
  254. _this.onCompleteObservable.addOnce(function () {
  255. resolve();
  256. });
  257. });
  258. };
  259. Object.defineProperty(GLTFFileLoader.prototype, "loaderState", {
  260. /**
  261. * The loader state or null if the loader is not active.
  262. */
  263. get: function () {
  264. return this._loader ? this._loader.state : null;
  265. },
  266. enumerable: true,
  267. configurable: true
  268. });
  269. /**
  270. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  271. */
  272. GLTFFileLoader.prototype.dispose = function () {
  273. if (this._loader) {
  274. this._loader.dispose();
  275. this._loader = null;
  276. }
  277. this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
  278. this.onMeshLoadedObservable.clear();
  279. this.onTextureLoadedObservable.clear();
  280. this.onMaterialLoadedObservable.clear();
  281. this.onCameraLoadedObservable.clear();
  282. this.onCompleteObservable.clear();
  283. this.onExtensionLoadedObservable.clear();
  284. this.onDisposeObservable.notifyObservers(this);
  285. this.onDisposeObservable.clear();
  286. };
  287. /**
  288. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  289. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  290. * @param scene the scene the meshes should be added to
  291. * @param data the glTF data to load
  292. * @param rootUrl root url to load from
  293. * @param onProgress event that fires when loading progress has occured
  294. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  295. */
  296. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  297. var _this = this;
  298. return Promise.resolve().then(function () {
  299. var loaderData = _this._parse(data);
  300. _this._loader = _this._getLoader(loaderData);
  301. return _this._loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onProgress);
  302. });
  303. };
  304. /**
  305. * Imports all objects from the loaded glTF data and adds them to the scene
  306. * @param scene the scene the objects should be added to
  307. * @param data the glTF data to load
  308. * @param rootUrl root url to load from
  309. * @param onProgress event that fires when loading progress has occured
  310. * @returns a promise which completes when objects have been loaded to the scene
  311. */
  312. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  313. var _this = this;
  314. return Promise.resolve().then(function () {
  315. var loaderData = _this._parse(data);
  316. _this._loader = _this._getLoader(loaderData);
  317. return _this._loader.loadAsync(scene, loaderData, rootUrl, onProgress);
  318. });
  319. };
  320. /**
  321. * Load into an asset container.
  322. * @param scene The scene to load into
  323. * @param data The data to import
  324. * @param rootUrl The root url for scene and resources
  325. * @param onProgress The callback when the load progresses
  326. * @returns The loaded asset container
  327. */
  328. GLTFFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress) {
  329. var _this = this;
  330. return Promise.resolve().then(function () {
  331. var loaderData = _this._parse(data);
  332. _this._loader = _this._getLoader(loaderData);
  333. return _this._loader.importMeshAsync(null, scene, loaderData, rootUrl, onProgress).then(function (result) {
  334. var container = new BABYLON.AssetContainer(scene);
  335. Array.prototype.push.apply(container.meshes, result.meshes);
  336. Array.prototype.push.apply(container.particleSystems, result.particleSystems);
  337. Array.prototype.push.apply(container.skeletons, result.skeletons);
  338. Array.prototype.push.apply(container.animationGroups, result.animationGroups);
  339. container.removeAllFromScene();
  340. return container;
  341. });
  342. });
  343. };
  344. /**
  345. * If the data string can be loaded directly.
  346. * @param data string contianing the file data
  347. * @returns if the data can be loaded directly
  348. */
  349. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  350. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  351. };
  352. /**
  353. * Instantiates a glTF file loader plugin.
  354. * @returns the created plugin
  355. */
  356. GLTFFileLoader.prototype.createPlugin = function () {
  357. return new GLTFFileLoader();
  358. };
  359. GLTFFileLoader.prototype._parse = function (data) {
  360. var parsedData;
  361. if (data instanceof ArrayBuffer) {
  362. parsedData = GLTFFileLoader._parseBinary(data);
  363. }
  364. else {
  365. parsedData = {
  366. json: JSON.parse(data),
  367. bin: null
  368. };
  369. }
  370. this.onParsedObservable.notifyObservers(parsedData);
  371. this.onParsedObservable.clear();
  372. return parsedData;
  373. };
  374. GLTFFileLoader.prototype._getLoader = function (loaderData) {
  375. var _this = this;
  376. var loaderVersion = { major: 2, minor: 0 };
  377. var asset = loaderData.json.asset || {};
  378. var version = GLTFFileLoader._parseVersion(asset.version);
  379. if (!version) {
  380. throw new Error("Invalid version: " + asset.version);
  381. }
  382. if (asset.minVersion !== undefined) {
  383. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  384. if (!minVersion) {
  385. throw new Error("Invalid minimum version: " + asset.minVersion);
  386. }
  387. if (GLTFFileLoader._compareVersion(minVersion, loaderVersion) > 0) {
  388. throw new Error("Incompatible minimum version: " + asset.minVersion);
  389. }
  390. }
  391. var createLoaders = {
  392. 1: GLTFFileLoader.CreateGLTFLoaderV1,
  393. 2: GLTFFileLoader.CreateGLTFLoaderV2
  394. };
  395. var createLoader = createLoaders[version.major];
  396. if (!createLoader) {
  397. throw new Error("Unsupported version: " + asset.version);
  398. }
  399. var loader = createLoader();
  400. loader.coordinateSystemMode = this.coordinateSystemMode;
  401. loader.animationStartMode = this.animationStartMode;
  402. loader.compileMaterials = this.compileMaterials;
  403. loader.useClipPlane = this.useClipPlane;
  404. loader.compileShadowGenerators = this.compileShadowGenerators;
  405. loader.transparencyAsCoverage = this.transparencyAsCoverage;
  406. loader._normalizeAnimationGroupsToBeginAtZero = this._normalizeAnimationGroupsToBeginAtZero;
  407. loader.preprocessUrlAsync = this.preprocessUrlAsync;
  408. loader.onMeshLoadedObservable.add(function (mesh) { return _this.onMeshLoadedObservable.notifyObservers(mesh); });
  409. loader.onTextureLoadedObservable.add(function (texture) { return _this.onTextureLoadedObservable.notifyObservers(texture); });
  410. loader.onMaterialLoadedObservable.add(function (material) { return _this.onMaterialLoadedObservable.notifyObservers(material); });
  411. loader.onCameraLoadedObservable.add(function (camera) { return _this.onCameraLoadedObservable.notifyObservers(camera); });
  412. loader.onExtensionLoadedObservable.add(function (extension) { return _this.onExtensionLoadedObservable.notifyObservers(extension); });
  413. loader.onCompleteObservable.add(function () {
  414. _this.onMeshLoadedObservable.clear();
  415. _this.onTextureLoadedObservable.clear();
  416. _this.onMaterialLoadedObservable.clear();
  417. _this.onCameraLoadedObservable.clear();
  418. _this.onExtensionLoadedObservable.clear();
  419. _this.onCompleteObservable.notifyObservers(_this);
  420. _this.onCompleteObservable.clear();
  421. });
  422. return loader;
  423. };
  424. GLTFFileLoader._parseBinary = function (data) {
  425. var Binary = {
  426. Magic: 0x46546C67
  427. };
  428. var binaryReader = new BinaryReader(data);
  429. var magic = binaryReader.readUint32();
  430. if (magic !== Binary.Magic) {
  431. throw new Error("Unexpected magic: " + magic);
  432. }
  433. var version = binaryReader.readUint32();
  434. switch (version) {
  435. case 1: return GLTFFileLoader._parseV1(binaryReader);
  436. case 2: return GLTFFileLoader._parseV2(binaryReader);
  437. }
  438. throw new Error("Unsupported version: " + version);
  439. };
  440. GLTFFileLoader._parseV1 = function (binaryReader) {
  441. var ContentFormat = {
  442. JSON: 0
  443. };
  444. var length = binaryReader.readUint32();
  445. if (length != binaryReader.getLength()) {
  446. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  447. }
  448. var contentLength = binaryReader.readUint32();
  449. var contentFormat = binaryReader.readUint32();
  450. var content;
  451. switch (contentFormat) {
  452. case ContentFormat.JSON: {
  453. content = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength)));
  454. break;
  455. }
  456. default: {
  457. throw new Error("Unexpected content format: " + contentFormat);
  458. }
  459. }
  460. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  461. var body = binaryReader.readUint8Array(bytesRemaining);
  462. return {
  463. json: content,
  464. bin: body
  465. };
  466. };
  467. GLTFFileLoader._parseV2 = function (binaryReader) {
  468. var ChunkFormat = {
  469. JSON: 0x4E4F534A,
  470. BIN: 0x004E4942
  471. };
  472. var length = binaryReader.readUint32();
  473. if (length !== binaryReader.getLength()) {
  474. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  475. }
  476. // JSON chunk
  477. var chunkLength = binaryReader.readUint32();
  478. var chunkFormat = binaryReader.readUint32();
  479. if (chunkFormat !== ChunkFormat.JSON) {
  480. throw new Error("First chunk format is not JSON");
  481. }
  482. var json = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength)));
  483. // Look for BIN chunk
  484. var bin = null;
  485. while (binaryReader.getPosition() < binaryReader.getLength()) {
  486. var chunkLength_1 = binaryReader.readUint32();
  487. var chunkFormat_1 = binaryReader.readUint32();
  488. switch (chunkFormat_1) {
  489. case ChunkFormat.JSON: {
  490. throw new Error("Unexpected JSON chunk");
  491. }
  492. case ChunkFormat.BIN: {
  493. bin = binaryReader.readUint8Array(chunkLength_1);
  494. break;
  495. }
  496. default: {
  497. // ignore unrecognized chunkFormat
  498. binaryReader.skipBytes(chunkLength_1);
  499. break;
  500. }
  501. }
  502. }
  503. return {
  504. json: json,
  505. bin: bin
  506. };
  507. };
  508. GLTFFileLoader._parseVersion = function (version) {
  509. if (version === "1.0" || version === "1.0.1") {
  510. return {
  511. major: 1,
  512. minor: 0
  513. };
  514. }
  515. var match = (version + "").match(/^(\d+)\.(\d+)/);
  516. if (!match) {
  517. return null;
  518. }
  519. return {
  520. major: parseInt(match[1]),
  521. minor: parseInt(match[2])
  522. };
  523. };
  524. GLTFFileLoader._compareVersion = function (a, b) {
  525. if (a.major > b.major)
  526. return 1;
  527. if (a.major < b.major)
  528. return -1;
  529. if (a.minor > b.minor)
  530. return 1;
  531. if (a.minor < b.minor)
  532. return -1;
  533. return 0;
  534. };
  535. GLTFFileLoader._decodeBufferToText = function (buffer) {
  536. var result = "";
  537. var length = buffer.byteLength;
  538. for (var i = 0; i < length; i++) {
  539. result += String.fromCharCode(buffer[i]);
  540. }
  541. return result;
  542. };
  543. // #endregion
  544. // #region V1 options
  545. /**
  546. * 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.
  547. * Textures always loads asynchronously. For example, the success callback can compute the bounding information of the loaded meshes when incremental loading is disabled.
  548. * Defaults to true.
  549. */
  550. GLTFFileLoader.IncrementalLoading = true;
  551. /**
  552. * Set this property to true in order to work with homogeneous coordinates, available with some converters and exporters.
  553. * Defaults to false. See https://en.wikipedia.org/wiki/Homogeneous_coordinates.
  554. */
  555. GLTFFileLoader.HomogeneousCoordinates = false;
  556. return GLTFFileLoader;
  557. }());
  558. BABYLON.GLTFFileLoader = GLTFFileLoader;
  559. var BinaryReader = /** @class */ (function () {
  560. function BinaryReader(arrayBuffer) {
  561. this._arrayBuffer = arrayBuffer;
  562. this._dataView = new DataView(arrayBuffer);
  563. this._byteOffset = 0;
  564. }
  565. BinaryReader.prototype.getPosition = function () {
  566. return this._byteOffset;
  567. };
  568. BinaryReader.prototype.getLength = function () {
  569. return this._arrayBuffer.byteLength;
  570. };
  571. BinaryReader.prototype.readUint32 = function () {
  572. var value = this._dataView.getUint32(this._byteOffset, true);
  573. this._byteOffset += 4;
  574. return value;
  575. };
  576. BinaryReader.prototype.readUint8Array = function (length) {
  577. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  578. this._byteOffset += length;
  579. return value;
  580. };
  581. BinaryReader.prototype.skipBytes = function (length) {
  582. this._byteOffset += length;
  583. };
  584. return BinaryReader;
  585. }());
  586. if (BABYLON.SceneLoader) {
  587. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  588. }
  589. })(BABYLON || (BABYLON = {}));
  590. //# sourceMappingURL=babylon.glTFFileLoader.js.map
  591. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  592. var BABYLON;
  593. (function (BABYLON) {
  594. var GLTF2;
  595. (function (GLTF2) {
  596. /** @hidden */
  597. var _ArrayItem = /** @class */ (function () {
  598. function _ArrayItem() {
  599. }
  600. /** @hidden */
  601. _ArrayItem.Assign = function (values) {
  602. if (values) {
  603. for (var index = 0; index < values.length; index++) {
  604. values[index]._index = index;
  605. }
  606. }
  607. };
  608. return _ArrayItem;
  609. }());
  610. GLTF2._ArrayItem = _ArrayItem;
  611. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  612. })(BABYLON || (BABYLON = {}));
  613. //# sourceMappingURL=babylon.glTFLoaderUtilities.js.map
  614. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  615. /// <reference path="../../../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"/>
  616. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  617. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  618. /**
  619. * Defines the module used to import/export glTF 2.0 assets
  620. */
  621. var BABYLON;
  622. (function (BABYLON) {
  623. var GLTF2;
  624. (function (GLTF2) {
  625. /**
  626. * Loader for loading a glTF 2.0 asset
  627. */
  628. var GLTFLoader = /** @class */ (function () {
  629. function GLTFLoader() {
  630. /** @hidden */
  631. this._completePromises = new Array();
  632. /** @hidden */
  633. this._onReadyObservable = new BABYLON.Observable();
  634. this._disposed = false;
  635. this._state = null;
  636. this._extensions = {};
  637. this._defaultSampler = {};
  638. this._defaultBabylonMaterials = {};
  639. this._requests = new Array();
  640. /**
  641. * Mode that determines the coordinate system to use.
  642. */
  643. this.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.AUTO;
  644. /**
  645. * Mode that determines what animations will start.
  646. */
  647. this.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.FIRST;
  648. /**
  649. * Defines if the loader should compile materials.
  650. */
  651. this.compileMaterials = false;
  652. /**
  653. * Defines if the loader should also compile materials with clip planes.
  654. */
  655. this.useClipPlane = false;
  656. /**
  657. * Defines if the loader should compile shadow generators.
  658. */
  659. this.compileShadowGenerators = false;
  660. /**
  661. * Defines if the Alpha blended materials are only applied as coverage.
  662. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
  663. * If true, no extra effects are applied to transparent pixels.
  664. */
  665. this.transparencyAsCoverage = false;
  666. /** @hidden */
  667. this._normalizeAnimationGroupsToBeginAtZero = true;
  668. /**
  669. * Function called before loading a url referenced by the asset.
  670. */
  671. this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
  672. /**
  673. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  674. */
  675. this.onMeshLoadedObservable = new BABYLON.Observable();
  676. /**
  677. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  678. */
  679. this.onTextureLoadedObservable = new BABYLON.Observable();
  680. /**
  681. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  682. */
  683. this.onMaterialLoadedObservable = new BABYLON.Observable();
  684. /**
  685. * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
  686. */
  687. this.onCameraLoadedObservable = new BABYLON.Observable();
  688. /**
  689. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  690. * For assets with LODs, raised when all of the LODs are complete.
  691. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  692. */
  693. this.onCompleteObservable = new BABYLON.Observable();
  694. /**
  695. * Observable raised after the loader is disposed.
  696. */
  697. this.onDisposeObservable = new BABYLON.Observable();
  698. /**
  699. * Observable raised after a loader extension is created.
  700. * Set additional options for a loader extension in this event.
  701. */
  702. this.onExtensionLoadedObservable = new BABYLON.Observable();
  703. }
  704. /** @hidden */
  705. GLTFLoader._Register = function (name, factory) {
  706. if (GLTFLoader._ExtensionFactories[name]) {
  707. BABYLON.Tools.Error("Extension with the name '" + name + "' already exists");
  708. return;
  709. }
  710. GLTFLoader._ExtensionFactories[name] = factory;
  711. // Keep the order of registration so that extensions registered first are called first.
  712. GLTFLoader._ExtensionNames.push(name);
  713. };
  714. Object.defineProperty(GLTFLoader.prototype, "state", {
  715. /**
  716. * Loader state or null if the loader is not active.
  717. */
  718. get: function () {
  719. return this._state;
  720. },
  721. enumerable: true,
  722. configurable: true
  723. });
  724. /**
  725. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  726. */
  727. GLTFLoader.prototype.dispose = function () {
  728. if (this._disposed) {
  729. return;
  730. }
  731. this._disposed = true;
  732. this.onDisposeObservable.notifyObservers(this);
  733. this.onDisposeObservable.clear();
  734. this._clear();
  735. };
  736. /**
  737. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  738. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  739. * @param scene the scene the meshes should be added to
  740. * @param data the glTF data to load
  741. * @param rootUrl root url to load from
  742. * @param onProgress event that fires when loading progress has occured
  743. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  744. */
  745. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  746. var _this = this;
  747. return Promise.resolve().then(function () {
  748. _this._babylonScene = scene;
  749. _this._rootUrl = rootUrl;
  750. _this._progressCallback = onProgress;
  751. _this._loadData(data);
  752. var nodes = null;
  753. if (meshesNames) {
  754. var nodeMap_1 = {};
  755. if (_this._gltf.nodes) {
  756. for (var _i = 0, _a = _this._gltf.nodes; _i < _a.length; _i++) {
  757. var node = _a[_i];
  758. if (node.name) {
  759. nodeMap_1[node.name] = node;
  760. }
  761. }
  762. }
  763. var names = (meshesNames instanceof Array) ? meshesNames : [meshesNames];
  764. nodes = names.map(function (name) {
  765. var node = nodeMap_1[name];
  766. if (!node) {
  767. throw new Error("Failed to find node '" + name + "'");
  768. }
  769. return node;
  770. });
  771. }
  772. return _this._loadAsync(nodes).then(function () {
  773. return {
  774. meshes: _this._getMeshes(),
  775. particleSystems: [],
  776. skeletons: _this._getSkeletons(),
  777. animationGroups: _this._getAnimationGroups()
  778. };
  779. });
  780. });
  781. };
  782. /**
  783. * Imports all objects from the loaded glTF data and adds them to the scene
  784. * @param scene the scene the objects should be added to
  785. * @param data the glTF data to load
  786. * @param rootUrl root url to load from
  787. * @param onProgress event that fires when loading progress has occured
  788. * @returns a promise which completes when objects have been loaded to the scene
  789. */
  790. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  791. var _this = this;
  792. return Promise.resolve().then(function () {
  793. _this._babylonScene = scene;
  794. _this._rootUrl = rootUrl;
  795. _this._progressCallback = onProgress;
  796. _this._loadData(data);
  797. return _this._loadAsync(null);
  798. });
  799. };
  800. GLTFLoader.prototype._loadAsync = function (nodes) {
  801. var _this = this;
  802. return Promise.resolve().then(function () {
  803. _this._state = BABYLON.GLTFLoaderState.LOADING;
  804. _this._loadExtensions();
  805. _this._checkExtensions();
  806. var promises = new Array();
  807. if (nodes) {
  808. promises.push(_this._loadNodesAsync(nodes));
  809. }
  810. else {
  811. var scene = GLTFLoader._GetProperty("#/scene", _this._gltf.scenes, _this._gltf.scene || 0);
  812. promises.push(_this._loadSceneAsync("#/scenes/" + scene._index, scene));
  813. }
  814. if (_this.compileMaterials) {
  815. promises.push(_this._compileMaterialsAsync());
  816. }
  817. if (_this.compileShadowGenerators) {
  818. promises.push(_this._compileShadowGeneratorsAsync());
  819. }
  820. var resultPromise = Promise.all(promises).then(function () {
  821. _this._state = BABYLON.GLTFLoaderState.READY;
  822. _this._onReadyObservable.notifyObservers(_this);
  823. _this._startAnimations();
  824. });
  825. resultPromise.then(function () {
  826. if (_this._rootBabylonMesh) {
  827. _this._rootBabylonMesh.setEnabled(true);
  828. }
  829. BABYLON.Tools.SetImmediate(function () {
  830. if (!_this._disposed) {
  831. Promise.all(_this._completePromises).then(function () {
  832. _this._state = BABYLON.GLTFLoaderState.COMPLETE;
  833. _this.onCompleteObservable.notifyObservers(_this);
  834. _this.onCompleteObservable.clear();
  835. _this._clear();
  836. }).catch(function (error) {
  837. BABYLON.Tools.Error("glTF Loader: " + error.message);
  838. _this._clear();
  839. });
  840. }
  841. });
  842. });
  843. return resultPromise;
  844. }).catch(function (error) {
  845. if (!_this._disposed) {
  846. BABYLON.Tools.Error("glTF Loader: " + error.message);
  847. _this._clear();
  848. throw error;
  849. }
  850. });
  851. };
  852. GLTFLoader.prototype._loadData = function (data) {
  853. this._gltf = data.json;
  854. this._setupData();
  855. if (data.bin) {
  856. var buffers = this._gltf.buffers;
  857. if (buffers && buffers[0] && !buffers[0].uri) {
  858. var binaryBuffer = buffers[0];
  859. if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
  860. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  861. }
  862. binaryBuffer._data = Promise.resolve(data.bin);
  863. }
  864. else {
  865. BABYLON.Tools.Warn("Unexpected BIN chunk");
  866. }
  867. }
  868. };
  869. GLTFLoader.prototype._setupData = function () {
  870. GLTF2._ArrayItem.Assign(this._gltf.accessors);
  871. GLTF2._ArrayItem.Assign(this._gltf.animations);
  872. GLTF2._ArrayItem.Assign(this._gltf.buffers);
  873. GLTF2._ArrayItem.Assign(this._gltf.bufferViews);
  874. GLTF2._ArrayItem.Assign(this._gltf.cameras);
  875. GLTF2._ArrayItem.Assign(this._gltf.images);
  876. GLTF2._ArrayItem.Assign(this._gltf.materials);
  877. GLTF2._ArrayItem.Assign(this._gltf.meshes);
  878. GLTF2._ArrayItem.Assign(this._gltf.nodes);
  879. GLTF2._ArrayItem.Assign(this._gltf.samplers);
  880. GLTF2._ArrayItem.Assign(this._gltf.scenes);
  881. GLTF2._ArrayItem.Assign(this._gltf.skins);
  882. GLTF2._ArrayItem.Assign(this._gltf.textures);
  883. if (this._gltf.nodes) {
  884. var nodeParents = {};
  885. for (var _i = 0, _a = this._gltf.nodes; _i < _a.length; _i++) {
  886. var node = _a[_i];
  887. if (node.children) {
  888. for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
  889. var index = _c[_b];
  890. nodeParents[index] = node._index;
  891. }
  892. }
  893. }
  894. var rootNode = this._createRootNode();
  895. for (var _d = 0, _e = this._gltf.nodes; _d < _e.length; _d++) {
  896. var node = _e[_d];
  897. var parentIndex = nodeParents[node._index];
  898. node._parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
  899. }
  900. }
  901. };
  902. GLTFLoader.prototype._loadExtensions = function () {
  903. for (var _i = 0, _a = GLTFLoader._ExtensionNames; _i < _a.length; _i++) {
  904. var name_1 = _a[_i];
  905. var extension = GLTFLoader._ExtensionFactories[name_1](this);
  906. this._extensions[name_1] = extension;
  907. this.onExtensionLoadedObservable.notifyObservers(extension);
  908. }
  909. this.onExtensionLoadedObservable.clear();
  910. };
  911. GLTFLoader.prototype._checkExtensions = function () {
  912. if (this._gltf.extensionsRequired) {
  913. for (var _i = 0, _a = this._gltf.extensionsRequired; _i < _a.length; _i++) {
  914. var name_2 = _a[_i];
  915. var extension = this._extensions[name_2];
  916. if (!extension || !extension.enabled) {
  917. throw new Error("Require extension " + name_2 + " is not available");
  918. }
  919. }
  920. }
  921. };
  922. GLTFLoader.prototype._createRootNode = function () {
  923. this._rootBabylonMesh = new BABYLON.Mesh("__root__", this._babylonScene);
  924. this._rootBabylonMesh.setEnabled(false);
  925. var rootNode = { _babylonMesh: this._rootBabylonMesh };
  926. switch (this.coordinateSystemMode) {
  927. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO: {
  928. if (!this._babylonScene.useRightHandedSystem) {
  929. rootNode.rotation = [0, 1, 0, 0];
  930. rootNode.scale = [1, 1, -1];
  931. GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
  932. }
  933. break;
  934. }
  935. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
  936. this._babylonScene.useRightHandedSystem = true;
  937. break;
  938. }
  939. default: {
  940. throw new Error("Invalid coordinate system mode (" + this.coordinateSystemMode + ")");
  941. }
  942. }
  943. this.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
  944. return rootNode;
  945. };
  946. GLTFLoader.prototype._loadNodesAsync = function (nodes) {
  947. var promises = new Array();
  948. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  949. var node = nodes_1[_i];
  950. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  951. }
  952. promises.push(this._loadAnimationsAsync());
  953. return Promise.all(promises).then(function () { });
  954. };
  955. /** @hidden */
  956. GLTFLoader.prototype._loadSceneAsync = function (context, scene) {
  957. var promise = GLTF2.GLTFLoaderExtension._LoadSceneAsync(this, context, scene);
  958. if (promise) {
  959. return promise;
  960. }
  961. var promises = new Array();
  962. for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
  963. var index = _a[_i];
  964. var node = GLTFLoader._GetProperty(context + "/nodes/" + index, this._gltf.nodes, index);
  965. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  966. }
  967. promises.push(this._loadAnimationsAsync());
  968. return Promise.all(promises).then(function () { });
  969. };
  970. GLTFLoader.prototype._forEachPrimitive = function (node, callback) {
  971. if (node._primitiveBabylonMeshes) {
  972. for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
  973. var babylonMesh = _a[_i];
  974. callback(babylonMesh);
  975. }
  976. }
  977. else {
  978. callback(node._babylonMesh);
  979. }
  980. };
  981. GLTFLoader.prototype._getMeshes = function () {
  982. var meshes = new Array();
  983. // Root mesh is always first.
  984. meshes.push(this._rootBabylonMesh);
  985. var nodes = this._gltf.nodes;
  986. if (nodes) {
  987. for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
  988. var node = nodes_2[_i];
  989. if (node._babylonMesh) {
  990. meshes.push(node._babylonMesh);
  991. }
  992. if (node._primitiveBabylonMeshes) {
  993. for (var _a = 0, _b = node._primitiveBabylonMeshes; _a < _b.length; _a++) {
  994. var babylonMesh = _b[_a];
  995. meshes.push(babylonMesh);
  996. }
  997. }
  998. }
  999. }
  1000. return meshes;
  1001. };
  1002. GLTFLoader.prototype._getSkeletons = function () {
  1003. var skeletons = new Array();
  1004. var skins = this._gltf.skins;
  1005. if (skins) {
  1006. for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
  1007. var skin = skins_1[_i];
  1008. if (skin._babylonSkeleton) {
  1009. skeletons.push(skin._babylonSkeleton);
  1010. }
  1011. }
  1012. }
  1013. return skeletons;
  1014. };
  1015. GLTFLoader.prototype._getAnimationGroups = function () {
  1016. var animationGroups = new Array();
  1017. var animations = this._gltf.animations;
  1018. if (animations) {
  1019. for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
  1020. var animation = animations_1[_i];
  1021. if (animation._babylonAnimationGroup) {
  1022. animationGroups.push(animation._babylonAnimationGroup);
  1023. }
  1024. }
  1025. }
  1026. return animationGroups;
  1027. };
  1028. GLTFLoader.prototype._startAnimations = function () {
  1029. switch (this.animationStartMode) {
  1030. case BABYLON.GLTFLoaderAnimationStartMode.NONE: {
  1031. // do nothing
  1032. break;
  1033. }
  1034. case BABYLON.GLTFLoaderAnimationStartMode.FIRST: {
  1035. var babylonAnimationGroups = this._getAnimationGroups();
  1036. if (babylonAnimationGroups.length !== 0) {
  1037. babylonAnimationGroups[0].start(true);
  1038. }
  1039. break;
  1040. }
  1041. case BABYLON.GLTFLoaderAnimationStartMode.ALL: {
  1042. var babylonAnimationGroups = this._getAnimationGroups();
  1043. for (var _i = 0, babylonAnimationGroups_1 = babylonAnimationGroups; _i < babylonAnimationGroups_1.length; _i++) {
  1044. var babylonAnimationGroup = babylonAnimationGroups_1[_i];
  1045. babylonAnimationGroup.start(true);
  1046. }
  1047. break;
  1048. }
  1049. default: {
  1050. BABYLON.Tools.Error("Invalid animation start mode (" + this.animationStartMode + ")");
  1051. return;
  1052. }
  1053. }
  1054. };
  1055. /** @hidden */
  1056. GLTFLoader.prototype._loadNodeAsync = function (context, node) {
  1057. var promise = GLTF2.GLTFLoaderExtension._LoadNodeAsync(this, context, node);
  1058. if (promise) {
  1059. return promise;
  1060. }
  1061. if (node._babylonMesh) {
  1062. throw new Error(context + ": Invalid recursive node hierarchy");
  1063. }
  1064. var promises = new Array();
  1065. var babylonMesh = new BABYLON.Mesh(node.name || "node" + node._index, this._babylonScene, node._parent._babylonMesh);
  1066. node._babylonMesh = babylonMesh;
  1067. GLTFLoader._LoadTransform(node, babylonMesh);
  1068. if (node.mesh != undefined) {
  1069. var mesh = GLTFLoader._GetProperty(context + "/mesh", this._gltf.meshes, node.mesh);
  1070. promises.push(this._loadMeshAsync("#/meshes/" + mesh._index, node, mesh, babylonMesh));
  1071. }
  1072. if (node.camera != undefined) {
  1073. var camera = GLTFLoader._GetProperty(context + "/camera", this._gltf.cameras, node.camera);
  1074. this._loadCamera("#/cameras/" + camera._index, camera, babylonMesh);
  1075. }
  1076. if (node.children) {
  1077. for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
  1078. var index = _a[_i];
  1079. var childNode = GLTFLoader._GetProperty(context + "/children/" + index, this._gltf.nodes, index);
  1080. promises.push(this._loadNodeAsync("#/nodes/" + index, childNode));
  1081. }
  1082. }
  1083. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  1084. return Promise.all(promises).then(function () { });
  1085. };
  1086. GLTFLoader.prototype._loadMeshAsync = function (context, node, mesh, babylonMesh) {
  1087. var _this = this;
  1088. var promises = new Array();
  1089. var primitives = mesh.primitives;
  1090. if (!primitives || primitives.length === 0) {
  1091. throw new Error(context + ": Primitives are missing");
  1092. }
  1093. GLTF2._ArrayItem.Assign(primitives);
  1094. if (primitives.length === 1) {
  1095. var primitive = primitives[0];
  1096. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, babylonMesh));
  1097. }
  1098. else {
  1099. node._primitiveBabylonMeshes = [];
  1100. for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
  1101. var primitive = primitives_1[_i];
  1102. var primitiveBabylonMesh = new BABYLON.Mesh((mesh.name || babylonMesh.name) + "_" + primitive._index, this._babylonScene, babylonMesh);
  1103. node._primitiveBabylonMeshes.push(primitiveBabylonMesh);
  1104. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, primitiveBabylonMesh));
  1105. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  1106. }
  1107. }
  1108. if (node.skin != undefined) {
  1109. var skin = GLTFLoader._GetProperty(context + "/skin", this._gltf.skins, node.skin);
  1110. promises.push(this._loadSkinAsync("#/skins/" + skin._index, node, mesh, skin));
  1111. }
  1112. return Promise.all(promises).then(function () {
  1113. _this._forEachPrimitive(node, function (babylonMesh) {
  1114. babylonMesh._refreshBoundingInfo(true);
  1115. });
  1116. });
  1117. };
  1118. GLTFLoader.prototype._loadPrimitiveAsync = function (context, node, mesh, primitive, babylonMesh) {
  1119. var _this = this;
  1120. var promises = new Array();
  1121. this._createMorphTargets(context, node, mesh, primitive, babylonMesh);
  1122. promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh).then(function (babylonGeometry) {
  1123. return _this._loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry).then(function () {
  1124. babylonGeometry.applyToMesh(babylonMesh);
  1125. });
  1126. }));
  1127. var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
  1128. if (primitive.material == undefined) {
  1129. babylonMesh.material = this._getDefaultMaterial(babylonDrawMode);
  1130. }
  1131. else {
  1132. var material = GLTFLoader._GetProperty(context + "/material}", this._gltf.materials, primitive.material);
  1133. promises.push(this._loadMaterialAsync("#/materials/" + material._index, material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
  1134. babylonMesh.material = babylonMaterial;
  1135. }));
  1136. }
  1137. return Promise.all(promises).then(function () { });
  1138. };
  1139. GLTFLoader.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  1140. var _this = this;
  1141. var promise = GLTF2.GLTFLoaderExtension._LoadVertexDataAsync(this, context, primitive, babylonMesh);
  1142. if (promise) {
  1143. return promise;
  1144. }
  1145. var attributes = primitive.attributes;
  1146. if (!attributes) {
  1147. throw new Error(context + ": Attributes are missing");
  1148. }
  1149. var promises = new Array();
  1150. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, this._babylonScene);
  1151. if (primitive.indices == undefined) {
  1152. babylonMesh.isUnIndexed = true;
  1153. }
  1154. else {
  1155. var accessor = GLTFLoader._GetProperty(context + "/indices", this._gltf.accessors, primitive.indices);
  1156. promises.push(this._loadIndicesAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1157. babylonGeometry.setIndices(data);
  1158. }));
  1159. }
  1160. var loadAttribute = function (attribute, kind, callback) {
  1161. if (attributes[attribute] == undefined) {
  1162. return;
  1163. }
  1164. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  1165. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  1166. babylonMesh._delayInfo.push(kind);
  1167. }
  1168. var accessor = GLTFLoader._GetProperty(context + "/attributes/" + attribute, _this._gltf.accessors, attributes[attribute]);
  1169. promises.push(_this._loadVertexAccessorAsync("#/accessors/" + accessor._index, accessor, kind).then(function (babylonVertexBuffer) {
  1170. babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
  1171. }));
  1172. if (callback) {
  1173. callback(accessor);
  1174. }
  1175. };
  1176. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  1177. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  1178. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  1179. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  1180. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  1181. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  1182. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  1183. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind, function (accessor) {
  1184. if (accessor.type === "VEC4" /* VEC4 */) {
  1185. babylonMesh.hasVertexAlpha = true;
  1186. }
  1187. });
  1188. return Promise.all(promises).then(function () {
  1189. return babylonGeometry;
  1190. });
  1191. };
  1192. GLTFLoader.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
  1193. if (!primitive.targets) {
  1194. return;
  1195. }
  1196. if (node._numMorphTargets == undefined) {
  1197. node._numMorphTargets = primitive.targets.length;
  1198. }
  1199. else if (primitive.targets.length !== node._numMorphTargets) {
  1200. throw new Error(context + ": Primitives do not have the same number of targets");
  1201. }
  1202. babylonMesh.morphTargetManager = new BABYLON.MorphTargetManager();
  1203. for (var index = 0; index < primitive.targets.length; index++) {
  1204. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  1205. babylonMesh.morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  1206. // TODO: tell the target whether it has positions, normals, tangents
  1207. }
  1208. };
  1209. GLTFLoader.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonGeometry) {
  1210. if (!primitive.targets) {
  1211. return Promise.resolve();
  1212. }
  1213. var promises = new Array();
  1214. var morphTargetManager = babylonMesh.morphTargetManager;
  1215. for (var index = 0; index < morphTargetManager.numTargets; index++) {
  1216. var babylonMorphTarget = morphTargetManager.getTarget(index);
  1217. promises.push(this._loadMorphTargetVertexDataAsync(context + "/targets/" + index, babylonGeometry, primitive.targets[index], babylonMorphTarget));
  1218. }
  1219. return Promise.all(promises).then(function () { });
  1220. };
  1221. GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, babylonGeometry, attributes, babylonMorphTarget) {
  1222. var _this = this;
  1223. var promises = new Array();
  1224. var loadAttribute = function (attribute, kind, setData) {
  1225. if (attributes[attribute] == undefined) {
  1226. return;
  1227. }
  1228. var babylonVertexBuffer = babylonGeometry.getVertexBuffer(kind);
  1229. if (!babylonVertexBuffer) {
  1230. return;
  1231. }
  1232. var accessor = GLTFLoader._GetProperty(context + "/" + attribute, _this._gltf.accessors, attributes[attribute]);
  1233. promises.push(_this._loadFloatAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1234. setData(babylonVertexBuffer, data);
  1235. }));
  1236. };
  1237. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind, function (babylonVertexBuffer, data) {
  1238. babylonVertexBuffer.forEach(data.length, function (value, index) {
  1239. data[index] += value;
  1240. });
  1241. babylonMorphTarget.setPositions(data);
  1242. });
  1243. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind, function (babylonVertexBuffer, data) {
  1244. babylonVertexBuffer.forEach(data.length, function (value, index) {
  1245. data[index] += value;
  1246. });
  1247. babylonMorphTarget.setNormals(data);
  1248. });
  1249. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind, function (babylonVertexBuffer, data) {
  1250. var dataIndex = 0;
  1251. babylonVertexBuffer.forEach(data.length / 3 * 4, function (value, index) {
  1252. // Tangent data for morph targets is stored as xyz delta.
  1253. // The vertexData.tangent is stored as xyzw.
  1254. // So we need to skip every fourth vertexData.tangent.
  1255. if (((index + 1) % 4) !== 0) {
  1256. data[dataIndex++] += value;
  1257. }
  1258. });
  1259. babylonMorphTarget.setTangents(data);
  1260. });
  1261. return Promise.all(promises).then(function () { });
  1262. };
  1263. GLTFLoader._LoadTransform = function (node, babylonNode) {
  1264. var position = BABYLON.Vector3.Zero();
  1265. var rotation = BABYLON.Quaternion.Identity();
  1266. var scaling = BABYLON.Vector3.One();
  1267. if (node.matrix) {
  1268. var matrix = BABYLON.Matrix.FromArray(node.matrix);
  1269. matrix.decompose(scaling, rotation, position);
  1270. }
  1271. else {
  1272. if (node.translation)
  1273. position = BABYLON.Vector3.FromArray(node.translation);
  1274. if (node.rotation)
  1275. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  1276. if (node.scale)
  1277. scaling = BABYLON.Vector3.FromArray(node.scale);
  1278. }
  1279. babylonNode.position = position;
  1280. babylonNode.rotationQuaternion = rotation;
  1281. babylonNode.scaling = scaling;
  1282. };
  1283. GLTFLoader.prototype._loadSkinAsync = function (context, node, mesh, skin) {
  1284. var _this = this;
  1285. var assignSkeleton = function (skeleton) {
  1286. _this._forEachPrimitive(node, function (babylonMesh) {
  1287. babylonMesh.skeleton = skeleton;
  1288. });
  1289. // Ignore the TRS of skinned nodes.
  1290. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  1291. node._babylonMesh.parent = _this._rootBabylonMesh;
  1292. node._babylonMesh.position = BABYLON.Vector3.Zero();
  1293. node._babylonMesh.rotationQuaternion = BABYLON.Quaternion.Identity();
  1294. node._babylonMesh.scaling = BABYLON.Vector3.One();
  1295. };
  1296. if (skin._loaded) {
  1297. return skin._loaded.then(function () {
  1298. assignSkeleton(skin._babylonSkeleton);
  1299. });
  1300. }
  1301. var skeletonId = "skeleton" + skin._index;
  1302. var babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
  1303. skin._babylonSkeleton = babylonSkeleton;
  1304. this._loadBones(context, skin);
  1305. assignSkeleton(babylonSkeleton);
  1306. return (skin._loaded = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
  1307. _this._updateBoneMatrices(babylonSkeleton, inverseBindMatricesData);
  1308. }));
  1309. };
  1310. GLTFLoader.prototype._loadBones = function (context, skin) {
  1311. var babylonBones = {};
  1312. for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
  1313. var index = _a[_i];
  1314. var node = GLTFLoader._GetProperty(context + "/joints/" + index, this._gltf.nodes, index);
  1315. this._loadBone(node, skin, babylonBones);
  1316. }
  1317. };
  1318. GLTFLoader.prototype._loadBone = function (node, skin, babylonBones) {
  1319. var babylonBone = babylonBones[node._index];
  1320. if (babylonBone) {
  1321. return babylonBone;
  1322. }
  1323. var babylonParentBone = null;
  1324. if (node._parent._babylonMesh !== this._rootBabylonMesh) {
  1325. babylonParentBone = this._loadBone(node._parent, skin, babylonBones);
  1326. }
  1327. var boneIndex = skin.joints.indexOf(node._index);
  1328. babylonBone = new BABYLON.Bone(node.name || "joint" + node._index, skin._babylonSkeleton, babylonParentBone, this._getNodeMatrix(node), null, null, boneIndex);
  1329. babylonBones[node._index] = babylonBone;
  1330. node._babylonBones = node._babylonBones || [];
  1331. node._babylonBones.push(babylonBone);
  1332. return babylonBone;
  1333. };
  1334. GLTFLoader.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
  1335. if (skin.inverseBindMatrices == undefined) {
  1336. return Promise.resolve(null);
  1337. }
  1338. var accessor = GLTFLoader._GetProperty(context + "/inverseBindMatrices", this._gltf.accessors, skin.inverseBindMatrices);
  1339. return this._loadFloatAccessorAsync("#/accessors/" + accessor._index, accessor);
  1340. };
  1341. GLTFLoader.prototype._updateBoneMatrices = function (babylonSkeleton, inverseBindMatricesData) {
  1342. for (var _i = 0, _a = babylonSkeleton.bones; _i < _a.length; _i++) {
  1343. var babylonBone = _a[_i];
  1344. var baseMatrix = BABYLON.Matrix.Identity();
  1345. var boneIndex = babylonBone._index;
  1346. if (inverseBindMatricesData && boneIndex !== -1) {
  1347. BABYLON.Matrix.FromArrayToRef(inverseBindMatricesData, boneIndex * 16, baseMatrix);
  1348. baseMatrix.invertToRef(baseMatrix);
  1349. }
  1350. var babylonParentBone = babylonBone.getParent();
  1351. if (babylonParentBone) {
  1352. baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
  1353. }
  1354. babylonBone.updateMatrix(baseMatrix, false, false);
  1355. babylonBone._updateDifferenceMatrix(undefined, false);
  1356. }
  1357. };
  1358. GLTFLoader.prototype._getNodeMatrix = function (node) {
  1359. return node.matrix ?
  1360. BABYLON.Matrix.FromArray(node.matrix) :
  1361. 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());
  1362. };
  1363. GLTFLoader.prototype._loadCamera = function (context, camera, babylonMesh) {
  1364. var babylonCamera = new BABYLON.FreeCamera(camera.name || "camera" + camera._index, BABYLON.Vector3.Zero(), this._babylonScene, false);
  1365. babylonCamera.parent = babylonMesh;
  1366. babylonCamera.rotation = new BABYLON.Vector3(0, Math.PI, 0);
  1367. switch (camera.type) {
  1368. case "perspective" /* PERSPECTIVE */: {
  1369. var perspective = camera.perspective;
  1370. if (!perspective) {
  1371. throw new Error(context + ": Camera perspective properties are missing");
  1372. }
  1373. babylonCamera.fov = perspective.yfov;
  1374. babylonCamera.minZ = perspective.znear;
  1375. babylonCamera.maxZ = perspective.zfar || Number.MAX_VALUE;
  1376. break;
  1377. }
  1378. case "orthographic" /* ORTHOGRAPHIC */: {
  1379. if (!camera.orthographic) {
  1380. throw new Error(context + ": Camera orthographic properties are missing");
  1381. }
  1382. babylonCamera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
  1383. babylonCamera.orthoLeft = -camera.orthographic.xmag;
  1384. babylonCamera.orthoRight = camera.orthographic.xmag;
  1385. babylonCamera.orthoBottom = -camera.orthographic.ymag;
  1386. babylonCamera.orthoTop = camera.orthographic.ymag;
  1387. babylonCamera.minZ = camera.orthographic.znear;
  1388. babylonCamera.maxZ = camera.orthographic.zfar;
  1389. break;
  1390. }
  1391. default: {
  1392. throw new Error(context + ": Invalid camera type (" + camera.type + ")");
  1393. }
  1394. }
  1395. this.onCameraLoadedObservable.notifyObservers(babylonCamera);
  1396. };
  1397. GLTFLoader.prototype._loadAnimationsAsync = function () {
  1398. var animations = this._gltf.animations;
  1399. if (!animations) {
  1400. return Promise.resolve();
  1401. }
  1402. var promises = new Array();
  1403. for (var index = 0; index < animations.length; index++) {
  1404. var animation = animations[index];
  1405. promises.push(this._loadAnimationAsync("#/animations/" + index, animation));
  1406. }
  1407. return Promise.all(promises).then(function () { });
  1408. };
  1409. GLTFLoader.prototype._loadAnimationAsync = function (context, animation) {
  1410. var _this = this;
  1411. var babylonAnimationGroup = new BABYLON.AnimationGroup(animation.name || "animation" + animation._index, this._babylonScene);
  1412. animation._babylonAnimationGroup = babylonAnimationGroup;
  1413. var promises = new Array();
  1414. GLTF2._ArrayItem.Assign(animation.channels);
  1415. GLTF2._ArrayItem.Assign(animation.samplers);
  1416. for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
  1417. var channel = _a[_i];
  1418. promises.push(this._loadAnimationChannelAsync(context + "/channels/" + channel._index, context, animation, channel, babylonAnimationGroup));
  1419. }
  1420. return Promise.all(promises).then(function () {
  1421. babylonAnimationGroup.normalize(_this._normalizeAnimationGroupsToBeginAtZero ? 0 : null);
  1422. });
  1423. };
  1424. GLTFLoader.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup) {
  1425. var _this = this;
  1426. var targetNode = GLTFLoader._GetProperty(context + "/target/node", this._gltf.nodes, channel.target.node);
  1427. // Ignore animations that have no animation targets.
  1428. if ((channel.target.path === "weights" /* WEIGHTS */ && !targetNode._numMorphTargets) ||
  1429. (channel.target.path !== "weights" /* WEIGHTS */ && !targetNode._babylonMesh)) {
  1430. return Promise.resolve();
  1431. }
  1432. // Ignore animations targeting TRS of skinned nodes.
  1433. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  1434. if (targetNode.skin != undefined && channel.target.path !== "weights" /* WEIGHTS */) {
  1435. return Promise.resolve();
  1436. }
  1437. var sampler = GLTFLoader._GetProperty(context + "/sampler", animation.samplers, channel.sampler);
  1438. return this._loadAnimationSamplerAsync(animationContext + "/samplers/" + channel.sampler, sampler).then(function (data) {
  1439. var targetPath;
  1440. var animationType;
  1441. switch (channel.target.path) {
  1442. case "translation" /* TRANSLATION */: {
  1443. targetPath = "position";
  1444. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1445. break;
  1446. }
  1447. case "rotation" /* ROTATION */: {
  1448. targetPath = "rotationQuaternion";
  1449. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  1450. break;
  1451. }
  1452. case "scale" /* SCALE */: {
  1453. targetPath = "scaling";
  1454. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1455. break;
  1456. }
  1457. case "weights" /* WEIGHTS */: {
  1458. targetPath = "influence";
  1459. animationType = BABYLON.Animation.ANIMATIONTYPE_FLOAT;
  1460. break;
  1461. }
  1462. default: {
  1463. throw new Error(context + ": Invalid target path (" + channel.target.path + ")");
  1464. }
  1465. }
  1466. var outputBufferOffset = 0;
  1467. var getNextOutputValue;
  1468. switch (targetPath) {
  1469. case "position": {
  1470. getNextOutputValue = function () {
  1471. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1472. outputBufferOffset += 3;
  1473. return value;
  1474. };
  1475. break;
  1476. }
  1477. case "rotationQuaternion": {
  1478. getNextOutputValue = function () {
  1479. var value = BABYLON.Quaternion.FromArray(data.output, outputBufferOffset);
  1480. outputBufferOffset += 4;
  1481. return value;
  1482. };
  1483. break;
  1484. }
  1485. case "scaling": {
  1486. getNextOutputValue = function () {
  1487. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1488. outputBufferOffset += 3;
  1489. return value;
  1490. };
  1491. break;
  1492. }
  1493. case "influence": {
  1494. getNextOutputValue = function () {
  1495. var value = new Array(targetNode._numMorphTargets);
  1496. for (var i = 0; i < targetNode._numMorphTargets; i++) {
  1497. value[i] = data.output[outputBufferOffset++];
  1498. }
  1499. return value;
  1500. };
  1501. break;
  1502. }
  1503. }
  1504. var getNextKey;
  1505. switch (data.interpolation) {
  1506. case "STEP" /* STEP */: {
  1507. getNextKey = function (frameIndex) { return ({
  1508. frame: data.input[frameIndex],
  1509. value: getNextOutputValue(),
  1510. interpolation: BABYLON.AnimationKeyInterpolation.STEP
  1511. }); };
  1512. break;
  1513. }
  1514. case "LINEAR" /* LINEAR */: {
  1515. getNextKey = function (frameIndex) { return ({
  1516. frame: data.input[frameIndex],
  1517. value: getNextOutputValue()
  1518. }); };
  1519. break;
  1520. }
  1521. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1522. getNextKey = function (frameIndex) { return ({
  1523. frame: data.input[frameIndex],
  1524. inTangent: getNextOutputValue(),
  1525. value: getNextOutputValue(),
  1526. outTangent: getNextOutputValue()
  1527. }); };
  1528. break;
  1529. }
  1530. }
  1531. var keys = new Array(data.input.length);
  1532. for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
  1533. keys[frameIndex] = getNextKey(frameIndex);
  1534. }
  1535. if (targetPath === "influence") {
  1536. var _loop_1 = function (targetIndex) {
  1537. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1538. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1539. babylonAnimation.setKeys(keys.map(function (key) { return ({
  1540. frame: key.frame,
  1541. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  1542. value: key.value[targetIndex],
  1543. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  1544. }); }));
  1545. _this._forEachPrimitive(targetNode, function (babylonMesh) {
  1546. var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
  1547. var babylonAnimationClone = babylonAnimation.clone();
  1548. morphTarget.animations.push(babylonAnimationClone);
  1549. babylonAnimationGroup.addTargetedAnimation(babylonAnimationClone, morphTarget);
  1550. });
  1551. };
  1552. for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
  1553. _loop_1(targetIndex);
  1554. }
  1555. }
  1556. else {
  1557. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1558. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1559. babylonAnimation.setKeys(keys);
  1560. if (targetNode._babylonBones) {
  1561. var babylonAnimationTargets = [targetNode._babylonMesh].concat(targetNode._babylonBones);
  1562. for (var _i = 0, babylonAnimationTargets_1 = babylonAnimationTargets; _i < babylonAnimationTargets_1.length; _i++) {
  1563. var babylonAnimationTarget = babylonAnimationTargets_1[_i];
  1564. babylonAnimationTarget.animations.push(babylonAnimation);
  1565. }
  1566. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, babylonAnimationTargets);
  1567. }
  1568. else {
  1569. targetNode._babylonMesh.animations.push(babylonAnimation);
  1570. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, targetNode._babylonMesh);
  1571. }
  1572. }
  1573. });
  1574. };
  1575. GLTFLoader.prototype._loadAnimationSamplerAsync = function (context, sampler) {
  1576. if (sampler._data) {
  1577. return sampler._data;
  1578. }
  1579. var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
  1580. switch (interpolation) {
  1581. case "STEP" /* STEP */:
  1582. case "LINEAR" /* LINEAR */:
  1583. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1584. break;
  1585. }
  1586. default: {
  1587. throw new Error(context + ": Invalid interpolation (" + sampler.interpolation + ")");
  1588. }
  1589. }
  1590. var inputAccessor = GLTFLoader._GetProperty(context + "/input", this._gltf.accessors, sampler.input);
  1591. var outputAccessor = GLTFLoader._GetProperty(context + "/output", this._gltf.accessors, sampler.output);
  1592. sampler._data = Promise.all([
  1593. this._loadFloatAccessorAsync("#/accessors/" + inputAccessor._index, inputAccessor),
  1594. this._loadFloatAccessorAsync("#/accessors/" + outputAccessor._index, outputAccessor)
  1595. ]).then(function (_a) {
  1596. var inputData = _a[0], outputData = _a[1];
  1597. return {
  1598. input: inputData,
  1599. interpolation: interpolation,
  1600. output: outputData,
  1601. };
  1602. });
  1603. return sampler._data;
  1604. };
  1605. GLTFLoader.prototype._loadBufferAsync = function (context, buffer) {
  1606. if (buffer._data) {
  1607. return buffer._data;
  1608. }
  1609. if (!buffer.uri) {
  1610. throw new Error(context + ": Uri is missing");
  1611. }
  1612. buffer._data = this._loadUriAsync(context, buffer.uri);
  1613. return buffer._data;
  1614. };
  1615. /** @hidden */
  1616. GLTFLoader.prototype._loadBufferViewAsync = function (context, bufferView) {
  1617. if (bufferView._data) {
  1618. return bufferView._data;
  1619. }
  1620. var buffer = GLTFLoader._GetProperty(context + "/buffer", this._gltf.buffers, bufferView.buffer);
  1621. bufferView._data = this._loadBufferAsync("#/buffers/" + buffer._index, buffer).then(function (data) {
  1622. try {
  1623. return new Uint8Array(data.buffer, data.byteOffset + (bufferView.byteOffset || 0), bufferView.byteLength);
  1624. }
  1625. catch (e) {
  1626. throw new Error(context + ": " + e.message);
  1627. }
  1628. });
  1629. return bufferView._data;
  1630. };
  1631. GLTFLoader.prototype._loadIndicesAccessorAsync = function (context, accessor) {
  1632. if (accessor.type !== "SCALAR" /* SCALAR */) {
  1633. throw new Error(context + ": Invalid type " + accessor.type);
  1634. }
  1635. if (accessor.componentType !== 5121 /* UNSIGNED_BYTE */ &&
  1636. accessor.componentType !== 5123 /* UNSIGNED_SHORT */ &&
  1637. accessor.componentType !== 5125 /* UNSIGNED_INT */) {
  1638. throw new Error(context + ": Invalid component type " + accessor.componentType);
  1639. }
  1640. if (accessor._data) {
  1641. return accessor._data;
  1642. }
  1643. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1644. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  1645. return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, accessor.count);
  1646. });
  1647. return accessor._data;
  1648. };
  1649. GLTFLoader.prototype._loadFloatAccessorAsync = function (context, accessor) {
  1650. // TODO: support normalized and stride
  1651. var _this = this;
  1652. if (accessor.componentType !== 5126 /* FLOAT */) {
  1653. throw new Error("Invalid component type " + accessor.componentType);
  1654. }
  1655. if (accessor._data) {
  1656. return accessor._data;
  1657. }
  1658. var numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
  1659. var length = numComponents * accessor.count;
  1660. if (accessor.bufferView == undefined) {
  1661. accessor._data = Promise.resolve(new Float32Array(length));
  1662. }
  1663. else {
  1664. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1665. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  1666. return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, length);
  1667. });
  1668. }
  1669. if (accessor.sparse) {
  1670. var sparse_1 = accessor.sparse;
  1671. accessor._data = accessor._data.then(function (data) {
  1672. var indicesBufferView = GLTFLoader._GetProperty(context + "/sparse/indices/bufferView", _this._gltf.bufferViews, sparse_1.indices.bufferView);
  1673. var valuesBufferView = GLTFLoader._GetProperty(context + "/sparse/values/bufferView", _this._gltf.bufferViews, sparse_1.values.bufferView);
  1674. return Promise.all([
  1675. _this._loadBufferViewAsync("#/bufferViews/" + indicesBufferView._index, indicesBufferView),
  1676. _this._loadBufferViewAsync("#/bufferViews/" + valuesBufferView._index, valuesBufferView)
  1677. ]).then(function (_a) {
  1678. var indicesData = _a[0], valuesData = _a[1];
  1679. var indices = GLTFLoader._GetTypedArray(context + "/sparse/indices", sparse_1.indices.componentType, indicesData, sparse_1.indices.byteOffset, sparse_1.count);
  1680. var values = GLTFLoader._GetTypedArray(context + "/sparse/values", accessor.componentType, valuesData, sparse_1.values.byteOffset, numComponents * sparse_1.count);
  1681. var valuesIndex = 0;
  1682. for (var indicesIndex = 0; indicesIndex < indices.length; indicesIndex++) {
  1683. var dataIndex = indices[indicesIndex] * numComponents;
  1684. for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
  1685. data[dataIndex++] = values[valuesIndex++];
  1686. }
  1687. }
  1688. return data;
  1689. });
  1690. });
  1691. }
  1692. return accessor._data;
  1693. };
  1694. /** @hidden */
  1695. GLTFLoader.prototype._loadVertexBufferViewAsync = function (context, bufferView, kind) {
  1696. var _this = this;
  1697. if (bufferView._babylonBuffer) {
  1698. return bufferView._babylonBuffer;
  1699. }
  1700. bufferView._babylonBuffer = this._loadBufferViewAsync(context, bufferView).then(function (data) {
  1701. return new BABYLON.Buffer(_this._babylonScene.getEngine(), data, false);
  1702. });
  1703. return bufferView._babylonBuffer;
  1704. };
  1705. GLTFLoader.prototype._loadVertexAccessorAsync = function (context, accessor, kind) {
  1706. var _this = this;
  1707. if (accessor._babylonVertexBuffer) {
  1708. return accessor._babylonVertexBuffer;
  1709. }
  1710. if (accessor.sparse) {
  1711. accessor._babylonVertexBuffer = this._loadFloatAccessorAsync(context, accessor).then(function (data) {
  1712. return new BABYLON.VertexBuffer(_this._babylonScene.getEngine(), data, kind, false);
  1713. });
  1714. }
  1715. else {
  1716. var bufferView_1 = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1717. accessor._babylonVertexBuffer = this._loadVertexBufferViewAsync("#/bufferViews/" + bufferView_1._index, bufferView_1, kind).then(function (buffer) {
  1718. var size = GLTFLoader._GetNumComponents(context, accessor.type);
  1719. return new BABYLON.VertexBuffer(_this._babylonScene.getEngine(), buffer, kind, false, false, bufferView_1.byteStride, false, accessor.byteOffset, size, accessor.componentType, accessor.normalized, true);
  1720. });
  1721. }
  1722. return accessor._babylonVertexBuffer;
  1723. };
  1724. GLTFLoader.prototype._getDefaultMaterial = function (drawMode) {
  1725. var babylonMaterial = this._defaultBabylonMaterials[drawMode];
  1726. if (!babylonMaterial) {
  1727. babylonMaterial = this._createMaterial("__gltf_default", drawMode);
  1728. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1729. babylonMaterial.metallic = 1;
  1730. babylonMaterial.roughness = 1;
  1731. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1732. }
  1733. return babylonMaterial;
  1734. };
  1735. GLTFLoader.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, material, babylonMaterial) {
  1736. var promises = new Array();
  1737. // Ensure metallic workflow
  1738. babylonMaterial.metallic = 1;
  1739. babylonMaterial.roughness = 1;
  1740. var properties = material.pbrMetallicRoughness;
  1741. if (properties) {
  1742. if (properties.baseColorFactor) {
  1743. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  1744. babylonMaterial.alpha = properties.baseColorFactor[3];
  1745. }
  1746. else {
  1747. babylonMaterial.albedoColor = BABYLON.Color3.White();
  1748. }
  1749. babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
  1750. babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
  1751. if (properties.baseColorTexture) {
  1752. promises.push(this._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  1753. babylonMaterial.albedoTexture = texture;
  1754. }));
  1755. }
  1756. if (properties.metallicRoughnessTexture) {
  1757. promises.push(this._loadTextureAsync(context + "/metallicRoughnessTexture", properties.metallicRoughnessTexture, function (texture) {
  1758. babylonMaterial.metallicTexture = texture;
  1759. }));
  1760. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  1761. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  1762. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  1763. }
  1764. }
  1765. this._loadMaterialAlphaProperties(context, material, babylonMaterial);
  1766. return Promise.all(promises).then(function () { });
  1767. };
  1768. /** @hidden */
  1769. GLTFLoader.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  1770. var promise = GLTF2.GLTFLoaderExtension._LoadMaterialAsync(this, context, material, babylonMesh, babylonDrawMode, assign);
  1771. if (promise) {
  1772. return promise;
  1773. }
  1774. material._babylonData = material._babylonData || {};
  1775. var babylonData = material._babylonData[babylonDrawMode];
  1776. if (!babylonData) {
  1777. var promises = new Array();
  1778. var name_3 = material.name || "materialSG_" + material._index;
  1779. var babylonMaterial = this._createMaterial(name_3, babylonDrawMode);
  1780. promises.push(this._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  1781. promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync(context, material, babylonMaterial));
  1782. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1783. babylonData = {
  1784. material: babylonMaterial,
  1785. meshes: [],
  1786. loaded: Promise.all(promises).then(function () { })
  1787. };
  1788. material._babylonData[babylonDrawMode] = babylonData;
  1789. }
  1790. babylonData.meshes.push(babylonMesh);
  1791. assign(babylonData.material);
  1792. return babylonData.loaded;
  1793. };
  1794. /** @hidden */
  1795. GLTFLoader.prototype._createMaterial = function (name, drawMode) {
  1796. var babylonMaterial = new BABYLON.PBRMaterial(name, this._babylonScene);
  1797. babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? BABYLON.Material.CounterClockWiseSideOrientation : BABYLON.Material.ClockWiseSideOrientation;
  1798. babylonMaterial.fillMode = drawMode;
  1799. babylonMaterial.enableSpecularAntiAliasing = true;
  1800. babylonMaterial.useRadianceOverAlpha = !this.transparencyAsCoverage;
  1801. babylonMaterial.useSpecularOverAlpha = !this.transparencyAsCoverage;
  1802. return babylonMaterial;
  1803. };
  1804. /** @hidden */
  1805. GLTFLoader.prototype._loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
  1806. var promises = new Array();
  1807. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  1808. if (material.doubleSided) {
  1809. babylonMaterial.backFaceCulling = false;
  1810. babylonMaterial.twoSidedLighting = true;
  1811. }
  1812. if (material.normalTexture) {
  1813. promises.push(this._loadTextureAsync(context + "/normalTexture", material.normalTexture, function (texture) {
  1814. babylonMaterial.bumpTexture = texture;
  1815. }));
  1816. babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
  1817. babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
  1818. if (material.normalTexture.scale != undefined) {
  1819. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  1820. }
  1821. }
  1822. if (material.occlusionTexture) {
  1823. promises.push(this._loadTextureAsync(context + "/occlusionTexture", material.occlusionTexture, function (texture) {
  1824. babylonMaterial.ambientTexture = texture;
  1825. }));
  1826. babylonMaterial.useAmbientInGrayScale = true;
  1827. if (material.occlusionTexture.strength != undefined) {
  1828. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  1829. }
  1830. }
  1831. if (material.emissiveTexture) {
  1832. promises.push(this._loadTextureAsync(context + "/emissiveTexture", material.emissiveTexture, function (texture) {
  1833. babylonMaterial.emissiveTexture = texture;
  1834. }));
  1835. }
  1836. return Promise.all(promises).then(function () { });
  1837. };
  1838. /** @hidden */
  1839. GLTFLoader.prototype._loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
  1840. var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
  1841. switch (alphaMode) {
  1842. case "OPAQUE" /* OPAQUE */: {
  1843. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1844. break;
  1845. }
  1846. case "MASK" /* MASK */: {
  1847. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  1848. babylonMaterial.alphaCutOff = (material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff);
  1849. if (babylonMaterial.albedoTexture) {
  1850. babylonMaterial.albedoTexture.hasAlpha = true;
  1851. }
  1852. break;
  1853. }
  1854. case "BLEND" /* BLEND */: {
  1855. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  1856. if (babylonMaterial.albedoTexture) {
  1857. babylonMaterial.albedoTexture.hasAlpha = true;
  1858. babylonMaterial.useAlphaFromAlbedoTexture = true;
  1859. }
  1860. break;
  1861. }
  1862. default: {
  1863. throw new Error(context + ": Invalid alpha mode (" + material.alphaMode + ")");
  1864. }
  1865. }
  1866. };
  1867. /** @hidden */
  1868. GLTFLoader.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  1869. var _this = this;
  1870. var promise = GLTF2.GLTFLoaderExtension._LoadTextureAsync(this, context, textureInfo, assign);
  1871. if (promise) {
  1872. return promise;
  1873. }
  1874. var texture = GLTFLoader._GetProperty(context + "/index", this._gltf.textures, textureInfo.index);
  1875. context = "#/textures/" + textureInfo.index;
  1876. var promises = new Array();
  1877. var sampler = (texture.sampler == undefined ? this._defaultSampler : GLTFLoader._GetProperty(context + "/sampler", this._gltf.samplers, texture.sampler));
  1878. var samplerData = this._loadSampler("#/samplers/" + sampler._index, sampler);
  1879. var deferred = new BABYLON.Deferred();
  1880. var babylonTexture = new BABYLON.Texture(null, this._babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, function () {
  1881. if (!_this._disposed) {
  1882. deferred.resolve();
  1883. }
  1884. }, function (message, exception) {
  1885. if (!_this._disposed) {
  1886. deferred.reject(new Error(context + ": " + ((exception && exception.message) ? exception.message : message || "Failed to load texture")));
  1887. }
  1888. });
  1889. promises.push(deferred.promise);
  1890. babylonTexture.name = texture.name || "texture" + texture._index;
  1891. babylonTexture.wrapU = samplerData.wrapU;
  1892. babylonTexture.wrapV = samplerData.wrapV;
  1893. babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
  1894. var image = GLTFLoader._GetProperty(context + "/source", this._gltf.images, texture.source);
  1895. promises.push(this._loadImageAsync("#/images/" + image._index, image).then(function (blob) {
  1896. var dataUrl = "data:" + _this._rootUrl + (image.uri || "image" + image._index);
  1897. babylonTexture.updateURL(dataUrl, blob);
  1898. }));
  1899. assign(babylonTexture);
  1900. this.onTextureLoadedObservable.notifyObservers(babylonTexture);
  1901. return Promise.all(promises).then(function () { });
  1902. };
  1903. GLTFLoader.prototype._loadSampler = function (context, sampler) {
  1904. if (!sampler._data) {
  1905. sampler._data = {
  1906. noMipMaps: (sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */),
  1907. samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler.magFilter, sampler.minFilter),
  1908. wrapU: GLTFLoader._GetTextureWrapMode(context, sampler.wrapS),
  1909. wrapV: GLTFLoader._GetTextureWrapMode(context, sampler.wrapT)
  1910. };
  1911. }
  1912. ;
  1913. return sampler._data;
  1914. };
  1915. GLTFLoader.prototype._loadImageAsync = function (context, image) {
  1916. if (image._blob) {
  1917. return image._blob;
  1918. }
  1919. var promise;
  1920. if (image.uri) {
  1921. promise = this._loadUriAsync(context, image.uri);
  1922. }
  1923. else {
  1924. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, image.bufferView);
  1925. promise = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView);
  1926. }
  1927. image._blob = promise.then(function (data) {
  1928. return new Blob([data], { type: image.mimeType });
  1929. });
  1930. return image._blob;
  1931. };
  1932. /** @hidden */
  1933. GLTFLoader.prototype._loadUriAsync = function (context, uri) {
  1934. var _this = this;
  1935. var promise = GLTF2.GLTFLoaderExtension._LoadUriAsync(this, context, uri);
  1936. if (promise) {
  1937. return promise;
  1938. }
  1939. if (!GLTFLoader._ValidateUri(uri)) {
  1940. throw new Error(context + ": Uri '" + uri + "' is invalid");
  1941. }
  1942. if (BABYLON.Tools.IsBase64(uri)) {
  1943. return Promise.resolve(new Uint8Array(BABYLON.Tools.DecodeBase64(uri)));
  1944. }
  1945. return this.preprocessUrlAsync(this._rootUrl + uri).then(function (url) {
  1946. return new Promise(function (resolve, reject) {
  1947. if (!_this._disposed) {
  1948. var request_1 = BABYLON.Tools.LoadFile(url, function (data) {
  1949. if (!_this._disposed) {
  1950. resolve(new Uint8Array(data));
  1951. }
  1952. }, function (event) {
  1953. if (!_this._disposed) {
  1954. try {
  1955. if (request_1 && _this._state === BABYLON.GLTFLoaderState.LOADING) {
  1956. request_1._lengthComputable = event.lengthComputable;
  1957. request_1._loaded = event.loaded;
  1958. request_1._total = event.total;
  1959. _this._onProgress();
  1960. }
  1961. }
  1962. catch (e) {
  1963. reject(e);
  1964. }
  1965. }
  1966. }, _this._babylonScene.database, true, function (request, exception) {
  1967. if (!_this._disposed) {
  1968. reject(new BABYLON.LoadFileError(context + ": Failed to load '" + uri + "'" + (request ? ": " + request.status + " " + request.statusText : ""), request));
  1969. }
  1970. });
  1971. _this._requests.push(request_1);
  1972. }
  1973. });
  1974. });
  1975. };
  1976. GLTFLoader.prototype._onProgress = function () {
  1977. if (!this._progressCallback) {
  1978. return;
  1979. }
  1980. var lengthComputable = true;
  1981. var loaded = 0;
  1982. var total = 0;
  1983. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  1984. var request = _a[_i];
  1985. if (request._lengthComputable === undefined || request._loaded === undefined || request._total === undefined) {
  1986. return;
  1987. }
  1988. lengthComputable = lengthComputable && request._lengthComputable;
  1989. loaded += request._loaded;
  1990. total += request._total;
  1991. }
  1992. this._progressCallback(new BABYLON.SceneLoaderProgressEvent(lengthComputable, loaded, lengthComputable ? total : 0));
  1993. };
  1994. /** @hidden */
  1995. GLTFLoader._GetProperty = function (context, array, index) {
  1996. if (!array || index == undefined || !array[index]) {
  1997. throw new Error(context + ": Failed to find index (" + index + ")");
  1998. }
  1999. return array[index];
  2000. };
  2001. GLTFLoader._GetTextureWrapMode = function (context, mode) {
  2002. // Set defaults if undefined
  2003. mode = mode == undefined ? 10497 /* REPEAT */ : mode;
  2004. switch (mode) {
  2005. case 33071 /* CLAMP_TO_EDGE */: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  2006. case 33648 /* MIRRORED_REPEAT */: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  2007. case 10497 /* REPEAT */: return BABYLON.Texture.WRAP_ADDRESSMODE;
  2008. default:
  2009. BABYLON.Tools.Warn(context + ": Invalid texture wrap mode (" + mode + ")");
  2010. return BABYLON.Texture.WRAP_ADDRESSMODE;
  2011. }
  2012. };
  2013. GLTFLoader._GetTextureSamplingMode = function (context, magFilter, minFilter) {
  2014. // Set defaults if undefined
  2015. magFilter = magFilter == undefined ? 9729 /* LINEAR */ : magFilter;
  2016. minFilter = minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : minFilter;
  2017. if (magFilter === 9729 /* LINEAR */) {
  2018. switch (minFilter) {
  2019. case 9728 /* NEAREST */: return BABYLON.Texture.LINEAR_NEAREST;
  2020. case 9729 /* LINEAR */: return BABYLON.Texture.LINEAR_LINEAR;
  2021. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_NEAREST_MIPNEAREST;
  2022. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_LINEAR_MIPNEAREST;
  2023. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_NEAREST_MIPLINEAR;
  2024. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  2025. default:
  2026. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  2027. return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  2028. }
  2029. }
  2030. else {
  2031. if (magFilter !== 9728 /* NEAREST */) {
  2032. BABYLON.Tools.Warn(context + ": Invalid texture magnification filter (" + magFilter + ")");
  2033. }
  2034. switch (minFilter) {
  2035. case 9728 /* NEAREST */: return BABYLON.Texture.NEAREST_NEAREST;
  2036. case 9729 /* LINEAR */: return BABYLON.Texture.NEAREST_LINEAR;
  2037. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  2038. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_LINEAR_MIPNEAREST;
  2039. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_NEAREST_MIPLINEAR;
  2040. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_LINEAR_MIPLINEAR;
  2041. default:
  2042. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  2043. return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  2044. }
  2045. }
  2046. };
  2047. GLTFLoader._GetTypedArray = function (context, componentType, bufferView, byteOffset, length) {
  2048. var buffer = bufferView.buffer;
  2049. byteOffset = bufferView.byteOffset + (byteOffset || 0);
  2050. try {
  2051. switch (componentType) {
  2052. case 5120 /* BYTE */: return new Int8Array(buffer, byteOffset, length);
  2053. case 5121 /* UNSIGNED_BYTE */: return new Uint8Array(buffer, byteOffset, length);
  2054. case 5122 /* SHORT */: return new Int16Array(buffer, byteOffset, length);
  2055. case 5123 /* UNSIGNED_SHORT */: return new Uint16Array(buffer, byteOffset, length);
  2056. case 5125 /* UNSIGNED_INT */: return new Uint32Array(buffer, byteOffset, length);
  2057. case 5126 /* FLOAT */: return new Float32Array(buffer, byteOffset, length);
  2058. default: throw new Error("Invalid component type " + componentType);
  2059. }
  2060. }
  2061. catch (e) {
  2062. throw new Error(context + ": " + e);
  2063. }
  2064. };
  2065. GLTFLoader._GetNumComponents = function (context, type) {
  2066. switch (type) {
  2067. case "SCALAR": return 1;
  2068. case "VEC2": return 2;
  2069. case "VEC3": return 3;
  2070. case "VEC4": return 4;
  2071. case "MAT2": return 4;
  2072. case "MAT3": return 9;
  2073. case "MAT4": return 16;
  2074. }
  2075. throw new Error(context + ": Invalid type (" + type + ")");
  2076. };
  2077. GLTFLoader._ValidateUri = function (uri) {
  2078. return (BABYLON.Tools.IsBase64(uri) || uri.indexOf("..") === -1);
  2079. };
  2080. GLTFLoader._GetDrawMode = function (context, mode) {
  2081. if (mode == undefined) {
  2082. mode = 4 /* TRIANGLES */;
  2083. }
  2084. switch (mode) {
  2085. case 0 /* POINTS */: return BABYLON.Material.PointListDrawMode;
  2086. case 1 /* LINES */: return BABYLON.Material.LineListDrawMode;
  2087. case 2 /* LINE_LOOP */: return BABYLON.Material.LineLoopDrawMode;
  2088. case 3 /* LINE_STRIP */: return BABYLON.Material.LineStripDrawMode;
  2089. case 4 /* TRIANGLES */: return BABYLON.Material.TriangleFillMode;
  2090. case 5 /* TRIANGLE_STRIP */: return BABYLON.Material.TriangleStripDrawMode;
  2091. case 6 /* TRIANGLE_FAN */: return BABYLON.Material.TriangleFanDrawMode;
  2092. }
  2093. throw new Error(context + ": Invalid mesh primitive mode (" + mode + ")");
  2094. };
  2095. GLTFLoader.prototype._compileMaterialsAsync = function () {
  2096. var promises = new Array();
  2097. if (this._gltf.materials) {
  2098. for (var _i = 0, _a = this._gltf.materials; _i < _a.length; _i++) {
  2099. var material = _a[_i];
  2100. if (material._babylonData) {
  2101. for (var babylonDrawMode in material._babylonData) {
  2102. var babylonData = material._babylonData[babylonDrawMode];
  2103. for (var _b = 0, _c = babylonData.meshes; _b < _c.length; _b++) {
  2104. var babylonMesh = _c[_b];
  2105. // Ensure nonUniformScaling is set if necessary.
  2106. babylonMesh.computeWorldMatrix(true);
  2107. var babylonMaterial = babylonData.material;
  2108. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
  2109. if (this.useClipPlane) {
  2110. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
  2111. }
  2112. }
  2113. }
  2114. }
  2115. }
  2116. }
  2117. return Promise.all(promises).then(function () { });
  2118. };
  2119. GLTFLoader.prototype._compileShadowGeneratorsAsync = function () {
  2120. var promises = new Array();
  2121. var lights = this._babylonScene.lights;
  2122. for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
  2123. var light = lights_1[_i];
  2124. var generator = light.getShadowGenerator();
  2125. if (generator) {
  2126. promises.push(generator.forceCompilationAsync());
  2127. }
  2128. }
  2129. return Promise.all(promises).then(function () { });
  2130. };
  2131. GLTFLoader.prototype._clear = function () {
  2132. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  2133. var request = _a[_i];
  2134. request.abort();
  2135. }
  2136. this._requests.length = 0;
  2137. delete this._gltf;
  2138. delete this._babylonScene;
  2139. this._completePromises.length = 0;
  2140. this._onReadyObservable.clear();
  2141. for (var name_4 in this._extensions) {
  2142. this._extensions[name_4].dispose();
  2143. }
  2144. this._extensions = {};
  2145. delete this._rootBabylonMesh;
  2146. delete this._progressCallback;
  2147. this.onMeshLoadedObservable.clear();
  2148. this.onTextureLoadedObservable.clear();
  2149. this.onMaterialLoadedObservable.clear();
  2150. this.onCameraLoadedObservable.clear();
  2151. };
  2152. /** @hidden */
  2153. GLTFLoader.prototype._applyExtensions = function (actionAsync) {
  2154. for (var _i = 0, _a = GLTFLoader._ExtensionNames; _i < _a.length; _i++) {
  2155. var name_5 = _a[_i];
  2156. var extension = this._extensions[name_5];
  2157. if (extension.enabled) {
  2158. var promise = actionAsync(extension);
  2159. if (promise) {
  2160. return promise;
  2161. }
  2162. }
  2163. }
  2164. return null;
  2165. };
  2166. GLTFLoader._ExtensionNames = new Array();
  2167. GLTFLoader._ExtensionFactories = {};
  2168. return GLTFLoader;
  2169. }());
  2170. GLTF2.GLTFLoader = GLTFLoader;
  2171. BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = function () { return new GLTFLoader(); };
  2172. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2173. })(BABYLON || (BABYLON = {}));
  2174. //# sourceMappingURL=babylon.glTFLoader.js.map
  2175. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2176. var BABYLON;
  2177. (function (BABYLON) {
  2178. var GLTF2;
  2179. (function (GLTF2) {
  2180. /**
  2181. * Abstract class that can be implemented to extend existing glTF loader behavior.
  2182. */
  2183. var GLTFLoaderExtension = /** @class */ (function () {
  2184. /**
  2185. * Creates new GLTFLoaderExtension
  2186. * @param loader defines the GLTFLoader to use
  2187. */
  2188. function GLTFLoaderExtension(loader) {
  2189. /** Gets or sets a boolean indicating if the extension is enabled */
  2190. this.enabled = true;
  2191. this._loader = loader;
  2192. }
  2193. /** Release all resources */
  2194. GLTFLoaderExtension.prototype.dispose = function () {
  2195. delete this._loader;
  2196. };
  2197. // #region Overridable Methods
  2198. /**
  2199. * Override this method to modify the default behavior for loading scenes.
  2200. * @hidden
  2201. */
  2202. GLTFLoaderExtension.prototype._loadSceneAsync = function (context, node) { return null; };
  2203. /**
  2204. * Override this method to modify the default behavior for loading nodes.
  2205. * @hidden
  2206. */
  2207. GLTFLoaderExtension.prototype._loadNodeAsync = function (context, node) { return null; };
  2208. /** Override this method to modify the default behavior for loading mesh primitive vertex data. */
  2209. GLTFLoaderExtension.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) { return null; };
  2210. /** Override this method to modify the default behavior for loading materials.
  2211. * @hidden
  2212. */
  2213. GLTFLoaderExtension.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) { return null; };
  2214. /**
  2215. * Override this method to modify the default behavior for loading textures.
  2216. * @hidden
  2217. */
  2218. GLTFLoaderExtension.prototype._loadTextureAsync = function (context, textureInfo, assign) { return null; };
  2219. /**
  2220. * Override this method to modify the default behavior for loading uris.
  2221. * @hidden
  2222. */
  2223. GLTFLoaderExtension.prototype._loadUriAsync = function (context, uri) { return null; };
  2224. // #endregion
  2225. /**
  2226. * Helper method called by a loader extension to load an glTF extension.
  2227. * @hidden
  2228. */
  2229. GLTFLoaderExtension.prototype._loadExtensionAsync = function (context, property, actionAsync) {
  2230. if (!property.extensions) {
  2231. return null;
  2232. }
  2233. var extensions = property.extensions;
  2234. var extension = extensions[this.name];
  2235. if (!extension) {
  2236. return null;
  2237. }
  2238. // Clear out the extension before executing the action to avoid recursing into the same property.
  2239. delete extensions[this.name];
  2240. try {
  2241. return actionAsync(context + "/extensions/" + this.name, extension);
  2242. }
  2243. finally {
  2244. // Restore the extension after executing the action.
  2245. extensions[this.name] = extension;
  2246. }
  2247. };
  2248. /**
  2249. * Helper method called by the loader to allow extensions to override loading scenes.
  2250. * @hidden
  2251. */
  2252. GLTFLoaderExtension._LoadSceneAsync = function (loader, context, scene) {
  2253. return loader._applyExtensions(function (extension) { return extension._loadSceneAsync(context, scene); });
  2254. };
  2255. /**
  2256. * Helper method called by the loader to allow extensions to override loading nodes.
  2257. * @hidden
  2258. */
  2259. GLTFLoaderExtension._LoadNodeAsync = function (loader, context, node) {
  2260. return loader._applyExtensions(function (extension) { return extension._loadNodeAsync(context, node); });
  2261. };
  2262. /**
  2263. * Helper method called by the loader to allow extensions to override loading mesh primitive vertex data.
  2264. * @hidden
  2265. */
  2266. GLTFLoaderExtension._LoadVertexDataAsync = function (loader, context, primitive, babylonMesh) {
  2267. return loader._applyExtensions(function (extension) { return extension._loadVertexDataAsync(context, primitive, babylonMesh); });
  2268. };
  2269. /**
  2270. * Helper method called by the loader to allow extensions to override loading materials.
  2271. * @hidden
  2272. */
  2273. GLTFLoaderExtension._LoadMaterialAsync = function (loader, context, material, babylonMesh, babylonDrawMode, assign) {
  2274. return loader._applyExtensions(function (extension) { return extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
  2275. };
  2276. /**
  2277. * Helper method called by the loader to allow extensions to override loading textures.
  2278. * @hidden
  2279. */
  2280. GLTFLoaderExtension._LoadTextureAsync = function (loader, context, textureInfo, assign) {
  2281. return loader._applyExtensions(function (extension) { return extension._loadTextureAsync(context, textureInfo, assign); });
  2282. };
  2283. /**
  2284. * Helper method called by the loader to allow extensions to override loading uris.
  2285. * @hidden
  2286. */
  2287. GLTFLoaderExtension._LoadUriAsync = function (loader, context, uri) {
  2288. return loader._applyExtensions(function (extension) { return extension._loadUriAsync(context, uri); });
  2289. };
  2290. return GLTFLoaderExtension;
  2291. }());
  2292. GLTF2.GLTFLoaderExtension = GLTFLoaderExtension;
  2293. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2294. })(BABYLON || (BABYLON = {}));
  2295. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  2296. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2297. var __extends = (this && this.__extends) || (function () {
  2298. var extendStatics = Object.setPrototypeOf ||
  2299. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2300. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2301. return function (d, b) {
  2302. extendStatics(d, b);
  2303. function __() { this.constructor = d; }
  2304. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2305. };
  2306. })();
  2307. var BABYLON;
  2308. (function (BABYLON) {
  2309. var GLTF2;
  2310. (function (GLTF2) {
  2311. var Extensions;
  2312. (function (Extensions) {
  2313. var NAME = "MSFT_lod";
  2314. /**
  2315. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/MSFT_lod)
  2316. */
  2317. var MSFT_lod = /** @class */ (function (_super) {
  2318. __extends(MSFT_lod, _super);
  2319. function MSFT_lod(loader) {
  2320. var _this = _super.call(this, loader) || this;
  2321. _this.name = NAME;
  2322. /**
  2323. * Maximum number of LODs to load, starting from the lowest LOD.
  2324. */
  2325. _this.maxLODsToLoad = Number.MAX_VALUE;
  2326. /**
  2327. * Observable raised when all node LODs of one level are loaded.
  2328. * The event data is the index of the loaded LOD starting from zero.
  2329. * Dispose the loader to cancel the loading of the next level of LODs.
  2330. */
  2331. _this.onNodeLODsLoadedObservable = new BABYLON.Observable();
  2332. /**
  2333. * Observable raised when all material LODs of one level are loaded.
  2334. * The event data is the index of the loaded LOD starting from zero.
  2335. * Dispose the loader to cancel the loading of the next level of LODs.
  2336. */
  2337. _this.onMaterialLODsLoadedObservable = new BABYLON.Observable();
  2338. _this._loadingNodeLOD = null;
  2339. _this._loadNodeSignals = {};
  2340. _this._loadNodePromises = new Array();
  2341. _this._loadingMaterialLOD = null;
  2342. _this._loadMaterialSignals = {};
  2343. _this._loadMaterialPromises = new Array();
  2344. _this._loader._onReadyObservable.addOnce(function () {
  2345. var _loop_1 = function (indexLOD) {
  2346. Promise.all(_this._loadNodePromises[indexLOD]).then(function () {
  2347. _this.onNodeLODsLoadedObservable.notifyObservers(indexLOD);
  2348. });
  2349. };
  2350. for (var indexLOD = 0; indexLOD < _this._loadNodePromises.length; indexLOD++) {
  2351. _loop_1(indexLOD);
  2352. }
  2353. var _loop_2 = function (indexLOD) {
  2354. Promise.all(_this._loadMaterialPromises[indexLOD]).then(function () {
  2355. _this.onMaterialLODsLoadedObservable.notifyObservers(indexLOD);
  2356. });
  2357. };
  2358. for (var indexLOD = 0; indexLOD < _this._loadMaterialPromises.length; indexLOD++) {
  2359. _loop_2(indexLOD);
  2360. }
  2361. });
  2362. return _this;
  2363. }
  2364. MSFT_lod.prototype.dispose = function () {
  2365. _super.prototype.dispose.call(this);
  2366. this._loadingNodeLOD = null;
  2367. this._loadNodeSignals = {};
  2368. this._loadingMaterialLOD = null;
  2369. this._loadMaterialSignals = {};
  2370. this.onMaterialLODsLoadedObservable.clear();
  2371. this.onNodeLODsLoadedObservable.clear();
  2372. };
  2373. MSFT_lod.prototype._loadNodeAsync = function (context, node) {
  2374. var _this = this;
  2375. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  2376. var firstPromise;
  2377. var nodeLODs = _this._getLODs(extensionContext, node, _this._loader._gltf.nodes, extension.ids);
  2378. var _loop_3 = function (indexLOD) {
  2379. var nodeLOD = nodeLODs[indexLOD];
  2380. if (indexLOD !== 0) {
  2381. _this._loadingNodeLOD = nodeLOD;
  2382. if (!_this._loadNodeSignals[nodeLOD._index]) {
  2383. _this._loadNodeSignals[nodeLOD._index] = new BABYLON.Deferred();
  2384. }
  2385. }
  2386. var promise = _this._loader._loadNodeAsync("#/nodes/" + nodeLOD._index, nodeLOD).then(function () {
  2387. if (indexLOD !== 0) {
  2388. var previousNodeLOD = nodeLODs[indexLOD - 1];
  2389. if (previousNodeLOD._babylonMesh) {
  2390. previousNodeLOD._babylonMesh.dispose(false, true);
  2391. delete previousNodeLOD._babylonMesh;
  2392. }
  2393. }
  2394. if (indexLOD !== nodeLODs.length - 1) {
  2395. var nodeIndex = nodeLODs[indexLOD + 1]._index;
  2396. if (_this._loadNodeSignals[nodeIndex]) {
  2397. _this._loadNodeSignals[nodeIndex].resolve();
  2398. delete _this._loadNodeSignals[nodeIndex];
  2399. }
  2400. }
  2401. });
  2402. if (indexLOD === 0) {
  2403. firstPromise = promise;
  2404. }
  2405. else {
  2406. _this._loader._completePromises.push(promise);
  2407. _this._loadingNodeLOD = null;
  2408. }
  2409. _this._loadNodePromises[indexLOD] = _this._loadNodePromises[indexLOD] || [];
  2410. _this._loadNodePromises[indexLOD].push(promise);
  2411. };
  2412. for (var indexLOD = 0; indexLOD < nodeLODs.length; indexLOD++) {
  2413. _loop_3(indexLOD);
  2414. }
  2415. return firstPromise;
  2416. });
  2417. };
  2418. MSFT_lod.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2419. var _this = this;
  2420. // Don't load material LODs if already loading a node LOD.
  2421. if (this._loadingNodeLOD) {
  2422. return null;
  2423. }
  2424. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  2425. var firstPromise;
  2426. var materialLODs = _this._getLODs(extensionContext, material, _this._loader._gltf.materials, extension.ids);
  2427. var _loop_4 = function (indexLOD) {
  2428. var materialLOD = materialLODs[indexLOD];
  2429. if (indexLOD !== 0) {
  2430. _this._loadingMaterialLOD = materialLOD;
  2431. if (!_this._loadMaterialSignals[materialLOD._index]) {
  2432. _this._loadMaterialSignals[materialLOD._index] = new BABYLON.Deferred();
  2433. }
  2434. }
  2435. var promise = _this._loader._loadMaterialAsync("#/materials/" + materialLOD._index, materialLOD, babylonMesh, babylonDrawMode, indexLOD === 0 ? assign : function () { }).then(function () {
  2436. if (indexLOD !== 0) {
  2437. var babylonDataLOD = materialLOD._babylonData;
  2438. assign(babylonDataLOD[babylonDrawMode].material);
  2439. var previousBabylonDataLOD = materialLODs[indexLOD - 1]._babylonData;
  2440. if (previousBabylonDataLOD[babylonDrawMode]) {
  2441. previousBabylonDataLOD[babylonDrawMode].material.dispose();
  2442. delete previousBabylonDataLOD[babylonDrawMode];
  2443. }
  2444. }
  2445. if (indexLOD !== materialLODs.length - 1) {
  2446. var materialIndex = materialLODs[indexLOD + 1]._index;
  2447. if (_this._loadMaterialSignals[materialIndex]) {
  2448. _this._loadMaterialSignals[materialIndex].resolve();
  2449. delete _this._loadMaterialSignals[materialIndex];
  2450. }
  2451. }
  2452. });
  2453. if (indexLOD === 0) {
  2454. firstPromise = promise;
  2455. }
  2456. else {
  2457. _this._loader._completePromises.push(promise);
  2458. _this._loadingMaterialLOD = null;
  2459. }
  2460. _this._loadMaterialPromises[indexLOD] = _this._loadMaterialPromises[indexLOD] || [];
  2461. _this._loadMaterialPromises[indexLOD].push(promise);
  2462. };
  2463. for (var indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  2464. _loop_4(indexLOD);
  2465. }
  2466. return firstPromise;
  2467. });
  2468. };
  2469. MSFT_lod.prototype._loadUriAsync = function (context, uri) {
  2470. var _this = this;
  2471. // Defer the loading of uris if loading a material or node LOD.
  2472. if (this._loadingMaterialLOD) {
  2473. var index = this._loadingMaterialLOD._index;
  2474. return this._loadMaterialSignals[index].promise.then(function () {
  2475. return _this._loader._loadUriAsync(context, uri);
  2476. });
  2477. }
  2478. else if (this._loadingNodeLOD) {
  2479. var index = this._loadingNodeLOD._index;
  2480. return this._loadNodeSignals[index].promise.then(function () {
  2481. return _this._loader._loadUriAsync(context, uri);
  2482. });
  2483. }
  2484. return null;
  2485. };
  2486. /**
  2487. * Gets an array of LOD properties from lowest to highest.
  2488. */
  2489. MSFT_lod.prototype._getLODs = function (context, property, array, ids) {
  2490. if (this.maxLODsToLoad <= 0) {
  2491. throw new Error("maxLODsToLoad must be greater than zero");
  2492. }
  2493. var properties = new Array();
  2494. for (var i = ids.length - 1; i >= 0; i--) {
  2495. properties.push(GLTF2.GLTFLoader._GetProperty(context + "/ids/" + ids[i], array, ids[i]));
  2496. if (properties.length === this.maxLODsToLoad) {
  2497. return properties;
  2498. }
  2499. }
  2500. properties.push(property);
  2501. return properties;
  2502. };
  2503. return MSFT_lod;
  2504. }(GLTF2.GLTFLoaderExtension));
  2505. Extensions.MSFT_lod = MSFT_lod;
  2506. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_lod(loader); });
  2507. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2508. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2509. })(BABYLON || (BABYLON = {}));
  2510. //# sourceMappingURL=MSFT_lod.js.map
  2511. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2512. var __extends = (this && this.__extends) || (function () {
  2513. var extendStatics = Object.setPrototypeOf ||
  2514. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2515. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2516. return function (d, b) {
  2517. extendStatics(d, b);
  2518. function __() { this.constructor = d; }
  2519. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2520. };
  2521. })();
  2522. var BABYLON;
  2523. (function (BABYLON) {
  2524. var GLTF2;
  2525. (function (GLTF2) {
  2526. var Extensions;
  2527. (function (Extensions) {
  2528. var NAME = "MSFT_minecraftMesh";
  2529. /** @hidden */
  2530. var MSFT_minecraftMesh = /** @class */ (function (_super) {
  2531. __extends(MSFT_minecraftMesh, _super);
  2532. function MSFT_minecraftMesh(loader) {
  2533. var _this = _super.call(this, loader) || this;
  2534. _this.name = NAME;
  2535. _this._onMaterialLoaded = function (material) {
  2536. if (material.needAlphaBlending()) {
  2537. material.forceDepthWrite = true;
  2538. material.separateCullingPass = true;
  2539. }
  2540. material.backFaceCulling = material.forceDepthWrite;
  2541. material.twoSidedLighting = true;
  2542. };
  2543. var meshes = loader._gltf.meshes;
  2544. if (meshes && meshes.length) {
  2545. for (var _i = 0, meshes_1 = meshes; _i < meshes_1.length; _i++) {
  2546. var mesh = meshes_1[_i];
  2547. if (mesh && mesh.extras && mesh.extras.MSFT_minecraftMesh) {
  2548. _this._loader.onMaterialLoadedObservable.add(_this._onMaterialLoaded);
  2549. break;
  2550. }
  2551. }
  2552. }
  2553. return _this;
  2554. }
  2555. return MSFT_minecraftMesh;
  2556. }(GLTF2.GLTFLoaderExtension));
  2557. Extensions.MSFT_minecraftMesh = MSFT_minecraftMesh;
  2558. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_minecraftMesh(loader); });
  2559. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2560. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2561. })(BABYLON || (BABYLON = {}));
  2562. //# sourceMappingURL=MSFT_minecraftMesh.js.map
  2563. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2564. var __extends = (this && this.__extends) || (function () {
  2565. var extendStatics = Object.setPrototypeOf ||
  2566. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2567. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2568. return function (d, b) {
  2569. extendStatics(d, b);
  2570. function __() { this.constructor = d; }
  2571. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2572. };
  2573. })();
  2574. var BABYLON;
  2575. (function (BABYLON) {
  2576. var GLTF2;
  2577. (function (GLTF2) {
  2578. var Extensions;
  2579. (function (Extensions) {
  2580. var NAME = "MSFT_sRGBFactors";
  2581. /** @hidden */
  2582. var MSFT_sRGBFactors = /** @class */ (function (_super) {
  2583. __extends(MSFT_sRGBFactors, _super);
  2584. function MSFT_sRGBFactors(loader) {
  2585. var _this = _super.call(this, loader) || this;
  2586. _this.name = NAME;
  2587. _this._onMaterialLoaded = function (material) {
  2588. if (!material.albedoTexture) {
  2589. material.albedoColor.toLinearSpaceToRef(material.albedoColor);
  2590. }
  2591. if (!material.reflectivityTexture) {
  2592. material.reflectivityColor.toLinearSpaceToRef(material.reflectivityColor);
  2593. }
  2594. };
  2595. var materials = loader._gltf.materials;
  2596. if (materials && materials.length) {
  2597. for (var _i = 0, materials_1 = materials; _i < materials_1.length; _i++) {
  2598. var material = materials_1[_i];
  2599. if (material && material.extras && material.extras.MSFT_sRGBFactors) {
  2600. _this._loader.onMaterialLoadedObservable.add(_this._onMaterialLoaded);
  2601. break;
  2602. }
  2603. }
  2604. }
  2605. return _this;
  2606. }
  2607. return MSFT_sRGBFactors;
  2608. }(GLTF2.GLTFLoaderExtension));
  2609. Extensions.MSFT_sRGBFactors = MSFT_sRGBFactors;
  2610. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_sRGBFactors(loader); });
  2611. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2612. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2613. })(BABYLON || (BABYLON = {}));
  2614. //# sourceMappingURL=MSFT_sRGBFactors.js.map
  2615. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2616. var __extends = (this && this.__extends) || (function () {
  2617. var extendStatics = Object.setPrototypeOf ||
  2618. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2619. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2620. return function (d, b) {
  2621. extendStatics(d, b);
  2622. function __() { this.constructor = d; }
  2623. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2624. };
  2625. })();
  2626. var BABYLON;
  2627. (function (BABYLON) {
  2628. var GLTF2;
  2629. (function (GLTF2) {
  2630. var Extensions;
  2631. (function (Extensions) {
  2632. var NAME = "KHR_draco_mesh_compression";
  2633. /**
  2634. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression)
  2635. */
  2636. var KHR_draco_mesh_compression = /** @class */ (function (_super) {
  2637. __extends(KHR_draco_mesh_compression, _super);
  2638. function KHR_draco_mesh_compression(loader) {
  2639. var _this = _super.call(this, loader) || this;
  2640. _this.name = NAME;
  2641. _this._dracoCompression = null;
  2642. // Disable extension if decoder is not available.
  2643. if (!BABYLON.DracoCompression.DecoderAvailable) {
  2644. _this.enabled = false;
  2645. }
  2646. return _this;
  2647. }
  2648. KHR_draco_mesh_compression.prototype.dispose = function () {
  2649. if (this._dracoCompression) {
  2650. this._dracoCompression.dispose();
  2651. }
  2652. _super.prototype.dispose.call(this);
  2653. };
  2654. KHR_draco_mesh_compression.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  2655. var _this = this;
  2656. return this._loadExtensionAsync(context, primitive, function (extensionContext, extension) {
  2657. if (primitive.mode != undefined) {
  2658. if (primitive.mode !== 5 /* TRIANGLE_STRIP */ &&
  2659. primitive.mode !== 4 /* TRIANGLES */) {
  2660. throw new Error(context + ": Unsupported mode " + primitive.mode);
  2661. }
  2662. // TODO: handle triangle strips
  2663. if (primitive.mode === 5 /* TRIANGLE_STRIP */) {
  2664. throw new Error(context + ": Mode " + primitive.mode + " is not currently supported");
  2665. }
  2666. }
  2667. var attributes = {};
  2668. var loadAttribute = function (name, kind) {
  2669. var uniqueId = extension.attributes[name];
  2670. if (uniqueId == undefined) {
  2671. return;
  2672. }
  2673. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  2674. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  2675. babylonMesh._delayInfo.push(kind);
  2676. }
  2677. attributes[kind] = uniqueId;
  2678. };
  2679. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  2680. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  2681. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  2682. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  2683. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  2684. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  2685. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  2686. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind);
  2687. var bufferView = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._loader._gltf.bufferViews, extension.bufferView);
  2688. if (!bufferView._dracoBabylonGeometry) {
  2689. bufferView._dracoBabylonGeometry = _this._loader._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  2690. if (!_this._dracoCompression) {
  2691. _this._dracoCompression = new BABYLON.DracoCompression();
  2692. }
  2693. return _this._dracoCompression.decodeMeshAsync(data, attributes).then(function (babylonVertexData) {
  2694. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, _this._loader._babylonScene);
  2695. babylonVertexData.applyToGeometry(babylonGeometry);
  2696. return babylonGeometry;
  2697. }).catch(function (error) {
  2698. throw new Error(context + ": " + error.message);
  2699. });
  2700. });
  2701. }
  2702. return bufferView._dracoBabylonGeometry;
  2703. });
  2704. };
  2705. return KHR_draco_mesh_compression;
  2706. }(GLTF2.GLTFLoaderExtension));
  2707. Extensions.KHR_draco_mesh_compression = KHR_draco_mesh_compression;
  2708. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_draco_mesh_compression(loader); });
  2709. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2710. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2711. })(BABYLON || (BABYLON = {}));
  2712. //# sourceMappingURL=KHR_draco_mesh_compression.js.map
  2713. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2714. var __extends = (this && this.__extends) || (function () {
  2715. var extendStatics = Object.setPrototypeOf ||
  2716. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2717. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2718. return function (d, b) {
  2719. extendStatics(d, b);
  2720. function __() { this.constructor = d; }
  2721. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2722. };
  2723. })();
  2724. var BABYLON;
  2725. (function (BABYLON) {
  2726. var GLTF2;
  2727. (function (GLTF2) {
  2728. var Extensions;
  2729. (function (Extensions) {
  2730. var NAME = "KHR_materials_pbrSpecularGlossiness";
  2731. /**
  2732. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness)
  2733. */
  2734. var KHR_materials_pbrSpecularGlossiness = /** @class */ (function (_super) {
  2735. __extends(KHR_materials_pbrSpecularGlossiness, _super);
  2736. function KHR_materials_pbrSpecularGlossiness() {
  2737. var _this = _super !== null && _super.apply(this, arguments) || this;
  2738. _this.name = NAME;
  2739. return _this;
  2740. }
  2741. /** @hidden */
  2742. KHR_materials_pbrSpecularGlossiness.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2743. var _this = this;
  2744. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  2745. material._babylonData = material._babylonData || {};
  2746. var babylonData = material._babylonData[babylonDrawMode];
  2747. if (!babylonData) {
  2748. var promises = new Array();
  2749. var name_1 = material.name || "materialSG_" + material._index;
  2750. var babylonMaterial = _this._loader._createMaterial(name_1, babylonDrawMode);
  2751. promises.push(_this._loader._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  2752. promises.push(_this._loadSpecularGlossinessPropertiesAsync(extensionContext, material, extension, babylonMaterial));
  2753. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  2754. babylonData = {
  2755. material: babylonMaterial,
  2756. meshes: [],
  2757. loaded: Promise.all(promises).then(function () { })
  2758. };
  2759. material._babylonData[babylonDrawMode] = babylonData;
  2760. }
  2761. babylonData.meshes.push(babylonMesh);
  2762. assign(babylonData.material);
  2763. return babylonData.loaded;
  2764. });
  2765. };
  2766. KHR_materials_pbrSpecularGlossiness.prototype._loadSpecularGlossinessPropertiesAsync = function (context, material, properties, babylonMaterial) {
  2767. var promises = new Array();
  2768. if (properties.diffuseFactor) {
  2769. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.diffuseFactor);
  2770. babylonMaterial.alpha = properties.diffuseFactor[3];
  2771. }
  2772. else {
  2773. babylonMaterial.albedoColor = BABYLON.Color3.White();
  2774. }
  2775. babylonMaterial.reflectivityColor = properties.specularFactor ? BABYLON.Color3.FromArray(properties.specularFactor) : BABYLON.Color3.White();
  2776. babylonMaterial.microSurface = properties.glossinessFactor == undefined ? 1 : properties.glossinessFactor;
  2777. if (properties.diffuseTexture) {
  2778. promises.push(this._loader._loadTextureAsync(context + "/diffuseTexture", properties.diffuseTexture, function (texture) {
  2779. babylonMaterial.albedoTexture = texture;
  2780. }));
  2781. }
  2782. if (properties.specularGlossinessTexture) {
  2783. promises.push(this._loader._loadTextureAsync(context + "/specularGlossinessTexture", properties.specularGlossinessTexture, function (texture) {
  2784. babylonMaterial.reflectivityTexture = texture;
  2785. }));
  2786. babylonMaterial.reflectivityTexture.hasAlpha = true;
  2787. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  2788. }
  2789. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  2790. return Promise.all(promises).then(function () { });
  2791. };
  2792. return KHR_materials_pbrSpecularGlossiness;
  2793. }(GLTF2.GLTFLoaderExtension));
  2794. Extensions.KHR_materials_pbrSpecularGlossiness = KHR_materials_pbrSpecularGlossiness;
  2795. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_pbrSpecularGlossiness(loader); });
  2796. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2797. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2798. })(BABYLON || (BABYLON = {}));
  2799. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map
  2800. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2801. var __extends = (this && this.__extends) || (function () {
  2802. var extendStatics = Object.setPrototypeOf ||
  2803. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2804. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2805. return function (d, b) {
  2806. extendStatics(d, b);
  2807. function __() { this.constructor = d; }
  2808. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2809. };
  2810. })();
  2811. var BABYLON;
  2812. (function (BABYLON) {
  2813. var GLTF2;
  2814. (function (GLTF2) {
  2815. var Extensions;
  2816. (function (Extensions) {
  2817. var NAME = "KHR_materials_unlit";
  2818. /**
  2819. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit)
  2820. */
  2821. var KHR_materials_unlit = /** @class */ (function (_super) {
  2822. __extends(KHR_materials_unlit, _super);
  2823. function KHR_materials_unlit() {
  2824. var _this = _super !== null && _super.apply(this, arguments) || this;
  2825. _this.name = NAME;
  2826. return _this;
  2827. }
  2828. KHR_materials_unlit.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2829. var _this = this;
  2830. return this._loadExtensionAsync(context, material, function () {
  2831. material._babylonData = material._babylonData || {};
  2832. var babylonData = material._babylonData[babylonDrawMode];
  2833. if (!babylonData) {
  2834. var name_1 = material.name || "materialUnlit_" + material._index;
  2835. var babylonMaterial = _this._loader._createMaterial(name_1, babylonDrawMode);
  2836. babylonMaterial.unlit = true;
  2837. var promise = _this._loadUnlitPropertiesAsync(context, material, babylonMaterial);
  2838. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  2839. babylonData = {
  2840. material: babylonMaterial,
  2841. meshes: [],
  2842. loaded: promise
  2843. };
  2844. material._babylonData[babylonDrawMode] = babylonData;
  2845. }
  2846. babylonData.meshes.push(babylonMesh);
  2847. assign(babylonData.material);
  2848. return babylonData.loaded;
  2849. });
  2850. };
  2851. KHR_materials_unlit.prototype._loadUnlitPropertiesAsync = function (context, material, babylonMaterial) {
  2852. var promises = new Array();
  2853. // Ensure metallic workflow
  2854. babylonMaterial.metallic = 1;
  2855. babylonMaterial.roughness = 1;
  2856. var properties = material.pbrMetallicRoughness;
  2857. if (properties) {
  2858. if (properties.baseColorFactor) {
  2859. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  2860. babylonMaterial.alpha = properties.baseColorFactor[3];
  2861. }
  2862. else {
  2863. babylonMaterial.albedoColor = BABYLON.Color3.White();
  2864. }
  2865. if (properties.baseColorTexture) {
  2866. promises.push(this._loader._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  2867. babylonMaterial.albedoTexture = texture;
  2868. }));
  2869. }
  2870. }
  2871. if (material.doubleSided) {
  2872. babylonMaterial.backFaceCulling = false;
  2873. babylonMaterial.twoSidedLighting = true;
  2874. }
  2875. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  2876. return Promise.all(promises).then(function () { });
  2877. };
  2878. return KHR_materials_unlit;
  2879. }(GLTF2.GLTFLoaderExtension));
  2880. Extensions.KHR_materials_unlit = KHR_materials_unlit;
  2881. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_unlit(loader); });
  2882. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2883. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2884. })(BABYLON || (BABYLON = {}));
  2885. //# sourceMappingURL=KHR_materials_unlit.js.map
  2886. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2887. var __extends = (this && this.__extends) || (function () {
  2888. var extendStatics = Object.setPrototypeOf ||
  2889. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2890. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2891. return function (d, b) {
  2892. extendStatics(d, b);
  2893. function __() { this.constructor = d; }
  2894. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2895. };
  2896. })();
  2897. var BABYLON;
  2898. (function (BABYLON) {
  2899. var GLTF2;
  2900. (function (GLTF2) {
  2901. var Extensions;
  2902. (function (Extensions) {
  2903. var NAME = "KHR_lights";
  2904. var LightType;
  2905. (function (LightType) {
  2906. LightType["AMBIENT"] = "ambient";
  2907. LightType["DIRECTIONAL"] = "directional";
  2908. LightType["POINT"] = "point";
  2909. LightType["SPOT"] = "spot";
  2910. })(LightType || (LightType = {}));
  2911. /**
  2912. * [Specification](https://github.com/MiiBond/glTF/tree/khr_lights_v1/extensions/Khronos/KHR_lights) (Experimental)
  2913. */
  2914. var KHR_lights = /** @class */ (function (_super) {
  2915. __extends(KHR_lights, _super);
  2916. function KHR_lights() {
  2917. var _this = _super !== null && _super.apply(this, arguments) || this;
  2918. _this.name = NAME;
  2919. return _this;
  2920. }
  2921. KHR_lights.prototype._loadSceneAsync = function (context, scene) {
  2922. var _this = this;
  2923. return this._loadExtensionAsync(context, scene, function (extensionContext, extension) {
  2924. var promise = _this._loader._loadSceneAsync(extensionContext, scene);
  2925. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  2926. if (light.type !== LightType.AMBIENT) {
  2927. throw new Error(extensionContext + ": Only ambient lights are allowed on a scene");
  2928. }
  2929. _this._loader._babylonScene.ambientColor = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.Black();
  2930. return promise;
  2931. });
  2932. };
  2933. KHR_lights.prototype._loadNodeAsync = function (context, node) {
  2934. var _this = this;
  2935. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  2936. var promise = _this._loader._loadNodeAsync(extensionContext, node);
  2937. var babylonLight;
  2938. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  2939. var name = node._babylonMesh.name;
  2940. switch (light.type) {
  2941. case LightType.AMBIENT: {
  2942. throw new Error(extensionContext + ": Ambient lights are not allowed on a node");
  2943. }
  2944. case LightType.DIRECTIONAL: {
  2945. babylonLight = new BABYLON.DirectionalLight(name, BABYLON.Vector3.Forward(), _this._loader._babylonScene);
  2946. break;
  2947. }
  2948. case LightType.POINT: {
  2949. babylonLight = new BABYLON.PointLight(name, BABYLON.Vector3.Zero(), _this._loader._babylonScene);
  2950. break;
  2951. }
  2952. case LightType.SPOT: {
  2953. var spotLight = light;
  2954. // TODO: support inner and outer cone angles
  2955. //const innerConeAngle = spotLight.innerConeAngle || 0;
  2956. var outerConeAngle = spotLight.outerConeAngle || Math.PI / 4;
  2957. babylonLight = new BABYLON.SpotLight(name, BABYLON.Vector3.Zero(), BABYLON.Vector3.Forward(), outerConeAngle, 2, _this._loader._babylonScene);
  2958. break;
  2959. }
  2960. default: {
  2961. throw new Error(extensionContext + ": Invalid light type (" + light.type + ")");
  2962. }
  2963. }
  2964. babylonLight.diffuse = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.White();
  2965. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  2966. babylonLight.parent = node._babylonMesh;
  2967. return promise;
  2968. });
  2969. };
  2970. Object.defineProperty(KHR_lights.prototype, "_lights", {
  2971. get: function () {
  2972. var extensions = this._loader._gltf.extensions;
  2973. if (!extensions || !extensions[this.name]) {
  2974. throw new Error("#/extensions: '" + this.name + "' not found");
  2975. }
  2976. var extension = extensions[this.name];
  2977. return extension.lights;
  2978. },
  2979. enumerable: true,
  2980. configurable: true
  2981. });
  2982. return KHR_lights;
  2983. }(GLTF2.GLTFLoaderExtension));
  2984. Extensions.KHR_lights = KHR_lights;
  2985. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_lights(loader); });
  2986. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2987. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2988. })(BABYLON || (BABYLON = {}));
  2989. //# sourceMappingURL=KHR_lights.js.map
  2990. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2991. var __extends = (this && this.__extends) || (function () {
  2992. var extendStatics = Object.setPrototypeOf ||
  2993. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2994. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2995. return function (d, b) {
  2996. extendStatics(d, b);
  2997. function __() { this.constructor = d; }
  2998. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2999. };
  3000. })();
  3001. var BABYLON;
  3002. (function (BABYLON) {
  3003. var GLTF2;
  3004. (function (GLTF2) {
  3005. var Extensions;
  3006. (function (Extensions) {
  3007. var NAME = "KHR_texture_transform";
  3008. /**
  3009. * [Specification](https://github.com/AltspaceVR/glTF/blob/avr-sampler-offset-tile/extensions/2.0/Khronos/KHR_texture_transform/README.md) (Experimental)
  3010. */
  3011. var KHR_texture_transform = /** @class */ (function (_super) {
  3012. __extends(KHR_texture_transform, _super);
  3013. function KHR_texture_transform() {
  3014. var _this = _super !== null && _super.apply(this, arguments) || this;
  3015. _this.name = NAME;
  3016. return _this;
  3017. }
  3018. KHR_texture_transform.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  3019. var _this = this;
  3020. return this._loadExtensionAsync(context, textureInfo, function (extensionContext, extension) {
  3021. return _this._loader._loadTextureAsync(context, textureInfo, function (babylonTexture) {
  3022. if (extension.offset) {
  3023. babylonTexture.uOffset = extension.offset[0];
  3024. babylonTexture.vOffset = extension.offset[1];
  3025. }
  3026. // Always rotate around the origin.
  3027. babylonTexture.uRotationCenter = 0;
  3028. babylonTexture.vRotationCenter = 0;
  3029. if (extension.rotation) {
  3030. babylonTexture.wAng = -extension.rotation;
  3031. }
  3032. if (extension.scale) {
  3033. babylonTexture.uScale = extension.scale[0];
  3034. babylonTexture.vScale = extension.scale[1];
  3035. }
  3036. if (extension.texCoord != undefined) {
  3037. babylonTexture.coordinatesIndex = extension.texCoord;
  3038. }
  3039. assign(babylonTexture);
  3040. });
  3041. });
  3042. };
  3043. return KHR_texture_transform;
  3044. }(GLTF2.GLTFLoaderExtension));
  3045. Extensions.KHR_texture_transform = KHR_texture_transform;
  3046. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_texture_transform(loader); });
  3047. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  3048. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  3049. })(BABYLON || (BABYLON = {}));
  3050. //# sourceMappingURL=KHR_texture_transform.js.map