babylon.glTF2FileLoader.js 79 KB

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