babylon.glTF2FileLoader.js 78 KB

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