babylon.glTF2FileLoader.js 77 KB

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