babylon.glTF2FileLoader.js 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985
  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.add(function () {
  255. resolve();
  256. }, undefined, undefined, undefined, true);
  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. this._disposed = false;
  633. this._state = null;
  634. this._extensions = {};
  635. this._defaultSampler = {};
  636. this._defaultBabylonMaterials = {};
  637. this._requests = new Array();
  638. /**
  639. * Mode that determines the coordinate system to use.
  640. */
  641. this.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.AUTO;
  642. /**
  643. * Mode that determines what animations will start.
  644. */
  645. this.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.FIRST;
  646. /**
  647. * Defines if the loader should compile materials.
  648. */
  649. this.compileMaterials = false;
  650. /**
  651. * Defines if the loader should also compile materials with clip planes.
  652. */
  653. this.useClipPlane = false;
  654. /**
  655. * Defines if the loader should compile shadow generators.
  656. */
  657. this.compileShadowGenerators = false;
  658. /**
  659. * Defines if the Alpha blended materials are only applied as coverage.
  660. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
  661. * If true, no extra effects are applied to transparent pixels.
  662. */
  663. this.transparencyAsCoverage = false;
  664. /** @hidden */
  665. this._normalizeAnimationGroupsToBeginAtZero = true;
  666. /**
  667. * Function called before loading a url referenced by the asset.
  668. */
  669. this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
  670. /**
  671. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  672. */
  673. this.onMeshLoadedObservable = new BABYLON.Observable();
  674. /**
  675. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  676. */
  677. this.onTextureLoadedObservable = new BABYLON.Observable();
  678. /**
  679. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  680. */
  681. this.onMaterialLoadedObservable = new BABYLON.Observable();
  682. /**
  683. * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
  684. */
  685. this.onCameraLoadedObservable = new BABYLON.Observable();
  686. /**
  687. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  688. * For assets with LODs, raised when all of the LODs are complete.
  689. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  690. */
  691. this.onCompleteObservable = new BABYLON.Observable();
  692. /**
  693. * Observable raised after the loader is disposed.
  694. */
  695. this.onDisposeObservable = new BABYLON.Observable();
  696. /**
  697. * Observable raised after a loader extension is created.
  698. * Set additional options for a loader extension in this event.
  699. */
  700. this.onExtensionLoadedObservable = new BABYLON.Observable();
  701. }
  702. /** @hidden */
  703. GLTFLoader._Register = function (name, factory) {
  704. if (GLTFLoader._ExtensionFactories[name]) {
  705. BABYLON.Tools.Error("Extension with the name '" + name + "' already exists");
  706. return;
  707. }
  708. GLTFLoader._ExtensionFactories[name] = factory;
  709. // Keep the order of registration so that extensions registered first are called first.
  710. GLTFLoader._ExtensionNames.push(name);
  711. };
  712. Object.defineProperty(GLTFLoader.prototype, "state", {
  713. /**
  714. * Loader state or null if the loader is not active.
  715. */
  716. get: function () {
  717. return this._state;
  718. },
  719. enumerable: true,
  720. configurable: true
  721. });
  722. /**
  723. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  724. */
  725. GLTFLoader.prototype.dispose = function () {
  726. if (this._disposed) {
  727. return;
  728. }
  729. this._disposed = true;
  730. this.onDisposeObservable.notifyObservers(this);
  731. this.onDisposeObservable.clear();
  732. this._clear();
  733. };
  734. /**
  735. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  736. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  737. * @param scene the scene the meshes should be added to
  738. * @param data the glTF data to load
  739. * @param rootUrl root url to load from
  740. * @param onProgress event that fires when loading progress has occured
  741. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  742. */
  743. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  744. var _this = this;
  745. return Promise.resolve().then(function () {
  746. var nodes = null;
  747. if (meshesNames) {
  748. var nodeMap_1 = {};
  749. if (_this._gltf.nodes) {
  750. for (var _i = 0, _a = _this._gltf.nodes; _i < _a.length; _i++) {
  751. var node = _a[_i];
  752. if (node.name) {
  753. nodeMap_1[node.name] = node;
  754. }
  755. }
  756. }
  757. var names = (meshesNames instanceof Array) ? meshesNames : [meshesNames];
  758. nodes = names.map(function (name) {
  759. var node = nodeMap_1[name];
  760. if (!node) {
  761. throw new Error("Failed to find node '" + name + "'");
  762. }
  763. return node;
  764. });
  765. }
  766. return _this._loadAsync(nodes, scene, data, rootUrl, onProgress).then(function () {
  767. return {
  768. meshes: _this._getMeshes(),
  769. particleSystems: [],
  770. skeletons: _this._getSkeletons(),
  771. animationGroups: _this._getAnimationGroups()
  772. };
  773. });
  774. });
  775. };
  776. /**
  777. * Imports all objects from the loaded glTF data and adds them to the scene
  778. * @param scene the scene the objects should be added to
  779. * @param data the glTF data to load
  780. * @param rootUrl root url to load from
  781. * @param onProgress event that fires when loading progress has occured
  782. * @returns a promise which completes when objects have been loaded to the scene
  783. */
  784. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  785. return this._loadAsync(null, scene, data, rootUrl, onProgress);
  786. };
  787. GLTFLoader.prototype._loadAsync = function (nodes, scene, data, rootUrl, onProgress) {
  788. var _this = this;
  789. return Promise.resolve().then(function () {
  790. _this._babylonScene = scene;
  791. _this._rootUrl = rootUrl;
  792. _this._progressCallback = onProgress;
  793. _this._state = BABYLON.GLTFLoaderState.LOADING;
  794. _this._loadData(data);
  795. _this._loadExtensions();
  796. _this._checkExtensions();
  797. var promises = new Array();
  798. if (nodes) {
  799. promises.push(_this._loadNodesAsync(nodes));
  800. }
  801. else {
  802. var scene_1 = GLTFLoader._GetProperty("#/scene", _this._gltf.scenes, _this._gltf.scene || 0);
  803. promises.push(_this._loadSceneAsync("#/scenes/" + scene_1._index, scene_1));
  804. }
  805. if (_this.compileMaterials) {
  806. promises.push(_this._compileMaterialsAsync());
  807. }
  808. if (_this.compileShadowGenerators) {
  809. promises.push(_this._compileShadowGeneratorsAsync());
  810. }
  811. var resultPromise = Promise.all(promises).then(function () {
  812. _this._state = BABYLON.GLTFLoaderState.READY;
  813. _this._startAnimations();
  814. });
  815. resultPromise.then(function () {
  816. _this._rootBabylonMesh.setEnabled(true);
  817. BABYLON.Tools.SetImmediate(function () {
  818. if (!_this._disposed) {
  819. Promise.all(_this._completePromises).then(function () {
  820. _this._state = BABYLON.GLTFLoaderState.COMPLETE;
  821. _this.onCompleteObservable.notifyObservers(_this);
  822. _this.onCompleteObservable.clear();
  823. _this._clear();
  824. }).catch(function (error) {
  825. BABYLON.Tools.Error("glTF Loader: " + error.message);
  826. _this._clear();
  827. });
  828. }
  829. });
  830. });
  831. return resultPromise;
  832. }).catch(function (error) {
  833. if (!_this._disposed) {
  834. BABYLON.Tools.Error("glTF Loader: " + error.message);
  835. _this._clear();
  836. throw error;
  837. }
  838. });
  839. };
  840. GLTFLoader.prototype._loadData = function (data) {
  841. this._gltf = data.json;
  842. this._setupData();
  843. if (data.bin) {
  844. var buffers = this._gltf.buffers;
  845. if (buffers && buffers[0] && !buffers[0].uri) {
  846. var binaryBuffer = buffers[0];
  847. if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
  848. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  849. }
  850. binaryBuffer._data = Promise.resolve(data.bin);
  851. }
  852. else {
  853. BABYLON.Tools.Warn("Unexpected BIN chunk");
  854. }
  855. }
  856. };
  857. GLTFLoader.prototype._setupData = function () {
  858. GLTF2._ArrayItem.Assign(this._gltf.accessors);
  859. GLTF2._ArrayItem.Assign(this._gltf.animations);
  860. GLTF2._ArrayItem.Assign(this._gltf.buffers);
  861. GLTF2._ArrayItem.Assign(this._gltf.bufferViews);
  862. GLTF2._ArrayItem.Assign(this._gltf.cameras);
  863. GLTF2._ArrayItem.Assign(this._gltf.images);
  864. GLTF2._ArrayItem.Assign(this._gltf.materials);
  865. GLTF2._ArrayItem.Assign(this._gltf.meshes);
  866. GLTF2._ArrayItem.Assign(this._gltf.nodes);
  867. GLTF2._ArrayItem.Assign(this._gltf.samplers);
  868. GLTF2._ArrayItem.Assign(this._gltf.scenes);
  869. GLTF2._ArrayItem.Assign(this._gltf.skins);
  870. GLTF2._ArrayItem.Assign(this._gltf.textures);
  871. if (this._gltf.nodes) {
  872. var nodeParents = {};
  873. for (var _i = 0, _a = this._gltf.nodes; _i < _a.length; _i++) {
  874. var node = _a[_i];
  875. if (node.children) {
  876. for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
  877. var index = _c[_b];
  878. nodeParents[index] = node._index;
  879. }
  880. }
  881. }
  882. var rootNode = this._createRootNode();
  883. for (var _d = 0, _e = this._gltf.nodes; _d < _e.length; _d++) {
  884. var node = _e[_d];
  885. var parentIndex = nodeParents[node._index];
  886. node._parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
  887. }
  888. }
  889. };
  890. GLTFLoader.prototype._loadExtensions = function () {
  891. for (var _i = 0, _a = GLTFLoader._ExtensionNames; _i < _a.length; _i++) {
  892. var name_1 = _a[_i];
  893. var extension = GLTFLoader._ExtensionFactories[name_1](this);
  894. this._extensions[name_1] = extension;
  895. this.onExtensionLoadedObservable.notifyObservers(extension);
  896. }
  897. this.onExtensionLoadedObservable.clear();
  898. };
  899. GLTFLoader.prototype._checkExtensions = function () {
  900. if (this._gltf.extensionsRequired) {
  901. for (var _i = 0, _a = this._gltf.extensionsRequired; _i < _a.length; _i++) {
  902. var name_2 = _a[_i];
  903. var extension = this._extensions[name_2];
  904. if (!extension || !extension.enabled) {
  905. throw new Error("Require extension " + name_2 + " is not available");
  906. }
  907. }
  908. }
  909. };
  910. GLTFLoader.prototype._createRootNode = function () {
  911. this._rootBabylonMesh = new BABYLON.Mesh("__root__", this._babylonScene);
  912. this._rootBabylonMesh.setEnabled(false);
  913. var rootNode = { _babylonMesh: this._rootBabylonMesh };
  914. switch (this.coordinateSystemMode) {
  915. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO: {
  916. if (!this._babylonScene.useRightHandedSystem) {
  917. rootNode.rotation = [0, 1, 0, 0];
  918. rootNode.scale = [1, 1, -1];
  919. GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
  920. }
  921. break;
  922. }
  923. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
  924. this._babylonScene.useRightHandedSystem = true;
  925. break;
  926. }
  927. default: {
  928. throw new Error("Invalid coordinate system mode (" + this.coordinateSystemMode + ")");
  929. }
  930. }
  931. this.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
  932. return rootNode;
  933. };
  934. GLTFLoader.prototype._loadNodesAsync = function (nodes) {
  935. var promises = new Array();
  936. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  937. var node = nodes_1[_i];
  938. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  939. }
  940. promises.push(this._loadAnimationsAsync());
  941. return Promise.all(promises).then(function () { });
  942. };
  943. /** @hidden */
  944. GLTFLoader.prototype._loadSceneAsync = function (context, scene) {
  945. var promise = GLTF2.GLTFLoaderExtension._LoadSceneAsync(this, context, scene);
  946. if (promise) {
  947. return promise;
  948. }
  949. var promises = new Array();
  950. for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
  951. var index = _a[_i];
  952. var node = GLTFLoader._GetProperty(context + "/nodes/" + index, this._gltf.nodes, index);
  953. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  954. }
  955. promises.push(this._loadAnimationsAsync());
  956. return Promise.all(promises).then(function () { });
  957. };
  958. GLTFLoader.prototype._forEachPrimitive = function (node, callback) {
  959. if (node._primitiveBabylonMeshes) {
  960. for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
  961. var babylonMesh = _a[_i];
  962. callback(babylonMesh);
  963. }
  964. }
  965. else {
  966. callback(node._babylonMesh);
  967. }
  968. };
  969. GLTFLoader.prototype._getMeshes = function () {
  970. var meshes = new Array();
  971. // Root mesh is always first.
  972. meshes.push(this._rootBabylonMesh);
  973. var nodes = this._gltf.nodes;
  974. if (nodes) {
  975. for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
  976. var node = nodes_2[_i];
  977. if (node._babylonMesh) {
  978. meshes.push(node._babylonMesh);
  979. }
  980. if (node._primitiveBabylonMeshes) {
  981. for (var _a = 0, _b = node._primitiveBabylonMeshes; _a < _b.length; _a++) {
  982. var babylonMesh = _b[_a];
  983. meshes.push(babylonMesh);
  984. }
  985. }
  986. }
  987. }
  988. return meshes;
  989. };
  990. GLTFLoader.prototype._getSkeletons = function () {
  991. var skeletons = new Array();
  992. var skins = this._gltf.skins;
  993. if (skins) {
  994. for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
  995. var skin = skins_1[_i];
  996. if (skin._babylonSkeleton) {
  997. skeletons.push(skin._babylonSkeleton);
  998. }
  999. }
  1000. }
  1001. return skeletons;
  1002. };
  1003. GLTFLoader.prototype._getAnimationGroups = function () {
  1004. var animationGroups = new Array();
  1005. var animations = this._gltf.animations;
  1006. if (animations) {
  1007. for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
  1008. var animation = animations_1[_i];
  1009. if (animation._babylonAnimationGroup) {
  1010. animationGroups.push(animation._babylonAnimationGroup);
  1011. }
  1012. }
  1013. }
  1014. return animationGroups;
  1015. };
  1016. GLTFLoader.prototype._startAnimations = function () {
  1017. switch (this.animationStartMode) {
  1018. case BABYLON.GLTFLoaderAnimationStartMode.NONE: {
  1019. // do nothing
  1020. break;
  1021. }
  1022. case BABYLON.GLTFLoaderAnimationStartMode.FIRST: {
  1023. var babylonAnimationGroups = this._getAnimationGroups();
  1024. if (babylonAnimationGroups.length !== 0) {
  1025. babylonAnimationGroups[0].start(true);
  1026. }
  1027. break;
  1028. }
  1029. case BABYLON.GLTFLoaderAnimationStartMode.ALL: {
  1030. var babylonAnimationGroups = this._getAnimationGroups();
  1031. for (var _i = 0, babylonAnimationGroups_1 = babylonAnimationGroups; _i < babylonAnimationGroups_1.length; _i++) {
  1032. var babylonAnimationGroup = babylonAnimationGroups_1[_i];
  1033. babylonAnimationGroup.start(true);
  1034. }
  1035. break;
  1036. }
  1037. default: {
  1038. BABYLON.Tools.Error("Invalid animation start mode (" + this.animationStartMode + ")");
  1039. return;
  1040. }
  1041. }
  1042. };
  1043. /** @hidden */
  1044. GLTFLoader.prototype._loadNodeAsync = function (context, node) {
  1045. var promise = GLTF2.GLTFLoaderExtension._LoadNodeAsync(this, context, node);
  1046. if (promise) {
  1047. return promise;
  1048. }
  1049. if (node._babylonMesh) {
  1050. throw new Error(context + ": Invalid recursive node hierarchy");
  1051. }
  1052. var promises = new Array();
  1053. var babylonMesh = new BABYLON.Mesh(node.name || "node" + node._index, this._babylonScene, node._parent._babylonMesh);
  1054. node._babylonMesh = babylonMesh;
  1055. GLTFLoader._LoadTransform(node, babylonMesh);
  1056. if (node.mesh != undefined) {
  1057. var mesh = GLTFLoader._GetProperty(context + "/mesh", this._gltf.meshes, node.mesh);
  1058. promises.push(this._loadMeshAsync("#/meshes/" + mesh._index, node, mesh, babylonMesh));
  1059. }
  1060. if (node.camera != undefined) {
  1061. var camera = GLTFLoader._GetProperty(context + "/camera", this._gltf.cameras, node.camera);
  1062. this._loadCamera("#/cameras/" + camera._index, camera, babylonMesh);
  1063. }
  1064. if (node.children) {
  1065. for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
  1066. var index = _a[_i];
  1067. var childNode = GLTFLoader._GetProperty(context + "/children/" + index, this._gltf.nodes, index);
  1068. promises.push(this._loadNodeAsync("#/nodes/" + index, childNode));
  1069. }
  1070. }
  1071. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  1072. return Promise.all(promises).then(function () { });
  1073. };
  1074. GLTFLoader.prototype._loadMeshAsync = function (context, node, mesh, babylonMesh) {
  1075. var _this = this;
  1076. var promises = new Array();
  1077. var primitives = mesh.primitives;
  1078. if (!primitives || primitives.length === 0) {
  1079. throw new Error(context + ": Primitives are missing");
  1080. }
  1081. GLTF2._ArrayItem.Assign(primitives);
  1082. if (primitives.length === 1) {
  1083. var primitive = primitives[0];
  1084. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, babylonMesh));
  1085. }
  1086. else {
  1087. node._primitiveBabylonMeshes = [];
  1088. for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
  1089. var primitive = primitives_1[_i];
  1090. var primitiveBabylonMesh = new BABYLON.Mesh((mesh.name || babylonMesh.name) + "_" + primitive._index, this._babylonScene, babylonMesh);
  1091. node._primitiveBabylonMeshes.push(primitiveBabylonMesh);
  1092. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, primitiveBabylonMesh));
  1093. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  1094. }
  1095. }
  1096. if (node.skin != undefined) {
  1097. var skin = GLTFLoader._GetProperty(context + "/skin", this._gltf.skins, node.skin);
  1098. promises.push(this._loadSkinAsync("#/skins/" + skin._index, node, mesh, skin));
  1099. }
  1100. return Promise.all(promises).then(function () {
  1101. _this._forEachPrimitive(node, function (babylonMesh) {
  1102. babylonMesh._refreshBoundingInfo(true);
  1103. });
  1104. });
  1105. };
  1106. GLTFLoader.prototype._loadPrimitiveAsync = function (context, node, mesh, primitive, babylonMesh) {
  1107. var _this = this;
  1108. var promises = new Array();
  1109. this._createMorphTargets(context, node, mesh, primitive, babylonMesh);
  1110. promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh).then(function (babylonGeometry) {
  1111. return _this._loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry).then(function () {
  1112. babylonGeometry.applyToMesh(babylonMesh);
  1113. });
  1114. }));
  1115. var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
  1116. if (primitive.material == undefined) {
  1117. babylonMesh.material = this._getDefaultMaterial(babylonDrawMode);
  1118. }
  1119. else {
  1120. var material = GLTFLoader._GetProperty(context + "/material}", this._gltf.materials, primitive.material);
  1121. promises.push(this._loadMaterialAsync("#/materials/" + material._index, material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
  1122. babylonMesh.material = babylonMaterial;
  1123. }));
  1124. }
  1125. return Promise.all(promises).then(function () { });
  1126. };
  1127. GLTFLoader.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  1128. var _this = this;
  1129. var promise = GLTF2.GLTFLoaderExtension._LoadVertexDataAsync(this, context, primitive, babylonMesh);
  1130. if (promise) {
  1131. return promise;
  1132. }
  1133. var attributes = primitive.attributes;
  1134. if (!attributes) {
  1135. throw new Error(context + ": Attributes are missing");
  1136. }
  1137. var promises = new Array();
  1138. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, this._babylonScene);
  1139. if (primitive.indices == undefined) {
  1140. babylonMesh.isUnIndexed = true;
  1141. }
  1142. else {
  1143. var accessor = GLTFLoader._GetProperty(context + "/indices", this._gltf.accessors, primitive.indices);
  1144. promises.push(this._loadIndicesAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1145. babylonGeometry.setIndices(data);
  1146. }));
  1147. }
  1148. var loadAttribute = function (attribute, kind, callback) {
  1149. if (attributes[attribute] == undefined) {
  1150. return;
  1151. }
  1152. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  1153. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  1154. babylonMesh._delayInfo.push(kind);
  1155. }
  1156. var accessor = GLTFLoader._GetProperty(context + "/attributes/" + attribute, _this._gltf.accessors, attributes[attribute]);
  1157. promises.push(_this._loadVertexAccessorAsync("#/accessors/" + accessor._index, accessor, kind).then(function (babylonVertexBuffer) {
  1158. babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
  1159. }));
  1160. if (callback) {
  1161. callback(accessor);
  1162. }
  1163. };
  1164. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  1165. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  1166. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  1167. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  1168. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  1169. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  1170. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  1171. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind, function (accessor) {
  1172. if (accessor.type === "VEC4" /* VEC4 */) {
  1173. babylonMesh.hasVertexAlpha = true;
  1174. }
  1175. });
  1176. return Promise.all(promises).then(function () {
  1177. return babylonGeometry;
  1178. });
  1179. };
  1180. GLTFLoader.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
  1181. if (!primitive.targets) {
  1182. return;
  1183. }
  1184. if (node._numMorphTargets == undefined) {
  1185. node._numMorphTargets = primitive.targets.length;
  1186. }
  1187. else if (primitive.targets.length !== node._numMorphTargets) {
  1188. throw new Error(context + ": Primitives do not have the same number of targets");
  1189. }
  1190. babylonMesh.morphTargetManager = new BABYLON.MorphTargetManager();
  1191. for (var index = 0; index < primitive.targets.length; index++) {
  1192. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  1193. babylonMesh.morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  1194. // TODO: tell the target whether it has positions, normals, tangents
  1195. }
  1196. };
  1197. GLTFLoader.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonGeometry) {
  1198. if (!primitive.targets) {
  1199. return Promise.resolve();
  1200. }
  1201. var promises = new Array();
  1202. var morphTargetManager = babylonMesh.morphTargetManager;
  1203. for (var index = 0; index < morphTargetManager.numTargets; index++) {
  1204. var babylonMorphTarget = morphTargetManager.getTarget(index);
  1205. promises.push(this._loadMorphTargetVertexDataAsync(context + "/targets/" + index, babylonGeometry, primitive.targets[index], babylonMorphTarget));
  1206. }
  1207. return Promise.all(promises).then(function () { });
  1208. };
  1209. GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, babylonGeometry, attributes, babylonMorphTarget) {
  1210. var _this = this;
  1211. var promises = new Array();
  1212. var loadAttribute = function (attribute, kind, setData) {
  1213. if (attributes[attribute] == undefined) {
  1214. return;
  1215. }
  1216. var babylonVertexBuffer = babylonGeometry.getVertexBuffer(kind);
  1217. if (!babylonVertexBuffer) {
  1218. return;
  1219. }
  1220. var accessor = GLTFLoader._GetProperty(context + "/" + attribute, _this._gltf.accessors, attributes[attribute]);
  1221. promises.push(_this._loadFloatAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1222. setData(babylonVertexBuffer, data);
  1223. }));
  1224. };
  1225. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind, function (babylonVertexBuffer, data) {
  1226. babylonVertexBuffer.forEach(data.length, function (value, index) {
  1227. data[index] += value;
  1228. });
  1229. babylonMorphTarget.setPositions(data);
  1230. });
  1231. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind, function (babylonVertexBuffer, data) {
  1232. babylonVertexBuffer.forEach(data.length, function (value, index) {
  1233. data[index] += value;
  1234. });
  1235. babylonMorphTarget.setNormals(data);
  1236. });
  1237. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind, function (babylonVertexBuffer, data) {
  1238. var dataIndex = 0;
  1239. babylonVertexBuffer.forEach(data.length / 3 * 4, function (value, index) {
  1240. // Tangent data for morph targets is stored as xyz delta.
  1241. // The vertexData.tangent is stored as xyzw.
  1242. // So we need to skip every fourth vertexData.tangent.
  1243. if (((index + 1) % 4) !== 0) {
  1244. data[dataIndex++] += value;
  1245. }
  1246. });
  1247. babylonMorphTarget.setTangents(data);
  1248. });
  1249. return Promise.all(promises).then(function () { });
  1250. };
  1251. GLTFLoader._LoadTransform = function (node, babylonNode) {
  1252. var position = BABYLON.Vector3.Zero();
  1253. var rotation = BABYLON.Quaternion.Identity();
  1254. var scaling = BABYLON.Vector3.One();
  1255. if (node.matrix) {
  1256. var matrix = BABYLON.Matrix.FromArray(node.matrix);
  1257. matrix.decompose(scaling, rotation, position);
  1258. }
  1259. else {
  1260. if (node.translation)
  1261. position = BABYLON.Vector3.FromArray(node.translation);
  1262. if (node.rotation)
  1263. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  1264. if (node.scale)
  1265. scaling = BABYLON.Vector3.FromArray(node.scale);
  1266. }
  1267. babylonNode.position = position;
  1268. babylonNode.rotationQuaternion = rotation;
  1269. babylonNode.scaling = scaling;
  1270. };
  1271. GLTFLoader.prototype._loadSkinAsync = function (context, node, mesh, skin) {
  1272. var _this = this;
  1273. var assignSkeleton = function (skeleton) {
  1274. _this._forEachPrimitive(node, function (babylonMesh) {
  1275. babylonMesh.skeleton = skeleton;
  1276. });
  1277. // Ignore the TRS of skinned nodes.
  1278. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  1279. node._babylonMesh.parent = _this._rootBabylonMesh;
  1280. node._babylonMesh.position = BABYLON.Vector3.Zero();
  1281. node._babylonMesh.rotationQuaternion = BABYLON.Quaternion.Identity();
  1282. node._babylonMesh.scaling = BABYLON.Vector3.One();
  1283. };
  1284. if (skin._loaded) {
  1285. return skin._loaded.then(function () {
  1286. assignSkeleton(skin._babylonSkeleton);
  1287. });
  1288. }
  1289. var skeletonId = "skeleton" + skin._index;
  1290. var babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
  1291. skin._babylonSkeleton = babylonSkeleton;
  1292. this._loadBones(context, skin);
  1293. assignSkeleton(babylonSkeleton);
  1294. return (skin._loaded = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
  1295. _this._updateBoneMatrices(babylonSkeleton, inverseBindMatricesData);
  1296. }));
  1297. };
  1298. GLTFLoader.prototype._loadBones = function (context, skin) {
  1299. var babylonBones = {};
  1300. for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
  1301. var index = _a[_i];
  1302. var node = GLTFLoader._GetProperty(context + "/joints/" + index, this._gltf.nodes, index);
  1303. this._loadBone(node, skin, babylonBones);
  1304. }
  1305. };
  1306. GLTFLoader.prototype._loadBone = function (node, skin, babylonBones) {
  1307. var babylonBone = babylonBones[node._index];
  1308. if (babylonBone) {
  1309. return babylonBone;
  1310. }
  1311. var babylonParentBone = null;
  1312. if (node._parent._babylonMesh !== this._rootBabylonMesh) {
  1313. babylonParentBone = this._loadBone(node._parent, skin, babylonBones);
  1314. }
  1315. var boneIndex = skin.joints.indexOf(node._index);
  1316. babylonBone = new BABYLON.Bone(node.name || "joint" + node._index, skin._babylonSkeleton, babylonParentBone, this._getNodeMatrix(node), null, null, boneIndex);
  1317. babylonBones[node._index] = babylonBone;
  1318. node._babylonBones = node._babylonBones || [];
  1319. node._babylonBones.push(babylonBone);
  1320. return babylonBone;
  1321. };
  1322. GLTFLoader.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
  1323. if (skin.inverseBindMatrices == undefined) {
  1324. return Promise.resolve(null);
  1325. }
  1326. var accessor = GLTFLoader._GetProperty(context + "/inverseBindMatrices", this._gltf.accessors, skin.inverseBindMatrices);
  1327. return this._loadFloatAccessorAsync("#/accessors/" + accessor._index, accessor);
  1328. };
  1329. GLTFLoader.prototype._updateBoneMatrices = function (babylonSkeleton, inverseBindMatricesData) {
  1330. for (var _i = 0, _a = babylonSkeleton.bones; _i < _a.length; _i++) {
  1331. var babylonBone = _a[_i];
  1332. var baseMatrix = BABYLON.Matrix.Identity();
  1333. var boneIndex = babylonBone._index;
  1334. if (inverseBindMatricesData && boneIndex !== -1) {
  1335. BABYLON.Matrix.FromArrayToRef(inverseBindMatricesData, boneIndex * 16, baseMatrix);
  1336. baseMatrix.invertToRef(baseMatrix);
  1337. }
  1338. var babylonParentBone = babylonBone.getParent();
  1339. if (babylonParentBone) {
  1340. baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
  1341. }
  1342. babylonBone.updateMatrix(baseMatrix, false, false);
  1343. babylonBone._updateDifferenceMatrix(undefined, false);
  1344. }
  1345. };
  1346. GLTFLoader.prototype._getNodeMatrix = function (node) {
  1347. return node.matrix ?
  1348. BABYLON.Matrix.FromArray(node.matrix) :
  1349. 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());
  1350. };
  1351. GLTFLoader.prototype._loadCamera = function (context, camera, babylonMesh) {
  1352. var babylonCamera = new BABYLON.FreeCamera(camera.name || "camera" + camera._index, BABYLON.Vector3.Zero(), this._babylonScene, false);
  1353. babylonCamera.parent = babylonMesh;
  1354. babylonCamera.rotation = new BABYLON.Vector3(0, Math.PI, 0);
  1355. switch (camera.type) {
  1356. case "perspective" /* PERSPECTIVE */: {
  1357. var perspective = camera.perspective;
  1358. if (!perspective) {
  1359. throw new Error(context + ": Camera perspective properties are missing");
  1360. }
  1361. babylonCamera.fov = perspective.yfov;
  1362. babylonCamera.minZ = perspective.znear;
  1363. babylonCamera.maxZ = perspective.zfar || Number.MAX_VALUE;
  1364. break;
  1365. }
  1366. case "orthographic" /* ORTHOGRAPHIC */: {
  1367. if (!camera.orthographic) {
  1368. throw new Error(context + ": Camera orthographic properties are missing");
  1369. }
  1370. babylonCamera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
  1371. babylonCamera.orthoLeft = -camera.orthographic.xmag;
  1372. babylonCamera.orthoRight = camera.orthographic.xmag;
  1373. babylonCamera.orthoBottom = -camera.orthographic.ymag;
  1374. babylonCamera.orthoTop = camera.orthographic.ymag;
  1375. babylonCamera.minZ = camera.orthographic.znear;
  1376. babylonCamera.maxZ = camera.orthographic.zfar;
  1377. break;
  1378. }
  1379. default: {
  1380. throw new Error(context + ": Invalid camera type (" + camera.type + ")");
  1381. }
  1382. }
  1383. this.onCameraLoadedObservable.notifyObservers(babylonCamera);
  1384. };
  1385. GLTFLoader.prototype._loadAnimationsAsync = function () {
  1386. var animations = this._gltf.animations;
  1387. if (!animations) {
  1388. return Promise.resolve();
  1389. }
  1390. var promises = new Array();
  1391. for (var index = 0; index < animations.length; index++) {
  1392. var animation = animations[index];
  1393. promises.push(this._loadAnimationAsync("#/animations/" + index, animation));
  1394. }
  1395. return Promise.all(promises).then(function () { });
  1396. };
  1397. GLTFLoader.prototype._loadAnimationAsync = function (context, animation) {
  1398. var _this = this;
  1399. var babylonAnimationGroup = new BABYLON.AnimationGroup(animation.name || "animation" + animation._index, this._babylonScene);
  1400. animation._babylonAnimationGroup = babylonAnimationGroup;
  1401. var promises = new Array();
  1402. GLTF2._ArrayItem.Assign(animation.channels);
  1403. GLTF2._ArrayItem.Assign(animation.samplers);
  1404. for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
  1405. var channel = _a[_i];
  1406. promises.push(this._loadAnimationChannelAsync(context + "/channels/" + channel._index, context, animation, channel, babylonAnimationGroup));
  1407. }
  1408. return Promise.all(promises).then(function () {
  1409. babylonAnimationGroup.normalize(_this._normalizeAnimationGroupsToBeginAtZero ? 0 : null);
  1410. });
  1411. };
  1412. GLTFLoader.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup) {
  1413. var _this = this;
  1414. var targetNode = GLTFLoader._GetProperty(context + "/target/node", this._gltf.nodes, channel.target.node);
  1415. // Ignore animations that have no animation targets.
  1416. if ((channel.target.path === "weights" /* WEIGHTS */ && !targetNode._numMorphTargets) ||
  1417. (channel.target.path !== "weights" /* WEIGHTS */ && !targetNode._babylonMesh)) {
  1418. return Promise.resolve();
  1419. }
  1420. // Ignore animations targeting TRS of skinned nodes.
  1421. // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
  1422. if (targetNode.skin != undefined && channel.target.path !== "weights" /* WEIGHTS */) {
  1423. return Promise.resolve();
  1424. }
  1425. var sampler = GLTFLoader._GetProperty(context + "/sampler", animation.samplers, channel.sampler);
  1426. return this._loadAnimationSamplerAsync(animationContext + "/samplers/" + channel.sampler, sampler).then(function (data) {
  1427. var targetPath;
  1428. var animationType;
  1429. switch (channel.target.path) {
  1430. case "translation" /* TRANSLATION */: {
  1431. targetPath = "position";
  1432. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1433. break;
  1434. }
  1435. case "rotation" /* ROTATION */: {
  1436. targetPath = "rotationQuaternion";
  1437. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  1438. break;
  1439. }
  1440. case "scale" /* SCALE */: {
  1441. targetPath = "scaling";
  1442. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1443. break;
  1444. }
  1445. case "weights" /* WEIGHTS */: {
  1446. targetPath = "influence";
  1447. animationType = BABYLON.Animation.ANIMATIONTYPE_FLOAT;
  1448. break;
  1449. }
  1450. default: {
  1451. throw new Error(context + ": Invalid target path (" + channel.target.path + ")");
  1452. }
  1453. }
  1454. var outputBufferOffset = 0;
  1455. var getNextOutputValue;
  1456. switch (targetPath) {
  1457. case "position": {
  1458. getNextOutputValue = function () {
  1459. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1460. outputBufferOffset += 3;
  1461. return value;
  1462. };
  1463. break;
  1464. }
  1465. case "rotationQuaternion": {
  1466. getNextOutputValue = function () {
  1467. var value = BABYLON.Quaternion.FromArray(data.output, outputBufferOffset);
  1468. outputBufferOffset += 4;
  1469. return value;
  1470. };
  1471. break;
  1472. }
  1473. case "scaling": {
  1474. getNextOutputValue = function () {
  1475. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1476. outputBufferOffset += 3;
  1477. return value;
  1478. };
  1479. break;
  1480. }
  1481. case "influence": {
  1482. getNextOutputValue = function () {
  1483. var value = new Array(targetNode._numMorphTargets);
  1484. for (var i = 0; i < targetNode._numMorphTargets; i++) {
  1485. value[i] = data.output[outputBufferOffset++];
  1486. }
  1487. return value;
  1488. };
  1489. break;
  1490. }
  1491. }
  1492. var getNextKey;
  1493. switch (data.interpolation) {
  1494. case "STEP" /* STEP */: {
  1495. getNextKey = function (frameIndex) { return ({
  1496. frame: data.input[frameIndex],
  1497. value: getNextOutputValue(),
  1498. interpolation: BABYLON.AnimationKeyInterpolation.STEP
  1499. }); };
  1500. break;
  1501. }
  1502. case "LINEAR" /* LINEAR */: {
  1503. getNextKey = function (frameIndex) { return ({
  1504. frame: data.input[frameIndex],
  1505. value: getNextOutputValue()
  1506. }); };
  1507. break;
  1508. }
  1509. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1510. getNextKey = function (frameIndex) { return ({
  1511. frame: data.input[frameIndex],
  1512. inTangent: getNextOutputValue(),
  1513. value: getNextOutputValue(),
  1514. outTangent: getNextOutputValue()
  1515. }); };
  1516. break;
  1517. }
  1518. }
  1519. var keys = new Array(data.input.length);
  1520. for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
  1521. keys[frameIndex] = getNextKey(frameIndex);
  1522. }
  1523. if (targetPath === "influence") {
  1524. var _loop_1 = function (targetIndex) {
  1525. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1526. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1527. babylonAnimation.setKeys(keys.map(function (key) { return ({
  1528. frame: key.frame,
  1529. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  1530. value: key.value[targetIndex],
  1531. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  1532. }); }));
  1533. _this._forEachPrimitive(targetNode, function (babylonMesh) {
  1534. var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
  1535. var babylonAnimationClone = babylonAnimation.clone();
  1536. morphTarget.animations.push(babylonAnimationClone);
  1537. babylonAnimationGroup.addTargetedAnimation(babylonAnimationClone, morphTarget);
  1538. });
  1539. };
  1540. for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
  1541. _loop_1(targetIndex);
  1542. }
  1543. }
  1544. else {
  1545. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1546. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1547. babylonAnimation.setKeys(keys);
  1548. if (targetNode._babylonBones) {
  1549. var babylonAnimationTargets = [targetNode._babylonMesh].concat(targetNode._babylonBones);
  1550. for (var _i = 0, babylonAnimationTargets_1 = babylonAnimationTargets; _i < babylonAnimationTargets_1.length; _i++) {
  1551. var babylonAnimationTarget = babylonAnimationTargets_1[_i];
  1552. babylonAnimationTarget.animations.push(babylonAnimation);
  1553. }
  1554. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, babylonAnimationTargets);
  1555. }
  1556. else {
  1557. targetNode._babylonMesh.animations.push(babylonAnimation);
  1558. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, targetNode._babylonMesh);
  1559. }
  1560. }
  1561. });
  1562. };
  1563. GLTFLoader.prototype._loadAnimationSamplerAsync = function (context, sampler) {
  1564. if (sampler._data) {
  1565. return sampler._data;
  1566. }
  1567. var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
  1568. switch (interpolation) {
  1569. case "STEP" /* STEP */:
  1570. case "LINEAR" /* LINEAR */:
  1571. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1572. break;
  1573. }
  1574. default: {
  1575. throw new Error(context + ": Invalid interpolation (" + sampler.interpolation + ")");
  1576. }
  1577. }
  1578. var inputAccessor = GLTFLoader._GetProperty(context + "/input", this._gltf.accessors, sampler.input);
  1579. var outputAccessor = GLTFLoader._GetProperty(context + "/output", this._gltf.accessors, sampler.output);
  1580. sampler._data = Promise.all([
  1581. this._loadFloatAccessorAsync("#/accessors/" + inputAccessor._index, inputAccessor),
  1582. this._loadFloatAccessorAsync("#/accessors/" + outputAccessor._index, outputAccessor)
  1583. ]).then(function (_a) {
  1584. var inputData = _a[0], outputData = _a[1];
  1585. return {
  1586. input: inputData,
  1587. interpolation: interpolation,
  1588. output: outputData,
  1589. };
  1590. });
  1591. return sampler._data;
  1592. };
  1593. GLTFLoader.prototype._loadBufferAsync = function (context, buffer) {
  1594. if (buffer._data) {
  1595. return buffer._data;
  1596. }
  1597. if (!buffer.uri) {
  1598. throw new Error(context + ": Uri is missing");
  1599. }
  1600. buffer._data = this._loadUriAsync(context, buffer.uri);
  1601. return buffer._data;
  1602. };
  1603. /** @hidden */
  1604. GLTFLoader.prototype._loadBufferViewAsync = function (context, bufferView) {
  1605. if (bufferView._data) {
  1606. return bufferView._data;
  1607. }
  1608. var buffer = GLTFLoader._GetProperty(context + "/buffer", this._gltf.buffers, bufferView.buffer);
  1609. bufferView._data = this._loadBufferAsync("#/buffers/" + buffer._index, buffer).then(function (data) {
  1610. try {
  1611. return new Uint8Array(data.buffer, data.byteOffset + (bufferView.byteOffset || 0), bufferView.byteLength);
  1612. }
  1613. catch (e) {
  1614. throw new Error(context + ": " + e.message);
  1615. }
  1616. });
  1617. return bufferView._data;
  1618. };
  1619. GLTFLoader.prototype._loadIndicesAccessorAsync = function (context, accessor) {
  1620. if (accessor.type !== "SCALAR" /* SCALAR */) {
  1621. throw new Error(context + ": Invalid type " + accessor.type);
  1622. }
  1623. if (accessor.componentType !== 5121 /* UNSIGNED_BYTE */ &&
  1624. accessor.componentType !== 5123 /* UNSIGNED_SHORT */ &&
  1625. accessor.componentType !== 5125 /* UNSIGNED_INT */) {
  1626. throw new Error(context + ": Invalid component type " + accessor.componentType);
  1627. }
  1628. if (accessor._data) {
  1629. return accessor._data;
  1630. }
  1631. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1632. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  1633. return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, accessor.count);
  1634. });
  1635. return accessor._data;
  1636. };
  1637. GLTFLoader.prototype._loadFloatAccessorAsync = function (context, accessor) {
  1638. // TODO: support normalized and stride
  1639. var _this = this;
  1640. if (accessor.componentType !== 5126 /* FLOAT */) {
  1641. throw new Error("Invalid component type " + accessor.componentType);
  1642. }
  1643. if (accessor._data) {
  1644. return accessor._data;
  1645. }
  1646. var numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
  1647. var length = numComponents * accessor.count;
  1648. if (accessor.bufferView == undefined) {
  1649. accessor._data = Promise.resolve(new Float32Array(length));
  1650. }
  1651. else {
  1652. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1653. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  1654. return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, length);
  1655. });
  1656. }
  1657. if (accessor.sparse) {
  1658. var sparse_1 = accessor.sparse;
  1659. accessor._data = accessor._data.then(function (data) {
  1660. var indicesBufferView = GLTFLoader._GetProperty(context + "/sparse/indices/bufferView", _this._gltf.bufferViews, sparse_1.indices.bufferView);
  1661. var valuesBufferView = GLTFLoader._GetProperty(context + "/sparse/values/bufferView", _this._gltf.bufferViews, sparse_1.values.bufferView);
  1662. return Promise.all([
  1663. _this._loadBufferViewAsync("#/bufferViews/" + indicesBufferView._index, indicesBufferView),
  1664. _this._loadBufferViewAsync("#/bufferViews/" + valuesBufferView._index, valuesBufferView)
  1665. ]).then(function (_a) {
  1666. var indicesData = _a[0], valuesData = _a[1];
  1667. var indices = GLTFLoader._GetTypedArray(context + "/sparse/indices", sparse_1.indices.componentType, indicesData, sparse_1.indices.byteOffset, sparse_1.count);
  1668. var values = GLTFLoader._GetTypedArray(context + "/sparse/values", accessor.componentType, valuesData, sparse_1.values.byteOffset, numComponents * sparse_1.count);
  1669. var valuesIndex = 0;
  1670. for (var indicesIndex = 0; indicesIndex < indices.length; indicesIndex++) {
  1671. var dataIndex = indices[indicesIndex] * numComponents;
  1672. for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
  1673. data[dataIndex++] = values[valuesIndex++];
  1674. }
  1675. }
  1676. return data;
  1677. });
  1678. });
  1679. }
  1680. return accessor._data;
  1681. };
  1682. /** @hidden */
  1683. GLTFLoader.prototype._loadVertexBufferViewAsync = function (context, bufferView, kind) {
  1684. var _this = this;
  1685. if (bufferView._babylonBuffer) {
  1686. return bufferView._babylonBuffer;
  1687. }
  1688. bufferView._babylonBuffer = this._loadBufferViewAsync(context, bufferView).then(function (data) {
  1689. return new BABYLON.Buffer(_this._babylonScene.getEngine(), data, false);
  1690. });
  1691. return bufferView._babylonBuffer;
  1692. };
  1693. GLTFLoader.prototype._loadVertexAccessorAsync = function (context, accessor, kind) {
  1694. var _this = this;
  1695. if (accessor._babylonVertexBuffer) {
  1696. return accessor._babylonVertexBuffer;
  1697. }
  1698. if (accessor.sparse) {
  1699. accessor._babylonVertexBuffer = this._loadFloatAccessorAsync(context, accessor).then(function (data) {
  1700. return new BABYLON.VertexBuffer(_this._babylonScene.getEngine(), data, kind, false);
  1701. });
  1702. }
  1703. else {
  1704. var bufferView_1 = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1705. accessor._babylonVertexBuffer = this._loadVertexBufferViewAsync("#/bufferViews/" + bufferView_1._index, bufferView_1, kind).then(function (buffer) {
  1706. var size = GLTFLoader._GetNumComponents(context, accessor.type);
  1707. return new BABYLON.VertexBuffer(_this._babylonScene.getEngine(), buffer, kind, false, false, bufferView_1.byteStride, false, accessor.byteOffset, size, accessor.componentType, accessor.normalized, true);
  1708. });
  1709. }
  1710. return accessor._babylonVertexBuffer;
  1711. };
  1712. GLTFLoader.prototype._getDefaultMaterial = function (drawMode) {
  1713. var babylonMaterial = this._defaultBabylonMaterials[drawMode];
  1714. if (!babylonMaterial) {
  1715. babylonMaterial = this._createMaterial("__gltf_default", drawMode);
  1716. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1717. babylonMaterial.metallic = 1;
  1718. babylonMaterial.roughness = 1;
  1719. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1720. }
  1721. return babylonMaterial;
  1722. };
  1723. GLTFLoader.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, material, babylonMaterial) {
  1724. var promises = new Array();
  1725. // Ensure metallic workflow
  1726. babylonMaterial.metallic = 1;
  1727. babylonMaterial.roughness = 1;
  1728. var properties = material.pbrMetallicRoughness;
  1729. if (properties) {
  1730. if (properties.baseColorFactor) {
  1731. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  1732. babylonMaterial.alpha = properties.baseColorFactor[3];
  1733. }
  1734. else {
  1735. babylonMaterial.albedoColor = BABYLON.Color3.White();
  1736. }
  1737. babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
  1738. babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
  1739. if (properties.baseColorTexture) {
  1740. promises.push(this._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  1741. babylonMaterial.albedoTexture = texture;
  1742. }));
  1743. }
  1744. if (properties.metallicRoughnessTexture) {
  1745. promises.push(this._loadTextureAsync(context + "/metallicRoughnessTexture", properties.metallicRoughnessTexture, function (texture) {
  1746. babylonMaterial.metallicTexture = texture;
  1747. }));
  1748. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  1749. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  1750. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  1751. }
  1752. }
  1753. this._loadMaterialAlphaProperties(context, material, babylonMaterial);
  1754. return Promise.all(promises).then(function () { });
  1755. };
  1756. /** @hidden */
  1757. GLTFLoader.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  1758. var promise = GLTF2.GLTFLoaderExtension._LoadMaterialAsync(this, context, material, babylonMesh, babylonDrawMode, assign);
  1759. if (promise) {
  1760. return promise;
  1761. }
  1762. material._babylonData = material._babylonData || {};
  1763. var babylonData = material._babylonData[babylonDrawMode];
  1764. if (!babylonData) {
  1765. var promises = new Array();
  1766. var name_3 = material.name || "materialSG_" + material._index;
  1767. var babylonMaterial = this._createMaterial(name_3, babylonDrawMode);
  1768. promises.push(this._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  1769. promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync(context, material, babylonMaterial));
  1770. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1771. babylonData = {
  1772. material: babylonMaterial,
  1773. meshes: [],
  1774. loaded: Promise.all(promises).then(function () { })
  1775. };
  1776. material._babylonData[babylonDrawMode] = babylonData;
  1777. }
  1778. babylonData.meshes.push(babylonMesh);
  1779. assign(babylonData.material);
  1780. return babylonData.loaded;
  1781. };
  1782. /** @hidden */
  1783. GLTFLoader.prototype._createMaterial = function (name, drawMode) {
  1784. var babylonMaterial = new BABYLON.PBRMaterial(name, this._babylonScene);
  1785. babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? BABYLON.Material.CounterClockWiseSideOrientation : BABYLON.Material.ClockWiseSideOrientation;
  1786. babylonMaterial.fillMode = drawMode;
  1787. babylonMaterial.enableSpecularAntiAliasing = true;
  1788. babylonMaterial.useRadianceOverAlpha = !this.transparencyAsCoverage;
  1789. babylonMaterial.useSpecularOverAlpha = !this.transparencyAsCoverage;
  1790. return babylonMaterial;
  1791. };
  1792. /** @hidden */
  1793. GLTFLoader.prototype._loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
  1794. var promises = new Array();
  1795. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  1796. if (material.doubleSided) {
  1797. babylonMaterial.backFaceCulling = false;
  1798. babylonMaterial.twoSidedLighting = true;
  1799. }
  1800. if (material.normalTexture) {
  1801. promises.push(this._loadTextureAsync(context + "/normalTexture", material.normalTexture, function (texture) {
  1802. babylonMaterial.bumpTexture = texture;
  1803. }));
  1804. babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
  1805. babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
  1806. if (material.normalTexture.scale != undefined) {
  1807. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  1808. }
  1809. }
  1810. if (material.occlusionTexture) {
  1811. promises.push(this._loadTextureAsync(context + "/occlusionTexture", material.occlusionTexture, function (texture) {
  1812. babylonMaterial.ambientTexture = texture;
  1813. }));
  1814. babylonMaterial.useAmbientInGrayScale = true;
  1815. if (material.occlusionTexture.strength != undefined) {
  1816. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  1817. }
  1818. }
  1819. if (material.emissiveTexture) {
  1820. promises.push(this._loadTextureAsync(context + "/emissiveTexture", material.emissiveTexture, function (texture) {
  1821. babylonMaterial.emissiveTexture = texture;
  1822. }));
  1823. }
  1824. return Promise.all(promises).then(function () { });
  1825. };
  1826. /** @hidden */
  1827. GLTFLoader.prototype._loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
  1828. var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
  1829. switch (alphaMode) {
  1830. case "OPAQUE" /* OPAQUE */: {
  1831. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1832. break;
  1833. }
  1834. case "MASK" /* MASK */: {
  1835. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  1836. babylonMaterial.alphaCutOff = (material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff);
  1837. if (babylonMaterial.albedoTexture) {
  1838. babylonMaterial.albedoTexture.hasAlpha = true;
  1839. }
  1840. break;
  1841. }
  1842. case "BLEND" /* BLEND */: {
  1843. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  1844. if (babylonMaterial.albedoTexture) {
  1845. babylonMaterial.albedoTexture.hasAlpha = true;
  1846. babylonMaterial.useAlphaFromAlbedoTexture = true;
  1847. }
  1848. break;
  1849. }
  1850. default: {
  1851. throw new Error(context + ": Invalid alpha mode (" + material.alphaMode + ")");
  1852. }
  1853. }
  1854. };
  1855. /** @hidden */
  1856. GLTFLoader.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  1857. var _this = this;
  1858. var promise = GLTF2.GLTFLoaderExtension._LoadTextureAsync(this, context, textureInfo, assign);
  1859. if (promise) {
  1860. return promise;
  1861. }
  1862. var texture = GLTFLoader._GetProperty(context + "/index", this._gltf.textures, textureInfo.index);
  1863. context = "#/textures/" + textureInfo.index;
  1864. var promises = new Array();
  1865. var sampler = (texture.sampler == undefined ? this._defaultSampler : GLTFLoader._GetProperty(context + "/sampler", this._gltf.samplers, texture.sampler));
  1866. var samplerData = this._loadSampler("#/samplers/" + sampler._index, sampler);
  1867. var deferred = new BABYLON.Deferred();
  1868. var babylonTexture = new BABYLON.Texture(null, this._babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, function () {
  1869. if (!_this._disposed) {
  1870. deferred.resolve();
  1871. }
  1872. }, function (message, exception) {
  1873. if (!_this._disposed) {
  1874. deferred.reject(new Error(context + ": " + ((exception && exception.message) ? exception.message : message || "Failed to load texture")));
  1875. }
  1876. });
  1877. promises.push(deferred.promise);
  1878. babylonTexture.name = texture.name || "texture" + texture._index;
  1879. babylonTexture.wrapU = samplerData.wrapU;
  1880. babylonTexture.wrapV = samplerData.wrapV;
  1881. babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
  1882. var image = GLTFLoader._GetProperty(context + "/source", this._gltf.images, texture.source);
  1883. promises.push(this._loadImageAsync("#/images/" + image._index, image).then(function (objectURL) {
  1884. babylonTexture.updateURL(objectURL);
  1885. }));
  1886. assign(babylonTexture);
  1887. this.onTextureLoadedObservable.notifyObservers(babylonTexture);
  1888. return Promise.all(promises).then(function () { });
  1889. };
  1890. GLTFLoader.prototype._loadSampler = function (context, sampler) {
  1891. if (!sampler._data) {
  1892. sampler._data = {
  1893. noMipMaps: (sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */),
  1894. samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler.magFilter, sampler.minFilter),
  1895. wrapU: GLTFLoader._GetTextureWrapMode(context, sampler.wrapS),
  1896. wrapV: GLTFLoader._GetTextureWrapMode(context, sampler.wrapT)
  1897. };
  1898. }
  1899. ;
  1900. return sampler._data;
  1901. };
  1902. GLTFLoader.prototype._loadImageAsync = function (context, image) {
  1903. if (image._objectURL) {
  1904. return image._objectURL;
  1905. }
  1906. var promise;
  1907. if (image.uri) {
  1908. promise = this._loadUriAsync(context, image.uri);
  1909. }
  1910. else {
  1911. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, image.bufferView);
  1912. promise = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView);
  1913. }
  1914. image._objectURL = promise.then(function (data) {
  1915. return URL.createObjectURL(new Blob([data], { type: image.mimeType }));
  1916. });
  1917. return image._objectURL;
  1918. };
  1919. /** @hidden */
  1920. GLTFLoader.prototype._loadUriAsync = function (context, uri) {
  1921. var _this = this;
  1922. var promise = GLTF2.GLTFLoaderExtension._LoadUriAsync(this, context, uri);
  1923. if (promise) {
  1924. return promise;
  1925. }
  1926. if (!GLTFLoader._ValidateUri(uri)) {
  1927. throw new Error(context + ": Uri '" + uri + "' is invalid");
  1928. }
  1929. if (BABYLON.Tools.IsBase64(uri)) {
  1930. return Promise.resolve(new Uint8Array(BABYLON.Tools.DecodeBase64(uri)));
  1931. }
  1932. return this.preprocessUrlAsync(this._rootUrl + uri).then(function (url) {
  1933. return new Promise(function (resolve, reject) {
  1934. if (!_this._disposed) {
  1935. var request_1 = BABYLON.Tools.LoadFile(url, function (data) {
  1936. if (!_this._disposed) {
  1937. resolve(new Uint8Array(data));
  1938. }
  1939. }, function (event) {
  1940. if (!_this._disposed) {
  1941. try {
  1942. if (request_1 && _this._state === BABYLON.GLTFLoaderState.LOADING) {
  1943. request_1._lengthComputable = event.lengthComputable;
  1944. request_1._loaded = event.loaded;
  1945. request_1._total = event.total;
  1946. _this._onProgress();
  1947. }
  1948. }
  1949. catch (e) {
  1950. reject(e);
  1951. }
  1952. }
  1953. }, _this._babylonScene.database, true, function (request, exception) {
  1954. if (!_this._disposed) {
  1955. reject(new BABYLON.LoadFileError(context + ": Failed to load '" + uri + "'" + (request ? ": " + request.status + " " + request.statusText : ""), request));
  1956. }
  1957. });
  1958. _this._requests.push(request_1);
  1959. }
  1960. });
  1961. });
  1962. };
  1963. GLTFLoader.prototype._onProgress = function () {
  1964. if (!this._progressCallback) {
  1965. return;
  1966. }
  1967. var lengthComputable = true;
  1968. var loaded = 0;
  1969. var total = 0;
  1970. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  1971. var request = _a[_i];
  1972. if (request._lengthComputable === undefined || request._loaded === undefined || request._total === undefined) {
  1973. return;
  1974. }
  1975. lengthComputable = lengthComputable && request._lengthComputable;
  1976. loaded += request._loaded;
  1977. total += request._total;
  1978. }
  1979. this._progressCallback(new BABYLON.SceneLoaderProgressEvent(lengthComputable, loaded, lengthComputable ? total : 0));
  1980. };
  1981. /** @hidden */
  1982. GLTFLoader._GetProperty = function (context, array, index) {
  1983. if (!array || index == undefined || !array[index]) {
  1984. throw new Error(context + ": Failed to find index (" + index + ")");
  1985. }
  1986. return array[index];
  1987. };
  1988. GLTFLoader._GetTextureWrapMode = function (context, mode) {
  1989. // Set defaults if undefined
  1990. mode = mode == undefined ? 10497 /* REPEAT */ : mode;
  1991. switch (mode) {
  1992. case 33071 /* CLAMP_TO_EDGE */: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  1993. case 33648 /* MIRRORED_REPEAT */: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  1994. case 10497 /* REPEAT */: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1995. default:
  1996. BABYLON.Tools.Warn(context + ": Invalid texture wrap mode (" + mode + ")");
  1997. return BABYLON.Texture.WRAP_ADDRESSMODE;
  1998. }
  1999. };
  2000. GLTFLoader._GetTextureSamplingMode = function (context, magFilter, minFilter) {
  2001. // Set defaults if undefined
  2002. magFilter = magFilter == undefined ? 9729 /* LINEAR */ : magFilter;
  2003. minFilter = minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : minFilter;
  2004. if (magFilter === 9729 /* LINEAR */) {
  2005. switch (minFilter) {
  2006. case 9728 /* NEAREST */: return BABYLON.Texture.LINEAR_NEAREST;
  2007. case 9729 /* LINEAR */: return BABYLON.Texture.LINEAR_LINEAR;
  2008. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_NEAREST_MIPNEAREST;
  2009. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_LINEAR_MIPNEAREST;
  2010. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_NEAREST_MIPLINEAR;
  2011. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  2012. default:
  2013. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  2014. return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  2015. }
  2016. }
  2017. else {
  2018. if (magFilter !== 9728 /* NEAREST */) {
  2019. BABYLON.Tools.Warn(context + ": Invalid texture magnification filter (" + magFilter + ")");
  2020. }
  2021. switch (minFilter) {
  2022. case 9728 /* NEAREST */: return BABYLON.Texture.NEAREST_NEAREST;
  2023. case 9729 /* LINEAR */: return BABYLON.Texture.NEAREST_LINEAR;
  2024. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  2025. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_LINEAR_MIPNEAREST;
  2026. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_NEAREST_MIPLINEAR;
  2027. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_LINEAR_MIPLINEAR;
  2028. default:
  2029. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  2030. return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  2031. }
  2032. }
  2033. };
  2034. GLTFLoader._GetTypedArray = function (context, componentType, bufferView, byteOffset, length) {
  2035. var buffer = bufferView.buffer;
  2036. byteOffset = bufferView.byteOffset + (byteOffset || 0);
  2037. try {
  2038. switch (componentType) {
  2039. case 5120 /* BYTE */: return new Int8Array(buffer, byteOffset, length);
  2040. case 5121 /* UNSIGNED_BYTE */: return new Uint8Array(buffer, byteOffset, length);
  2041. case 5122 /* SHORT */: return new Int16Array(buffer, byteOffset, length);
  2042. case 5123 /* UNSIGNED_SHORT */: return new Uint16Array(buffer, byteOffset, length);
  2043. case 5125 /* UNSIGNED_INT */: return new Uint32Array(buffer, byteOffset, length);
  2044. case 5126 /* FLOAT */: return new Float32Array(buffer, byteOffset, length);
  2045. default: throw new Error("Invalid component type " + componentType);
  2046. }
  2047. }
  2048. catch (e) {
  2049. throw new Error(context + ": " + e);
  2050. }
  2051. };
  2052. GLTFLoader._GetNumComponents = function (context, type) {
  2053. switch (type) {
  2054. case "SCALAR": return 1;
  2055. case "VEC2": return 2;
  2056. case "VEC3": return 3;
  2057. case "VEC4": return 4;
  2058. case "MAT2": return 4;
  2059. case "MAT3": return 9;
  2060. case "MAT4": return 16;
  2061. }
  2062. throw new Error(context + ": Invalid type (" + type + ")");
  2063. };
  2064. GLTFLoader._ValidateUri = function (uri) {
  2065. return (BABYLON.Tools.IsBase64(uri) || uri.indexOf("..") === -1);
  2066. };
  2067. GLTFLoader._GetDrawMode = function (context, mode) {
  2068. if (mode == undefined) {
  2069. mode = 4 /* TRIANGLES */;
  2070. }
  2071. switch (mode) {
  2072. case 0 /* POINTS */: return BABYLON.Material.PointListDrawMode;
  2073. case 1 /* LINES */: return BABYLON.Material.LineListDrawMode;
  2074. case 2 /* LINE_LOOP */: return BABYLON.Material.LineLoopDrawMode;
  2075. case 3 /* LINE_STRIP */: return BABYLON.Material.LineStripDrawMode;
  2076. case 4 /* TRIANGLES */: return BABYLON.Material.TriangleFillMode;
  2077. case 5 /* TRIANGLE_STRIP */: return BABYLON.Material.TriangleStripDrawMode;
  2078. case 6 /* TRIANGLE_FAN */: return BABYLON.Material.TriangleFanDrawMode;
  2079. }
  2080. throw new Error(context + ": Invalid mesh primitive mode (" + mode + ")");
  2081. };
  2082. GLTFLoader.prototype._compileMaterialsAsync = function () {
  2083. var promises = new Array();
  2084. if (this._gltf.materials) {
  2085. for (var _i = 0, _a = this._gltf.materials; _i < _a.length; _i++) {
  2086. var material = _a[_i];
  2087. if (material._babylonData) {
  2088. for (var babylonDrawMode in material._babylonData) {
  2089. var babylonData = material._babylonData[babylonDrawMode];
  2090. for (var _b = 0, _c = babylonData.meshes; _b < _c.length; _b++) {
  2091. var babylonMesh = _c[_b];
  2092. // Ensure nonUniformScaling is set if necessary.
  2093. babylonMesh.computeWorldMatrix(true);
  2094. var babylonMaterial = babylonData.material;
  2095. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
  2096. if (this.useClipPlane) {
  2097. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
  2098. }
  2099. }
  2100. }
  2101. }
  2102. }
  2103. }
  2104. return Promise.all(promises).then(function () { });
  2105. };
  2106. GLTFLoader.prototype._compileShadowGeneratorsAsync = function () {
  2107. var promises = new Array();
  2108. var lights = this._babylonScene.lights;
  2109. for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
  2110. var light = lights_1[_i];
  2111. var generator = light.getShadowGenerator();
  2112. if (generator) {
  2113. promises.push(generator.forceCompilationAsync());
  2114. }
  2115. }
  2116. return Promise.all(promises).then(function () { });
  2117. };
  2118. GLTFLoader.prototype._clear = function () {
  2119. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  2120. var request = _a[_i];
  2121. request.abort();
  2122. }
  2123. this._requests.length = 0;
  2124. if (this._gltf && this._gltf.images) {
  2125. for (var _b = 0, _c = this._gltf.images; _b < _c.length; _b++) {
  2126. var image = _c[_b];
  2127. if (image._objectURL) {
  2128. image._objectURL.then(function (value) {
  2129. URL.revokeObjectURL(value);
  2130. });
  2131. image._objectURL = undefined;
  2132. }
  2133. }
  2134. }
  2135. delete this._gltf;
  2136. delete this._babylonScene;
  2137. this._completePromises.length = 0;
  2138. for (var name_4 in this._extensions) {
  2139. this._extensions[name_4].dispose();
  2140. }
  2141. this._extensions = {};
  2142. delete this._rootBabylonMesh;
  2143. delete this._progressCallback;
  2144. this.onMeshLoadedObservable.clear();
  2145. this.onTextureLoadedObservable.clear();
  2146. this.onMaterialLoadedObservable.clear();
  2147. this.onCameraLoadedObservable.clear();
  2148. };
  2149. /** @hidden */
  2150. GLTFLoader.prototype._applyExtensions = function (actionAsync) {
  2151. for (var _i = 0, _a = GLTFLoader._ExtensionNames; _i < _a.length; _i++) {
  2152. var name_5 = _a[_i];
  2153. var extension = this._extensions[name_5];
  2154. if (extension.enabled) {
  2155. var promise = actionAsync(extension);
  2156. if (promise) {
  2157. return promise;
  2158. }
  2159. }
  2160. }
  2161. return null;
  2162. };
  2163. GLTFLoader._ExtensionNames = new Array();
  2164. GLTFLoader._ExtensionFactories = {};
  2165. return GLTFLoader;
  2166. }());
  2167. GLTF2.GLTFLoader = GLTFLoader;
  2168. BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = function () { return new GLTFLoader(); };
  2169. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2170. })(BABYLON || (BABYLON = {}));
  2171. //# sourceMappingURL=babylon.glTFLoader.js.map
  2172. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2173. var BABYLON;
  2174. (function (BABYLON) {
  2175. var GLTF2;
  2176. (function (GLTF2) {
  2177. /**
  2178. * Abstract class that can be implemented to extend existing glTF loader behavior.
  2179. */
  2180. var GLTFLoaderExtension = /** @class */ (function () {
  2181. function GLTFLoaderExtension(loader) {
  2182. this.enabled = true;
  2183. this._loader = loader;
  2184. }
  2185. GLTFLoaderExtension.prototype.dispose = function () {
  2186. delete this._loader;
  2187. };
  2188. // #region Overridable Methods
  2189. /** Override this method to modify the default behavior for loading scenes. */
  2190. GLTFLoaderExtension.prototype._loadSceneAsync = function (context, node) { return null; };
  2191. /** Override this method to modify the default behavior for loading nodes. */
  2192. GLTFLoaderExtension.prototype._loadNodeAsync = function (context, node) { return null; };
  2193. /** Override this method to modify the default behavior for loading mesh primitive vertex data. */
  2194. GLTFLoaderExtension.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) { return null; };
  2195. /** Override this method to modify the default behavior for loading materials. */
  2196. GLTFLoaderExtension.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) { return null; };
  2197. /** Override this method to modify the default behavior for loading textures. */
  2198. GLTFLoaderExtension.prototype._loadTextureAsync = function (context, textureInfo, assign) { return null; };
  2199. /** Override this method to modify the default behavior for loading uris. */
  2200. GLTFLoaderExtension.prototype._loadUriAsync = function (context, uri) { return null; };
  2201. // #endregion
  2202. /** Helper method called by a loader extension to load an glTF extension. */
  2203. GLTFLoaderExtension.prototype._loadExtensionAsync = function (context, property, actionAsync) {
  2204. if (!property.extensions) {
  2205. return null;
  2206. }
  2207. var extensions = property.extensions;
  2208. var extension = extensions[this.name];
  2209. if (!extension) {
  2210. return null;
  2211. }
  2212. // Clear out the extension before executing the action to avoid recursing into the same property.
  2213. delete extensions[this.name];
  2214. try {
  2215. return actionAsync(context + "/extensions/" + this.name, extension);
  2216. }
  2217. finally {
  2218. // Restore the extension after executing the action.
  2219. extensions[this.name] = extension;
  2220. }
  2221. };
  2222. /** Helper method called by the loader to allow extensions to override loading scenes. */
  2223. GLTFLoaderExtension._LoadSceneAsync = function (loader, context, scene) {
  2224. return loader._applyExtensions(function (extension) { return extension._loadSceneAsync(context, scene); });
  2225. };
  2226. /** Helper method called by the loader to allow extensions to override loading nodes. */
  2227. GLTFLoaderExtension._LoadNodeAsync = function (loader, context, node) {
  2228. return loader._applyExtensions(function (extension) { return extension._loadNodeAsync(context, node); });
  2229. };
  2230. /** Helper method called by the loader to allow extensions to override loading mesh primitive vertex data. */
  2231. GLTFLoaderExtension._LoadVertexDataAsync = function (loader, context, primitive, babylonMesh) {
  2232. return loader._applyExtensions(function (extension) { return extension._loadVertexDataAsync(context, primitive, babylonMesh); });
  2233. };
  2234. /** Helper method called by the loader to allow extensions to override loading materials. */
  2235. GLTFLoaderExtension._LoadMaterialAsync = function (loader, context, material, babylonMesh, babylonDrawMode, assign) {
  2236. return loader._applyExtensions(function (extension) { return extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
  2237. };
  2238. /** Helper method called by the loader to allow extensions to override loading textures. */
  2239. GLTFLoaderExtension._LoadTextureAsync = function (loader, context, textureInfo, assign) {
  2240. return loader._applyExtensions(function (extension) { return extension._loadTextureAsync(context, textureInfo, assign); });
  2241. };
  2242. /** Helper method called by the loader to allow extensions to override loading uris. */
  2243. GLTFLoaderExtension._LoadUriAsync = function (loader, context, uri) {
  2244. return loader._applyExtensions(function (extension) { return extension._loadUriAsync(context, uri); });
  2245. };
  2246. return GLTFLoaderExtension;
  2247. }());
  2248. GLTF2.GLTFLoaderExtension = GLTFLoaderExtension;
  2249. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2250. })(BABYLON || (BABYLON = {}));
  2251. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  2252. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2253. var __extends = (this && this.__extends) || (function () {
  2254. var extendStatics = Object.setPrototypeOf ||
  2255. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2256. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2257. return function (d, b) {
  2258. extendStatics(d, b);
  2259. function __() { this.constructor = d; }
  2260. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2261. };
  2262. })();
  2263. var BABYLON;
  2264. (function (BABYLON) {
  2265. var GLTF2;
  2266. (function (GLTF2) {
  2267. var Extensions;
  2268. (function (Extensions) {
  2269. var NAME = "MSFT_lod";
  2270. /**
  2271. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/MSFT_lod)
  2272. */
  2273. var MSFT_lod = /** @class */ (function (_super) {
  2274. __extends(MSFT_lod, _super);
  2275. function MSFT_lod() {
  2276. var _this = _super !== null && _super.apply(this, arguments) || this;
  2277. _this.name = NAME;
  2278. /**
  2279. * Maximum number of LODs to load, starting from the lowest LOD.
  2280. */
  2281. _this.maxLODsToLoad = Number.MAX_VALUE;
  2282. _this._loadingNodeLOD = null;
  2283. _this._loadNodeSignals = {};
  2284. _this._loadingMaterialLOD = null;
  2285. _this._loadMaterialSignals = {};
  2286. return _this;
  2287. }
  2288. MSFT_lod.prototype._loadNodeAsync = function (context, node) {
  2289. var _this = this;
  2290. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  2291. var firstPromise;
  2292. var nodeLODs = _this._getLODs(extensionContext, node, _this._loader._gltf.nodes, extension.ids);
  2293. var _loop_1 = function (indexLOD) {
  2294. var nodeLOD = nodeLODs[indexLOD];
  2295. if (indexLOD !== 0) {
  2296. _this._loadingNodeLOD = nodeLOD;
  2297. if (!_this._loadNodeSignals[nodeLOD._index]) {
  2298. _this._loadNodeSignals[nodeLOD._index] = new BABYLON.Deferred();
  2299. }
  2300. }
  2301. var promise = _this._loader._loadNodeAsync("#/nodes/" + nodeLOD._index, nodeLOD).then(function () {
  2302. if (indexLOD !== 0) {
  2303. var previousNodeLOD = nodeLODs[indexLOD - 1];
  2304. if (previousNodeLOD._babylonMesh) {
  2305. previousNodeLOD._babylonMesh.dispose(false, true);
  2306. delete previousNodeLOD._babylonMesh;
  2307. }
  2308. }
  2309. if (indexLOD !== nodeLODs.length - 1) {
  2310. var nodeIndex = nodeLODs[indexLOD + 1]._index;
  2311. if (_this._loadNodeSignals[nodeIndex]) {
  2312. _this._loadNodeSignals[nodeIndex].resolve();
  2313. delete _this._loadNodeSignals[nodeIndex];
  2314. }
  2315. }
  2316. });
  2317. if (indexLOD === 0) {
  2318. firstPromise = promise;
  2319. }
  2320. else {
  2321. _this._loader._completePromises.push(promise);
  2322. _this._loadingNodeLOD = null;
  2323. }
  2324. };
  2325. for (var indexLOD = 0; indexLOD < nodeLODs.length; indexLOD++) {
  2326. _loop_1(indexLOD);
  2327. }
  2328. return firstPromise;
  2329. });
  2330. };
  2331. MSFT_lod.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2332. var _this = this;
  2333. // Don't load material LODs if already loading a node LOD.
  2334. if (this._loadingNodeLOD) {
  2335. return null;
  2336. }
  2337. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  2338. var firstPromise;
  2339. var materialLODs = _this._getLODs(extensionContext, material, _this._loader._gltf.materials, extension.ids);
  2340. var _loop_2 = function (indexLOD) {
  2341. var materialLOD = materialLODs[indexLOD];
  2342. if (indexLOD !== 0) {
  2343. _this._loadingMaterialLOD = materialLOD;
  2344. if (!_this._loadMaterialSignals[materialLOD._index]) {
  2345. _this._loadMaterialSignals[materialLOD._index] = new BABYLON.Deferred();
  2346. }
  2347. }
  2348. var promise = _this._loader._loadMaterialAsync("#/materials/" + materialLOD._index, materialLOD, babylonMesh, babylonDrawMode, indexLOD === 0 ? assign : function () { }).then(function () {
  2349. if (indexLOD !== 0) {
  2350. var babylonDataLOD = materialLOD._babylonData;
  2351. assign(babylonDataLOD[babylonDrawMode].material);
  2352. var previousBabylonDataLOD = materialLODs[indexLOD - 1]._babylonData;
  2353. if (previousBabylonDataLOD[babylonDrawMode]) {
  2354. previousBabylonDataLOD[babylonDrawMode].material.dispose();
  2355. delete previousBabylonDataLOD[babylonDrawMode];
  2356. }
  2357. }
  2358. if (indexLOD !== materialLODs.length - 1) {
  2359. var materialIndex = materialLODs[indexLOD + 1]._index;
  2360. if (_this._loadMaterialSignals[materialIndex]) {
  2361. _this._loadMaterialSignals[materialIndex].resolve();
  2362. delete _this._loadMaterialSignals[materialIndex];
  2363. }
  2364. }
  2365. });
  2366. if (indexLOD === 0) {
  2367. firstPromise = promise;
  2368. }
  2369. else {
  2370. _this._loader._completePromises.push(promise);
  2371. _this._loadingMaterialLOD = null;
  2372. }
  2373. };
  2374. for (var indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  2375. _loop_2(indexLOD);
  2376. }
  2377. return firstPromise;
  2378. });
  2379. };
  2380. MSFT_lod.prototype._loadUriAsync = function (context, uri) {
  2381. var _this = this;
  2382. // Defer the loading of uris if loading a material or node LOD.
  2383. if (this._loadingMaterialLOD) {
  2384. var index = this._loadingMaterialLOD._index;
  2385. return this._loadMaterialSignals[index].promise.then(function () {
  2386. return _this._loader._loadUriAsync(context, uri);
  2387. });
  2388. }
  2389. else if (this._loadingNodeLOD) {
  2390. var index = this._loadingNodeLOD._index;
  2391. return this._loadNodeSignals[index].promise.then(function () {
  2392. return _this._loader._loadUriAsync(context, uri);
  2393. });
  2394. }
  2395. return null;
  2396. };
  2397. /**
  2398. * Gets an array of LOD properties from lowest to highest.
  2399. */
  2400. MSFT_lod.prototype._getLODs = function (context, property, array, ids) {
  2401. if (this.maxLODsToLoad <= 0) {
  2402. throw new Error("maxLODsToLoad must be greater than zero");
  2403. }
  2404. var properties = new Array();
  2405. for (var i = ids.length - 1; i >= 0; i--) {
  2406. properties.push(GLTF2.GLTFLoader._GetProperty(context + "/ids/" + ids[i], array, ids[i]));
  2407. if (properties.length === this.maxLODsToLoad) {
  2408. return properties;
  2409. }
  2410. }
  2411. properties.push(property);
  2412. return properties;
  2413. };
  2414. return MSFT_lod;
  2415. }(GLTF2.GLTFLoaderExtension));
  2416. Extensions.MSFT_lod = MSFT_lod;
  2417. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_lod(loader); });
  2418. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2419. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2420. })(BABYLON || (BABYLON = {}));
  2421. //# sourceMappingURL=MSFT_lod.js.map
  2422. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2423. var __extends = (this && this.__extends) || (function () {
  2424. var extendStatics = Object.setPrototypeOf ||
  2425. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2426. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2427. return function (d, b) {
  2428. extendStatics(d, b);
  2429. function __() { this.constructor = d; }
  2430. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2431. };
  2432. })();
  2433. var BABYLON;
  2434. (function (BABYLON) {
  2435. var GLTF2;
  2436. (function (GLTF2) {
  2437. var Extensions;
  2438. (function (Extensions) {
  2439. var NAME = "MSFT_minecraftMesh";
  2440. /** @hidden */
  2441. var MSFT_minecraftMesh = /** @class */ (function (_super) {
  2442. __extends(MSFT_minecraftMesh, _super);
  2443. function MSFT_minecraftMesh(loader) {
  2444. var _this = _super.call(this, loader) || this;
  2445. _this.name = NAME;
  2446. _this._onMaterialLoaded = function (material) {
  2447. if (material.needAlphaBlending()) {
  2448. material.forceDepthWrite = true;
  2449. material.separateCullingPass = true;
  2450. }
  2451. material.backFaceCulling = material.forceDepthWrite;
  2452. material.twoSidedLighting = true;
  2453. };
  2454. var meshes = loader._gltf.meshes;
  2455. if (meshes && meshes.length) {
  2456. for (var _i = 0, meshes_1 = meshes; _i < meshes_1.length; _i++) {
  2457. var mesh = meshes_1[_i];
  2458. if (mesh && mesh.extras && mesh.extras.MSFT_minecraftMesh) {
  2459. _this._loader.onMaterialLoadedObservable.add(_this._onMaterialLoaded);
  2460. break;
  2461. }
  2462. }
  2463. }
  2464. return _this;
  2465. }
  2466. return MSFT_minecraftMesh;
  2467. }(GLTF2.GLTFLoaderExtension));
  2468. Extensions.MSFT_minecraftMesh = MSFT_minecraftMesh;
  2469. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_minecraftMesh(loader); });
  2470. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2471. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2472. })(BABYLON || (BABYLON = {}));
  2473. //# sourceMappingURL=MSFT_minecraftMesh.js.map
  2474. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2475. var __extends = (this && this.__extends) || (function () {
  2476. var extendStatics = Object.setPrototypeOf ||
  2477. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2478. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2479. return function (d, b) {
  2480. extendStatics(d, b);
  2481. function __() { this.constructor = d; }
  2482. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2483. };
  2484. })();
  2485. var BABYLON;
  2486. (function (BABYLON) {
  2487. var GLTF2;
  2488. (function (GLTF2) {
  2489. var Extensions;
  2490. (function (Extensions) {
  2491. var NAME = "MSFT_sRGBFactors";
  2492. /** @hidden */
  2493. var MSFT_sRGBFactors = /** @class */ (function (_super) {
  2494. __extends(MSFT_sRGBFactors, _super);
  2495. function MSFT_sRGBFactors(loader) {
  2496. var _this = _super.call(this, loader) || this;
  2497. _this.name = NAME;
  2498. _this._onMaterialLoaded = function (material) {
  2499. if (!material.albedoTexture) {
  2500. material.albedoColor.toLinearSpaceToRef(material.albedoColor);
  2501. }
  2502. if (!material.reflectivityTexture) {
  2503. material.reflectivityColor.toLinearSpaceToRef(material.reflectivityColor);
  2504. }
  2505. };
  2506. var materials = loader._gltf.materials;
  2507. if (materials && materials.length) {
  2508. for (var _i = 0, materials_1 = materials; _i < materials_1.length; _i++) {
  2509. var material = materials_1[_i];
  2510. if (material && material.extras && material.extras.MSFT_sRGBFactors) {
  2511. _this._loader.onMaterialLoadedObservable.add(_this._onMaterialLoaded);
  2512. break;
  2513. }
  2514. }
  2515. }
  2516. return _this;
  2517. }
  2518. return MSFT_sRGBFactors;
  2519. }(GLTF2.GLTFLoaderExtension));
  2520. Extensions.MSFT_sRGBFactors = MSFT_sRGBFactors;
  2521. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_sRGBFactors(loader); });
  2522. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2523. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2524. })(BABYLON || (BABYLON = {}));
  2525. //# sourceMappingURL=MSFT_sRGBFactors.js.map
  2526. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2527. var __extends = (this && this.__extends) || (function () {
  2528. var extendStatics = Object.setPrototypeOf ||
  2529. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2530. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2531. return function (d, b) {
  2532. extendStatics(d, b);
  2533. function __() { this.constructor = d; }
  2534. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2535. };
  2536. })();
  2537. var BABYLON;
  2538. (function (BABYLON) {
  2539. var GLTF2;
  2540. (function (GLTF2) {
  2541. var Extensions;
  2542. (function (Extensions) {
  2543. var NAME = "KHR_draco_mesh_compression";
  2544. /**
  2545. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression)
  2546. */
  2547. var KHR_draco_mesh_compression = /** @class */ (function (_super) {
  2548. __extends(KHR_draco_mesh_compression, _super);
  2549. function KHR_draco_mesh_compression(loader) {
  2550. var _this = _super.call(this, loader) || this;
  2551. _this.name = NAME;
  2552. _this._dracoCompression = null;
  2553. // Disable extension if decoder is not available.
  2554. if (!BABYLON.DracoCompression.DecoderAvailable) {
  2555. _this.enabled = false;
  2556. }
  2557. return _this;
  2558. }
  2559. KHR_draco_mesh_compression.prototype.dispose = function () {
  2560. if (this._dracoCompression) {
  2561. this._dracoCompression.dispose();
  2562. }
  2563. _super.prototype.dispose.call(this);
  2564. };
  2565. KHR_draco_mesh_compression.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  2566. var _this = this;
  2567. return this._loadExtensionAsync(context, primitive, function (extensionContext, extension) {
  2568. if (primitive.mode != undefined) {
  2569. if (primitive.mode !== 5 /* TRIANGLE_STRIP */ &&
  2570. primitive.mode !== 4 /* TRIANGLES */) {
  2571. throw new Error(context + ": Unsupported mode " + primitive.mode);
  2572. }
  2573. // TODO: handle triangle strips
  2574. if (primitive.mode === 5 /* TRIANGLE_STRIP */) {
  2575. throw new Error(context + ": Mode " + primitive.mode + " is not currently supported");
  2576. }
  2577. }
  2578. var attributes = {};
  2579. var loadAttribute = function (name, kind) {
  2580. var uniqueId = extension.attributes[name];
  2581. if (uniqueId == undefined) {
  2582. return;
  2583. }
  2584. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  2585. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  2586. babylonMesh._delayInfo.push(kind);
  2587. }
  2588. attributes[kind] = uniqueId;
  2589. };
  2590. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  2591. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  2592. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  2593. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  2594. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  2595. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  2596. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  2597. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind);
  2598. var bufferView = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._loader._gltf.bufferViews, extension.bufferView);
  2599. if (!bufferView._dracoBabylonGeometry) {
  2600. bufferView._dracoBabylonGeometry = _this._loader._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  2601. if (!_this._dracoCompression) {
  2602. _this._dracoCompression = new BABYLON.DracoCompression();
  2603. }
  2604. return _this._dracoCompression.decodeMeshAsync(data, attributes).then(function (babylonVertexData) {
  2605. var babylonGeometry = new BABYLON.Geometry(babylonMesh.name, _this._loader._babylonScene);
  2606. babylonVertexData.applyToGeometry(babylonGeometry);
  2607. return babylonGeometry;
  2608. }).catch(function (error) {
  2609. throw new Error(context + ": " + error.message);
  2610. });
  2611. });
  2612. }
  2613. return bufferView._dracoBabylonGeometry;
  2614. });
  2615. };
  2616. return KHR_draco_mesh_compression;
  2617. }(GLTF2.GLTFLoaderExtension));
  2618. Extensions.KHR_draco_mesh_compression = KHR_draco_mesh_compression;
  2619. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_draco_mesh_compression(loader); });
  2620. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2621. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2622. })(BABYLON || (BABYLON = {}));
  2623. //# sourceMappingURL=KHR_draco_mesh_compression.js.map
  2624. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2625. var __extends = (this && this.__extends) || (function () {
  2626. var extendStatics = Object.setPrototypeOf ||
  2627. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2628. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2629. return function (d, b) {
  2630. extendStatics(d, b);
  2631. function __() { this.constructor = d; }
  2632. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2633. };
  2634. })();
  2635. var BABYLON;
  2636. (function (BABYLON) {
  2637. var GLTF2;
  2638. (function (GLTF2) {
  2639. var Extensions;
  2640. (function (Extensions) {
  2641. var NAME = "KHR_materials_pbrSpecularGlossiness";
  2642. /**
  2643. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness)
  2644. */
  2645. var KHR_materials_pbrSpecularGlossiness = /** @class */ (function (_super) {
  2646. __extends(KHR_materials_pbrSpecularGlossiness, _super);
  2647. function KHR_materials_pbrSpecularGlossiness() {
  2648. var _this = _super !== null && _super.apply(this, arguments) || this;
  2649. _this.name = NAME;
  2650. return _this;
  2651. }
  2652. KHR_materials_pbrSpecularGlossiness.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2653. var _this = this;
  2654. return this._loadExtensionAsync(context, material, function (extensionContext, extension) {
  2655. material._babylonData = material._babylonData || {};
  2656. var babylonData = material._babylonData[babylonDrawMode];
  2657. if (!babylonData) {
  2658. var promises = new Array();
  2659. var name_1 = material.name || "materialSG_" + material._index;
  2660. var babylonMaterial = _this._loader._createMaterial(name_1, babylonDrawMode);
  2661. promises.push(_this._loader._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  2662. promises.push(_this._loadSpecularGlossinessPropertiesAsync(extensionContext, material, extension, babylonMaterial));
  2663. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  2664. babylonData = {
  2665. material: babylonMaterial,
  2666. meshes: [],
  2667. loaded: Promise.all(promises).then(function () { })
  2668. };
  2669. material._babylonData[babylonDrawMode] = babylonData;
  2670. }
  2671. babylonData.meshes.push(babylonMesh);
  2672. assign(babylonData.material);
  2673. return babylonData.loaded;
  2674. });
  2675. };
  2676. KHR_materials_pbrSpecularGlossiness.prototype._loadSpecularGlossinessPropertiesAsync = function (context, material, properties, babylonMaterial) {
  2677. var promises = new Array();
  2678. if (properties.diffuseFactor) {
  2679. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.diffuseFactor);
  2680. babylonMaterial.alpha = properties.diffuseFactor[3];
  2681. }
  2682. else {
  2683. babylonMaterial.albedoColor = BABYLON.Color3.White();
  2684. }
  2685. babylonMaterial.reflectivityColor = properties.specularFactor ? BABYLON.Color3.FromArray(properties.specularFactor) : BABYLON.Color3.White();
  2686. babylonMaterial.microSurface = properties.glossinessFactor == undefined ? 1 : properties.glossinessFactor;
  2687. if (properties.diffuseTexture) {
  2688. promises.push(this._loader._loadTextureAsync(context + "/diffuseTexture", properties.diffuseTexture, function (texture) {
  2689. babylonMaterial.albedoTexture = texture;
  2690. }));
  2691. }
  2692. if (properties.specularGlossinessTexture) {
  2693. promises.push(this._loader._loadTextureAsync(context + "/specularGlossinessTexture", properties.specularGlossinessTexture, function (texture) {
  2694. babylonMaterial.reflectivityTexture = texture;
  2695. }));
  2696. babylonMaterial.reflectivityTexture.hasAlpha = true;
  2697. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  2698. }
  2699. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  2700. return Promise.all(promises).then(function () { });
  2701. };
  2702. return KHR_materials_pbrSpecularGlossiness;
  2703. }(GLTF2.GLTFLoaderExtension));
  2704. Extensions.KHR_materials_pbrSpecularGlossiness = KHR_materials_pbrSpecularGlossiness;
  2705. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_pbrSpecularGlossiness(loader); });
  2706. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2707. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2708. })(BABYLON || (BABYLON = {}));
  2709. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map
  2710. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2711. var __extends = (this && this.__extends) || (function () {
  2712. var extendStatics = Object.setPrototypeOf ||
  2713. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2714. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2715. return function (d, b) {
  2716. extendStatics(d, b);
  2717. function __() { this.constructor = d; }
  2718. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2719. };
  2720. })();
  2721. var BABYLON;
  2722. (function (BABYLON) {
  2723. var GLTF2;
  2724. (function (GLTF2) {
  2725. var Extensions;
  2726. (function (Extensions) {
  2727. var NAME = "KHR_materials_unlit";
  2728. /**
  2729. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit)
  2730. */
  2731. var KHR_materials_unlit = /** @class */ (function (_super) {
  2732. __extends(KHR_materials_unlit, _super);
  2733. function KHR_materials_unlit() {
  2734. var _this = _super !== null && _super.apply(this, arguments) || this;
  2735. _this.name = NAME;
  2736. return _this;
  2737. }
  2738. KHR_materials_unlit.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2739. var _this = this;
  2740. return this._loadExtensionAsync(context, material, function () {
  2741. material._babylonData = material._babylonData || {};
  2742. var babylonData = material._babylonData[babylonDrawMode];
  2743. if (!babylonData) {
  2744. var name_1 = material.name || "materialUnlit_" + material._index;
  2745. var babylonMaterial = _this._loader._createMaterial(name_1, babylonDrawMode);
  2746. babylonMaterial.unlit = true;
  2747. var promise = _this._loadUnlitPropertiesAsync(context, material, babylonMaterial);
  2748. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  2749. babylonData = {
  2750. material: babylonMaterial,
  2751. meshes: [],
  2752. loaded: promise
  2753. };
  2754. material._babylonData[babylonDrawMode] = babylonData;
  2755. }
  2756. babylonData.meshes.push(babylonMesh);
  2757. assign(babylonData.material);
  2758. return babylonData.loaded;
  2759. });
  2760. };
  2761. KHR_materials_unlit.prototype._loadUnlitPropertiesAsync = function (context, material, babylonMaterial) {
  2762. var promises = new Array();
  2763. // Ensure metallic workflow
  2764. babylonMaterial.metallic = 1;
  2765. babylonMaterial.roughness = 1;
  2766. var properties = material.pbrMetallicRoughness;
  2767. if (properties) {
  2768. if (properties.baseColorFactor) {
  2769. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  2770. babylonMaterial.alpha = properties.baseColorFactor[3];
  2771. }
  2772. else {
  2773. babylonMaterial.albedoColor = BABYLON.Color3.White();
  2774. }
  2775. if (properties.baseColorTexture) {
  2776. promises.push(this._loader._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  2777. babylonMaterial.albedoTexture = texture;
  2778. }));
  2779. }
  2780. }
  2781. if (material.doubleSided) {
  2782. babylonMaterial.backFaceCulling = false;
  2783. babylonMaterial.twoSidedLighting = true;
  2784. }
  2785. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  2786. return Promise.all(promises).then(function () { });
  2787. };
  2788. return KHR_materials_unlit;
  2789. }(GLTF2.GLTFLoaderExtension));
  2790. Extensions.KHR_materials_unlit = KHR_materials_unlit;
  2791. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_unlit(loader); });
  2792. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2793. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2794. })(BABYLON || (BABYLON = {}));
  2795. //# sourceMappingURL=KHR_materials_unlit.js.map
  2796. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2797. var __extends = (this && this.__extends) || (function () {
  2798. var extendStatics = Object.setPrototypeOf ||
  2799. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2800. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2801. return function (d, b) {
  2802. extendStatics(d, b);
  2803. function __() { this.constructor = d; }
  2804. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2805. };
  2806. })();
  2807. var BABYLON;
  2808. (function (BABYLON) {
  2809. var GLTF2;
  2810. (function (GLTF2) {
  2811. var Extensions;
  2812. (function (Extensions) {
  2813. var NAME = "KHR_lights";
  2814. var LightType;
  2815. (function (LightType) {
  2816. LightType["AMBIENT"] = "ambient";
  2817. LightType["DIRECTIONAL"] = "directional";
  2818. LightType["POINT"] = "point";
  2819. LightType["SPOT"] = "spot";
  2820. })(LightType || (LightType = {}));
  2821. /**
  2822. * [Specification](https://github.com/MiiBond/glTF/tree/khr_lights_v1/extensions/Khronos/KHR_lights) (Experimental)
  2823. */
  2824. var KHR_lights = /** @class */ (function (_super) {
  2825. __extends(KHR_lights, _super);
  2826. function KHR_lights() {
  2827. var _this = _super !== null && _super.apply(this, arguments) || this;
  2828. _this.name = NAME;
  2829. return _this;
  2830. }
  2831. KHR_lights.prototype._loadSceneAsync = function (context, scene) {
  2832. var _this = this;
  2833. return this._loadExtensionAsync(context, scene, function (extensionContext, extension) {
  2834. var promise = _this._loader._loadSceneAsync(extensionContext, scene);
  2835. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  2836. if (light.type !== LightType.AMBIENT) {
  2837. throw new Error(extensionContext + ": Only ambient lights are allowed on a scene");
  2838. }
  2839. _this._loader._babylonScene.ambientColor = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.Black();
  2840. return promise;
  2841. });
  2842. };
  2843. KHR_lights.prototype._loadNodeAsync = function (context, node) {
  2844. var _this = this;
  2845. return this._loadExtensionAsync(context, node, function (extensionContext, extension) {
  2846. var promise = _this._loader._loadNodeAsync(extensionContext, node);
  2847. var babylonLight;
  2848. var light = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._lights, extension.light);
  2849. var name = node._babylonMesh.name;
  2850. switch (light.type) {
  2851. case LightType.AMBIENT: {
  2852. throw new Error(extensionContext + ": Ambient lights are not allowed on a node");
  2853. }
  2854. case LightType.DIRECTIONAL: {
  2855. babylonLight = new BABYLON.DirectionalLight(name, BABYLON.Vector3.Forward(), _this._loader._babylonScene);
  2856. break;
  2857. }
  2858. case LightType.POINT: {
  2859. babylonLight = new BABYLON.PointLight(name, BABYLON.Vector3.Zero(), _this._loader._babylonScene);
  2860. break;
  2861. }
  2862. case LightType.SPOT: {
  2863. var spotLight = light;
  2864. // TODO: support inner and outer cone angles
  2865. //const innerConeAngle = spotLight.innerConeAngle || 0;
  2866. var outerConeAngle = spotLight.outerConeAngle || Math.PI / 4;
  2867. babylonLight = new BABYLON.SpotLight(name, BABYLON.Vector3.Zero(), BABYLON.Vector3.Forward(), outerConeAngle, 2, _this._loader._babylonScene);
  2868. break;
  2869. }
  2870. default: {
  2871. throw new Error(extensionContext + ": Invalid light type (" + light.type + ")");
  2872. }
  2873. }
  2874. babylonLight.diffuse = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.White();
  2875. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  2876. babylonLight.parent = node._babylonMesh;
  2877. return promise;
  2878. });
  2879. };
  2880. Object.defineProperty(KHR_lights.prototype, "_lights", {
  2881. get: function () {
  2882. var extensions = this._loader._gltf.extensions;
  2883. if (!extensions || !extensions[this.name]) {
  2884. throw new Error("#/extensions: '" + this.name + "' not found");
  2885. }
  2886. var extension = extensions[this.name];
  2887. return extension.lights;
  2888. },
  2889. enumerable: true,
  2890. configurable: true
  2891. });
  2892. return KHR_lights;
  2893. }(GLTF2.GLTFLoaderExtension));
  2894. Extensions.KHR_lights = KHR_lights;
  2895. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_lights(loader); });
  2896. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2897. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2898. })(BABYLON || (BABYLON = {}));
  2899. //# sourceMappingURL=KHR_lights.js.map
  2900. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2901. var __extends = (this && this.__extends) || (function () {
  2902. var extendStatics = Object.setPrototypeOf ||
  2903. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2904. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2905. return function (d, b) {
  2906. extendStatics(d, b);
  2907. function __() { this.constructor = d; }
  2908. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2909. };
  2910. })();
  2911. var BABYLON;
  2912. (function (BABYLON) {
  2913. var GLTF2;
  2914. (function (GLTF2) {
  2915. var Extensions;
  2916. (function (Extensions) {
  2917. var NAME = "KHR_texture_transform";
  2918. /**
  2919. * [Specification](https://github.com/AltspaceVR/glTF/blob/avr-sampler-offset-tile/extensions/2.0/Khronos/KHR_texture_transform/README.md) (Experimental)
  2920. */
  2921. var KHR_texture_transform = /** @class */ (function (_super) {
  2922. __extends(KHR_texture_transform, _super);
  2923. function KHR_texture_transform() {
  2924. var _this = _super !== null && _super.apply(this, arguments) || this;
  2925. _this.name = NAME;
  2926. return _this;
  2927. }
  2928. KHR_texture_transform.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  2929. var _this = this;
  2930. return this._loadExtensionAsync(context, textureInfo, function (extensionContext, extension) {
  2931. return _this._loader._loadTextureAsync(context, textureInfo, function (babylonTexture) {
  2932. if (extension.offset) {
  2933. babylonTexture.uOffset = extension.offset[0];
  2934. babylonTexture.vOffset = extension.offset[1];
  2935. }
  2936. // Always rotate around the origin.
  2937. babylonTexture.uRotationCenter = 0;
  2938. babylonTexture.vRotationCenter = 0;
  2939. if (extension.rotation) {
  2940. babylonTexture.wAng = -extension.rotation;
  2941. }
  2942. if (extension.scale) {
  2943. babylonTexture.uScale = extension.scale[0];
  2944. babylonTexture.vScale = extension.scale[1];
  2945. }
  2946. if (extension.texCoord != undefined) {
  2947. babylonTexture.coordinatesIndex = extension.texCoord;
  2948. }
  2949. assign(babylonTexture);
  2950. });
  2951. });
  2952. };
  2953. return KHR_texture_transform;
  2954. }(GLTF2.GLTFLoaderExtension));
  2955. Extensions.KHR_texture_transform = KHR_texture_transform;
  2956. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_texture_transform(loader); });
  2957. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2958. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2959. })(BABYLON || (BABYLON = {}));
  2960. //# sourceMappingURL=KHR_texture_transform.js.map