babylon.glTF2FileLoader.js 163 KB

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