babylon.glTF2FileLoader.js 163 KB

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