babylon.glTF2FileLoader.js 78 KB

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