babylon.glTF2FileLoader.js 132 KB

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