babylon.glTF2FileLoader.js 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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 = (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.importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  25. var loaderData = GLTFFileLoader._parse(data, onError);
  26. if (!loaderData) {
  27. return;
  28. }
  29. var loader = this._getLoader(loaderData, onError);
  30. if (!loader) {
  31. return;
  32. }
  33. loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onSuccess, onProgress, onError);
  34. };
  35. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onSuccess, onProgress, onError) {
  36. var loaderData = GLTFFileLoader._parse(data, onError);
  37. if (!loaderData) {
  38. return;
  39. }
  40. var loader = this._getLoader(loaderData, onError);
  41. if (!loader) {
  42. return;
  43. }
  44. return loader.loadAsync(scene, loaderData, rootUrl, onSuccess, onProgress, onError);
  45. };
  46. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  47. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  48. };
  49. GLTFFileLoader._parse = function (data, onError) {
  50. if (data instanceof ArrayBuffer) {
  51. return GLTFFileLoader._parseBinary(data, onError);
  52. }
  53. try {
  54. return {
  55. json: JSON.parse(data),
  56. bin: null
  57. };
  58. }
  59. catch (e) {
  60. onError(e.message);
  61. return null;
  62. }
  63. };
  64. GLTFFileLoader.prototype._getLoader = function (loaderData, onError) {
  65. var loaderVersion = { major: 2, minor: 0 };
  66. var asset = loaderData.json.asset || {};
  67. var version = GLTFFileLoader._parseVersion(asset.version);
  68. if (!version) {
  69. onError("Invalid version: " + asset.version);
  70. return null;
  71. }
  72. if (asset.minVersion !== undefined) {
  73. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  74. if (!minVersion) {
  75. onError("Invalid minimum version: " + asset.minVersion);
  76. return null;
  77. }
  78. if (GLTFFileLoader._compareVersion(minVersion, loaderVersion) > 0) {
  79. onError("Incompatible minimum version: " + asset.minVersion);
  80. return null;
  81. }
  82. }
  83. var createLoaders = {
  84. 1: GLTFFileLoader.CreateGLTFLoaderV1,
  85. 2: GLTFFileLoader.CreateGLTFLoaderV2
  86. };
  87. var createLoader = createLoaders[version.major];
  88. if (!createLoader) {
  89. onError("Unsupported version: " + asset.version);
  90. return null;
  91. }
  92. return createLoader(this);
  93. };
  94. GLTFFileLoader._parseBinary = function (data, onError) {
  95. var Binary = {
  96. Magic: 0x46546C67
  97. };
  98. var binaryReader = new BinaryReader(data);
  99. var magic = binaryReader.readUint32();
  100. if (magic !== Binary.Magic) {
  101. onError("Unexpected magic: " + magic);
  102. return null;
  103. }
  104. var version = binaryReader.readUint32();
  105. switch (version) {
  106. case 1: return GLTFFileLoader._parseV1(binaryReader, onError);
  107. case 2: return GLTFFileLoader._parseV2(binaryReader, onError);
  108. }
  109. onError("Unsupported version: " + version);
  110. return null;
  111. };
  112. GLTFFileLoader._parseV1 = function (binaryReader, onError) {
  113. var ContentFormat = {
  114. JSON: 0
  115. };
  116. var length = binaryReader.readUint32();
  117. if (length != binaryReader.getLength()) {
  118. onError("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  119. return null;
  120. }
  121. var contentLength = binaryReader.readUint32();
  122. var contentFormat = binaryReader.readUint32();
  123. var content;
  124. switch (contentFormat) {
  125. case ContentFormat.JSON:
  126. content = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength)));
  127. break;
  128. default:
  129. onError("Unexpected content format: " + contentFormat);
  130. return null;
  131. }
  132. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  133. var body = binaryReader.readUint8Array(bytesRemaining);
  134. return {
  135. json: content,
  136. bin: body
  137. };
  138. };
  139. GLTFFileLoader._parseV2 = function (binaryReader, onError) {
  140. var ChunkFormat = {
  141. JSON: 0x4E4F534A,
  142. BIN: 0x004E4942
  143. };
  144. var length = binaryReader.readUint32();
  145. if (length !== binaryReader.getLength()) {
  146. onError("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  147. return null;
  148. }
  149. // JSON chunk
  150. var chunkLength = binaryReader.readUint32();
  151. var chunkFormat = binaryReader.readUint32();
  152. if (chunkFormat !== ChunkFormat.JSON) {
  153. onError("First chunk format is not JSON");
  154. return null;
  155. }
  156. var json = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength)));
  157. // Look for BIN chunk
  158. var bin = null;
  159. while (binaryReader.getPosition() < binaryReader.getLength()) {
  160. chunkLength = binaryReader.readUint32();
  161. chunkFormat = binaryReader.readUint32();
  162. switch (chunkFormat) {
  163. case ChunkFormat.JSON:
  164. onError("Unexpected JSON chunk");
  165. return null;
  166. case ChunkFormat.BIN:
  167. bin = binaryReader.readUint8Array(chunkLength);
  168. break;
  169. default:
  170. // ignore unrecognized chunkFormat
  171. binaryReader.skipBytes(chunkLength);
  172. break;
  173. }
  174. }
  175. return {
  176. json: json,
  177. bin: bin
  178. };
  179. };
  180. GLTFFileLoader._parseVersion = function (version) {
  181. if (!version) {
  182. return null;
  183. }
  184. var parts = version.split(".");
  185. if (parts.length != 2) {
  186. return null;
  187. }
  188. var major = +parts[0];
  189. if (isNaN(major)) {
  190. return null;
  191. }
  192. var minor = +parts[1];
  193. if (isNaN(minor)) {
  194. return null;
  195. }
  196. return {
  197. major: major,
  198. minor: minor
  199. };
  200. };
  201. GLTFFileLoader._compareVersion = function (a, b) {
  202. if (a.major > b.major)
  203. return 1;
  204. if (a.major < b.major)
  205. return -1;
  206. if (a.minor > b.minor)
  207. return 1;
  208. if (a.minor < b.minor)
  209. return -1;
  210. return 0;
  211. };
  212. GLTFFileLoader._decodeBufferToText = function (view) {
  213. var result = "";
  214. var length = view.byteLength;
  215. for (var i = 0; i < length; ++i) {
  216. result += String.fromCharCode(view[i]);
  217. }
  218. return result;
  219. };
  220. return GLTFFileLoader;
  221. }());
  222. // V1 options
  223. GLTFFileLoader.HomogeneousCoordinates = false;
  224. GLTFFileLoader.IncrementalLoading = true;
  225. BABYLON.GLTFFileLoader = GLTFFileLoader;
  226. var BinaryReader = (function () {
  227. function BinaryReader(arrayBuffer) {
  228. this._arrayBuffer = arrayBuffer;
  229. this._dataView = new DataView(arrayBuffer);
  230. this._byteOffset = 0;
  231. }
  232. BinaryReader.prototype.getPosition = function () {
  233. return this._byteOffset;
  234. };
  235. BinaryReader.prototype.getLength = function () {
  236. return this._arrayBuffer.byteLength;
  237. };
  238. BinaryReader.prototype.readUint32 = function () {
  239. var value = this._dataView.getUint32(this._byteOffset, true);
  240. this._byteOffset += 4;
  241. return value;
  242. };
  243. BinaryReader.prototype.readUint8Array = function (length) {
  244. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  245. this._byteOffset += length;
  246. return value;
  247. };
  248. BinaryReader.prototype.skipBytes = function (length) {
  249. this._byteOffset += length;
  250. };
  251. return BinaryReader;
  252. }());
  253. if (BABYLON.SceneLoader) {
  254. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  255. }
  256. })(BABYLON || (BABYLON = {}));
  257. //# sourceMappingURL=babylon.glTFFileLoader.js.map
  258. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  259. var BABYLON;
  260. (function (BABYLON) {
  261. var GLTF2;
  262. (function (GLTF2) {
  263. /**
  264. * Enums
  265. */
  266. var EComponentType;
  267. (function (EComponentType) {
  268. EComponentType[EComponentType["BYTE"] = 5120] = "BYTE";
  269. EComponentType[EComponentType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  270. EComponentType[EComponentType["SHORT"] = 5122] = "SHORT";
  271. EComponentType[EComponentType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  272. EComponentType[EComponentType["UNSIGNED_INT"] = 5125] = "UNSIGNED_INT";
  273. EComponentType[EComponentType["FLOAT"] = 5126] = "FLOAT";
  274. })(EComponentType = GLTF2.EComponentType || (GLTF2.EComponentType = {}));
  275. var EMeshPrimitiveMode;
  276. (function (EMeshPrimitiveMode) {
  277. EMeshPrimitiveMode[EMeshPrimitiveMode["POINTS"] = 0] = "POINTS";
  278. EMeshPrimitiveMode[EMeshPrimitiveMode["LINES"] = 1] = "LINES";
  279. EMeshPrimitiveMode[EMeshPrimitiveMode["LINE_LOOP"] = 2] = "LINE_LOOP";
  280. EMeshPrimitiveMode[EMeshPrimitiveMode["LINE_STRIP"] = 3] = "LINE_STRIP";
  281. EMeshPrimitiveMode[EMeshPrimitiveMode["TRIANGLES"] = 4] = "TRIANGLES";
  282. EMeshPrimitiveMode[EMeshPrimitiveMode["TRIANGLE_STRIP"] = 5] = "TRIANGLE_STRIP";
  283. EMeshPrimitiveMode[EMeshPrimitiveMode["TRIANGLE_FAN"] = 6] = "TRIANGLE_FAN";
  284. })(EMeshPrimitiveMode = GLTF2.EMeshPrimitiveMode || (GLTF2.EMeshPrimitiveMode = {}));
  285. var ETextureMagFilter;
  286. (function (ETextureMagFilter) {
  287. ETextureMagFilter[ETextureMagFilter["NEAREST"] = 9728] = "NEAREST";
  288. ETextureMagFilter[ETextureMagFilter["LINEAR"] = 9729] = "LINEAR";
  289. })(ETextureMagFilter = GLTF2.ETextureMagFilter || (GLTF2.ETextureMagFilter = {}));
  290. var ETextureMinFilter;
  291. (function (ETextureMinFilter) {
  292. ETextureMinFilter[ETextureMinFilter["NEAREST"] = 9728] = "NEAREST";
  293. ETextureMinFilter[ETextureMinFilter["LINEAR"] = 9729] = "LINEAR";
  294. ETextureMinFilter[ETextureMinFilter["NEAREST_MIPMAP_NEAREST"] = 9984] = "NEAREST_MIPMAP_NEAREST";
  295. ETextureMinFilter[ETextureMinFilter["LINEAR_MIPMAP_NEAREST"] = 9985] = "LINEAR_MIPMAP_NEAREST";
  296. ETextureMinFilter[ETextureMinFilter["NEAREST_MIPMAP_LINEAR"] = 9986] = "NEAREST_MIPMAP_LINEAR";
  297. ETextureMinFilter[ETextureMinFilter["LINEAR_MIPMAP_LINEAR"] = 9987] = "LINEAR_MIPMAP_LINEAR";
  298. })(ETextureMinFilter = GLTF2.ETextureMinFilter || (GLTF2.ETextureMinFilter = {}));
  299. var ETextureWrapMode;
  300. (function (ETextureWrapMode) {
  301. ETextureWrapMode[ETextureWrapMode["CLAMP_TO_EDGE"] = 33071] = "CLAMP_TO_EDGE";
  302. ETextureWrapMode[ETextureWrapMode["MIRRORED_REPEAT"] = 33648] = "MIRRORED_REPEAT";
  303. ETextureWrapMode[ETextureWrapMode["REPEAT"] = 10497] = "REPEAT";
  304. })(ETextureWrapMode = GLTF2.ETextureWrapMode || (GLTF2.ETextureWrapMode = {}));
  305. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  306. })(BABYLON || (BABYLON = {}));
  307. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  308. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  309. var BABYLON;
  310. (function (BABYLON) {
  311. var GLTF2;
  312. (function (GLTF2) {
  313. var GLTFLoader = (function () {
  314. function GLTFLoader(parent) {
  315. this._renderReady = false;
  316. this._disposed = false;
  317. this._objectURLs = new Array();
  318. this._blockPendingTracking = false;
  319. // Observable with boolean indicating success or error.
  320. this._renderReadyObservable = new BABYLON.Observable();
  321. // Count of pending work that needs to complete before the asset is rendered.
  322. this._renderPendingCount = 0;
  323. // Count of pending work that needs to complete before the loader is cleared.
  324. this._loaderPendingCount = 0;
  325. this._parent = parent;
  326. }
  327. GLTFLoader.RegisterExtension = function (extension) {
  328. if (GLTFLoader.Extensions[extension.name]) {
  329. BABYLON.Tools.Error("Extension with the same name '" + extension.name + "' already exists");
  330. return;
  331. }
  332. GLTFLoader.Extensions[extension.name] = extension;
  333. // Keep the order of registration so that extensions registered first are called first.
  334. GLTF2.GLTFLoaderExtension._Extensions.push(extension);
  335. };
  336. Object.defineProperty(GLTFLoader.prototype, "gltf", {
  337. get: function () {
  338. return this._gltf;
  339. },
  340. enumerable: true,
  341. configurable: true
  342. });
  343. Object.defineProperty(GLTFLoader.prototype, "babylonScene", {
  344. get: function () {
  345. return this._babylonScene;
  346. },
  347. enumerable: true,
  348. configurable: true
  349. });
  350. GLTFLoader.prototype.executeWhenRenderReady = function (func) {
  351. if (this._renderReady) {
  352. func();
  353. }
  354. else {
  355. this._renderReadyObservable.add(func);
  356. }
  357. };
  358. GLTFLoader.prototype.dispose = function () {
  359. if (this._disposed) {
  360. return;
  361. }
  362. this._disposed = true;
  363. // Revoke object urls created during load
  364. this._objectURLs.forEach(function (url) { return URL.revokeObjectURL(url); });
  365. this._objectURLs.length = 0;
  366. this._gltf = undefined;
  367. this._babylonScene = undefined;
  368. this._rootUrl = undefined;
  369. this._defaultMaterial = undefined;
  370. this._successCallback = undefined;
  371. this._errorCallback = undefined;
  372. this._renderReady = false;
  373. this._renderReadyObservable.clear();
  374. this._renderPendingCount = 0;
  375. this._loaderPendingCount = 0;
  376. };
  377. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  378. var _this = this;
  379. this._loadAsync(meshesNames, scene, data, rootUrl, function () {
  380. onSuccess(_this._getMeshes(), null, _this._getSkeletons());
  381. }, onProgress, onError);
  382. };
  383. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onSuccess, onProgress, onError) {
  384. this._loadAsync(null, scene, data, rootUrl, onSuccess, onProgress, onError);
  385. };
  386. GLTFLoader.prototype._loadAsync = function (nodeNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  387. this._loadData(data);
  388. this._babylonScene = scene;
  389. this._rootUrl = rootUrl;
  390. this._successCallback = onSuccess;
  391. this._progressCallback = onProgress;
  392. this._errorCallback = onError;
  393. this.addPendingData(this);
  394. this._loadScene(nodeNames);
  395. this._loadAnimations();
  396. this.removePendingData(this);
  397. };
  398. GLTFLoader.prototype._onError = function (message) {
  399. this._errorCallback(message);
  400. this.dispose();
  401. };
  402. GLTFLoader.prototype._onProgress = function (event) {
  403. this._progressCallback(event);
  404. };
  405. GLTFLoader.prototype._onRenderReady = function () {
  406. switch (this._parent.coordinateSystemMode) {
  407. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO:
  408. if (!this._babylonScene.useRightHandedSystem) {
  409. this._addRightHandToLeftHandRootTransform();
  410. }
  411. break;
  412. case BABYLON.GLTFLoaderCoordinateSystemMode.PASS_THROUGH:
  413. // do nothing
  414. break;
  415. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED:
  416. this._babylonScene.useRightHandedSystem = true;
  417. break;
  418. default:
  419. BABYLON.Tools.Error("Invalid coordinate system mode (" + this._parent.coordinateSystemMode + ")");
  420. break;
  421. }
  422. this._showMeshes();
  423. this._startAnimations();
  424. this._successCallback();
  425. this._renderReadyObservable.notifyObservers(this);
  426. };
  427. GLTFLoader.prototype._onLoaderComplete = function () {
  428. if (this._parent.onComplete) {
  429. this._parent.onComplete();
  430. }
  431. };
  432. GLTFLoader.prototype._onLoaderFirstLODComplete = function () {
  433. if (this._parent.onFirstLODComplete) {
  434. this._parent.onFirstLODComplete();
  435. }
  436. };
  437. GLTFLoader.prototype._loadData = function (data) {
  438. this._gltf = data.json;
  439. var binaryBuffer;
  440. var buffers = this._gltf.buffers;
  441. if (buffers.length > 0 && buffers[0].uri === undefined) {
  442. binaryBuffer = buffers[0];
  443. }
  444. if (data.bin) {
  445. if (binaryBuffer) {
  446. if (binaryBuffer.byteLength != data.bin.byteLength) {
  447. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  448. }
  449. }
  450. else {
  451. BABYLON.Tools.Warn("Unexpected BIN chunk");
  452. }
  453. binaryBuffer.loadedData = data.bin;
  454. }
  455. };
  456. GLTFLoader.prototype._addRightHandToLeftHandRootTransform = function () {
  457. var rootMesh = new BABYLON.Mesh("root", this._babylonScene);
  458. rootMesh.scaling = new BABYLON.Vector3(1, 1, -1);
  459. rootMesh.rotation.y = Math.PI;
  460. var nodes = this._gltf.nodes;
  461. for (var i = 0; i < nodes.length; i++) {
  462. var mesh = nodes[i].babylonMesh;
  463. if (mesh && !mesh.parent) {
  464. mesh.parent = rootMesh;
  465. }
  466. }
  467. };
  468. GLTFLoader.prototype._getMeshes = function () {
  469. var meshes = [];
  470. var nodes = this._gltf.nodes;
  471. if (nodes) {
  472. nodes.forEach(function (node) {
  473. if (node.babylonMesh) {
  474. meshes.push(node.babylonMesh);
  475. }
  476. });
  477. }
  478. return meshes;
  479. };
  480. GLTFLoader.prototype._getSkeletons = function () {
  481. var skeletons = [];
  482. var skins = this._gltf.skins;
  483. if (skins) {
  484. skins.forEach(function (skin) {
  485. if (skin.babylonSkeleton instanceof BABYLON.Skeleton) {
  486. skeletons.push(skin.babylonSkeleton);
  487. }
  488. });
  489. }
  490. return skeletons;
  491. };
  492. GLTFLoader.prototype._getAnimationTargets = function () {
  493. var targets = [];
  494. var animations = this._gltf.animations;
  495. if (animations) {
  496. animations.forEach(function (animation) {
  497. targets.push.apply(targets, animation.targets);
  498. });
  499. }
  500. return targets;
  501. };
  502. GLTFLoader.prototype._showMeshes = function () {
  503. this._getMeshes().forEach(function (mesh) { return mesh.isVisible = true; });
  504. };
  505. GLTFLoader.prototype._startAnimations = function () {
  506. var _this = this;
  507. this._getAnimationTargets().forEach(function (target) { return _this._babylonScene.beginAnimation(target, 0, Number.MAX_VALUE, true); });
  508. };
  509. GLTFLoader.prototype._loadScene = function (nodeNames) {
  510. var _this = this;
  511. var scene = this._gltf.scenes[this._gltf.scene || 0];
  512. var nodeIndices = scene.nodes;
  513. this._traverseNodes(nodeIndices, function (node, index, parentNode) {
  514. node.index = index;
  515. node.parent = parentNode;
  516. return true;
  517. });
  518. var materials = this._gltf.materials;
  519. if (materials) {
  520. materials.forEach(function (material, index) { return material.index = index; });
  521. }
  522. if (nodeNames) {
  523. if (!(nodeNames instanceof Array)) {
  524. nodeNames = [nodeNames];
  525. }
  526. var filteredNodeIndices = new Array();
  527. this._traverseNodes(nodeIndices, function (node) {
  528. if (nodeNames.indexOf(node.name) === -1) {
  529. return true;
  530. }
  531. filteredNodeIndices.push(node.index);
  532. return false;
  533. });
  534. nodeIndices = filteredNodeIndices;
  535. }
  536. this._traverseNodes(nodeIndices, function (node) { return _this._loadSkin(node); });
  537. this._traverseNodes(nodeIndices, function (node) { return _this._loadMesh(node); });
  538. };
  539. GLTFLoader.prototype._loadSkin = function (node) {
  540. var _this = this;
  541. if (node.skin !== undefined) {
  542. var skin = this._gltf.skins[node.skin];
  543. var skeletonId = "skeleton" + node.skin;
  544. skin.babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
  545. skin.index = node.skin;
  546. for (var i = 0; i < skin.joints.length; i++) {
  547. this._createBone(this._gltf.nodes[skin.joints[i]], skin);
  548. }
  549. if (skin.skeleton === undefined) {
  550. // TODO: handle when skeleton is not defined
  551. throw new Error("Not implemented");
  552. }
  553. if (skin.inverseBindMatrices === undefined) {
  554. // TODO: handle when inverse bind matrices are not defined
  555. throw new Error("Not implemented");
  556. }
  557. var accessor = this._gltf.accessors[skin.inverseBindMatrices];
  558. this._loadAccessorAsync(accessor, function (data) {
  559. _this._traverseNode(skin.skeleton, function (node, index, parent) { return _this._updateBone(node, parent, skin, data); });
  560. });
  561. }
  562. return true;
  563. };
  564. GLTFLoader.prototype._updateBone = function (node, parentNode, skin, inverseBindMatrixData) {
  565. var jointIndex = skin.joints.indexOf(node.index);
  566. if (jointIndex === -1) {
  567. this._createBone(node, skin);
  568. }
  569. var babylonBone = node.babylonSkinToBones[skin.index];
  570. // TODO: explain the math
  571. var matrix = jointIndex === -1 ? BABYLON.Matrix.Identity() : BABYLON.Matrix.FromArray(inverseBindMatrixData, jointIndex * 16);
  572. matrix.invertToRef(matrix);
  573. if (parentNode) {
  574. babylonBone.setParent(parentNode.babylonSkinToBones[skin.index], false);
  575. matrix.multiplyToRef(babylonBone.getParent().getInvertedAbsoluteTransform(), matrix);
  576. }
  577. babylonBone.updateMatrix(matrix);
  578. return true;
  579. };
  580. GLTFLoader.prototype._createBone = function (node, skin) {
  581. var babylonBone = new BABYLON.Bone(node.name || "bone" + node.index, skin.babylonSkeleton);
  582. node.babylonSkinToBones = node.babylonSkinToBones || {};
  583. node.babylonSkinToBones[skin.index] = babylonBone;
  584. node.babylonAnimationTargets = node.babylonAnimationTargets || [];
  585. node.babylonAnimationTargets.push(babylonBone);
  586. return babylonBone;
  587. };
  588. GLTFLoader.prototype._loadMesh = function (node) {
  589. var babylonMesh = new BABYLON.Mesh(node.name || "mesh" + node.index, this._babylonScene);
  590. babylonMesh.isVisible = false;
  591. this._loadTransform(node, babylonMesh);
  592. if (node.mesh !== undefined) {
  593. var mesh = this._gltf.meshes[node.mesh];
  594. this._loadMeshData(node, mesh, babylonMesh);
  595. }
  596. babylonMesh.parent = node.parent ? node.parent.babylonMesh : null;
  597. node.babylonMesh = babylonMesh;
  598. node.babylonAnimationTargets = node.babylonAnimationTargets || [];
  599. node.babylonAnimationTargets.push(node.babylonMesh);
  600. if (node.skin !== undefined) {
  601. var skin = this._gltf.skins[node.skin];
  602. babylonMesh.skeleton = skin.babylonSkeleton;
  603. }
  604. if (node.camera !== undefined) {
  605. // TODO: handle cameras
  606. }
  607. return true;
  608. };
  609. GLTFLoader.prototype._loadMeshData = function (node, mesh, babylonMesh) {
  610. var _this = this;
  611. babylonMesh.name = mesh.name || babylonMesh.name;
  612. var babylonMultiMaterial = new BABYLON.MultiMaterial(babylonMesh.name, this._babylonScene);
  613. babylonMesh.material = babylonMultiMaterial;
  614. var geometry = new BABYLON.Geometry(babylonMesh.name, this._babylonScene, null, false, babylonMesh);
  615. var vertexData = new BABYLON.VertexData();
  616. vertexData.positions = [];
  617. vertexData.indices = [];
  618. var subMeshInfos = [];
  619. var loadedPrimitives = 0;
  620. var totalPrimitives = mesh.primitives.length;
  621. var _loop_1 = function (i) {
  622. var primitive = mesh.primitives[i];
  623. if (primitive.mode && primitive.mode !== GLTF2.EMeshPrimitiveMode.TRIANGLES) {
  624. // TODO: handle other primitive modes
  625. throw new Error("Not implemented");
  626. }
  627. this_1._createMorphTargets(node, mesh, primitive, babylonMesh);
  628. this_1._loadVertexDataAsync(primitive, function (subVertexData) {
  629. _this._loadMorphTargetsData(mesh, primitive, subVertexData, babylonMesh);
  630. subMeshInfos.push({
  631. materialIndex: i,
  632. verticesStart: vertexData.positions.length,
  633. verticesCount: subVertexData.positions.length,
  634. indicesStart: vertexData.indices.length,
  635. indicesCount: subVertexData.indices.length,
  636. loadMaterial: function () {
  637. if (primitive.material === undefined) {
  638. babylonMultiMaterial.subMaterials[i] = _this._getDefaultMaterial();
  639. }
  640. else {
  641. var material = _this._gltf.materials[primitive.material];
  642. _this.loadMaterial(material, function (babylonMaterial, isNew) {
  643. if (isNew && _this._parent.onMaterialLoaded) {
  644. _this._parent.onMaterialLoaded(babylonMaterial);
  645. }
  646. if (_this._parent.onBeforeMaterialReadyAsync) {
  647. _this._parent.onBeforeMaterialReadyAsync(babylonMaterial, babylonMesh, babylonMultiMaterial.subMaterials[i] != null, function () {
  648. babylonMultiMaterial.subMaterials[i] = babylonMaterial;
  649. });
  650. }
  651. else {
  652. babylonMultiMaterial.subMaterials[i] = babylonMaterial;
  653. }
  654. });
  655. }
  656. }
  657. });
  658. vertexData.merge(subVertexData);
  659. if (++loadedPrimitives === totalPrimitives) {
  660. geometry.setAllVerticesData(vertexData, false);
  661. subMeshInfos.forEach(function (info) { return info.loadMaterial(); });
  662. // TODO: optimize this so that sub meshes can be created without being overwritten after setting vertex data.
  663. // Sub meshes must be cleared and created after setting vertex data because of mesh._createGlobalSubMesh.
  664. babylonMesh.subMeshes = [];
  665. subMeshInfos.forEach(function (info) { return new BABYLON.SubMesh(info.materialIndex, info.verticesStart, info.verticesCount, info.indicesStart, info.indicesCount, babylonMesh); });
  666. }
  667. });
  668. };
  669. var this_1 = this;
  670. for (var i = 0; i < totalPrimitives; i++) {
  671. _loop_1(i);
  672. }
  673. };
  674. GLTFLoader.prototype._loadVertexDataAsync = function (primitive, onSuccess) {
  675. var _this = this;
  676. var attributes = primitive.attributes;
  677. if (!attributes) {
  678. this._onError("Primitive has no attributes");
  679. return;
  680. }
  681. var vertexData = new BABYLON.VertexData();
  682. var loadedAttributes = 0;
  683. var totalAttributes = Object.keys(attributes).length;
  684. var _loop_2 = function (semantic) {
  685. accessor = this_2._gltf.accessors[attributes[semantic]];
  686. this_2._loadAccessorAsync(accessor, function (data) {
  687. switch (semantic) {
  688. case "NORMAL":
  689. vertexData.normals = data;
  690. break;
  691. case "POSITION":
  692. vertexData.positions = data;
  693. break;
  694. case "TANGENT":
  695. vertexData.tangents = data;
  696. break;
  697. case "TEXCOORD_0":
  698. vertexData.uvs = data;
  699. break;
  700. case "TEXCOORD_1":
  701. vertexData.uvs2 = data;
  702. break;
  703. case "JOINTS_0":
  704. vertexData.matricesIndices = new Float32Array(Array.prototype.slice.apply(data));
  705. break;
  706. case "WEIGHTS_0":
  707. vertexData.matricesWeights = data;
  708. break;
  709. case "COLOR_0":
  710. vertexData.colors = data;
  711. break;
  712. default:
  713. BABYLON.Tools.Warn("Ignoring unrecognized semantic '" + semantic + "'");
  714. break;
  715. }
  716. if (++loadedAttributes === totalAttributes) {
  717. var indicesAccessor = _this._gltf.accessors[primitive.indices];
  718. if (indicesAccessor) {
  719. _this._loadAccessorAsync(indicesAccessor, function (data) {
  720. vertexData.indices = data;
  721. onSuccess(vertexData);
  722. });
  723. }
  724. else {
  725. vertexData.indices = new Uint32Array(vertexData.positions.length / 3);
  726. vertexData.indices.forEach(function (v, i) { return vertexData.indices[i] = i; });
  727. onSuccess(vertexData);
  728. }
  729. }
  730. });
  731. };
  732. var this_2 = this, accessor;
  733. for (var semantic in attributes) {
  734. _loop_2(semantic);
  735. }
  736. };
  737. GLTFLoader.prototype._createMorphTargets = function (node, mesh, primitive, babylonMesh) {
  738. var targets = primitive.targets;
  739. if (!targets) {
  740. return;
  741. }
  742. if (!babylonMesh.morphTargetManager) {
  743. babylonMesh.morphTargetManager = new BABYLON.MorphTargetManager();
  744. }
  745. for (var index = 0; index < targets.length; index++) {
  746. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  747. babylonMesh.morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  748. }
  749. };
  750. GLTFLoader.prototype._loadMorphTargetsData = function (mesh, primitive, vertexData, babylonMesh) {
  751. var targets = primitive.targets;
  752. if (!targets) {
  753. return;
  754. }
  755. var _loop_3 = function () {
  756. var babylonMorphTarget = babylonMesh.morphTargetManager.getTarget(index);
  757. attributes = targets[index];
  758. var _loop_4 = function (semantic) {
  759. accessor = this_3._gltf.accessors[attributes[semantic]];
  760. this_3._loadAccessorAsync(accessor, function (data) {
  761. if (accessor.name) {
  762. babylonMorphTarget.name = accessor.name;
  763. }
  764. // glTF stores morph target information as deltas while babylon.js expects the final data.
  765. // As a result we have to add the original data to the delta to calculate the final data.
  766. var values = data;
  767. switch (semantic) {
  768. case "NORMAL":
  769. GLTF2.GLTFUtils.ForEach(values, function (v, i) { return values[i] += vertexData.normals[i]; });
  770. babylonMorphTarget.setNormals(values);
  771. break;
  772. case "POSITION":
  773. GLTF2.GLTFUtils.ForEach(values, function (v, i) { return values[i] += vertexData.positions[i]; });
  774. babylonMorphTarget.setPositions(values);
  775. break;
  776. case "TANGENT":
  777. // Tangent data for morph targets is stored as xyz delta.
  778. // The vertexData.tangent is stored as xyzw.
  779. // So we need to skip every fourth vertexData.tangent.
  780. for (var i = 0, j = 0; i < values.length; i++, j++) {
  781. values[i] += vertexData.tangents[j];
  782. if ((i + 1) % 3 == 0) {
  783. j++;
  784. }
  785. }
  786. babylonMorphTarget.setTangents(values);
  787. break;
  788. default:
  789. BABYLON.Tools.Warn("Ignoring unrecognized semantic '" + semantic + "'");
  790. break;
  791. }
  792. });
  793. };
  794. for (var semantic in attributes) {
  795. _loop_4(semantic);
  796. }
  797. };
  798. var this_3 = this, attributes, accessor;
  799. for (var index = 0; index < targets.length; index++) {
  800. _loop_3();
  801. }
  802. };
  803. GLTFLoader.prototype._loadTransform = function (node, babylonMesh) {
  804. var position = BABYLON.Vector3.Zero();
  805. var rotation = BABYLON.Quaternion.Identity();
  806. var scaling = BABYLON.Vector3.One();
  807. if (node.matrix) {
  808. var mat = BABYLON.Matrix.FromArray(node.matrix);
  809. mat.decompose(scaling, rotation, position);
  810. }
  811. else {
  812. if (node.translation)
  813. position = BABYLON.Vector3.FromArray(node.translation);
  814. if (node.rotation)
  815. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  816. if (node.scale)
  817. scaling = BABYLON.Vector3.FromArray(node.scale);
  818. }
  819. babylonMesh.position = position;
  820. babylonMesh.rotationQuaternion = rotation;
  821. babylonMesh.scaling = scaling;
  822. };
  823. GLTFLoader.prototype._traverseNodes = function (indices, action, parentNode) {
  824. if (parentNode === void 0) { parentNode = null; }
  825. for (var i = 0; i < indices.length; i++) {
  826. this._traverseNode(indices[i], action, parentNode);
  827. }
  828. };
  829. GLTFLoader.prototype._traverseNode = function (index, action, parentNode) {
  830. if (parentNode === void 0) { parentNode = null; }
  831. var node = this._gltf.nodes[index];
  832. if (!action(node, index, parentNode)) {
  833. return;
  834. }
  835. if (node.children) {
  836. for (var i = 0; i < node.children.length; i++) {
  837. this._traverseNode(node.children[i], action, node);
  838. }
  839. }
  840. };
  841. GLTFLoader.prototype._loadAnimations = function () {
  842. var animations = this._gltf.animations;
  843. if (!animations || animations.length === 0) {
  844. return;
  845. }
  846. for (var animationIndex = 0; animationIndex < animations.length; animationIndex++) {
  847. var animation = animations[animationIndex];
  848. for (var channelIndex = 0; channelIndex < animation.channels.length; channelIndex++) {
  849. this._loadAnimationChannel(animation, animationIndex, channelIndex);
  850. }
  851. }
  852. };
  853. GLTFLoader.prototype._loadAnimationChannel = function (animation, animationIndex, channelIndex) {
  854. var channel = animation.channels[channelIndex];
  855. var samplerIndex = channel.sampler;
  856. var sampler = animation.samplers[samplerIndex];
  857. var targetNode = this._gltf.nodes[channel.target.node];
  858. if (!targetNode) {
  859. BABYLON.Tools.Warn("Animation channel target node (" + channel.target.node + ") does not exist");
  860. return;
  861. }
  862. var targetPath = {
  863. "translation": "position",
  864. "rotation": "rotationQuaternion",
  865. "scale": "scaling",
  866. "weights": "influence"
  867. }[channel.target.path];
  868. if (!targetPath) {
  869. BABYLON.Tools.Warn("Animation channel target path '" + channel.target.path + "' is not valid");
  870. return;
  871. }
  872. var animationType = {
  873. "position": BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
  874. "rotationQuaternion": BABYLON.Animation.ANIMATIONTYPE_QUATERNION,
  875. "scaling": BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
  876. "influence": BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  877. }[targetPath];
  878. var inputData;
  879. var outputData;
  880. var checkSuccess = function () {
  881. if (!inputData || !outputData) {
  882. return;
  883. }
  884. var outputBufferOffset = 0;
  885. var getNextOutputValue = {
  886. "position": function () {
  887. var value = BABYLON.Vector3.FromArray(outputData, outputBufferOffset);
  888. outputBufferOffset += 3;
  889. return value;
  890. },
  891. "rotationQuaternion": function () {
  892. var value = BABYLON.Quaternion.FromArray(outputData, outputBufferOffset);
  893. outputBufferOffset += 4;
  894. return value;
  895. },
  896. "scaling": function () {
  897. var value = BABYLON.Vector3.FromArray(outputData, outputBufferOffset);
  898. outputBufferOffset += 3;
  899. return value;
  900. },
  901. "influence": function () {
  902. var numTargets = targetNode.babylonMesh.morphTargetManager.numTargets;
  903. var value = new Array(numTargets);
  904. for (var i = 0; i < numTargets; i++) {
  905. value[i] = outputData[outputBufferOffset++];
  906. }
  907. return value;
  908. },
  909. }[targetPath];
  910. var getNextKey = {
  911. "LINEAR": function (frameIndex) { return ({
  912. frame: inputData[frameIndex],
  913. value: getNextOutputValue()
  914. }); },
  915. "CUBICSPLINE": function (frameIndex) { return ({
  916. frame: inputData[frameIndex],
  917. inTangent: getNextOutputValue(),
  918. value: getNextOutputValue(),
  919. outTangent: getNextOutputValue()
  920. }); },
  921. }[sampler.interpolation];
  922. var keys = new Array(inputData.length);
  923. for (var frameIndex = 0; frameIndex < inputData.length; frameIndex++) {
  924. keys[frameIndex] = getNextKey(frameIndex);
  925. }
  926. animation.targets = animation.targets || [];
  927. if (targetPath === "influence") {
  928. var morphTargetManager = targetNode.babylonMesh.morphTargetManager;
  929. for (var targetIndex = 0; targetIndex < morphTargetManager.numTargets; targetIndex++) {
  930. var morphTarget = morphTargetManager.getTarget(targetIndex);
  931. var animationName = (animation.name || "anim" + animationIndex) + "_" + targetIndex;
  932. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  933. babylonAnimation.setKeys(keys.map(function (key) { return ({
  934. frame: key.frame,
  935. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  936. value: key.value[targetIndex],
  937. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  938. }); }));
  939. morphTarget.animations.push(babylonAnimation);
  940. animation.targets.push(morphTarget);
  941. }
  942. }
  943. else {
  944. var animationName = animation.name || "anim" + animationIndex;
  945. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  946. babylonAnimation.setKeys(keys);
  947. for (var i = 0; i < targetNode.babylonAnimationTargets.length; i++) {
  948. var target = targetNode.babylonAnimationTargets[i];
  949. target.animations.push(babylonAnimation.clone());
  950. animation.targets.push(target);
  951. }
  952. }
  953. };
  954. this._loadAccessorAsync(this._gltf.accessors[sampler.input], function (data) {
  955. inputData = data;
  956. checkSuccess();
  957. });
  958. this._loadAccessorAsync(this._gltf.accessors[sampler.output], function (data) {
  959. outputData = data;
  960. checkSuccess();
  961. });
  962. };
  963. GLTFLoader.prototype._loadBufferAsync = function (index, onSuccess) {
  964. var _this = this;
  965. var buffer = this._gltf.buffers[index];
  966. this.addPendingData(buffer);
  967. if (buffer.loadedData) {
  968. setTimeout(function () {
  969. onSuccess(buffer.loadedData);
  970. _this.removePendingData(buffer);
  971. });
  972. }
  973. else if (GLTF2.GLTFUtils.IsBase64(buffer.uri)) {
  974. var data = GLTF2.GLTFUtils.DecodeBase64(buffer.uri);
  975. buffer.loadedData = new Uint8Array(data);
  976. setTimeout(function () {
  977. onSuccess(buffer.loadedData);
  978. _this.removePendingData(buffer);
  979. });
  980. }
  981. else if (buffer.loadedObservable) {
  982. buffer.loadedObservable.add(function (buffer) {
  983. onSuccess(buffer.loadedData);
  984. _this.removePendingData(buffer);
  985. });
  986. }
  987. else {
  988. buffer.loadedObservable = new BABYLON.Observable();
  989. buffer.loadedObservable.add(function (buffer) {
  990. onSuccess(buffer.loadedData);
  991. _this.removePendingData(buffer);
  992. });
  993. BABYLON.Tools.LoadFile(this._rootUrl + buffer.uri, function (data) {
  994. buffer.loadedData = new Uint8Array(data);
  995. buffer.loadedObservable.notifyObservers(buffer);
  996. buffer.loadedObservable = null;
  997. }, function (event) {
  998. if (!_this._disposed) {
  999. _this._onProgress(event);
  1000. }
  1001. }, this._babylonScene.database, true, function (request) {
  1002. if (!_this._disposed) {
  1003. _this._onError("Failed to load file '" + buffer.uri + "': " + request.status + " " + request.statusText);
  1004. _this.removePendingData(buffer);
  1005. }
  1006. });
  1007. }
  1008. };
  1009. GLTFLoader.prototype._loadBufferViewAsync = function (bufferView, byteOffset, byteLength, componentType, onSuccess) {
  1010. var _this = this;
  1011. byteOffset += (bufferView.byteOffset || 0);
  1012. this._loadBufferAsync(bufferView.buffer, function (bufferData) {
  1013. if (byteOffset + byteLength > bufferData.byteLength) {
  1014. _this._onError("Buffer access is out of range");
  1015. return;
  1016. }
  1017. var buffer = bufferData.buffer;
  1018. byteOffset += bufferData.byteOffset;
  1019. var bufferViewData;
  1020. switch (componentType) {
  1021. case GLTF2.EComponentType.BYTE:
  1022. bufferViewData = new Int8Array(buffer, byteOffset, byteLength);
  1023. break;
  1024. case GLTF2.EComponentType.UNSIGNED_BYTE:
  1025. bufferViewData = new Uint8Array(buffer, byteOffset, byteLength);
  1026. break;
  1027. case GLTF2.EComponentType.SHORT:
  1028. bufferViewData = new Int16Array(buffer, byteOffset, byteLength);
  1029. break;
  1030. case GLTF2.EComponentType.UNSIGNED_SHORT:
  1031. bufferViewData = new Uint16Array(buffer, byteOffset, byteLength);
  1032. break;
  1033. case GLTF2.EComponentType.UNSIGNED_INT:
  1034. bufferViewData = new Uint32Array(buffer, byteOffset, byteLength);
  1035. break;
  1036. case GLTF2.EComponentType.FLOAT:
  1037. bufferViewData = new Float32Array(buffer, byteOffset, byteLength);
  1038. break;
  1039. default:
  1040. _this._onError("Invalid component type (" + componentType + ")");
  1041. return;
  1042. }
  1043. onSuccess(bufferViewData);
  1044. });
  1045. };
  1046. GLTFLoader.prototype._loadAccessorAsync = function (accessor, onSuccess) {
  1047. var bufferView = this._gltf.bufferViews[accessor.bufferView];
  1048. var byteOffset = accessor.byteOffset || 0;
  1049. var byteLength = accessor.count * this._getByteStrideFromType(accessor);
  1050. this._loadBufferViewAsync(bufferView, byteOffset, byteLength, accessor.componentType, onSuccess);
  1051. };
  1052. GLTFLoader.prototype._getByteStrideFromType = function (accessor) {
  1053. switch (accessor.type) {
  1054. case "SCALAR": return 1;
  1055. case "VEC2": return 2;
  1056. case "VEC3": return 3;
  1057. case "VEC4": return 4;
  1058. case "MAT2": return 4;
  1059. case "MAT3": return 9;
  1060. case "MAT4": return 16;
  1061. default:
  1062. this._onError("Invalid accessor type (" + accessor.type + ")");
  1063. return 0;
  1064. }
  1065. };
  1066. Object.defineProperty(GLTFLoader.prototype, "blockPendingTracking", {
  1067. set: function (value) {
  1068. this._blockPendingTracking = value;
  1069. },
  1070. enumerable: true,
  1071. configurable: true
  1072. });
  1073. GLTFLoader.prototype.addPendingData = function (data) {
  1074. if (!this._renderReady) {
  1075. this._renderPendingCount++;
  1076. }
  1077. this.addLoaderPendingData(data);
  1078. };
  1079. GLTFLoader.prototype.removePendingData = function (data) {
  1080. if (!this._renderReady) {
  1081. if (--this._renderPendingCount === 0) {
  1082. this._renderReady = true;
  1083. this._onRenderReady();
  1084. }
  1085. }
  1086. this.removeLoaderPendingData(data);
  1087. };
  1088. GLTFLoader.prototype.addLoaderNonBlockingPendingData = function (data) {
  1089. if (!this._nonBlockingData) {
  1090. this._nonBlockingData = new Array();
  1091. }
  1092. this._nonBlockingData.push(data);
  1093. };
  1094. GLTFLoader.prototype.addLoaderPendingData = function (data) {
  1095. if (this._blockPendingTracking) {
  1096. this.addLoaderNonBlockingPendingData(data);
  1097. return;
  1098. }
  1099. this._loaderPendingCount++;
  1100. };
  1101. GLTFLoader.prototype.removeLoaderPendingData = function (data) {
  1102. var indexInPending = this._nonBlockingData ? this._nonBlockingData.indexOf(data) : -1;
  1103. if (indexInPending !== -1) {
  1104. this._nonBlockingData.splice(indexInPending, 1);
  1105. }
  1106. else if (--this._loaderPendingCount === 0) {
  1107. this._onLoaderFirstLODComplete();
  1108. }
  1109. if ((!this._nonBlockingData || this._nonBlockingData.length === 0) && this._loaderPendingCount === 0) {
  1110. this._onLoaderComplete();
  1111. this.dispose();
  1112. }
  1113. };
  1114. GLTFLoader.prototype._getDefaultMaterial = function () {
  1115. if (!this._defaultMaterial) {
  1116. var id = "__gltf_default";
  1117. var material = this._babylonScene.getMaterialByName(id);
  1118. if (!material) {
  1119. material = new BABYLON.PBRMaterial(id, this._babylonScene);
  1120. material.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1121. material.metallic = 1;
  1122. material.roughness = 1;
  1123. }
  1124. this._defaultMaterial = material;
  1125. }
  1126. return this._defaultMaterial;
  1127. };
  1128. GLTFLoader.prototype._loadMaterialMetallicRoughnessProperties = function (material) {
  1129. var babylonMaterial = material.babylonMaterial;
  1130. // Ensure metallic workflow
  1131. babylonMaterial.metallic = 1;
  1132. babylonMaterial.roughness = 1;
  1133. var properties = material.pbrMetallicRoughness;
  1134. if (!properties) {
  1135. return;
  1136. }
  1137. babylonMaterial.albedoColor = properties.baseColorFactor ? BABYLON.Color3.FromArray(properties.baseColorFactor) : new BABYLON.Color3(1, 1, 1);
  1138. babylonMaterial.metallic = properties.metallicFactor === undefined ? 1 : properties.metallicFactor;
  1139. babylonMaterial.roughness = properties.roughnessFactor === undefined ? 1 : properties.roughnessFactor;
  1140. if (properties.baseColorTexture) {
  1141. babylonMaterial.albedoTexture = this.loadTexture(properties.baseColorTexture);
  1142. }
  1143. if (properties.metallicRoughnessTexture) {
  1144. babylonMaterial.metallicTexture = this.loadTexture(properties.metallicRoughnessTexture);
  1145. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  1146. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  1147. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  1148. }
  1149. this.loadMaterialAlphaProperties(material, properties.baseColorFactor);
  1150. };
  1151. GLTFLoader.prototype.loadMaterial = function (material, assign) {
  1152. if (material.babylonMaterial) {
  1153. assign(material.babylonMaterial, false);
  1154. return;
  1155. }
  1156. if (GLTF2.GLTFLoaderExtension.LoadMaterial(this, material, assign)) {
  1157. return;
  1158. }
  1159. this.createPbrMaterial(material);
  1160. this.loadMaterialBaseProperties(material);
  1161. this._loadMaterialMetallicRoughnessProperties(material);
  1162. assign(material.babylonMaterial, true);
  1163. };
  1164. GLTFLoader.prototype.createPbrMaterial = function (material) {
  1165. var babylonMaterial = new BABYLON.PBRMaterial(material.name || "mat" + material.index, this._babylonScene);
  1166. babylonMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1167. material.babylonMaterial = babylonMaterial;
  1168. };
  1169. GLTFLoader.prototype.loadMaterialBaseProperties = function (material) {
  1170. var babylonMaterial = material.babylonMaterial;
  1171. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  1172. if (material.doubleSided) {
  1173. babylonMaterial.backFaceCulling = false;
  1174. babylonMaterial.twoSidedLighting = true;
  1175. }
  1176. if (material.normalTexture) {
  1177. babylonMaterial.bumpTexture = this.loadTexture(material.normalTexture);
  1178. babylonMaterial.invertNormalMapX = true;
  1179. babylonMaterial.invertNormalMapY = true;
  1180. if (material.normalTexture.scale !== undefined) {
  1181. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  1182. }
  1183. }
  1184. if (material.occlusionTexture) {
  1185. babylonMaterial.ambientTexture = this.loadTexture(material.occlusionTexture);
  1186. babylonMaterial.useAmbientInGrayScale = true;
  1187. if (material.occlusionTexture.strength !== undefined) {
  1188. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  1189. }
  1190. }
  1191. if (material.emissiveTexture) {
  1192. babylonMaterial.emissiveTexture = this.loadTexture(material.emissiveTexture);
  1193. }
  1194. };
  1195. GLTFLoader.prototype.loadMaterialAlphaProperties = function (material, colorFactor) {
  1196. var babylonMaterial = material.babylonMaterial;
  1197. var alphaMode = material.alphaMode || "OPAQUE";
  1198. switch (alphaMode) {
  1199. case "OPAQUE":
  1200. // default is opaque
  1201. break;
  1202. case "MASK":
  1203. case "BLEND":
  1204. if (colorFactor) {
  1205. babylonMaterial.alpha = colorFactor[3];
  1206. }
  1207. if (babylonMaterial.albedoTexture) {
  1208. babylonMaterial.albedoTexture.hasAlpha = true;
  1209. babylonMaterial.useAlphaFromAlbedoTexture = (alphaMode === "BLEND");
  1210. }
  1211. break;
  1212. default:
  1213. BABYLON.Tools.Warn("Invalid alpha mode '" + material.alphaMode + "'");
  1214. break;
  1215. }
  1216. };
  1217. GLTFLoader.prototype.loadTexture = function (textureInfo) {
  1218. var _this = this;
  1219. var texture = this._gltf.textures[textureInfo.index];
  1220. var texCoord = textureInfo.texCoord || 0;
  1221. if (!texture || texture.source === undefined) {
  1222. return null;
  1223. }
  1224. // check the cache first
  1225. var babylonTexture;
  1226. if (texture.babylonTextures) {
  1227. babylonTexture = texture.babylonTextures[texCoord];
  1228. if (!babylonTexture) {
  1229. for (var i = 0; i < texture.babylonTextures.length; i++) {
  1230. babylonTexture = texture.babylonTextures[i];
  1231. if (babylonTexture) {
  1232. babylonTexture = babylonTexture.clone();
  1233. babylonTexture.coordinatesIndex = texCoord;
  1234. break;
  1235. }
  1236. }
  1237. }
  1238. return babylonTexture;
  1239. }
  1240. var source = this._gltf.images[texture.source];
  1241. var sampler = (texture.sampler === undefined ? {} : this._gltf.samplers[texture.sampler]);
  1242. var noMipMaps = (sampler.minFilter === GLTF2.ETextureMinFilter.NEAREST || sampler.minFilter === GLTF2.ETextureMinFilter.LINEAR);
  1243. var samplingMode = GLTF2.GLTFUtils.GetTextureSamplingMode(sampler.magFilter, sampler.minFilter);
  1244. this.addPendingData(texture);
  1245. babylonTexture = new BABYLON.Texture(null, this._babylonScene, noMipMaps, false, samplingMode, function () {
  1246. if (!_this._disposed) {
  1247. _this.removePendingData(texture);
  1248. }
  1249. }, function () {
  1250. if (!_this._disposed) {
  1251. _this._onError("Failed to load texture '" + source.uri + "'");
  1252. _this.removePendingData(texture);
  1253. }
  1254. });
  1255. var setTextureData = function (data) {
  1256. var url = URL.createObjectURL(new Blob([data], { type: source.mimeType }));
  1257. _this._objectURLs.push(url);
  1258. babylonTexture.updateURL(url);
  1259. };
  1260. if (!source.uri) {
  1261. var bufferView = this._gltf.bufferViews[source.bufferView];
  1262. this._loadBufferViewAsync(bufferView, 0, bufferView.byteLength, GLTF2.EComponentType.UNSIGNED_BYTE, setTextureData);
  1263. }
  1264. else if (GLTF2.GLTFUtils.IsBase64(source.uri)) {
  1265. setTextureData(new Uint8Array(GLTF2.GLTFUtils.DecodeBase64(source.uri)));
  1266. }
  1267. else {
  1268. BABYLON.Tools.LoadFile(this._rootUrl + source.uri, setTextureData, function (event) {
  1269. if (!_this._disposed) {
  1270. _this._onProgress(event);
  1271. }
  1272. }, this._babylonScene.database, true, function (request) {
  1273. _this._onError("Failed to load file '" + source.uri + "': " + request.status + " " + request.statusText);
  1274. });
  1275. }
  1276. babylonTexture.coordinatesIndex = texCoord;
  1277. babylonTexture.wrapU = GLTF2.GLTFUtils.GetTextureWrapMode(sampler.wrapS);
  1278. babylonTexture.wrapV = GLTF2.GLTFUtils.GetTextureWrapMode(sampler.wrapT);
  1279. babylonTexture.name = texture.name || "texture" + textureInfo.index;
  1280. // Cache the texture
  1281. texture.babylonTextures = texture.babylonTextures || [];
  1282. texture.babylonTextures[texCoord] = babylonTexture;
  1283. if (this._parent.onTextureLoaded) {
  1284. this._parent.onTextureLoaded(babylonTexture);
  1285. }
  1286. return babylonTexture;
  1287. };
  1288. return GLTFLoader;
  1289. }());
  1290. GLTFLoader.Extensions = {};
  1291. GLTF2.GLTFLoader = GLTFLoader;
  1292. BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = function (parent) { return new GLTFLoader(parent); };
  1293. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1294. })(BABYLON || (BABYLON = {}));
  1295. //# sourceMappingURL=babylon.glTFLoader.js.map
  1296. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  1297. var BABYLON;
  1298. (function (BABYLON) {
  1299. var GLTF2;
  1300. (function (GLTF2) {
  1301. /**
  1302. * Utils functions for GLTF
  1303. */
  1304. var GLTFUtils = (function () {
  1305. function GLTFUtils() {
  1306. }
  1307. /**
  1308. * If the uri is a base64 string
  1309. * @param uri: the uri to test
  1310. */
  1311. GLTFUtils.IsBase64 = function (uri) {
  1312. return uri.length < 5 ? false : uri.substr(0, 5) === "data:";
  1313. };
  1314. /**
  1315. * Decode the base64 uri
  1316. * @param uri: the uri to decode
  1317. */
  1318. GLTFUtils.DecodeBase64 = function (uri) {
  1319. var decodedString = atob(uri.split(",")[1]);
  1320. var bufferLength = decodedString.length;
  1321. var bufferView = new Uint8Array(new ArrayBuffer(bufferLength));
  1322. for (var i = 0; i < bufferLength; i++) {
  1323. bufferView[i] = decodedString.charCodeAt(i);
  1324. }
  1325. return bufferView.buffer;
  1326. };
  1327. GLTFUtils.ForEach = function (view, func) {
  1328. for (var index = 0; index < view.length; index++) {
  1329. func(view[index], index);
  1330. }
  1331. };
  1332. GLTFUtils.GetTextureWrapMode = function (mode) {
  1333. // Set defaults if undefined
  1334. mode = mode === undefined ? GLTF2.ETextureWrapMode.REPEAT : mode;
  1335. switch (mode) {
  1336. case GLTF2.ETextureWrapMode.CLAMP_TO_EDGE: BABYLON.Texture.CLAMP_ADDRESSMODE;
  1337. case GLTF2.ETextureWrapMode.MIRRORED_REPEAT: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  1338. case GLTF2.ETextureWrapMode.REPEAT: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1339. default:
  1340. BABYLON.Tools.Warn("Invalid texture wrap mode (" + mode + ")");
  1341. return BABYLON.Texture.WRAP_ADDRESSMODE;
  1342. }
  1343. };
  1344. GLTFUtils.GetTextureSamplingMode = function (magFilter, minFilter) {
  1345. // Set defaults if undefined
  1346. magFilter = magFilter === undefined ? GLTF2.ETextureMagFilter.LINEAR : magFilter;
  1347. minFilter = minFilter === undefined ? GLTF2.ETextureMinFilter.LINEAR_MIPMAP_NEAREST : minFilter;
  1348. if (magFilter === GLTF2.ETextureMagFilter.LINEAR) {
  1349. switch (minFilter) {
  1350. case GLTF2.ETextureMinFilter.NEAREST: return BABYLON.Texture.LINEAR_NEAREST;
  1351. case GLTF2.ETextureMinFilter.LINEAR: return BABYLON.Texture.LINEAR_LINEAR;
  1352. case GLTF2.ETextureMinFilter.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.LINEAR_NEAREST_MIPNEAREST;
  1353. case GLTF2.ETextureMinFilter.LINEAR_MIPMAP_NEAREST: return BABYLON.Texture.LINEAR_LINEAR_MIPNEAREST;
  1354. case GLTF2.ETextureMinFilter.NEAREST_MIPMAP_LINEAR: return BABYLON.Texture.LINEAR_NEAREST_MIPLINEAR;
  1355. case GLTF2.ETextureMinFilter.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  1356. default:
  1357. BABYLON.Tools.Warn("Invalid texture minification filter (" + minFilter + ")");
  1358. return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  1359. }
  1360. }
  1361. else {
  1362. if (magFilter !== GLTF2.ETextureMagFilter.NEAREST) {
  1363. BABYLON.Tools.Warn("Invalid texture magnification filter (" + magFilter + ")");
  1364. }
  1365. switch (minFilter) {
  1366. case GLTF2.ETextureMinFilter.NEAREST: return BABYLON.Texture.NEAREST_NEAREST;
  1367. case GLTF2.ETextureMinFilter.LINEAR: return BABYLON.Texture.NEAREST_LINEAR;
  1368. case GLTF2.ETextureMinFilter.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  1369. case GLTF2.ETextureMinFilter.LINEAR_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_LINEAR_MIPNEAREST;
  1370. case GLTF2.ETextureMinFilter.NEAREST_MIPMAP_LINEAR: return BABYLON.Texture.NEAREST_NEAREST_MIPLINEAR;
  1371. case GLTF2.ETextureMinFilter.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.NEAREST_LINEAR_MIPLINEAR;
  1372. default:
  1373. BABYLON.Tools.Warn("Invalid texture minification filter (" + minFilter + ")");
  1374. return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  1375. }
  1376. }
  1377. };
  1378. /**
  1379. * Decodes a buffer view into a string
  1380. * @param view: the buffer view
  1381. */
  1382. GLTFUtils.DecodeBufferToText = function (view) {
  1383. var result = "";
  1384. var length = view.byteLength;
  1385. for (var i = 0; i < length; ++i) {
  1386. result += String.fromCharCode(view[i]);
  1387. }
  1388. return result;
  1389. };
  1390. return GLTFUtils;
  1391. }());
  1392. GLTF2.GLTFUtils = GLTFUtils;
  1393. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1394. })(BABYLON || (BABYLON = {}));
  1395. //# sourceMappingURL=babylon.glTFLoaderUtils.js.map
  1396. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  1397. var BABYLON;
  1398. (function (BABYLON) {
  1399. var GLTF2;
  1400. (function (GLTF2) {
  1401. var GLTFLoaderExtension = (function () {
  1402. function GLTFLoaderExtension() {
  1403. this.enabled = true;
  1404. }
  1405. GLTFLoaderExtension.prototype.loadMaterial = function (loader, material, assign) { return false; };
  1406. GLTFLoaderExtension.LoadMaterial = function (loader, material, assign) {
  1407. return this._ApplyExtensions(function (extension) { return extension.loadMaterial(loader, material, assign); });
  1408. };
  1409. GLTFLoaderExtension._ApplyExtensions = function (action) {
  1410. var extensions = GLTFLoaderExtension._Extensions;
  1411. if (!extensions) {
  1412. return;
  1413. }
  1414. for (var i = 0; i < extensions.length; i++) {
  1415. var extension = extensions[i];
  1416. if (extension.enabled && action(extension)) {
  1417. return true;
  1418. }
  1419. }
  1420. return false;
  1421. };
  1422. return GLTFLoaderExtension;
  1423. }());
  1424. //
  1425. // Utilities
  1426. //
  1427. GLTFLoaderExtension._Extensions = [];
  1428. GLTF2.GLTFLoaderExtension = GLTFLoaderExtension;
  1429. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1430. })(BABYLON || (BABYLON = {}));
  1431. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  1432. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  1433. var __extends = (this && this.__extends) || (function () {
  1434. var extendStatics = Object.setPrototypeOf ||
  1435. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  1436. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  1437. return function (d, b) {
  1438. extendStatics(d, b);
  1439. function __() { this.constructor = d; }
  1440. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  1441. };
  1442. })();
  1443. var BABYLON;
  1444. (function (BABYLON) {
  1445. var GLTF2;
  1446. (function (GLTF2) {
  1447. var Extensions;
  1448. (function (Extensions) {
  1449. var MSFTLOD = (function (_super) {
  1450. __extends(MSFTLOD, _super);
  1451. function MSFTLOD() {
  1452. return _super !== null && _super.apply(this, arguments) || this;
  1453. }
  1454. Object.defineProperty(MSFTLOD.prototype, "name", {
  1455. get: function () {
  1456. return "MSFT_lod";
  1457. },
  1458. enumerable: true,
  1459. configurable: true
  1460. });
  1461. MSFTLOD.prototype.loadMaterial = function (loader, material, assign) {
  1462. if (!material.extensions) {
  1463. return false;
  1464. }
  1465. var properties = material.extensions[this.name];
  1466. if (!properties) {
  1467. return false;
  1468. }
  1469. // Clear out the extension so that it won't get loaded again.
  1470. material.extensions[this.name] = undefined;
  1471. // Tell the loader not to clear its state until the highest LOD is loaded.
  1472. var materialLODs = [material.index].concat(properties.ids);
  1473. loader.addLoaderPendingData(material);
  1474. for (var index = 0; index < materialLODs.length; index++) {
  1475. loader.addLoaderNonBlockingPendingData(index);
  1476. }
  1477. // Start with the lowest quality LOD.
  1478. this.loadMaterialLOD(loader, material, materialLODs, materialLODs.length - 1, assign);
  1479. return true;
  1480. };
  1481. MSFTLOD.prototype.loadMaterialLOD = function (loader, material, materialLODs, lod, assign) {
  1482. var _this = this;
  1483. var materialLOD = loader.gltf.materials[materialLODs[lod]];
  1484. if (lod !== materialLODs.length - 1) {
  1485. loader.blockPendingTracking = true;
  1486. }
  1487. loader.loadMaterial(materialLOD, function (babylonMaterial, isNew) {
  1488. assign(babylonMaterial, isNew);
  1489. loader.removeLoaderPendingData(lod);
  1490. // Loading is considered complete if this is the lowest quality LOD.
  1491. if (lod === materialLODs.length - 1) {
  1492. loader.removeLoaderPendingData(material);
  1493. }
  1494. if (lod === 0) {
  1495. loader.blockPendingTracking = false;
  1496. return;
  1497. }
  1498. // Load the next LOD when the loader is ready to render and
  1499. // all active material textures of the current LOD are loaded.
  1500. loader.executeWhenRenderReady(function () {
  1501. BABYLON.BaseTexture.WhenAllReady(babylonMaterial.getActiveTextures(), function () {
  1502. setTimeout(function () {
  1503. _this.loadMaterialLOD(loader, material, materialLODs, lod - 1, assign);
  1504. }, MSFTLOD.MinimalLODDelay);
  1505. });
  1506. });
  1507. });
  1508. };
  1509. return MSFTLOD;
  1510. }(GLTF2.GLTFLoaderExtension));
  1511. /**
  1512. * Specify the minimal delay between LODs in ms (default = 250)
  1513. */
  1514. MSFTLOD.MinimalLODDelay = 250;
  1515. Extensions.MSFTLOD = MSFTLOD;
  1516. GLTF2.GLTFLoader.RegisterExtension(new MSFTLOD());
  1517. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  1518. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1519. })(BABYLON || (BABYLON = {}));
  1520. //# sourceMappingURL=MSFT_lod.js.map
  1521. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  1522. var __extends = (this && this.__extends) || (function () {
  1523. var extendStatics = Object.setPrototypeOf ||
  1524. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  1525. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  1526. return function (d, b) {
  1527. extendStatics(d, b);
  1528. function __() { this.constructor = d; }
  1529. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  1530. };
  1531. })();
  1532. var BABYLON;
  1533. (function (BABYLON) {
  1534. var GLTF2;
  1535. (function (GLTF2) {
  1536. var Extensions;
  1537. (function (Extensions) {
  1538. var KHRMaterialsPbrSpecularGlossiness = (function (_super) {
  1539. __extends(KHRMaterialsPbrSpecularGlossiness, _super);
  1540. function KHRMaterialsPbrSpecularGlossiness() {
  1541. return _super !== null && _super.apply(this, arguments) || this;
  1542. }
  1543. Object.defineProperty(KHRMaterialsPbrSpecularGlossiness.prototype, "name", {
  1544. get: function () {
  1545. return "KHR_materials_pbrSpecularGlossiness";
  1546. },
  1547. enumerable: true,
  1548. configurable: true
  1549. });
  1550. KHRMaterialsPbrSpecularGlossiness.prototype.loadMaterial = function (loader, material, assign) {
  1551. if (!material.extensions) {
  1552. return false;
  1553. }
  1554. var properties = material.extensions[this.name];
  1555. if (!properties) {
  1556. return false;
  1557. }
  1558. loader.createPbrMaterial(material);
  1559. loader.loadMaterialBaseProperties(material);
  1560. this._loadSpecularGlossinessProperties(loader, material, properties);
  1561. assign(material.babylonMaterial, true);
  1562. return true;
  1563. };
  1564. KHRMaterialsPbrSpecularGlossiness.prototype._loadSpecularGlossinessProperties = function (loader, material, properties) {
  1565. var babylonMaterial = material.babylonMaterial;
  1566. babylonMaterial.albedoColor = properties.diffuseFactor ? BABYLON.Color3.FromArray(properties.diffuseFactor) : new BABYLON.Color3(1, 1, 1);
  1567. babylonMaterial.reflectivityColor = properties.specularFactor ? BABYLON.Color3.FromArray(properties.specularFactor) : new BABYLON.Color3(1, 1, 1);
  1568. babylonMaterial.microSurface = properties.glossinessFactor === undefined ? 1 : properties.glossinessFactor;
  1569. if (properties.diffuseTexture) {
  1570. babylonMaterial.albedoTexture = loader.loadTexture(properties.diffuseTexture);
  1571. loader.loadMaterialAlphaProperties(material);
  1572. }
  1573. if (properties.specularGlossinessTexture) {
  1574. babylonMaterial.reflectivityTexture = loader.loadTexture(properties.specularGlossinessTexture);
  1575. babylonMaterial.reflectivityTexture.hasAlpha = true;
  1576. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  1577. }
  1578. loader.loadMaterialAlphaProperties(material, properties.diffuseFactor);
  1579. };
  1580. return KHRMaterialsPbrSpecularGlossiness;
  1581. }(GLTF2.GLTFLoaderExtension));
  1582. Extensions.KHRMaterialsPbrSpecularGlossiness = KHRMaterialsPbrSpecularGlossiness;
  1583. GLTF2.GLTFLoader.RegisterExtension(new KHRMaterialsPbrSpecularGlossiness());
  1584. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  1585. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1586. })(BABYLON || (BABYLON = {}));
  1587. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map