babylon.glTF2FileLoader.js 132 KB

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