babylon.glTF2FileLoader.js 154 KB

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