babylon.glTF2FileLoader.js 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429
  1. "use strict";
  2. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  3. var BABYLON;
  4. (function (BABYLON) {
  5. var GLTFLoaderCoordinateSystemMode;
  6. (function (GLTFLoaderCoordinateSystemMode) {
  7. /**
  8. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  9. */
  10. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
  11. /**
  12. * Sets the useRightHandedSystem flag on the scene.
  13. */
  14. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
  15. })(GLTFLoaderCoordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode || (BABYLON.GLTFLoaderCoordinateSystemMode = {}));
  16. var GLTFLoaderAnimationStartMode;
  17. (function (GLTFLoaderAnimationStartMode) {
  18. /**
  19. * No animation will start.
  20. */
  21. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
  22. /**
  23. * The first animation will start.
  24. */
  25. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
  26. /**
  27. * All animations will start.
  28. */
  29. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
  30. })(GLTFLoaderAnimationStartMode = BABYLON.GLTFLoaderAnimationStartMode || (BABYLON.GLTFLoaderAnimationStartMode = {}));
  31. var GLTFLoaderState;
  32. (function (GLTFLoaderState) {
  33. GLTFLoaderState[GLTFLoaderState["Loading"] = 0] = "Loading";
  34. GLTFLoaderState[GLTFLoaderState["Ready"] = 1] = "Ready";
  35. GLTFLoaderState[GLTFLoaderState["Complete"] = 2] = "Complete";
  36. })(GLTFLoaderState = BABYLON.GLTFLoaderState || (BABYLON.GLTFLoaderState = {}));
  37. var GLTFFileLoader = /** @class */ (function () {
  38. function GLTFFileLoader() {
  39. // #region Common options
  40. /**
  41. * Raised when the asset has been parsed.
  42. * The data.json property stores the glTF JSON.
  43. * The data.bin property stores the BIN chunk from a glTF binary or null if the input is not a glTF binary.
  44. */
  45. this.onParsedObservable = new BABYLON.Observable();
  46. // #endregion
  47. // #region V2 options
  48. /**
  49. * The coordinate system mode (AUTO, FORCE_RIGHT_HANDED).
  50. */
  51. this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
  52. /**
  53. * The animation start mode (NONE, FIRST, ALL).
  54. */
  55. this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
  56. /**
  57. * Set to true to compile materials before raising the success callback.
  58. */
  59. this.compileMaterials = false;
  60. /**
  61. * Set to true to also compile materials with clip planes.
  62. */
  63. this.useClipPlane = false;
  64. /**
  65. * Set to true to compile shadow generators before raising the success callback.
  66. */
  67. this.compileShadowGenerators = false;
  68. /**
  69. * Raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  70. */
  71. this.onMeshLoadedObservable = new BABYLON.Observable();
  72. /**
  73. * Raised when the loader creates a texture after parsing the glTF properties of the texture.
  74. */
  75. this.onTextureLoadedObservable = new BABYLON.Observable();
  76. /**
  77. * Raised when the loader creates a material after parsing the glTF properties of the material.
  78. */
  79. this.onMaterialLoadedObservable = new BABYLON.Observable();
  80. /**
  81. * Raised when the loader creates an animation group after parsing the glTF properties of the animation.
  82. */
  83. this.onAnimationGroupLoadedObservable = new BABYLON.Observable();
  84. /**
  85. * Raised when the asset is completely loaded, immediately before the loader is disposed.
  86. * For assets with LODs, raised when all of the LODs are complete.
  87. * For assets without LODs, raised when the model is complete, immediately after onSuccess.
  88. */
  89. this.onCompleteObservable = new BABYLON.Observable();
  90. /**
  91. * Raised when the loader is disposed.
  92. */
  93. this.onDisposeObservable = new BABYLON.Observable();
  94. /**
  95. * Raised after a loader extension is created.
  96. * Set additional options for a loader extension in this event.
  97. */
  98. this.onExtensionLoadedObservable = new BABYLON.Observable();
  99. // #endregion
  100. this._loader = null;
  101. this.name = "gltf";
  102. this.extensions = {
  103. ".gltf": { isBinary: false },
  104. ".glb": { isBinary: true }
  105. };
  106. }
  107. Object.defineProperty(GLTFFileLoader.prototype, "onParsed", {
  108. set: function (callback) {
  109. if (this._onParsedObserver) {
  110. this.onParsedObservable.remove(this._onParsedObserver);
  111. }
  112. this._onParsedObserver = this.onParsedObservable.add(callback);
  113. },
  114. enumerable: true,
  115. configurable: true
  116. });
  117. Object.defineProperty(GLTFFileLoader.prototype, "onMeshLoaded", {
  118. set: function (callback) {
  119. if (this._onMeshLoadedObserver) {
  120. this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver);
  121. }
  122. this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback);
  123. },
  124. enumerable: true,
  125. configurable: true
  126. });
  127. Object.defineProperty(GLTFFileLoader.prototype, "onTextureLoaded", {
  128. set: function (callback) {
  129. if (this._onTextureLoadedObserver) {
  130. this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver);
  131. }
  132. this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback);
  133. },
  134. enumerable: true,
  135. configurable: true
  136. });
  137. Object.defineProperty(GLTFFileLoader.prototype, "onMaterialLoaded", {
  138. set: function (callback) {
  139. if (this._onMaterialLoadedObserver) {
  140. this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver);
  141. }
  142. this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback);
  143. },
  144. enumerable: true,
  145. configurable: true
  146. });
  147. Object.defineProperty(GLTFFileLoader.prototype, "onAnimationGroupLoaded", {
  148. set: function (callback) {
  149. if (this._onAnimationGroupLoadedObserver) {
  150. this.onAnimationGroupLoadedObservable.remove(this._onAnimationGroupLoadedObserver);
  151. }
  152. this._onAnimationGroupLoadedObserver = this.onAnimationGroupLoadedObservable.add(callback);
  153. },
  154. enumerable: true,
  155. configurable: true
  156. });
  157. Object.defineProperty(GLTFFileLoader.prototype, "onComplete", {
  158. set: function (callback) {
  159. if (this._onCompleteObserver) {
  160. this.onCompleteObservable.remove(this._onCompleteObserver);
  161. }
  162. this._onCompleteObserver = this.onCompleteObservable.add(callback);
  163. },
  164. enumerable: true,
  165. configurable: true
  166. });
  167. Object.defineProperty(GLTFFileLoader.prototype, "onDispose", {
  168. set: function (callback) {
  169. if (this._onDisposeObserver) {
  170. this.onDisposeObservable.remove(this._onDisposeObserver);
  171. }
  172. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  173. },
  174. enumerable: true,
  175. configurable: true
  176. });
  177. Object.defineProperty(GLTFFileLoader.prototype, "onExtensionLoaded", {
  178. set: function (callback) {
  179. if (this._onExtensionLoadedObserver) {
  180. this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver);
  181. }
  182. this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback);
  183. },
  184. enumerable: true,
  185. configurable: true
  186. });
  187. /**
  188. * Gets a promise that resolves when the asset is completely loaded.
  189. * @returns A promise that resolves when the asset is completely loaded.
  190. */
  191. GLTFFileLoader.prototype.whenCompleteAsync = function () {
  192. var _this = this;
  193. return new Promise(function (resolve) {
  194. _this.onCompleteObservable.add(function () {
  195. resolve();
  196. }, undefined, undefined, undefined, true);
  197. });
  198. };
  199. Object.defineProperty(GLTFFileLoader.prototype, "loaderState", {
  200. /**
  201. * The loader state or null if not active.
  202. */
  203. get: function () {
  204. return this._loader ? this._loader.state : null;
  205. },
  206. enumerable: true,
  207. configurable: true
  208. });
  209. /**
  210. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  211. */
  212. GLTFFileLoader.prototype.dispose = function () {
  213. if (this._loader) {
  214. this._loader.dispose();
  215. this._loader = null;
  216. }
  217. this.onMeshLoadedObservable.clear();
  218. this.onTextureLoadedObservable.clear();
  219. this.onMaterialLoadedObservable.clear();
  220. this.onDisposeObservable.notifyObservers(this);
  221. this.onDisposeObservable.clear();
  222. };
  223. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  224. var _this = this;
  225. return Promise.resolve().then(function () {
  226. var loaderData = _this._parse(data);
  227. _this._loader = _this._getLoader(loaderData);
  228. return _this._loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onProgress);
  229. });
  230. };
  231. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  232. var _this = this;
  233. return Promise.resolve().then(function () {
  234. var loaderData = _this._parse(data);
  235. _this._loader = _this._getLoader(loaderData);
  236. return _this._loader.loadAsync(scene, loaderData, rootUrl, onProgress);
  237. });
  238. };
  239. GLTFFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress) {
  240. var _this = this;
  241. return Promise.resolve().then(function () {
  242. var loaderData = _this._parse(data);
  243. _this._loader = _this._getLoader(loaderData);
  244. return _this._loader.importMeshAsync(null, scene, loaderData, rootUrl, onProgress).then(function (result) {
  245. var container = new BABYLON.AssetContainer(scene);
  246. Array.prototype.push.apply(container.meshes, result.meshes);
  247. Array.prototype.push.apply(container.particleSystems, result.particleSystems);
  248. Array.prototype.push.apply(container.skeletons, result.skeletons);
  249. container.removeAllFromScene();
  250. return container;
  251. });
  252. });
  253. };
  254. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  255. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  256. };
  257. GLTFFileLoader.prototype.createPlugin = function () {
  258. return new GLTFFileLoader();
  259. };
  260. GLTFFileLoader.prototype._parse = function (data) {
  261. var parsedData;
  262. if (data instanceof ArrayBuffer) {
  263. parsedData = GLTFFileLoader._parseBinary(data);
  264. }
  265. else {
  266. parsedData = {
  267. json: JSON.parse(data),
  268. bin: null
  269. };
  270. }
  271. this.onParsedObservable.notifyObservers(parsedData);
  272. this.onParsedObservable.clear();
  273. return parsedData;
  274. };
  275. GLTFFileLoader.prototype._getLoader = function (loaderData) {
  276. var _this = this;
  277. var loaderVersion = { major: 2, minor: 0 };
  278. var asset = loaderData.json.asset || {};
  279. var version = GLTFFileLoader._parseVersion(asset.version);
  280. if (!version) {
  281. throw new Error("Invalid version: " + asset.version);
  282. }
  283. if (asset.minVersion !== undefined) {
  284. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  285. if (!minVersion) {
  286. throw new Error("Invalid minimum version: " + asset.minVersion);
  287. }
  288. if (GLTFFileLoader._compareVersion(minVersion, loaderVersion) > 0) {
  289. throw new Error("Incompatible minimum version: " + asset.minVersion);
  290. }
  291. }
  292. var createLoaders = {
  293. 1: GLTFFileLoader.CreateGLTFLoaderV1,
  294. 2: GLTFFileLoader.CreateGLTFLoaderV2
  295. };
  296. var createLoader = createLoaders[version.major];
  297. if (!createLoader) {
  298. throw new Error("Unsupported version: " + asset.version);
  299. }
  300. var loader = createLoader();
  301. loader.coordinateSystemMode = this.coordinateSystemMode;
  302. loader.animationStartMode = this.animationStartMode;
  303. loader.compileMaterials = this.compileMaterials;
  304. loader.useClipPlane = this.useClipPlane;
  305. loader.compileShadowGenerators = this.compileShadowGenerators;
  306. loader.onMeshLoadedObservable.add(function (mesh) { return _this.onMeshLoadedObservable.notifyObservers(mesh); });
  307. loader.onTextureLoadedObservable.add(function (texture) { return _this.onTextureLoadedObservable.notifyObservers(texture); });
  308. loader.onMaterialLoadedObservable.add(function (material) { return _this.onMaterialLoadedObservable.notifyObservers(material); });
  309. loader.onExtensionLoadedObservable.add(function (extension) { return _this.onExtensionLoadedObservable.notifyObservers(extension); });
  310. loader.onCompleteObservable.add(function () {
  311. _this.onMeshLoadedObservable.clear();
  312. _this.onTextureLoadedObservable.clear();
  313. _this.onMaterialLoadedObservable.clear();
  314. _this.onCompleteObservable.notifyObservers(_this);
  315. _this.onCompleteObservable.clear();
  316. });
  317. return loader;
  318. };
  319. GLTFFileLoader._parseBinary = function (data) {
  320. var Binary = {
  321. Magic: 0x46546C67
  322. };
  323. var binaryReader = new BinaryReader(data);
  324. var magic = binaryReader.readUint32();
  325. if (magic !== Binary.Magic) {
  326. throw new Error("Unexpected magic: " + magic);
  327. }
  328. var version = binaryReader.readUint32();
  329. switch (version) {
  330. case 1: return GLTFFileLoader._parseV1(binaryReader);
  331. case 2: return GLTFFileLoader._parseV2(binaryReader);
  332. }
  333. throw new Error("Unsupported version: " + version);
  334. };
  335. GLTFFileLoader._parseV1 = function (binaryReader) {
  336. var ContentFormat = {
  337. JSON: 0
  338. };
  339. var length = binaryReader.readUint32();
  340. if (length != binaryReader.getLength()) {
  341. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  342. }
  343. var contentLength = binaryReader.readUint32();
  344. var contentFormat = binaryReader.readUint32();
  345. var content;
  346. switch (contentFormat) {
  347. case ContentFormat.JSON: {
  348. content = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength)));
  349. break;
  350. }
  351. default: {
  352. throw new Error("Unexpected content format: " + contentFormat);
  353. }
  354. }
  355. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  356. var body = binaryReader.readUint8Array(bytesRemaining);
  357. return {
  358. json: content,
  359. bin: body
  360. };
  361. };
  362. GLTFFileLoader._parseV2 = function (binaryReader) {
  363. var ChunkFormat = {
  364. JSON: 0x4E4F534A,
  365. BIN: 0x004E4942
  366. };
  367. var length = binaryReader.readUint32();
  368. if (length !== binaryReader.getLength()) {
  369. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  370. }
  371. // JSON chunk
  372. var chunkLength = binaryReader.readUint32();
  373. var chunkFormat = binaryReader.readUint32();
  374. if (chunkFormat !== ChunkFormat.JSON) {
  375. throw new Error("First chunk format is not JSON");
  376. }
  377. var json = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength)));
  378. // Look for BIN chunk
  379. var bin = null;
  380. while (binaryReader.getPosition() < binaryReader.getLength()) {
  381. var chunkLength_1 = binaryReader.readUint32();
  382. var chunkFormat_1 = binaryReader.readUint32();
  383. switch (chunkFormat_1) {
  384. case ChunkFormat.JSON: {
  385. throw new Error("Unexpected JSON chunk");
  386. }
  387. case ChunkFormat.BIN: {
  388. bin = binaryReader.readUint8Array(chunkLength_1);
  389. break;
  390. }
  391. default: {
  392. // ignore unrecognized chunkFormat
  393. binaryReader.skipBytes(chunkLength_1);
  394. break;
  395. }
  396. }
  397. }
  398. return {
  399. json: json,
  400. bin: bin
  401. };
  402. };
  403. GLTFFileLoader._parseVersion = function (version) {
  404. if (version === "1.0" || version === "1.0.1") {
  405. return {
  406. major: 1,
  407. minor: 0
  408. };
  409. }
  410. var match = (version + "").match(/^(\d+)\.(\d+)/);
  411. if (!match) {
  412. return null;
  413. }
  414. return {
  415. major: parseInt(match[1]),
  416. minor: parseInt(match[2])
  417. };
  418. };
  419. GLTFFileLoader._compareVersion = function (a, b) {
  420. if (a.major > b.major)
  421. return 1;
  422. if (a.major < b.major)
  423. return -1;
  424. if (a.minor > b.minor)
  425. return 1;
  426. if (a.minor < b.minor)
  427. return -1;
  428. return 0;
  429. };
  430. GLTFFileLoader._decodeBufferToText = function (buffer) {
  431. var result = "";
  432. var length = buffer.byteLength;
  433. for (var i = 0; i < length; i++) {
  434. result += String.fromCharCode(buffer[i]);
  435. }
  436. return result;
  437. };
  438. // #endregion
  439. // #region V1 options
  440. GLTFFileLoader.IncrementalLoading = true;
  441. GLTFFileLoader.HomogeneousCoordinates = false;
  442. return GLTFFileLoader;
  443. }());
  444. BABYLON.GLTFFileLoader = GLTFFileLoader;
  445. var BinaryReader = /** @class */ (function () {
  446. function BinaryReader(arrayBuffer) {
  447. this._arrayBuffer = arrayBuffer;
  448. this._dataView = new DataView(arrayBuffer);
  449. this._byteOffset = 0;
  450. }
  451. BinaryReader.prototype.getPosition = function () {
  452. return this._byteOffset;
  453. };
  454. BinaryReader.prototype.getLength = function () {
  455. return this._arrayBuffer.byteLength;
  456. };
  457. BinaryReader.prototype.readUint32 = function () {
  458. var value = this._dataView.getUint32(this._byteOffset, true);
  459. this._byteOffset += 4;
  460. return value;
  461. };
  462. BinaryReader.prototype.readUint8Array = function (length) {
  463. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  464. this._byteOffset += length;
  465. return value;
  466. };
  467. BinaryReader.prototype.skipBytes = function (length) {
  468. this._byteOffset += length;
  469. };
  470. return BinaryReader;
  471. }());
  472. if (BABYLON.SceneLoader) {
  473. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  474. }
  475. })(BABYLON || (BABYLON = {}));
  476. //# sourceMappingURL=babylon.glTFFileLoader.js.map
  477. "use strict";
  478. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  479. var BABYLON;
  480. (function (BABYLON) {
  481. var GLTF2;
  482. (function (GLTF2) {
  483. var ArrayItem = /** @class */ (function () {
  484. function ArrayItem() {
  485. }
  486. ArrayItem.Assign = function (values) {
  487. if (values) {
  488. for (var index = 0; index < values.length; index++) {
  489. values[index]._index = index;
  490. }
  491. }
  492. };
  493. return ArrayItem;
  494. }());
  495. GLTF2.ArrayItem = ArrayItem;
  496. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  497. })(BABYLON || (BABYLON = {}));
  498. //# sourceMappingURL=babylon.glTFLoaderUtilities.js.map
  499. "use strict";
  500. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  501. /// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
  502. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  503. "use strict";
  504. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  505. var BABYLON;
  506. (function (BABYLON) {
  507. var GLTF2;
  508. (function (GLTF2) {
  509. var GLTFLoader = /** @class */ (function () {
  510. function GLTFLoader() {
  511. this._completePromises = new Array();
  512. this._disposed = false;
  513. this._state = null;
  514. this._extensions = {};
  515. this._defaultSampler = {};
  516. this._defaultBabylonMaterials = {};
  517. this._requests = new Array();
  518. this.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.AUTO;
  519. this.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.FIRST;
  520. this.compileMaterials = false;
  521. this.useClipPlane = false;
  522. this.compileShadowGenerators = false;
  523. this.onDisposeObservable = new BABYLON.Observable();
  524. this.onMeshLoadedObservable = new BABYLON.Observable();
  525. this.onTextureLoadedObservable = new BABYLON.Observable();
  526. this.onMaterialLoadedObservable = new BABYLON.Observable();
  527. this.onAnimationGroupLoadedObservable = new BABYLON.Observable();
  528. this.onExtensionLoadedObservable = new BABYLON.Observable();
  529. this.onCompleteObservable = new BABYLON.Observable();
  530. }
  531. GLTFLoader._Register = function (name, factory) {
  532. if (GLTFLoader._Factories[name]) {
  533. BABYLON.Tools.Error("Extension with the name '" + name + "' already exists");
  534. return;
  535. }
  536. GLTFLoader._Factories[name] = factory;
  537. // Keep the order of registration so that extensions registered first are called first.
  538. GLTFLoader._Names.push(name);
  539. };
  540. Object.defineProperty(GLTFLoader.prototype, "state", {
  541. get: function () {
  542. return this._state;
  543. },
  544. enumerable: true,
  545. configurable: true
  546. });
  547. GLTFLoader.prototype.dispose = function () {
  548. if (this._disposed) {
  549. return;
  550. }
  551. this._disposed = true;
  552. this.onDisposeObservable.notifyObservers(this);
  553. this.onDisposeObservable.clear();
  554. this._clear();
  555. };
  556. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  557. var _this = this;
  558. return Promise.resolve().then(function () {
  559. var nodes = null;
  560. if (meshesNames) {
  561. var nodeMap_1 = {};
  562. if (_this._gltf.nodes) {
  563. for (var _i = 0, _a = _this._gltf.nodes; _i < _a.length; _i++) {
  564. var node = _a[_i];
  565. if (node.name) {
  566. nodeMap_1[node.name] = node;
  567. }
  568. }
  569. }
  570. var names = (meshesNames instanceof Array) ? meshesNames : [meshesNames];
  571. nodes = names.map(function (name) {
  572. var node = nodeMap_1[name];
  573. if (!node) {
  574. throw new Error("Failed to find node '" + name + "'");
  575. }
  576. return node;
  577. });
  578. }
  579. return _this._loadAsync(nodes, scene, data, rootUrl, onProgress).then(function () {
  580. return {
  581. meshes: _this._getMeshes(),
  582. particleSystems: [],
  583. skeletons: _this._getSkeletons(),
  584. };
  585. });
  586. });
  587. };
  588. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  589. return this._loadAsync(null, scene, data, rootUrl, onProgress);
  590. };
  591. GLTFLoader.prototype._loadAsync = function (nodes, scene, data, rootUrl, onProgress) {
  592. var _this = this;
  593. return Promise.resolve().then(function () {
  594. _this._loadExtensions();
  595. _this._babylonScene = scene;
  596. _this._rootUrl = rootUrl;
  597. _this._progressCallback = onProgress;
  598. _this._state = BABYLON.GLTFLoaderState.Loading;
  599. _this._loadData(data);
  600. _this._checkExtensions();
  601. var promises = new Array();
  602. if (nodes) {
  603. promises.push(_this._loadNodesAsync(nodes));
  604. }
  605. else {
  606. var scene_1 = GLTFLoader._GetProperty("#/scene", _this._gltf.scenes, _this._gltf.scene || 0);
  607. promises.push(_this._loadSceneAsync("#/scenes/" + scene_1._index, scene_1));
  608. }
  609. if (_this.compileMaterials) {
  610. promises.push(_this._compileMaterialsAsync());
  611. }
  612. if (_this.compileShadowGenerators) {
  613. promises.push(_this._compileShadowGeneratorsAsync());
  614. }
  615. var resultPromise = Promise.all(promises).then(function () {
  616. _this._state = BABYLON.GLTFLoaderState.Ready;
  617. _this._startAnimations();
  618. });
  619. resultPromise.then(function () {
  620. _this._rootBabylonMesh.setEnabled(true);
  621. BABYLON.Tools.SetImmediate(function () {
  622. if (!_this._disposed) {
  623. Promise.all(_this._completePromises).then(function () {
  624. _this._state = BABYLON.GLTFLoaderState.Complete;
  625. _this.onCompleteObservable.notifyObservers(_this);
  626. _this.onCompleteObservable.clear();
  627. _this._clear();
  628. }).catch(function (error) {
  629. BABYLON.Tools.Error("glTF Loader: " + error.message);
  630. _this._clear();
  631. });
  632. }
  633. });
  634. });
  635. return resultPromise;
  636. }).catch(function (error) {
  637. BABYLON.Tools.Error("glTF Loader: " + error.message);
  638. _this._clear();
  639. throw error;
  640. });
  641. };
  642. GLTFLoader.prototype._loadExtensions = function () {
  643. for (var _i = 0, _a = GLTFLoader._Names; _i < _a.length; _i++) {
  644. var name_1 = _a[_i];
  645. var extension = GLTFLoader._Factories[name_1](this);
  646. this._extensions[name_1] = extension;
  647. this.onExtensionLoadedObservable.notifyObservers(extension);
  648. }
  649. this.onExtensionLoadedObservable.clear();
  650. };
  651. GLTFLoader.prototype._loadData = function (data) {
  652. this._gltf = data.json;
  653. this._setupData();
  654. if (data.bin) {
  655. var buffers = this._gltf.buffers;
  656. if (buffers && buffers[0] && !buffers[0].uri) {
  657. var binaryBuffer = buffers[0];
  658. if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
  659. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  660. }
  661. binaryBuffer._data = Promise.resolve(data.bin);
  662. }
  663. else {
  664. BABYLON.Tools.Warn("Unexpected BIN chunk");
  665. }
  666. }
  667. };
  668. GLTFLoader.prototype._setupData = function () {
  669. GLTF2.ArrayItem.Assign(this._gltf.accessors);
  670. GLTF2.ArrayItem.Assign(this._gltf.animations);
  671. GLTF2.ArrayItem.Assign(this._gltf.buffers);
  672. GLTF2.ArrayItem.Assign(this._gltf.bufferViews);
  673. GLTF2.ArrayItem.Assign(this._gltf.cameras);
  674. GLTF2.ArrayItem.Assign(this._gltf.images);
  675. GLTF2.ArrayItem.Assign(this._gltf.materials);
  676. GLTF2.ArrayItem.Assign(this._gltf.meshes);
  677. GLTF2.ArrayItem.Assign(this._gltf.nodes);
  678. GLTF2.ArrayItem.Assign(this._gltf.samplers);
  679. GLTF2.ArrayItem.Assign(this._gltf.scenes);
  680. GLTF2.ArrayItem.Assign(this._gltf.skins);
  681. GLTF2.ArrayItem.Assign(this._gltf.textures);
  682. if (this._gltf.nodes) {
  683. var nodeParents = {};
  684. for (var _i = 0, _a = this._gltf.nodes; _i < _a.length; _i++) {
  685. var node = _a[_i];
  686. if (node.children) {
  687. for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
  688. var index = _c[_b];
  689. nodeParents[index] = node._index;
  690. }
  691. }
  692. }
  693. var rootNode = this._createRootNode();
  694. for (var _d = 0, _e = this._gltf.nodes; _d < _e.length; _d++) {
  695. var node = _e[_d];
  696. var parentIndex = nodeParents[node._index];
  697. node._parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
  698. }
  699. }
  700. };
  701. GLTFLoader.prototype._checkExtensions = function () {
  702. if (this._gltf.extensionsRequired) {
  703. for (var _i = 0, _a = this._gltf.extensionsRequired; _i < _a.length; _i++) {
  704. var name_2 = _a[_i];
  705. var extension = this._extensions[name_2];
  706. if (!extension || !extension.enabled) {
  707. throw new Error("Require extension " + name_2 + " is not available");
  708. }
  709. }
  710. }
  711. };
  712. GLTFLoader.prototype._createRootNode = function () {
  713. this._rootBabylonMesh = new BABYLON.Mesh("__root__", this._babylonScene);
  714. this._rootBabylonMesh.setEnabled(false);
  715. var rootNode = { _babylonMesh: this._rootBabylonMesh };
  716. switch (this.coordinateSystemMode) {
  717. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO: {
  718. if (!this._babylonScene.useRightHandedSystem) {
  719. rootNode.rotation = [0, 1, 0, 0];
  720. rootNode.scale = [1, 1, -1];
  721. GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
  722. }
  723. break;
  724. }
  725. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
  726. this._babylonScene.useRightHandedSystem = true;
  727. break;
  728. }
  729. default: {
  730. throw new Error("Invalid coordinate system mode (" + this.coordinateSystemMode + ")");
  731. }
  732. }
  733. this.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
  734. return rootNode;
  735. };
  736. GLTFLoader.prototype._loadNodesAsync = function (nodes) {
  737. var promises = new Array();
  738. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  739. var node = nodes_1[_i];
  740. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  741. }
  742. promises.push(this._loadAnimationsAsync());
  743. return Promise.all(promises).then(function () { });
  744. };
  745. GLTFLoader.prototype._loadSceneAsync = function (context, scene) {
  746. var promise = GLTF2.GLTFLoaderExtension._LoadSceneAsync(this, context, scene);
  747. if (promise) {
  748. return promise;
  749. }
  750. var promises = new Array();
  751. for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
  752. var index = _a[_i];
  753. var node = GLTFLoader._GetProperty(context + "/nodes/" + index, this._gltf.nodes, index);
  754. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  755. }
  756. promises.push(this._loadAnimationsAsync());
  757. return Promise.all(promises).then(function () { });
  758. };
  759. GLTFLoader.prototype._forEachPrimitive = function (node, callback) {
  760. if (node._primitiveBabylonMeshes) {
  761. for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
  762. var babylonMesh = _a[_i];
  763. callback(babylonMesh);
  764. }
  765. }
  766. else {
  767. callback(node._babylonMesh);
  768. }
  769. };
  770. GLTFLoader.prototype._getMeshes = function () {
  771. var meshes = new Array();
  772. // Root mesh is always first.
  773. meshes.push(this._rootBabylonMesh);
  774. var nodes = this._gltf.nodes;
  775. if (nodes) {
  776. for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
  777. var node = nodes_2[_i];
  778. if (node._babylonMesh) {
  779. meshes.push(node._babylonMesh);
  780. }
  781. if (node._primitiveBabylonMeshes) {
  782. for (var _a = 0, _b = node._primitiveBabylonMeshes; _a < _b.length; _a++) {
  783. var babylonMesh = _b[_a];
  784. meshes.push(babylonMesh);
  785. }
  786. }
  787. }
  788. }
  789. return meshes;
  790. };
  791. GLTFLoader.prototype._getSkeletons = function () {
  792. var skeletons = new Array();
  793. var skins = this._gltf.skins;
  794. if (skins) {
  795. for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
  796. var skin = skins_1[_i];
  797. if (skin._babylonSkeleton) {
  798. skeletons.push(skin._babylonSkeleton);
  799. }
  800. }
  801. }
  802. return skeletons;
  803. };
  804. GLTFLoader.prototype._startAnimations = function () {
  805. var animations = this._gltf.animations;
  806. if (!animations) {
  807. return;
  808. }
  809. switch (this.animationStartMode) {
  810. case BABYLON.GLTFLoaderAnimationStartMode.NONE: {
  811. // do nothing
  812. break;
  813. }
  814. case BABYLON.GLTFLoaderAnimationStartMode.FIRST: {
  815. var animation = animations[0];
  816. animation._babylonAnimationGroup.start(true);
  817. break;
  818. }
  819. case BABYLON.GLTFLoaderAnimationStartMode.ALL: {
  820. for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
  821. var animation = animations_1[_i];
  822. animation._babylonAnimationGroup.start(true);
  823. }
  824. break;
  825. }
  826. default: {
  827. BABYLON.Tools.Error("Invalid animation start mode (" + this.animationStartMode + ")");
  828. return;
  829. }
  830. }
  831. };
  832. GLTFLoader.prototype._loadNodeAsync = function (context, node) {
  833. var promise = GLTF2.GLTFLoaderExtension._LoadNodeAsync(this, context, node);
  834. if (promise) {
  835. return promise;
  836. }
  837. if (node._babylonMesh) {
  838. throw new Error(context + ": Invalid recursive node hierarchy");
  839. }
  840. var promises = new Array();
  841. var babylonMesh = new BABYLON.Mesh(node.name || "node" + node._index, this._babylonScene, node._parent._babylonMesh);
  842. node._babylonMesh = babylonMesh;
  843. node._babylonAnimationTargets = node._babylonAnimationTargets || [];
  844. node._babylonAnimationTargets.push(babylonMesh);
  845. GLTFLoader._LoadTransform(node, babylonMesh);
  846. if (node.mesh != undefined) {
  847. var mesh = GLTFLoader._GetProperty(context + "/mesh", this._gltf.meshes, node.mesh);
  848. promises.push(this._loadMeshAsync("#/meshes/" + mesh._index, node, mesh, babylonMesh));
  849. }
  850. if (node.children) {
  851. for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
  852. var index = _a[_i];
  853. var childNode = GLTFLoader._GetProperty(context + "/children/" + index, this._gltf.nodes, index);
  854. promises.push(this._loadNodeAsync("#/nodes/" + index, childNode));
  855. }
  856. }
  857. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  858. return Promise.all(promises).then(function () { });
  859. };
  860. GLTFLoader.prototype._loadMeshAsync = function (context, node, mesh, babylonMesh) {
  861. // TODO: instancing
  862. var _this = this;
  863. var promises = new Array();
  864. var primitives = mesh.primitives;
  865. if (!primitives || primitives.length === 0) {
  866. throw new Error(context + ": Primitives are missing");
  867. }
  868. GLTF2.ArrayItem.Assign(primitives);
  869. if (primitives.length === 1) {
  870. var primitive = primitives[0];
  871. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, babylonMesh));
  872. }
  873. else {
  874. node._primitiveBabylonMeshes = [];
  875. for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
  876. var primitive = primitives_1[_i];
  877. var primitiveBabylonMesh = new BABYLON.Mesh((mesh.name || babylonMesh.name) + "_" + primitive._index, this._babylonScene, babylonMesh);
  878. node._primitiveBabylonMeshes.push(primitiveBabylonMesh);
  879. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, primitiveBabylonMesh));
  880. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  881. }
  882. }
  883. if (node.skin != undefined) {
  884. var skin = GLTFLoader._GetProperty(context + "/skin", this._gltf.skins, node.skin);
  885. promises.push(this._loadSkinAsync("#/skins/" + skin._index, node, mesh, skin));
  886. }
  887. return Promise.all(promises).then(function () {
  888. _this._forEachPrimitive(node, function (babylonMesh) {
  889. babylonMesh._refreshBoundingInfo(true);
  890. });
  891. });
  892. };
  893. GLTFLoader.prototype._loadPrimitiveAsync = function (context, node, mesh, primitive, babylonMesh) {
  894. var _this = this;
  895. var promises = new Array();
  896. this._createMorphTargets(context, node, mesh, primitive, babylonMesh);
  897. promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh).then(function (babylonVertexData) {
  898. new BABYLON.Geometry(babylonMesh.name, _this._babylonScene, babylonVertexData, false, babylonMesh);
  899. return _this._loadMorphTargetsAsync(context, primitive, babylonMesh, babylonVertexData);
  900. }));
  901. var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
  902. if (primitive.material == undefined) {
  903. babylonMesh.material = this._getDefaultMaterial(babylonDrawMode);
  904. }
  905. else {
  906. var material = GLTFLoader._GetProperty(context + "/material}", this._gltf.materials, primitive.material);
  907. promises.push(this._loadMaterialAsync("#/materials/" + material._index, material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
  908. babylonMesh.material = babylonMaterial;
  909. }));
  910. }
  911. return Promise.all(promises).then(function () { });
  912. };
  913. GLTFLoader.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  914. var _this = this;
  915. var promise = GLTF2.GLTFLoaderExtension._LoadVertexDataAsync(this, context, primitive, babylonMesh);
  916. if (promise) {
  917. return promise;
  918. }
  919. var attributes = primitive.attributes;
  920. if (!attributes) {
  921. throw new Error(context + ": Attributes are missing");
  922. }
  923. var promises = new Array();
  924. var babylonVertexData = new BABYLON.VertexData();
  925. if (primitive.indices == undefined) {
  926. var positionAccessorIndex = attributes["POSITION"];
  927. if (positionAccessorIndex != undefined) {
  928. var accessor = GLTFLoader._GetProperty(context + "/attributes/POSITION", this._gltf.accessors, positionAccessorIndex);
  929. babylonVertexData.indices = new Uint32Array(accessor.count);
  930. for (var i = 0; i < babylonVertexData.indices.length; i++) {
  931. babylonVertexData.indices[i] = i;
  932. }
  933. }
  934. }
  935. else {
  936. var indicesAccessor = GLTFLoader._GetProperty(context + "/indices", this._gltf.accessors, primitive.indices);
  937. promises.push(this._loadAccessorAsync("#/accessors/" + indicesAccessor._index, indicesAccessor).then(function (data) {
  938. babylonVertexData.indices = data;
  939. }));
  940. }
  941. var loadAttribute = function (attribute, kind) {
  942. if (attributes[attribute] == undefined) {
  943. return;
  944. }
  945. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  946. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  947. babylonMesh._delayInfo.push(kind);
  948. }
  949. if (attribute === "COLOR_0") {
  950. // Assume vertex color has alpha on the mesh. The alphaMode of the material controls whether the material should use alpha or not.
  951. babylonMesh.hasVertexAlpha = true;
  952. }
  953. var accessor = GLTFLoader._GetProperty(context + "/attributes/" + attribute, _this._gltf.accessors, attributes[attribute]);
  954. promises.push(_this._loadAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  955. var attributeData = GLTFLoader._ConvertToFloat32Array(context, accessor, data);
  956. if (attribute === "COLOR_0" && accessor.type === "VEC3") {
  957. attributeData = GLTFLoader._ConvertVec3ToVec4(context, attributeData);
  958. }
  959. babylonVertexData.set(attributeData, kind);
  960. }));
  961. };
  962. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  963. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  964. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  965. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  966. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  967. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  968. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  969. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind);
  970. return Promise.all(promises).then(function () {
  971. return babylonVertexData;
  972. });
  973. };
  974. GLTFLoader.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
  975. if (!primitive.targets) {
  976. return;
  977. }
  978. if (node._numMorphTargets == undefined) {
  979. node._numMorphTargets = primitive.targets.length;
  980. }
  981. else if (primitive.targets.length !== node._numMorphTargets) {
  982. throw new Error(context + ": Primitives do not have the same number of targets");
  983. }
  984. babylonMesh.morphTargetManager = new BABYLON.MorphTargetManager();
  985. for (var index = 0; index < primitive.targets.length; index++) {
  986. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  987. babylonMesh.morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  988. // TODO: tell the target whether it has positions, normals, tangents
  989. }
  990. };
  991. GLTFLoader.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonVertexData) {
  992. if (!primitive.targets) {
  993. return Promise.resolve();
  994. }
  995. var promises = new Array();
  996. var morphTargetManager = babylonMesh.morphTargetManager;
  997. for (var index = 0; index < morphTargetManager.numTargets; index++) {
  998. var babylonMorphTarget = morphTargetManager.getTarget(index);
  999. promises.push(this._loadMorphTargetVertexDataAsync(context + "/targets/" + index, babylonVertexData, primitive.targets[index], babylonMorphTarget));
  1000. }
  1001. return Promise.all(promises).then(function () { });
  1002. };
  1003. GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, babylonVertexData, attributes, babylonMorphTarget) {
  1004. var _this = this;
  1005. var promises = new Array();
  1006. var loadAttribute = function (attribute, setData) {
  1007. if (attributes[attribute] == undefined) {
  1008. return;
  1009. }
  1010. var accessor = GLTFLoader._GetProperty(context + "/" + attribute, _this._gltf.accessors, attributes[attribute]);
  1011. promises.push(_this._loadAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1012. setData(data);
  1013. }));
  1014. };
  1015. loadAttribute("POSITION", function (data) {
  1016. if (babylonVertexData.positions) {
  1017. for (var i = 0; i < data.length; i++) {
  1018. data[i] += babylonVertexData.positions[i];
  1019. }
  1020. babylonMorphTarget.setPositions(data);
  1021. }
  1022. });
  1023. loadAttribute("NORMAL", function (data) {
  1024. if (babylonVertexData.normals) {
  1025. for (var i = 0; i < data.length; i++) {
  1026. data[i] += babylonVertexData.normals[i];
  1027. }
  1028. babylonMorphTarget.setNormals(data);
  1029. }
  1030. });
  1031. loadAttribute("TANGENT", function (data) {
  1032. if (babylonVertexData.tangents) {
  1033. // Tangent data for morph targets is stored as xyz delta.
  1034. // The vertexData.tangent is stored as xyzw.
  1035. // So we need to skip every fourth vertexData.tangent.
  1036. for (var i = 0, j = 0; i < data.length; i++) {
  1037. data[i] += babylonVertexData.tangents[j++];
  1038. if ((i + 1) % 3 == 0) {
  1039. j++;
  1040. }
  1041. }
  1042. babylonMorphTarget.setTangents(data);
  1043. }
  1044. });
  1045. return Promise.all(promises).then(function () { });
  1046. };
  1047. GLTFLoader._ConvertToFloat32Array = function (context, accessor, data) {
  1048. if (accessor.componentType == 5126 /* FLOAT */) {
  1049. return data;
  1050. }
  1051. var factor = 1;
  1052. if (accessor.normalized) {
  1053. switch (accessor.componentType) {
  1054. case 5121 /* UNSIGNED_BYTE */: {
  1055. factor = 1 / 255;
  1056. break;
  1057. }
  1058. case 5123 /* UNSIGNED_SHORT */: {
  1059. factor = 1 / 65535;
  1060. break;
  1061. }
  1062. default: {
  1063. throw new Error(context + ": Invalid component type (" + accessor.componentType + ")");
  1064. }
  1065. }
  1066. }
  1067. var result = new Float32Array(accessor.count * GLTFLoader._GetNumComponents(context, accessor.type));
  1068. for (var i = 0; i < result.length; i++) {
  1069. result[i] = data[i] * factor;
  1070. }
  1071. return result;
  1072. };
  1073. GLTFLoader._ConvertVec3ToVec4 = function (context, data) {
  1074. var result = new Float32Array(data.length / 3 * 4);
  1075. var offset = 0;
  1076. for (var i = 0; i < result.length; i++) {
  1077. if ((i + 1) % 4 === 0) {
  1078. result[i] = 1;
  1079. }
  1080. else {
  1081. result[i] = data[offset++];
  1082. }
  1083. }
  1084. return result;
  1085. };
  1086. GLTFLoader._LoadTransform = function (node, babylonNode) {
  1087. var position = BABYLON.Vector3.Zero();
  1088. var rotation = BABYLON.Quaternion.Identity();
  1089. var scaling = BABYLON.Vector3.One();
  1090. if (node.matrix) {
  1091. var matrix = BABYLON.Matrix.FromArray(node.matrix);
  1092. matrix.decompose(scaling, rotation, position);
  1093. }
  1094. else {
  1095. if (node.translation)
  1096. position = BABYLON.Vector3.FromArray(node.translation);
  1097. if (node.rotation)
  1098. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  1099. if (node.scale)
  1100. scaling = BABYLON.Vector3.FromArray(node.scale);
  1101. }
  1102. babylonNode.position = position;
  1103. babylonNode.rotationQuaternion = rotation;
  1104. babylonNode.scaling = scaling;
  1105. };
  1106. GLTFLoader.prototype._loadSkinAsync = function (context, node, mesh, skin) {
  1107. var _this = this;
  1108. var assignSkeleton = function () {
  1109. _this._forEachPrimitive(node, function (babylonMesh) {
  1110. babylonMesh.skeleton = skin._babylonSkeleton;
  1111. });
  1112. node._babylonMesh.parent = _this._rootBabylonMesh;
  1113. node._babylonMesh.position = BABYLON.Vector3.Zero();
  1114. node._babylonMesh.rotationQuaternion = BABYLON.Quaternion.Identity();
  1115. node._babylonMesh.scaling = BABYLON.Vector3.One();
  1116. };
  1117. if (skin._loaded) {
  1118. return skin._loaded.then(function () {
  1119. assignSkeleton();
  1120. });
  1121. }
  1122. // TODO: split into two parts so that bones are created before inverseBindMatricesData is loaded (for compiling materials).
  1123. return (skin._loaded = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
  1124. var skeletonId = "skeleton" + skin._index;
  1125. var babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, _this._babylonScene);
  1126. skin._babylonSkeleton = babylonSkeleton;
  1127. _this._loadBones(context, skin, inverseBindMatricesData);
  1128. assignSkeleton();
  1129. }));
  1130. };
  1131. GLTFLoader.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
  1132. if (skin.inverseBindMatrices == undefined) {
  1133. return Promise.resolve(null);
  1134. }
  1135. var accessor = GLTFLoader._GetProperty(context + "/inverseBindMatrices", this._gltf.accessors, skin.inverseBindMatrices);
  1136. return this._loadAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1137. return data;
  1138. });
  1139. };
  1140. GLTFLoader.prototype._createBone = function (node, skin, parent, localMatrix, baseMatrix, index) {
  1141. var babylonBone = new BABYLON.Bone(node.name || "joint" + node._index, skin._babylonSkeleton, parent, localMatrix, null, baseMatrix, index);
  1142. node._babylonAnimationTargets = node._babylonAnimationTargets || [];
  1143. node._babylonAnimationTargets.push(babylonBone);
  1144. return babylonBone;
  1145. };
  1146. GLTFLoader.prototype._loadBones = function (context, skin, inverseBindMatricesData) {
  1147. var babylonBones = {};
  1148. for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
  1149. var index = _a[_i];
  1150. var node = GLTFLoader._GetProperty(context + "/joints/" + index, this._gltf.nodes, index);
  1151. this._loadBone(node, skin, inverseBindMatricesData, babylonBones);
  1152. }
  1153. };
  1154. GLTFLoader.prototype._loadBone = function (node, skin, inverseBindMatricesData, babylonBones) {
  1155. var babylonBone = babylonBones[node._index];
  1156. if (babylonBone) {
  1157. return babylonBone;
  1158. }
  1159. var boneIndex = skin.joints.indexOf(node._index);
  1160. var baseMatrix = BABYLON.Matrix.Identity();
  1161. if (inverseBindMatricesData && boneIndex !== -1) {
  1162. baseMatrix = BABYLON.Matrix.FromArray(inverseBindMatricesData, boneIndex * 16);
  1163. baseMatrix.invertToRef(baseMatrix);
  1164. }
  1165. var babylonParentBone = null;
  1166. if (node._parent._babylonMesh !== this._rootBabylonMesh) {
  1167. babylonParentBone = this._loadBone(node._parent, skin, inverseBindMatricesData, babylonBones);
  1168. baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
  1169. }
  1170. babylonBone = this._createBone(node, skin, babylonParentBone, this._getNodeMatrix(node), baseMatrix, boneIndex);
  1171. babylonBones[node._index] = babylonBone;
  1172. return babylonBone;
  1173. };
  1174. GLTFLoader.prototype._getNodeMatrix = function (node) {
  1175. return node.matrix ?
  1176. BABYLON.Matrix.FromArray(node.matrix) :
  1177. BABYLON.Matrix.Compose(node.scale ? BABYLON.Vector3.FromArray(node.scale) : BABYLON.Vector3.One(), node.rotation ? BABYLON.Quaternion.FromArray(node.rotation) : BABYLON.Quaternion.Identity(), node.translation ? BABYLON.Vector3.FromArray(node.translation) : BABYLON.Vector3.Zero());
  1178. };
  1179. GLTFLoader.prototype._loadAnimationsAsync = function () {
  1180. var animations = this._gltf.animations;
  1181. if (!animations) {
  1182. return Promise.resolve();
  1183. }
  1184. var promises = new Array();
  1185. for (var index = 0; index < animations.length; index++) {
  1186. var animation = animations[index];
  1187. promises.push(this._loadAnimationAsync("#/animations/" + index, animation));
  1188. }
  1189. return Promise.all(promises).then(function () { });
  1190. };
  1191. GLTFLoader.prototype._loadAnimationAsync = function (context, animation) {
  1192. var babylonAnimationGroup = new BABYLON.AnimationGroup(animation.name || "animation" + animation._index, this._babylonScene);
  1193. animation._babylonAnimationGroup = babylonAnimationGroup;
  1194. var promises = new Array();
  1195. GLTF2.ArrayItem.Assign(animation.channels);
  1196. GLTF2.ArrayItem.Assign(animation.samplers);
  1197. for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
  1198. var channel = _a[_i];
  1199. promises.push(this._loadAnimationChannelAsync(context + "/channels/" + channel._index, context, animation, channel, babylonAnimationGroup));
  1200. }
  1201. this.onAnimationGroupLoadedObservable.notifyObservers(babylonAnimationGroup);
  1202. return Promise.all(promises).then(function () {
  1203. babylonAnimationGroup.normalize();
  1204. });
  1205. };
  1206. GLTFLoader.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup) {
  1207. var _this = this;
  1208. var targetNode = GLTFLoader._GetProperty(context + "/target/node", this._gltf.nodes, channel.target.node);
  1209. if (!targetNode._babylonMesh || targetNode.skin != undefined) {
  1210. return Promise.resolve();
  1211. }
  1212. var sampler = GLTFLoader._GetProperty(context + "/sampler", animation.samplers, channel.sampler);
  1213. return this._loadAnimationSamplerAsync(animationContext + "/samplers/" + channel.sampler, sampler).then(function (data) {
  1214. var targetPath;
  1215. var animationType;
  1216. switch (channel.target.path) {
  1217. case "translation" /* TRANSLATION */: {
  1218. targetPath = "position";
  1219. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1220. break;
  1221. }
  1222. case "rotation" /* ROTATION */: {
  1223. targetPath = "rotationQuaternion";
  1224. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  1225. break;
  1226. }
  1227. case "scale" /* SCALE */: {
  1228. targetPath = "scaling";
  1229. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1230. break;
  1231. }
  1232. case "weights" /* WEIGHTS */: {
  1233. targetPath = "influence";
  1234. animationType = BABYLON.Animation.ANIMATIONTYPE_FLOAT;
  1235. break;
  1236. }
  1237. default: {
  1238. throw new Error(context + ": Invalid target path (" + channel.target.path + ")");
  1239. }
  1240. }
  1241. var outputBufferOffset = 0;
  1242. var getNextOutputValue;
  1243. switch (targetPath) {
  1244. case "position": {
  1245. getNextOutputValue = function () {
  1246. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1247. outputBufferOffset += 3;
  1248. return value;
  1249. };
  1250. break;
  1251. }
  1252. case "rotationQuaternion": {
  1253. getNextOutputValue = function () {
  1254. var value = BABYLON.Quaternion.FromArray(data.output, outputBufferOffset);
  1255. outputBufferOffset += 4;
  1256. return value;
  1257. };
  1258. break;
  1259. }
  1260. case "scaling": {
  1261. getNextOutputValue = function () {
  1262. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1263. outputBufferOffset += 3;
  1264. return value;
  1265. };
  1266. break;
  1267. }
  1268. case "influence": {
  1269. getNextOutputValue = function () {
  1270. var value = new Array(targetNode._numMorphTargets);
  1271. for (var i = 0; i < targetNode._numMorphTargets; i++) {
  1272. value[i] = data.output[outputBufferOffset++];
  1273. }
  1274. return value;
  1275. };
  1276. break;
  1277. }
  1278. }
  1279. var getNextKey;
  1280. switch (data.interpolation) {
  1281. case "STEP" /* STEP */: {
  1282. getNextKey = function (frameIndex) { return ({
  1283. frame: data.input[frameIndex],
  1284. value: getNextOutputValue(),
  1285. interpolation: BABYLON.AnimationKeyInterpolation.STEP
  1286. }); };
  1287. break;
  1288. }
  1289. case "LINEAR" /* LINEAR */: {
  1290. getNextKey = function (frameIndex) { return ({
  1291. frame: data.input[frameIndex],
  1292. value: getNextOutputValue()
  1293. }); };
  1294. break;
  1295. }
  1296. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1297. getNextKey = function (frameIndex) { return ({
  1298. frame: data.input[frameIndex],
  1299. inTangent: getNextOutputValue(),
  1300. value: getNextOutputValue(),
  1301. outTangent: getNextOutputValue()
  1302. }); };
  1303. break;
  1304. }
  1305. }
  1306. var keys = new Array(data.input.length);
  1307. for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
  1308. keys[frameIndex] = getNextKey(frameIndex);
  1309. }
  1310. if (targetPath === "influence") {
  1311. var _loop_1 = function (targetIndex) {
  1312. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1313. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1314. babylonAnimation.setKeys(keys.map(function (key) { return ({
  1315. frame: key.frame,
  1316. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  1317. value: key.value[targetIndex],
  1318. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  1319. }); }));
  1320. _this._forEachPrimitive(targetNode, function (babylonMesh) {
  1321. var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
  1322. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, morphTarget);
  1323. });
  1324. };
  1325. for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
  1326. _loop_1(targetIndex);
  1327. }
  1328. }
  1329. else {
  1330. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1331. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1332. babylonAnimation.setKeys(keys);
  1333. if (targetNode._babylonAnimationTargets) {
  1334. for (var _i = 0, _a = targetNode._babylonAnimationTargets; _i < _a.length; _i++) {
  1335. var target = _a[_i];
  1336. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, target);
  1337. }
  1338. }
  1339. }
  1340. });
  1341. };
  1342. GLTFLoader.prototype._loadAnimationSamplerAsync = function (context, sampler) {
  1343. if (sampler._data) {
  1344. return sampler._data;
  1345. }
  1346. var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
  1347. switch (interpolation) {
  1348. case "STEP" /* STEP */:
  1349. case "LINEAR" /* LINEAR */:
  1350. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1351. break;
  1352. }
  1353. default: {
  1354. throw new Error(context + ": Invalid interpolation (" + sampler.interpolation + ")");
  1355. }
  1356. }
  1357. var inputData;
  1358. var outputData;
  1359. var inputAccessor = GLTFLoader._GetProperty(context + "/input", this._gltf.accessors, sampler.input);
  1360. var outputAccessor = GLTFLoader._GetProperty(context + "/output", this._gltf.accessors, sampler.output);
  1361. sampler._data = Promise.all([
  1362. this._loadAccessorAsync("#/accessors/" + inputAccessor._index, inputAccessor).then(function (data) {
  1363. inputData = data;
  1364. }),
  1365. this._loadAccessorAsync("#/accessors/" + outputAccessor._index, outputAccessor).then(function (data) {
  1366. outputData = data;
  1367. })
  1368. ]).then(function () {
  1369. return {
  1370. input: inputData,
  1371. interpolation: interpolation,
  1372. output: outputData,
  1373. };
  1374. });
  1375. return sampler._data;
  1376. };
  1377. GLTFLoader.prototype._loadBufferAsync = function (context, buffer) {
  1378. if (buffer._data) {
  1379. return buffer._data;
  1380. }
  1381. if (!buffer.uri) {
  1382. throw new Error(context + ": Uri is missing");
  1383. }
  1384. buffer._data = this._loadUriAsync(context, buffer.uri);
  1385. return buffer._data;
  1386. };
  1387. GLTFLoader.prototype._loadBufferViewAsync = function (context, bufferView) {
  1388. if (bufferView._data) {
  1389. return bufferView._data;
  1390. }
  1391. var buffer = GLTFLoader._GetProperty(context + "/buffer", this._gltf.buffers, bufferView.buffer);
  1392. bufferView._data = this._loadBufferAsync("#/buffers/" + buffer._index, buffer).then(function (bufferData) {
  1393. try {
  1394. return new Uint8Array(bufferData.buffer, bufferData.byteOffset + (bufferView.byteOffset || 0), bufferView.byteLength);
  1395. }
  1396. catch (e) {
  1397. throw new Error(context + ": " + e.message);
  1398. }
  1399. });
  1400. return bufferView._data;
  1401. };
  1402. GLTFLoader.prototype._loadAccessorAsync = function (context, accessor) {
  1403. var _this = this;
  1404. if (accessor.sparse) {
  1405. throw new Error(context + ": Sparse accessors are not currently supported");
  1406. }
  1407. if (accessor._data) {
  1408. return accessor._data;
  1409. }
  1410. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1411. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (bufferViewData) {
  1412. var numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
  1413. var byteOffset = accessor.byteOffset || 0;
  1414. var byteStride = bufferView.byteStride;
  1415. if (byteStride === 0) {
  1416. BABYLON.Tools.Warn(context + ": Byte stride of 0 is not valid");
  1417. }
  1418. try {
  1419. switch (accessor.componentType) {
  1420. case 5120 /* BYTE */: {
  1421. return _this._buildArrayBuffer(Float32Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1422. }
  1423. case 5121 /* UNSIGNED_BYTE */: {
  1424. return _this._buildArrayBuffer(Uint8Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1425. }
  1426. case 5122 /* SHORT */: {
  1427. return _this._buildArrayBuffer(Int16Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1428. }
  1429. case 5123 /* UNSIGNED_SHORT */: {
  1430. return _this._buildArrayBuffer(Uint16Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1431. }
  1432. case 5125 /* UNSIGNED_INT */: {
  1433. return _this._buildArrayBuffer(Uint32Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1434. }
  1435. case 5126 /* FLOAT */: {
  1436. return _this._buildArrayBuffer(Float32Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1437. }
  1438. default: {
  1439. throw new Error(context + ": Invalid component type (" + accessor.componentType + ")");
  1440. }
  1441. }
  1442. }
  1443. catch (e) {
  1444. throw new Error(context + ": " + e.messsage);
  1445. }
  1446. });
  1447. return accessor._data;
  1448. };
  1449. GLTFLoader.prototype._buildArrayBuffer = function (typedArray, data, byteOffset, count, numComponents, byteStride) {
  1450. byteOffset += data.byteOffset;
  1451. var targetLength = count * numComponents;
  1452. if (!byteStride || byteStride === numComponents * typedArray.BYTES_PER_ELEMENT) {
  1453. return new typedArray(data.buffer, byteOffset, targetLength);
  1454. }
  1455. var elementStride = byteStride / typedArray.BYTES_PER_ELEMENT;
  1456. var sourceBuffer = new typedArray(data.buffer, byteOffset, elementStride * count);
  1457. var targetBuffer = new typedArray(targetLength);
  1458. var sourceIndex = 0;
  1459. var targetIndex = 0;
  1460. while (targetIndex < targetLength) {
  1461. for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
  1462. targetBuffer[targetIndex] = sourceBuffer[sourceIndex + componentIndex];
  1463. targetIndex++;
  1464. }
  1465. sourceIndex += elementStride;
  1466. }
  1467. return targetBuffer;
  1468. };
  1469. GLTFLoader.prototype._getDefaultMaterial = function (drawMode) {
  1470. var babylonMaterial = this._defaultBabylonMaterials[drawMode];
  1471. if (!babylonMaterial) {
  1472. babylonMaterial = this._createMaterial(BABYLON.PBRMaterial, "__gltf_default", drawMode);
  1473. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1474. babylonMaterial.metallic = 1;
  1475. babylonMaterial.roughness = 1;
  1476. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1477. }
  1478. return babylonMaterial;
  1479. };
  1480. GLTFLoader.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, material, babylonMaterial) {
  1481. var promises = new Array();
  1482. // Ensure metallic workflow
  1483. babylonMaterial.metallic = 1;
  1484. babylonMaterial.roughness = 1;
  1485. var properties = material.pbrMetallicRoughness;
  1486. if (properties) {
  1487. if (properties.baseColorFactor) {
  1488. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  1489. babylonMaterial.alpha = properties.baseColorFactor[3];
  1490. }
  1491. else {
  1492. babylonMaterial.albedoColor = BABYLON.Color3.White();
  1493. }
  1494. babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
  1495. babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
  1496. if (properties.baseColorTexture) {
  1497. promises.push(this._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  1498. babylonMaterial.albedoTexture = texture;
  1499. }));
  1500. }
  1501. if (properties.metallicRoughnessTexture) {
  1502. promises.push(this._loadTextureAsync(context + "/metallicRoughnessTexture", properties.metallicRoughnessTexture, function (texture) {
  1503. babylonMaterial.metallicTexture = texture;
  1504. }));
  1505. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  1506. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  1507. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  1508. }
  1509. }
  1510. this._loadMaterialAlphaProperties(context, material, babylonMaterial);
  1511. return Promise.all(promises).then(function () { });
  1512. };
  1513. GLTFLoader.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  1514. var promise = GLTF2.GLTFLoaderExtension._LoadMaterialAsync(this, context, material, babylonMesh, babylonDrawMode, assign);
  1515. if (promise) {
  1516. return promise;
  1517. }
  1518. material._babylonData = material._babylonData || {};
  1519. var babylonData = material._babylonData[babylonDrawMode];
  1520. if (!babylonData) {
  1521. var promises = new Array();
  1522. var name_3 = material.name || "materialSG_" + material._index;
  1523. var babylonMaterial = this._createMaterial(BABYLON.PBRMaterial, name_3, babylonDrawMode);
  1524. promises.push(this._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  1525. promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync(context, material, babylonMaterial));
  1526. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1527. babylonData = {
  1528. material: babylonMaterial,
  1529. meshes: [],
  1530. loaded: Promise.all(promises).then(function () { })
  1531. };
  1532. material._babylonData[babylonDrawMode] = babylonData;
  1533. }
  1534. babylonData.meshes.push(babylonMesh);
  1535. assign(babylonData.material);
  1536. return babylonData.loaded;
  1537. };
  1538. GLTFLoader.prototype._createMaterial = function (type, name, drawMode) {
  1539. var babylonMaterial = new type(name, this._babylonScene);
  1540. babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? BABYLON.Material.CounterClockWiseSideOrientation : BABYLON.Material.ClockWiseSideOrientation;
  1541. babylonMaterial.fillMode = drawMode;
  1542. return babylonMaterial;
  1543. };
  1544. GLTFLoader.prototype._loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
  1545. var promises = new Array();
  1546. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  1547. if (material.doubleSided) {
  1548. babylonMaterial.backFaceCulling = false;
  1549. babylonMaterial.twoSidedLighting = true;
  1550. }
  1551. if (material.normalTexture) {
  1552. promises.push(this._loadTextureAsync(context + "/normalTexture", material.normalTexture, function (texture) {
  1553. babylonMaterial.bumpTexture = texture;
  1554. }));
  1555. babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
  1556. babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
  1557. if (material.normalTexture.scale != undefined) {
  1558. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  1559. }
  1560. }
  1561. if (material.occlusionTexture) {
  1562. promises.push(this._loadTextureAsync(context + "/occlusionTexture", material.occlusionTexture, function (texture) {
  1563. babylonMaterial.ambientTexture = texture;
  1564. }));
  1565. babylonMaterial.useAmbientInGrayScale = true;
  1566. if (material.occlusionTexture.strength != undefined) {
  1567. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  1568. }
  1569. }
  1570. if (material.emissiveTexture) {
  1571. promises.push(this._loadTextureAsync(context + "/emissiveTexture", material.emissiveTexture, function (texture) {
  1572. babylonMaterial.emissiveTexture = texture;
  1573. }));
  1574. }
  1575. return Promise.all(promises).then(function () { });
  1576. };
  1577. GLTFLoader.prototype._loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
  1578. var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
  1579. switch (alphaMode) {
  1580. case "OPAQUE" /* OPAQUE */: {
  1581. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1582. break;
  1583. }
  1584. case "MASK" /* MASK */: {
  1585. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  1586. babylonMaterial.alphaCutOff = (material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff);
  1587. if (babylonMaterial.albedoTexture) {
  1588. babylonMaterial.albedoTexture.hasAlpha = true;
  1589. }
  1590. break;
  1591. }
  1592. case "BLEND" /* BLEND */: {
  1593. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  1594. if (babylonMaterial.albedoTexture) {
  1595. babylonMaterial.albedoTexture.hasAlpha = true;
  1596. babylonMaterial.useAlphaFromAlbedoTexture = true;
  1597. }
  1598. break;
  1599. }
  1600. default: {
  1601. throw new Error(context + ": Invalid alpha mode (" + material.alphaMode + ")");
  1602. }
  1603. }
  1604. };
  1605. GLTFLoader.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  1606. var _this = this;
  1607. var texture = GLTFLoader._GetProperty(context + "/index", this._gltf.textures, textureInfo.index);
  1608. context = "#/textures/" + textureInfo.index;
  1609. var promises = new Array();
  1610. var sampler = (texture.sampler == undefined ? this._defaultSampler : GLTFLoader._GetProperty(context + "/sampler", this._gltf.samplers, texture.sampler));
  1611. var samplerData = this._loadSampler("#/samplers/" + sampler._index, sampler);
  1612. var deferred = new BABYLON.Deferred();
  1613. var babylonTexture = new BABYLON.Texture(null, this._babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, function () {
  1614. if (!_this._disposed) {
  1615. deferred.resolve();
  1616. }
  1617. }, function (message, exception) {
  1618. if (!_this._disposed) {
  1619. deferred.reject(new Error(context + ": " + ((exception && exception.message) ? exception.message : message || "Failed to load texture")));
  1620. }
  1621. });
  1622. promises.push(deferred.promise);
  1623. babylonTexture.name = texture.name || "texture" + texture._index;
  1624. babylonTexture.wrapU = samplerData.wrapU;
  1625. babylonTexture.wrapV = samplerData.wrapV;
  1626. babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
  1627. var image = GLTFLoader._GetProperty(context + "/source", this._gltf.images, texture.source);
  1628. promises.push(this._loadImageAsync("#/images/" + image._index, image).then(function (objectURL) {
  1629. babylonTexture.updateURL(objectURL);
  1630. }));
  1631. assign(babylonTexture);
  1632. this.onTextureLoadedObservable.notifyObservers(babylonTexture);
  1633. return Promise.all(promises).then(function () { });
  1634. };
  1635. GLTFLoader.prototype._loadSampler = function (context, sampler) {
  1636. if (!sampler._data) {
  1637. sampler._data = {
  1638. noMipMaps: (sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */),
  1639. samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler.magFilter, sampler.minFilter),
  1640. wrapU: GLTFLoader._GetTextureWrapMode(context, sampler.wrapS),
  1641. wrapV: GLTFLoader._GetTextureWrapMode(context, sampler.wrapT)
  1642. };
  1643. }
  1644. ;
  1645. return sampler._data;
  1646. };
  1647. GLTFLoader.prototype._loadImageAsync = function (context, image) {
  1648. if (image._objectURL) {
  1649. return image._objectURL;
  1650. }
  1651. var promise;
  1652. if (image.uri) {
  1653. promise = this._loadUriAsync(context, image.uri);
  1654. }
  1655. else {
  1656. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, image.bufferView);
  1657. promise = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView);
  1658. }
  1659. image._objectURL = promise.then(function (data) {
  1660. return URL.createObjectURL(new Blob([data], { type: image.mimeType }));
  1661. });
  1662. return image._objectURL;
  1663. };
  1664. GLTFLoader.prototype._loadUriAsync = function (context, uri) {
  1665. var _this = this;
  1666. var promise = GLTF2.GLTFLoaderExtension._LoadUriAsync(this, context, uri);
  1667. if (promise) {
  1668. return promise;
  1669. }
  1670. if (!GLTFLoader._ValidateUri(uri)) {
  1671. throw new Error(context + ": Uri '" + uri + "' is invalid");
  1672. }
  1673. if (BABYLON.Tools.IsBase64(uri)) {
  1674. return Promise.resolve(new Uint8Array(BABYLON.Tools.DecodeBase64(uri)));
  1675. }
  1676. return new Promise(function (resolve, reject) {
  1677. var request = BABYLON.Tools.LoadFile(_this._rootUrl + uri, function (data) {
  1678. if (!_this._disposed) {
  1679. resolve(new Uint8Array(data));
  1680. }
  1681. }, function (event) {
  1682. if (!_this._disposed) {
  1683. try {
  1684. if (request && _this._state === BABYLON.GLTFLoaderState.Loading) {
  1685. request._lengthComputable = event.lengthComputable;
  1686. request._loaded = event.loaded;
  1687. request._total = event.total;
  1688. _this._onProgress();
  1689. }
  1690. }
  1691. catch (e) {
  1692. reject(e);
  1693. }
  1694. }
  1695. }, _this._babylonScene.database, true, function (request, exception) {
  1696. if (!_this._disposed) {
  1697. reject(new BABYLON.LoadFileError(context + ": Failed to load '" + uri + "'" + (request ? ": " + request.status + " " + request.statusText : ""), request));
  1698. }
  1699. });
  1700. _this._requests.push(request);
  1701. });
  1702. };
  1703. GLTFLoader.prototype._onProgress = function () {
  1704. if (!this._progressCallback) {
  1705. return;
  1706. }
  1707. var lengthComputable = true;
  1708. var loaded = 0;
  1709. var total = 0;
  1710. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  1711. var request = _a[_i];
  1712. if (request._lengthComputable === undefined || request._loaded === undefined || request._total === undefined) {
  1713. return;
  1714. }
  1715. lengthComputable = lengthComputable && request._lengthComputable;
  1716. loaded += request._loaded;
  1717. total += request._total;
  1718. }
  1719. this._progressCallback(new BABYLON.SceneLoaderProgressEvent(lengthComputable, loaded, lengthComputable ? total : 0));
  1720. };
  1721. GLTFLoader._GetProperty = function (context, array, index) {
  1722. if (!array || index == undefined || !array[index]) {
  1723. throw new Error(context + ": Failed to find index (" + index + ")");
  1724. }
  1725. return array[index];
  1726. };
  1727. GLTFLoader._GetTextureWrapMode = function (context, mode) {
  1728. // Set defaults if undefined
  1729. mode = mode == undefined ? 10497 /* REPEAT */ : mode;
  1730. switch (mode) {
  1731. case 33071 /* CLAMP_TO_EDGE */: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  1732. case 33648 /* MIRRORED_REPEAT */: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  1733. case 10497 /* REPEAT */: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1734. default:
  1735. BABYLON.Tools.Warn(context + ": Invalid texture wrap mode (" + mode + ")");
  1736. return BABYLON.Texture.WRAP_ADDRESSMODE;
  1737. }
  1738. };
  1739. GLTFLoader._GetTextureSamplingMode = function (context, magFilter, minFilter) {
  1740. // Set defaults if undefined
  1741. magFilter = magFilter == undefined ? 9729 /* LINEAR */ : magFilter;
  1742. minFilter = minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : minFilter;
  1743. if (magFilter === 9729 /* LINEAR */) {
  1744. switch (minFilter) {
  1745. case 9728 /* NEAREST */: return BABYLON.Texture.LINEAR_NEAREST;
  1746. case 9729 /* LINEAR */: return BABYLON.Texture.LINEAR_LINEAR;
  1747. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_NEAREST_MIPNEAREST;
  1748. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_LINEAR_MIPNEAREST;
  1749. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_NEAREST_MIPLINEAR;
  1750. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  1751. default:
  1752. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  1753. return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  1754. }
  1755. }
  1756. else {
  1757. if (magFilter !== 9728 /* NEAREST */) {
  1758. BABYLON.Tools.Warn(context + ": Invalid texture magnification filter (" + magFilter + ")");
  1759. }
  1760. switch (minFilter) {
  1761. case 9728 /* NEAREST */: return BABYLON.Texture.NEAREST_NEAREST;
  1762. case 9729 /* LINEAR */: return BABYLON.Texture.NEAREST_LINEAR;
  1763. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  1764. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_LINEAR_MIPNEAREST;
  1765. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_NEAREST_MIPLINEAR;
  1766. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_LINEAR_MIPLINEAR;
  1767. default:
  1768. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  1769. return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  1770. }
  1771. }
  1772. };
  1773. GLTFLoader._GetNumComponents = function (context, type) {
  1774. switch (type) {
  1775. case "SCALAR": return 1;
  1776. case "VEC2": return 2;
  1777. case "VEC3": return 3;
  1778. case "VEC4": return 4;
  1779. case "MAT2": return 4;
  1780. case "MAT3": return 9;
  1781. case "MAT4": return 16;
  1782. }
  1783. throw new Error(context + ": Invalid type (" + type + ")");
  1784. };
  1785. GLTFLoader._ValidateUri = function (uri) {
  1786. return (BABYLON.Tools.IsBase64(uri) || uri.indexOf("..") === -1);
  1787. };
  1788. GLTFLoader._GetDrawMode = function (context, mode) {
  1789. if (mode == undefined) {
  1790. mode = 4 /* TRIANGLES */;
  1791. }
  1792. switch (mode) {
  1793. case 0 /* POINTS */: return BABYLON.Material.PointListDrawMode;
  1794. case 1 /* LINES */: return BABYLON.Material.LineListDrawMode;
  1795. case 2 /* LINE_LOOP */: return BABYLON.Material.LineLoopDrawMode;
  1796. case 3 /* LINE_STRIP */: return BABYLON.Material.LineStripDrawMode;
  1797. case 4 /* TRIANGLES */: return BABYLON.Material.TriangleFillMode;
  1798. case 5 /* TRIANGLE_STRIP */: return BABYLON.Material.TriangleStripDrawMode;
  1799. case 6 /* TRIANGLE_FAN */: return BABYLON.Material.TriangleFanDrawMode;
  1800. }
  1801. throw new Error(context + ": Invalid mesh primitive mode (" + mode + ")");
  1802. };
  1803. GLTFLoader.prototype._compileMaterialsAsync = function () {
  1804. var promises = new Array();
  1805. if (this._gltf.materials) {
  1806. for (var _i = 0, _a = this._gltf.materials; _i < _a.length; _i++) {
  1807. var material = _a[_i];
  1808. if (material._babylonData) {
  1809. for (var babylonDrawMode in material._babylonData) {
  1810. var babylonData = material._babylonData[babylonDrawMode];
  1811. for (var _b = 0, _c = babylonData.meshes; _b < _c.length; _b++) {
  1812. var babylonMesh = _c[_b];
  1813. // Ensure nonUniformScaling is set if necessary.
  1814. babylonMesh.computeWorldMatrix(true);
  1815. var babylonMaterial = babylonData.material;
  1816. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
  1817. if (this.useClipPlane) {
  1818. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
  1819. }
  1820. }
  1821. }
  1822. }
  1823. }
  1824. }
  1825. return Promise.all(promises).then(function () { });
  1826. };
  1827. GLTFLoader.prototype._compileShadowGeneratorsAsync = function () {
  1828. var promises = new Array();
  1829. var lights = this._babylonScene.lights;
  1830. for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
  1831. var light = lights_1[_i];
  1832. var generator = light.getShadowGenerator();
  1833. if (generator) {
  1834. promises.push(generator.forceCompilationAsync());
  1835. }
  1836. }
  1837. return Promise.all(promises).then(function () { });
  1838. };
  1839. GLTFLoader.prototype._clear = function () {
  1840. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  1841. var request = _a[_i];
  1842. request.abort();
  1843. }
  1844. this._requests.length = 0;
  1845. if (this._gltf && this._gltf.images) {
  1846. for (var _b = 0, _c = this._gltf.images; _b < _c.length; _b++) {
  1847. var image = _c[_b];
  1848. if (image._objectURL) {
  1849. image._objectURL.then(function (value) {
  1850. URL.revokeObjectURL(value);
  1851. });
  1852. image._objectURL = undefined;
  1853. }
  1854. }
  1855. }
  1856. delete this._gltf;
  1857. delete this._babylonScene;
  1858. this._completePromises.length = 0;
  1859. for (var name_4 in this._extensions) {
  1860. this._extensions[name_4].dispose();
  1861. }
  1862. this._extensions = {};
  1863. delete this._rootBabylonMesh;
  1864. delete this._progressCallback;
  1865. this.onMeshLoadedObservable.clear();
  1866. this.onTextureLoadedObservable.clear();
  1867. this.onMaterialLoadedObservable.clear();
  1868. };
  1869. GLTFLoader.prototype._applyExtensions = function (actionAsync) {
  1870. for (var _i = 0, _a = GLTFLoader._Names; _i < _a.length; _i++) {
  1871. var name_5 = _a[_i];
  1872. var extension = this._extensions[name_5];
  1873. if (extension.enabled) {
  1874. var promise = actionAsync(extension);
  1875. if (promise) {
  1876. return promise;
  1877. }
  1878. }
  1879. }
  1880. return null;
  1881. };
  1882. GLTFLoader._Names = new Array();
  1883. GLTFLoader._Factories = {};
  1884. return GLTFLoader;
  1885. }());
  1886. GLTF2.GLTFLoader = GLTFLoader;
  1887. BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = function () { return new GLTFLoader(); };
  1888. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1889. })(BABYLON || (BABYLON = {}));
  1890. //# sourceMappingURL=babylon.glTFLoader.js.map
  1891. "use strict";
  1892. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  1893. var BABYLON;
  1894. (function (BABYLON) {
  1895. var GLTF2;
  1896. (function (GLTF2) {
  1897. var GLTFLoaderExtension = /** @class */ (function () {
  1898. function GLTFLoaderExtension(loader) {
  1899. this.enabled = true;
  1900. this._loader = loader;
  1901. }
  1902. GLTFLoaderExtension.prototype.dispose = function () {
  1903. delete this._loader;
  1904. };
  1905. // #region Overridable Methods
  1906. /** Override this method to modify the default behavior for loading scenes. */
  1907. GLTFLoaderExtension.prototype._loadSceneAsync = function (context, node) { return null; };
  1908. /** Override this method to modify the default behavior for loading nodes. */
  1909. GLTFLoaderExtension.prototype._loadNodeAsync = function (context, node) { return null; };
  1910. /** Override this method to modify the default behavior for loading mesh primitive vertex data. */
  1911. GLTFLoaderExtension.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) { return null; };
  1912. /** Override this method to modify the default behavior for loading materials. */
  1913. GLTFLoaderExtension.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) { return null; };
  1914. /** Override this method to modify the default behavior for loading uris. */
  1915. GLTFLoaderExtension.prototype._loadUriAsync = function (context, uri) { return null; };
  1916. // #endregion
  1917. /** Helper method called by a loader extension to load an glTF extension. */
  1918. GLTFLoaderExtension.prototype._loadExtensionAsync = function (context, property, actionAsync) {
  1919. if (!property.extensions) {
  1920. return null;
  1921. }
  1922. var extensions = property.extensions;
  1923. var extension = extensions[this.name];
  1924. if (!extension) {
  1925. return null;
  1926. }
  1927. // Clear out the extension before executing the action to avoid recursing into the same property.
  1928. delete extensions[this.name];
  1929. try {
  1930. return actionAsync(context + "/extensions/" + this.name, extension);
  1931. }
  1932. finally {
  1933. // Restore the extension after executing the action.
  1934. extensions[this.name] = extension;
  1935. }
  1936. };
  1937. /** Helper method called by the loader to allow extensions to override loading scenes. */
  1938. GLTFLoaderExtension._LoadSceneAsync = function (loader, context, scene) {
  1939. return loader._applyExtensions(function (extension) { return extension._loadSceneAsync(context, scene); });
  1940. };
  1941. /** Helper method called by the loader to allow extensions to override loading nodes. */
  1942. GLTFLoaderExtension._LoadNodeAsync = function (loader, context, node) {
  1943. return loader._applyExtensions(function (extension) { return extension._loadNodeAsync(context, node); });
  1944. };
  1945. /** Helper method called by the loader to allow extensions to override loading mesh primitive vertex data. */
  1946. GLTFLoaderExtension._LoadVertexDataAsync = function (loader, context, primitive, babylonMesh) {
  1947. return loader._applyExtensions(function (extension) { return extension._loadVertexDataAsync(context, primitive, babylonMesh); });
  1948. };
  1949. /** Helper method called by the loader to allow extensions to override loading materials. */
  1950. GLTFLoaderExtension._LoadMaterialAsync = function (loader, context, material, babylonMesh, babylonDrawMode, assign) {
  1951. return loader._applyExtensions(function (extension) { return extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
  1952. };
  1953. /** Helper method called by the loader to allow extensions to override loading uris. */
  1954. GLTFLoaderExtension._LoadUriAsync = function (loader, context, uri) {
  1955. return loader._applyExtensions(function (extension) { return extension._loadUriAsync(context, uri); });
  1956. };
  1957. return GLTFLoaderExtension;
  1958. }());
  1959. GLTF2.GLTFLoaderExtension = GLTFLoaderExtension;
  1960. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1961. })(BABYLON || (BABYLON = {}));
  1962. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  1963. "use strict";
  1964. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  1965. var __extends = (this && this.__extends) || (function () {
  1966. var extendStatics = Object.setPrototypeOf ||
  1967. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  1968. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  1969. return function (d, b) {
  1970. extendStatics(d, b);
  1971. function __() { this.constructor = d; }
  1972. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  1973. };
  1974. })();
  1975. var BABYLON;
  1976. (function (BABYLON) {
  1977. var GLTF2;
  1978. (function (GLTF2) {
  1979. var Extensions;
  1980. (function (Extensions) {
  1981. // https://github.com/sbtron/glTF/tree/MSFT_lod/extensions/Vendor/MSFT_lod
  1982. var NAME = "MSFT_lod";
  1983. var MSFT_lod = /** @class */ (function (_super) {
  1984. __extends(MSFT_lod, _super);
  1985. function MSFT_lod() {
  1986. var _this = _super !== null && _super.apply(this, arguments) || this;
  1987. _this.name = NAME;
  1988. /**
  1989. * Maximum number of LODs to load, starting from the lowest LOD.
  1990. */
  1991. _this.maxLODsToLoad = Number.MAX_VALUE;
  1992. _this._loadingNodeLOD = null;
  1993. _this._loadNodeSignals = {};
  1994. _this._loadingMaterialLOD = null;
  1995. _this._loadMaterialSignals = {};
  1996. return _this;
  1997. }
  1998. MSFT_lod.prototype._loadNodeAsync = function (context, node) {
  1999. var _this = this;
  2000. return this._loadExtensionAsync(context, node, function (context, extension) {
  2001. var firstPromise;
  2002. var nodeLODs = _this._getLODs(context, node, _this._loader._gltf.nodes, extension.ids);
  2003. var _loop_1 = function (indexLOD) {
  2004. var nodeLOD = nodeLODs[indexLOD];
  2005. if (indexLOD !== 0) {
  2006. _this._loadingNodeLOD = nodeLOD;
  2007. if (!_this._loadNodeSignals[nodeLOD._index]) {
  2008. _this._loadNodeSignals[nodeLOD._index] = new BABYLON.Deferred();
  2009. }
  2010. }
  2011. var promise = _this._loader._loadNodeAsync("#/nodes/" + nodeLOD._index, nodeLOD).then(function () {
  2012. if (indexLOD !== 0) {
  2013. var previousNodeLOD = nodeLODs[indexLOD - 1];
  2014. if (previousNodeLOD._babylonMesh) {
  2015. previousNodeLOD._babylonMesh.dispose(false, true);
  2016. delete previousNodeLOD._babylonMesh;
  2017. }
  2018. }
  2019. if (indexLOD !== nodeLODs.length - 1) {
  2020. var nodeIndex = nodeLODs[indexLOD + 1]._index;
  2021. if (_this._loadNodeSignals[nodeIndex]) {
  2022. _this._loadNodeSignals[nodeIndex].resolve();
  2023. delete _this._loadNodeSignals[nodeIndex];
  2024. }
  2025. }
  2026. });
  2027. if (indexLOD === 0) {
  2028. firstPromise = promise;
  2029. }
  2030. else {
  2031. _this._loader._completePromises.push(promise);
  2032. _this._loadingNodeLOD = null;
  2033. }
  2034. };
  2035. for (var indexLOD = 0; indexLOD < nodeLODs.length; indexLOD++) {
  2036. _loop_1(indexLOD);
  2037. }
  2038. return firstPromise;
  2039. });
  2040. };
  2041. MSFT_lod.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2042. var _this = this;
  2043. // Don't load material LODs if already loading a node LOD.
  2044. if (this._loadingNodeLOD) {
  2045. return null;
  2046. }
  2047. return this._loadExtensionAsync(context, material, function (context, extension) {
  2048. var firstPromise;
  2049. var materialLODs = _this._getLODs(context, material, _this._loader._gltf.materials, extension.ids);
  2050. var _loop_2 = function (indexLOD) {
  2051. var materialLOD = materialLODs[indexLOD];
  2052. if (indexLOD !== 0) {
  2053. _this._loadingMaterialLOD = materialLOD;
  2054. if (!_this._loadMaterialSignals[materialLOD._index]) {
  2055. _this._loadMaterialSignals[materialLOD._index] = new BABYLON.Deferred();
  2056. }
  2057. }
  2058. var promise = _this._loader._loadMaterialAsync("#/materials/" + materialLOD._index, materialLOD, babylonMesh, babylonDrawMode, indexLOD === 0 ? assign : function () { }).then(function () {
  2059. if (indexLOD !== 0) {
  2060. var babylonDataLOD = materialLOD._babylonData;
  2061. assign(babylonDataLOD[babylonDrawMode].material);
  2062. var previousBabylonDataLOD = materialLODs[indexLOD - 1]._babylonData;
  2063. if (previousBabylonDataLOD[babylonDrawMode]) {
  2064. previousBabylonDataLOD[babylonDrawMode].material.dispose();
  2065. delete previousBabylonDataLOD[babylonDrawMode];
  2066. }
  2067. }
  2068. if (indexLOD !== materialLODs.length - 1) {
  2069. var materialIndex = materialLODs[indexLOD + 1]._index;
  2070. if (_this._loadMaterialSignals[materialIndex]) {
  2071. _this._loadMaterialSignals[materialIndex].resolve();
  2072. delete _this._loadMaterialSignals[materialIndex];
  2073. }
  2074. }
  2075. });
  2076. if (indexLOD === 0) {
  2077. firstPromise = promise;
  2078. }
  2079. else {
  2080. _this._loader._completePromises.push(promise);
  2081. _this._loadingMaterialLOD = null;
  2082. }
  2083. };
  2084. for (var indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  2085. _loop_2(indexLOD);
  2086. }
  2087. return firstPromise;
  2088. });
  2089. };
  2090. MSFT_lod.prototype._loadUriAsync = function (context, uri) {
  2091. var _this = this;
  2092. // Defer the loading of uris if loading a material or node LOD.
  2093. if (this._loadingMaterialLOD) {
  2094. var index = this._loadingMaterialLOD._index;
  2095. return this._loadMaterialSignals[index].promise.then(function () {
  2096. return _this._loader._loadUriAsync(context, uri);
  2097. });
  2098. }
  2099. else if (this._loadingNodeLOD) {
  2100. var index = this._loadingNodeLOD._index;
  2101. return this._loadNodeSignals[index].promise.then(function () {
  2102. return _this._loader._loadUriAsync(context, uri);
  2103. });
  2104. }
  2105. return null;
  2106. };
  2107. /**
  2108. * Gets an array of LOD properties from lowest to highest.
  2109. */
  2110. MSFT_lod.prototype._getLODs = function (context, property, array, ids) {
  2111. if (this.maxLODsToLoad <= 0) {
  2112. throw new Error("maxLODsToLoad must be greater than zero");
  2113. }
  2114. var properties = new Array();
  2115. for (var i = ids.length - 1; i >= 0; i--) {
  2116. properties.push(GLTF2.GLTFLoader._GetProperty(context + "/ids/" + ids[i], array, ids[i]));
  2117. if (properties.length === this.maxLODsToLoad) {
  2118. return properties;
  2119. }
  2120. }
  2121. properties.push(property);
  2122. return properties;
  2123. };
  2124. return MSFT_lod;
  2125. }(GLTF2.GLTFLoaderExtension));
  2126. Extensions.MSFT_lod = MSFT_lod;
  2127. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_lod(loader); });
  2128. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2129. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2130. })(BABYLON || (BABYLON = {}));
  2131. //# sourceMappingURL=MSFT_lod.js.map
  2132. "use strict";
  2133. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2134. var __extends = (this && this.__extends) || (function () {
  2135. var extendStatics = Object.setPrototypeOf ||
  2136. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2137. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2138. return function (d, b) {
  2139. extendStatics(d, b);
  2140. function __() { this.constructor = d; }
  2141. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2142. };
  2143. })();
  2144. var BABYLON;
  2145. (function (BABYLON) {
  2146. var GLTF2;
  2147. (function (GLTF2) {
  2148. var Extensions;
  2149. (function (Extensions) {
  2150. // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression
  2151. var NAME = "KHR_draco_mesh_compression";
  2152. var KHR_draco_mesh_compression = /** @class */ (function (_super) {
  2153. __extends(KHR_draco_mesh_compression, _super);
  2154. function KHR_draco_mesh_compression(loader) {
  2155. var _this = _super.call(this, loader) || this;
  2156. _this.name = NAME;
  2157. _this._dracoCompression = null;
  2158. // Disable extension if decoder is not available.
  2159. if (!BABYLON.DracoCompression.DecoderUrl) {
  2160. _this.enabled = false;
  2161. }
  2162. return _this;
  2163. }
  2164. KHR_draco_mesh_compression.prototype.dispose = function () {
  2165. if (this._dracoCompression) {
  2166. this._dracoCompression.dispose();
  2167. }
  2168. _super.prototype.dispose.call(this);
  2169. };
  2170. KHR_draco_mesh_compression.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  2171. var _this = this;
  2172. return this._loadExtensionAsync(context, primitive, function (extensionContext, extension) {
  2173. if (primitive.mode != undefined) {
  2174. if (primitive.mode !== 5 /* TRIANGLE_STRIP */ &&
  2175. primitive.mode !== 4 /* TRIANGLES */) {
  2176. throw new Error(context + ": Unsupported mode " + primitive.mode);
  2177. }
  2178. // TODO: handle triangle strips
  2179. if (primitive.mode === 5 /* TRIANGLE_STRIP */) {
  2180. throw new Error(context + ": Mode " + primitive.mode + " is not currently supported");
  2181. }
  2182. }
  2183. var attributes = {};
  2184. var loadAttribute = function (name, kind) {
  2185. var uniqueId = extension.attributes[name];
  2186. if (uniqueId == undefined) {
  2187. return;
  2188. }
  2189. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  2190. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  2191. babylonMesh._delayInfo.push(kind);
  2192. }
  2193. attributes[kind] = uniqueId;
  2194. };
  2195. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  2196. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  2197. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  2198. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  2199. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  2200. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  2201. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  2202. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind);
  2203. var bufferView = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._loader._gltf.bufferViews, extension.bufferView);
  2204. return _this._loader._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  2205. try {
  2206. if (!_this._dracoCompression) {
  2207. _this._dracoCompression = new BABYLON.DracoCompression();
  2208. }
  2209. return _this._dracoCompression.decodeMeshAsync(data, attributes);
  2210. }
  2211. catch (e) {
  2212. throw new Error(context + ": " + e.message);
  2213. }
  2214. });
  2215. });
  2216. };
  2217. return KHR_draco_mesh_compression;
  2218. }(GLTF2.GLTFLoaderExtension));
  2219. Extensions.KHR_draco_mesh_compression = KHR_draco_mesh_compression;
  2220. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_draco_mesh_compression(loader); });
  2221. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2222. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2223. })(BABYLON || (BABYLON = {}));
  2224. //# sourceMappingURL=KHR_draco_mesh_compression.js.map
  2225. "use strict";
  2226. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2227. var __extends = (this && this.__extends) || (function () {
  2228. var extendStatics = Object.setPrototypeOf ||
  2229. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2230. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2231. return function (d, b) {
  2232. extendStatics(d, b);
  2233. function __() { this.constructor = d; }
  2234. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2235. };
  2236. })();
  2237. var BABYLON;
  2238. (function (BABYLON) {
  2239. var GLTF2;
  2240. (function (GLTF2) {
  2241. var Extensions;
  2242. (function (Extensions) {
  2243. // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness
  2244. var NAME = "KHR_materials_pbrSpecularGlossiness";
  2245. var KHR_materials_pbrSpecularGlossiness = /** @class */ (function (_super) {
  2246. __extends(KHR_materials_pbrSpecularGlossiness, _super);
  2247. function KHR_materials_pbrSpecularGlossiness() {
  2248. var _this = _super !== null && _super.apply(this, arguments) || this;
  2249. _this.name = NAME;
  2250. return _this;
  2251. }
  2252. KHR_materials_pbrSpecularGlossiness.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2253. var _this = this;
  2254. return this._loadExtensionAsync(context, material, function (context, extension) {
  2255. material._babylonData = material._babylonData || {};
  2256. var babylonData = material._babylonData[babylonDrawMode];
  2257. if (!babylonData) {
  2258. var promises = new Array();
  2259. var name_1 = material.name || "materialSG_" + material._index;
  2260. var babylonMaterial = _this._loader._createMaterial(BABYLON.PBRMaterial, name_1, babylonDrawMode);
  2261. promises.push(_this._loader._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  2262. promises.push(_this._loadSpecularGlossinessPropertiesAsync(context, material, extension, babylonMaterial));
  2263. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  2264. babylonData = {
  2265. material: babylonMaterial,
  2266. meshes: [],
  2267. loaded: Promise.all(promises).then(function () { })
  2268. };
  2269. material._babylonData[babylonDrawMode] = babylonData;
  2270. }
  2271. babylonData.meshes.push(babylonMesh);
  2272. assign(babylonData.material);
  2273. return babylonData.loaded;
  2274. });
  2275. };
  2276. KHR_materials_pbrSpecularGlossiness.prototype._loadSpecularGlossinessPropertiesAsync = function (context, material, properties, babylonMaterial) {
  2277. var promises = new Array();
  2278. if (properties.diffuseFactor) {
  2279. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.diffuseFactor);
  2280. babylonMaterial.alpha = properties.diffuseFactor[3];
  2281. }
  2282. else {
  2283. babylonMaterial.albedoColor = BABYLON.Color3.White();
  2284. }
  2285. babylonMaterial.reflectivityColor = properties.specularFactor ? BABYLON.Color3.FromArray(properties.specularFactor) : BABYLON.Color3.White();
  2286. babylonMaterial.microSurface = properties.glossinessFactor == undefined ? 1 : properties.glossinessFactor;
  2287. if (properties.diffuseTexture) {
  2288. promises.push(this._loader._loadTextureAsync(context + "/diffuseTexture", properties.diffuseTexture, function (texture) {
  2289. babylonMaterial.albedoTexture = texture;
  2290. }));
  2291. }
  2292. if (properties.specularGlossinessTexture) {
  2293. promises.push(this._loader._loadTextureAsync(context + "/specularGlossinessTexture", properties.specularGlossinessTexture, function (texture) {
  2294. babylonMaterial.reflectivityTexture = texture;
  2295. }));
  2296. babylonMaterial.reflectivityTexture.hasAlpha = true;
  2297. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  2298. }
  2299. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  2300. return Promise.all(promises).then(function () { });
  2301. };
  2302. return KHR_materials_pbrSpecularGlossiness;
  2303. }(GLTF2.GLTFLoaderExtension));
  2304. Extensions.KHR_materials_pbrSpecularGlossiness = KHR_materials_pbrSpecularGlossiness;
  2305. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_pbrSpecularGlossiness(loader); });
  2306. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2307. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2308. })(BABYLON || (BABYLON = {}));
  2309. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map
  2310. "use strict";
  2311. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2312. var __extends = (this && this.__extends) || (function () {
  2313. var extendStatics = Object.setPrototypeOf ||
  2314. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2315. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2316. return function (d, b) {
  2317. extendStatics(d, b);
  2318. function __() { this.constructor = d; }
  2319. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2320. };
  2321. })();
  2322. var BABYLON;
  2323. (function (BABYLON) {
  2324. var GLTF2;
  2325. (function (GLTF2) {
  2326. var Extensions;
  2327. (function (Extensions) {
  2328. // https://github.com/MiiBond/glTF/tree/khr_lights_v1/extensions/Khronos/KHR_lights
  2329. var NAME = "KHR_lights";
  2330. var LightType;
  2331. (function (LightType) {
  2332. LightType["AMBIENT"] = "ambient";
  2333. LightType["DIRECTIONAL"] = "directional";
  2334. LightType["POINT"] = "point";
  2335. LightType["SPOT"] = "spot";
  2336. })(LightType || (LightType = {}));
  2337. var KHR_lights = /** @class */ (function (_super) {
  2338. __extends(KHR_lights, _super);
  2339. function KHR_lights() {
  2340. var _this = _super !== null && _super.apply(this, arguments) || this;
  2341. _this.name = NAME;
  2342. return _this;
  2343. }
  2344. KHR_lights.prototype._loadSceneAsync = function (context, scene) {
  2345. var _this = this;
  2346. return this._loadExtensionAsync(context, scene, function (context, extension) {
  2347. var promise = _this._loader._loadSceneAsync(context, scene);
  2348. var light = GLTF2.GLTFLoader._GetProperty(context, _this._lights, extension.light);
  2349. if (light.type !== LightType.AMBIENT) {
  2350. throw new Error(context + ": Only ambient lights are allowed on a scene");
  2351. }
  2352. _this._loader._babylonScene.ambientColor = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.Black();
  2353. return promise;
  2354. });
  2355. };
  2356. KHR_lights.prototype._loadNodeAsync = function (context, node) {
  2357. var _this = this;
  2358. return this._loadExtensionAsync(context, node, function (context, extension) {
  2359. var promise = _this._loader._loadNodeAsync(context, node);
  2360. var babylonLight;
  2361. var light = GLTF2.GLTFLoader._GetProperty(context, _this._lights, extension.light);
  2362. var name = node._babylonMesh.name;
  2363. switch (light.type) {
  2364. case LightType.AMBIENT: {
  2365. throw new Error(context + ": Ambient lights are not allowed on a node");
  2366. }
  2367. case LightType.DIRECTIONAL: {
  2368. babylonLight = new BABYLON.DirectionalLight(name, BABYLON.Vector3.Forward(), _this._loader._babylonScene);
  2369. break;
  2370. }
  2371. case LightType.POINT: {
  2372. babylonLight = new BABYLON.PointLight(name, BABYLON.Vector3.Zero(), _this._loader._babylonScene);
  2373. break;
  2374. }
  2375. case LightType.SPOT: {
  2376. var spotLight = light;
  2377. // TODO: support inner and outer cone angles
  2378. //const innerConeAngle = spotLight.innerConeAngle || 0;
  2379. var outerConeAngle = spotLight.outerConeAngle || Math.PI / 4;
  2380. babylonLight = new BABYLON.SpotLight(name, BABYLON.Vector3.Zero(), BABYLON.Vector3.Forward(), outerConeAngle, 2, _this._loader._babylonScene);
  2381. break;
  2382. }
  2383. default: {
  2384. throw new Error(context + ": Invalid light type (" + light.type + ")");
  2385. }
  2386. }
  2387. babylonLight.diffuse = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.White();
  2388. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  2389. babylonLight.parent = node._babylonMesh;
  2390. return promise;
  2391. });
  2392. };
  2393. Object.defineProperty(KHR_lights.prototype, "_lights", {
  2394. get: function () {
  2395. var extensions = this._loader._gltf.extensions;
  2396. if (!extensions || !extensions[this.name]) {
  2397. throw new Error("#/extensions: '" + this.name + "' not found");
  2398. }
  2399. var extension = extensions[this.name];
  2400. return extension.lights;
  2401. },
  2402. enumerable: true,
  2403. configurable: true
  2404. });
  2405. return KHR_lights;
  2406. }(GLTF2.GLTFLoaderExtension));
  2407. Extensions.KHR_lights = KHR_lights;
  2408. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_lights(loader); });
  2409. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2410. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2411. })(BABYLON || (BABYLON = {}));
  2412. //# sourceMappingURL=KHR_lights.js.map