babylon.glTF2FileLoader.js 77 KB

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