babylon.glTF1FileLoader.js 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var GLTFLoaderCoordinateSystemMode;
  5. (function (GLTFLoaderCoordinateSystemMode) {
  6. /**
  7. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  8. */
  9. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
  10. /**
  11. * Sets the useRightHandedSystem flag on the scene.
  12. */
  13. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
  14. })(GLTFLoaderCoordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode || (BABYLON.GLTFLoaderCoordinateSystemMode = {}));
  15. var GLTFLoaderAnimationStartMode;
  16. (function (GLTFLoaderAnimationStartMode) {
  17. /**
  18. * No animation will start.
  19. */
  20. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
  21. /**
  22. * The first animation will start.
  23. */
  24. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
  25. /**
  26. * All animations will start.
  27. */
  28. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
  29. })(GLTFLoaderAnimationStartMode = BABYLON.GLTFLoaderAnimationStartMode || (BABYLON.GLTFLoaderAnimationStartMode = {}));
  30. var GLTFFileLoader = /** @class */ (function () {
  31. function GLTFFileLoader() {
  32. // #endregion
  33. // #region V2 options
  34. /**
  35. * The coordinate system mode (AUTO, FORCE_RIGHT_HANDED).
  36. */
  37. this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
  38. /**
  39. * The animation start mode (NONE, FIRST, ALL).
  40. */
  41. this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
  42. /**
  43. * Set to true to compile materials before raising the success callback.
  44. */
  45. this.compileMaterials = false;
  46. /**
  47. * Set to true to also compile materials with clip planes.
  48. */
  49. this.useClipPlane = false;
  50. /**
  51. * Set to true to compile shadow generators before raising the success callback.
  52. */
  53. this.compileShadowGenerators = false;
  54. this.name = "gltf";
  55. this.extensions = {
  56. ".gltf": { isBinary: false },
  57. ".glb": { isBinary: true }
  58. };
  59. }
  60. /**
  61. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  62. */
  63. GLTFFileLoader.prototype.dispose = function () {
  64. if (this._loader) {
  65. this._loader.dispose();
  66. }
  67. };
  68. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  69. try {
  70. var loaderData = GLTFFileLoader._parse(data);
  71. if (this.onParsed) {
  72. this.onParsed(loaderData);
  73. }
  74. this._loader = this._getLoader(loaderData);
  75. this._loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onSuccess, onProgress, onError);
  76. }
  77. catch (e) {
  78. if (onError) {
  79. onError(e.message, e);
  80. }
  81. else {
  82. BABYLON.Tools.Error(e.message);
  83. }
  84. }
  85. };
  86. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onSuccess, onProgress, onError) {
  87. try {
  88. var loaderData = GLTFFileLoader._parse(data);
  89. if (this.onParsed) {
  90. this.onParsed(loaderData);
  91. }
  92. this._loader = this._getLoader(loaderData);
  93. this._loader.loadAsync(scene, loaderData, rootUrl, onSuccess, onProgress, onError);
  94. }
  95. catch (e) {
  96. if (onError) {
  97. onError(e.message, e);
  98. }
  99. else {
  100. BABYLON.Tools.Error(e.message);
  101. }
  102. }
  103. };
  104. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  105. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  106. };
  107. GLTFFileLoader.prototype.createPlugin = function () {
  108. return new GLTFFileLoader();
  109. };
  110. GLTFFileLoader._parse = function (data) {
  111. if (data instanceof ArrayBuffer) {
  112. return GLTFFileLoader._parseBinary(data);
  113. }
  114. return {
  115. json: JSON.parse(data),
  116. bin: null
  117. };
  118. };
  119. GLTFFileLoader.prototype._getLoader = function (loaderData) {
  120. var loaderVersion = { major: 2, minor: 0 };
  121. var asset = loaderData.json.asset || {};
  122. var version = GLTFFileLoader._parseVersion(asset.version);
  123. if (!version) {
  124. throw new Error("Invalid version: " + asset.version);
  125. }
  126. if (asset.minVersion !== undefined) {
  127. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  128. if (!minVersion) {
  129. throw new Error("Invalid minimum version: " + asset.minVersion);
  130. }
  131. if (GLTFFileLoader._compareVersion(minVersion, loaderVersion) > 0) {
  132. throw new Error("Incompatible minimum version: " + asset.minVersion);
  133. }
  134. }
  135. var createLoaders = {
  136. 1: GLTFFileLoader.CreateGLTFLoaderV1,
  137. 2: GLTFFileLoader.CreateGLTFLoaderV2
  138. };
  139. var createLoader = createLoaders[version.major];
  140. if (!createLoader) {
  141. throw new Error("Unsupported version: " + asset.version);
  142. }
  143. return createLoader(this);
  144. };
  145. GLTFFileLoader._parseBinary = function (data) {
  146. var Binary = {
  147. Magic: 0x46546C67
  148. };
  149. var binaryReader = new BinaryReader(data);
  150. var magic = binaryReader.readUint32();
  151. if (magic !== Binary.Magic) {
  152. throw new Error("Unexpected magic: " + magic);
  153. }
  154. var version = binaryReader.readUint32();
  155. switch (version) {
  156. case 1: return GLTFFileLoader._parseV1(binaryReader);
  157. case 2: return GLTFFileLoader._parseV2(binaryReader);
  158. }
  159. throw new Error("Unsupported version: " + version);
  160. };
  161. GLTFFileLoader._parseV1 = function (binaryReader) {
  162. var ContentFormat = {
  163. JSON: 0
  164. };
  165. var length = binaryReader.readUint32();
  166. if (length != binaryReader.getLength()) {
  167. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  168. }
  169. var contentLength = binaryReader.readUint32();
  170. var contentFormat = binaryReader.readUint32();
  171. var content;
  172. switch (contentFormat) {
  173. case ContentFormat.JSON: {
  174. content = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength)));
  175. break;
  176. }
  177. default: {
  178. throw new Error("Unexpected content format: " + contentFormat);
  179. }
  180. }
  181. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  182. var body = binaryReader.readUint8Array(bytesRemaining);
  183. return {
  184. json: content,
  185. bin: body
  186. };
  187. };
  188. GLTFFileLoader._parseV2 = function (binaryReader) {
  189. var ChunkFormat = {
  190. JSON: 0x4E4F534A,
  191. BIN: 0x004E4942
  192. };
  193. var length = binaryReader.readUint32();
  194. if (length !== binaryReader.getLength()) {
  195. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  196. }
  197. // JSON chunk
  198. var chunkLength = binaryReader.readUint32();
  199. var chunkFormat = binaryReader.readUint32();
  200. if (chunkFormat !== ChunkFormat.JSON) {
  201. throw new Error("First chunk format is not JSON");
  202. }
  203. var json = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength)));
  204. // Look for BIN chunk
  205. var bin = null;
  206. while (binaryReader.getPosition() < binaryReader.getLength()) {
  207. var chunkLength_1 = binaryReader.readUint32();
  208. var chunkFormat_1 = binaryReader.readUint32();
  209. switch (chunkFormat_1) {
  210. case ChunkFormat.JSON: {
  211. throw new Error("Unexpected JSON chunk");
  212. }
  213. case ChunkFormat.BIN: {
  214. bin = binaryReader.readUint8Array(chunkLength_1);
  215. break;
  216. }
  217. default: {
  218. // ignore unrecognized chunkFormat
  219. binaryReader.skipBytes(chunkLength_1);
  220. break;
  221. }
  222. }
  223. }
  224. return {
  225. json: json,
  226. bin: bin
  227. };
  228. };
  229. GLTFFileLoader._parseVersion = function (version) {
  230. if (version === "1.0" || version === "1.0.1") {
  231. return {
  232. major: 1,
  233. minor: 0
  234. };
  235. }
  236. var match = (version + "").match(/^(\d+)\.(\d+)/);
  237. if (!match) {
  238. return null;
  239. }
  240. return {
  241. major: parseInt(match[1]),
  242. minor: parseInt(match[2])
  243. };
  244. };
  245. GLTFFileLoader._compareVersion = function (a, b) {
  246. if (a.major > b.major)
  247. return 1;
  248. if (a.major < b.major)
  249. return -1;
  250. if (a.minor > b.minor)
  251. return 1;
  252. if (a.minor < b.minor)
  253. return -1;
  254. return 0;
  255. };
  256. GLTFFileLoader._decodeBufferToText = function (buffer) {
  257. var result = "";
  258. var length = buffer.byteLength;
  259. for (var i = 0; i < length; i++) {
  260. result += String.fromCharCode(buffer[i]);
  261. }
  262. return result;
  263. };
  264. // #endregion
  265. // #region V1 options
  266. GLTFFileLoader.IncrementalLoading = true;
  267. GLTFFileLoader.HomogeneousCoordinates = false;
  268. return GLTFFileLoader;
  269. }());
  270. BABYLON.GLTFFileLoader = GLTFFileLoader;
  271. var BinaryReader = /** @class */ (function () {
  272. function BinaryReader(arrayBuffer) {
  273. this._arrayBuffer = arrayBuffer;
  274. this._dataView = new DataView(arrayBuffer);
  275. this._byteOffset = 0;
  276. }
  277. BinaryReader.prototype.getPosition = function () {
  278. return this._byteOffset;
  279. };
  280. BinaryReader.prototype.getLength = function () {
  281. return this._arrayBuffer.byteLength;
  282. };
  283. BinaryReader.prototype.readUint32 = function () {
  284. var value = this._dataView.getUint32(this._byteOffset, true);
  285. this._byteOffset += 4;
  286. return value;
  287. };
  288. BinaryReader.prototype.readUint8Array = function (length) {
  289. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  290. this._byteOffset += length;
  291. return value;
  292. };
  293. BinaryReader.prototype.skipBytes = function (length) {
  294. this._byteOffset += length;
  295. };
  296. return BinaryReader;
  297. }());
  298. if (BABYLON.SceneLoader) {
  299. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  300. }
  301. })(BABYLON || (BABYLON = {}));
  302. //# sourceMappingURL=babylon.glTFFileLoader.js.map
  303. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  304. var BABYLON;
  305. (function (BABYLON) {
  306. var GLTF1;
  307. (function (GLTF1) {
  308. /**
  309. * Enums
  310. */
  311. var EComponentType;
  312. (function (EComponentType) {
  313. EComponentType[EComponentType["BYTE"] = 5120] = "BYTE";
  314. EComponentType[EComponentType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  315. EComponentType[EComponentType["SHORT"] = 5122] = "SHORT";
  316. EComponentType[EComponentType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  317. EComponentType[EComponentType["FLOAT"] = 5126] = "FLOAT";
  318. })(EComponentType = GLTF1.EComponentType || (GLTF1.EComponentType = {}));
  319. var EShaderType;
  320. (function (EShaderType) {
  321. EShaderType[EShaderType["FRAGMENT"] = 35632] = "FRAGMENT";
  322. EShaderType[EShaderType["VERTEX"] = 35633] = "VERTEX";
  323. })(EShaderType = GLTF1.EShaderType || (GLTF1.EShaderType = {}));
  324. var EParameterType;
  325. (function (EParameterType) {
  326. EParameterType[EParameterType["BYTE"] = 5120] = "BYTE";
  327. EParameterType[EParameterType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  328. EParameterType[EParameterType["SHORT"] = 5122] = "SHORT";
  329. EParameterType[EParameterType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  330. EParameterType[EParameterType["INT"] = 5124] = "INT";
  331. EParameterType[EParameterType["UNSIGNED_INT"] = 5125] = "UNSIGNED_INT";
  332. EParameterType[EParameterType["FLOAT"] = 5126] = "FLOAT";
  333. EParameterType[EParameterType["FLOAT_VEC2"] = 35664] = "FLOAT_VEC2";
  334. EParameterType[EParameterType["FLOAT_VEC3"] = 35665] = "FLOAT_VEC3";
  335. EParameterType[EParameterType["FLOAT_VEC4"] = 35666] = "FLOAT_VEC4";
  336. EParameterType[EParameterType["INT_VEC2"] = 35667] = "INT_VEC2";
  337. EParameterType[EParameterType["INT_VEC3"] = 35668] = "INT_VEC3";
  338. EParameterType[EParameterType["INT_VEC4"] = 35669] = "INT_VEC4";
  339. EParameterType[EParameterType["BOOL"] = 35670] = "BOOL";
  340. EParameterType[EParameterType["BOOL_VEC2"] = 35671] = "BOOL_VEC2";
  341. EParameterType[EParameterType["BOOL_VEC3"] = 35672] = "BOOL_VEC3";
  342. EParameterType[EParameterType["BOOL_VEC4"] = 35673] = "BOOL_VEC4";
  343. EParameterType[EParameterType["FLOAT_MAT2"] = 35674] = "FLOAT_MAT2";
  344. EParameterType[EParameterType["FLOAT_MAT3"] = 35675] = "FLOAT_MAT3";
  345. EParameterType[EParameterType["FLOAT_MAT4"] = 35676] = "FLOAT_MAT4";
  346. EParameterType[EParameterType["SAMPLER_2D"] = 35678] = "SAMPLER_2D";
  347. })(EParameterType = GLTF1.EParameterType || (GLTF1.EParameterType = {}));
  348. var ETextureWrapMode;
  349. (function (ETextureWrapMode) {
  350. ETextureWrapMode[ETextureWrapMode["CLAMP_TO_EDGE"] = 33071] = "CLAMP_TO_EDGE";
  351. ETextureWrapMode[ETextureWrapMode["MIRRORED_REPEAT"] = 33648] = "MIRRORED_REPEAT";
  352. ETextureWrapMode[ETextureWrapMode["REPEAT"] = 10497] = "REPEAT";
  353. })(ETextureWrapMode = GLTF1.ETextureWrapMode || (GLTF1.ETextureWrapMode = {}));
  354. var ETextureFilterType;
  355. (function (ETextureFilterType) {
  356. ETextureFilterType[ETextureFilterType["NEAREST"] = 9728] = "NEAREST";
  357. ETextureFilterType[ETextureFilterType["LINEAR"] = 9728] = "LINEAR";
  358. ETextureFilterType[ETextureFilterType["NEAREST_MIPMAP_NEAREST"] = 9984] = "NEAREST_MIPMAP_NEAREST";
  359. ETextureFilterType[ETextureFilterType["LINEAR_MIPMAP_NEAREST"] = 9985] = "LINEAR_MIPMAP_NEAREST";
  360. ETextureFilterType[ETextureFilterType["NEAREST_MIPMAP_LINEAR"] = 9986] = "NEAREST_MIPMAP_LINEAR";
  361. ETextureFilterType[ETextureFilterType["LINEAR_MIPMAP_LINEAR"] = 9987] = "LINEAR_MIPMAP_LINEAR";
  362. })(ETextureFilterType = GLTF1.ETextureFilterType || (GLTF1.ETextureFilterType = {}));
  363. var ETextureFormat;
  364. (function (ETextureFormat) {
  365. ETextureFormat[ETextureFormat["ALPHA"] = 6406] = "ALPHA";
  366. ETextureFormat[ETextureFormat["RGB"] = 6407] = "RGB";
  367. ETextureFormat[ETextureFormat["RGBA"] = 6408] = "RGBA";
  368. ETextureFormat[ETextureFormat["LUMINANCE"] = 6409] = "LUMINANCE";
  369. ETextureFormat[ETextureFormat["LUMINANCE_ALPHA"] = 6410] = "LUMINANCE_ALPHA";
  370. })(ETextureFormat = GLTF1.ETextureFormat || (GLTF1.ETextureFormat = {}));
  371. var ECullingType;
  372. (function (ECullingType) {
  373. ECullingType[ECullingType["FRONT"] = 1028] = "FRONT";
  374. ECullingType[ECullingType["BACK"] = 1029] = "BACK";
  375. ECullingType[ECullingType["FRONT_AND_BACK"] = 1032] = "FRONT_AND_BACK";
  376. })(ECullingType = GLTF1.ECullingType || (GLTF1.ECullingType = {}));
  377. var EBlendingFunction;
  378. (function (EBlendingFunction) {
  379. EBlendingFunction[EBlendingFunction["ZERO"] = 0] = "ZERO";
  380. EBlendingFunction[EBlendingFunction["ONE"] = 1] = "ONE";
  381. EBlendingFunction[EBlendingFunction["SRC_COLOR"] = 768] = "SRC_COLOR";
  382. EBlendingFunction[EBlendingFunction["ONE_MINUS_SRC_COLOR"] = 769] = "ONE_MINUS_SRC_COLOR";
  383. EBlendingFunction[EBlendingFunction["DST_COLOR"] = 774] = "DST_COLOR";
  384. EBlendingFunction[EBlendingFunction["ONE_MINUS_DST_COLOR"] = 775] = "ONE_MINUS_DST_COLOR";
  385. EBlendingFunction[EBlendingFunction["SRC_ALPHA"] = 770] = "SRC_ALPHA";
  386. EBlendingFunction[EBlendingFunction["ONE_MINUS_SRC_ALPHA"] = 771] = "ONE_MINUS_SRC_ALPHA";
  387. EBlendingFunction[EBlendingFunction["DST_ALPHA"] = 772] = "DST_ALPHA";
  388. EBlendingFunction[EBlendingFunction["ONE_MINUS_DST_ALPHA"] = 773] = "ONE_MINUS_DST_ALPHA";
  389. EBlendingFunction[EBlendingFunction["CONSTANT_COLOR"] = 32769] = "CONSTANT_COLOR";
  390. EBlendingFunction[EBlendingFunction["ONE_MINUS_CONSTANT_COLOR"] = 32770] = "ONE_MINUS_CONSTANT_COLOR";
  391. EBlendingFunction[EBlendingFunction["CONSTANT_ALPHA"] = 32771] = "CONSTANT_ALPHA";
  392. EBlendingFunction[EBlendingFunction["ONE_MINUS_CONSTANT_ALPHA"] = 32772] = "ONE_MINUS_CONSTANT_ALPHA";
  393. EBlendingFunction[EBlendingFunction["SRC_ALPHA_SATURATE"] = 776] = "SRC_ALPHA_SATURATE";
  394. })(EBlendingFunction = GLTF1.EBlendingFunction || (GLTF1.EBlendingFunction = {}));
  395. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  396. })(BABYLON || (BABYLON = {}));
  397. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  398. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  399. var BABYLON;
  400. (function (BABYLON) {
  401. var GLTF1;
  402. (function (GLTF1) {
  403. /**
  404. * Tokenizer. Used for shaders compatibility
  405. * Automatically map world, view, projection, worldViewProjection, attributes and so on
  406. */
  407. var ETokenType;
  408. (function (ETokenType) {
  409. ETokenType[ETokenType["IDENTIFIER"] = 1] = "IDENTIFIER";
  410. ETokenType[ETokenType["UNKNOWN"] = 2] = "UNKNOWN";
  411. ETokenType[ETokenType["END_OF_INPUT"] = 3] = "END_OF_INPUT";
  412. })(ETokenType || (ETokenType = {}));
  413. var Tokenizer = /** @class */ (function () {
  414. function Tokenizer(toParse) {
  415. this._pos = 0;
  416. this.isLetterOrDigitPattern = /^[a-zA-Z0-9]+$/;
  417. this._toParse = toParse;
  418. this._maxPos = toParse.length;
  419. }
  420. Tokenizer.prototype.getNextToken = function () {
  421. if (this.isEnd())
  422. return ETokenType.END_OF_INPUT;
  423. this.currentString = this.read();
  424. this.currentToken = ETokenType.UNKNOWN;
  425. if (this.currentString === "_" || this.isLetterOrDigitPattern.test(this.currentString)) {
  426. this.currentToken = ETokenType.IDENTIFIER;
  427. this.currentIdentifier = this.currentString;
  428. while (!this.isEnd() && (this.isLetterOrDigitPattern.test(this.currentString = this.peek()) || this.currentString === "_")) {
  429. this.currentIdentifier += this.currentString;
  430. this.forward();
  431. }
  432. }
  433. return this.currentToken;
  434. };
  435. Tokenizer.prototype.peek = function () {
  436. return this._toParse[this._pos];
  437. };
  438. Tokenizer.prototype.read = function () {
  439. return this._toParse[this._pos++];
  440. };
  441. Tokenizer.prototype.forward = function () {
  442. this._pos++;
  443. };
  444. Tokenizer.prototype.isEnd = function () {
  445. return this._pos >= this._maxPos;
  446. };
  447. return Tokenizer;
  448. }());
  449. /**
  450. * Values
  451. */
  452. var glTFTransforms = ["MODEL", "VIEW", "PROJECTION", "MODELVIEW", "MODELVIEWPROJECTION", "JOINTMATRIX"];
  453. var babylonTransforms = ["world", "view", "projection", "worldView", "worldViewProjection", "mBones"];
  454. var glTFAnimationPaths = ["translation", "rotation", "scale"];
  455. var babylonAnimationPaths = ["position", "rotationQuaternion", "scaling"];
  456. /**
  457. * Parse
  458. */
  459. var parseBuffers = function (parsedBuffers, gltfRuntime) {
  460. for (var buf in parsedBuffers) {
  461. var parsedBuffer = parsedBuffers[buf];
  462. gltfRuntime.buffers[buf] = parsedBuffer;
  463. gltfRuntime.buffersCount++;
  464. }
  465. };
  466. var parseShaders = function (parsedShaders, gltfRuntime) {
  467. for (var sha in parsedShaders) {
  468. var parsedShader = parsedShaders[sha];
  469. gltfRuntime.shaders[sha] = parsedShader;
  470. gltfRuntime.shaderscount++;
  471. }
  472. };
  473. var parseObject = function (parsedObjects, runtimeProperty, gltfRuntime) {
  474. for (var object in parsedObjects) {
  475. var parsedObject = parsedObjects[object];
  476. gltfRuntime[runtimeProperty][object] = parsedObject;
  477. }
  478. };
  479. /**
  480. * Utils
  481. */
  482. var normalizeUVs = function (buffer) {
  483. if (!buffer) {
  484. return;
  485. }
  486. for (var i = 0; i < buffer.length / 2; i++) {
  487. buffer[i * 2 + 1] = 1.0 - buffer[i * 2 + 1];
  488. }
  489. };
  490. var getAttribute = function (attributeParameter) {
  491. if (attributeParameter.semantic === "NORMAL") {
  492. return "normal";
  493. }
  494. else if (attributeParameter.semantic === "POSITION") {
  495. return "position";
  496. }
  497. else if (attributeParameter.semantic === "JOINT") {
  498. return "matricesIndices";
  499. }
  500. else if (attributeParameter.semantic === "WEIGHT") {
  501. return "matricesWeights";
  502. }
  503. else if (attributeParameter.semantic === "COLOR") {
  504. return "color";
  505. }
  506. else if (attributeParameter.semantic && attributeParameter.semantic.indexOf("TEXCOORD_") !== -1) {
  507. var channel = Number(attributeParameter.semantic.split("_")[1]);
  508. return "uv" + (channel === 0 ? "" : channel + 1);
  509. }
  510. return null;
  511. };
  512. /**
  513. * Loads and creates animations
  514. */
  515. var loadAnimations = function (gltfRuntime) {
  516. for (var anim in gltfRuntime.animations) {
  517. var animation = gltfRuntime.animations[anim];
  518. if (!animation.channels || !animation.samplers) {
  519. continue;
  520. }
  521. var lastAnimation = null;
  522. for (var i = 0; i < animation.channels.length; i++) {
  523. // Get parameters and load buffers
  524. var channel = animation.channels[i];
  525. var sampler = animation.samplers[channel.sampler];
  526. if (!sampler) {
  527. continue;
  528. }
  529. var inputData = null;
  530. var outputData = null;
  531. if (animation.parameters) {
  532. inputData = animation.parameters[sampler.input];
  533. outputData = animation.parameters[sampler.output];
  534. }
  535. else {
  536. inputData = sampler.input;
  537. outputData = sampler.output;
  538. }
  539. var bufferInput = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[inputData]);
  540. var bufferOutput = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[outputData]);
  541. var targetID = channel.target.id;
  542. var targetNode = gltfRuntime.scene.getNodeByID(targetID);
  543. if (targetNode === null) {
  544. targetNode = gltfRuntime.scene.getNodeByName(targetID);
  545. }
  546. if (targetNode === null) {
  547. BABYLON.Tools.Warn("Creating animation named " + anim + ". But cannot find node named " + targetID + " to attach to");
  548. continue;
  549. }
  550. var isBone = targetNode instanceof BABYLON.Bone;
  551. // Get target path (position, rotation or scaling)
  552. var targetPath = channel.target.path;
  553. var targetPathIndex = glTFAnimationPaths.indexOf(targetPath);
  554. if (targetPathIndex !== -1) {
  555. targetPath = babylonAnimationPaths[targetPathIndex];
  556. }
  557. // Determine animation type
  558. var animationType = BABYLON.Animation.ANIMATIONTYPE_MATRIX;
  559. if (!isBone) {
  560. if (targetPath === "rotationQuaternion") {
  561. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  562. targetNode.rotationQuaternion = new BABYLON.Quaternion();
  563. }
  564. else {
  565. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  566. }
  567. }
  568. // Create animation and key frames
  569. var babylonAnimation = null;
  570. var keys = [];
  571. var arrayOffset = 0;
  572. var modifyKey = false;
  573. if (isBone && lastAnimation && lastAnimation.getKeys().length === bufferInput.length) {
  574. babylonAnimation = lastAnimation;
  575. modifyKey = true;
  576. }
  577. if (!modifyKey) {
  578. babylonAnimation = new BABYLON.Animation(anim, isBone ? "_matrix" : targetPath, 1, animationType, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  579. }
  580. // For each frame
  581. for (var j = 0; j < bufferInput.length; j++) {
  582. var value = null;
  583. if (targetPath === "rotationQuaternion") {
  584. value = BABYLON.Quaternion.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2], bufferOutput[arrayOffset + 3]]);
  585. arrayOffset += 4;
  586. }
  587. else {
  588. value = BABYLON.Vector3.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2]]);
  589. arrayOffset += 3;
  590. }
  591. if (isBone) {
  592. var bone = targetNode;
  593. var translation = BABYLON.Vector3.Zero();
  594. var rotationQuaternion = new BABYLON.Quaternion();
  595. var scaling = BABYLON.Vector3.Zero();
  596. // Warning on decompose
  597. var mat = bone.getBaseMatrix();
  598. if (modifyKey && lastAnimation) {
  599. mat = lastAnimation.getKeys()[j].value;
  600. }
  601. mat.decompose(scaling, rotationQuaternion, translation);
  602. if (targetPath === "position") {
  603. translation = value;
  604. }
  605. else if (targetPath === "rotationQuaternion") {
  606. rotationQuaternion = value;
  607. }
  608. else {
  609. scaling = value;
  610. }
  611. value = BABYLON.Matrix.Compose(scaling, rotationQuaternion, translation);
  612. }
  613. if (!modifyKey) {
  614. keys.push({
  615. frame: bufferInput[j],
  616. value: value
  617. });
  618. }
  619. else if (lastAnimation) {
  620. lastAnimation.getKeys()[j].value = value;
  621. }
  622. }
  623. // Finish
  624. if (!modifyKey && babylonAnimation) {
  625. babylonAnimation.setKeys(keys);
  626. targetNode.animations.push(babylonAnimation);
  627. }
  628. lastAnimation = babylonAnimation;
  629. gltfRuntime.scene.stopAnimation(targetNode);
  630. gltfRuntime.scene.beginAnimation(targetNode, 0, bufferInput[bufferInput.length - 1], true, 1.0);
  631. }
  632. }
  633. };
  634. /**
  635. * Returns the bones transformation matrix
  636. */
  637. var configureBoneTransformation = function (node) {
  638. var mat = null;
  639. if (node.translation || node.rotation || node.scale) {
  640. var scale = BABYLON.Vector3.FromArray(node.scale || [1, 1, 1]);
  641. var rotation = BABYLON.Quaternion.FromArray(node.rotation || [0, 0, 0, 1]);
  642. var position = BABYLON.Vector3.FromArray(node.translation || [0, 0, 0]);
  643. mat = BABYLON.Matrix.Compose(scale, rotation, position);
  644. }
  645. else {
  646. mat = BABYLON.Matrix.FromArray(node.matrix);
  647. }
  648. return mat;
  649. };
  650. /**
  651. * Returns the parent bone
  652. */
  653. var getParentBone = function (gltfRuntime, skins, jointName, newSkeleton) {
  654. // Try to find
  655. for (var i = 0; i < newSkeleton.bones.length; i++) {
  656. if (newSkeleton.bones[i].name === jointName) {
  657. return newSkeleton.bones[i];
  658. }
  659. }
  660. // Not found, search in gltf nodes
  661. var nodes = gltfRuntime.nodes;
  662. for (var nde in nodes) {
  663. var node = nodes[nde];
  664. if (!node.jointName) {
  665. continue;
  666. }
  667. var children = node.children;
  668. for (var i = 0; i < children.length; i++) {
  669. var child = gltfRuntime.nodes[children[i]];
  670. if (!child.jointName) {
  671. continue;
  672. }
  673. if (child.jointName === jointName) {
  674. var mat = configureBoneTransformation(node);
  675. var bone = new BABYLON.Bone(node.name || "", newSkeleton, getParentBone(gltfRuntime, skins, node.jointName, newSkeleton), mat);
  676. bone.id = nde;
  677. return bone;
  678. }
  679. }
  680. }
  681. return null;
  682. };
  683. /**
  684. * Returns the appropriate root node
  685. */
  686. var getNodeToRoot = function (nodesToRoot, id) {
  687. for (var i = 0; i < nodesToRoot.length; i++) {
  688. var nodeToRoot = nodesToRoot[i];
  689. for (var j = 0; j < nodeToRoot.node.children.length; j++) {
  690. var child = nodeToRoot.node.children[j];
  691. if (child === id) {
  692. return nodeToRoot.bone;
  693. }
  694. }
  695. }
  696. return null;
  697. };
  698. /**
  699. * Returns the node with the joint name
  700. */
  701. var getJointNode = function (gltfRuntime, jointName) {
  702. var nodes = gltfRuntime.nodes;
  703. var node = nodes[jointName];
  704. if (node) {
  705. return {
  706. node: node,
  707. id: jointName
  708. };
  709. }
  710. for (var nde in nodes) {
  711. node = nodes[nde];
  712. if (node.jointName === jointName) {
  713. return {
  714. node: node,
  715. id: nde
  716. };
  717. }
  718. }
  719. return null;
  720. };
  721. /**
  722. * Checks if a nodes is in joints
  723. */
  724. var nodeIsInJoints = function (skins, id) {
  725. for (var i = 0; i < skins.jointNames.length; i++) {
  726. if (skins.jointNames[i] === id) {
  727. return true;
  728. }
  729. }
  730. return false;
  731. };
  732. /**
  733. * Fills the nodes to root for bones and builds hierarchy
  734. */
  735. var getNodesToRoot = function (gltfRuntime, newSkeleton, skins, nodesToRoot) {
  736. // Creates nodes for root
  737. for (var nde in gltfRuntime.nodes) {
  738. var node = gltfRuntime.nodes[nde];
  739. var id = nde;
  740. if (!node.jointName || nodeIsInJoints(skins, node.jointName)) {
  741. continue;
  742. }
  743. // Create node to root bone
  744. var mat = configureBoneTransformation(node);
  745. var bone = new BABYLON.Bone(node.name || "", newSkeleton, null, mat);
  746. bone.id = id;
  747. nodesToRoot.push({ bone: bone, node: node, id: id });
  748. }
  749. // Parenting
  750. for (var i = 0; i < nodesToRoot.length; i++) {
  751. var nodeToRoot = nodesToRoot[i];
  752. var children = nodeToRoot.node.children;
  753. for (var j = 0; j < children.length; j++) {
  754. var child = null;
  755. for (var k = 0; k < nodesToRoot.length; k++) {
  756. if (nodesToRoot[k].id === children[j]) {
  757. child = nodesToRoot[k];
  758. break;
  759. }
  760. }
  761. if (child) {
  762. child.bone._parent = nodeToRoot.bone;
  763. nodeToRoot.bone.children.push(child.bone);
  764. }
  765. }
  766. }
  767. };
  768. /**
  769. * Imports a skeleton
  770. */
  771. var importSkeleton = function (gltfRuntime, skins, mesh, newSkeleton, id) {
  772. if (!newSkeleton) {
  773. newSkeleton = new BABYLON.Skeleton(skins.name || "", "", gltfRuntime.scene);
  774. }
  775. if (!skins.babylonSkeleton) {
  776. return newSkeleton;
  777. }
  778. // Find the root bones
  779. var nodesToRoot = [];
  780. var nodesToRootToAdd = [];
  781. getNodesToRoot(gltfRuntime, newSkeleton, skins, nodesToRoot);
  782. newSkeleton.bones = [];
  783. // Joints
  784. for (var i = 0; i < skins.jointNames.length; i++) {
  785. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  786. if (!jointNode) {
  787. continue;
  788. }
  789. var node = jointNode.node;
  790. if (!node) {
  791. BABYLON.Tools.Warn("Joint named " + skins.jointNames[i] + " does not exist");
  792. continue;
  793. }
  794. var id = jointNode.id;
  795. // Optimize, if the bone already exists...
  796. var existingBone = gltfRuntime.scene.getBoneByID(id);
  797. if (existingBone) {
  798. newSkeleton.bones.push(existingBone);
  799. continue;
  800. }
  801. // Search for parent bone
  802. var foundBone = false;
  803. var parentBone = null;
  804. for (var j = 0; j < i; j++) {
  805. var jointNode_1 = getJointNode(gltfRuntime, skins.jointNames[j]);
  806. if (!jointNode_1) {
  807. continue;
  808. }
  809. var joint = jointNode_1.node;
  810. if (!joint) {
  811. BABYLON.Tools.Warn("Joint named " + skins.jointNames[j] + " does not exist when looking for parent");
  812. continue;
  813. }
  814. var children = joint.children;
  815. if (!children) {
  816. continue;
  817. }
  818. foundBone = false;
  819. for (var k = 0; k < children.length; k++) {
  820. if (children[k] === id) {
  821. parentBone = getParentBone(gltfRuntime, skins, skins.jointNames[j], newSkeleton);
  822. foundBone = true;
  823. break;
  824. }
  825. }
  826. if (foundBone) {
  827. break;
  828. }
  829. }
  830. // Create bone
  831. var mat = configureBoneTransformation(node);
  832. if (!parentBone && nodesToRoot.length > 0) {
  833. parentBone = getNodeToRoot(nodesToRoot, id);
  834. if (parentBone) {
  835. if (nodesToRootToAdd.indexOf(parentBone) === -1) {
  836. nodesToRootToAdd.push(parentBone);
  837. }
  838. }
  839. }
  840. var bone = new BABYLON.Bone(node.jointName || "", newSkeleton, parentBone, mat);
  841. bone.id = id;
  842. }
  843. // Polish
  844. var bones = newSkeleton.bones;
  845. newSkeleton.bones = [];
  846. for (var i = 0; i < skins.jointNames.length; i++) {
  847. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  848. if (!jointNode) {
  849. continue;
  850. }
  851. for (var j = 0; j < bones.length; j++) {
  852. if (bones[j].id === jointNode.id) {
  853. newSkeleton.bones.push(bones[j]);
  854. break;
  855. }
  856. }
  857. }
  858. newSkeleton.prepare();
  859. // Finish
  860. for (var i = 0; i < nodesToRootToAdd.length; i++) {
  861. newSkeleton.bones.push(nodesToRootToAdd[i]);
  862. }
  863. return newSkeleton;
  864. };
  865. /**
  866. * Imports a mesh and its geometries
  867. */
  868. var importMesh = function (gltfRuntime, node, meshes, id, newMesh) {
  869. if (!newMesh) {
  870. newMesh = new BABYLON.Mesh(node.name || "", gltfRuntime.scene);
  871. newMesh.id = id;
  872. }
  873. if (!node.babylonNode) {
  874. return newMesh;
  875. }
  876. var subMaterials = [];
  877. var vertexData = new BABYLON.VertexData();
  878. var geometry = new BABYLON.Geometry(id, gltfRuntime.scene, vertexData, false, newMesh);
  879. var verticesStarts = new Array();
  880. var verticesCounts = new Array();
  881. var indexStarts = new Array();
  882. var indexCounts = new Array();
  883. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  884. var meshID = meshes[meshIndex];
  885. var mesh = gltfRuntime.meshes[meshID];
  886. if (!mesh) {
  887. continue;
  888. }
  889. // Positions, normals and UVs
  890. for (var i = 0; i < mesh.primitives.length; i++) {
  891. // Temporary vertex data
  892. var tempVertexData = new BABYLON.VertexData();
  893. var primitive = mesh.primitives[i];
  894. if (primitive.mode !== 4) {
  895. // continue;
  896. }
  897. var attributes = primitive.attributes;
  898. var accessor = null;
  899. var buffer = null;
  900. // Set positions, normal and uvs
  901. for (var semantic in attributes) {
  902. // Link accessor and buffer view
  903. accessor = gltfRuntime.accessors[attributes[semantic]];
  904. buffer = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  905. if (semantic === "NORMAL") {
  906. tempVertexData.normals = new Float32Array(buffer.length);
  907. tempVertexData.normals.set(buffer);
  908. }
  909. else if (semantic === "POSITION") {
  910. if (BABYLON.GLTFFileLoader.HomogeneousCoordinates) {
  911. tempVertexData.positions = new Float32Array(buffer.length - buffer.length / 4);
  912. for (var j = 0; j < buffer.length; j += 4) {
  913. tempVertexData.positions[j] = buffer[j];
  914. tempVertexData.positions[j + 1] = buffer[j + 1];
  915. tempVertexData.positions[j + 2] = buffer[j + 2];
  916. }
  917. }
  918. else {
  919. tempVertexData.positions = new Float32Array(buffer.length);
  920. tempVertexData.positions.set(buffer);
  921. }
  922. verticesCounts.push(tempVertexData.positions.length);
  923. }
  924. else if (semantic.indexOf("TEXCOORD_") !== -1) {
  925. var channel = Number(semantic.split("_")[1]);
  926. var uvKind = BABYLON.VertexBuffer.UVKind + (channel === 0 ? "" : (channel + 1));
  927. var uvs = new Float32Array(buffer.length);
  928. uvs.set(buffer);
  929. normalizeUVs(uvs);
  930. tempVertexData.set(uvs, uvKind);
  931. }
  932. else if (semantic === "JOINT") {
  933. tempVertexData.matricesIndices = new Float32Array(buffer.length);
  934. tempVertexData.matricesIndices.set(buffer);
  935. }
  936. else if (semantic === "WEIGHT") {
  937. tempVertexData.matricesWeights = new Float32Array(buffer.length);
  938. tempVertexData.matricesWeights.set(buffer);
  939. }
  940. else if (semantic === "COLOR") {
  941. tempVertexData.colors = new Float32Array(buffer.length);
  942. tempVertexData.colors.set(buffer);
  943. }
  944. }
  945. // Indices
  946. accessor = gltfRuntime.accessors[primitive.indices];
  947. if (accessor) {
  948. buffer = GLTF1.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  949. tempVertexData.indices = new Int32Array(buffer.length);
  950. tempVertexData.indices.set(buffer);
  951. indexCounts.push(tempVertexData.indices.length);
  952. }
  953. else {
  954. // Set indices on the fly
  955. var indices = [];
  956. for (var j = 0; j < tempVertexData.positions.length / 3; j++) {
  957. indices.push(j);
  958. }
  959. tempVertexData.indices = new Int32Array(indices);
  960. indexCounts.push(tempVertexData.indices.length);
  961. }
  962. vertexData.merge(tempVertexData);
  963. // Sub material
  964. var material_1 = gltfRuntime.scene.getMaterialByID(primitive.material);
  965. subMaterials.push(material_1 === null ? GLTF1.GLTFUtils.GetDefaultMaterial(gltfRuntime.scene) : material_1);
  966. // Update vertices start and index start
  967. verticesStarts.push(verticesStarts.length === 0 ? 0 : verticesStarts[verticesStarts.length - 1] + verticesCounts[verticesCounts.length - 2]);
  968. indexStarts.push(indexStarts.length === 0 ? 0 : indexStarts[indexStarts.length - 1] + indexCounts[indexCounts.length - 2]);
  969. }
  970. }
  971. var material;
  972. if (subMaterials.length > 1) {
  973. material = new BABYLON.MultiMaterial("multimat" + id, gltfRuntime.scene);
  974. material.subMaterials = subMaterials;
  975. }
  976. else {
  977. material = new BABYLON.StandardMaterial("multimat" + id, gltfRuntime.scene);
  978. }
  979. if (subMaterials.length === 1) {
  980. material = subMaterials[0];
  981. }
  982. if (!newMesh.material) {
  983. newMesh.material = material;
  984. }
  985. // Apply geometry
  986. geometry.setAllVerticesData(vertexData, false);
  987. newMesh.computeWorldMatrix(true);
  988. // Apply submeshes
  989. newMesh.subMeshes = [];
  990. var index = 0;
  991. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  992. var meshID = meshes[meshIndex];
  993. var mesh = gltfRuntime.meshes[meshID];
  994. if (!mesh) {
  995. continue;
  996. }
  997. for (var i = 0; i < mesh.primitives.length; i++) {
  998. if (mesh.primitives[i].mode !== 4) {
  999. //continue;
  1000. }
  1001. BABYLON.SubMesh.AddToMesh(index, verticesStarts[index], verticesCounts[index], indexStarts[index], indexCounts[index], newMesh, newMesh, true);
  1002. index++;
  1003. }
  1004. }
  1005. // Finish
  1006. return newMesh;
  1007. };
  1008. /**
  1009. * Configure node transformation from position, rotation and scaling
  1010. */
  1011. var configureNode = function (newNode, position, rotation, scaling) {
  1012. if (newNode.position) {
  1013. newNode.position = position;
  1014. }
  1015. if (newNode.rotationQuaternion || newNode.rotation) {
  1016. newNode.rotationQuaternion = rotation;
  1017. }
  1018. if (newNode.scaling) {
  1019. newNode.scaling = scaling;
  1020. }
  1021. };
  1022. /**
  1023. * Configures node from transformation matrix
  1024. */
  1025. var configureNodeFromMatrix = function (newNode, node, parent) {
  1026. if (node.matrix) {
  1027. var position = new BABYLON.Vector3(0, 0, 0);
  1028. var rotation = new BABYLON.Quaternion();
  1029. var scaling = new BABYLON.Vector3(0, 0, 0);
  1030. var mat = BABYLON.Matrix.FromArray(node.matrix);
  1031. mat.decompose(scaling, rotation, position);
  1032. configureNode(newNode, position, rotation, scaling);
  1033. }
  1034. else if (node.translation && node.rotation && node.scale) {
  1035. configureNode(newNode, BABYLON.Vector3.FromArray(node.translation), BABYLON.Quaternion.FromArray(node.rotation), BABYLON.Vector3.FromArray(node.scale));
  1036. }
  1037. newNode.computeWorldMatrix(true);
  1038. };
  1039. /**
  1040. * Imports a node
  1041. */
  1042. var importNode = function (gltfRuntime, node, id, parent) {
  1043. var lastNode = null;
  1044. if (gltfRuntime.importOnlyMeshes && (node.skin || node.meshes)) {
  1045. if (gltfRuntime.importMeshesNames && gltfRuntime.importMeshesNames.length > 0 && gltfRuntime.importMeshesNames.indexOf(node.name || "") === -1) {
  1046. return null;
  1047. }
  1048. }
  1049. // Meshes
  1050. if (node.skin) {
  1051. if (node.meshes) {
  1052. var skin = gltfRuntime.skins[node.skin];
  1053. var newMesh = importMesh(gltfRuntime, node, node.meshes, id, node.babylonNode);
  1054. newMesh.skeleton = gltfRuntime.scene.getLastSkeletonByID(node.skin);
  1055. if (newMesh.skeleton === null) {
  1056. newMesh.skeleton = importSkeleton(gltfRuntime, skin, newMesh, skin.babylonSkeleton, node.skin);
  1057. if (!skin.babylonSkeleton) {
  1058. skin.babylonSkeleton = newMesh.skeleton;
  1059. }
  1060. }
  1061. lastNode = newMesh;
  1062. }
  1063. }
  1064. else if (node.meshes) {
  1065. /**
  1066. * Improve meshes property
  1067. */
  1068. var newMesh = importMesh(gltfRuntime, node, node.mesh ? [node.mesh] : node.meshes, id, node.babylonNode);
  1069. lastNode = newMesh;
  1070. }
  1071. else if (node.light && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  1072. var light = gltfRuntime.lights[node.light];
  1073. if (light) {
  1074. if (light.type === "ambient") {
  1075. var ambienLight = light[light.type];
  1076. var hemiLight = new BABYLON.HemisphericLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1077. hemiLight.name = node.name || "";
  1078. if (ambienLight.color) {
  1079. hemiLight.diffuse = BABYLON.Color3.FromArray(ambienLight.color);
  1080. }
  1081. lastNode = hemiLight;
  1082. }
  1083. else if (light.type === "directional") {
  1084. var directionalLight = light[light.type];
  1085. var dirLight = new BABYLON.DirectionalLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1086. dirLight.name = node.name || "";
  1087. if (directionalLight.color) {
  1088. dirLight.diffuse = BABYLON.Color3.FromArray(directionalLight.color);
  1089. }
  1090. lastNode = dirLight;
  1091. }
  1092. else if (light.type === "point") {
  1093. var pointLight = light[light.type];
  1094. var ptLight = new BABYLON.PointLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1095. ptLight.name = node.name || "";
  1096. if (pointLight.color) {
  1097. ptLight.diffuse = BABYLON.Color3.FromArray(pointLight.color);
  1098. }
  1099. lastNode = ptLight;
  1100. }
  1101. else if (light.type === "spot") {
  1102. var spotLight = light[light.type];
  1103. var spLight = new BABYLON.SpotLight(node.light, BABYLON.Vector3.Zero(), BABYLON.Vector3.Zero(), 0, 0, gltfRuntime.scene);
  1104. spLight.name = node.name || "";
  1105. if (spotLight.color) {
  1106. spLight.diffuse = BABYLON.Color3.FromArray(spotLight.color);
  1107. }
  1108. if (spotLight.fallOfAngle) {
  1109. spLight.angle = spotLight.fallOfAngle;
  1110. }
  1111. if (spotLight.fallOffExponent) {
  1112. spLight.exponent = spotLight.fallOffExponent;
  1113. }
  1114. lastNode = spLight;
  1115. }
  1116. }
  1117. }
  1118. else if (node.camera && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  1119. var camera = gltfRuntime.cameras[node.camera];
  1120. if (camera) {
  1121. if (camera.type === "orthographic") {
  1122. var orthoCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1123. orthoCamera.name = node.name || "";
  1124. orthoCamera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
  1125. orthoCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  1126. lastNode = orthoCamera;
  1127. }
  1128. else if (camera.type === "perspective") {
  1129. var perspectiveCamera = camera[camera.type];
  1130. var persCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  1131. persCamera.name = node.name || "";
  1132. persCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  1133. if (!perspectiveCamera.aspectRatio) {
  1134. perspectiveCamera.aspectRatio = gltfRuntime.scene.getEngine().getRenderWidth() / gltfRuntime.scene.getEngine().getRenderHeight();
  1135. }
  1136. if (perspectiveCamera.znear && perspectiveCamera.zfar) {
  1137. persCamera.maxZ = perspectiveCamera.zfar;
  1138. persCamera.minZ = perspectiveCamera.znear;
  1139. }
  1140. lastNode = persCamera;
  1141. }
  1142. }
  1143. }
  1144. // Empty node
  1145. if (!node.jointName) {
  1146. if (node.babylonNode) {
  1147. return node.babylonNode;
  1148. }
  1149. else if (lastNode === null) {
  1150. var dummy = new BABYLON.Mesh(node.name || "", gltfRuntime.scene);
  1151. node.babylonNode = dummy;
  1152. lastNode = dummy;
  1153. }
  1154. }
  1155. if (lastNode !== null) {
  1156. if (node.matrix && lastNode instanceof BABYLON.Mesh) {
  1157. configureNodeFromMatrix(lastNode, node, parent);
  1158. }
  1159. else {
  1160. var translation = node.translation || [0, 0, 0];
  1161. var rotation = node.rotation || [0, 0, 0, 1];
  1162. var scale = node.scale || [1, 1, 1];
  1163. configureNode(lastNode, BABYLON.Vector3.FromArray(translation), BABYLON.Quaternion.FromArray(rotation), BABYLON.Vector3.FromArray(scale));
  1164. }
  1165. lastNode.updateCache(true);
  1166. node.babylonNode = lastNode;
  1167. }
  1168. return lastNode;
  1169. };
  1170. /**
  1171. * Traverses nodes and creates them
  1172. */
  1173. var traverseNodes = function (gltfRuntime, id, parent, meshIncluded) {
  1174. if (meshIncluded === void 0) { meshIncluded = false; }
  1175. var node = gltfRuntime.nodes[id];
  1176. var newNode = null;
  1177. if (gltfRuntime.importOnlyMeshes && !meshIncluded && gltfRuntime.importMeshesNames) {
  1178. if (gltfRuntime.importMeshesNames.indexOf(node.name || "") !== -1 || gltfRuntime.importMeshesNames.length === 0) {
  1179. meshIncluded = true;
  1180. }
  1181. else {
  1182. meshIncluded = false;
  1183. }
  1184. }
  1185. else {
  1186. meshIncluded = true;
  1187. }
  1188. if (!node.jointName && meshIncluded) {
  1189. newNode = importNode(gltfRuntime, node, id, parent);
  1190. if (newNode !== null) {
  1191. newNode.id = id;
  1192. newNode.parent = parent;
  1193. }
  1194. }
  1195. if (node.children) {
  1196. for (var i = 0; i < node.children.length; i++) {
  1197. traverseNodes(gltfRuntime, node.children[i], newNode, meshIncluded);
  1198. }
  1199. }
  1200. };
  1201. /**
  1202. * do stuff after buffers, shaders are loaded (e.g. hook up materials, load animations, etc.)
  1203. */
  1204. var postLoad = function (gltfRuntime) {
  1205. // Nodes
  1206. var currentScene = gltfRuntime.currentScene;
  1207. if (currentScene) {
  1208. for (var i = 0; i < currentScene.nodes.length; i++) {
  1209. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1210. }
  1211. }
  1212. else {
  1213. for (var thing in gltfRuntime.scenes) {
  1214. currentScene = gltfRuntime.scenes[thing];
  1215. for (var i = 0; i < currentScene.nodes.length; i++) {
  1216. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1217. }
  1218. }
  1219. }
  1220. // Set animations
  1221. loadAnimations(gltfRuntime);
  1222. for (var i = 0; i < gltfRuntime.scene.skeletons.length; i++) {
  1223. var skeleton = gltfRuntime.scene.skeletons[i];
  1224. gltfRuntime.scene.beginAnimation(skeleton, 0, Number.MAX_VALUE, true, 1.0);
  1225. }
  1226. };
  1227. /**
  1228. * onBind shaderrs callback to set uniforms and matrices
  1229. */
  1230. var onBindShaderMaterial = function (mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess) {
  1231. var materialValues = material.values || technique.parameters;
  1232. for (var unif in unTreatedUniforms) {
  1233. var uniform = unTreatedUniforms[unif];
  1234. var type = uniform.type;
  1235. if (type === GLTF1.EParameterType.FLOAT_MAT2 || type === GLTF1.EParameterType.FLOAT_MAT3 || type === GLTF1.EParameterType.FLOAT_MAT4) {
  1236. if (uniform.semantic && !uniform.source && !uniform.node) {
  1237. GLTF1.GLTFUtils.SetMatrix(gltfRuntime.scene, mesh, uniform, unif, shaderMaterial.getEffect());
  1238. }
  1239. else if (uniform.semantic && (uniform.source || uniform.node)) {
  1240. var source = gltfRuntime.scene.getNodeByName(uniform.source || uniform.node || "");
  1241. if (source === null) {
  1242. source = gltfRuntime.scene.getNodeByID(uniform.source || uniform.node || "");
  1243. }
  1244. if (source === null) {
  1245. continue;
  1246. }
  1247. GLTF1.GLTFUtils.SetMatrix(gltfRuntime.scene, source, uniform, unif, shaderMaterial.getEffect());
  1248. }
  1249. }
  1250. else {
  1251. var value = materialValues[technique.uniforms[unif]];
  1252. if (!value) {
  1253. continue;
  1254. }
  1255. if (type === GLTF1.EParameterType.SAMPLER_2D) {
  1256. var texture = gltfRuntime.textures[material.values ? value : uniform.value].babylonTexture;
  1257. if (texture === null || texture === undefined) {
  1258. continue;
  1259. }
  1260. shaderMaterial.getEffect().setTexture(unif, texture);
  1261. }
  1262. else {
  1263. GLTF1.GLTFUtils.SetUniform((shaderMaterial.getEffect()), unif, value, type);
  1264. }
  1265. }
  1266. }
  1267. onSuccess(shaderMaterial);
  1268. };
  1269. /**
  1270. * Prepare uniforms to send the only one time
  1271. * Loads the appropriate textures
  1272. */
  1273. var prepareShaderMaterialUniforms = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms) {
  1274. var materialValues = material.values || technique.parameters;
  1275. var techniqueUniforms = technique.uniforms;
  1276. /**
  1277. * Prepare values here (not matrices)
  1278. */
  1279. for (var unif in unTreatedUniforms) {
  1280. var uniform = unTreatedUniforms[unif];
  1281. var type = uniform.type;
  1282. var value = materialValues[techniqueUniforms[unif]];
  1283. if (value === undefined) {
  1284. // In case the value is the same for all materials
  1285. value = uniform.value;
  1286. }
  1287. if (!value) {
  1288. continue;
  1289. }
  1290. var onLoadTexture = function (uniformName) {
  1291. return function (texture) {
  1292. if (uniform.value && uniformName) {
  1293. // Static uniform
  1294. shaderMaterial.setTexture(uniformName, texture);
  1295. delete unTreatedUniforms[uniformName];
  1296. }
  1297. };
  1298. };
  1299. // Texture (sampler2D)
  1300. if (type === GLTF1.EParameterType.SAMPLER_2D) {
  1301. GLTF1.GLTFLoaderExtension.LoadTextureAsync(gltfRuntime, material.values ? value : uniform.value, onLoadTexture(unif), function () { return onLoadTexture(null); });
  1302. }
  1303. else {
  1304. if (uniform.value && GLTF1.GLTFUtils.SetUniform(shaderMaterial, unif, material.values ? value : uniform.value, type)) {
  1305. // Static uniform
  1306. delete unTreatedUniforms[unif];
  1307. }
  1308. }
  1309. }
  1310. };
  1311. /**
  1312. * Shader compilation failed
  1313. */
  1314. var onShaderCompileError = function (program, shaderMaterial, onError) {
  1315. return function (effect, error) {
  1316. shaderMaterial.dispose(true);
  1317. onError("Cannot compile program named " + program.name + ". Error: " + error + ". Default material will be applied");
  1318. };
  1319. };
  1320. /**
  1321. * Shader compilation success
  1322. */
  1323. var onShaderCompileSuccess = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess) {
  1324. return function (_) {
  1325. prepareShaderMaterialUniforms(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms);
  1326. shaderMaterial.onBind = function (mesh) {
  1327. onBindShaderMaterial(mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess);
  1328. };
  1329. };
  1330. };
  1331. /**
  1332. * Returns the appropriate uniform if already handled by babylon
  1333. */
  1334. var parseShaderUniforms = function (tokenizer, technique, unTreatedUniforms) {
  1335. for (var unif in technique.uniforms) {
  1336. var uniform = technique.uniforms[unif];
  1337. var uniformParameter = technique.parameters[uniform];
  1338. if (tokenizer.currentIdentifier === unif) {
  1339. if (uniformParameter.semantic && !uniformParameter.source && !uniformParameter.node) {
  1340. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1341. if (transformIndex !== -1) {
  1342. delete unTreatedUniforms[unif];
  1343. return babylonTransforms[transformIndex];
  1344. }
  1345. }
  1346. }
  1347. }
  1348. return tokenizer.currentIdentifier;
  1349. };
  1350. /**
  1351. * All shaders loaded. Create materials one by one
  1352. */
  1353. var importMaterials = function (gltfRuntime) {
  1354. // Create materials
  1355. for (var mat in gltfRuntime.materials) {
  1356. GLTF1.GLTFLoaderExtension.LoadMaterialAsync(gltfRuntime, mat, function (material) { }, function () { });
  1357. }
  1358. };
  1359. /**
  1360. * Implementation of the base glTF spec
  1361. */
  1362. var GLTFLoaderBase = /** @class */ (function () {
  1363. function GLTFLoaderBase() {
  1364. }
  1365. GLTFLoaderBase.CreateRuntime = function (parsedData, scene, rootUrl) {
  1366. var gltfRuntime = {
  1367. extensions: {},
  1368. accessors: {},
  1369. buffers: {},
  1370. bufferViews: {},
  1371. meshes: {},
  1372. lights: {},
  1373. cameras: {},
  1374. nodes: {},
  1375. images: {},
  1376. textures: {},
  1377. shaders: {},
  1378. programs: {},
  1379. samplers: {},
  1380. techniques: {},
  1381. materials: {},
  1382. animations: {},
  1383. skins: {},
  1384. extensionsUsed: [],
  1385. scenes: {},
  1386. buffersCount: 0,
  1387. shaderscount: 0,
  1388. scene: scene,
  1389. rootUrl: rootUrl,
  1390. loadedBufferCount: 0,
  1391. loadedBufferViews: {},
  1392. loadedShaderCount: 0,
  1393. importOnlyMeshes: false,
  1394. dummyNodes: []
  1395. };
  1396. // Parse
  1397. if (parsedData.extensions) {
  1398. parseObject(parsedData.extensions, "extensions", gltfRuntime);
  1399. }
  1400. if (parsedData.extensionsUsed) {
  1401. parseObject(parsedData.extensionsUsed, "extensionsUsed", gltfRuntime);
  1402. }
  1403. if (parsedData.buffers) {
  1404. parseBuffers(parsedData.buffers, gltfRuntime);
  1405. }
  1406. if (parsedData.bufferViews) {
  1407. parseObject(parsedData.bufferViews, "bufferViews", gltfRuntime);
  1408. }
  1409. if (parsedData.accessors) {
  1410. parseObject(parsedData.accessors, "accessors", gltfRuntime);
  1411. }
  1412. if (parsedData.meshes) {
  1413. parseObject(parsedData.meshes, "meshes", gltfRuntime);
  1414. }
  1415. if (parsedData.lights) {
  1416. parseObject(parsedData.lights, "lights", gltfRuntime);
  1417. }
  1418. if (parsedData.cameras) {
  1419. parseObject(parsedData.cameras, "cameras", gltfRuntime);
  1420. }
  1421. if (parsedData.nodes) {
  1422. parseObject(parsedData.nodes, "nodes", gltfRuntime);
  1423. }
  1424. if (parsedData.images) {
  1425. parseObject(parsedData.images, "images", gltfRuntime);
  1426. }
  1427. if (parsedData.textures) {
  1428. parseObject(parsedData.textures, "textures", gltfRuntime);
  1429. }
  1430. if (parsedData.shaders) {
  1431. parseShaders(parsedData.shaders, gltfRuntime);
  1432. }
  1433. if (parsedData.programs) {
  1434. parseObject(parsedData.programs, "programs", gltfRuntime);
  1435. }
  1436. if (parsedData.samplers) {
  1437. parseObject(parsedData.samplers, "samplers", gltfRuntime);
  1438. }
  1439. if (parsedData.techniques) {
  1440. parseObject(parsedData.techniques, "techniques", gltfRuntime);
  1441. }
  1442. if (parsedData.materials) {
  1443. parseObject(parsedData.materials, "materials", gltfRuntime);
  1444. }
  1445. if (parsedData.animations) {
  1446. parseObject(parsedData.animations, "animations", gltfRuntime);
  1447. }
  1448. if (parsedData.skins) {
  1449. parseObject(parsedData.skins, "skins", gltfRuntime);
  1450. }
  1451. if (parsedData.scenes) {
  1452. gltfRuntime.scenes = parsedData.scenes;
  1453. }
  1454. if (parsedData.scene && parsedData.scenes) {
  1455. gltfRuntime.currentScene = parsedData.scenes[parsedData.scene];
  1456. }
  1457. return gltfRuntime;
  1458. };
  1459. GLTFLoaderBase.LoadBufferAsync = function (gltfRuntime, id, onSuccess, onError, onProgress) {
  1460. var buffer = gltfRuntime.buffers[id];
  1461. if (GLTF1.GLTFUtils.IsBase64(buffer.uri)) {
  1462. setTimeout(function () { return onSuccess(new Uint8Array(GLTF1.GLTFUtils.DecodeBase64(buffer.uri))); });
  1463. }
  1464. else {
  1465. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + buffer.uri, function (data) { return onSuccess(new Uint8Array(data)); }, onProgress, undefined, true, function (request) {
  1466. if (request) {
  1467. onError(request.status + " " + request.statusText);
  1468. }
  1469. });
  1470. }
  1471. };
  1472. GLTFLoaderBase.LoadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  1473. var texture = gltfRuntime.textures[id];
  1474. if (!texture || !texture.source) {
  1475. onError("");
  1476. return;
  1477. }
  1478. if (texture.babylonTexture) {
  1479. onSuccess(null);
  1480. return;
  1481. }
  1482. var source = gltfRuntime.images[texture.source];
  1483. if (GLTF1.GLTFUtils.IsBase64(source.uri)) {
  1484. setTimeout(function () { return onSuccess(new Uint8Array(GLTF1.GLTFUtils.DecodeBase64(source.uri))); });
  1485. }
  1486. else {
  1487. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + source.uri, function (data) { return onSuccess(new Uint8Array(data)); }, undefined, undefined, true, function (request) {
  1488. if (request) {
  1489. onError(request.status + " " + request.statusText);
  1490. }
  1491. });
  1492. }
  1493. };
  1494. GLTFLoaderBase.CreateTextureAsync = function (gltfRuntime, id, buffer, onSuccess, onError) {
  1495. var texture = gltfRuntime.textures[id];
  1496. if (texture.babylonTexture) {
  1497. onSuccess(texture.babylonTexture);
  1498. return;
  1499. }
  1500. var sampler = gltfRuntime.samplers[texture.sampler];
  1501. var createMipMaps = (sampler.minFilter === GLTF1.ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  1502. (sampler.minFilter === GLTF1.ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  1503. (sampler.minFilter === GLTF1.ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  1504. (sampler.minFilter === GLTF1.ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  1505. var samplingMode = BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  1506. var blob = new Blob([buffer]);
  1507. var blobURL = URL.createObjectURL(blob);
  1508. var revokeBlobURL = function () { return URL.revokeObjectURL(blobURL); };
  1509. var newTexture = new BABYLON.Texture(blobURL, gltfRuntime.scene, !createMipMaps, true, samplingMode, revokeBlobURL, revokeBlobURL);
  1510. if (sampler.wrapS !== undefined) {
  1511. newTexture.wrapU = GLTF1.GLTFUtils.GetWrapMode(sampler.wrapS);
  1512. }
  1513. if (sampler.wrapT !== undefined) {
  1514. newTexture.wrapV = GLTF1.GLTFUtils.GetWrapMode(sampler.wrapT);
  1515. }
  1516. newTexture.name = id;
  1517. texture.babylonTexture = newTexture;
  1518. onSuccess(newTexture);
  1519. };
  1520. GLTFLoaderBase.LoadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  1521. var shader = gltfRuntime.shaders[id];
  1522. if (GLTF1.GLTFUtils.IsBase64(shader.uri)) {
  1523. var shaderString = atob(shader.uri.split(",")[1]);
  1524. onSuccess(shaderString);
  1525. }
  1526. else {
  1527. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + shader.uri, onSuccess, undefined, undefined, false, function (request) {
  1528. if (request) {
  1529. onError(request.status + " " + request.statusText);
  1530. }
  1531. });
  1532. }
  1533. };
  1534. GLTFLoaderBase.LoadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  1535. var material = gltfRuntime.materials[id];
  1536. if (!material.technique) {
  1537. if (onError) {
  1538. onError("No technique found.");
  1539. }
  1540. return;
  1541. }
  1542. var technique = gltfRuntime.techniques[material.technique];
  1543. if (!technique) {
  1544. var defaultMaterial = new BABYLON.StandardMaterial(id, gltfRuntime.scene);
  1545. defaultMaterial.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  1546. defaultMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1547. onSuccess(defaultMaterial);
  1548. return;
  1549. }
  1550. var program = gltfRuntime.programs[technique.program];
  1551. var states = technique.states;
  1552. var vertexShader = BABYLON.Effect.ShadersStore[program.vertexShader + "VertexShader"];
  1553. var pixelShader = BABYLON.Effect.ShadersStore[program.fragmentShader + "PixelShader"];
  1554. var newVertexShader = "";
  1555. var newPixelShader = "";
  1556. var vertexTokenizer = new Tokenizer(vertexShader);
  1557. var pixelTokenizer = new Tokenizer(pixelShader);
  1558. var unTreatedUniforms = {};
  1559. var uniforms = [];
  1560. var attributes = [];
  1561. var samplers = [];
  1562. // Fill uniform, sampler2D and attributes
  1563. for (var unif in technique.uniforms) {
  1564. var uniform = technique.uniforms[unif];
  1565. var uniformParameter = technique.parameters[uniform];
  1566. unTreatedUniforms[unif] = uniformParameter;
  1567. if (uniformParameter.semantic && !uniformParameter.node && !uniformParameter.source) {
  1568. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1569. if (transformIndex !== -1) {
  1570. uniforms.push(babylonTransforms[transformIndex]);
  1571. delete unTreatedUniforms[unif];
  1572. }
  1573. else {
  1574. uniforms.push(unif);
  1575. }
  1576. }
  1577. else if (uniformParameter.type === GLTF1.EParameterType.SAMPLER_2D) {
  1578. samplers.push(unif);
  1579. }
  1580. else {
  1581. uniforms.push(unif);
  1582. }
  1583. }
  1584. for (var attr in technique.attributes) {
  1585. var attribute = technique.attributes[attr];
  1586. var attributeParameter = technique.parameters[attribute];
  1587. if (attributeParameter.semantic) {
  1588. attributes.push(getAttribute(attributeParameter));
  1589. }
  1590. }
  1591. // Configure vertex shader
  1592. while (!vertexTokenizer.isEnd() && vertexTokenizer.getNextToken()) {
  1593. var tokenType = vertexTokenizer.currentToken;
  1594. if (tokenType !== ETokenType.IDENTIFIER) {
  1595. newVertexShader += vertexTokenizer.currentString;
  1596. continue;
  1597. }
  1598. var foundAttribute = false;
  1599. for (var attr in technique.attributes) {
  1600. var attribute = technique.attributes[attr];
  1601. var attributeParameter = technique.parameters[attribute];
  1602. if (vertexTokenizer.currentIdentifier === attr && attributeParameter.semantic) {
  1603. newVertexShader += getAttribute(attributeParameter);
  1604. foundAttribute = true;
  1605. break;
  1606. }
  1607. }
  1608. if (foundAttribute) {
  1609. continue;
  1610. }
  1611. newVertexShader += parseShaderUniforms(vertexTokenizer, technique, unTreatedUniforms);
  1612. }
  1613. // Configure pixel shader
  1614. while (!pixelTokenizer.isEnd() && pixelTokenizer.getNextToken()) {
  1615. var tokenType = pixelTokenizer.currentToken;
  1616. if (tokenType !== ETokenType.IDENTIFIER) {
  1617. newPixelShader += pixelTokenizer.currentString;
  1618. continue;
  1619. }
  1620. newPixelShader += parseShaderUniforms(pixelTokenizer, technique, unTreatedUniforms);
  1621. }
  1622. // Create shader material
  1623. var shaderPath = {
  1624. vertex: program.vertexShader + id,
  1625. fragment: program.fragmentShader + id
  1626. };
  1627. var options = {
  1628. attributes: attributes,
  1629. uniforms: uniforms,
  1630. samplers: samplers,
  1631. needAlphaBlending: states && states.enable && states.enable.indexOf(3042) !== -1
  1632. };
  1633. BABYLON.Effect.ShadersStore[program.vertexShader + id + "VertexShader"] = newVertexShader;
  1634. BABYLON.Effect.ShadersStore[program.fragmentShader + id + "PixelShader"] = newPixelShader;
  1635. var shaderMaterial = new BABYLON.ShaderMaterial(id, gltfRuntime.scene, shaderPath, options);
  1636. shaderMaterial.onError = onShaderCompileError(program, shaderMaterial, onError);
  1637. shaderMaterial.onCompiled = onShaderCompileSuccess(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess);
  1638. shaderMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  1639. if (states && states.functions) {
  1640. var functions = states.functions;
  1641. if (functions.cullFace && functions.cullFace[0] !== GLTF1.ECullingType.BACK) {
  1642. shaderMaterial.backFaceCulling = false;
  1643. }
  1644. var blendFunc = functions.blendFuncSeparate;
  1645. if (blendFunc) {
  1646. if (blendFunc[0] === GLTF1.EBlendingFunction.SRC_ALPHA && blendFunc[1] === GLTF1.EBlendingFunction.ONE_MINUS_SRC_ALPHA && blendFunc[2] === GLTF1.EBlendingFunction.ONE && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  1647. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_COMBINE;
  1648. }
  1649. else if (blendFunc[0] === GLTF1.EBlendingFunction.ONE && blendFunc[1] === GLTF1.EBlendingFunction.ONE && blendFunc[2] === GLTF1.EBlendingFunction.ZERO && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  1650. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ONEONE;
  1651. }
  1652. else if (blendFunc[0] === GLTF1.EBlendingFunction.SRC_ALPHA && blendFunc[1] === GLTF1.EBlendingFunction.ONE && blendFunc[2] === GLTF1.EBlendingFunction.ZERO && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  1653. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ADD;
  1654. }
  1655. else if (blendFunc[0] === GLTF1.EBlendingFunction.ZERO && blendFunc[1] === GLTF1.EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === GLTF1.EBlendingFunction.ONE && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  1656. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_SUBTRACT;
  1657. }
  1658. else if (blendFunc[0] === GLTF1.EBlendingFunction.DST_COLOR && blendFunc[1] === GLTF1.EBlendingFunction.ZERO && blendFunc[2] === GLTF1.EBlendingFunction.ONE && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  1659. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MULTIPLY;
  1660. }
  1661. else if (blendFunc[0] === GLTF1.EBlendingFunction.SRC_ALPHA && blendFunc[1] === GLTF1.EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === GLTF1.EBlendingFunction.ONE && blendFunc[3] === GLTF1.EBlendingFunction.ONE) {
  1662. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MAXIMIZED;
  1663. }
  1664. }
  1665. }
  1666. };
  1667. return GLTFLoaderBase;
  1668. }());
  1669. GLTF1.GLTFLoaderBase = GLTFLoaderBase;
  1670. /**
  1671. * glTF V1 Loader
  1672. */
  1673. var GLTFLoader = /** @class */ (function () {
  1674. function GLTFLoader() {
  1675. }
  1676. GLTFLoader.RegisterExtension = function (extension) {
  1677. if (GLTFLoader.Extensions[extension.name]) {
  1678. BABYLON.Tools.Error("Tool with the same name \"" + extension.name + "\" already exists");
  1679. return;
  1680. }
  1681. GLTFLoader.Extensions[extension.name] = extension;
  1682. };
  1683. GLTFLoader.prototype.dispose = function () {
  1684. // do nothing
  1685. };
  1686. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onProgress, onError) {
  1687. var _this = this;
  1688. scene.useRightHandedSystem = true;
  1689. GLTF1.GLTFLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  1690. gltfRuntime.importOnlyMeshes = true;
  1691. if (meshesNames === "") {
  1692. gltfRuntime.importMeshesNames = [];
  1693. }
  1694. else if (typeof meshesNames === "string") {
  1695. gltfRuntime.importMeshesNames = [meshesNames];
  1696. }
  1697. else if (meshesNames && !(meshesNames instanceof Array)) {
  1698. gltfRuntime.importMeshesNames = [meshesNames];
  1699. }
  1700. else {
  1701. gltfRuntime.importMeshesNames = [];
  1702. BABYLON.Tools.Warn("Argument meshesNames must be of type string or string[]");
  1703. }
  1704. // Create nodes
  1705. _this._createNodes(gltfRuntime);
  1706. var meshes = new Array();
  1707. var skeletons = new Array();
  1708. // Fill arrays of meshes and skeletons
  1709. for (var nde in gltfRuntime.nodes) {
  1710. var node = gltfRuntime.nodes[nde];
  1711. if (node.babylonNode instanceof BABYLON.AbstractMesh) {
  1712. meshes.push(node.babylonNode);
  1713. }
  1714. }
  1715. for (var skl in gltfRuntime.skins) {
  1716. var skin = gltfRuntime.skins[skl];
  1717. if (skin.babylonSkeleton instanceof BABYLON.Skeleton) {
  1718. skeletons.push(skin.babylonSkeleton);
  1719. }
  1720. }
  1721. // Load buffers, shaders, materials, etc.
  1722. _this._loadBuffersAsync(gltfRuntime, function () {
  1723. _this._loadShadersAsync(gltfRuntime, function () {
  1724. importMaterials(gltfRuntime);
  1725. postLoad(gltfRuntime);
  1726. if (!BABYLON.GLTFFileLoader.IncrementalLoading && onSuccess) {
  1727. onSuccess(meshes, [], skeletons);
  1728. }
  1729. });
  1730. }, onProgress);
  1731. if (BABYLON.GLTFFileLoader.IncrementalLoading && onSuccess) {
  1732. onSuccess(meshes, [], skeletons);
  1733. }
  1734. }, onError);
  1735. return true;
  1736. };
  1737. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onSuccess, onProgress, onError) {
  1738. var _this = this;
  1739. scene.useRightHandedSystem = true;
  1740. GLTF1.GLTFLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  1741. // Load runtime extensios
  1742. GLTF1.GLTFLoaderExtension.LoadRuntimeExtensionsAsync(gltfRuntime, function () {
  1743. // Create nodes
  1744. _this._createNodes(gltfRuntime);
  1745. // Load buffers, shaders, materials, etc.
  1746. _this._loadBuffersAsync(gltfRuntime, function () {
  1747. _this._loadShadersAsync(gltfRuntime, function () {
  1748. importMaterials(gltfRuntime);
  1749. postLoad(gltfRuntime);
  1750. if (!BABYLON.GLTFFileLoader.IncrementalLoading) {
  1751. onSuccess();
  1752. }
  1753. });
  1754. });
  1755. if (BABYLON.GLTFFileLoader.IncrementalLoading) {
  1756. onSuccess();
  1757. }
  1758. }, onError);
  1759. }, onError);
  1760. };
  1761. GLTFLoader.prototype._loadShadersAsync = function (gltfRuntime, onload) {
  1762. var hasShaders = false;
  1763. var processShader = function (sha, shader) {
  1764. GLTF1.GLTFLoaderExtension.LoadShaderStringAsync(gltfRuntime, sha, function (shaderString) {
  1765. gltfRuntime.loadedShaderCount++;
  1766. if (shaderString) {
  1767. BABYLON.Effect.ShadersStore[sha + (shader.type === GLTF1.EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = shaderString;
  1768. }
  1769. if (gltfRuntime.loadedShaderCount === gltfRuntime.shaderscount) {
  1770. onload();
  1771. }
  1772. }, function () {
  1773. BABYLON.Tools.Error("Error when loading shader program named " + sha + " located at " + shader.uri);
  1774. });
  1775. };
  1776. for (var sha in gltfRuntime.shaders) {
  1777. hasShaders = true;
  1778. var shader = gltfRuntime.shaders[sha];
  1779. if (shader) {
  1780. processShader.bind(this, sha, shader)();
  1781. }
  1782. else {
  1783. BABYLON.Tools.Error("No shader named: " + sha);
  1784. }
  1785. }
  1786. if (!hasShaders) {
  1787. onload();
  1788. }
  1789. };
  1790. ;
  1791. GLTFLoader.prototype._loadBuffersAsync = function (gltfRuntime, onLoad, onProgress) {
  1792. var hasBuffers = false;
  1793. var processBuffer = function (buf, buffer) {
  1794. GLTF1.GLTFLoaderExtension.LoadBufferAsync(gltfRuntime, buf, function (bufferView) {
  1795. gltfRuntime.loadedBufferCount++;
  1796. if (bufferView) {
  1797. if (bufferView.byteLength != gltfRuntime.buffers[buf].byteLength) {
  1798. BABYLON.Tools.Error("Buffer named " + buf + " is length " + bufferView.byteLength + ". Expected: " + buffer.byteLength); // Improve error message
  1799. }
  1800. gltfRuntime.loadedBufferViews[buf] = bufferView;
  1801. }
  1802. if (gltfRuntime.loadedBufferCount === gltfRuntime.buffersCount) {
  1803. onLoad();
  1804. }
  1805. }, function () {
  1806. BABYLON.Tools.Error("Error when loading buffer named " + buf + " located at " + buffer.uri);
  1807. });
  1808. };
  1809. for (var buf in gltfRuntime.buffers) {
  1810. hasBuffers = true;
  1811. var buffer = gltfRuntime.buffers[buf];
  1812. if (buffer) {
  1813. processBuffer.bind(this, buf, buffer)();
  1814. }
  1815. else {
  1816. BABYLON.Tools.Error("No buffer named: " + buf);
  1817. }
  1818. }
  1819. if (!hasBuffers) {
  1820. onLoad();
  1821. }
  1822. };
  1823. GLTFLoader.prototype._createNodes = function (gltfRuntime) {
  1824. var currentScene = gltfRuntime.currentScene;
  1825. if (currentScene) {
  1826. // Only one scene even if multiple scenes are defined
  1827. for (var i = 0; i < currentScene.nodes.length; i++) {
  1828. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1829. }
  1830. }
  1831. else {
  1832. // Load all scenes
  1833. for (var thing in gltfRuntime.scenes) {
  1834. currentScene = gltfRuntime.scenes[thing];
  1835. for (var i = 0; i < currentScene.nodes.length; i++) {
  1836. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1837. }
  1838. }
  1839. }
  1840. };
  1841. GLTFLoader.Extensions = {};
  1842. return GLTFLoader;
  1843. }());
  1844. GLTF1.GLTFLoader = GLTFLoader;
  1845. ;
  1846. BABYLON.GLTFFileLoader.CreateGLTFLoaderV1 = function () { return new GLTFLoader(); };
  1847. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  1848. })(BABYLON || (BABYLON = {}));
  1849. //# sourceMappingURL=babylon.glTFLoader.js.map
  1850. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  1851. var BABYLON;
  1852. (function (BABYLON) {
  1853. var GLTF1;
  1854. (function (GLTF1) {
  1855. /**
  1856. * Utils functions for GLTF
  1857. */
  1858. var GLTFUtils = /** @class */ (function () {
  1859. function GLTFUtils() {
  1860. }
  1861. /**
  1862. * Sets the given "parameter" matrix
  1863. * @param scene: the {BABYLON.Scene} object
  1864. * @param source: the source node where to pick the matrix
  1865. * @param parameter: the GLTF technique parameter
  1866. * @param uniformName: the name of the shader's uniform
  1867. * @param shaderMaterial: the shader material
  1868. */
  1869. GLTFUtils.SetMatrix = function (scene, source, parameter, uniformName, shaderMaterial) {
  1870. var mat = null;
  1871. if (parameter.semantic === "MODEL") {
  1872. mat = source.getWorldMatrix();
  1873. }
  1874. else if (parameter.semantic === "PROJECTION") {
  1875. mat = scene.getProjectionMatrix();
  1876. }
  1877. else if (parameter.semantic === "VIEW") {
  1878. mat = scene.getViewMatrix();
  1879. }
  1880. else if (parameter.semantic === "MODELVIEWINVERSETRANSPOSE") {
  1881. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().multiply(scene.getViewMatrix()).invert());
  1882. }
  1883. else if (parameter.semantic === "MODELVIEW") {
  1884. mat = source.getWorldMatrix().multiply(scene.getViewMatrix());
  1885. }
  1886. else if (parameter.semantic === "MODELVIEWPROJECTION") {
  1887. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix());
  1888. }
  1889. else if (parameter.semantic === "MODELINVERSE") {
  1890. mat = source.getWorldMatrix().invert();
  1891. }
  1892. else if (parameter.semantic === "VIEWINVERSE") {
  1893. mat = scene.getViewMatrix().invert();
  1894. }
  1895. else if (parameter.semantic === "PROJECTIONINVERSE") {
  1896. mat = scene.getProjectionMatrix().invert();
  1897. }
  1898. else if (parameter.semantic === "MODELVIEWINVERSE") {
  1899. mat = source.getWorldMatrix().multiply(scene.getViewMatrix()).invert();
  1900. }
  1901. else if (parameter.semantic === "MODELVIEWPROJECTIONINVERSE") {
  1902. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix()).invert();
  1903. }
  1904. else if (parameter.semantic === "MODELINVERSETRANSPOSE") {
  1905. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().invert());
  1906. }
  1907. else {
  1908. debugger;
  1909. }
  1910. if (mat) {
  1911. switch (parameter.type) {
  1912. case GLTF1.EParameterType.FLOAT_MAT2:
  1913. shaderMaterial.setMatrix2x2(uniformName, BABYLON.Matrix.GetAsMatrix2x2(mat));
  1914. break;
  1915. case GLTF1.EParameterType.FLOAT_MAT3:
  1916. shaderMaterial.setMatrix3x3(uniformName, BABYLON.Matrix.GetAsMatrix3x3(mat));
  1917. break;
  1918. case GLTF1.EParameterType.FLOAT_MAT4:
  1919. shaderMaterial.setMatrix(uniformName, mat);
  1920. break;
  1921. default: break;
  1922. }
  1923. }
  1924. };
  1925. /**
  1926. * Sets the given "parameter" matrix
  1927. * @param shaderMaterial: the shader material
  1928. * @param uniform: the name of the shader's uniform
  1929. * @param value: the value of the uniform
  1930. * @param type: the uniform's type (EParameterType FLOAT, VEC2, VEC3 or VEC4)
  1931. */
  1932. GLTFUtils.SetUniform = function (shaderMaterial, uniform, value, type) {
  1933. switch (type) {
  1934. case GLTF1.EParameterType.FLOAT:
  1935. shaderMaterial.setFloat(uniform, value);
  1936. return true;
  1937. case GLTF1.EParameterType.FLOAT_VEC2:
  1938. shaderMaterial.setVector2(uniform, BABYLON.Vector2.FromArray(value));
  1939. return true;
  1940. case GLTF1.EParameterType.FLOAT_VEC3:
  1941. shaderMaterial.setVector3(uniform, BABYLON.Vector3.FromArray(value));
  1942. return true;
  1943. case GLTF1.EParameterType.FLOAT_VEC4:
  1944. shaderMaterial.setVector4(uniform, BABYLON.Vector4.FromArray(value));
  1945. return true;
  1946. default: return false;
  1947. }
  1948. };
  1949. /**
  1950. * If the uri is a base64 string
  1951. * @param uri: the uri to test
  1952. */
  1953. GLTFUtils.IsBase64 = function (uri) {
  1954. return uri.length < 5 ? false : uri.substr(0, 5) === "data:";
  1955. };
  1956. /**
  1957. * Decode the base64 uri
  1958. * @param uri: the uri to decode
  1959. */
  1960. GLTFUtils.DecodeBase64 = function (uri) {
  1961. var decodedString = atob(uri.split(",")[1]);
  1962. var bufferLength = decodedString.length;
  1963. var bufferView = new Uint8Array(new ArrayBuffer(bufferLength));
  1964. for (var i = 0; i < bufferLength; i++) {
  1965. bufferView[i] = decodedString.charCodeAt(i);
  1966. }
  1967. return bufferView.buffer;
  1968. };
  1969. /**
  1970. * Returns the wrap mode of the texture
  1971. * @param mode: the mode value
  1972. */
  1973. GLTFUtils.GetWrapMode = function (mode) {
  1974. switch (mode) {
  1975. case GLTF1.ETextureWrapMode.CLAMP_TO_EDGE: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  1976. case GLTF1.ETextureWrapMode.MIRRORED_REPEAT: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  1977. case GLTF1.ETextureWrapMode.REPEAT: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1978. default: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1979. }
  1980. };
  1981. /**
  1982. * Returns the byte stride giving an accessor
  1983. * @param accessor: the GLTF accessor objet
  1984. */
  1985. GLTFUtils.GetByteStrideFromType = function (accessor) {
  1986. // Needs this function since "byteStride" isn't requiered in glTF format
  1987. var type = accessor.type;
  1988. switch (type) {
  1989. case "VEC2": return 2;
  1990. case "VEC3": return 3;
  1991. case "VEC4": return 4;
  1992. case "MAT2": return 4;
  1993. case "MAT3": return 9;
  1994. case "MAT4": return 16;
  1995. default: return 1;
  1996. }
  1997. };
  1998. /**
  1999. * Returns the texture filter mode giving a mode value
  2000. * @param mode: the filter mode value
  2001. */
  2002. GLTFUtils.GetTextureFilterMode = function (mode) {
  2003. switch (mode) {
  2004. case GLTF1.ETextureFilterType.LINEAR:
  2005. case GLTF1.ETextureFilterType.LINEAR_MIPMAP_NEAREST:
  2006. case GLTF1.ETextureFilterType.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  2007. case GLTF1.ETextureFilterType.NEAREST:
  2008. case GLTF1.ETextureFilterType.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_SAMPLINGMODE;
  2009. default: return BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  2010. }
  2011. };
  2012. GLTFUtils.GetBufferFromBufferView = function (gltfRuntime, bufferView, byteOffset, byteLength, componentType) {
  2013. var byteOffset = bufferView.byteOffset + byteOffset;
  2014. var loadedBufferView = gltfRuntime.loadedBufferViews[bufferView.buffer];
  2015. if (byteOffset + byteLength > loadedBufferView.byteLength) {
  2016. throw new Error("Buffer access is out of range");
  2017. }
  2018. var buffer = loadedBufferView.buffer;
  2019. byteOffset += loadedBufferView.byteOffset;
  2020. switch (componentType) {
  2021. case GLTF1.EComponentType.BYTE: return new Int8Array(buffer, byteOffset, byteLength);
  2022. case GLTF1.EComponentType.UNSIGNED_BYTE: return new Uint8Array(buffer, byteOffset, byteLength);
  2023. case GLTF1.EComponentType.SHORT: return new Int16Array(buffer, byteOffset, byteLength);
  2024. case GLTF1.EComponentType.UNSIGNED_SHORT: return new Uint16Array(buffer, byteOffset, byteLength);
  2025. default: return new Float32Array(buffer, byteOffset, byteLength);
  2026. }
  2027. };
  2028. /**
  2029. * Returns a buffer from its accessor
  2030. * @param gltfRuntime: the GLTF runtime
  2031. * @param accessor: the GLTF accessor
  2032. */
  2033. GLTFUtils.GetBufferFromAccessor = function (gltfRuntime, accessor) {
  2034. var bufferView = gltfRuntime.bufferViews[accessor.bufferView];
  2035. var byteLength = accessor.count * GLTFUtils.GetByteStrideFromType(accessor);
  2036. return GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, accessor.byteOffset, byteLength, accessor.componentType);
  2037. };
  2038. /**
  2039. * Decodes a buffer view into a string
  2040. * @param view: the buffer view
  2041. */
  2042. GLTFUtils.DecodeBufferToText = function (view) {
  2043. var result = "";
  2044. var length = view.byteLength;
  2045. for (var i = 0; i < length; ++i) {
  2046. result += String.fromCharCode(view[i]);
  2047. }
  2048. return result;
  2049. };
  2050. /**
  2051. * Returns the default material of gltf. Related to
  2052. * https://github.com/KhronosGroup/glTF/tree/master/specification/1.0#appendix-a-default-material
  2053. * @param scene: the Babylon.js scene
  2054. */
  2055. GLTFUtils.GetDefaultMaterial = function (scene) {
  2056. if (!GLTFUtils._DefaultMaterial) {
  2057. BABYLON.Effect.ShadersStore["GLTFDefaultMaterialVertexShader"] = [
  2058. "precision highp float;",
  2059. "",
  2060. "uniform mat4 worldView;",
  2061. "uniform mat4 projection;",
  2062. "",
  2063. "attribute vec3 position;",
  2064. "",
  2065. "void main(void)",
  2066. "{",
  2067. " gl_Position = projection * worldView * vec4(position, 1.0);",
  2068. "}"
  2069. ].join("\n");
  2070. BABYLON.Effect.ShadersStore["GLTFDefaultMaterialPixelShader"] = [
  2071. "precision highp float;",
  2072. "",
  2073. "uniform vec4 u_emission;",
  2074. "",
  2075. "void main(void)",
  2076. "{",
  2077. " gl_FragColor = u_emission;",
  2078. "}"
  2079. ].join("\n");
  2080. var shaderPath = {
  2081. vertex: "GLTFDefaultMaterial",
  2082. fragment: "GLTFDefaultMaterial"
  2083. };
  2084. var options = {
  2085. attributes: ["position"],
  2086. uniforms: ["worldView", "projection", "u_emission"],
  2087. samplers: new Array(),
  2088. needAlphaBlending: false
  2089. };
  2090. GLTFUtils._DefaultMaterial = new BABYLON.ShaderMaterial("GLTFDefaultMaterial", scene, shaderPath, options);
  2091. GLTFUtils._DefaultMaterial.setColor4("u_emission", new BABYLON.Color4(0.5, 0.5, 0.5, 1.0));
  2092. }
  2093. return GLTFUtils._DefaultMaterial;
  2094. };
  2095. // The GLTF default material
  2096. GLTFUtils._DefaultMaterial = null;
  2097. return GLTFUtils;
  2098. }());
  2099. GLTF1.GLTFUtils = GLTFUtils;
  2100. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  2101. })(BABYLON || (BABYLON = {}));
  2102. //# sourceMappingURL=babylon.glTFLoaderUtils.js.map
  2103. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2104. var BABYLON;
  2105. (function (BABYLON) {
  2106. var GLTF1;
  2107. (function (GLTF1) {
  2108. var GLTFLoaderExtension = /** @class */ (function () {
  2109. function GLTFLoaderExtension(name) {
  2110. this._name = name;
  2111. }
  2112. Object.defineProperty(GLTFLoaderExtension.prototype, "name", {
  2113. get: function () {
  2114. return this._name;
  2115. },
  2116. enumerable: true,
  2117. configurable: true
  2118. });
  2119. /**
  2120. * Defines an override for loading the runtime
  2121. * Return true to stop further extensions from loading the runtime
  2122. */
  2123. GLTFLoaderExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  2124. return false;
  2125. };
  2126. /**
  2127. * Defines an onverride for creating gltf runtime
  2128. * Return true to stop further extensions from creating the runtime
  2129. */
  2130. GLTFLoaderExtension.prototype.loadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
  2131. return false;
  2132. };
  2133. /**
  2134. * Defines an override for loading buffers
  2135. * Return true to stop further extensions from loading this buffer
  2136. */
  2137. GLTFLoaderExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError, onProgress) {
  2138. return false;
  2139. };
  2140. /**
  2141. * Defines an override for loading texture buffers
  2142. * Return true to stop further extensions from loading this texture data
  2143. */
  2144. GLTFLoaderExtension.prototype.loadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  2145. return false;
  2146. };
  2147. /**
  2148. * Defines an override for creating textures
  2149. * Return true to stop further extensions from loading this texture
  2150. */
  2151. GLTFLoaderExtension.prototype.createTextureAsync = function (gltfRuntime, id, buffer, onSuccess, onError) {
  2152. return false;
  2153. };
  2154. /**
  2155. * Defines an override for loading shader strings
  2156. * Return true to stop further extensions from loading this shader data
  2157. */
  2158. GLTFLoaderExtension.prototype.loadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  2159. return false;
  2160. };
  2161. /**
  2162. * Defines an override for loading materials
  2163. * Return true to stop further extensions from loading this material
  2164. */
  2165. GLTFLoaderExtension.prototype.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  2166. return false;
  2167. };
  2168. // ---------
  2169. // Utilities
  2170. // ---------
  2171. GLTFLoaderExtension.LoadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  2172. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2173. return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
  2174. }, function () {
  2175. setTimeout(function () {
  2176. onSuccess(GLTF1.GLTFLoaderBase.CreateRuntime(data.json, scene, rootUrl));
  2177. });
  2178. });
  2179. };
  2180. GLTFLoaderExtension.LoadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
  2181. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2182. return loaderExtension.loadRuntimeExtensionsAsync(gltfRuntime, onSuccess, onError);
  2183. }, function () {
  2184. setTimeout(function () {
  2185. onSuccess();
  2186. });
  2187. });
  2188. };
  2189. GLTFLoaderExtension.LoadBufferAsync = function (gltfRuntime, id, onSuccess, onError, onProgress) {
  2190. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2191. return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
  2192. }, function () {
  2193. GLTF1.GLTFLoaderBase.LoadBufferAsync(gltfRuntime, id, onSuccess, onError, onProgress);
  2194. });
  2195. };
  2196. GLTFLoaderExtension.LoadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  2197. GLTFLoaderExtension.LoadTextureBufferAsync(gltfRuntime, id, function (buffer) { return GLTFLoaderExtension.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError); }, onError);
  2198. };
  2199. GLTFLoaderExtension.LoadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  2200. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2201. return loaderExtension.loadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  2202. }, function () {
  2203. GLTF1.GLTFLoaderBase.LoadShaderStringAsync(gltfRuntime, id, onSuccess, onError);
  2204. });
  2205. };
  2206. GLTFLoaderExtension.LoadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  2207. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2208. return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  2209. }, function () {
  2210. GLTF1.GLTFLoaderBase.LoadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  2211. });
  2212. };
  2213. GLTFLoaderExtension.LoadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  2214. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2215. return loaderExtension.loadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  2216. }, function () {
  2217. GLTF1.GLTFLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, onSuccess, onError);
  2218. });
  2219. };
  2220. GLTFLoaderExtension.CreateTextureAsync = function (gltfRuntime, id, buffer, onSuccess, onError) {
  2221. GLTFLoaderExtension.ApplyExtensions(function (loaderExtension) {
  2222. return loaderExtension.createTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  2223. }, function () {
  2224. GLTF1.GLTFLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, onSuccess, onError);
  2225. });
  2226. };
  2227. GLTFLoaderExtension.ApplyExtensions = function (func, defaultFunc) {
  2228. for (var extensionName in GLTF1.GLTFLoader.Extensions) {
  2229. var loaderExtension = GLTF1.GLTFLoader.Extensions[extensionName];
  2230. if (func(loaderExtension)) {
  2231. return;
  2232. }
  2233. }
  2234. defaultFunc();
  2235. };
  2236. return GLTFLoaderExtension;
  2237. }());
  2238. GLTF1.GLTFLoaderExtension = GLTFLoaderExtension;
  2239. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  2240. })(BABYLON || (BABYLON = {}));
  2241. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  2242. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2243. var __extends = (this && this.__extends) || (function () {
  2244. var extendStatics = Object.setPrototypeOf ||
  2245. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2246. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2247. return function (d, b) {
  2248. extendStatics(d, b);
  2249. function __() { this.constructor = d; }
  2250. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2251. };
  2252. })();
  2253. var BABYLON;
  2254. (function (BABYLON) {
  2255. var GLTF1;
  2256. (function (GLTF1) {
  2257. var BinaryExtensionBufferName = "binary_glTF";
  2258. ;
  2259. ;
  2260. var GLTFBinaryExtension = /** @class */ (function (_super) {
  2261. __extends(GLTFBinaryExtension, _super);
  2262. function GLTFBinaryExtension() {
  2263. return _super.call(this, "KHR_binary_glTF") || this;
  2264. }
  2265. GLTFBinaryExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  2266. var extensionsUsed = data.json.extensionsUsed;
  2267. if (!extensionsUsed || extensionsUsed.indexOf(this.name) === -1 || !data.bin) {
  2268. return false;
  2269. }
  2270. this._bin = data.bin;
  2271. onSuccess(GLTF1.GLTFLoaderBase.CreateRuntime(data.json, scene, rootUrl));
  2272. return true;
  2273. };
  2274. GLTFBinaryExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  2275. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  2276. return false;
  2277. }
  2278. if (id !== BinaryExtensionBufferName) {
  2279. return false;
  2280. }
  2281. onSuccess(this._bin);
  2282. return true;
  2283. };
  2284. GLTFBinaryExtension.prototype.loadTextureBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  2285. var texture = gltfRuntime.textures[id];
  2286. var source = gltfRuntime.images[texture.source];
  2287. if (!source.extensions || !(this.name in source.extensions)) {
  2288. return false;
  2289. }
  2290. var sourceExt = source.extensions[this.name];
  2291. var bufferView = gltfRuntime.bufferViews[sourceExt.bufferView];
  2292. var buffer = GLTF1.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, GLTF1.EComponentType.UNSIGNED_BYTE);
  2293. onSuccess(buffer);
  2294. return true;
  2295. };
  2296. GLTFBinaryExtension.prototype.loadShaderStringAsync = function (gltfRuntime, id, onSuccess, onError) {
  2297. var shader = gltfRuntime.shaders[id];
  2298. if (!shader.extensions || !(this.name in shader.extensions)) {
  2299. return false;
  2300. }
  2301. var binaryExtensionShader = shader.extensions[this.name];
  2302. var bufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
  2303. var shaderBytes = GLTF1.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, GLTF1.EComponentType.UNSIGNED_BYTE);
  2304. setTimeout(function () {
  2305. var shaderString = GLTF1.GLTFUtils.DecodeBufferToText(shaderBytes);
  2306. onSuccess(shaderString);
  2307. });
  2308. return true;
  2309. };
  2310. return GLTFBinaryExtension;
  2311. }(GLTF1.GLTFLoaderExtension));
  2312. GLTF1.GLTFBinaryExtension = GLTFBinaryExtension;
  2313. GLTF1.GLTFLoader.RegisterExtension(new GLTFBinaryExtension());
  2314. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  2315. })(BABYLON || (BABYLON = {}));
  2316. //# sourceMappingURL=babylon.glTFBinaryExtension.js.map
  2317. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2318. var __extends = (this && this.__extends) || (function () {
  2319. var extendStatics = Object.setPrototypeOf ||
  2320. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2321. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2322. return function (d, b) {
  2323. extendStatics(d, b);
  2324. function __() { this.constructor = d; }
  2325. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2326. };
  2327. })();
  2328. var BABYLON;
  2329. (function (BABYLON) {
  2330. var GLTF1;
  2331. (function (GLTF1) {
  2332. ;
  2333. ;
  2334. ;
  2335. var GLTFMaterialsCommonExtension = /** @class */ (function (_super) {
  2336. __extends(GLTFMaterialsCommonExtension, _super);
  2337. function GLTFMaterialsCommonExtension() {
  2338. return _super.call(this, "KHR_materials_common") || this;
  2339. }
  2340. GLTFMaterialsCommonExtension.prototype.loadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
  2341. if (!gltfRuntime.extensions)
  2342. return false;
  2343. var extension = gltfRuntime.extensions[this.name];
  2344. if (!extension)
  2345. return false;
  2346. // Create lights
  2347. var lights = extension.lights;
  2348. if (lights) {
  2349. for (var thing in lights) {
  2350. var light = lights[thing];
  2351. switch (light.type) {
  2352. case "ambient":
  2353. var ambientLight = new BABYLON.HemisphericLight(light.name, new BABYLON.Vector3(0, 1, 0), gltfRuntime.scene);
  2354. var ambient = light.ambient;
  2355. if (ambient) {
  2356. ambientLight.diffuse = BABYLON.Color3.FromArray(ambient.color || [1, 1, 1]);
  2357. }
  2358. break;
  2359. case "point":
  2360. var pointLight = new BABYLON.PointLight(light.name, new BABYLON.Vector3(10, 10, 10), gltfRuntime.scene);
  2361. var point = light.point;
  2362. if (point) {
  2363. pointLight.diffuse = BABYLON.Color3.FromArray(point.color || [1, 1, 1]);
  2364. }
  2365. break;
  2366. case "directional":
  2367. var dirLight = new BABYLON.DirectionalLight(light.name, new BABYLON.Vector3(0, -1, 0), gltfRuntime.scene);
  2368. var directional = light.directional;
  2369. if (directional) {
  2370. dirLight.diffuse = BABYLON.Color3.FromArray(directional.color || [1, 1, 1]);
  2371. }
  2372. break;
  2373. case "spot":
  2374. var spot = light.spot;
  2375. if (spot) {
  2376. var spotLight = new BABYLON.SpotLight(light.name, new BABYLON.Vector3(0, 10, 0), new BABYLON.Vector3(0, -1, 0), spot.fallOffAngle || Math.PI, spot.fallOffExponent || 0.0, gltfRuntime.scene);
  2377. spotLight.diffuse = BABYLON.Color3.FromArray(spot.color || [1, 1, 1]);
  2378. }
  2379. break;
  2380. default:
  2381. BABYLON.Tools.Warn("GLTF Material Common extension: light type \"" + light.type + "\” not supported");
  2382. break;
  2383. }
  2384. }
  2385. }
  2386. return false;
  2387. };
  2388. GLTFMaterialsCommonExtension.prototype.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  2389. var material = gltfRuntime.materials[id];
  2390. if (!material || !material.extensions)
  2391. return false;
  2392. var extension = material.extensions[this.name];
  2393. if (!extension)
  2394. return false;
  2395. var standardMaterial = new BABYLON.StandardMaterial(id, gltfRuntime.scene);
  2396. standardMaterial.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
  2397. if (extension.technique === "CONSTANT") {
  2398. standardMaterial.disableLighting = true;
  2399. }
  2400. standardMaterial.backFaceCulling = extension.doubleSided === undefined ? false : !extension.doubleSided;
  2401. standardMaterial.alpha = extension.values.transparency === undefined ? 1.0 : extension.values.transparency;
  2402. standardMaterial.specularPower = extension.values.shininess === undefined ? 0.0 : extension.values.shininess;
  2403. // Ambient
  2404. if (typeof extension.values.ambient === "string") {
  2405. this._loadTexture(gltfRuntime, extension.values.ambient, standardMaterial, "ambientTexture", onError);
  2406. }
  2407. else {
  2408. standardMaterial.ambientColor = BABYLON.Color3.FromArray(extension.values.ambient || [0, 0, 0]);
  2409. }
  2410. // Diffuse
  2411. if (typeof extension.values.diffuse === "string") {
  2412. this._loadTexture(gltfRuntime, extension.values.diffuse, standardMaterial, "diffuseTexture", onError);
  2413. }
  2414. else {
  2415. standardMaterial.diffuseColor = BABYLON.Color3.FromArray(extension.values.diffuse || [0, 0, 0]);
  2416. }
  2417. // Emission
  2418. if (typeof extension.values.emission === "string") {
  2419. this._loadTexture(gltfRuntime, extension.values.emission, standardMaterial, "emissiveTexture", onError);
  2420. }
  2421. else {
  2422. standardMaterial.emissiveColor = BABYLON.Color3.FromArray(extension.values.emission || [0, 0, 0]);
  2423. }
  2424. // Specular
  2425. if (typeof extension.values.specular === "string") {
  2426. this._loadTexture(gltfRuntime, extension.values.specular, standardMaterial, "specularTexture", onError);
  2427. }
  2428. else {
  2429. standardMaterial.specularColor = BABYLON.Color3.FromArray(extension.values.specular || [0, 0, 0]);
  2430. }
  2431. return true;
  2432. };
  2433. GLTFMaterialsCommonExtension.prototype._loadTexture = function (gltfRuntime, id, material, propertyPath, onError) {
  2434. // Create buffer from texture url
  2435. GLTF1.GLTFLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, function (buffer) {
  2436. // Create texture from buffer
  2437. GLTF1.GLTFLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, function (texture) { return material[propertyPath] = texture; }, onError);
  2438. }, onError);
  2439. };
  2440. return GLTFMaterialsCommonExtension;
  2441. }(GLTF1.GLTFLoaderExtension));
  2442. GLTF1.GLTFMaterialsCommonExtension = GLTFMaterialsCommonExtension;
  2443. GLTF1.GLTFLoader.RegisterExtension(new GLTFMaterialsCommonExtension());
  2444. })(GLTF1 = BABYLON.GLTF1 || (BABYLON.GLTF1 = {}));
  2445. })(BABYLON || (BABYLON = {}));
  2446. //# sourceMappingURL=babylon.glTFMaterialsCommonExtension.js.map