babylon.glTF2FileLoader.js 128 KB

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