babylon.glTF2FileLoader.js 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var GLTFLoaderCoordinateSystemMode;
  5. (function (GLTFLoaderCoordinateSystemMode) {
  6. // Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene (scene.useRightHandedSystem).
  7. // NOTE: When scene.useRightHandedSystem is false, an additional transform will be added to the root to transform the data from right-handed to left-handed.
  8. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
  9. // The glTF right-handed data is not transformed in any form and is loaded directly.
  10. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["PASS_THROUGH"] = 1] = "PASS_THROUGH";
  11. // Sets the useRightHandedSystem flag on the scene.
  12. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 2] = "FORCE_RIGHT_HANDED";
  13. })(GLTFLoaderCoordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode || (BABYLON.GLTFLoaderCoordinateSystemMode = {}));
  14. var GLTFFileLoader = /** @class */ (function () {
  15. function GLTFFileLoader() {
  16. // V2 options
  17. this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
  18. this.name = "gltf";
  19. this.extensions = {
  20. ".gltf": { isBinary: false },
  21. ".glb": { isBinary: true }
  22. };
  23. }
  24. GLTFFileLoader.prototype.dispose = function () {
  25. if (this._loader) {
  26. this._loader.dispose();
  27. this._loader = null;
  28. }
  29. };
  30. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  31. var loaderData = GLTFFileLoader._parse(data, onError);
  32. if (!loaderData) {
  33. return;
  34. }
  35. if (this.onParsed) {
  36. this.onParsed(loaderData);
  37. }
  38. this._loader = this._getLoader(loaderData, onError);
  39. if (!this._loader) {
  40. return;
  41. }
  42. this._loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onSuccess, onProgress, onError);
  43. };
  44. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onSuccess, onProgress, onError) {
  45. var loaderData = GLTFFileLoader._parse(data, onError);
  46. if (!loaderData) {
  47. return;
  48. }
  49. if (this.onParsed) {
  50. this.onParsed(loaderData);
  51. }
  52. this._loader = this._getLoader(loaderData, onError);
  53. if (!this._loader) {
  54. return;
  55. }
  56. return this._loader.loadAsync(scene, loaderData, rootUrl, onSuccess, onProgress, onError);
  57. };
  58. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  59. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  60. };
  61. GLTFFileLoader._parse = function (data, onError) {
  62. try {
  63. if (data instanceof ArrayBuffer) {
  64. return GLTFFileLoader._parseBinary(data, onError);
  65. }
  66. return {
  67. json: JSON.parse(data),
  68. bin: null
  69. };
  70. }
  71. catch (e) {
  72. onError(e.message);
  73. return null;
  74. }
  75. };
  76. GLTFFileLoader.prototype._getLoader = function (loaderData, onError) {
  77. var loaderVersion = { major: 2, minor: 0 };
  78. var asset = loaderData.json.asset || {};
  79. var version = GLTFFileLoader._parseVersion(asset.version);
  80. if (!version) {
  81. onError("Invalid version: " + asset.version);
  82. return null;
  83. }
  84. if (asset.minVersion !== undefined) {
  85. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  86. if (!minVersion) {
  87. onError("Invalid minimum version: " + asset.minVersion);
  88. return null;
  89. }
  90. if (GLTFFileLoader._compareVersion(minVersion, loaderVersion) > 0) {
  91. onError("Incompatible minimum version: " + asset.minVersion);
  92. return null;
  93. }
  94. }
  95. var createLoaders = {
  96. 1: GLTFFileLoader.CreateGLTFLoaderV1,
  97. 2: GLTFFileLoader.CreateGLTFLoaderV2
  98. };
  99. var createLoader = createLoaders[version.major];
  100. if (!createLoader) {
  101. onError("Unsupported version: " + asset.version);
  102. return null;
  103. }
  104. return createLoader(this);
  105. };
  106. GLTFFileLoader._parseBinary = function (data, onError) {
  107. var Binary = {
  108. Magic: 0x46546C67
  109. };
  110. var binaryReader = new BinaryReader(data);
  111. var magic = binaryReader.readUint32();
  112. if (magic !== Binary.Magic) {
  113. onError("Unexpected magic: " + magic);
  114. return null;
  115. }
  116. var version = binaryReader.readUint32();
  117. switch (version) {
  118. case 1: return GLTFFileLoader._parseV1(binaryReader, onError);
  119. case 2: return GLTFFileLoader._parseV2(binaryReader, onError);
  120. }
  121. onError("Unsupported version: " + version);
  122. return null;
  123. };
  124. GLTFFileLoader._parseV1 = function (binaryReader, onError) {
  125. var ContentFormat = {
  126. JSON: 0
  127. };
  128. var length = binaryReader.readUint32();
  129. if (length != binaryReader.getLength()) {
  130. onError("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  131. return null;
  132. }
  133. var contentLength = binaryReader.readUint32();
  134. var contentFormat = binaryReader.readUint32();
  135. var content;
  136. switch (contentFormat) {
  137. case ContentFormat.JSON:
  138. content = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength)));
  139. break;
  140. default:
  141. onError("Unexpected content format: " + contentFormat);
  142. return null;
  143. }
  144. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  145. var body = binaryReader.readUint8Array(bytesRemaining);
  146. return {
  147. json: content,
  148. bin: body
  149. };
  150. };
  151. GLTFFileLoader._parseV2 = function (binaryReader, onError) {
  152. var ChunkFormat = {
  153. JSON: 0x4E4F534A,
  154. BIN: 0x004E4942
  155. };
  156. var length = binaryReader.readUint32();
  157. if (length !== binaryReader.getLength()) {
  158. onError("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  159. return null;
  160. }
  161. // JSON chunk
  162. var chunkLength = binaryReader.readUint32();
  163. var chunkFormat = binaryReader.readUint32();
  164. if (chunkFormat !== ChunkFormat.JSON) {
  165. onError("First chunk format is not JSON");
  166. return null;
  167. }
  168. var json = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength)));
  169. // Look for BIN chunk
  170. var bin = null;
  171. while (binaryReader.getPosition() < binaryReader.getLength()) {
  172. var chunkLength_1 = binaryReader.readUint32();
  173. var chunkFormat_1 = binaryReader.readUint32();
  174. switch (chunkFormat_1) {
  175. case ChunkFormat.JSON:
  176. onError("Unexpected JSON chunk");
  177. return null;
  178. case ChunkFormat.BIN:
  179. bin = binaryReader.readUint8Array(chunkLength_1);
  180. break;
  181. default:
  182. // ignore unrecognized chunkFormat
  183. binaryReader.skipBytes(chunkLength_1);
  184. break;
  185. }
  186. }
  187. return {
  188. json: json,
  189. bin: bin
  190. };
  191. };
  192. GLTFFileLoader._parseVersion = function (version) {
  193. var match = (version + "").match(/^(\d+)\.(\d+)$/);
  194. if (!match) {
  195. return null;
  196. }
  197. return {
  198. major: parseInt(match[1]),
  199. minor: parseInt(match[2])
  200. };
  201. };
  202. GLTFFileLoader._compareVersion = function (a, b) {
  203. if (a.major > b.major)
  204. return 1;
  205. if (a.major < b.major)
  206. return -1;
  207. if (a.minor > b.minor)
  208. return 1;
  209. if (a.minor < b.minor)
  210. return -1;
  211. return 0;
  212. };
  213. GLTFFileLoader._decodeBufferToText = function (buffer) {
  214. var result = "";
  215. var length = buffer.byteLength;
  216. for (var i = 0; i < length; i++) {
  217. result += String.fromCharCode(buffer[i]);
  218. }
  219. return result;
  220. };
  221. // V1 options
  222. GLTFFileLoader.HomogeneousCoordinates = false;
  223. GLTFFileLoader.IncrementalLoading = true;
  224. return GLTFFileLoader;
  225. }());
  226. BABYLON.GLTFFileLoader = GLTFFileLoader;
  227. var BinaryReader = /** @class */ (function () {
  228. function BinaryReader(arrayBuffer) {
  229. this._arrayBuffer = arrayBuffer;
  230. this._dataView = new DataView(arrayBuffer);
  231. this._byteOffset = 0;
  232. }
  233. BinaryReader.prototype.getPosition = function () {
  234. return this._byteOffset;
  235. };
  236. BinaryReader.prototype.getLength = function () {
  237. return this._arrayBuffer.byteLength;
  238. };
  239. BinaryReader.prototype.readUint32 = function () {
  240. var value = this._dataView.getUint32(this._byteOffset, true);
  241. this._byteOffset += 4;
  242. return value;
  243. };
  244. BinaryReader.prototype.readUint8Array = function (length) {
  245. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  246. this._byteOffset += length;
  247. return value;
  248. };
  249. BinaryReader.prototype.skipBytes = function (length) {
  250. this._byteOffset += length;
  251. };
  252. return BinaryReader;
  253. }());
  254. if (BABYLON.SceneLoader) {
  255. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  256. }
  257. })(BABYLON || (BABYLON = {}));
  258. //# sourceMappingURL=babylon.glTFFileLoader.js.map
  259. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  260. var BABYLON;
  261. (function (BABYLON) {
  262. var GLTF2;
  263. (function (GLTF2) {
  264. /**
  265. * Enums
  266. */
  267. var EComponentType;
  268. (function (EComponentType) {
  269. EComponentType[EComponentType["BYTE"] = 5120] = "BYTE";
  270. EComponentType[EComponentType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  271. EComponentType[EComponentType["SHORT"] = 5122] = "SHORT";
  272. EComponentType[EComponentType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  273. EComponentType[EComponentType["UNSIGNED_INT"] = 5125] = "UNSIGNED_INT";
  274. EComponentType[EComponentType["FLOAT"] = 5126] = "FLOAT";
  275. })(EComponentType = GLTF2.EComponentType || (GLTF2.EComponentType = {}));
  276. var EMeshPrimitiveMode;
  277. (function (EMeshPrimitiveMode) {
  278. EMeshPrimitiveMode[EMeshPrimitiveMode["POINTS"] = 0] = "POINTS";
  279. EMeshPrimitiveMode[EMeshPrimitiveMode["LINES"] = 1] = "LINES";
  280. EMeshPrimitiveMode[EMeshPrimitiveMode["LINE_LOOP"] = 2] = "LINE_LOOP";
  281. EMeshPrimitiveMode[EMeshPrimitiveMode["LINE_STRIP"] = 3] = "LINE_STRIP";
  282. EMeshPrimitiveMode[EMeshPrimitiveMode["TRIANGLES"] = 4] = "TRIANGLES";
  283. EMeshPrimitiveMode[EMeshPrimitiveMode["TRIANGLE_STRIP"] = 5] = "TRIANGLE_STRIP";
  284. EMeshPrimitiveMode[EMeshPrimitiveMode["TRIANGLE_FAN"] = 6] = "TRIANGLE_FAN";
  285. })(EMeshPrimitiveMode = GLTF2.EMeshPrimitiveMode || (GLTF2.EMeshPrimitiveMode = {}));
  286. var ETextureMagFilter;
  287. (function (ETextureMagFilter) {
  288. ETextureMagFilter[ETextureMagFilter["NEAREST"] = 9728] = "NEAREST";
  289. ETextureMagFilter[ETextureMagFilter["LINEAR"] = 9729] = "LINEAR";
  290. })(ETextureMagFilter = GLTF2.ETextureMagFilter || (GLTF2.ETextureMagFilter = {}));
  291. var ETextureMinFilter;
  292. (function (ETextureMinFilter) {
  293. ETextureMinFilter[ETextureMinFilter["NEAREST"] = 9728] = "NEAREST";
  294. ETextureMinFilter[ETextureMinFilter["LINEAR"] = 9729] = "LINEAR";
  295. ETextureMinFilter[ETextureMinFilter["NEAREST_MIPMAP_NEAREST"] = 9984] = "NEAREST_MIPMAP_NEAREST";
  296. ETextureMinFilter[ETextureMinFilter["LINEAR_MIPMAP_NEAREST"] = 9985] = "LINEAR_MIPMAP_NEAREST";
  297. ETextureMinFilter[ETextureMinFilter["NEAREST_MIPMAP_LINEAR"] = 9986] = "NEAREST_MIPMAP_LINEAR";
  298. ETextureMinFilter[ETextureMinFilter["LINEAR_MIPMAP_LINEAR"] = 9987] = "LINEAR_MIPMAP_LINEAR";
  299. })(ETextureMinFilter = GLTF2.ETextureMinFilter || (GLTF2.ETextureMinFilter = {}));
  300. var ETextureWrapMode;
  301. (function (ETextureWrapMode) {
  302. ETextureWrapMode[ETextureWrapMode["CLAMP_TO_EDGE"] = 33071] = "CLAMP_TO_EDGE";
  303. ETextureWrapMode[ETextureWrapMode["MIRRORED_REPEAT"] = 33648] = "MIRRORED_REPEAT";
  304. ETextureWrapMode[ETextureWrapMode["REPEAT"] = 10497] = "REPEAT";
  305. })(ETextureWrapMode = GLTF2.ETextureWrapMode || (GLTF2.ETextureWrapMode = {}));
  306. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  307. })(BABYLON || (BABYLON = {}));
  308. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  309. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  310. var BABYLON;
  311. (function (BABYLON) {
  312. var GLTF2;
  313. (function (GLTF2) {
  314. var GLTFLoaderTracker = /** @class */ (function () {
  315. function GLTFLoaderTracker(onComplete) {
  316. this._pendingCount = 0;
  317. this._callback = onComplete;
  318. }
  319. GLTFLoaderTracker.prototype._addPendingData = function (data) {
  320. this._pendingCount++;
  321. };
  322. GLTFLoaderTracker.prototype._removePendingData = function (data) {
  323. if (--this._pendingCount === 0) {
  324. this._callback();
  325. }
  326. };
  327. return GLTFLoaderTracker;
  328. }());
  329. var GLTFLoader = /** @class */ (function () {
  330. function GLTFLoader(parent) {
  331. this._disposed = false;
  332. this._renderReady = false;
  333. this._requests = new Array();
  334. this._renderReadyObservable = new BABYLON.Observable();
  335. // Count of pending work that needs to complete before the asset is rendered.
  336. this._renderPendingCount = 0;
  337. // Count of pending work that needs to complete before the loader is disposed.
  338. this._loaderPendingCount = 0;
  339. this._loaderTrackers = new Array();
  340. this._parent = parent;
  341. }
  342. GLTFLoader.RegisterExtension = function (extension) {
  343. if (GLTFLoader.Extensions[extension.name]) {
  344. BABYLON.Tools.Error("Extension with the same name '" + extension.name + "' already exists");
  345. return;
  346. }
  347. GLTFLoader.Extensions[extension.name] = extension;
  348. // Keep the order of registration so that extensions registered first are called first.
  349. GLTF2.GLTFLoaderExtension._Extensions.push(extension);
  350. };
  351. GLTFLoader.prototype.dispose = function () {
  352. if (this._disposed) {
  353. return;
  354. }
  355. this._disposed = true;
  356. // Abort requests that are not complete
  357. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  358. var request = _a[_i];
  359. if (request.readyState !== (XMLHttpRequest.DONE || 4)) {
  360. request.abort();
  361. }
  362. }
  363. // Revoke object urls created during load
  364. if (this._gltf.textures) {
  365. for (var _b = 0, _c = this._gltf.textures; _b < _c.length; _b++) {
  366. var texture = _c[_b];
  367. if (texture.url) {
  368. URL.revokeObjectURL(texture.url);
  369. }
  370. }
  371. }
  372. this._gltf = undefined;
  373. this._babylonScene = undefined;
  374. this._parent = undefined;
  375. this._rootUrl = undefined;
  376. this._defaultMaterial = undefined;
  377. this._rootNode = undefined;
  378. this._successCallback = undefined;
  379. this._progressCallback = undefined;
  380. this._errorCallback = undefined;
  381. this._renderReady = false;
  382. this._requests = undefined;
  383. this._renderReadyObservable = undefined;
  384. this._renderPendingCount = 0;
  385. this._loaderPendingCount = 0;
  386. this._loaderTrackers = undefined;
  387. };
  388. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  389. var _this = this;
  390. this._loadAsync(meshesNames, scene, data, rootUrl, function () {
  391. onSuccess(_this._getMeshes(), null, _this._getSkeletons());
  392. }, onProgress, onError);
  393. };
  394. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onSuccess, onProgress, onError) {
  395. this._loadAsync(null, scene, data, rootUrl, onSuccess, onProgress, onError);
  396. };
  397. GLTFLoader.prototype._loadAsync = function (nodeNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  398. var _this = this;
  399. this._tryCatchOnError(function () {
  400. _this._loadData(data);
  401. _this._babylonScene = scene;
  402. _this._rootUrl = rootUrl;
  403. _this._successCallback = onSuccess;
  404. _this._progressCallback = onProgress;
  405. _this._errorCallback = onError;
  406. GLTF2.GLTFUtils.AssignIndices(_this._gltf.accessors);
  407. GLTF2.GLTFUtils.AssignIndices(_this._gltf.animations);
  408. GLTF2.GLTFUtils.AssignIndices(_this._gltf.buffers);
  409. GLTF2.GLTFUtils.AssignIndices(_this._gltf.bufferViews);
  410. GLTF2.GLTFUtils.AssignIndices(_this._gltf.images);
  411. GLTF2.GLTFUtils.AssignIndices(_this._gltf.materials);
  412. GLTF2.GLTFUtils.AssignIndices(_this._gltf.meshes);
  413. GLTF2.GLTFUtils.AssignIndices(_this._gltf.nodes);
  414. GLTF2.GLTFUtils.AssignIndices(_this._gltf.scenes);
  415. GLTF2.GLTFUtils.AssignIndices(_this._gltf.skins);
  416. GLTF2.GLTFUtils.AssignIndices(_this._gltf.textures);
  417. _this._addPendingData(_this);
  418. _this._loadDefaultScene(nodeNames);
  419. _this._loadAnimations();
  420. _this._removePendingData(_this);
  421. });
  422. };
  423. GLTFLoader.prototype._onError = function (message) {
  424. if (this._disposed) {
  425. return;
  426. }
  427. BABYLON.Tools.Error("glTF Loader: " + message);
  428. if (this._errorCallback) {
  429. this._errorCallback(message);
  430. }
  431. this.dispose();
  432. };
  433. GLTFLoader.prototype._onProgress = function (event) {
  434. if (this._disposed) {
  435. return;
  436. }
  437. if (this._progressCallback) {
  438. this._progressCallback(event);
  439. }
  440. };
  441. GLTFLoader.prototype._executeWhenRenderReady = function (func) {
  442. if (this._renderReady) {
  443. func();
  444. }
  445. else {
  446. this._renderReadyObservable.add(func);
  447. }
  448. };
  449. GLTFLoader.prototype._onRenderReady = function () {
  450. if (this._rootNode.babylonMesh) {
  451. this._rootNode.babylonMesh.setEnabled(true);
  452. }
  453. this._startAnimations();
  454. this._successCallback();
  455. this._renderReadyObservable.notifyObservers(this);
  456. };
  457. GLTFLoader.prototype._onComplete = function () {
  458. if (this._parent.onComplete) {
  459. this._parent.onComplete();
  460. }
  461. this.dispose();
  462. };
  463. GLTFLoader.prototype._loadData = function (data) {
  464. this._gltf = data.json;
  465. if (data.bin) {
  466. var buffers = this._gltf.buffers;
  467. if (buffers && buffers[0] && !buffers[0].uri) {
  468. var binaryBuffer = buffers[0];
  469. if (binaryBuffer.byteLength != data.bin.byteLength) {
  470. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  471. }
  472. binaryBuffer.loadedData = data.bin;
  473. }
  474. else {
  475. BABYLON.Tools.Warn("Unexpected BIN chunk");
  476. }
  477. }
  478. };
  479. GLTFLoader.prototype._getMeshes = function () {
  480. var meshes = [this._rootNode.babylonMesh || null];
  481. var nodes = this._gltf.nodes;
  482. if (nodes) {
  483. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  484. var node = nodes_1[_i];
  485. if (node.babylonMesh) {
  486. meshes.push(node.babylonMesh);
  487. }
  488. }
  489. }
  490. return meshes;
  491. };
  492. GLTFLoader.prototype._getSkeletons = function () {
  493. var skeletons = new Array();
  494. var skins = this._gltf.skins;
  495. if (skins) {
  496. for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
  497. var skin = skins_1[_i];
  498. if (skin.babylonSkeleton instanceof BABYLON.Skeleton) {
  499. skeletons.push(skin.babylonSkeleton);
  500. }
  501. }
  502. }
  503. return skeletons;
  504. };
  505. GLTFLoader.prototype._getAnimationTargets = function () {
  506. var targets = new Array();
  507. var animations = this._gltf.animations;
  508. if (animations) {
  509. for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
  510. var animation = animations_1[_i];
  511. targets.push.apply(targets, animation.targets);
  512. }
  513. }
  514. return targets;
  515. };
  516. GLTFLoader.prototype._startAnimations = function () {
  517. for (var _i = 0, _a = this._getAnimationTargets(); _i < _a.length; _i++) {
  518. var target = _a[_i];
  519. this._babylonScene.beginAnimation(target, 0, Number.MAX_VALUE, true);
  520. }
  521. };
  522. GLTFLoader.prototype._loadDefaultScene = function (nodeNames) {
  523. var scene = GLTF2.GLTFUtils.GetArrayItem(this._gltf.scenes, this._gltf.scene || 0);
  524. if (!scene) {
  525. throw new Error("Failed to find scene " + (this._gltf.scene || 0));
  526. }
  527. this._loadScene("#/scenes/" + scene.index, scene, nodeNames);
  528. };
  529. GLTFLoader.prototype._loadScene = function (context, scene, nodeNames) {
  530. this._rootNode = { babylonMesh: new BABYLON.Mesh("__root__", this._babylonScene) };
  531. switch (this._parent.coordinateSystemMode) {
  532. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO:
  533. if (!this._babylonScene.useRightHandedSystem && this._rootNode.babylonMesh) {
  534. this._rootNode.babylonMesh.rotation = new BABYLON.Vector3(0, Math.PI, 0);
  535. this._rootNode.babylonMesh.scaling = new BABYLON.Vector3(1, 1, -1);
  536. }
  537. break;
  538. case BABYLON.GLTFLoaderCoordinateSystemMode.PASS_THROUGH:
  539. // do nothing
  540. break;
  541. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED:
  542. this._babylonScene.useRightHandedSystem = true;
  543. break;
  544. default:
  545. BABYLON.Tools.Error("Invalid coordinate system mode (" + this._parent.coordinateSystemMode + ")");
  546. return;
  547. }
  548. var nodeIndices = scene.nodes;
  549. this._traverseNodes(context, nodeIndices, function (node, parentNode) {
  550. node.parent = parentNode;
  551. return true;
  552. }, this._rootNode);
  553. if (nodeNames) {
  554. if (!(nodeNames instanceof Array)) {
  555. nodeNames = [nodeNames];
  556. }
  557. var filteredNodeIndices_1 = new Array();
  558. this._traverseNodes(context, nodeIndices, function (node) {
  559. if (nodeNames.indexOf(node.name) !== -1) {
  560. filteredNodeIndices_1.push(node.index);
  561. return false;
  562. }
  563. return true;
  564. }, this._rootNode);
  565. nodeIndices = filteredNodeIndices_1;
  566. }
  567. for (var _i = 0, nodeIndices_1 = nodeIndices; _i < nodeIndices_1.length; _i++) {
  568. var index = nodeIndices_1[_i];
  569. var node = GLTF2.GLTFUtils.GetArrayItem(this._gltf.nodes, index);
  570. if (!node) {
  571. throw new Error(context + ": Failed to find node " + index);
  572. }
  573. this._loadNode("#/nodes/" + index, node);
  574. }
  575. // Disable the root mesh until the asset is ready to render.
  576. if (this._rootNode.babylonMesh) {
  577. this._rootNode.babylonMesh.setEnabled(false);
  578. }
  579. };
  580. GLTFLoader.prototype._loadNode = function (context, node) {
  581. if (GLTF2.GLTFLoaderExtension.LoadNode(this, context, node)) {
  582. return;
  583. }
  584. node.babylonMesh = new BABYLON.Mesh(node.name || "mesh" + node.index, this._babylonScene);
  585. this._loadTransform(node);
  586. if (node.mesh != null) {
  587. var mesh = GLTF2.GLTFUtils.GetArrayItem(this._gltf.meshes, node.mesh);
  588. if (!mesh) {
  589. throw new Error(context + ": Failed to find mesh " + node.mesh);
  590. }
  591. this._loadMesh("#/meshes/" + node.mesh, node, mesh);
  592. }
  593. node.babylonMesh.parent = (node.parent ? node.parent.babylonMesh : null);
  594. node.babylonAnimationTargets = node.babylonAnimationTargets || [];
  595. node.babylonAnimationTargets.push(node.babylonMesh);
  596. if (node.skin != null) {
  597. var skin = GLTF2.GLTFUtils.GetArrayItem(this._gltf.skins, node.skin);
  598. if (!skin) {
  599. throw new Error(context + ": Failed to find skin " + node.skin);
  600. }
  601. node.babylonMesh.skeleton = this._loadSkin("#/skins/" + node.skin, skin);
  602. }
  603. if (node.camera != null) {
  604. // TODO: handle cameras
  605. }
  606. if (node.children) {
  607. for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
  608. var index = _a[_i];
  609. var childNode = GLTF2.GLTFUtils.GetArrayItem(this._gltf.nodes, index);
  610. if (!childNode) {
  611. throw new Error(context + ": Failed to find child node " + index);
  612. }
  613. this._loadNode("#/nodes/" + index, childNode);
  614. }
  615. }
  616. };
  617. GLTFLoader.prototype._loadMesh = function (context, node, mesh) {
  618. var _this = this;
  619. if (!node.babylonMesh) {
  620. return;
  621. }
  622. node.babylonMesh.name = node.babylonMesh.name || mesh.name || "";
  623. var primitives = mesh.primitives;
  624. if (!primitives || primitives.length === 0) {
  625. throw new Error(context + ": Primitives are missing");
  626. }
  627. this._createMorphTargets(context, node, mesh);
  628. this._loadAllVertexDataAsync(context, mesh, function () {
  629. if (!node.babylonMesh) {
  630. return;
  631. }
  632. _this._loadMorphTargets(context, node, mesh);
  633. var vertexData = new BABYLON.VertexData();
  634. for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
  635. var primitive = primitives_1[_i];
  636. vertexData.merge(primitive.vertexData);
  637. }
  638. new BABYLON.Geometry(node.babylonMesh.name, _this._babylonScene, vertexData, false, node.babylonMesh);
  639. // TODO: optimize this so that sub meshes can be created without being overwritten after setting vertex data.
  640. // Sub meshes must be cleared and created after setting vertex data because of mesh._createGlobalSubMesh.
  641. node.babylonMesh.subMeshes = [];
  642. var verticesStart = 0;
  643. var indicesStart = 0;
  644. for (var index = 0; index < primitives.length; index++) {
  645. var vertexData_1 = primitives[index].vertexData;
  646. var verticesCount = vertexData_1.positions.length;
  647. var indicesCount = vertexData_1.indices.length;
  648. BABYLON.SubMesh.AddToMesh(index, verticesStart, verticesCount, indicesStart, indicesCount, node.babylonMesh);
  649. verticesStart += verticesCount;
  650. indicesStart += indicesCount;
  651. }
  652. ;
  653. });
  654. var multiMaterial = new BABYLON.MultiMaterial(node.babylonMesh.name, this._babylonScene);
  655. node.babylonMesh.material = multiMaterial;
  656. var subMaterials = multiMaterial.subMaterials;
  657. var _loop_1 = function (index) {
  658. var primitive = primitives[index];
  659. if (primitive.material == null) {
  660. subMaterials[index] = this_1._getDefaultMaterial();
  661. }
  662. else {
  663. var material_1 = GLTF2.GLTFUtils.GetArrayItem(this_1._gltf.materials, primitive.material);
  664. if (!material_1) {
  665. throw new Error(context + ": Failed to find material " + primitive.material);
  666. }
  667. this_1._loadMaterial("#/materials/" + material_1.index, material_1, function (babylonMaterial, isNew) {
  668. if (isNew && _this._parent.onMaterialLoaded) {
  669. _this._parent.onMaterialLoaded(babylonMaterial);
  670. }
  671. if (_this._parent.onBeforeMaterialReadyAsync) {
  672. _this._addLoaderPendingData(material_1);
  673. _this._parent.onBeforeMaterialReadyAsync(babylonMaterial, node.babylonMesh, subMaterials[index] != null, function () {
  674. _this._tryCatchOnError(function () {
  675. subMaterials[index] = babylonMaterial;
  676. _this._removeLoaderPendingData(material_1);
  677. });
  678. });
  679. }
  680. else {
  681. subMaterials[index] = babylonMaterial;
  682. }
  683. });
  684. }
  685. };
  686. var this_1 = this;
  687. for (var index = 0; index < primitives.length; index++) {
  688. _loop_1(index);
  689. }
  690. ;
  691. };
  692. GLTFLoader.prototype._loadAllVertexDataAsync = function (context, mesh, onSuccess) {
  693. var primitives = mesh.primitives;
  694. var numRemainingPrimitives = primitives.length;
  695. var _loop_2 = function (index) {
  696. var primitive = primitives[index];
  697. this_2._loadVertexDataAsync(context + "/primitive/" + index, mesh, primitive, function (vertexData) {
  698. primitive.vertexData = vertexData;
  699. if (--numRemainingPrimitives === 0) {
  700. onSuccess();
  701. }
  702. });
  703. };
  704. var this_2 = this;
  705. for (var index = 0; index < primitives.length; index++) {
  706. _loop_2(index);
  707. }
  708. };
  709. GLTFLoader.prototype._loadVertexDataAsync = function (context, mesh, primitive, onSuccess) {
  710. var _this = this;
  711. var attributes = primitive.attributes;
  712. if (!attributes) {
  713. throw new Error(context + ": Attributes are missing");
  714. }
  715. if (primitive.mode && primitive.mode !== GLTF2.EMeshPrimitiveMode.TRIANGLES) {
  716. // TODO: handle other primitive modes
  717. throw new Error(context + ": Mode " + primitive.mode + " is not currently supported");
  718. }
  719. var vertexData = new BABYLON.VertexData();
  720. var numRemainingAttributes = Object.keys(attributes).length;
  721. var _loop_3 = function (attribute) {
  722. var accessor = GLTF2.GLTFUtils.GetArrayItem(this_3._gltf.accessors, attributes[attribute]);
  723. if (!accessor) {
  724. throw new Error(context + ": Failed to find attribute '" + attribute + "' accessor " + attributes[attribute]);
  725. }
  726. this_3._loadAccessorAsync("#/accessors/" + accessor.index, accessor, function (data) {
  727. switch (attribute) {
  728. case "NORMAL":
  729. vertexData.normals = data;
  730. break;
  731. case "POSITION":
  732. vertexData.positions = data;
  733. break;
  734. case "TANGENT":
  735. vertexData.tangents = data;
  736. break;
  737. case "TEXCOORD_0":
  738. vertexData.uvs = data;
  739. break;
  740. case "TEXCOORD_1":
  741. vertexData.uvs2 = data;
  742. break;
  743. case "JOINTS_0":
  744. vertexData.matricesIndices = new Float32Array(Array.prototype.slice.apply(data));
  745. break;
  746. case "WEIGHTS_0":
  747. vertexData.matricesWeights = data;
  748. break;
  749. case "COLOR_0":
  750. vertexData.colors = data;
  751. break;
  752. default:
  753. BABYLON.Tools.Warn("Ignoring unrecognized attribute '" + attribute + "'");
  754. break;
  755. }
  756. if (--numRemainingAttributes === 0) {
  757. if (primitive.indices == null) {
  758. vertexData.indices = new Uint32Array(vertexData.positions.length / 3);
  759. for (var i = 0; i < vertexData.indices.length; i++) {
  760. vertexData.indices[i] = i;
  761. }
  762. onSuccess(vertexData);
  763. }
  764. else {
  765. var indicesAccessor = GLTF2.GLTFUtils.GetArrayItem(_this._gltf.accessors, primitive.indices);
  766. if (!indicesAccessor) {
  767. throw new Error(context + ": Failed to find indices accessor " + primitive.indices);
  768. }
  769. _this._loadAccessorAsync("#/accessors/" + indicesAccessor.index, indicesAccessor, function (data) {
  770. vertexData.indices = data;
  771. onSuccess(vertexData);
  772. });
  773. }
  774. }
  775. });
  776. };
  777. var this_3 = this;
  778. for (var attribute in attributes) {
  779. _loop_3(attribute);
  780. }
  781. };
  782. GLTFLoader.prototype._createMorphTargets = function (context, node, mesh) {
  783. var primitives = mesh.primitives;
  784. var targets = primitives[0].targets;
  785. if (!targets || !node.babylonMesh) {
  786. return;
  787. }
  788. for (var _i = 0, primitives_2 = primitives; _i < primitives_2.length; _i++) {
  789. var primitive = primitives_2[_i];
  790. if (!primitive.targets || primitive.targets.length != targets.length) {
  791. throw new Error(context + ": All primitives are required to list the same number of targets");
  792. }
  793. }
  794. var morphTargetManager = new BABYLON.MorphTargetManager();
  795. node.babylonMesh.morphTargetManager = morphTargetManager;
  796. for (var index = 0; index < targets.length; index++) {
  797. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  798. morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  799. }
  800. };
  801. GLTFLoader.prototype._loadMorphTargets = function (context, node, mesh) {
  802. if (!node.babylonMesh) {
  803. return;
  804. }
  805. var morphTargetManager = node.babylonMesh.morphTargetManager;
  806. if (!morphTargetManager) {
  807. return;
  808. }
  809. this._loadAllMorphTargetVertexDataAsync(context, node, mesh, function () {
  810. var numTargets = morphTargetManager.numTargets;
  811. for (var index = 0; index < numTargets; index++) {
  812. var vertexData = new BABYLON.VertexData();
  813. for (var _i = 0, _a = mesh.primitives; _i < _a.length; _i++) {
  814. var primitive = _a[_i];
  815. vertexData.merge(primitive.targetsVertexData[index], { tangentLength: 3 });
  816. }
  817. var target = morphTargetManager.getTarget(index);
  818. target.setNormals(vertexData.normals);
  819. target.setPositions(vertexData.positions);
  820. target.setTangents(vertexData.tangents);
  821. }
  822. });
  823. };
  824. GLTFLoader.prototype._loadAllMorphTargetVertexDataAsync = function (context, node, mesh, onSuccess) {
  825. if (!node.babylonMesh || !node.babylonMesh.morphTargetManager) {
  826. return;
  827. }
  828. var numRemainingTargets = mesh.primitives.length * node.babylonMesh.morphTargetManager.numTargets;
  829. var _loop_4 = function (primitive) {
  830. var targets = primitive.targets;
  831. if (!targets) {
  832. return "continue";
  833. }
  834. primitive.targetsVertexData = new Array(targets.length);
  835. var _loop_5 = function (index) {
  836. this_4._loadMorphTargetVertexDataAsync(context + "/targets/" + index, primitive.vertexData, targets[index], function (vertexData) {
  837. primitive.targetsVertexData[index] = vertexData;
  838. if (--numRemainingTargets === 0) {
  839. onSuccess();
  840. }
  841. });
  842. };
  843. for (var index = 0; index < targets.length; index++) {
  844. _loop_5(index);
  845. }
  846. };
  847. var this_4 = this;
  848. for (var _i = 0, _a = mesh.primitives; _i < _a.length; _i++) {
  849. var primitive = _a[_i];
  850. _loop_4(primitive);
  851. }
  852. };
  853. GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, vertexData, attributes, onSuccess) {
  854. var targetVertexData = new BABYLON.VertexData();
  855. var numRemainingAttributes = Object.keys(attributes).length;
  856. var _loop_6 = function (attribute) {
  857. var accessor = GLTF2.GLTFUtils.GetArrayItem(this_5._gltf.accessors, attributes[attribute]);
  858. if (!accessor) {
  859. throw new Error(context + ": Failed to find attribute '" + attribute + "' accessor " + attributes[attribute]);
  860. }
  861. this_5._loadAccessorAsync("#/accessors/" + accessor.index, accessor, function (data) {
  862. // glTF stores morph target information as deltas while babylon.js expects the final data.
  863. // As a result we have to add the original data to the delta to calculate the final data.
  864. var values = data;
  865. switch (attribute) {
  866. case "NORMAL":
  867. for (var i = 0; i < values.length; i++) {
  868. values[i] += vertexData.normals[i];
  869. }
  870. targetVertexData.normals = values;
  871. break;
  872. case "POSITION":
  873. for (var i = 0; i < values.length; i++) {
  874. values[i] += vertexData.positions[i];
  875. }
  876. targetVertexData.positions = values;
  877. break;
  878. case "TANGENT":
  879. // Tangent data for morph targets is stored as xyz delta.
  880. // The vertexData.tangent is stored as xyzw.
  881. // So we need to skip every fourth vertexData.tangent.
  882. for (var i = 0, j = 0; i < values.length; i++, j++) {
  883. values[i] += vertexData.tangents[j];
  884. if ((i + 1) % 3 == 0) {
  885. j++;
  886. }
  887. }
  888. targetVertexData.tangents = values;
  889. break;
  890. default:
  891. BABYLON.Tools.Warn("Ignoring unrecognized attribute '" + attribute + "'");
  892. break;
  893. }
  894. if (--numRemainingAttributes === 0) {
  895. onSuccess(targetVertexData);
  896. }
  897. });
  898. };
  899. var this_5 = this;
  900. for (var attribute in attributes) {
  901. _loop_6(attribute);
  902. }
  903. };
  904. GLTFLoader.prototype._loadTransform = function (node) {
  905. if (!node.babylonMesh) {
  906. return;
  907. }
  908. var position = BABYLON.Vector3.Zero();
  909. var rotation = BABYLON.Quaternion.Identity();
  910. var scaling = BABYLON.Vector3.One();
  911. if (node.matrix) {
  912. var matrix = BABYLON.Matrix.FromArray(node.matrix);
  913. matrix.decompose(scaling, rotation, position);
  914. }
  915. else {
  916. if (node.translation)
  917. position = BABYLON.Vector3.FromArray(node.translation);
  918. if (node.rotation)
  919. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  920. if (node.scale)
  921. scaling = BABYLON.Vector3.FromArray(node.scale);
  922. }
  923. node.babylonMesh.position = position;
  924. node.babylonMesh.rotationQuaternion = rotation;
  925. node.babylonMesh.scaling = scaling;
  926. };
  927. GLTFLoader.prototype._loadSkin = function (context, skin) {
  928. var _this = this;
  929. var skeletonId = "skeleton" + skin.index;
  930. skin.babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
  931. if (skin.inverseBindMatrices == null) {
  932. this._loadBones(context, skin, null);
  933. }
  934. else {
  935. var accessor = GLTF2.GLTFUtils.GetArrayItem(this._gltf.accessors, skin.inverseBindMatrices);
  936. if (!accessor) {
  937. throw new Error(context + ": Failed to find inverse bind matrices attribute " + skin.inverseBindMatrices);
  938. }
  939. this._loadAccessorAsync("#/accessors/" + accessor.index, accessor, function (data) {
  940. _this._loadBones(context, skin, data);
  941. });
  942. }
  943. return skin.babylonSkeleton;
  944. };
  945. GLTFLoader.prototype._createBone = function (node, skin, parent, localMatrix, baseMatrix, index) {
  946. var babylonBone = new BABYLON.Bone(node.name || "bone" + node.index, skin.babylonSkeleton, parent, localMatrix, null, baseMatrix, index);
  947. node.babylonBones = node.babylonBones || {};
  948. node.babylonBones[skin.index] = babylonBone;
  949. node.babylonAnimationTargets = node.babylonAnimationTargets || [];
  950. node.babylonAnimationTargets.push(babylonBone);
  951. return babylonBone;
  952. };
  953. GLTFLoader.prototype._loadBones = function (context, skin, inverseBindMatrixData) {
  954. var babylonBones = {};
  955. for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
  956. var index = _a[_i];
  957. var node = GLTF2.GLTFUtils.GetArrayItem(this._gltf.nodes, index);
  958. if (!node) {
  959. throw new Error(context + ": Failed to find joint " + index);
  960. }
  961. this._loadBone(node, skin, inverseBindMatrixData, babylonBones);
  962. }
  963. };
  964. GLTFLoader.prototype._loadBone = function (node, skin, inverseBindMatrixData, babylonBones) {
  965. var babylonBone = babylonBones[node.index];
  966. if (babylonBone) {
  967. return babylonBone;
  968. }
  969. var boneIndex = skin.joints.indexOf(node.index);
  970. var baseMatrix = BABYLON.Matrix.Identity();
  971. if (inverseBindMatrixData && boneIndex !== -1) {
  972. baseMatrix = BABYLON.Matrix.FromArray(inverseBindMatrixData, boneIndex * 16);
  973. baseMatrix.invertToRef(baseMatrix);
  974. }
  975. var babylonParentBone = null;
  976. if (node.index !== skin.skeleton && node.parent !== this._rootNode) {
  977. babylonParentBone = this._loadBone(node.parent, skin, inverseBindMatrixData, babylonBones);
  978. baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
  979. }
  980. babylonBone = this._createBone(node, skin, babylonParentBone, this._getNodeMatrix(node), baseMatrix, boneIndex);
  981. babylonBones[node.index] = babylonBone;
  982. return babylonBone;
  983. };
  984. GLTFLoader.prototype._getNodeMatrix = function (node) {
  985. return node.matrix ?
  986. BABYLON.Matrix.FromArray(node.matrix) :
  987. 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());
  988. };
  989. GLTFLoader.prototype._traverseNodes = function (context, indices, action, parentNode) {
  990. if (parentNode === void 0) { parentNode = null; }
  991. for (var _i = 0, indices_1 = indices; _i < indices_1.length; _i++) {
  992. var index = indices_1[_i];
  993. var node = GLTF2.GLTFUtils.GetArrayItem(this._gltf.nodes, index);
  994. if (!node) {
  995. throw new Error(context + ": Failed to find node " + index);
  996. }
  997. this._traverseNode(context, node, action, parentNode);
  998. }
  999. };
  1000. GLTFLoader.prototype._traverseNode = function (context, node, action, parentNode) {
  1001. if (parentNode === void 0) { parentNode = null; }
  1002. if (GLTF2.GLTFLoaderExtension.TraverseNode(this, context, node, action, parentNode)) {
  1003. return;
  1004. }
  1005. if (!action(node, parentNode)) {
  1006. return;
  1007. }
  1008. if (node.children) {
  1009. this._traverseNodes(context, node.children, action, node);
  1010. }
  1011. };
  1012. GLTFLoader.prototype._loadAnimations = function () {
  1013. var animations = this._gltf.animations;
  1014. if (!animations) {
  1015. return;
  1016. }
  1017. for (var animationIndex = 0; animationIndex < animations.length; animationIndex++) {
  1018. var animation = animations[animationIndex];
  1019. var context = "#/animations/" + animationIndex;
  1020. for (var channelIndex = 0; channelIndex < animation.channels.length; channelIndex++) {
  1021. var channel = GLTF2.GLTFUtils.GetArrayItem(animation.channels, channelIndex);
  1022. if (!channel) {
  1023. throw new Error(context + ": Failed to find channel " + channelIndex);
  1024. }
  1025. var sampler = GLTF2.GLTFUtils.GetArrayItem(animation.samplers, channel.sampler);
  1026. if (!sampler) {
  1027. throw new Error(context + ": Failed to find sampler " + channel.sampler);
  1028. }
  1029. this._loadAnimationChannel(animation, context + "/channels/" + channelIndex, channel, context + "/samplers/" + channel.sampler, sampler);
  1030. }
  1031. }
  1032. };
  1033. GLTFLoader.prototype._loadAnimationChannel = function (animation, channelContext, channel, samplerContext, sampler) {
  1034. var targetNode = GLTF2.GLTFUtils.GetArrayItem(this._gltf.nodes, channel.target.node);
  1035. if (!targetNode) {
  1036. throw new Error(channelContext + ": Failed to find target node " + channel.target.node);
  1037. }
  1038. var targetPath;
  1039. var animationType;
  1040. switch (channel.target.path) {
  1041. case "translation":
  1042. targetPath = "position";
  1043. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1044. break;
  1045. case "rotation":
  1046. targetPath = "rotationQuaternion";
  1047. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  1048. break;
  1049. case "scale":
  1050. targetPath = "scaling";
  1051. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1052. break;
  1053. case "weights":
  1054. targetPath = "influence";
  1055. animationType = BABYLON.Animation.ANIMATIONTYPE_FLOAT;
  1056. break;
  1057. default:
  1058. throw new Error(channelContext + ": Invalid target path '" + channel.target.path + "'");
  1059. }
  1060. var inputData;
  1061. var outputData;
  1062. var checkSuccess = function () {
  1063. if (!inputData || !outputData) {
  1064. return;
  1065. }
  1066. var outputBufferOffset = 0;
  1067. var getNextOutputValue;
  1068. switch (targetPath) {
  1069. case "position":
  1070. getNextOutputValue = function () {
  1071. var value = BABYLON.Vector3.FromArray(outputData, outputBufferOffset);
  1072. outputBufferOffset += 3;
  1073. return value;
  1074. };
  1075. break;
  1076. case "rotationQuaternion":
  1077. getNextOutputValue = function () {
  1078. var value = BABYLON.Quaternion.FromArray(outputData, outputBufferOffset);
  1079. outputBufferOffset += 4;
  1080. return value;
  1081. };
  1082. break;
  1083. case "scaling":
  1084. getNextOutputValue = function () {
  1085. var value = BABYLON.Vector3.FromArray(outputData, outputBufferOffset);
  1086. outputBufferOffset += 3;
  1087. return value;
  1088. };
  1089. break;
  1090. case "influence":
  1091. getNextOutputValue = function () {
  1092. if (!targetNode.babylonMesh || !targetNode.babylonMesh.morphTargetManager) {
  1093. return;
  1094. }
  1095. var numTargets = targetNode.babylonMesh.morphTargetManager.numTargets;
  1096. var value = new Array(numTargets);
  1097. for (var i = 0; i < numTargets; i++) {
  1098. value[i] = outputData[outputBufferOffset++];
  1099. }
  1100. return value;
  1101. };
  1102. break;
  1103. }
  1104. var getNextKey;
  1105. switch (sampler.interpolation) {
  1106. case "LINEAR":
  1107. getNextKey = function (frameIndex) { return ({
  1108. frame: inputData[frameIndex],
  1109. value: getNextOutputValue()
  1110. }); };
  1111. break;
  1112. case "CUBICSPLINE":
  1113. getNextKey = function (frameIndex) { return ({
  1114. frame: inputData[frameIndex],
  1115. inTangent: getNextOutputValue(),
  1116. value: getNextOutputValue(),
  1117. outTangent: getNextOutputValue()
  1118. }); };
  1119. break;
  1120. default:
  1121. throw new Error(samplerContext + ": Invalid interpolation '" + sampler.interpolation + "'");
  1122. }
  1123. ;
  1124. var keys = new Array(inputData.length);
  1125. for (var frameIndex = 0; frameIndex < inputData.length; frameIndex++) {
  1126. keys[frameIndex] = getNextKey(frameIndex);
  1127. }
  1128. animation.targets = animation.targets || [];
  1129. if (targetPath === "influence" && targetNode.babylonMesh && targetNode.babylonMesh.morphTargetManager) {
  1130. var morphTargetManager = targetNode.babylonMesh.morphTargetManager;
  1131. var _loop_7 = function (targetIndex) {
  1132. var morphTarget = morphTargetManager.getTarget(targetIndex);
  1133. var animationName = (animation.name || "anim" + animation.index) + "_" + targetIndex;
  1134. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1135. babylonAnimation.setKeys(keys.map(function (key) { return ({
  1136. frame: key.frame,
  1137. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  1138. value: key.value[targetIndex],
  1139. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  1140. }); }));
  1141. morphTarget.animations.push(babylonAnimation);
  1142. animation.targets.push(morphTarget);
  1143. };
  1144. for (var targetIndex = 0; targetIndex < morphTargetManager.numTargets; targetIndex++) {
  1145. _loop_7(targetIndex);
  1146. }
  1147. }
  1148. else if (targetNode.babylonAnimationTargets) {
  1149. var animationName = animation.name || "anim" + animation.index;
  1150. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1151. babylonAnimation.setKeys(keys);
  1152. for (var _i = 0, _a = targetNode.babylonAnimationTargets; _i < _a.length; _i++) {
  1153. var target = _a[_i];
  1154. target.animations.push(babylonAnimation.clone());
  1155. animation.targets.push(target);
  1156. }
  1157. }
  1158. };
  1159. var inputAccessor = GLTF2.GLTFUtils.GetArrayItem(this._gltf.accessors, sampler.input);
  1160. if (!inputAccessor) {
  1161. throw new Error(samplerContext + ": Failed to find input accessor " + sampler.input);
  1162. }
  1163. this._loadAccessorAsync("#/accessors/" + inputAccessor.index, inputAccessor, function (data) {
  1164. inputData = data;
  1165. checkSuccess();
  1166. });
  1167. var outputAccessor = GLTF2.GLTFUtils.GetArrayItem(this._gltf.accessors, sampler.output);
  1168. if (!outputAccessor) {
  1169. throw new Error(samplerContext + ": Failed to find output accessor " + sampler.output);
  1170. }
  1171. this._loadAccessorAsync("#/accessors/" + outputAccessor.index, outputAccessor, function (data) {
  1172. outputData = data;
  1173. checkSuccess();
  1174. });
  1175. };
  1176. GLTFLoader.prototype._loadBufferAsync = function (context, buffer, onSuccess) {
  1177. var _this = this;
  1178. this._addPendingData(buffer);
  1179. if (buffer.loadedData) {
  1180. onSuccess(buffer.loadedData);
  1181. this._removePendingData(buffer);
  1182. }
  1183. else if (buffer.loadedObservable) {
  1184. buffer.loadedObservable.add(function (buffer) {
  1185. onSuccess(buffer.loadedData);
  1186. _this._removePendingData(buffer);
  1187. });
  1188. }
  1189. else {
  1190. if (!buffer.uri) {
  1191. throw new Error(context + ": Uri is missing");
  1192. }
  1193. if (GLTF2.GLTFUtils.IsBase64(buffer.uri)) {
  1194. var data = GLTF2.GLTFUtils.DecodeBase64(buffer.uri);
  1195. buffer.loadedData = new Uint8Array(data);
  1196. onSuccess(buffer.loadedData);
  1197. this._removePendingData(buffer);
  1198. }
  1199. else {
  1200. buffer.loadedObservable = new BABYLON.Observable();
  1201. buffer.loadedObservable.add(function (buffer) {
  1202. onSuccess(buffer.loadedData);
  1203. _this._removePendingData(buffer);
  1204. });
  1205. this._loadUri(context, buffer.uri, function (data) {
  1206. buffer.loadedData = data;
  1207. if (buffer.loadedObservable) {
  1208. buffer.loadedObservable.notifyObservers(buffer);
  1209. buffer.loadedObservable = undefined;
  1210. }
  1211. });
  1212. }
  1213. }
  1214. };
  1215. GLTFLoader.prototype._loadBufferViewAsync = function (context, bufferView, onSuccess) {
  1216. var _this = this;
  1217. var buffer = GLTF2.GLTFUtils.GetArrayItem(this._gltf.buffers, bufferView.buffer);
  1218. if (!buffer) {
  1219. throw new Error(context + ": Failed to find buffer " + bufferView.buffer);
  1220. }
  1221. this._loadBufferAsync("#/buffers/" + buffer.index, buffer, function (bufferData) {
  1222. if (_this._disposed || !bufferData) {
  1223. return;
  1224. }
  1225. var data;
  1226. try {
  1227. data = new Uint8Array(bufferData.buffer, bufferData.byteOffset + (bufferView.byteOffset || 0), bufferView.byteLength);
  1228. }
  1229. catch (e) {
  1230. throw new Error(context + ": " + e.message);
  1231. }
  1232. onSuccess(data);
  1233. });
  1234. };
  1235. GLTFLoader.prototype._loadAccessorAsync = function (context, accessor, onSuccess) {
  1236. var _this = this;
  1237. if (accessor.sparse) {
  1238. throw new Error(context + ": Sparse accessors are not currently supported");
  1239. }
  1240. if (accessor.normalized) {
  1241. throw new Error(context + ": Normalized accessors are not currently supported");
  1242. }
  1243. var bufferView = GLTF2.GLTFUtils.GetArrayItem(this._gltf.bufferViews, accessor.bufferView);
  1244. if (!bufferView) {
  1245. throw new Error(context + ": Failed to find buffer view " + accessor.bufferView);
  1246. }
  1247. this._loadBufferViewAsync("#/bufferViews/" + bufferView.index, bufferView, function (bufferViewData) {
  1248. var numComponents = _this._getNumComponentsOfType(accessor.type);
  1249. if (numComponents === 0) {
  1250. throw new Error(context + ": Invalid type (" + accessor.type + ")");
  1251. }
  1252. var data;
  1253. var byteOffset = accessor.byteOffset;
  1254. var byteStride = bufferView.byteStride;
  1255. try {
  1256. switch (accessor.componentType) {
  1257. case GLTF2.EComponentType.BYTE:
  1258. data = _this._buildArrayBuffer(Float32Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1259. break;
  1260. case GLTF2.EComponentType.UNSIGNED_BYTE:
  1261. data = _this._buildArrayBuffer(Uint8Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1262. break;
  1263. case GLTF2.EComponentType.SHORT:
  1264. data = _this._buildArrayBuffer(Int16Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1265. break;
  1266. case GLTF2.EComponentType.UNSIGNED_SHORT:
  1267. data = _this._buildArrayBuffer(Uint16Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1268. break;
  1269. case GLTF2.EComponentType.UNSIGNED_INT:
  1270. data = _this._buildArrayBuffer(Uint32Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1271. break;
  1272. case GLTF2.EComponentType.FLOAT:
  1273. data = _this._buildArrayBuffer(Float32Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1274. break;
  1275. default:
  1276. throw new Error(context + ": Invalid component type (" + accessor.componentType + ")");
  1277. }
  1278. }
  1279. catch (e) {
  1280. throw new Error(context + ": " + e);
  1281. }
  1282. onSuccess(data);
  1283. });
  1284. };
  1285. GLTFLoader.prototype._getNumComponentsOfType = function (type) {
  1286. switch (type) {
  1287. case "SCALAR": return 1;
  1288. case "VEC2": return 2;
  1289. case "VEC3": return 3;
  1290. case "VEC4": return 4;
  1291. case "MAT2": return 4;
  1292. case "MAT3": return 9;
  1293. case "MAT4": return 16;
  1294. }
  1295. return 0;
  1296. };
  1297. GLTFLoader.prototype._buildArrayBuffer = function (typedArray, data, byteOffset, count, numComponents, byteStride) {
  1298. byteOffset = data.byteOffset + (byteOffset || 0);
  1299. var targetLength = count * numComponents;
  1300. if (byteStride == null || byteStride === numComponents * typedArray.BYTES_PER_ELEMENT) {
  1301. return new typedArray(data.buffer, byteOffset, targetLength);
  1302. }
  1303. var elementStride = byteStride / typedArray.BYTES_PER_ELEMENT;
  1304. var sourceBuffer = new typedArray(data.buffer, byteOffset, elementStride * count);
  1305. var targetBuffer = new typedArray(targetLength);
  1306. var sourceIndex = 0;
  1307. var targetIndex = 0;
  1308. while (targetIndex < targetLength) {
  1309. for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
  1310. targetBuffer[targetIndex] = sourceBuffer[sourceIndex + componentIndex];
  1311. targetIndex++;
  1312. }
  1313. sourceIndex += elementStride;
  1314. }
  1315. return targetBuffer;
  1316. };
  1317. GLTFLoader.prototype._addPendingData = function (data) {
  1318. if (!this._renderReady) {
  1319. this._renderPendingCount++;
  1320. }
  1321. this._addLoaderPendingData(data);
  1322. };
  1323. GLTFLoader.prototype._removePendingData = function (data) {
  1324. if (!this._renderReady) {
  1325. if (--this._renderPendingCount === 0) {
  1326. this._renderReady = true;
  1327. this._onRenderReady();
  1328. }
  1329. }
  1330. this._removeLoaderPendingData(data);
  1331. };
  1332. GLTFLoader.prototype._addLoaderPendingData = function (data) {
  1333. this._loaderPendingCount++;
  1334. for (var _i = 0, _a = this._loaderTrackers; _i < _a.length; _i++) {
  1335. var tracker = _a[_i];
  1336. tracker._addPendingData(data);
  1337. }
  1338. };
  1339. GLTFLoader.prototype._removeLoaderPendingData = function (data) {
  1340. for (var _i = 0, _a = this._loaderTrackers; _i < _a.length; _i++) {
  1341. var tracker = _a[_i];
  1342. tracker._removePendingData(data);
  1343. }
  1344. if (--this._loaderPendingCount === 0) {
  1345. this._onComplete();
  1346. }
  1347. };
  1348. GLTFLoader.prototype._whenAction = function (action, onComplete) {
  1349. var _this = this;
  1350. var tracker = new GLTFLoaderTracker(function () {
  1351. _this._loaderTrackers.splice(_this._loaderTrackers.indexOf(tracker), 1);
  1352. onComplete();
  1353. });
  1354. this._loaderTrackers.push(tracker);
  1355. this._addLoaderPendingData(tracker);
  1356. action();
  1357. this._removeLoaderPendingData(tracker);
  1358. };
  1359. GLTFLoader.prototype._getDefaultMaterial = function () {
  1360. if (!this._defaultMaterial) {
  1361. var id = "__gltf_default";
  1362. var material = this._babylonScene.getMaterialByName(id);
  1363. if (!material) {
  1364. material = new BABYLON.PBRMaterial(id, this._babylonScene);
  1365. material.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1366. material.metallic = 1;
  1367. material.roughness = 1;
  1368. }
  1369. this._defaultMaterial = material;
  1370. }
  1371. return this._defaultMaterial;
  1372. };
  1373. GLTFLoader.prototype._loadMaterialMetallicRoughnessProperties = function (context, material) {
  1374. var babylonMaterial = material.babylonMaterial;
  1375. // Ensure metallic workflow
  1376. babylonMaterial.metallic = 1;
  1377. babylonMaterial.roughness = 1;
  1378. var properties = material.pbrMetallicRoughness;
  1379. if (!properties) {
  1380. return;
  1381. }
  1382. babylonMaterial.albedoColor = properties.baseColorFactor ? BABYLON.Color3.FromArray(properties.baseColorFactor) : new BABYLON.Color3(1, 1, 1);
  1383. babylonMaterial.metallic = properties.metallicFactor == null ? 1 : properties.metallicFactor;
  1384. babylonMaterial.roughness = properties.roughnessFactor == null ? 1 : properties.roughnessFactor;
  1385. if (properties.baseColorTexture && properties.baseColorTexture.texCoord) {
  1386. var texture = GLTF2.GLTFUtils.GetArrayItem(this._gltf.textures, properties.baseColorTexture.index);
  1387. if (!texture) {
  1388. throw new Error(context + ": Failed to find base color texture " + properties.baseColorTexture.index);
  1389. }
  1390. babylonMaterial.albedoTexture = this._loadTexture("#/textures/" + texture.index, texture, properties.baseColorTexture.texCoord);
  1391. }
  1392. if (properties.metallicRoughnessTexture && properties.metallicRoughnessTexture.texCoord) {
  1393. var texture = GLTF2.GLTFUtils.GetArrayItem(this._gltf.textures, properties.metallicRoughnessTexture.index);
  1394. if (!texture) {
  1395. throw new Error(context + ": Failed to find metallic roughness texture " + properties.metallicRoughnessTexture.index);
  1396. }
  1397. babylonMaterial.metallicTexture = this._loadTexture("#/textures/" + texture.index, texture, properties.metallicRoughnessTexture.texCoord);
  1398. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  1399. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  1400. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  1401. }
  1402. this._loadMaterialAlphaProperties(context, material, properties.baseColorFactor);
  1403. };
  1404. GLTFLoader.prototype._loadMaterial = function (context, material, assign) {
  1405. if (material.babylonMaterial) {
  1406. assign(material.babylonMaterial, false);
  1407. return;
  1408. }
  1409. if (GLTF2.GLTFLoaderExtension.LoadMaterial(this, context, material, assign)) {
  1410. return;
  1411. }
  1412. this._createPbrMaterial(material);
  1413. this._loadMaterialBaseProperties(context, material);
  1414. this._loadMaterialMetallicRoughnessProperties(context, material);
  1415. if (material.babylonMaterial) {
  1416. assign(material.babylonMaterial, true);
  1417. }
  1418. };
  1419. GLTFLoader.prototype._createPbrMaterial = function (material) {
  1420. var babylonMaterial = new BABYLON.PBRMaterial(material.name || "mat" + material.index, this._babylonScene);
  1421. babylonMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1422. material.babylonMaterial = babylonMaterial;
  1423. };
  1424. GLTFLoader.prototype._loadMaterialBaseProperties = function (context, material) {
  1425. var babylonMaterial = material.babylonMaterial;
  1426. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  1427. if (material.doubleSided) {
  1428. babylonMaterial.backFaceCulling = false;
  1429. babylonMaterial.twoSidedLighting = true;
  1430. }
  1431. if (material.normalTexture && material.normalTexture.texCoord) {
  1432. var texture = GLTF2.GLTFUtils.GetArrayItem(this._gltf.textures, material.normalTexture.index);
  1433. if (!texture) {
  1434. throw new Error(context + ": Failed to find normal texture " + material.normalTexture.index);
  1435. }
  1436. babylonMaterial.bumpTexture = this._loadTexture("#/textures/" + texture.index, texture, material.normalTexture.texCoord);
  1437. babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
  1438. babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
  1439. if (material.normalTexture.scale != null) {
  1440. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  1441. }
  1442. }
  1443. if (material.occlusionTexture && material.occlusionTexture.texCoord) {
  1444. var texture = GLTF2.GLTFUtils.GetArrayItem(this._gltf.textures, material.occlusionTexture.index);
  1445. if (!texture) {
  1446. throw new Error(context + ": Failed to find occlusion texture " + material.occlusionTexture.index);
  1447. }
  1448. babylonMaterial.ambientTexture = this._loadTexture("#/textures/" + texture.index, texture, material.occlusionTexture.texCoord);
  1449. babylonMaterial.useAmbientInGrayScale = true;
  1450. if (material.occlusionTexture.strength != null) {
  1451. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  1452. }
  1453. }
  1454. if (material.emissiveTexture && material.emissiveTexture.texCoord) {
  1455. var texture = GLTF2.GLTFUtils.GetArrayItem(this._gltf.textures, material.emissiveTexture.index);
  1456. if (!texture) {
  1457. throw new Error(context + ": Failed to find emissive texture " + material.emissiveTexture.index);
  1458. }
  1459. babylonMaterial.emissiveTexture = this._loadTexture("#/textures/" + texture.index, texture, material.emissiveTexture.texCoord);
  1460. }
  1461. };
  1462. GLTFLoader.prototype._loadMaterialAlphaProperties = function (context, material, colorFactor) {
  1463. var babylonMaterial = material.babylonMaterial;
  1464. var alphaMode = material.alphaMode || "OPAQUE";
  1465. switch (alphaMode) {
  1466. case "OPAQUE":
  1467. // default is opaque
  1468. break;
  1469. case "MASK":
  1470. babylonMaterial.alphaCutOff = (material.alphaCutoff == null ? 0.5 : material.alphaCutoff);
  1471. if (colorFactor) {
  1472. if (colorFactor[3] == 0) {
  1473. babylonMaterial.alphaCutOff = 1;
  1474. }
  1475. else {
  1476. babylonMaterial.alphaCutOff /= colorFactor[3];
  1477. }
  1478. }
  1479. if (babylonMaterial.albedoTexture) {
  1480. babylonMaterial.albedoTexture.hasAlpha = true;
  1481. }
  1482. break;
  1483. case "BLEND":
  1484. if (colorFactor) {
  1485. babylonMaterial.alpha = colorFactor[3];
  1486. }
  1487. if (babylonMaterial.albedoTexture) {
  1488. babylonMaterial.albedoTexture.hasAlpha = true;
  1489. babylonMaterial.useAlphaFromAlbedoTexture = true;
  1490. }
  1491. break;
  1492. default:
  1493. throw new Error(context + ": Invalid alpha mode '" + material.alphaMode + "'");
  1494. }
  1495. };
  1496. GLTFLoader.prototype._loadTexture = function (context, texture, coordinatesIndex) {
  1497. var _this = this;
  1498. var sampler = (texture.sampler == null ? {} : GLTF2.GLTFUtils.GetArrayItem(this._gltf.samplers, texture.sampler));
  1499. if (!sampler) {
  1500. throw new Error(context + ": Failed to find sampler " + texture.sampler);
  1501. }
  1502. var noMipMaps = (sampler.minFilter === GLTF2.ETextureMinFilter.NEAREST || sampler.minFilter === GLTF2.ETextureMinFilter.LINEAR);
  1503. var samplingMode = GLTF2.GLTFUtils.GetTextureSamplingMode(sampler.magFilter, sampler.minFilter);
  1504. this._addPendingData(texture);
  1505. var babylonTexture = new BABYLON.Texture(null, this._babylonScene, noMipMaps, false, samplingMode, function () {
  1506. _this._tryCatchOnError(function () {
  1507. _this._removePendingData(texture);
  1508. });
  1509. }, function (message) {
  1510. _this._tryCatchOnError(function () {
  1511. throw new Error(context + ": " + message);
  1512. });
  1513. });
  1514. if (texture.url) {
  1515. babylonTexture.updateURL(texture.url);
  1516. }
  1517. else if (texture.dataReadyObservable) {
  1518. texture.dataReadyObservable.add(function (texture) {
  1519. babylonTexture.updateURL(texture.url);
  1520. });
  1521. }
  1522. else {
  1523. texture.dataReadyObservable = new BABYLON.Observable();
  1524. texture.dataReadyObservable.add(function (texture) {
  1525. babylonTexture.updateURL(texture.url);
  1526. });
  1527. var image_1 = GLTF2.GLTFUtils.GetArrayItem(this._gltf.images, texture.source);
  1528. if (!image_1) {
  1529. throw new Error(context + ": Failed to find source " + texture.source);
  1530. }
  1531. this._loadImage("#/images/" + image_1.index, image_1, function (data) {
  1532. texture.url = URL.createObjectURL(new Blob([data], { type: image_1.mimeType }));
  1533. if (texture.dataReadyObservable) {
  1534. texture.dataReadyObservable.notifyObservers(texture);
  1535. }
  1536. });
  1537. }
  1538. babylonTexture.coordinatesIndex = coordinatesIndex || 0;
  1539. if (sampler.wrapS !== undefined) {
  1540. babylonTexture.wrapU = GLTF2.GLTFUtils.GetTextureWrapMode(sampler.wrapS);
  1541. }
  1542. if (sampler.wrapT !== undefined) {
  1543. babylonTexture.wrapV = GLTF2.GLTFUtils.GetTextureWrapMode(sampler.wrapT);
  1544. }
  1545. babylonTexture.name = texture.name || "texture" + texture.index;
  1546. if (this._parent.onTextureLoaded) {
  1547. this._parent.onTextureLoaded(babylonTexture);
  1548. }
  1549. return babylonTexture;
  1550. };
  1551. GLTFLoader.prototype._loadImage = function (context, image, onSuccess) {
  1552. if (image.uri) {
  1553. if (GLTF2.GLTFUtils.IsBase64(image.uri)) {
  1554. onSuccess(new Uint8Array(GLTF2.GLTFUtils.DecodeBase64(image.uri)));
  1555. }
  1556. else {
  1557. this._loadUri(context, image.uri, onSuccess);
  1558. }
  1559. }
  1560. else {
  1561. var bufferView = GLTF2.GLTFUtils.GetArrayItem(this._gltf.bufferViews, image.bufferView);
  1562. if (!bufferView) {
  1563. throw new Error(context + ": Failed to find buffer view " + image.bufferView);
  1564. }
  1565. this._loadBufferViewAsync("#/bufferViews/" + bufferView.index, bufferView, onSuccess);
  1566. }
  1567. };
  1568. GLTFLoader.prototype._loadUri = function (context, uri, onSuccess) {
  1569. var _this = this;
  1570. if (!GLTF2.GLTFUtils.ValidateUri(uri)) {
  1571. throw new Error(context + ": Uri '" + uri + "' is invalid");
  1572. }
  1573. var request = BABYLON.Tools.LoadFile(this._rootUrl + uri, function (data) {
  1574. _this._tryCatchOnError(function () {
  1575. onSuccess(new Uint8Array(data));
  1576. });
  1577. }, function (event) {
  1578. _this._tryCatchOnError(function () {
  1579. _this._onProgress(event);
  1580. });
  1581. }, this._babylonScene.database, true, function (request) {
  1582. _this._tryCatchOnError(function () {
  1583. throw new Error(context + ": Failed to load '" + uri + "'" + (request ? ": " + request.status + " " + request.statusText : ""));
  1584. });
  1585. });
  1586. if (request) {
  1587. this._requests.push(request);
  1588. }
  1589. };
  1590. GLTFLoader.prototype._tryCatchOnError = function (handler) {
  1591. try {
  1592. handler();
  1593. }
  1594. catch (e) {
  1595. this._onError(e.message);
  1596. }
  1597. };
  1598. GLTFLoader.Extensions = {};
  1599. return GLTFLoader;
  1600. }());
  1601. GLTF2.GLTFLoader = GLTFLoader;
  1602. BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = function (parent) { return new GLTFLoader(parent); };
  1603. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1604. })(BABYLON || (BABYLON = {}));
  1605. //# sourceMappingURL=babylon.glTFLoader.js.map
  1606. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  1607. var BABYLON;
  1608. (function (BABYLON) {
  1609. var GLTF2;
  1610. (function (GLTF2) {
  1611. /**
  1612. * Utils functions for GLTF
  1613. */
  1614. var GLTFUtils = /** @class */ (function () {
  1615. function GLTFUtils() {
  1616. }
  1617. /**
  1618. * If the uri is a base64 string
  1619. * @param uri: the uri to test
  1620. */
  1621. GLTFUtils.IsBase64 = function (uri) {
  1622. return uri.length < 5 ? false : uri.substr(0, 5) === "data:";
  1623. };
  1624. /**
  1625. * Decode the base64 uri
  1626. * @param uri: the uri to decode
  1627. */
  1628. GLTFUtils.DecodeBase64 = function (uri) {
  1629. var decodedString = atob(uri.split(",")[1]);
  1630. var bufferLength = decodedString.length;
  1631. var bufferView = new Uint8Array(new ArrayBuffer(bufferLength));
  1632. for (var i = 0; i < bufferLength; i++) {
  1633. bufferView[i] = decodedString.charCodeAt(i);
  1634. }
  1635. return bufferView.buffer;
  1636. };
  1637. GLTFUtils.ValidateUri = function (uri) {
  1638. return (uri.indexOf("..") === -1);
  1639. };
  1640. GLTFUtils.AssignIndices = function (array) {
  1641. if (array) {
  1642. for (var index = 0; index < array.length; index++) {
  1643. array[index].index = index;
  1644. }
  1645. }
  1646. };
  1647. GLTFUtils.GetArrayItem = function (array, index) {
  1648. if (!array || !array[index]) {
  1649. return null;
  1650. }
  1651. return array[index];
  1652. };
  1653. GLTFUtils.GetTextureWrapMode = function (mode) {
  1654. // Set defaults if undefined
  1655. mode = mode === undefined ? GLTF2.ETextureWrapMode.REPEAT : mode;
  1656. switch (mode) {
  1657. case GLTF2.ETextureWrapMode.CLAMP_TO_EDGE: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  1658. case GLTF2.ETextureWrapMode.MIRRORED_REPEAT: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  1659. case GLTF2.ETextureWrapMode.REPEAT: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1660. default:
  1661. BABYLON.Tools.Warn("Invalid texture wrap mode (" + mode + ")");
  1662. return BABYLON.Texture.WRAP_ADDRESSMODE;
  1663. }
  1664. };
  1665. GLTFUtils.GetTextureSamplingMode = function (magFilter, minFilter) {
  1666. // Set defaults if undefined
  1667. magFilter = magFilter === undefined ? GLTF2.ETextureMagFilter.LINEAR : magFilter;
  1668. minFilter = minFilter === undefined ? GLTF2.ETextureMinFilter.LINEAR_MIPMAP_LINEAR : minFilter;
  1669. if (magFilter === GLTF2.ETextureMagFilter.LINEAR) {
  1670. switch (minFilter) {
  1671. case GLTF2.ETextureMinFilter.NEAREST: return BABYLON.Texture.LINEAR_NEAREST;
  1672. case GLTF2.ETextureMinFilter.LINEAR: return BABYLON.Texture.LINEAR_LINEAR;
  1673. case GLTF2.ETextureMinFilter.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.LINEAR_NEAREST_MIPNEAREST;
  1674. case GLTF2.ETextureMinFilter.LINEAR_MIPMAP_NEAREST: return BABYLON.Texture.LINEAR_LINEAR_MIPNEAREST;
  1675. case GLTF2.ETextureMinFilter.NEAREST_MIPMAP_LINEAR: return BABYLON.Texture.LINEAR_NEAREST_MIPLINEAR;
  1676. case GLTF2.ETextureMinFilter.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  1677. default:
  1678. BABYLON.Tools.Warn("Invalid texture minification filter (" + minFilter + ")");
  1679. return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  1680. }
  1681. }
  1682. else {
  1683. if (magFilter !== GLTF2.ETextureMagFilter.NEAREST) {
  1684. BABYLON.Tools.Warn("Invalid texture magnification filter (" + magFilter + ")");
  1685. }
  1686. switch (minFilter) {
  1687. case GLTF2.ETextureMinFilter.NEAREST: return BABYLON.Texture.NEAREST_NEAREST;
  1688. case GLTF2.ETextureMinFilter.LINEAR: return BABYLON.Texture.NEAREST_LINEAR;
  1689. case GLTF2.ETextureMinFilter.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  1690. case GLTF2.ETextureMinFilter.LINEAR_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_LINEAR_MIPNEAREST;
  1691. case GLTF2.ETextureMinFilter.NEAREST_MIPMAP_LINEAR: return BABYLON.Texture.NEAREST_NEAREST_MIPLINEAR;
  1692. case GLTF2.ETextureMinFilter.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.NEAREST_LINEAR_MIPLINEAR;
  1693. default:
  1694. BABYLON.Tools.Warn("Invalid texture minification filter (" + minFilter + ")");
  1695. return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  1696. }
  1697. }
  1698. };
  1699. return GLTFUtils;
  1700. }());
  1701. GLTF2.GLTFUtils = GLTFUtils;
  1702. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1703. })(BABYLON || (BABYLON = {}));
  1704. //# sourceMappingURL=babylon.glTFLoaderUtils.js.map
  1705. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  1706. var BABYLON;
  1707. (function (BABYLON) {
  1708. var GLTF2;
  1709. (function (GLTF2) {
  1710. var GLTFLoaderExtension = /** @class */ (function () {
  1711. function GLTFLoaderExtension() {
  1712. this.enabled = true;
  1713. }
  1714. GLTFLoaderExtension.prototype._traverseNode = function (loader, context, node, action, parentNode) { return false; };
  1715. GLTFLoaderExtension.prototype._loadNode = function (loader, context, node) { return false; };
  1716. GLTFLoaderExtension.prototype._loadMaterial = function (loader, context, material, assign) { return false; };
  1717. GLTFLoaderExtension.prototype._loadExtension = function (property, action) {
  1718. var _this = this;
  1719. if (!property.extensions) {
  1720. return false;
  1721. }
  1722. var extension = property.extensions[this.name];
  1723. if (!extension) {
  1724. return false;
  1725. }
  1726. // Clear out the extension before executing the action to avoid recursing into the same property.
  1727. property.extensions[this.name] = undefined;
  1728. action(extension, function () {
  1729. // Restore the extension after completing the action.
  1730. if (property.extensions) {
  1731. property.extensions[_this.name] = extension;
  1732. }
  1733. });
  1734. return true;
  1735. };
  1736. GLTFLoaderExtension.TraverseNode = function (loader, context, node, action, parentNode) {
  1737. return this._ApplyExtensions(function (extension) { return extension._traverseNode(loader, context, node, action, parentNode); });
  1738. };
  1739. GLTFLoaderExtension.LoadNode = function (loader, context, node) {
  1740. return this._ApplyExtensions(function (extension) { return extension._loadNode(loader, context, node); });
  1741. };
  1742. GLTFLoaderExtension.LoadMaterial = function (loader, context, material, assign) {
  1743. return this._ApplyExtensions(function (extension) { return extension._loadMaterial(loader, context, material, assign); });
  1744. };
  1745. GLTFLoaderExtension._ApplyExtensions = function (action) {
  1746. var extensions = GLTFLoaderExtension._Extensions;
  1747. if (!extensions) {
  1748. return false;
  1749. }
  1750. for (var _i = 0, extensions_1 = extensions; _i < extensions_1.length; _i++) {
  1751. var extension = extensions_1[_i];
  1752. if (extension.enabled && action(extension)) {
  1753. return true;
  1754. }
  1755. }
  1756. return false;
  1757. };
  1758. //
  1759. // Utilities
  1760. //
  1761. GLTFLoaderExtension._Extensions = [];
  1762. return GLTFLoaderExtension;
  1763. }());
  1764. GLTF2.GLTFLoaderExtension = GLTFLoaderExtension;
  1765. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1766. })(BABYLON || (BABYLON = {}));
  1767. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  1768. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  1769. var __extends = (this && this.__extends) || (function () {
  1770. var extendStatics = Object.setPrototypeOf ||
  1771. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  1772. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  1773. return function (d, b) {
  1774. extendStatics(d, b);
  1775. function __() { this.constructor = d; }
  1776. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  1777. };
  1778. })();
  1779. var BABYLON;
  1780. (function (BABYLON) {
  1781. var GLTF2;
  1782. (function (GLTF2) {
  1783. var Extensions;
  1784. (function (Extensions) {
  1785. // See https://github.com/sbtron/glTF/tree/MSFT_lod/extensions/Vendor/MSFT_lod for more information about this extension.
  1786. var MSFTLOD = /** @class */ (function (_super) {
  1787. __extends(MSFTLOD, _super);
  1788. function MSFTLOD() {
  1789. return _super !== null && _super.apply(this, arguments) || this;
  1790. }
  1791. Object.defineProperty(MSFTLOD.prototype, "name", {
  1792. get: function () {
  1793. return "MSFT_lod";
  1794. },
  1795. enumerable: true,
  1796. configurable: true
  1797. });
  1798. MSFTLOD.prototype._traverseNode = function (loader, context, node, action, parentNode) {
  1799. return this._loadExtension(node, function (extension, onComplete) {
  1800. if (!loader._gltf.nodes) {
  1801. return;
  1802. }
  1803. for (var i = extension.ids.length - 1; i >= 0; i--) {
  1804. var lodNode = GLTF2.GLTFUtils.GetArrayItem(loader._gltf.nodes, extension.ids[i]);
  1805. if (!lodNode) {
  1806. throw new Error(context + ": Failed to find node " + extension.ids[i]);
  1807. }
  1808. loader._traverseNode(context, lodNode, action, parentNode);
  1809. }
  1810. loader._traverseNode(context, node, action, parentNode);
  1811. onComplete();
  1812. });
  1813. };
  1814. MSFTLOD.prototype._loadNode = function (loader, context, node) {
  1815. var _this = this;
  1816. return this._loadExtension(node, function (extension, onComplete) {
  1817. var nodes = [node.index].concat(extension.ids).map(function (index) { return loader._gltf.nodes[index]; });
  1818. loader._addLoaderPendingData(node);
  1819. _this._loadNodeLOD(loader, context, nodes, nodes.length - 1, function () {
  1820. loader._removeLoaderPendingData(node);
  1821. onComplete();
  1822. });
  1823. });
  1824. };
  1825. MSFTLOD.prototype._loadNodeLOD = function (loader, context, nodes, index, onComplete) {
  1826. var _this = this;
  1827. loader._whenAction(function () {
  1828. loader._loadNode(context, nodes[index]);
  1829. }, function () {
  1830. if (index !== nodes.length - 1) {
  1831. var previousNode = nodes[index + 1];
  1832. if (previousNode.babylonMesh) {
  1833. previousNode.babylonMesh.setEnabled(false);
  1834. }
  1835. }
  1836. if (index === 0) {
  1837. onComplete();
  1838. return;
  1839. }
  1840. setTimeout(function () {
  1841. loader._tryCatchOnError(function () {
  1842. _this._loadNodeLOD(loader, context, nodes, index - 1, onComplete);
  1843. });
  1844. }, MSFTLOD.MinimalLODDelay);
  1845. });
  1846. };
  1847. MSFTLOD.prototype._loadMaterial = function (loader, context, material, assign) {
  1848. var _this = this;
  1849. return this._loadExtension(material, function (extension, onComplete) {
  1850. var materials = [material.index].concat(extension.ids).map(function (index) { return loader._gltf.materials[index]; });
  1851. loader._addLoaderPendingData(material);
  1852. _this._loadMaterialLOD(loader, context, materials, materials.length - 1, assign, function () {
  1853. if (material.extensions) {
  1854. material.extensions[_this.name] = extension;
  1855. }
  1856. loader._removeLoaderPendingData(material);
  1857. onComplete();
  1858. });
  1859. });
  1860. };
  1861. MSFTLOD.prototype._loadMaterialLOD = function (loader, context, materials, index, assign, onComplete) {
  1862. var _this = this;
  1863. loader._loadMaterial(context, materials[index], function (babylonMaterial, isNew) {
  1864. assign(babylonMaterial, isNew);
  1865. if (index === 0) {
  1866. onComplete();
  1867. return;
  1868. }
  1869. // Load the next LOD when the loader is ready to render and
  1870. // all active material textures of the current LOD are loaded.
  1871. loader._executeWhenRenderReady(function () {
  1872. BABYLON.BaseTexture.WhenAllReady(babylonMaterial.getActiveTextures(), function () {
  1873. setTimeout(function () {
  1874. loader._tryCatchOnError(function () {
  1875. _this._loadMaterialLOD(loader, context, materials, index - 1, assign, onComplete);
  1876. });
  1877. }, MSFTLOD.MinimalLODDelay);
  1878. });
  1879. });
  1880. });
  1881. };
  1882. /**
  1883. * Specify the minimal delay between LODs in ms (default = 250)
  1884. */
  1885. MSFTLOD.MinimalLODDelay = 250;
  1886. return MSFTLOD;
  1887. }(GLTF2.GLTFLoaderExtension));
  1888. Extensions.MSFTLOD = MSFTLOD;
  1889. GLTF2.GLTFLoader.RegisterExtension(new MSFTLOD());
  1890. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  1891. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1892. })(BABYLON || (BABYLON = {}));
  1893. //# sourceMappingURL=MSFT_lod.js.map
  1894. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  1895. var __extends = (this && this.__extends) || (function () {
  1896. var extendStatics = Object.setPrototypeOf ||
  1897. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  1898. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  1899. return function (d, b) {
  1900. extendStatics(d, b);
  1901. function __() { this.constructor = d; }
  1902. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  1903. };
  1904. })();
  1905. var BABYLON;
  1906. (function (BABYLON) {
  1907. var GLTF2;
  1908. (function (GLTF2) {
  1909. var Extensions;
  1910. (function (Extensions) {
  1911. var KHRMaterialsPbrSpecularGlossiness = /** @class */ (function (_super) {
  1912. __extends(KHRMaterialsPbrSpecularGlossiness, _super);
  1913. function KHRMaterialsPbrSpecularGlossiness() {
  1914. return _super !== null && _super.apply(this, arguments) || this;
  1915. }
  1916. Object.defineProperty(KHRMaterialsPbrSpecularGlossiness.prototype, "name", {
  1917. get: function () {
  1918. return "KHR_materials_pbrSpecularGlossiness";
  1919. },
  1920. enumerable: true,
  1921. configurable: true
  1922. });
  1923. KHRMaterialsPbrSpecularGlossiness.prototype._loadMaterial = function (loader, context, material, assign) {
  1924. var _this = this;
  1925. return this._loadExtension(material, function (extension, onComplete) {
  1926. loader._createPbrMaterial(material);
  1927. loader._loadMaterialBaseProperties(context, material);
  1928. _this._loadSpecularGlossinessProperties(loader, context, material, extension);
  1929. if (material.babylonMaterial) {
  1930. assign(material.babylonMaterial, true);
  1931. }
  1932. });
  1933. };
  1934. KHRMaterialsPbrSpecularGlossiness.prototype._loadSpecularGlossinessProperties = function (loader, context, material, properties) {
  1935. var babylonMaterial = material.babylonMaterial;
  1936. babylonMaterial.albedoColor = properties.diffuseFactor ? BABYLON.Color3.FromArray(properties.diffuseFactor) : new BABYLON.Color3(1, 1, 1);
  1937. babylonMaterial.reflectivityColor = properties.specularFactor ? BABYLON.Color3.FromArray(properties.specularFactor) : new BABYLON.Color3(1, 1, 1);
  1938. babylonMaterial.microSurface = properties.glossinessFactor == null ? 1 : properties.glossinessFactor;
  1939. if (loader._gltf.textures) {
  1940. if (properties.diffuseTexture) {
  1941. var texture = GLTF2.GLTFUtils.GetArrayItem(loader._gltf.textures, properties.diffuseTexture.index);
  1942. if (!texture) {
  1943. throw new Error(context + ": Failed to find diffuse texture " + properties.diffuseTexture.index);
  1944. }
  1945. if (properties.diffuseTexture.texCoord) {
  1946. babylonMaterial.albedoTexture = loader._loadTexture("textures[" + texture.index + "]", texture, properties.diffuseTexture.texCoord);
  1947. }
  1948. }
  1949. if (properties.specularGlossinessTexture) {
  1950. var texture = GLTF2.GLTFUtils.GetArrayItem(loader._gltf.textures, properties.specularGlossinessTexture.index);
  1951. if (!texture) {
  1952. throw new Error(context + ": Failed to find diffuse texture " + properties.specularGlossinessTexture.index);
  1953. }
  1954. if (properties.specularGlossinessTexture.texCoord) {
  1955. babylonMaterial.reflectivityTexture = loader._loadTexture("textures[" + texture.index + "]", texture, properties.specularGlossinessTexture.texCoord);
  1956. }
  1957. babylonMaterial.reflectivityTexture.hasAlpha = true;
  1958. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  1959. }
  1960. }
  1961. loader._loadMaterialAlphaProperties(context, material, properties.diffuseFactor);
  1962. };
  1963. return KHRMaterialsPbrSpecularGlossiness;
  1964. }(GLTF2.GLTFLoaderExtension));
  1965. Extensions.KHRMaterialsPbrSpecularGlossiness = KHRMaterialsPbrSpecularGlossiness;
  1966. GLTF2.GLTFLoader.RegisterExtension(new KHRMaterialsPbrSpecularGlossiness());
  1967. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  1968. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1969. })(BABYLON || (BABYLON = {}));
  1970. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map