babylon.glTF2FileLoader.js 160 KB

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