babylon.glTF2FileLoader.js 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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. var meshes = [];
  380. if (_this._gltf.nodes) {
  381. for (var i = 0; i < _this._gltf.nodes.length; i++) {
  382. var node = _this._gltf.nodes[i];
  383. if (node.babylonMesh) {
  384. meshes.push(node.babylonMesh);
  385. }
  386. }
  387. }
  388. var skeletons = [];
  389. if (_this._gltf.skins) {
  390. for (var i = 0; i < _this._gltf.skins.length; i++) {
  391. var skin = _this._gltf.skins[i];
  392. if (skin.babylonSkeleton instanceof BABYLON.Skeleton) {
  393. skeletons.push(skin.babylonSkeleton);
  394. }
  395. }
  396. }
  397. onSuccess(meshes, null, skeletons);
  398. }, onProgress, onError);
  399. };
  400. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onSuccess, onProgress, onError) {
  401. this._loadAsync(null, scene, data, rootUrl, onSuccess, onProgress, onError);
  402. };
  403. GLTFLoader.prototype._loadAsync = function (nodeNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  404. this._loadData(data);
  405. this._babylonScene = scene;
  406. this._rootUrl = rootUrl;
  407. this._successCallback = onSuccess;
  408. this._progressCallback = onProgress;
  409. this._errorCallback = onError;
  410. this.addPendingData(this);
  411. this._loadScene(nodeNames);
  412. this._loadAnimations();
  413. this.removePendingData(this);
  414. };
  415. GLTFLoader.prototype._onError = function (message) {
  416. this._errorCallback(message);
  417. this.dispose();
  418. };
  419. GLTFLoader.prototype._onProgress = function (event) {
  420. this._progressCallback(event);
  421. };
  422. GLTFLoader.prototype._onRenderReady = function () {
  423. switch (this._parent.coordinateSystemMode) {
  424. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO:
  425. if (!this._babylonScene.useRightHandedSystem) {
  426. this._addRightHandToLeftHandRootTransform();
  427. }
  428. break;
  429. case BABYLON.GLTFLoaderCoordinateSystemMode.PASS_THROUGH:
  430. // do nothing
  431. break;
  432. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED:
  433. this._babylonScene.useRightHandedSystem = true;
  434. break;
  435. default:
  436. BABYLON.Tools.Error("Invalid coordinate system mode (" + this._parent.coordinateSystemMode + ")");
  437. break;
  438. }
  439. this._showMeshes();
  440. this._startAnimations();
  441. this._successCallback();
  442. this._renderReadyObservable.notifyObservers(this);
  443. };
  444. GLTFLoader.prototype._onLoaderComplete = function () {
  445. this.dispose();
  446. if (this._parent.onComplete) {
  447. this._parent.onComplete();
  448. }
  449. };
  450. GLTFLoader.prototype._loadData = function (data) {
  451. this._gltf = data.json;
  452. var binaryBuffer;
  453. var buffers = this._gltf.buffers;
  454. if (buffers.length > 0 && buffers[0].uri === undefined) {
  455. binaryBuffer = buffers[0];
  456. }
  457. if (data.bin) {
  458. if (binaryBuffer) {
  459. if (binaryBuffer.byteLength != data.bin.byteLength) {
  460. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  461. }
  462. }
  463. else {
  464. BABYLON.Tools.Warn("Unexpected BIN chunk");
  465. }
  466. binaryBuffer.loadedData = data.bin;
  467. }
  468. };
  469. GLTFLoader.prototype._addRightHandToLeftHandRootTransform = function () {
  470. var rootMesh = new BABYLON.Mesh("root", this._babylonScene);
  471. rootMesh.scaling = new BABYLON.Vector3(1, 1, -1);
  472. rootMesh.rotation.y = Math.PI;
  473. var nodes = this._gltf.nodes;
  474. for (var i = 0; i < nodes.length; i++) {
  475. var mesh = nodes[i].babylonMesh;
  476. if (mesh && !mesh.parent) {
  477. mesh.parent = rootMesh;
  478. }
  479. }
  480. };
  481. GLTFLoader.prototype._showMeshes = function () {
  482. var nodes = this._gltf.nodes;
  483. for (var i = 0; i < nodes.length; i++) {
  484. var node = nodes[i];
  485. if (node.babylonMesh) {
  486. node.babylonMesh.isVisible = true;
  487. }
  488. }
  489. };
  490. GLTFLoader.prototype._startAnimations = function () {
  491. var animations = this._gltf.animations;
  492. if (!animations) {
  493. return;
  494. }
  495. for (var i = 0; i < animations.length; i++) {
  496. var animation = animations[i];
  497. for (var j = 0; j < animation.targets.length; j++) {
  498. this._babylonScene.beginAnimation(animation.targets[j], 0, Number.MAX_VALUE, true);
  499. }
  500. }
  501. };
  502. GLTFLoader.prototype._loadScene = function (nodeNames) {
  503. var _this = this;
  504. var scene = this._gltf.scenes[this._gltf.scene || 0];
  505. var nodeIndices = scene.nodes;
  506. this._traverseNodes(nodeIndices, function (node, index, parentNode) {
  507. node.index = index;
  508. node.parent = parentNode;
  509. return true;
  510. });
  511. if (nodeNames) {
  512. if (!(nodeNames instanceof Array)) {
  513. nodeNames = [nodeNames];
  514. }
  515. var filteredNodeIndices = new Array();
  516. this._traverseNodes(nodeIndices, function (node) {
  517. if (nodeNames.indexOf(node.name) === -1) {
  518. return true;
  519. }
  520. filteredNodeIndices.push(node.index);
  521. return false;
  522. });
  523. nodeIndices = filteredNodeIndices;
  524. }
  525. this._traverseNodes(nodeIndices, function (node) { return _this._loadSkin(node); });
  526. this._traverseNodes(nodeIndices, function (node) { return _this._loadMesh(node); });
  527. };
  528. GLTFLoader.prototype._loadSkin = function (node) {
  529. var _this = this;
  530. if (node.skin !== undefined) {
  531. var skin = this._gltf.skins[node.skin];
  532. var skeletonId = "skeleton" + node.skin;
  533. skin.babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
  534. skin.index = node.skin;
  535. for (var i = 0; i < skin.joints.length; i++) {
  536. this._createBone(this._gltf.nodes[skin.joints[i]], skin);
  537. }
  538. if (skin.skeleton === undefined) {
  539. // TODO: handle when skeleton is not defined
  540. throw new Error("Not implemented");
  541. }
  542. if (skin.inverseBindMatrices === undefined) {
  543. // TODO: handle when inverse bind matrices are not defined
  544. throw new Error("Not implemented");
  545. }
  546. var accessor = this._gltf.accessors[skin.inverseBindMatrices];
  547. this._loadAccessorAsync(accessor, function (data) {
  548. _this._traverseNode(skin.skeleton, function (node, index, parent) { return _this._updateBone(node, parent, skin, data); });
  549. });
  550. }
  551. return true;
  552. };
  553. GLTFLoader.prototype._updateBone = function (node, parentNode, skin, inverseBindMatrixData) {
  554. var jointIndex = skin.joints.indexOf(node.index);
  555. if (jointIndex === -1) {
  556. this._createBone(node, skin);
  557. }
  558. var babylonBone = node.babylonSkinToBones[skin.index];
  559. // TODO: explain the math
  560. var matrix = jointIndex === -1 ? BABYLON.Matrix.Identity() : BABYLON.Matrix.FromArray(inverseBindMatrixData, jointIndex * 16);
  561. matrix.invertToRef(matrix);
  562. if (parentNode) {
  563. babylonBone.setParent(parentNode.babylonSkinToBones[skin.index], false);
  564. matrix.multiplyToRef(babylonBone.getParent().getInvertedAbsoluteTransform(), matrix);
  565. }
  566. babylonBone.updateMatrix(matrix);
  567. return true;
  568. };
  569. GLTFLoader.prototype._createBone = function (node, skin) {
  570. var babylonBone = new BABYLON.Bone(node.name || "bone" + node.index, skin.babylonSkeleton);
  571. node.babylonSkinToBones = node.babylonSkinToBones || {};
  572. node.babylonSkinToBones[skin.index] = babylonBone;
  573. node.babylonAnimationTargets = node.babylonAnimationTargets || [];
  574. node.babylonAnimationTargets.push(babylonBone);
  575. return babylonBone;
  576. };
  577. GLTFLoader.prototype._loadMesh = function (node) {
  578. var babylonMesh = new BABYLON.Mesh(node.name || "mesh" + node.index, this._babylonScene);
  579. babylonMesh.isVisible = false;
  580. this._loadTransform(node, babylonMesh);
  581. if (node.mesh !== undefined) {
  582. var mesh = this._gltf.meshes[node.mesh];
  583. this._loadMeshData(node, mesh, babylonMesh);
  584. }
  585. babylonMesh.parent = node.parent ? node.parent.babylonMesh : null;
  586. node.babylonMesh = babylonMesh;
  587. node.babylonAnimationTargets = node.babylonAnimationTargets || [];
  588. node.babylonAnimationTargets.push(node.babylonMesh);
  589. if (node.skin !== undefined) {
  590. var skin = this._gltf.skins[node.skin];
  591. babylonMesh.skeleton = skin.babylonSkeleton;
  592. }
  593. if (node.camera !== undefined) {
  594. // TODO: handle cameras
  595. }
  596. return true;
  597. };
  598. GLTFLoader.prototype._loadMeshData = function (node, mesh, babylonMesh) {
  599. var _this = this;
  600. babylonMesh.name = mesh.name || babylonMesh.name;
  601. var babylonMultiMaterial = new BABYLON.MultiMaterial(babylonMesh.name, this._babylonScene);
  602. babylonMesh.material = babylonMultiMaterial;
  603. var geometry = new BABYLON.Geometry(babylonMesh.name, this._babylonScene, null, false, babylonMesh);
  604. var vertexData = new BABYLON.VertexData();
  605. vertexData.positions = [];
  606. vertexData.indices = [];
  607. var subMeshInfos = [];
  608. var loadedPrimitives = 0;
  609. var totalPrimitives = mesh.primitives.length;
  610. var _loop_1 = function (i) {
  611. var primitive = mesh.primitives[i];
  612. if (primitive.mode && primitive.mode !== GLTF2.EMeshPrimitiveMode.TRIANGLES) {
  613. // TODO: handle other primitive modes
  614. throw new Error("Not implemented");
  615. }
  616. this_1._createMorphTargets(node, mesh, primitive, babylonMesh);
  617. this_1._loadVertexDataAsync(primitive, function (subVertexData) {
  618. _this._loadMorphTargetsData(mesh, primitive, subVertexData, babylonMesh);
  619. subMeshInfos.push({
  620. materialIndex: i,
  621. verticesStart: vertexData.positions.length,
  622. verticesCount: subVertexData.positions.length,
  623. indicesStart: vertexData.indices.length,
  624. indicesCount: subVertexData.indices.length
  625. });
  626. vertexData.merge(subVertexData);
  627. if (primitive.material === undefined) {
  628. babylonMultiMaterial.subMaterials[i] = _this._getDefaultMaterial();
  629. }
  630. else {
  631. _this.loadMaterial(primitive.material, function (babylonMaterial, isNew) {
  632. if (isNew && _this._parent.onMaterialLoaded) {
  633. _this._parent.onMaterialLoaded(babylonMaterial);
  634. }
  635. if (_this._renderReady) {
  636. babylonMaterial.forceCompilation(babylonMesh, function (babylonSubMaterial) {
  637. babylonMultiMaterial.subMaterials[i] = babylonSubMaterial;
  638. });
  639. }
  640. else {
  641. babylonMultiMaterial.subMaterials[i] = babylonMaterial;
  642. }
  643. });
  644. }
  645. if (++loadedPrimitives === totalPrimitives) {
  646. geometry.setAllVerticesData(vertexData, false);
  647. // TODO: optimize this so that sub meshes can be created without being overwritten after setting vertex data.
  648. // Sub meshes must be cleared and created after setting vertex data because of mesh._createGlobalSubMesh.
  649. babylonMesh.subMeshes = [];
  650. subMeshInfos.forEach(function (info) { return new BABYLON.SubMesh(info.materialIndex, info.verticesStart, info.verticesCount, info.indicesStart, info.indicesCount, babylonMesh); });
  651. }
  652. });
  653. };
  654. var this_1 = this;
  655. for (var i = 0; i < totalPrimitives; i++) {
  656. _loop_1(i);
  657. }
  658. };
  659. GLTFLoader.prototype._loadVertexDataAsync = function (primitive, onSuccess) {
  660. var _this = this;
  661. var attributes = primitive.attributes;
  662. if (!attributes) {
  663. this._onError("Primitive has no attributes");
  664. return;
  665. }
  666. var vertexData = new BABYLON.VertexData();
  667. var loadedAttributes = 0;
  668. var totalAttributes = Object.keys(attributes).length;
  669. var _loop_2 = function (semantic) {
  670. accessor = this_2._gltf.accessors[attributes[semantic]];
  671. this_2._loadAccessorAsync(accessor, function (data) {
  672. switch (semantic) {
  673. case "NORMAL":
  674. vertexData.normals = data;
  675. break;
  676. case "POSITION":
  677. vertexData.positions = data;
  678. break;
  679. case "TANGENT":
  680. vertexData.tangents = data;
  681. break;
  682. case "TEXCOORD_0":
  683. vertexData.uvs = data;
  684. break;
  685. case "TEXCOORD_1":
  686. vertexData.uvs2 = data;
  687. break;
  688. case "JOINTS_0":
  689. vertexData.matricesIndices = new Float32Array(Array.prototype.slice.apply(data));
  690. break;
  691. case "WEIGHTS_0":
  692. vertexData.matricesWeights = data;
  693. break;
  694. case "COLOR_0":
  695. vertexData.colors = data;
  696. break;
  697. default:
  698. BABYLON.Tools.Warn("Ignoring unrecognized semantic '" + semantic + "'");
  699. break;
  700. }
  701. if (++loadedAttributes === totalAttributes) {
  702. var indicesAccessor = _this._gltf.accessors[primitive.indices];
  703. if (indicesAccessor) {
  704. _this._loadAccessorAsync(indicesAccessor, function (data) {
  705. vertexData.indices = data;
  706. onSuccess(vertexData);
  707. });
  708. }
  709. else {
  710. vertexData.indices = new Uint32Array(vertexData.positions.length / 3);
  711. vertexData.indices.forEach(function (v, i) { return vertexData.indices[i] = i; });
  712. onSuccess(vertexData);
  713. }
  714. }
  715. });
  716. };
  717. var this_2 = this, accessor;
  718. for (var semantic in attributes) {
  719. _loop_2(semantic);
  720. }
  721. };
  722. GLTFLoader.prototype._createMorphTargets = function (node, mesh, primitive, babylonMesh) {
  723. var targets = primitive.targets;
  724. if (!targets) {
  725. return;
  726. }
  727. if (!babylonMesh.morphTargetManager) {
  728. babylonMesh.morphTargetManager = new BABYLON.MorphTargetManager();
  729. }
  730. for (var index = 0; index < targets.length; index++) {
  731. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  732. babylonMesh.morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  733. }
  734. };
  735. GLTFLoader.prototype._loadMorphTargetsData = function (mesh, primitive, vertexData, babylonMesh) {
  736. var targets = primitive.targets;
  737. if (!targets) {
  738. return;
  739. }
  740. var _loop_3 = function () {
  741. var babylonMorphTarget = babylonMesh.morphTargetManager.getTarget(index);
  742. attributes = targets[index];
  743. var _loop_4 = function (semantic) {
  744. accessor = this_3._gltf.accessors[attributes[semantic]];
  745. this_3._loadAccessorAsync(accessor, function (data) {
  746. if (accessor.name) {
  747. babylonMorphTarget.name = accessor.name;
  748. }
  749. // glTF stores morph target information as deltas while babylon.js expects the final data.
  750. // As a result we have to add the original data to the delta to calculate the final data.
  751. var values = data;
  752. switch (semantic) {
  753. case "NORMAL":
  754. GLTF2.GLTFUtils.ForEach(values, function (v, i) { return values[i] += vertexData.normals[i]; });
  755. babylonMorphTarget.setNormals(values);
  756. break;
  757. case "POSITION":
  758. GLTF2.GLTFUtils.ForEach(values, function (v, i) { return values[i] += vertexData.positions[i]; });
  759. babylonMorphTarget.setPositions(values);
  760. break;
  761. case "TANGENT":
  762. // Tangent data for morph targets is stored as xyz delta.
  763. // The vertexData.tangent is stored as xyzw.
  764. // So we need to skip every fourth vertexData.tangent.
  765. for (var i = 0, j = 0; i < values.length; i++, j++) {
  766. values[i] += vertexData.tangents[j];
  767. if ((i + 1) % 3 == 0) {
  768. j++;
  769. }
  770. }
  771. babylonMorphTarget.setTangents(values);
  772. break;
  773. default:
  774. BABYLON.Tools.Warn("Ignoring unrecognized semantic '" + semantic + "'");
  775. break;
  776. }
  777. });
  778. };
  779. for (var semantic in attributes) {
  780. _loop_4(semantic);
  781. }
  782. };
  783. var this_3 = this, attributes, accessor;
  784. for (var index = 0; index < targets.length; index++) {
  785. _loop_3();
  786. }
  787. };
  788. GLTFLoader.prototype._loadTransform = function (node, babylonMesh) {
  789. var position = BABYLON.Vector3.Zero();
  790. var rotation = BABYLON.Quaternion.Identity();
  791. var scaling = BABYLON.Vector3.One();
  792. if (node.matrix) {
  793. var mat = BABYLON.Matrix.FromArray(node.matrix);
  794. mat.decompose(scaling, rotation, position);
  795. }
  796. else {
  797. if (node.translation)
  798. position = BABYLON.Vector3.FromArray(node.translation);
  799. if (node.rotation)
  800. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  801. if (node.scale)
  802. scaling = BABYLON.Vector3.FromArray(node.scale);
  803. }
  804. babylonMesh.position = position;
  805. babylonMesh.rotationQuaternion = rotation;
  806. babylonMesh.scaling = scaling;
  807. };
  808. GLTFLoader.prototype._traverseNodes = function (indices, action, parentNode) {
  809. if (parentNode === void 0) { parentNode = null; }
  810. for (var i = 0; i < indices.length; i++) {
  811. this._traverseNode(indices[i], action, parentNode);
  812. }
  813. };
  814. GLTFLoader.prototype._traverseNode = function (index, action, parentNode) {
  815. if (parentNode === void 0) { parentNode = null; }
  816. var node = this._gltf.nodes[index];
  817. if (!action(node, index, parentNode)) {
  818. return;
  819. }
  820. if (node.children) {
  821. for (var i = 0; i < node.children.length; i++) {
  822. this._traverseNode(node.children[i], action, node);
  823. }
  824. }
  825. };
  826. GLTFLoader.prototype._loadAnimations = function () {
  827. var animations = this._gltf.animations;
  828. if (!animations || animations.length === 0) {
  829. return;
  830. }
  831. for (var animationIndex = 0; animationIndex < animations.length; animationIndex++) {
  832. var animation = animations[animationIndex];
  833. for (var channelIndex = 0; channelIndex < animation.channels.length; channelIndex++) {
  834. this._loadAnimationChannel(animation, animationIndex, channelIndex);
  835. }
  836. }
  837. };
  838. GLTFLoader.prototype._loadAnimationChannel = function (animation, animationIndex, channelIndex) {
  839. var channel = animation.channels[channelIndex];
  840. var samplerIndex = channel.sampler;
  841. var sampler = animation.samplers[samplerIndex];
  842. var targetNode = this._gltf.nodes[channel.target.node];
  843. if (!targetNode) {
  844. BABYLON.Tools.Warn("Animation channel target node (" + channel.target.node + ") does not exist");
  845. return;
  846. }
  847. var targetPath = {
  848. "translation": "position",
  849. "rotation": "rotationQuaternion",
  850. "scale": "scaling",
  851. "weights": "influence"
  852. }[channel.target.path];
  853. if (!targetPath) {
  854. BABYLON.Tools.Warn("Animation channel target path '" + channel.target.path + "' is not valid");
  855. return;
  856. }
  857. var animationType = {
  858. "position": BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
  859. "rotationQuaternion": BABYLON.Animation.ANIMATIONTYPE_QUATERNION,
  860. "scaling": BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
  861. "influence": BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  862. }[targetPath];
  863. var inputData;
  864. var outputData;
  865. var checkSuccess = function () {
  866. if (!inputData || !outputData) {
  867. return;
  868. }
  869. var outputBufferOffset = 0;
  870. var getNextOutputValue = {
  871. "position": function () {
  872. var value = BABYLON.Vector3.FromArray(outputData, outputBufferOffset);
  873. outputBufferOffset += 3;
  874. return value;
  875. },
  876. "rotationQuaternion": function () {
  877. var value = BABYLON.Quaternion.FromArray(outputData, outputBufferOffset);
  878. outputBufferOffset += 4;
  879. return value;
  880. },
  881. "scaling": function () {
  882. var value = BABYLON.Vector3.FromArray(outputData, outputBufferOffset);
  883. outputBufferOffset += 3;
  884. return value;
  885. },
  886. "influence": function () {
  887. var numTargets = targetNode.babylonMesh.morphTargetManager.numTargets;
  888. var value = new Array(numTargets);
  889. for (var i = 0; i < numTargets; i++) {
  890. value[i] = outputData[outputBufferOffset++];
  891. }
  892. return value;
  893. },
  894. }[targetPath];
  895. var getNextKey = {
  896. "LINEAR": function (frameIndex) { return ({
  897. frame: inputData[frameIndex],
  898. value: getNextOutputValue()
  899. }); },
  900. "CUBICSPLINE": function (frameIndex) { return ({
  901. frame: inputData[frameIndex],
  902. inTangent: getNextOutputValue(),
  903. value: getNextOutputValue(),
  904. outTangent: getNextOutputValue()
  905. }); },
  906. }[sampler.interpolation];
  907. var keys = new Array(inputData.length);
  908. for (var frameIndex = 0; frameIndex < inputData.length; frameIndex++) {
  909. keys[frameIndex] = getNextKey(frameIndex);
  910. }
  911. animation.targets = animation.targets || [];
  912. if (targetPath === "influence") {
  913. var morphTargetManager = targetNode.babylonMesh.morphTargetManager;
  914. for (var targetIndex = 0; targetIndex < morphTargetManager.numTargets; targetIndex++) {
  915. var morphTarget = morphTargetManager.getTarget(targetIndex);
  916. var animationName = (animation.name || "anim" + animationIndex) + "_" + targetIndex;
  917. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  918. babylonAnimation.setKeys(keys.map(function (key) { return ({
  919. frame: key.frame,
  920. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  921. value: key.value[targetIndex],
  922. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  923. }); }));
  924. morphTarget.animations.push(babylonAnimation);
  925. animation.targets.push(morphTarget);
  926. }
  927. }
  928. else {
  929. var animationName = animation.name || "anim" + animationIndex;
  930. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  931. babylonAnimation.setKeys(keys);
  932. for (var i = 0; i < targetNode.babylonAnimationTargets.length; i++) {
  933. var target = targetNode.babylonAnimationTargets[i];
  934. target.animations.push(babylonAnimation.clone());
  935. animation.targets.push(target);
  936. }
  937. }
  938. };
  939. this._loadAccessorAsync(this._gltf.accessors[sampler.input], function (data) {
  940. inputData = data;
  941. checkSuccess();
  942. });
  943. this._loadAccessorAsync(this._gltf.accessors[sampler.output], function (data) {
  944. outputData = data;
  945. checkSuccess();
  946. });
  947. };
  948. GLTFLoader.prototype._loadBufferAsync = function (index, onSuccess) {
  949. var _this = this;
  950. var buffer = this._gltf.buffers[index];
  951. this.addPendingData(buffer);
  952. if (buffer.loadedData) {
  953. setTimeout(function () {
  954. onSuccess(buffer.loadedData);
  955. _this.removePendingData(buffer);
  956. });
  957. }
  958. else if (GLTF2.GLTFUtils.IsBase64(buffer.uri)) {
  959. var data = GLTF2.GLTFUtils.DecodeBase64(buffer.uri);
  960. buffer.loadedData = new Uint8Array(data);
  961. setTimeout(function () {
  962. onSuccess(buffer.loadedData);
  963. _this.removePendingData(buffer);
  964. });
  965. }
  966. else if (buffer.loadedObservable) {
  967. buffer.loadedObservable.add(function (buffer) {
  968. onSuccess(buffer.loadedData);
  969. _this.removePendingData(buffer);
  970. });
  971. }
  972. else {
  973. buffer.loadedObservable = new BABYLON.Observable();
  974. buffer.loadedObservable.add(function (buffer) {
  975. onSuccess(buffer.loadedData);
  976. _this.removePendingData(buffer);
  977. });
  978. BABYLON.Tools.LoadFile(this._rootUrl + buffer.uri, function (data) {
  979. buffer.loadedData = new Uint8Array(data);
  980. buffer.loadedObservable.notifyObservers(buffer);
  981. buffer.loadedObservable = null;
  982. }, function (event) {
  983. if (!_this._disposed) {
  984. _this._onProgress(event);
  985. }
  986. }, this._babylonScene.database, true, function (request) {
  987. if (!_this._disposed) {
  988. _this._onError("Failed to load file '" + buffer.uri + "': " + request.status + " " + request.statusText);
  989. _this.removePendingData(buffer);
  990. }
  991. });
  992. }
  993. };
  994. GLTFLoader.prototype._loadBufferViewAsync = function (bufferView, byteOffset, byteLength, componentType, onSuccess) {
  995. var _this = this;
  996. byteOffset += (bufferView.byteOffset || 0);
  997. this._loadBufferAsync(bufferView.buffer, function (bufferData) {
  998. if (byteOffset + byteLength > bufferData.byteLength) {
  999. _this._onError("Buffer access is out of range");
  1000. return;
  1001. }
  1002. var buffer = bufferData.buffer;
  1003. byteOffset += bufferData.byteOffset;
  1004. var bufferViewData;
  1005. switch (componentType) {
  1006. case GLTF2.EComponentType.BYTE:
  1007. bufferViewData = new Int8Array(buffer, byteOffset, byteLength);
  1008. break;
  1009. case GLTF2.EComponentType.UNSIGNED_BYTE:
  1010. bufferViewData = new Uint8Array(buffer, byteOffset, byteLength);
  1011. break;
  1012. case GLTF2.EComponentType.SHORT:
  1013. bufferViewData = new Int16Array(buffer, byteOffset, byteLength);
  1014. break;
  1015. case GLTF2.EComponentType.UNSIGNED_SHORT:
  1016. bufferViewData = new Uint16Array(buffer, byteOffset, byteLength);
  1017. break;
  1018. case GLTF2.EComponentType.UNSIGNED_INT:
  1019. bufferViewData = new Uint32Array(buffer, byteOffset, byteLength);
  1020. break;
  1021. case GLTF2.EComponentType.FLOAT:
  1022. bufferViewData = new Float32Array(buffer, byteOffset, byteLength);
  1023. break;
  1024. default:
  1025. _this._onError("Invalid component type (" + componentType + ")");
  1026. return;
  1027. }
  1028. onSuccess(bufferViewData);
  1029. });
  1030. };
  1031. GLTFLoader.prototype._loadAccessorAsync = function (accessor, onSuccess) {
  1032. var bufferView = this._gltf.bufferViews[accessor.bufferView];
  1033. var byteOffset = accessor.byteOffset || 0;
  1034. var byteLength = accessor.count * this._getByteStrideFromType(accessor);
  1035. this._loadBufferViewAsync(bufferView, byteOffset, byteLength, accessor.componentType, onSuccess);
  1036. };
  1037. GLTFLoader.prototype._getByteStrideFromType = function (accessor) {
  1038. switch (accessor.type) {
  1039. case "SCALAR": return 1;
  1040. case "VEC2": return 2;
  1041. case "VEC3": return 3;
  1042. case "VEC4": return 4;
  1043. case "MAT2": return 4;
  1044. case "MAT3": return 9;
  1045. case "MAT4": return 16;
  1046. default:
  1047. this._onError("Invalid accessor type (" + accessor.type + ")");
  1048. return 0;
  1049. }
  1050. };
  1051. GLTFLoader.prototype.addPendingData = function (data) {
  1052. if (!this._renderReady) {
  1053. this._renderPendingCount++;
  1054. }
  1055. this.addLoaderPendingData(data);
  1056. };
  1057. GLTFLoader.prototype.removePendingData = function (data) {
  1058. if (!this._renderReady) {
  1059. if (--this._renderPendingCount === 0) {
  1060. this._renderReady = true;
  1061. this._onRenderReady();
  1062. }
  1063. }
  1064. this.removeLoaderPendingData(data);
  1065. };
  1066. GLTFLoader.prototype.addLoaderPendingData = function (data) {
  1067. this._loaderPendingCount++;
  1068. };
  1069. GLTFLoader.prototype.removeLoaderPendingData = function (data) {
  1070. if (--this._loaderPendingCount === 0) {
  1071. this._onLoaderComplete();
  1072. }
  1073. };
  1074. GLTFLoader.prototype._getDefaultMaterial = function () {
  1075. if (!this._defaultMaterial) {
  1076. var id = "__gltf_default";
  1077. var material = this._babylonScene.getMaterialByName(id);
  1078. if (!material) {
  1079. material = new BABYLON.PBRMaterial(id, this._babylonScene);
  1080. material.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1081. material.metallic = 1;
  1082. material.roughness = 1;
  1083. }
  1084. this._defaultMaterial = material;
  1085. }
  1086. return this._defaultMaterial;
  1087. };
  1088. GLTFLoader.prototype._loadMaterialMetallicRoughnessProperties = function (material) {
  1089. var babylonMaterial = material.babylonMaterial;
  1090. // Ensure metallic workflow
  1091. babylonMaterial.metallic = 1;
  1092. babylonMaterial.roughness = 1;
  1093. var properties = material.pbrMetallicRoughness;
  1094. if (!properties) {
  1095. return;
  1096. }
  1097. babylonMaterial.albedoColor = properties.baseColorFactor ? BABYLON.Color3.FromArray(properties.baseColorFactor) : new BABYLON.Color3(1, 1, 1);
  1098. babylonMaterial.metallic = properties.metallicFactor === undefined ? 1 : properties.metallicFactor;
  1099. babylonMaterial.roughness = properties.roughnessFactor === undefined ? 1 : properties.roughnessFactor;
  1100. if (properties.baseColorTexture) {
  1101. babylonMaterial.albedoTexture = this.loadTexture(properties.baseColorTexture);
  1102. }
  1103. if (properties.metallicRoughnessTexture) {
  1104. babylonMaterial.metallicTexture = this.loadTexture(properties.metallicRoughnessTexture);
  1105. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  1106. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  1107. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  1108. }
  1109. this.loadMaterialAlphaProperties(material, properties.baseColorFactor);
  1110. };
  1111. GLTFLoader.prototype.loadMaterial = function (index, assign) {
  1112. var material = this._gltf.materials[index];
  1113. material.index = index;
  1114. if (material.babylonMaterial) {
  1115. assign(material.babylonMaterial, false);
  1116. return;
  1117. }
  1118. if (GLTF2.GLTFLoaderExtension.LoadMaterial(this, material, assign)) {
  1119. return;
  1120. }
  1121. this.createPbrMaterial(material);
  1122. this.loadMaterialBaseProperties(material);
  1123. this._loadMaterialMetallicRoughnessProperties(material);
  1124. assign(material.babylonMaterial, true);
  1125. };
  1126. GLTFLoader.prototype.createPbrMaterial = function (material) {
  1127. var babylonMaterial = new BABYLON.PBRMaterial(material.name || "mat" + material.index, this._babylonScene);
  1128. babylonMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1129. material.babylonMaterial = babylonMaterial;
  1130. };
  1131. GLTFLoader.prototype.loadMaterialBaseProperties = function (material) {
  1132. var babylonMaterial = material.babylonMaterial;
  1133. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  1134. if (material.doubleSided) {
  1135. babylonMaterial.backFaceCulling = false;
  1136. babylonMaterial.twoSidedLighting = true;
  1137. }
  1138. if (material.normalTexture) {
  1139. babylonMaterial.bumpTexture = this.loadTexture(material.normalTexture);
  1140. babylonMaterial.invertNormalMapX = true;
  1141. babylonMaterial.invertNormalMapY = true;
  1142. if (material.normalTexture.scale !== undefined) {
  1143. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  1144. }
  1145. }
  1146. if (material.occlusionTexture) {
  1147. babylonMaterial.ambientTexture = this.loadTexture(material.occlusionTexture);
  1148. babylonMaterial.useAmbientInGrayScale = true;
  1149. if (material.occlusionTexture.strength !== undefined) {
  1150. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  1151. }
  1152. }
  1153. if (material.emissiveTexture) {
  1154. babylonMaterial.emissiveTexture = this.loadTexture(material.emissiveTexture);
  1155. }
  1156. };
  1157. GLTFLoader.prototype.loadMaterialAlphaProperties = function (material, colorFactor) {
  1158. var babylonMaterial = material.babylonMaterial;
  1159. var alphaMode = material.alphaMode || "OPAQUE";
  1160. switch (alphaMode) {
  1161. case "OPAQUE":
  1162. // default is opaque
  1163. break;
  1164. case "MASK":
  1165. case "BLEND":
  1166. if (colorFactor) {
  1167. babylonMaterial.alpha = colorFactor[3];
  1168. }
  1169. if (babylonMaterial.albedoTexture) {
  1170. babylonMaterial.albedoTexture.hasAlpha = true;
  1171. babylonMaterial.useAlphaFromAlbedoTexture = (alphaMode === "BLEND");
  1172. }
  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, isNew) {
  1443. assign(babylonMaterial, isNew);
  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, true);
  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.reflectivityTexture.hasAlpha = true;
  1521. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  1522. }
  1523. loader.loadMaterialAlphaProperties(material, properties.diffuseFactor);
  1524. };
  1525. return KHRMaterialsPbrSpecularGlossiness;
  1526. }(GLTF2.GLTFLoaderExtension));
  1527. Extensions.KHRMaterialsPbrSpecularGlossiness = KHRMaterialsPbrSpecularGlossiness;
  1528. GLTF2.GLTFLoader.RegisterExtension(new KHRMaterialsPbrSpecularGlossiness());
  1529. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  1530. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1531. })(BABYLON || (BABYLON = {}));
  1532. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map